summaryrefslogtreecommitdiff
path: root/codegen.c
diff options
context:
space:
mode:
authorCarson Fleming <cflems@cflems.net>2026-07-18 23:38:59 -0700
committerCarson Fleming <cflems@cflems.net>2026-07-18 23:38:59 -0700
commit1e712cf04567c48100526f9a3a0f796497168268 (patch)
tree28a1676d36620500578d1a386d8d0b976637ce9f /codegen.c
parentfeb1cac139ca5aa2ba76ac6a0a318bced03d8638 (diff)
downloadccc-1e712cf04567c48100526f9a3a0f796497168268.tar.gz
type system surely
Diffstat (limited to 'codegen.c')
-rw-r--r--codegen.c34
1 files changed, 15 insertions, 19 deletions
diff --git a/codegen.c b/codegen.c
index f9dbc15..955a095 100644
--- a/codegen.c
+++ b/codegen.c
@@ -225,12 +225,6 @@ static void emit_cmp_zero(FILE* outfile, const struct lval_def* lval) {
fprintf(outfile, ", 0\n");
}
-/* TODO: move this utility to the type checker and use resolved_type */
-static unsigned long long get_type_size(const struct type_ref_node* type) {
- if (type->ptr_level > 0) return PTR_SIZE;
- return type->def_ref->sz;
-}
-
static inline struct lval_def make_stack_lval(
FILE* outfile,
const struct type_def* type
@@ -310,7 +304,8 @@ static struct var_def* emit_var_decl(
FILE* outfile,
const struct var_decl_node* node
) {
- struct lval_def var_dst = make_stack_lval(outfile, node->type.def_ref);
+ struct lval_def var_dst =
+ make_stack_lval(outfile, node->def_ref->resolved_type);
fprintf(outfile, "\t; '%s' lives in: ", node->def_ref->name);
emit_storage_loc(outfile, &var_dst.loc, var_dst.type->sz);
@@ -365,7 +360,7 @@ static void emit_call(
struct args_eval_node* arg_eval = node->args;
while (arg_decl != NULL && arg_eval != NULL) {
struct lval_def arg_dst =
- make_stack_lval(outfile, arg_decl->decl->type.def_ref);
+ make_stack_lval(outfile, arg_decl->decl->def_ref->resolved_type);
emit_expr(outfile, arg_eval->expr, &arg_dst);
arg_decl = arg_decl->next;
@@ -384,20 +379,21 @@ static void emit_call(
unsigned char arg_regnum = 0;
arg_decl = node->called_fn_ref->args;
while (arg_decl != NULL) {
- unsigned long long type_sz = get_type_size(&arg_decl->decl->type);
- arg_bp_offset += type_sz;
+ const struct type_def* arg_type =
+ arg_decl->decl->def_ref->resolved_type;
+ arg_bp_offset += arg_type->sz;
struct lval_def arg_dst;
if (arg_regnum < CC_N_REGS)
arg_dst = (struct lval_def) {
- .type = arg_decl->decl->type.def_ref,
+ .type = arg_type,
.loc = (struct storage_location) {
.type = STO_REG,
.reg = CALLING_CONV[arg_regnum++],
},
};
else
- arg_dst = make_stack_lval(outfile, arg_decl->decl->type.def_ref);
+ arg_dst = make_stack_lval(outfile, arg_type);
emit_mov(outfile, &arg_dst, &(struct storage_location) {
.type = STO_STACK,
@@ -408,7 +404,7 @@ static void emit_call(
fprintf(outfile, "\tcall %s\n", node->called_fn_ref->name);
if (dst != NULL) {
- if (get_type_size(&node->called_fn_ref->return_type) == 0)
+ if (node->called_fn_ref->resolved_return_type->sz == 0)
CGEN_PANIC("can't assign the result of a void function");
emit_mov(outfile, dst, &RV_LOC);
@@ -542,10 +538,9 @@ static void emit_expr(
static void emit_return(FILE* outfile, const struct return_node* node) {
if (active_fn == NULL) CGEN_PANIC("must be inside a function to return");
- const struct type_def* return_type = active_fn->return_type.def_ref;
if (node->ret_val != NULL) {
- if (return_type->sz == 0)
+ if (active_fn->resolved_return_type->sz == 0)
CGEN_PANIC(
"returning a value from void function %s", active_fn->name);
@@ -554,9 +549,9 @@ static void emit_return(FILE* outfile, const struct return_node* node) {
node->ret_val,
&(struct lval_def) {
.loc = RV_LOC,
- .type = return_type,
+ .type = active_fn->resolved_return_type,
});
- } else if (return_type > 0) {
+ } else if (active_fn->resolved_return_type > 0) {
CGEN_PANIC(
"non-void function %s should return a value", active_fn->name);
}
@@ -667,6 +662,7 @@ static void emit_fn_decl(FILE* outfile, const struct fn_decl_node* node) {
active_fn->name);
active_fn = node;
+ /* TODO: we need to account for the base pointer moving in var locs */
fprintf(outfile, "%s:\n", node->name);
fprintf(outfile, "\tpush rbp\n");
fprintf(outfile, "\tmov rbp, rsp\n");
@@ -679,7 +675,7 @@ static void emit_fn_decl(FILE* outfile, const struct fn_decl_node* node) {
for (; arg_decl != NULL; arg_decl = arg_decl->next) {
struct var_def* arg_def = arg_decl->decl->def_ref;
struct lval_def arg_dst =
- make_stack_lval(outfile, arg_decl->decl->type.def_ref);
+ make_stack_lval(outfile, arg_def->resolved_type);
arg_def->loc = arg_dst.loc;
struct storage_location arg_src;
@@ -693,7 +689,7 @@ static void emit_fn_decl(FILE* outfile, const struct fn_decl_node* node) {
.type = STO_STACK,
.bp_offset = spilled_bp_ofs,
};
- spilled_bp_ofs -= arg_decl->decl->type.def_ref->sz;
+ spilled_bp_ofs -= arg_def->resolved_type->sz;
}
emit_mov(outfile, &arg_dst, &arg_src);
}