stage1: More fixes for BE targets

* Fix packed struct alignment
* Adjust some tests
master
LemonBoy 2020-04-13 12:49:42 +02:00 committed by Andrew Kelley
parent cf750a58d5
commit ce21a784a4
3 changed files with 19 additions and 10 deletions

View File

@ -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;

View File

@ -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 {

View File

@ -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).*;