procedure tag helpers; bug fixes
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 11 May 2022 03:23:42 +0000 (22:23 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 11 May 2022 03:23:42 +0000 (22:23 -0500)
core/container/array.onyx
core/runtime/info/proc_tags.onyx
include/astnodes.h
src/builtins.c
src/onyx.c
src/wasm_emit.c
src/wasm_type_table.h

index 3dd35b6d24ee9f25a9bac7c7103c2d41e7f4f586..8b96afbdf011137d6091fa3697deed9bef35b870 100644 (file)
@@ -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;
index 0c8c4336618d06178e707a53fd67259f092bd205..3b3112247f6b3f75eb5ac24fde51a9109d5c613a 100644 (file)
@@ -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;
+}
index 16acbc5f4f45ad3977aa46c098eb08229628e1a0..d0cdca9f20bab6b47e08f1e3d98c35cbb09092cf 100644 (file)
@@ -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);
 
 
index 9e9b16c5d4c5d27e733f9543fe6d793a12d2a170..7f24614146d7e817d9b1867a56c0bfa60cc26ea1 100644 (file)
@@ -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);
 
index 69dd81aa47f92a4f40d9f6eea54329e945917f8a..1275ae23138abcaf4aa198bf04adbef7b0f6db81 100644 (file)
@@ -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++;
index a20c7e7afdcefb42d4c5f5cc8cedfda3c039bae7..8fde864bb33064e49cc872e6c31c8aa1d2f13ba5 100644 (file)
 
 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);
 
index 0b13fe7739c80ae5ab04698938a650cce1d9b3d5..d92537d7de7b6a2ffd0ab16ca7f24f0ee0821a4b 100644 (file)
@@ -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;