Skip to content

Commit

Permalink
translate-c: Use @constCast and @volatileCast to remove CV-qualif…
Browse files Browse the repository at this point in the history
…iers

Instead of converting a pointer to an int and then back to a pointer.
  • Loading branch information
ehaas authored and Vexu committed Jun 29, 2023
1 parent 614bc67 commit 0a6cd25
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4063,12 +4063,12 @@ fn transCreateCompoundAssign(
return block_scope.complete(c);
}

// Casting away const or volatile requires us to use @ptrFromInt
fn removeCVQualifiers(c: *Context, dst_type_node: Node, expr: Node) Error!Node {
const int_from_ptr = try Tag.int_from_ptr.create(c.arena, expr);
const const_casted = try Tag.const_cast.create(c.arena, expr);
const volatile_casted = try Tag.volatile_cast.create(c.arena, const_casted);
return Tag.as.create(c.arena, .{
.lhs = dst_type_node,
.rhs = try Tag.ptr_from_int.create(c.arena, int_from_ptr),
.rhs = try Tag.ptr_cast.create(c.arena, volatile_casted),
});
}

Expand Down
16 changes: 16 additions & 0 deletions src/translate_c/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub const Node = extern union {
import_c_builtin,
/// @intCast(operand)
int_cast,
/// @constCast(operand)
const_cast,
/// @volatileCast(operand)
volatile_cast,
/// @import("std").zig.c_translation.promoteIntLiteral(value, type, base)
helpers_promoteIntLiteral,
/// @import("std").zig.c_translation.signedRemainder(lhs, rhs)
Expand Down Expand Up @@ -278,6 +282,8 @@ pub const Node = extern union {
.ptr_from_int,
.ptr_cast,
.int_cast,
.const_cast,
.volatile_cast,
=> Payload.UnOp,

.add,
Expand Down Expand Up @@ -1315,6 +1321,14 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
const payload = node.castTag(.int_cast).?.data;
return renderBuiltinCall(c, "@intCast", &.{payload});
},
.const_cast => {
const payload = node.castTag(.const_cast).?.data;
return renderBuiltinCall(c, "@constCast", &.{payload});
},
.volatile_cast => {
const payload = node.castTag(.volatile_cast).?.data;
return renderBuiltinCall(c, "@volatileCast", &.{payload});
},
.signed_remainder => {
const payload = node.castTag(.signed_remainder).?.data;
const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "signedRemainder" });
Expand Down Expand Up @@ -2291,6 +2305,8 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
.div_trunc,
.signed_remainder,
.int_cast,
.const_cast,
.volatile_cast,
.as,
.truncate,
.bit_cast,
Expand Down
4 changes: 2 additions & 2 deletions test/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3473,11 +3473,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
\\pub export fn bar(arg_a: [*c]const c_int) void {
\\ var a = arg_a;
\\ foo(@as([*c]c_int, @ptrFromInt(@intFromPtr(a))));
\\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
\\}
\\pub export fn baz(arg_a: [*c]volatile c_int) void {
\\ var a = arg_a;
\\ foo(@as([*c]c_int, @ptrFromInt(@intFromPtr(a))));
\\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
\\}
});

Expand Down

0 comments on commit 0a6cd25

Please sign in to comment.