From 491b34624c7ed68879669509ba45944c5aa1afae Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 28 Jun 2022 23:05:15 -0500 Subject: [PATCH] cleanup and bugfixes --- core/builtin.onyx | 149 +++++++++++++++++++++------------------- core/std.onyx | 8 +-- src/onyx.c | 39 ++++++----- src/wasm_emit.c | 7 +- tests/parallel_for.onyx | 9 ++- 5 files changed, 111 insertions(+), 101 deletions(-) diff --git a/core/builtin.onyx b/core/builtin.onyx index e90b3166..5977704d 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -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); + } } } diff --git a/core/std.onyx b/core/std.onyx index fe416ce7..15afa529 100644 --- a/core/std.onyx +++ b/core/std.onyx @@ -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" diff --git a/src/onyx.c b/src/onyx.c index 13f80d2b..6432a985 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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) { diff --git a/src/wasm_emit.c b/src/wasm_emit.c index bfe22faf..179caf44 100644 --- a/src/wasm_emit.c +++ b/src/wasm_emit.c @@ -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, diff --git a/tests/parallel_for.onyx b/tests/parallel_for.onyx index a827bcfd..20009840 100644 --- a/tests/parallel_for.onyx +++ b/tests/parallel_for.onyx @@ -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) { -- 2.25.1