Testing language features; small bugfixes
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 30 Jul 2020 03:41:08 +0000 (22:41 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 30 Jul 2020 03:41:08 +0000 (22:41 -0500)
.vimspector.json
onyx
progs/alloc.onyx
progs/alloc_test.onyx
src/onyxchecker.c
src/onyxparser.c
src/onyxwasm.c

index d4b26ba989abfdc90c1fe38a05eee36162d5d5ca..343544d8da763434d1a4109c557bec9e96054430 100644 (file)
@@ -6,7 +6,7 @@
                 "type": "cppdbg",
                 "request": "launch",
                 "program": "${workspaceFolder}/onyx",
-                "args": ["progs/fcf.onyx"],
+                "args": ["progs/alloc_test.onyx"],
                 "stopAtEntry": true,
                 "cwd": "${workspaceFolder}",
                 "environment": [],
diff --git a/onyx b/onyx
index 10d3bbe2ac6f73e0d98adb162947ad2927080364..b7b0cae59a832202f5d6df92aa18a7b5d34c4fd5 100755 (executable)
Binary files a/onyx and b/onyx differ
index 064665e31a761a3b79b7fd89e4158b224eaa4353..7ca0b8504236d0c3771a3befe750998052f09e65 100644 (file)
@@ -1,4 +1,4 @@
-package alloc
+package memory
 
 use "progs/intrinsics"
 
@@ -11,6 +11,35 @@ use package intrinsics {
 // Need to define this somewhere
 null :: cast(rawptr) 0;
 
+AllocAction :: enum {
+    Alloc;
+    Free;
+    Resize;
+}
+
+Allocator :: struct {
+    data: rawptr;
+    func: alloc_proc; 
+}
+
+alloc_proc :: #type proc (rawptr, AllocAction, u32, u32, rawptr) -> rawptr;
+
+alloc :: proc (a: ^Allocator, size: u32) -> rawptr {
+    return a.func(a.data, AllocAction.Alloc, size, 16, null);
+}
+
+free :: proc (a: ^Allocator, ptr: rawptr) {
+    a.func(a.data, AllocAction.Free, 0, 0, ptr);
+}
+
+
+
+
+
+
+
+heap_allocator : Allocator;
+
 heap_state : struct {
     free_list       : ^heap_block;
     next_alloc      : rawptr;
@@ -18,7 +47,7 @@ heap_state : struct {
 }
 
 heap_block :: struct {
-    size : i32;
+    size : u32;
     next : ^heap_block;
 }
 
@@ -26,16 +55,17 @@ heap_init :: proc {
     heap_state.free_list = null;
     heap_state.next_alloc = __heap_start;
     heap_state.remaining_space = (memory_size() << 16) - cast(u32) __heap_start;
-}
 
-heap_align_to :: 16
+    heap_allocator.data = null;
+    heap_allocator.func = heap_alloc_proc;
+}
 
-heap_alloc :: proc (size_: i32) -> rawptr {
+heap_alloc :: proc (size_: u32, align: u32) -> rawptr {
     if size_ == 0 return null;
 
     size := size_ + sizeof heap_block;
-    if size % heap_align_to != 0 {
-        size += heap_align_to - (size % heap_align_to);
+    if size % align != 0 {
+        size += align - (size % align);
     }
 
     prev := ^heap_state.free_list;
@@ -62,13 +92,36 @@ heap_alloc :: proc (size_: i32) -> rawptr {
 
         return cast(rawptr) (cast(u32) ret + sizeof heap_block);
     }
-    
-    // grow the memory with memory_grow
-    return cast(rawptr) -1;
+
+    new_pages :: (size - heap_state.remaining_space) >> 16;
+    if memory_grow(new_pages) == -1 {
+        // out of memory    
+        return null;
+    }
+    heap_state.remaining_space += new_pages << 16;
+
+    ret := cast(^heap_block) heap_state.next_alloc;
+    ret.size = size;
+    ret.next = null;
+
+    heap_state.next_alloc = cast(rawptr) (cast(u32) heap_state.next_alloc + size);
+    heap_state.remaining_space -= size;
+
+    return cast(rawptr) (cast(u32) ret + sizeof heap_block);
 }
 
 heap_free :: proc (ptr: rawptr) {
     hb_ptr := cast(^heap_block) (cast(u32) ptr - sizeof heap_block);
     hb_ptr.next = heap_state.free_list;
     heap_state.free_list = hb_ptr;
-}
\ No newline at end of file
+}
+
+heap_alloc_proc :: proc (data: rawptr, aa: AllocAction, size: u32, align: u32, oldptr: rawptr) -> rawptr {
+    if aa == AllocAction.Alloc return heap_alloc(size, align);
+    if aa == AllocAction.Free {
+        heap_free(oldptr);
+        return null;
+    }
+
+    return null;
+}
index 72fa5af8413b45505ab1423fb3416b4f9431f86c..212e1fa1104ca25623fc1c577e927ba14e6a19dc 100644 (file)
@@ -1,17 +1,17 @@
 use "progs/alloc"
 use "progs/print_funcs"
 
-use package alloc
+use package memory
 use package printing
 
 proc #export "main" {
        asdf :: "staring asdfkjasd asdflkjasdflkajsdflk";
        heap_init();
 
-       first := cast([] i32) heap_alloc(sizeof [4] i32);
+       first := cast([] i32) alloc(^heap_allocator, sizeof [4] i32);
        for i: 0, 4 first[i] = i * 2;
 
-       second := cast([] f32) heap_alloc(sizeof [24] f32);
+       second := cast([] f32) alloc(^heap_allocator, sizeof [24] f32);
        for i: 0, 24 second[i] = cast(f32) i;
 
        print(cast(u32) first);
@@ -20,22 +20,19 @@ proc #export "main" {
        for i: 0, 4 print(first[i]);
        for i: 0, 24 print(second[i]);
 
-       heap_free(first);
+       free(^heap_allocator, first);
 
-       third := cast(^i32) heap_alloc(sizeof i32);
+       third := cast(^i32) alloc(^heap_allocator, sizeof i32);
 
        print(cast(u32) third);
        *third = 1234;
        print(*third);
 
-       heap_free(second);
+       free(^heap_allocator, second);
 
-       fourth := cast([] i32) heap_alloc(sizeof [128]i32);
+       fourth := cast([] i32) alloc(^heap_allocator, sizeof [128]i32);
        print(cast(u32) fourth);
 
-       fifth := cast(^i32) heap_alloc(sizeof i32);
+       fifth := cast(^i32) alloc(^heap_allocator, sizeof i32);
        print(cast(u32) fifth);
-
-       sixth := heap_alloc(8 << 16);
-       print(cast(u32) sixth);
 }
\ No newline at end of file
index 0bf8eadedf0edf83c88eaa974f06176cc8b1525d..c170994be11c3a444a8107b591c41c9cbf04632f 100644 (file)
@@ -467,7 +467,8 @@ CHECK(address_of, AstAddressOf* aof) {
 
     if (aof->expr->kind != Ast_Kind_Array_Access
             && aof->expr->kind != Ast_Kind_Dereference
-            && aof->expr->kind != Ast_Kind_Field_Access) {
+            && aof->expr->kind != Ast_Kind_Field_Access
+            && aof->expr->kind != Ast_Kind_Memres) {
         onyx_message_add(Msg_Type_Literal,
                 aof->token->pos,
                 "cannot take the address of this");
index c439108a8e6d0a3c0d8f79d0fad49f798f8533c5..583c5993652d5d1722cc351376e86bf65052f93e 100644 (file)
@@ -1312,6 +1312,10 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) {
                             "%b", symbol->text, symbol->length);
                     }
 
+                    if (node->kind == Ast_Kind_Type_Alias) {
+                        node->token = symbol;
+                    }
+
                     // HACK
                     add_node_to_process(parser, (AstNode *) node);
                 }
index 687e27b6b73c0d66420c6dadabf07ed54cc56696..17c8b8d2872e22c1530b8c9eb2c85ea28d4c693a 100644 (file)
@@ -958,6 +958,12 @@ COMPILE_FUNC(expression, AstTyped* expr) {
                     break;
                 }
 
+                case Ast_Kind_Memres: {
+                    AstMemRes* memres = (AstMemRes *) aof->expr;
+                    WID(WI_I32_CONST, memres->addr);
+                    break;
+                }
+
                 default:
                     onyx_message_add(Msg_Type_Literal,
                             aof->token->pos,