diff --git a/bang/examples/gol.bang b/bang/examples/gol.bang index ee85c26..36b56c7 100644 --- a/bang/examples/gol.bang +++ b/bang/examples/gol.bang @@ -9,18 +9,6 @@ var board_next: i64; var display_base: ptr; -var cell_ptr: ptr; -var cell: u8; - -var row: i64; -var col: i64; -var drow: i64; -var dcol: i64; - -var nbors: i64; - -var i: i64; - proc init() { boards_base = heap_base; board_rows = 10; @@ -38,8 +26,13 @@ proc init() { } proc print_board_current() { + var row: i64; row = 0; while row < board_rows { + var cell_ptr: ptr; + var cell: u8; + + var col: i64; col = 0; while col < board_cols { cell_ptr = boards_base + cast(ptr, board_size * board_current + row * board_cols + col); @@ -66,14 +59,23 @@ proc print_board_current() { proc next_board() { board_next = 1 - board_current; + var row: i64; row = 0; while row < board_rows { + var cell_ptr: ptr; + var cell: u8; + + var col: i64; col = 0; while col < board_cols { # Nbors begin + var nbors: i64; nbors = 0; + + var drow: i64; drow = row - 1; while drow < row + 2 { + var dcol: i64; dcol = col - 1; while dcol < col + 2 { if drow >= 0 && drow < board_rows { @@ -122,6 +124,7 @@ proc next_board() { proc main() { init(); + var i: i64; i = 0; while i < 10 { print_board_current(); diff --git a/bang/examples/rule110.bang b/bang/examples/rule110.bang index 47a4c21..777a331 100644 --- a/bang/examples/rule110.bang +++ b/bang/examples/rule110.bang @@ -11,12 +11,6 @@ var state_next: i64; var display_base: ptr; -var i: i64; -var j: i64; - -var cell_ptr: ptr; -var cell: u8; - proc init() { # Init lookup table table_base = heap_base; @@ -41,7 +35,10 @@ proc init() { } proc print_state_current() { + var cell_ptr: ptr; + var cell: u8; + var i: i64; i = 0; while i < state_size { cell_ptr = states_base + cast(ptr, state_current * state_size + i); @@ -63,7 +60,12 @@ proc print_state_current() { } proc next_state() { + var cell_ptr: ptr; + var cell: u8; + state_next = 1 - state_current; + + var i: i64; i = 1; while i < state_size - 1 { table_index = cast(u8, 0); @@ -93,10 +95,11 @@ proc next_state() { proc main() { init(); - j = 0; - while j < state_size - 2 { + var i: i64; + i = 0; + while i < state_size - 2 { print_state_current(); next_state(); - j = j + 1; + i = i + 1; } } diff --git a/bang/src/bang.c b/bang/src/bang.c index 2c96b48..71b0a71 100644 --- a/bang/src/bang.c +++ b/bang/src/bang.c @@ -114,7 +114,7 @@ static void run_subcommand(int argc, char **argv) bang.write_id = basm_push_external_native(&basm, SV("write")); bang_prepare_var_stack(&bang, &basm, stack_size); - bang_push_new_scope(&bang, &basm); + bang_push_new_scope(&bang); { compile_bang_module_into_basm(&bang, &basm, module); @@ -122,7 +122,7 @@ static void run_subcommand(int argc, char **argv) bang_generate_heap_base(&bang, &basm, SV("heap_base")); assert(basm.has_entry); } - bang_pop_scope(&bang, &basm); + bang_pop_scope(&bang); basm_save_to_bm(&basm, &bm); @@ -264,7 +264,7 @@ static void build_subcommand(int argc, char **argv) bang.write_id = basm_push_external_native(&basm, SV("write")); bang_prepare_var_stack(&bang, &basm, stack_size); - bang_push_new_scope(&bang, &basm); + bang_push_new_scope(&bang); { compile_bang_module_into_basm(&bang, &basm, module); @@ -273,7 +273,7 @@ static void build_subcommand(int argc, char **argv) assert(basm.has_entry); basm_save_to_file_as_target(&basm, output_file_path, output_target); } - bang_pop_scope(&bang, &basm); + bang_pop_scope(&bang); arena_free(&basm.arena); arena_free(&bang.arena); diff --git a/bang/src/bang_compiler.c b/bang/src/bang_compiler.c index b140864..bc70ae2 100644 --- a/bang/src/bang_compiler.c +++ b/bang/src/bang_compiler.c @@ -261,18 +261,8 @@ Compiled_Expr compile_bang_expr_into_basm(Bang *bang, Basm *basm, Bang_Expr expr } Compiled_Var *var = bang_get_compiled_var_by_name(bang, arg.as.var_read.name); - switch (var->storage) { - case BANG_VAR_STATIC_STORAGE: { - basm_push_inst(basm, INST_PUSH, word_u64(var->addr)); - result.type = BANG_TYPE_PTR; - } - break; - - case BANG_VAR_STACK_STORAGE: { - assert(false && "TODO(#455): Taking a pointer to stack variable is not implemented"); - } - break; - } + compile_get_var_addr(bang, basm, var); + result.type = BANG_TYPE_PTR; } else if (sv_eq(funcall.name, SV("write_ptr"))) { bang_funcall_expect_arity(funcall, 2); Bang_Funcall_Arg *args = funcall.args; @@ -369,7 +359,9 @@ Compiled_Expr compile_bang_expr_into_basm(Bang *bang, Basm *basm, Bang_Expr expr SV_Arg(funcall.name)); exit(1); } + compile_push_new_frame(bang, basm); basm_push_inst(basm, INST_CALL, word_u64(proc->addr)); + compile_pop_frame(bang, basm); result.type = BANG_TYPE_VOID; } } @@ -561,12 +553,12 @@ void compile_stmt_into_basm(Bang *bang, Basm *basm, Bang_Stmt stmt) void compile_block_into_basm(Bang *bang, Basm *basm, Bang_Block *block) { - bang_push_new_scope(bang, basm); + bang_push_new_scope(bang); while (block) { compile_stmt_into_basm(bang, basm, block->stmt); block = block->next; } - bang_pop_scope(bang, basm); + bang_pop_scope(bang); } Compiled_Proc *bang_get_compiled_proc_by_name(Bang *bang, String_View name) @@ -764,8 +756,8 @@ void compile_stack_var_def_into_basm(Bang *bang, Bang_Var_Def var_def) new_var.type = type; new_var.storage = BANG_VAR_STACK_STORAGE; assert(type_def.size > 0); - bang->scope->frame_top_offset += type_def.size; - new_var.addr = bang->scope->frame_top_offset; + bang->frame_size += type_def.size; + new_var.addr = bang->frame_size; bang_scope_push_var(bang->scope, new_var); } @@ -777,7 +769,7 @@ void compile_push_new_frame(Bang *bang, Basm *basm) // 2. offset the frame addr to find the top of the stack assert(bang->scope != NULL); - basm_push_inst(basm, INST_PUSH, word_u64(bang->scope->frame_top_offset)); + basm_push_inst(basm, INST_PUSH, word_u64(bang->frame_size)); basm_push_inst(basm, INST_MINUSI, word_u64(0)); // 3. allocate memory to store the prev frame addr @@ -804,20 +796,26 @@ void compile_pop_frame(Bang *bang, Basm *basm) compile_write_frame_addr(bang, basm); } -void bang_push_new_scope(Bang *bang, Basm *basm) +void bang_push_new_scope(Bang *bang) { - if (bang->scope) { - compile_push_new_frame(bang, basm); - } - Bang_Scope *scope = arena_alloc(&bang->arena, sizeof(Bang_Scope)); scope->parent = bang->scope; bang->scope = scope; } -void bang_pop_scope(Bang *bang, Basm *basm) +void bang_pop_scope(Bang *bang) { - compile_pop_frame(bang, basm); assert(bang->scope != NULL); + Bang_Scope *scope = bang->scope; + + size_t dealloc_size = 0; + for (size_t i = 0; i < scope->vars_count; ++i) { + if (scope->vars[i].storage == BANG_VAR_STACK_STORAGE) { + Bang_Type_Def def = bang_type_def(scope->vars[i].type); + dealloc_size += def.size; + } + } + bang->frame_size -= dealloc_size; + bang->scope = bang->scope->parent; } diff --git a/bang/src/bang_compiler.h b/bang/src/bang_compiler.h index 21a109e..2038af4 100644 --- a/bang/src/bang_compiler.h +++ b/bang/src/bang_compiler.h @@ -57,7 +57,6 @@ struct Bang_Scope { Bang_Scope *parent; Compiled_Var vars[BANG_SCOPE_VARS_CAPACITY]; size_t vars_count; - Memory_Addr frame_top_offset; }; Compiled_Var *bang_scope_get_compiled_var_by_name(Bang_Scope *scope, String_View name); @@ -74,6 +73,7 @@ typedef struct { Memory_Addr stack_frame_var_addr; Bang_Scope *scope; + size_t frame_size; Compiled_Proc procs[BANG_PROCS_CAPACITY]; size_t procs_count; @@ -111,8 +111,8 @@ void compile_write_frame_addr(Bang *bang, Basm *basm); void compile_push_new_frame(Bang *bang, Basm *basm); void compile_pop_frame(Bang *bang, Basm *basm); -void bang_push_new_scope(Bang *bang, Basm *basm); -void bang_pop_scope(Bang *bang, Basm *basm); +void bang_push_new_scope(Bang *bang); +void bang_pop_scope(Bang *bang); void bang_generate_entry_point(Bang *bang, Basm *basm, String_View entry_proc_name); void bang_generate_heap_base(Bang *bang, Basm *basm, String_View heap_base_var_name);