| ... | ... | @@ -5,12 +5,13 @@ const mem = std.mem; |
| 5 | 5 | const log = std.log.scoped(.c); |
| 6 | 6 | |
| 7 | 7 | const link = @import("../link.zig"); |
| 8 | | const Module = @import("../Module.zig"); |
| 8 | const Zcu = @import("../Module.zig"); |
| 9 | const Module = @import("../Package/Module.zig"); |
| 9 | 10 | const Compilation = @import("../Compilation.zig"); |
| 10 | 11 | const Value = @import("../Value.zig"); |
| 11 | 12 | const Type = @import("../type.zig").Type; |
| 12 | 13 | const C = link.File.C; |
| 13 | | const Decl = Module.Decl; |
| 14 | const Decl = Zcu.Decl; |
| 14 | 15 | const trace = @import("../tracy.zig").trace; |
| 15 | 16 | const LazySrcLoc = std.zig.LazySrcLoc; |
| 16 | 17 | const Air = @import("../Air.zig"); |
| ... | ... | @@ -30,7 +31,7 @@ pub const CValue = union(enum) { |
| 30 | 31 | /// Address of a local. |
| 31 | 32 | local_ref: LocalIndex, |
| 32 | 33 | /// A constant instruction, to be rendered inline. |
| 33 | | constant: InternPool.Index, |
| 34 | constant: Value, |
| 34 | 35 | /// Index into the parameters |
| 35 | 36 | arg: usize, |
| 36 | 37 | /// The array field of a parameter |
| ... | ... | @@ -72,13 +73,15 @@ pub const LazyFnValue = struct { |
| 72 | 73 | }; |
| 73 | 74 | pub const LazyFnMap = std.AutoArrayHashMapUnmanaged(LazyFnKey, LazyFnValue); |
| 74 | 75 | |
| 75 | | const LoopDepth = u16; |
| 76 | 76 | const Local = struct { |
| 77 | 77 | cty_idx: CType.Index, |
| 78 | | alignas: CType.AlignAs, |
| 78 | flags: packed struct(u32) { |
| 79 | alignas: CType.AlignAs, |
| 80 | _: u20 = undefined, |
| 81 | }, |
| 79 | 82 | |
| 80 | 83 | pub fn getType(local: Local) LocalType { |
| 81 | | return .{ .cty_idx = local.cty_idx, .alignas = local.alignas }; |
| 84 | return .{ .cty_idx = local.cty_idx, .alignas = local.flags.alignas }; |
| 82 | 85 | } |
| 83 | 86 | }; |
| 84 | 87 | |
| ... | ... | @@ -300,11 +303,11 @@ pub const Function = struct { |
| 300 | 303 | const gop = try f.value_map.getOrPut(ref); |
| 301 | 304 | if (gop.found_existing) return gop.value_ptr.*; |
| 302 | 305 | |
| 303 | | const mod = f.object.dg.module; |
| 304 | | const val = (try f.air.value(ref, mod)).?; |
| 306 | const zcu = f.object.dg.zcu; |
| 307 | const val = (try f.air.value(ref, zcu)).?; |
| 305 | 308 | const ty = f.typeOf(ref); |
| 306 | 309 | |
| 307 | | const result: CValue = if (lowersToArray(ty, mod)) result: { |
| 310 | const result: CValue = if (lowersToArray(ty, zcu)) result: { |
| 308 | 311 | const writer = f.object.codeHeaderWriter(); |
| 309 | 312 | const alignment: Alignment = .none; |
| 310 | 313 | const decl_c_value = try f.allocLocalValue(ty, alignment); |
| ... | ... | @@ -313,17 +316,17 @@ pub const Function = struct { |
| 313 | 316 | try writer.writeAll("static "); |
| 314 | 317 | try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, Const, alignment, .complete); |
| 315 | 318 | try writer.writeAll(" = "); |
| 316 | | try f.object.dg.renderValue(writer, ty, val, .StaticInitializer); |
| 319 | try f.object.dg.renderValue(writer, val, .StaticInitializer); |
| 317 | 320 | try writer.writeAll(";\n "); |
| 318 | 321 | break :result decl_c_value; |
| 319 | | } else .{ .constant = val.toIntern() }; |
| 322 | } else .{ .constant = val }; |
| 320 | 323 | |
| 321 | 324 | gop.value_ptr.* = result; |
| 322 | 325 | return result; |
| 323 | 326 | } |
| 324 | 327 | |
| 325 | 328 | fn wantSafety(f: *Function) bool { |
| 326 | | return switch (f.object.dg.module.optimizeMode()) { |
| 329 | return switch (f.object.dg.zcu.optimizeMode()) { |
| 327 | 330 | .Debug, .ReleaseSafe => true, |
| 328 | 331 | .ReleaseFast, .ReleaseSmall => false, |
| 329 | 332 | }; |
| ... | ... | @@ -333,11 +336,13 @@ pub const Function = struct { |
| 333 | 336 | /// those which go into `allocs`. This function does not add the resulting local into `allocs`; |
| 334 | 337 | /// that responsibility lies with the caller. |
| 335 | 338 | fn allocLocalValue(f: *Function, ty: Type, alignment: Alignment) !CValue { |
| 336 | | const mod = f.object.dg.module; |
| 339 | const zcu = f.object.dg.zcu; |
| 337 | 340 | const gpa = f.object.dg.gpa; |
| 338 | 341 | try f.locals.append(gpa, .{ |
| 339 | 342 | .cty_idx = try f.typeToIndex(ty, .complete), |
| 340 | | .alignas = CType.AlignAs.init(alignment, ty.abiAlignment(mod)), |
| 343 | .flags = .{ |
| 344 | .alignas = CType.AlignAs.init(alignment, ty.abiAlignment(zcu)), |
| 345 | }, |
| 341 | 346 | }); |
| 342 | 347 | return .{ .new_local = @intCast(f.locals.items.len - 1) }; |
| 343 | 348 | } |
| ... | ... | @@ -355,79 +360,100 @@ pub const Function = struct { |
| 355 | 360 | /// Only allocates the local; does not print anything. Will attempt to re-use locals, so should |
| 356 | 361 | /// not be used for persistent locals (i.e. those in `allocs`). |
| 357 | 362 | fn allocAlignedLocal(f: *Function, ty: Type, _: CQualifiers, alignment: Alignment) !CValue { |
| 358 | | const mod = f.object.dg.module; |
| 363 | const zcu = f.object.dg.zcu; |
| 359 | 364 | if (f.free_locals_map.getPtr(.{ |
| 360 | 365 | .cty_idx = try f.typeToIndex(ty, .complete), |
| 361 | | .alignas = CType.AlignAs.init(alignment, ty.abiAlignment(mod)), |
| 366 | .alignas = CType.AlignAs.init(alignment, ty.abiAlignment(zcu)), |
| 362 | 367 | })) |locals_list| { |
| 363 | 368 | if (locals_list.popOrNull()) |local_entry| { |
| 364 | 369 | return .{ .new_local = local_entry.key }; |
| 365 | 370 | } |
| 366 | 371 | } |
| 367 | 372 | |
| 368 | | return try f.allocLocalValue(ty, alignment); |
| 373 | return f.allocLocalValue(ty, alignment); |
| 369 | 374 | } |
| 370 | 375 | |
| 371 | 376 | fn writeCValue(f: *Function, w: anytype, c_value: CValue, location: ValueRenderLocation) !void { |
| 372 | 377 | switch (c_value) { |
| 373 | | .constant => |val| try f.object.dg.renderValue( |
| 374 | | w, |
| 375 | | Type.fromInterned(f.object.dg.module.intern_pool.typeOf(val)), |
| 376 | | Value.fromInterned(val), |
| 377 | | location, |
| 378 | | ), |
| 379 | | .undef => |ty| try f.object.dg.renderValue(w, ty, Value.undef, location), |
| 378 | .none => unreachable, |
| 379 | .new_local, .local => |i| try w.print("t{d}", .{i}), |
| 380 | .local_ref => |i| { |
| 381 | const local = &f.locals.items[i]; |
| 382 | if (local.flags.alignas.abiOrder().compare(.lt)) { |
| 383 | const zcu = f.object.dg.zcu; |
| 384 | const pointee_ty = try zcu.intType(.unsigned, @min( |
| 385 | local.flags.alignas.@"align".toByteUnitsOptional().?, |
| 386 | f.object.dg.mod.resolved_target.result.maxIntAlignment(), |
| 387 | ) * 8); |
| 388 | const ptr_ty = try zcu.singleMutPtrType(pointee_ty); |
| 389 | |
| 390 | try w.writeByte('('); |
| 391 | try f.renderType(w, ptr_ty); |
| 392 | try w.writeByte(')'); |
| 393 | } |
| 394 | try w.print("&t{d}", .{i}); |
| 395 | }, |
| 396 | .constant => |val| try f.object.dg.renderValue(w, val, location), |
| 397 | .arg => |i| try w.print("a{d}", .{i}), |
| 398 | .arg_array => |i| try f.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" }), |
| 399 | .undef => |ty| try f.object.dg.renderUndefValue(w, ty, location), |
| 380 | 400 | else => try f.object.dg.writeCValue(w, c_value), |
| 381 | 401 | } |
| 382 | 402 | } |
| 383 | 403 | |
| 384 | 404 | fn writeCValueDeref(f: *Function, w: anytype, c_value: CValue) !void { |
| 385 | 405 | switch (c_value) { |
| 386 | | .constant => |val| { |
| 406 | .none => unreachable, |
| 407 | .new_local, .local, .constant => { |
| 387 | 408 | try w.writeAll("(*"); |
| 388 | | try f.object.dg.renderValue( |
| 389 | | w, |
| 390 | | Type.fromInterned(f.object.dg.module.intern_pool.typeOf(val)), |
| 391 | | Value.fromInterned(val), |
| 392 | | .Other, |
| 393 | | ); |
| 409 | try f.writeCValue(w, c_value, .Other); |
| 410 | try w.writeByte(')'); |
| 411 | }, |
| 412 | .local_ref => |i| try w.print("t{d}", .{i}), |
| 413 | .arg => |i| try w.print("(*a{d})", .{i}), |
| 414 | .arg_array => |i| { |
| 415 | try w.writeAll("(*"); |
| 416 | try f.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" }); |
| 394 | 417 | try w.writeByte(')'); |
| 395 | 418 | }, |
| 396 | 419 | else => try f.object.dg.writeCValueDeref(w, c_value), |
| 397 | 420 | } |
| 398 | 421 | } |
| 399 | 422 | |
| 400 | | fn writeCValueMember(f: *Function, w: anytype, c_value: CValue, member: CValue) !void { |
| 423 | fn writeCValueMember( |
| 424 | f: *Function, |
| 425 | writer: anytype, |
| 426 | c_value: CValue, |
| 427 | member: CValue, |
| 428 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 401 | 429 | switch (c_value) { |
| 402 | | .constant => |val| { |
| 403 | | try f.object.dg.renderValue( |
| 404 | | w, |
| 405 | | Type.fromInterned(f.object.dg.module.intern_pool.typeOf(val)), |
| 406 | | Value.fromInterned(val), |
| 407 | | .Other, |
| 408 | | ); |
| 409 | | try w.writeByte('.'); |
| 410 | | try f.writeCValue(w, member, .Other); |
| 430 | .new_local, .local, .local_ref, .constant, .arg, .arg_array => { |
| 431 | try f.writeCValue(writer, c_value, .Other); |
| 432 | try writer.writeByte('.'); |
| 433 | try f.writeCValue(writer, member, .Other); |
| 411 | 434 | }, |
| 412 | | else => try f.object.dg.writeCValueMember(w, c_value, member), |
| 435 | else => return f.object.dg.writeCValueMember(writer, c_value, member), |
| 413 | 436 | } |
| 414 | 437 | } |
| 415 | 438 | |
| 416 | | fn writeCValueDerefMember(f: *Function, w: anytype, c_value: CValue, member: CValue) !void { |
| 439 | fn writeCValueDerefMember(f: *Function, writer: anytype, c_value: CValue, member: CValue) !void { |
| 417 | 440 | switch (c_value) { |
| 418 | | .constant => |val| { |
| 419 | | try w.writeByte('('); |
| 420 | | try f.object.dg.renderValue( |
| 421 | | w, |
| 422 | | Type.fromInterned(f.object.dg.module.intern_pool.typeOf(val)), |
| 423 | | Value.fromInterned(val), |
| 424 | | .Other, |
| 425 | | ); |
| 426 | | try w.writeAll(")->"); |
| 427 | | try f.writeCValue(w, member, .Other); |
| 441 | .new_local, .local, .arg, .arg_array => { |
| 442 | try f.writeCValue(writer, c_value, .Other); |
| 443 | try writer.writeAll("->"); |
| 444 | }, |
| 445 | .constant => { |
| 446 | try writer.writeByte('('); |
| 447 | try f.writeCValue(writer, c_value, .Other); |
| 448 | try writer.writeAll(")->"); |
| 449 | }, |
| 450 | .local_ref => { |
| 451 | try f.writeCValueDeref(writer, c_value); |
| 452 | try writer.writeByte('.'); |
| 428 | 453 | }, |
| 429 | | else => try f.object.dg.writeCValueDerefMember(w, c_value, member), |
| 454 | else => return f.object.dg.writeCValueDerefMember(writer, c_value, member), |
| 430 | 455 | } |
| 456 | try f.writeCValue(writer, member, .Other); |
| 431 | 457 | } |
| 432 | 458 | |
| 433 | 459 | fn fail(f: *Function, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } { |
| ... | ... | @@ -462,8 +488,8 @@ pub const Function = struct { |
| 462 | 488 | return f.object.dg.renderIntCast(w, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location); |
| 463 | 489 | } |
| 464 | 490 | |
| 465 | | fn fmtIntLiteral(f: *Function, ty: Type, val: Value) !std.fmt.Formatter(formatIntLiteral) { |
| 466 | | return f.object.dg.fmtIntLiteral(ty, val, .Other); |
| 491 | fn fmtIntLiteral(f: *Function, val: Value) !std.fmt.Formatter(formatIntLiteral) { |
| 492 | return f.object.dg.fmtIntLiteral(val, .Other); |
| 467 | 493 | } |
| 468 | 494 | |
| 469 | 495 | fn getLazyFnName(f: *Function, key: LazyFnKey, data: LazyFnValue.Data) ![]const u8 { |
| ... | ... | @@ -475,7 +501,7 @@ pub const Function = struct { |
| 475 | 501 | var promoted = f.object.dg.ctypes.promote(gpa); |
| 476 | 502 | defer f.object.dg.ctypes.demote(promoted); |
| 477 | 503 | const arena = promoted.arena.allocator(); |
| 478 | | const mod = f.object.dg.module; |
| 504 | const zcu = f.object.dg.zcu; |
| 479 | 505 | |
| 480 | 506 | gop.value_ptr.* = .{ |
| 481 | 507 | .fn_name = switch (key) { |
| ... | ... | @@ -484,7 +510,7 @@ pub const Function = struct { |
| 484 | 510 | .never_inline, |
| 485 | 511 | => |owner_decl| try std.fmt.allocPrint(arena, "zig_{s}_{}__{d}", .{ |
| 486 | 512 | @tagName(key), |
| 487 | | fmtIdent(mod.intern_pool.stringToSlice(mod.declPtr(owner_decl).name)), |
| 513 | fmtIdent(zcu.intern_pool.stringToSlice(zcu.declPtr(owner_decl).name)), |
| 488 | 514 | @intFromEnum(owner_decl), |
| 489 | 515 | }), |
| 490 | 516 | }, |
| ... | ... | @@ -510,17 +536,17 @@ pub const Function = struct { |
| 510 | 536 | } |
| 511 | 537 | |
| 512 | 538 | fn typeOf(f: *Function, inst: Air.Inst.Ref) Type { |
| 513 | | const mod = f.object.dg.module; |
| 514 | | return f.air.typeOf(inst, &mod.intern_pool); |
| 539 | const zcu = f.object.dg.zcu; |
| 540 | return f.air.typeOf(inst, &zcu.intern_pool); |
| 515 | 541 | } |
| 516 | 542 | |
| 517 | 543 | fn typeOfIndex(f: *Function, inst: Air.Inst.Index) Type { |
| 518 | | const mod = f.object.dg.module; |
| 519 | | return f.air.typeOfIndex(inst, &mod.intern_pool); |
| 544 | const zcu = f.object.dg.zcu; |
| 545 | return f.air.typeOfIndex(inst, &zcu.intern_pool); |
| 520 | 546 | } |
| 521 | 547 | }; |
| 522 | 548 | |
| 523 | | /// This data is available when outputting .c code for a `Module`. |
| 549 | /// This data is available when outputting .c code for a `Zcu`. |
| 524 | 550 | /// It is not available when generating .h file. |
| 525 | 551 | pub const Object = struct { |
| 526 | 552 | dg: DeclGen, |
| ... | ... | @@ -542,12 +568,13 @@ pub const Object = struct { |
| 542 | 568 | /// This data is available both when outputting .c code and when outputting an .h file. |
| 543 | 569 | pub const DeclGen = struct { |
| 544 | 570 | gpa: mem.Allocator, |
| 545 | | module: *Module, |
| 571 | zcu: *Zcu, |
| 572 | mod: *Module, |
| 546 | 573 | pass: Pass, |
| 547 | 574 | is_naked_fn: bool, |
| 548 | 575 | /// This is a borrowed reference from `link.C`. |
| 549 | 576 | fwd_decl: std.ArrayList(u8), |
| 550 | | error_msg: ?*Module.ErrorMsg, |
| 577 | error_msg: ?*Zcu.ErrorMsg, |
| 551 | 578 | ctypes: CType.Store, |
| 552 | 579 | /// Keeps track of anonymous decls that need to be rendered before this |
| 553 | 580 | /// (named) Decl in the output C code. |
| ... | ... | @@ -566,75 +593,70 @@ pub const DeclGen = struct { |
| 566 | 593 | |
| 567 | 594 | fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } { |
| 568 | 595 | @setCold(true); |
| 569 | | const mod = dg.module; |
| 596 | const zcu = dg.zcu; |
| 570 | 597 | const decl_index = dg.pass.decl; |
| 571 | | const decl = mod.declPtr(decl_index); |
| 572 | | const src_loc = decl.srcLoc(mod); |
| 573 | | dg.error_msg = try Module.ErrorMsg.create(dg.gpa, src_loc, format, args); |
| 598 | const decl = zcu.declPtr(decl_index); |
| 599 | const src_loc = decl.srcLoc(zcu); |
| 600 | dg.error_msg = try Zcu.ErrorMsg.create(dg.gpa, src_loc, format, args); |
| 574 | 601 | return error.AnalysisFail; |
| 575 | 602 | } |
| 576 | 603 | |
| 577 | 604 | fn renderAnonDeclValue( |
| 578 | 605 | dg: *DeclGen, |
| 579 | 606 | writer: anytype, |
| 580 | | ty: Type, |
| 581 | 607 | ptr_val: Value, |
| 582 | 608 | anon_decl: InternPool.Key.Ptr.Addr.AnonDecl, |
| 583 | 609 | location: ValueRenderLocation, |
| 584 | 610 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 585 | | const mod = dg.module; |
| 586 | | const ip = &mod.intern_pool; |
| 587 | | const decl_val = anon_decl.val; |
| 588 | | const decl_ty = Type.fromInterned(ip.typeOf(decl_val)); |
| 611 | const zcu = dg.zcu; |
| 612 | const ip = &zcu.intern_pool; |
| 613 | const decl_val = Value.fromInterned(anon_decl.val); |
| 614 | const decl_ty = decl_val.typeOf(zcu); |
| 589 | 615 | |
| 590 | 616 | // Render an undefined pointer if we have a pointer to a zero-bit or comptime type. |
| 591 | | if (ty.isPtrAtRuntime(mod) and !decl_ty.isFnOrHasRuntimeBits(mod)) { |
| 592 | | return dg.writeCValue(writer, .{ .undef = ty }); |
| 617 | const ptr_ty = ptr_val.typeOf(zcu); |
| 618 | if (ptr_ty.isPtrAtRuntime(zcu) and !decl_ty.isFnOrHasRuntimeBits(zcu)) { |
| 619 | return dg.writeCValue(writer, .{ .undef = ptr_ty }); |
| 593 | 620 | } |
| 594 | 621 | |
| 595 | 622 | // Chase function values in order to be able to reference the original function. |
| 596 | | if (Value.fromInterned(decl_val).getFunction(mod)) |func| { |
| 597 | | _ = func; |
| 598 | | _ = ptr_val; |
| 599 | | _ = location; |
| 600 | | @panic("TODO"); |
| 601 | | } |
| 602 | | if (Value.fromInterned(decl_val).getExternFunc(mod)) |extern_func| { |
| 603 | | _ = extern_func; |
| 604 | | _ = ptr_val; |
| 605 | | _ = location; |
| 606 | | @panic("TODO"); |
| 607 | | } |
| 623 | if (decl_val.getFunction(zcu)) |func| |
| 624 | return dg.renderDeclValue(writer, ptr_val, func.owner_decl, location); |
| 625 | if (decl_val.getExternFunc(zcu)) |extern_func| |
| 626 | return dg.renderDeclValue(writer, ptr_val, extern_func.decl, location); |
| 608 | 627 | |
| 609 | | assert(Value.fromInterned(decl_val).getVariable(mod) == null); |
| 628 | assert(decl_val.getVariable(zcu) == null); |
| 610 | 629 | |
| 611 | 630 | // We shouldn't cast C function pointers as this is UB (when you call |
| 612 | 631 | // them). The analysis until now should ensure that the C function |
| 613 | 632 | // pointers are compatible. If they are not, then there is a bug |
| 614 | 633 | // somewhere and we should let the C compiler tell us about it. |
| 615 | | const need_typecast = if (ty.castPtrToFn(mod)) |_| false else !ty.childType(mod).eql(decl_ty, mod); |
| 616 | | if (need_typecast) { |
| 634 | const child_cty = (try dg.typeToCType(ptr_ty, .complete)).cast(CType.Payload.Child).?.data; |
| 635 | const decl_cty = try dg.typeToIndex(decl_ty, .complete); |
| 636 | const need_cast = child_cty != decl_cty and |
| 637 | (dg.indexToCType(child_cty).tag() != .function or dg.indexToCType(decl_cty).tag() != .function); |
| 638 | if (need_cast) { |
| 617 | 639 | try writer.writeAll("(("); |
| 618 | | try dg.renderType(writer, ty); |
| 640 | try dg.renderType(writer, ptr_ty); |
| 619 | 641 | try writer.writeByte(')'); |
| 620 | 642 | } |
| 621 | 643 | try writer.writeByte('&'); |
| 622 | 644 | try renderAnonDeclName(writer, decl_val); |
| 623 | | if (need_typecast) try writer.writeByte(')'); |
| 645 | if (need_cast) try writer.writeByte(')'); |
| 624 | 646 | |
| 625 | 647 | // Indicate that the anon decl should be rendered to the output so that |
| 626 | 648 | // our reference above is not undefined. |
| 627 | 649 | const ptr_type = ip.indexToKey(anon_decl.orig_ty).ptr_type; |
| 628 | | const gop = try dg.anon_decl_deps.getOrPut(dg.gpa, decl_val); |
| 650 | const gop = try dg.anon_decl_deps.getOrPut(dg.gpa, anon_decl.val); |
| 629 | 651 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 630 | 652 | |
| 631 | 653 | // Only insert an alignment entry if the alignment is greater than ABI |
| 632 | 654 | // alignment. If there is already an entry, keep the greater alignment. |
| 633 | 655 | const explicit_alignment = ptr_type.flags.alignment; |
| 634 | 656 | if (explicit_alignment != .none) { |
| 635 | | const abi_alignment = Type.fromInterned(ptr_type.child).abiAlignment(mod); |
| 657 | const abi_alignment = Type.fromInterned(ptr_type.child).abiAlignment(zcu); |
| 636 | 658 | if (explicit_alignment.compareStrict(.gt, abi_alignment)) { |
| 637 | | const aligned_gop = try dg.aligned_anon_decls.getOrPut(dg.gpa, decl_val); |
| 659 | const aligned_gop = try dg.aligned_anon_decls.getOrPut(dg.gpa, anon_decl.val); |
| 638 | 660 | aligned_gop.value_ptr.* = if (aligned_gop.found_existing) |
| 639 | 661 | aligned_gop.value_ptr.maxStrict(explicit_alignment) |
| 640 | 662 | else |
| ... | ... | @@ -646,41 +668,45 @@ pub const DeclGen = struct { |
| 646 | 668 | fn renderDeclValue( |
| 647 | 669 | dg: *DeclGen, |
| 648 | 670 | writer: anytype, |
| 649 | | ty: Type, |
| 650 | 671 | val: Value, |
| 651 | 672 | decl_index: InternPool.DeclIndex, |
| 652 | 673 | location: ValueRenderLocation, |
| 653 | 674 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 654 | | const mod = dg.module; |
| 655 | | const decl = mod.declPtr(decl_index); |
| 675 | const zcu = dg.zcu; |
| 676 | const decl = zcu.declPtr(decl_index); |
| 656 | 677 | assert(decl.has_tv); |
| 657 | 678 | |
| 658 | 679 | // Render an undefined pointer if we have a pointer to a zero-bit or comptime type. |
| 659 | | if (ty.isPtrAtRuntime(mod) and !decl.typeOf(mod).isFnOrHasRuntimeBits(mod)) { |
| 680 | const ty = val.typeOf(zcu); |
| 681 | const decl_ty = decl.typeOf(zcu); |
| 682 | if (ty.isPtrAtRuntime(zcu) and !decl_ty.isFnOrHasRuntimeBits(zcu)) { |
| 660 | 683 | return dg.writeCValue(writer, .{ .undef = ty }); |
| 661 | 684 | } |
| 662 | 685 | |
| 663 | 686 | // Chase function values in order to be able to reference the original function. |
| 664 | | if (decl.val.getFunction(mod)) |func| if (func.owner_decl != decl_index) |
| 665 | | return dg.renderDeclValue(writer, ty, val, func.owner_decl, location); |
| 666 | | if (decl.val.getExternFunc(mod)) |extern_func| if (extern_func.decl != decl_index) |
| 667 | | return dg.renderDeclValue(writer, ty, val, extern_func.decl, location); |
| 687 | if (decl.val.getFunction(zcu)) |func| if (func.owner_decl != decl_index) |
| 688 | return dg.renderDeclValue(writer, val, func.owner_decl, location); |
| 689 | if (decl.val.getExternFunc(zcu)) |extern_func| if (extern_func.decl != decl_index) |
| 690 | return dg.renderDeclValue(writer, val, extern_func.decl, location); |
| 668 | 691 | |
| 669 | | if (decl.val.getVariable(mod)) |variable| try dg.renderFwdDecl(decl_index, variable, .tentative); |
| 692 | if (decl.val.getVariable(zcu)) |variable| try dg.renderFwdDecl(decl_index, variable, .tentative); |
| 670 | 693 | |
| 671 | 694 | // We shouldn't cast C function pointers as this is UB (when you call |
| 672 | 695 | // them). The analysis until now should ensure that the C function |
| 673 | 696 | // pointers are compatible. If they are not, then there is a bug |
| 674 | 697 | // somewhere and we should let the C compiler tell us about it. |
| 675 | | const need_typecast = if (ty.castPtrToFn(mod)) |_| false else !ty.childType(mod).eql(decl.typeOf(mod), mod); |
| 676 | | if (need_typecast) { |
| 698 | const child_cty = (try dg.typeToCType(ty, .complete)).cast(CType.Payload.Child).?.data; |
| 699 | const decl_cty = try dg.typeToIndex(decl_ty, .complete); |
| 700 | const need_cast = child_cty != decl_cty and |
| 701 | (dg.indexToCType(child_cty).tag() != .function or dg.indexToCType(decl_cty).tag() != .function); |
| 702 | if (need_cast) { |
| 677 | 703 | try writer.writeAll("(("); |
| 678 | 704 | try dg.renderType(writer, ty); |
| 679 | 705 | try writer.writeByte(')'); |
| 680 | 706 | } |
| 681 | 707 | try writer.writeByte('&'); |
| 682 | 708 | try dg.renderDeclName(writer, decl_index, 0); |
| 683 | | if (need_typecast) try writer.writeByte(')'); |
| 709 | if (need_cast) try writer.writeByte(')'); |
| 684 | 710 | } |
| 685 | 711 | |
| 686 | 712 | /// Renders a "parent" pointer by recursing to the root decl/variable |
| ... | ... | @@ -691,31 +717,32 @@ pub const DeclGen = struct { |
| 691 | 717 | ptr_val: InternPool.Index, |
| 692 | 718 | location: ValueRenderLocation, |
| 693 | 719 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 694 | | const mod = dg.module; |
| 695 | | const ptr_ty = Type.fromInterned(mod.intern_pool.typeOf(ptr_val)); |
| 720 | const zcu = dg.zcu; |
| 721 | const ip = &zcu.intern_pool; |
| 722 | const ptr_ty = Type.fromInterned(ip.typeOf(ptr_val)); |
| 696 | 723 | const ptr_cty = try dg.typeToIndex(ptr_ty, .complete); |
| 697 | | const ptr = mod.intern_pool.indexToKey(ptr_val).ptr; |
| 724 | const ptr_child_cty = dg.indexToCType(ptr_cty).cast(CType.Payload.Child).?.data; |
| 725 | const ptr = ip.indexToKey(ptr_val).ptr; |
| 698 | 726 | switch (ptr.addr) { |
| 699 | | .decl => |d| try dg.renderDeclValue(writer, ptr_ty, Value.fromInterned(ptr_val), d, location), |
| 700 | | .anon_decl => |anon_decl| try dg.renderAnonDeclValue(writer, ptr_ty, Value.fromInterned(ptr_val), anon_decl, location), |
| 727 | .decl => |d| try dg.renderDeclValue(writer, Value.fromInterned(ptr_val), d, location), |
| 728 | .anon_decl => |anon_decl| try dg.renderAnonDeclValue(writer, Value.fromInterned(ptr_val), anon_decl, location), |
| 701 | 729 | .int => |int| { |
| 702 | 730 | try writer.writeByte('('); |
| 703 | 731 | try dg.renderCType(writer, ptr_cty); |
| 704 | | try writer.print("){x}", .{try dg.fmtIntLiteral(Type.usize, Value.fromInterned(int), .Other)}); |
| 732 | try writer.print("){x}", .{try dg.fmtIntLiteral(Value.fromInterned(int), .Other)}); |
| 705 | 733 | }, |
| 706 | 734 | .eu_payload, .opt_payload => |base| { |
| 707 | | const ptr_base_ty = Type.fromInterned(mod.intern_pool.typeOf(base)); |
| 708 | | const base_ty = ptr_base_ty.childType(mod); |
| 735 | const ptr_base_ty = Type.fromInterned(ip.typeOf(base)); |
| 736 | const base_ty = ptr_base_ty.childType(zcu); |
| 709 | 737 | // Ensure complete type definition is visible before accessing fields. |
| 710 | 738 | _ = try dg.typeToIndex(base_ty, .complete); |
| 711 | 739 | const payload_ty = switch (ptr.addr) { |
| 712 | | .eu_payload => base_ty.errorUnionPayload(mod), |
| 713 | | .opt_payload => base_ty.optionalChild(mod), |
| 740 | .eu_payload => base_ty.errorUnionPayload(zcu), |
| 741 | .opt_payload => base_ty.optionalChild(zcu), |
| 714 | 742 | else => unreachable, |
| 715 | 743 | }; |
| 716 | | const ptr_payload_ty = try mod.adjustPtrTypeChild(ptr_base_ty, payload_ty); |
| 717 | | const ptr_payload_cty = try dg.typeToIndex(ptr_payload_ty, .complete); |
| 718 | | if (ptr_cty != ptr_payload_cty) { |
| 744 | const payload_cty = try dg.typeToIndex(payload_ty, .forward); |
| 745 | if (ptr_child_cty != payload_cty) { |
| 719 | 746 | try writer.writeByte('('); |
| 720 | 747 | try dg.renderCType(writer, ptr_cty); |
| 721 | 748 | try writer.writeByte(')'); |
| ... | ... | @@ -725,70 +752,90 @@ pub const DeclGen = struct { |
| 725 | 752 | try writer.writeAll(")->payload"); |
| 726 | 753 | }, |
| 727 | 754 | .elem => |elem| { |
| 728 | | const ptr_base_ty = Type.fromInterned(mod.intern_pool.typeOf(elem.base)); |
| 729 | | const elem_ty = ptr_base_ty.elemType2(mod); |
| 730 | | const ptr_elem_ty = try mod.adjustPtrTypeChild(ptr_base_ty, elem_ty); |
| 731 | | const ptr_elem_cty = try dg.typeToIndex(ptr_elem_ty, .complete); |
| 732 | | if (ptr_cty != ptr_elem_cty) { |
| 755 | const ptr_base_ty = Type.fromInterned(ip.typeOf(elem.base)); |
| 756 | const elem_ty = ptr_base_ty.elemType2(zcu); |
| 757 | const elem_cty = try dg.typeToIndex(elem_ty, .forward); |
| 758 | if (ptr_child_cty != elem_cty) { |
| 733 | 759 | try writer.writeByte('('); |
| 734 | 760 | try dg.renderCType(writer, ptr_cty); |
| 735 | 761 | try writer.writeByte(')'); |
| 736 | 762 | } |
| 737 | 763 | try writer.writeAll("&("); |
| 738 | | if (mod.intern_pool.indexToKey(ptr_base_ty.toIntern()).ptr_type.flags.size == .One) |
| 764 | if (ip.indexToKey(ptr_base_ty.toIntern()).ptr_type.flags.size == .One) |
| 739 | 765 | try writer.writeByte('*'); |
| 740 | 766 | try dg.renderParentPtr(writer, elem.base, location); |
| 741 | 767 | try writer.print(")[{d}]", .{elem.index}); |
| 742 | 768 | }, |
| 743 | 769 | .field => |field| { |
| 744 | | const ptr_base_ty = Type.fromInterned(mod.intern_pool.typeOf(field.base)); |
| 745 | | const base_ty = ptr_base_ty.childType(mod); |
| 770 | const ptr_base_ty = Type.fromInterned(ip.typeOf(field.base)); |
| 771 | const base_ty = ptr_base_ty.childType(zcu); |
| 746 | 772 | // Ensure complete type definition is visible before accessing fields. |
| 747 | 773 | _ = try dg.typeToIndex(base_ty, .complete); |
| 748 | | const field_ty = switch (mod.intern_pool.indexToKey(base_ty.toIntern())) { |
| 749 | | .anon_struct_type, .struct_type, .union_type => base_ty.structFieldType(@as(usize, @intCast(field.index)), mod), |
| 750 | | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { |
| 751 | | .One, .Many, .C => unreachable, |
| 752 | | .Slice => switch (field.index) { |
| 753 | | Value.slice_ptr_index => base_ty.slicePtrFieldType(mod), |
| 754 | | Value.slice_len_index => Type.usize, |
| 755 | | else => unreachable, |
| 756 | | }, |
| 774 | switch (fieldLocation(ptr_base_ty, ptr_ty, @as(u32, @intCast(field.index)), zcu)) { |
| 775 | .begin => { |
| 776 | const ptr_base_cty = try dg.typeToIndex(ptr_base_ty, .complete); |
| 777 | if (ptr_cty != ptr_base_cty) { |
| 778 | try writer.writeByte('('); |
| 779 | try dg.renderCType(writer, ptr_cty); |
| 780 | try writer.writeByte(')'); |
| 781 | } |
| 782 | try dg.renderParentPtr(writer, field.base, location); |
| 757 | 783 | }, |
| 758 | | else => unreachable, |
| 759 | | }; |
| 760 | | const ptr_field_ty = try mod.adjustPtrTypeChild(ptr_base_ty, field_ty); |
| 761 | | const ptr_field_cty = try dg.typeToIndex(ptr_field_ty, .complete); |
| 762 | | if (ptr_cty != ptr_field_cty) { |
| 763 | | try writer.writeByte('('); |
| 764 | | try dg.renderCType(writer, ptr_cty); |
| 765 | | try writer.writeByte(')'); |
| 766 | | } |
| 767 | | switch (fieldLocation(ptr_base_ty, ptr_ty, @as(u32, @intCast(field.index)), mod)) { |
| 768 | | .begin => try dg.renderParentPtr(writer, field.base, location), |
| 769 | 784 | .field => |name| { |
| 785 | const field_ty = switch (ip.indexToKey(base_ty.toIntern())) { |
| 786 | .anon_struct_type, |
| 787 | .struct_type, |
| 788 | .union_type, |
| 789 | => base_ty.structFieldType(@as(usize, @intCast(field.index)), zcu), |
| 790 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { |
| 791 | .One, .Many, .C => unreachable, |
| 792 | .Slice => switch (field.index) { |
| 793 | Value.slice_ptr_index => base_ty.slicePtrFieldType(zcu), |
| 794 | Value.slice_len_index => Type.usize, |
| 795 | else => unreachable, |
| 796 | }, |
| 797 | }, |
| 798 | else => unreachable, |
| 799 | }; |
| 800 | const field_cty = try dg.typeToIndex(field_ty, .forward); |
| 801 | if (ptr_child_cty != field_cty) { |
| 802 | try writer.writeByte('('); |
| 803 | try dg.renderCType(writer, ptr_cty); |
| 804 | try writer.writeByte(')'); |
| 805 | } |
| 770 | 806 | try writer.writeAll("&("); |
| 771 | 807 | try dg.renderParentPtr(writer, field.base, location); |
| 772 | 808 | try writer.writeAll(")->"); |
| 773 | 809 | try dg.writeCValue(writer, name); |
| 774 | 810 | }, |
| 775 | 811 | .byte_offset => |byte_offset| { |
| 776 | | const u8_ptr_ty = try mod.adjustPtrTypeChild(ptr_ty, Type.u8); |
| 777 | | const byte_offset_val = try mod.intValue(Type.usize, byte_offset); |
| 812 | const u8_ptr_ty = try zcu.adjustPtrTypeChild(ptr_ty, Type.u8); |
| 813 | const u8_ptr_cty = try dg.typeToIndex(u8_ptr_ty, .complete); |
| 778 | 814 | |
| 815 | if (ptr_cty != u8_ptr_cty) { |
| 816 | try writer.writeByte('('); |
| 817 | try dg.renderCType(writer, ptr_cty); |
| 818 | try writer.writeByte(')'); |
| 819 | } |
| 779 | 820 | try writer.writeAll("(("); |
| 780 | | try dg.renderType(writer, u8_ptr_ty); |
| 821 | try dg.renderCType(writer, u8_ptr_cty); |
| 781 | 822 | try writer.writeByte(')'); |
| 782 | 823 | try dg.renderParentPtr(writer, field.base, location); |
| 783 | 824 | try writer.print(" + {})", .{ |
| 784 | | try dg.fmtIntLiteral(Type.usize, byte_offset_val, .Other), |
| 825 | try dg.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset), .Other), |
| 785 | 826 | }); |
| 786 | 827 | }, |
| 787 | 828 | .end => { |
| 829 | const ptr_base_cty = try dg.typeToIndex(ptr_base_ty, .complete); |
| 830 | if (ptr_cty != ptr_base_cty) { |
| 831 | try writer.writeByte('('); |
| 832 | try dg.renderCType(writer, ptr_cty); |
| 833 | try writer.writeByte(')'); |
| 834 | } |
| 788 | 835 | try writer.writeAll("(("); |
| 789 | 836 | try dg.renderParentPtr(writer, field.base, location); |
| 790 | 837 | try writer.print(") + {})", .{ |
| 791 | | try dg.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 1), .Other), |
| 838 | try dg.fmtIntLiteral(try zcu.intValue(Type.usize, 1), .Other), |
| 792 | 839 | }); |
| 793 | 840 | }, |
| 794 | 841 | } |
| ... | ... | @@ -800,215 +847,21 @@ pub const DeclGen = struct { |
| 800 | 847 | fn renderValue( |
| 801 | 848 | dg: *DeclGen, |
| 802 | 849 | writer: anytype, |
| 803 | | ty: Type, |
| 804 | 850 | val: Value, |
| 805 | 851 | location: ValueRenderLocation, |
| 806 | 852 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 807 | | const mod = dg.module; |
| 808 | | const ip = &mod.intern_pool; |
| 853 | const zcu = dg.zcu; |
| 854 | const ip = &zcu.intern_pool; |
| 855 | const target = &dg.mod.resolved_target.result; |
| 809 | 856 | |
| 810 | | const target = mod.getTarget(); |
| 811 | 857 | const initializer_type: ValueRenderLocation = switch (location) { |
| 812 | 858 | .StaticInitializer => .StaticInitializer, |
| 813 | 859 | else => .Initializer, |
| 814 | 860 | }; |
| 815 | 861 | |
| 816 | | const safety_on = switch (mod.optimizeMode()) { |
| 817 | | .Debug, .ReleaseSafe => true, |
| 818 | | .ReleaseFast, .ReleaseSmall => false, |
| 819 | | }; |
| 820 | | |
| 821 | | if (val.isUndefDeep(mod)) { |
| 822 | | switch (ty.zigTypeTag(mod)) { |
| 823 | | .Bool => { |
| 824 | | if (safety_on) { |
| 825 | | return writer.writeAll("0xaa"); |
| 826 | | } else { |
| 827 | | return writer.writeAll("false"); |
| 828 | | } |
| 829 | | }, |
| 830 | | .Int, .Enum, .ErrorSet => return writer.print("{x}", .{try dg.fmtIntLiteral(ty, val, location)}), |
| 831 | | .Float => { |
| 832 | | const bits = ty.floatBits(target); |
| 833 | | // All unsigned ints matching float types are pre-allocated. |
| 834 | | const repr_ty = mod.intType(.unsigned, bits) catch unreachable; |
| 835 | | |
| 836 | | try writer.writeAll("zig_make_"); |
| 837 | | try dg.renderTypeForBuiltinFnName(writer, ty); |
| 838 | | try writer.writeByte('('); |
| 839 | | switch (bits) { |
| 840 | | 16 => try writer.print("{x}", .{@as(f16, @bitCast(undefPattern(i16)))}), |
| 841 | | 32 => try writer.print("{x}", .{@as(f32, @bitCast(undefPattern(i32)))}), |
| 842 | | 64 => try writer.print("{x}", .{@as(f64, @bitCast(undefPattern(i64)))}), |
| 843 | | 80 => try writer.print("{x}", .{@as(f80, @bitCast(undefPattern(i80)))}), |
| 844 | | 128 => try writer.print("{x}", .{@as(f128, @bitCast(undefPattern(i128)))}), |
| 845 | | else => unreachable, |
| 846 | | } |
| 847 | | try writer.writeAll(", "); |
| 848 | | try dg.renderValue(writer, repr_ty, Value.undef, .FunctionArgument); |
| 849 | | return writer.writeByte(')'); |
| 850 | | }, |
| 851 | | .Pointer => if (ty.isSlice(mod)) { |
| 852 | | if (!location.isInitializer()) { |
| 853 | | try writer.writeByte('('); |
| 854 | | try dg.renderType(writer, ty); |
| 855 | | try writer.writeByte(')'); |
| 856 | | } |
| 857 | | |
| 858 | | try writer.writeAll("{("); |
| 859 | | const ptr_ty = ty.slicePtrFieldType(mod); |
| 860 | | try dg.renderType(writer, ptr_ty); |
| 861 | | return writer.print("){x}, {0x}}}", .{try dg.fmtIntLiteral(Type.usize, val, .Other)}); |
| 862 | | } else { |
| 863 | | try writer.writeAll("(("); |
| 864 | | try dg.renderType(writer, ty); |
| 865 | | return writer.print("){x})", .{try dg.fmtIntLiteral(Type.usize, val, .Other)}); |
| 866 | | }, |
| 867 | | .Optional => { |
| 868 | | const payload_ty = ty.optionalChild(mod); |
| 869 | | |
| 870 | | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 871 | | return dg.renderValue(writer, Type.bool, val, location); |
| 872 | | } |
| 873 | | |
| 874 | | if (ty.optionalReprIsPayload(mod)) { |
| 875 | | return dg.renderValue(writer, payload_ty, val, location); |
| 876 | | } |
| 877 | | |
| 878 | | if (!location.isInitializer()) { |
| 879 | | try writer.writeByte('('); |
| 880 | | try dg.renderType(writer, ty); |
| 881 | | try writer.writeByte(')'); |
| 882 | | } |
| 883 | | |
| 884 | | try writer.writeAll("{ .payload = "); |
| 885 | | try dg.renderValue(writer, payload_ty, val, initializer_type); |
| 886 | | try writer.writeAll(", .is_null = "); |
| 887 | | try dg.renderValue(writer, Type.bool, val, initializer_type); |
| 888 | | return writer.writeAll(" }"); |
| 889 | | }, |
| 890 | | .Struct => switch (ty.containerLayout(mod)) { |
| 891 | | .auto, .@"extern" => { |
| 892 | | if (!location.isInitializer()) { |
| 893 | | try writer.writeByte('('); |
| 894 | | try dg.renderType(writer, ty); |
| 895 | | try writer.writeByte(')'); |
| 896 | | } |
| 897 | | |
| 898 | | try writer.writeByte('{'); |
| 899 | | var empty = true; |
| 900 | | for (0..ty.structFieldCount(mod)) |field_index| { |
| 901 | | if (ty.structFieldIsComptime(field_index, mod)) continue; |
| 902 | | const field_ty = ty.structFieldType(field_index, mod); |
| 903 | | if (!field_ty.hasRuntimeBits(mod)) continue; |
| 904 | | |
| 905 | | if (!empty) try writer.writeByte(','); |
| 906 | | try dg.renderValue(writer, field_ty, val, initializer_type); |
| 907 | | |
| 908 | | empty = false; |
| 909 | | } |
| 910 | | |
| 911 | | return writer.writeByte('}'); |
| 912 | | }, |
| 913 | | .@"packed" => return writer.print("{x}", .{try dg.fmtIntLiteral(ty, Value.undef, .Other)}), |
| 914 | | }, |
| 915 | | .Union => { |
| 916 | | if (!location.isInitializer()) { |
| 917 | | try writer.writeByte('('); |
| 918 | | try dg.renderType(writer, ty); |
| 919 | | try writer.writeByte(')'); |
| 920 | | } |
| 921 | | |
| 922 | | try writer.writeByte('{'); |
| 923 | | if (ty.unionTagTypeSafety(mod)) |tag_ty| { |
| 924 | | const layout = ty.unionGetLayout(mod); |
| 925 | | if (layout.tag_size != 0) { |
| 926 | | try writer.writeAll(" .tag = "); |
| 927 | | try dg.renderValue(writer, tag_ty, val, initializer_type); |
| 928 | | } |
| 929 | | if (ty.unionHasAllZeroBitFieldTypes(mod)) return try writer.writeByte('}'); |
| 930 | | if (layout.tag_size != 0) try writer.writeByte(','); |
| 931 | | try writer.writeAll(" .payload = {"); |
| 932 | | } |
| 933 | | const union_obj = mod.typeToUnion(ty).?; |
| 934 | | for (0..union_obj.field_types.len) |field_index| { |
| 935 | | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 936 | | if (!field_ty.hasRuntimeBits(mod)) continue; |
| 937 | | try dg.renderValue(writer, field_ty, val, initializer_type); |
| 938 | | break; |
| 939 | | } |
| 940 | | if (ty.unionTagTypeSafety(mod)) |_| try writer.writeByte('}'); |
| 941 | | return writer.writeByte('}'); |
| 942 | | }, |
| 943 | | .ErrorUnion => { |
| 944 | | const payload_ty = ty.errorUnionPayload(mod); |
| 945 | | const error_ty = ty.errorUnionSet(mod); |
| 946 | | |
| 947 | | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 948 | | return dg.renderValue(writer, error_ty, val, location); |
| 949 | | } |
| 950 | | |
| 951 | | if (!location.isInitializer()) { |
| 952 | | try writer.writeByte('('); |
| 953 | | try dg.renderType(writer, ty); |
| 954 | | try writer.writeByte(')'); |
| 955 | | } |
| 956 | | |
| 957 | | try writer.writeAll("{ .payload = "); |
| 958 | | try dg.renderValue(writer, payload_ty, val, initializer_type); |
| 959 | | try writer.writeAll(", .error = "); |
| 960 | | try dg.renderValue(writer, error_ty, val, initializer_type); |
| 961 | | return writer.writeAll(" }"); |
| 962 | | }, |
| 963 | | .Array, .Vector => { |
| 964 | | const ai = ty.arrayInfo(mod); |
| 965 | | if (ai.elem_type.eql(Type.u8, mod)) { |
| 966 | | const c_len = ty.arrayLenIncludingSentinel(mod); |
| 967 | | var literal = stringLiteral(writer, c_len); |
| 968 | | try literal.start(); |
| 969 | | var index: u64 = 0; |
| 970 | | while (index < c_len) : (index += 1) |
| 971 | | try literal.writeChar(0xaa); |
| 972 | | return literal.end(); |
| 973 | | } else { |
| 974 | | if (!location.isInitializer()) { |
| 975 | | try writer.writeByte('('); |
| 976 | | try dg.renderType(writer, ty); |
| 977 | | try writer.writeByte(')'); |
| 978 | | } |
| 979 | | |
| 980 | | try writer.writeByte('{'); |
| 981 | | const c_len = ty.arrayLenIncludingSentinel(mod); |
| 982 | | var index: u64 = 0; |
| 983 | | while (index < c_len) : (index += 1) { |
| 984 | | if (index > 0) try writer.writeAll(", "); |
| 985 | | try dg.renderValue(writer, ty.childType(mod), val, initializer_type); |
| 986 | | } |
| 987 | | return writer.writeByte('}'); |
| 988 | | } |
| 989 | | }, |
| 990 | | .ComptimeInt, |
| 991 | | .ComptimeFloat, |
| 992 | | .Type, |
| 993 | | .EnumLiteral, |
| 994 | | .Void, |
| 995 | | .NoReturn, |
| 996 | | .Undefined, |
| 997 | | .Null, |
| 998 | | .Opaque, |
| 999 | | => unreachable, |
| 1000 | | |
| 1001 | | .Fn, |
| 1002 | | .Frame, |
| 1003 | | .AnyFrame, |
| 1004 | | => |tag| return dg.fail("TODO: C backend: implement value of type {s}", .{ |
| 1005 | | @tagName(tag), |
| 1006 | | }), |
| 1007 | | } |
| 1008 | | unreachable; |
| 1009 | | } |
| 1010 | | |
| 1011 | | switch (ip.indexToKey(val.ip_index)) { |
| 862 | const ty = val.typeOf(zcu); |
| 863 | if (val.isUndefDeep(zcu)) return dg.renderUndefValue(writer, ty, location); |
| 864 | switch (ip.indexToKey(val.toIntern())) { |
| 1012 | 865 | // types, not values |
| 1013 | 866 | .int_type, |
| 1014 | 867 | .ptr_type, |
| ... | ... | @@ -1050,26 +903,28 @@ pub const DeclGen = struct { |
| 1050 | 903 | .empty_enum_value, |
| 1051 | 904 | => unreachable, // non-runtime values |
| 1052 | 905 | .int => |int| switch (int.storage) { |
| 1053 | | .u64, .i64, .big_int => try writer.print("{}", .{try dg.fmtIntLiteral(ty, val, location)}), |
| 906 | .u64, .i64, .big_int => try writer.print("{}", .{try dg.fmtIntLiteral(val, location)}), |
| 1054 | 907 | .lazy_align, .lazy_size => { |
| 1055 | 908 | try writer.writeAll("(("); |
| 1056 | 909 | try dg.renderType(writer, ty); |
| 1057 | | return writer.print("){x})", .{try dg.fmtIntLiteral(Type.usize, val, .Other)}); |
| 910 | try writer.print("){x})", .{try dg.fmtIntLiteral( |
| 911 | try zcu.intValue(Type.usize, val.toUnsignedInt(zcu)), |
| 912 | .Other, |
| 913 | )}); |
| 1058 | 914 | }, |
| 1059 | 915 | }, |
| 1060 | 916 | .err => |err| try writer.print("zig_error_{}", .{ |
| 1061 | 917 | fmtIdent(ip.stringToSlice(err.name)), |
| 1062 | 918 | }), |
| 1063 | 919 | .error_union => |error_union| { |
| 1064 | | const payload_ty = ty.errorUnionPayload(mod); |
| 1065 | | const error_ty = ty.errorUnionSet(mod); |
| 1066 | | const err_int_ty = try mod.errorIntType(); |
| 1067 | | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 920 | const payload_ty = ty.errorUnionPayload(zcu); |
| 921 | const error_ty = ty.errorUnionSet(zcu); |
| 922 | const err_int_ty = try zcu.errorIntType(); |
| 923 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 1068 | 924 | switch (error_union.val) { |
| 1069 | 925 | .err_name => |err_name| return dg.renderValue( |
| 1070 | 926 | writer, |
| 1071 | | error_ty, |
| 1072 | | Value.fromInterned((try mod.intern(.{ .err = .{ |
| 927 | Value.fromInterned((try zcu.intern(.{ .err = .{ |
| 1073 | 928 | .ty = error_ty.toIntern(), |
| 1074 | 929 | .name = err_name, |
| 1075 | 930 | } }))), |
| ... | ... | @@ -1077,8 +932,7 @@ pub const DeclGen = struct { |
| 1077 | 932 | ), |
| 1078 | 933 | .payload => return dg.renderValue( |
| 1079 | 934 | writer, |
| 1080 | | err_int_ty, |
| 1081 | | try mod.intValue(err_int_ty, 0), |
| 935 | try zcu.intValue(err_int_ty, 0), |
| 1082 | 936 | location, |
| 1083 | 937 | ), |
| 1084 | 938 | } |
| ... | ... | @@ -1093,9 +947,8 @@ pub const DeclGen = struct { |
| 1093 | 947 | try writer.writeAll("{ .payload = "); |
| 1094 | 948 | try dg.renderValue( |
| 1095 | 949 | writer, |
| 1096 | | payload_ty, |
| 1097 | 950 | Value.fromInterned(switch (error_union.val) { |
| 1098 | | .err_name => try mod.intern(.{ .undef = payload_ty.ip_index }), |
| 951 | .err_name => (try zcu.undefValue(payload_ty)).toIntern(), |
| 1099 | 952 | .payload => |payload| payload, |
| 1100 | 953 | }), |
| 1101 | 954 | initializer_type, |
| ... | ... | @@ -1104,8 +957,7 @@ pub const DeclGen = struct { |
| 1104 | 957 | switch (error_union.val) { |
| 1105 | 958 | .err_name => |err_name| try dg.renderValue( |
| 1106 | 959 | writer, |
| 1107 | | error_ty, |
| 1108 | | Value.fromInterned((try mod.intern(.{ .err = .{ |
| 960 | Value.fromInterned((try zcu.intern(.{ .err = .{ |
| 1109 | 961 | .ty = error_ty.toIntern(), |
| 1110 | 962 | .name = err_name, |
| 1111 | 963 | } }))), |
| ... | ... | @@ -1113,24 +965,23 @@ pub const DeclGen = struct { |
| 1113 | 965 | ), |
| 1114 | 966 | .payload => try dg.renderValue( |
| 1115 | 967 | writer, |
| 1116 | | err_int_ty, |
| 1117 | | try mod.intValue(err_int_ty, 0), |
| 968 | try zcu.intValue(err_int_ty, 0), |
| 1118 | 969 | location, |
| 1119 | 970 | ), |
| 1120 | 971 | } |
| 1121 | 972 | try writer.writeAll(" }"); |
| 1122 | 973 | }, |
| 1123 | | .enum_tag => { |
| 1124 | | const enum_tag = ip.indexToKey(val.ip_index).enum_tag; |
| 1125 | | const int_tag_ty = ip.typeOf(enum_tag.int); |
| 1126 | | try dg.renderValue(writer, Type.fromInterned(int_tag_ty), Value.fromInterned(enum_tag.int), location); |
| 1127 | | }, |
| 974 | .enum_tag => |enum_tag| try dg.renderValue( |
| 975 | writer, |
| 976 | Value.fromInterned(enum_tag.int), |
| 977 | location, |
| 978 | ), |
| 1128 | 979 | .float => { |
| 1129 | | const bits = ty.floatBits(target); |
| 1130 | | const f128_val = val.toFloat(f128, mod); |
| 980 | const bits = ty.floatBits(target.*); |
| 981 | const f128_val = val.toFloat(f128, zcu); |
| 1131 | 982 | |
| 1132 | 983 | // All unsigned ints matching float types are pre-allocated. |
| 1133 | | const repr_ty = mod.intType(.unsigned, bits) catch unreachable; |
| 984 | const repr_ty = zcu.intType(.unsigned, bits) catch unreachable; |
| 1134 | 985 | |
| 1135 | 986 | assert(bits <= 128); |
| 1136 | 987 | var repr_val_limbs: [BigInt.calcTwosCompLimbCount(128)]BigIntLimb = undefined; |
| ... | ... | @@ -1141,26 +992,24 @@ pub const DeclGen = struct { |
| 1141 | 992 | }; |
| 1142 | 993 | |
| 1143 | 994 | switch (bits) { |
| 1144 | | 16 => repr_val_big.set(@as(u16, @bitCast(val.toFloat(f16, mod)))), |
| 1145 | | 32 => repr_val_big.set(@as(u32, @bitCast(val.toFloat(f32, mod)))), |
| 1146 | | 64 => repr_val_big.set(@as(u64, @bitCast(val.toFloat(f64, mod)))), |
| 1147 | | 80 => repr_val_big.set(@as(u80, @bitCast(val.toFloat(f80, mod)))), |
| 995 | 16 => repr_val_big.set(@as(u16, @bitCast(val.toFloat(f16, zcu)))), |
| 996 | 32 => repr_val_big.set(@as(u32, @bitCast(val.toFloat(f32, zcu)))), |
| 997 | 64 => repr_val_big.set(@as(u64, @bitCast(val.toFloat(f64, zcu)))), |
| 998 | 80 => repr_val_big.set(@as(u80, @bitCast(val.toFloat(f80, zcu)))), |
| 1148 | 999 | 128 => repr_val_big.set(@as(u128, @bitCast(f128_val))), |
| 1149 | 1000 | else => unreachable, |
| 1150 | 1001 | } |
| 1151 | 1002 | |
| 1152 | | const repr_val = try mod.intValue_big(repr_ty, repr_val_big.toConst()); |
| 1153 | | |
| 1154 | 1003 | var empty = true; |
| 1155 | 1004 | if (std.math.isFinite(f128_val)) { |
| 1156 | 1005 | try writer.writeAll("zig_make_"); |
| 1157 | 1006 | try dg.renderTypeForBuiltinFnName(writer, ty); |
| 1158 | 1007 | try writer.writeByte('('); |
| 1159 | 1008 | switch (bits) { |
| 1160 | | 16 => try writer.print("{x}", .{val.toFloat(f16, mod)}), |
| 1161 | | 32 => try writer.print("{x}", .{val.toFloat(f32, mod)}), |
| 1162 | | 64 => try writer.print("{x}", .{val.toFloat(f64, mod)}), |
| 1163 | | 80 => try writer.print("{x}", .{val.toFloat(f80, mod)}), |
| 1009 | 16 => try writer.print("{x}", .{val.toFloat(f16, zcu)}), |
| 1010 | 32 => try writer.print("{x}", .{val.toFloat(f32, zcu)}), |
| 1011 | 64 => try writer.print("{x}", .{val.toFloat(f64, zcu)}), |
| 1012 | 80 => try writer.print("{x}", .{val.toFloat(f80, zcu)}), |
| 1164 | 1013 | 128 => try writer.print("{x}", .{f128_val}), |
| 1165 | 1014 | else => unreachable, |
| 1166 | 1015 | } |
| ... | ... | @@ -1200,17 +1049,20 @@ pub const DeclGen = struct { |
| 1200 | 1049 | if (std.math.isNan(f128_val)) switch (bits) { |
| 1201 | 1050 | // We only actually need to pass the significand, but it will get |
| 1202 | 1051 | // properly masked anyway, so just pass the whole value. |
| 1203 | | 16 => try writer.print("\"0x{x}\"", .{@as(u16, @bitCast(val.toFloat(f16, mod)))}), |
| 1204 | | 32 => try writer.print("\"0x{x}\"", .{@as(u32, @bitCast(val.toFloat(f32, mod)))}), |
| 1205 | | 64 => try writer.print("\"0x{x}\"", .{@as(u64, @bitCast(val.toFloat(f64, mod)))}), |
| 1206 | | 80 => try writer.print("\"0x{x}\"", .{@as(u80, @bitCast(val.toFloat(f80, mod)))}), |
| 1052 | 16 => try writer.print("\"0x{x}\"", .{@as(u16, @bitCast(val.toFloat(f16, zcu)))}), |
| 1053 | 32 => try writer.print("\"0x{x}\"", .{@as(u32, @bitCast(val.toFloat(f32, zcu)))}), |
| 1054 | 64 => try writer.print("\"0x{x}\"", .{@as(u64, @bitCast(val.toFloat(f64, zcu)))}), |
| 1055 | 80 => try writer.print("\"0x{x}\"", .{@as(u80, @bitCast(val.toFloat(f80, zcu)))}), |
| 1207 | 1056 | 128 => try writer.print("\"0x{x}\"", .{@as(u128, @bitCast(f128_val))}), |
| 1208 | 1057 | else => unreachable, |
| 1209 | 1058 | }; |
| 1210 | 1059 | try writer.writeAll(", "); |
| 1211 | 1060 | empty = false; |
| 1212 | 1061 | } |
| 1213 | | try writer.print("{x}", .{try dg.fmtIntLiteral(repr_ty, repr_val, location)}); |
| 1062 | try writer.print("{x}", .{try dg.fmtIntLiteral( |
| 1063 | try zcu.intValue_big(repr_ty, repr_val_big.toConst()), |
| 1064 | location, |
| 1065 | )}); |
| 1214 | 1066 | if (!empty) try writer.writeByte(')'); |
| 1215 | 1067 | }, |
| 1216 | 1068 | .slice => |slice| { |
| ... | ... | @@ -1220,42 +1072,39 @@ pub const DeclGen = struct { |
| 1220 | 1072 | try writer.writeByte(')'); |
| 1221 | 1073 | } |
| 1222 | 1074 | try writer.writeByte('{'); |
| 1223 | | try dg.renderValue(writer, ty.slicePtrFieldType(mod), Value.fromInterned(slice.ptr), initializer_type); |
| 1075 | try dg.renderValue(writer, Value.fromInterned(slice.ptr), initializer_type); |
| 1224 | 1076 | try writer.writeAll(", "); |
| 1225 | | try dg.renderValue(writer, Type.usize, Value.fromInterned(slice.len), initializer_type); |
| 1077 | try dg.renderValue(writer, Value.fromInterned(slice.len), initializer_type); |
| 1226 | 1078 | try writer.writeByte('}'); |
| 1227 | 1079 | }, |
| 1228 | 1080 | .ptr => |ptr| switch (ptr.addr) { |
| 1229 | | .decl => |d| try dg.renderDeclValue(writer, ty, val, d, location), |
| 1230 | | .anon_decl => |decl_val| try dg.renderAnonDeclValue(writer, ty, val, decl_val, location), |
| 1081 | .decl => |d| try dg.renderDeclValue(writer, val, d, location), |
| 1082 | .anon_decl => |decl_val| try dg.renderAnonDeclValue(writer, val, decl_val, location), |
| 1231 | 1083 | .int => |int| { |
| 1232 | 1084 | try writer.writeAll("(("); |
| 1233 | 1085 | try dg.renderType(writer, ty); |
| 1234 | | try writer.print("){x})", .{ |
| 1235 | | try dg.fmtIntLiteral(Type.usize, Value.fromInterned(int), location), |
| 1236 | | }); |
| 1086 | try writer.print("){x})", .{try dg.fmtIntLiteral(Value.fromInterned(int), location)}); |
| 1237 | 1087 | }, |
| 1238 | 1088 | .eu_payload, |
| 1239 | 1089 | .opt_payload, |
| 1240 | 1090 | .elem, |
| 1241 | 1091 | .field, |
| 1242 | | => try dg.renderParentPtr(writer, val.ip_index, location), |
| 1092 | => try dg.renderParentPtr(writer, val.toIntern(), location), |
| 1243 | 1093 | .comptime_field, .comptime_alloc => unreachable, |
| 1244 | 1094 | }, |
| 1245 | 1095 | .opt => |opt| { |
| 1246 | | const payload_ty = ty.optionalChild(mod); |
| 1096 | const payload_ty = ty.optionalChild(zcu); |
| 1247 | 1097 | |
| 1248 | 1098 | const is_null_val = Value.makeBool(opt.val == .none); |
| 1249 | | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) |
| 1250 | | return dg.renderValue(writer, Type.bool, is_null_val, location); |
| 1099 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) |
| 1100 | return dg.renderValue(writer, is_null_val, location); |
| 1251 | 1101 | |
| 1252 | | if (ty.optionalReprIsPayload(mod)) return dg.renderValue( |
| 1102 | if (ty.optionalReprIsPayload(zcu)) return dg.renderValue( |
| 1253 | 1103 | writer, |
| 1254 | | payload_ty, |
| 1255 | 1104 | switch (opt.val) { |
| 1256 | | .none => switch (payload_ty.zigTypeTag(mod)) { |
| 1257 | | .ErrorSet => try mod.intValue(try mod.errorIntType(), 0), |
| 1258 | | .Pointer => try mod.getCoerced(val, payload_ty), |
| 1105 | .none => switch (payload_ty.zigTypeTag(zcu)) { |
| 1106 | .ErrorSet => try zcu.intValue(try zcu.errorIntType(), 0), |
| 1107 | .Pointer => try zcu.getCoerced(val, payload_ty), |
| 1259 | 1108 | else => unreachable, |
| 1260 | 1109 | }, |
| 1261 | 1110 | else => |payload| Value.fromInterned(payload), |
| ... | ... | @@ -1270,15 +1119,19 @@ pub const DeclGen = struct { |
| 1270 | 1119 | } |
| 1271 | 1120 | |
| 1272 | 1121 | try writer.writeAll("{ .payload = "); |
| 1273 | | try dg.renderValue(writer, payload_ty, Value.fromInterned(switch (opt.val) { |
| 1274 | | .none => try mod.intern(.{ .undef = payload_ty.ip_index }), |
| 1275 | | else => |payload| payload, |
| 1276 | | }), initializer_type); |
| 1122 | switch (opt.val) { |
| 1123 | .none => try dg.renderUndefValue(writer, payload_ty, initializer_type), |
| 1124 | else => |payload| try dg.renderValue( |
| 1125 | writer, |
| 1126 | Value.fromInterned(payload), |
| 1127 | initializer_type, |
| 1128 | ), |
| 1129 | } |
| 1277 | 1130 | try writer.writeAll(", .is_null = "); |
| 1278 | | try dg.renderValue(writer, Type.bool, is_null_val, initializer_type); |
| 1131 | try dg.renderValue(writer, is_null_val, initializer_type); |
| 1279 | 1132 | try writer.writeAll(" }"); |
| 1280 | 1133 | }, |
| 1281 | | .aggregate => switch (ip.indexToKey(ty.ip_index)) { |
| 1134 | .aggregate => switch (ip.indexToKey(ty.toIntern())) { |
| 1282 | 1135 | .array_type, .vector_type => { |
| 1283 | 1136 | if (location == .FunctionArgument) { |
| 1284 | 1137 | try writer.writeByte('('); |
| ... | ... | @@ -1287,21 +1140,21 @@ pub const DeclGen = struct { |
| 1287 | 1140 | } |
| 1288 | 1141 | // Fall back to generic implementation. |
| 1289 | 1142 | |
| 1290 | | const ai = ty.arrayInfo(mod); |
| 1291 | | if (ai.elem_type.eql(Type.u8, mod)) { |
| 1292 | | var literal = stringLiteral(writer, ty.arrayLenIncludingSentinel(mod)); |
| 1143 | const ai = ty.arrayInfo(zcu); |
| 1144 | if (ai.elem_type.eql(Type.u8, zcu)) { |
| 1145 | var literal = stringLiteral(writer, ty.arrayLenIncludingSentinel(zcu)); |
| 1293 | 1146 | try literal.start(); |
| 1294 | 1147 | var index: usize = 0; |
| 1295 | 1148 | while (index < ai.len) : (index += 1) { |
| 1296 | | const elem_val = try val.elemValue(mod, index); |
| 1297 | | const elem_val_u8: u8 = if (elem_val.isUndef(mod)) |
| 1149 | const elem_val = try val.elemValue(zcu, index); |
| 1150 | const elem_val_u8: u8 = if (elem_val.isUndef(zcu)) |
| 1298 | 1151 | undefPattern(u8) |
| 1299 | 1152 | else |
| 1300 | | @intCast(elem_val.toUnsignedInt(mod)); |
| 1153 | @intCast(elem_val.toUnsignedInt(zcu)); |
| 1301 | 1154 | try literal.writeChar(elem_val_u8); |
| 1302 | 1155 | } |
| 1303 | 1156 | if (ai.sentinel) |s| { |
| 1304 | | const s_u8: u8 = @intCast(s.toUnsignedInt(mod)); |
| 1157 | const s_u8: u8 = @intCast(s.toUnsignedInt(zcu)); |
| 1305 | 1158 | if (s_u8 != 0) try literal.writeChar(s_u8); |
| 1306 | 1159 | } |
| 1307 | 1160 | try literal.end(); |
| ... | ... | @@ -1310,12 +1163,12 @@ pub const DeclGen = struct { |
| 1310 | 1163 | var index: usize = 0; |
| 1311 | 1164 | while (index < ai.len) : (index += 1) { |
| 1312 | 1165 | if (index != 0) try writer.writeByte(','); |
| 1313 | | const elem_val = try val.elemValue(mod, index); |
| 1314 | | try dg.renderValue(writer, ai.elem_type, elem_val, initializer_type); |
| 1166 | const elem_val = try val.elemValue(zcu, index); |
| 1167 | try dg.renderValue(writer, elem_val, initializer_type); |
| 1315 | 1168 | } |
| 1316 | 1169 | if (ai.sentinel) |s| { |
| 1317 | 1170 | if (index != 0) try writer.writeByte(','); |
| 1318 | | try dg.renderValue(writer, ai.elem_type, s, initializer_type); |
| 1171 | try dg.renderValue(writer, s, initializer_type); |
| 1319 | 1172 | } |
| 1320 | 1173 | try writer.writeByte('}'); |
| 1321 | 1174 | } |
| ... | ... | @@ -1333,19 +1186,21 @@ pub const DeclGen = struct { |
| 1333 | 1186 | const comptime_val = tuple.values.get(ip)[field_index]; |
| 1334 | 1187 | if (comptime_val != .none) continue; |
| 1335 | 1188 | const field_ty = Type.fromInterned(tuple.types.get(ip)[field_index]); |
| 1336 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 1189 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 1337 | 1190 | |
| 1338 | 1191 | if (!empty) try writer.writeByte(','); |
| 1339 | 1192 | |
| 1340 | | const field_val = Value.fromInterned(switch (ip.indexToKey(val.ip_index).aggregate.storage) { |
| 1341 | | .bytes => |bytes| try ip.get(mod.gpa, .{ .int = .{ |
| 1342 | | .ty = field_ty.toIntern(), |
| 1343 | | .storage = .{ .u64 = bytes[field_index] }, |
| 1344 | | } }), |
| 1345 | | .elems => |elems| elems[field_index], |
| 1346 | | .repeated_elem => |elem| elem, |
| 1347 | | }); |
| 1348 | | try dg.renderValue(writer, field_ty, field_val, initializer_type); |
| 1193 | const field_val = Value.fromInterned( |
| 1194 | switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 1195 | .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ |
| 1196 | .ty = field_ty.toIntern(), |
| 1197 | .storage = .{ .u64 = bytes[field_index] }, |
| 1198 | } }), |
| 1199 | .elems => |elems| elems[field_index], |
| 1200 | .repeated_elem => |elem| elem, |
| 1201 | }, |
| 1202 | ); |
| 1203 | try dg.renderValue(writer, field_val, initializer_type); |
| 1349 | 1204 | |
| 1350 | 1205 | empty = false; |
| 1351 | 1206 | } |
| ... | ... | @@ -1366,43 +1221,43 @@ pub const DeclGen = struct { |
| 1366 | 1221 | for (0..struct_type.field_types.len) |field_index| { |
| 1367 | 1222 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 1368 | 1223 | if (struct_type.fieldIsComptime(ip, field_index)) continue; |
| 1369 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 1224 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 1370 | 1225 | |
| 1371 | 1226 | if (!empty) try writer.writeByte(','); |
| 1372 | | const field_val = switch (ip.indexToKey(val.ip_index).aggregate.storage) { |
| 1373 | | .bytes => |bytes| try ip.get(mod.gpa, .{ .int = .{ |
| 1227 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 1228 | .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ |
| 1374 | 1229 | .ty = field_ty.toIntern(), |
| 1375 | 1230 | .storage = .{ .u64 = bytes[field_index] }, |
| 1376 | 1231 | } }), |
| 1377 | 1232 | .elems => |elems| elems[field_index], |
| 1378 | 1233 | .repeated_elem => |elem| elem, |
| 1379 | 1234 | }; |
| 1380 | | try dg.renderValue(writer, field_ty, Value.fromInterned(field_val), initializer_type); |
| 1235 | try dg.renderValue(writer, Value.fromInterned(field_val), initializer_type); |
| 1381 | 1236 | |
| 1382 | 1237 | empty = false; |
| 1383 | 1238 | } |
| 1384 | 1239 | try writer.writeByte('}'); |
| 1385 | 1240 | }, |
| 1386 | 1241 | .@"packed" => { |
| 1387 | | const int_info = ty.intInfo(mod); |
| 1242 | const int_info = ty.intInfo(zcu); |
| 1388 | 1243 | |
| 1389 | 1244 | const bits = Type.smallestUnsignedBits(int_info.bits - 1); |
| 1390 | | const bit_offset_ty = try mod.intType(.unsigned, bits); |
| 1245 | const bit_offset_ty = try zcu.intType(.unsigned, bits); |
| 1391 | 1246 | |
| 1392 | 1247 | var bit_offset: u64 = 0; |
| 1393 | 1248 | var eff_num_fields: usize = 0; |
| 1394 | 1249 | |
| 1395 | 1250 | for (0..struct_type.field_types.len) |field_index| { |
| 1396 | 1251 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 1397 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 1252 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 1398 | 1253 | eff_num_fields += 1; |
| 1399 | 1254 | } |
| 1400 | 1255 | |
| 1401 | 1256 | if (eff_num_fields == 0) { |
| 1402 | 1257 | try writer.writeByte('('); |
| 1403 | | try dg.renderValue(writer, ty, Value.undef, initializer_type); |
| 1258 | try dg.renderUndefValue(writer, ty, initializer_type); |
| 1404 | 1259 | try writer.writeByte(')'); |
| 1405 | | } else if (ty.bitSize(mod) > 64) { |
| 1260 | } else if (ty.bitSize(zcu) > 64) { |
| 1406 | 1261 | // zig_or_u128(zig_or_u128(zig_shl_u128(a, a_off), zig_shl_u128(b, b_off)), zig_shl_u128(c, c_off)) |
| 1407 | 1262 | var num_or = eff_num_fields - 1; |
| 1408 | 1263 | while (num_or > 0) : (num_or -= 1) { |
| ... | ... | @@ -1415,10 +1270,10 @@ pub const DeclGen = struct { |
| 1415 | 1270 | var needs_closing_paren = false; |
| 1416 | 1271 | for (0..struct_type.field_types.len) |field_index| { |
| 1417 | 1272 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 1418 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 1273 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 1419 | 1274 | |
| 1420 | | const field_val = switch (ip.indexToKey(val.ip_index).aggregate.storage) { |
| 1421 | | .bytes => |bytes| try ip.get(mod.gpa, .{ .int = .{ |
| 1275 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 1276 | .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ |
| 1422 | 1277 | .ty = field_ty.toIntern(), |
| 1423 | 1278 | .storage = .{ .u64 = bytes[field_index] }, |
| 1424 | 1279 | } }), |
| ... | ... | @@ -1432,8 +1287,7 @@ pub const DeclGen = struct { |
| 1432 | 1287 | try writer.writeByte('('); |
| 1433 | 1288 | try dg.renderIntCast(writer, ty, cast_context, field_ty, .FunctionArgument); |
| 1434 | 1289 | try writer.writeAll(", "); |
| 1435 | | const bit_offset_val = try mod.intValue(bit_offset_ty, bit_offset); |
| 1436 | | try dg.renderValue(writer, bit_offset_ty, bit_offset_val, .FunctionArgument); |
| 1290 | try dg.renderValue(writer, try zcu.intValue(bit_offset_ty, bit_offset), .FunctionArgument); |
| 1437 | 1291 | try writer.writeByte(')'); |
| 1438 | 1292 | } else { |
| 1439 | 1293 | try dg.renderIntCast(writer, ty, cast_context, field_ty, .FunctionArgument); |
| ... | ... | @@ -1442,7 +1296,7 @@ pub const DeclGen = struct { |
| 1442 | 1296 | if (needs_closing_paren) try writer.writeByte(')'); |
| 1443 | 1297 | if (eff_index != eff_num_fields - 1) try writer.writeAll(", "); |
| 1444 | 1298 | |
| 1445 | | bit_offset += field_ty.bitSize(mod); |
| 1299 | bit_offset += field_ty.bitSize(zcu); |
| 1446 | 1300 | needs_closing_paren = true; |
| 1447 | 1301 | eff_index += 1; |
| 1448 | 1302 | } |
| ... | ... | @@ -1452,15 +1306,15 @@ pub const DeclGen = struct { |
| 1452 | 1306 | var empty = true; |
| 1453 | 1307 | for (0..struct_type.field_types.len) |field_index| { |
| 1454 | 1308 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 1455 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 1309 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 1456 | 1310 | |
| 1457 | 1311 | if (!empty) try writer.writeAll(" | "); |
| 1458 | 1312 | try writer.writeByte('('); |
| 1459 | 1313 | try dg.renderType(writer, ty); |
| 1460 | 1314 | try writer.writeByte(')'); |
| 1461 | 1315 | |
| 1462 | | const field_val = switch (ip.indexToKey(val.ip_index).aggregate.storage) { |
| 1463 | | .bytes => |bytes| try ip.get(mod.gpa, .{ .int = .{ |
| 1316 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 1317 | .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ |
| 1464 | 1318 | .ty = field_ty.toIntern(), |
| 1465 | 1319 | .storage = .{ .u64 = bytes[field_index] }, |
| 1466 | 1320 | } }), |
| ... | ... | @@ -1469,15 +1323,14 @@ pub const DeclGen = struct { |
| 1469 | 1323 | }; |
| 1470 | 1324 | |
| 1471 | 1325 | if (bit_offset != 0) { |
| 1472 | | try dg.renderValue(writer, field_ty, Value.fromInterned(field_val), .Other); |
| 1326 | try dg.renderValue(writer, Value.fromInterned(field_val), .Other); |
| 1473 | 1327 | try writer.writeAll(" << "); |
| 1474 | | const bit_offset_val = try mod.intValue(bit_offset_ty, bit_offset); |
| 1475 | | try dg.renderValue(writer, bit_offset_ty, bit_offset_val, .FunctionArgument); |
| 1328 | try dg.renderValue(writer, try zcu.intValue(bit_offset_ty, bit_offset), .FunctionArgument); |
| 1476 | 1329 | } else { |
| 1477 | | try dg.renderValue(writer, field_ty, Value.fromInterned(field_val), .Other); |
| 1330 | try dg.renderValue(writer, Value.fromInterned(field_val), .Other); |
| 1478 | 1331 | } |
| 1479 | 1332 | |
| 1480 | | bit_offset += field_ty.bitSize(mod); |
| 1333 | bit_offset += field_ty.bitSize(zcu); |
| 1481 | 1334 | empty = false; |
| 1482 | 1335 | } |
| 1483 | 1336 | try writer.writeByte(')'); |
| ... | ... | @@ -1488,9 +1341,9 @@ pub const DeclGen = struct { |
| 1488 | 1341 | else => unreachable, |
| 1489 | 1342 | }, |
| 1490 | 1343 | .un => |un| { |
| 1491 | | const union_obj = mod.typeToUnion(ty).?; |
| 1344 | const union_obj = zcu.typeToUnion(ty).?; |
| 1492 | 1345 | if (un.tag == .none) { |
| 1493 | | const backing_ty = try ty.unionBackingType(mod); |
| 1346 | const backing_ty = try ty.unionBackingType(zcu); |
| 1494 | 1347 | switch (union_obj.getLayout(ip)) { |
| 1495 | 1348 | .@"packed" => { |
| 1496 | 1349 | if (!location.isInitializer()) { |
| ... | ... | @@ -1498,20 +1351,20 @@ pub const DeclGen = struct { |
| 1498 | 1351 | try dg.renderType(writer, backing_ty); |
| 1499 | 1352 | try writer.writeByte(')'); |
| 1500 | 1353 | } |
| 1501 | | try dg.renderValue(writer, backing_ty, Value.fromInterned(un.val), initializer_type); |
| 1354 | try dg.renderValue(writer, Value.fromInterned(un.val), initializer_type); |
| 1502 | 1355 | }, |
| 1503 | 1356 | .@"extern" => { |
| 1504 | 1357 | if (location == .StaticInitializer) { |
| 1505 | 1358 | return dg.fail("TODO: C backend: implement extern union backing type rendering in static initializers", .{}); |
| 1506 | 1359 | } |
| 1507 | 1360 | |
| 1508 | | const ptr_ty = try mod.singleConstPtrType(ty); |
| 1361 | const ptr_ty = try zcu.singleConstPtrType(ty); |
| 1509 | 1362 | try writer.writeAll("*(("); |
| 1510 | 1363 | try dg.renderType(writer, ptr_ty); |
| 1511 | 1364 | try writer.writeAll(")("); |
| 1512 | 1365 | try dg.renderType(writer, backing_ty); |
| 1513 | 1366 | try writer.writeAll("){"); |
| 1514 | | try dg.renderValue(writer, backing_ty, Value.fromInterned(un.val), initializer_type); |
| 1367 | try dg.renderValue(writer, Value.fromInterned(un.val), initializer_type); |
| 1515 | 1368 | try writer.writeAll("})"); |
| 1516 | 1369 | }, |
| 1517 | 1370 | else => unreachable, |
| ... | ... | @@ -1523,21 +1376,21 @@ pub const DeclGen = struct { |
| 1523 | 1376 | try writer.writeByte(')'); |
| 1524 | 1377 | } |
| 1525 | 1378 | |
| 1526 | | const field_index = mod.unionTagFieldIndex(union_obj, Value.fromInterned(un.tag)).?; |
| 1379 | const field_index = zcu.unionTagFieldIndex(union_obj, Value.fromInterned(un.tag)).?; |
| 1527 | 1380 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 1528 | 1381 | const field_name = union_obj.loadTagType(ip).names.get(ip)[field_index]; |
| 1529 | 1382 | if (union_obj.getLayout(ip) == .@"packed") { |
| 1530 | | if (field_ty.hasRuntimeBits(mod)) { |
| 1531 | | if (field_ty.isPtrAtRuntime(mod)) { |
| 1383 | if (field_ty.hasRuntimeBits(zcu)) { |
| 1384 | if (field_ty.isPtrAtRuntime(zcu)) { |
| 1532 | 1385 | try writer.writeByte('('); |
| 1533 | 1386 | try dg.renderType(writer, ty); |
| 1534 | 1387 | try writer.writeByte(')'); |
| 1535 | | } else if (field_ty.zigTypeTag(mod) == .Float) { |
| 1388 | } else if (field_ty.zigTypeTag(zcu) == .Float) { |
| 1536 | 1389 | try writer.writeByte('('); |
| 1537 | 1390 | try dg.renderType(writer, ty); |
| 1538 | 1391 | try writer.writeByte(')'); |
| 1539 | 1392 | } |
| 1540 | | try dg.renderValue(writer, field_ty, Value.fromInterned(un.val), initializer_type); |
| 1393 | try dg.renderValue(writer, Value.fromInterned(un.val), initializer_type); |
| 1541 | 1394 | } else { |
| 1542 | 1395 | try writer.writeAll("0"); |
| 1543 | 1396 | } |
| ... | ... | @@ -1545,30 +1398,236 @@ pub const DeclGen = struct { |
| 1545 | 1398 | } |
| 1546 | 1399 | |
| 1547 | 1400 | try writer.writeByte('{'); |
| 1548 | | if (ty.unionTagTypeSafety(mod)) |tag_ty| { |
| 1549 | | const layout = mod.getUnionLayout(union_obj); |
| 1401 | if (ty.unionTagTypeSafety(zcu)) |_| { |
| 1402 | const layout = zcu.getUnionLayout(union_obj); |
| 1550 | 1403 | if (layout.tag_size != 0) { |
| 1551 | 1404 | try writer.writeAll(" .tag = "); |
| 1552 | | try dg.renderValue(writer, tag_ty, Value.fromInterned(un.tag), initializer_type); |
| 1405 | try dg.renderValue(writer, Value.fromInterned(un.tag), initializer_type); |
| 1553 | 1406 | } |
| 1554 | | if (ty.unionHasAllZeroBitFieldTypes(mod)) return try writer.writeByte('}'); |
| 1407 | if (ty.unionHasAllZeroBitFieldTypes(zcu)) return try writer.writeByte('}'); |
| 1555 | 1408 | if (layout.tag_size != 0) try writer.writeByte(','); |
| 1556 | 1409 | try writer.writeAll(" .payload = {"); |
| 1557 | 1410 | } |
| 1558 | | if (field_ty.hasRuntimeBits(mod)) { |
| 1411 | if (field_ty.hasRuntimeBits(zcu)) { |
| 1559 | 1412 | try writer.print(" .{ } = ", .{fmtIdent(ip.stringToSlice(field_name))}); |
| 1560 | | try dg.renderValue(writer, field_ty, Value.fromInterned(un.val), initializer_type); |
| 1413 | try dg.renderValue(writer, Value.fromInterned(un.val), initializer_type); |
| 1561 | 1414 | try writer.writeByte(' '); |
| 1562 | 1415 | } else for (0..union_obj.field_types.len) |this_field_index| { |
| 1563 | 1416 | const this_field_ty = Type.fromInterned(union_obj.field_types.get(ip)[this_field_index]); |
| 1564 | | if (!this_field_ty.hasRuntimeBits(mod)) continue; |
| 1565 | | try dg.renderValue(writer, this_field_ty, Value.undef, initializer_type); |
| 1417 | if (!this_field_ty.hasRuntimeBits(zcu)) continue; |
| 1418 | try dg.renderUndefValue(writer, this_field_ty, initializer_type); |
| 1566 | 1419 | break; |
| 1567 | 1420 | } |
| 1568 | | if (ty.unionTagTypeSafety(mod)) |_| try writer.writeByte('}'); |
| 1421 | if (ty.unionTagTypeSafety(zcu)) |_| try writer.writeByte('}'); |
| 1422 | try writer.writeByte('}'); |
| 1423 | } |
| 1424 | }, |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | fn renderUndefValue( |
| 1429 | dg: *DeclGen, |
| 1430 | writer: anytype, |
| 1431 | ty: Type, |
| 1432 | location: ValueRenderLocation, |
| 1433 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 1434 | const zcu = dg.zcu; |
| 1435 | const ip = &zcu.intern_pool; |
| 1436 | const target = &dg.mod.resolved_target.result; |
| 1437 | |
| 1438 | const initializer_type: ValueRenderLocation = switch (location) { |
| 1439 | .StaticInitializer => .StaticInitializer, |
| 1440 | else => .Initializer, |
| 1441 | }; |
| 1442 | |
| 1443 | const safety_on = switch (zcu.optimizeMode()) { |
| 1444 | .Debug, .ReleaseSafe => true, |
| 1445 | .ReleaseFast, .ReleaseSmall => false, |
| 1446 | }; |
| 1447 | |
| 1448 | switch (ty.zigTypeTag(zcu)) { |
| 1449 | .Bool => try writer.writeAll(if (safety_on) "0xaa" else "false"), |
| 1450 | .Int, .Enum, .ErrorSet => try writer.print("{x}", .{ |
| 1451 | try dg.fmtIntLiteral(try zcu.undefValue(ty), location), |
| 1452 | }), |
| 1453 | .Float => { |
| 1454 | const bits = ty.floatBits(target.*); |
| 1455 | // All unsigned ints matching float types are pre-allocated. |
| 1456 | const repr_ty = zcu.intType(.unsigned, bits) catch unreachable; |
| 1457 | |
| 1458 | try writer.writeAll("zig_make_"); |
| 1459 | try dg.renderTypeForBuiltinFnName(writer, ty); |
| 1460 | try writer.writeByte('('); |
| 1461 | switch (bits) { |
| 1462 | 16 => try writer.print("{x}", .{@as(f16, @bitCast(undefPattern(i16)))}), |
| 1463 | 32 => try writer.print("{x}", .{@as(f32, @bitCast(undefPattern(i32)))}), |
| 1464 | 64 => try writer.print("{x}", .{@as(f64, @bitCast(undefPattern(i64)))}), |
| 1465 | 80 => try writer.print("{x}", .{@as(f80, @bitCast(undefPattern(i80)))}), |
| 1466 | 128 => try writer.print("{x}", .{@as(f128, @bitCast(undefPattern(i128)))}), |
| 1467 | else => unreachable, |
| 1468 | } |
| 1469 | try writer.writeAll(", "); |
| 1470 | try dg.renderUndefValue(writer, repr_ty, .FunctionArgument); |
| 1471 | try writer.writeByte(')'); |
| 1472 | }, |
| 1473 | .Pointer => if (ty.isSlice(zcu)) { |
| 1474 | if (!location.isInitializer()) { |
| 1475 | try writer.writeByte('('); |
| 1476 | try dg.renderType(writer, ty); |
| 1477 | try writer.writeByte(')'); |
| 1478 | } |
| 1479 | |
| 1480 | try writer.writeAll("{("); |
| 1481 | const ptr_ty = ty.slicePtrFieldType(zcu); |
| 1482 | try dg.renderType(writer, ptr_ty); |
| 1483 | try writer.print("){x}, {0x}}}", .{try dg.fmtIntLiteral(try zcu.undefValue(Type.usize), .Other)}); |
| 1484 | } else { |
| 1485 | try writer.writeAll("(("); |
| 1486 | try dg.renderType(writer, ty); |
| 1487 | try writer.print("){x})", .{try dg.fmtIntLiteral(try zcu.undefValue(Type.usize), .Other)}); |
| 1488 | }, |
| 1489 | .Optional => { |
| 1490 | const payload_ty = ty.optionalChild(zcu); |
| 1491 | |
| 1492 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 1493 | return dg.renderUndefValue(writer, Type.bool, location); |
| 1494 | } |
| 1495 | |
| 1496 | if (ty.optionalReprIsPayload(zcu)) { |
| 1497 | return dg.renderUndefValue(writer, payload_ty, location); |
| 1498 | } |
| 1499 | |
| 1500 | if (!location.isInitializer()) { |
| 1501 | try writer.writeByte('('); |
| 1502 | try dg.renderType(writer, ty); |
| 1503 | try writer.writeByte(')'); |
| 1504 | } |
| 1505 | |
| 1506 | try writer.writeAll("{ .payload = "); |
| 1507 | try dg.renderUndefValue(writer, payload_ty, initializer_type); |
| 1508 | try writer.writeAll(", .is_null = "); |
| 1509 | try dg.renderUndefValue(writer, Type.bool, initializer_type); |
| 1510 | try writer.writeAll(" }"); |
| 1511 | }, |
| 1512 | .Struct => switch (ty.containerLayout(zcu)) { |
| 1513 | .auto, .@"extern" => { |
| 1514 | if (!location.isInitializer()) { |
| 1515 | try writer.writeByte('('); |
| 1516 | try dg.renderType(writer, ty); |
| 1517 | try writer.writeByte(')'); |
| 1518 | } |
| 1519 | |
| 1520 | try writer.writeByte('{'); |
| 1521 | var empty = true; |
| 1522 | for (0..ty.structFieldCount(zcu)) |field_index| { |
| 1523 | if (ty.structFieldIsComptime(field_index, zcu)) continue; |
| 1524 | const field_ty = ty.structFieldType(field_index, zcu); |
| 1525 | if (!field_ty.hasRuntimeBits(zcu)) continue; |
| 1526 | |
| 1527 | if (!empty) try writer.writeByte(','); |
| 1528 | try dg.renderUndefValue(writer, field_ty, initializer_type); |
| 1529 | |
| 1530 | empty = false; |
| 1531 | } |
| 1532 | |
| 1569 | 1533 | try writer.writeByte('}'); |
| 1534 | }, |
| 1535 | .@"packed" => try writer.print("{x}", .{ |
| 1536 | try dg.fmtIntLiteral(try zcu.undefValue(ty), .Other), |
| 1537 | }), |
| 1538 | }, |
| 1539 | .Union => { |
| 1540 | if (!location.isInitializer()) { |
| 1541 | try writer.writeByte('('); |
| 1542 | try dg.renderType(writer, ty); |
| 1543 | try writer.writeByte(')'); |
| 1570 | 1544 | } |
| 1545 | |
| 1546 | try writer.writeByte('{'); |
| 1547 | if (ty.unionTagTypeSafety(zcu)) |tag_ty| { |
| 1548 | const layout = ty.unionGetLayout(zcu); |
| 1549 | if (layout.tag_size != 0) { |
| 1550 | try writer.writeAll(" .tag = "); |
| 1551 | try dg.renderUndefValue(writer, tag_ty, initializer_type); |
| 1552 | } |
| 1553 | if (ty.unionHasAllZeroBitFieldTypes(zcu)) return try writer.writeByte('}'); |
| 1554 | if (layout.tag_size != 0) try writer.writeByte(','); |
| 1555 | try writer.writeAll(" .payload = {"); |
| 1556 | } |
| 1557 | const union_obj = zcu.typeToUnion(ty).?; |
| 1558 | for (0..union_obj.field_types.len) |field_index| { |
| 1559 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 1560 | if (!field_ty.hasRuntimeBits(zcu)) continue; |
| 1561 | try dg.renderUndefValue(writer, field_ty, initializer_type); |
| 1562 | break; |
| 1563 | } |
| 1564 | if (ty.unionTagTypeSafety(zcu)) |_| try writer.writeByte('}'); |
| 1565 | try writer.writeByte('}'); |
| 1571 | 1566 | }, |
| 1567 | .ErrorUnion => { |
| 1568 | const payload_ty = ty.errorUnionPayload(zcu); |
| 1569 | const error_ty = ty.errorUnionSet(zcu); |
| 1570 | |
| 1571 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 1572 | return dg.renderUndefValue(writer, error_ty, location); |
| 1573 | } |
| 1574 | |
| 1575 | if (!location.isInitializer()) { |
| 1576 | try writer.writeByte('('); |
| 1577 | try dg.renderType(writer, ty); |
| 1578 | try writer.writeByte(')'); |
| 1579 | } |
| 1580 | |
| 1581 | try writer.writeAll("{ .payload = "); |
| 1582 | try dg.renderUndefValue(writer, payload_ty, initializer_type); |
| 1583 | try writer.writeAll(", .error = "); |
| 1584 | try dg.renderUndefValue(writer, error_ty, initializer_type); |
| 1585 | try writer.writeAll(" }"); |
| 1586 | }, |
| 1587 | .Array, .Vector => { |
| 1588 | const ai = ty.arrayInfo(zcu); |
| 1589 | if (ai.elem_type.eql(Type.u8, zcu)) { |
| 1590 | const c_len = ty.arrayLenIncludingSentinel(zcu); |
| 1591 | var literal = stringLiteral(writer, c_len); |
| 1592 | try literal.start(); |
| 1593 | var index: u64 = 0; |
| 1594 | while (index < c_len) : (index += 1) |
| 1595 | try literal.writeChar(0xaa); |
| 1596 | try literal.end(); |
| 1597 | } else { |
| 1598 | if (!location.isInitializer()) { |
| 1599 | try writer.writeByte('('); |
| 1600 | try dg.renderType(writer, ty); |
| 1601 | try writer.writeByte(')'); |
| 1602 | } |
| 1603 | |
| 1604 | try writer.writeByte('{'); |
| 1605 | const c_len = ty.arrayLenIncludingSentinel(zcu); |
| 1606 | var index: u64 = 0; |
| 1607 | while (index < c_len) : (index += 1) { |
| 1608 | if (index > 0) try writer.writeAll(", "); |
| 1609 | try dg.renderUndefValue(writer, ty.childType(zcu), initializer_type); |
| 1610 | } |
| 1611 | try writer.writeByte('}'); |
| 1612 | } |
| 1613 | }, |
| 1614 | .ComptimeInt, |
| 1615 | .ComptimeFloat, |
| 1616 | .Type, |
| 1617 | .EnumLiteral, |
| 1618 | .Void, |
| 1619 | .NoReturn, |
| 1620 | .Undefined, |
| 1621 | .Null, |
| 1622 | .Opaque, |
| 1623 | => unreachable, |
| 1624 | |
| 1625 | .Fn, |
| 1626 | .Frame, |
| 1627 | .AnyFrame, |
| 1628 | => |tag| return dg.fail("TODO: C backend: implement value of type {s}", .{ |
| 1629 | @tagName(tag), |
| 1630 | }), |
| 1572 | 1631 | } |
| 1573 | 1632 | } |
| 1574 | 1633 | |
| ... | ... | @@ -1583,14 +1642,14 @@ pub const DeclGen = struct { |
| 1583 | 1642 | }, |
| 1584 | 1643 | ) !void { |
| 1585 | 1644 | const store = &dg.ctypes.set; |
| 1586 | | const mod = dg.module; |
| 1587 | | const ip = &mod.intern_pool; |
| 1645 | const zcu = dg.zcu; |
| 1646 | const ip = &zcu.intern_pool; |
| 1588 | 1647 | |
| 1589 | | const fn_decl = mod.declPtr(fn_decl_index); |
| 1590 | | const fn_ty = fn_decl.typeOf(mod); |
| 1648 | const fn_decl = zcu.declPtr(fn_decl_index); |
| 1649 | const fn_ty = fn_decl.typeOf(zcu); |
| 1591 | 1650 | const fn_cty_idx = try dg.typeToIndex(fn_ty, kind); |
| 1592 | 1651 | |
| 1593 | | const fn_info = mod.typeToFunc(fn_ty).?; |
| 1652 | const fn_info = zcu.typeToFunc(fn_ty).?; |
| 1594 | 1653 | if (fn_info.cc == .Naked) { |
| 1595 | 1654 | switch (kind) { |
| 1596 | 1655 | .forward => try w.writeAll("zig_naked_decl "), |
| ... | ... | @@ -1598,11 +1657,11 @@ pub const DeclGen = struct { |
| 1598 | 1657 | else => unreachable, |
| 1599 | 1658 | } |
| 1600 | 1659 | } |
| 1601 | | if (fn_decl.val.getFunction(mod)) |func| if (func.analysis(ip).is_cold) |
| 1660 | if (fn_decl.val.getFunction(zcu)) |func| if (func.analysis(ip).is_cold) |
| 1602 | 1661 | try w.writeAll("zig_cold "); |
| 1603 | 1662 | if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn "); |
| 1604 | 1663 | |
| 1605 | | var trailing = try renderTypePrefix(dg.pass, store.*, mod, w, fn_cty_idx, .suffix, .{}); |
| 1664 | var trailing = try renderTypePrefix(dg.pass, store.*, zcu, w, fn_cty_idx, .suffix, .{}); |
| 1606 | 1665 | |
| 1607 | 1666 | if (toCallingConvention(fn_info.cc)) |call_conv| { |
| 1608 | 1667 | try w.print("{}zig_callconv({s})", .{ trailing, call_conv }); |
| ... | ... | @@ -1629,7 +1688,7 @@ pub const DeclGen = struct { |
| 1629 | 1688 | try renderTypeSuffix( |
| 1630 | 1689 | dg.pass, |
| 1631 | 1690 | store.*, |
| 1632 | | mod, |
| 1691 | zcu, |
| 1633 | 1692 | w, |
| 1634 | 1693 | fn_cty_idx, |
| 1635 | 1694 | .suffix, |
| ... | ... | @@ -1647,11 +1706,11 @@ pub const DeclGen = struct { |
| 1647 | 1706 | } |
| 1648 | 1707 | switch (name) { |
| 1649 | 1708 | .export_index => |export_index| mangled: { |
| 1650 | | const maybe_exports = mod.decl_exports.get(fn_decl_index); |
| 1709 | const maybe_exports = zcu.decl_exports.get(fn_decl_index); |
| 1651 | 1710 | const external_name = ip.stringToSlice( |
| 1652 | 1711 | if (maybe_exports) |exports| |
| 1653 | 1712 | exports.items[export_index].opts.name |
| 1654 | | else if (fn_decl.isExtern(mod)) |
| 1713 | else if (fn_decl.isExtern(zcu)) |
| 1655 | 1714 | fn_decl.name |
| 1656 | 1715 | else |
| 1657 | 1716 | break :mangled, |
| ... | ... | @@ -1694,15 +1753,15 @@ pub const DeclGen = struct { |
| 1694 | 1753 | } |
| 1695 | 1754 | |
| 1696 | 1755 | fn typeToIndex(dg: *DeclGen, ty: Type, kind: CType.Kind) !CType.Index { |
| 1697 | | return dg.ctypes.typeToIndex(dg.gpa, ty, dg.module, kind); |
| 1756 | return dg.ctypes.typeToIndex(dg.gpa, ty, dg.zcu, dg.mod, kind); |
| 1698 | 1757 | } |
| 1699 | 1758 | |
| 1700 | 1759 | fn typeToCType(dg: *DeclGen, ty: Type, kind: CType.Kind) !CType { |
| 1701 | | return dg.ctypes.typeToCType(dg.gpa, ty, dg.module, kind); |
| 1760 | return dg.ctypes.typeToCType(dg.gpa, ty, dg.zcu, dg.mod, kind); |
| 1702 | 1761 | } |
| 1703 | 1762 | |
| 1704 | 1763 | fn byteSize(dg: *DeclGen, cty: CType) u64 { |
| 1705 | | return cty.byteSize(dg.ctypes.set, dg.module.getTarget()); |
| 1764 | return cty.byteSize(dg.ctypes.set, dg.mod); |
| 1706 | 1765 | } |
| 1707 | 1766 | |
| 1708 | 1767 | /// Renders a type as a single identifier, generating intermediate typedefs |
| ... | ... | @@ -1722,9 +1781,9 @@ pub const DeclGen = struct { |
| 1722 | 1781 | |
| 1723 | 1782 | fn renderCType(dg: *DeclGen, w: anytype, idx: CType.Index) error{ OutOfMemory, AnalysisFail }!void { |
| 1724 | 1783 | const store = &dg.ctypes.set; |
| 1725 | | const mod = dg.module; |
| 1726 | | _ = try renderTypePrefix(dg.pass, store.*, mod, w, idx, .suffix, .{}); |
| 1727 | | try renderTypeSuffix(dg.pass, store.*, mod, w, idx, .suffix, .{}); |
| 1784 | const zcu = dg.zcu; |
| 1785 | _ = try renderTypePrefix(dg.pass, store.*, zcu, w, idx, .suffix, .{}); |
| 1786 | try renderTypeSuffix(dg.pass, store.*, zcu, w, idx, .suffix, .{}); |
| 1728 | 1787 | } |
| 1729 | 1788 | |
| 1730 | 1789 | const IntCastContext = union(enum) { |
| ... | ... | @@ -1737,15 +1796,13 @@ pub const DeclGen = struct { |
| 1737 | 1796 | value: Value, |
| 1738 | 1797 | }, |
| 1739 | 1798 | |
| 1740 | | pub fn writeValue(self: *const IntCastContext, dg: *DeclGen, w: anytype, value_ty: Type, location: ValueRenderLocation) !void { |
| 1799 | pub fn writeValue(self: *const IntCastContext, dg: *DeclGen, w: anytype, location: ValueRenderLocation) !void { |
| 1741 | 1800 | switch (self.*) { |
| 1742 | 1801 | .c_value => |v| { |
| 1743 | 1802 | try v.f.writeCValue(w, v.value, location); |
| 1744 | 1803 | try v.v.elem(v.f, w); |
| 1745 | 1804 | }, |
| 1746 | | .value => |v| { |
| 1747 | | try dg.renderValue(w, value_ty, v.value, location); |
| 1748 | | }, |
| 1805 | .value => |v| try dg.renderValue(w, v.value, location), |
| 1749 | 1806 | } |
| 1750 | 1807 | } |
| 1751 | 1808 | }; |
| ... | ... | @@ -1764,18 +1821,18 @@ pub const DeclGen = struct { |
| 1764 | 1821 | /// | > 64 bit integer | < 64 bit integer | zig_make_<dest_ty>(0, src) |
| 1765 | 1822 | /// | > 64 bit integer | > 64 bit integer | zig_make_<dest_ty>(zig_hi_<src_ty>(src), zig_lo_<src_ty>(src)) |
| 1766 | 1823 | fn renderIntCast(dg: *DeclGen, w: anytype, dest_ty: Type, context: IntCastContext, src_ty: Type, location: ValueRenderLocation) !void { |
| 1767 | | const mod = dg.module; |
| 1768 | | const dest_bits = dest_ty.bitSize(mod); |
| 1769 | | const dest_int_info = dest_ty.intInfo(mod); |
| 1824 | const zcu = dg.zcu; |
| 1825 | const dest_bits = dest_ty.bitSize(zcu); |
| 1826 | const dest_int_info = dest_ty.intInfo(zcu); |
| 1770 | 1827 | |
| 1771 | | const src_is_ptr = src_ty.isPtrAtRuntime(mod); |
| 1828 | const src_is_ptr = src_ty.isPtrAtRuntime(zcu); |
| 1772 | 1829 | const src_eff_ty: Type = if (src_is_ptr) switch (dest_int_info.signedness) { |
| 1773 | 1830 | .unsigned => Type.usize, |
| 1774 | 1831 | .signed => Type.isize, |
| 1775 | 1832 | } else src_ty; |
| 1776 | 1833 | |
| 1777 | | const src_bits = src_eff_ty.bitSize(mod); |
| 1778 | | const src_int_info = if (src_eff_ty.isAbiInt(mod)) src_eff_ty.intInfo(mod) else null; |
| 1834 | const src_bits = src_eff_ty.bitSize(zcu); |
| 1835 | const src_int_info = if (src_eff_ty.isAbiInt(zcu)) src_eff_ty.intInfo(zcu) else null; |
| 1779 | 1836 | if (dest_bits <= 64 and src_bits <= 64) { |
| 1780 | 1837 | const needs_cast = src_int_info == null or |
| 1781 | 1838 | (toCIntBits(dest_int_info.bits) != toCIntBits(src_int_info.?.bits) or |
| ... | ... | @@ -1791,7 +1848,7 @@ pub const DeclGen = struct { |
| 1791 | 1848 | try dg.renderType(w, src_eff_ty); |
| 1792 | 1849 | try w.writeByte(')'); |
| 1793 | 1850 | } |
| 1794 | | try context.writeValue(dg, w, src_ty, location); |
| 1851 | try context.writeValue(dg, w, location); |
| 1795 | 1852 | } else if (dest_bits <= 64 and src_bits > 64) { |
| 1796 | 1853 | assert(!src_is_ptr); |
| 1797 | 1854 | if (dest_bits < 64) { |
| ... | ... | @@ -1802,7 +1859,7 @@ pub const DeclGen = struct { |
| 1802 | 1859 | try w.writeAll("zig_lo_"); |
| 1803 | 1860 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); |
| 1804 | 1861 | try w.writeByte('('); |
| 1805 | | try context.writeValue(dg, w, src_ty, .FunctionArgument); |
| 1862 | try context.writeValue(dg, w, .FunctionArgument); |
| 1806 | 1863 | try w.writeByte(')'); |
| 1807 | 1864 | } else if (dest_bits > 64 and src_bits <= 64) { |
| 1808 | 1865 | try w.writeAll("zig_make_"); |
| ... | ... | @@ -1813,7 +1870,7 @@ pub const DeclGen = struct { |
| 1813 | 1870 | try dg.renderType(w, src_eff_ty); |
| 1814 | 1871 | try w.writeByte(')'); |
| 1815 | 1872 | } |
| 1816 | | try context.writeValue(dg, w, src_ty, .FunctionArgument); |
| 1873 | try context.writeValue(dg, w, .FunctionArgument); |
| 1817 | 1874 | try w.writeByte(')'); |
| 1818 | 1875 | } else { |
| 1819 | 1876 | assert(!src_is_ptr); |
| ... | ... | @@ -1822,11 +1879,11 @@ pub const DeclGen = struct { |
| 1822 | 1879 | try w.writeAll("(zig_hi_"); |
| 1823 | 1880 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); |
| 1824 | 1881 | try w.writeByte('('); |
| 1825 | | try context.writeValue(dg, w, src_ty, .FunctionArgument); |
| 1882 | try context.writeValue(dg, w, .FunctionArgument); |
| 1826 | 1883 | try w.writeAll("), zig_lo_"); |
| 1827 | 1884 | try dg.renderTypeForBuiltinFnName(w, src_eff_ty); |
| 1828 | 1885 | try w.writeByte('('); |
| 1829 | | try context.writeValue(dg, w, src_ty, .FunctionArgument); |
| 1886 | try context.writeValue(dg, w, .FunctionArgument); |
| 1830 | 1887 | try w.writeAll("))"); |
| 1831 | 1888 | } |
| 1832 | 1889 | } |
| ... | ... | @@ -1848,8 +1905,8 @@ pub const DeclGen = struct { |
| 1848 | 1905 | alignment: Alignment, |
| 1849 | 1906 | kind: CType.Kind, |
| 1850 | 1907 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 1851 | | const mod = dg.module; |
| 1852 | | const alignas = CType.AlignAs.init(alignment, ty.abiAlignment(mod)); |
| 1908 | const zcu = dg.zcu; |
| 1909 | const alignas = CType.AlignAs.init(alignment, ty.abiAlignment(zcu)); |
| 1853 | 1910 | try dg.renderCTypeAndName(w, try dg.typeToIndex(ty, kind), name, qualifiers, alignas); |
| 1854 | 1911 | } |
| 1855 | 1912 | |
| ... | ... | @@ -1862,7 +1919,7 @@ pub const DeclGen = struct { |
| 1862 | 1919 | alignas: CType.AlignAs, |
| 1863 | 1920 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 1864 | 1921 | const store = &dg.ctypes.set; |
| 1865 | | const mod = dg.module; |
| 1922 | const zcu = dg.zcu; |
| 1866 | 1923 | |
| 1867 | 1924 | switch (alignas.abiOrder()) { |
| 1868 | 1925 | .lt => try w.print("zig_under_align({}) ", .{alignas.toByteUnits()}), |
| ... | ... | @@ -1870,39 +1927,46 @@ pub const DeclGen = struct { |
| 1870 | 1927 | .gt => try w.print("zig_align({}) ", .{alignas.toByteUnits()}), |
| 1871 | 1928 | } |
| 1872 | 1929 | |
| 1873 | | const trailing = try renderTypePrefix(dg.pass, store.*, mod, w, cty_idx, .suffix, qualifiers); |
| 1930 | const trailing = try renderTypePrefix(dg.pass, store.*, zcu, w, cty_idx, .suffix, qualifiers); |
| 1874 | 1931 | try w.print("{}", .{trailing}); |
| 1875 | | try dg.writeCValue(w, name); |
| 1876 | | try renderTypeSuffix(dg.pass, store.*, mod, w, cty_idx, .suffix, .{}); |
| 1932 | try dg.writeName(w, name); |
| 1933 | try renderTypeSuffix(dg.pass, store.*, zcu, w, cty_idx, .suffix, .{}); |
| 1877 | 1934 | } |
| 1878 | 1935 | |
| 1879 | 1936 | fn declIsGlobal(dg: *DeclGen, val: Value) bool { |
| 1880 | | const mod = dg.module; |
| 1881 | | return switch (mod.intern_pool.indexToKey(val.ip_index)) { |
| 1882 | | .variable => |variable| mod.decl_exports.contains(variable.decl), |
| 1937 | const zcu = dg.zcu; |
| 1938 | return switch (zcu.intern_pool.indexToKey(val.toIntern())) { |
| 1939 | .variable => |variable| zcu.decl_exports.contains(variable.decl), |
| 1883 | 1940 | .extern_func => true, |
| 1884 | | .func => |func| mod.decl_exports.contains(func.owner_decl), |
| 1941 | .func => |func| zcu.decl_exports.contains(func.owner_decl), |
| 1885 | 1942 | else => unreachable, |
| 1886 | 1943 | }; |
| 1887 | 1944 | } |
| 1888 | 1945 | |
| 1946 | fn writeName(dg: *DeclGen, w: anytype, c_value: CValue) !void { |
| 1947 | switch (c_value) { |
| 1948 | .new_local, .local => |i| try w.print("t{d}", .{i}), |
| 1949 | .constant => |val| try renderAnonDeclName(w, val), |
| 1950 | .decl => |decl| try dg.renderDeclName(w, decl, 0), |
| 1951 | .identifier => |ident| try w.print("{ }", .{fmtIdent(ident)}), |
| 1952 | else => unreachable, |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1889 | 1956 | fn writeCValue(dg: *DeclGen, w: anytype, c_value: CValue) !void { |
| 1890 | 1957 | switch (c_value) { |
| 1891 | | .none => unreachable, |
| 1892 | | .local, .new_local => |i| return w.print("t{d}", .{i}), |
| 1893 | | .local_ref => |i| return w.print("&t{d}", .{i}), |
| 1894 | | .constant => |val| return renderAnonDeclName(w, val), |
| 1895 | | .arg => |i| return w.print("a{d}", .{i}), |
| 1896 | | .arg_array => |i| return dg.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" }), |
| 1897 | | .field => |i| return w.print("f{d}", .{i}), |
| 1898 | | .decl => |decl| return dg.renderDeclName(w, decl, 0), |
| 1958 | .none, .new_local, .local, .local_ref => unreachable, |
| 1959 | .constant => |val| try renderAnonDeclName(w, val), |
| 1960 | .arg, .arg_array => unreachable, |
| 1961 | .field => |i| try w.print("f{d}", .{i}), |
| 1962 | .decl => |decl| try dg.renderDeclName(w, decl, 0), |
| 1899 | 1963 | .decl_ref => |decl| { |
| 1900 | 1964 | try w.writeByte('&'); |
| 1901 | | return dg.renderDeclName(w, decl, 0); |
| 1965 | try dg.renderDeclName(w, decl, 0); |
| 1902 | 1966 | }, |
| 1903 | | .undef => |ty| return dg.renderValue(w, ty, Value.undef, .Other), |
| 1904 | | .identifier => |ident| return w.print("{ }", .{fmtIdent(ident)}), |
| 1905 | | .payload_identifier => |ident| return w.print("{ }.{ }", .{ |
| 1967 | .undef => |ty| try dg.renderUndefValue(w, ty, .Other), |
| 1968 | .identifier => |ident| try w.print("{ }", .{fmtIdent(ident)}), |
| 1969 | .payload_identifier => |ident| try w.print("{ }.{ }", .{ |
| 1906 | 1970 | fmtIdent("payload"), |
| 1907 | 1971 | fmtIdent(ident), |
| 1908 | 1972 | }), |
| ... | ... | @@ -1911,26 +1975,17 @@ pub const DeclGen = struct { |
| 1911 | 1975 | |
| 1912 | 1976 | fn writeCValueDeref(dg: *DeclGen, w: anytype, c_value: CValue) !void { |
| 1913 | 1977 | switch (c_value) { |
| 1914 | | .none => unreachable, |
| 1915 | | .local, .new_local => |i| return w.print("(*t{d})", .{i}), |
| 1916 | | .local_ref => |i| return w.print("t{d}", .{i}), |
| 1917 | | .constant => unreachable, |
| 1918 | | .arg => |i| return w.print("(*a{d})", .{i}), |
| 1919 | | .arg_array => |i| { |
| 1920 | | try w.writeAll("(*"); |
| 1921 | | try dg.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" }); |
| 1922 | | return w.writeByte(')'); |
| 1923 | | }, |
| 1924 | | .field => |i| return w.print("f{d}", .{i}), |
| 1978 | .none, .new_local, .local, .local_ref, .constant, .arg, .arg_array => unreachable, |
| 1979 | .field => |i| try w.print("f{d}", .{i}), |
| 1925 | 1980 | .decl => |decl| { |
| 1926 | 1981 | try w.writeAll("(*"); |
| 1927 | 1982 | try dg.renderDeclName(w, decl, 0); |
| 1928 | | return w.writeByte(')'); |
| 1983 | try w.writeByte(')'); |
| 1929 | 1984 | }, |
| 1930 | | .decl_ref => |decl| return dg.renderDeclName(w, decl, 0), |
| 1985 | .decl_ref => |decl| try dg.renderDeclName(w, decl, 0), |
| 1931 | 1986 | .undef => unreachable, |
| 1932 | | .identifier => |ident| return w.print("(*{ })", .{fmtIdent(ident)}), |
| 1933 | | .payload_identifier => |ident| return w.print("(*{ }.{ })", .{ |
| 1987 | .identifier => |ident| try w.print("(*{ })", .{fmtIdent(ident)}), |
| 1988 | .payload_identifier => |ident| try w.print("(*{ }.{ })", .{ |
| 1934 | 1989 | fmtIdent("payload"), |
| 1935 | 1990 | fmtIdent(ident), |
| 1936 | 1991 | }), |
| ... | ... | @@ -1950,12 +2005,12 @@ pub const DeclGen = struct { |
| 1950 | 2005 | |
| 1951 | 2006 | fn writeCValueDerefMember(dg: *DeclGen, writer: anytype, c_value: CValue, member: CValue) !void { |
| 1952 | 2007 | switch (c_value) { |
| 1953 | | .none, .constant, .field, .undef => unreachable, |
| 1954 | | .new_local, .local, .arg, .arg_array, .decl, .identifier, .payload_identifier => { |
| 2008 | .none, .new_local, .local, .local_ref, .constant, .field, .undef, .arg, .arg_array => unreachable, |
| 2009 | .decl, .identifier, .payload_identifier => { |
| 1955 | 2010 | try dg.writeCValue(writer, c_value); |
| 1956 | 2011 | try writer.writeAll("->"); |
| 1957 | 2012 | }, |
| 1958 | | .local_ref, .decl_ref => { |
| 2013 | .decl_ref => { |
| 1959 | 2014 | try dg.writeCValueDeref(writer, c_value); |
| 1960 | 2015 | try writer.writeByte('.'); |
| 1961 | 2016 | }, |
| ... | ... | @@ -1969,11 +2024,12 @@ pub const DeclGen = struct { |
| 1969 | 2024 | variable: InternPool.Key.Variable, |
| 1970 | 2025 | fwd_kind: enum { tentative, final }, |
| 1971 | 2026 | ) !void { |
| 1972 | | const decl = dg.module.declPtr(decl_index); |
| 2027 | const zcu = dg.zcu; |
| 2028 | const decl = zcu.declPtr(decl_index); |
| 1973 | 2029 | const fwd = dg.fwdDeclWriter(); |
| 1974 | 2030 | const is_global = variable.is_extern or dg.declIsGlobal(decl.val); |
| 1975 | 2031 | try fwd.writeAll(if (is_global) "zig_extern " else "static "); |
| 1976 | | const maybe_exports = dg.module.decl_exports.get(decl_index); |
| 2032 | const maybe_exports = zcu.decl_exports.get(decl_index); |
| 1977 | 2033 | const export_weak_linkage = if (maybe_exports) |exports| |
| 1978 | 2034 | exports.items[0].opts.linkage == .weak |
| 1979 | 2035 | else |
| ... | ... | @@ -1982,14 +2038,14 @@ pub const DeclGen = struct { |
| 1982 | 2038 | if (variable.is_threadlocal) try fwd.writeAll("zig_threadlocal "); |
| 1983 | 2039 | try dg.renderTypeAndName( |
| 1984 | 2040 | fwd, |
| 1985 | | decl.typeOf(dg.module), |
| 2041 | decl.typeOf(zcu), |
| 1986 | 2042 | .{ .decl = decl_index }, |
| 1987 | 2043 | CQualifiers.init(.{ .@"const" = variable.is_const }), |
| 1988 | 2044 | decl.alignment, |
| 1989 | 2045 | .complete, |
| 1990 | 2046 | ); |
| 1991 | 2047 | mangled: { |
| 1992 | | const external_name = dg.module.intern_pool.stringToSlice(if (maybe_exports) |exports| |
| 2048 | const external_name = zcu.intern_pool.stringToSlice(if (maybe_exports) |exports| |
| 1993 | 2049 | exports.items[0].opts.name |
| 1994 | 2050 | else if (variable.is_extern) |
| 1995 | 2051 | decl.name |
| ... | ... | @@ -2007,23 +2063,23 @@ pub const DeclGen = struct { |
| 2007 | 2063 | } |
| 2008 | 2064 | |
| 2009 | 2065 | fn renderDeclName(dg: *DeclGen, writer: anytype, decl_index: InternPool.DeclIndex, export_index: u32) !void { |
| 2010 | | const mod = dg.module; |
| 2011 | | const decl = mod.declPtr(decl_index); |
| 2066 | const zcu = dg.zcu; |
| 2067 | const decl = zcu.declPtr(decl_index); |
| 2012 | 2068 | |
| 2013 | | if (mod.decl_exports.get(decl_index)) |exports| { |
| 2069 | if (zcu.decl_exports.get(decl_index)) |exports| { |
| 2014 | 2070 | try writer.print("{ }", .{ |
| 2015 | | fmtIdent(mod.intern_pool.stringToSlice(exports.items[export_index].opts.name)), |
| 2071 | fmtIdent(zcu.intern_pool.stringToSlice(exports.items[export_index].opts.name)), |
| 2016 | 2072 | }); |
| 2017 | | } else if (decl.getExternDecl(mod).unwrap()) |extern_decl_index| { |
| 2073 | } else if (decl.getExternDecl(zcu).unwrap()) |extern_decl_index| { |
| 2018 | 2074 | try writer.print("{ }", .{ |
| 2019 | | fmtIdent(mod.intern_pool.stringToSlice(mod.declPtr(extern_decl_index).name)), |
| 2075 | fmtIdent(zcu.intern_pool.stringToSlice(zcu.declPtr(extern_decl_index).name)), |
| 2020 | 2076 | }); |
| 2021 | 2077 | } else { |
| 2022 | 2078 | // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case), |
| 2023 | 2079 | // expand to 3x the length of its input, but let's cut it off at a much shorter limit. |
| 2024 | 2080 | var name: [100]u8 = undefined; |
| 2025 | 2081 | var name_stream = std.io.fixedBufferStream(&name); |
| 2026 | | decl.renderFullyQualifiedName(mod, name_stream.writer()) catch |err| switch (err) { |
| 2082 | decl.renderFullyQualifiedName(zcu, name_stream.writer()) catch |err| switch (err) { |
| 2027 | 2083 | error.NoSpaceLeft => {}, |
| 2028 | 2084 | }; |
| 2029 | 2085 | try writer.print("{}__{d}", .{ |
| ... | ... | @@ -2033,8 +2089,8 @@ pub const DeclGen = struct { |
| 2033 | 2089 | } |
| 2034 | 2090 | } |
| 2035 | 2091 | |
| 2036 | | fn renderAnonDeclName(writer: anytype, anon_decl_val: InternPool.Index) !void { |
| 2037 | | return writer.print("__anon_{d}", .{@intFromEnum(anon_decl_val)}); |
| 2092 | fn renderAnonDeclName(writer: anytype, anon_decl_val: Value) !void { |
| 2093 | try writer.print("__anon_{d}", .{@intFromEnum(anon_decl_val.toIntern())}); |
| 2038 | 2094 | } |
| 2039 | 2095 | |
| 2040 | 2096 | fn renderTypeForBuiltinFnName(dg: *DeclGen, writer: anytype, ty: Type) !void { |
| ... | ... | @@ -2047,7 +2103,7 @@ pub const DeclGen = struct { |
| 2047 | 2103 | if (cty.isBool()) |
| 2048 | 2104 | signAbbrev(.unsigned) |
| 2049 | 2105 | else if (cty.isInteger()) |
| 2050 | | signAbbrev(cty.signedness(dg.module.getTarget())) |
| 2106 | signAbbrev(cty.signedness(dg.mod)) |
| 2051 | 2107 | else if (cty.isFloat()) |
| 2052 | 2108 | @as(u8, 'f') |
| 2053 | 2109 | else if (cty.isPointer()) |
| ... | ... | @@ -2056,7 +2112,7 @@ pub const DeclGen = struct { |
| 2056 | 2112 | return dg.fail("TODO: CBE: implement renderTypeForBuiltinFnName for type {}", .{ |
| 2057 | 2113 | cty.tag(), |
| 2058 | 2114 | }), |
| 2059 | | if (cty.isFloat()) cty.floatActiveBits(dg.module.getTarget()) else dg.byteSize(cty) * 8, |
| 2115 | if (cty.isFloat()) cty.floatActiveBits(dg.mod) else dg.byteSize(cty) * 8, |
| 2060 | 2116 | }), |
| 2061 | 2117 | .array => try writer.writeAll("big"), |
| 2062 | 2118 | } |
| ... | ... | @@ -2065,43 +2121,39 @@ pub const DeclGen = struct { |
| 2065 | 2121 | fn renderBuiltinInfo(dg: *DeclGen, writer: anytype, ty: Type, info: BuiltinInfo) !void { |
| 2066 | 2122 | const cty = try dg.typeToCType(ty, .complete); |
| 2067 | 2123 | const is_big = cty.tag() == .array; |
| 2068 | | |
| 2069 | 2124 | switch (info) { |
| 2070 | 2125 | .none => if (!is_big) return, |
| 2071 | 2126 | .bits => {}, |
| 2072 | 2127 | } |
| 2073 | 2128 | |
| 2074 | | const mod = dg.module; |
| 2075 | | const int_info = if (ty.isAbiInt(mod)) ty.intInfo(mod) else std.builtin.Type.Int{ |
| 2129 | const zcu = dg.zcu; |
| 2130 | const int_info = if (ty.isAbiInt(zcu)) ty.intInfo(zcu) else std.builtin.Type.Int{ |
| 2076 | 2131 | .signedness = .unsigned, |
| 2077 | | .bits = @as(u16, @intCast(ty.bitSize(mod))), |
| 2132 | .bits = @as(u16, @intCast(ty.bitSize(zcu))), |
| 2078 | 2133 | }; |
| 2079 | 2134 | |
| 2080 | 2135 | if (is_big) try writer.print(", {}", .{int_info.signedness == .signed}); |
| 2081 | | |
| 2082 | | const bits_ty = if (is_big) Type.u16 else Type.u8; |
| 2083 | 2136 | try writer.print(", {}", .{try dg.fmtIntLiteral( |
| 2084 | | bits_ty, |
| 2085 | | try mod.intValue(bits_ty, int_info.bits), |
| 2137 | try zcu.intValue(if (is_big) Type.u16 else Type.u8, int_info.bits), |
| 2086 | 2138 | .FunctionArgument, |
| 2087 | 2139 | )}); |
| 2088 | 2140 | } |
| 2089 | 2141 | |
| 2090 | 2142 | fn fmtIntLiteral( |
| 2091 | 2143 | dg: *DeclGen, |
| 2092 | | ty: Type, |
| 2093 | 2144 | val: Value, |
| 2094 | 2145 | loc: ValueRenderLocation, |
| 2095 | 2146 | ) !std.fmt.Formatter(formatIntLiteral) { |
| 2096 | | const mod = dg.module; |
| 2147 | const zcu = dg.zcu; |
| 2097 | 2148 | const kind: CType.Kind = switch (loc) { |
| 2098 | 2149 | .FunctionArgument => .parameter, |
| 2099 | 2150 | .Initializer, .Other => .complete, |
| 2100 | 2151 | .StaticInitializer => .global, |
| 2101 | 2152 | }; |
| 2153 | const ty = val.typeOf(zcu); |
| 2102 | 2154 | return std.fmt.Formatter(formatIntLiteral){ .data = .{ |
| 2103 | 2155 | .dg = dg, |
| 2104 | | .int_info = ty.intInfo(mod), |
| 2156 | .int_info = ty.intInfo(zcu), |
| 2105 | 2157 | .kind = kind, |
| 2106 | 2158 | .cty = try dg.typeToCType(ty, kind), |
| 2107 | 2159 | .val = val, |
| ... | ... | @@ -2133,7 +2185,7 @@ const RenderCTypeTrailing = enum { |
| 2133 | 2185 | } |
| 2134 | 2186 | }; |
| 2135 | 2187 | fn renderTypeName( |
| 2136 | | mod: *Module, |
| 2188 | zcu: *Zcu, |
| 2137 | 2189 | w: anytype, |
| 2138 | 2190 | idx: CType.Index, |
| 2139 | 2191 | cty: CType, |
| ... | ... | @@ -2157,7 +2209,7 @@ fn renderTypeName( |
| 2157 | 2209 | try w.print("{s} {s}{}__{d}", .{ |
| 2158 | 2210 | @tagName(tag)["fwd_".len..], |
| 2159 | 2211 | attributes, |
| 2160 | | fmtIdent(mod.intern_pool.stringToSlice(mod.declPtr(owner_decl).name)), |
| 2212 | fmtIdent(zcu.intern_pool.stringToSlice(zcu.declPtr(owner_decl).name)), |
| 2161 | 2213 | @intFromEnum(owner_decl), |
| 2162 | 2214 | }); |
| 2163 | 2215 | }, |
| ... | ... | @@ -2166,7 +2218,7 @@ fn renderTypeName( |
| 2166 | 2218 | fn renderTypePrefix( |
| 2167 | 2219 | pass: DeclGen.Pass, |
| 2168 | 2220 | store: CType.Store.Set, |
| 2169 | | mod: *Module, |
| 2221 | zcu: *Zcu, |
| 2170 | 2222 | w: anytype, |
| 2171 | 2223 | idx: CType.Index, |
| 2172 | 2224 | parent_fix: CTypeFix, |
| ... | ... | @@ -2224,7 +2276,7 @@ fn renderTypePrefix( |
| 2224 | 2276 | const child_trailing = try renderTypePrefix( |
| 2225 | 2277 | pass, |
| 2226 | 2278 | store, |
| 2227 | | mod, |
| 2279 | zcu, |
| 2228 | 2280 | w, |
| 2229 | 2281 | child_idx, |
| 2230 | 2282 | .prefix, |
| ... | ... | @@ -2247,7 +2299,7 @@ fn renderTypePrefix( |
| 2247 | 2299 | => { |
| 2248 | 2300 | const child_idx = cty.cast(CType.Payload.Sequence).?.data.elem_type; |
| 2249 | 2301 | const child_trailing = |
| 2250 | | try renderTypePrefix(pass, store, mod, w, child_idx, .suffix, qualifiers); |
| 2302 | try renderTypePrefix(pass, store, zcu, w, child_idx, .suffix, qualifiers); |
| 2251 | 2303 | switch (parent_fix) { |
| 2252 | 2304 | .prefix => { |
| 2253 | 2305 | try w.print("{}(", .{child_trailing}); |
| ... | ... | @@ -2262,12 +2314,12 @@ fn renderTypePrefix( |
| 2262 | 2314 | => switch (pass) { |
| 2263 | 2315 | .decl => |decl_index| try w.print("decl__{d}_{d}", .{ @intFromEnum(decl_index), idx }), |
| 2264 | 2316 | .anon => |anon_decl| try w.print("anon__{d}_{d}", .{ @intFromEnum(anon_decl), idx }), |
| 2265 | | .flush => try renderTypeName(mod, w, idx, cty, ""), |
| 2317 | .flush => try renderTypeName(zcu, w, idx, cty, ""), |
| 2266 | 2318 | }, |
| 2267 | 2319 | |
| 2268 | 2320 | .fwd_struct, |
| 2269 | 2321 | .fwd_union, |
| 2270 | | => try renderTypeName(mod, w, idx, cty, ""), |
| 2322 | => try renderTypeName(zcu, w, idx, cty, ""), |
| 2271 | 2323 | |
| 2272 | 2324 | .unnamed_struct, |
| 2273 | 2325 | .unnamed_union, |
| ... | ... | @@ -2278,7 +2330,7 @@ fn renderTypePrefix( |
| 2278 | 2330 | @tagName(tag)["unnamed_".len..], |
| 2279 | 2331 | if (cty.isPacked()) "zig_packed(" else "", |
| 2280 | 2332 | }); |
| 2281 | | try renderAggregateFields(mod, w, store, cty, 1); |
| 2333 | try renderAggregateFields(zcu, w, store, cty, 1); |
| 2282 | 2334 | if (cty.isPacked()) try w.writeByte(')'); |
| 2283 | 2335 | }, |
| 2284 | 2336 | |
| ... | ... | @@ -2291,7 +2343,7 @@ fn renderTypePrefix( |
| 2291 | 2343 | => return renderTypePrefix( |
| 2292 | 2344 | pass, |
| 2293 | 2345 | store, |
| 2294 | | mod, |
| 2346 | zcu, |
| 2295 | 2347 | w, |
| 2296 | 2348 | cty.cast(CType.Payload.Aggregate).?.data.fwd_decl, |
| 2297 | 2349 | parent_fix, |
| ... | ... | @@ -2304,7 +2356,7 @@ fn renderTypePrefix( |
| 2304 | 2356 | const child_trailing = try renderTypePrefix( |
| 2305 | 2357 | pass, |
| 2306 | 2358 | store, |
| 2307 | | mod, |
| 2359 | zcu, |
| 2308 | 2360 | w, |
| 2309 | 2361 | cty.cast(CType.Payload.Function).?.data.return_type, |
| 2310 | 2362 | .suffix, |
| ... | ... | @@ -2331,7 +2383,7 @@ fn renderTypePrefix( |
| 2331 | 2383 | fn renderTypeSuffix( |
| 2332 | 2384 | pass: DeclGen.Pass, |
| 2333 | 2385 | store: CType.Store.Set, |
| 2334 | | mod: *Module, |
| 2386 | zcu: *Zcu, |
| 2335 | 2387 | w: anytype, |
| 2336 | 2388 | idx: CType.Index, |
| 2337 | 2389 | parent_fix: CTypeFix, |
| ... | ... | @@ -2385,7 +2437,7 @@ fn renderTypeSuffix( |
| 2385 | 2437 | => try renderTypeSuffix( |
| 2386 | 2438 | pass, |
| 2387 | 2439 | store, |
| 2388 | | mod, |
| 2440 | zcu, |
| 2389 | 2441 | w, |
| 2390 | 2442 | cty.cast(CType.Payload.Child).?.data, |
| 2391 | 2443 | .prefix, |
| ... | ... | @@ -2404,7 +2456,7 @@ fn renderTypeSuffix( |
| 2404 | 2456 | try renderTypeSuffix( |
| 2405 | 2457 | pass, |
| 2406 | 2458 | store, |
| 2407 | | mod, |
| 2459 | zcu, |
| 2408 | 2460 | w, |
| 2409 | 2461 | cty.cast(CType.Payload.Sequence).?.data.elem_type, |
| 2410 | 2462 | .suffix, |
| ... | ... | @@ -2444,9 +2496,9 @@ fn renderTypeSuffix( |
| 2444 | 2496 | if (need_comma) try w.writeAll(", "); |
| 2445 | 2497 | need_comma = true; |
| 2446 | 2498 | const trailing = |
| 2447 | | try renderTypePrefix(pass, store, mod, w, param_type, .suffix, qualifiers); |
| 2499 | try renderTypePrefix(pass, store, zcu, w, param_type, .suffix, qualifiers); |
| 2448 | 2500 | if (qualifiers.contains(.@"const")) try w.print("{}a{d}", .{ trailing, param_i }); |
| 2449 | | try renderTypeSuffix(pass, store, mod, w, param_type, .suffix, .{}); |
| 2501 | try renderTypeSuffix(pass, store, zcu, w, param_type, .suffix, .{}); |
| 2450 | 2502 | } |
| 2451 | 2503 | switch (tag) { |
| 2452 | 2504 | .function => {}, |
| ... | ... | @@ -2460,12 +2512,12 @@ fn renderTypeSuffix( |
| 2460 | 2512 | if (!need_comma) try w.writeAll("void"); |
| 2461 | 2513 | try w.writeByte(')'); |
| 2462 | 2514 | |
| 2463 | | try renderTypeSuffix(pass, store, mod, w, data.return_type, .suffix, .{}); |
| 2515 | try renderTypeSuffix(pass, store, zcu, w, data.return_type, .suffix, .{}); |
| 2464 | 2516 | }, |
| 2465 | 2517 | } |
| 2466 | 2518 | } |
| 2467 | 2519 | fn renderAggregateFields( |
| 2468 | | mod: *Module, |
| 2520 | zcu: *Zcu, |
| 2469 | 2521 | writer: anytype, |
| 2470 | 2522 | store: CType.Store.Set, |
| 2471 | 2523 | cty: CType, |
| ... | ... | @@ -2480,9 +2532,9 @@ fn renderAggregateFields( |
| 2480 | 2532 | .eq => {}, |
| 2481 | 2533 | .gt => try writer.print("zig_align({}) ", .{field.alignas.toByteUnits()}), |
| 2482 | 2534 | } |
| 2483 | | const trailing = try renderTypePrefix(.flush, store, mod, writer, field.type, .suffix, .{}); |
| 2535 | const trailing = try renderTypePrefix(.flush, store, zcu, writer, field.type, .suffix, .{}); |
| 2484 | 2536 | try writer.print("{}{ }", .{ trailing, fmtIdent(mem.span(field.name)) }); |
| 2485 | | try renderTypeSuffix(.flush, store, mod, writer, field.type, .suffix, .{}); |
| 2537 | try renderTypeSuffix(.flush, store, zcu, writer, field.type, .suffix, .{}); |
| 2486 | 2538 | try writer.writeAll(";\n"); |
| 2487 | 2539 | } |
| 2488 | 2540 | try writer.writeByteNTimes(' ', indent); |
| ... | ... | @@ -2490,7 +2542,7 @@ fn renderAggregateFields( |
| 2490 | 2542 | } |
| 2491 | 2543 | |
| 2492 | 2544 | pub fn genTypeDecl( |
| 2493 | | mod: *Module, |
| 2545 | zcu: *Zcu, |
| 2494 | 2546 | writer: anytype, |
| 2495 | 2547 | global_store: CType.Store.Set, |
| 2496 | 2548 | global_idx: CType.Index, |
| ... | ... | @@ -2503,9 +2555,9 @@ pub fn genTypeDecl( |
| 2503 | 2555 | switch (global_cty.tag()) { |
| 2504 | 2556 | .fwd_anon_struct => if (pass != .flush) { |
| 2505 | 2557 | try writer.writeAll("typedef "); |
| 2506 | | _ = try renderTypePrefix(.flush, global_store, mod, writer, global_idx, .suffix, .{}); |
| 2558 | _ = try renderTypePrefix(.flush, global_store, zcu, writer, global_idx, .suffix, .{}); |
| 2507 | 2559 | try writer.writeByte(' '); |
| 2508 | | _ = try renderTypePrefix(pass, decl_store, mod, writer, decl_idx, .suffix, .{}); |
| 2560 | _ = try renderTypePrefix(pass, decl_store, zcu, writer, decl_idx, .suffix, .{}); |
| 2509 | 2561 | try writer.writeAll(";\n"); |
| 2510 | 2562 | }, |
| 2511 | 2563 | |
| ... | ... | @@ -2526,14 +2578,14 @@ pub fn genTypeDecl( |
| 2526 | 2578 | _ = try renderTypePrefix( |
| 2527 | 2579 | .flush, |
| 2528 | 2580 | global_store, |
| 2529 | | mod, |
| 2581 | zcu, |
| 2530 | 2582 | writer, |
| 2531 | 2583 | global_idx, |
| 2532 | 2584 | .suffix, |
| 2533 | 2585 | .{}, |
| 2534 | 2586 | ); |
| 2535 | 2587 | try writer.writeAll("; /* "); |
| 2536 | | try mod.declPtr(owner_decl).renderFullyQualifiedName(mod, writer); |
| 2588 | try zcu.declPtr(owner_decl).renderFullyQualifiedName(zcu, writer); |
| 2537 | 2589 | try writer.writeAll(" */\n"); |
| 2538 | 2590 | }, |
| 2539 | 2591 | |
| ... | ... | @@ -2546,14 +2598,14 @@ pub fn genTypeDecl( |
| 2546 | 2598 | => { |
| 2547 | 2599 | const fwd_idx = global_cty.cast(CType.Payload.Aggregate).?.data.fwd_decl; |
| 2548 | 2600 | try renderTypeName( |
| 2549 | | mod, |
| 2601 | zcu, |
| 2550 | 2602 | writer, |
| 2551 | 2603 | fwd_idx, |
| 2552 | 2604 | global_store.indexToCType(fwd_idx), |
| 2553 | 2605 | if (global_cty.isPacked()) "zig_packed(" else "", |
| 2554 | 2606 | ); |
| 2555 | 2607 | try writer.writeByte(' '); |
| 2556 | | try renderAggregateFields(mod, writer, global_store, global_cty, 0); |
| 2608 | try renderAggregateFields(zcu, writer, global_store, global_cty, 0); |
| 2557 | 2609 | if (global_cty.isPacked()) try writer.writeByte(')'); |
| 2558 | 2610 | try writer.writeAll(";\n"); |
| 2559 | 2611 | }, |
| ... | ... | @@ -2566,30 +2618,30 @@ pub fn genTypeDecl( |
| 2566 | 2618 | } |
| 2567 | 2619 | } |
| 2568 | 2620 | |
| 2569 | | pub fn genGlobalAsm(mod: *Module, writer: anytype) !void { |
| 2570 | | for (mod.global_assembly.values()) |asm_source| { |
| 2621 | pub fn genGlobalAsm(zcu: *Zcu, writer: anytype) !void { |
| 2622 | for (zcu.global_assembly.values()) |asm_source| { |
| 2571 | 2623 | try writer.print("__asm({s});\n", .{fmtStringLiteral(asm_source, null)}); |
| 2572 | 2624 | } |
| 2573 | 2625 | } |
| 2574 | 2626 | |
| 2575 | 2627 | pub fn genErrDecls(o: *Object) !void { |
| 2576 | | const mod = o.dg.module; |
| 2577 | | const ip = &mod.intern_pool; |
| 2628 | const zcu = o.dg.zcu; |
| 2629 | const ip = &zcu.intern_pool; |
| 2578 | 2630 | const writer = o.writer(); |
| 2579 | 2631 | |
| 2580 | 2632 | var max_name_len: usize = 0; |
| 2581 | 2633 | // do not generate an invalid empty enum when the global error set is empty |
| 2582 | | if (mod.global_error_set.keys().len > 1) { |
| 2634 | if (zcu.global_error_set.keys().len > 1) { |
| 2583 | 2635 | try writer.writeAll("enum {\n"); |
| 2584 | 2636 | o.indent_writer.pushIndent(); |
| 2585 | | for (mod.global_error_set.keys()[1..], 1..) |name_nts, value| { |
| 2637 | for (zcu.global_error_set.keys()[1..], 1..) |name_nts, value| { |
| 2586 | 2638 | const name = ip.stringToSlice(name_nts); |
| 2587 | 2639 | max_name_len = @max(name.len, max_name_len); |
| 2588 | | const err_val = try mod.intern(.{ .err = .{ |
| 2640 | const err_val = try zcu.intern(.{ .err = .{ |
| 2589 | 2641 | .ty = .anyerror_type, |
| 2590 | 2642 | .name = name_nts, |
| 2591 | 2643 | } }); |
| 2592 | | try o.dg.renderValue(writer, Type.anyerror, Value.fromInterned(err_val), .Other); |
| 2644 | try o.dg.renderValue(writer, Value.fromInterned(err_val), .Other); |
| 2593 | 2645 | try writer.print(" = {d}u,\n", .{value}); |
| 2594 | 2646 | } |
| 2595 | 2647 | o.indent_writer.popIndent(); |
| ... | ... | @@ -2601,44 +2653,56 @@ pub fn genErrDecls(o: *Object) !void { |
| 2601 | 2653 | defer o.dg.gpa.free(name_buf); |
| 2602 | 2654 | |
| 2603 | 2655 | @memcpy(name_buf[0..name_prefix.len], name_prefix); |
| 2604 | | for (mod.global_error_set.keys()) |name_ip| { |
| 2656 | for (zcu.global_error_set.keys()) |name_ip| { |
| 2605 | 2657 | const name = ip.stringToSlice(name_ip); |
| 2606 | 2658 | @memcpy(name_buf[name_prefix.len..][0..name.len], name); |
| 2607 | 2659 | const identifier = name_buf[0 .. name_prefix.len + name.len]; |
| 2608 | 2660 | |
| 2609 | | const name_ty = try mod.arrayType(.{ |
| 2661 | const name_ty = try zcu.arrayType(.{ |
| 2610 | 2662 | .len = name.len, |
| 2611 | 2663 | .child = .u8_type, |
| 2612 | 2664 | .sentinel = .zero_u8, |
| 2613 | 2665 | }); |
| 2614 | | const name_val = try mod.intern(.{ .aggregate = .{ |
| 2666 | const name_val = try zcu.intern(.{ .aggregate = .{ |
| 2615 | 2667 | .ty = name_ty.toIntern(), |
| 2616 | 2668 | .storage = .{ .bytes = name }, |
| 2617 | 2669 | } }); |
| 2618 | 2670 | |
| 2619 | 2671 | try writer.writeAll("static "); |
| 2620 | | try o.dg.renderTypeAndName(writer, name_ty, .{ .identifier = identifier }, Const, .none, .complete); |
| 2672 | try o.dg.renderTypeAndName( |
| 2673 | writer, |
| 2674 | name_ty, |
| 2675 | .{ .identifier = identifier }, |
| 2676 | Const, |
| 2677 | .none, |
| 2678 | .complete, |
| 2679 | ); |
| 2621 | 2680 | try writer.writeAll(" = "); |
| 2622 | | try o.dg.renderValue(writer, name_ty, Value.fromInterned(name_val), .StaticInitializer); |
| 2681 | try o.dg.renderValue(writer, Value.fromInterned(name_val), .StaticInitializer); |
| 2623 | 2682 | try writer.writeAll(";\n"); |
| 2624 | 2683 | } |
| 2625 | 2684 | |
| 2626 | | const name_array_ty = try mod.arrayType(.{ |
| 2627 | | .len = mod.global_error_set.count(), |
| 2685 | const name_array_ty = try zcu.arrayType(.{ |
| 2686 | .len = zcu.global_error_set.count(), |
| 2628 | 2687 | .child = .slice_const_u8_sentinel_0_type, |
| 2629 | 2688 | }); |
| 2630 | 2689 | |
| 2631 | 2690 | try writer.writeAll("static "); |
| 2632 | | try o.dg.renderTypeAndName(writer, name_array_ty, .{ .identifier = array_identifier }, Const, .none, .complete); |
| 2691 | try o.dg.renderTypeAndName( |
| 2692 | writer, |
| 2693 | name_array_ty, |
| 2694 | .{ .identifier = array_identifier }, |
| 2695 | Const, |
| 2696 | .none, |
| 2697 | .complete, |
| 2698 | ); |
| 2633 | 2699 | try writer.writeAll(" = {"); |
| 2634 | | for (mod.global_error_set.keys(), 0..) |name_nts, value| { |
| 2700 | for (zcu.global_error_set.keys(), 0..) |name_nts, value| { |
| 2635 | 2701 | const name = ip.stringToSlice(name_nts); |
| 2636 | 2702 | if (value != 0) try writer.writeByte(','); |
| 2637 | | |
| 2638 | | const len_val = try mod.intValue(Type.usize, name.len); |
| 2639 | | |
| 2640 | 2703 | try writer.print("{{" ++ name_prefix ++ "{}, {}}}", .{ |
| 2641 | | fmtIdent(name), try o.dg.fmtIntLiteral(Type.usize, len_val, .StaticInitializer), |
| 2704 | fmtIdent(name), |
| 2705 | try o.dg.fmtIntLiteral(try zcu.intValue(Type.usize, name.len), .StaticInitializer), |
| 2642 | 2706 | }); |
| 2643 | 2707 | } |
| 2644 | 2708 | try writer.writeAll("};\n"); |
| ... | ... | @@ -2648,16 +2712,16 @@ fn genExports(o: *Object) !void { |
| 2648 | 2712 | const tracy = trace(@src()); |
| 2649 | 2713 | defer tracy.end(); |
| 2650 | 2714 | |
| 2651 | | const mod = o.dg.module; |
| 2652 | | const ip = &mod.intern_pool; |
| 2715 | const zcu = o.dg.zcu; |
| 2716 | const ip = &zcu.intern_pool; |
| 2653 | 2717 | const decl_index = switch (o.dg.pass) { |
| 2654 | 2718 | .decl => |decl| decl, |
| 2655 | 2719 | .anon, .flush => return, |
| 2656 | 2720 | }; |
| 2657 | | const decl = mod.declPtr(decl_index); |
| 2721 | const decl = zcu.declPtr(decl_index); |
| 2658 | 2722 | const fwd = o.dg.fwdDeclWriter(); |
| 2659 | 2723 | |
| 2660 | | const exports = mod.decl_exports.get(decl_index) orelse return; |
| 2724 | const exports = zcu.decl_exports.get(decl_index) orelse return; |
| 2661 | 2725 | if (exports.items.len < 2) return; |
| 2662 | 2726 | |
| 2663 | 2727 | const is_variable_const = switch (ip.indexToKey(decl.val.toIntern())) { |
| ... | ... | @@ -2685,7 +2749,7 @@ fn genExports(o: *Object) !void { |
| 2685 | 2749 | const export_name = ip.stringToSlice(@"export".opts.name); |
| 2686 | 2750 | try o.dg.renderTypeAndName( |
| 2687 | 2751 | fwd, |
| 2688 | | decl.typeOf(mod), |
| 2752 | decl.typeOf(zcu), |
| 2689 | 2753 | .{ .identifier = export_name }, |
| 2690 | 2754 | CQualifiers.init(.{ .@"const" = is_variable_const }), |
| 2691 | 2755 | decl.alignment, |
| ... | ... | @@ -2708,8 +2772,8 @@ fn genExports(o: *Object) !void { |
| 2708 | 2772 | } |
| 2709 | 2773 | |
| 2710 | 2774 | pub fn genLazyFn(o: *Object, lazy_fn: LazyFnMap.Entry) !void { |
| 2711 | | const mod = o.dg.module; |
| 2712 | | const ip = &mod.intern_pool; |
| 2775 | const zcu = o.dg.zcu; |
| 2776 | const ip = &zcu.intern_pool; |
| 2713 | 2777 | const w = o.writer(); |
| 2714 | 2778 | const key = lazy_fn.key_ptr.*; |
| 2715 | 2779 | const val = lazy_fn.value_ptr; |
| ... | ... | @@ -2727,47 +2791,45 @@ pub fn genLazyFn(o: *Object, lazy_fn: LazyFnMap.Entry) !void { |
| 2727 | 2791 | try w.writeByte('('); |
| 2728 | 2792 | try o.dg.renderTypeAndName(w, enum_ty, .{ .identifier = "tag" }, Const, .none, .complete); |
| 2729 | 2793 | try w.writeAll(") {\n switch (tag) {\n"); |
| 2730 | | const tag_names = enum_ty.enumFields(mod); |
| 2794 | const tag_names = enum_ty.enumFields(zcu); |
| 2731 | 2795 | for (0..tag_names.len) |tag_index| { |
| 2732 | 2796 | const tag_name = ip.stringToSlice(tag_names.get(ip)[tag_index]); |
| 2733 | | const tag_val = try mod.enumValueFieldIndex(enum_ty, @intCast(tag_index)); |
| 2797 | const tag_val = try zcu.enumValueFieldIndex(enum_ty, @intCast(tag_index)); |
| 2734 | 2798 | |
| 2735 | | const int_val = try tag_val.intFromEnum(enum_ty, mod); |
| 2736 | | |
| 2737 | | const name_ty = try mod.arrayType(.{ |
| 2799 | const name_ty = try zcu.arrayType(.{ |
| 2738 | 2800 | .len = tag_name.len, |
| 2739 | 2801 | .child = .u8_type, |
| 2740 | 2802 | .sentinel = .zero_u8, |
| 2741 | 2803 | }); |
| 2742 | | const name_val = try mod.intern(.{ .aggregate = .{ |
| 2804 | const name_val = try zcu.intern(.{ .aggregate = .{ |
| 2743 | 2805 | .ty = name_ty.toIntern(), |
| 2744 | 2806 | .storage = .{ .bytes = tag_name }, |
| 2745 | 2807 | } }); |
| 2746 | | const len_val = try mod.intValue(Type.usize, tag_name.len); |
| 2747 | 2808 | |
| 2748 | 2809 | try w.print(" case {}: {{\n static ", .{ |
| 2749 | | try o.dg.fmtIntLiteral(enum_ty, int_val, .Other), |
| 2810 | try o.dg.fmtIntLiteral(try tag_val.intFromEnum(enum_ty, zcu), .Other), |
| 2750 | 2811 | }); |
| 2751 | 2812 | try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete); |
| 2752 | 2813 | try w.writeAll(" = "); |
| 2753 | | try o.dg.renderValue(w, name_ty, Value.fromInterned(name_val), .Initializer); |
| 2814 | try o.dg.renderValue(w, Value.fromInterned(name_val), .Initializer); |
| 2754 | 2815 | try w.writeAll(";\n return ("); |
| 2755 | 2816 | try o.dg.renderType(w, name_slice_ty); |
| 2756 | 2817 | try w.print("){{{}, {}}};\n", .{ |
| 2757 | | fmtIdent("name"), try o.dg.fmtIntLiteral(Type.usize, len_val, .Other), |
| 2818 | fmtIdent("name"), |
| 2819 | try o.dg.fmtIntLiteral(try zcu.intValue(Type.usize, tag_name.len), .Other), |
| 2758 | 2820 | }); |
| 2759 | 2821 | |
| 2760 | 2822 | try w.writeAll(" }\n"); |
| 2761 | 2823 | } |
| 2762 | 2824 | try w.writeAll(" }\n while ("); |
| 2763 | | try o.dg.renderValue(w, Type.bool, Value.true, .Other); |
| 2825 | try o.dg.renderValue(w, Value.true, .Other); |
| 2764 | 2826 | try w.writeAll(") "); |
| 2765 | 2827 | _ = try airBreakpoint(w); |
| 2766 | 2828 | try w.writeAll("}\n"); |
| 2767 | 2829 | }, |
| 2768 | 2830 | .never_tail, .never_inline => |fn_decl_index| { |
| 2769 | | const fn_decl = mod.declPtr(fn_decl_index); |
| 2770 | | const fn_cty = try o.dg.typeToCType(fn_decl.typeOf(mod), .complete); |
| 2831 | const fn_decl = zcu.declPtr(fn_decl_index); |
| 2832 | const fn_cty = try o.dg.typeToCType(fn_decl.typeOf(zcu), .complete); |
| 2771 | 2833 | const fn_info = fn_cty.cast(CType.Payload.Function).?.data; |
| 2772 | 2834 | |
| 2773 | 2835 | const fwd_decl_writer = o.dg.fwdDeclWriter(); |
| ... | ... | @@ -2799,10 +2861,10 @@ pub fn genFunc(f: *Function) !void { |
| 2799 | 2861 | defer tracy.end(); |
| 2800 | 2862 | |
| 2801 | 2863 | const o = &f.object; |
| 2802 | | const mod = o.dg.module; |
| 2864 | const zcu = o.dg.zcu; |
| 2803 | 2865 | const gpa = o.dg.gpa; |
| 2804 | 2866 | const decl_index = o.dg.pass.decl; |
| 2805 | | const decl = mod.declPtr(decl_index); |
| 2867 | const decl = zcu.declPtr(decl_index); |
| 2806 | 2868 | |
| 2807 | 2869 | o.code_header = std.ArrayList(u8).init(gpa); |
| 2808 | 2870 | defer o.code_header.deinit(); |
| ... | ... | @@ -2811,7 +2873,7 @@ pub fn genFunc(f: *Function) !void { |
| 2811 | 2873 | const fwd_decl_writer = o.dg.fwdDeclWriter(); |
| 2812 | 2874 | try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static "); |
| 2813 | 2875 | |
| 2814 | | if (mod.decl_exports.get(decl_index)) |exports| |
| 2876 | if (zcu.decl_exports.get(decl_index)) |exports| |
| 2815 | 2877 | if (exports.items[0].opts.linkage == .weak) try fwd_decl_writer.writeAll("zig_weak_linkage_fn "); |
| 2816 | 2878 | try o.dg.renderFunctionSignature(fwd_decl_writer, decl_index, .forward, .{ .export_index = 0 }); |
| 2817 | 2879 | try fwd_decl_writer.writeAll(";\n"); |
| ... | ... | @@ -2819,6 +2881,8 @@ pub fn genFunc(f: *Function) !void { |
| 2819 | 2881 | |
| 2820 | 2882 | try o.indent_writer.insertNewline(); |
| 2821 | 2883 | if (!is_global) try o.writer().writeAll("static "); |
| 2884 | if (zcu.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s| |
| 2885 | try o.writer().print("zig_linksection_fn({s}) ", .{fmtStringLiteral(s, null)}); |
| 2822 | 2886 | try o.dg.renderFunctionSignature(o.writer(), decl_index, .complete, .{ .export_index = 0 }); |
| 2823 | 2887 | try o.writer().writeByte(' '); |
| 2824 | 2888 | |
| ... | ... | @@ -2867,7 +2931,7 @@ pub fn genFunc(f: *Function) !void { |
| 2867 | 2931 | for (free_locals.values()) |list| { |
| 2868 | 2932 | for (list.keys()) |local_index| { |
| 2869 | 2933 | const local = f.locals.items[local_index]; |
| 2870 | | try o.dg.renderCTypeAndName(w, local.cty_idx, .{ .local = local_index }, .{}, local.alignas); |
| 2934 | try o.dg.renderCTypeAndName(w, local.cty_idx, .{ .local = local_index }, .{}, local.flags.alignas); |
| 2871 | 2935 | try w.writeAll(";\n "); |
| 2872 | 2936 | } |
| 2873 | 2937 | } |
| ... | ... | @@ -2884,43 +2948,41 @@ pub fn genDecl(o: *Object) !void { |
| 2884 | 2948 | const tracy = trace(@src()); |
| 2885 | 2949 | defer tracy.end(); |
| 2886 | 2950 | |
| 2887 | | const mod = o.dg.module; |
| 2951 | const zcu = o.dg.zcu; |
| 2888 | 2952 | const decl_index = o.dg.pass.decl; |
| 2889 | | const decl = mod.declPtr(decl_index); |
| 2890 | | const decl_val = decl.val; |
| 2891 | | const decl_ty = decl_val.typeOf(mod); |
| 2953 | const decl = zcu.declPtr(decl_index); |
| 2954 | const decl_ty = decl.typeOf(zcu); |
| 2892 | 2955 | |
| 2893 | | if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return; |
| 2894 | | if (decl_val.getExternFunc(mod)) |_| { |
| 2956 | if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) return; |
| 2957 | if (decl.val.getExternFunc(zcu)) |_| { |
| 2895 | 2958 | const fwd_decl_writer = o.dg.fwdDeclWriter(); |
| 2896 | 2959 | try fwd_decl_writer.writeAll("zig_extern "); |
| 2897 | 2960 | try o.dg.renderFunctionSignature(fwd_decl_writer, decl_index, .forward, .{ .export_index = 0 }); |
| 2898 | 2961 | try fwd_decl_writer.writeAll(";\n"); |
| 2899 | 2962 | try genExports(o); |
| 2900 | | } else if (decl_val.getVariable(mod)) |variable| { |
| 2963 | } else if (decl.val.getVariable(zcu)) |variable| { |
| 2901 | 2964 | try o.dg.renderFwdDecl(decl_index, variable, .final); |
| 2902 | 2965 | try genExports(o); |
| 2903 | 2966 | |
| 2904 | 2967 | if (variable.is_extern) return; |
| 2905 | 2968 | |
| 2906 | | const is_global = variable.is_extern or o.dg.declIsGlobal(decl_val); |
| 2969 | const is_global = variable.is_extern or o.dg.declIsGlobal(decl.val); |
| 2907 | 2970 | const w = o.writer(); |
| 2908 | 2971 | if (!is_global) try w.writeAll("static "); |
| 2909 | 2972 | if (variable.is_weak_linkage) try w.writeAll("zig_weak_linkage "); |
| 2910 | 2973 | if (variable.is_threadlocal) try w.writeAll("zig_threadlocal "); |
| 2911 | | if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s| |
| 2912 | | try w.print("zig_linksection(\"{s}\", ", .{s}); |
| 2974 | if (zcu.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s| |
| 2975 | try w.print("zig_linksection({s}) ", .{fmtStringLiteral(s, null)}); |
| 2913 | 2976 | const decl_c_value = .{ .decl = decl_index }; |
| 2914 | 2977 | try o.dg.renderTypeAndName(w, decl_ty, decl_c_value, .{}, decl.alignment, .complete); |
| 2915 | | if (decl.@"linksection" != .none) try w.writeAll(", read, write)"); |
| 2916 | 2978 | try w.writeAll(" = "); |
| 2917 | | try o.dg.renderValue(w, decl_ty, Value.fromInterned(variable.init), .StaticInitializer); |
| 2979 | try o.dg.renderValue(w, Value.fromInterned(variable.init), .StaticInitializer); |
| 2918 | 2980 | try w.writeByte(';'); |
| 2919 | 2981 | try o.indent_writer.insertNewline(); |
| 2920 | 2982 | } else { |
| 2921 | | const is_global = o.dg.module.decl_exports.contains(decl_index); |
| 2983 | const is_global = o.dg.zcu.decl_exports.contains(decl_index); |
| 2922 | 2984 | const decl_c_value = .{ .decl = decl_index }; |
| 2923 | | try genDeclValue(o, decl_val, is_global, decl_c_value, decl.alignment, decl.@"linksection"); |
| 2985 | try genDeclValue(o, decl.val, is_global, decl_c_value, decl.alignment, decl.@"linksection"); |
| 2924 | 2986 | } |
| 2925 | 2987 | } |
| 2926 | 2988 | |
| ... | ... | @@ -2930,19 +2992,19 @@ pub fn genDeclValue( |
| 2930 | 2992 | is_global: bool, |
| 2931 | 2993 | decl_c_value: CValue, |
| 2932 | 2994 | alignment: Alignment, |
| 2933 | | link_section: InternPool.OptionalNullTerminatedString, |
| 2995 | @"linksection": InternPool.OptionalNullTerminatedString, |
| 2934 | 2996 | ) !void { |
| 2935 | | const mod = o.dg.module; |
| 2997 | const zcu = o.dg.zcu; |
| 2936 | 2998 | const fwd_decl_writer = o.dg.fwdDeclWriter(); |
| 2937 | 2999 | |
| 2938 | | const ty = val.typeOf(mod); |
| 3000 | const ty = val.typeOf(zcu); |
| 2939 | 3001 | |
| 2940 | 3002 | try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static "); |
| 2941 | 3003 | try o.dg.renderTypeAndName(fwd_decl_writer, ty, decl_c_value, Const, alignment, .complete); |
| 2942 | 3004 | switch (o.dg.pass) { |
| 2943 | 3005 | .decl => |decl_index| { |
| 2944 | | if (mod.decl_exports.get(decl_index)) |exports| { |
| 2945 | | const export_name = mod.intern_pool.stringToSlice(exports.items[0].opts.name); |
| 3006 | if (zcu.decl_exports.get(decl_index)) |exports| { |
| 3007 | const export_name = zcu.intern_pool.stringToSlice(exports.items[0].opts.name); |
| 2946 | 3008 | if (isMangledIdent(export_name, true)) { |
| 2947 | 3009 | try fwd_decl_writer.print(" zig_mangled_final({ }, {s})", .{ |
| 2948 | 3010 | fmtIdent(export_name), fmtStringLiteral(export_name, null), |
| ... | ... | @@ -2958,13 +3020,11 @@ pub fn genDeclValue( |
| 2958 | 3020 | |
| 2959 | 3021 | const w = o.writer(); |
| 2960 | 3022 | if (!is_global) try w.writeAll("static "); |
| 2961 | | |
| 2962 | | if (mod.intern_pool.stringToSliceUnwrap(link_section)) |s| |
| 2963 | | try w.print("zig_linksection(\"{s}\", ", .{s}); |
| 3023 | if (zcu.intern_pool.stringToSliceUnwrap(@"linksection")) |s| |
| 3024 | try w.print("zig_linksection({s}) ", .{fmtStringLiteral(s, null)}); |
| 2964 | 3025 | try o.dg.renderTypeAndName(w, ty, decl_c_value, Const, alignment, .complete); |
| 2965 | | if (link_section != .none) try w.writeAll(", read)"); |
| 2966 | 3026 | try w.writeAll(" = "); |
| 2967 | | try o.dg.renderValue(w, ty, val, .StaticInitializer); |
| 3027 | try o.dg.renderValue(w, val, .StaticInitializer); |
| 2968 | 3028 | try w.writeAll(";\n"); |
| 2969 | 3029 | } |
| 2970 | 3030 | |
| ... | ... | @@ -2972,12 +3032,12 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void { |
| 2972 | 3032 | const tracy = trace(@src()); |
| 2973 | 3033 | defer tracy.end(); |
| 2974 | 3034 | |
| 2975 | | const mod = dg.module; |
| 3035 | const zcu = dg.zcu; |
| 2976 | 3036 | const decl_index = dg.pass.decl; |
| 2977 | | const decl = mod.declPtr(decl_index); |
| 3037 | const decl = zcu.declPtr(decl_index); |
| 2978 | 3038 | const writer = dg.fwdDeclWriter(); |
| 2979 | 3039 | |
| 2980 | | switch (decl.val.typeOf(mod).zigTypeTag(mod)) { |
| 3040 | switch (decl.typeOf(zcu).zigTypeTag(zcu)) { |
| 2981 | 3041 | .Fn => if (dg.declIsGlobal(decl.val)) { |
| 2982 | 3042 | try writer.writeAll("zig_extern "); |
| 2983 | 3043 | try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 }); |
| ... | ... | @@ -3060,8 +3120,8 @@ fn genBodyResolveState(f: *Function, inst: Air.Inst.Index, leading_deaths: []con |
| 3060 | 3120 | } |
| 3061 | 3121 | |
| 3062 | 3122 | fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfMemory }!void { |
| 3063 | | const mod = f.object.dg.module; |
| 3064 | | const ip = &mod.intern_pool; |
| 3123 | const zcu = f.object.dg.zcu; |
| 3124 | const ip = &zcu.intern_pool; |
| 3065 | 3125 | const air_tags = f.air.instructions.items(.tag); |
| 3066 | 3126 | |
| 3067 | 3127 | for (body) |inst| { |
| ... | ... | @@ -3096,10 +3156,10 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, |
| 3096 | 3156 | .div_trunc, .div_exact => try airBinOp(f, inst, "/", "div_trunc", .none), |
| 3097 | 3157 | .rem => blk: { |
| 3098 | 3158 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3099 | | const lhs_scalar_ty = f.typeOf(bin_op.lhs).scalarType(mod); |
| 3159 | const lhs_scalar_ty = f.typeOf(bin_op.lhs).scalarType(zcu); |
| 3100 | 3160 | // For binary operations @TypeOf(lhs)==@TypeOf(rhs), |
| 3101 | 3161 | // so we only check one. |
| 3102 | | break :blk if (lhs_scalar_ty.isInt(mod)) |
| 3162 | break :blk if (lhs_scalar_ty.isInt(zcu)) |
| 3103 | 3163 | try airBinOp(f, inst, "%", "rem", .none) |
| 3104 | 3164 | else |
| 3105 | 3165 | try airBinFloatOp(f, inst, "fmod"); |
| ... | ... | @@ -3359,10 +3419,10 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [ |
| 3359 | 3419 | } |
| 3360 | 3420 | |
| 3361 | 3421 | fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3362 | | const mod = f.object.dg.module; |
| 3422 | const zcu = f.object.dg.zcu; |
| 3363 | 3423 | const inst_ty = f.typeOfIndex(inst); |
| 3364 | 3424 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3365 | | if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 3425 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 3366 | 3426 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3367 | 3427 | return .none; |
| 3368 | 3428 | } |
| ... | ... | @@ -3385,14 +3445,17 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3385 | 3445 | } |
| 3386 | 3446 | |
| 3387 | 3447 | fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3388 | | const mod = f.object.dg.module; |
| 3448 | const zcu = f.object.dg.zcu; |
| 3389 | 3449 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3390 | 3450 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3391 | 3451 | |
| 3392 | 3452 | const inst_ty = f.typeOfIndex(inst); |
| 3393 | 3453 | const ptr_ty = f.typeOf(bin_op.lhs); |
| 3394 | | const elem_ty = ptr_ty.childType(mod); |
| 3395 | | const elem_has_bits = elem_ty.hasRuntimeBitsIgnoreComptime(mod); |
| 3454 | const ptr_align = ptr_ty.ptrAlignment(zcu); |
| 3455 | const elem_ty = ptr_ty.elemType2(zcu); |
| 3456 | const elem_align = elem_ty.abiAlignment(zcu); |
| 3457 | const is_under_aligned = ptr_align.compareStrict(.lt, elem_align); |
| 3458 | const elem_has_bits = elem_ty.hasRuntimeBitsIgnoreComptime(zcu); |
| 3396 | 3459 | |
| 3397 | 3460 | const ptr = try f.resolveInst(bin_op.lhs); |
| 3398 | 3461 | const index = try f.resolveInst(bin_op.rhs); |
| ... | ... | @@ -3407,13 +3470,22 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3407 | 3470 | try f.renderType(writer, inst_ty); |
| 3408 | 3471 | try writer.writeByte(')'); |
| 3409 | 3472 | if (elem_has_bits) try writer.writeByte('&'); |
| 3410 | | if (elem_has_bits and ptr_ty.ptrSize(mod) == .One) { |
| 3473 | if (elem_has_bits and ptr_ty.ptrSize(zcu) == .One and !is_under_aligned) { |
| 3411 | 3474 | // It's a pointer to an array, so we need to de-reference. |
| 3412 | 3475 | try f.writeCValueDeref(writer, ptr); |
| 3413 | 3476 | } else try f.writeCValue(writer, ptr, .Other); |
| 3414 | 3477 | if (elem_has_bits) { |
| 3415 | 3478 | try writer.writeByte('['); |
| 3416 | 3479 | try f.writeCValue(writer, index, .Other); |
| 3480 | if (is_under_aligned) { |
| 3481 | const factor = @divExact(elem_align.toByteUnitsOptional().?, @min( |
| 3482 | ptr_align.toByteUnitsOptional().?, |
| 3483 | f.object.dg.mod.resolved_target.result.maxIntAlignment(), |
| 3484 | )); |
| 3485 | try writer.print(" * {}", .{ |
| 3486 | try f.fmtIntLiteral(try zcu.intValue(Type.usize, factor)), |
| 3487 | }); |
| 3488 | } |
| 3417 | 3489 | try writer.writeByte(']'); |
| 3418 | 3490 | } |
| 3419 | 3491 | try a.end(f, writer); |
| ... | ... | @@ -3421,10 +3493,10 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3421 | 3493 | } |
| 3422 | 3494 | |
| 3423 | 3495 | fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3424 | | const mod = f.object.dg.module; |
| 3496 | const zcu = f.object.dg.zcu; |
| 3425 | 3497 | const inst_ty = f.typeOfIndex(inst); |
| 3426 | 3498 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3427 | | if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 3499 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 3428 | 3500 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3429 | 3501 | return .none; |
| 3430 | 3502 | } |
| ... | ... | @@ -3447,14 +3519,14 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3447 | 3519 | } |
| 3448 | 3520 | |
| 3449 | 3521 | fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3450 | | const mod = f.object.dg.module; |
| 3522 | const zcu = f.object.dg.zcu; |
| 3451 | 3523 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3452 | 3524 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3453 | 3525 | |
| 3454 | 3526 | const inst_ty = f.typeOfIndex(inst); |
| 3455 | 3527 | const slice_ty = f.typeOf(bin_op.lhs); |
| 3456 | | const elem_ty = slice_ty.elemType2(mod); |
| 3457 | | const elem_has_bits = elem_ty.hasRuntimeBitsIgnoreComptime(mod); |
| 3528 | const elem_ty = slice_ty.elemType2(zcu); |
| 3529 | const elem_has_bits = elem_ty.hasRuntimeBitsIgnoreComptime(zcu); |
| 3458 | 3530 | |
| 3459 | 3531 | const slice = try f.resolveInst(bin_op.lhs); |
| 3460 | 3532 | const index = try f.resolveInst(bin_op.rhs); |
| ... | ... | @@ -3477,10 +3549,10 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3477 | 3549 | } |
| 3478 | 3550 | |
| 3479 | 3551 | fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3480 | | const mod = f.object.dg.module; |
| 3552 | const zcu = f.object.dg.zcu; |
| 3481 | 3553 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3482 | 3554 | const inst_ty = f.typeOfIndex(inst); |
| 3483 | | if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 3555 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 3484 | 3556 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3485 | 3557 | return .none; |
| 3486 | 3558 | } |
| ... | ... | @@ -3503,33 +3575,33 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3503 | 3575 | } |
| 3504 | 3576 | |
| 3505 | 3577 | fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3506 | | const mod = f.object.dg.module; |
| 3578 | const zcu = f.object.dg.zcu; |
| 3507 | 3579 | const inst_ty = f.typeOfIndex(inst); |
| 3508 | | const elem_type = inst_ty.childType(mod); |
| 3509 | | if (!elem_type.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return .{ .undef = inst_ty }; |
| 3580 | const elem_type = inst_ty.childType(zcu); |
| 3581 | if (!elem_type.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) return .{ .undef = inst_ty }; |
| 3510 | 3582 | |
| 3511 | 3583 | const local = try f.allocLocalValue( |
| 3512 | 3584 | elem_type, |
| 3513 | | inst_ty.ptrAlignment(mod), |
| 3585 | inst_ty.ptrAlignment(zcu), |
| 3514 | 3586 | ); |
| 3515 | 3587 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.new_local }); |
| 3516 | | const gpa = f.object.dg.module.gpa; |
| 3588 | const gpa = f.object.dg.zcu.gpa; |
| 3517 | 3589 | try f.allocs.put(gpa, local.new_local, true); |
| 3518 | 3590 | return .{ .local_ref = local.new_local }; |
| 3519 | 3591 | } |
| 3520 | 3592 | |
| 3521 | 3593 | fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3522 | | const mod = f.object.dg.module; |
| 3594 | const zcu = f.object.dg.zcu; |
| 3523 | 3595 | const inst_ty = f.typeOfIndex(inst); |
| 3524 | | const elem_ty = inst_ty.childType(mod); |
| 3525 | | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return .{ .undef = inst_ty }; |
| 3596 | const elem_ty = inst_ty.childType(zcu); |
| 3597 | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) return .{ .undef = inst_ty }; |
| 3526 | 3598 | |
| 3527 | 3599 | const local = try f.allocLocalValue( |
| 3528 | 3600 | elem_ty, |
| 3529 | | inst_ty.ptrAlignment(mod), |
| 3601 | inst_ty.ptrAlignment(zcu), |
| 3530 | 3602 | ); |
| 3531 | 3603 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.new_local }); |
| 3532 | | const gpa = f.object.dg.module.gpa; |
| 3604 | const gpa = f.object.dg.zcu.gpa; |
| 3533 | 3605 | try f.allocs.put(gpa, local.new_local, true); |
| 3534 | 3606 | return .{ .local_ref = local.new_local }; |
| 3535 | 3607 | } |
| ... | ... | @@ -3559,15 +3631,15 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3559 | 3631 | } |
| 3560 | 3632 | |
| 3561 | 3633 | fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3562 | | const mod = f.object.dg.module; |
| 3634 | const zcu = f.object.dg.zcu; |
| 3563 | 3635 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3564 | 3636 | |
| 3565 | 3637 | const ptr_ty = f.typeOf(ty_op.operand); |
| 3566 | | const ptr_scalar_ty = ptr_ty.scalarType(mod); |
| 3567 | | const ptr_info = ptr_scalar_ty.ptrInfo(mod); |
| 3638 | const ptr_scalar_ty = ptr_ty.scalarType(zcu); |
| 3639 | const ptr_info = ptr_scalar_ty.ptrInfo(zcu); |
| 3568 | 3640 | const src_ty = Type.fromInterned(ptr_info.child); |
| 3569 | 3641 | |
| 3570 | | if (!src_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 3642 | if (!src_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 3571 | 3643 | try reap(f, inst, &.{ty_op.operand}); |
| 3572 | 3644 | return .none; |
| 3573 | 3645 | } |
| ... | ... | @@ -3577,10 +3649,10 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3577 | 3649 | try reap(f, inst, &.{ty_op.operand}); |
| 3578 | 3650 | |
| 3579 | 3651 | const is_aligned = if (ptr_info.flags.alignment != .none) |
| 3580 | | ptr_info.flags.alignment.compare(.gte, src_ty.abiAlignment(mod)) |
| 3652 | ptr_info.flags.alignment.compare(.gte, src_ty.abiAlignment(zcu)) |
| 3581 | 3653 | else |
| 3582 | 3654 | true; |
| 3583 | | const is_array = lowersToArray(src_ty, mod); |
| 3655 | const is_array = lowersToArray(src_ty, zcu); |
| 3584 | 3656 | const need_memcpy = !is_aligned or is_array; |
| 3585 | 3657 | |
| 3586 | 3658 | const writer = f.object.writer(); |
| ... | ... | @@ -3600,12 +3672,12 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3600 | 3672 | try writer.writeAll("))"); |
| 3601 | 3673 | } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) { |
| 3602 | 3674 | const host_bits: u16 = ptr_info.packed_offset.host_size * 8; |
| 3603 | | const host_ty = try mod.intType(.unsigned, host_bits); |
| 3675 | const host_ty = try zcu.intType(.unsigned, host_bits); |
| 3604 | 3676 | |
| 3605 | | const bit_offset_ty = try mod.intType(.unsigned, Type.smallestUnsignedBits(host_bits - 1)); |
| 3606 | | const bit_offset_val = try mod.intValue(bit_offset_ty, ptr_info.packed_offset.bit_offset); |
| 3677 | const bit_offset_ty = try zcu.intType(.unsigned, Type.smallestUnsignedBits(host_bits - 1)); |
| 3678 | const bit_offset_val = try zcu.intValue(bit_offset_ty, ptr_info.packed_offset.bit_offset); |
| 3607 | 3679 | |
| 3608 | | const field_ty = try mod.intType(.unsigned, @as(u16, @intCast(src_ty.bitSize(mod)))); |
| 3680 | const field_ty = try zcu.intType(.unsigned, @as(u16, @intCast(src_ty.bitSize(zcu)))); |
| 3609 | 3681 | |
| 3610 | 3682 | try f.writeCValue(writer, local, .Other); |
| 3611 | 3683 | try v.elem(f, writer); |
| ... | ... | @@ -3616,9 +3688,9 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3616 | 3688 | try writer.writeAll("(("); |
| 3617 | 3689 | try f.renderType(writer, field_ty); |
| 3618 | 3690 | try writer.writeByte(')'); |
| 3619 | | const cant_cast = host_ty.isInt(mod) and host_ty.bitSize(mod) > 64; |
| 3691 | const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64; |
| 3620 | 3692 | if (cant_cast) { |
| 3621 | | if (field_ty.bitSize(mod) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); |
| 3693 | if (field_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); |
| 3622 | 3694 | try writer.writeAll("zig_lo_"); |
| 3623 | 3695 | try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty); |
| 3624 | 3696 | try writer.writeByte('('); |
| ... | ... | @@ -3628,7 +3700,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3628 | 3700 | try writer.writeByte('('); |
| 3629 | 3701 | try f.writeCValueDeref(writer, operand); |
| 3630 | 3702 | try v.elem(f, writer); |
| 3631 | | try writer.print(", {})", .{try f.fmtIntLiteral(bit_offset_ty, bit_offset_val)}); |
| 3703 | try writer.print(", {})", .{try f.fmtIntLiteral(bit_offset_val)}); |
| 3632 | 3704 | if (cant_cast) try writer.writeByte(')'); |
| 3633 | 3705 | try f.object.dg.renderBuiltinInfo(writer, field_ty, .bits); |
| 3634 | 3706 | try writer.writeByte(')'); |
| ... | ... | @@ -3646,22 +3718,22 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3646 | 3718 | } |
| 3647 | 3719 | |
| 3648 | 3720 | fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 3649 | | const mod = f.object.dg.module; |
| 3721 | const zcu = f.object.dg.zcu; |
| 3650 | 3722 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3651 | 3723 | const writer = f.object.writer(); |
| 3652 | 3724 | const op_inst = un_op.toIndex(); |
| 3653 | 3725 | const op_ty = f.typeOf(un_op); |
| 3654 | | const ret_ty = if (is_ptr) op_ty.childType(mod) else op_ty; |
| 3655 | | const lowered_ret_ty = try lowerFnRetTy(ret_ty, mod); |
| 3726 | const ret_ty = if (is_ptr) op_ty.childType(zcu) else op_ty; |
| 3727 | const lowered_ret_ty = try lowerFnRetTy(ret_ty, zcu); |
| 3656 | 3728 | |
| 3657 | 3729 | if (op_inst != null and f.air.instructions.items(.tag)[@intFromEnum(op_inst.?)] == .call_always_tail) { |
| 3658 | 3730 | try reap(f, inst, &.{un_op}); |
| 3659 | 3731 | _ = try airCall(f, op_inst.?, .always_tail); |
| 3660 | | } else if (lowered_ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 3732 | } else if (lowered_ret_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 3661 | 3733 | const operand = try f.resolveInst(un_op); |
| 3662 | 3734 | try reap(f, inst, &.{un_op}); |
| 3663 | 3735 | var deref = is_ptr; |
| 3664 | | const is_array = lowersToArray(ret_ty, mod); |
| 3736 | const is_array = lowersToArray(ret_ty, zcu); |
| 3665 | 3737 | const ret_val = if (is_array) ret_val: { |
| 3666 | 3738 | const array_local = try f.allocLocal(inst, lowered_ret_ty); |
| 3667 | 3739 | try writer.writeAll("memcpy("); |
| ... | ... | @@ -3696,16 +3768,16 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 3696 | 3768 | } |
| 3697 | 3769 | |
| 3698 | 3770 | fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3699 | | const mod = f.object.dg.module; |
| 3771 | const zcu = f.object.dg.zcu; |
| 3700 | 3772 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3701 | 3773 | |
| 3702 | 3774 | const operand = try f.resolveInst(ty_op.operand); |
| 3703 | 3775 | try reap(f, inst, &.{ty_op.operand}); |
| 3704 | 3776 | |
| 3705 | 3777 | const inst_ty = f.typeOfIndex(inst); |
| 3706 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 3778 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 3707 | 3779 | const operand_ty = f.typeOf(ty_op.operand); |
| 3708 | | const scalar_ty = operand_ty.scalarType(mod); |
| 3780 | const scalar_ty = operand_ty.scalarType(zcu); |
| 3709 | 3781 | |
| 3710 | 3782 | const writer = f.object.writer(); |
| 3711 | 3783 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -3722,20 +3794,20 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3722 | 3794 | } |
| 3723 | 3795 | |
| 3724 | 3796 | fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3725 | | const mod = f.object.dg.module; |
| 3797 | const zcu = f.object.dg.zcu; |
| 3726 | 3798 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3727 | 3799 | |
| 3728 | 3800 | const operand = try f.resolveInst(ty_op.operand); |
| 3729 | 3801 | try reap(f, inst, &.{ty_op.operand}); |
| 3730 | 3802 | const inst_ty = f.typeOfIndex(inst); |
| 3731 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 3732 | | const dest_int_info = inst_scalar_ty.intInfo(mod); |
| 3803 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 3804 | const dest_int_info = inst_scalar_ty.intInfo(zcu); |
| 3733 | 3805 | const dest_bits = dest_int_info.bits; |
| 3734 | 3806 | const dest_c_bits = toCIntBits(dest_int_info.bits) orelse |
| 3735 | 3807 | return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{}); |
| 3736 | 3808 | const operand_ty = f.typeOf(ty_op.operand); |
| 3737 | | const scalar_ty = operand_ty.scalarType(mod); |
| 3738 | | const scalar_int_info = scalar_ty.intInfo(mod); |
| 3809 | const scalar_ty = operand_ty.scalarType(zcu); |
| 3810 | const scalar_int_info = scalar_ty.intInfo(zcu); |
| 3739 | 3811 | |
| 3740 | 3812 | const writer = f.object.writer(); |
| 3741 | 3813 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -3763,18 +3835,19 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3763 | 3835 | try v.elem(f, writer); |
| 3764 | 3836 | } else switch (dest_int_info.signedness) { |
| 3765 | 3837 | .unsigned => { |
| 3766 | | const mask_val = try inst_scalar_ty.maxIntScalar(mod, scalar_ty); |
| 3767 | 3838 | try writer.writeAll("zig_and_"); |
| 3768 | 3839 | try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty); |
| 3769 | 3840 | try writer.writeByte('('); |
| 3770 | 3841 | try f.writeCValue(writer, operand, .FunctionArgument); |
| 3771 | 3842 | try v.elem(f, writer); |
| 3772 | | try writer.print(", {x})", .{try f.fmtIntLiteral(scalar_ty, mask_val)}); |
| 3843 | try writer.print(", {x})", .{ |
| 3844 | try f.fmtIntLiteral(try inst_scalar_ty.maxIntScalar(zcu, scalar_ty)), |
| 3845 | }); |
| 3773 | 3846 | }, |
| 3774 | 3847 | .signed => { |
| 3775 | 3848 | const c_bits = toCIntBits(scalar_int_info.bits) orelse |
| 3776 | 3849 | return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{}); |
| 3777 | | const shift_val = try mod.intValue(Type.u8, c_bits - dest_bits); |
| 3850 | const shift_val = try zcu.intValue(Type.u8, c_bits - dest_bits); |
| 3778 | 3851 | |
| 3779 | 3852 | try writer.writeAll("zig_shr_"); |
| 3780 | 3853 | try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty); |
| ... | ... | @@ -3792,9 +3865,9 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3792 | 3865 | try f.writeCValue(writer, operand, .FunctionArgument); |
| 3793 | 3866 | try v.elem(f, writer); |
| 3794 | 3867 | if (c_bits == 128) try writer.writeByte(')'); |
| 3795 | | try writer.print(", {})", .{try f.fmtIntLiteral(Type.u8, shift_val)}); |
| 3868 | try writer.print(", {})", .{try f.fmtIntLiteral(shift_val)}); |
| 3796 | 3869 | if (c_bits == 128) try writer.writeByte(')'); |
| 3797 | | try writer.print(", {})", .{try f.fmtIntLiteral(Type.u8, shift_val)}); |
| 3870 | try writer.print(", {})", .{try f.fmtIntLiteral(shift_val)}); |
| 3798 | 3871 | }, |
| 3799 | 3872 | } |
| 3800 | 3873 | |
| ... | ... | @@ -3821,18 +3894,18 @@ fn airIntFromBool(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3821 | 3894 | } |
| 3822 | 3895 | |
| 3823 | 3896 | fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3824 | | const mod = f.object.dg.module; |
| 3897 | const zcu = f.object.dg.zcu; |
| 3825 | 3898 | // *a = b; |
| 3826 | 3899 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3827 | 3900 | |
| 3828 | 3901 | const ptr_ty = f.typeOf(bin_op.lhs); |
| 3829 | | const ptr_scalar_ty = ptr_ty.scalarType(mod); |
| 3830 | | const ptr_info = ptr_scalar_ty.ptrInfo(mod); |
| 3902 | const ptr_scalar_ty = ptr_ty.scalarType(zcu); |
| 3903 | const ptr_info = ptr_scalar_ty.ptrInfo(zcu); |
| 3831 | 3904 | |
| 3832 | 3905 | const ptr_val = try f.resolveInst(bin_op.lhs); |
| 3833 | 3906 | const src_ty = f.typeOf(bin_op.rhs); |
| 3834 | 3907 | |
| 3835 | | const val_is_undef = if (try f.air.value(bin_op.rhs, mod)) |v| v.isUndefDeep(mod) else false; |
| 3908 | const val_is_undef = if (try f.air.value(bin_op.rhs, zcu)) |v| v.isUndefDeep(zcu) else false; |
| 3836 | 3909 | |
| 3837 | 3910 | if (val_is_undef) { |
| 3838 | 3911 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| ... | ... | @@ -3848,10 +3921,10 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3848 | 3921 | } |
| 3849 | 3922 | |
| 3850 | 3923 | const is_aligned = if (ptr_info.flags.alignment != .none) |
| 3851 | | ptr_info.flags.alignment.compare(.gte, src_ty.abiAlignment(mod)) |
| 3924 | ptr_info.flags.alignment.compare(.gte, src_ty.abiAlignment(zcu)) |
| 3852 | 3925 | else |
| 3853 | 3926 | true; |
| 3854 | | const is_array = lowersToArray(Type.fromInterned(ptr_info.child), mod); |
| 3927 | const is_array = lowersToArray(Type.fromInterned(ptr_info.child), zcu); |
| 3855 | 3928 | const need_memcpy = !is_aligned or is_array; |
| 3856 | 3929 | |
| 3857 | 3930 | const src_val = try f.resolveInst(bin_op.rhs); |
| ... | ... | @@ -3863,7 +3936,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3863 | 3936 | if (need_memcpy) { |
| 3864 | 3937 | // For this memcpy to safely work we need the rhs to have the same |
| 3865 | 3938 | // underlying type as the lhs (i.e. they must both be arrays of the same underlying type). |
| 3866 | | assert(src_ty.eql(Type.fromInterned(ptr_info.child), f.object.dg.module)); |
| 3939 | assert(src_ty.eql(Type.fromInterned(ptr_info.child), f.object.dg.zcu)); |
| 3867 | 3940 | |
| 3868 | 3941 | // If the source is a constant, writeCValue will emit a brace initialization |
| 3869 | 3942 | // so work around this by initializing into new local. |
| ... | ... | @@ -3893,12 +3966,12 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3893 | 3966 | } |
| 3894 | 3967 | } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) { |
| 3895 | 3968 | const host_bits = ptr_info.packed_offset.host_size * 8; |
| 3896 | | const host_ty = try mod.intType(.unsigned, host_bits); |
| 3969 | const host_ty = try zcu.intType(.unsigned, host_bits); |
| 3897 | 3970 | |
| 3898 | | const bit_offset_ty = try mod.intType(.unsigned, Type.smallestUnsignedBits(host_bits - 1)); |
| 3899 | | const bit_offset_val = try mod.intValue(bit_offset_ty, ptr_info.packed_offset.bit_offset); |
| 3971 | const bit_offset_ty = try zcu.intType(.unsigned, Type.smallestUnsignedBits(host_bits - 1)); |
| 3972 | const bit_offset_val = try zcu.intValue(bit_offset_ty, ptr_info.packed_offset.bit_offset); |
| 3900 | 3973 | |
| 3901 | | const src_bits = src_ty.bitSize(mod); |
| 3974 | const src_bits = src_ty.bitSize(zcu); |
| 3902 | 3975 | |
| 3903 | 3976 | const ExpectedContents = [BigInt.Managed.default_capacity]BigIntLimb; |
| 3904 | 3977 | var stack align(@alignOf(ExpectedContents)) = |
| ... | ... | @@ -3911,7 +3984,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3911 | 3984 | try mask.shiftLeft(&mask, ptr_info.packed_offset.bit_offset); |
| 3912 | 3985 | try mask.bitNotWrap(&mask, .unsigned, host_bits); |
| 3913 | 3986 | |
| 3914 | | const mask_val = try mod.intValue_big(host_ty, mask.toConst()); |
| 3987 | const mask_val = try zcu.intValue_big(host_ty, mask.toConst()); |
| 3915 | 3988 | |
| 3916 | 3989 | try f.writeCValueDeref(writer, ptr_val); |
| 3917 | 3990 | try v.elem(f, writer); |
| ... | ... | @@ -3922,12 +3995,12 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3922 | 3995 | try writer.writeByte('('); |
| 3923 | 3996 | try f.writeCValueDeref(writer, ptr_val); |
| 3924 | 3997 | try v.elem(f, writer); |
| 3925 | | try writer.print(", {x}), zig_shl_", .{try f.fmtIntLiteral(host_ty, mask_val)}); |
| 3998 | try writer.print(", {x}), zig_shl_", .{try f.fmtIntLiteral(mask_val)}); |
| 3926 | 3999 | try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty); |
| 3927 | 4000 | try writer.writeByte('('); |
| 3928 | | const cant_cast = host_ty.isInt(mod) and host_ty.bitSize(mod) > 64; |
| 4001 | const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64; |
| 3929 | 4002 | if (cant_cast) { |
| 3930 | | if (src_ty.bitSize(mod) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); |
| 4003 | if (src_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); |
| 3931 | 4004 | try writer.writeAll("zig_make_"); |
| 3932 | 4005 | try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty); |
| 3933 | 4006 | try writer.writeAll("(0, "); |
| ... | ... | @@ -3937,7 +4010,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3937 | 4010 | try writer.writeByte(')'); |
| 3938 | 4011 | } |
| 3939 | 4012 | |
| 3940 | | if (src_ty.isPtrAtRuntime(mod)) { |
| 4013 | if (src_ty.isPtrAtRuntime(zcu)) { |
| 3941 | 4014 | try writer.writeByte('('); |
| 3942 | 4015 | try f.renderType(writer, Type.usize); |
| 3943 | 4016 | try writer.writeByte(')'); |
| ... | ... | @@ -3945,7 +4018,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3945 | 4018 | try f.writeCValue(writer, src_val, .Other); |
| 3946 | 4019 | try v.elem(f, writer); |
| 3947 | 4020 | if (cant_cast) try writer.writeByte(')'); |
| 3948 | | try writer.print(", {}))", .{try f.fmtIntLiteral(bit_offset_ty, bit_offset_val)}); |
| 4021 | try writer.print(", {}))", .{try f.fmtIntLiteral(bit_offset_val)}); |
| 3949 | 4022 | } else { |
| 3950 | 4023 | try f.writeCValueDeref(writer, ptr_val); |
| 3951 | 4024 | try v.elem(f, writer); |
| ... | ... | @@ -3960,7 +4033,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3960 | 4033 | } |
| 3961 | 4034 | |
| 3962 | 4035 | fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: BuiltinInfo) !CValue { |
| 3963 | | const mod = f.object.dg.module; |
| 4036 | const zcu = f.object.dg.zcu; |
| 3964 | 4037 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3965 | 4038 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3966 | 4039 | |
| ... | ... | @@ -3970,7 +4043,7 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: |
| 3970 | 4043 | |
| 3971 | 4044 | const inst_ty = f.typeOfIndex(inst); |
| 3972 | 4045 | const operand_ty = f.typeOf(bin_op.lhs); |
| 3973 | | const scalar_ty = operand_ty.scalarType(mod); |
| 4046 | const scalar_ty = operand_ty.scalarType(zcu); |
| 3974 | 4047 | |
| 3975 | 4048 | const w = f.object.writer(); |
| 3976 | 4049 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -3998,11 +4071,11 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: |
| 3998 | 4071 | } |
| 3999 | 4072 | |
| 4000 | 4073 | fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4001 | | const mod = f.object.dg.module; |
| 4074 | const zcu = f.object.dg.zcu; |
| 4002 | 4075 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4003 | 4076 | const operand_ty = f.typeOf(ty_op.operand); |
| 4004 | | const scalar_ty = operand_ty.scalarType(mod); |
| 4005 | | if (scalar_ty.ip_index != .bool_type) return try airUnBuiltinCall(f, inst, "not", .bits); |
| 4077 | const scalar_ty = operand_ty.scalarType(zcu); |
| 4078 | if (scalar_ty.toIntern() != .bool_type) return try airUnBuiltinCall(f, inst, "not", .bits); |
| 4006 | 4079 | |
| 4007 | 4080 | const op = try f.resolveInst(ty_op.operand); |
| 4008 | 4081 | try reap(f, inst, &.{ty_op.operand}); |
| ... | ... | @@ -4031,11 +4104,11 @@ fn airBinOp( |
| 4031 | 4104 | operation: []const u8, |
| 4032 | 4105 | info: BuiltinInfo, |
| 4033 | 4106 | ) !CValue { |
| 4034 | | const mod = f.object.dg.module; |
| 4107 | const zcu = f.object.dg.zcu; |
| 4035 | 4108 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4036 | 4109 | const operand_ty = f.typeOf(bin_op.lhs); |
| 4037 | | const scalar_ty = operand_ty.scalarType(mod); |
| 4038 | | if ((scalar_ty.isInt(mod) and scalar_ty.bitSize(mod) > 64) or scalar_ty.isRuntimeFloat()) |
| 4110 | const scalar_ty = operand_ty.scalarType(zcu); |
| 4111 | if ((scalar_ty.isInt(zcu) and scalar_ty.bitSize(zcu) > 64) or scalar_ty.isRuntimeFloat()) |
| 4039 | 4112 | return try airBinBuiltinCall(f, inst, operation, info); |
| 4040 | 4113 | |
| 4041 | 4114 | const lhs = try f.resolveInst(bin_op.lhs); |
| ... | ... | @@ -4069,12 +4142,12 @@ fn airCmpOp( |
| 4069 | 4142 | data: anytype, |
| 4070 | 4143 | operator: std.math.CompareOperator, |
| 4071 | 4144 | ) !CValue { |
| 4072 | | const mod = f.object.dg.module; |
| 4145 | const zcu = f.object.dg.zcu; |
| 4073 | 4146 | const lhs_ty = f.typeOf(data.lhs); |
| 4074 | | const scalar_ty = lhs_ty.scalarType(mod); |
| 4147 | const scalar_ty = lhs_ty.scalarType(zcu); |
| 4075 | 4148 | |
| 4076 | | const scalar_bits = scalar_ty.bitSize(mod); |
| 4077 | | if (scalar_ty.isInt(mod) and scalar_bits > 64) |
| 4149 | const scalar_bits = scalar_ty.bitSize(zcu); |
| 4150 | if (scalar_ty.isInt(zcu) and scalar_bits > 64) |
| 4078 | 4151 | return airCmpBuiltinCall( |
| 4079 | 4152 | f, |
| 4080 | 4153 | inst, |
| ... | ... | @@ -4092,7 +4165,7 @@ fn airCmpOp( |
| 4092 | 4165 | try reap(f, inst, &.{ data.lhs, data.rhs }); |
| 4093 | 4166 | |
| 4094 | 4167 | const rhs_ty = f.typeOf(data.rhs); |
| 4095 | | const need_cast = lhs_ty.isSinglePointer(mod) or rhs_ty.isSinglePointer(mod); |
| 4168 | const need_cast = lhs_ty.isSinglePointer(zcu) or rhs_ty.isSinglePointer(zcu); |
| 4096 | 4169 | const writer = f.object.writer(); |
| 4097 | 4170 | const local = try f.allocLocal(inst, inst_ty); |
| 4098 | 4171 | const v = try Vectorize.start(f, inst, writer, lhs_ty); |
| ... | ... | @@ -4117,12 +4190,12 @@ fn airEquality( |
| 4117 | 4190 | inst: Air.Inst.Index, |
| 4118 | 4191 | operator: std.math.CompareOperator, |
| 4119 | 4192 | ) !CValue { |
| 4120 | | const mod = f.object.dg.module; |
| 4193 | const zcu = f.object.dg.zcu; |
| 4121 | 4194 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4122 | 4195 | |
| 4123 | 4196 | const operand_ty = f.typeOf(bin_op.lhs); |
| 4124 | | const operand_bits = operand_ty.bitSize(mod); |
| 4125 | | if (operand_ty.isInt(mod) and operand_bits > 64) |
| 4197 | const operand_bits = operand_ty.bitSize(zcu); |
| 4198 | if (operand_ty.isInt(zcu) and operand_bits > 64) |
| 4126 | 4199 | return airCmpBuiltinCall( |
| 4127 | 4200 | f, |
| 4128 | 4201 | inst, |
| ... | ... | @@ -4145,7 +4218,7 @@ fn airEquality( |
| 4145 | 4218 | try f.writeCValue(writer, local, .Other); |
| 4146 | 4219 | try a.assign(f, writer); |
| 4147 | 4220 | |
| 4148 | | if (operand_ty.zigTypeTag(mod) == .Optional and !operand_ty.optionalReprIsPayload(mod)) { |
| 4221 | if (operand_ty.zigTypeTag(zcu) == .Optional and !operand_ty.optionalReprIsPayload(zcu)) { |
| 4149 | 4222 | try f.writeCValueMember(writer, lhs, .{ .identifier = "is_null" }); |
| 4150 | 4223 | try writer.writeAll(" || "); |
| 4151 | 4224 | try f.writeCValueMember(writer, rhs, .{ .identifier = "is_null" }); |
| ... | ... | @@ -4184,7 +4257,7 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4184 | 4257 | } |
| 4185 | 4258 | |
| 4186 | 4259 | fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 4187 | | const mod = f.object.dg.module; |
| 4260 | const zcu = f.object.dg.zcu; |
| 4188 | 4261 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4189 | 4262 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4190 | 4263 | |
| ... | ... | @@ -4193,8 +4266,8 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 4193 | 4266 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4194 | 4267 | |
| 4195 | 4268 | const inst_ty = f.typeOfIndex(inst); |
| 4196 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 4197 | | const elem_ty = inst_scalar_ty.elemType2(mod); |
| 4269 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 4270 | const elem_ty = inst_scalar_ty.elemType2(zcu); |
| 4198 | 4271 | |
| 4199 | 4272 | const local = try f.allocLocal(inst, inst_ty); |
| 4200 | 4273 | const writer = f.object.writer(); |
| ... | ... | @@ -4203,7 +4276,7 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 4203 | 4276 | try v.elem(f, writer); |
| 4204 | 4277 | try writer.writeAll(" = "); |
| 4205 | 4278 | |
| 4206 | | if (elem_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 4279 | if (elem_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 4207 | 4280 | // We must convert to and from integer types to prevent UB if the operation |
| 4208 | 4281 | // results in a NULL pointer, or if LHS is NULL. The operation is only UB |
| 4209 | 4282 | // if the result is NULL and then dereferenced. |
| ... | ... | @@ -4232,13 +4305,13 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 4232 | 4305 | } |
| 4233 | 4306 | |
| 4234 | 4307 | fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []const u8) !CValue { |
| 4235 | | const mod = f.object.dg.module; |
| 4308 | const zcu = f.object.dg.zcu; |
| 4236 | 4309 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4237 | 4310 | |
| 4238 | 4311 | const inst_ty = f.typeOfIndex(inst); |
| 4239 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 4312 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 4240 | 4313 | |
| 4241 | | if (inst_scalar_ty.isInt(mod) and inst_scalar_ty.bitSize(mod) > 64) |
| 4314 | if (inst_scalar_ty.isInt(zcu) and inst_scalar_ty.bitSize(zcu) > 64) |
| 4242 | 4315 | return try airBinBuiltinCall(f, inst, operation[1..], .none); |
| 4243 | 4316 | if (inst_scalar_ty.isRuntimeFloat()) |
| 4244 | 4317 | return try airBinFloatOp(f, inst, operation); |
| ... | ... | @@ -4274,7 +4347,7 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons |
| 4274 | 4347 | } |
| 4275 | 4348 | |
| 4276 | 4349 | fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4277 | | const mod = f.object.dg.module; |
| 4350 | const zcu = f.object.dg.zcu; |
| 4278 | 4351 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4279 | 4352 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4280 | 4353 | |
| ... | ... | @@ -4283,7 +4356,7 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4283 | 4356 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4284 | 4357 | |
| 4285 | 4358 | const inst_ty = f.typeOfIndex(inst); |
| 4286 | | const ptr_ty = inst_ty.slicePtrFieldType(mod); |
| 4359 | const ptr_ty = inst_ty.slicePtrFieldType(zcu); |
| 4287 | 4360 | |
| 4288 | 4361 | const writer = f.object.writer(); |
| 4289 | 4362 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -4291,9 +4364,6 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4291 | 4364 | const a = try Assignment.start(f, writer, ptr_ty); |
| 4292 | 4365 | try f.writeCValueMember(writer, local, .{ .identifier = "ptr" }); |
| 4293 | 4366 | try a.assign(f, writer); |
| 4294 | | try writer.writeByte('('); |
| 4295 | | try f.renderType(writer, ptr_ty); |
| 4296 | | try writer.writeByte(')'); |
| 4297 | 4367 | try f.writeCValue(writer, ptr, .Other); |
| 4298 | 4368 | try a.end(f, writer); |
| 4299 | 4369 | } |
| ... | ... | @@ -4301,7 +4371,7 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4301 | 4371 | const a = try Assignment.start(f, writer, Type.usize); |
| 4302 | 4372 | try f.writeCValueMember(writer, local, .{ .identifier = "len" }); |
| 4303 | 4373 | try a.assign(f, writer); |
| 4304 | | try f.writeCValue(writer, len, .Other); |
| 4374 | try f.writeCValue(writer, len, .Initializer); |
| 4305 | 4375 | try a.end(f, writer); |
| 4306 | 4376 | } |
| 4307 | 4377 | return local; |
| ... | ... | @@ -4312,7 +4382,7 @@ fn airCall( |
| 4312 | 4382 | inst: Air.Inst.Index, |
| 4313 | 4383 | modifier: std.builtin.CallModifier, |
| 4314 | 4384 | ) !CValue { |
| 4315 | | const mod = f.object.dg.module; |
| 4385 | const zcu = f.object.dg.zcu; |
| 4316 | 4386 | // Not even allowed to call panic in a naked function. |
| 4317 | 4387 | if (f.object.dg.is_naked_fn) return .none; |
| 4318 | 4388 | |
| ... | ... | @@ -4334,7 +4404,7 @@ fn airCall( |
| 4334 | 4404 | } |
| 4335 | 4405 | resolved_arg.* = try f.resolveInst(arg); |
| 4336 | 4406 | if (arg_cty != try f.typeToIndex(arg_ty, .complete)) { |
| 4337 | | const lowered_arg_ty = try lowerFnRetTy(arg_ty, mod); |
| 4407 | const lowered_arg_ty = try lowerFnRetTy(arg_ty, zcu); |
| 4338 | 4408 | |
| 4339 | 4409 | const array_local = try f.allocLocal(inst, lowered_arg_ty); |
| 4340 | 4410 | try writer.writeAll("memcpy("); |
| ... | ... | @@ -4357,20 +4427,19 @@ fn airCall( |
| 4357 | 4427 | } |
| 4358 | 4428 | |
| 4359 | 4429 | const callee_ty = f.typeOf(pl_op.operand); |
| 4360 | | const fn_ty = switch (callee_ty.zigTypeTag(mod)) { |
| 4430 | const fn_info = zcu.typeToFunc(switch (callee_ty.zigTypeTag(zcu)) { |
| 4361 | 4431 | .Fn => callee_ty, |
| 4362 | | .Pointer => callee_ty.childType(mod), |
| 4432 | .Pointer => callee_ty.childType(zcu), |
| 4363 | 4433 | else => unreachable, |
| 4364 | | }; |
| 4365 | | |
| 4366 | | const ret_ty = fn_ty.fnReturnType(mod); |
| 4367 | | const lowered_ret_ty = try lowerFnRetTy(ret_ty, mod); |
| 4434 | }).?; |
| 4435 | const ret_ty = Type.fromInterned(fn_info.return_type); |
| 4436 | const lowered_ret_ty = try lowerFnRetTy(ret_ty, zcu); |
| 4368 | 4437 | |
| 4369 | 4438 | const result_local = result: { |
| 4370 | 4439 | if (modifier == .always_tail) { |
| 4371 | 4440 | try writer.writeAll("zig_always_tail return "); |
| 4372 | 4441 | break :result .none; |
| 4373 | | } else if (!lowered_ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 4442 | } else if (!lowered_ret_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 4374 | 4443 | break :result .none; |
| 4375 | 4444 | } else if (f.liveness.isUnused(inst)) { |
| 4376 | 4445 | try writer.writeByte('('); |
| ... | ... | @@ -4388,8 +4457,8 @@ fn airCall( |
| 4388 | 4457 | callee: { |
| 4389 | 4458 | known: { |
| 4390 | 4459 | const fn_decl = fn_decl: { |
| 4391 | | const callee_val = (try f.air.value(pl_op.operand, mod)) orelse break :known; |
| 4392 | | break :fn_decl switch (mod.intern_pool.indexToKey(callee_val.ip_index)) { |
| 4460 | const callee_val = (try f.air.value(pl_op.operand, zcu)) orelse break :known; |
| 4461 | break :fn_decl switch (zcu.intern_pool.indexToKey(callee_val.toIntern())) { |
| 4393 | 4462 | .extern_func => |extern_func| extern_func.decl, |
| 4394 | 4463 | .func => |func| func.owner_decl, |
| 4395 | 4464 | .ptr => |ptr| switch (ptr.addr) { |
| ... | ... | @@ -4420,18 +4489,21 @@ fn airCall( |
| 4420 | 4489 | } |
| 4421 | 4490 | |
| 4422 | 4491 | try writer.writeByte('('); |
| 4423 | | var args_written: usize = 0; |
| 4492 | var need_comma = false; |
| 4424 | 4493 | for (resolved_args) |resolved_arg| { |
| 4425 | 4494 | if (resolved_arg == .none) continue; |
| 4426 | | if (args_written != 0) try writer.writeAll(", "); |
| 4495 | if (need_comma) try writer.writeAll(", "); |
| 4496 | need_comma = true; |
| 4427 | 4497 | try f.writeCValue(writer, resolved_arg, .FunctionArgument); |
| 4428 | | if (resolved_arg == .new_local) try freeLocal(f, inst, resolved_arg.new_local, null); |
| 4429 | | args_written += 1; |
| 4498 | switch (resolved_arg) { |
| 4499 | .new_local => |local| try freeLocal(f, inst, local, null), |
| 4500 | else => {}, |
| 4501 | } |
| 4430 | 4502 | } |
| 4431 | 4503 | try writer.writeAll(");\n"); |
| 4432 | 4504 | |
| 4433 | 4505 | const result = result: { |
| 4434 | | if (result_local == .none or !lowersToArray(ret_ty, mod)) |
| 4506 | if (result_local == .none or !lowersToArray(ret_ty, zcu)) |
| 4435 | 4507 | break :result result_local; |
| 4436 | 4508 | |
| 4437 | 4509 | const array_local = try f.allocLocal(inst, ret_ty); |
| ... | ... | @@ -4465,22 +4537,22 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4465 | 4537 | } |
| 4466 | 4538 | |
| 4467 | 4539 | fn airDbgInlineBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4468 | | const mod = f.object.dg.module; |
| 4540 | const zcu = f.object.dg.zcu; |
| 4469 | 4541 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4470 | 4542 | const extra = f.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 4471 | | const owner_decl = mod.funcOwnerDeclPtr(extra.data.func); |
| 4543 | const owner_decl = zcu.funcOwnerDeclPtr(extra.data.func); |
| 4472 | 4544 | const writer = f.object.writer(); |
| 4473 | 4545 | try writer.writeAll("/* "); |
| 4474 | | try owner_decl.renderFullyQualifiedName(mod, writer); |
| 4546 | try owner_decl.renderFullyQualifiedName(zcu, writer); |
| 4475 | 4547 | try writer.writeAll(" */ "); |
| 4476 | 4548 | return lowerBlock(f, inst, @ptrCast(f.air.extra[extra.end..][0..extra.data.body_len])); |
| 4477 | 4549 | } |
| 4478 | 4550 | |
| 4479 | 4551 | fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4480 | | const mod = f.object.dg.module; |
| 4552 | const zcu = f.object.dg.zcu; |
| 4481 | 4553 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4482 | 4554 | const name = f.air.nullTerminatedString(pl_op.payload); |
| 4483 | | const operand_is_undef = if (try f.air.value(pl_op.operand, mod)) |v| v.isUndefDeep(mod) else false; |
| 4555 | const operand_is_undef = if (try f.air.value(pl_op.operand, zcu)) |v| v.isUndefDeep(zcu) else false; |
| 4484 | 4556 | if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand); |
| 4485 | 4557 | |
| 4486 | 4558 | try reap(f, inst, &.{pl_op.operand}); |
| ... | ... | @@ -4496,7 +4568,7 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4496 | 4568 | } |
| 4497 | 4569 | |
| 4498 | 4570 | fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) !CValue { |
| 4499 | | const mod = f.object.dg.module; |
| 4571 | const zcu = f.object.dg.zcu; |
| 4500 | 4572 | const liveness_block = f.liveness.getBlock(inst); |
| 4501 | 4573 | |
| 4502 | 4574 | const block_id: usize = f.next_block_index; |
| ... | ... | @@ -4504,7 +4576,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 4504 | 4576 | const writer = f.object.writer(); |
| 4505 | 4577 | |
| 4506 | 4578 | const inst_ty = f.typeOfIndex(inst); |
| 4507 | | const result = if (inst_ty.hasRuntimeBitsIgnoreComptime(mod) and !f.liveness.isUnused(inst)) |
| 4579 | const result = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu) and !f.liveness.isUnused(inst)) |
| 4508 | 4580 | try f.allocLocal(inst, inst_ty) |
| 4509 | 4581 | else |
| 4510 | 4582 | .none; |
| ... | ... | @@ -4526,7 +4598,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 4526 | 4598 | try f.object.indent_writer.insertNewline(); |
| 4527 | 4599 | |
| 4528 | 4600 | // noreturn blocks have no `br` instructions reaching them, so we don't want a label |
| 4529 | | if (!f.typeOfIndex(inst).isNoReturn(mod)) { |
| 4601 | if (!f.typeOfIndex(inst).isNoReturn(zcu)) { |
| 4530 | 4602 | // label must be followed by an expression, include an empty one. |
| 4531 | 4603 | try writer.print("zig_block_{d}:;\n", .{block_id}); |
| 4532 | 4604 | } |
| ... | ... | @@ -4543,11 +4615,11 @@ fn airTry(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4543 | 4615 | } |
| 4544 | 4616 | |
| 4545 | 4617 | fn airTryPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4546 | | const mod = f.object.dg.module; |
| 4618 | const zcu = f.object.dg.zcu; |
| 4547 | 4619 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4548 | 4620 | const extra = f.air.extraData(Air.TryPtr, ty_pl.payload); |
| 4549 | 4621 | const body: []const Air.Inst.Index = @ptrCast(f.air.extra[extra.end..][0..extra.data.body_len]); |
| 4550 | | const err_union_ty = f.typeOf(extra.data.ptr).childType(mod); |
| 4622 | const err_union_ty = f.typeOf(extra.data.ptr).childType(zcu); |
| 4551 | 4623 | return lowerTry(f, inst, extra.data.ptr, body, err_union_ty, true); |
| 4552 | 4624 | } |
| 4553 | 4625 | |
| ... | ... | @@ -4559,15 +4631,15 @@ fn lowerTry( |
| 4559 | 4631 | err_union_ty: Type, |
| 4560 | 4632 | is_ptr: bool, |
| 4561 | 4633 | ) !CValue { |
| 4562 | | const mod = f.object.dg.module; |
| 4634 | const zcu = f.object.dg.zcu; |
| 4563 | 4635 | const err_union = try f.resolveInst(operand); |
| 4564 | 4636 | const inst_ty = f.typeOfIndex(inst); |
| 4565 | 4637 | const liveness_condbr = f.liveness.getCondBr(inst); |
| 4566 | 4638 | const writer = f.object.writer(); |
| 4567 | | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 4568 | | const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(mod); |
| 4639 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 4640 | const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(zcu); |
| 4569 | 4641 | |
| 4570 | | if (!err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { |
| 4642 | if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { |
| 4571 | 4643 | try writer.writeAll("if ("); |
| 4572 | 4644 | if (!payload_has_bits) { |
| 4573 | 4645 | if (is_ptr) |
| ... | ... | @@ -4661,7 +4733,7 @@ const LocalResult = struct { |
| 4661 | 4733 | need_free: bool, |
| 4662 | 4734 | |
| 4663 | 4735 | fn move(lr: LocalResult, f: *Function, inst: Air.Inst.Index, dest_ty: Type) !CValue { |
| 4664 | | const mod = f.object.dg.module; |
| 4736 | const zcu = f.object.dg.zcu; |
| 4665 | 4737 | |
| 4666 | 4738 | if (lr.need_free) { |
| 4667 | 4739 | // Move the freshly allocated local to be owned by this instruction, |
| ... | ... | @@ -4673,7 +4745,7 @@ const LocalResult = struct { |
| 4673 | 4745 | try lr.free(f); |
| 4674 | 4746 | const writer = f.object.writer(); |
| 4675 | 4747 | try f.writeCValue(writer, local, .Other); |
| 4676 | | if (dest_ty.isAbiInt(mod)) { |
| 4748 | if (dest_ty.isAbiInt(zcu)) { |
| 4677 | 4749 | try writer.writeAll(" = "); |
| 4678 | 4750 | } else { |
| 4679 | 4751 | try writer.writeAll(" = ("); |
| ... | ... | @@ -4693,13 +4765,13 @@ const LocalResult = struct { |
| 4693 | 4765 | }; |
| 4694 | 4766 | |
| 4695 | 4767 | fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !LocalResult { |
| 4696 | | const mod = f.object.dg.module; |
| 4697 | | const target = mod.getTarget(); |
| 4768 | const zcu = f.object.dg.zcu; |
| 4769 | const target = &f.object.dg.mod.resolved_target.result; |
| 4698 | 4770 | const writer = f.object.writer(); |
| 4699 | 4771 | |
| 4700 | | if (operand_ty.isAbiInt(mod) and dest_ty.isAbiInt(mod)) { |
| 4701 | | const src_info = dest_ty.intInfo(mod); |
| 4702 | | const dest_info = operand_ty.intInfo(mod); |
| 4772 | if (operand_ty.isAbiInt(zcu) and dest_ty.isAbiInt(zcu)) { |
| 4773 | const src_info = dest_ty.intInfo(zcu); |
| 4774 | const dest_info = operand_ty.intInfo(zcu); |
| 4703 | 4775 | if (src_info.signedness == dest_info.signedness and |
| 4704 | 4776 | src_info.bits == dest_info.bits) |
| 4705 | 4777 | { |
| ... | ... | @@ -4710,7 +4782,7 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca |
| 4710 | 4782 | } |
| 4711 | 4783 | } |
| 4712 | 4784 | |
| 4713 | | if (dest_ty.isPtrAtRuntime(mod) and operand_ty.isPtrAtRuntime(mod)) { |
| 4785 | if (dest_ty.isPtrAtRuntime(zcu) and operand_ty.isPtrAtRuntime(zcu)) { |
| 4714 | 4786 | const local = try f.allocLocal(null, dest_ty); |
| 4715 | 4787 | try f.writeCValue(writer, local, .Other); |
| 4716 | 4788 | try writer.writeAll(" = ("); |
| ... | ... | @@ -4727,7 +4799,7 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca |
| 4727 | 4799 | const operand_lval = if (operand == .constant) blk: { |
| 4728 | 4800 | const operand_local = try f.allocLocal(null, operand_ty); |
| 4729 | 4801 | try f.writeCValue(writer, operand_local, .Other); |
| 4730 | | if (operand_ty.isAbiInt(mod)) { |
| 4802 | if (operand_ty.isAbiInt(zcu)) { |
| 4731 | 4803 | try writer.writeAll(" = "); |
| 4732 | 4804 | } else { |
| 4733 | 4805 | try writer.writeAll(" = ("); |
| ... | ... | @@ -4747,14 +4819,14 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca |
| 4747 | 4819 | try writer.writeAll(", sizeof("); |
| 4748 | 4820 | try f.renderType( |
| 4749 | 4821 | writer, |
| 4750 | | if (dest_ty.abiSize(mod) <= operand_ty.abiSize(mod)) dest_ty else operand_ty, |
| 4822 | if (dest_ty.abiSize(zcu) <= operand_ty.abiSize(zcu)) dest_ty else operand_ty, |
| 4751 | 4823 | ); |
| 4752 | 4824 | try writer.writeAll("));\n"); |
| 4753 | 4825 | |
| 4754 | 4826 | // Ensure padding bits have the expected value. |
| 4755 | | if (dest_ty.isAbiInt(mod)) { |
| 4827 | if (dest_ty.isAbiInt(zcu)) { |
| 4756 | 4828 | const dest_cty = try f.typeToCType(dest_ty, .complete); |
| 4757 | | const dest_info = dest_ty.intInfo(mod); |
| 4829 | const dest_info = dest_ty.intInfo(zcu); |
| 4758 | 4830 | var bits: u16 = dest_info.bits; |
| 4759 | 4831 | var wrap_cty: ?CType = null; |
| 4760 | 4832 | var need_bitcasts = false; |
| ... | ... | @@ -4779,7 +4851,7 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca |
| 4779 | 4851 | try writer.writeByte('('); |
| 4780 | 4852 | } |
| 4781 | 4853 | try writer.writeAll("zig_wrap_"); |
| 4782 | | const info_ty = try mod.intType(dest_info.signedness, bits); |
| 4854 | const info_ty = try zcu.intType(dest_info.signedness, bits); |
| 4783 | 4855 | if (wrap_cty) |cty| |
| 4784 | 4856 | try f.object.dg.renderCTypeForBuiltinFnName(writer, cty) |
| 4785 | 4857 | else |
| ... | ... | @@ -4912,7 +4984,7 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4912 | 4984 | } |
| 4913 | 4985 | |
| 4914 | 4986 | fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4915 | | const mod = f.object.dg.module; |
| 4987 | const zcu = f.object.dg.zcu; |
| 4916 | 4988 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4917 | 4989 | const condition = try f.resolveInst(pl_op.operand); |
| 4918 | 4990 | try reap(f, inst, &.{pl_op.operand}); |
| ... | ... | @@ -4921,11 +4993,11 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4921 | 4993 | const writer = f.object.writer(); |
| 4922 | 4994 | |
| 4923 | 4995 | try writer.writeAll("switch ("); |
| 4924 | | if (condition_ty.zigTypeTag(mod) == .Bool) { |
| 4996 | if (condition_ty.zigTypeTag(zcu) == .Bool) { |
| 4925 | 4997 | try writer.writeByte('('); |
| 4926 | 4998 | try f.renderType(writer, Type.u1); |
| 4927 | 4999 | try writer.writeByte(')'); |
| 4928 | | } else if (condition_ty.isPtrAtRuntime(mod)) { |
| 5000 | } else if (condition_ty.isPtrAtRuntime(zcu)) { |
| 4929 | 5001 | try writer.writeByte('('); |
| 4930 | 5002 | try f.renderType(writer, Type.usize); |
| 4931 | 5003 | try writer.writeByte(')'); |
| ... | ... | @@ -4952,12 +5024,12 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4952 | 5024 | for (items) |item| { |
| 4953 | 5025 | try f.object.indent_writer.insertNewline(); |
| 4954 | 5026 | try writer.writeAll("case "); |
| 4955 | | if (condition_ty.isPtrAtRuntime(mod)) { |
| 5027 | if (condition_ty.isPtrAtRuntime(zcu)) { |
| 4956 | 5028 | try writer.writeByte('('); |
| 4957 | 5029 | try f.renderType(writer, Type.usize); |
| 4958 | 5030 | try writer.writeByte(')'); |
| 4959 | 5031 | } |
| 4960 | | try f.object.dg.renderValue(writer, condition_ty, (try f.air.value(item, mod)).?, .Other); |
| 5032 | try f.object.dg.renderValue(writer, (try f.air.value(item, zcu)).?, .Other); |
| 4961 | 5033 | try writer.writeByte(':'); |
| 4962 | 5034 | } |
| 4963 | 5035 | try writer.writeByte(' '); |
| ... | ... | @@ -4994,13 +5066,13 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4994 | 5066 | } |
| 4995 | 5067 | |
| 4996 | 5068 | fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool { |
| 4997 | | const target = f.object.dg.module.getTarget(); |
| 5069 | const target = &f.object.dg.mod.resolved_target.result; |
| 4998 | 5070 | return switch (constraint[0]) { |
| 4999 | 5071 | '{' => true, |
| 5000 | 5072 | 'i', 'r' => false, |
| 5001 | 5073 | 'I' => !target.cpu.arch.isArmOrThumb(), |
| 5002 | 5074 | else => switch (value) { |
| 5003 | | .constant => |val| switch (f.object.dg.module.intern_pool.indexToKey(val)) { |
| 5075 | .constant => |val| switch (f.object.dg.zcu.intern_pool.indexToKey(val.toIntern())) { |
| 5004 | 5076 | .ptr => |ptr| switch (ptr.addr) { |
| 5005 | 5077 | .decl => false, |
| 5006 | 5078 | else => true, |
| ... | ... | @@ -5013,7 +5085,7 @@ fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool |
| 5013 | 5085 | } |
| 5014 | 5086 | |
| 5015 | 5087 | fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5016 | | const mod = f.object.dg.module; |
| 5088 | const zcu = f.object.dg.zcu; |
| 5017 | 5089 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5018 | 5090 | const extra = f.air.extraData(Air.Asm, ty_pl.payload); |
| 5019 | 5091 | const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0; |
| ... | ... | @@ -5028,7 +5100,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5028 | 5100 | const result = result: { |
| 5029 | 5101 | const writer = f.object.writer(); |
| 5030 | 5102 | const inst_ty = f.typeOfIndex(inst); |
| 5031 | | const local = if (inst_ty.hasRuntimeBitsIgnoreComptime(mod)) local: { |
| 5103 | const local = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) local: { |
| 5032 | 5104 | const local = try f.allocLocal(inst, inst_ty); |
| 5033 | 5105 | if (f.wantSafety()) { |
| 5034 | 5106 | try f.writeCValue(writer, local, .Other); |
| ... | ... | @@ -5057,7 +5129,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5057 | 5129 | |
| 5058 | 5130 | const is_reg = constraint[1] == '{'; |
| 5059 | 5131 | if (is_reg) { |
| 5060 | | const output_ty = if (output == .none) inst_ty else f.typeOf(output).childType(mod); |
| 5132 | const output_ty = if (output == .none) inst_ty else f.typeOf(output).childType(zcu); |
| 5061 | 5133 | try writer.writeAll("register "); |
| 5062 | 5134 | const alignment: Alignment = .none; |
| 5063 | 5135 | const local_value = try f.allocLocalValue(output_ty, alignment); |
| ... | ... | @@ -5275,7 +5347,7 @@ fn airIsNull( |
| 5275 | 5347 | operator: []const u8, |
| 5276 | 5348 | is_ptr: bool, |
| 5277 | 5349 | ) !CValue { |
| 5278 | | const mod = f.object.dg.module; |
| 5350 | const zcu = f.object.dg.zcu; |
| 5279 | 5351 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5280 | 5352 | |
| 5281 | 5353 | const writer = f.object.writer(); |
| ... | ... | @@ -5292,22 +5364,22 @@ fn airIsNull( |
| 5292 | 5364 | } |
| 5293 | 5365 | |
| 5294 | 5366 | const operand_ty = f.typeOf(un_op); |
| 5295 | | const optional_ty = if (is_ptr) operand_ty.childType(mod) else operand_ty; |
| 5296 | | const payload_ty = optional_ty.optionalChild(mod); |
| 5297 | | const err_int_ty = try mod.errorIntType(); |
| 5367 | const optional_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; |
| 5368 | const payload_ty = optional_ty.optionalChild(zcu); |
| 5369 | const err_int_ty = try zcu.errorIntType(); |
| 5298 | 5370 | |
| 5299 | | const rhs = if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) |
| 5371 | const rhs = if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) |
| 5300 | 5372 | Value.true |
| 5301 | | else if (optional_ty.isPtrLikeOptional(mod)) |
| 5373 | else if (optional_ty.isPtrLikeOptional(zcu)) |
| 5302 | 5374 | // operand is a regular pointer, test `operand !=/== NULL` |
| 5303 | | try mod.getCoerced(Value.null, optional_ty) |
| 5304 | | else if (payload_ty.zigTypeTag(mod) == .ErrorSet) |
| 5305 | | try mod.intValue(err_int_ty, 0) |
| 5306 | | else if (payload_ty.isSlice(mod) and optional_ty.optionalReprIsPayload(mod)) rhs: { |
| 5375 | try zcu.getCoerced(Value.null, optional_ty) |
| 5376 | else if (payload_ty.zigTypeTag(zcu) == .ErrorSet) |
| 5377 | try zcu.intValue(err_int_ty, 0) |
| 5378 | else if (payload_ty.isSlice(zcu) and optional_ty.optionalReprIsPayload(zcu)) rhs: { |
| 5307 | 5379 | try writer.writeAll(".ptr"); |
| 5308 | | const slice_ptr_ty = payload_ty.slicePtrFieldType(mod); |
| 5309 | | const opt_slice_ptr_ty = try mod.optionalType(slice_ptr_ty.toIntern()); |
| 5310 | | break :rhs try mod.nullValue(opt_slice_ptr_ty); |
| 5380 | const slice_ptr_ty = payload_ty.slicePtrFieldType(zcu); |
| 5381 | const opt_slice_ptr_ty = try zcu.optionalType(slice_ptr_ty.toIntern()); |
| 5382 | break :rhs try zcu.nullValue(opt_slice_ptr_ty); |
| 5311 | 5383 | } else rhs: { |
| 5312 | 5384 | try writer.writeAll(".is_null"); |
| 5313 | 5385 | break :rhs Value.true; |
| ... | ... | @@ -5315,22 +5387,22 @@ fn airIsNull( |
| 5315 | 5387 | try writer.writeByte(' '); |
| 5316 | 5388 | try writer.writeAll(operator); |
| 5317 | 5389 | try writer.writeByte(' '); |
| 5318 | | try f.object.dg.renderValue(writer, rhs.typeOf(mod), rhs, .Other); |
| 5390 | try f.object.dg.renderValue(writer, rhs, .Other); |
| 5319 | 5391 | try writer.writeAll(";\n"); |
| 5320 | 5392 | return local; |
| 5321 | 5393 | } |
| 5322 | 5394 | |
| 5323 | 5395 | fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5324 | | const mod = f.object.dg.module; |
| 5396 | const zcu = f.object.dg.zcu; |
| 5325 | 5397 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5326 | 5398 | |
| 5327 | 5399 | const operand = try f.resolveInst(ty_op.operand); |
| 5328 | 5400 | try reap(f, inst, &.{ty_op.operand}); |
| 5329 | 5401 | const opt_ty = f.typeOf(ty_op.operand); |
| 5330 | 5402 | |
| 5331 | | const payload_ty = opt_ty.optionalChild(mod); |
| 5403 | const payload_ty = opt_ty.optionalChild(zcu); |
| 5332 | 5404 | |
| 5333 | | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 5405 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 5334 | 5406 | return .none; |
| 5335 | 5407 | } |
| 5336 | 5408 | |
| ... | ... | @@ -5338,7 +5410,7 @@ fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5338 | 5410 | const writer = f.object.writer(); |
| 5339 | 5411 | const local = try f.allocLocal(inst, inst_ty); |
| 5340 | 5412 | |
| 5341 | | if (opt_ty.optionalReprIsPayload(mod)) { |
| 5413 | if (opt_ty.optionalReprIsPayload(zcu)) { |
| 5342 | 5414 | try f.writeCValue(writer, local, .Other); |
| 5343 | 5415 | try writer.writeAll(" = "); |
| 5344 | 5416 | try f.writeCValue(writer, operand, .Other); |
| ... | ... | @@ -5355,24 +5427,24 @@ fn airOptionalPayload(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5355 | 5427 | } |
| 5356 | 5428 | |
| 5357 | 5429 | fn airOptionalPayloadPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5358 | | const mod = f.object.dg.module; |
| 5430 | const zcu = f.object.dg.zcu; |
| 5359 | 5431 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5360 | 5432 | |
| 5361 | 5433 | const writer = f.object.writer(); |
| 5362 | 5434 | const operand = try f.resolveInst(ty_op.operand); |
| 5363 | 5435 | try reap(f, inst, &.{ty_op.operand}); |
| 5364 | 5436 | const ptr_ty = f.typeOf(ty_op.operand); |
| 5365 | | const opt_ty = ptr_ty.childType(mod); |
| 5437 | const opt_ty = ptr_ty.childType(zcu); |
| 5366 | 5438 | const inst_ty = f.typeOfIndex(inst); |
| 5367 | 5439 | |
| 5368 | | if (!inst_ty.childType(mod).hasRuntimeBitsIgnoreComptime(mod)) { |
| 5440 | if (!inst_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(zcu)) { |
| 5369 | 5441 | return .{ .undef = inst_ty }; |
| 5370 | 5442 | } |
| 5371 | 5443 | |
| 5372 | 5444 | const local = try f.allocLocal(inst, inst_ty); |
| 5373 | 5445 | try f.writeCValue(writer, local, .Other); |
| 5374 | 5446 | |
| 5375 | | if (opt_ty.optionalReprIsPayload(mod)) { |
| 5447 | if (opt_ty.optionalReprIsPayload(zcu)) { |
| 5376 | 5448 | // the operand is just a regular pointer, no need to do anything special. |
| 5377 | 5449 | // *?*T -> **T and ?*T -> *T are **T -> **T and *T -> *T in C |
| 5378 | 5450 | try writer.writeAll(" = "); |
| ... | ... | @@ -5386,18 +5458,18 @@ fn airOptionalPayloadPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5386 | 5458 | } |
| 5387 | 5459 | |
| 5388 | 5460 | fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5389 | | const mod = f.object.dg.module; |
| 5461 | const zcu = f.object.dg.zcu; |
| 5390 | 5462 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5391 | 5463 | const writer = f.object.writer(); |
| 5392 | 5464 | const operand = try f.resolveInst(ty_op.operand); |
| 5393 | 5465 | try reap(f, inst, &.{ty_op.operand}); |
| 5394 | 5466 | const operand_ty = f.typeOf(ty_op.operand); |
| 5395 | 5467 | |
| 5396 | | const opt_ty = operand_ty.childType(mod); |
| 5468 | const opt_ty = operand_ty.childType(zcu); |
| 5397 | 5469 | |
| 5398 | 5470 | const inst_ty = f.typeOfIndex(inst); |
| 5399 | 5471 | |
| 5400 | | if (opt_ty.optionalReprIsPayload(mod)) { |
| 5472 | if (opt_ty.optionalReprIsPayload(zcu)) { |
| 5401 | 5473 | if (f.liveness.isUnused(inst)) { |
| 5402 | 5474 | return .none; |
| 5403 | 5475 | } |
| ... | ... | @@ -5412,7 +5484,7 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5412 | 5484 | } else { |
| 5413 | 5485 | try f.writeCValueDeref(writer, operand); |
| 5414 | 5486 | try writer.writeAll(".is_null = "); |
| 5415 | | try f.object.dg.renderValue(writer, Type.bool, Value.false, .Initializer); |
| 5487 | try f.object.dg.renderValue(writer, Value.false, .Initializer); |
| 5416 | 5488 | try writer.writeAll(";\n"); |
| 5417 | 5489 | |
| 5418 | 5490 | if (f.liveness.isUnused(inst)) { |
| ... | ... | @@ -5432,50 +5504,50 @@ fn fieldLocation( |
| 5432 | 5504 | container_ptr_ty: Type, |
| 5433 | 5505 | field_ptr_ty: Type, |
| 5434 | 5506 | field_index: u32, |
| 5435 | | mod: *Module, |
| 5507 | zcu: *Zcu, |
| 5436 | 5508 | ) union(enum) { |
| 5437 | 5509 | begin: void, |
| 5438 | 5510 | field: CValue, |
| 5439 | 5511 | byte_offset: u32, |
| 5440 | 5512 | end: void, |
| 5441 | 5513 | } { |
| 5442 | | const ip = &mod.intern_pool; |
| 5443 | | const container_ty = container_ptr_ty.childType(mod); |
| 5444 | | return switch (container_ty.zigTypeTag(mod)) { |
| 5514 | const ip = &zcu.intern_pool; |
| 5515 | const container_ty = container_ptr_ty.childType(zcu); |
| 5516 | return switch (container_ty.zigTypeTag(zcu)) { |
| 5445 | 5517 | .Struct => blk: { |
| 5446 | | if (mod.typeToPackedStruct(container_ty)) |struct_type| { |
| 5447 | | if (field_ptr_ty.ptrInfo(mod).packed_offset.host_size == 0) |
| 5448 | | break :blk .{ .byte_offset = @divExact(mod.structPackedFieldBitOffset(struct_type, field_index) + container_ptr_ty.ptrInfo(mod).packed_offset.bit_offset, 8) } |
| 5518 | if (zcu.typeToPackedStruct(container_ty)) |struct_type| { |
| 5519 | if (field_ptr_ty.ptrInfo(zcu).packed_offset.host_size == 0) |
| 5520 | break :blk .{ .byte_offset = @divExact(zcu.structPackedFieldBitOffset(struct_type, field_index) + container_ptr_ty.ptrInfo(zcu).packed_offset.bit_offset, 8) } |
| 5449 | 5521 | else |
| 5450 | 5522 | break :blk .begin; |
| 5451 | 5523 | } |
| 5452 | 5524 | |
| 5453 | | for (field_index..container_ty.structFieldCount(mod)) |next_field_index_usize| { |
| 5525 | for (field_index..container_ty.structFieldCount(zcu)) |next_field_index_usize| { |
| 5454 | 5526 | const next_field_index: u32 = @intCast(next_field_index_usize); |
| 5455 | | if (container_ty.structFieldIsComptime(next_field_index, mod)) continue; |
| 5456 | | const field_ty = container_ty.structFieldType(next_field_index, mod); |
| 5457 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 5527 | if (container_ty.structFieldIsComptime(next_field_index, zcu)) continue; |
| 5528 | const field_ty = container_ty.structFieldType(next_field_index, zcu); |
| 5529 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 5458 | 5530 | |
| 5459 | | break :blk .{ .field = if (container_ty.isSimpleTuple(mod)) |
| 5531 | break :blk .{ .field = if (container_ty.isSimpleTuple(zcu)) |
| 5460 | 5532 | .{ .field = next_field_index } |
| 5461 | 5533 | else |
| 5462 | | .{ .identifier = ip.stringToSlice(container_ty.legacyStructFieldName(next_field_index, mod)) } }; |
| 5534 | .{ .identifier = ip.stringToSlice(container_ty.legacyStructFieldName(next_field_index, zcu)) } }; |
| 5463 | 5535 | } |
| 5464 | | break :blk if (container_ty.hasRuntimeBitsIgnoreComptime(mod)) .end else .begin; |
| 5536 | break :blk if (container_ty.hasRuntimeBitsIgnoreComptime(zcu)) .end else .begin; |
| 5465 | 5537 | }, |
| 5466 | 5538 | .Union => { |
| 5467 | | const union_obj = mod.typeToUnion(container_ty).?; |
| 5539 | const union_obj = zcu.typeToUnion(container_ty).?; |
| 5468 | 5540 | return switch (union_obj.getLayout(ip)) { |
| 5469 | 5541 | .auto, .@"extern" => { |
| 5470 | 5542 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 5471 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) |
| 5472 | | return if (container_ty.unionTagTypeSafety(mod) != null and |
| 5473 | | !container_ty.unionHasAllZeroBitFieldTypes(mod)) |
| 5543 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) |
| 5544 | return if (container_ty.unionTagTypeSafety(zcu) != null and |
| 5545 | !container_ty.unionHasAllZeroBitFieldTypes(zcu)) |
| 5474 | 5546 | .{ .field = .{ .identifier = "payload" } } |
| 5475 | 5547 | else |
| 5476 | 5548 | .begin; |
| 5477 | 5549 | const field_name = union_obj.loadTagType(ip).names.get(ip)[field_index]; |
| 5478 | | return .{ .field = if (container_ty.unionTagTypeSafety(mod)) |_| |
| 5550 | return .{ .field = if (container_ty.unionTagTypeSafety(zcu)) |_| |
| 5479 | 5551 | .{ .payload_identifier = ip.stringToSlice(field_name) } |
| 5480 | 5552 | else |
| 5481 | 5553 | .{ .identifier = ip.stringToSlice(field_name) } }; |
| ... | ... | @@ -5483,7 +5555,7 @@ fn fieldLocation( |
| 5483 | 5555 | .@"packed" => .begin, |
| 5484 | 5556 | }; |
| 5485 | 5557 | }, |
| 5486 | | .Pointer => switch (container_ty.ptrSize(mod)) { |
| 5558 | .Pointer => switch (container_ty.ptrSize(zcu)) { |
| 5487 | 5559 | .Slice => switch (field_index) { |
| 5488 | 5560 | 0 => .{ .field = .{ .identifier = "ptr" } }, |
| 5489 | 5561 | 1 => .{ .field = .{ .identifier = "len" } }, |
| ... | ... | @@ -5515,12 +5587,12 @@ fn airStructFieldPtrIndex(f: *Function, inst: Air.Inst.Index, index: u8) !CValue |
| 5515 | 5587 | } |
| 5516 | 5588 | |
| 5517 | 5589 | fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5518 | | const mod = f.object.dg.module; |
| 5590 | const zcu = f.object.dg.zcu; |
| 5519 | 5591 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5520 | 5592 | const extra = f.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 5521 | 5593 | |
| 5522 | 5594 | const container_ptr_ty = f.typeOfIndex(inst); |
| 5523 | | const container_ty = container_ptr_ty.childType(mod); |
| 5595 | const container_ty = container_ptr_ty.childType(zcu); |
| 5524 | 5596 | |
| 5525 | 5597 | const field_ptr_ty = f.typeOf(extra.field_ptr); |
| 5526 | 5598 | const field_ptr_val = try f.resolveInst(extra.field_ptr); |
| ... | ... | @@ -5533,10 +5605,10 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5533 | 5605 | try f.renderType(writer, container_ptr_ty); |
| 5534 | 5606 | try writer.writeByte(')'); |
| 5535 | 5607 | |
| 5536 | | switch (fieldLocation(container_ptr_ty, field_ptr_ty, extra.field_index, mod)) { |
| 5608 | switch (fieldLocation(container_ptr_ty, field_ptr_ty, extra.field_index, zcu)) { |
| 5537 | 5609 | .begin => try f.writeCValue(writer, field_ptr_val, .Initializer), |
| 5538 | 5610 | .field => |field| { |
| 5539 | | const u8_ptr_ty = try mod.adjustPtrTypeChild(field_ptr_ty, Type.u8); |
| 5611 | const u8_ptr_ty = try zcu.adjustPtrTypeChild(field_ptr_ty, Type.u8); |
| 5540 | 5612 | |
| 5541 | 5613 | try writer.writeAll("(("); |
| 5542 | 5614 | try f.renderType(writer, u8_ptr_ty); |
| ... | ... | @@ -5549,19 +5621,19 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5549 | 5621 | try writer.writeAll("))"); |
| 5550 | 5622 | }, |
| 5551 | 5623 | .byte_offset => |byte_offset| { |
| 5552 | | const u8_ptr_ty = try mod.adjustPtrTypeChild(field_ptr_ty, Type.u8); |
| 5553 | | |
| 5554 | | const byte_offset_val = try mod.intValue(Type.usize, byte_offset); |
| 5624 | const u8_ptr_ty = try zcu.adjustPtrTypeChild(field_ptr_ty, Type.u8); |
| 5555 | 5625 | |
| 5556 | 5626 | try writer.writeAll("(("); |
| 5557 | 5627 | try f.renderType(writer, u8_ptr_ty); |
| 5558 | 5628 | try writer.writeByte(')'); |
| 5559 | 5629 | try f.writeCValue(writer, field_ptr_val, .Other); |
| 5560 | | try writer.print(" - {})", .{try f.fmtIntLiteral(Type.usize, byte_offset_val)}); |
| 5630 | try writer.print(" - {})", .{ |
| 5631 | try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)), |
| 5632 | }); |
| 5561 | 5633 | }, |
| 5562 | 5634 | .end => { |
| 5563 | 5635 | try f.writeCValue(writer, field_ptr_val, .Other); |
| 5564 | | try writer.print(" - {}", .{try f.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 1))}); |
| 5636 | try writer.print(" - {}", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 1))}); |
| 5565 | 5637 | }, |
| 5566 | 5638 | } |
| 5567 | 5639 | |
| ... | ... | @@ -5576,8 +5648,8 @@ fn fieldPtr( |
| 5576 | 5648 | container_ptr_val: CValue, |
| 5577 | 5649 | field_index: u32, |
| 5578 | 5650 | ) !CValue { |
| 5579 | | const mod = f.object.dg.module; |
| 5580 | | const container_ty = container_ptr_ty.childType(mod); |
| 5651 | const zcu = f.object.dg.zcu; |
| 5652 | const container_ty = container_ptr_ty.childType(zcu); |
| 5581 | 5653 | const field_ptr_ty = f.typeOfIndex(inst); |
| 5582 | 5654 | |
| 5583 | 5655 | // Ensure complete type definition is visible before accessing fields. |
| ... | ... | @@ -5590,27 +5662,27 @@ fn fieldPtr( |
| 5590 | 5662 | try f.renderType(writer, field_ptr_ty); |
| 5591 | 5663 | try writer.writeByte(')'); |
| 5592 | 5664 | |
| 5593 | | switch (fieldLocation(container_ptr_ty, field_ptr_ty, field_index, mod)) { |
| 5665 | switch (fieldLocation(container_ptr_ty, field_ptr_ty, field_index, zcu)) { |
| 5594 | 5666 | .begin => try f.writeCValue(writer, container_ptr_val, .Initializer), |
| 5595 | 5667 | .field => |field| { |
| 5596 | 5668 | try writer.writeByte('&'); |
| 5597 | 5669 | try f.writeCValueDerefMember(writer, container_ptr_val, field); |
| 5598 | 5670 | }, |
| 5599 | 5671 | .byte_offset => |byte_offset| { |
| 5600 | | const u8_ptr_ty = try mod.adjustPtrTypeChild(field_ptr_ty, Type.u8); |
| 5601 | | |
| 5602 | | const byte_offset_val = try mod.intValue(Type.usize, byte_offset); |
| 5672 | const u8_ptr_ty = try zcu.adjustPtrTypeChild(field_ptr_ty, Type.u8); |
| 5603 | 5673 | |
| 5604 | 5674 | try writer.writeAll("(("); |
| 5605 | 5675 | try f.renderType(writer, u8_ptr_ty); |
| 5606 | 5676 | try writer.writeByte(')'); |
| 5607 | 5677 | try f.writeCValue(writer, container_ptr_val, .Other); |
| 5608 | | try writer.print(" + {})", .{try f.fmtIntLiteral(Type.usize, byte_offset_val)}); |
| 5678 | try writer.print(" + {})", .{ |
| 5679 | try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)), |
| 5680 | }); |
| 5609 | 5681 | }, |
| 5610 | 5682 | .end => { |
| 5611 | 5683 | try writer.writeByte('('); |
| 5612 | 5684 | try f.writeCValue(writer, container_ptr_val, .Other); |
| 5613 | | try writer.print(" + {})", .{try f.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 1))}); |
| 5685 | try writer.print(" + {})", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 1))}); |
| 5614 | 5686 | }, |
| 5615 | 5687 | } |
| 5616 | 5688 | |
| ... | ... | @@ -5619,13 +5691,13 @@ fn fieldPtr( |
| 5619 | 5691 | } |
| 5620 | 5692 | |
| 5621 | 5693 | fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5622 | | const mod = f.object.dg.module; |
| 5623 | | const ip = &mod.intern_pool; |
| 5694 | const zcu = f.object.dg.zcu; |
| 5695 | const ip = &zcu.intern_pool; |
| 5624 | 5696 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5625 | 5697 | const extra = f.air.extraData(Air.StructField, ty_pl.payload).data; |
| 5626 | 5698 | |
| 5627 | 5699 | const inst_ty = f.typeOfIndex(inst); |
| 5628 | | if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 5700 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 5629 | 5701 | try reap(f, inst, &.{extra.struct_operand}); |
| 5630 | 5702 | return .none; |
| 5631 | 5703 | } |
| ... | ... | @@ -5638,26 +5710,25 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5638 | 5710 | // Ensure complete type definition is visible before accessing fields. |
| 5639 | 5711 | _ = try f.typeToIndex(struct_ty, .complete); |
| 5640 | 5712 | |
| 5641 | | const field_name: CValue = switch (mod.intern_pool.indexToKey(struct_ty.ip_index)) { |
| 5642 | | .struct_type => switch (struct_ty.containerLayout(mod)) { |
| 5643 | | .auto, .@"extern" => if (struct_ty.isSimpleTuple(mod)) |
| 5713 | const field_name: CValue = switch (zcu.intern_pool.indexToKey(struct_ty.toIntern())) { |
| 5714 | .struct_type => switch (struct_ty.containerLayout(zcu)) { |
| 5715 | .auto, .@"extern" => if (struct_ty.isSimpleTuple(zcu)) |
| 5644 | 5716 | .{ .field = extra.field_index } |
| 5645 | 5717 | else |
| 5646 | | .{ .identifier = ip.stringToSlice(struct_ty.legacyStructFieldName(extra.field_index, mod)) }, |
| 5718 | .{ .identifier = ip.stringToSlice(struct_ty.legacyStructFieldName(extra.field_index, zcu)) }, |
| 5647 | 5719 | .@"packed" => { |
| 5648 | | const struct_type = mod.typeToStruct(struct_ty).?; |
| 5649 | | const int_info = struct_ty.intInfo(mod); |
| 5720 | const struct_type = zcu.typeToStruct(struct_ty).?; |
| 5721 | const int_info = struct_ty.intInfo(zcu); |
| 5650 | 5722 | |
| 5651 | | const bit_offset_ty = try mod.intType(.unsigned, Type.smallestUnsignedBits(int_info.bits - 1)); |
| 5723 | const bit_offset_ty = try zcu.intType(.unsigned, Type.smallestUnsignedBits(int_info.bits - 1)); |
| 5652 | 5724 | |
| 5653 | | const bit_offset = mod.structPackedFieldBitOffset(struct_type, extra.field_index); |
| 5654 | | const bit_offset_val = try mod.intValue(bit_offset_ty, bit_offset); |
| 5725 | const bit_offset = zcu.structPackedFieldBitOffset(struct_type, extra.field_index); |
| 5655 | 5726 | |
| 5656 | | const field_int_signedness = if (inst_ty.isAbiInt(mod)) |
| 5657 | | inst_ty.intInfo(mod).signedness |
| 5727 | const field_int_signedness = if (inst_ty.isAbiInt(zcu)) |
| 5728 | inst_ty.intInfo(zcu).signedness |
| 5658 | 5729 | else |
| 5659 | 5730 | .unsigned; |
| 5660 | | const field_int_ty = try mod.intType(field_int_signedness, @as(u16, @intCast(inst_ty.bitSize(mod)))); |
| 5731 | const field_int_ty = try zcu.intType(field_int_signedness, @as(u16, @intCast(inst_ty.bitSize(zcu)))); |
| 5661 | 5732 | |
| 5662 | 5733 | const temp_local = try f.allocLocal(inst, field_int_ty); |
| 5663 | 5734 | try f.writeCValue(writer, temp_local, .Other); |
| ... | ... | @@ -5668,7 +5739,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5668 | 5739 | try writer.writeByte(')'); |
| 5669 | 5740 | const cant_cast = int_info.bits > 64; |
| 5670 | 5741 | if (cant_cast) { |
| 5671 | | if (field_int_ty.bitSize(mod) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); |
| 5742 | if (field_int_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); |
| 5672 | 5743 | try writer.writeAll("zig_lo_"); |
| 5673 | 5744 | try f.object.dg.renderTypeForBuiltinFnName(writer, struct_ty); |
| 5674 | 5745 | try writer.writeByte('('); |
| ... | ... | @@ -5681,13 +5752,13 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5681 | 5752 | try f.writeCValue(writer, struct_byval, .Other); |
| 5682 | 5753 | if (bit_offset > 0) { |
| 5683 | 5754 | try writer.writeAll(", "); |
| 5684 | | try f.object.dg.renderValue(writer, bit_offset_ty, bit_offset_val, .FunctionArgument); |
| 5755 | try f.object.dg.renderValue(writer, try zcu.intValue(bit_offset_ty, bit_offset), .FunctionArgument); |
| 5685 | 5756 | try writer.writeByte(')'); |
| 5686 | 5757 | } |
| 5687 | 5758 | if (cant_cast) try writer.writeByte(')'); |
| 5688 | 5759 | try f.object.dg.renderBuiltinInfo(writer, field_int_ty, .bits); |
| 5689 | 5760 | try writer.writeAll(");\n"); |
| 5690 | | if (inst_ty.eql(field_int_ty, f.object.dg.module)) return temp_local; |
| 5761 | if (inst_ty.eql(field_int_ty, f.object.dg.zcu)) return temp_local; |
| 5691 | 5762 | |
| 5692 | 5763 | const local = try f.allocLocal(inst, inst_ty); |
| 5693 | 5764 | try writer.writeAll("memcpy("); |
| ... | ... | @@ -5705,7 +5776,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5705 | 5776 | .anon_struct_type => |anon_struct_type| if (anon_struct_type.names.len == 0) |
| 5706 | 5777 | .{ .field = extra.field_index } |
| 5707 | 5778 | else |
| 5708 | | .{ .identifier = ip.stringToSlice(struct_ty.legacyStructFieldName(extra.field_index, mod)) }, |
| 5779 | .{ .identifier = ip.stringToSlice(struct_ty.legacyStructFieldName(extra.field_index, zcu)) }, |
| 5709 | 5780 | |
| 5710 | 5781 | .union_type => field_name: { |
| 5711 | 5782 | const union_obj = ip.loadUnionType(struct_ty.toIntern()); |
| ... | ... | @@ -5757,7 +5828,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5757 | 5828 | /// *(E!T) -> E |
| 5758 | 5829 | /// Note that the result is never a pointer. |
| 5759 | 5830 | fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5760 | | const mod = f.object.dg.module; |
| 5831 | const zcu = f.object.dg.zcu; |
| 5761 | 5832 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5762 | 5833 | |
| 5763 | 5834 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -5765,13 +5836,13 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5765 | 5836 | const operand_ty = f.typeOf(ty_op.operand); |
| 5766 | 5837 | try reap(f, inst, &.{ty_op.operand}); |
| 5767 | 5838 | |
| 5768 | | const operand_is_ptr = operand_ty.zigTypeTag(mod) == .Pointer; |
| 5769 | | const error_union_ty = if (operand_is_ptr) operand_ty.childType(mod) else operand_ty; |
| 5770 | | const error_ty = error_union_ty.errorUnionSet(mod); |
| 5771 | | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 5839 | const operand_is_ptr = operand_ty.zigTypeTag(zcu) == .Pointer; |
| 5840 | const error_union_ty = if (operand_is_ptr) operand_ty.childType(zcu) else operand_ty; |
| 5841 | const error_ty = error_union_ty.errorUnionSet(zcu); |
| 5842 | const payload_ty = error_union_ty.errorUnionPayload(zcu); |
| 5772 | 5843 | const local = try f.allocLocal(inst, inst_ty); |
| 5773 | 5844 | |
| 5774 | | if (!payload_ty.hasRuntimeBits(mod) and operand == .local and operand.local == local.new_local) { |
| 5845 | if (!payload_ty.hasRuntimeBits(zcu) and operand == .local and operand.local == local.new_local) { |
| 5775 | 5846 | // The store will be 'x = x'; elide it. |
| 5776 | 5847 | return local; |
| 5777 | 5848 | } |
| ... | ... | @@ -5780,35 +5851,32 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5780 | 5851 | try f.writeCValue(writer, local, .Other); |
| 5781 | 5852 | try writer.writeAll(" = "); |
| 5782 | 5853 | |
| 5783 | | if (!payload_ty.hasRuntimeBits(mod)) { |
| 5784 | | try f.writeCValue(writer, operand, .Other); |
| 5785 | | } else { |
| 5786 | | if (!error_ty.errorSetIsEmpty(mod)) |
| 5787 | | if (operand_is_ptr) |
| 5788 | | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }) |
| 5789 | | else |
| 5790 | | try f.writeCValueMember(writer, operand, .{ .identifier = "error" }) |
| 5791 | | else { |
| 5792 | | const err_int_ty = try mod.errorIntType(); |
| 5793 | | try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Initializer); |
| 5794 | | } |
| 5795 | | } |
| 5854 | if (!payload_ty.hasRuntimeBits(zcu)) |
| 5855 | try f.writeCValue(writer, operand, .Other) |
| 5856 | else if (error_ty.errorSetIsEmpty(zcu)) |
| 5857 | try writer.print("{}", .{ |
| 5858 | try f.fmtIntLiteral(try zcu.intValue(try zcu.errorIntType(), 0)), |
| 5859 | }) |
| 5860 | else if (operand_is_ptr) |
| 5861 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }) |
| 5862 | else |
| 5863 | try f.writeCValueMember(writer, operand, .{ .identifier = "error" }); |
| 5796 | 5864 | try writer.writeAll(";\n"); |
| 5797 | 5865 | return local; |
| 5798 | 5866 | } |
| 5799 | 5867 | |
| 5800 | 5868 | fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 5801 | | const mod = f.object.dg.module; |
| 5869 | const zcu = f.object.dg.zcu; |
| 5802 | 5870 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5803 | 5871 | |
| 5804 | 5872 | const inst_ty = f.typeOfIndex(inst); |
| 5805 | 5873 | const operand = try f.resolveInst(ty_op.operand); |
| 5806 | 5874 | try reap(f, inst, &.{ty_op.operand}); |
| 5807 | 5875 | const operand_ty = f.typeOf(ty_op.operand); |
| 5808 | | const error_union_ty = if (is_ptr) operand_ty.childType(mod) else operand_ty; |
| 5876 | const error_union_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; |
| 5809 | 5877 | |
| 5810 | 5878 | const writer = f.object.writer(); |
| 5811 | | if (!error_union_ty.errorUnionPayload(mod).hasRuntimeBits(mod)) { |
| 5879 | if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) { |
| 5812 | 5880 | if (!is_ptr) return .none; |
| 5813 | 5881 | |
| 5814 | 5882 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -5834,11 +5902,11 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu |
| 5834 | 5902 | } |
| 5835 | 5903 | |
| 5836 | 5904 | fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5837 | | const mod = f.object.dg.module; |
| 5905 | const zcu = f.object.dg.zcu; |
| 5838 | 5906 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5839 | 5907 | |
| 5840 | 5908 | const inst_ty = f.typeOfIndex(inst); |
| 5841 | | const repr_is_payload = inst_ty.optionalReprIsPayload(mod); |
| 5909 | const repr_is_payload = inst_ty.optionalReprIsPayload(zcu); |
| 5842 | 5910 | const payload_ty = f.typeOf(ty_op.operand); |
| 5843 | 5911 | const payload = try f.resolveInst(ty_op.operand); |
| 5844 | 5912 | try reap(f, inst, &.{ty_op.operand}); |
| ... | ... | @@ -5859,20 +5927,20 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5859 | 5927 | const a = try Assignment.start(f, writer, Type.bool); |
| 5860 | 5928 | try f.writeCValueMember(writer, local, .{ .identifier = "is_null" }); |
| 5861 | 5929 | try a.assign(f, writer); |
| 5862 | | try f.object.dg.renderValue(writer, Type.bool, Value.false, .Other); |
| 5930 | try f.object.dg.renderValue(writer, Value.false, .Other); |
| 5863 | 5931 | try a.end(f, writer); |
| 5864 | 5932 | } |
| 5865 | 5933 | return local; |
| 5866 | 5934 | } |
| 5867 | 5935 | |
| 5868 | 5936 | fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5869 | | const mod = f.object.dg.module; |
| 5937 | const zcu = f.object.dg.zcu; |
| 5870 | 5938 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5871 | 5939 | |
| 5872 | 5940 | const inst_ty = f.typeOfIndex(inst); |
| 5873 | | const payload_ty = inst_ty.errorUnionPayload(mod); |
| 5874 | | const repr_is_err = !payload_ty.hasRuntimeBitsIgnoreComptime(mod); |
| 5875 | | const err_ty = inst_ty.errorUnionSet(mod); |
| 5941 | const payload_ty = inst_ty.errorUnionPayload(zcu); |
| 5942 | const repr_is_err = !payload_ty.hasRuntimeBitsIgnoreComptime(zcu); |
| 5943 | const err_ty = inst_ty.errorUnionSet(zcu); |
| 5876 | 5944 | const err = try f.resolveInst(ty_op.operand); |
| 5877 | 5945 | try reap(f, inst, &.{ty_op.operand}); |
| 5878 | 5946 | |
| ... | ... | @@ -5888,7 +5956,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5888 | 5956 | const a = try Assignment.start(f, writer, payload_ty); |
| 5889 | 5957 | try f.writeCValueMember(writer, local, .{ .identifier = "payload" }); |
| 5890 | 5958 | try a.assign(f, writer); |
| 5891 | | try f.object.dg.renderValue(writer, payload_ty, Value.undef, .Other); |
| 5959 | try f.object.dg.renderUndefValue(writer, payload_ty, .Other); |
| 5892 | 5960 | try a.end(f, writer); |
| 5893 | 5961 | } |
| 5894 | 5962 | { |
| ... | ... | @@ -5905,29 +5973,25 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5905 | 5973 | } |
| 5906 | 5974 | |
| 5907 | 5975 | fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5908 | | const mod = f.object.dg.module; |
| 5976 | const zcu = f.object.dg.zcu; |
| 5909 | 5977 | const writer = f.object.writer(); |
| 5910 | 5978 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5911 | 5979 | const operand = try f.resolveInst(ty_op.operand); |
| 5912 | | const error_union_ty = f.typeOf(ty_op.operand).childType(mod); |
| 5980 | const error_union_ty = f.typeOf(ty_op.operand).childType(zcu); |
| 5913 | 5981 | |
| 5914 | | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 5915 | | const err_int_ty = try mod.errorIntType(); |
| 5982 | const payload_ty = error_union_ty.errorUnionPayload(zcu); |
| 5983 | const err_int_ty = try zcu.errorIntType(); |
| 5984 | const no_err = try zcu.intValue(err_int_ty, 0); |
| 5916 | 5985 | |
| 5917 | 5986 | // First, set the non-error value. |
| 5918 | | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 5987 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 5919 | 5988 | try f.writeCValueDeref(writer, operand); |
| 5920 | | try writer.writeAll(" = "); |
| 5921 | | try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Other); |
| 5922 | | try writer.writeAll(";\n "); |
| 5923 | | |
| 5989 | try writer.print(" = {};\n", .{try f.fmtIntLiteral(no_err)}); |
| 5924 | 5990 | return operand; |
| 5925 | 5991 | } |
| 5926 | 5992 | try reap(f, inst, &.{ty_op.operand}); |
| 5927 | 5993 | try f.writeCValueDeref(writer, operand); |
| 5928 | | try writer.writeAll(".error = "); |
| 5929 | | try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Other); |
| 5930 | | try writer.writeAll(";\n"); |
| 5994 | try writer.print(".error = {};\n", .{try f.fmtIntLiteral(no_err)}); |
| 5931 | 5995 | |
| 5932 | 5996 | // Then return the payload pointer (only if it is used) |
| 5933 | 5997 | if (f.liveness.isUnused(inst)) return .none; |
| ... | ... | @@ -5956,14 +6020,14 @@ fn airSaveErrReturnTraceIndex(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5956 | 6020 | } |
| 5957 | 6021 | |
| 5958 | 6022 | fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5959 | | const mod = f.object.dg.module; |
| 6023 | const zcu = f.object.dg.zcu; |
| 5960 | 6024 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5961 | 6025 | |
| 5962 | 6026 | const inst_ty = f.typeOfIndex(inst); |
| 5963 | | const payload_ty = inst_ty.errorUnionPayload(mod); |
| 6027 | const payload_ty = inst_ty.errorUnionPayload(zcu); |
| 5964 | 6028 | const payload = try f.resolveInst(ty_op.operand); |
| 5965 | | const repr_is_err = !payload_ty.hasRuntimeBitsIgnoreComptime(mod); |
| 5966 | | const err_ty = inst_ty.errorUnionSet(mod); |
| 6029 | const repr_is_err = !payload_ty.hasRuntimeBitsIgnoreComptime(zcu); |
| 6030 | const err_ty = inst_ty.errorUnionSet(zcu); |
| 5967 | 6031 | try reap(f, inst, &.{ty_op.operand}); |
| 5968 | 6032 | |
| 5969 | 6033 | const writer = f.object.writer(); |
| ... | ... | @@ -5982,15 +6046,14 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5982 | 6046 | else |
| 5983 | 6047 | try f.writeCValueMember(writer, local, .{ .identifier = "error" }); |
| 5984 | 6048 | try a.assign(f, writer); |
| 5985 | | const err_int_ty = try mod.errorIntType(); |
| 5986 | | try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Other); |
| 6049 | try f.object.dg.renderValue(writer, try zcu.intValue(try zcu.errorIntType(), 0), .Other); |
| 5987 | 6050 | try a.end(f, writer); |
| 5988 | 6051 | } |
| 5989 | 6052 | return local; |
| 5990 | 6053 | } |
| 5991 | 6054 | |
| 5992 | 6055 | fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !CValue { |
| 5993 | | const mod = f.object.dg.module; |
| 6056 | const zcu = f.object.dg.zcu; |
| 5994 | 6057 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5995 | 6058 | |
| 5996 | 6059 | const writer = f.object.writer(); |
| ... | ... | @@ -5998,16 +6061,16 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const |
| 5998 | 6061 | try reap(f, inst, &.{un_op}); |
| 5999 | 6062 | const operand_ty = f.typeOf(un_op); |
| 6000 | 6063 | const local = try f.allocLocal(inst, Type.bool); |
| 6001 | | const err_union_ty = if (is_ptr) operand_ty.childType(mod) else operand_ty; |
| 6002 | | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 6003 | | const error_ty = err_union_ty.errorUnionSet(mod); |
| 6064 | const err_union_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; |
| 6065 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 6066 | const error_ty = err_union_ty.errorUnionSet(zcu); |
| 6004 | 6067 | |
| 6068 | const a = try Assignment.start(f, writer, Type.bool); |
| 6005 | 6069 | try f.writeCValue(writer, local, .Other); |
| 6006 | | try writer.writeAll(" = "); |
| 6007 | | |
| 6008 | | const err_int_ty = try mod.errorIntType(); |
| 6009 | | if (!error_ty.errorSetIsEmpty(mod)) |
| 6010 | | if (payload_ty.hasRuntimeBits(mod)) |
| 6070 | try a.assign(f, writer); |
| 6071 | const err_int_ty = try zcu.errorIntType(); |
| 6072 | if (!error_ty.errorSetIsEmpty(zcu)) |
| 6073 | if (payload_ty.hasRuntimeBits(zcu)) |
| 6011 | 6074 | if (is_ptr) |
| 6012 | 6075 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }) |
| 6013 | 6076 | else |
| ... | ... | @@ -6015,63 +6078,84 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const |
| 6015 | 6078 | else |
| 6016 | 6079 | try f.writeCValue(writer, operand, .Other) |
| 6017 | 6080 | else |
| 6018 | | try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Other); |
| 6081 | try f.object.dg.renderValue(writer, try zcu.intValue(err_int_ty, 0), .Other); |
| 6019 | 6082 | try writer.writeByte(' '); |
| 6020 | 6083 | try writer.writeAll(operator); |
| 6021 | 6084 | try writer.writeByte(' '); |
| 6022 | | try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Other); |
| 6023 | | try writer.writeAll(";\n"); |
| 6085 | try f.object.dg.renderValue(writer, try zcu.intValue(err_int_ty, 0), .Other); |
| 6086 | try a.end(f, writer); |
| 6024 | 6087 | return local; |
| 6025 | 6088 | } |
| 6026 | 6089 | |
| 6027 | 6090 | fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6028 | | const mod = f.object.dg.module; |
| 6091 | const zcu = f.object.dg.zcu; |
| 6029 | 6092 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6030 | 6093 | |
| 6031 | 6094 | const operand = try f.resolveInst(ty_op.operand); |
| 6032 | 6095 | try reap(f, inst, &.{ty_op.operand}); |
| 6033 | 6096 | const inst_ty = f.typeOfIndex(inst); |
| 6097 | const ptr_ty = inst_ty.slicePtrFieldType(zcu); |
| 6034 | 6098 | const writer = f.object.writer(); |
| 6035 | 6099 | const local = try f.allocLocal(inst, inst_ty); |
| 6036 | | const array_ty = f.typeOf(ty_op.operand).childType(mod); |
| 6037 | | |
| 6038 | | try f.writeCValueMember(writer, local, .{ .identifier = "ptr" }); |
| 6039 | | try writer.writeAll(" = "); |
| 6040 | | // Unfortunately, C does not support any equivalent to |
| 6041 | | // &(*(void *)p)[0], although LLVM does via GetElementPtr |
| 6042 | | if (operand == .undef) { |
| 6043 | | try f.writeCValue(writer, .{ .undef = inst_ty.slicePtrFieldType(mod) }, .Initializer); |
| 6044 | | } else if (array_ty.hasRuntimeBitsIgnoreComptime(mod)) { |
| 6045 | | try writer.writeAll("&("); |
| 6046 | | try f.writeCValueDeref(writer, operand); |
| 6047 | | try writer.print(")[{}]", .{try f.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 0))}); |
| 6048 | | } else try f.writeCValue(writer, operand, .Initializer); |
| 6049 | | try writer.writeAll("; "); |
| 6100 | const operand_ty = f.typeOf(ty_op.operand); |
| 6101 | const array_ty = operand_ty.childType(zcu); |
| 6050 | 6102 | |
| 6051 | | const len_val = try mod.intValue(Type.usize, array_ty.arrayLen(mod)); |
| 6052 | | try f.writeCValueMember(writer, local, .{ .identifier = "len" }); |
| 6053 | | try writer.print(" = {};\n", .{try f.fmtIntLiteral(Type.usize, len_val)}); |
| 6103 | { |
| 6104 | const a = try Assignment.start(f, writer, ptr_ty); |
| 6105 | try f.writeCValueMember(writer, local, .{ .identifier = "ptr" }); |
| 6106 | try a.assign(f, writer); |
| 6107 | if (operand == .undef) { |
| 6108 | try f.writeCValue(writer, .{ .undef = inst_ty.slicePtrFieldType(zcu) }, .Initializer); |
| 6109 | } else { |
| 6110 | const ptr_cty = try f.typeToIndex(ptr_ty, .complete); |
| 6111 | const ptr_child_cty = f.indexToCType(ptr_cty).cast(CType.Payload.Child).?.data; |
| 6112 | const elem_ty = array_ty.childType(zcu); |
| 6113 | const elem_cty = try f.typeToIndex(elem_ty, .complete); |
| 6114 | if (ptr_child_cty != elem_cty) { |
| 6115 | try writer.writeByte('('); |
| 6116 | try f.renderCType(writer, ptr_cty); |
| 6117 | try writer.writeByte(')'); |
| 6118 | } |
| 6119 | const operand_cty = try f.typeToCType(operand_ty, .complete); |
| 6120 | const operand_child_cty = operand_cty.cast(CType.Payload.Child).?.data; |
| 6121 | if (f.indexToCType(operand_child_cty).tag() == .array) { |
| 6122 | try writer.writeByte('&'); |
| 6123 | try f.writeCValueDeref(writer, operand); |
| 6124 | try writer.print("[{}]", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 0))}); |
| 6125 | } else try f.writeCValue(writer, operand, .Initializer); |
| 6126 | } |
| 6127 | try a.end(f, writer); |
| 6128 | } |
| 6129 | { |
| 6130 | const a = try Assignment.start(f, writer, Type.usize); |
| 6131 | try f.writeCValueMember(writer, local, .{ .identifier = "len" }); |
| 6132 | try a.assign(f, writer); |
| 6133 | try writer.print("{}", .{ |
| 6134 | try f.fmtIntLiteral(try zcu.intValue(Type.usize, array_ty.arrayLen(zcu))), |
| 6135 | }); |
| 6136 | try a.end(f, writer); |
| 6137 | } |
| 6054 | 6138 | |
| 6055 | 6139 | return local; |
| 6056 | 6140 | } |
| 6057 | 6141 | |
| 6058 | 6142 | fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6059 | | const mod = f.object.dg.module; |
| 6143 | const zcu = f.object.dg.zcu; |
| 6060 | 6144 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6061 | 6145 | |
| 6062 | 6146 | const inst_ty = f.typeOfIndex(inst); |
| 6063 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 6147 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 6064 | 6148 | const operand = try f.resolveInst(ty_op.operand); |
| 6065 | 6149 | try reap(f, inst, &.{ty_op.operand}); |
| 6066 | 6150 | const operand_ty = f.typeOf(ty_op.operand); |
| 6067 | | const scalar_ty = operand_ty.scalarType(mod); |
| 6068 | | const target = f.object.dg.module.getTarget(); |
| 6151 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6152 | const target = &f.object.dg.mod.resolved_target.result; |
| 6069 | 6153 | const operation = if (inst_scalar_ty.isRuntimeFloat() and scalar_ty.isRuntimeFloat()) |
| 6070 | | if (inst_scalar_ty.floatBits(target) < scalar_ty.floatBits(target)) "trunc" else "extend" |
| 6071 | | else if (inst_scalar_ty.isInt(mod) and scalar_ty.isRuntimeFloat()) |
| 6072 | | if (inst_scalar_ty.isSignedInt(mod)) "fix" else "fixuns" |
| 6073 | | else if (inst_scalar_ty.isRuntimeFloat() and scalar_ty.isInt(mod)) |
| 6074 | | if (scalar_ty.isSignedInt(mod)) "float" else "floatun" |
| 6154 | if (inst_scalar_ty.floatBits(target.*) < scalar_ty.floatBits(target.*)) "trunc" else "extend" |
| 6155 | else if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) |
| 6156 | if (inst_scalar_ty.isSignedInt(zcu)) "fix" else "fixuns" |
| 6157 | else if (inst_scalar_ty.isRuntimeFloat() and scalar_ty.isInt(zcu)) |
| 6158 | if (scalar_ty.isSignedInt(zcu)) "float" else "floatun" |
| 6075 | 6159 | else |
| 6076 | 6160 | unreachable; |
| 6077 | 6161 | |
| ... | ... | @@ -6082,20 +6166,20 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6082 | 6166 | try f.writeCValue(writer, local, .Other); |
| 6083 | 6167 | try v.elem(f, writer); |
| 6084 | 6168 | try a.assign(f, writer); |
| 6085 | | if (inst_scalar_ty.isInt(mod) and scalar_ty.isRuntimeFloat()) { |
| 6169 | if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) { |
| 6086 | 6170 | try writer.writeAll("zig_wrap_"); |
| 6087 | 6171 | try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty); |
| 6088 | 6172 | try writer.writeByte('('); |
| 6089 | 6173 | } |
| 6090 | 6174 | try writer.writeAll("zig_"); |
| 6091 | 6175 | try writer.writeAll(operation); |
| 6092 | | try writer.writeAll(compilerRtAbbrev(scalar_ty, mod)); |
| 6093 | | try writer.writeAll(compilerRtAbbrev(inst_scalar_ty, mod)); |
| 6176 | try writer.writeAll(compilerRtAbbrev(scalar_ty, zcu, target.*)); |
| 6177 | try writer.writeAll(compilerRtAbbrev(inst_scalar_ty, zcu, target.*)); |
| 6094 | 6178 | try writer.writeByte('('); |
| 6095 | 6179 | try f.writeCValue(writer, operand, .FunctionArgument); |
| 6096 | 6180 | try v.elem(f, writer); |
| 6097 | 6181 | try writer.writeByte(')'); |
| 6098 | | if (inst_scalar_ty.isInt(mod) and scalar_ty.isRuntimeFloat()) { |
| 6182 | if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) { |
| 6099 | 6183 | try f.object.dg.renderBuiltinInfo(writer, inst_scalar_ty, .bits); |
| 6100 | 6184 | try writer.writeByte(')'); |
| 6101 | 6185 | } |
| ... | ... | @@ -6106,7 +6190,7 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6106 | 6190 | } |
| 6107 | 6191 | |
| 6108 | 6192 | fn airIntFromPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6109 | | const mod = f.object.dg.module; |
| 6193 | const zcu = f.object.dg.zcu; |
| 6110 | 6194 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6111 | 6195 | |
| 6112 | 6196 | const operand = try f.resolveInst(un_op); |
| ... | ... | @@ -6120,7 +6204,7 @@ fn airIntFromPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6120 | 6204 | try writer.writeAll(" = ("); |
| 6121 | 6205 | try f.renderType(writer, inst_ty); |
| 6122 | 6206 | try writer.writeByte(')'); |
| 6123 | | if (operand_ty.isSlice(mod)) { |
| 6207 | if (operand_ty.isSlice(zcu)) { |
| 6124 | 6208 | try f.writeCValueMember(writer, operand, .{ .identifier = "ptr" }); |
| 6125 | 6209 | } else { |
| 6126 | 6210 | try f.writeCValue(writer, operand, .Other); |
| ... | ... | @@ -6135,15 +6219,15 @@ fn airUnBuiltinCall( |
| 6135 | 6219 | operation: []const u8, |
| 6136 | 6220 | info: BuiltinInfo, |
| 6137 | 6221 | ) !CValue { |
| 6138 | | const mod = f.object.dg.module; |
| 6222 | const zcu = f.object.dg.zcu; |
| 6139 | 6223 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6140 | 6224 | |
| 6141 | 6225 | const operand = try f.resolveInst(ty_op.operand); |
| 6142 | 6226 | try reap(f, inst, &.{ty_op.operand}); |
| 6143 | 6227 | const inst_ty = f.typeOfIndex(inst); |
| 6144 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 6228 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 6145 | 6229 | const operand_ty = f.typeOf(ty_op.operand); |
| 6146 | | const scalar_ty = operand_ty.scalarType(mod); |
| 6230 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6147 | 6231 | |
| 6148 | 6232 | const inst_scalar_cty = try f.typeToCType(inst_scalar_ty, .complete); |
| 6149 | 6233 | const ref_ret = inst_scalar_cty.tag() == .array; |
| ... | ... | @@ -6179,7 +6263,7 @@ fn airBinBuiltinCall( |
| 6179 | 6263 | operation: []const u8, |
| 6180 | 6264 | info: BuiltinInfo, |
| 6181 | 6265 | ) !CValue { |
| 6182 | | const mod = f.object.dg.module; |
| 6266 | const zcu = f.object.dg.zcu; |
| 6183 | 6267 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6184 | 6268 | |
| 6185 | 6269 | const operand_ty = f.typeOf(bin_op.lhs); |
| ... | ... | @@ -6191,8 +6275,8 @@ fn airBinBuiltinCall( |
| 6191 | 6275 | if (!is_big) try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6192 | 6276 | |
| 6193 | 6277 | const inst_ty = f.typeOfIndex(inst); |
| 6194 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 6195 | | const scalar_ty = operand_ty.scalarType(mod); |
| 6278 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 6279 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6196 | 6280 | |
| 6197 | 6281 | const inst_scalar_cty = try f.typeToCType(inst_scalar_ty, .complete); |
| 6198 | 6282 | const ref_ret = inst_scalar_cty.tag() == .array; |
| ... | ... | @@ -6234,15 +6318,15 @@ fn airCmpBuiltinCall( |
| 6234 | 6318 | operation: enum { cmp, operator }, |
| 6235 | 6319 | info: BuiltinInfo, |
| 6236 | 6320 | ) !CValue { |
| 6237 | | const mod = f.object.dg.module; |
| 6321 | const zcu = f.object.dg.zcu; |
| 6238 | 6322 | const lhs = try f.resolveInst(data.lhs); |
| 6239 | 6323 | const rhs = try f.resolveInst(data.rhs); |
| 6240 | 6324 | try reap(f, inst, &.{ data.lhs, data.rhs }); |
| 6241 | 6325 | |
| 6242 | 6326 | const inst_ty = f.typeOfIndex(inst); |
| 6243 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 6327 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 6244 | 6328 | const operand_ty = f.typeOf(data.lhs); |
| 6245 | | const scalar_ty = operand_ty.scalarType(mod); |
| 6329 | const scalar_ty = operand_ty.scalarType(zcu); |
| 6246 | 6330 | |
| 6247 | 6331 | const inst_scalar_cty = try f.typeToCType(inst_scalar_ty, .complete); |
| 6248 | 6332 | const ref_ret = inst_scalar_cty.tag() == .array; |
| ... | ... | @@ -6275,7 +6359,7 @@ fn airCmpBuiltinCall( |
| 6275 | 6359 | try writer.writeByte(')'); |
| 6276 | 6360 | if (!ref_ret) try writer.print("{s}{}", .{ |
| 6277 | 6361 | compareOperatorC(operator), |
| 6278 | | try f.fmtIntLiteral(Type.i32, try mod.intValue(Type.i32, 0)), |
| 6362 | try f.fmtIntLiteral(try zcu.intValue(Type.i32, 0)), |
| 6279 | 6363 | }); |
| 6280 | 6364 | try writer.writeAll(";\n"); |
| 6281 | 6365 | try v.end(f, inst, writer); |
| ... | ... | @@ -6284,7 +6368,7 @@ fn airCmpBuiltinCall( |
| 6284 | 6368 | } |
| 6285 | 6369 | |
| 6286 | 6370 | fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue { |
| 6287 | | const mod = f.object.dg.module; |
| 6371 | const zcu = f.object.dg.zcu; |
| 6288 | 6372 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6289 | 6373 | const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| 6290 | 6374 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -6292,19 +6376,19 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6292 | 6376 | const expected_value = try f.resolveInst(extra.expected_value); |
| 6293 | 6377 | const new_value = try f.resolveInst(extra.new_value); |
| 6294 | 6378 | const ptr_ty = f.typeOf(extra.ptr); |
| 6295 | | const ty = ptr_ty.childType(mod); |
| 6379 | const ty = ptr_ty.childType(zcu); |
| 6296 | 6380 | |
| 6297 | 6381 | const writer = f.object.writer(); |
| 6298 | 6382 | const new_value_mat = try Materialize.start(f, inst, writer, ty, new_value); |
| 6299 | 6383 | try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value }); |
| 6300 | 6384 | |
| 6301 | 6385 | const repr_ty = if (ty.isRuntimeFloat()) |
| 6302 | | mod.intType(.unsigned, @as(u16, @intCast(ty.abiSize(mod) * 8))) catch unreachable |
| 6386 | zcu.intType(.unsigned, @as(u16, @intCast(ty.abiSize(zcu) * 8))) catch unreachable |
| 6303 | 6387 | else |
| 6304 | 6388 | ty; |
| 6305 | 6389 | |
| 6306 | 6390 | const local = try f.allocLocal(inst, inst_ty); |
| 6307 | | if (inst_ty.isPtrLikeOptional(mod)) { |
| 6391 | if (inst_ty.isPtrLikeOptional(zcu)) { |
| 6308 | 6392 | { |
| 6309 | 6393 | const a = try Assignment.start(f, writer, ty); |
| 6310 | 6394 | try f.writeCValue(writer, local, .Other); |
| ... | ... | @@ -6317,7 +6401,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6317 | 6401 | try writer.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor}); |
| 6318 | 6402 | try f.renderType(writer, ty); |
| 6319 | 6403 | try writer.writeByte(')'); |
| 6320 | | if (ptr_ty.isVolatilePtr(mod)) try writer.writeAll(" volatile"); |
| 6404 | if (ptr_ty.isVolatilePtr(zcu)) try writer.writeAll(" volatile"); |
| 6321 | 6405 | try writer.writeAll(" *)"); |
| 6322 | 6406 | try f.writeCValue(writer, ptr, .Other); |
| 6323 | 6407 | try writer.writeAll(", "); |
| ... | ... | @@ -6331,7 +6415,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6331 | 6415 | try writer.writeAll(", "); |
| 6332 | 6416 | try f.object.dg.renderTypeForBuiltinFnName(writer, ty); |
| 6333 | 6417 | try writer.writeAll(", "); |
| 6334 | | try f.object.dg.renderType(writer, repr_ty); |
| 6418 | try f.renderType(writer, repr_ty); |
| 6335 | 6419 | try writer.writeByte(')'); |
| 6336 | 6420 | try writer.writeAll(") {\n"); |
| 6337 | 6421 | f.object.indent_writer.pushIndent(); |
| ... | ... | @@ -6359,7 +6443,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6359 | 6443 | try writer.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor}); |
| 6360 | 6444 | try f.renderType(writer, ty); |
| 6361 | 6445 | try writer.writeByte(')'); |
| 6362 | | if (ptr_ty.isVolatilePtr(mod)) try writer.writeAll(" volatile"); |
| 6446 | if (ptr_ty.isVolatilePtr(zcu)) try writer.writeAll(" volatile"); |
| 6363 | 6447 | try writer.writeAll(" *)"); |
| 6364 | 6448 | try f.writeCValue(writer, ptr, .Other); |
| 6365 | 6449 | try writer.writeAll(", "); |
| ... | ... | @@ -6373,7 +6457,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6373 | 6457 | try writer.writeAll(", "); |
| 6374 | 6458 | try f.object.dg.renderTypeForBuiltinFnName(writer, ty); |
| 6375 | 6459 | try writer.writeAll(", "); |
| 6376 | | try f.object.dg.renderType(writer, repr_ty); |
| 6460 | try f.renderType(writer, repr_ty); |
| 6377 | 6461 | try writer.writeByte(')'); |
| 6378 | 6462 | try a.end(f, writer); |
| 6379 | 6463 | } |
| ... | ... | @@ -6389,12 +6473,12 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6389 | 6473 | } |
| 6390 | 6474 | |
| 6391 | 6475 | fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6392 | | const mod = f.object.dg.module; |
| 6476 | const zcu = f.object.dg.zcu; |
| 6393 | 6477 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6394 | 6478 | const extra = f.air.extraData(Air.AtomicRmw, pl_op.payload).data; |
| 6395 | 6479 | const inst_ty = f.typeOfIndex(inst); |
| 6396 | 6480 | const ptr_ty = f.typeOf(pl_op.operand); |
| 6397 | | const ty = ptr_ty.childType(mod); |
| 6481 | const ty = ptr_ty.childType(zcu); |
| 6398 | 6482 | const ptr = try f.resolveInst(pl_op.operand); |
| 6399 | 6483 | const operand = try f.resolveInst(extra.operand); |
| 6400 | 6484 | |
| ... | ... | @@ -6402,10 +6486,10 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6402 | 6486 | const operand_mat = try Materialize.start(f, inst, writer, ty, operand); |
| 6403 | 6487 | try reap(f, inst, &.{ pl_op.operand, extra.operand }); |
| 6404 | 6488 | |
| 6405 | | const repr_bits = @as(u16, @intCast(ty.abiSize(mod) * 8)); |
| 6489 | const repr_bits = @as(u16, @intCast(ty.abiSize(zcu) * 8)); |
| 6406 | 6490 | const is_float = ty.isRuntimeFloat(); |
| 6407 | 6491 | const is_128 = repr_bits == 128; |
| 6408 | | const repr_ty = if (is_float) mod.intType(.unsigned, repr_bits) catch unreachable else ty; |
| 6492 | const repr_ty = if (is_float) zcu.intType(.unsigned, repr_bits) catch unreachable else ty; |
| 6409 | 6493 | |
| 6410 | 6494 | const local = try f.allocLocal(inst, inst_ty); |
| 6411 | 6495 | try writer.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())}); |
| ... | ... | @@ -6421,7 +6505,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6421 | 6505 | if (use_atomic) try writer.writeAll("zig_atomic("); |
| 6422 | 6506 | try f.renderType(writer, ty); |
| 6423 | 6507 | if (use_atomic) try writer.writeByte(')'); |
| 6424 | | if (ptr_ty.isVolatilePtr(mod)) try writer.writeAll(" volatile"); |
| 6508 | if (ptr_ty.isVolatilePtr(zcu)) try writer.writeAll(" volatile"); |
| 6425 | 6509 | try writer.writeAll(" *)"); |
| 6426 | 6510 | try f.writeCValue(writer, ptr, .Other); |
| 6427 | 6511 | try writer.writeAll(", "); |
| ... | ... | @@ -6431,7 +6515,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6431 | 6515 | try writer.writeAll(", "); |
| 6432 | 6516 | try f.object.dg.renderTypeForBuiltinFnName(writer, ty); |
| 6433 | 6517 | try writer.writeAll(", "); |
| 6434 | | try f.object.dg.renderType(writer, repr_ty); |
| 6518 | try f.renderType(writer, repr_ty); |
| 6435 | 6519 | try writer.writeAll(");\n"); |
| 6436 | 6520 | try operand_mat.end(f, inst); |
| 6437 | 6521 | |
| ... | ... | @@ -6444,15 +6528,15 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6444 | 6528 | } |
| 6445 | 6529 | |
| 6446 | 6530 | fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6447 | | const mod = f.object.dg.module; |
| 6531 | const zcu = f.object.dg.zcu; |
| 6448 | 6532 | const atomic_load = f.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load; |
| 6449 | 6533 | const ptr = try f.resolveInst(atomic_load.ptr); |
| 6450 | 6534 | try reap(f, inst, &.{atomic_load.ptr}); |
| 6451 | 6535 | const ptr_ty = f.typeOf(atomic_load.ptr); |
| 6452 | | const ty = ptr_ty.childType(mod); |
| 6536 | const ty = ptr_ty.childType(zcu); |
| 6453 | 6537 | |
| 6454 | 6538 | const repr_ty = if (ty.isRuntimeFloat()) |
| 6455 | | mod.intType(.unsigned, @as(u16, @intCast(ty.abiSize(mod) * 8))) catch unreachable |
| 6539 | zcu.intType(.unsigned, @as(u16, @intCast(ty.abiSize(zcu) * 8))) catch unreachable |
| 6456 | 6540 | else |
| 6457 | 6541 | ty; |
| 6458 | 6542 | |
| ... | ... | @@ -6465,7 +6549,7 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6465 | 6549 | try writer.writeAll(", (zig_atomic("); |
| 6466 | 6550 | try f.renderType(writer, ty); |
| 6467 | 6551 | try writer.writeByte(')'); |
| 6468 | | if (ptr_ty.isVolatilePtr(mod)) try writer.writeAll(" volatile"); |
| 6552 | if (ptr_ty.isVolatilePtr(zcu)) try writer.writeAll(" volatile"); |
| 6469 | 6553 | try writer.writeAll(" *)"); |
| 6470 | 6554 | try f.writeCValue(writer, ptr, .Other); |
| 6471 | 6555 | try writer.writeAll(", "); |
| ... | ... | @@ -6473,17 +6557,17 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6473 | 6557 | try writer.writeAll(", "); |
| 6474 | 6558 | try f.object.dg.renderTypeForBuiltinFnName(writer, ty); |
| 6475 | 6559 | try writer.writeAll(", "); |
| 6476 | | try f.object.dg.renderType(writer, repr_ty); |
| 6560 | try f.renderType(writer, repr_ty); |
| 6477 | 6561 | try writer.writeAll(");\n"); |
| 6478 | 6562 | |
| 6479 | 6563 | return local; |
| 6480 | 6564 | } |
| 6481 | 6565 | |
| 6482 | 6566 | fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CValue { |
| 6483 | | const mod = f.object.dg.module; |
| 6567 | const zcu = f.object.dg.zcu; |
| 6484 | 6568 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6485 | 6569 | const ptr_ty = f.typeOf(bin_op.lhs); |
| 6486 | | const ty = ptr_ty.childType(mod); |
| 6570 | const ty = ptr_ty.childType(zcu); |
| 6487 | 6571 | const ptr = try f.resolveInst(bin_op.lhs); |
| 6488 | 6572 | const element = try f.resolveInst(bin_op.rhs); |
| 6489 | 6573 | |
| ... | ... | @@ -6492,14 +6576,14 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa |
| 6492 | 6576 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6493 | 6577 | |
| 6494 | 6578 | const repr_ty = if (ty.isRuntimeFloat()) |
| 6495 | | mod.intType(.unsigned, @as(u16, @intCast(ty.abiSize(mod) * 8))) catch unreachable |
| 6579 | zcu.intType(.unsigned, @as(u16, @intCast(ty.abiSize(zcu) * 8))) catch unreachable |
| 6496 | 6580 | else |
| 6497 | 6581 | ty; |
| 6498 | 6582 | |
| 6499 | 6583 | try writer.writeAll("zig_atomic_store((zig_atomic("); |
| 6500 | 6584 | try f.renderType(writer, ty); |
| 6501 | 6585 | try writer.writeByte(')'); |
| 6502 | | if (ptr_ty.isVolatilePtr(mod)) try writer.writeAll(" volatile"); |
| 6586 | if (ptr_ty.isVolatilePtr(zcu)) try writer.writeAll(" volatile"); |
| 6503 | 6587 | try writer.writeAll(" *)"); |
| 6504 | 6588 | try f.writeCValue(writer, ptr, .Other); |
| 6505 | 6589 | try writer.writeAll(", "); |
| ... | ... | @@ -6507,7 +6591,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa |
| 6507 | 6591 | try writer.print(", {s}, ", .{order}); |
| 6508 | 6592 | try f.object.dg.renderTypeForBuiltinFnName(writer, ty); |
| 6509 | 6593 | try writer.writeAll(", "); |
| 6510 | | try f.object.dg.renderType(writer, repr_ty); |
| 6594 | try f.renderType(writer, repr_ty); |
| 6511 | 6595 | try writer.writeAll(");\n"); |
| 6512 | 6596 | try element_mat.end(f, inst); |
| 6513 | 6597 | |
| ... | ... | @@ -6515,8 +6599,8 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa |
| 6515 | 6599 | } |
| 6516 | 6600 | |
| 6517 | 6601 | fn writeSliceOrPtr(f: *Function, writer: anytype, ptr: CValue, ptr_ty: Type) !void { |
| 6518 | | const mod = f.object.dg.module; |
| 6519 | | if (ptr_ty.isSlice(mod)) { |
| 6602 | const zcu = f.object.dg.zcu; |
| 6603 | if (ptr_ty.isSlice(zcu)) { |
| 6520 | 6604 | try f.writeCValueMember(writer, ptr, .{ .identifier = "ptr" }); |
| 6521 | 6605 | } else { |
| 6522 | 6606 | try f.writeCValue(writer, ptr, .FunctionArgument); |
| ... | ... | @@ -6524,14 +6608,14 @@ fn writeSliceOrPtr(f: *Function, writer: anytype, ptr: CValue, ptr_ty: Type) !vo |
| 6524 | 6608 | } |
| 6525 | 6609 | |
| 6526 | 6610 | fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6527 | | const mod = f.object.dg.module; |
| 6611 | const zcu = f.object.dg.zcu; |
| 6528 | 6612 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6529 | 6613 | const dest_ty = f.typeOf(bin_op.lhs); |
| 6530 | 6614 | const dest_slice = try f.resolveInst(bin_op.lhs); |
| 6531 | 6615 | const value = try f.resolveInst(bin_op.rhs); |
| 6532 | 6616 | const elem_ty = f.typeOf(bin_op.rhs); |
| 6533 | | const elem_abi_size = elem_ty.abiSize(mod); |
| 6534 | | const val_is_undef = if (try f.air.value(bin_op.rhs, mod)) |val| val.isUndefDeep(mod) else false; |
| 6617 | const elem_abi_size = elem_ty.abiSize(zcu); |
| 6618 | const val_is_undef = if (try f.air.value(bin_op.rhs, zcu)) |val| val.isUndefDeep(zcu) else false; |
| 6535 | 6619 | const writer = f.object.writer(); |
| 6536 | 6620 | |
| 6537 | 6621 | if (val_is_undef) { |
| ... | ... | @@ -6541,7 +6625,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6541 | 6625 | } |
| 6542 | 6626 | |
| 6543 | 6627 | try writer.writeAll("memset("); |
| 6544 | | switch (dest_ty.ptrSize(mod)) { |
| 6628 | switch (dest_ty.ptrSize(zcu)) { |
| 6545 | 6629 | .Slice => { |
| 6546 | 6630 | try f.writeCValueMember(writer, dest_slice, .{ .identifier = "ptr" }); |
| 6547 | 6631 | try writer.writeAll(", 0xaa, "); |
| ... | ... | @@ -6553,8 +6637,8 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6553 | 6637 | } |
| 6554 | 6638 | }, |
| 6555 | 6639 | .One => { |
| 6556 | | const array_ty = dest_ty.childType(mod); |
| 6557 | | const len = array_ty.arrayLen(mod) * elem_abi_size; |
| 6640 | const array_ty = dest_ty.childType(zcu); |
| 6641 | const len = array_ty.arrayLen(zcu) * elem_abi_size; |
| 6558 | 6642 | |
| 6559 | 6643 | try f.writeCValue(writer, dest_slice, .FunctionArgument); |
| 6560 | 6644 | try writer.print(", 0xaa, {d});\n", .{len}); |
| ... | ... | @@ -6565,12 +6649,12 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6565 | 6649 | return .none; |
| 6566 | 6650 | } |
| 6567 | 6651 | |
| 6568 | | if (elem_abi_size > 1 or dest_ty.isVolatilePtr(mod)) { |
| 6652 | if (elem_abi_size > 1 or dest_ty.isVolatilePtr(zcu)) { |
| 6569 | 6653 | // For the assignment in this loop, the array pointer needs to get |
| 6570 | 6654 | // casted to a regular pointer, otherwise an error like this occurs: |
| 6571 | 6655 | // error: array type 'uint32_t[20]' (aka 'unsigned int[20]') is not assignable |
| 6572 | | const elem_ptr_ty = try mod.ptrType(.{ |
| 6573 | | .child = elem_ty.ip_index, |
| 6656 | const elem_ptr_ty = try zcu.ptrType(.{ |
| 6657 | .child = elem_ty.toIntern(), |
| 6574 | 6658 | .flags = .{ |
| 6575 | 6659 | .size = .C, |
| 6576 | 6660 | }, |
| ... | ... | @@ -6581,17 +6665,17 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6581 | 6665 | try writer.writeAll("for ("); |
| 6582 | 6666 | try f.writeCValue(writer, index, .Other); |
| 6583 | 6667 | try writer.writeAll(" = "); |
| 6584 | | try f.object.dg.renderValue(writer, Type.usize, try mod.intValue(Type.usize, 0), .Initializer); |
| 6668 | try f.object.dg.renderValue(writer, try zcu.intValue(Type.usize, 0), .Initializer); |
| 6585 | 6669 | try writer.writeAll("; "); |
| 6586 | 6670 | try f.writeCValue(writer, index, .Other); |
| 6587 | 6671 | try writer.writeAll(" != "); |
| 6588 | | switch (dest_ty.ptrSize(mod)) { |
| 6672 | switch (dest_ty.ptrSize(zcu)) { |
| 6589 | 6673 | .Slice => { |
| 6590 | 6674 | try f.writeCValueMember(writer, dest_slice, .{ .identifier = "len" }); |
| 6591 | 6675 | }, |
| 6592 | 6676 | .One => { |
| 6593 | | const array_ty = dest_ty.childType(mod); |
| 6594 | | try writer.print("{d}", .{array_ty.arrayLen(mod)}); |
| 6677 | const array_ty = dest_ty.childType(zcu); |
| 6678 | try writer.print("{d}", .{array_ty.arrayLen(zcu)}); |
| 6595 | 6679 | }, |
| 6596 | 6680 | .Many, .C => unreachable, |
| 6597 | 6681 | } |
| ... | ... | @@ -6620,7 +6704,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6620 | 6704 | const bitcasted = try bitcast(f, Type.u8, value, elem_ty); |
| 6621 | 6705 | |
| 6622 | 6706 | try writer.writeAll("memset("); |
| 6623 | | switch (dest_ty.ptrSize(mod)) { |
| 6707 | switch (dest_ty.ptrSize(zcu)) { |
| 6624 | 6708 | .Slice => { |
| 6625 | 6709 | try f.writeCValueMember(writer, dest_slice, .{ .identifier = "ptr" }); |
| 6626 | 6710 | try writer.writeAll(", "); |
| ... | ... | @@ -6630,8 +6714,8 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6630 | 6714 | try writer.writeAll(");\n"); |
| 6631 | 6715 | }, |
| 6632 | 6716 | .One => { |
| 6633 | | const array_ty = dest_ty.childType(mod); |
| 6634 | | const len = array_ty.arrayLen(mod) * elem_abi_size; |
| 6717 | const array_ty = dest_ty.childType(zcu); |
| 6718 | const len = array_ty.arrayLen(zcu) * elem_abi_size; |
| 6635 | 6719 | |
| 6636 | 6720 | try f.writeCValue(writer, dest_slice, .FunctionArgument); |
| 6637 | 6721 | try writer.writeAll(", "); |
| ... | ... | @@ -6646,7 +6730,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6646 | 6730 | } |
| 6647 | 6731 | |
| 6648 | 6732 | fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6649 | | const mod = f.object.dg.module; |
| 6733 | const zcu = f.object.dg.zcu; |
| 6650 | 6734 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6651 | 6735 | const dest_ptr = try f.resolveInst(bin_op.lhs); |
| 6652 | 6736 | const src_ptr = try f.resolveInst(bin_op.rhs); |
| ... | ... | @@ -6659,10 +6743,10 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6659 | 6743 | try writer.writeAll(", "); |
| 6660 | 6744 | try writeSliceOrPtr(f, writer, src_ptr, src_ty); |
| 6661 | 6745 | try writer.writeAll(", "); |
| 6662 | | switch (dest_ty.ptrSize(mod)) { |
| 6746 | switch (dest_ty.ptrSize(zcu)) { |
| 6663 | 6747 | .Slice => { |
| 6664 | | const elem_ty = dest_ty.childType(mod); |
| 6665 | | const elem_abi_size = elem_ty.abiSize(mod); |
| 6748 | const elem_ty = dest_ty.childType(zcu); |
| 6749 | const elem_abi_size = elem_ty.abiSize(zcu); |
| 6666 | 6750 | try f.writeCValueMember(writer, dest_ptr, .{ .identifier = "len" }); |
| 6667 | 6751 | if (elem_abi_size > 1) { |
| 6668 | 6752 | try writer.print(" * {d});\n", .{elem_abi_size}); |
| ... | ... | @@ -6671,10 +6755,10 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6671 | 6755 | } |
| 6672 | 6756 | }, |
| 6673 | 6757 | .One => { |
| 6674 | | const array_ty = dest_ty.childType(mod); |
| 6675 | | const elem_ty = array_ty.childType(mod); |
| 6676 | | const elem_abi_size = elem_ty.abiSize(mod); |
| 6677 | | const len = array_ty.arrayLen(mod) * elem_abi_size; |
| 6758 | const array_ty = dest_ty.childType(zcu); |
| 6759 | const elem_ty = array_ty.childType(zcu); |
| 6760 | const elem_abi_size = elem_ty.abiSize(zcu); |
| 6761 | const len = array_ty.arrayLen(zcu) * elem_abi_size; |
| 6678 | 6762 | try writer.print("{d});\n", .{len}); |
| 6679 | 6763 | }, |
| 6680 | 6764 | .Many, .C => unreachable, |
| ... | ... | @@ -6685,16 +6769,16 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6685 | 6769 | } |
| 6686 | 6770 | |
| 6687 | 6771 | fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6688 | | const mod = f.object.dg.module; |
| 6772 | const zcu = f.object.dg.zcu; |
| 6689 | 6773 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6690 | 6774 | const union_ptr = try f.resolveInst(bin_op.lhs); |
| 6691 | 6775 | const new_tag = try f.resolveInst(bin_op.rhs); |
| 6692 | 6776 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6693 | 6777 | |
| 6694 | | const union_ty = f.typeOf(bin_op.lhs).childType(mod); |
| 6695 | | const layout = union_ty.unionGetLayout(mod); |
| 6778 | const union_ty = f.typeOf(bin_op.lhs).childType(zcu); |
| 6779 | const layout = union_ty.unionGetLayout(zcu); |
| 6696 | 6780 | if (layout.tag_size == 0) return .none; |
| 6697 | | const tag_ty = union_ty.unionTagTypeSafety(mod).?; |
| 6781 | const tag_ty = union_ty.unionTagTypeSafety(zcu).?; |
| 6698 | 6782 | |
| 6699 | 6783 | const writer = f.object.writer(); |
| 6700 | 6784 | const a = try Assignment.start(f, writer, tag_ty); |
| ... | ... | @@ -6706,14 +6790,14 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6706 | 6790 | } |
| 6707 | 6791 | |
| 6708 | 6792 | fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6709 | | const mod = f.object.dg.module; |
| 6793 | const zcu = f.object.dg.zcu; |
| 6710 | 6794 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6711 | 6795 | |
| 6712 | 6796 | const operand = try f.resolveInst(ty_op.operand); |
| 6713 | 6797 | try reap(f, inst, &.{ty_op.operand}); |
| 6714 | 6798 | |
| 6715 | 6799 | const union_ty = f.typeOf(ty_op.operand); |
| 6716 | | const layout = union_ty.unionGetLayout(mod); |
| 6800 | const layout = union_ty.unionGetLayout(zcu); |
| 6717 | 6801 | if (layout.tag_size == 0) return .none; |
| 6718 | 6802 | |
| 6719 | 6803 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -6728,7 +6812,7 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6728 | 6812 | } |
| 6729 | 6813 | |
| 6730 | 6814 | fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6731 | | const mod = f.object.dg.module; |
| 6815 | const zcu = f.object.dg.zcu; |
| 6732 | 6816 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6733 | 6817 | |
| 6734 | 6818 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -6740,7 +6824,7 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6740 | 6824 | const local = try f.allocLocal(inst, inst_ty); |
| 6741 | 6825 | try f.writeCValue(writer, local, .Other); |
| 6742 | 6826 | try writer.print(" = {s}(", .{ |
| 6743 | | try f.getLazyFnName(.{ .tag_name = enum_ty.getOwnerDecl(mod) }, .{ .tag_name = enum_ty }), |
| 6827 | try f.getLazyFnName(.{ .tag_name = enum_ty.getOwnerDecl(zcu) }, .{ .tag_name = enum_ty }), |
| 6744 | 6828 | }); |
| 6745 | 6829 | try f.writeCValue(writer, operand, .Other); |
| 6746 | 6830 | try writer.writeAll(");\n"); |
| ... | ... | @@ -6765,14 +6849,14 @@ fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6765 | 6849 | } |
| 6766 | 6850 | |
| 6767 | 6851 | fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6768 | | const mod = f.object.dg.module; |
| 6852 | const zcu = f.object.dg.zcu; |
| 6769 | 6853 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6770 | 6854 | |
| 6771 | 6855 | const operand = try f.resolveInst(ty_op.operand); |
| 6772 | 6856 | try reap(f, inst, &.{ty_op.operand}); |
| 6773 | 6857 | |
| 6774 | 6858 | const inst_ty = f.typeOfIndex(inst); |
| 6775 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 6859 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 6776 | 6860 | |
| 6777 | 6861 | const writer = f.object.writer(); |
| 6778 | 6862 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -6820,7 +6904,7 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6820 | 6904 | } |
| 6821 | 6905 | |
| 6822 | 6906 | fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6823 | | const mod = f.object.dg.module; |
| 6907 | const zcu = f.object.dg.zcu; |
| 6824 | 6908 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6825 | 6909 | const extra = f.air.extraData(Air.Shuffle, ty_pl.payload).data; |
| 6826 | 6910 | |
| ... | ... | @@ -6836,15 +6920,15 @@ fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6836 | 6920 | for (0..extra.mask_len) |index| { |
| 6837 | 6921 | try f.writeCValue(writer, local, .Other); |
| 6838 | 6922 | try writer.writeByte('['); |
| 6839 | | try f.object.dg.renderValue(writer, Type.usize, try mod.intValue(Type.usize, index), .Other); |
| 6923 | try f.object.dg.renderValue(writer, try zcu.intValue(Type.usize, index), .Other); |
| 6840 | 6924 | try writer.writeAll("] = "); |
| 6841 | 6925 | |
| 6842 | | const mask_elem = (try mask.elemValue(mod, index)).toSignedInt(mod); |
| 6843 | | const src_val = try mod.intValue(Type.usize, @as(u64, @intCast(mask_elem ^ mask_elem >> 63))); |
| 6926 | const mask_elem = (try mask.elemValue(zcu, index)).toSignedInt(zcu); |
| 6927 | const src_val = try zcu.intValue(Type.usize, @as(u64, @intCast(mask_elem ^ mask_elem >> 63))); |
| 6844 | 6928 | |
| 6845 | 6929 | try f.writeCValue(writer, if (mask_elem >= 0) lhs else rhs, .Other); |
| 6846 | 6930 | try writer.writeByte('['); |
| 6847 | | try f.object.dg.renderValue(writer, Type.usize, src_val, .Other); |
| 6931 | try f.object.dg.renderValue(writer, src_val, .Other); |
| 6848 | 6932 | try writer.writeAll("];\n"); |
| 6849 | 6933 | } |
| 6850 | 6934 | |
| ... | ... | @@ -6852,7 +6936,7 @@ fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6852 | 6936 | } |
| 6853 | 6937 | |
| 6854 | 6938 | fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6855 | | const mod = f.object.dg.module; |
| 6939 | const zcu = f.object.dg.zcu; |
| 6856 | 6940 | const reduce = f.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 6857 | 6941 | |
| 6858 | 6942 | const scalar_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -6861,7 +6945,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6861 | 6945 | const operand_ty = f.typeOf(reduce.operand); |
| 6862 | 6946 | const writer = f.object.writer(); |
| 6863 | 6947 | |
| 6864 | | const use_operator = scalar_ty.bitSize(mod) <= 64; |
| 6948 | const use_operator = scalar_ty.bitSize(zcu) <= 64; |
| 6865 | 6949 | const op: union(enum) { |
| 6866 | 6950 | const Func = struct { operation: []const u8, info: BuiltinInfo = .none }; |
| 6867 | 6951 | float_op: Func, |
| ... | ... | @@ -6872,28 +6956,28 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6872 | 6956 | .And => if (use_operator) .{ .infix = " &= " } else .{ .builtin = .{ .operation = "and" } }, |
| 6873 | 6957 | .Or => if (use_operator) .{ .infix = " |= " } else .{ .builtin = .{ .operation = "or" } }, |
| 6874 | 6958 | .Xor => if (use_operator) .{ .infix = " ^= " } else .{ .builtin = .{ .operation = "xor" } }, |
| 6875 | | .Min => switch (scalar_ty.zigTypeTag(mod)) { |
| 6959 | .Min => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6876 | 6960 | .Int => if (use_operator) .{ .ternary = " < " } else .{ |
| 6877 | 6961 | .builtin = .{ .operation = "min" }, |
| 6878 | 6962 | }, |
| 6879 | 6963 | .Float => .{ .float_op = .{ .operation = "fmin" } }, |
| 6880 | 6964 | else => unreachable, |
| 6881 | 6965 | }, |
| 6882 | | .Max => switch (scalar_ty.zigTypeTag(mod)) { |
| 6966 | .Max => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6883 | 6967 | .Int => if (use_operator) .{ .ternary = " > " } else .{ |
| 6884 | 6968 | .builtin = .{ .operation = "max" }, |
| 6885 | 6969 | }, |
| 6886 | 6970 | .Float => .{ .float_op = .{ .operation = "fmax" } }, |
| 6887 | 6971 | else => unreachable, |
| 6888 | 6972 | }, |
| 6889 | | .Add => switch (scalar_ty.zigTypeTag(mod)) { |
| 6973 | .Add => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6890 | 6974 | .Int => if (use_operator) .{ .infix = " += " } else .{ |
| 6891 | 6975 | .builtin = .{ .operation = "addw", .info = .bits }, |
| 6892 | 6976 | }, |
| 6893 | 6977 | .Float => .{ .builtin = .{ .operation = "add" } }, |
| 6894 | 6978 | else => unreachable, |
| 6895 | 6979 | }, |
| 6896 | | .Mul => switch (scalar_ty.zigTypeTag(mod)) { |
| 6980 | .Mul => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6897 | 6981 | .Int => if (use_operator) .{ .infix = " *= " } else .{ |
| 6898 | 6982 | .builtin = .{ .operation = "mulw", .info = .bits }, |
| 6899 | 6983 | }, |
| ... | ... | @@ -6908,7 +6992,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6908 | 6992 | // Equivalent to: |
| 6909 | 6993 | // reduce: { |
| 6910 | 6994 | // var accum: T = init; |
| 6911 | | // for (vec) : (elem) { |
| 6995 | // for (vec) |elem| { |
| 6912 | 6996 | // accum = func(accum, elem); |
| 6913 | 6997 | // } |
| 6914 | 6998 | // break :reduce accum; |
| ... | ... | @@ -6918,40 +7002,40 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6918 | 7002 | try f.writeCValue(writer, accum, .Other); |
| 6919 | 7003 | try writer.writeAll(" = "); |
| 6920 | 7004 | |
| 6921 | | try f.object.dg.renderValue(writer, scalar_ty, switch (reduce.operation) { |
| 6922 | | .Or, .Xor => switch (scalar_ty.zigTypeTag(mod)) { |
| 7005 | try f.object.dg.renderValue(writer, switch (reduce.operation) { |
| 7006 | .Or, .Xor => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6923 | 7007 | .Bool => Value.false, |
| 6924 | | .Int => try mod.intValue(scalar_ty, 0), |
| 7008 | .Int => try zcu.intValue(scalar_ty, 0), |
| 6925 | 7009 | else => unreachable, |
| 6926 | 7010 | }, |
| 6927 | | .And => switch (scalar_ty.zigTypeTag(mod)) { |
| 7011 | .And => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6928 | 7012 | .Bool => Value.true, |
| 6929 | | .Int => switch (scalar_ty.intInfo(mod).signedness) { |
| 6930 | | .unsigned => try scalar_ty.maxIntScalar(mod, scalar_ty), |
| 6931 | | .signed => try mod.intValue(scalar_ty, -1), |
| 7013 | .Int => switch (scalar_ty.intInfo(zcu).signedness) { |
| 7014 | .unsigned => try scalar_ty.maxIntScalar(zcu, scalar_ty), |
| 7015 | .signed => try zcu.intValue(scalar_ty, -1), |
| 6932 | 7016 | }, |
| 6933 | 7017 | else => unreachable, |
| 6934 | 7018 | }, |
| 6935 | | .Add => switch (scalar_ty.zigTypeTag(mod)) { |
| 6936 | | .Int => try mod.intValue(scalar_ty, 0), |
| 6937 | | .Float => try mod.floatValue(scalar_ty, 0.0), |
| 7019 | .Add => switch (scalar_ty.zigTypeTag(zcu)) { |
| 7020 | .Int => try zcu.intValue(scalar_ty, 0), |
| 7021 | .Float => try zcu.floatValue(scalar_ty, 0.0), |
| 6938 | 7022 | else => unreachable, |
| 6939 | 7023 | }, |
| 6940 | | .Mul => switch (scalar_ty.zigTypeTag(mod)) { |
| 6941 | | .Int => try mod.intValue(scalar_ty, 1), |
| 6942 | | .Float => try mod.floatValue(scalar_ty, 1.0), |
| 7024 | .Mul => switch (scalar_ty.zigTypeTag(zcu)) { |
| 7025 | .Int => try zcu.intValue(scalar_ty, 1), |
| 7026 | .Float => try zcu.floatValue(scalar_ty, 1.0), |
| 6943 | 7027 | else => unreachable, |
| 6944 | 7028 | }, |
| 6945 | | .Min => switch (scalar_ty.zigTypeTag(mod)) { |
| 7029 | .Min => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6946 | 7030 | .Bool => Value.true, |
| 6947 | | .Int => try scalar_ty.maxIntScalar(mod, scalar_ty), |
| 6948 | | .Float => try mod.floatValue(scalar_ty, std.math.nan(f128)), |
| 7031 | .Int => try scalar_ty.maxIntScalar(zcu, scalar_ty), |
| 7032 | .Float => try zcu.floatValue(scalar_ty, std.math.nan(f128)), |
| 6949 | 7033 | else => unreachable, |
| 6950 | 7034 | }, |
| 6951 | | .Max => switch (scalar_ty.zigTypeTag(mod)) { |
| 7035 | .Max => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6952 | 7036 | .Bool => Value.false, |
| 6953 | | .Int => try scalar_ty.minIntScalar(mod, scalar_ty), |
| 6954 | | .Float => try mod.floatValue(scalar_ty, std.math.nan(f128)), |
| 7037 | .Int => try scalar_ty.minIntScalar(zcu, scalar_ty), |
| 7038 | .Float => try zcu.floatValue(scalar_ty, std.math.nan(f128)), |
| 6955 | 7039 | else => unreachable, |
| 6956 | 7040 | }, |
| 6957 | 7041 | }, .Initializer); |
| ... | ... | @@ -7007,11 +7091,11 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7007 | 7091 | } |
| 7008 | 7092 | |
| 7009 | 7093 | fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7010 | | const mod = f.object.dg.module; |
| 7011 | | const ip = &mod.intern_pool; |
| 7094 | const zcu = f.object.dg.zcu; |
| 7095 | const ip = &zcu.intern_pool; |
| 7012 | 7096 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7013 | 7097 | const inst_ty = f.typeOfIndex(inst); |
| 7014 | | const len = @as(usize, @intCast(inst_ty.arrayLen(mod))); |
| 7098 | const len = @as(usize, @intCast(inst_ty.arrayLen(zcu))); |
| 7015 | 7099 | const elements = @as([]const Air.Inst.Ref, @ptrCast(f.air.extra[ty_pl.payload..][0..len])); |
| 7016 | 7100 | const gpa = f.object.dg.gpa; |
| 7017 | 7101 | const resolved_elements = try gpa.alloc(CValue, elements.len); |
| ... | ... | @@ -7028,10 +7112,9 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7028 | 7112 | |
| 7029 | 7113 | const writer = f.object.writer(); |
| 7030 | 7114 | const local = try f.allocLocal(inst, inst_ty); |
| 7031 | | switch (inst_ty.zigTypeTag(mod)) { |
| 7115 | switch (inst_ty.zigTypeTag(zcu)) { |
| 7032 | 7116 | .Array, .Vector => { |
| 7033 | | const elem_ty = inst_ty.childType(mod); |
| 7034 | | const a = try Assignment.init(f, elem_ty); |
| 7117 | const a = try Assignment.init(f, inst_ty.childType(zcu)); |
| 7035 | 7118 | for (resolved_elements, 0..) |element, i| { |
| 7036 | 7119 | try a.restart(f, writer); |
| 7037 | 7120 | try f.writeCValue(writer, local, .Other); |
| ... | ... | @@ -7040,26 +7123,26 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7040 | 7123 | try f.writeCValue(writer, element, .Other); |
| 7041 | 7124 | try a.end(f, writer); |
| 7042 | 7125 | } |
| 7043 | | if (inst_ty.sentinel(mod)) |sentinel| { |
| 7126 | if (inst_ty.sentinel(zcu)) |sentinel| { |
| 7044 | 7127 | try a.restart(f, writer); |
| 7045 | 7128 | try f.writeCValue(writer, local, .Other); |
| 7046 | 7129 | try writer.print("[{d}]", .{resolved_elements.len}); |
| 7047 | 7130 | try a.assign(f, writer); |
| 7048 | | try f.object.dg.renderValue(writer, elem_ty, sentinel, .Other); |
| 7131 | try f.object.dg.renderValue(writer, sentinel, .Other); |
| 7049 | 7132 | try a.end(f, writer); |
| 7050 | 7133 | } |
| 7051 | 7134 | }, |
| 7052 | | .Struct => switch (inst_ty.containerLayout(mod)) { |
| 7135 | .Struct => switch (inst_ty.containerLayout(zcu)) { |
| 7053 | 7136 | .auto, .@"extern" => for (resolved_elements, 0..) |element, field_index| { |
| 7054 | | if (inst_ty.structFieldIsComptime(field_index, mod)) continue; |
| 7055 | | const field_ty = inst_ty.structFieldType(field_index, mod); |
| 7056 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 7137 | if (inst_ty.structFieldIsComptime(field_index, zcu)) continue; |
| 7138 | const field_ty = inst_ty.structFieldType(field_index, zcu); |
| 7139 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 7057 | 7140 | |
| 7058 | 7141 | const a = try Assignment.start(f, writer, field_ty); |
| 7059 | | try f.writeCValueMember(writer, local, if (inst_ty.isSimpleTuple(mod)) |
| 7142 | try f.writeCValueMember(writer, local, if (inst_ty.isSimpleTuple(zcu)) |
| 7060 | 7143 | .{ .field = field_index } |
| 7061 | 7144 | else |
| 7062 | | .{ .identifier = ip.stringToSlice(inst_ty.legacyStructFieldName(@intCast(field_index), mod)) }); |
| 7145 | .{ .identifier = ip.stringToSlice(inst_ty.legacyStructFieldName(@intCast(field_index), zcu)) }); |
| 7063 | 7146 | try a.assign(f, writer); |
| 7064 | 7147 | try f.writeCValue(writer, element, .Other); |
| 7065 | 7148 | try a.end(f, writer); |
| ... | ... | @@ -7067,17 +7150,17 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7067 | 7150 | .@"packed" => { |
| 7068 | 7151 | try f.writeCValue(writer, local, .Other); |
| 7069 | 7152 | try writer.writeAll(" = "); |
| 7070 | | const int_info = inst_ty.intInfo(mod); |
| 7153 | const int_info = inst_ty.intInfo(zcu); |
| 7071 | 7154 | |
| 7072 | | const bit_offset_ty = try mod.intType(.unsigned, Type.smallestUnsignedBits(int_info.bits - 1)); |
| 7155 | const bit_offset_ty = try zcu.intType(.unsigned, Type.smallestUnsignedBits(int_info.bits - 1)); |
| 7073 | 7156 | |
| 7074 | 7157 | var bit_offset: u64 = 0; |
| 7075 | 7158 | |
| 7076 | 7159 | var empty = true; |
| 7077 | 7160 | for (0..elements.len) |field_index| { |
| 7078 | | if (inst_ty.structFieldIsComptime(field_index, mod)) continue; |
| 7079 | | const field_ty = inst_ty.structFieldType(field_index, mod); |
| 7080 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 7161 | if (inst_ty.structFieldIsComptime(field_index, zcu)) continue; |
| 7162 | const field_ty = inst_ty.structFieldType(field_index, zcu); |
| 7163 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 7081 | 7164 | |
| 7082 | 7165 | if (!empty) { |
| 7083 | 7166 | try writer.writeAll("zig_or_"); |
| ... | ... | @@ -7088,9 +7171,9 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7088 | 7171 | } |
| 7089 | 7172 | empty = true; |
| 7090 | 7173 | for (resolved_elements, 0..) |element, field_index| { |
| 7091 | | if (inst_ty.structFieldIsComptime(field_index, mod)) continue; |
| 7092 | | const field_ty = inst_ty.structFieldType(field_index, mod); |
| 7093 | | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; |
| 7174 | if (inst_ty.structFieldIsComptime(field_index, zcu)) continue; |
| 7175 | const field_ty = inst_ty.structFieldType(field_index, zcu); |
| 7176 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 7094 | 7177 | |
| 7095 | 7178 | if (!empty) try writer.writeAll(", "); |
| 7096 | 7179 | // TODO: Skip this entire shift if val is 0? |
| ... | ... | @@ -7098,13 +7181,13 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7098 | 7181 | try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty); |
| 7099 | 7182 | try writer.writeByte('('); |
| 7100 | 7183 | |
| 7101 | | if (inst_ty.isAbiInt(mod) and (field_ty.isAbiInt(mod) or field_ty.isPtrAtRuntime(mod))) { |
| 7184 | if (inst_ty.isAbiInt(zcu) and (field_ty.isAbiInt(zcu) or field_ty.isPtrAtRuntime(zcu))) { |
| 7102 | 7185 | try f.renderIntCast(writer, inst_ty, element, .{}, field_ty, .FunctionArgument); |
| 7103 | 7186 | } else { |
| 7104 | 7187 | try writer.writeByte('('); |
| 7105 | 7188 | try f.renderType(writer, inst_ty); |
| 7106 | 7189 | try writer.writeByte(')'); |
| 7107 | | if (field_ty.isPtrAtRuntime(mod)) { |
| 7190 | if (field_ty.isPtrAtRuntime(zcu)) { |
| 7108 | 7191 | try writer.writeByte('('); |
| 7109 | 7192 | try f.renderType(writer, switch (int_info.signedness) { |
| 7110 | 7193 | .unsigned => Type.usize, |
| ... | ... | @@ -7115,14 +7198,14 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7115 | 7198 | try f.writeCValue(writer, element, .Other); |
| 7116 | 7199 | } |
| 7117 | 7200 | |
| 7118 | | try writer.writeAll(", "); |
| 7119 | | const bit_offset_val = try mod.intValue(bit_offset_ty, bit_offset); |
| 7120 | | try f.object.dg.renderValue(writer, bit_offset_ty, bit_offset_val, .FunctionArgument); |
| 7201 | try writer.print(", {}", .{ |
| 7202 | try f.fmtIntLiteral(try zcu.intValue(bit_offset_ty, bit_offset)), |
| 7203 | }); |
| 7121 | 7204 | try f.object.dg.renderBuiltinInfo(writer, inst_ty, .bits); |
| 7122 | 7205 | try writer.writeByte(')'); |
| 7123 | 7206 | if (!empty) try writer.writeByte(')'); |
| 7124 | 7207 | |
| 7125 | | bit_offset += field_ty.bitSize(mod); |
| 7208 | bit_offset += field_ty.bitSize(zcu); |
| 7126 | 7209 | empty = false; |
| 7127 | 7210 | } |
| 7128 | 7211 | |
| ... | ... | @@ -7136,13 +7219,13 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7136 | 7219 | } |
| 7137 | 7220 | |
| 7138 | 7221 | fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7139 | | const mod = f.object.dg.module; |
| 7140 | | const ip = &mod.intern_pool; |
| 7222 | const zcu = f.object.dg.zcu; |
| 7223 | const ip = &zcu.intern_pool; |
| 7141 | 7224 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7142 | 7225 | const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 7143 | 7226 | |
| 7144 | 7227 | const union_ty = f.typeOfIndex(inst); |
| 7145 | | const union_obj = mod.typeToUnion(union_ty).?; |
| 7228 | const union_obj = zcu.typeToUnion(union_ty).?; |
| 7146 | 7229 | const field_name = union_obj.loadTagType(ip).names.get(ip)[extra.field_index]; |
| 7147 | 7230 | const payload_ty = f.typeOf(extra.init); |
| 7148 | 7231 | const payload = try f.resolveInst(extra.init); |
| ... | ... | @@ -7158,19 +7241,16 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7158 | 7241 | return local; |
| 7159 | 7242 | } |
| 7160 | 7243 | |
| 7161 | | const field: CValue = if (union_ty.unionTagTypeSafety(mod)) |tag_ty| field: { |
| 7162 | | const layout = union_ty.unionGetLayout(mod); |
| 7244 | const field: CValue = if (union_ty.unionTagTypeSafety(zcu)) |tag_ty| field: { |
| 7245 | const layout = union_ty.unionGetLayout(zcu); |
| 7163 | 7246 | if (layout.tag_size != 0) { |
| 7164 | | const field_index = tag_ty.enumFieldIndex(field_name, mod).?; |
| 7165 | | |
| 7166 | | const tag_val = try mod.enumValueFieldIndex(tag_ty, field_index); |
| 7167 | | |
| 7168 | | const int_val = try tag_val.intFromEnum(tag_ty, mod); |
| 7247 | const field_index = tag_ty.enumFieldIndex(field_name, zcu).?; |
| 7248 | const tag_val = try zcu.enumValueFieldIndex(tag_ty, field_index); |
| 7169 | 7249 | |
| 7170 | 7250 | const a = try Assignment.start(f, writer, tag_ty); |
| 7171 | 7251 | try f.writeCValueMember(writer, local, .{ .identifier = "tag" }); |
| 7172 | 7252 | try a.assign(f, writer); |
| 7173 | | try writer.print("{}", .{try f.fmtIntLiteral(tag_ty, int_val)}); |
| 7253 | try writer.print("{}", .{try f.fmtIntLiteral(try tag_val.intFromEnum(tag_ty, zcu))}); |
| 7174 | 7254 | try a.end(f, writer); |
| 7175 | 7255 | } |
| 7176 | 7256 | break :field .{ .payload_identifier = ip.stringToSlice(field_name) }; |
| ... | ... | @@ -7185,7 +7265,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7185 | 7265 | } |
| 7186 | 7266 | |
| 7187 | 7267 | fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7188 | | const mod = f.object.dg.module; |
| 7268 | const zcu = f.object.dg.zcu; |
| 7189 | 7269 | const prefetch = f.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; |
| 7190 | 7270 | |
| 7191 | 7271 | const ptr_ty = f.typeOf(prefetch.ptr); |
| ... | ... | @@ -7196,7 +7276,7 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7196 | 7276 | switch (prefetch.cache) { |
| 7197 | 7277 | .data => { |
| 7198 | 7278 | try writer.writeAll("zig_prefetch("); |
| 7199 | | if (ptr_ty.isSlice(mod)) |
| 7279 | if (ptr_ty.isSlice(zcu)) |
| 7200 | 7280 | try f.writeCValueMember(writer, ptr, .{ .identifier = "ptr" }) |
| 7201 | 7281 | else |
| 7202 | 7282 | try f.writeCValue(writer, ptr, .FunctionArgument); |
| ... | ... | @@ -7242,14 +7322,14 @@ fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7242 | 7322 | } |
| 7243 | 7323 | |
| 7244 | 7324 | fn airFloatNeg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7245 | | const mod = f.object.dg.module; |
| 7325 | const zcu = f.object.dg.zcu; |
| 7246 | 7326 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7247 | 7327 | |
| 7248 | 7328 | const operand = try f.resolveInst(un_op); |
| 7249 | 7329 | try reap(f, inst, &.{un_op}); |
| 7250 | 7330 | |
| 7251 | 7331 | const operand_ty = f.typeOf(un_op); |
| 7252 | | const scalar_ty = operand_ty.scalarType(mod); |
| 7332 | const scalar_ty = operand_ty.scalarType(zcu); |
| 7253 | 7333 | |
| 7254 | 7334 | const writer = f.object.writer(); |
| 7255 | 7335 | const local = try f.allocLocal(inst, operand_ty); |
| ... | ... | @@ -7268,15 +7348,15 @@ fn airFloatNeg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7268 | 7348 | } |
| 7269 | 7349 | |
| 7270 | 7350 | fn airAbs(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7271 | | const mod = f.object.dg.module; |
| 7351 | const zcu = f.object.dg.zcu; |
| 7272 | 7352 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7273 | 7353 | const operand = try f.resolveInst(ty_op.operand); |
| 7274 | 7354 | const ty = f.typeOf(ty_op.operand); |
| 7275 | | const scalar_ty = ty.scalarType(mod); |
| 7355 | const scalar_ty = ty.scalarType(zcu); |
| 7276 | 7356 | |
| 7277 | | switch (scalar_ty.zigTypeTag(mod)) { |
| 7278 | | .Int => if (ty.zigTypeTag(mod) == .Vector) { |
| 7279 | | return f.fail("TODO implement airAbs for '{}'", .{ty.fmt(mod)}); |
| 7357 | switch (scalar_ty.zigTypeTag(zcu)) { |
| 7358 | .Int => if (ty.zigTypeTag(zcu) == .Vector) { |
| 7359 | return f.fail("TODO implement airAbs for '{}'", .{ty.fmt(zcu)}); |
| 7280 | 7360 | } else { |
| 7281 | 7361 | return airUnBuiltinCall(f, inst, "abs", .none); |
| 7282 | 7362 | }, |
| ... | ... | @@ -7286,8 +7366,8 @@ fn airAbs(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7286 | 7366 | } |
| 7287 | 7367 | |
| 7288 | 7368 | fn unFloatOp(f: *Function, inst: Air.Inst.Index, operand: CValue, ty: Type, operation: []const u8) !CValue { |
| 7289 | | const mod = f.object.dg.module; |
| 7290 | | const scalar_ty = ty.scalarType(mod); |
| 7369 | const zcu = f.object.dg.zcu; |
| 7370 | const scalar_ty = ty.scalarType(zcu); |
| 7291 | 7371 | |
| 7292 | 7372 | const writer = f.object.writer(); |
| 7293 | 7373 | const local = try f.allocLocal(inst, ty); |
| ... | ... | @@ -7316,7 +7396,7 @@ fn airUnFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVal |
| 7316 | 7396 | } |
| 7317 | 7397 | |
| 7318 | 7398 | fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CValue { |
| 7319 | | const mod = f.object.dg.module; |
| 7399 | const zcu = f.object.dg.zcu; |
| 7320 | 7400 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7321 | 7401 | |
| 7322 | 7402 | const lhs = try f.resolveInst(bin_op.lhs); |
| ... | ... | @@ -7324,7 +7404,7 @@ fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVa |
| 7324 | 7404 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 7325 | 7405 | |
| 7326 | 7406 | const inst_ty = f.typeOfIndex(inst); |
| 7327 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 7407 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 7328 | 7408 | |
| 7329 | 7409 | const writer = f.object.writer(); |
| 7330 | 7410 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -7346,7 +7426,7 @@ fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVa |
| 7346 | 7426 | } |
| 7347 | 7427 | |
| 7348 | 7428 | fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7349 | | const mod = f.object.dg.module; |
| 7429 | const zcu = f.object.dg.zcu; |
| 7350 | 7430 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 7351 | 7431 | const bin_op = f.air.extraData(Air.Bin, pl_op.payload).data; |
| 7352 | 7432 | |
| ... | ... | @@ -7356,7 +7436,7 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7356 | 7436 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs, pl_op.operand }); |
| 7357 | 7437 | |
| 7358 | 7438 | const inst_ty = f.typeOfIndex(inst); |
| 7359 | | const inst_scalar_ty = inst_ty.scalarType(mod); |
| 7439 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 7360 | 7440 | |
| 7361 | 7441 | const writer = f.object.writer(); |
| 7362 | 7442 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -7381,11 +7461,11 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7381 | 7461 | } |
| 7382 | 7462 | |
| 7383 | 7463 | fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7384 | | const mod = f.object.dg.module; |
| 7464 | const zcu = f.object.dg.zcu; |
| 7385 | 7465 | const inst_ty = f.typeOfIndex(inst); |
| 7386 | 7466 | const decl_index = f.object.dg.pass.decl; |
| 7387 | | const decl = mod.declPtr(decl_index); |
| 7388 | | const fn_cty = try f.typeToCType(decl.typeOf(mod), .complete); |
| 7467 | const decl = zcu.declPtr(decl_index); |
| 7468 | const fn_cty = try f.typeToCType(decl.typeOf(zcu), .complete); |
| 7389 | 7469 | const param_len = fn_cty.castTag(.varargs_function).?.data.param_types.len; |
| 7390 | 7470 | |
| 7391 | 7471 | const writer = f.object.writer(); |
| ... | ... | @@ -7589,9 +7669,8 @@ fn signAbbrev(signedness: std.builtin.Signedness) u8 { |
| 7589 | 7669 | }; |
| 7590 | 7670 | } |
| 7591 | 7671 | |
| 7592 | | fn compilerRtAbbrev(ty: Type, mod: *Module) []const u8 { |
| 7593 | | const target = mod.getTarget(); |
| 7594 | | return if (ty.isInt(mod)) switch (ty.intInfo(mod).bits) { |
| 7672 | fn compilerRtAbbrev(ty: Type, zcu: *Zcu, target: std.Target) []const u8 { |
| 7673 | return if (ty.isInt(zcu)) switch (ty.intInfo(zcu).bits) { |
| 7595 | 7674 | 1...32 => "si", |
| 7596 | 7675 | 33...64 => "di", |
| 7597 | 7676 | 65...128 => "ti", |
| ... | ... | @@ -7753,8 +7832,8 @@ fn formatIntLiteral( |
| 7753 | 7832 | options: std.fmt.FormatOptions, |
| 7754 | 7833 | writer: anytype, |
| 7755 | 7834 | ) @TypeOf(writer).Error!void { |
| 7756 | | const mod = data.dg.module; |
| 7757 | | const target = mod.getTarget(); |
| 7835 | const zcu = data.dg.zcu; |
| 7836 | const target = &data.dg.mod.resolved_target.result; |
| 7758 | 7837 | |
| 7759 | 7838 | const ExpectedContents = struct { |
| 7760 | 7839 | const base = 10; |
| ... | ... | @@ -7774,7 +7853,7 @@ fn formatIntLiteral( |
| 7774 | 7853 | defer allocator.free(undef_limbs); |
| 7775 | 7854 | |
| 7776 | 7855 | var int_buf: Value.BigIntSpace = undefined; |
| 7777 | | const int = if (data.val.isUndefDeep(mod)) blk: { |
| 7856 | const int = if (data.val.isUndefDeep(zcu)) blk: { |
| 7778 | 7857 | undef_limbs = try allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits)); |
| 7779 | 7858 | @memset(undef_limbs, undefPattern(BigIntLimb)); |
| 7780 | 7859 | |
| ... | ... | @@ -7785,10 +7864,10 @@ fn formatIntLiteral( |
| 7785 | 7864 | }; |
| 7786 | 7865 | undef_int.truncate(undef_int.toConst(), data.int_info.signedness, data.int_info.bits); |
| 7787 | 7866 | break :blk undef_int.toConst(); |
| 7788 | | } else data.val.toBigInt(&int_buf, mod); |
| 7867 | } else data.val.toBigInt(&int_buf, zcu); |
| 7789 | 7868 | assert(int.fitsInTwosComp(data.int_info.signedness, data.int_info.bits)); |
| 7790 | 7869 | |
| 7791 | | const c_bits: usize = @intCast(data.cty.byteSize(data.dg.ctypes.set, target) * 8); |
| 7870 | const c_bits: usize = @intCast(data.cty.byteSize(data.dg.ctypes.set, data.dg.mod) * 8); |
| 7792 | 7871 | var one_limbs: [BigInt.calcLimbLen(1)]BigIntLimb = undefined; |
| 7793 | 7872 | const one = BigInt.Mutable.init(&one_limbs, 1).toConst(); |
| 7794 | 7873 | |
| ... | ... | @@ -7919,7 +7998,7 @@ fn formatIntLiteral( |
| 7919 | 7998 | .int_info = c_limb_int_info, |
| 7920 | 7999 | .kind = data.kind, |
| 7921 | 8000 | .cty = c_limb_cty, |
| 7922 | | .val = try mod.intValue_big(Type.comptime_int, c_limb_mut.toConst()), |
| 8001 | .val = try zcu.intValue_big(Type.comptime_int, c_limb_mut.toConst()), |
| 7923 | 8002 | }, fmt, options, writer); |
| 7924 | 8003 | } |
| 7925 | 8004 | } |
| ... | ... | @@ -8016,21 +8095,17 @@ const Vectorize = struct { |
| 8016 | 8095 | index: CValue = .none, |
| 8017 | 8096 | |
| 8018 | 8097 | pub fn start(f: *Function, inst: Air.Inst.Index, writer: anytype, ty: Type) !Vectorize { |
| 8019 | | const mod = f.object.dg.module; |
| 8020 | | return if (ty.zigTypeTag(mod) == .Vector) index: { |
| 8021 | | const len_val = try mod.intValue(Type.usize, ty.vectorLen(mod)); |
| 8022 | | |
| 8098 | const zcu = f.object.dg.zcu; |
| 8099 | return if (ty.zigTypeTag(zcu) == .Vector) index: { |
| 8023 | 8100 | const local = try f.allocLocal(inst, Type.usize); |
| 8024 | 8101 | |
| 8025 | 8102 | try writer.writeAll("for ("); |
| 8026 | 8103 | try f.writeCValue(writer, local, .Other); |
| 8027 | | try writer.print(" = {d}; ", .{try f.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 0))}); |
| 8104 | try writer.print(" = {d}; ", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 0))}); |
| 8028 | 8105 | try f.writeCValue(writer, local, .Other); |
| 8029 | | try writer.print(" < {d}; ", .{ |
| 8030 | | try f.fmtIntLiteral(Type.usize, len_val), |
| 8031 | | }); |
| 8106 | try writer.print(" < {d}; ", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, ty.vectorLen(zcu)))}); |
| 8032 | 8107 | try f.writeCValue(writer, local, .Other); |
| 8033 | | try writer.print(" += {d}) {{\n", .{try f.fmtIntLiteral(Type.usize, try mod.intValue(Type.usize, 1))}); |
| 8108 | try writer.print(" += {d}) {{\n", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 1))}); |
| 8034 | 8109 | f.object.indent_writer.pushIndent(); |
| 8035 | 8110 | |
| 8036 | 8111 | break :index .{ .index = local }; |
| ... | ... | @@ -8054,16 +8129,16 @@ const Vectorize = struct { |
| 8054 | 8129 | } |
| 8055 | 8130 | }; |
| 8056 | 8131 | |
| 8057 | | fn lowerFnRetTy(ret_ty: Type, mod: *Module) !Type { |
| 8058 | | if (ret_ty.ip_index == .noreturn_type) return Type.noreturn; |
| 8132 | fn lowerFnRetTy(ret_ty: Type, zcu: *Zcu) !Type { |
| 8133 | if (ret_ty.toIntern() == .noreturn_type) return Type.noreturn; |
| 8059 | 8134 | |
| 8060 | | if (lowersToArray(ret_ty, mod)) { |
| 8061 | | const gpa = mod.gpa; |
| 8062 | | const ip = &mod.intern_pool; |
| 8135 | if (lowersToArray(ret_ty, zcu)) { |
| 8136 | const gpa = zcu.gpa; |
| 8137 | const ip = &zcu.intern_pool; |
| 8063 | 8138 | const names = [1]InternPool.NullTerminatedString{ |
| 8064 | 8139 | try ip.getOrPutString(gpa, "array"), |
| 8065 | 8140 | }; |
| 8066 | | const types = [1]InternPool.Index{ret_ty.ip_index}; |
| 8141 | const types = [1]InternPool.Index{ret_ty.toIntern()}; |
| 8067 | 8142 | const values = [1]InternPool.Index{.none}; |
| 8068 | 8143 | const interned = try ip.getAnonStructType(gpa, .{ |
| 8069 | 8144 | .names = &names, |
| ... | ... | @@ -8073,13 +8148,13 @@ fn lowerFnRetTy(ret_ty: Type, mod: *Module) !Type { |
| 8073 | 8148 | return Type.fromInterned(interned); |
| 8074 | 8149 | } |
| 8075 | 8150 | |
| 8076 | | return if (ret_ty.hasRuntimeBitsIgnoreComptime(mod)) ret_ty else Type.void; |
| 8151 | return if (ret_ty.hasRuntimeBitsIgnoreComptime(zcu)) ret_ty else Type.void; |
| 8077 | 8152 | } |
| 8078 | 8153 | |
| 8079 | | fn lowersToArray(ty: Type, mod: *Module) bool { |
| 8080 | | return switch (ty.zigTypeTag(mod)) { |
| 8154 | fn lowersToArray(ty: Type, zcu: *Zcu) bool { |
| 8155 | return switch (ty.zigTypeTag(zcu)) { |
| 8081 | 8156 | .Array, .Vector => return true, |
| 8082 | | else => return ty.isAbiInt(mod) and toCIntBits(@as(u32, @intCast(ty.bitSize(mod)))) == null, |
| 8157 | else => return ty.isAbiInt(zcu) and toCIntBits(@as(u32, @intCast(ty.bitSize(zcu)))) == null, |
| 8083 | 8158 | }; |
| 8084 | 8159 | } |
| 8085 | 8160 | |
| ... | ... | @@ -8098,7 +8173,7 @@ fn die(f: *Function, inst: Air.Inst.Index, ref: Air.Inst.Ref) !void { |
| 8098 | 8173 | const ref_inst = ref.toIndex() orelse return; |
| 8099 | 8174 | const c_value = (f.value_map.fetchRemove(ref) orelse return).value; |
| 8100 | 8175 | const local_index = switch (c_value) { |
| 8101 | | .local, .new_local => |l| l, |
| 8176 | .new_local, .local => |l| l, |
| 8102 | 8177 | else => return, |
| 8103 | 8178 | }; |
| 8104 | 8179 | try freeLocal(f, inst, local_index, ref_inst); |