working type table!
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 22 Jun 2021 20:01:41 +0000 (15:01 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 22 Jun 2021 20:01:41 +0000 (15:01 -0500)
bin/onyx
include/bh.h
include/onyxastnodes.h
src/onyx.c
src/onyxbuiltins.c
src/onyxwasm.c
src/onyxwasm_type_table.c

index 7a061c3bc6f9d30bc3fce96e76b79e1029ef5c8b..410ed5f91f8b1cbed6a556348701ea53ba31599d 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 742034c575383411defb91b60a0448a3c114266a..a3a56f19ce10a1b8a2d394caff74b61395dcb02e 100644 (file)
@@ -1963,7 +1963,7 @@ void bh_buffer_write_u32(bh_buffer* buffer, u32 i) {
 
 void bh_buffer_write_u64(bh_buffer* buffer, u64 i) {
     bh_buffer_grow(buffer, buffer->length + 8);
-    *((u8 *) bh_pointer_add(buffer->data, buffer->length)) = i;
+    *((u64 *) bh_pointer_add(buffer->data, buffer->length)) = i;
     buffer->length += 8;
 }
 
index 5a7bd3dbf2866af068e8aac35f156a6df241fb3f..5213bcae0e8b1a3236b6b4f807aa15bc419deb48 100644 (file)
@@ -1204,6 +1204,7 @@ extern AstTyped *builtin_context_variable;
 extern AstType  *builtin_allocator_type;
 extern AstType  *builtin_iterator_type;
 extern AstType  *builtin_callsite_type;
+extern AstTyped *type_table_node;
 
 typedef struct BuiltinSymbol {
     char*    package;
index d64b22c9727e93abe6f287dfeee14675172fe37d..c25bb89f4b40d04a55e322ea7ca3bdc7b9cc389c 100644 (file)
@@ -202,6 +202,13 @@ static void context_init(CompileOptions* opts) {
         .package = NULL,
         .include = create_load(context.ast_alloc, "core/builtin"),
     }));
+
+    entity_heap_insert(&context.entities, ((Entity) {
+        .state = Entity_State_Parse_Builtin,
+        .type = Entity_Type_Load_File,
+        .package = NULL,
+        .include = create_load(context.ast_alloc, "core/type_info"),
+    }));
     
     add_entities_for_node(NULL, (AstNode *) &builtin_stack_top, context.global_scope, NULL);
 
index 54475b59ae067ecf4a5be94efd0f075b83799f1a..404e419911a2d2b9123ce677551294eed040adc5 100644 (file)
@@ -51,6 +51,8 @@ AstType  *builtin_allocator_type;
 AstType  *builtin_iterator_type;
 AstType  *builtin_callsite_type;
 
+AstTyped *type_table_node = NULL;
+
 const BuiltinSymbol builtin_symbols[] = {
     { NULL, "void",       (AstNode *) &basic_type_void },
     { NULL, "bool",       (AstNode *) &basic_type_bool },
@@ -392,6 +394,10 @@ void initialize_builtins(bh_allocator a) {
         return;
     }
 
+    p = package_lookup("core.type_info");
+    if (p != NULL) {
+        type_table_node = (AstTyped *) symbol_raw_resolve(p->scope, "type_table");
+    }
 
     fori (i, 0, Binary_Op_Count) {
         bh_arr_new(global_heap_allocator, operator_overloads[i], 4); 
index f2a9a7ac2ee81f34c593ab6d7980c425b2851f6b..bb910401157812ee4a07a9cbc4e1c377f0113458 100644 (file)
@@ -3163,6 +3163,13 @@ static void emit_memory_reservation(OnyxWasmModule* mod, AstMemRes* memres) {
     u64 alignment = type_alignment_of(effective_type);
     u64 size = type_size_of(effective_type);
 
+    if (type_table_node != NULL && (AstMemRes *) type_table_node == memres) {
+        u64 table_location = build_type_table(mod);
+        memres->addr = table_location;
+
+        return;
+    }
+
     u32 offset = mod->next_datum_offset;
     bh_align(offset, alignment);
 
index c945f17de12f17a81c5b1cebe96ae416bf6ea017..0d89b347920f57778181c76a79fc0585b52188e5 100644 (file)
@@ -2,7 +2,7 @@
 // It is here purely to decrease the amount of clutter in the main file.
 
 
-void build_type_table(OnyxWasmModule* module) {
+u64 build_type_table(OnyxWasmModule* module) {
 
     bh_arr(u32) base_patch_locations=NULL;
     bh_arr_new(global_heap_allocator, base_patch_locations, 256);
@@ -10,7 +10,8 @@ void build_type_table(OnyxWasmModule* module) {
 #define PATCH (bh_arr_push(base_patch_locations, table_buffer.length))
 
     // This is the data behind the "type_table" slice in type_info.onyx
-    u32* table_info = bh_alloc_array(global_heap_allocator, u32, bh_arr_length(type_map.entries));
+    u32 type_count = bh_arr_length(type_map.entries);
+    u64* table_info = bh_alloc_array(global_heap_allocator, u64, type_count + 4); // HACK
 
     bh_buffer table_buffer;
     bh_buffer_init(&table_buffer, global_heap_allocator, 4096);
@@ -19,7 +20,6 @@ void build_type_table(OnyxWasmModule* module) {
         u64 type_idx = type_entry->key;
         Type* type = (Type *) type_entry->value;
 
-
         switch (type->kind) {
             case Type_Kind_Basic: {
                 table_info[type_idx] = table_buffer.length;
@@ -172,24 +172,78 @@ void build_type_table(OnyxWasmModule* module) {
                     bh_buffer_write_byte(&table_buffer, mem->initial_value != NULL ? 1 : 0);
                 }
 
-                u32 name_base = table_buffer.length;
-                u32 name_length = strlen(s->name);
-                bh_buffer_append(&table_buffer, s->name, name_length);
-                bh_buffer_align(&table_buffer, 8);
+                u32 name_base = 0;
+                u32 name_length = 0;
+                if (s->name) {
+                    name_length = strlen(s->name);
+                    name_base = table_buffer.length;
+                    bh_buffer_append(&table_buffer, s->name, name_length);
+                }
 
+                bh_buffer_align(&table_buffer, 8);
                 table_info[type_idx] = table_buffer.length;
                 bh_buffer_write_u32(&table_buffer, type->kind);
                 bh_buffer_write_u32(&table_buffer, type_size_of(type));
                 bh_buffer_write_u32(&table_buffer, type_alignment_of(type));
+                bh_buffer_write_u32(&table_buffer, 0);
                 PATCH;
                 bh_buffer_write_u64(&table_buffer, name_base);
                 bh_buffer_write_u64(&table_buffer, name_length);
                 PATCH;
                 bh_buffer_write_u64(&table_buffer, members_base);
                 bh_buffer_write_u64(&table_buffer, s->mem_count);
+
+                break;
             }
         }
     }
 
+    u32 offset = module->next_datum_offset;
+    bh_align(offset, 8);
+
+    u64 type_table_location = offset;
+
+    WasmDatum type_table_data = {
+        .offset = offset,
+        .length = type_count * 8,
+        .data = table_info,
+    };
+    bh_arr_push(module->data, type_table_data);
+
+    offset += type_table_data.length;
+
+    fori (i, 0, type_count) {
+        table_info[i] += offset;
+    }
+
+    bh_arr_each(u32, patch_loc, base_patch_locations) {
+        *(u64 *) (bh_pointer_add(table_buffer.data, *patch_loc)) += offset;
+    }
+
+    WasmDatum type_info_data = {
+        .offset = offset,
+        .length = table_buffer.length,
+        .data = table_buffer.data,
+    };
+    bh_arr_push(module->data, type_info_data);
+    offset += type_info_data.length;
+
+    u64 global_data_ptr = offset;
+
+    u64* tmp_data = bh_alloc(global_heap_allocator, 16);
+    tmp_data[0] = type_table_location;
+    tmp_data[1] = type_count;
+    WasmDatum type_table_global_data = {
+        .offset = offset,
+        .length = 16,
+        .data = tmp_data,
+    };
+    bh_arr_push(module->data, type_table_global_data);
+    offset += type_table_global_data.length;
+
+    module->next_datum_offset = offset;
+
+    return global_data_ptr;
+
 #undef PATCH
-}
\ No newline at end of file
+}