random bugfixes; better __zero_value support for arrays.
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 1 Oct 2021 15:34:24 +0000 (10:34 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 1 Oct 2021 15:34:24 +0000 (10:34 -0500)
bin/onyx
core/string.onyx
core/type_info/helper.onyx
modules/wasm_utils/parser.onyx
src/parser.c
src/wasm.c

index a2196a345d09bdab7b32cdb27e15d36d36ddd97c..8880c46c97b9a2f2e1171aaafe3fd8dda3171814 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index b5d011bc6d39b4b3fb4f11344ac1fa29cd4d4ddc..aa639618e9f0f9319cae3229541bda0d2a197759 100644 (file)
@@ -141,7 +141,6 @@ compare :: (str1: str, str2: str) -> i32 {
     return ~~(str1[i] - str2[i]);
 }
 
-#operator == equal
 equal :: (str1: str, str2: str) -> bool {
     if str1.count != str2.count do return false;
     while i := 0; i < str1.count {
@@ -151,6 +150,9 @@ equal :: (str1: str, str2: str) -> bool {
     return true;
 }
 
+#operator == equal
+#operator != macro (s1: str, s2: str) => !(s1 == s2);
+
 starts_with :: (s: str, prefix: str) -> bool {
     if s.count < prefix.count do return false;
     while i := 0; i < prefix.count {
index f0bc2ad907e67d16d38ee2bd7c4d89bc90d34335..48773cac8c13118ef359b093d130f6c4cfb451f2 100644 (file)
@@ -141,3 +141,27 @@ offset_of :: (T: type_expr, member: str) -> u32 {
 
     return true;
 }
+
+enum_name :: (value: $Backing_Type) -> str {
+    info := get_type_info(Backing_Type);
+    if info.kind != .Enum do return null_str;
+
+    etype := cast(^Type_Info_Enum) info;
+    for ^member: etype.members {
+        if member.value == ~~value do return member.name;
+    }
+    
+    return null_str;
+}
+
+enum_value :: ($E: type_expr, name: str) -> E {
+    info := get_type_info(E); 
+    if info.kind != .Enum do return ~~0;
+
+    etype := cast(^Type_Info_Enum) info;
+    for ^member: etype.members {
+        if member.name == name do return ~~member.value;
+    }
+
+    return ~~0;
+}
index 7d83b963a79b5ab1b2b5750d6721cfdf171d06d3..d72fd2b4d1880fe9fddb411e6a5727201aa97282 100644 (file)
@@ -365,11 +365,11 @@ parse_section_locations :: (use bin: ^WasmBinary) -> bool {
 
     {
         // Checking the magic string
-        magic_buffer: [4] u8; 
+        magic_buffer: [4] u8;
 
         @Bug // If these are string literals, then the null byte messes up the compiler and it thinks its a 0-character string.
-        if !(io.read_bytes(^reader, cast([] u8) magic_buffer) == ~~u8.[ 0, #char "a", #char "s", #char "m" ]) do return false;
-        if !(io.read_bytes(^reader, cast([] u8) magic_buffer) == ~~u8.[ 1, 0, 0, 0 ]) do return false; // This may not be necessary
+        if io.read_bytes(^reader, magic_buffer) != u8.[ 0, #char "a", #char "s", #char "m" ] do return false;
+        if io.read_bytes(^reader, magic_buffer) != u8.[ 1, 0, 0, 0 ] do return false; // This may not be necessary
     }
  
     while !io.stream_end_of_file(^stream) {
index a31e24a80dbba98c3a1b5ad1b36812c6f53b9be5..71cac81eb65978d7309eb0fad0ea410c70174126 100644 (file)
@@ -313,8 +313,14 @@ static AstTyped* parse_factor(OnyxParser* parser) {
 
     switch ((u16) parser->curr->type) {
         case '(': {
-            if (parse_possible_function_definition(parser, &retval)) break;
-            if (parse_possible_quick_function_definition(parser, &retval)) break;
+            if (parse_possible_function_definition(parser, &retval)) {
+                ENTITY_SUBMIT(retval);
+                break;
+            }
+            if (parse_possible_quick_function_definition(parser, &retval)) {
+                ENTITY_SUBMIT(retval);
+                break;
+            }
 
             consume_token(parser);
             retval = parse_compound_expression(parser, 0);
@@ -2232,7 +2238,6 @@ static b32 parse_possible_function_definition(OnyxParser* parser, AstTyped** ret
 
         OnyxToken* proc_token = parser->curr;
         AstFunction* func_node = parse_function_definition(parser, proc_token);
-        ENTITY_SUBMIT(func_node);
         *ret = (AstTyped *) func_node;
         return 1;
     }
@@ -2361,7 +2366,6 @@ static b32 parse_possible_quick_function_definition(OnyxParser* parser, AstTyped
     }
     poly_proc->base_func = func_node;
 
-    ENTITY_SUBMIT(poly_proc);
     *ret = (AstTyped *) poly_proc;
 
     bh_arr_free(params);
@@ -2496,15 +2500,19 @@ static AstIf* parse_static_if_stmt(OnyxParser* parser, b32 parse_block_as_statem
 static AstMacro* parse_macro(OnyxParser* parser) {
     AstMacro* macro = make_node(AstMacro, Ast_Kind_Macro);
     macro->token = expect_token(parser, Token_Type_Keyword_Macro);
-    
-    // First try quick function
-    if (!parse_possible_quick_function_definition(parser, &macro->body)) {
-        // Otherwise, do a normal function
-        macro->body  = (AstTyped *) parse_function_definition(parser, macro->token);
+
+    if (parse_possible_function_definition(parser, &macro->body)) {
+        ENTITY_SUBMIT(macro);
+        return macro;
+    }
+
+    if (parse_possible_quick_function_definition(parser, &macro->body)) {
+        ENTITY_SUBMIT(macro);
+        return macro;
     }
 
-    ENTITY_SUBMIT(macro);
-    return macro;
+    onyx_report_error(parser->curr->pos, "'macro' expects to be followed by a producure definition.");
+    return NULL;
 }
 
 static AstTyped* parse_top_level_expression(OnyxParser* parser) {
index f811c1c60ec20b610350cd0bcabc533dc048b57e..a7fcfec4a17c0366f93aafb0fa2b68afc902046f 100644 (file)
@@ -2153,6 +2153,11 @@ EMIT_FUNC(array_store, Type* type, u32 offset) {
     WIL(WI_LOCAL_SET, rptr_local);
     WIL(WI_LOCAL_SET, lptr_local);
 
+    WIL(WI_LOCAL_GET, rptr_local);
+    WID(WI_I32_CONST, 0);
+    WI(WI_I32_NE);
+    emit_enter_structured_block(mod, &code, SBT_Basic_If);
+
     if (elem_count <= 2) {
         // Inline copying for a small number of elements. It still may be faster to do this in a tight loop.
 
@@ -2175,8 +2180,11 @@ EMIT_FUNC(array_store, Type* type, u32 offset) {
             bh_arr_last(code).type = WI_LOCAL_TEE;
         else
             WIL(WI_LOCAL_GET, lptr_local);
-        WIL(WI_PTR_CONST, offset);
-        WI(WI_PTR_ADD);
+
+        if (offset != 0) {
+            WIL(WI_PTR_CONST, offset);
+            WI(WI_PTR_ADD);
+        }
 
         WIL(WI_LOCAL_GET, rptr_local);
         WIL(WI_I32_CONST, elem_count * elem_size);
@@ -2219,9 +2227,30 @@ EMIT_FUNC(array_store, Type* type, u32 offset) {
         local_raw_free(mod->local_alloc, WASM_TYPE_PTR);
     }
 
+    WI(WI_ELSE);
+
+    { // If the source ptr is null (0), then just copy in 0 bytes.
+        WIL(WI_LOCAL_GET, lptr_local);
+        if (offset != 0) {
+            WIL(WI_PTR_CONST, offset);
+            WI(WI_PTR_ADD);
+        }
+
+        WIL(WI_I32_CONST, 0);
+
+        WIL(WI_I32_CONST, elem_count * elem_size);
+
+        if (context.options->use_post_mvp_features) {
+            WI(WI_MEMORY_FILL);
+        } else {
+            emit_intrinsic_memory_fill(mod, &code);
+        }
+    }
+
     local_raw_free(mod->local_alloc, WASM_TYPE_PTR);
     local_raw_free(mod->local_alloc, WASM_TYPE_PTR);
 
+    emit_leave_structured_block(mod, &code);
     *pcode = code;
     return;
 }
@@ -2932,7 +2961,6 @@ EMIT_FUNC(zero_value_for_type, Type* type, OnyxToken* where) {
             type_linear_member_lookup(type, i, &two);
             emit_zero_value_for_type(mod, &code, two.type, where);
         }
-
     }
     else if (type->kind == Type_Kind_Function) {
         WID(WI_I32_CONST, mod->null_proc_func_idx);