From ce21a784a4bccc66d555b337ebdf98454aaa652f Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 13 Apr 2020 12:49:42 +0200 Subject: [PATCH] stage1: More fixes for BE targets * Fix packed struct alignment * Adjust some tests --- src/codegen.cpp | 12 +++++++----- test/stage1/behavior/ptrcast.zig | 10 +++++++--- test/stage1/behavior/struct.zig | 7 +++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index 23ab2a130e..1a091584b4 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -7265,7 +7265,8 @@ check: switch (const_val->special) { LLVMTypeRef field_ty = LLVMStructGetTypeAtIndex(get_llvm_type(g, type_entry), (unsigned)type_struct_field->gen_index); const size_t size_in_bytes = LLVMStoreSizeOfType(g->target_data_ref, field_ty); - LLVMTypeRef big_int_type_ref = LLVMIntType(size_in_bytes * 8); + const size_t size_in_bits = size_in_bytes * 8; + LLVMTypeRef big_int_type_ref = LLVMIntType(size_in_bits); LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false); size_t used_bits = 0; for (size_t i = src_field_index; i < src_field_index_end; i += 1) { @@ -7278,16 +7279,17 @@ check: switch (const_val->special) { uint32_t packed_bits_size = type_size_bits(g, it_field->type_entry); if (is_big_endian) { LLVMValueRef shift_amt = LLVMConstInt(big_int_type_ref, - packed_bits_size, false); - val = LLVMConstShl(val, shift_amt); - val = LLVMConstOr(val, child_val); + size_in_bits - used_bits - packed_bits_size, false); + LLVMValueRef child_val_shifted = LLVMConstShl(child_val, shift_amt); + val = LLVMConstOr(val, child_val_shifted); } else { LLVMValueRef shift_amt = LLVMConstInt(big_int_type_ref, used_bits, false); LLVMValueRef child_val_shifted = LLVMConstShl(child_val, shift_amt); val = LLVMConstOr(val, child_val_shifted); - used_bits += packed_bits_size; } + used_bits += packed_bits_size; } + assert(size_in_bits >= used_bits); if (LLVMGetTypeKind(field_ty) != LLVMArrayTypeKind) { assert(LLVMGetTypeKind(field_ty) == LLVMIntegerTypeKind); fields[type_struct_field->gen_index] = val; diff --git a/test/stage1/behavior/ptrcast.zig b/test/stage1/behavior/ptrcast.zig index 3925325e61..26e9545248 100644 --- a/test/stage1/behavior/ptrcast.zig +++ b/test/stage1/behavior/ptrcast.zig @@ -1,5 +1,5 @@ -const builtin = @import("builtin"); const std = @import("std"); +const builtin = std.builtin; const expect = std.testing.expect; test "reinterpret bytes as integer with nonzero offset" { @@ -36,8 +36,12 @@ fn testReinterpretBytesAsExternStruct() void { } test "reinterpret struct field at comptime" { - const numLittle = comptime Bytes.init(0x12345678); - expect(std.mem.eql(u8, &[_]u8{ 0x78, 0x56, 0x34, 0x12 }, &numLittle.bytes)); + const numNative = comptime Bytes.init(0x12345678); + if (builtin.endian != .Little) { + expect(std.mem.eql(u8, &[_]u8{ 0x12, 0x34, 0x56, 0x78 }, &numNative.bytes)); + } else { + expect(std.mem.eql(u8, &[_]u8{ 0x78, 0x56, 0x34, 0x12 }, &numNative.bytes)); + } } const Bytes = struct { diff --git a/test/stage1/behavior/struct.zig b/test/stage1/behavior/struct.zig index 0365991ed3..1203399a24 100644 --- a/test/stage1/behavior/struct.zig +++ b/test/stage1/behavior/struct.zig @@ -1,8 +1,8 @@ const std = @import("std"); +const builtin = std.builtin; const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectEqualSlices = std.testing.expectEqualSlices; -const builtin = @import("builtin"); const maxInt = std.math.maxInt; const StructWithNoFields = struct { fn add(a: i32, b: i32) i32 { @@ -407,7 +407,10 @@ const Bitfields = packed struct { }; test "native bit field understands endianness" { - var all: u64 = 0x7765443322221111; + var all: u64 = if (builtin.endian != .Little) + 0x1111222233445677 + else + 0x7765443322221111; var bytes: [8]u8 = undefined; @memcpy(&bytes, @ptrCast([*]u8, &all), 8); var bitfields = @ptrCast(*Bitfields, &bytes).*;