[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[COMMITTED 6/7] poked: add Poke disasm pokelet
From: |
Mohammad-Reza Nabipoor |
Subject: |
[COMMITTED 6/7] poked: add Poke disasm pokelet |
Date: |
Sun, 4 Dec 2022 23:05:01 +0100 |
2022-12-04 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* poked/poked.c (PDISAS_ITER_BEGIN): New macro.
(PDISAS_ITER_END): Likewise.
(PDISAS_ERR): Likewise.
(PDISAS_TXT): Likewise.
(PDISAS_KIND): Likewise.
(termout_disas): New function.
(poked_disasm_send): Likewise.
(iteration_send): Add new param (`chan').
(iteration_begin): Likewise.
(iteration_end): Likewise.
(poked_compile): Add support for Poke disasm.
(main): Update `iteration_begin' and `iteration_end'
invocation.
* poked/poked.pk (PLET_DISAS_KIND_FUNC): New const.
(PLET_DISAS_KIND_FUNC_NATIVE): Likewise.
(PLET_DISAS_KIND_EXPR): Likewise.
(PLET_DISAS_KIND_EXPR_NATIVE): Likewise.
(__Poked_Disas): New type.
(__poked_disas_data): New variable.
(__poked_disas_p): Likewise.
(__poked_disas_reset): New function.
(plet_disas_func): Likewise.
(plet_disas_expr): Likewise.
---
ChangeLog | 26 ++++++++++++++
poked/poked.c | 98 +++++++++++++++++++++++++++++++++++++++++++++-----
poked/poked.pk | 43 ++++++++++++++++++++++
3 files changed, 158 insertions(+), 9 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 8c2facc5..a6be7f23 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,29 @@
+2022-12-04 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
+
+ * poked/poked.c (PDISAS_ITER_BEGIN): New macro.
+ (PDISAS_ITER_END): Likewise.
+ (PDISAS_ERR): Likewise.
+ (PDISAS_TXT): Likewise.
+ (PDISAS_KIND): Likewise.
+ (termout_disas): New function.
+ (poked_disasm_send): Likewise.
+ (iteration_send): Add new param (`chan').
+ (iteration_begin): Likewise.
+ (iteration_end): Likewise.
+ (poked_compile): Add support for Poke disasm.
+ (main): Update `iteration_begin' and `iteration_end'
+ invocation.
+ * poked/poked.pk (PLET_DISAS_KIND_FUNC): New const.
+ (PLET_DISAS_KIND_FUNC_NATIVE): Likewise.
+ (PLET_DISAS_KIND_EXPR): Likewise.
+ (PLET_DISAS_KIND_EXPR_NATIVE): Likewise.
+ (__Poked_Disas): New type.
+ (__poked_disas_data): New variable.
+ (__poked_disas_p): Likewise.
+ (__poked_disas_reset): New function.
+ (plet_disas_func): Likewise.
+ (plet_disas_expr): Likewise.
+
2022-12-04 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* poked/poked.c (OUTCMD_TXT): Change the value.
diff --git a/poked/poked.c b/poked/poked.c
index 48e55952..5c316750 100644
--- a/poked/poked.c
+++ b/poked/poked.c
@@ -57,6 +57,13 @@ static void poked_free (void);
#define AUTOCMPL_IDENT 1
#define AUTOCMPL_IOS 2
+/* Poke disassmbler */
+#define PDISAS_ITER_BEGIN OUTCMD_ITER_BEGIN
+#define PDISAS_ITER_END OUTCMD_ITER_END
+#define PDISAS_ERR OUTCMD_ERR
+#define PDISAS_TXT OUTCMD_TXT
+#define PDISAS_KIND 5
+
static uint8_t termout_chan = USOCK_CHAN_OUT_OUT;
static uint32_t termout_cmdkind = OUTCMD_TXT;
@@ -81,6 +88,13 @@ termout_eval (void)
termout_cmdkind = OUTCMD_EVAL;
}
+static void
+termout_disas (void)
+{
+ termout_chan = USOCK_CHAN_OUT_PDISAS;
+ termout_cmdkind = PDISAS_TXT;
+}
+
//---
static void *
@@ -135,7 +149,8 @@ poked_buf_send (void)
//---
static void
-iteration_send (struct usock *srv, uint64_t n_iteration, int begin_p)
+iteration_send (struct usock *srv, uint8_t chan, uint64_t n_iteration,
+ int begin_p)
{
uint8_t buf[8] = {
#define b(i) (uint8_t) (n_iteration >> (i))
@@ -143,20 +158,20 @@ iteration_send (struct usock *srv, uint64_t n_iteration,
int begin_p)
#undef b
};
- usock_out (srv, USOCK_CHAN_OUT_OUT,
- begin_p ? OUTCMD_ITER_BEGIN : OUTCMD_ITER_END, buf, sizeof (buf));
+ usock_out (srv, chan, begin_p ? OUTCMD_ITER_BEGIN : OUTCMD_ITER_END, buf,
+ sizeof (buf));
}
static void
-iteration_begin (struct usock *srv, uint64_t n_iteration)
+iteration_begin (struct usock *srv, uint8_t chan, uint64_t n_iteration)
{
- return iteration_send (srv, n_iteration, 1);
+ return iteration_send (srv, chan, n_iteration, 1);
}
static void
-iteration_end (struct usock *srv, uint64_t n_iteration)
+iteration_end (struct usock *srv, uint8_t chan, uint64_t n_iteration)
{
- return iteration_send (srv, n_iteration, 0);
+ return iteration_send (srv, chan, n_iteration, 0);
}
#define iteration_send private_
@@ -288,6 +303,69 @@ poked_autocmpl_send (void)
//---
+static void
+poked_disas_send (void)
+{
+ enum
+ {
+ /* Keep these in-sync with `poked.pk'. */
+ PLET_DISAS_KIND_FUNC = 1U,
+ PLET_DISAS_KIND_FUNC_NATIVE = 2U,
+ PLET_DISAS_KIND_EXPR = 3U,
+ PLET_DISAS_KIND_EXPR_NATIVE = 4U,
+ };
+ static uint64_t iteration;
+
+ pk_val disas_arr = pk_decl_val (pkc, "__poked_disas_data");
+ uint64_t nelem = pk_uint_value (pk_array_nelem (disas_arr));
+ pk_val exc;
+
+ for (uint64_t i = 0; i < nelem; ++i)
+ {
+ pk_val data = pk_array_elem_value (disas_arr, i);
+ uint64_t kind = pk_uint_value (pk_struct_ref_field_value (data, "kind"));
+ const char *str
+ = pk_string_str (pk_struct_ref_field_value (data, "str"));
+
+ assert (kind == PLET_DISAS_KIND_FUNC
+ || kind == PLET_DISAS_KIND_FUNC_NATIVE
+ || kind == PLET_DISAS_KIND_EXPR
+ || kind == PLET_DISAS_KIND_EXPR_NATIVE);
+
+ ++iteration;
+ iteration_begin (srv, USOCK_CHAN_OUT_PDISAS, iteration);
+
+ /* Send PDISAS_KIND message with the function_name/expression. */
+ usock_out_printf (srv, USOCK_CHAN_OUT_PDISAS, PDISAS_KIND, "%c%s",
+ (int)kind, str);
+
+ termout_disas ();
+ if (kind == PLET_DISAS_KIND_FUNC || kind == PLET_DISAS_KIND_FUNC_NATIVE)
+ {
+ int native_p = kind == PLET_DISAS_KIND_FUNC_NATIVE;
+
+ if (pk_disassemble_function (pkc, str, native_p) != PK_OK)
+ usock_out_printf (srv, USOCK_CHAN_OUT_PDISAS, PDISAS_ERR,
+ "invalid function `%s' to disassemble", str);
+ }
+ else if (kind == PLET_DISAS_KIND_EXPR
+ || kind == PLET_DISAS_KIND_EXPR_NATIVE)
+ {
+ int native_p = kind == PLET_DISAS_KIND_EXPR_NATIVE;
+
+ if (pk_disassemble_expression (pkc, str, native_p) != PK_OK)
+ usock_out_printf (srv, USOCK_CHAN_OUT_PDISAS, PDISAS_ERR,
+ "invalid expression `%s' to disassemble", str);
+ }
+ termout_restore ();
+
+ iteration_end (srv, USOCK_CHAN_OUT_PDISAS, iteration);
+ }
+ (void)pk_call (pkc, pk_decl_val (pkc, "__poked_disas_reset"), NULL, &exc, 0);
+}
+
+//---
+
static int
poked_compile (const char *src, uint8_t chan, int *poked_restart_p,
int *done_p)
@@ -349,6 +427,8 @@ poked_compile (const char *src, uint8_t chan, int
*poked_restart_p,
}
if (pk_int_value (pk_decl_val (pkc, "__poked_autocmpl_p")))
poked_autocmpl_send ();
+ if (pk_int_value (pk_decl_val (pkc, "__poked_disas_p")))
+ poked_disas_send ();
if (pk_int_value (pk_decl_val (pkc, "__vu_do_p")))
{
const char *filt = pk_string_str (pk_decl_val (pkc, "__vu_filter"));
@@ -507,9 +587,9 @@ poked_restart:
if (poked_options.debug_p)
printf ("< '%.*s'\n", (int)srclen, src);
n_iteration++;
- iteration_begin (srv, n_iteration);
+ iteration_begin (srv, USOCK_CHAN_OUT_OUT, n_iteration);
(void)poked_compile (src, chan, &poked_restart_p, &done_p);
- iteration_end (srv, n_iteration);
+ iteration_end (srv, USOCK_CHAN_OUT_OUT, n_iteration);
if (poked_restart_p)
{
usock_buf_free_chain (inbuf);
diff --git a/poked/poked.pk b/poked/poked.pk
index bc0053f6..9b0690a2 100644
--- a/poked/poked.pk
+++ b/poked/poked.pk
@@ -429,6 +429,49 @@ fun plet_autocomplete = (uint kind, string str) void:
__poked_autocmpl_p = 1;
}
+//--- poke disassembly
+
+var PLET_DISAS_KIND_FUNC = 1U,
+ PLET_DISAS_KIND_FUNC_NATIVE = 2U,
+ PLET_DISAS_KIND_EXPR = 3U,
+ PLET_DISAS_KIND_EXPR_NATIVE = 4U;
+
+type __Poked_Disas = struct
+ {
+ uint<8> kind : 0 < kind && kind <= 4U;
+ string str; /* Function name or Poke expression. */
+ };
+
+var __poked_disas_data = __Poked_Disas[] (),
+ __poked_disas_p = 0;
+fun __poked_disas_reset = void:
+ {
+ __poked_disas_data = __Poked_Disas[] ();
+ __poked_disas_p = 0;
+ }
+
+fun plet_disas_func = (string func_name, int native_p = 0) void:
+ {
+ var d = __Poked_Disas {
+ kind=native_p ? PLET_DISAS_KIND_FUNC_NATIVE : PLET_DISAS_KIND_FUNC,
+ str=func_name,
+ };
+
+ apush (__poked_disas_data, d);
+ __poked_disas_p = 1;
+ }
+
+fun plet_disas_expr = (string expr, int native_p = 0) void:
+ {
+ var d = __Poked_Disas {
+ kind=native_p ? PLET_DISAS_KIND_EXPR_NATIVE : PLET_DISAS_KIND_EXPR,
+ str=expr,
+ };
+
+ apush (__poked_disas_data, d);
+ __poked_disas_p = 1;
+ }
+
//---
print "//---\nGNU poke in daemon mode!\n";
--
2.38.1
- [COMMITTED 1/7] poked: print PDAP version and define `poked_pdap_version', Mohammad-Reza Nabipoor, 2022/12/04
- [COMMITTED 2/7] poked: move `poked_libpoke_version' def to `poked.pk', Mohammad-Reza Nabipoor, 2022/12/04
- [COMMITTED 3/7] poked: update poked output channel numbers, Mohammad-Reza Nabipoor, 2022/12/04
- [COMMITTED 4/7] poked: add new function `usock_out_printf', Mohammad-Reza Nabipoor, 2022/12/04
- [COMMITTED 5/7] poked: re-organize the OUTCMD_* kinds, Mohammad-Reza Nabipoor, 2022/12/04
- [COMMITTED 7/7] poked: plet_disasm: add assertion for valid arches, Mohammad-Reza Nabipoor, 2022/12/04
- [COMMITTED 6/7] poked: add Poke disasm pokelet,
Mohammad-Reza Nabipoor <=