aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-01-31 02:51:42 -0500
committerMustafa Quraish <[email protected]>2022-01-31 02:51:42 -0500
commite48b7eced89f47b7259648405a4da461c2ea0853 (patch)
treebf355a50cd302290ad29305a0b583a5c86affc20 /src
parentMake `Lexer_new` return a pointer instead of the object. (diff)
downloadcup-e48b7eced89f47b7259648405a4da461c2ea0853.tar.xz
cup-e48b7eced89f47b7259648405a4da461c2ea0853.zip
Fix offset for local variables
The stack actually grows downwards, so we need to account for that. The previous implementation was incorrect and exploded if you tried to use local variables in other functions except main.
Diffstat (limited to 'src')
-rw-r--r--src/parser.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/parser.c b/src/parser.c
index 2c11656..128f18a 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -164,7 +164,6 @@ void add_variable_to_current_block(Variable *var)
{
// Set offset for variable
Node *cur_block = block_stack[block_stack_count - 1];
- var->offset = cur_stack_offset;
int new_len = (cur_block->block.num_locals + 1);
int var_size = 8; // TODO: Compute sizes based on different types
@@ -179,6 +178,7 @@ void add_variable_to_current_block(Variable *var)
// Update current stack offset (w.r.t function stack frame) and block size
cur_stack_offset += var_size;
block_stack[block_stack_count-1]->block.locals_size += var_size;
+ var->offset = cur_stack_offset;
// Update function's max locals size
i64 max_offset = i64max(current_function->func.max_locals_size, cur_stack_offset);