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(...@@ -4063,12 +4063,12 @@ fn transCreateCompoundAssign(
4063 return block_scope.complete(c);4063 return block_scope.complete(c);
4064}4064}
40654065
4066// Casting away const or volatile requires us to use @ptrFromInt
4067fn removeCVQualifiers(c: *Context, dst_type_node: Node, expr: Node) Error!Node {4066fn 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);
4069 return Tag.as.create(c.arena, .{4069 return Tag.as.create(c.arena, .{
4070 .lhs = dst_type_node,4070 .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),
4072 });4072 });
4073}4073}
40744074
src/translate_c/ast.zig+16
...@@ -117,6 +117,10 @@ pub const Node = extern union {...@@ -117,6 +117,10 @@ pub const Node = extern union {
117 import_c_builtin,117 import_c_builtin,
118 /// @intCast(operand)118 /// @intCast(operand)
119 int_cast,119 int_cast,
120 /// @constCast(operand)
121 const_cast,
122 /// @volatileCast(operand)
123 volatile_cast,
120 /// @import("std").zig.c_translation.promoteIntLiteral(value, type, base)124 /// @import("std").zig.c_translation.promoteIntLiteral(value, type, base)
121 helpers_promoteIntLiteral,125 helpers_promoteIntLiteral,
122 /// @import("std").zig.c_translation.signedRemainder(lhs, rhs)126 /// @import("std").zig.c_translation.signedRemainder(lhs, rhs)
...@@ -278,6 +282,8 @@ pub const Node = extern union {...@@ -278,6 +282,8 @@ pub const Node = extern union {
278 .ptr_from_int,282 .ptr_from_int,
279 .ptr_cast,283 .ptr_cast,
280 .int_cast,284 .int_cast,
285 .const_cast,
286 .volatile_cast,
281 => Payload.UnOp,287 => Payload.UnOp,
282288
283 .add,289 .add,
...@@ -1315,6 +1321,14 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1315,6 +1321,14 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1315 const payload = node.castTag(.int_cast).?.data;1321 const payload = node.castTag(.int_cast).?.data;
1316 return renderBuiltinCall(c, "@intCast", &.{payload});1322 return renderBuiltinCall(c, "@intCast", &.{payload});
1317 },1323 },
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 },
1318 .signed_remainder => {1332 .signed_remainder => {
1319 const payload = node.castTag(.signed_remainder).?.data;1333 const payload = node.castTag(.signed_remainder).?.data;
1320 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "signedRemainder" });1334 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "signedRemainder" });
...@@ -2291,6 +2305,8 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {...@@ -2291,6 +2305,8 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
2291 .div_trunc,2305 .div_trunc,
2292 .signed_remainder,2306 .signed_remainder,
2293 .int_cast,2307 .int_cast,
2308 .const_cast,
2309 .volatile_cast,
2294 .as,2310 .as,
2295 .truncate,2311 .truncate,
2296 .bit_cast,2312 .bit_cast,
test/translate_c.zig+2-2
...@@ -3473,11 +3473,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3473,11 +3473,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3473 \\}3473 \\}
3474 \\pub export fn bar(arg_a: [*c]const c_int) void {3474 \\pub export fn bar(arg_a: [*c]const c_int) void {
3475 \\ var a = arg_a;3475 \\ var a = arg_a;
3476 \\ foo(@as([*c]c_int, @ptrFromInt(@intFromPtr(a))));3476 \\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
3477 \\}3477 \\}
3478 \\pub export fn baz(arg_a: [*c]volatile c_int) void {3478 \\pub export fn baz(arg_a: [*c]volatile c_int) void {
3479 \\ var a = arg_a;3479 \\ var a = arg_a;
3480 \\ foo(@as([*c]c_int, @ptrFromInt(@intFromPtr(a))));3480 \\ foo(@as([*c]c_int, @ptrCast(@volatileCast(@constCast(a)))));
3481 \\}3481 \\}
3482 });3482 });
34833483