authorgravatar for justin.b.alexander1@gmail.comvegecode <justin.b.alexander1@gmail.com> 2020-07-04 10:04:15-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-16 20:04:42-07:00
log0456b2145de52bf0da3a18cfa3313ca42d7afd29
treee623e22a62324c65606d49945554c35c34108227
parent79ef96b6a4af3fff751d5d7ad679634643c4fc6e

byteOffsetOf rounds up using bit offset in host integer


2 files changed, 56 insertions(+), 7 deletions(-)

src/stage1/ir.cpp+8-7
......@@ -24646,7 +24646,7 @@ static IrInstGen *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,
2464624646 return ir_build_field_parent_ptr_gen(ira, &instruction->base.base, casted_field_ptr, field, result_type);
2464724647}
2464824648
24649static TypeStructField *validate_byte_offset(IrAnalyze *ira,
24649static TypeStructField *validate_host_int_byte_offset(IrAnalyze *ira,
2465024650 IrInstGen *type_value,
2465124651 IrInstGen *field_name_value,
2465224652 size_t *byte_offset)
......@@ -24694,11 +24694,12 @@ static IrInstGen *ir_analyze_instruction_byte_offset_of(IrAnalyze *ira, IrInstSr
2469424694 return ira->codegen->invalid_inst_gen;
2469524695
2469624696 IrInstGen *field_name_value = instruction->field_name->child;
24697 size_t byte_offset = 0;
24698 if (!validate_byte_offset(ira, type_value, field_name_value, &byte_offset))
24697 size_t host_int_byte_offset = 0;
24698 TypeStructField *field = nullptr;
24699 if (!(field = validate_host_int_byte_offset(ira, type_value, field_name_value, &host_int_byte_offset)))
2469924700 return ira->codegen->invalid_inst_gen;
2470024701
24701
24702 size_t byte_offset = host_int_byte_offset + (field->bit_offset_in_host / 8);
2470224703 return ir_const_unsigned(ira, &instruction->base.base, byte_offset);
2470324704}
2470424705
......@@ -24707,12 +24708,12 @@ static IrInstGen *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira, IrInstSrc
2470724708 if (type_is_invalid(type_value->value->type))
2470824709 return ira->codegen->invalid_inst_gen;
2470924710 IrInstGen *field_name_value = instruction->field_name->child;
24710 size_t byte_offset = 0;
24711 size_t host_int_byte_offset = 0;
2471124712 TypeStructField *field = nullptr;
24712 if (!(field = validate_byte_offset(ira, type_value, field_name_value, &byte_offset)))
24713 if (!(field = validate_host_int_byte_offset(ira, type_value, field_name_value, &host_int_byte_offset)))
2471324714 return ira->codegen->invalid_inst_gen;
2471424715
24715 size_t bit_offset = byte_offset * 8 + field->bit_offset_in_host;
24716 size_t bit_offset = host_int_byte_offset * 8 + field->bit_offset_in_host;
2471624717 return ir_const_unsigned(ira, &instruction->base.base, bit_offset);
2471724718}
2471824719
test/stage1/behavior/sizeof_and_typeof.zig+48
......@@ -28,6 +28,8 @@ const P = packed struct {
2828 e: u5,
2929 f: u16,
3030 g: u16,
31 h: u9,
32 i: u7,
3133};
3234
3335test "@byteOffsetOf" {
......@@ -39,6 +41,8 @@ test "@byteOffsetOf" {
3941 expect(@byteOffsetOf(P, "e") == 6);
4042 expect(@byteOffsetOf(P, "f") == 7);
4143 expect(@byteOffsetOf(P, "g") == 9);
44 expect(@byteOffsetOf(P, "h") == 11);
45 expect(@byteOffsetOf(P, "i") == 12);
4246
4347 // Normal struct fields can be moved/padded
4448 var a: A = undefined;
......@@ -51,6 +55,50 @@ test "@byteOffsetOf" {
5155 expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g"));
5256}
5357
58test "@byteOffsetOf packed struct, array length not power of 2 or multiple of native pointer width in bytes" {
59 const p3a_len = 3;
60 const P3 = packed struct {
61 a: [p3a_len]u8,
62 b: usize,
63 };
64 std.testing.expectEqual(0, @byteOffsetOf(P3, "a"));
65 std.testing.expectEqual(p3a_len, @byteOffsetOf(P3, "b"));
66
67 const p5a_len = 5;
68 const P5 = packed struct {
69 a: [p5a_len]u8,
70 b: usize,
71 };
72 std.testing.expectEqual(0, @byteOffsetOf(P5, "a"));
73 std.testing.expectEqual(p5a_len, @byteOffsetOf(P5, "b"));
74
75 const p6a_len = 6;
76 const P6 = packed struct {
77 a: [p6a_len]u8,
78 b: usize,
79 };
80 std.testing.expectEqual(0, @byteOffsetOf(P6, "a"));
81 std.testing.expectEqual(p6a_len, @byteOffsetOf(P6, "b"));
82
83 const p7a_len = 7;
84 const P7 = packed struct {
85 a: [p7a_len]u8,
86 b: usize,
87 };
88 std.testing.expectEqual(0, @byteOffsetOf(P7, "a"));
89 std.testing.expectEqual(p7a_len, @byteOffsetOf(P7, "b"));
90
91 const p9a_len = 9;
92 const P9 = packed struct {
93 a: [p9a_len]u8,
94 b: usize,
95 };
96 std.testing.expectEqual(0, @byteOffsetOf(P9, "a"));
97 std.testing.expectEqual(p9a_len, @byteOffsetOf(P9, "b"));
98
99 // 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 25 etc. are further cases
100}
101
54102test "@bitOffsetOf" {
55103 // Packed structs have fixed memory layout
56104 expect(@bitOffsetOf(P, "a") == 0);