Skip to content

Commit

Permalink
tb: starting on scopes, improved flatten IR print
Browse files Browse the repository at this point in the history
  • Loading branch information
RealNeGate committed Jul 15, 2023
1 parent d2b7f94 commit fd9faf4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 30 deletions.
7 changes: 5 additions & 2 deletions libCuik/lib/back/ir_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
5 changes: 4 additions & 1 deletion tb/include/tb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions tb/src/opt/mem2reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
52 changes: 50 additions & 2 deletions tb/src/opt/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -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) {
Expand Down
28 changes: 12 additions & 16 deletions tb/src/tb_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions tb/src/tb_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ struct TB_Attrib {
TB_AttribType type;

union {
struct {
TB_Attrib* parent;
} scope;
struct {
char* name;
TB_DebugType* storage;
Expand Down

0 comments on commit fd9faf4

Please sign in to comment.