cleanup and bugfixes
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 29 Jun 2022 04:05:15 +0000 (23:05 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 29 Jun 2022 04:05:15 +0000 (23:05 -0500)
core/builtin.onyx
core/std.onyx
src/onyx.c
src/wasm_emit.c
tests/parallel_for.onyx

index e90b31662d56f420d20c913ea126dfdd4b562a02..5977704ded1b8cb95f88cd9ecd0b6c2de11e6f4e 100644 (file)
@@ -134,87 +134,92 @@ calloc  :: (size: u32) -> rawptr do return raw_alloc(context.allocator, size);
 cresize :: (ptr: rawptr, size: u32) -> rawptr do return raw_resize(context.allocator, ptr, size);
 cfree   :: (ptr: rawptr) do raw_free(context.allocator, ptr);
 
-new :: #match {
-    ($T: type_expr, allocator := context.allocator) -> ^T {
-        use package core.intrinsics.onyx { __initialize }
-        memory :: package core.memory
-
-        res := cast(^T) raw_alloc(allocator, sizeof T);
-        memory.set(res, 0, sizeof T);
-        __initialize(res);
-
-        return res;
-    },
-
-    (T: type_expr, allocator := context.allocator) -> rawptr {
-        memory :: package core.memory
-        type_info :: package runtime.info
-
-        info := type_info.get_type_info(T);
-        size := type_info.size_of(T);
-        if size == 0 do return null;
-
-        res := raw_alloc(allocator, size);
-        memory.set(res, 0, size);
-
-        if info.kind == .Struct {
-            s_info := cast(^type_info.Type_Info_Struct) info;
-            for s_info.members {
-                if it.default != null {
-                    member_size := type_info.size_of(it.type);
-                    memory.copy(cast(^u8) res + it.offset, it.default, member_size);
+//
+// This cannot be used in a custom runtime, as the other core
+// packages are not included.
+#if runtime.runtime != .Custom {
+    new :: #match {
+        ($T: type_expr, allocator := context.allocator) -> ^T {
+            use package core.intrinsics.onyx { __initialize }
+            memory :: package core.memory
+
+            res := cast(^T) raw_alloc(allocator, sizeof T);
+            memory.set(res, 0, sizeof T);
+            __initialize(res);
+
+            return res;
+        },
+
+        (T: type_expr, allocator := context.allocator) -> rawptr {
+            memory :: package core.memory
+            type_info :: package runtime.info
+
+            info := type_info.get_type_info(T);
+            size := type_info.size_of(T);
+            if size == 0 do return null;
+
+            res := raw_alloc(allocator, size);
+            memory.set(res, 0, size);
+
+            if info.kind == .Struct {
+                s_info := cast(^type_info.Type_Info_Struct) info;
+                for s_info.members {
+                    if it.default != null {
+                        member_size := type_info.size_of(it.type);
+                        memory.copy(cast(^u8) res + it.offset, it.default, member_size);
+                    }
                 }
             }
-        }
 
-        return res;
+            return res;
+        }
     }
-}
 
-make :: #match {
-    macro ($T: type_expr, allocator := context.allocator) => {
-        return __make_overload(cast(^T) null, allocator=allocator);
-    },
+    make :: #match {
+        macro ($T: type_expr, allocator := context.allocator) => {
+            return __make_overload(cast(^T) null, allocator=allocator);
+        },
 
-    macro ($T: type_expr, n: u32, allocator := context.allocator) => {
-        return __make_overload(cast(^T) null, n, allocator=allocator);
-    },
-}
+        macro ($T: type_expr, n: u32, allocator := context.allocator) => {
+            return __make_overload(cast(^T) null, n, allocator=allocator);
+        },
+    }
 
-//
-// This is a rather unique way of using the type matching system
-// to select an overload. What is desired here is that when you say:
-//
-//    make(Foo)
-//
-// You match the overload for make that is designed for making a Foo.
-// However, you cannot use the type matching system to match by value.
-// In order to get around this, `make` will pass a null pointer to this
-// match procedure, that is casted to be a *pointer* to the desired type.
-// Therefore, if you want to add your own make overload, you have to add
-// a match to `__make_overload` that takes a *pointer* to the desired
-// type as the first argument, and then an allocator as the second.
-// Optionally, you can take a parameter between them that is an integer,
-// useful when constructing things like arrays.
-//
-// See core/container/array.onyx for an example.
-//
-__make_overload :: #match {
     //
-    // This is the fallback option for make. It simply allocates a zero-intialized
-    // element of type T.
-    #precedence 1000 (_: ^$T, allocator := context.allocator) -> ^T {
-        memory :: package core.memory
-
-        res := cast(^T) raw_alloc(allocator, sizeof T);
-        memory.set(res, 0, sizeof T);
-        return res;
-    },
-}
+    // This is a rather unique way of using the type matching system
+    // to select an overload. What is desired here is that when you say:
+    //
+    //    make(Foo)
+    //
+    // You match the overload for make that is designed for making a Foo.
+    // However, you cannot use the type matching system to match by value.
+    // In order to get around this, `make` will pass a null pointer to this
+    // match procedure, that is casted to be a *pointer* to the desired type.
+    // Therefore, if you want to add your own make overload, you have to add
+    // a match to `__make_overload` that takes a *pointer* to the desired
+    // type as the first argument, and then an allocator as the second.
+    // Optionally, you can take a parameter between them that is an integer,
+    // useful when constructing things like arrays.
+    //
+    // See core/container/array.onyx for an example.
+    //
+    __make_overload :: #match {
+        //
+        // This is the fallback option for make. It simply allocates a zero-intialized
+        // element of type T.
+        #precedence 1000 (_: ^$T, allocator := context.allocator) -> ^T {
+            memory :: package core.memory
+
+            res := cast(^T) raw_alloc(allocator, sizeof T);
+            memory.set(res, 0, sizeof T);
+            return res;
+        },
+    }
 
