Skip to content

Commit 9ba0ea4

Browse files
committed
translate-c: initial support for bitfields
1 parent a931bfa commit 9ba0ea4

14 files changed

+853
-60
lines changed

lib/compiler/aro_translate_c/ast.zig

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ pub const Node = extern union {
227227
/// [1]type{val} ** count
228228
array_filler,
229229

230+
/// @import("std").zig.c_translation.EmulateBitfieldStruct(S)
231+
helpers_emulate_bitfield_struct,
232+
230233
pub const last_no_payload_tag = Tag.@"break";
231234
pub const no_payload_count = @intFromEnum(last_no_payload_tag) + 1;
232235

@@ -376,6 +379,7 @@ pub const Node = extern union {
376379
.shuffle => Payload.Shuffle,
377380
.builtin_extern => Payload.Extern,
378381
.macro_arithmetic => Payload.MacroArithmetic,
382+
.helpers_emulate_bitfield_struct => Payload.EmulateBitfieldStruct,
379383
};
380384
}
381385

@@ -698,6 +702,15 @@ pub const Payload = struct {
698702
},
699703
};
700704

705+
pub const EmulateBitfieldStruct = struct {
706+
base: Payload,
707+
data: struct {
708+
record: Node,
709+
backings: Node,
710+
cfg: Node,
711+
},
712+
};
713+
701714
pub const StringSlice = struct {
702715
base: Payload,
703716
data: struct {
@@ -917,6 +930,11 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
917930
const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "shuffleVectorIndex" });
918931
return renderCall(c, import_node, &.{ payload.lhs, payload.rhs });
919932
},
933+
.helpers_emulate_bitfield_struct => {
934+
const payload = node.castTag(.helpers_emulate_bitfield_struct).?.data;
935+
const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "EmulateBitfieldStruct" });
936+
return renderCall(c, import_node, &.{ payload.record, payload.backings, payload.cfg });
937+
},
920938
.vector => {
921939
const payload = node.castTag(.vector).?.data;
922940
return renderBuiltinCall(c, "@Vector", &.{ payload.lhs, payload.rhs });
@@ -2042,25 +2060,34 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
20422060
}
20432061
_ = try c.addToken(.r_brace, "}");
20442062

2045-
if (payload.len < 3) {
2046-
return c.addNode(.{
2047-
.tag = .struct_init_dot_two_comma,
2063+
switch (payload.len) {
2064+
0 => return c.addNode(.{
2065+
.tag = .struct_init_dot_two, // the inits[0], inits[1] are both 0
20482066
.main_token = l_brace,
20492067
.data = .{
20502068
.lhs = inits[0],
20512069
.rhs = inits[1],
20522070
},
2053-
});
2054-
} else {
2055-
const span = try c.listToSpan(inits);
2056-
return c.addNode(.{
2057-
.tag = .struct_init_dot_comma,
2071+
}),
2072+
1, 2 => return c.addNode(.{
2073+
.tag = .struct_init_dot_two_comma,
20582074
.main_token = l_brace,
20592075
.data = .{
2060-
.lhs = span.start,
2061-
.rhs = span.end,
2076+
.lhs = inits[0],
2077+
.rhs = inits[1],
20622078
},
2063-
});
2079+
}),
2080+
else => {
2081+
const span = try c.listToSpan(inits);
2082+
return c.addNode(.{
2083+
.tag = .struct_init_dot_comma,
2084+
.main_token = l_brace,
2085+
.data = .{
2086+
.lhs = span.start,
2087+
.rhs = span.end,
2088+
},
2089+
});
2090+
},
20642091
}
20652092
},
20662093
.container_init => {
@@ -2387,6 +2414,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
23872414
.helpers_promoteIntLiteral,
23882415
.helpers_shuffle_vector_index,
23892416
.helpers_flexible_array_type,
2417+
.helpers_emulate_bitfield_struct,
23902418
.std_mem_zeroinit,
23912419
.integer_literal,
23922420
.float_literal,

0 commit comments

Comments
 (0)