From 7cd21c5f8f1a5db277b7a34be8e26fc3fa734b5b Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Fri, 28 Sep 2018 17:12:51 +0800 Subject: [PATCH] Cherry pick JIT stuff --- src/elab.c | 35 +++++++++++++++++++++++++++++++---- src/vcode.c | 25 ++++++++++++++++++------- src/vcode.h | 7 ++++++- 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/elab.c b/src/elab.c index 735c893e..c6b57ea5 100644 --- a/src/elab.c +++ b/src/elab.c @@ -2253,13 +2253,35 @@ typedef struct { e_node_t signal; } reg_data_t; -static void drv_dump_fn(int op, void *context) +static int drv_dump_fn(vcode_dump_reason_t why, int what, void *context) { reg_data_t *regs = (reg_data_t *)context; - vcode_reg_t result = vcode_get_result(op); - if (result != VCODE_INVALID_REG && regs[result].state == CONST) - printf("%"PRIi64"\n", regs[result].ival); + if (why != VCODE_DUMP_REG) + return 0; + + assert(what != VCODE_INVALID_REG); + + switch (regs[what].state) { + case NONE: + return color_printf("$!red$(!)$$"); + case CONST: + return color_printf("$!magenta$(%"PRIi64")$$", regs[what].ival); + case SIGNAL: + if (regs[what].ival != 0) + return color_printf("$!magenta$(%s+%"PRIi64")$$", + istr(e_ident(regs[what].signal)), regs[what].ival); + else + return color_printf("$!magenta$(%s)$$", + istr(e_ident(regs[what].signal))); + case UNKNOWN: + return color_printf("$!magenta$(?)$$"); + case UNKNOWN_SIGNAL: + return color_printf("$!magenta$(%s+?)$$", + istr(e_ident(regs[what].signal))); + } + + return 0; } static void drv_add(e_node_t signal, e_node_t proc, unsigned offset, @@ -2442,6 +2464,11 @@ static void drv_extract(e_node_t proc, elab_ctx_t *ctx) case VCODE_OP_RESOLVED: case VCODE_OP_LOAD_INDIRECT: case VCODE_OP_CONST_ARRAY: + { + vcode_reg_t result = vcode_get_result(op); + assert(result != VCODE_INVALID_REG); + regs[result].state = UNKNOWN; + } break; default: diff --git a/src/vcode.c b/src/vcode.c index fbc26368..5d240a46 100644 --- a/src/vcode.c +++ b/src/vcode.c @@ -239,9 +239,11 @@ struct vcode_unit { #define VCODE_VERSION 7 #define VCODE_CHECK_UNIONS 0 -static vcode_unit_t active_unit = NULL; -static vcode_block_t active_block = VCODE_INVALID_BLOCK; -static hash_t *registry = NULL; +static vcode_unit_t active_unit = NULL; +static vcode_block_t active_block = VCODE_INVALID_BLOCK; +static hash_t *registry = NULL; +static vcode_dump_fn_t dump_callback = NULL; +static void *dump_arg = NULL; static inline int64_t sadd64(int64_t a, int64_t b) { @@ -1079,10 +1081,16 @@ const char *vcode_op_string(vcode_op_t op) static int vcode_dump_reg(vcode_reg_t reg) { + int printed; if (reg == VCODE_INVALID_REG) - return color_printf("$red$invalid$$"); + printed = color_printf("$red$invalid$$"); else - return color_printf("$green$r%d$$", reg); + printed = color_printf("$green$r%d$$", reg); + + if (dump_callback != NULL) + printed += (*dump_callback)(VCODE_DUMP_REG, reg, dump_arg); + + return printed; } static int vcode_pretty_print_int(int64_t n) @@ -1248,6 +1256,9 @@ void vcode_dump_with_mark(int mark_op, vcode_dump_fn_t callback, void *arg) { assert(active_unit != NULL); + dump_callback = callback; + dump_arg = arg; + const vcode_unit_t vu = active_unit; vcode_block_t old_block = active_block; @@ -1351,7 +1362,7 @@ void vcode_dump_with_mark(int mark_op, vcode_dump_fn_t callback, void *arg) const param_t *p = &(vu->params.items[i]); int col = printf(" "); col += vcode_dump_reg(p->reg); - while (col < 8) + while (col < (dump_callback ? 12 : 8)) col += printf(" "); col += color_printf("$magenta$%s$$", istr(p->name)); vcode_dump_type(col, p->type, p->bounds); @@ -2247,7 +2258,7 @@ void vcode_dump_with_mark(int mark_op, vcode_dump_fn_t callback, void *arg) color_printf("$$\n"); if (callback != NULL) - (*callback)(j, arg); + (*callback)(VCODE_DUMP_OP, j, arg); } if (b->ops.count == 0) diff --git a/src/vcode.h b/src/vcode.h index 3e5bb45a..77ec1101 100644 --- a/src/vcode.h +++ b/src/vcode.h @@ -210,7 +210,12 @@ typedef struct { #define VCODE_INVALID_HINT 0xffffffff -typedef void (*vcode_dump_fn_t)(int, void *); +typedef enum { + VCODE_DUMP_OP, + VCODE_DUMP_REG, +} vcode_dump_reason_t; + +typedef int (*vcode_dump_fn_t)(vcode_dump_reason_t, int, void *); vcode_type_t vtype_int(int64_t low, int64_t high); vcode_type_t vtype_dynamic(vcode_reg_t low, vcode_reg_t high); -- 2.39.2