authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-06-22 22:40:13+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-24 16:47:49-07:00
log447ca4e3fff021f471b748187b53f0a4744ad0bc
tree16c4edaf3014dc03809b67be52112b361003f44c
parent283d6509730a0ca6fae0ed07a1814f5a9237f282

translate-c: update to new cast builtin syntax


2 files changed, 113 insertions(+), 121 deletions(-)

src/translate_c.zig+86-60
...@@ -1010,17 +1010,23 @@ fn buildFlexibleArrayFn(...@@ -1010,17 +1010,23 @@ fn buildFlexibleArrayFn(
1010 const bit_offset = layout.getFieldOffset(field_index); // this is a target-specific constant based on the struct layout1010 const bit_offset = layout.getFieldOffset(field_index); // this is a target-specific constant based on the struct layout
1011 const byte_offset = bit_offset / 8;1011 const byte_offset = bit_offset / 8;
10121012
1013 const casted_self = try Tag.ptr_cast.create(c.arena, .{1013 const casted_self = try Tag.as.create(c.arena, .{
1014 .lhs = intermediate_type_ident,1014 .lhs = intermediate_type_ident,
1015 .rhs = self_param,1015 .rhs = try Tag.ptr_cast.create(c.arena, self_param),
1016 });1016 });
1017 const field_offset = try transCreateNodeNumber(c, byte_offset, .int);1017 const field_offset = try transCreateNodeNumber(c, byte_offset, .int);
1018 const field_ptr = try Tag.add.create(c.arena, .{ .lhs = casted_self, .rhs = field_offset });1018 const field_ptr = try Tag.add.create(c.arena, .{ .lhs = casted_self, .rhs = field_offset });
10191019
1020 const alignment = try Tag.alignof.create(c.arena, element_type);1020 const ptr_cast = try Tag.as.create(c.arena, .{
10211021 .lhs = return_type_ident,
1022 const ptr_val = try Tag.align_cast.create(c.arena, .{ .lhs = alignment, .rhs = field_ptr });1022 .rhs = try Tag.ptr_cast.create(
1023 const ptr_cast = try Tag.ptr_cast.create(c.arena, .{ .lhs = return_type_ident, .rhs = ptr_val });1023 c.arena,
1024 try Tag.align_cast.create(
1025 c.arena,
1026 field_ptr,
1027 ),
1028 ),
1029 });
1024 const return_stmt = try Tag.@"return".create(c.arena, ptr_cast);1030 const return_stmt = try Tag.@"return".create(c.arena, ptr_cast);
1025 try block_scope.statements.append(return_stmt);1031 try block_scope.statements.append(return_stmt);
10261032
...@@ -1579,14 +1585,14 @@ fn transOffsetOfExpr(...@@ -1579,14 +1585,14 @@ fn transOffsetOfExpr(
1579/// pointer arithmetic expressions, where wraparound will ensure we get the correct value.1585/// pointer arithmetic expressions, where wraparound will ensure we get the correct value.
1580/// node -> @bitCast(usize, @intCast(isize, node))1586/// node -> @bitCast(usize, @intCast(isize, node))
1581fn usizeCastForWrappingPtrArithmetic(gpa: mem.Allocator, node: Node) TransError!Node {1587fn usizeCastForWrappingPtrArithmetic(gpa: mem.Allocator, node: Node) TransError!Node {
1582 const intcast_node = try Tag.int_cast.create(gpa, .{1588 const intcast_node = try Tag.as.create(gpa, .{
1583 .lhs = try Tag.type.create(gpa, "isize"),1589 .lhs = try Tag.type.create(gpa, "isize"),
1584 .rhs = node,1590 .rhs = try Tag.int_cast.create(gpa, node),
1585 });1591 });
15861592
1587 return Tag.bit_cast.create(gpa, .{1593 return Tag.as.create(gpa, .{
1588 .lhs = try Tag.type.create(gpa, "usize"),1594 .lhs = try Tag.type.create(gpa, "usize"),
1589 .rhs = intcast_node,1595 .rhs = try Tag.bit_cast.create(gpa, intcast_node),
1590 });1596 });
1591}1597}
15921598
...@@ -1781,7 +1787,10 @@ fn transBinaryOperator(...@@ -1781,7 +1787,10 @@ fn transBinaryOperator(
1781 const elem_type = c_pointer.castTag(.c_pointer).?.data.elem_type;1787 const elem_type = c_pointer.castTag(.c_pointer).?.data.elem_type;
1782 const sizeof = try Tag.sizeof.create(c.arena, elem_type);1788 const sizeof = try Tag.sizeof.create(c.arena, elem_type);
17831789
1784 const bitcast = try Tag.bit_cast.create(c.arena, .{ .lhs = ptrdiff_type, .rhs = infixOpNode });1790 const bitcast = try Tag.as.create(c.arena, .{
1791 .lhs = ptrdiff_type,
1792 .rhs = try Tag.bit_cast.create(c.arena, infixOpNode),
1793 });
17851794
1786 return Tag.div_exact.create(c.arena, .{1795 return Tag.div_exact.create(c.arena, .{
1787 .lhs = bitcast,1796 .lhs = bitcast,
...@@ -2310,7 +2319,7 @@ fn transIntegerLiteral(...@@ -2310,7 +2319,7 @@ fn transIntegerLiteral(
2310 // unsigned char y = 256;2319 // unsigned char y = 256;
2311 // How this gets evaluated is the 256 is an integer, which gets truncated to signed char, then bit-casted2320 // How this gets evaluated is the 256 is an integer, which gets truncated to signed char, then bit-casted
2312 // to unsigned char, resulting in 0. In order for this to work, we have to emit this zig code:2321 // to unsigned char, resulting in 0. In order for this to work, we have to emit this zig code:
2313 // var y = @bitCast(u8, @truncate(i8, @as(c_int, 256)));2322 // var y = @as(u8, @bitCast(@as(i8, @truncate(@as(c_int, 256)))));
2314 // Ideally in translate-c we could flatten this out to simply:2323 // Ideally in translate-c we could flatten this out to simply:
2315 // var y: u8 = 0;2324 // var y: u8 = 0;
2316 // But the first step is to be correct, and the next step is to make the output more elegant.2325 // But the first step is to be correct, and the next step is to make the output more elegant.
...@@ -2501,7 +2510,10 @@ fn transCCast(...@@ -2501,7 +2510,10 @@ fn transCCast(
2501 .lt => {2510 .lt => {
2502 // @truncate(SameSignSmallerInt, src_int_expr)2511 // @truncate(SameSignSmallerInt, src_int_expr)
2503 const ty_node = try transQualTypeIntWidthOf(c, dst_type, src_type_is_signed);2512 const ty_node = try transQualTypeIntWidthOf(c, dst_type, src_type_is_signed);
2504 src_int_expr = try Tag.truncate.create(c.arena, .{ .lhs = ty_node, .rhs = src_int_expr });2513 src_int_expr = try Tag.as.create(c.arena, .{
2514 .lhs = ty_node,
2515 .rhs = try Tag.truncate.create(c.arena, src_int_expr),
2516 });
2505 },2517 },
2506 .gt => {2518 .gt => {
2507 // @as(SameSignBiggerInt, src_int_expr)2519 // @as(SameSignBiggerInt, src_int_expr)
...@@ -2512,36 +2524,57 @@ fn transCCast(...@@ -2512,36 +2524,57 @@ fn transCCast(
2512 // src_int_expr = src_int_expr2524 // src_int_expr = src_int_expr
2513 },2525 },
2514 }2526 }
2515 // @bitCast(dest_type, intermediate_value)2527 // @as(dest_type, @bitCast(intermediate_value))
2516 return Tag.bit_cast.create(c.arena, .{ .lhs = dst_node, .rhs = src_int_expr });2528 return Tag.as.create(c.arena, .{
2529 .lhs = dst_node,
2530 .rhs = try Tag.bit_cast.create(c.arena, src_int_expr),
2531 });
2517 }2532 }
2518 if (cIsVector(src_type) or cIsVector(dst_type)) {2533 if (cIsVector(src_type) or cIsVector(dst_type)) {
2519 // C cast where at least 1 operand is a vector requires them to be same size2534 // C cast where at least 1 operand is a vector requires them to be same size
2520 // @bitCast(dest_type, val)2535 // @as(dest_type, @bitCast(val))
2521 return Tag.bit_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr });2536 return Tag.as.create(c.arena, .{
2537 .lhs = dst_node,
2538 .rhs = try Tag.bit_cast.create(c.arena, expr),
2539 });
2522 }2540 }
2523 if (cIsInteger(dst_type) and qualTypeIsPtr(src_type)) {2541 if (cIsInteger(dst_type) and qualTypeIsPtr(src_type)) {
2524 // @intCast(dest_type, @intFromPtr(val))2542 // @intCast(dest_type, @intFromPtr(val))
2525 const int_from_ptr = try Tag.int_from_ptr.create(c.arena, expr);2543 const int_from_ptr = try Tag.int_from_ptr.create(c.arena, expr);
2526 return Tag.int_cast.create(c.arena, .{ .lhs = dst_node, .rhs = int_from_ptr });2544 return Tag.as.create(c.arena, .{
2545 .lhs = dst_node,
2546 .rhs = try Tag.int_cast.create(c.arena, int_from_ptr),
2547 });
2527 }2548 }
2528 if (cIsInteger(src_type) and qualTypeIsPtr(dst_type)) {2549 if (cIsInteger(src_type) and qualTypeIsPtr(dst_type)) {
2529 // @ptrFromInt(dest_type, val)2550 // @as(dest_type, @ptrFromInt(val))
2530 return Tag.ptr_from_int.create(c.arena, .{ .lhs = dst_node, .rhs = expr });2551 return Tag.as.create(c.arena, .{
2552 .lhs = dst_node,
2553 .rhs = try Tag.ptr_from_int.create(c.arena, expr),
2554 });
2531 }2555 }
2532 if (cIsFloating(src_type) and cIsFloating(dst_type)) {2556 if (cIsFloating(src_type) and cIsFloating(dst_type)) {
2533 // @floatCast(dest_type, val)2557 // @as(dest_type, @floatCast(val))
2534 return Tag.float_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr });2558 return Tag.as.create(c.arena, .{
2559 .lhs = dst_node,
2560 .rhs = try Tag.float_cast.create(c.arena, expr),
2561 });
2535 }2562 }
2536 if (cIsFloating(src_type) and !cIsFloating(dst_type)) {2563 if (cIsFloating(src_type) and !cIsFloating(dst_type)) {
2537 // @intFromFloat(dest_type, val)2564 // @as(dest_type, @intFromFloat(val))
2538 return Tag.int_from_float.create(c.arena, .{ .lhs = dst_node, .rhs = expr });2565 return Tag.as.create(c.arena, .{
2566 .lhs = dst_node,
2567 .rhs = try Tag.int_from_float.create(c.arena, expr),
2568 });
2539 }2569 }
2540 if (!cIsFloating(src_type) and cIsFloating(dst_type)) {2570 if (!cIsFloating(src_type) and cIsFloating(dst_type)) {
2541 var rhs = expr;2571 var rhs = expr;
2542 if (qualTypeIsBoolean(src_type)) rhs = try Tag.int_from_bool.create(c.arena, expr);2572 if (qualTypeIsBoolean(src_type)) rhs = try Tag.int_from_bool.create(c.arena, expr);
2543 // @floatFromInt(dest_type, val)2573 // @as(dest_type, @floatFromInt(val))
2544 return Tag.float_from_int.create(c.arena, .{ .lhs = dst_node, .rhs = rhs });2574 return Tag.as.create(c.arena, .{
2575 .lhs = dst_node,
2576 .rhs = try Tag.float_from_int.create(c.arena, rhs),
2577 });
2545 }2578 }
2546 if (qualTypeIsBoolean(src_type) and !qualTypeIsBoolean(dst_type)) {2579 if (qualTypeIsBoolean(src_type) and !qualTypeIsBoolean(dst_type)) {
2547 // @intFromBool returns a u12580 // @intFromBool returns a u1
...@@ -3487,9 +3520,9 @@ fn transSignedArrayAccess(...@@ -3487,9 +3520,9 @@ fn transSignedArrayAccess(
34873520
3488 const then_value = try Tag.add.create(c.arena, .{3521 const then_value = try Tag.add.create(c.arena, .{
3489 .lhs = container_node,3522 .lhs = container_node,
3490 .rhs = try Tag.int_cast.create(c.arena, .{3523 .rhs = try Tag.as.create(c.arena, .{
3491 .lhs = try Tag.type.create(c.arena, "usize"),3524 .lhs = try Tag.type.create(c.arena, "usize"),
3492 .rhs = tmp_ref,3525 .rhs = try Tag.int_cast.create(c.arena, tmp_ref),
3493 }),3526 }),
3494 });3527 });
34953528
...@@ -3499,17 +3532,17 @@ fn transSignedArrayAccess(...@@ -3499,17 +3532,17 @@ fn transSignedArrayAccess(
3499 });3532 });
35003533
3501 const minuend = container_node;3534 const minuend = container_node;
3502 const signed_size = try Tag.int_cast.create(c.arena, .{3535 const signed_size = try Tag.as.create(c.arena, .{
3503 .lhs = try Tag.type.create(c.arena, "isize"),3536 .lhs = try Tag.type.create(c.arena, "isize"),
3504 .rhs = tmp_ref,3537 .rhs = try Tag.int_cast.create(c.arena, tmp_ref),
3505 });3538 });
3506 const to_cast = try Tag.add_wrap.create(c.arena, .{3539 const to_cast = try Tag.add_wrap.create(c.arena, .{
3507 .lhs = signed_size,3540 .lhs = signed_size,
3508 .rhs = try Tag.negate.create(c.arena, Tag.one_literal.init()),3541 .rhs = try Tag.negate.create(c.arena, Tag.one_literal.init()),
3509 });3542 });
3510 const bitcast_node = try Tag.bit_cast.create(c.arena, .{3543 const bitcast_node = try Tag.as.create(c.arena, .{
3511 .lhs = try Tag.type.create(c.arena, "usize"),3544 .lhs = try Tag.type.create(c.arena, "usize"),
3512 .rhs = to_cast,3545 .rhs = try Tag.bit_cast.create(c.arena, to_cast),
3513 });3546 });
3514 const subtrahend = try Tag.bit_not.create(c.arena, bitcast_node);3547 const subtrahend = try Tag.bit_not.create(c.arena, bitcast_node);
3515 const difference = try Tag.sub.create(c.arena, .{3548 const difference = try Tag.sub.create(c.arena, .{
...@@ -3566,7 +3599,13 @@ fn transArrayAccess(c: *Context, scope: *Scope, stmt: *const clang.ArraySubscrip...@@ -3566,7 +3599,13 @@ fn transArrayAccess(c: *Context, scope: *Scope, stmt: *const clang.ArraySubscrip
3566 const rhs = if (is_longlong or is_signed) blk: {3599 const rhs = if (is_longlong or is_signed) blk: {
3567 // check if long long first so that signed long long doesn't just become unsigned long long3600 // check if long long first so that signed long long doesn't just become unsigned long long
3568 const typeid_node = if (is_longlong) try Tag.type.create(c.arena, "usize") else try transQualTypeIntWidthOf(c, subscr_qt, false);3601 const typeid_node = if (is_longlong) try Tag.type.create(c.arena, "usize") else try transQualTypeIntWidthOf(c, subscr_qt, false);
3569 break :blk try Tag.int_cast.create(c.arena, .{ .lhs = typeid_node, .rhs = try transExpr(c, scope, subscr_expr, .used) });3602 break :blk try Tag.as.create(c.arena, .{
3603 .lhs = typeid_node,
3604 .rhs = try Tag.int_cast.create(
3605 c.arena,
3606 try transExpr(c, scope, subscr_expr, .used),
3607 ),
3608 });
3570 } else try transExpr(c, scope, subscr_expr, .used);3609 } else try transExpr(c, scope, subscr_expr, .used);
35713610
3572 const node = try Tag.array_access.create(c.arena, .{3611 const node = try Tag.array_access.create(c.arena, .{
...@@ -3968,8 +4007,7 @@ fn transCreateCompoundAssign(...@@ -3968,8 +4007,7 @@ fn transCreateCompoundAssign(
3968 }4007 }
39694008
3970 if (is_shift) {4009 if (is_shift) {
3971 const cast_to_type = try qualTypeToLog2IntRef(c, scope, rhs_qt, loc);4010 rhs_node = try Tag.int_cast.create(c.arena, rhs_node);
3972 rhs_node = try Tag.int_cast.create(c.arena, .{ .lhs = cast_to_type, .rhs = rhs_node });
3973 } else if (requires_int_cast) {4011 } else if (requires_int_cast) {
3974 rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node);4012 rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node);
3975 }4013 }
...@@ -4008,8 +4046,7 @@ fn transCreateCompoundAssign(...@@ -4008,8 +4046,7 @@ fn transCreateCompoundAssign(
4008 try block_scope.statements.append(assign);4046 try block_scope.statements.append(assign);
4009 } else {4047 } else {
4010 if (is_shift) {4048 if (is_shift) {
4011 const cast_to_type = try qualTypeToLog2IntRef(c, &block_scope.base, rhs_qt, loc);4049 rhs_node = try Tag.int_cast.create(c.arena, rhs_node);
4012 rhs_node = try Tag.int_cast.create(c.arena, .{ .lhs = cast_to_type, .rhs = rhs_node });
4013 } else if (requires_int_cast) {4050 } else if (requires_int_cast) {
4014 rhs_node = try transCCast(c, &block_scope.base, loc, lhs_qt, rhs_qt, rhs_node);4051 rhs_node = try transCCast(c, &block_scope.base, loc, lhs_qt, rhs_qt, rhs_node);
4015 }4052 }
...@@ -4029,7 +4066,10 @@ fn transCreateCompoundAssign(...@@ -4029,7 +4066,10 @@ fn transCreateCompoundAssign(
4029// Casting away const or volatile requires us to use @ptrFromInt4066// Casting away const or volatile requires us to use @ptrFromInt
4030fn removeCVQualifiers(c: *Context, dst_type_node: Node, expr: Node) Error!Node {4067fn removeCVQualifiers(c: *Context, dst_type_node: Node, expr: Node) Error!Node {
4031 const int_from_ptr = try Tag.int_from_ptr.create(c.arena, expr);4068 const int_from_ptr = try Tag.int_from_ptr.create(c.arena, expr);
4032 return Tag.ptr_from_int.create(c.arena, .{ .lhs = dst_type_node, .rhs = int_from_ptr });4069 return Tag.as.create(c.arena, .{
4070 .lhs = dst_type_node,
4071 .rhs = try Tag.ptr_from_int.create(c.arena, int_from_ptr),
4072 });
4033}4073}
40344074
4035fn transCPtrCast(4075fn transCPtrCast(
...@@ -4062,11 +4102,12 @@ fn transCPtrCast(...@@ -4062,11 +4102,12 @@ fn transCPtrCast(
4062 // For opaque types a ptrCast is enough4102 // For opaque types a ptrCast is enough
4063 expr4103 expr
4064 else blk: {4104 else blk: {
4065 const alignof = try Tag.std_meta_alignment.create(c.arena, dst_type_node);4105 break :blk try Tag.align_cast.create(c.arena, expr);
4066 const align_cast = try Tag.align_cast.create(c.arena, .{ .lhs = alignof, .rhs = expr });
4067 break :blk align_cast;
4068 };4106 };
4069 return Tag.ptr_cast.create(c.arena, .{ .lhs = dst_type_node, .rhs = rhs });4107 return Tag.as.create(c.arena, .{
4108 .lhs = dst_type_node,
4109 .rhs = try Tag.ptr_cast.create(c.arena, rhs),
4110 });
4070 }4111 }
4071}4112}
40724113
...@@ -4337,19 +4378,6 @@ fn qualTypeIntBitWidth(c: *Context, qt: clang.QualType) !u32 {...@@ -4337,19 +4378,6 @@ fn qualTypeIntBitWidth(c: *Context, qt: clang.QualType) !u32 {
4337 }4378 }
4338}4379}
43394380
4340fn qualTypeToLog2IntRef(c: *Context, scope: *Scope, qt: clang.QualType, source_loc: clang.SourceLocation) !Node {
4341 const int_bit_width = try qualTypeIntBitWidth(c, qt);
4342
4343 if (int_bit_width != 0) {
4344 // we can perform the log2 now.
4345 const cast_bit_width = math.log2_int(u64, int_bit_width);
4346 return Tag.log2_int_type.create(c.arena, cast_bit_width);
4347 }
4348
4349 const zig_type = try transQualType(c, scope, qt, source_loc);
4350 return Tag.std_math_Log2Int.create(c.arena, zig_type);
4351}
4352
4353fn qualTypeChildIsFnProto(qt: clang.QualType) bool {4381fn qualTypeChildIsFnProto(qt: clang.QualType) bool {
4354 const ty = qualTypeCanon(qt);4382 const ty = qualTypeCanon(qt);
43554383
...@@ -4731,14 +4759,12 @@ fn transCreateNodeShiftOp(...@@ -4731,14 +4759,12 @@ fn transCreateNodeShiftOp(
47314759
4732 const lhs_expr = stmt.getLHS();4760 const lhs_expr = stmt.getLHS();
4733 const rhs_expr = stmt.getRHS();4761 const rhs_expr = stmt.getRHS();
4734 const rhs_location = rhs_expr.getBeginLoc();
4735 // lhs >> @as(u5, rh)4762 // lhs >> @as(u5, rh)
47364763
4737 const lhs = try transExpr(c, scope, lhs_expr, .used);4764 const lhs = try transExpr(c, scope, lhs_expr, .used);
47384765
4739 const rhs_type = try qualTypeToLog2IntRef(c, scope, stmt.getType(), rhs_location);
4740 const rhs = try transExprCoercing(c, scope, rhs_expr, .used);4766 const rhs = try transExprCoercing(c, scope, rhs_expr, .used);
4741 const rhs_casted = try Tag.int_cast.create(c.arena, .{ .lhs = rhs_type, .rhs = rhs });4767 const rhs_casted = try Tag.int_cast.create(c.arena, rhs);
47424768
4743 return transCreateNodeInfixOp(c, op, lhs, rhs_casted, used);4769 return transCreateNodeInfixOp(c, op, lhs, rhs_casted, used);
4744}4770}
...@@ -6513,9 +6539,9 @@ fn parseCPostfixExpr(c: *Context, m: *MacroCtx, scope: *Scope, type_name: ?Node)...@@ -6513,9 +6539,9 @@ fn parseCPostfixExpr(c: *Context, m: *MacroCtx, scope: *Scope, type_name: ?Node)
6513 },6539 },
6514 .LBracket => {6540 .LBracket => {
6515 const index_val = try macroIntFromBool(c, try parseCExpr(c, m, scope));6541 const index_val = try macroIntFromBool(c, try parseCExpr(c, m, scope));
6516 const index = try Tag.int_cast.create(c.arena, .{6542 const index = try Tag.as.create(c.arena, .{
6517 .lhs = try Tag.type.create(c.arena, "usize"),6543 .lhs = try Tag.type.create(c.arena, "usize"),
6518 .rhs = index_val,6544 .rhs = try Tag.int_cast.create(c.arena, index_val),
6519 });6545 });
6520 node = try Tag.array_access.create(c.arena, .{ .lhs = node, .rhs = index });6546 node = try Tag.array_access.create(c.arena, .{ .lhs = node, .rhs = index });
6521 try m.skip(c, .RBracket);6547 try m.skip(c, .RBracket);
src/translate_c/ast.zig+27-61
...@@ -115,15 +115,10 @@ pub const Node = extern union {...@@ -115,15 +115,10 @@ pub const Node = extern union {
115115
116 /// @import("std").zig.c_builtins.<name>116 /// @import("std").zig.c_builtins.<name>
117 import_c_builtin,117 import_c_builtin,
118 log2_int_type,118 /// @intCast(operand)
119 /// @import("std").math.Log2Int(operand)
120 std_math_Log2Int,
121 /// @intCast(lhs, rhs)
122 int_cast,119 int_cast,
123 /// @import("std").zig.c_translation.promoteIntLiteral(value, type, base)120 /// @import("std").zig.c_translation.promoteIntLiteral(value, type, base)
124 helpers_promoteIntLiteral,121 helpers_promoteIntLiteral,
125 /// @import("std").meta.alignment(value)
126 std_meta_alignment,
127 /// @import("std").zig.c_translation.signedRemainder(lhs, rhs)122 /// @import("std").zig.c_translation.signedRemainder(lhs, rhs)
128 signed_remainder,123 signed_remainder,
129 /// @divTrunc(lhs, rhs)124 /// @divTrunc(lhs, rhs)
...@@ -132,23 +127,23 @@ pub const Node = extern union {...@@ -132,23 +127,23 @@ pub const Node = extern union {
132 int_from_bool,127 int_from_bool,
133 /// @as(lhs, rhs)128 /// @as(lhs, rhs)
134 as,129 as,
135 /// @truncate(lhs, rhs)130 /// @truncate(operand)
136 truncate,131 truncate,
137 /// @bitCast(lhs, rhs)132 /// @bitCast(operand)
138 bit_cast,133 bit_cast,
139 /// @floatCast(lhs, rhs)134 /// @floatCast(operand)
140 float_cast,135 float_cast,
141 /// @intFromFloat(lhs, rhs)136 /// @intFromFloat(operand)
142 int_from_float,137 int_from_float,
143 /// @floatFromInt(lhs, rhs)138 /// @floatFromInt(operand)
144 float_from_int,139 float_from_int,
145 /// @ptrFromInt(lhs, rhs)140 /// @ptrFromInt(operand)
146 ptr_from_int,141 ptr_from_int,
147 /// @intFromPtr(operand)142 /// @intFromPtr(operand)
148 int_from_ptr,143 int_from_ptr,
149 /// @alignCast(lhs, rhs)144 /// @alignCast(operand)
150 align_cast,145 align_cast,
151 /// @ptrCast(lhs, rhs)146 /// @ptrCast(operand)
152 ptr_cast,147 ptr_cast,
153 /// @divExact(lhs, rhs)148 /// @divExact(lhs, rhs)
154 div_exact,149 div_exact,
...@@ -254,7 +249,6 @@ pub const Node = extern union {...@@ -254,7 +249,6 @@ pub const Node = extern union {
254 .@"comptime",249 .@"comptime",
255 .@"defer",250 .@"defer",
256 .asm_simple,251 .asm_simple,
257 .std_math_Log2Int,
258 .negate,252 .negate,
259 .negate_wrap,253 .negate_wrap,
260 .bit_not,254 .bit_not,
...@@ -270,12 +264,20 @@ pub const Node = extern union {...@@ -270,12 +264,20 @@ pub const Node = extern union {
270 .switch_else,264 .switch_else,
271 .block_single,265 .block_single,
272 .helpers_sizeof,266 .helpers_sizeof,
273 .std_meta_alignment,
274 .int_from_bool,267 .int_from_bool,
275 .sizeof,268 .sizeof,
276 .alignof,269 .alignof,
277 .typeof,270 .typeof,
278 .typeinfo,271 .typeinfo,
272 .align_cast,
273 .truncate,
274 .bit_cast,
275 .float_cast,
276 .int_from_float,
277 .float_from_int,
278 .ptr_from_int,
279 .ptr_cast,
280 .int_cast,
279 => Payload.UnOp,281 => Payload.UnOp,
280282
281 .add,283 .add,
...@@ -314,24 +316,15 @@ pub const Node = extern union {...@@ -314,24 +316,15 @@ pub const Node = extern union {
314 .bit_xor_assign,316 .bit_xor_assign,
315 .div_trunc,317 .div_trunc,
316 .signed_remainder,318 .signed_remainder,
317 .int_cast,
318 .as,319 .as,
319 .truncate,
320 .bit_cast,
321 .float_cast,
322 .int_from_float,
323 .float_from_int,
324 .ptr_from_int,
325 .array_cat,320 .array_cat,
326 .ellipsis3,321 .ellipsis3,
327 .assign,322 .assign,
328 .align_cast,
329 .array_access,323 .array_access,
330 .std_mem_zeroinit,324 .std_mem_zeroinit,
331 .helpers_flexible_array_type,325 .helpers_flexible_array_type,
332 .helpers_shuffle_vector_index,326 .helpers_shuffle_vector_index,
333 .vector,327 .vector,
334 .ptr_cast,
335 .div_exact,328 .div_exact,
336 .offset_of,329 .offset_of,
337 .helpers_cast,330 .helpers_cast,
...@@ -367,7 +360,6 @@ pub const Node = extern union {...@@ -367,7 +360,6 @@ pub const Node = extern union {
367 .c_pointer, .single_pointer => Payload.Pointer,360 .c_pointer, .single_pointer => Payload.Pointer,
368 .array_type, .null_sentinel_array_type => Payload.Array,361 .array_type, .null_sentinel_array_type => Payload.Array,
369 .arg_redecl, .alias, .fail_decl => Payload.ArgRedecl,362 .arg_redecl, .alias, .fail_decl => Payload.ArgRedecl,
370 .log2_int_type => Payload.Log2IntType,
371 .var_simple, .pub_var_simple, .static_local_var, .mut_str => Payload.SimpleVarDecl,363 .var_simple, .pub_var_simple, .static_local_var, .mut_str => Payload.SimpleVarDecl,
372 .enum_constant => Payload.EnumConstant,364 .enum_constant => Payload.EnumConstant,
373 .array_filler => Payload.ArrayFiller,365 .array_filler => Payload.ArrayFiller,
...@@ -644,11 +636,6 @@ pub const Payload = struct {...@@ -644,11 +636,6 @@ pub const Payload = struct {
644 },636 },
645 };637 };
646638
647 pub const Log2IntType = struct {
648 base: Payload,
649 data: std.math.Log2Int(u64),
650 };
651
652 pub const SimpleVarDecl = struct {639 pub const SimpleVarDecl = struct {
653 base: Payload,640 base: Payload,
654 data: struct {641 data: struct {
...@@ -885,11 +872,6 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -885,11 +872,6 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
885 try c.buf.append('\n');872 try c.buf.append('\n');
886 return @as(NodeIndex, 0); // error: integer value 0 cannot be coerced to type 'std.mem.Allocator.Error!u32'873 return @as(NodeIndex, 0); // error: integer value 0 cannot be coerced to type 'std.mem.Allocator.Error!u32'
887 },874 },
888 .std_math_Log2Int => {
889 const payload = node.castTag(.std_math_Log2Int).?.data;
890 const import_node = try renderStdImport(c, &.{ "math", "Log2Int" });
891 return renderCall(c, import_node, &.{payload});
892 },
893 .helpers_cast => {875 .helpers_cast => {
894 const payload = node.castTag(.helpers_cast).?.data;876 const payload = node.castTag(.helpers_cast).?.data;
895 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "cast" });877 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "cast" });
...@@ -900,11 +882,6 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -900,11 +882,6 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
900 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "promoteIntLiteral" });882 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "promoteIntLiteral" });
901 return renderCall(c, import_node, &.{ payload.type, payload.value, payload.base });883 return renderCall(c, import_node, &.{ payload.type, payload.value, payload.base });
902 },884 },
903 .std_meta_alignment => {
904 const payload = node.castTag(.std_meta_alignment).?.data;
905 const import_node = try renderStdImport(c, &.{ "meta", "alignment" });
906 return renderCall(c, import_node, &.{payload});
907 },
908 .helpers_sizeof => {885 .helpers_sizeof => {
909 const payload = node.castTag(.helpers_sizeof).?.data;886 const payload = node.castTag(.helpers_sizeof).?.data;
910 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "sizeof" });887 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "sizeof" });
...@@ -1081,14 +1058,6 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1081,14 +1058,6 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1081 .data = undefined,1058 .data = undefined,
1082 });1059 });
1083 },1060 },
1084 .log2_int_type => {
1085 const payload = node.castTag(.log2_int_type).?.data;
1086 return c.addNode(.{
1087 .tag = .identifier,
1088 .main_token = try c.addTokenFmt(.identifier, "u{d}", .{payload}),
1089 .data = undefined,
1090 });
1091 },
1092 .identifier => {1061 .identifier => {
1093 const payload = node.castTag(.identifier).?.data;1062 const payload = node.castTag(.identifier).?.data;
1094 return c.addNode(.{1063 return c.addNode(.{
...@@ -1344,7 +1313,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1344,7 +1313,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1344 },1313 },
1345 .int_cast => {1314 .int_cast => {
1346 const payload = node.castTag(.int_cast).?.data;1315 const payload = node.castTag(.int_cast).?.data;
1347 return renderBuiltinCall(c, "@intCast", &.{ payload.lhs, payload.rhs });1316 return renderBuiltinCall(c, "@intCast", &.{payload});
1348 },1317 },
1349 .signed_remainder => {1318 .signed_remainder => {
1350 const payload = node.castTag(.signed_remainder).?.data;1319 const payload = node.castTag(.signed_remainder).?.data;
...@@ -1365,27 +1334,27 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1365,27 +1334,27 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1365 },1334 },
1366 .truncate => {1335 .truncate => {
1367 const payload = node.castTag(.truncate).?.data;1336 const payload = node.castTag(.truncate).?.data;
1368 return renderBuiltinCall(c, "@truncate", &.{ payload.lhs, payload.rhs });1337 return renderBuiltinCall(c, "@truncate", &.{payload});
1369 },1338 },
1370 .bit_cast => {1339 .bit_cast => {
1371 const payload = node.castTag(.bit_cast).?.data;1340 const payload = node.castTag(.bit_cast).?.data;
1372 return renderBuiltinCall(c, "@bitCast", &.{ payload.lhs, payload.rhs });1341 return renderBuiltinCall(c, "@bitCast", &.{payload});
1373 },1342 },
1374 .float_cast => {1343 .float_cast => {
1375 const payload = node.castTag(.float_cast).?.data;1344 const payload = node.castTag(.float_cast).?.data;
1376 return renderBuiltinCall(c, "@floatCast", &.{ payload.lhs, payload.rhs });1345 return renderBuiltinCall(c, "@floatCast", &.{payload});
1377 },1346 },
1378 .int_from_float => {1347 .int_from_float => {
1379 const payload = node.castTag(.int_from_float).?.data;1348 const payload = node.castTag(.int_from_float).?.data;
1380 return renderBuiltinCall(c, "@intFromFloat", &.{ payload.lhs, payload.rhs });1349 return renderBuiltinCall(c, "@intFromFloat", &.{payload});
1381 },1350 },
1382 .float_from_int => {1351 .float_from_int => {
1383 const payload = node.castTag(.float_from_int).?.data;1352 const payload = node.castTag(.float_from_int).?.data;
1384 return renderBuiltinCall(c, "@floatFromInt", &.{ payload.lhs, payload.rhs });1353 return renderBuiltinCall(c, "@floatFromInt", &.{payload});
1385 },1354 },
1386 .ptr_from_int => {1355 .ptr_from_int => {
1387 const payload = node.castTag(.ptr_from_int).?.data;1356 const payload = node.castTag(.ptr_from_int).?.data;
1388 return renderBuiltinCall(c, "@ptrFromInt", &.{ payload.lhs, payload.rhs });1357 return renderBuiltinCall(c, "@ptrFromInt", &.{payload});
1389 },1358 },
1390 .int_from_ptr => {1359 .int_from_ptr => {
1391 const payload = node.castTag(.int_from_ptr).?.data;1360 const payload = node.castTag(.int_from_ptr).?.data;
...@@ -1393,11 +1362,11 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1393,11 +1362,11 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1393 },1362 },
1394 .align_cast => {1363 .align_cast => {
1395 const payload = node.castTag(.align_cast).?.data;1364 const payload = node.castTag(.align_cast).?.data;
1396 return renderBuiltinCall(c, "@alignCast", &.{ payload.lhs, payload.rhs });1365 return renderBuiltinCall(c, "@alignCast", &.{payload});
1397 },1366 },
1398 .ptr_cast => {1367 .ptr_cast => {
1399 const payload = node.castTag(.ptr_cast).?.data;1368 const payload = node.castTag(.ptr_cast).?.data;
1400 return renderBuiltinCall(c, "@ptrCast", &.{ payload.lhs, payload.rhs });1369 return renderBuiltinCall(c, "@ptrCast", &.{payload});
1401 },1370 },
1402 .div_exact => {1371 .div_exact => {
1403 const payload = node.castTag(.div_exact).?.data;1372 const payload = node.castTag(.div_exact).?.data;
...@@ -2330,14 +2299,11 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {...@@ -2330,14 +2299,11 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
2330 .float_from_int,2299 .float_from_int,
2331 .ptr_from_int,2300 .ptr_from_int,
2332 .std_mem_zeroes,2301 .std_mem_zeroes,
2333 .std_math_Log2Int,
2334 .log2_int_type,
2335 .int_from_ptr,2302 .int_from_ptr,
2336 .sizeof,2303 .sizeof,
2337 .alignof,2304 .alignof,
2338 .typeof,2305 .typeof,
2339 .typeinfo,2306 .typeinfo,
2340 .std_meta_alignment,
2341 .vector,2307 .vector,
2342 .helpers_sizeof,2308 .helpers_sizeof,
2343 .helpers_cast,2309 .helpers_cast,