From c7f9a9b2db509dd73f1e8089efce2e98e111b3d8 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 10 May 2022 22:23:42 -0500 Subject: [PATCH] procedure tag helpers; bug fixes --- core/container/array.onyx | 21 ++++++++++++---- core/runtime/info/proc_tags.onyx | 33 +++++++++++++++++++++++++ include/astnodes.h | 1 + src/builtins.c | 18 +++++++------- src/onyx.c | 41 +++++++++++++++++++++++--------- src/wasm_emit.c | 13 ++++++++-- src/wasm_type_table.h | 2 +- 7 files changed, 102 insertions(+), 27 deletions(-) diff --git a/core/container/array.onyx b/core/container/array.onyx index 3dd35b6d..8b96afbd 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -287,14 +287,14 @@ set :: (arr: [] $T, idx: i32, value: T) { } contains :: #match { - // Uses '==' to compare for equality. - (arr: [] $T, x: T) -> bool { - for it: arr do if it == x do return true; + macro (arr: [] $T, $cmp: Code) -> bool { + for it: arr do if #unquote cmp do return true; return false; }, - macro (arr: [] $T, $cmp: Code) -> bool { - for it: arr do if #unquote cmp do return true; + // Uses '==' to compare for equality. + (arr: [] $T, x: T) -> bool { + for it: arr do if it == x do return true; return false; } } @@ -508,6 +508,17 @@ first :: (arr: [] $T, predicate: (T) -> bool) -> ^T { return null; } +first_where :: macro (arr: [] $T, cond: Code) -> ^T { + // This is to preserve the semantics that "it" is + // not a pointer (same as contains) + for^ it_: arr { + it := *it_; + if #unquote cond do return it_; + } + + return null; +} + count_where :: #match { (arr: [] $T, predicate: (T) -> bool) -> u32 { count: u32 = 0; diff --git a/core/runtime/info/proc_tags.onyx b/core/runtime/info/proc_tags.onyx index 0c8c4336..3b311224 100644 --- a/core/runtime/info/proc_tags.onyx +++ b/core/runtime/info/proc_tags.onyx @@ -10,3 +10,36 @@ Tagged_Procedure :: struct { type: type_expr; tags: [] any; } + +get_tags_for_procedure :: (func: $T) -> [] any { + if get_type_info(T).kind != .Function do return .[]; + + for tagged_procedures { + if it.func == func do return it.tags; + } + + return .[]; +} + +#local GPWT_Result :: struct (T: type_expr) { + func: () -> void; + type: type_expr; + tag : ^T; +} + +get_procedures_with_tag :: ($tag_type: type_expr) -> [] GPWT_Result(tag_type) { + array :: package core.array + results := make([..] GPWT_Result(tag_type)); + + for proc: tagged_procedures { + if tag := array.first_where(proc.tags, #(it.type == tag_type)); tag != null { + array.push(^results, .{ + func = proc.func, + type = proc.type, + tag = cast(^tag_type) tag.data + }); + } + } + + return results; +} diff --git a/include/astnodes.h b/include/astnodes.h index 16acbc5f..d0cdca9f 100644 --- a/include/astnodes.h +++ b/include/astnodes.h @@ -1615,6 +1615,7 @@ extern IntrinsicTable intrinsic_table; extern bh_arr(OverloadOption) operator_overloads[Binary_Op_Count]; void initialize_builtins(bh_allocator a); +void initalize_special_globals(); void introduce_build_options(bh_allocator a); diff --git a/src/builtins.c b/src/builtins.c index 9e9b16c5..7f246141 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -458,14 +458,6 @@ void initialize_builtins(bh_allocator a) { bh_arr_new(global_heap_allocator, init_procedures, 4); - p = package_lookup("runtime.info"); - if (p != NULL) { - type_table_node = (AstTyped *) symbol_raw_resolve(p->scope, "type_table"); - foreign_blocks_node = (AstTyped *) symbol_raw_resolve(p->scope, "foreign_blocks"); - foreign_block_type = (AstType *) symbol_raw_resolve(p->scope, "foreign_block"); - tagged_procedures_node = (AstTyped *) symbol_raw_resolve(p->scope, "tagged_procedures"); - } - fori (i, 0, Binary_Op_Count) { bh_arr_new(global_heap_allocator, operator_overloads[i], 4); } @@ -477,6 +469,16 @@ void initialize_builtins(bh_allocator a) { } } +void initalize_special_globals() { + Package *p = package_lookup("runtime.info"); + if (p != NULL) { + type_table_node = (AstTyped *) symbol_raw_resolve(p->scope, "type_table"); + foreign_blocks_node = (AstTyped *) symbol_raw_resolve(p->scope, "foreign_blocks"); + foreign_block_type = (AstType *) symbol_raw_resolve(p->scope, "foreign_block"); + tagged_procedures_node = (AstTyped *) symbol_raw_resolve(p->scope, "tagged_procedures"); + } +} + void introduce_build_options(bh_allocator a) { Package* p = package_lookup_or_create("runtime", context.global_scope, a); diff --git a/src/onyx.c b/src/onyx.c index 69dd81aa..1275ae23 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -223,6 +223,12 @@ static AstInclude* create_load(bh_allocator alloc, char* filename) { return include_node; } +// HACK +static u32 special_global_entities_remaining = 3; +static Entity *runtime_info_types_entity; +static Entity *runtime_info_foreign_entity; +static Entity *runtime_info_proc_tags_entity; + static void context_init(CompileOptions* opts) { types_init(); @@ -262,26 +268,26 @@ static void context_init(CompileOptions* opts) { .state = Entity_State_Parse_Builtin, .type = Entity_Type_Load_File, .package = NULL, - .include = create_load(context.ast_alloc, "core/runtime/info/types"), + .include = create_load(context.ast_alloc, "core/runtime/build_opts"), })); - entity_heap_insert(&context.entities, ((Entity) { - .state = Entity_State_Parse_Builtin, + + 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/foreign_blocks"), + .include = create_load(context.ast_alloc, "core/runtime/info/types"), })); - entity_heap_insert(&context.entities, ((Entity) { - .state = Entity_State_Parse_Builtin, + 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/proc_tags"), + .include = create_load(context.ast_alloc, "core/runtime/info/foreign_blocks"), })); - - entity_heap_insert(&context.entities, ((Entity) { - .state = Entity_State_Parse_Builtin, + 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/build_opts"), + .include = create_load(context.ast_alloc, "core/runtime/info/proc_tags"), })); add_entities_for_node(NULL, (AstNode *) &builtin_stack_top, context.global_scope, NULL); @@ -457,7 +463,20 @@ static b32 process_entity(Entity* ent) { introduce_build_options(context.ast_alloc); } + // GROSS + if (special_global_entities_remaining == 0) { + special_global_entities_remaining--; + initalize_special_globals(); + } + if (process_load_entity(ent)) { + // GROSS + if (ent == runtime_info_types_entity + || ent == runtime_info_proc_tags_entity + || ent == runtime_info_foreign_entity) { + special_global_entities_remaining--; + } + ent->state = Entity_State_Finalized; } else { ent->macro_attempts++; diff --git a/src/wasm_emit.c b/src/wasm_emit.c index a20c7e7a..8fde864b 100644 --- a/src/wasm_emit.c +++ b/src/wasm_emit.c @@ -28,6 +28,10 @@ static WasmType onyx_type_to_wasm_type(Type* type) { if (type->kind == Type_Kind_Struct) { + if (type_linear_member_count(type) == 1) { + return onyx_type_to_wasm_type(type->Struct.linear_members[0].type); + } + return WASM_TYPE_VOID; } @@ -35,6 +39,10 @@ static WasmType onyx_type_to_wasm_type(Type* type) { return WASM_TYPE_VOID; } + if (type->kind == Type_Kind_Compound) { + return WASM_TYPE_VOID; + } + if (type->kind == Type_Kind_Enum) { return onyx_type_to_wasm_type(type->Enum.backing); } @@ -3327,8 +3335,9 @@ static i32 generate_type_idx(OnyxWasmModule* mod, Type* ft) { type->return_type = return_type; type->param_count = param_count; - // HACK ish thing - memcpy(type->param_types, type_repr_buf, type->param_count); + fori (i, 0, type->param_count) { + type->param_types[i] = type_repr_buf[i]; + } bh_arr_push(mod->types, type); diff --git a/src/wasm_type_table.h b/src/wasm_type_table.h index 0b13fe77..d92537d7 100644 --- a/src/wasm_type_table.h +++ b/src/wasm_type_table.h @@ -828,7 +828,7 @@ static u64 build_tagged_procedures(OnyxWasmModule *module) { } if (context.options->verbose_output == 1) { - bh_printf("Foreign blocks size: %d bytes.\n", tag_proc_buffer.length); + bh_printf("Tagged procedure size: %d bytes.\n", tag_proc_buffer.length); } u32 offset = module->next_datum_offset; -- 2.25.1