aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-02 02:34:19 -0500
committerMustafa Quraish <[email protected]>2022-02-02 02:34:19 -0500
commitae3638402a41f1c75f59aa1da923d6f7ffd058ac (patch)
treed1f985eb73ce3def8aa3caa6220687af71a4d125 /src
parentModify how types are stored. (diff)
downloadcup-ae3638402a41f1c75f59aa1da923d6f7ffd058ac.tar.xz
cup-ae3638402a41f1c75f59aa1da923d6f7ffd058ac.zip
Add `size_for_type()` helper instead of hard-coding variable sizes
Diffstat (limited to 'src')
-rw-r--r--src/ast.c10
-rw-r--r--src/ast.h1
-rw-r--r--src/parser.c10
3 files changed, 16 insertions, 5 deletions
diff --git a/src/ast.c b/src/ast.c
index edaf5c3..2a388bb 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -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);
diff --git a/src/ast.h b/src/ast.h
index 74a8ef8..287f51b 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -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;
}
}