flagged everywhere 32-bit pointers are assumed
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 25 Jan 2021 17:47:30 +0000 (11:47 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 25 Jan 2021 17:47:30 +0000 (11:47 -0600)
bin/onyx
core/math.onyx
onyx.exe
src/onyxwasm.c

index 02dedce095a61faef823275f3b88c3f3030dc0ae..10117b08792905b5f16c8138023edd4d548c7458 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index ad3fa9ac70502b5e51c99246fcfde13cef709f14..c205fe87a552812ad22bb85b7e23a2843c20ed9d 100644 (file)
@@ -85,7 +85,7 @@ pow_int :: proc (base: $T, p: i32) -> T {
     if base == 0 do return 0;
     if p == 0    do return 1;
 
-    a := 1;
+    a: T = 1;
     while p > 0 {
         if p % 2 == 1 do a *= base;
         p = p >> 1;
index 3b67597bcce4e7674034494d2039ebdb6a71ded7..8b6348ea96f3bd714d2b340b6fbec91db8938c06 100644 (file)
Binary files a/onyx.exe and b/onyx.exe differ
index cc2b5c894f82e5f7c1003045b4f6dda1db4a5a56..aafd83cca07ff64fe51eb6768d4b2bbc1b50cb5e 100644 (file)
@@ -449,6 +449,7 @@ EMIT_FUNC(assignment_of_array, AstTyped* left, AstTyped* right) {
         }
         u32 elem_size = type_size_of(elem_type);
 
+        // :32BitPointers
         u64 lptr_local = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
 
         emit_location(mod, &code, left);
@@ -563,6 +564,7 @@ EMIT_FUNC(load_instruction, Type* type, u32 offset) {
 
     if (type->kind == Type_Kind_Array) {
         if (offset != 0) {
+            // :32BitPointers
             WID(WI_I32_CONST, offset);
             WI(WI_I32_ADD);
         }
@@ -770,6 +772,7 @@ EMIT_FUNC(for_array, AstFor* for_node, u64 iter_local) {
     if (for_node->by_pointer) elem_size = type_size_of(var->type->Pointer.elem);
     else                      elem_size = type_size_of(var->type);
 
+    // :32BitPointers
     WIL(WI_LOCAL_TEE, ptr_local);
     WIL(WI_I32_CONST, for_node->iter->type->Array.count * elem_size);
     WI(WI_I32_ADD);
@@ -779,6 +782,7 @@ EMIT_FUNC(for_array, AstFor* for_node, u64 iter_local) {
     emit_enter_structured_block(mod, &code, SBT_Basic_Loop);
     emit_enter_structured_block(mod, &code, SBT_Continue_Block);
 
+    // :32BitPointers
     WIL(WI_LOCAL_GET, ptr_local);
     WIL(WI_LOCAL_GET, end_ptr_local);
     WI(WI_I32_GE_U);
@@ -798,6 +802,7 @@ EMIT_FUNC(for_array, AstFor* for_node, u64 iter_local) {
 
     emit_leave_structured_block(mod, &code);
 
+    // :32BitPointers
     WIL(WI_LOCAL_GET, ptr_local);
     WIL(WI_I32_CONST, elem_size);
     WI(WI_I32_ADD);
@@ -835,6 +840,7 @@ EMIT_FUNC(for_slice, AstFor* for_node, u64 iter_local) {
     if (for_node->by_pointer) elem_size = type_size_of(var->type->Pointer.elem);
     else                      elem_size = type_size_of(var->type);
 
+    // :32BitPointers
     WIL(WI_LOCAL_SET, end_ptr_local);
     WIL(WI_LOCAL_TEE, ptr_local);
     WIL(WI_LOCAL_GET, end_ptr_local);
@@ -849,6 +855,7 @@ EMIT_FUNC(for_slice, AstFor* for_node, u64 iter_local) {
     emit_enter_structured_block(mod, &code, SBT_Basic_Loop);
     emit_enter_structured_block(mod, &code, SBT_Continue_Block);
 
+    // :32BitPointers
     WIL(WI_LOCAL_GET, ptr_local);
     WIL(WI_LOCAL_GET, end_ptr_local);
     WI(WI_I32_GE_U);
@@ -868,6 +875,7 @@ EMIT_FUNC(for_slice, AstFor* for_node, u64 iter_local) {
 
     emit_leave_structured_block(mod, &code);
 
+    // :32BitPointers
     WIL(WI_LOCAL_GET, ptr_local);
     WIL(WI_I32_CONST, elem_size);
     WI(WI_I32_ADD);
@@ -1200,6 +1208,7 @@ EMIT_FUNC(call, AstCall* call) {
             WID(WI_GLOBAL_GET, stack_top_idx);
             WIL(WI_LOCAL_SET, stack_top_store_local);
 
+            // :32BitPointers
             WID(WI_GLOBAL_GET, stack_top_idx);
             WID(WI_I32_CONST, stack_grow_amm);
             WI(WI_I32_ADD);
@@ -1220,6 +1229,7 @@ EMIT_FUNC(call, AstCall* call) {
 
             if (arg->va_kind != VA_Kind_Not_VA) vararg_count += 1;
             else {
+                // :32BitPointers
                 WID(WI_GLOBAL_GET, stack_top_idx);
                 WID(WI_I32_CONST, stack_grow_amm);
                 WI(WI_I32_ADD);
@@ -1231,6 +1241,7 @@ EMIT_FUNC(call, AstCall* call) {
 
     switch (call->va_kind) {
         case VA_Kind_Typed: {
+            // :32BitPointers
             WID(WI_GLOBAL_GET, stack_top_idx);
             WID(WI_I32_CONST, vararg_offset);
             WI(WI_I32_ADD);
@@ -1239,6 +1250,7 @@ EMIT_FUNC(call, AstCall* call) {
         }
 
         case VA_Kind_Untyped: {
+            // :32BitPointers
             WID(WI_GLOBAL_GET, stack_top_idx);
             WID(WI_GLOBAL_GET, stack_top_idx);
             WID(WI_I32_CONST, vararg_offset);
@@ -1280,6 +1292,7 @@ EMIT_FUNC(call, AstCall* call) {
     bh_align(stack_grow_amm, 16);
 
     if (needs_stack) {
+        // :32BitPointers
         WID(WI_GLOBAL_GET, stack_top_idx);
         WID(WI_I32_CONST, stack_grow_amm);
         WI(WI_I32_ADD);
@@ -1298,6 +1311,7 @@ EMIT_FUNC(call, AstCall* call) {
     }
 
     if (needs_stack) {
+        // :32BitPointers
         WID(WI_GLOBAL_GET, stack_top_idx);
         WID(WI_I32_CONST, stack_grow_amm);
         WI(WI_I32_SUB);
@@ -1682,6 +1696,7 @@ EMIT_FUNC(array_access_location, AstArrayAccess* aa, u64* offset_return) {
 
     emit_expression(mod, &code, aa->expr);
     if (aa->elem_size != 1) {
+        // :32BitPointers
         WID(WI_I32_CONST, aa->elem_size);
         WI(WI_I32_MUL);
     }
@@ -1702,6 +1717,8 @@ EMIT_FUNC(array_access_location, AstArrayAccess* aa, u64* offset_return) {
     } else {
         emit_expression(mod, &code, aa->addr);
     }
+
+    // :32BitPointers
     WI(WI_I32_ADD);
 
     *offset_return += offset;
@@ -1748,6 +1765,7 @@ EMIT_FUNC(field_access_location, AstFieldAccess* field, u64* offset_return) {
 EMIT_FUNC(memory_reservation_location, AstMemRes* memres) {
     bh_arr(WasmInstruction) code = *pcode;
 
+    // :32BitPointers
     WID(WI_I32_CONST, memres->addr);
 
     *pcode = code;
@@ -1918,6 +1936,7 @@ EMIT_FUNC(array_literal, AstArrayLiteral* al) {
     }
 
     WIL(WI_LOCAL_GET, mod->stack_base_idx);
+    // :32BitPointers
     WIL(WI_I32_CONST, local_offset);
     WI(WI_I32_ADD);
 
@@ -1964,6 +1983,7 @@ EMIT_FUNC(location_return_offset, AstTyped* expr, u64* offset_return) {
 
         case Ast_Kind_Memres: {
             AstMemRes* memres = (AstMemRes *) expr;
+            // :32BitPointers
             WID(WI_I32_CONST, memres->addr);
             *offset_return = 0;
             break;
@@ -1984,6 +2004,7 @@ EMIT_FUNC(location, AstTyped* expr) {
     u64 offset = 0;
     emit_location_return_offset(mod, &code, expr, &offset);
     if (offset != 0) {
+        // :32BitPointers
         WID(WI_I32_CONST, offset);
         WI(WI_I32_ADD);
     } 
@@ -2040,6 +2061,7 @@ EMIT_FUNC(expression, AstTyped* expr) {
                 if (expr->type->kind != Type_Kind_Array) {
                     emit_load_instruction(mod, &code, expr->type, offset);
                 } else if (offset != 0) {
+                    // :32BitPointers
                     WID(WI_I32_CONST, offset);
                     WI(WI_I32_ADD);
                 }
@@ -2079,6 +2101,7 @@ EMIT_FUNC(expression, AstTyped* expr) {
         }
 
         case Ast_Kind_StrLit: {
+            // :32BitPointers
             WID(WI_I32_CONST, ((AstStrLit *) expr)->addr);
             WID(WI_I32_CONST, ((AstStrLit *) expr)->length);
             break;
@@ -2101,6 +2124,8 @@ EMIT_FUNC(expression, AstTyped* expr) {
 
         case Ast_Kind_Function: {
             i32 elemidx = get_element_idx(mod, (AstFunction *) expr);
+
+            // :32BitPointers
             WID(WI_I32_CONST, elemidx);
             break;
         }
@@ -2138,9 +2163,7 @@ EMIT_FUNC(expression, AstTyped* expr) {
 
             // CLEANUP structs-by-value
             if (field->expr->kind == Ast_Kind_Param) {
-                AstLocal* param = (AstLocal *) field->expr;
-
-                if (type_get_param_pass(param->type) == Param_Pass_By_Value && !type_is_pointer(param->type)) {
+                if (type_get_param_pass(field->expr->type) == Param_Pass_By_Value && !type_is_pointer(field->expr->type)) {
                     u64 localidx = bh_imap_get(&mod->local_map, (u64) field->expr) + field->idx;
                     WIL(WI_LOCAL_GET, localidx);
                     break;
@@ -2149,6 +2172,24 @@ EMIT_FUNC(expression, AstTyped* expr) {
 
 
             // HACK
+            // This is only used in the weird case where you write something like this:
+            //
+            //    data_ptr := "Test string".data;
+            //    string_count := "Some other string".count;
+            //
+            // I don't think this is every used in the core libraries or anything written in
+            // Onyx yet, so this might go away at some point. Actually, thinking about it,
+            // I think accessing fields on r-values is completely disabled currently, so this
+            // is never possible.
+            //
+            // I think a more general case of accessing fields on r-values is better. For example,
+            //
+            //     returns_foo :: proc () -> Foo { ... }
+            //
+            //     value := returns_foo().foo_member;
+            //
+            // This does not work in the language at the moment, but maybe should?
+            /*
             else if (field->expr->kind == Ast_Kind_StrLit) {
                 StructMember smem;
 
@@ -2164,6 +2205,7 @@ EMIT_FUNC(expression, AstTyped* expr) {
 
                 break;
             }
+            */
 
             u64 offset = 0;
             emit_field_access_location(mod, &code, field, &offset);
@@ -2212,15 +2254,9 @@ EMIT_FUNC(expression, AstTyped* expr) {
         case Ast_Kind_Enum_Value: {
             AstEnumValue* ev = (AstEnumValue *) expr;
             WasmType backing_type = onyx_type_to_wasm_type(ev->type);
-            if (backing_type == WASM_TYPE_INT32) {
-                WID(WI_I32_CONST, ev->value->value.i);
-            }
-            else if (backing_type == WASM_TYPE_INT64) {
-                WID(WI_I64_CONST, ev->value->value.l);
-            }
-            else {
-                onyx_report_error(ev->token->pos, "Invalid backing type for enum.");
-            }
+            if      (backing_type == WASM_TYPE_INT32) WID(WI_I32_CONST, ev->value->value.i);
+            else if (backing_type == WASM_TYPE_INT64) WID(WI_I64_CONST, ev->value->value.l);
+            else onyx_report_error(ev->token->pos, "Invalid backing type for enum.");
             break;
         }
 
@@ -2234,6 +2270,7 @@ EMIT_FUNC(expression, AstTyped* expr) {
         case Ast_Kind_File_Contents: {
             AstFileContents* fc = (AstFileContents *) expr;
 
+            // :32BitPointers
             WID(WI_I32_CONST, fc->addr);
             WID(WI_I32_CONST, fc->size);
             break;