| author | |
| committer | |
| log | 6850e54cc02ddf067d3c85d0f75afb1013df0d34 |
| tree | 23b09d503fe6384286bee704d3ddf7486cde976f |
| parent | 79ef96b6a4af3fff751d5d7ad679634643c4fc6e |
| parent | 2545f44db0ce3e88dc9c59a9cbb18e67e02cc1e4 |
closes #57132 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, |
| 24646 | 24646 | return ir_build_field_parent_ptr_gen(ira, &instruction->base.base, casted_field_ptr, field, result_type); |
| 24647 | 24647 | } |
| 24648 | 24648 | |
| 24649 | static TypeStructField *validate_byte_offset(IrAnalyze *ira, | |
| 24649 | static TypeStructField *validate_host_int_byte_offset(IrAnalyze *ira, | |
| 24650 | 24650 | IrInstGen *type_value, |
| 24651 | 24651 | IrInstGen *field_name_value, |
| 24652 | 24652 | size_t *byte_offset) |
| ... | ... | @@ -24694,11 +24694,12 @@ static IrInstGen *ir_analyze_instruction_byte_offset_of(IrAnalyze *ira, IrInstSr |
| 24694 | 24694 | return ira->codegen->invalid_inst_gen; |
| 24695 | 24695 | |
| 24696 | 24696 | 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))) | |
| 24699 | 24700 | return ira->codegen->invalid_inst_gen; |
| 24700 | 24701 | |
| 24701 | ||
| 24702 | size_t byte_offset = host_int_byte_offset + (field->bit_offset_in_host / 8); | |
| 24702 | 24703 | return ir_const_unsigned(ira, &instruction->base.base, byte_offset); |
| 24703 | 24704 | } |
| 24704 | 24705 | |
| ... | ... | @@ -24707,12 +24708,12 @@ static IrInstGen *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira, IrInstSrc |
| 24707 | 24708 | if (type_is_invalid(type_value->value->type)) |
| 24708 | 24709 | return ira->codegen->invalid_inst_gen; |
| 24709 | 24710 | IrInstGen *field_name_value = instruction->field_name->child; |
| 24710 | size_t byte_offset = 0; | |
| 24711 | size_t host_int_byte_offset = 0; | |
| 24711 | 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 | 24714 | return ira->codegen->invalid_inst_gen; |
| 24714 | 24715 | |
| 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 | 24717 | return ir_const_unsigned(ira, &instruction->base.base, bit_offset); |
| 24717 | 24718 | } |
| 24718 | 24719 |
test/stage1/behavior/sizeof_and_typeof.zig+52| ... | ... | @@ -18,6 +18,8 @@ const A = struct { |
| 18 | 18 | e: u5, |
| 19 | 19 | f: u16, |
| 20 | 20 | g: u16, |
| 21 | h: u9, | |
| 22 | i: u7, | |
| 21 | 23 | }; |
| 22 | 24 | |
| 23 | 25 | const P = packed struct { |
| ... | ... | @@ -28,6 +30,8 @@ const P = packed struct { |
| 28 | 30 | e: u5, |
| 29 | 31 | f: u16, |
| 30 | 32 | g: u16, |
| 33 | h: u9, | |
| 34 | i: u7, | |
| 31 | 35 | }; |
| 32 | 36 | |
| 33 | 37 | test "@byteOffsetOf" { |
| ... | ... | @@ -39,6 +43,8 @@ test "@byteOffsetOf" { |
| 39 | 43 | expect(@byteOffsetOf(P, "e") == 6); |
| 40 | 44 | expect(@byteOffsetOf(P, "f") == 7); |
| 41 | 45 | expect(@byteOffsetOf(P, "g") == 9); |
| 46 | expect(@byteOffsetOf(P, "h") == 11); | |
| 47 | expect(@byteOffsetOf(P, "i") == 12); | |
| 42 | 48 | |
| 43 | 49 | // Normal struct fields can be moved/padded |
| 44 | 50 | var a: A = undefined; |
| ... | ... | @@ -49,6 +55,52 @@ test "@byteOffsetOf" { |
| 49 | 55 | expect(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e")); |
| 50 | 56 | expect(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f")); |
| 51 | 57 | 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 | ||
| 62 | test "@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 | |
| 52 | 104 | } |
| 53 | 105 | |
| 54 | 106 | test "@bitOffsetOf" { |