small bugfixes; testing pointers
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 20 Jul 2020 01:37:53 +0000 (20:37 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 20 Jul 2020 01:37:53 +0000 (20:37 -0500)
include/onyxastnodes.h
onyx
progs/arrays.onyx
src/onyxparser.c
src/onyxsymres.c
src/onyxwasm.c

index c2fc7c676f6a97e0a4caa9444099daadc261f87c..14281d03d99d02bdbbf87a5615a51cdcb80db7b2 100644 (file)
@@ -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 03babad22da9c27cb3fd3ccf2e1e3a45c95bc39e..deefae6b1fd5570dbcc721f8cf2e93d9430fd83d 100755 (executable)
Binary files a/onyx and b/onyx differ
index a7ec6d2daefe8bde8f472df9686f3afbd4eb0b44..5b0f5d8ea3eba60a9150a87275a957ca1bfe53f0 100644 (file)
@@ -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]);
+}
index a630e36b3ba7a3cddc6cd6b6ca75ce41c353c5c1..03de4daa3c35580c8c90c1107f448efe79a7aa93 100644 (file)
@@ -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);
index ca3c274f51b1040f524ec46b531b497f54161d64..02cff8b6ef8c0889786e191d853f28ea2d3127a8 100644 (file)
@@ -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 },
index 7e949cc3aa520a103a4baace04abb3c81776e314..cefd6e721bd8ddde91c1533c2c99e1f22b869676 100644 (file)
@@ -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;