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,...@@ -24646,7 +24646,7 @@ static IrInstGen *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,
24646 return ir_build_field_parent_ptr_gen(ira, &instruction->base.base, casted_field_ptr, field, result_type);24646 return ir_build_field_parent_ptr_gen(ira, &instruction->base.base, casted_field_ptr, field, result_type);
24647}24647}
2464824648
24649static TypeStructField *validate_byte_offset(IrAnalyze *ira,24649static TypeStructField *validate_host_int_byte_offset(IrAnalyze *ira,
24650 IrInstGen *type_value,24650 IrInstGen *type_value,
24651 IrInstGen *field_name_value,24651 IrInstGen *field_name_value,
24652 size_t *byte_offset)24652 size_t *byte_offset)
...@@ -24694,11 +24694,12 @@ static IrInstGen *ir_analyze_instruction_byte_offset_of(IrAnalyze *ira, IrInstSr...@@ -24694,11 +24694,12 @@ static IrInstGen *ir_analyze_instruction_byte_offset_of(IrAnalyze *ira, IrInstSr
24694 return ira->codegen->invalid_inst_gen;24694 return ira->codegen->invalid_inst_gen;
2469524695
24696 IrInstGen *field_name_value = instruction->field_name->child;24696 IrInstGen *field_name_value = instruction->field_name->child;
24697 size_t byte_offset = 0;24697 size_t host_int_byte_offset = 0;
24698 if (!validate_byte_offset(ira, type_value, field_name_value, &byte_offset))24698 TypeStructField *field = nullptr;
24699 if (!(field = validate_host_int_byte_offset(ira, type_value, field_name_value, &host_int_byte_offset)))
24699 return ira->codegen->invalid_inst_gen;24700 return ira->codegen->invalid_inst_gen;
2470024701
2470124702 size_t byte_offset = host_int_byte_offset + (field->bit_offset_in_host / 8);
24702 return ir_const_unsigned(ira, &instruction->base.base, byte_offset);24703 return ir_const_unsigned(ira, &instruction->base.base, byte_offset);
24703}24704}
2470424705
...@@ -24707,12 +24708,12 @@ static IrInstGen *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira, IrInstSrc...@@ -24707,12 +24708,12 @@ static IrInstGen *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira, IrInstSrc
24707 if (type_is_invalid(type_value->value->type))24708 if (type_is_invalid(type_value->value->type))
24708 return ira->codegen->invalid_inst_gen;24709 return ira->codegen->invalid_inst_gen;
24709 IrInstGen *field_name_value = instruction->field_name->child;24710 IrInstGen *field_name_value = instruction->field_name->child;
24710 size_t byte_offset = 0;24711 size_t host_int_byte_offset = 0;
24711 TypeStructField *field = nullptr;24712 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)))
24713 return ira->codegen->invalid_inst_gen;24714 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;
24716 return ir_const_unsigned(ira, &instruction->base.base, bit_offset);24717 return ir_const_unsigned(ira, &instruction->base.base, bit_offset);
24717}24718}
2471824719
test/stage1/behavior/sizeof_and_typeof.zig+48
...@@ -28,6 +28,8 @@ const P = packed struct {...@@ -28,6 +28,8 @@ const P = packed struct {
28 e: u5,28 e: u5,
29 f: u16,29 f: u16,
30 g: u16,30 g: u16,
31 h: u9,
32 i: u7,
31};33};
3234
33test "@byteOffsetOf" {35test "@byteOffsetOf" {
...@@ -39,6 +41,8 @@ test "@byteOffsetOf" {...@@ -39,6 +41,8 @@ test "@byteOffsetOf" {
39 expect(@byteOffsetOf(P, "e") == 6);41 expect(@byteOffsetOf(P, "e") == 6);
40 expect(@byteOffsetOf(P, "f") == 7);42 expect(@byteOffsetOf(P, "f") == 7);
41 expect(@byteOffsetOf(P, "g") == 9);43 expect(@byteOffsetOf(P, "g") == 9);
44 expect(@byteOffsetOf(P, "h") == 11);
45 expect(@byteOffsetOf(P, "i") == 12);
4246
43 // Normal struct fields can be moved/padded47 // Normal struct fields can be moved/padded
44 var a: A = undefined;48 var a: A = undefined;
...@@ -51,6 +55,50 @@ test "@byteOffsetOf" {...@@ -51,6 +55,50 @@ test "@byteOffsetOf" {
51 expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g"));55 expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g"));
52}56}
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
54test "@bitOffsetOf" {102test "@bitOffsetOf" {
55 // Packed structs have fixed memory layout103 // Packed structs have fixed memory layout
56 expect(@bitOffsetOf(P, "a") == 0);104 expect(@bitOffsetOf(P, "a") == 0);