From: Brendan Hansen Date: Tue, 22 Jun 2021 20:01:41 +0000 (-0500) Subject: working type table! X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=67e120bcfed8cbd662c06a8b35d2f3726b0c89f0;p=onyx.git working type table! --- diff --git a/bin/onyx b/bin/onyx index 7a061c3b..410ed5f9 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/include/bh.h b/include/bh.h index 742034c5..a3a56f19 100644 --- a/include/bh.h +++ b/include/bh.h @@ -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; } diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 5a7bd3db..5213bcae 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -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; diff --git a/src/onyx.c b/src/onyx.c index d64b22c9..c25bb89f 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -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); diff --git a/src/onyxbuiltins.c b/src/onyxbuiltins.c index 54475b59..404e4199 100644 --- a/src/onyxbuiltins.c +++ b/src/onyxbuiltins.c @@ -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); diff --git a/src/onyxwasm.c b/src/onyxwasm.c index f2a9a7ac..bb910401 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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); diff --git a/src/onyxwasm_type_table.c b/src/onyxwasm_type_table.c index c945f17d..0d89b347 100644 --- a/src/onyxwasm_type_table.c +++ b/src/onyxwasm_type_table.c @@ -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 +}