Skip to content

Commit

Permalink
translate-c: initial support for bitfields
Browse files Browse the repository at this point in the history
  • Loading branch information
thislight committed Aug 5, 2024
1 parent a655c15 commit b038eaa
Show file tree
Hide file tree
Showing 14 changed files with 921 additions and 60 deletions.
50 changes: 39 additions & 11 deletions lib/compiler/aro_translate_c/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ pub const Node = extern union {
/// [1]type{val} ** count
array_filler,

/// @import("std").zig.c_translation.EmulateBitfieldStruct(S)
helpers_emulate_bitfield_struct,

pub const last_no_payload_tag = Tag.@"break";
pub const no_payload_count = @intFromEnum(last_no_payload_tag) + 1;

Expand Down Expand Up @@ -376,6 +379,7 @@ pub const Node = extern union {
.shuffle => Payload.Shuffle,
.builtin_extern => Payload.Extern,
.macro_arithmetic => Payload.MacroArithmetic,
.helpers_emulate_bitfield_struct => Payload.EmulateBitfieldStruct,
};
}

Expand Down Expand Up @@ -698,6 +702,15 @@ pub const Payload = struct {
},
};

pub const EmulateBitfieldStruct = struct {
base: Payload,
data: struct {
record: Node,
backings: Node,
cfg: Node,
},
};

pub const StringSlice = struct {
base: Payload,
data: struct {
Expand Down Expand Up @@ -917,6 +930,11 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "shuffleVectorIndex" });
return renderCall(c, import_node, &.{ payload.lhs, payload.rhs });
},
.helpers_emulate_bitfield_struct => {
const payload = node.castTag(.helpers_emulate_bitfield_struct).?.data;
const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "EmulateBitfieldStruct" });
return renderCall(c, import_node, &.{ payload.record, payload.backings, payload.cfg });
},
.vector => {
const payload = node.castTag(.vector).?.data;
return renderBuiltinCall(c, "@Vector", &.{ payload.lhs, payload.rhs });
Expand Down Expand Up @@ -2042,25 +2060,34 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
}
_ = try c.addToken(.r_brace, "}");

if (payload.len < 3) {
return c.addNode(.{
.tag = .struct_init_dot_two_comma,
switch (payload.len) {
0 => return c.addNode(.{
.tag = .struct_init_dot_two, // the inits[0], inits[1] are both 0
.main_token = l_brace,
.data = .{
.lhs = inits[0],
.rhs = inits[1],
},
});
} else {
const span = try c.listToSpan(inits);
return c.addNode(.{
.tag = .struct_init_dot_comma,
}),
1, 2 => return c.addNode(.{
.tag = .struct_init_dot_two_comma,
.main_token = l_brace,
.data = .{
.lhs = span.start,
.rhs = span.end,
.lhs = inits[0],
.rhs = inits[1],
},
});
}),
else => {
const span = try c.listToSpan(inits);
return c.addNode(.{
.tag = .struct_init_dot_comma,
.main_token = l_brace,
.data = .{
.lhs = span.start,
.rhs = span.end,
},
});
},
}
},
.container_init => {
Expand Down Expand Up @@ -2387,6 +2414,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
.helpers_promoteIntLiteral,
.helpers_shuffle_vector_index,
.helpers_flexible_array_type,
.helpers_emulate_bitfield_struct,
.std_mem_zeroinit,
.integer_literal,
.float_literal,
Expand Down
Loading

0 comments on commit b038eaa

Please sign in to comment.