From: Brendan Hansen Date: Mon, 20 Jul 2020 01:37:53 +0000 (-0500) Subject: small bugfixes; testing pointers X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=47f09833fe1e6d5ba730e17f059bee935a5c98c6;p=onyx.git small bugfixes; testing pointers --- diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index c2fc7c67..14281d03 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -361,6 +361,7 @@ extern const BuiltinSymbol builtin_symbols[]; static inline b32 is_lval(AstNode* node) { return (node->kind == Ast_Kind_Local) || (node->kind == Ast_Kind_Global) + || (node->kind == Ast_Kind_Dereference) || (node->kind == Ast_Kind_Array_Access); } diff --git a/onyx b/onyx index 03babad2..deefae6b 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/arrays.onyx b/progs/arrays.onyx index a7ec6d2d..5b0f5d8e 100644 --- a/progs/arrays.onyx +++ b/progs/arrays.onyx @@ -52,11 +52,15 @@ str_test :: proc #export { } // Don't need to bind this function to a symbol -proc #export "main" { +proc #export "main2" { print(__heap_start as i32); - global_arr = __heap_start as ^i32; len :: 10; + global_arr = alloc((len * 4) as u32) as ^i32; + other_arr := alloc((len * 4) as u32) as ^i32; + + print(global_arr as i32); + print(other_arr as i32); for i: 0, len global_arr[i] = (len - i) * 10; @@ -67,3 +71,40 @@ proc #export "main" { print(1234567); print(global_arr, len); } + + + + + + + + + + + + +alloc :: proc (size: u32) -> rawptr { + heap_u32 :: __heap_start as ^u32; + + curr_offset := *heap_u32; + if curr_offset == 0 as u32 curr_offset = 8 as u32; + + *heap_u32 = curr_offset + size; + + return ((__heap_start as u32) + curr_offset) as rawptr; +} + + + + + +multi_arr_test :: proc #export "main" { + arrs := alloc((10 * 4) as u32) as ^^i32; + + for i: 0, 10 { + arrs[i] = alloc((10 * 4) as u32) as ^i32; + } + + for y: 0, 10 for x: 0, 10 arrs[y][x] = x + y * 10; + for y: 0, 10 for x: 0, 10 print(arrs[x][y]); +} diff --git a/src/onyxparser.c b/src/onyxparser.c index a630e36b..03de4daa 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -602,6 +602,7 @@ static AstNode* parse_statement(OnyxParser* parser) { case '+': case '-': case '!': + case '*': case Token_Type_Literal_Numeric: case Token_Type_Literal_String: retval = (AstNode *) parse_expression(parser); diff --git a/src/onyxsymres.c b/src/onyxsymres.c index ca3c274f..02cff8b6 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -15,7 +15,7 @@ AstBasicType basic_type_f32 = { { Ast_Kind_Basic_Type, 0, "f32" }, &basic_ AstBasicType basic_type_f64 = { { Ast_Kind_Basic_Type, 0, "f64" }, &basic_types[Basic_Kind_F64] }; AstBasicType basic_type_rawptr = { { Ast_Kind_Basic_Type, 0, "rawptr" }, &basic_types[Basic_Kind_Rawptr] }; -AstNumLit builtin_heap_start = { Ast_Kind_NumLit, 0, NULL, NULL, (AstType *) &basic_type_rawptr, NULL, 0 }; +AstNumLit builtin_heap_start = { Ast_Kind_NumLit, Ast_Flag_Const, NULL, NULL, (AstType *) &basic_type_rawptr, NULL, 0 }; const BuiltinSymbol builtin_symbols[] = { { "void", (AstNode *) &basic_type_void }, diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 7e949cc3..cefd6e72 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -332,6 +332,25 @@ COMPILE_FUNC(assignment, AstBinaryOp* assign) { compile_expression(mod, &code, assign->right); WID(WI_GLOBAL_SET, globalidx); + } else if (lval->kind == Ast_Kind_Dereference) { + AstDereference* deref = (AstDereference *) lval; + compile_expression(mod, &code, deref->expr); + compile_expression(mod, &code, assign->right); + + i32 store_size = deref->type->Basic.size; + i32 is_integer = (deref->type->Basic.flags & Basic_Flag_Integer) + || (deref->type->Basic.flags & Basic_Flag_Pointer); + + if (is_integer) { + if (store_size == 1) WID(WI_I32_STORE_8, ((WasmInstructionData) { 0, 0 })); + else if (store_size == 2) WID(WI_I32_STORE_16, ((WasmInstructionData) { 1, 0 })); + else if (store_size == 4) WID(WI_I32_STORE, ((WasmInstructionData) { 2, 0 })); + else if (store_size == 8) WID(WI_I64_STORE, ((WasmInstructionData) { 3, 0 })); + } else { + if (store_size == 4) WID(WI_F32_STORE, ((WasmInstructionData) { 2, 0 })); + else if (store_size == 8) WID(WI_F64_STORE, ((WasmInstructionData) { 3, 0 })); + } + } else if (lval->kind == Ast_Kind_Array_Access) { AstArrayAccess* aa = (AstArrayAccess *) lval; compile_expression(mod, &code, aa->expr); @@ -786,7 +805,7 @@ COMPILE_FUNC(expression, AstTyped* expr) { else if (load_size == 4) instr = WI_I32_LOAD; else if (load_size == 8) instr = WI_I64_LOAD; - if (alignment < 4 && is_unsigned) instr += 1; + if (load_size < 4 && is_unsigned) instr += 1; } else { if (load_size == 4) instr = WI_F32_LOAD; else if (load_size == 8) instr = WI_F64_LOAD; @@ -825,7 +844,7 @@ COMPILE_FUNC(expression, AstTyped* expr) { else if (load_size == 4) instr = WI_I32_LOAD; else if (load_size == 8) instr = WI_I64_LOAD; - if (alignment < 4 && is_unsigned) instr += 1; + if (load_size < 4 && is_unsigned) instr += 1; } else { if (load_size == 4) instr = WI_F32_LOAD; else if (load_size == 8) instr = WI_F64_LOAD;