code cleanup; can 'use' bare structures
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 15 Aug 2020 15:19:04 +0000 (10:19 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 15 Aug 2020 15:19:04 +0000 (10:19 -0500)
core/alloc.onyx
include/onyxastnodes.h
onyx
progs/stack_based.onyx
src/onyxchecker.c
src/onyxsymres.c
src/onyxwasm.c

index 81a96dd2ed12651e82edcd76ebbe199e7e31514b..e70f900d4d6e308060ef7e04437c2f451f9ae531 100644 (file)
@@ -22,15 +22,15 @@ Allocator :: struct {
     func: alloc_proc; 
 }
 
-alloc :: proc (use a: ^Allocator, size: u32) -> rawptr {
+alloc :: proc (use a: Allocator, size: u32) -> rawptr {
     return func(data, AllocAction.Alloc, size, 16, null);
 }
 
-resize :: proc (use a: ^Allocator, ptr: rawptr, size: u32) -> rawptr {
+resize :: proc (use a: Allocator, ptr: rawptr, size: u32) -> rawptr {
     return func(data, AllocAction.Resize, size, 16, ptr);
 }
 
-free :: proc (use a: ^Allocator, ptr: rawptr) {
+free :: proc (use a: Allocator, ptr: rawptr) {
     func(data, AllocAction.Free, 0, 0, ptr);
 }
 
@@ -159,9 +159,9 @@ heap_alloc_proc :: proc (data: rawptr, aa: AllocAction, size: u32, align: u32, o
     return null;
 }
 
-malloc  :: proc (size: u32) -> rawptr do return alloc(^heap_allocator, size);
-mfree   :: proc (ptr: rawptr) do free(^heap_allocator, ptr);
-mresize :: proc (ptr: rawptr, size: u32) -> rawptr do return resize(^heap_allocator, ptr, size);
+malloc  :: proc (size: u32) -> rawptr do return alloc(heap_allocator, size);
+mfree   :: proc (ptr: rawptr) do free(heap_allocator, ptr);
+mresize :: proc (ptr: rawptr, size: u32) -> rawptr do return resize(heap_allocator, ptr, size);
 
 
 
@@ -206,10 +206,10 @@ scratch_alloc_init :: proc (a: ^Allocator, ss: ^ScratchState) {
 }
 
 
-#private return_scratch_size :: 256
-#private return_scratch_buff :  [return_scratch_size] u8
+#private return_scratch_size  :: 256
+#private return_scratch_buff  :  [return_scratch_size] u8
+#private return_scratch_state :  ScratchState;
 
-return_scratch_state : ScratchState;
 return_scratch_alloc : Allocator;
 
 memory_init :: proc {
index 7fb649f6d99e583262ebc0595d3378c4a208ab14..c9b0e1460849e49379df381603413b53775e20c8 100644 (file)
@@ -353,7 +353,7 @@ struct AstTypeAlias { AstType_base; AstType* to; };
 
 // Top level nodes
 struct AstBinding       { AstTyped_base; AstNode* node; };
-struct AstMemRes        { AstTyped_base; u64 addr; AstTyped *initial_value; }; // HACK: This has to be the same size or bigger as AstDereference
+struct AstMemRes        { AstTyped_base; u64 addr; AstTyped *initial_value; };
 struct AstInclude       { AstNode_base; OnyxToken *name; };
 struct AstUsePackage    {
     AstNode_base;
diff --git a/onyx b/onyx
index 9e1bc71ea4113c5886750394d5353ce4b709c02e..77ebf6e3fbae2fecd5b18ece9fba250d61bbbb04 100755 (executable)
Binary files a/onyx and b/onyx differ
index dadd2ade0416b6904c621f5b5586e7515cd44b84..b9d80080ddefa8ed487c23aabe2d8a648bba5652 100644 (file)
@@ -142,29 +142,27 @@ start :: proc #export {
 
     stupid_idea(1234)() |> print();
 
-    {
-        varr : [5] Vec3;
-        varr[2].x = 4;
-        varr[2].y = 5;
-        varr[2].z = 6;
-        mag_squared(varr[2]) |> print();
-
-        v1 : Vec3;
-        v1.x = 1;
-        v1.y = 2;
-        v1.z = 4;
-
-        v2 := v1;
-        
-        v3 : Vec3 = *vadd(v1, v2);
-        print(v3.x);
-        print(v3.y);
-        print(v3.z);
-    }
+    varr : [5] Vec3;
+    varr[2].x = 4;
+    varr[2].y = 5;
+    varr[2].z = 6;
+    mag_squared(varr[2]) |> print();
+
+    v1 : Vec3;
+    v1.x = 1;
+    v1.y = 2;
+    v1.z = 4;
+
+    v2 := v1;
+
+    v3 := *vadd(v1, v2);
+    print(v3.x);
+    print(v3.y);
+    print(v3.z);
 }
 
 vadd :: proc (v1: Vec3, v2: Vec3) -> ^Vec3 {
-    out := cast(^Vec3) alloc(^return_scratch_alloc, sizeof Vec3);
+    out := cast(^Vec3) alloc(return_scratch_alloc, sizeof Vec3);
     out.x = v1.x + v2.x;
     out.y = v1.y + v2.y;
     out.z = v1.z + v2.z;
index c0b50b8d96611dc8ccc0b12688d465694f115496..68f21b102f5780dfe402f61ffabe90c12ae75fdc 100644 (file)
@@ -1048,7 +1048,6 @@ CHECK(memres, AstMemRes* memres) {
         }
 
         Type* memres_type = memres->type;
-        if (!type_is_compound(memres_type)) memres_type = memres_type->Pointer.elem;
 
         if (!types_are_compatible(memres_type, memres->initial_value->type)) {
             onyx_message_add(Msg_Type_Binop_Mismatch,
index 83cc6091084c9bdab456cdbdb1d3c5e1e660bb78..34573b4c47748e01d46dbffeaed6b24bc5ada141 100644 (file)
@@ -338,12 +338,13 @@ static void symres_function(AstFunction* func) {
         symbol_introduce(semstate.curr_scope, param->token, (AstNode *) param);
 
         if (param->flags & Ast_Flag_Param_Use) {
-            if (param->type->kind != Type_Kind_Pointer || param->type->Pointer.elem->kind != Type_Kind_Struct) {
-                onyx_message_add(Msg_Type_Literal,
-                        param->token->pos,
-                        "can only 'use' pointers to structures.");
-            } else {
-                AstStructType* st = (AstStructType *) ((AstPointerType *) param->type_node)->elem;
+            if (type_is_struct(param->type)) {
+                AstStructType* st;
+                if (param->type->kind == Type_Kind_Struct) {
+                    st = (AstStructType *) param->type_node;
+                } else {
+                    st = (AstStructType *) ((AstPointerType *) param->type_node)->elem;
+                }
 
                 bh_arr_each(AstStructMember *, mem, st->members) {
                     AstFieldAccess* fa = onyx_ast_node_new(semstate.node_allocator, sizeof(AstFieldAccess), Ast_Kind_Field_Access);
@@ -358,6 +359,11 @@ static void symres_function(AstFunction* func) {
                             (AstNode *) fa);
                     token_toggle_end((*mem)->token);
                 }
+
+            } else {
+                onyx_message_add(Msg_Type_Literal,
+                        param->token->pos,
+                        "can only 'use' structures or pointers to structures.");
             }
         }
     }
@@ -478,26 +484,6 @@ static void symres_memres(AstMemRes** memres) {
     } else {
         if ((*memres)->type_node == NULL) return;
     }
-
-    (*memres)->type = type_build_from_ast(semstate.allocator, (*memres)->type_node);
-
-    if (!type_is_compound((*memres)->type)) {
-        Type* ptr_type = type_make_pointer(semstate.allocator, (*memres)->type);
-        (*memres)->type = ptr_type;
-
-        AstMemRes* new_memres = onyx_ast_node_new(semstate.node_allocator, sizeof(AstMemRes), Ast_Kind_Memres);
-        memcpy(new_memres, (*memres), sizeof(AstMemRes));
-
-        // BIG HACK: converting the (*memres) node to a dereference node to not break
-        // already resolved symbols
-        ((AstDereference *) (*memres))->kind = Ast_Kind_Dereference;
-        ((AstDereference *) (*memres))->type_node = (*memres)->type_node;
-        ((AstDereference *) (*memres))->type = (*memres)->type->Pointer.elem;
-        ((AstDereference *) (*memres))->expr = (AstTyped *) new_memres;
-
-        // BUT retain the 'old' memres in the entity list
-        *memres = new_memres;
-    }
 }
 
 void onyx_resolve_symbols() {
index debefaae792696dd88f847e005bf4cefe08aac0b..822480c5249e13b6d5e4c1737695a126c057fb7e 100644 (file)
@@ -343,31 +343,32 @@ static u64 local_lookup_idx(LocalAllocator* la, u64 value) {
 #define WIL(instr, data) bh_arr_push(code, ((WasmInstruction){ instr, { .l = data } }))
 #define COMPILE_FUNC(kind, ...) static void compile_ ## kind (OnyxWasmModule* mod, bh_arr(WasmInstruction)* pcode, __VA_ARGS__)
 
-COMPILE_FUNC(function_body,         AstFunction* fd);
-COMPILE_FUNC(block,                 AstBlock* block, b32 generate_block_headers);
-COMPILE_FUNC(statement,             AstNode* stmt);
-COMPILE_FUNC(assignment,            AstBinaryOp* assign);
-COMPILE_FUNC(store_instruction,     Type* type, u32 offset);
-COMPILE_FUNC(load_instruction,      Type* type, u32 offset);
-COMPILE_FUNC(if,                    AstIf* if_node);
-COMPILE_FUNC(while,                 AstWhile* while_node);
-COMPILE_FUNC(for,                   AstFor* for_node);
-COMPILE_FUNC(defer,                 AstDefer* defer);
-COMPILE_FUNC(deferred_stmts,        AstNode* node);
-COMPILE_FUNC(binop,                 AstBinaryOp* binop);
-COMPILE_FUNC(unaryop,               AstUnaryOp* unop);
-COMPILE_FUNC(call,                  AstCall* call);
-COMPILE_FUNC(intrinsic_call,        AstIntrinsicCall* call);
-COMPILE_FUNC(array_access_location, AstArrayAccess* aa, u64* offset_return);
-COMPILE_FUNC(field_access_location, AstFieldAccess* field, u64* offset_return);
-COMPILE_FUNC(local_location,        AstLocal* local, u64* offset_return);
-COMPILE_FUNC(struct_load,           Type* type, u64 offset);
-COMPILE_FUNC(struct_store,          AstTyped* lval);
-COMPILE_FUNC(expression,            AstTyped* expr);
-COMPILE_FUNC(cast,                  AstUnaryOp* cast);
-COMPILE_FUNC(return,                AstReturn* ret);
-COMPILE_FUNC(stack_enter,           u64 stacksize);
-COMPILE_FUNC(stack_leave,           u32 unused);
+COMPILE_FUNC(function_body,                 AstFunction* fd);
+COMPILE_FUNC(block,                         AstBlock* block, b32 generate_block_headers);
+COMPILE_FUNC(statement,                     AstNode* stmt);
+COMPILE_FUNC(assignment,                    AstBinaryOp* assign);
+COMPILE_FUNC(store_instruction,             Type* type, u32 offset);
+COMPILE_FUNC(load_instruction,              Type* type, u32 offset);
+COMPILE_FUNC(if,                            AstIf* if_node);
+COMPILE_FUNC(while,                         AstWhile* while_node);
+COMPILE_FUNC(for,                           AstFor* for_node);
+COMPILE_FUNC(defer,                         AstDefer* defer);
+COMPILE_FUNC(deferred_stmts,                AstNode* node);
+COMPILE_FUNC(binop,                         AstBinaryOp* binop);
+COMPILE_FUNC(unaryop,                       AstUnaryOp* unop);
+COMPILE_FUNC(call,                          AstCall* call);
+COMPILE_FUNC(intrinsic_call,                AstIntrinsicCall* call);
+COMPILE_FUNC(array_access_location,         AstArrayAccess* aa, u64* offset_return);
+COMPILE_FUNC(field_access_location,         AstFieldAccess* field, u64* offset_return);
+COMPILE_FUNC(local_location,                AstLocal* local, u64* offset_return);
+COMPILE_FUNC(memory_reservation_location,   AstMemRes* memres);
+COMPILE_FUNC(struct_load,                   Type* type, u64 offset);
+COMPILE_FUNC(struct_store,                  AstTyped* lval);
+COMPILE_FUNC(expression,                    AstTyped* expr);
+COMPILE_FUNC(cast,                          AstUnaryOp* cast);
+COMPILE_FUNC(return,                        AstReturn* ret);
+COMPILE_FUNC(stack_enter,                   u64 stacksize);
+COMPILE_FUNC(stack_leave,                   u32 unused);
 
 COMPILE_FUNC(function_body, AstFunction* fd) {
     if (fd->body == NULL) return;
@@ -1002,6 +1003,9 @@ COMPILE_FUNC(array_access_location, AstArrayAccess* aa, u64* offset_return) {
     } else if (aa->addr->kind == Ast_Kind_Local
         && aa->addr->type->kind == Type_Kind_Array) {
         compile_local_location(mod, &code, (AstLocal *) aa->addr, &offset);
+    } else if (aa->addr->kind == Ast_Kind_Memres
+        && aa->addr->type->kind != Type_Kind_Array) {
+        compile_memory_reservation_location(mod, &code, (AstMemRes *) aa->addr);
     } else {
         compile_expression(mod, &code, aa->addr);
     }
@@ -1033,6 +1037,9 @@ COMPILE_FUNC(field_access_location, AstFieldAccess* field, u64* offset_return) {
         u64 o2 = 0;
         compile_local_location(mod, &code, (AstLocal *) source_expr, &o2);
         offset += o2;
+    } else if (source_expr->kind == Ast_Kind_Memres
+        && source_expr->type->kind != Type_Kind_Pointer) {
+        compile_memory_reservation_location(mod, &code, (AstMemRes *) source_expr);
     } else {
         compile_expression(mod, &code, source_expr);
     }
@@ -1042,6 +1049,14 @@ COMPILE_FUNC(field_access_location, AstFieldAccess* field, u64* offset_return) {
     *pcode = code;
 }
 
+COMPILE_FUNC(memory_reservation_location, AstMemRes* memres) {
+    bh_arr(WasmInstruction) code = *pcode;
+
+    WID(WI_I32_CONST, memres->addr);
+
+    *pcode = code;
+}
+
 COMPILE_FUNC(local_location, AstLocal* local, u64* offset_return) {
     bh_arr(WasmInstruction) code = *pcode;
 
@@ -1328,6 +1343,7 @@ COMPILE_FUNC(expression, AstTyped* expr) {
         case Ast_Kind_Memres: {
             AstMemRes* memres = (AstMemRes *) expr;
             WID(WI_I32_CONST, memres->addr);
+            compile_load_instruction(mod, &code, memres->type, 0);
             break;
         }
 
@@ -1658,16 +1674,17 @@ static void compile_function(OnyxWasmModule* mod, AstFunction* fd) {
         mod->local_alloc = &wasm_func.locals;
         mod->local_alloc->param_count = localidx;
 
-        mod->stack_base_idx = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
-
         mod->has_stack_locals = 0;
         bh_arr_each(AstLocal *, local, fd->locals)
             mod->has_stack_locals |= !local_is_wasm_local(*local);
 
-        if (mod->has_stack_locals)
+        if (mod->has_stack_locals) {
             // NOTE: '5' needs to match the number of instructions it takes
             // to setup a stack frame
             bh_arr_insert_end(wasm_func.code, 5);
+            mod->stack_base_idx = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
+        }
+
 
         // Generate code
         compile_function_body(mod, &wasm_func.code, fd);
@@ -1833,7 +1850,6 @@ static void compile_raw_data(OnyxWasmModule* mod, ptr data, AstTyped* node) {
 
 static void compile_memory_reservation(OnyxWasmModule* mod, AstMemRes* memres) {
     Type* effective_type = memres->type;
-    if (!type_is_compound(effective_type)) effective_type = effective_type->Pointer.elem;
 
     u64 alignment = type_alignment_of(effective_type);
     u64 size = type_size_of(effective_type);