authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-04-13 12:49:42+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-13 17:35:14-04:00
logce21a784a4bccc66d555b337ebdf98454aaa652f
treeea84e562d1009ae2ee6121fe190760a8d1dd2543
parentcf750a58d517c03b38509d9d3189c7b7af41820d

stage1: More fixes for BE targets

* Fix packed struct alignment * Adjust some tests

3 files changed, 19 insertions(+), 10 deletions(-)

src/codegen.cpp+7-5
......@@ -7265,7 +7265,8 @@ check: switch (const_val->special) {
72657265 LLVMTypeRef field_ty = LLVMStructGetTypeAtIndex(get_llvm_type(g, type_entry),
72667266 (unsigned)type_struct_field->gen_index);
72677267 const size_t size_in_bytes = LLVMStoreSizeOfType(g->target_data_ref, field_ty);
7268 LLVMTypeRef big_int_type_ref = LLVMIntType(size_in_bytes * 8);
7268 const size_t size_in_bits = size_in_bytes * 8;
7269 LLVMTypeRef big_int_type_ref = LLVMIntType(size_in_bits);
72697270 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);
72707271 size_t used_bits = 0;
72717272 for (size_t i = src_field_index; i < src_field_index_end; i += 1) {
......@@ -7278,16 +7279,17 @@ check: switch (const_val->special) {
72787279 uint32_t packed_bits_size = type_size_bits(g, it_field->type_entry);
72797280 if (is_big_endian) {
72807281 LLVMValueRef shift_amt = LLVMConstInt(big_int_type_ref,
7281 packed_bits_size, false);
7282 val = LLVMConstShl(val, shift_amt);
7283 val = LLVMConstOr(val, child_val);
7282 size_in_bits - used_bits - packed_bits_size, false);
7283 LLVMValueRef child_val_shifted = LLVMConstShl(child_val, shift_amt);
7284 val = LLVMConstOr(val, child_val_shifted);
72847285 } else {
72857286 LLVMValueRef shift_amt = LLVMConstInt(big_int_type_ref, used_bits, false);
72867287 LLVMValueRef child_val_shifted = LLVMConstShl(child_val, shift_amt);
72877288 val = LLVMConstOr(val, child_val_shifted);
7288 used_bits += packed_bits_size;
72897289 }
7290 used_bits += packed_bits_size;
72907291 }
7292 assert(size_in_bits >= used_bits);
72917293 if (LLVMGetTypeKind(field_ty) != LLVMArrayTypeKind) {
72927294 assert(LLVMGetTypeKind(field_ty) == LLVMIntegerTypeKind);
72937295 fields[type_struct_field->gen_index] = val;
test/stage1/behavior/ptrcast.zig+7-3
......@@ -1,5 +1,5 @@
1const builtin = @import("builtin");
21const std = @import("std");
2const builtin = std.builtin;
33const expect = std.testing.expect;
44
55test "reinterpret bytes as integer with nonzero offset" {
......@@ -36,8 +36,12 @@ fn testReinterpretBytesAsExternStruct() void {
3636}
3737
3838test "reinterpret struct field at comptime" {
39 const numLittle = comptime Bytes.init(0x12345678);
40 expect(std.mem.eql(u8, &[_]u8{ 0x78, 0x56, 0x34, 0x12 }, &numLittle.bytes));
39 const numNative = comptime Bytes.init(0x12345678);
40 if (builtin.endian != .Little) {
41 expect(std.mem.eql(u8, &[_]u8{ 0x12, 0x34, 0x56, 0x78 }, &numNative.bytes));
42 } else {
43 expect(std.mem.eql(u8, &[_]u8{ 0x78, 0x56, 0x34, 0x12 }, &numNative.bytes));
44 }
4145}
4246
4347const Bytes = struct {
test/stage1/behavior/struct.zig+5-2
......@@ -1,8 +1,8 @@
11const std = @import("std");
2const builtin = std.builtin;
23const expect = std.testing.expect;
34const expectEqual = std.testing.expectEqual;
45const expectEqualSlices = std.testing.expectEqualSlices;
5const builtin = @import("builtin");
66const maxInt = std.math.maxInt;
77const StructWithNoFields = struct {
88 fn add(a: i32, b: i32) i32 {
......@@ -407,7 +407,10 @@ const Bitfields = packed struct {
407407};
408408
409409test "native bit field understands endianness" {
410 var all: u64 = 0x7765443322221111;
410 var all: u64 = if (builtin.endian != .Little)
411 0x1111222233445677
412 else
413 0x7765443322221111;
411414 var bytes: [8]u8 = undefined;
412415 @memcpy(&bytes, @ptrCast([*]u8, &all), 8);
413416 var bitfields = @ptrCast(*Bitfields, &bytes).*;