diff options
| author | Mustafa Quraish <[email protected]> | 2022-02-02 02:34:19 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-02-02 02:34:19 -0500 |
| commit | ae3638402a41f1c75f59aa1da923d6f7ffd058ac (patch) | |
| tree | d1f985eb73ce3def8aa3caa6220687af71a4d125 | |
| parent | Modify how types are stored. (diff) | |
| download | cup-ae3638402a41f1c75f59aa1da923d6f7ffd058ac.tar.xz cup-ae3638402a41f1c75f59aa1da923d6f7ffd058ac.zip | |
Add `size_for_type()` helper instead of hard-coding variable sizes
| -rw-r--r-- | src/ast.c | 10 | ||||
| -rw-r--r-- | src/ast.h | 1 | ||||
| -rw-r--r-- | src/parser.c | 10 |
3 files changed, 16 insertions, 5 deletions
@@ -55,6 +55,16 @@ char *data_type_to_str(DataType type) } } +i64 size_for_type(Type *type) +{ + switch (type->type) + { + case TYPE_INT: return 8; + case TYPE_PTR: return 8; + default: assert(false && "Unreachable type"); + } +} + Type *type_new(DataType type) { Type *t = calloc(sizeof(Type), 1); @@ -71,6 +71,7 @@ typedef struct data_type_node { } Type; Type *type_new(DataType type); +i64 size_for_type(Type *type); typedef struct { char *name; diff --git a/src/parser.c b/src/parser.c index 67cbfbc..a9a26e3 100644 --- a/src/parser.c +++ b/src/parser.c @@ -144,8 +144,7 @@ Node *find_function_definition(Token *token) void add_global_variable(Variable *var) { var->offset = global_vars_offset; - // TODO: Compute based on type - int var_size = 8; + int var_size = size_for_type(var->type); global_vars_offset += var_size; global_vars[global_vars_count++] = var; } @@ -157,7 +156,8 @@ void add_variable_to_current_block(Variable *var) Node *cur_block = block_stack[block_stack_count - 1]; int new_len = (cur_block->block.num_locals + 1); - int var_size = 8; // TODO: Compute sizes based on different types + // TODO: Align the stack to a certain size? + int var_size = size_for_type(var->type); // Add to the block // FIXME: Use a map here @@ -560,8 +560,8 @@ void parse_func_args(Lexer *lexer, Node *func) for (int i = 0; i < func->func.num_args; i++) { Variable *var = &func->func.args[i]; var->offset = offset; - // TODO: Compute this for different types - int var_size = 8; + // TODO: Do we need to align the stack here? + int var_size = size_for_type(var->type); offset -= var_size; } } |