fixed #19
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 18 Sep 2020 02:19:41 +0000 (21:19 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 18 Sep 2020 02:19:41 +0000 (21:19 -0500)
CHANGELOG
include/onyxastnodes.h
include/onyxwasm.h
onyx
src/onyxparser.c
src/onyxwasm.c

index 5baaf7d9af33d039175ce884a95b023e506b020d..ca2b210745cf0c78ddb9ab3881453aa17d1cc5ea 100644 (file)
--- 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.
index 838b87e8a359a92f9317a99d5242f5b7495c116c..b5307cea7f20db36a83a790a1826cfc3f0f93540 100644 (file)
@@ -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;
 
index 6bbd2202afadf1131a6155e6ebf005ef95ae3506..e9dcdaed52a0680decc38b84e075a44b50f24448 100644 (file)
@@ -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 3691c2febeca9090994741ee6603f419eff94555..451f3906e178a4018d68bb3413fc73df7072e7b2 100755 (executable)
Binary files a/onyx and b/onyx differ
index 1b05461faa377eab3208528af2350f91a0fbba10..38c473638fc03b65bffafb42ca34c153f9c63b81 100644 (file)
@@ -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);
 
index 7a691dfaa4ab7c875d042b0891ced6200555f491..f75a143a8f0283eb40abea6a8763c895205c7632 100644 (file)
@@ -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);