From e48b7eced89f47b7259648405a4da461c2ea0853 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Mon, 31 Jan 2022 02:51:42 -0500 Subject: 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. --- src/parser.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') 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); -- cgit v1.2.3