cleaned up assignment emission
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 25 Jan 2021 13:56:28 +0000 (07:56 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 25 Jan 2021 13:56:28 +0000 (07:56 -0600)
bin/onyx
onyx.exe
src/onyxwasm.c

index cbc1797145c7f3ad083ca887a8e5c8f788d73092..049fe3fcc57dd83cdf04bd78f7cc829bb602a2b7 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 2a61a6e6b139a42a4b145434010b038df5246a13..065709166d25619092e8d1c869c5b66e040151e0 100644 (file)
Binary files a/onyx.exe and b/onyx.exe differ
index ccfb40eba8e3dd2ab9eb9ac6e6b89c1fe8debb8d..43cef707130bf537e5819bdeec8215a5cdd3c8ce 100644 (file)
@@ -406,59 +406,31 @@ EMIT_FUNC(assignment, AstBinaryOp* assign) {
 
     if (lval->kind == Ast_Kind_Local || lval->kind == Ast_Kind_Param) {
         if (bh_imap_get(&mod->local_map, (u64) lval) & LOCAL_IS_WASM) {
-            u64 localidx = bh_imap_get(&mod->local_map, (u64) lval);
             emit_expression(mod, &code, assign->right);
+
+            u64 localidx = bh_imap_get(&mod->local_map, (u64) lval);
             WIL(WI_LOCAL_SET, localidx);
 
-        } else {
-            u64 offset = 0;
-            emit_local_location(mod, &code, (AstLocal *) lval, &offset);
-            emit_expression(mod, &code, assign->right);
-            emit_store_instruction(mod, &code, lval->type, offset);
+            *pcode = code;
+            return;
         }
+    }
 
-    } else if (lval->kind == Ast_Kind_Global) {
-        i32 globalidx = (i32) bh_imap_get(&mod->index_map, (u64) lval);
-
-        emit_expression(mod, &code, assign->right);
-        WID(WI_GLOBAL_SET, globalidx);
-
-    } else if (lval->kind == Ast_Kind_Dereference) {
-        AstDereference* deref = (AstDereference *) lval;
-        emit_expression(mod, &code, deref->expr);
-        emit_expression(mod, &code, assign->right);
-
-        emit_store_instruction(mod, &code, deref->type, 0);
-
-    } else if (lval->kind == Ast_Kind_Array_Access) {
-        AstArrayAccess* aa = (AstArrayAccess *) lval;
-
-        u64 offset = 0;
-        emit_array_access_location(mod, &code, aa, &offset);
-        emit_expression(mod, &code, assign->right);
-
-        emit_store_instruction(mod, &code, aa->type, offset);
-
-    } else if (lval->kind == Ast_Kind_Field_Access) {
-        AstFieldAccess* field = (AstFieldAccess *) lval;
-
-        u64 offset = 0;
-        emit_field_access_location(mod, &code, field, &offset);
+    if (lval->kind == Ast_Kind_Global) {
         emit_expression(mod, &code, assign->right);
 
-        emit_store_instruction(mod, &code, field->type, offset);
-
-    } else if (lval->kind == Ast_Kind_Memres) {
-        AstMemRes* memres = (AstMemRes *) lval;
-
-        emit_memory_reservation_location(mod, &code, memres);
-        emit_expression(mod, &code, assign->right);
-        emit_store_instruction(mod, &code, memres->type, 0);
+        i32 globalidx = (i32) bh_imap_get(&mod->index_map, (u64) lval);
+        WID(WI_GLOBAL_SET, globalidx);
 
-    } else {
-        assert(("Invalid lval", 0));
+        *pcode = code;
+        return;
     }
 
+    u64 offset = 0;
+    emit_location_return_offset(mod, &code, lval, &offset);
+    emit_expression(mod, &code, assign->right);
+    emit_store_instruction(mod, &code, lval->type, offset);
+
     *pcode = code;
 }
 
@@ -492,9 +464,10 @@ EMIT_FUNC(assignment_of_array, AstTyped* left, AstTyped* right) {
         local_raw_free(mod->local_alloc, WASM_TYPE_INT32);
 
     } else {
-        emit_location(mod, &code, left);
+        u64 offset = 0;
+        emit_location_return_offset(mod, &code, left, &offset);
         emit_expression(mod, &code, right);
-        emit_array_store(mod, &code, rtype, 0);
+        emit_array_store(mod, &code, rtype, offset);
     }
 
     *pcode = code;
@@ -523,13 +496,14 @@ EMIT_FUNC(compound_assignment, AstBinaryOp* assign) {
             }
         }
 
-        u64 expr_tmp = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
+        WasmType wt = onyx_type_to_wasm_type(lval->type);
+        u64 expr_tmp = local_raw_allocate(mod->local_alloc, wt);
         WIL(WI_LOCAL_SET, expr_tmp);
         u64 offset = 0;
         emit_location_return_offset(mod, &code, lval, &offset);
         WIL(WI_LOCAL_GET, expr_tmp);
         
-        local_raw_free(mod->local_alloc, WASM_TYPE_INT32);
+        local_raw_free(mod->local_alloc, wt);
         emit_store_instruction(mod, &code, lval->type, offset);
     }