From: Brendan Hansen Date: Fri, 15 Jan 2021 19:04:13 +0000 (-0600) Subject: made pointers 8 bytes in size; easier transition to WASM64 or C backend X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=673c40103fb38675c8782ce9f79d751c76bd0852;p=onyx.git made pointers 8 bytes in size; easier transition to WASM64 or C backend --- diff --git a/bin/onyx b/bin/onyx index 978ceb02..ea7ee4c9 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/include/onyxtypes.h b/include/onyxtypes.h index 6d92d21b..16e71891 100644 --- a/include/onyxtypes.h +++ b/include/onyxtypes.h @@ -146,6 +146,7 @@ struct AstCompound; b32 types_are_compatible(Type* t1, Type* t2); u32 type_size_of(Type* type); u32 type_alignment_of(Type* type); +u32 type_aligned_size_of(Type* type); Type* type_build_from_ast(bh_allocator alloc, struct AstType* type_node); Type* type_build_function_type(bh_allocator alloc, struct AstFunction* func); diff --git a/onyx.exe b/onyx.exe index a089c778..932a0a46 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/src/onyxtypes.c b/src/onyxtypes.c index a6fb6817..8a5365bd 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -26,7 +26,7 @@ Type basic_types[] = { { Type_Kind_Basic, 0, { Basic_Kind_F32, Basic_Flag_Float, 4, 4, "f32" } }, { Type_Kind_Basic, 0, { Basic_Kind_F64, Basic_Flag_Float, 8, 4, "f64" } }, - { Type_Kind_Basic, 0, { Basic_Kind_Rawptr, Basic_Flag_Pointer, 4, 4, "rawptr" } }, + { Type_Kind_Basic, 0, { Basic_Kind_Rawptr, Basic_Flag_Pointer, 8, 8, "rawptr" } }, { Type_Kind_Basic, 0, { Basic_Kind_I8X16, Basic_Flag_SIMD, 16, 16, "i8x16" } }, { Type_Kind_Basic, 0, { Basic_Kind_I16X8, Basic_Flag_SIMD, 16, 16, "i16x8" } }, @@ -255,14 +255,14 @@ u32 type_size_of(Type* type) { switch (type->kind) { case Type_Kind_Basic: return type->Basic.size; - case Type_Kind_Pointer: return 4; + case Type_Kind_Pointer: return 8; case Type_Kind_Function: return 4; case Type_Kind_Array: return type->Array.size; case Type_Kind_Struct: return type->Struct.size; case Type_Kind_Enum: return type_size_of(type->Enum.backing); - case Type_Kind_Slice: return 8; - case Type_Kind_VarArgs: return 8; - case Type_Kind_DynArray: return 12; + case Type_Kind_Slice: return 16; // HACK: These should not have to be 16 bytes in size, they should only have to be 12, + case Type_Kind_VarArgs: return 16; // but there are alignment issues right now with that so I decided to not fight it and just make them 16 bytes in size. + case Type_Kind_DynArray: return 16; case Type_Kind_Compound: return type->Compound.size; default: return 0; } @@ -273,19 +273,28 @@ u32 type_alignment_of(Type* type) { switch (type->kind) { case Type_Kind_Basic: return type->Basic.alignment; - case Type_Kind_Pointer: return 4; + case Type_Kind_Pointer: return 8; case Type_Kind_Function: return 4; case Type_Kind_Array: return type_alignment_of(type->Array.elem); case Type_Kind_Struct: return type->Struct.alignment; case Type_Kind_Enum: return type_alignment_of(type->Enum.backing); - case Type_Kind_Slice: return 4; - case Type_Kind_VarArgs: return 4; - case Type_Kind_DynArray: return 4; + case Type_Kind_Slice: return 8; + case Type_Kind_VarArgs: return 8; + case Type_Kind_DynArray: return 8; case Type_Kind_Compound: return 8; // HACK default: return 1; } } +u32 type_aligned_size_of(Type* type) { + u32 size = type_size_of(type); + u32 alignment = type_alignment_of(type); + + bh_align(size, alignment); + + return size; +} + Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) { if (type_node == NULL) return NULL; @@ -603,7 +612,7 @@ Type* type_make_pointer(bh_allocator alloc, Type* to) { ptr_type->kind = Type_Kind_Pointer; ptr_type->Pointer.base.flags |= Basic_Flag_Pointer; - ptr_type->Pointer.base.size = 4; + ptr_type->Pointer.base.size = 8; ptr_type->Pointer.elem = to; return ptr_type; @@ -781,7 +790,7 @@ b32 type_lookup_member(Type* type, char* member, StructMember* smem) { } if (strcmp(member, "count") == 0) { smem->idx = 1; - smem->offset = 4; + smem->offset = 8; smem->type = &basic_types[Basic_Kind_U32]; smem->name = "count"; return 1; @@ -800,14 +809,14 @@ b32 type_lookup_member(Type* type, char* member, StructMember* smem) { } if (strcmp(member, "count") == 0) { smem->idx = 1; - smem->offset = 4; + smem->offset = 8; smem->type = &basic_types[Basic_Kind_U32]; smem->name = "count"; return 1; } if (strcmp(member, "capacity") == 0) { smem->idx = 2; - smem->offset = 8; + smem->offset = 12; smem->type = &basic_types[Basic_Kind_U32]; smem->name = "capacity"; return 1; @@ -843,7 +852,7 @@ b32 type_lookup_member_by_idx(Type* type, i32 idx, StructMember* smem) { } if (idx == 1) { smem->idx = 1; - smem->offset = 4; + smem->offset = 8; smem->type = &basic_types[Basic_Kind_U32]; smem->name = "count"; return 1; @@ -862,14 +871,14 @@ b32 type_lookup_member_by_idx(Type* type, i32 idx, StructMember* smem) { } if (idx == 1) { smem->idx = 1; - smem->offset = 4; + smem->offset = 8; smem->type = &basic_types[Basic_Kind_U32]; smem->name = "count"; return 1; } if (idx == 2) { smem->idx = 2; - smem->offset = 8; + smem->offset = 12; smem->type = &basic_types[Basic_Kind_U32]; smem->name = "capacity"; return 1; diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 794a3224..326c1fdb 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -559,7 +559,7 @@ EMIT_FUNC(store_instruction, Type* type, u32 offset) { i32 is_simd = is_basic && (type->Basic.flags & Basic_Flag_SIMD); if (is_pointer) { - WID(WI_I32_STORE, ((WasmInstructionData) { alignment, offset })); + WID(WI_I32_STORE, ((WasmInstructionData) { 2, offset })); } else if (is_integer) { if (store_size == 1) WID(WI_I32_STORE_8, ((WasmInstructionData) { alignment, offset })); else if (store_size == 2) WID(WI_I32_STORE_16, ((WasmInstructionData) { alignment, offset })); @@ -635,6 +635,7 @@ EMIT_FUNC(load_instruction, Type* type, u32 offset) { if (is_pointer) { instr = WI_I32_LOAD; + alignment = 2; } else if (is_integer) { if (load_size == 1) instr = WI_I32_LOAD_8_S; @@ -1392,17 +1393,19 @@ EMIT_FUNC(call, AstCall* call) { WID(WI_GLOBAL_GET, stack_top_idx); WID(WI_I32_CONST, vararg_offset); WI(WI_I32_ADD); - emit_store_instruction(mod, &code, &basic_types[Basic_Kind_I32], stack_grow_amm); + emit_store_instruction(mod, &code, &basic_types[Basic_Kind_Rawptr], stack_grow_amm); + + // NOTE: There will be 4 uninitialized bytes here, because pointers are only 4 bytes in WASM. WID(WI_GLOBAL_GET, stack_top_idx); WID(WI_I32_CONST, vararg_count); - emit_store_instruction(mod, &code, &basic_types[Basic_Kind_I32], stack_grow_amm + 4); + emit_store_instruction(mod, &code, &basic_types[Basic_Kind_I32], stack_grow_amm + 8); WID(WI_GLOBAL_GET, stack_top_idx); WID(WI_I32_CONST, stack_grow_amm); WI(WI_I32_ADD); - stack_grow_amm += 8; + stack_grow_amm += 12; break; } @@ -2282,7 +2285,6 @@ EMIT_FUNC(expression, AstTyped* expr) { } case Ast_Kind_Array_Literal: { - // assert(("Array literals are not allowed as expressions currently. This will be added in a future update.", 0)); emit_array_literal(mod, &code, (AstArrayLiteral *) expr); break; } @@ -2997,7 +2999,8 @@ static void emit_raw_data(OnyxWasmModule* mod, ptr data, AstTyped* node) { // by emit_string_literal. u32* sdata = (u32 *) data; sdata[0] = sl->addr; - sdata[1] = sl->length; + sdata[1] = 0x00; + sdata[2] = sl->length; break; } @@ -3039,12 +3042,12 @@ static void emit_raw_data(OnyxWasmModule* mod, ptr data, AstTyped* node) { case Basic_Kind_I32: case Basic_Kind_U32: - case Basic_Kind_Rawptr: *((i32 *) data) = ((AstNumLit *) node)->value.i; return; case Basic_Kind_I64: case Basic_Kind_U64: + case Basic_Kind_Rawptr: *((i64 *) data) = ((AstNumLit *) node)->value.l; return; diff --git a/tests/struct_robustness b/tests/struct_robustness index 5c7b967c..75163735 100644 --- a/tests/struct_robustness +++ b/tests/struct_robustness @@ -1,5 +1,5 @@ Testing a simple structure. -SimpleStruct<16, 4>(41, 67, Steve) +SimpleStruct<24, 8>(41, 67, Steve) Testing a simple union. @@ -36,5 +36,5 @@ PolyUnion(1234) Testing a polymorphic union with use. -8 == 8 +16 == 16 0, 5678.0 diff --git a/tests/struct_robustness.onyx b/tests/struct_robustness.onyx index 4e75fb46..70df0f08 100644 --- a/tests/struct_robustness.onyx +++ b/tests/struct_robustness.onyx @@ -145,7 +145,7 @@ main :: proc (args: [] cstr) { }; } - printf("%i == 8\n", sizeof PolyStruct(i32, [] u32)); + printf("%i == 16\n", sizeof PolyStruct(i32, [] u32)); ps : PolyStruct(i32, f64); ps.t_data = 1234;