diff --git a/libCuik/lib/back/ir_gen.c b/libCuik/lib/back/ir_gen.c index 324ecd24..518431d2 100644 --- a/libCuik/lib/back/ir_gen.c +++ b/libCuik/lib/back/ir_gen.c @@ -10,6 +10,7 @@ static thread_local const char* function_name; // For aggregate returns static _Thread_local TB_Node* return_value_address; +static _Thread_local TB_Attrib* scope_attrib; static TB_Symbol* get_external(CompilationUnit* restrict cu, const char* name) { // if this is the first time we've seen this name, add it to the table @@ -2163,7 +2164,8 @@ static void irgen_stmt(TranslationUnit* tu, TB_Function* func, Stmt* restrict s) TB_Node* addr = tb_inst_local(func, size, align); if (tu->has_tb_debug_info && s->decl.name != NULL) { - tb_function_attrib_variable(func, addr, -1, s->decl.name, cuik__as_tb_debug_type(tu->ir_mod, type)); + TB_Attrib* a = tb_function_attrib_variable(func, -1, s->decl.name, cuik__as_tb_debug_type(tu->ir_mod, type)); + tb_node_append_attrib(addr, a); } if (s->decl.initial) { @@ -2467,7 +2469,8 @@ TB_Symbol* cuikcg_top_level(TranslationUnit* restrict tu, TB_Module* m, TB_Arena params[i] = tb_inst_param_addr(func, i + param_bias); if (proto->params[i].name) { - tb_function_attrib_variable(func, params[i], -1, proto->params[i].name, proto->params[i].debug_type); + TB_Attrib* a = tb_function_attrib_variable(func, -1, proto->params[i].name, proto->params[i].debug_type); + tb_node_append_attrib(params[i], a); } } diff --git a/tb/include/tb.h b/tb/include/tb.h index d3f7fe6b..8b698ff3 100644 --- a/tb/include/tb.h +++ b/tb/include/tb.h @@ -805,8 +805,11 @@ TB_API TB_ModuleSection* tb_module_get_tls(TB_Module* m); //////////////////////////////// // Function Attributes //////////////////////////////// +TB_API void tb_node_append_attrib(TB_Node* n, TB_Attrib* a); + // These are parts of a function that describe metadata for instructions -TB_API void tb_function_attrib_variable(TB_Function* f, TB_Node* n, ptrdiff_t len, const char* name, TB_DebugType* type); +TB_API TB_Attrib* tb_function_attrib_variable(TB_Function* f, ptrdiff_t len, const char* name, TB_DebugType* type); +TB_API TB_Attrib* tb_function_attrib_scope(TB_Function* f, TB_Attrib* parent_scope); //////////////////////////////// // Debug info Generation diff --git a/tb/src/opt/mem2reg.h b/tb/src/opt/mem2reg.h index 15181d66..f5e43c20 100644 --- a/tb/src/opt/mem2reg.h +++ b/tb/src/opt/mem2reg.h @@ -66,19 +66,12 @@ static TB_Node* new_phi(Mem2Reg_Ctx* restrict c, TB_Function* f, int var, TB_Nod FOREACH_N(i, 0, block->input_count) n->inputs[1 + i] = NULL; // append variable attrib - const char* name = NULL; for (TB_Attrib* a = c->to_promote[var]->first_attrib; a; a = a->next) if (a->type == TB_ATTRIB_VARIABLE) { - append_attrib(f, n, a); - name = a->var.name; + tb_node_append_attrib(n, a); break; } - if (name) { - log_debug("%s: %p: insert new PHI node (in %p)", name, n, block); - } else { - log_debug("%p: insert new PHI node (in %p)", n, block); - } - + log_debug("%p: insert new PHI node (in %p)", n, block); tb_funcopt_mark(c->opt, n); return n; } diff --git a/tb/src/opt/print.h b/tb/src/opt/print.h index a6e4c4c0..d478d289 100644 --- a/tb/src/opt/print.h +++ b/tb/src/opt/print.h @@ -40,6 +40,17 @@ static void print_ref_to_node(PrinterCtx* ctx, TB_Node* n) { printf("%016"PRIx64, num->words[i]); } } + } else if (n->type == TB_PHI || n->type == TB_LOAD) { + ptrdiff_t search = nl_map_get(ctx->ordinals, n); + if (search >= 0) { + // alloc new ID + int id = ctx->count++; + nl_map_put(ctx->ordinals, n, id); + + printf("v%d", id); + } else { + printf("v%d", ctx->ordinals[search].v); + } } else { ptrdiff_t search = nl_map_get(ctx->ordinals, n); assert(search >= 0); @@ -96,14 +107,16 @@ static void print_node(PrinterCtx* ctx, TB_Node* n) { // print operands size_t first = tb_uses_effects(n); FOREACH_N(i, first, n->input_count) { - print_node(ctx, n->inputs[i]); + if (n->inputs[i]->type != TB_LOAD && n->inputs[i]->type != TB_PHI) { + print_node(ctx, n->inputs[i]); + } } // print as instruction printf(" v%d = %s.", id, tb_node_get_name(n)); TB_DataType dt = n->dt; - if (n->type >= TB_CMP_EQ && n->type <= TB_CMP_SLE) { + if (n->type >= TB_CMP_EQ && n->type <= TB_CMP_FLE) { dt = TB_NODE_GET_EXTRA_T(n, TB_NodeCompare)->cmp_dt; } print_type(dt); @@ -168,6 +181,36 @@ static void print_node(PrinterCtx* ctx, TB_Node* n) { break; } + case TB_GET_SYMBOL_ADDRESS: { + TB_Symbol* sym = TB_NODE_GET_EXTRA_T(n, TB_NodeSymbol)->sym; + if (sym->name) { + printf("%s", sym->name); + } else { + printf("%p", sym); + } + break; + } + + case TB_CALL: break; + + case TB_LOCAL: { + TB_NodeLocal* l = TB_NODE_GET_EXTRA(n); + printf(" !size %u !align %u", l->size, l->align); + break; + } + + case TB_FLOAT32_CONST: { + TB_NodeFloat32* f = TB_NODE_GET_EXTRA(n); + printf(" %f", f->value); + break; + } + + case TB_FLOAT64_CONST: { + TB_NodeFloat64* f = TB_NODE_GET_EXTRA(n); + printf(" %f", f->value); + break; + } + default: tb_assert(n->extra_count == 0, "TODO"); } @@ -238,6 +281,11 @@ static void print_effect(PrinterCtx* ctx, TB_Node* n) { break; } + case TB_CALL: { + print_node(ctx, n); + break; + } + case TB_RET: { printf(" ret "); FOREACH_N(i, 1, n->input_count) { diff --git a/tb/src/tb_builder.c b/tb/src/tb_builder.c index b709eb7c..e08ecc30 100644 --- a/tb/src/tb_builder.c +++ b/tb/src/tb_builder.c @@ -49,25 +49,21 @@ TB_API bool tb_node_is_constant_zero(TB_Node* n) { return false; } -static void append_attrib(TB_Function* f, TB_Node* n, TB_Attrib* a) { - TB_Attrib* chain = n->first_attrib; - if (chain == NULL) { - n->first_attrib = a; - } else { - // skip till the end - while (chain->next != NULL) chain = chain->next; - - chain->next = a; - } +TB_API void tb_node_append_attrib(TB_Node* n, TB_Attrib* a) { + a->next = n->first_attrib; + n->first_attrib = a; } -TB_API void tb_function_attrib_variable(TB_Function* f, TB_Node* n, ptrdiff_t len, const char* name, TB_DebugType* type) { - assert(name != NULL); - assert(type != NULL); - +TB_API TB_Attrib* tb_function_attrib_variable(TB_Function* f, ptrdiff_t len, const char* name, TB_DebugType* type) { TB_Attrib* a = tb_platform_heap_alloc(sizeof(TB_Attrib)); *a = (TB_Attrib) { .type = TB_ATTRIB_VARIABLE, .var = { tb__arena_strdup(f->super.module, len, name), type } }; - append_attrib(f, n, a); + return a; +} + +TB_API TB_Attrib* tb_function_attrib_scope(TB_Function* f, TB_Attrib* parent_scope) { + TB_Attrib* a = tb_platform_heap_alloc(sizeof(TB_Attrib)); + *a = (TB_Attrib) { .type = TB_ATTRIB_VARIABLE, .scope = { parent_scope } }; + return a; } static void* alloc_from_node_arena(TB_Function* f, size_t necessary_size) { @@ -100,7 +96,7 @@ TB_Node* tb_alloc_node(TB_Function* f, int type, TB_DataType dt, int input_count } if (f->line_attrib != NULL) { - append_attrib(f, n, f->line_attrib); + tb_node_append_attrib(n, f->line_attrib); } return n; diff --git a/tb/src/tb_internal.h b/tb/src/tb_internal.h index f5af4c60..2423a8de 100644 --- a/tb/src/tb_internal.h +++ b/tb/src/tb_internal.h @@ -229,6 +229,9 @@ struct TB_Attrib { TB_AttribType type; union { + struct { + TB_Attrib* parent; + } scope; struct { char* name; TB_DebugType* storage;