| ... | ... | @@ -50,6 +50,8 @@ pub const CValue = union(enum) { |
| 50 | 50 | /// Render these bytes literally. |
| 51 | 51 | /// TODO make this a [*:0]const u8 to save memory |
| 52 | 52 | bytes: []const u8, |
| 53 | /// Index of an instruction that should later be rendered inline. |
| 54 | inline_index: Air.Inst.Index, |
| 53 | 55 | }; |
| 54 | 56 | |
| 55 | 57 | const BlockData = struct { |
| ... | ... | @@ -79,6 +81,7 @@ const ValueRenderLocation = enum { |
| 79 | 81 | FunctionArgument, |
| 80 | 82 | Initializer, |
| 81 | 83 | Other, |
| 84 | condition, |
| 82 | 85 | }; |
| 83 | 86 | |
| 84 | 87 | const BuiltinInfo = enum { |
| ... | ... | @@ -278,6 +281,19 @@ pub const Function = struct { |
| 278 | 281 | return result; |
| 279 | 282 | } |
| 280 | 283 | |
| 284 | fn resolveInstNoInline(f: *Function, inst: Air.Inst.Ref) !CValue { |
| 285 | const operand = try f.resolveInst(inst); |
| 286 | if (operand != .inline_index) return operand; |
| 287 | |
| 288 | const inst_ty = f.air.typeOf(inst); |
| 289 | const writer = f.object.writer(); |
| 290 | const local = try f.allocLocal(inst_ty, .Const); |
| 291 | try writer.writeAll(" = "); |
| 292 | try f.writeCValueInline(operand.inline_index); |
| 293 | try writer.writeAll(";\n"); |
| 294 | return local; |
| 295 | } |
| 296 | |
| 281 | 297 | fn wantSafety(f: *Function) bool { |
| 282 | 298 | return switch (f.object.dg.module.optimizeMode()) { |
| 283 | 299 | .Debug, .ReleaseSafe => true, |
| ... | ... | @@ -313,10 +329,95 @@ pub const Function = struct { |
| 313 | 329 | .constant => |inst| { |
| 314 | 330 | const ty = f.air.typeOf(inst); |
| 315 | 331 | const val = f.air.value(inst).?; |
| 316 | | return f.object.dg.renderValue(w, ty, val, location); |
| 332 | try f.object.dg.renderValue(w, ty, val, location); |
| 333 | }, |
| 334 | .undef => |ty| try f.object.dg.renderValue(w, ty, Value.undef, location), |
| 335 | .inline_index => |node| { |
| 336 | if (location != .condition) try w.writeByte('('); |
| 337 | try f.writeCValueInline(node); |
| 338 | if (location != .condition) try w.writeByte(')'); |
| 317 | 339 | }, |
| 318 | | .undef => |ty| return f.object.dg.renderValue(w, ty, Value.undef, location), |
| 319 | | else => return f.object.dg.writeCValue(w, c_value), |
| 340 | else => try f.object.dg.writeCValue(w, c_value), |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | const E = error{ OutOfMemory, AnalysisFail }; |
| 345 | |
| 346 | fn writeCValueInline(f: *Function, inst: Air.Inst.Index) E!void { |
| 347 | switch (f.air.instructions.items(.tag)[inst]) { |
| 348 | // zig fmt: off |
| 349 | // TODO use a different strategy for add, sub, mul, div |
| 350 | // that communicates to the optimizer that wrapping is UB. |
| 351 | .add => try airBinOp(f, inst, "+", "add", .None), |
| 352 | .sub => try airBinOp(f, inst, "-", "sub", .None), |
| 353 | .mul => try airBinOp(f, inst, "*", "mul", .None), |
| 354 | |
| 355 | .div_float => try airBinBuiltinCall(f, inst, "div", .None), |
| 356 | |
| 357 | .div_trunc, .div_exact => try airBinOp(f, inst, "/", "div_trunc", .None), |
| 358 | .rem => { |
| 359 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 360 | const lhs_ty = f.air.typeOf(bin_op.lhs); |
| 361 | // For binary operations @TypeOf(lhs)==@TypeOf(rhs), |
| 362 | // so we only check one. |
| 363 | if (lhs_ty.isInt()) |
| 364 | try airBinOp(f, inst, "%", "rem", .None) |
| 365 | else |
| 366 | try airBinFloatOp(f, inst, "fmod"); |
| 367 | }, |
| 368 | .div_floor => try airBinBuiltinCall(f, inst, "div_floor", .None), |
| 369 | .mod => try airBinBuiltinCall(f, inst, "mod", .None), |
| 370 | |
| 371 | .addwrap => try airBinBuiltinCall(f, inst, "addw", .Bits), |
| 372 | .subwrap => try airBinBuiltinCall(f, inst, "subw", .Bits), |
| 373 | .mulwrap => try airBinBuiltinCall(f, inst, "mulw", .Bits), |
| 374 | |
| 375 | .add_sat => try airBinBuiltinCall(f, inst, "adds", .Bits), |
| 376 | .sub_sat => try airBinBuiltinCall(f, inst, "subs", .Bits), |
| 377 | .mul_sat => try airBinBuiltinCall(f, inst, "muls", .Bits), |
| 378 | .shl_sat => try airBinBuiltinCall(f, inst, "shls", .Bits), |
| 379 | |
| 380 | .min => try airMinMax(f, inst, '<', "fmin"), |
| 381 | .max => try airMinMax(f, inst, '>', "fmax"), |
| 382 | |
| 383 | .cmp_gt => try airCmpOp(f, inst, ">", "gt"), |
| 384 | .cmp_gte => try airCmpOp(f, inst, ">=", "ge"), |
| 385 | .cmp_lt => try airCmpOp(f, inst, "<", "lt"), |
| 386 | .cmp_lte => try airCmpOp(f, inst, "<=", "le"), |
| 387 | |
| 388 | .cmp_eq => try airEquality(f, inst, "((", "==", "eq"), |
| 389 | .cmp_neq => try airEquality(f, inst, "!((", "!=", "ne"), |
| 390 | |
| 391 | .bool_and, .bit_and => try airBinOp(f, inst, "&", "and", .None), |
| 392 | .bool_or, .bit_or => try airBinOp(f, inst, "|", "or", .None), |
| 393 | .xor => try airBinOp(f, inst, "^", "xor", .None), |
| 394 | .shr, .shr_exact => try airBinBuiltinCall(f, inst, "shr", .None), |
| 395 | .shl, => try airBinBuiltinCall(f, inst, "shlw", .Bits), |
| 396 | .shl_exact => try airBinOp(f, inst, "<<", "shl", .None), |
| 397 | .not => try airNot (f, inst), |
| 398 | |
| 399 | .is_err => try airIsErr(f, inst, false, "!="), |
| 400 | .is_non_err => try airIsErr(f, inst, false, "=="), |
| 401 | .is_err_ptr => try airIsErr(f, inst, true, "!="), |
| 402 | .is_non_err_ptr => try airIsErr(f, inst, true, "=="), |
| 403 | |
| 404 | .is_null => try airIsNull(f, inst, "==", false), |
| 405 | .is_non_null => try airIsNull(f, inst, "!=", false), |
| 406 | .is_null_ptr => try airIsNull(f, inst, "==", true), |
| 407 | .is_non_null_ptr => try airIsNull(f, inst, "!=", true), |
| 408 | |
| 409 | .get_union_tag => try airGetUnionTag(f, inst), |
| 410 | .clz => try airUnBuiltinCall(f, inst, "clz", .Bits), |
| 411 | .ctz => try airUnBuiltinCall(f, inst, "ctz", .Bits), |
| 412 | .popcount => try airUnBuiltinCall(f, inst, "popcount", .Bits), |
| 413 | .byte_swap => try airUnBuiltinCall(f, inst, "byte_swap", .Bits), |
| 414 | .bit_reverse => try airUnBuiltinCall(f, inst, "bit_reverse", .Bits), |
| 415 | .tag_name => try airTagName(f, inst), |
| 416 | .error_name => try airErrorName(f, inst), |
| 417 | |
| 418 | .ptrtoint => try airPtrToInt(f, inst), |
| 419 | else => unreachable, |
| 420 | // zig fmt: on |
| 320 | 421 | } |
| 321 | 422 | } |
| 322 | 423 | |
| ... | ... | @@ -342,6 +443,7 @@ pub const Function = struct { |
| 342 | 443 | try w.writeByte('.'); |
| 343 | 444 | return f.writeCValue(w, member, .Other); |
| 344 | 445 | }, |
| 446 | .inline_index => unreachable, // Use resolveInstNoInline |
| 345 | 447 | else => return f.object.dg.writeCValueMember(w, c_value, member), |
| 346 | 448 | } |
| 347 | 449 | } |
| ... | ... | @@ -542,9 +644,14 @@ pub const DeclGen = struct { |
| 542 | 644 | return dg.renderParentPtr(writer, field_ptr.container_ptr, host_ty); |
| 543 | 645 | }, |
| 544 | 646 | }, |
| 545 | | .Union => FieldInfo{ |
| 546 | | .name = container_ty.unionFields().keys()[index], |
| 547 | | .ty = container_ty.unionFields().values()[index].ty, |
| 647 | .Union => switch (container_ty.containerLayout()) { |
| 648 | .Auto, .Extern => FieldInfo{ |
| 649 | .name = container_ty.unionFields().keys()[index], |
| 650 | .ty = container_ty.unionFields().values()[index].ty, |
| 651 | }, |
| 652 | .Packed => { |
| 653 | return dg.renderParentPtr(writer, field_ptr.container_ptr, ptr_ty); |
| 654 | }, |
| 548 | 655 | }, |
| 549 | 656 | .Pointer => field_info: { |
| 550 | 657 | assert(container_ty.isSlice()); |
| ... | ... | @@ -1165,6 +1272,27 @@ pub const DeclGen = struct { |
| 1165 | 1272 | try writer.writeByte(')'); |
| 1166 | 1273 | } |
| 1167 | 1274 | |
| 1275 | const index = ty.unionTagFieldIndex(union_obj.tag, dg.module).?; |
| 1276 | const field_ty = ty.unionFields().values()[index].ty; |
| 1277 | const field_name = ty.unionFields().keys()[index]; |
| 1278 | if (ty.containerLayout() == .Packed) { |
| 1279 | if (field_ty.hasRuntimeBits()) { |
| 1280 | if (field_ty.isPtrAtRuntime()) { |
| 1281 | try writer.writeByte('('); |
| 1282 | try dg.renderTypecast(writer, ty); |
| 1283 | try writer.writeByte(')'); |
| 1284 | } else if (field_ty.zigTypeTag() == .Float) { |
| 1285 | try writer.writeByte('('); |
| 1286 | try dg.renderTypecast(writer, ty); |
| 1287 | try writer.writeByte(')'); |
| 1288 | } |
| 1289 | try dg.renderValue(writer, field_ty, union_obj.val, .Initializer); |
| 1290 | } else { |
| 1291 | try writer.writeAll("0"); |
| 1292 | } |
| 1293 | return; |
| 1294 | } |
| 1295 | |
| 1168 | 1296 | try writer.writeByte('{'); |
| 1169 | 1297 | if (ty.unionTagTypeSafety()) |tag_ty| { |
| 1170 | 1298 | const layout = ty.unionGetLayout(target); |
| ... | ... | @@ -1176,9 +1304,6 @@ pub const DeclGen = struct { |
| 1176 | 1304 | try writer.writeAll(".payload = {"); |
| 1177 | 1305 | } |
| 1178 | 1306 | |
| 1179 | | const index = ty.unionTagFieldIndex(union_obj.tag, dg.module).?; |
| 1180 | | const field_ty = ty.unionFields().values()[index].ty; |
| 1181 | | const field_name = ty.unionFields().keys()[index]; |
| 1182 | 1307 | var it = ty.unionFields().iterator(); |
| 1183 | 1308 | if (field_ty.hasRuntimeBits()) { |
| 1184 | 1309 | try writer.print(".{ } = ", .{fmtIdent(field_name)}); |
| ... | ... | @@ -1445,7 +1570,7 @@ pub const DeclGen = struct { |
| 1445 | 1570 | if (field_id == 0) try buffer.appendSlice(" char empty_tuple;\n"); |
| 1446 | 1571 | } |
| 1447 | 1572 | const name_begin = buffer.items.len + "} ".len; |
| 1448 | | try buffer.writer().print("}} zig_T_{};\n", .{typeToCIdentifier(t, dg.module)}); |
| 1573 | try buffer.writer().print("}} zig_T_{}_{d};\n", .{ typeToCIdentifier(t, dg.module), @truncate(u16, t.hash(dg.module)) }); |
| 1449 | 1574 | const name_end = buffer.items.len - ";\n".len; |
| 1450 | 1575 | |
| 1451 | 1576 | const rendered = try buffer.toOwnedSlice(); |
| ... | ... | @@ -1794,9 +1919,17 @@ pub const DeclGen = struct { |
| 1794 | 1919 | |
| 1795 | 1920 | return w.writeAll(name); |
| 1796 | 1921 | }, |
| 1797 | | .Struct, .Union => |tag| if (tag == .Struct and t.containerLayout() == .Packed) |
| 1798 | | try dg.renderType(w, t.castTag(.@"struct").?.data.backing_int_ty, kind) |
| 1799 | | else if (t.isSimpleTupleOrAnonStruct()) { |
| 1922 | .Struct, .Union => |tag| if (t.containerLayout() == .Packed) { |
| 1923 | if (t.castTag(.@"struct")) |struct_obj| { |
| 1924 | try dg.renderType(w, struct_obj.data.backing_int_ty, kind); |
| 1925 | } else { |
| 1926 | var buf: Type.Payload.Bits = .{ |
| 1927 | .base = .{ .tag = .int_unsigned }, |
| 1928 | .data = @intCast(u16, t.bitSize(target)), |
| 1929 | }; |
| 1930 | try dg.renderType(w, Type.initPayload(&buf.base), kind); |
| 1931 | } |
| 1932 | } else if (t.isSimpleTupleOrAnonStruct()) { |
| 1800 | 1933 | const ExpectedContents = struct { types: [8]Type, values: [8]Value }; |
| 1801 | 1934 | var stack align(@alignOf(ExpectedContents)) = |
| 1802 | 1935 | std.heap.stackFallback(@sizeOf(ExpectedContents), dg.gpa); |
| ... | ... | @@ -1961,7 +2094,7 @@ pub const DeclGen = struct { |
| 1961 | 2094 | try buffer.appendSlice("static "); |
| 1962 | 2095 | try dg.renderType(bw, name_slice_ty, .Complete); |
| 1963 | 2096 | const name_begin = buffer.items.len + " ".len; |
| 1964 | | try bw.print(" zig_tagName_{}(", .{typeToCIdentifier(enum_ty, dg.module)}); |
| 2097 | try bw.print(" zig_tagName_{}_{d}(", .{ typeToCIdentifier(enum_ty, dg.module), @enumToInt(enum_ty.getOwnerDecl()) }); |
| 1965 | 2098 | const name_end = buffer.items.len - "(".len; |
| 1966 | 2099 | try dg.renderTypeAndName(bw, enum_ty, .{ .identifier = "tag" }, .Const, 0, .Complete); |
| 1967 | 2100 | try buffer.appendSlice(") {\n switch (tag) {\n"); |
| ... | ... | @@ -2041,7 +2174,7 @@ pub const DeclGen = struct { |
| 2041 | 2174 | |
| 2042 | 2175 | fn writeCValue(dg: *DeclGen, w: anytype, c_value: CValue) !void { |
| 2043 | 2176 | switch (c_value) { |
| 2044 | | .none => unreachable, |
| 2177 | .none, .inline_index => unreachable, |
| 2045 | 2178 | .local => |i| return w.print("t{d}", .{i}), |
| 2046 | 2179 | .local_ref => |i| return w.print("&t{d}", .{i}), |
| 2047 | 2180 | .constant => unreachable, |
| ... | ... | @@ -2060,7 +2193,7 @@ pub const DeclGen = struct { |
| 2060 | 2193 | |
| 2061 | 2194 | fn writeCValueDeref(dg: *DeclGen, w: anytype, c_value: CValue) !void { |
| 2062 | 2195 | switch (c_value) { |
| 2063 | | .none => unreachable, |
| 2196 | .none, .inline_index => unreachable, |
| 2064 | 2197 | .local => |i| return w.print("(*t{d})", .{i}), |
| 2065 | 2198 | .local_ref => |i| return w.print("t{d}", .{i}), |
| 2066 | 2199 | .constant => unreachable, |
| ... | ... | @@ -2090,7 +2223,7 @@ pub const DeclGen = struct { |
| 2090 | 2223 | |
| 2091 | 2224 | fn writeCValueDerefMember(dg: *DeclGen, writer: anytype, c_value: CValue, member: CValue) !void { |
| 2092 | 2225 | switch (c_value) { |
| 2093 | | .none, .constant, .field, .undef => unreachable, |
| 2226 | .none, .constant, .field, .undef, .inline_index => unreachable, |
| 2094 | 2227 | .local, .arg, .decl, .identifier, .bytes => { |
| 2095 | 2228 | try dg.writeCValue(writer, c_value); |
| 2096 | 2229 | try writer.writeAll("->"); |
| ... | ... | @@ -2111,11 +2244,16 @@ pub const DeclGen = struct { |
| 2111 | 2244 | return writer.writeAll(exports.items[0].options.name); |
| 2112 | 2245 | } else if (decl.isExtern()) { |
| 2113 | 2246 | return writer.writeAll(mem.sliceTo(decl.name, 0)); |
| 2247 | } else if (dg.module.test_functions.get(decl_index)) |_| { |
| 2248 | const gpa = dg.gpa; |
| 2249 | const name = try decl.getFullyQualifiedName(dg.module); |
| 2250 | defer gpa.free(name); |
| 2251 | return writer.print("{}_{d}", .{ fmtIdent(name), @enumToInt(decl_index) }); |
| 2114 | 2252 | } else { |
| 2115 | 2253 | const gpa = dg.gpa; |
| 2116 | 2254 | const name = try decl.getFullyQualifiedName(dg.module); |
| 2117 | 2255 | defer gpa.free(name); |
| 2118 | | return writer.print("{ }", .{fmtIdent(name)}); |
| 2256 | return writer.print("{}", .{fmtIdent(name)}); |
| 2119 | 2257 | } |
| 2120 | 2258 | } |
| 2121 | 2259 | |
| ... | ... | @@ -2401,37 +2539,26 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO |
| 2401 | 2539 | .ptr_add => try airPtrAddSub(f, inst, '+'), |
| 2402 | 2540 | .ptr_sub => try airPtrAddSub(f, inst, '-'), |
| 2403 | 2541 | |
| 2404 | | // TODO use a different strategy for add, sub, mul, div |
| 2405 | | // that communicates to the optimizer that wrapping is UB. |
| 2406 | | .add => try airBinOp(f, inst, "+", "add", .None), |
| 2407 | | .sub => try airBinOp(f, inst, "-", "sub", .None), |
| 2408 | | .mul => try airBinOp(f, inst, "*", "mul", .None), |
| 2542 | .add => CValue{ .inline_index = inst }, |
| 2543 | .sub => CValue{ .inline_index = inst }, |
| 2544 | .mul => CValue{ .inline_index = inst }, |
| 2409 | 2545 | |
| 2410 | 2546 | .neg => try airFloatNeg(f, inst), |
| 2411 | | .div_float => try airBinBuiltinCall(f, inst, "div", .None), |
| 2547 | .div_float => CValue{ .inline_index = inst }, |
| 2412 | 2548 | |
| 2413 | | .div_trunc, .div_exact => try airBinOp(f, inst, "/", "div_trunc", .None), |
| 2414 | | .rem => blk: { |
| 2415 | | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 2416 | | const lhs_ty = f.air.typeOf(bin_op.lhs); |
| 2417 | | // For binary operations @TypeOf(lhs)==@TypeOf(rhs), |
| 2418 | | // so we only check one. |
| 2419 | | break :blk if (lhs_ty.isInt()) |
| 2420 | | try airBinOp(f, inst, "%", "rem", .None) |
| 2421 | | else |
| 2422 | | try airBinFloatOp(f, inst, "fmod"); |
| 2423 | | }, |
| 2424 | | .div_floor => try airBinBuiltinCall(f, inst, "div_floor", .None), |
| 2425 | | .mod => try airBinBuiltinCall(f, inst, "mod", .None), |
| 2549 | .div_trunc, .div_exact => CValue{ .inline_index = inst }, |
| 2550 | .rem => CValue{ .inline_index = inst }, |
| 2551 | .div_floor => CValue{ .inline_index = inst }, |
| 2552 | .mod => CValue{ .inline_index = inst }, |
| 2426 | 2553 | |
| 2427 | | .addwrap => try airBinBuiltinCall(f, inst, "addw", .Bits), |
| 2428 | | .subwrap => try airBinBuiltinCall(f, inst, "subw", .Bits), |
| 2429 | | .mulwrap => try airBinBuiltinCall(f, inst, "mulw", .Bits), |
| 2554 | .addwrap => CValue{ .inline_index = inst }, |
| 2555 | .subwrap => CValue{ .inline_index = inst }, |
| 2556 | .mulwrap => CValue{ .inline_index = inst }, |
| 2430 | 2557 | |
| 2431 | | .add_sat => try airBinBuiltinCall(f, inst, "adds", .Bits), |
| 2432 | | .sub_sat => try airBinBuiltinCall(f, inst, "subs", .Bits), |
| 2433 | | .mul_sat => try airBinBuiltinCall(f, inst, "muls", .Bits), |
| 2434 | | .shl_sat => try airBinBuiltinCall(f, inst, "shls", .Bits), |
| 2558 | .add_sat => CValue{ .inline_index = inst }, |
| 2559 | .sub_sat => CValue{ .inline_index = inst }, |
| 2560 | .mul_sat => CValue{ .inline_index = inst }, |
| 2561 | .shl_sat => CValue{ .inline_index = inst }, |
| 2435 | 2562 | |
| 2436 | 2563 | .sqrt, |
| 2437 | 2564 | .sin, |
| ... | ... | @@ -2456,45 +2583,45 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO |
| 2456 | 2583 | .mul_with_overflow => try airOverflow(f, inst, "mul", .Bits), |
| 2457 | 2584 | .shl_with_overflow => try airOverflow(f, inst, "shl", .Bits), |
| 2458 | 2585 | |
| 2459 | | .min => try airMinMax(f, inst, '<', "fmin"), |
| 2460 | | .max => try airMinMax(f, inst, '>', "fmax"), |
| 2586 | .min => CValue{ .inline_index = inst }, |
| 2587 | .max => CValue{ .inline_index = inst }, |
| 2461 | 2588 | |
| 2462 | 2589 | .slice => try airSlice(f, inst), |
| 2463 | 2590 | |
| 2464 | | .cmp_gt => try airCmpOp(f, inst, ">", "gt"), |
| 2465 | | .cmp_gte => try airCmpOp(f, inst, ">=", "ge"), |
| 2466 | | .cmp_lt => try airCmpOp(f, inst, "<", "lt"), |
| 2467 | | .cmp_lte => try airCmpOp(f, inst, "<=", "le"), |
| 2591 | .cmp_gt => CValue{ .inline_index = inst }, |
| 2592 | .cmp_gte => CValue{ .inline_index = inst }, |
| 2593 | .cmp_lt => CValue{ .inline_index = inst }, |
| 2594 | .cmp_lte => CValue{ .inline_index = inst }, |
| 2468 | 2595 | |
| 2469 | | .cmp_eq => try airEquality(f, inst, "((", "==", "eq"), |
| 2470 | | .cmp_neq => try airEquality(f, inst, "!((", "!=", "ne"), |
| 2596 | .cmp_eq => CValue{ .inline_index = inst }, |
| 2597 | .cmp_neq => CValue{ .inline_index = inst }, |
| 2471 | 2598 | |
| 2472 | 2599 | .cmp_vector => return f.fail("TODO: C backend: implement cmp_vector", .{}), |
| 2473 | 2600 | .cmp_lt_errors_len => try airCmpLtErrorsLen(f, inst), |
| 2474 | 2601 | |
| 2475 | 2602 | // bool_and and bool_or are non-short-circuit operations |
| 2476 | | .bool_and, .bit_and => try airBinOp(f, inst, "&", "and", .None), |
| 2477 | | .bool_or, .bit_or => try airBinOp(f, inst, "|", "or", .None), |
| 2478 | | .xor => try airBinOp(f, inst, "^", "xor", .None), |
| 2479 | | .shr, .shr_exact => try airBinBuiltinCall(f, inst, "shr", .None), |
| 2480 | | .shl, => try airBinBuiltinCall(f, inst, "shlw", .Bits), |
| 2481 | | .shl_exact => try airBinOp(f, inst, "<<", "shl", .None), |
| 2482 | | .not => try airNot (f, inst), |
| 2603 | .bool_and, .bit_and => CValue{ .inline_index = inst }, |
| 2604 | .bool_or, .bit_or => CValue{ .inline_index = inst }, |
| 2605 | .xor => CValue{ .inline_index = inst }, |
| 2606 | .shr, .shr_exact => CValue{ .inline_index = inst }, |
| 2607 | .shl, => CValue{ .inline_index = inst }, |
| 2608 | .shl_exact => CValue{ .inline_index = inst }, |
| 2609 | .not => CValue{ .inline_index = inst }, |
| 2483 | 2610 | |
| 2484 | 2611 | .optional_payload => try airOptionalPayload(f, inst), |
| 2485 | 2612 | .optional_payload_ptr => try airOptionalPayloadPtr(f, inst), |
| 2486 | 2613 | .optional_payload_ptr_set => try airOptionalPayloadPtrSet(f, inst), |
| 2487 | 2614 | .wrap_optional => try airWrapOptional(f, inst), |
| 2488 | 2615 | |
| 2489 | | .is_err => try airIsErr(f, inst, false, "!="), |
| 2490 | | .is_non_err => try airIsErr(f, inst, false, "=="), |
| 2491 | | .is_err_ptr => try airIsErr(f, inst, true, "!="), |
| 2492 | | .is_non_err_ptr => try airIsErr(f, inst, true, "=="), |
| 2616 | .is_err => CValue{ .inline_index = inst }, |
| 2617 | .is_non_err => CValue{ .inline_index = inst }, |
| 2618 | .is_err_ptr => CValue{ .inline_index = inst }, |
| 2619 | .is_non_err_ptr => CValue{ .inline_index = inst }, |
| 2493 | 2620 | |
| 2494 | | .is_null => try airIsNull(f, inst, "==", false), |
| 2495 | | .is_non_null => try airIsNull(f, inst, "!=", false), |
| 2496 | | .is_null_ptr => try airIsNull(f, inst, "==", true), |
| 2497 | | .is_non_null_ptr => try airIsNull(f, inst, "!=", true), |
| 2621 | .is_null => CValue{ .inline_index = inst }, |
| 2622 | .is_non_null => CValue{ .inline_index = inst }, |
| 2623 | .is_null_ptr => CValue{ .inline_index = inst }, |
| 2624 | .is_non_null_ptr => CValue{ .inline_index = inst }, |
| 2498 | 2625 | |
| 2499 | 2626 | .alloc => try airAlloc(f, inst), |
| 2500 | 2627 | .ret_ptr => try airRetPtr(f, inst), |
| ... | ... | @@ -2522,14 +2649,14 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO |
| 2522 | 2649 | .memset => try airMemset(f, inst), |
| 2523 | 2650 | .memcpy => try airMemcpy(f, inst), |
| 2524 | 2651 | .set_union_tag => try airSetUnionTag(f, inst), |
| 2525 | | .get_union_tag => try airGetUnionTag(f, inst), |
| 2526 | | .clz => try airUnBuiltinCall(f, inst, "clz", .Bits), |
| 2527 | | .ctz => try airUnBuiltinCall(f, inst, "ctz", .Bits), |
| 2528 | | .popcount => try airUnBuiltinCall(f, inst, "popcount", .Bits), |
| 2529 | | .byte_swap => try airUnBuiltinCall(f, inst, "byte_swap", .Bits), |
| 2530 | | .bit_reverse => try airUnBuiltinCall(f, inst, "bit_reverse", .Bits), |
| 2531 | | .tag_name => try airTagName(f, inst), |
| 2532 | | .error_name => try airErrorName(f, inst), |
| 2652 | .get_union_tag => CValue{ .inline_index = inst }, |
| 2653 | .clz => CValue{ .inline_index = inst }, |
| 2654 | .ctz => CValue{ .inline_index = inst }, |
| 2655 | .popcount => CValue{ .inline_index = inst }, |
| 2656 | .byte_swap => CValue{ .inline_index = inst }, |
| 2657 | .bit_reverse => CValue{ .inline_index = inst }, |
| 2658 | .tag_name => CValue{ .inline_index = inst }, |
| 2659 | .error_name => CValue{ .inline_index = inst }, |
| 2533 | 2660 | .splat => try airSplat(f, inst), |
| 2534 | 2661 | .select => try airSelect(f, inst), |
| 2535 | 2662 | .shuffle => try airShuffle(f, inst), |
| ... | ... | @@ -2565,7 +2692,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO |
| 2565 | 2692 | .fpext, |
| 2566 | 2693 | => try airFloatCast(f, inst), |
| 2567 | 2694 | |
| 2568 | | .ptrtoint => try airPtrToInt(f, inst), |
| 2695 | .ptrtoint => CValue{ .inline_index = inst }, |
| 2569 | 2696 | |
| 2570 | 2697 | .atomic_store_unordered => try airAtomicStore(f, inst, toMemoryOrder(.Unordered)), |
| 2571 | 2698 | .atomic_store_monotonic => try airAtomicStore(f, inst, toMemoryOrder(.Monotonic)), |
| ... | ... | @@ -2649,7 +2776,7 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [ |
| 2649 | 2776 | |
| 2650 | 2777 | const inst_ty = f.air.typeOfIndex(inst); |
| 2651 | 2778 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 2652 | | const operand = try f.resolveInst(ty_op.operand); |
| 2779 | const operand = try f.resolveInstNoInline(ty_op.operand); |
| 2653 | 2780 | const writer = f.object.writer(); |
| 2654 | 2781 | const local = try f.allocLocal(inst_ty, .Const); |
| 2655 | 2782 | try writer.writeAll(" = "); |
| ... | ... | @@ -3224,25 +3351,21 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: |
| 3224 | 3351 | return local; |
| 3225 | 3352 | } |
| 3226 | 3353 | |
| 3227 | | fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3228 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 3229 | | |
| 3354 | fn airNot(f: *Function, inst: Air.Inst.Index) !void { |
| 3230 | 3355 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 3231 | 3356 | const op = try f.resolveInst(ty_op.operand); |
| 3232 | 3357 | |
| 3233 | 3358 | const writer = f.object.writer(); |
| 3234 | 3359 | const inst_ty = f.air.typeOfIndex(inst); |
| 3235 | | const local = try f.allocLocal(inst_ty, .Const); |
| 3236 | 3360 | |
| 3237 | 3361 | const target = f.object.dg.module.getTarget(); |
| 3238 | 3362 | if (inst_ty.bitSize(target) > 64) {} |
| 3239 | 3363 | |
| 3240 | | try writer.writeAll(" = "); |
| 3364 | try writer.writeByte('('); |
| 3365 | try f.renderTypecast(writer, inst_ty); |
| 3366 | try writer.writeByte(')'); |
| 3241 | 3367 | try writer.writeByte(if (inst_ty.tag() == .bool) '!' else '~'); |
| 3242 | 3368 | try f.writeCValue(writer, op, .Other); |
| 3243 | | try writer.writeAll(";\n"); |
| 3244 | | |
| 3245 | | return local; |
| 3246 | 3369 | } |
| 3247 | 3370 | |
| 3248 | 3371 | fn airBinOp( |
| ... | ... | @@ -3251,62 +3374,54 @@ fn airBinOp( |
| 3251 | 3374 | operator: []const u8, |
| 3252 | 3375 | operation: []const u8, |
| 3253 | 3376 | info: BuiltinInfo, |
| 3254 | | ) !CValue { |
| 3255 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 3256 | | |
| 3377 | ) !void { |
| 3257 | 3378 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 3258 | 3379 | |
| 3259 | 3380 | const operand_ty = f.air.typeOf(bin_op.lhs); |
| 3260 | 3381 | const target = f.object.dg.module.getTarget(); |
| 3261 | 3382 | if ((operand_ty.isInt() and operand_ty.bitSize(target) > 64) or operand_ty.isRuntimeFloat()) |
| 3262 | | return try airBinBuiltinCall(f, inst, operation, info); |
| 3383 | return airBinBuiltinCall(f, inst, operation, info); |
| 3263 | 3384 | |
| 3264 | 3385 | const inst_ty = f.air.typeOfIndex(inst); |
| 3265 | 3386 | const lhs = try f.resolveInst(bin_op.lhs); |
| 3266 | 3387 | const rhs = try f.resolveInst(bin_op.rhs); |
| 3267 | 3388 | |
| 3268 | 3389 | const writer = f.object.writer(); |
| 3269 | | const local = try f.allocLocal(inst_ty, .Const); |
| 3270 | | |
| 3271 | | try writer.writeAll(" = "); |
| 3390 | try writer.writeByte('('); |
| 3391 | try f.renderTypecast(writer, inst_ty); |
| 3392 | try writer.writeAll(")("); |
| 3272 | 3393 | try f.writeCValue(writer, lhs, .Other); |
| 3273 | 3394 | try writer.writeByte(' '); |
| 3274 | 3395 | try writer.writeAll(operator); |
| 3275 | 3396 | try writer.writeByte(' '); |
| 3276 | 3397 | try f.writeCValue(writer, rhs, .Other); |
| 3277 | | try writer.writeAll(";\n"); |
| 3278 | | |
| 3279 | | return local; |
| 3398 | try writer.writeByte(')'); |
| 3280 | 3399 | } |
| 3281 | 3400 | |
| 3282 | | fn airCmpOp(f: *Function, inst: Air.Inst.Index, operator: []const u8, operation: []const u8) !CValue { |
| 3283 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 3284 | | |
| 3401 | fn airCmpOp( |
| 3402 | f: *Function, |
| 3403 | inst: Air.Inst.Index, |
| 3404 | operator: []const u8, |
| 3405 | operation: []const u8, |
| 3406 | ) !void { |
| 3285 | 3407 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 3286 | 3408 | |
| 3287 | 3409 | const operand_ty = f.air.typeOf(bin_op.lhs); |
| 3288 | 3410 | const target = f.object.dg.module.getTarget(); |
| 3289 | 3411 | if (operand_ty.isInt() and operand_ty.bitSize(target) > 64) |
| 3290 | | return try airCmpBuiltinCall(f, inst, operator, "cmp"); |
| 3412 | return airCmpBuiltinCall(f, inst, operator, "cmp"); |
| 3291 | 3413 | if (operand_ty.isRuntimeFloat()) |
| 3292 | | return try airCmpBuiltinCall(f, inst, operator, operation); |
| 3414 | return airCmpBuiltinCall(f, inst, operator, operation); |
| 3293 | 3415 | |
| 3294 | | const inst_ty = f.air.typeOfIndex(inst); |
| 3295 | 3416 | const lhs = try f.resolveInst(bin_op.lhs); |
| 3296 | 3417 | const rhs = try f.resolveInst(bin_op.rhs); |
| 3297 | 3418 | |
| 3298 | 3419 | const writer = f.object.writer(); |
| 3299 | | const local = try f.allocLocal(inst_ty, .Const); |
| 3300 | | |
| 3301 | | try writer.writeAll(" = "); |
| 3302 | 3420 | try f.writeCValue(writer, lhs, .Other); |
| 3303 | 3421 | try writer.writeByte(' '); |
| 3304 | 3422 | try writer.writeAll(operator); |
| 3305 | 3423 | try writer.writeByte(' '); |
| 3306 | 3424 | try f.writeCValue(writer, rhs, .Other); |
| 3307 | | try writer.writeAll(";\n"); |
| 3308 | | |
| 3309 | | return local; |
| 3310 | 3425 | } |
| 3311 | 3426 | |
| 3312 | 3427 | fn airEquality( |
| ... | ... | @@ -3315,27 +3430,20 @@ fn airEquality( |
| 3315 | 3430 | negate_prefix: []const u8, |
| 3316 | 3431 | operator: []const u8, |
| 3317 | 3432 | operation: []const u8, |
| 3318 | | ) !CValue { |
| 3319 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 3320 | | |
| 3433 | ) !void { |
| 3321 | 3434 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 3322 | 3435 | |
| 3323 | 3436 | const operand_ty = f.air.typeOf(bin_op.lhs); |
| 3324 | 3437 | const target = f.object.dg.module.getTarget(); |
| 3325 | 3438 | if (operand_ty.isInt() and operand_ty.bitSize(target) > 64) |
| 3326 | | return try airCmpBuiltinCall(f, inst, operator, "cmp"); |
| 3439 | return airCmpBuiltinCall(f, inst, operator, "cmp"); |
| 3327 | 3440 | if (operand_ty.isRuntimeFloat()) |
| 3328 | | return try airCmpBuiltinCall(f, inst, operator, operation); |
| 3441 | return airCmpBuiltinCall(f, inst, operator, operation); |
| 3329 | 3442 | |
| 3330 | 3443 | const lhs = try f.resolveInst(bin_op.lhs); |
| 3331 | 3444 | const rhs = try f.resolveInst(bin_op.rhs); |
| 3332 | 3445 | |
| 3333 | 3446 | const writer = f.object.writer(); |
| 3334 | | const inst_ty = f.air.typeOfIndex(inst); |
| 3335 | | const local = try f.allocLocal(inst_ty, .Const); |
| 3336 | | |
| 3337 | | try writer.writeAll(" = "); |
| 3338 | | |
| 3339 | 3447 | if (operand_ty.zigTypeTag() == .Optional and !operand_ty.isPtrLikeOptional()) { |
| 3340 | 3448 | // (A && B) || (C && (A == B)) |
| 3341 | 3449 | // A = lhs.is_null ; B = rhs.is_null ; C = rhs.payload == lhs.payload |
| ... | ... | @@ -3352,9 +3460,8 @@ fn airEquality( |
| 3352 | 3460 | try f.writeCValue(writer, lhs, .Other); |
| 3353 | 3461 | try writer.writeAll(".is_null == "); |
| 3354 | 3462 | try f.writeCValue(writer, rhs, .Other); |
| 3355 | | try writer.writeAll(".is_null));\n"); |
| 3356 | | |
| 3357 | | return local; |
| 3463 | try writer.writeAll(".is_null))"); |
| 3464 | return; |
| 3358 | 3465 | } |
| 3359 | 3466 | |
| 3360 | 3467 | try f.writeCValue(writer, lhs, .Other); |
| ... | ... | @@ -3362,9 +3469,6 @@ fn airEquality( |
| 3362 | 3469 | try writer.writeAll(operator); |
| 3363 | 3470 | try writer.writeByte(' '); |
| 3364 | 3471 | try f.writeCValue(writer, rhs, .Other); |
| 3365 | | try writer.writeAll(";\n"); |
| 3366 | | |
| 3367 | | return local; |
| 3368 | 3472 | } |
| 3369 | 3473 | |
| 3370 | 3474 | fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { |
| ... | ... | @@ -3418,26 +3522,23 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 3418 | 3522 | return local; |
| 3419 | 3523 | } |
| 3420 | 3524 | |
| 3421 | | fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []const u8) !CValue { |
| 3422 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 3423 | | |
| 3525 | fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []const u8) !void { |
| 3424 | 3526 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 3425 | 3527 | |
| 3426 | 3528 | const inst_ty = f.air.typeOfIndex(inst); |
| 3427 | 3529 | const target = f.object.dg.module.getTarget(); |
| 3428 | 3530 | if (inst_ty.isInt() and inst_ty.bitSize(target) > 64) |
| 3429 | | return try airBinBuiltinCall(f, inst, operation[1..], .None); |
| 3531 | return airBinBuiltinCall(f, inst, operation[1..], .None); |
| 3430 | 3532 | if (inst_ty.isRuntimeFloat()) |
| 3431 | | return try airBinFloatOp(f, inst, operation); |
| 3533 | return airBinFloatOp(f, inst, operation); |
| 3432 | 3534 | |
| 3433 | 3535 | const lhs = try f.resolveInst(bin_op.lhs); |
| 3434 | 3536 | const rhs = try f.resolveInst(bin_op.rhs); |
| 3435 | 3537 | |
| 3436 | 3538 | const writer = f.object.writer(); |
| 3437 | | const local = try f.allocLocal(inst_ty, .Const); |
| 3438 | 3539 | |
| 3439 | 3540 | // (lhs <> rhs) ? lhs : rhs |
| 3440 | | try writer.writeAll(" = ("); |
| 3541 | try writer.writeAll("("); |
| 3441 | 3542 | try f.writeCValue(writer, lhs, .Other); |
| 3442 | 3543 | try writer.writeByte(' '); |
| 3443 | 3544 | try writer.writeByte(operator); |
| ... | ... | @@ -3447,9 +3548,6 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons |
| 3447 | 3548 | try f.writeCValue(writer, lhs, .Other); |
| 3448 | 3549 | try writer.writeAll(" : "); |
| 3449 | 3550 | try f.writeCValue(writer, rhs, .Other); |
| 3450 | | try writer.writeAll(";\n"); |
| 3451 | | |
| 3452 | | return local; |
| 3453 | 3551 | } |
| 3454 | 3552 | |
| 3455 | 3553 | fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| ... | ... | @@ -3759,46 +3857,62 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3759 | 3857 | } |
| 3760 | 3858 | |
| 3761 | 3859 | fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3762 | | const inst_ty = f.air.typeOfIndex(inst); |
| 3860 | const src_ty = f.air.typeOfIndex(inst); |
| 3763 | 3861 | // No IgnoreComptime until Sema stops giving us garbage Air. |
| 3764 | 3862 | // https://github.com/ziglang/zig/issues/13410 |
| 3765 | | if (f.liveness.isUnused(inst) or !inst_ty.hasRuntimeBits()) return CValue.none; |
| 3863 | if (f.liveness.isUnused(inst) or !src_ty.hasRuntimeBits()) return CValue.none; |
| 3766 | 3864 | |
| 3767 | 3865 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 3768 | | const operand = try f.resolveInst(ty_op.operand); |
| 3866 | const operand = try f.resolveInstNoInline(ty_op.operand); |
| 3867 | const dest_ty = f.air.typeOf(ty_op.operand); |
| 3868 | const target = f.object.dg.module.getTarget(); |
| 3869 | |
| 3870 | if (dest_ty.isAbiInt() and src_ty.isAbiInt()) { |
| 3871 | const src_info = src_ty.intInfo(target); |
| 3872 | const dest_info = dest_ty.intInfo(target); |
| 3873 | if (std.meta.eql(src_info, dest_info)) { |
| 3874 | return operand; |
| 3875 | } |
| 3876 | } |
| 3769 | 3877 | |
| 3770 | 3878 | const writer = f.object.writer(); |
| 3771 | | if (inst_ty.isPtrAtRuntime() and |
| 3772 | | f.air.typeOf(ty_op.operand).isPtrAtRuntime()) |
| 3773 | | { |
| 3774 | | const local = try f.allocLocal(inst_ty, .Const); |
| 3879 | if (src_ty.isPtrAtRuntime() and dest_ty.isPtrAtRuntime()) { |
| 3880 | const local = try f.allocLocal(src_ty, .Const); |
| 3775 | 3881 | try writer.writeAll(" = ("); |
| 3776 | | try f.renderTypecast(writer, inst_ty); |
| 3882 | try f.renderTypecast(writer, src_ty); |
| 3777 | 3883 | try writer.writeByte(')'); |
| 3778 | 3884 | try f.writeCValue(writer, operand, .Other); |
| 3779 | 3885 | try writer.writeAll(";\n"); |
| 3780 | 3886 | return local; |
| 3781 | 3887 | } |
| 3782 | 3888 | |
| 3783 | | const local = try f.allocLocal(inst_ty, .Mut); |
| 3889 | const local = try f.allocLocal(src_ty, .Mut); |
| 3784 | 3890 | try writer.writeAll(";\n"); |
| 3785 | 3891 | |
| 3892 | const operand_lval = if (operand == .constant) blk: { |
| 3893 | const operand_local = try f.allocLocal(dest_ty, .Const); |
| 3894 | try writer.writeAll(" = "); |
| 3895 | try f.writeCValue(writer, operand, .Initializer); |
| 3896 | try writer.writeAll(";\n"); |
| 3897 | break :blk operand_local; |
| 3898 | } else operand; |
| 3899 | |
| 3786 | 3900 | try writer.writeAll("memcpy(&"); |
| 3787 | 3901 | try f.writeCValue(writer, local, .Other); |
| 3788 | 3902 | try writer.writeAll(", &"); |
| 3789 | | try f.writeCValue(writer, operand, .Other); |
| 3903 | try f.writeCValue(writer, operand_lval, .Other); |
| 3790 | 3904 | try writer.writeAll(", sizeof("); |
| 3791 | | try f.renderTypecast(writer, inst_ty); |
| 3905 | try f.renderTypecast(writer, src_ty); |
| 3792 | 3906 | try writer.writeAll("));\n"); |
| 3793 | 3907 | |
| 3794 | 3908 | // Ensure padding bits have the expected value. |
| 3795 | | if (inst_ty.isAbiInt()) { |
| 3909 | if (src_ty.isAbiInt()) { |
| 3796 | 3910 | try f.writeCValue(writer, local, .Other); |
| 3797 | 3911 | try writer.writeAll(" = zig_wrap_"); |
| 3798 | | try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty); |
| 3912 | try f.object.dg.renderTypeForBuiltinFnName(writer, src_ty); |
| 3799 | 3913 | try writer.writeByte('('); |
| 3800 | 3914 | try f.writeCValue(writer, local, .Other); |
| 3801 | | try f.object.dg.renderBuiltinInfo(writer, inst_ty, .Bits); |
| 3915 | try f.object.dg.renderBuiltinInfo(writer, src_ty, .Bits); |
| 3802 | 3916 | try writer.writeAll(");\n"); |
| 3803 | 3917 | } |
| 3804 | 3918 | |
| ... | ... | @@ -3855,7 +3969,7 @@ fn airLoop(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3855 | 3969 | const body = f.air.extra[loop.end..][0..loop.data.body_len]; |
| 3856 | 3970 | const writer = f.object.writer(); |
| 3857 | 3971 | try writer.writeAll("while ("); |
| 3858 | | try f.object.dg.renderValue(writer, Type.bool, Value.true, .Other); |
| 3972 | try f.object.dg.renderValue(writer, Type.bool, Value.true, .condition); |
| 3859 | 3973 | try writer.writeAll(") "); |
| 3860 | 3974 | try genBody(f, body); |
| 3861 | 3975 | try writer.writeByte('\n'); |
| ... | ... | @@ -3871,7 +3985,7 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3871 | 3985 | const writer = f.object.writer(); |
| 3872 | 3986 | |
| 3873 | 3987 | try writer.writeAll("if ("); |
| 3874 | | try f.writeCValue(writer, cond, .Other); |
| 3988 | try f.writeCValue(writer, cond, .condition); |
| 3875 | 3989 | try writer.writeAll(") "); |
| 3876 | 3990 | try genBody(f, then_body); |
| 3877 | 3991 | try writer.writeAll(" else "); |
| ... | ... | @@ -3889,10 +4003,14 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3889 | 4003 | const writer = f.object.writer(); |
| 3890 | 4004 | |
| 3891 | 4005 | try writer.writeAll("switch ("); |
| 3892 | | if (condition_ty.tag() == .bool) { |
| 4006 | if (condition_ty.zigTypeTag() == .Bool) { |
| 3893 | 4007 | try writer.writeByte('('); |
| 3894 | 4008 | try f.renderTypecast(writer, Type.u1); |
| 3895 | 4009 | try writer.writeByte(')'); |
| 4010 | } else if (condition_ty.isPtrAtRuntime()) { |
| 4011 | try writer.writeByte('('); |
| 4012 | try f.renderTypecast(writer, Type.usize); |
| 4013 | try writer.writeByte(')'); |
| 3896 | 4014 | } |
| 3897 | 4015 | try f.writeCValue(writer, condition, .Other); |
| 3898 | 4016 | try writer.writeAll(") {"); |
| ... | ... | @@ -3909,6 +4027,11 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3909 | 4027 | for (items) |item| { |
| 3910 | 4028 | try f.object.indent_writer.insertNewline(); |
| 3911 | 4029 | try writer.writeAll("case "); |
| 4030 | if (condition_ty.isPtrAtRuntime()) { |
| 4031 | try writer.writeByte('('); |
| 4032 | try f.renderTypecast(writer, Type.usize); |
| 4033 | try writer.writeByte(')'); |
| 4034 | } |
| 3912 | 4035 | try f.object.dg.renderValue(writer, condition_ty, f.air.value(item).?, .Other); |
| 3913 | 4036 | try writer.writeAll(": "); |
| 3914 | 4037 | } |
| ... | ... | @@ -4055,6 +4178,8 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4055 | 4178 | if (is_reg) { |
| 4056 | 4179 | try f.writeCValue(writer, .{ .local = locals_index }, .Other); |
| 4057 | 4180 | locals_index += 1; |
| 4181 | } else if (output == .none) { |
| 4182 | try f.writeCValue(writer, local, .FunctionArgument); |
| 4058 | 4183 | } else { |
| 4059 | 4184 | try f.writeCValueDeref(writer, try f.resolveInst(output)); |
| 4060 | 4185 | } |
| ... | ... | @@ -4131,16 +4256,11 @@ fn airIsNull( |
| 4131 | 4256 | inst: Air.Inst.Index, |
| 4132 | 4257 | operator: []const u8, |
| 4133 | 4258 | is_ptr: bool, |
| 4134 | | ) !CValue { |
| 4135 | | if (f.liveness.isUnused(inst)) |
| 4136 | | return CValue.none; |
| 4137 | | |
| 4259 | ) !void { |
| 4138 | 4260 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 4139 | 4261 | const writer = f.object.writer(); |
| 4140 | 4262 | const operand = try f.resolveInst(un_op); |
| 4141 | 4263 | |
| 4142 | | const local = try f.allocLocal(Type.initTag(.bool), .Const); |
| 4143 | | try writer.writeAll(" = "); |
| 4144 | 4264 | try if (is_ptr) f.writeCValueDeref(writer, operand) else f.writeCValue(writer, operand, .Other); |
| 4145 | 4265 | |
| 4146 | 4266 | const operand_ty = f.air.typeOf(un_op); |
| ... | ... | @@ -4168,8 +4288,6 @@ fn airIsNull( |
| 4168 | 4288 | try writer.writeAll(operator); |
| 4169 | 4289 | try writer.writeByte(' '); |
| 4170 | 4290 | try f.object.dg.renderValue(writer, rhs.ty, rhs.val, .Other); |
| 4171 | | try writer.writeAll(";\n"); |
| 4172 | | return local; |
| 4173 | 4291 | } |
| 4174 | 4292 | |
| 4175 | 4293 | fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue { |
| ... | ... | @@ -4358,6 +4476,10 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc |
| 4358 | 4476 | u8_ptr_pl.data.pointee_type = Type.u8; |
| 4359 | 4477 | const u8_ptr_ty = Type.initPayload(&u8_ptr_pl.base); |
| 4360 | 4478 | |
| 4479 | if (!std.mem.isAligned(byte_offset, field_ptr_ty.ptrAlignment(target))) { |
| 4480 | return f.fail("TODO: CBE: unaligned packed struct field pointer", .{}); |
| 4481 | } |
| 4482 | |
| 4361 | 4483 | try writer.writeAll("&(("); |
| 4362 | 4484 | try f.renderTypecast(writer, u8_ptr_ty); |
| 4363 | 4485 | try writer.writeByte(')'); |
| ... | ... | @@ -4366,7 +4488,11 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc |
| 4366 | 4488 | return local; |
| 4367 | 4489 | } else @as(CValue, CValue.none), // this @as is needed because of a stage1 bug |
| 4368 | 4490 | }, |
| 4369 | | .@"union", .union_safety_tagged, .union_tagged => .{ |
| 4491 | .@"union", .union_safety_tagged, .union_tagged => if (struct_ty.containerLayout() == .Packed) { |
| 4492 | try f.writeCValue(writer, struct_ptr, .Other); |
| 4493 | try writer.writeAll(";\n"); |
| 4494 | return local; |
| 4495 | } else .{ |
| 4370 | 4496 | .identifier = struct_ty.unionFields().keys()[index], |
| 4371 | 4497 | }, |
| 4372 | 4498 | .tuple, .anon_struct => field_name: { |
| ... | ... | @@ -4480,7 +4606,26 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4480 | 4606 | return local; |
| 4481 | 4607 | }, |
| 4482 | 4608 | }, |
| 4483 | | .@"union", .union_safety_tagged, .union_tagged => .{ |
| 4609 | .@"union", .union_safety_tagged, .union_tagged => if (struct_ty.containerLayout() == .Packed) { |
| 4610 | const operand_lval = if (struct_byval == .constant) blk: { |
| 4611 | const operand_local = try f.allocLocal(struct_ty, .Const); |
| 4612 | try writer.writeAll(" = "); |
| 4613 | try f.writeCValue(writer, struct_byval, .Initializer); |
| 4614 | try writer.writeAll(";\n"); |
| 4615 | break :blk operand_local; |
| 4616 | } else struct_byval; |
| 4617 | |
| 4618 | const local = try f.allocLocal(inst_ty, .Mut); |
| 4619 | try writer.writeAll(";\n"); |
| 4620 | try writer.writeAll("memcpy(&"); |
| 4621 | try f.writeCValue(writer, local, .FunctionArgument); |
| 4622 | try writer.writeAll(", &"); |
| 4623 | try f.writeCValue(writer, operand_lval, .FunctionArgument); |
| 4624 | try writer.writeAll(", sizeof("); |
| 4625 | try f.renderTypecast(writer, inst_ty); |
| 4626 | try writer.writeAll("));\n"); |
| 4627 | return local; |
| 4628 | } else .{ |
| 4484 | 4629 | .identifier = struct_ty.unionFields().keys()[extra.field_index], |
| 4485 | 4630 | }, |
| 4486 | 4631 | .tuple, .anon_struct => blk: { |
| ... | ... | @@ -4558,7 +4703,18 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu |
| 4558 | 4703 | const operand_is_ptr = operand_ty.zigTypeTag() == .Pointer; |
| 4559 | 4704 | const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty; |
| 4560 | 4705 | |
| 4561 | | if (!error_union_ty.errorUnionPayload().hasRuntimeBits()) return CValue.none; |
| 4706 | if (!error_union_ty.errorUnionPayload().hasRuntimeBits()) { |
| 4707 | if (!is_ptr) return CValue.none; |
| 4708 | |
| 4709 | const local = try f.allocLocal(inst_ty, .Const); |
| 4710 | const w = f.object.writer(); |
| 4711 | try w.writeAll(" = ("); |
| 4712 | try f.renderTypecast(w, inst_ty); |
| 4713 | try w.writeByte(')'); |
| 4714 | try f.writeCValue(w, operand, .Initializer); |
| 4715 | try w.writeAll(";\n"); |
| 4716 | return local; |
| 4717 | } |
| 4562 | 4718 | |
| 4563 | 4719 | const writer = f.object.writer(); |
| 4564 | 4720 | const local = try f.allocLocal(inst_ty, .Const); |
| ... | ... | @@ -4701,21 +4857,15 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4701 | 4857 | return local; |
| 4702 | 4858 | } |
| 4703 | 4859 | |
| 4704 | | fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !CValue { |
| 4705 | | if (f.liveness.isUnused(inst)) |
| 4706 | | return CValue.none; |
| 4707 | | |
| 4860 | fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !void { |
| 4708 | 4861 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 4709 | 4862 | const writer = f.object.writer(); |
| 4710 | 4863 | const operand = try f.resolveInst(un_op); |
| 4711 | 4864 | const operand_ty = f.air.typeOf(un_op); |
| 4712 | | const local = try f.allocLocal(Type.initTag(.bool), .Const); |
| 4713 | 4865 | const err_union_ty = if (is_ptr) operand_ty.childType() else operand_ty; |
| 4714 | 4866 | const payload_ty = err_union_ty.errorUnionPayload(); |
| 4715 | 4867 | const error_ty = err_union_ty.errorUnionSet(); |
| 4716 | 4868 | |
| 4717 | | try writer.writeAll(" = "); |
| 4718 | | |
| 4719 | 4869 | if (!error_ty.errorSetIsEmpty()) |
| 4720 | 4870 | if (payload_ty.hasRuntimeBits()) |
| 4721 | 4871 | if (is_ptr) |
| ... | ... | @@ -4730,8 +4880,6 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const |
| 4730 | 4880 | try writer.writeAll(operator); |
| 4731 | 4881 | try writer.writeByte(' '); |
| 4732 | 4882 | try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other); |
| 4733 | | try writer.writeAll(";\n"); |
| 4734 | | return local; |
| 4735 | 4883 | } |
| 4736 | 4884 | |
| 4737 | 4885 | fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| ... | ... | @@ -4805,21 +4953,16 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4805 | 4953 | return local; |
| 4806 | 4954 | } |
| 4807 | 4955 | |
| 4808 | | fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4809 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 4810 | | |
| 4956 | fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !void { |
| 4811 | 4957 | const inst_ty = f.air.typeOfIndex(inst); |
| 4812 | | const local = try f.allocLocal(inst_ty, .Const); |
| 4813 | 4958 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 4814 | 4959 | const writer = f.object.writer(); |
| 4815 | 4960 | const operand = try f.resolveInst(un_op); |
| 4816 | 4961 | |
| 4817 | | try writer.writeAll(" = ("); |
| 4962 | try writer.writeAll("("); |
| 4818 | 4963 | try f.renderTypecast(writer, inst_ty); |
| 4819 | 4964 | try writer.writeByte(')'); |
| 4820 | 4965 | try f.writeCValue(writer, operand, .Other); |
| 4821 | | try writer.writeAll(";\n"); |
| 4822 | | return local; |
| 4823 | 4966 | } |
| 4824 | 4967 | |
| 4825 | 4968 | fn airUnBuiltinCall( |
| ... | ... | @@ -4827,24 +4970,19 @@ fn airUnBuiltinCall( |
| 4827 | 4970 | inst: Air.Inst.Index, |
| 4828 | 4971 | operation: []const u8, |
| 4829 | 4972 | info: BuiltinInfo, |
| 4830 | | ) !CValue { |
| 4831 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 4832 | | |
| 4833 | | const inst_ty = f.air.typeOfIndex(inst); |
| 4973 | ) !void { |
| 4834 | 4974 | const operand = f.air.instructions.items(.data)[inst].ty_op.operand; |
| 4835 | 4975 | const operand_ty = f.air.typeOf(operand); |
| 4836 | 4976 | |
| 4837 | | const local = try f.allocLocal(inst_ty, .Const); |
| 4838 | 4977 | const writer = f.object.writer(); |
| 4839 | | try writer.writeAll(" = zig_"); |
| 4978 | try writer.writeAll("zig_"); |
| 4840 | 4979 | try writer.writeAll(operation); |
| 4841 | 4980 | try writer.writeByte('_'); |
| 4842 | 4981 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); |
| 4843 | 4982 | try writer.writeByte('('); |
| 4844 | 4983 | try f.writeCValue(writer, try f.resolveInst(operand), .FunctionArgument); |
| 4845 | 4984 | try f.object.dg.renderBuiltinInfo(writer, operand_ty, info); |
| 4846 | | try writer.writeAll(");\n"); |
| 4847 | | return local; |
| 4985 | try writer.writeAll(")"); |
| 4848 | 4986 | } |
| 4849 | 4987 | |
| 4850 | 4988 | fn airBinBuiltinCall( |
| ... | ... | @@ -4852,16 +4990,12 @@ fn airBinBuiltinCall( |
| 4852 | 4990 | inst: Air.Inst.Index, |
| 4853 | 4991 | operation: []const u8, |
| 4854 | 4992 | info: BuiltinInfo, |
| 4855 | | ) !CValue { |
| 4856 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 4857 | | |
| 4858 | | const inst_ty = f.air.typeOfIndex(inst); |
| 4993 | ) !void { |
| 4859 | 4994 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 4860 | 4995 | const operand_ty = f.air.typeOf(bin_op.lhs); |
| 4861 | 4996 | |
| 4862 | | const local = try f.allocLocal(inst_ty, .Const); |
| 4863 | 4997 | const writer = f.object.writer(); |
| 4864 | | try writer.writeAll(" = zig_"); |
| 4998 | try writer.writeAll("zig_"); |
| 4865 | 4999 | try writer.writeAll(operation); |
| 4866 | 5000 | try writer.writeByte('_'); |
| 4867 | 5001 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); |
| ... | ... | @@ -4870,8 +5004,7 @@ fn airBinBuiltinCall( |
| 4870 | 5004 | try writer.writeAll(", "); |
| 4871 | 5005 | try f.writeCValue(writer, try f.resolveInst(bin_op.rhs), .FunctionArgument); |
| 4872 | 5006 | try f.object.dg.renderBuiltinInfo(writer, operand_ty, info); |
| 4873 | | try writer.writeAll(");\n"); |
| 4874 | | return local; |
| 5007 | try writer.writeAll(")"); |
| 4875 | 5008 | } |
| 4876 | 5009 | |
| 4877 | 5010 | fn airCmpBuiltinCall( |
| ... | ... | @@ -4879,16 +5012,12 @@ fn airCmpBuiltinCall( |
| 4879 | 5012 | inst: Air.Inst.Index, |
| 4880 | 5013 | operator: []const u8, |
| 4881 | 5014 | operation: []const u8, |
| 4882 | | ) !CValue { |
| 4883 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 4884 | | |
| 4885 | | const inst_ty = f.air.typeOfIndex(inst); |
| 5015 | ) !void { |
| 4886 | 5016 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 4887 | 5017 | const operand_ty = f.air.typeOf(bin_op.lhs); |
| 4888 | 5018 | |
| 4889 | | const local = try f.allocLocal(inst_ty, .Const); |
| 4890 | 5019 | const writer = f.object.writer(); |
| 4891 | | try writer.writeAll(" = zig_"); |
| 5020 | try writer.writeAll("zig_"); |
| 4892 | 5021 | try writer.writeAll(operation); |
| 4893 | 5022 | try writer.writeByte('_'); |
| 4894 | 5023 | try f.object.dg.renderTypeForBuiltinFnName(writer, operand_ty); |
| ... | ... | @@ -4896,8 +5025,7 @@ fn airCmpBuiltinCall( |
| 4896 | 5025 | try f.writeCValue(writer, try f.resolveInst(bin_op.lhs), .FunctionArgument); |
| 4897 | 5026 | try writer.writeAll(", "); |
| 4898 | 5027 | try f.writeCValue(writer, try f.resolveInst(bin_op.rhs), .FunctionArgument); |
| 4899 | | try writer.print(") {s} {};\n", .{ operator, try f.fmtIntLiteral(Type.initTag(.i32), Value.zero) }); |
| 4900 | | return local; |
| 5028 | try writer.print(") {s} {}", .{ operator, try f.fmtIntLiteral(Type.initTag(.i32), Value.zero) }); |
| 4901 | 5029 | } |
| 4902 | 5030 | |
| 4903 | 5031 | fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue { |
| ... | ... | @@ -5127,12 +5255,7 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5127 | 5255 | return CValue.none; |
| 5128 | 5256 | } |
| 5129 | 5257 | |
| 5130 | | fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5131 | | if (f.liveness.isUnused(inst)) |
| 5132 | | return CValue.none; |
| 5133 | | |
| 5134 | | const inst_ty = f.air.typeOfIndex(inst); |
| 5135 | | const local = try f.allocLocal(inst_ty, .Const); |
| 5258 | fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !void { |
| 5136 | 5259 | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 5137 | 5260 | const un_ty = f.air.typeOf(ty_op.operand); |
| 5138 | 5261 | const writer = f.object.writer(); |
| ... | ... | @@ -5140,44 +5263,31 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5140 | 5263 | |
| 5141 | 5264 | const target = f.object.dg.module.getTarget(); |
| 5142 | 5265 | const layout = un_ty.unionGetLayout(target); |
| 5143 | | if (layout.tag_size == 0) return CValue.none; |
| 5266 | assert(layout.tag_size != 0); |
| 5144 | 5267 | |
| 5145 | | try writer.writeAll(" = "); |
| 5146 | 5268 | try f.writeCValue(writer, operand, .Other); |
| 5147 | | try writer.writeAll(".tag;\n"); |
| 5148 | | return local; |
| 5269 | try writer.writeAll(".tag"); |
| 5149 | 5270 | } |
| 5150 | 5271 | |
| 5151 | | fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5152 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 5153 | | |
| 5272 | fn airTagName(f: *Function, inst: Air.Inst.Index) !void { |
| 5154 | 5273 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 5155 | | const inst_ty = f.air.typeOfIndex(inst); |
| 5156 | 5274 | const enum_ty = f.air.typeOf(un_op); |
| 5157 | 5275 | const operand = try f.resolveInst(un_op); |
| 5158 | 5276 | |
| 5159 | 5277 | const writer = f.object.writer(); |
| 5160 | | const local = try f.allocLocal(inst_ty, .Const); |
| 5161 | | try writer.print(" = {s}(", .{try f.object.dg.getTagNameFn(enum_ty)}); |
| 5278 | try writer.print("{s}(", .{try f.object.dg.getTagNameFn(enum_ty)}); |
| 5162 | 5279 | try f.writeCValue(writer, operand, .Other); |
| 5163 | | try writer.writeAll(");\n"); |
| 5164 | | |
| 5165 | | return local; |
| 5280 | try writer.writeAll(")"); |
| 5166 | 5281 | } |
| 5167 | 5282 | |
| 5168 | | fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5169 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 5170 | | |
| 5283 | fn airErrorName(f: *Function, inst: Air.Inst.Index) !void { |
| 5171 | 5284 | const un_op = f.air.instructions.items(.data)[inst].un_op; |
| 5172 | 5285 | const writer = f.object.writer(); |
| 5173 | | const inst_ty = f.air.typeOfIndex(inst); |
| 5174 | 5286 | const operand = try f.resolveInst(un_op); |
| 5175 | | const local = try f.allocLocal(inst_ty, .Const); |
| 5176 | 5287 | |
| 5177 | | try writer.writeAll(" = zig_errorName["); |
| 5288 | try writer.writeAll("zig_errorName["); |
| 5178 | 5289 | try f.writeCValue(writer, operand, .Other); |
| 5179 | | try writer.writeAll("];\n"); |
| 5180 | | return local; |
| 5290 | try writer.writeAll("]"); |
| 5181 | 5291 | } |
| 5182 | 5292 | |
| 5183 | 5293 | fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { |
| ... | ... | @@ -5198,30 +5308,12 @@ fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5198 | 5308 | fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5199 | 5309 | if (f.liveness.isUnused(inst)) return CValue.none; |
| 5200 | 5310 | |
| 5201 | | const inst_ty = f.air.typeOfIndex(inst); |
| 5202 | | const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; |
| 5203 | | |
| 5204 | | const writer = f.object.writer(); |
| 5205 | | const local = try f.allocLocal(inst_ty, .Const); |
| 5206 | | try writer.writeAll(" = "); |
| 5207 | | |
| 5208 | | _ = local; |
| 5209 | | _ = ty_pl; |
| 5210 | 5311 | return f.fail("TODO: C backend: implement airSelect", .{}); |
| 5211 | 5312 | } |
| 5212 | 5313 | |
| 5213 | 5314 | fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5214 | 5315 | if (f.liveness.isUnused(inst)) return CValue.none; |
| 5215 | 5316 | |
| 5216 | | const inst_ty = f.air.typeOfIndex(inst); |
| 5217 | | const ty_op = f.air.instructions.items(.data)[inst].ty_op; |
| 5218 | | const operand = try f.resolveInst(ty_op.operand); |
| 5219 | | const writer = f.object.writer(); |
| 5220 | | const local = try f.allocLocal(inst_ty, .Const); |
| 5221 | | try writer.writeAll(" = "); |
| 5222 | | |
| 5223 | | _ = operand; |
| 5224 | | _ = local; |
| 5225 | 5317 | return f.fail("TODO: C backend: implement airShuffle", .{}); |
| 5226 | 5318 | } |
| 5227 | 5319 | |
| ... | ... | @@ -5532,6 +5624,13 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5532 | 5624 | |
| 5533 | 5625 | const writer = f.object.writer(); |
| 5534 | 5626 | const local = try f.allocLocal(union_ty, .Const); |
| 5627 | if (union_obj.layout == .Packed) { |
| 5628 | try writer.writeAll(" = "); |
| 5629 | try f.writeCValue(writer, payload, .Initializer); |
| 5630 | try writer.writeAll(";\n"); |
| 5631 | return local; |
| 5632 | } |
| 5633 | |
| 5535 | 5634 | try writer.writeAll(" = {"); |
| 5536 | 5635 | if (union_ty.unionTagTypeSafety()) |tag_ty| { |
| 5537 | 5636 | const layout = union_ty.unionGetLayout(target); |
| ... | ... | @@ -5645,15 +5744,18 @@ fn airUnFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVal |
| 5645 | 5744 | return local; |
| 5646 | 5745 | } |
| 5647 | 5746 | |
| 5648 | | fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CValue { |
| 5649 | | if (f.liveness.isUnused(inst)) return CValue.none; |
| 5747 | fn airBinFloatOp( |
| 5748 | f: *Function, |
| 5749 | inst: Air.Inst.Index, |
| 5750 | operation: []const u8, |
| 5751 | ) !void { |
| 5650 | 5752 | const bin_op = f.air.instructions.items(.data)[inst].bin_op; |
| 5651 | 5753 | const writer = f.object.writer(); |
| 5652 | 5754 | const inst_ty = f.air.typeOfIndex(inst); |
| 5653 | 5755 | const lhs = try f.resolveInst(bin_op.lhs); |
| 5654 | 5756 | const rhs = try f.resolveInst(bin_op.rhs); |
| 5655 | | const local = try f.allocLocal(inst_ty, .Const); |
| 5656 | | try writer.writeAll(" = zig_libc_name_"); |
| 5757 | |
| 5758 | try writer.writeAll("zig_libc_name_"); |
| 5657 | 5759 | try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty); |
| 5658 | 5760 | try writer.writeByte('('); |
| 5659 | 5761 | try writer.writeAll(operation); |
| ... | ... | @@ -5661,8 +5763,7 @@ fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVa |
| 5661 | 5763 | try f.writeCValue(writer, lhs, .FunctionArgument); |
| 5662 | 5764 | try writer.writeAll(", "); |
| 5663 | 5765 | try f.writeCValue(writer, rhs, .FunctionArgument); |
| 5664 | | try writer.writeAll(");\n"); |
| 5665 | | return local; |
| 5766 | try writer.writeAll(")"); |
| 5666 | 5767 | } |
| 5667 | 5768 | |
| 5668 | 5769 | fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |