authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2023-06-29 10:28:00-07:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-06-29 23:36:56+03:00
log0a6cd257b9c8a9093b966e3851dc8261e19b531a
tree84276cf942339bac3d0a63c2d81ad26f04e7dc64
parent614bc6755e99645e529fb0b648ffa34b1e3fc7ff

translate-c: Use `@constCast` and `@volatileCast` to remove CV-qualifiers

Instead of converting a pointer to an int and then back to a pointer.

3 files changed, 21 insertions(+), 5 deletions(-)

src/translate_c.zig+3-3
......@@ -4063,12 +4063,12 @@ fn transCreateCompoundAssign(
40634063 return block_scope.complete(c);
40644064}
40654065
4066// Casting away const or volatile requires us to use @ptrFromInt
40674066fn removeCVQualifiers(c: *Context, dst_type_node: Node, expr: Node) Error!Node {
4068 const int_from_ptr = try Tag.int_from_ptr.create(c.arena, expr);
4067 const const_casted = try Tag.const_cast.create(c.arena, expr);
4068 const volatile_casted = try Tag.volatile_cast.create(c.arena, const_casted);
40694069 return Tag.as.create(c.arena, .{
40704070 .lhs = dst_type_node,
4071 .rhs = try Tag.ptr_from_int.create(c.arena, int_from_ptr),
4071 .rhs = try Tag.ptr_cast.create(c.arena, volatile_casted),
40724072 });
40734073}
40744074
src/translate_c/ast.zig+16
......@@ -117,6 +117,10 @@ pub const Node = extern union {
117117 import_c_builtin,
118118 /// @intCast(operand)
119119 int_cast,
120 /// @constCast(operand)
121 const_cast,
122 /// @volatileCast(operand)
123 volatile_cast,
120124 /// @import("std").zig.c_translation.promoteIntLiteral(value, type, base)
121125 helpers_promoteIntLiteral,
122126 /// @import("std").zig.c_translation.signedRemainder(lhs, rhs)
......@@ -278,6 +282,8 @@ pub const Node = extern union {
278282 .ptr_from_int,
279283 .ptr_cast,
280284 .int_cast,
285 .const_cast,
286 .volatile_cast,
281287 => Payload.UnOp,
282288
283289 .add,
......@@ -1315,6 +1321,14 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
13151321 const payload = node.castTag(.int_cast).?.data;
13161322 return renderBuiltinCall(c, "@intCast", &.{payload});
13171323 },
1324 .const_cast => {
1325 const payload = node.castTag(.const_cast).?.data;
1326 return renderBuiltinCall(c, "@constCast", &.{payload});
1327 },
1328 .volatile_cast => {
1329 const payload = node.castTag(.volatile_cast).?.data;
1330 return renderBuiltinCall(c, "@volatileCast", &.{payload});
1331 },
13181332 .signed_remainder => {
13191333 const payload = node.castTag(.signed_remainder).?.data;
13201334 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "signedRemainder" });
......@@ -2291,6 +2305,8 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
22912305 .div_trunc,
22922306 .signed_remainder,
22932307 .int_cast,
2308 .const_cast,
2309 .volatile_cast,
22942310 .as,
22952311 .truncate,
22962312 .bit_cast,
test/translate_c.zig+2-2
......@@ -3473,11 +3473,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34733473 \\}
34743474 \\pub export fn bar(arg_a: [*c]const c_int) void {
34753475 \\ var a = arg_a;
3476 \\ foo(@as([*c]c_int, @ptrFromInt(@intFromPtr(a))));
3476 \\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
34773477 \\}
34783478 \\pub export fn baz(arg_a: [*c]volatile c_int) void {
34793479 \\ var a = arg_a;
3480 \\ foo(@as([*c]c_int, @ptrFromInt(@intFromPtr(a))));
3480 \\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
34813481 \\}
34823482 });
34833483