Skip to content

Commit 09a1162

Browse files
committed
builtin functions: @byteOffsetOf and @bitOffsetOf
1 parent b18af37 commit 09a1162

File tree

7 files changed

+176
-56
lines changed

7 files changed

+176
-56
lines changed

doc/langref.html.in

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5838,8 +5838,14 @@ fn add(a: i32, b: i32) i32 {
58385838
</p>
58395839
{#see_also|@inlineCall#}
58405840
{#header_close#}
5841-
{#header_open|@offsetOf#}
5842-
<pre><code class="zig">@offsetOf(comptime T: type, comptime field_name: [] const u8) (number literal)</code></pre>
5841+
{#header_open|@byteOffsetOf#}
5842+
<pre><code class="zig">@byteOffsetOf(comptime T: type, comptime field_name: [] const u8) (number literal)</code></pre>
5843+
<p>
5844+
This function returns the byte offset of a field relative to its containing struct.
5845+
</p>
5846+
{#header_close#}
5847+
{#header_open|@bitOffsetOf#}
5848+
<pre><code class="zig">@bitOffsetOf(comptime T: type, comptime field_name: [] const u8) (number literal)</code></pre>
58435849
<p>
58445850
This function returns the byte offset of a field relative to its containing struct.
58455851
</p>

src/all_types.hpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,8 @@ enum BuiltinFnId {
13951395
BuiltinFnIdTagName,
13961396
BuiltinFnIdTagType,
13971397
BuiltinFnIdFieldParentPtr,
1398-
BuiltinFnIdOffsetOf,
1398+
BuiltinFnIdByteOffsetOf,
1399+
BuiltinFnIdBitOffsetOf,
13991400
BuiltinFnIdInlineCall,
14001401
BuiltinFnIdNoInlineCall,
14011402
BuiltinFnIdNewStackCall,
@@ -2119,7 +2120,8 @@ enum IrInstructionId {
21192120
IrInstructionIdTagName,
21202121
IrInstructionIdTagType,
21212122
IrInstructionIdFieldParentPtr,
2122-
IrInstructionIdOffsetOf,
2123+
IrInstructionIdByteOffsetOf,
2124+
IrInstructionIdBitOffsetOf,
21232125
IrInstructionIdTypeInfo,
21242126
IrInstructionIdTypeId,
21252127
IrInstructionIdSetEvalBranchQuota,
@@ -3022,7 +3024,14 @@ struct IrInstructionFieldParentPtr {
30223024
TypeStructField *field;
30233025
};
30243026

3025-
struct IrInstructionOffsetOf {
3027+
struct IrInstructionByteOffsetOf {
3028+
IrInstruction base;
3029+
3030+
IrInstruction *type_value;
3031+
IrInstruction *field_name;
3032+
};
3033+
3034+
struct IrInstructionBitOffsetOf {
30263035
IrInstruction base;
30273036

30283037
IrInstruction *type_value;

src/codegen.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5194,7 +5194,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
51945194
case IrInstructionIdTypeName:
51955195
case IrInstructionIdDeclRef:
51965196
case IrInstructionIdSwitchVar:
5197-
case IrInstructionIdOffsetOf:
5197+
case IrInstructionIdByteOffsetOf:
5198+
case IrInstructionIdBitOffsetOf:
51985199
case IrInstructionIdTypeInfo:
51995200
case IrInstructionIdTypeId:
52005201
case IrInstructionIdSetEvalBranchQuota:
@@ -6766,7 +6767,8 @@ static void define_builtin_fns(CodeGen *g) {
67666767
create_builtin_fn(g, BuiltinFnIdTagName, "tagName", 1);
67676768
create_builtin_fn(g, BuiltinFnIdTagType, "TagType", 1);
67686769
create_builtin_fn(g, BuiltinFnIdFieldParentPtr, "fieldParentPtr", 3);
6769-
create_builtin_fn(g, BuiltinFnIdOffsetOf, "offsetOf", 2);
6770+
create_builtin_fn(g, BuiltinFnIdByteOffsetOf, "byteOffsetOf", 2);
6771+
create_builtin_fn(g, BuiltinFnIdBitOffsetOf, "bitOffsetOf", 2);
67706772
create_builtin_fn(g, BuiltinFnIdDivExact, "divExact", 2);
67716773
create_builtin_fn(g, BuiltinFnIdDivTrunc, "divTrunc", 2);
67726774
create_builtin_fn(g, BuiltinFnIdDivFloor, "divFloor", 2);

src/ir.cpp

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,12 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionFieldParentPtr *
719719
return IrInstructionIdFieldParentPtr;
720720
}
721721

722-
static constexpr IrInstructionId ir_instruction_id(IrInstructionOffsetOf *) {
723-
return IrInstructionIdOffsetOf;
722+
static constexpr IrInstructionId ir_instruction_id(IrInstructionByteOffsetOf *) {
723+
return IrInstructionIdByteOffsetOf;
724+
}
725+
726+
static constexpr IrInstructionId ir_instruction_id(IrInstructionBitOffsetOf *) {
727+
return IrInstructionIdBitOffsetOf;
724728
}
725729

726730
static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeInfo *) {
@@ -2645,10 +2649,23 @@ static IrInstruction *ir_build_field_parent_ptr(IrBuilder *irb, Scope *scope, As
26452649
return &instruction->base;
26462650
}
26472651

2648-
static IrInstruction *ir_build_offset_of(IrBuilder *irb, Scope *scope, AstNode *source_node,
2652+
static IrInstruction *ir_build_byte_offset_of(IrBuilder *irb, Scope *scope, AstNode *source_node,
2653+
IrInstruction *type_value, IrInstruction *field_name)
2654+
{
2655+
IrInstructionByteOffsetOf *instruction = ir_build_instruction<IrInstructionByteOffsetOf>(irb, scope, source_node);
2656+
instruction->type_value = type_value;
2657+
instruction->field_name = field_name;
2658+
2659+
ir_ref_instruction(type_value, irb->current_basic_block);
2660+
ir_ref_instruction(field_name, irb->current_basic_block);
2661+
2662+
return &instruction->base;
2663+
}
2664+
2665+
static IrInstruction *ir_build_bit_offset_of(IrBuilder *irb, Scope *scope, AstNode *source_node,
26492666
IrInstruction *type_value, IrInstruction *field_name)
26502667
{
2651-
IrInstructionOffsetOf *instruction = ir_build_instruction<IrInstructionOffsetOf>(irb, scope, source_node);
2668+
IrInstructionBitOffsetOf *instruction = ir_build_instruction<IrInstructionBitOffsetOf>(irb, scope, source_node);
26522669
instruction->type_value = type_value;
26532670
instruction->field_name = field_name;
26542671

@@ -4695,7 +4712,22 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
46954712
IrInstruction *field_parent_ptr = ir_build_field_parent_ptr(irb, scope, node, arg0_value, arg1_value, arg2_value, nullptr);
46964713
return ir_lval_wrap(irb, scope, field_parent_ptr, lval);
46974714
}
4698-
case BuiltinFnIdOffsetOf:
4715+
case BuiltinFnIdByteOffsetOf:
4716+
{
4717+
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4718+
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4719+
if (arg0_value == irb->codegen->invalid_instruction)
4720+
return arg0_value;
4721+
4722+
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
4723+
IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
4724+
if (arg1_value == irb->codegen->invalid_instruction)
4725+
return arg1_value;
4726+
4727+
IrInstruction *offset_of = ir_build_byte_offset_of(irb, scope, node, arg0_value, arg1_value);
4728+
return ir_lval_wrap(irb, scope, offset_of, lval);
4729+
}
4730+
case BuiltinFnIdBitOffsetOf:
46994731
{
47004732
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
47014733
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
@@ -4707,7 +4739,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
47074739
if (arg1_value == irb->codegen->invalid_instruction)
47084740
return arg1_value;
47094741

4710-
IrInstruction *offset_of = ir_build_offset_of(irb, scope, node, arg0_value, arg1_value);
4742+
IrInstruction *offset_of = ir_build_bit_offset_of(irb, scope, node, arg0_value, arg1_value);
47114743
return ir_lval_wrap(irb, scope, offset_of, lval);
47124744
}
47134745
case BuiltinFnIdInlineCall:
@@ -16909,47 +16941,76 @@ static ZigType *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,
1690916941
return result_type;
1691016942
}
1691116943

16912-
static ZigType *ir_analyze_instruction_offset_of(IrAnalyze *ira,
16913-
IrInstructionOffsetOf *instruction)
16944+
static TypeStructField *validate_byte_offset(IrAnalyze *ira,
16945+
IrInstruction *type_value,
16946+
IrInstruction *field_name_value,
16947+
size_t *byte_offset)
1691416948
{
16915-
Error err;
16916-
IrInstruction *type_value = instruction->type_value->other;
1691716949
ZigType *container_type = ir_resolve_type(ira, type_value);
1691816950
if (type_is_invalid(container_type))
16919-
return ira->codegen->builtin_types.entry_invalid;
16951+
return nullptr;
1692016952

16953+
Error err;
1692116954
if ((err = ensure_complete_type(ira->codegen, container_type)))
16922-
return ira->codegen->builtin_types.entry_invalid;
16955+
return nullptr;
1692316956

16924-
IrInstruction *field_name_value = instruction->field_name->other;
1692516957
Buf *field_name = ir_resolve_str(ira, field_name_value);
1692616958
if (!field_name)
16927-
return ira->codegen->builtin_types.entry_invalid;
16959+
return nullptr;
1692816960

1692916961
if (container_type->id != ZigTypeIdStruct) {
1693016962
ir_add_error(ira, type_value,
1693116963
buf_sprintf("expected struct type, found '%s'", buf_ptr(&container_type->name)));
16932-
return ira->codegen->builtin_types.entry_invalid;
16964+
return nullptr;
1693316965
}
1693416966

1693516967
TypeStructField *field = find_struct_type_field(container_type, field_name);
1693616968
if (field == nullptr) {
1693716969
ir_add_error(ira, field_name_value,
1693816970
buf_sprintf("struct '%s' has no field '%s'",
16939-
buf_ptr(&container_type->name), buf_ptr(field_name)));
16940-
return ira->codegen->builtin_types.entry_invalid;
16971+
buf_ptr(&container_type->name), buf_ptr(field_name)));
16972+
return nullptr;
1694116973
}
1694216974

1694316975
if (!type_has_bits(field->type_entry)) {
1694416976
ir_add_error(ira, field_name_value,
16945-
buf_sprintf("zero-bit field '%s' in struct '%s' has no offset",
16946-
buf_ptr(field_name), buf_ptr(&container_type->name)));
16947-
return ira->codegen->builtin_types.entry_invalid;
16977+
buf_sprintf("zero-bit field '%s' in struct '%s' has no offset",
16978+
buf_ptr(field_name), buf_ptr(&container_type->name)));
16979+
return nullptr;
1694816980
}
16949-
size_t byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, container_type->type_ref, field->gen_index);
16950-
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
16951-
bigint_init_unsigned(&out_val->data.x_bigint, byte_offset);
16952-
return ira->codegen->builtin_types.entry_num_lit_int;
16981+
16982+
*byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, container_type->type_ref, field->gen_index);
16983+
return field;
16984+
}
16985+
16986+
static ZigType *ir_analyze_instruction_byte_offset_of(IrAnalyze *ira,
16987+
IrInstructionByteOffsetOf *instruction)
16988+
{
16989+
IrInstruction *type_value = instruction->type_value->other;
16990+
IrInstruction *field_name_value = instruction->field_name->other;
16991+
size_t byte_offset = 0;
16992+
if (validate_byte_offset(ira, type_value, field_name_value, &byte_offset)) {
16993+
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
16994+
bigint_init_unsigned(&out_val->data.x_bigint, byte_offset);
16995+
return ira->codegen->builtin_types.entry_num_lit_int;
16996+
}
16997+
return ira->codegen->builtin_types.entry_invalid;
16998+
}
16999+
17000+
static ZigType *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira,
17001+
IrInstructionBitOffsetOf *instruction)
17002+
{
17003+
IrInstruction *type_value = instruction->type_value->other;
17004+
IrInstruction *field_name_value = instruction->field_name->other;
17005+
size_t byte_offset = 0;
17006+
TypeStructField *field = nullptr;
17007+
if ((field = validate_byte_offset(ira, type_value, field_name_value, &byte_offset))) {
17008+
size_t bit_offset = byte_offset * 8 + field->packed_bits_offset;
17009+
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
17010+
bigint_init_unsigned(&out_val->data.x_bigint, bit_offset);
17011+
return ira->codegen->builtin_types.entry_num_lit_int;
17012+
}
17013+
return ira->codegen->builtin_types.entry_invalid;
1695317014
}
1695417015

1695517016
static void ensure_field_index(ZigType *type, const char *field_name, size_t index)
@@ -21362,8 +21423,10 @@ static ZigType *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *ins
2136221423
return ir_analyze_instruction_enum_tag_name(ira, (IrInstructionTagName *)instruction);
2136321424
case IrInstructionIdFieldParentPtr:
2136421425
return ir_analyze_instruction_field_parent_ptr(ira, (IrInstructionFieldParentPtr *)instruction);
21365-
case IrInstructionIdOffsetOf:
21366-
return ir_analyze_instruction_offset_of(ira, (IrInstructionOffsetOf *)instruction);
21426+
case IrInstructionIdByteOffsetOf:
21427+
return ir_analyze_instruction_byte_offset_of(ira, (IrInstructionByteOffsetOf *)instruction);
21428+
case IrInstructionIdBitOffsetOf:
21429+
return ir_analyze_instruction_bit_offset_of(ira, (IrInstructionBitOffsetOf *)instruction);
2136721430
case IrInstructionIdTypeInfo:
2136821431
return ir_analyze_instruction_type_info(ira, (IrInstructionTypeInfo *) instruction);
2136921432
case IrInstructionIdTypeId:
@@ -21647,7 +21710,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2164721710
case IrInstructionIdTypeName:
2164821711
case IrInstructionIdTagName:
2164921712
case IrInstructionIdFieldParentPtr:
21650-
case IrInstructionIdOffsetOf:
21713+
case IrInstructionIdByteOffsetOf:
21714+
case IrInstructionIdBitOffsetOf:
2165121715
case IrInstructionIdTypeInfo:
2165221716
case IrInstructionIdTypeId:
2165321717
case IrInstructionIdAlignCast:

src/ir_print.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,16 @@ static void ir_print_field_parent_ptr(IrPrint *irp, IrInstructionFieldParentPtr
10411041
fprintf(irp->f, ")");
10421042
}
10431043

1044-
static void ir_print_offset_of(IrPrint *irp, IrInstructionOffsetOf *instruction) {
1045-
fprintf(irp->f, "@offset_of(");
1044+
static void ir_print_byte_offset_of(IrPrint *irp, IrInstructionByteOffsetOf *instruction) {
1045+
fprintf(irp->f, "@byte_offset_of(");
1046+
ir_print_other_instruction(irp, instruction->type_value);
1047+
fprintf(irp->f, ",");
1048+
ir_print_other_instruction(irp, instruction->field_name);
1049+
fprintf(irp->f, ")");
1050+
}
1051+
1052+
static void ir_print_bit_offset_of(IrPrint *irp, IrInstructionBitOffsetOf *instruction) {
1053+
fprintf(irp->f, "@bit_offset_of(");
10461054
ir_print_other_instruction(irp, instruction->type_value);
10471055
fprintf(irp->f, ",");
10481056
ir_print_other_instruction(irp, instruction->field_name);
@@ -1649,8 +1657,11 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
16491657
case IrInstructionIdFieldParentPtr:
16501658
ir_print_field_parent_ptr(irp, (IrInstructionFieldParentPtr *)instruction);
16511659
break;
1652-
case IrInstructionIdOffsetOf:
1653-
ir_print_offset_of(irp, (IrInstructionOffsetOf *)instruction);
1660+
case IrInstructionIdByteOffsetOf:
1661+
ir_print_byte_offset_of(irp, (IrInstructionByteOffsetOf *)instruction);
1662+
break;
1663+
case IrInstructionIdBitOffsetOf:
1664+
ir_print_bit_offset_of(irp, (IrInstructionBitOffsetOf *)instruction);
16541665
break;
16551666
case IrInstructionIdTypeInfo:
16561667
ir_print_type_info(irp, (IrInstructionTypeInfo *)instruction);

test/cases/sizeof_and_typeof.zig

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,44 @@ test "sizeofAndTypeOf" {
77
const x: u16 = 13;
88
const z: @typeOf(x) = 19;
99

10-
const A = struct {
10+
const S = struct {
1111
a: u8,
1212
b: u32,
1313
c: u8,
14+
d: u3,
15+
e: u5,
16+
f: u16,
17+
g: u16,
1418
};
1519

1620
const P = packed struct {
1721
a: u8,
1822
b: u32,
1923
c: u8,
24+
d: u3,
25+
e: u5,
26+
f: u16,
27+
g: u16,
2028
};
2129

22-
test "offsetOf" {
23-
// Packed structs have fixed memory layout
30+
test "byteOffsetOf" {
2431
const p: P = undefined;
25-
assert(@offsetOf(P, "a") == 0);
26-
assert(@offsetOf(@typeOf(p), "b") == 1);
27-
assert(@offsetOf(@typeOf(p), "c") == 5);
28-
29-
// Non-packed struct fields can be moved/padded
30-
const a: A = undefined;
31-
assert(@ptrToInt(&a.a) - @ptrToInt(&a) == @offsetOf(A, "a"));
32-
assert(@ptrToInt(&a.b) - @ptrToInt(&a) == @offsetOf(@typeOf(a), "b"));
33-
assert(@ptrToInt(&a.c) - @ptrToInt(&a) == @offsetOf(@typeOf(a), "c"));
32+
std.debug.assert(@byteOffsetOf(P, "a") == 0 and @byteOffsetOf(S, "a") == 0);
33+
std.debug.assert(@byteOffsetOf(P, "b") == 1 and @byteOffsetOf(S, "b") == 4);
34+
std.debug.assert(@byteOffsetOf(P, "c") == 5 and @byteOffsetOf(S, "c") == 8);
35+
std.debug.assert(@byteOffsetOf(P, "d") == 6 and @byteOffsetOf(S, "d") == 9);
36+
std.debug.assert(@byteOffsetOf(P, "e") == 6 and @byteOffsetOf(S, "e") == 10);
37+
std.debug.assert(@byteOffsetOf(P, "f") == 7 and @byteOffsetOf(S, "f") == 12);
38+
std.debug.assert(@byteOffsetOf(P, "g") == 9 and @byteOffsetOf(S, "g") == 14);
3439
}
40+
41+
test "bitOffsetOf" {
42+
const p: P = undefined;
43+
std.debug.assert(@bitOffsetOf(P, "a") == 0 and @bitOffsetOf(S, "a") == 0);
44+
std.debug.assert(@bitOffsetOf(P, "b") == 8 and @bitOffsetOf(S, "b") == 32);
45+
std.debug.assert(@bitOffsetOf(P, "c") == 40 and @bitOffsetOf(S, "c") == 64);
46+
std.debug.assert(@bitOffsetOf(P, "d") == 48 and @bitOffsetOf(S, "d") == 72);
47+
std.debug.assert(@bitOffsetOf(P, "e") == 51 and @bitOffsetOf(S, "e") == 80);
48+
std.debug.assert(@bitOffsetOf(P, "f") == 56 and @bitOffsetOf(S, "f") == 96);
49+
std.debug.assert(@bitOffsetOf(P, "g") == 72 and @bitOffsetOf(S, "g") == 112);
50+
}

0 commit comments

Comments
 (0)