authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-16 21:25:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-16 21:25:42-07:00
log6850e54cc02ddf067d3c85d0f75afb1013df0d34
tree23b09d503fe6384286bee704d3ddf7486cde976f
parent79ef96b6a4af3fff751d5d7ad679634643c4fc6e
parent2545f44db0ce3e88dc9c59a9cbb18e67e02cc1e4

Merge branch 'vegecode-byteOffsetOf_fix'

closes #5713

2 files changed, 60 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+52
......@@ -18,6 +18,8 @@ const A = struct {
1818 e: u5,
1919 f: u16,
2020 g: u16,
21 h: u9,
22 i: u7,
2123};
2224
2325const P = packed struct {
......@@ -28,6 +30,8 @@ const P = packed struct {
2830 e: u5,
2931 f: u16,
3032 g: u16,
33 h: u9,
34 i: u7,
3135};
3236
3337test "@byteOffsetOf" {
......@@ -39,6 +43,8 @@ test "@byteOffsetOf" {
3943 expect(@byteOffsetOf(P, "e") == 6);
4044 expect(@byteOffsetOf(P, "f") == 7);
4145 expect(@byteOffsetOf(P, "g") == 9);
46 expect(@byteOffsetOf(P, "h") == 11);
47 expect(@byteOffsetOf(P, "i") == 12);
4248
4349 // Normal struct fields can be moved/padded
4450 var a: A = undefined;
......@@ -49,6 +55,52 @@ test "@byteOffsetOf" {
4955 expect(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e"));
5056 expect(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f"));
5157 expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g"));
58 expect(@ptrToInt(&a.h) - @ptrToInt(&a) == @byteOffsetOf(A, "h"));
59 expect(@ptrToInt(&a.i) - @ptrToInt(&a) == @byteOffsetOf(A, "i"));
60}
61
62test "@byteOffsetOf packed struct, array length not power of 2 or multiple of native pointer width in bytes" {
63 const p3a_len = 3;
64 const P3 = packed struct {
65 a: [p3a_len]u8,
66 b: usize,
67 };
68 std.testing.expectEqual(0, @byteOffsetOf(P3, "a"));
69 std.testing.expectEqual(p3a_len, @byteOffsetOf(P3, "b"));
70
71 const p5a_len = 5;
72 const P5 = packed struct {
73 a: [p5a_len]u8,
74 b: usize,
75 };
76 std.testing.expectEqual(0, @byteOffsetOf(P5, "a"));
77 std.testing.expectEqual(p5a_len, @byteOffsetOf(P5, "b"));
78
79 const p6a_len = 6;
80 const P6 = packed struct {
81 a: [p6a_len]u8,
82 b: usize,
83 };
84 std.testing.expectEqual(0, @byteOffsetOf(P6, "a"));
85 std.testing.expectEqual(p6a_len, @byteOffsetOf(P6, "b"));
86
87 const p7a_len = 7;
88 const P7 = packed struct {
89 a: [p7a_len]u8,
90 b: usize,
91 };
92 std.testing.expectEqual(0, @byteOffsetOf(P7, "a"));
93 std.testing.expectEqual(p7a_len, @byteOffsetOf(P7, "b"));
94
95 const p9a_len = 9;
96 const P9 = packed struct {
97 a: [p9a_len]u8,
98 b: usize,
99 };
100 std.testing.expectEqual(0, @byteOffsetOf(P9, "a"));
101 std.testing.expectEqual(p9a_len, @byteOffsetOf(P9, "b"));
102
103 // 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 25 etc. are further cases
52104}
53105
54106test "@bitOffsetOf" {