From b48e27f5cc31af65d8bb92d4b81cd03a60c5bcdb Mon Sep 17 00:00:00 2001 From: Carson Fleming Date: Thu, 6 Aug 2026 00:24:49 -0400 Subject: clean up yesterday's hacks + functions in type system --- codegen.c | 87 ++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 42 insertions(+), 45 deletions(-) (limited to 'codegen.c') diff --git a/codegen.c b/codegen.c index 71350e4..3950912 100644 --- a/codegen.c +++ b/codegen.c @@ -7,6 +7,7 @@ #include #include #include +#include #define CGEN_PANIC(format, ...) {\ fprintf(\ @@ -32,7 +33,7 @@ static struct reg* MULDIV_OVERFLOW_REG = &RDX; static struct hash_map dseg; static struct scope* scope; -static const struct fn_decl_node* active_fn; +struct var_def* active_fn; static integral_t branch_counter = 0; static integral_t loop_counter = 0; @@ -90,8 +91,11 @@ static const struct data_type* get_effective_data_type( return type->data.data_type; case TP_PTR: return &long_long_type; + case TP_FN: + /* functions have size 1 I guess */ + return &char_type; } - CGEN_PANIC("unhandled type of type case"); + unreachable(); } static struct lval_def allocate_stack( @@ -132,11 +136,8 @@ static void release_temporary(FILE* outfile, const struct lval_def* tmp) { break; case STO_STACK: case STO_IMM: - case STO_FN: case STO_LABEL: break; - case STO_UNRESOLVED: - CGEN_PANIC("can't release unresolved storage"); } } @@ -149,9 +150,6 @@ static void emit_storage_loc( case STO_LABEL: fprintf(outfile, "%s", loc->label); break; - case STO_FN: - fprintf(outfile, "%s", loc->decl->name); - break; case STO_REG: if (sz > 4) fprintf(outfile, "%s", loc->reg->qword); else if (sz > 2) fprintf(outfile, "%s", loc->reg->dword); @@ -169,8 +167,6 @@ static void emit_storage_loc( case STO_IMM: fprintf(outfile, "0x%llx", loc->value); break; - case STO_UNRESOLVED: - CGEN_PANIC("can't emit unresolved storage location"); } } @@ -189,12 +185,8 @@ static bool locs_equal( return a->bp_offset == b->bp_offset; case STO_LABEL: return strcmp(a->label, b->label) == 0; - case STO_FN: - return a->decl == b->decl; - case STO_UNRESOLVED: - return false; } - CGEN_PANIC("unhandled storage type case"); + unreachable(); } static void emit_size_const(FILE* outfile, integral_t sz) { @@ -253,10 +245,8 @@ static void emit_label_lea( if (spill_reg) unspill_register(outfile, tmp_reg); else release_register(tmp_reg); return; - case STO_FN: case STO_IMM: case STO_LABEL: - case STO_UNRESOLVED: CGEN_PANIC("can't load label address into non-value storage"); } } @@ -331,9 +321,7 @@ static void emit_mov( break; case STO_LABEL: case STO_IMM: - case STO_FN: - case STO_UNRESOLVED: - CGEN_PANIC("can't move value into storage type"); + CGEN_PANIC("can't move value into non-storage type"); } fprintf(outfile, "\n"); } @@ -351,9 +339,7 @@ static void emit_cmp_zero(FILE* outfile, const struct lval_def* lval) { break; case STO_LABEL: case STO_IMM: - case STO_FN: - case STO_UNRESOLVED: - CGEN_PANIC("can't compare this storage type") + CGEN_PANIC("can't compare this non-storage type") } fprintf(outfile, ", 0\n"); } @@ -427,7 +413,7 @@ static void emit_var_ref( const struct lval_def* dst ) { if (dst != NULL) { - emit_mov(outfile, dst, &node->def_ref->loc); + emit_mov(outfile, dst, &node->def_ref->storage); } } @@ -439,7 +425,7 @@ static void emit_decl( ) { struct lval_def var_dst = allocate_stack(outfile, node->def_ref->type); - node->def_ref->loc = var_dst.loc; + node->def_ref->storage = var_dst.loc; fprintf(outfile, "\t; %s\n", node->def_ref->name); @@ -468,7 +454,7 @@ static void emit_assignment( struct var_ref_node* var_ref = &node->lval->inner.var_ref; lval_def = (struct lval_def) { .type = var_ref->def_ref->type, - .loc = var_ref->def_ref->loc, + .loc = var_ref->def_ref->storage, }; break; default: @@ -532,10 +518,14 @@ static void emit_call( } /* 3. `call