From: Brendan Hansen Date: Fri, 18 Sep 2020 02:19:41 +0000 (-0500) Subject: fixed #19 X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=7694364198b3fbccaf8c788bbb36dc312387e623;p=onyx.git fixed #19 --- diff --git a/CHANGELOG b/CHANGELOG index 5baaf7d9..ca2b2107 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -15,6 +15,7 @@ Changes: * BREAKING: 'use package' now places included symbols at the file scope, not package scope. * Switched to using TCC as the primary compiler, while maintaining support for GCC. * boolean literals are compile time known so they can be used at top level. +* #file_contents now results in [] u8, not *u8. Bug fixes: * Fixed error message index for struct literal member type mismatch. diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 838b87e8..b5307cea 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -432,7 +432,7 @@ struct AstArrayAccess { AstTyped_base; AstTyped *addr; AstTyped *expr; u64 ele struct AstFieldAccess { AstTyped_base; AstTyped *expr; u32 offset; u32 idx; }; struct AstSizeOf { AstTyped_base; AstType *so_type; u64 size; }; struct AstAlignOf { AstTyped_base; AstType *ao_type; u64 alignment; }; -struct AstFileContents { AstTyped_base; OnyxToken *filename; }; +struct AstFileContents { AstTyped_base; OnyxToken *filename; u32 addr, size; }; struct AstStructLiteral { AstTyped_base; diff --git a/include/onyxwasm.h b/include/onyxwasm.h index 6bbd2202..e9dcdaed 100644 --- a/include/onyxwasm.h +++ b/include/onyxwasm.h @@ -513,7 +513,7 @@ typedef struct OnyxWasmModule { // to the function type index if it has been created. bh_table(i32) type_map; - bh_table(u32) loaded_file_offsets; + bh_table(StrLitInfo) loaded_file_info; bh_table(StrLitInfo) string_literals; bh_arr(u8) structured_jump_target; diff --git a/onyx b/onyx index 3691c2fe..451f3906 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyxparser.c b/src/onyxparser.c index 1b05461f..38c47363 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -419,14 +419,10 @@ static AstTyped* parse_factor(OnyxParser* parser) { case '#': { if (parse_possible_directive(parser, "file_contents")) { - AstPointerType* fc_type = make_node(AstPointerType, Ast_Kind_Pointer_Type); - fc_type->flags |= Basic_Flag_Pointer; - fc_type->elem = (AstType *) &basic_type_u8; - AstFileContents* fc = make_node(AstFileContents, Ast_Kind_File_Contents); fc->token = parser->prev - 1; fc->filename = expect_token(parser, Token_Type_Literal_String); - fc->type_node = (AstType *) fc_type; + fc->type = type_make_slice(parser->allocator, &basic_types[Basic_Kind_U8]); add_node_to_process(parser, (AstNode *) fc); diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 7a691dfa..f75a143a 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -2235,12 +2235,9 @@ EMIT_FUNC(expression, AstTyped* expr) { case Ast_Kind_File_Contents: { AstFileContents* fc = (AstFileContents *) expr; - token_toggle_end(fc->filename); - u32 offset = bh_table_get(u32, mod->loaded_file_offsets, fc->filename->text); - WID(WI_I32_CONST, offset); - - token_toggle_end(fc->filename); + WID(WI_I32_CONST, fc->addr); + WID(WI_I32_CONST, fc->size); break; } @@ -2835,7 +2832,11 @@ static void emit_memory_reservation(OnyxWasmModule* mod, AstMemRes* memres) { static void emit_file_contents(OnyxWasmModule* mod, AstFileContents* fc) { token_toggle_end(fc->filename); - if (bh_table_has(u32, mod->loaded_file_offsets, fc->filename->text)) { + if (bh_table_has(StrLitInfo, mod->loaded_file_info, fc->filename->text)) { + StrLitInfo info = bh_table_get(StrLitInfo, mod->loaded_file_info, fc->filename->text); + fc->addr = info.addr; + fc->size = info.len; + token_toggle_end(fc->filename); return; } @@ -2843,7 +2844,6 @@ static void emit_file_contents(OnyxWasmModule* mod, AstFileContents* fc) { u32 offset = mod->next_datum_offset; if (offset % 16 != 0) offset += 16 - (offset % 16); - bh_table_put(u32, mod->loaded_file_offsets, fc->filename->text, offset); if (!bh_file_exists(fc->filename->text)) { onyx_report_error(fc->filename->pos, @@ -2861,6 +2861,14 @@ static void emit_file_contents(OnyxWasmModule* mod, AstFileContents* fc) { actual_data[contents.length] = 0; bh_file_contents_free(&contents); + bh_table_put(StrLitInfo, mod->loaded_file_info, fc->filename->text, ((StrLitInfo) { + .addr = offset, + .len = length - 1, + })); + + fc->addr = offset; + fc->size = length - 1; + WasmDatum datum = { .offset = offset, .length = length, @@ -2924,7 +2932,7 @@ OnyxWasmModule onyx_wasm_module_create(bh_allocator alloc) { bh_table_init(global_heap_allocator, module.type_map, 61); bh_table_init(global_heap_allocator, module.exports, 61); - bh_table_init(global_heap_allocator, module.loaded_file_offsets, 7); + bh_table_init(global_heap_allocator, module.loaded_file_info, 7); bh_table_init(global_heap_allocator, module.string_literals, 16); bh_imap_init(&module.index_map, global_heap_allocator, 128);