From: Brendan Hansen Date: Sun, 27 Nov 2022 05:05:51 +0000 (-0600) Subject: partially fixed long standing linking limitation X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=79ebe0dd39c33050568bd4ba72c075d11b47e0e8;p=onyx.git partially fixed long standing linking limitation --- diff --git a/compiler/include/wasm_emit.h b/compiler/include/wasm_emit.h index d057f5a9..f54daf91 100644 --- a/compiler/include/wasm_emit.h +++ b/compiler/include/wasm_emit.h @@ -628,6 +628,8 @@ typedef struct DatumPatchInfo { u32 offset; u32 location; u32 index; + + AstNode *node_to_use_if_data_id_is_null; } DatumPatchInfo; // Context used when building a constexpr buffer diff --git a/compiler/src/checker.c b/compiler/src/checker.c index 7b3ab8cc..edd04274 100644 --- a/compiler/src/checker.c +++ b/compiler/src/checker.c @@ -1649,6 +1649,10 @@ CheckStatus check_address_of(AstAddressOf** paof) { aof->type = type_make_pointer(context.ast_alloc, expr->type); + if (expr->kind == Ast_Kind_Memres) { + aof->flags |= Ast_Flag_Comptime; + } + return Check_Success; } diff --git a/compiler/src/wasm_emit.c b/compiler/src/wasm_emit.c index 5a31d87f..b8a046bc 100644 --- a/compiler/src/wasm_emit.c +++ b/compiler/src/wasm_emit.c @@ -4210,6 +4210,29 @@ static b32 emit_constexpr_(ConstExprContext *ctx, AstTyped *node, u32 offset) { break; } + case Ast_Kind_Address_Of: { + AstAddressOf *aof = (AstAddressOf *) node; + AstNode *expr = strip_aliases((AstNode *) aof->expr); + assert(expr->kind == Ast_Kind_Memres); + + DatumPatchInfo patch; + patch.kind = Datum_Patch_Data; + patch.index = ctx->data_id; + patch.location = offset; + patch.offset = 0; + + // Here, we cannot use the data_id property of the + // memory reservation because there is no guarantee that + // it will be assigned yet. And unlike the rest of the + // compiler, we cannot yield here, so we simply set a + // pointer that will be used later in the linking phase + // to get the actual data id of the addressed node. + patch.node_to_use_if_data_id_is_null = expr; + + bh_arr_push(ctx->module->data_patches, patch); + break; + } + case Ast_Kind_NumLit: { // NOTE: This makes a big assumption that we are running on a // little endian machine, since WebAssembly is little endian @@ -4643,7 +4666,17 @@ void onyx_wasm_module_link(OnyxWasmModule *module, OnyxWasmLinkOptions *options) } bh_arr_each(DatumPatchInfo, patch, module->data_patches) { - assert(patch->data_id > 0); + if (patch->data_id == 0) { + if (patch->node_to_use_if_data_id_is_null + && patch->node_to_use_if_data_id_is_null->kind == Ast_Kind_Memres) { + + patch->data_id = ((AstMemRes *) patch->node_to_use_if_data_id_is_null)->data_id; + + } else { + assert(("Unexpected empty data_id in linking!", 0)); + } + } + WasmDatum *datum = &module->data[patch->data_id - 1]; assert(datum->id == patch->data_id); diff --git a/core/io/stdio.onyx b/core/io/stdio.onyx index 43fa58a3..34a7dd54 100644 --- a/core/io/stdio.onyx +++ b/core/io/stdio.onyx @@ -11,7 +11,7 @@ package core #error "'stdio' can only be included in the 'wasi' or 'js' runtime." } -stdio_stream: io.Stream; +stdio_stream: io.Stream = .{ vtable = ^stdio_vtable }; auto_flush_stdio := true @@ -122,10 +122,6 @@ byte_dump :: (ptr: rawptr, byte_count: u32, bytes_per_line := 8) { __stdio_init :: () { stdio.print_stream = io.buffer_stream_make(2048, context.allocator); stdio.print_writer = io.writer_make(^stdio.print_stream, 0); - - // This shouldn't need to be here, but because ^stdin_vtable is not a compile-time - // known value (even through it should be). - stdio_stream.vtable = ^stdio_vtable; }