-delete :: #match {
-    #precedence 1000 macro (x: ^$T, allocator := context.allocator) {
-        raw_free(allocator, x);
+    delete :: #match {
+        #precedence 1000 macro (x: ^$T, allocator := context.allocator) {
+            raw_free(allocator, x);
+        }
     }
 }
 
index fe416ce73a10dc9fd7da785bb18d23815632f33c..15afa5298dbfb9906a2d86766ef19a0feb498ef3 100644 (file)
@@ -34,9 +34,6 @@ package core
 
 #load "./runtime/build_opts"
 #load "./runtime/common"
-
-#load "./runtime/info/helper"
-
 #load "./arg_parse"
 
 #local runtime :: package runtime
@@ -65,7 +62,10 @@ package core
     #load "./wasi/wasi_fs"
 }
 #if runtime.runtime == .Js     { #load "./runtime/js" }
-#if runtime.runtime != .Custom { #load "./stdio" }
+#if runtime.runtime != .Custom {
+    #load "./runtime/info/helper"
+    #load "./stdio"
+}
 
 #if runtime.Multi_Threading_Enabled {
     #load "./intrinsics/atomics"
index 13f80d2bd7b5e4b51ed48df1d5e6cb73c63466d3..6432a985d62c7f2beba6e613714bb44fd841ac39 100644 (file)
@@ -276,27 +276,30 @@ static void context_init(CompileOptions* opts) {
         .include = create_load(context.ast_alloc, "core/runtime/build_opts"),
     }));
 
-    runtime_info_types_entity = entity_heap_insert(&context.entities, ((Entity) {
-        .state = Entity_State_Parse,
-        .type = Entity_Type_Load_File,
-        .package = NULL,
-        .include = create_load(context.ast_alloc, "core/runtime/info/types"),
-    }));
-    runtime_info_foreign_entity = entity_heap_insert(&context.entities, ((Entity) {
-        .state = Entity_State_Parse,
-        .type = Entity_Type_Load_File,
-        .package = NULL,
-        .include = create_load(context.ast_alloc, "core/runtime/info/foreign_blocks"),
-    }));
-    runtime_info_proc_tags_entity = entity_heap_insert(&context.entities, ((Entity) {
-        .state = Entity_State_Parse,
-        .type = Entity_Type_Load_File,
-        .package = NULL,
-        .include = create_load(context.ast_alloc, "core/runtime/info/proc_tags"),
-    }));
+    if (context.options->runtime != Runtime_Custom) {
+        runtime_info_types_entity = entity_heap_insert(&context.entities, ((Entity) {
+            .state = Entity_State_Parse,
+            .type = Entity_Type_Load_File,
+            .package = NULL,
+            .include = create_load(context.ast_alloc, "core/runtime/info/types"),
+        }));
+        runtime_info_foreign_entity = entity_heap_insert(&context.entities, ((Entity) {
+            .state = Entity_State_Parse,
+            .type = Entity_Type_Load_File,
+            .package = NULL,
+            .include = create_load(context.ast_alloc, "core/runtime/info/foreign_blocks"),
+        }));
+        runtime_info_proc_tags_entity = entity_heap_insert(&context.entities, ((Entity) {
+            .state = Entity_State_Parse,
+            .type = Entity_Type_Load_File,
+            .package = NULL,
+            .include = create_load(context.ast_alloc, "core/runtime/info/proc_tags"),
+        }));
+    }
 
     add_entities_for_node(NULL, (AstNode *) &builtin_stack_top, context.global_scope, NULL);
     add_entities_for_node(NULL, (AstNode *) &builtin_tls_base, context.global_scope, NULL);
+    add_entities_for_node(NULL, (AstNode *) &builtin_tls_size, context.global_scope, NULL);
 
     // NOTE: Add all files passed by command line to the queue
     bh_arr_each(const char *, filename, opts->files) {
index bfe22faf7807ddc9aa2468923eadaa3a1b71f8f5..179caf44aa01e402e9b253ec6410094e737e4e71 100644 (file)
@@ -3546,8 +3546,10 @@ static void emit_global(OnyxWasmModule* module, AstGlobal* global) {
 
     if (global == &builtin_stack_top)
         module->stack_top_ptr = &module->globals[global_idx].initial_value[0].data.i1;
-    if (global == &builtin_tls_size)
-        module->tls_size_ptr  = &module->globals[global_idx].initial_value[0].data.i1;
+
+    if (global == &builtin_tls_size) {
+        module->globals[global_idx].initial_value[0].data.i1 =  module->next_tls_offset;
+    }
 }
 
 static void emit_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) {
@@ -3896,6 +3898,7 @@ OnyxWasmModule onyx_wasm_module_create(bh_allocator alloc) {
                                  // break constant data.       - brendanfh 2020/12/16
 
         .next_tls_offset = 0,
+        .tls_size_ptr = NULL,
 
         .elems = NULL,
         .next_elem_idx = 0,
index a827bcfdc65807c927f75b647ba77476eab9ad43..2000984030acf08751cf4ac14b62d4afc8dc3437 100644 (file)
@@ -2,14 +2,13 @@
 
 use package core
 
-main :: (args) => {
+main :: () {
     sum := 0;
 
-    thread_data: struct {
-        sum : ^i32; 
-        mutex: sync.Mutex;
+    thread_data := .{
+        sum = ^sum,
+        mutex = sync.Mutex.{},
     };
-    thread_data.sum = ^sum;
     sync.mutex_init(^thread_data.mutex);
 
     iter.parallel_for(0 .. 10000, 4, ^thread_data) {