authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-05-13 11:38:46-07:00
committergravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-06-13 02:22:05-07:00
log206e66858c69cd667aad3779837493968bfcd228
treecd63bedc020c731688345746f2b089b103c4b3b9
parentc10d1c6a75f818eda092ef7912fd3c2b996632f0
signaturelock-open Commit is signed but in an unrecognized format.

riscv: rename `Self` to `Func`

Very similar reasoning to the Wasm backend. I believe that "Self" is not the most descriptive possible name here and "Func" better explains it. The generation is happening for a Function, and accessing "Func" is like accessing the context of that current function.

1 files changed, 1868 insertions(+), 1877 deletions(-)

src/arch/riscv64/CodeGen.zig+1868-1877
......@@ -281,9 +281,9 @@ const MCValue = union(enum) {
281281const Branch = struct {
282282 inst_table: std.AutoArrayHashMapUnmanaged(Air.Inst.Index, MCValue) = .{},
283283
284 fn deinit(self: *Branch, gpa: Allocator) void {
285 self.inst_table.deinit(gpa);
286 self.* = undefined;
284 fn deinit(func: *Branch, gpa: Allocator) void {
285 func.inst_table.deinit(gpa);
286 func.* = undefined;
287287 }
288288};
289289
......@@ -317,33 +317,33 @@ const InstTracking = struct {
317317 }, .short = result };
318318 }
319319
320 fn getReg(self: InstTracking) ?Register {
321 return self.short.getReg();
320 fn getReg(func: InstTracking) ?Register {
321 return func.short.getReg();
322322 }
323323
324 fn getRegs(self: *const InstTracking) []const Register {
325 return self.short.getRegs();
324 fn getRegs(func: *const InstTracking) []const Register {
325 return func.short.getRegs();
326326 }
327327
328 fn spill(self: *InstTracking, function: *Self, inst: Air.Inst.Index) !void {
329 if (std.meta.eql(self.long, self.short)) return; // Already spilled
328 fn spill(func: *InstTracking, function: *Func, inst: Air.Inst.Index) !void {
329 if (std.meta.eql(func.long, func.short)) return; // Already spilled
330330 // Allocate or reuse frame index
331 switch (self.long) {
332 .none => self.long = try function.allocRegOrMem(inst, false),
331 switch (func.long) {
332 .none => func.long = try function.allocRegOrMem(inst, false),
333333 .load_frame => {},
334 .reserved_frame => |index| self.long = .{ .load_frame = .{ .index = index } },
334 .reserved_frame => |index| func.long = .{ .load_frame = .{ .index = index } },
335335 else => unreachable,
336336 }
337 tracking_log.debug("spill %{d} from {} to {}", .{ inst, self.short, self.long });
338 try function.genCopy(function.typeOfIndex(inst), self.long, self.short);
337 tracking_log.debug("spill %{d} from {} to {}", .{ inst, func.short, func.long });
338 try function.genCopy(function.typeOfIndex(inst), func.long, func.short);
339339 }
340340
341 fn reuseFrame(self: *InstTracking) void {
342 switch (self.long) {
343 .reserved_frame => |index| self.long = .{ .load_frame = .{ .index = index } },
341 fn reuseFrame(func: *InstTracking) void {
342 switch (func.long) {
343 .reserved_frame => |index| func.long = .{ .load_frame = .{ .index = index } },
344344 else => {},
345345 }
346 self.short = switch (self.long) {
346 func.short = switch (func.long) {
347347 .none,
348348 .unreach,
349349 .undef,
......@@ -353,7 +353,7 @@ const InstTracking = struct {
353353 .lea_frame,
354354 .load_symbol,
355355 .lea_symbol,
356 => self.long,
356 => func.long,
357357 .dead,
358358 .register,
359359 .register_pair,
......@@ -365,14 +365,14 @@ const InstTracking = struct {
365365 };
366366 }
367367
368 fn trackSpill(self: *InstTracking, function: *Self, inst: Air.Inst.Index) !void {
369 try function.freeValue(self.short);
370 self.reuseFrame();
371 tracking_log.debug("%{d} => {} (spilled)", .{ inst, self.* });
368 fn trackSpill(func: *InstTracking, function: *Func, inst: Air.Inst.Index) !void {
369 try function.freeValue(func.short);
370 func.reuseFrame();
371 tracking_log.debug("%{d} => {} (spilled)", .{ inst, func.* });
372372 }
373373
374 fn verifyMaterialize(self: InstTracking, target: InstTracking) void {
375 switch (self.long) {
374 fn verifyMaterialize(func: InstTracking, target: InstTracking) void {
375 switch (func.long) {
376376 .none,
377377 .unreach,
378378 .undef,
......@@ -381,7 +381,7 @@ const InstTracking = struct {
381381 .lea_frame,
382382 .load_symbol,
383383 .lea_symbol,
384 => assert(std.meta.eql(self.long, target.long)),
384 => assert(std.meta.eql(func.long, target.long)),
385385 .load_frame,
386386 .reserved_frame,
387387 => switch (target.long) {
......@@ -402,73 +402,73 @@ const InstTracking = struct {
402402 }
403403
404404 fn materialize(
405 self: *InstTracking,
406 function: *Self,
405 func: *InstTracking,
406 function: *Func,
407407 inst: Air.Inst.Index,
408408 target: InstTracking,
409409 ) !void {
410 self.verifyMaterialize(target);
411 try self.materializeUnsafe(function, inst, target);
410 func.verifyMaterialize(target);
411 try func.materializeUnsafe(function, inst, target);
412412 }
413413
414414 fn materializeUnsafe(
415 self: InstTracking,
416 function: *Self,
415 func: InstTracking,
416 function: *Func,
417417 inst: Air.Inst.Index,
418418 target: InstTracking,
419419 ) !void {
420420 const ty = function.typeOfIndex(inst);
421 if ((self.long == .none or self.long == .reserved_frame) and target.long == .load_frame)
422 try function.genCopy(ty, target.long, self.short);
423 try function.genCopy(ty, target.short, self.short);
421 if ((func.long == .none or func.long == .reserved_frame) and target.long == .load_frame)
422 try function.genCopy(ty, target.long, func.short);
423 try function.genCopy(ty, target.short, func.short);
424424 }
425425
426 fn trackMaterialize(self: *InstTracking, inst: Air.Inst.Index, target: InstTracking) void {
427 self.verifyMaterialize(target);
426 fn trackMaterialize(func: *InstTracking, inst: Air.Inst.Index, target: InstTracking) void {
427 func.verifyMaterialize(target);
428428 // Don't clobber reserved frame indices
429 self.long = if (target.long == .none) switch (self.long) {
429 func.long = if (target.long == .none) switch (func.long) {
430430 .load_frame => |addr| .{ .reserved_frame = addr.index },
431 .reserved_frame => self.long,
431 .reserved_frame => func.long,
432432 else => target.long,
433433 } else target.long;
434 self.short = target.short;
435 tracking_log.debug("%{d} => {} (materialize)", .{ inst, self.* });
434 func.short = target.short;
435 tracking_log.debug("%{d} => {} (materialize)", .{ inst, func.* });
436436 }
437437
438 fn resurrect(self: *InstTracking, inst: Air.Inst.Index, scope_generation: u32) void {
439 switch (self.short) {
438 fn resurrect(func: *InstTracking, inst: Air.Inst.Index, scope_generation: u32) void {
439 switch (func.short) {
440440 .dead => |die_generation| if (die_generation >= scope_generation) {
441 self.reuseFrame();
442 tracking_log.debug("%{d} => {} (resurrect)", .{ inst, self.* });
441 func.reuseFrame();
442 tracking_log.debug("%{d} => {} (resurrect)", .{ inst, func.* });
443443 },
444444 else => {},
445445 }
446446 }
447447
448 fn die(self: *InstTracking, function: *Self, inst: Air.Inst.Index) !void {
449 if (self.short == .dead) return;
450 try function.freeValue(self.short);
451 self.short = .{ .dead = function.scope_generation };
452 tracking_log.debug("%{d} => {} (death)", .{ inst, self.* });
448 fn die(func: *InstTracking, function: *Func, inst: Air.Inst.Index) !void {
449 if (func.short == .dead) return;
450 try function.freeValue(func.short);
451 func.short = .{ .dead = function.scope_generation };
452 tracking_log.debug("%{d} => {} (death)", .{ inst, func.* });
453453 }
454454
455455 fn reuse(
456 self: *InstTracking,
457 function: *Self,
456 func: *InstTracking,
457 function: *Func,
458458 new_inst: ?Air.Inst.Index,
459459 old_inst: Air.Inst.Index,
460460 ) void {
461 self.short = .{ .dead = function.scope_generation };
461 func.short = .{ .dead = function.scope_generation };
462462 if (new_inst) |inst|
463 tracking_log.debug("%{d} => {} (reuse %{d})", .{ inst, self.*, old_inst })
463 tracking_log.debug("%{d} => {} (reuse %{d})", .{ inst, func.*, old_inst })
464464 else
465 tracking_log.debug("tmp => {} (reuse %{d})", .{ self.*, old_inst });
465 tracking_log.debug("tmp => {} (reuse %{d})", .{ func.*, old_inst });
466466 }
467467
468 fn liveOut(self: *InstTracking, function: *Self, inst: Air.Inst.Index) void {
469 for (self.getRegs()) |reg| {
468 fn liveOut(func: *InstTracking, function: *Func, inst: Air.Inst.Index) void {
469 for (func.getRegs()) |reg| {
470470 if (function.register_manager.isRegFree(reg)) {
471 tracking_log.debug("%{d} => {} (live-out)", .{ inst, self.* });
471 tracking_log.debug("%{d} => {} (live-out)", .{ inst, func.* });
472472 continue;
473473 }
474474
......@@ -495,18 +495,18 @@ const InstTracking = struct {
495495 // Perform side-effects of freeValue manually.
496496 function.register_manager.freeReg(reg);
497497
498 tracking_log.debug("%{d} => {} (live-out %{d})", .{ inst, self.*, tracked_inst });
498 tracking_log.debug("%{d} => {} (live-out %{d})", .{ inst, func.*, tracked_inst });
499499 }
500500 }
501501
502502 pub fn format(
503 self: InstTracking,
503 func: InstTracking,
504504 comptime _: []const u8,
505505 _: std.fmt.FormatOptions,
506506 writer: anytype,
507507 ) @TypeOf(writer).Error!void {
508 if (!std.meta.eql(self.long, self.short)) try writer.print("|{}| ", .{self.long});
509 try writer.print("{}", .{self.short});
508 if (!std.meta.eql(func.long, func.short)) try writer.print("|{}| ", .{func.long});
509 try writer.print("{}", .{func.short});
510510 }
511511};
512512
......@@ -546,19 +546,13 @@ const FrameAlloc = struct {
546546 }
547547};
548548
549const StackAllocation = struct {
550 inst: ?Air.Inst.Index,
551 /// TODO: make the size inferred from the bits of the inst
552 size: u32,
553};
554
555549const BlockData = struct {
556550 relocs: std.ArrayListUnmanaged(Mir.Inst.Index) = .{},
557551 state: State,
558552
559 fn deinit(self: *BlockData, gpa: Allocator) void {
560 self.relocs.deinit(gpa);
561 self.* = undefined;
553 fn deinit(bd: *BlockData, gpa: Allocator) void {
554 bd.relocs.deinit(gpa);
555 bd.* = undefined;
562556 }
563557};
564558
......@@ -570,31 +564,31 @@ const State = struct {
570564 scope_generation: u32,
571565};
572566
573fn initRetroactiveState(self: *Self) State {
567fn initRetroactiveState(func: *Func) State {
574568 var state: State = undefined;
575 state.inst_tracking_len = @intCast(self.inst_tracking.count());
576 state.scope_generation = self.scope_generation;
569 state.inst_tracking_len = @intCast(func.inst_tracking.count());
570 state.scope_generation = func.scope_generation;
577571 return state;
578572}
579573
580fn saveRetroactiveState(self: *Self, state: *State) !void {
581 const free_registers = self.register_manager.free_registers;
574fn saveRetroactiveState(func: *Func, state: *State) !void {
575 const free_registers = func.register_manager.free_registers;
582576 var it = free_registers.iterator(.{ .kind = .unset });
583577 while (it.next()) |index| {
584 const tracked_inst = self.register_manager.registers[index];
578 const tracked_inst = func.register_manager.registers[index];
585579 state.registers[index] = tracked_inst;
586 state.reg_tracking[index] = self.inst_tracking.get(tracked_inst).?;
580 state.reg_tracking[index] = func.inst_tracking.get(tracked_inst).?;
587581 }
588582 state.free_registers = free_registers;
589583}
590584
591fn saveState(self: *Self) !State {
592 var state = self.initRetroactiveState();
593 try self.saveRetroactiveState(&state);
585fn saveState(func: *Func) !State {
586 var state = func.initRetroactiveState();
587 try func.saveRetroactiveState(&state);
594588 return state;
595589}
596590
597fn restoreState(self: *Self, state: State, deaths: []const Air.Inst.Index, comptime opts: struct {
591fn restoreState(func: *Func, state: State, deaths: []const Air.Inst.Index, comptime opts: struct {
598592 emit_instructions: bool,
599593 update_tracking: bool,
600594 resurrect: bool,
......@@ -602,81 +596,81 @@ fn restoreState(self: *Self, state: State, deaths: []const Air.Inst.Index, compt
602596}) !void {
603597 if (opts.close_scope) {
604598 for (
605 self.inst_tracking.keys()[state.inst_tracking_len..],
606 self.inst_tracking.values()[state.inst_tracking_len..],
607 ) |inst, *tracking| try tracking.die(self, inst);
608 self.inst_tracking.shrinkRetainingCapacity(state.inst_tracking_len);
599 func.inst_tracking.keys()[state.inst_tracking_len..],
600 func.inst_tracking.values()[state.inst_tracking_len..],
601 ) |inst, *tracking| try tracking.die(func, inst);
602 func.inst_tracking.shrinkRetainingCapacity(state.inst_tracking_len);
609603 }
610604
611605 if (opts.resurrect) for (
612 self.inst_tracking.keys()[0..state.inst_tracking_len],
613 self.inst_tracking.values()[0..state.inst_tracking_len],
606 func.inst_tracking.keys()[0..state.inst_tracking_len],
607 func.inst_tracking.values()[0..state.inst_tracking_len],
614608 ) |inst, *tracking| tracking.resurrect(inst, state.scope_generation);
615 for (deaths) |death| try self.processDeath(death);
609 for (deaths) |death| try func.processDeath(death);
616610
617611 const ExpectedContents = [@typeInfo(RegisterManager.TrackedRegisters).Array.len]RegisterLock;
618612 var stack align(@max(@alignOf(ExpectedContents), @alignOf(std.heap.StackFallbackAllocator(0)))) =
619613 if (opts.update_tracking)
620 {} else std.heap.stackFallback(@sizeOf(ExpectedContents), self.gpa);
614 {} else std.heap.stackFallback(@sizeOf(ExpectedContents), func.gpa);
621615
622616 var reg_locks = if (opts.update_tracking) {} else try std.ArrayList(RegisterLock).initCapacity(
623617 stack.get(),
624618 @typeInfo(ExpectedContents).Array.len,
625619 );
626620 defer if (!opts.update_tracking) {
627 for (reg_locks.items) |lock| self.register_manager.unlockReg(lock);
621 for (reg_locks.items) |lock| func.register_manager.unlockReg(lock);
628622 reg_locks.deinit();
629623 };
630624
631625 for (0..state.registers.len) |index| {
632 const current_maybe_inst = if (self.register_manager.free_registers.isSet(index))
626 const current_maybe_inst = if (func.register_manager.free_registers.isSet(index))
633627 null
634628 else
635 self.register_manager.registers[index];
629 func.register_manager.registers[index];
636630 const target_maybe_inst = if (state.free_registers.isSet(index))
637631 null
638632 else
639633 state.registers[index];
640634 if (std.debug.runtime_safety) if (target_maybe_inst) |target_inst|
641 assert(self.inst_tracking.getIndex(target_inst).? < state.inst_tracking_len);
635 assert(func.inst_tracking.getIndex(target_inst).? < state.inst_tracking_len);
642636 if (opts.emit_instructions) {
643637 if (current_maybe_inst) |current_inst| {
644 try self.inst_tracking.getPtr(current_inst).?.spill(self, current_inst);
638 try func.inst_tracking.getPtr(current_inst).?.spill(func, current_inst);
645639 }
646640 if (target_maybe_inst) |target_inst| {
647 const target_tracking = self.inst_tracking.getPtr(target_inst).?;
648 try target_tracking.materialize(self, target_inst, state.reg_tracking[index]);
641 const target_tracking = func.inst_tracking.getPtr(target_inst).?;
642 try target_tracking.materialize(func, target_inst, state.reg_tracking[index]);
649643 }
650644 }
651645 if (opts.update_tracking) {
652646 if (current_maybe_inst) |current_inst| {
653 try self.inst_tracking.getPtr(current_inst).?.trackSpill(self, current_inst);
647 try func.inst_tracking.getPtr(current_inst).?.trackSpill(func, current_inst);
654648 }
655649 blk: {
656650 const inst = target_maybe_inst orelse break :blk;
657651 const reg = RegisterManager.regAtTrackedIndex(@intCast(index));
658 self.register_manager.freeReg(reg);
659 self.register_manager.getRegAssumeFree(reg, inst);
652 func.register_manager.freeReg(reg);
653 func.register_manager.getRegAssumeFree(reg, inst);
660654 }
661655 if (target_maybe_inst) |target_inst| {
662 self.inst_tracking.getPtr(target_inst).?.trackMaterialize(
656 func.inst_tracking.getPtr(target_inst).?.trackMaterialize(
663657 target_inst,
664658 state.reg_tracking[index],
665659 );
666660 }
667661 } else if (target_maybe_inst) |_|
668 try reg_locks.append(self.register_manager.lockRegIndexAssumeUnused(@intCast(index)));
662 try reg_locks.append(func.register_manager.lockRegIndexAssumeUnused(@intCast(index)));
669663 }
670664
671665 if (opts.update_tracking and std.debug.runtime_safety) {
672 assert(self.register_manager.free_registers.eql(state.free_registers));
666 assert(func.register_manager.free_registers.eql(state.free_registers));
673667 var used_reg_it = state.free_registers.iterator(.{ .kind = .unset });
674668 while (used_reg_it.next()) |index|
675 assert(self.register_manager.registers[index] == state.registers[index]);
669 assert(func.register_manager.registers[index] == state.registers[index]);
676670 }
677671}
678672
679const Self = @This();
673const Func = @This();
680674
681675const CallView = enum(u1) {
682676 callee,
......@@ -712,7 +706,7 @@ pub fn generate(
712706 }
713707 try branch_stack.append(.{});
714708
715 var function = Self{
709 var function = Func{
716710 .gpa = gpa,
717711 .air = air,
718712 .mod = mod,
......@@ -852,51 +846,51 @@ pub fn generate(
852846 }
853847}
854848
855fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index {
856 const gpa = self.gpa;
849fn addInst(func: *Func, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index {
850 const gpa = func.gpa;
857851
858 try self.mir_instructions.ensureUnusedCapacity(gpa, 1);
852 try func.mir_instructions.ensureUnusedCapacity(gpa, 1);
859853
860 const result_index: Mir.Inst.Index = @intCast(self.mir_instructions.len);
861 self.mir_instructions.appendAssumeCapacity(inst);
854 const result_index: Mir.Inst.Index = @intCast(func.mir_instructions.len);
855 func.mir_instructions.appendAssumeCapacity(inst);
862856 return result_index;
863857}
864858
865fn addNop(self: *Self) error{OutOfMemory}!Mir.Inst.Index {
866 return self.addInst(.{
859fn addNop(func: *Func) error{OutOfMemory}!Mir.Inst.Index {
860 return func.addInst(.{
867861 .tag = .nop,
868862 .ops = .none,
869863 .data = undefined,
870864 });
871865}
872866
873fn addPseudoNone(self: *Self, ops: Mir.Inst.Ops) !void {
874 _ = try self.addInst(.{
867fn addPseudoNone(func: *Func, ops: Mir.Inst.Ops) !void {
868 _ = try func.addInst(.{
875869 .tag = .pseudo,
876870 .ops = ops,
877871 .data = undefined,
878872 });
879873}
880874
881fn addPseudo(self: *Self, ops: Mir.Inst.Ops) !Mir.Inst.Index {
882 return self.addInst(.{
875fn addPseudo(func: *Func, ops: Mir.Inst.Ops) !Mir.Inst.Index {
876 return func.addInst(.{
883877 .tag = .pseudo,
884878 .ops = ops,
885879 .data = undefined,
886880 });
887881}
888882
889pub fn addExtra(self: *Self, extra: anytype) Allocator.Error!u32 {
883pub fn addExtra(func: *Func, extra: anytype) Allocator.Error!u32 {
890884 const fields = std.meta.fields(@TypeOf(extra));
891 try self.mir_extra.ensureUnusedCapacity(self.gpa, fields.len);
892 return self.addExtraAssumeCapacity(extra);
885 try func.mir_extra.ensureUnusedCapacity(func.gpa, fields.len);
886 return func.addExtraAssumeCapacity(extra);
893887}
894888
895pub fn addExtraAssumeCapacity(self: *Self, extra: anytype) u32 {
889pub fn addExtraAssumeCapacity(func: *Func, extra: anytype) u32 {
896890 const fields = std.meta.fields(@TypeOf(extra));
897 const result: u32 = @intCast(self.mir_extra.items.len);
891 const result: u32 = @intCast(func.mir_extra.items.len);
898892 inline for (fields) |field| {
899 self.mir_extra.appendAssumeCapacity(switch (field.type) {
893 func.mir_extra.appendAssumeCapacity(switch (field.type) {
900894 u32 => @field(extra, field.name),
901895 i32 => @bitCast(@field(extra, field.name)),
902896 else => @compileError("bad field type"),
......@@ -910,13 +904,13 @@ const required_features = [_]Target.riscv.Feature{
910904 .m,
911905};
912906
913fn gen(self: *Self) !void {
914 const mod = self.bin_file.comp.module.?;
915 const fn_info = mod.typeToFunc(self.fn_type).?;
907fn gen(func: *Func) !void {
908 const mod = func.bin_file.comp.module.?;
909 const fn_info = mod.typeToFunc(func.fn_type).?;
916910
917911 inline for (required_features) |feature| {
918 if (!self.hasFeature(feature)) {
919 return self.fail(
912 if (!func.hasFeature(feature)) {
913 return func.fail(
920914 "target missing required feature {s}",
921915 .{@tagName(feature)},
922916 );
......@@ -924,52 +918,52 @@ fn gen(self: *Self) !void {
924918 }
925919
926920 if (fn_info.cc != .Naked) {
927 try self.addPseudoNone(.pseudo_dbg_prologue_end);
921 try func.addPseudoNone(.pseudo_dbg_prologue_end);
928922
929 const backpatch_stack_alloc = try self.addPseudo(.pseudo_dead);
930 const backpatch_ra_spill = try self.addPseudo(.pseudo_dead);
931 const backpatch_fp_spill = try self.addPseudo(.pseudo_dead);
932 const backpatch_fp_add = try self.addPseudo(.pseudo_dead);
933 const backpatch_spill_callee_preserved_regs = try self.addPseudo(.pseudo_dead);
923 const backpatch_stack_alloc = try func.addPseudo(.pseudo_dead);
924 const backpatch_ra_spill = try func.addPseudo(.pseudo_dead);
925 const backpatch_fp_spill = try func.addPseudo(.pseudo_dead);
926 const backpatch_fp_add = try func.addPseudo(.pseudo_dead);
927 const backpatch_spill_callee_preserved_regs = try func.addPseudo(.pseudo_dead);
934928
935 switch (self.ret_mcv.long) {
929 switch (func.ret_mcv.long) {
936930 .none, .unreach => {},
937931 .indirect => {
938932 // The address where to store the return value for the caller is in a
939933 // register which the callee is free to clobber. Therefore, we purposely
940934 // spill it to stack immediately.
941 const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(Type.usize, mod));
942 try self.genSetMem(
935 const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(Type.usize, mod));
936 try func.genSetMem(
943937 .{ .frame = frame_index },
944938 0,
945939 Type.usize,
946 self.ret_mcv.long.address().offset(-self.ret_mcv.short.indirect.off),
940 func.ret_mcv.long.address().offset(-func.ret_mcv.short.indirect.off),
947941 );
948 self.ret_mcv.long = .{ .load_frame = .{ .index = frame_index } };
949 tracking_log.debug("spill {} to {}", .{ self.ret_mcv.long, frame_index });
942 func.ret_mcv.long = .{ .load_frame = .{ .index = frame_index } };
943 tracking_log.debug("spill {} to {}", .{ func.ret_mcv.long, frame_index });
950944 },
951945 else => unreachable,
952946 }
953947
954 try self.genBody(self.air.getMainBody());
948 try func.genBody(func.air.getMainBody());
955949
956 for (self.exitlude_jump_relocs.items) |jmp_reloc| {
957 self.mir_instructions.items(.data)[jmp_reloc].inst =
958 @intCast(self.mir_instructions.len);
950 for (func.exitlude_jump_relocs.items) |jmp_reloc| {
951 func.mir_instructions.items(.data)[jmp_reloc].inst =
952 @intCast(func.mir_instructions.len);
959953 }
960954
961 try self.addPseudoNone(.pseudo_dbg_epilogue_begin);
955 try func.addPseudoNone(.pseudo_dbg_epilogue_begin);
962956
963 const backpatch_restore_callee_preserved_regs = try self.addPseudo(.pseudo_dead);
964 const backpatch_ra_restore = try self.addPseudo(.pseudo_dead);
965 const backpatch_fp_restore = try self.addPseudo(.pseudo_dead);
966 const backpatch_stack_alloc_restore = try self.addPseudo(.pseudo_dead);
967 try self.addPseudoNone(.pseudo_ret);
957 const backpatch_restore_callee_preserved_regs = try func.addPseudo(.pseudo_dead);
958 const backpatch_ra_restore = try func.addPseudo(.pseudo_dead);
959 const backpatch_fp_restore = try func.addPseudo(.pseudo_dead);
960 const backpatch_stack_alloc_restore = try func.addPseudo(.pseudo_dead);
961 try func.addPseudoNone(.pseudo_ret);
968962
969 const frame_layout = try self.computeFrameLayout();
963 const frame_layout = try func.computeFrameLayout();
970964 const need_save_reg = frame_layout.save_reg_list.count() > 0;
971965
972 self.mir_instructions.set(backpatch_stack_alloc, .{
966 func.mir_instructions.set(backpatch_stack_alloc, .{
973967 .tag = .addi,
974968 .ops = .rri,
975969 .data = .{ .i_type = .{
......@@ -978,7 +972,7 @@ fn gen(self: *Self) !void {
978972 .imm12 = Immediate.s(-@as(i32, @intCast(frame_layout.stack_adjust))),
979973 } },
980974 });
981 self.mir_instructions.set(backpatch_ra_spill, .{
975 func.mir_instructions.set(backpatch_ra_spill, .{
982976 .tag = .pseudo,
983977 .ops = .pseudo_store_rm,
984978 .data = .{ .rm = .{
......@@ -989,7 +983,7 @@ fn gen(self: *Self) !void {
989983 },
990984 } },
991985 });
992 self.mir_instructions.set(backpatch_ra_restore, .{
986 func.mir_instructions.set(backpatch_ra_restore, .{
993987 .tag = .pseudo,
994988 .ops = .pseudo_load_rm,
995989 .data = .{ .rm = .{
......@@ -1000,7 +994,7 @@ fn gen(self: *Self) !void {
1000994 },
1001995 } },
1002996 });
1003 self.mir_instructions.set(backpatch_fp_spill, .{
997 func.mir_instructions.set(backpatch_fp_spill, .{
1004998 .tag = .pseudo,
1005999 .ops = .pseudo_store_rm,
10061000 .data = .{ .rm = .{
......@@ -1011,7 +1005,7 @@ fn gen(self: *Self) !void {
10111005 },
10121006 } },
10131007 });
1014 self.mir_instructions.set(backpatch_fp_restore, .{
1008 func.mir_instructions.set(backpatch_fp_restore, .{
10151009 .tag = .pseudo,
10161010 .ops = .pseudo_load_rm,
10171011 .data = .{ .rm = .{
......@@ -1022,7 +1016,7 @@ fn gen(self: *Self) !void {
10221016 },
10231017 } },
10241018 });
1025 self.mir_instructions.set(backpatch_fp_add, .{
1019 func.mir_instructions.set(backpatch_fp_add, .{
10261020 .tag = .addi,
10271021 .ops = .rri,
10281022 .data = .{ .i_type = .{
......@@ -1031,7 +1025,7 @@ fn gen(self: *Self) !void {
10311025 .imm12 = Immediate.s(@intCast(frame_layout.stack_adjust)),
10321026 } },
10331027 });
1034 self.mir_instructions.set(backpatch_stack_alloc_restore, .{
1028 func.mir_instructions.set(backpatch_stack_alloc_restore, .{
10351029 .tag = .addi,
10361030 .ops = .rri,
10371031 .data = .{ .i_type = .{
......@@ -1042,72 +1036,72 @@ fn gen(self: *Self) !void {
10421036 });
10431037
10441038 if (need_save_reg) {
1045 self.mir_instructions.set(backpatch_spill_callee_preserved_regs, .{
1039 func.mir_instructions.set(backpatch_spill_callee_preserved_regs, .{
10461040 .tag = .pseudo,
10471041 .ops = .pseudo_spill_regs,
10481042 .data = .{ .reg_list = frame_layout.save_reg_list },
10491043 });
10501044
1051 self.mir_instructions.set(backpatch_restore_callee_preserved_regs, .{
1045 func.mir_instructions.set(backpatch_restore_callee_preserved_regs, .{
10521046 .tag = .pseudo,
10531047 .ops = .pseudo_restore_regs,
10541048 .data = .{ .reg_list = frame_layout.save_reg_list },
10551049 });
10561050 }
10571051 } else {
1058 try self.addPseudoNone(.pseudo_dbg_prologue_end);
1059 try self.genBody(self.air.getMainBody());
1060 try self.addPseudoNone(.pseudo_dbg_epilogue_begin);
1052 try func.addPseudoNone(.pseudo_dbg_prologue_end);
1053 try func.genBody(func.air.getMainBody());
1054 try func.addPseudoNone(.pseudo_dbg_epilogue_begin);
10611055 }
10621056
10631057 // Drop them off at the rbrace.
1064 _ = try self.addInst(.{
1058 _ = try func.addInst(.{
10651059 .tag = .pseudo,
10661060 .ops = .pseudo_dbg_line_column,
10671061 .data = .{ .pseudo_dbg_line_column = .{
1068 .line = self.end_di_line,
1069 .column = self.end_di_column,
1062 .line = func.end_di_line,
1063 .column = func.end_di_column,
10701064 } },
10711065 });
10721066}
10731067
1074fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
1075 const zcu = self.bin_file.comp.module.?;
1068fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void {
1069 const zcu = func.bin_file.comp.module.?;
10761070 const ip = &zcu.intern_pool;
1077 const air_tags = self.air.instructions.items(.tag);
1071 const air_tags = func.air.instructions.items(.tag);
10781072
10791073 for (body) |inst| {
1080 if (self.liveness.isUnused(inst) and !self.air.mustLower(inst, ip)) continue;
1074 if (func.liveness.isUnused(inst) and !func.air.mustLower(inst, ip)) continue;
10811075
1082 const old_air_bookkeeping = self.air_bookkeeping;
1083 try self.inst_tracking.ensureUnusedCapacity(self.gpa, 1);
1076 const old_air_bookkeeping = func.air_bookkeeping;
1077 try func.inst_tracking.ensureUnusedCapacity(func.gpa, 1);
10841078 switch (air_tags[@intFromEnum(inst)]) {
10851079 // zig fmt: off
1086 .ptr_add => try self.airPtrArithmetic(inst, .ptr_add),
1087 .ptr_sub => try self.airPtrArithmetic(inst, .ptr_sub),
1080 .ptr_add => try func.airPtrArithmetic(inst, .ptr_add),
1081 .ptr_sub => try func.airPtrArithmetic(inst, .ptr_sub),
10881082
1089 .add => try self.airBinOp(inst, .add),
1090 .sub => try self.airBinOp(inst, .sub),
1083 .add => try func.airBinOp(inst, .add),
1084 .sub => try func.airBinOp(inst, .sub),
10911085
10921086 .add_safe,
10931087 .sub_safe,
10941088 .mul_safe,
1095 => return self.fail("TODO implement safety_checked_instructions", .{}),
1096
1097 .add_wrap => try self.airAddWrap(inst),
1098 .add_sat => try self.airAddSat(inst),
1099 .sub_wrap => try self.airSubWrap(inst),
1100 .sub_sat => try self.airSubSat(inst),
1101 .mul => try self.airMul(inst),
1102 .mul_wrap => try self.airMulWrap(inst),
1103 .mul_sat => try self.airMulSat(inst),
1104 .rem => try self.airRem(inst),
1105 .mod => try self.airMod(inst),
1106 .shl, .shl_exact => try self.airShl(inst),
1107 .shl_sat => try self.airShlSat(inst),
1108 .min => try self.airMinMax(inst, .min),
1109 .max => try self.airMinMax(inst, .max),
1110 .slice => try self.airSlice(inst),
1089 => return func.fail("TODO implement safety_checked_instructions", .{}),
1090
1091 .add_wrap => try func.airAddWrap(inst),
1092 .add_sat => try func.airAddSat(inst),
1093 .sub_wrap => try func.airSubWrap(inst),
1094 .sub_sat => try func.airSubSat(inst),
1095 .mul => try func.airMul(inst),
1096 .mul_wrap => try func.airMulWrap(inst),
1097 .mul_sat => try func.airMulSat(inst),
1098 .rem => try func.airRem(inst),
1099 .mod => try func.airMod(inst),
1100 .shl, .shl_exact => try func.airShl(inst),
1101 .shl_sat => try func.airShlSat(inst),
1102 .min => try func.airMinMax(inst, .min),
1103 .max => try func.airMinMax(inst, .max),
1104 .slice => try func.airSlice(inst),
11111105
11121106 .sqrt,
11131107 .sin,
......@@ -1123,157 +1117,157 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
11231117 .round,
11241118 .trunc_float,
11251119 .neg,
1126 => try self.airUnaryMath(inst),
1127
1128 .add_with_overflow => try self.airAddWithOverflow(inst),
1129 .sub_with_overflow => try self.airSubWithOverflow(inst),
1130 .mul_with_overflow => try self.airMulWithOverflow(inst),
1131 .shl_with_overflow => try self.airShlWithOverflow(inst),
1132
1133 .div_float, .div_trunc, .div_floor, .div_exact => try self.airDiv(inst),
1134
1135 .cmp_lt => try self.airCmp(inst),
1136 .cmp_lte => try self.airCmp(inst),
1137 .cmp_eq => try self.airCmp(inst),
1138 .cmp_gte => try self.airCmp(inst),
1139 .cmp_gt => try self.airCmp(inst),
1140 .cmp_neq => try self.airCmp(inst),
1141
1142 .cmp_vector => try self.airCmpVector(inst),
1143 .cmp_lt_errors_len => try self.airCmpLtErrorsLen(inst),
1144
1145 .bool_and => try self.airBoolOp(inst),
1146 .bool_or => try self.airBoolOp(inst),
1147 .bit_and => try self.airBitAnd(inst),
1148 .bit_or => try self.airBitOr(inst),
1149 .xor => try self.airXor(inst),
1150 .shr, .shr_exact => try self.airShr(inst),
1151
1152 .alloc => try self.airAlloc(inst),
1153 .ret_ptr => try self.airRetPtr(inst),
1154 .arg => try self.airArg(inst),
1155 .assembly => try self.airAsm(inst),
1156 .bitcast => try self.airBitCast(inst),
1157 .block => try self.airBlock(inst),
1158 .br => try self.airBr(inst),
1159 .trap => try self.airTrap(),
1160 .breakpoint => try self.airBreakpoint(),
1161 .ret_addr => try self.airRetAddr(inst),
1162 .frame_addr => try self.airFrameAddress(inst),
1163 .fence => try self.airFence(),
1164 .cond_br => try self.airCondBr(inst),
1165 .dbg_stmt => try self.airDbgStmt(inst),
1166 .fptrunc => try self.airFptrunc(inst),
1167 .fpext => try self.airFpext(inst),
1168 .intcast => try self.airIntCast(inst),
1169 .trunc => try self.airTrunc(inst),
1170 .int_from_bool => try self.airIntFromBool(inst),
1171 .is_non_null => try self.airIsNonNull(inst),
1172 .is_non_null_ptr => try self.airIsNonNullPtr(inst),
1173 .is_null => try self.airIsNull(inst),
1174 .is_null_ptr => try self.airIsNullPtr(inst),
1175 .is_non_err => try self.airIsNonErr(inst),
1176 .is_non_err_ptr => try self.airIsNonErrPtr(inst),
1177 .is_err => try self.airIsErr(inst),
1178 .is_err_ptr => try self.airIsErrPtr(inst),
1179 .load => try self.airLoad(inst),
1180 .loop => try self.airLoop(inst),
1181 .not => try self.airNot(inst),
1182 .int_from_ptr => try self.airIntFromPtr(inst),
1183 .ret => try self.airRet(inst, false),
1184 .ret_safe => try self.airRet(inst, true),
1185 .ret_load => try self.airRetLoad(inst),
1186 .store => try self.airStore(inst, false),
1187 .store_safe => try self.airStore(inst, true),
1188 .struct_field_ptr=> try self.airStructFieldPtr(inst),
1189 .struct_field_val=> try self.airStructFieldVal(inst),
1190 .array_to_slice => try self.airArrayToSlice(inst),
1191 .float_from_int => try self.airFloatFromInt(inst),
1192 .int_from_float => try self.airIntFromFloat(inst),
1193 .cmpxchg_strong => try self.airCmpxchg(inst),
1194 .cmpxchg_weak => try self.airCmpxchg(inst),
1195 .atomic_rmw => try self.airAtomicRmw(inst),
1196 .atomic_load => try self.airAtomicLoad(inst),
1197 .memcpy => try self.airMemcpy(inst),
1198 .memset => try self.airMemset(inst, false),
1199 .memset_safe => try self.airMemset(inst, true),
1200 .set_union_tag => try self.airSetUnionTag(inst),
1201 .get_union_tag => try self.airGetUnionTag(inst),
1202 .clz => try self.airClz(inst),
1203 .ctz => try self.airCtz(inst),
1204 .popcount => try self.airPopcount(inst),
1205 .abs => try self.airAbs(inst),
1206 .byte_swap => try self.airByteSwap(inst),
1207 .bit_reverse => try self.airBitReverse(inst),
1208 .tag_name => try self.airTagName(inst),
1209 .error_name => try self.airErrorName(inst),
1210 .splat => try self.airSplat(inst),
1211 .select => try self.airSelect(inst),
1212 .shuffle => try self.airShuffle(inst),
1213 .reduce => try self.airReduce(inst),
1214 .aggregate_init => try self.airAggregateInit(inst),
1215 .union_init => try self.airUnionInit(inst),
1216 .prefetch => try self.airPrefetch(inst),
1217 .mul_add => try self.airMulAdd(inst),
1218 .addrspace_cast => return self.fail("TODO: addrspace_cast", .{}),
1219
1220 .@"try" => try self.airTry(inst),
1221 .try_ptr => return self.fail("TODO: try_ptr", .{}),
1120 => try func.airUnaryMath(inst),
1121
1122 .add_with_overflow => try func.airAddWithOverflow(inst),
1123 .sub_with_overflow => try func.airSubWithOverflow(inst),
1124 .mul_with_overflow => try func.airMulWithOverflow(inst),
1125 .shl_with_overflow => try func.airShlWithOverflow(inst),
1126
1127 .div_float, .div_trunc, .div_floor, .div_exact => try func.airDiv(inst),
1128
1129 .cmp_lt => try func.airCmp(inst),
1130 .cmp_lte => try func.airCmp(inst),
1131 .cmp_eq => try func.airCmp(inst),
1132 .cmp_gte => try func.airCmp(inst),
1133 .cmp_gt => try func.airCmp(inst),
1134 .cmp_neq => try func.airCmp(inst),
1135
1136 .cmp_vector => try func.airCmpVector(inst),
1137 .cmp_lt_errors_len => try func.airCmpLtErrorsLen(inst),
1138
1139 .bool_and => try func.airBoolOp(inst),
1140 .bool_or => try func.airBoolOp(inst),
1141 .bit_and => try func.airBitAnd(inst),
1142 .bit_or => try func.airBitOr(inst),
1143 .xor => try func.airXor(inst),
1144 .shr, .shr_exact => try func.airShr(inst),
1145
1146 .alloc => try func.airAlloc(inst),
1147 .ret_ptr => try func.airRetPtr(inst),
1148 .arg => try func.airArg(inst),
1149 .assembly => try func.airAsm(inst),
1150 .bitcast => try func.airBitCast(inst),
1151 .block => try func.airBlock(inst),
1152 .br => try func.airBr(inst),
1153 .trap => try func.airTrap(),
1154 .breakpoint => try func.airBreakpoint(),
1155 .ret_addr => try func.airRetAddr(inst),
1156 .frame_addr => try func.airFrameAddress(inst),
1157 .fence => try func.airFence(),
1158 .cond_br => try func.airCondBr(inst),
1159 .dbg_stmt => try func.airDbgStmt(inst),
1160 .fptrunc => try func.airFptrunc(inst),
1161 .fpext => try func.airFpext(inst),
1162 .intcast => try func.airIntCast(inst),
1163 .trunc => try func.airTrunc(inst),
1164 .int_from_bool => try func.airIntFromBool(inst),
1165 .is_non_null => try func.airIsNonNull(inst),
1166 .is_non_null_ptr => try func.airIsNonNullPtr(inst),
1167 .is_null => try func.airIsNull(inst),
1168 .is_null_ptr => try func.airIsNullPtr(inst),
1169 .is_non_err => try func.airIsNonErr(inst),
1170 .is_non_err_ptr => try func.airIsNonErrPtr(inst),
1171 .is_err => try func.airIsErr(inst),
1172 .is_err_ptr => try func.airIsErrPtr(inst),
1173 .load => try func.airLoad(inst),
1174 .loop => try func.airLoop(inst),
1175 .not => try func.airNot(inst),
1176 .int_from_ptr => try func.airIntFromPtr(inst),
1177 .ret => try func.airRet(inst, false),
1178 .ret_safe => try func.airRet(inst, true),
1179 .ret_load => try func.airRetLoad(inst),
1180 .store => try func.airStore(inst, false),
1181 .store_safe => try func.airStore(inst, true),
1182 .struct_field_ptr=> try func.airStructFieldPtr(inst),
1183 .struct_field_val=> try func.airStructFieldVal(inst),
1184 .array_to_slice => try func.airArrayToSlice(inst),
1185 .float_from_int => try func.airFloatFromInt(inst),
1186 .int_from_float => try func.airIntFromFloat(inst),
1187 .cmpxchg_strong => try func.airCmpxchg(inst),
1188 .cmpxchg_weak => try func.airCmpxchg(inst),
1189 .atomic_rmw => try func.airAtomicRmw(inst),
1190 .atomic_load => try func.airAtomicLoad(inst),
1191 .memcpy => try func.airMemcpy(inst),
1192 .memset => try func.airMemset(inst, false),
1193 .memset_safe => try func.airMemset(inst, true),
1194 .set_union_tag => try func.airSetUnionTag(inst),
1195 .get_union_tag => try func.airGetUnionTag(inst),
1196 .clz => try func.airClz(inst),
1197 .ctz => try func.airCtz(inst),
1198 .popcount => try func.airPopcount(inst),
1199 .abs => try func.airAbs(inst),
1200 .byte_swap => try func.airByteSwap(inst),
1201 .bit_reverse => try func.airBitReverse(inst),
1202 .tag_name => try func.airTagName(inst),
1203 .error_name => try func.airErrorName(inst),
1204 .splat => try func.airSplat(inst),
1205 .select => try func.airSelect(inst),
1206 .shuffle => try func.airShuffle(inst),
1207 .reduce => try func.airReduce(inst),
1208 .aggregate_init => try func.airAggregateInit(inst),
1209 .union_init => try func.airUnionInit(inst),
1210 .prefetch => try func.airPrefetch(inst),
1211 .mul_add => try func.airMulAdd(inst),
1212 .addrspace_cast => return func.fail("TODO: addrspace_cast", .{}),
1213
1214 .@"try" => try func.airTry(inst),
1215 .try_ptr => return func.fail("TODO: try_ptr", .{}),
12221216
12231217 .dbg_var_ptr,
12241218 .dbg_var_val,
1225 => try self.airDbgVar(inst),
1219 => try func.airDbgVar(inst),
12261220
1227 .dbg_inline_block => try self.airDbgInlineBlock(inst),
1221 .dbg_inline_block => try func.airDbgInlineBlock(inst),
12281222
1229 .call => try self.airCall(inst, .auto),
1230 .call_always_tail => try self.airCall(inst, .always_tail),
1231 .call_never_tail => try self.airCall(inst, .never_tail),
1232 .call_never_inline => try self.airCall(inst, .never_inline),
1223 .call => try func.airCall(inst, .auto),
1224 .call_always_tail => try func.airCall(inst, .always_tail),
1225 .call_never_tail => try func.airCall(inst, .never_tail),
1226 .call_never_inline => try func.airCall(inst, .never_inline),
12331227
1234 .atomic_store_unordered => try self.airAtomicStore(inst, .unordered),
1235 .atomic_store_monotonic => try self.airAtomicStore(inst, .monotonic),
1236 .atomic_store_release => try self.airAtomicStore(inst, .release),
1237 .atomic_store_seq_cst => try self.airAtomicStore(inst, .seq_cst),
1228 .atomic_store_unordered => try func.airAtomicStore(inst, .unordered),
1229 .atomic_store_monotonic => try func.airAtomicStore(inst, .monotonic),
1230 .atomic_store_release => try func.airAtomicStore(inst, .release),
1231 .atomic_store_seq_cst => try func.airAtomicStore(inst, .seq_cst),
12381232
1239 .struct_field_ptr_index_0 => try self.airStructFieldPtrIndex(inst, 0),
1240 .struct_field_ptr_index_1 => try self.airStructFieldPtrIndex(inst, 1),
1241 .struct_field_ptr_index_2 => try self.airStructFieldPtrIndex(inst, 2),
1242 .struct_field_ptr_index_3 => try self.airStructFieldPtrIndex(inst, 3),
1233 .struct_field_ptr_index_0 => try func.airStructFieldPtrIndex(inst, 0),
1234 .struct_field_ptr_index_1 => try func.airStructFieldPtrIndex(inst, 1),
1235 .struct_field_ptr_index_2 => try func.airStructFieldPtrIndex(inst, 2),
1236 .struct_field_ptr_index_3 => try func.airStructFieldPtrIndex(inst, 3),
12431237
1244 .field_parent_ptr => try self.airFieldParentPtr(inst),
1238 .field_parent_ptr => try func.airFieldParentPtr(inst),
12451239
1246 .switch_br => try self.airSwitchBr(inst),
1247 .slice_ptr => try self.airSlicePtr(inst),
1248 .slice_len => try self.airSliceLen(inst),
1240 .switch_br => try func.airSwitchBr(inst),
1241 .slice_ptr => try func.airSlicePtr(inst),
1242 .slice_len => try func.airSliceLen(inst),
12491243
1250 .ptr_slice_len_ptr => try self.airPtrSliceLenPtr(inst),
1251 .ptr_slice_ptr_ptr => try self.airPtrSlicePtrPtr(inst),
1244 .ptr_slice_len_ptr => try func.airPtrSliceLenPtr(inst),
1245 .ptr_slice_ptr_ptr => try func.airPtrSlicePtrPtr(inst),
12521246
1253 .array_elem_val => try self.airArrayElemVal(inst),
1254 .slice_elem_val => try self.airSliceElemVal(inst),
1255 .slice_elem_ptr => try self.airSliceElemPtr(inst),
1256 .ptr_elem_val => try self.airPtrElemVal(inst),
1257 .ptr_elem_ptr => try self.airPtrElemPtr(inst),
1247 .array_elem_val => try func.airArrayElemVal(inst),
1248 .slice_elem_val => try func.airSliceElemVal(inst),
1249 .slice_elem_ptr => try func.airSliceElemPtr(inst),
1250 .ptr_elem_val => try func.airPtrElemVal(inst),
1251 .ptr_elem_ptr => try func.airPtrElemPtr(inst),
12581252
12591253 .inferred_alloc, .inferred_alloc_comptime => unreachable,
1260 .unreach => self.finishAirBookkeeping(),
1261
1262 .optional_payload => try self.airOptionalPayload(inst),
1263 .optional_payload_ptr => try self.airOptionalPayloadPtr(inst),
1264 .optional_payload_ptr_set => try self.airOptionalPayloadPtrSet(inst),
1265 .unwrap_errunion_err => try self.airUnwrapErrErr(inst),
1266 .unwrap_errunion_payload => try self.airUnwrapErrPayload(inst),
1267 .unwrap_errunion_err_ptr => try self.airUnwrapErrErrPtr(inst),
1268 .unwrap_errunion_payload_ptr=> try self.airUnwrapErrPayloadPtr(inst),
1269 .errunion_payload_ptr_set => try self.airErrUnionPayloadPtrSet(inst),
1270 .err_return_trace => try self.airErrReturnTrace(inst),
1271 .set_err_return_trace => try self.airSetErrReturnTrace(inst),
1272 .save_err_return_trace_index=> try self.airSaveErrReturnTraceIndex(inst),
1273
1274 .wrap_optional => try self.airWrapOptional(inst),
1275 .wrap_errunion_payload => try self.airWrapErrUnionPayload(inst),
1276 .wrap_errunion_err => try self.airWrapErrUnionErr(inst),
1254 .unreach => func.finishAirBookkeeping(),
1255
1256 .optional_payload => try func.airOptionalPayload(inst),
1257 .optional_payload_ptr => try func.airOptionalPayloadPtr(inst),
1258 .optional_payload_ptr_set => try func.airOptionalPayloadPtrSet(inst),
1259 .unwrap_errunion_err => try func.airUnwrapErrErr(inst),
1260 .unwrap_errunion_payload => try func.airUnwrapErrPayload(inst),
1261 .unwrap_errunion_err_ptr => try func.airUnwrapErrErrPtr(inst),
1262 .unwrap_errunion_payload_ptr=> try func.airUnwrapErrPayloadPtr(inst),
1263 .errunion_payload_ptr_set => try func.airErrUnionPayloadPtrSet(inst),
1264 .err_return_trace => try func.airErrReturnTrace(inst),
1265 .set_err_return_trace => try func.airSetErrReturnTrace(inst),
1266 .save_err_return_trace_index=> try func.airSaveErrReturnTraceIndex(inst),
1267
1268 .wrap_optional => try func.airWrapOptional(inst),
1269 .wrap_errunion_payload => try func.airWrapErrUnionPayload(inst),
1270 .wrap_errunion_err => try func.airWrapErrUnionErr(inst),
12771271
12781272 .add_optimized,
12791273 .sub_optimized,
......@@ -1294,16 +1288,16 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
12941288 .cmp_vector_optimized,
12951289 .reduce_optimized,
12961290 .int_from_float_optimized,
1297 => return self.fail("TODO implement optimized float mode", .{}),
1291 => return func.fail("TODO implement optimized float mode", .{}),
12981292
1299 .is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}),
1300 .error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}),
1301 .vector_store_elem => return self.fail("TODO implement vector_store_elem", .{}),
1293 .is_named_enum_value => return func.fail("TODO implement is_named_enum_value", .{}),
1294 .error_set_has_value => return func.fail("TODO implement error_set_has_value", .{}),
1295 .vector_store_elem => return func.fail("TODO implement vector_store_elem", .{}),
13021296
1303 .c_va_arg => return self.fail("TODO implement c_va_arg", .{}),
1304 .c_va_copy => return self.fail("TODO implement c_va_copy", .{}),
1305 .c_va_end => return self.fail("TODO implement c_va_end", .{}),
1306 .c_va_start => return self.fail("TODO implement c_va_start", .{}),
1297 .c_va_arg => return func.fail("TODO implement c_va_arg", .{}),
1298 .c_va_copy => return func.fail("TODO implement c_va_copy", .{}),
1299 .c_va_end => return func.fail("TODO implement c_va_end", .{}),
1300 .c_va_start => return func.fail("TODO implement c_va_start", .{}),
13071301
13081302 .wasm_memory_size => unreachable,
13091303 .wasm_memory_grow => unreachable,
......@@ -1314,19 +1308,19 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
13141308 // zig fmt: on
13151309 }
13161310
1317 assert(!self.register_manager.lockedRegsExist());
1311 assert(!func.register_manager.lockedRegsExist());
13181312
13191313 if (std.debug.runtime_safety) {
1320 if (self.air_bookkeeping < old_air_bookkeeping + 1) {
1314 if (func.air_bookkeeping < old_air_bookkeeping + 1) {
13211315 std.debug.panic("in codegen.zig, handling of AIR instruction %{d} ('{}') did not do proper bookkeeping. Look for a missing call to finishAir.", .{ inst, air_tags[@intFromEnum(inst)] });
13221316 }
13231317
13241318 { // check consistency of tracked registers
1325 var it = self.register_manager.free_registers.iterator(.{ .kind = .unset });
1319 var it = func.register_manager.free_registers.iterator(.{ .kind = .unset });
13261320 while (it.next()) |index| {
1327 const tracked_inst = self.register_manager.registers[index];
1321 const tracked_inst = func.register_manager.registers[index];
13281322 tracking_log.debug("tracked inst: {}", .{tracked_inst});
1329 const tracking = self.getResolvedInstValue(tracked_inst);
1323 const tracking = func.getResolvedInstValue(tracked_inst);
13301324 for (tracking.getRegs()) |reg| {
13311325 if (RegisterManager.indexOfRegIntoTracked(reg).? == index) break;
13321326 } else return std.debug.panic(
......@@ -1338,72 +1332,72 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
13381332 }
13391333}
13401334
1341fn getValue(self: *Self, value: MCValue, inst: ?Air.Inst.Index) !void {
1342 for (value.getRegs()) |reg| try self.register_manager.getReg(reg, inst);
1335fn getValue(func: *Func, value: MCValue, inst: ?Air.Inst.Index) !void {
1336 for (value.getRegs()) |reg| try func.register_manager.getReg(reg, inst);
13431337}
13441338
1345fn getValueIfFree(self: *Self, value: MCValue, inst: ?Air.Inst.Index) void {
1346 for (value.getRegs()) |reg| if (self.register_manager.isRegFree(reg))
1347 self.register_manager.getRegAssumeFree(reg, inst);
1339fn getValueIfFree(func: *Func, value: MCValue, inst: ?Air.Inst.Index) void {
1340 for (value.getRegs()) |reg| if (func.register_manager.isRegFree(reg))
1341 func.register_manager.getRegAssumeFree(reg, inst);
13481342}
13491343
1350fn freeValue(self: *Self, value: MCValue) !void {
1344fn freeValue(func: *Func, value: MCValue) !void {
13511345 switch (value) {
1352 .register => |reg| self.register_manager.freeReg(reg),
1353 .register_pair => |regs| for (regs) |reg| self.register_manager.freeReg(reg),
1354 .register_offset => |reg_off| self.register_manager.freeReg(reg_off.reg),
1346 .register => |reg| func.register_manager.freeReg(reg),
1347 .register_pair => |regs| for (regs) |reg| func.register_manager.freeReg(reg),
1348 .register_offset => |reg_off| func.register_manager.freeReg(reg_off.reg),
13551349 else => {}, // TODO process stack allocation death
13561350 }
13571351}
13581352
1359fn feed(self: *Self, bt: *Liveness.BigTomb, operand: Air.Inst.Ref) !void {
1353fn feed(func: *Func, bt: *Liveness.BigTomb, operand: Air.Inst.Ref) !void {
13601354 if (bt.feed()) if (operand.toIndex()) |inst| {
13611355 log.debug("feed inst: %{}", .{inst});
1362 try self.processDeath(inst);
1356 try func.processDeath(inst);
13631357 };
13641358}
13651359
13661360/// Asserts there is already capacity to insert into top branch inst_table.
1367fn processDeath(self: *Self, inst: Air.Inst.Index) !void {
1368 try self.inst_tracking.getPtr(inst).?.die(self, inst);
1361fn processDeath(func: *Func, inst: Air.Inst.Index) !void {
1362 try func.inst_tracking.getPtr(inst).?.die(func, inst);
13691363}
13701364
13711365/// Called when there are no operands, and the instruction is always unreferenced.
1372fn finishAirBookkeeping(self: *Self) void {
1366fn finishAirBookkeeping(func: *Func) void {
13731367 if (std.debug.runtime_safety) {
1374 self.air_bookkeeping += 1;
1368 func.air_bookkeeping += 1;
13751369 }
13761370}
13771371
1378fn finishAirResult(self: *Self, inst: Air.Inst.Index, result: MCValue) void {
1379 if (self.liveness.isUnused(inst)) switch (result) {
1372fn finishAirResult(func: *Func, inst: Air.Inst.Index, result: MCValue) void {
1373 if (func.liveness.isUnused(inst)) switch (result) {
13801374 .none, .dead, .unreach => {},
13811375 else => unreachable, // Why didn't the result die?
13821376 } else {
13831377 tracking_log.debug("%{d} => {} (birth)", .{ inst, result });
1384 self.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(result));
1378 func.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(result));
13851379 // In some cases, an operand may be reused as the result.
13861380 // If that operand died and was a register, it was freed by
13871381 // processDeath, so we have to "re-allocate" the register.
1388 self.getValueIfFree(result, inst);
1382 func.getValueIfFree(result, inst);
13891383 }
1390 self.finishAirBookkeeping();
1384 func.finishAirBookkeeping();
13911385}
13921386
13931387fn finishAir(
1394 self: *Self,
1388 func: *Func,
13951389 inst: Air.Inst.Index,
13961390 result: MCValue,
13971391 operands: [Liveness.bpi - 1]Air.Inst.Ref,
13981392) !void {
1399 var tomb_bits = self.liveness.getTombBits(inst);
1393 var tomb_bits = func.liveness.getTombBits(inst);
14001394 for (operands) |op| {
14011395 const dies = @as(u1, @truncate(tomb_bits)) != 0;
14021396 tomb_bits >>= 1;
14031397 if (!dies) continue;
1404 try self.processDeath(op.toIndexAllowNone() orelse continue);
1398 try func.processDeath(op.toIndexAllowNone() orelse continue);
14051399 }
1406 self.finishAirResult(inst, result);
1400 func.finishAirResult(inst, result);
14071401}
14081402
14091403const FrameLayout = struct {
......@@ -1412,7 +1406,7 @@ const FrameLayout = struct {
14121406};
14131407
14141408fn setFrameLoc(
1415 self: *Self,
1409 func: *Func,
14161410 frame_index: FrameIndex,
14171411 base: Register,
14181412 offset: *i32,
......@@ -1420,24 +1414,24 @@ fn setFrameLoc(
14201414) void {
14211415 const frame_i = @intFromEnum(frame_index);
14221416 if (aligned) {
1423 const alignment: InternPool.Alignment = self.frame_allocs.items(.abi_align)[frame_i];
1417 const alignment: InternPool.Alignment = func.frame_allocs.items(.abi_align)[frame_i];
14241418 offset.* = if (math.sign(offset.*) < 0)
14251419 -1 * @as(i32, @intCast(alignment.backward(@intCast(@abs(offset.*)))))
14261420 else
14271421 @intCast(alignment.forward(@intCast(@abs(offset.*))));
14281422 }
1429 self.frame_locs.set(frame_i, .{ .base = base, .disp = offset.* });
1430 offset.* += self.frame_allocs.items(.abi_size)[frame_i];
1423 func.frame_locs.set(frame_i, .{ .base = base, .disp = offset.* });
1424 offset.* += func.frame_allocs.items(.abi_size)[frame_i];
14311425}
14321426
1433fn computeFrameLayout(self: *Self) !FrameLayout {
1434 const frame_allocs_len = self.frame_allocs.len;
1435 try self.frame_locs.resize(self.gpa, frame_allocs_len);
1436 const stack_frame_order = try self.gpa.alloc(FrameIndex, frame_allocs_len - FrameIndex.named_count);
1437 defer self.gpa.free(stack_frame_order);
1427fn computeFrameLayout(func: *Func) !FrameLayout {
1428 const frame_allocs_len = func.frame_allocs.len;
1429 try func.frame_locs.resize(func.gpa, frame_allocs_len);
1430 const stack_frame_order = try func.gpa.alloc(FrameIndex, frame_allocs_len - FrameIndex.named_count);
1431 defer func.gpa.free(stack_frame_order);
14381432
1439 const frame_size = self.frame_allocs.items(.abi_size);
1440 const frame_align = self.frame_allocs.items(.abi_align);
1433 const frame_size = func.frame_allocs.items(.abi_size);
1434 const frame_align = func.frame_allocs.items(.abi_align);
14411435
14421436 for (stack_frame_order, FrameIndex.named_count..) |*frame_order, frame_index|
14431437 frame_order.* = @enumFromInt(frame_index);
......@@ -1455,7 +1449,7 @@ fn computeFrameLayout(self: *Self) !FrameLayout {
14551449
14561450 var save_reg_list = Mir.RegisterList{};
14571451 for (abi.Registers.all_preserved) |reg| {
1458 if (self.register_manager.isRegAllocated(reg)) {
1452 if (func.register_manager.isRegAllocated(reg)) {
14591453 save_reg_list.push(&abi.Registers.all_preserved, reg);
14601454 }
14611455 }
......@@ -1489,11 +1483,11 @@ fn computeFrameLayout(self: *Self) !FrameLayout {
14891483
14901484 // store the ra at total_size - 8, so it's the very first thing in the stack
14911485 // relative to the fp
1492 self.frame_locs.set(
1486 func.frame_locs.set(
14931487 @intFromEnum(FrameIndex.ret_addr),
14941488 .{ .base = .sp, .disp = acc_frame_size - 8 },
14951489 );
1496 self.frame_locs.set(
1490 func.frame_locs.set(
14971491 @intFromEnum(FrameIndex.base_ptr),
14981492 .{ .base = .sp, .disp = acc_frame_size - 16 },
14991493 );
......@@ -1502,11 +1496,11 @@ fn computeFrameLayout(self: *Self) !FrameLayout {
15021496 // not need to know the size of the first allocation. Stack offsets point at the "bottom"
15031497 // of variables.
15041498 var s0_offset: i32 = -acc_frame_size;
1505 self.setFrameLoc(.stack_frame, .s0, &s0_offset, true);
1506 for (stack_frame_order) |frame_index| self.setFrameLoc(frame_index, .s0, &s0_offset, true);
1507 self.setFrameLoc(.args_frame, .s0, &s0_offset, true);
1508 self.setFrameLoc(.call_frame, .s0, &s0_offset, true);
1509 self.setFrameLoc(.spill_frame, .s0, &s0_offset, true);
1499 func.setFrameLoc(.stack_frame, .s0, &s0_offset, true);
1500 for (stack_frame_order) |frame_index| func.setFrameLoc(frame_index, .s0, &s0_offset, true);
1501 func.setFrameLoc(.args_frame, .s0, &s0_offset, true);
1502 func.setFrameLoc(.call_frame, .s0, &s0_offset, true);
1503 func.setFrameLoc(.spill_frame, .s0, &s0_offset, true);
15101504
15111505 return .{
15121506 .stack_adjust = @intCast(acc_frame_size),
......@@ -1514,21 +1508,21 @@ fn computeFrameLayout(self: *Self) !FrameLayout {
15141508 };
15151509}
15161510
1517fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void {
1518 const table = &self.branch_stack.items[self.branch_stack.items.len - 1].inst_table;
1519 try table.ensureUnusedCapacity(self.gpa, additional_count);
1511fn ensureProcessDeathCapacity(func: *Func, additional_count: usize) !void {
1512 const table = &func.branch_stack.items[func.branch_stack.items.len - 1].inst_table;
1513 try table.ensureUnusedCapacity(func.gpa, additional_count);
15201514}
15211515
1522fn memSize(self: *Self, ty: Type) Memory.Size {
1523 const mod = self.bin_file.comp.module.?;
1516fn memSize(func: *Func, ty: Type) Memory.Size {
1517 const mod = func.bin_file.comp.module.?;
15241518 return switch (ty.zigTypeTag(mod)) {
1525 .Float => Memory.Size.fromBitSize(ty.floatBits(self.target.*)),
1519 .Float => Memory.Size.fromBitSize(ty.floatBits(func.target.*)),
15261520 else => Memory.Size.fromByteSize(ty.abiSize(mod)),
15271521 };
15281522}
15291523
1530fn splitType(self: *Self, ty: Type) ![2]Type {
1531 const zcu = self.bin_file.comp.module.?;
1524fn splitType(func: *Func, ty: Type) ![2]Type {
1525 const zcu = func.bin_file.comp.module.?;
15321526 const classes = mem.sliceTo(&abi.classifySystem(ty, zcu), .none);
15331527 var parts: [2]Type = undefined;
15341528 if (classes.len == 2) for (&parts, classes, 0..) |*part, class, part_i| {
......@@ -1545,63 +1539,63 @@ fn splitType(self: *Self, ty: Type) ![2]Type {
15451539 },
15461540 else => unreachable,
15471541 },
1548 else => return self.fail("TODO: splitType class {}", .{class}),
1542 else => return func.fail("TODO: splitType class {}", .{class}),
15491543 };
15501544 } else if (parts[0].abiSize(zcu) + parts[1].abiSize(zcu) == ty.abiSize(zcu)) return parts;
1551 return self.fail("TODO implement splitType for {}", .{ty.fmt(zcu)});
1545 return func.fail("TODO implement splitType for {}", .{ty.fmt(zcu)});
15521546}
15531547
1554fn symbolIndex(self: *Self) !u32 {
1555 const zcu = self.bin_file.comp.module.?;
1556 const decl_index = zcu.funcOwnerDeclIndex(self.func_index);
1557 return switch (self.bin_file.tag) {
1548fn symbolIndex(func: *Func) !u32 {
1549 const zcu = func.bin_file.comp.module.?;
1550 const decl_index = zcu.funcOwnerDeclIndex(func.func_index);
1551 return switch (func.bin_file.tag) {
15581552 .elf => blk: {
1559 const elf_file = self.bin_file.cast(link.File.Elf).?;
1553 const elf_file = func.bin_file.cast(link.File.Elf).?;
15601554 const atom_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index);
15611555 break :blk atom_index;
15621556 },
1563 else => return self.fail("TODO symbolIndex {s}", .{@tagName(self.bin_file.tag)}),
1557 else => return func.fail("TODO symbolIndex {s}", .{@tagName(func.bin_file.tag)}),
15641558 };
15651559}
15661560
1567fn allocFrameIndex(self: *Self, alloc: FrameAlloc) !FrameIndex {
1568 const frame_allocs_slice = self.frame_allocs.slice();
1561fn allocFrameIndex(func: *Func, alloc: FrameAlloc) !FrameIndex {
1562 const frame_allocs_slice = func.frame_allocs.slice();
15691563 const frame_size = frame_allocs_slice.items(.abi_size);
15701564 const frame_align = frame_allocs_slice.items(.abi_align);
15711565
15721566 const stack_frame_align = &frame_align[@intFromEnum(FrameIndex.stack_frame)];
15731567 stack_frame_align.* = stack_frame_align.max(alloc.abi_align);
15741568
1575 for (self.free_frame_indices.keys(), 0..) |frame_index, free_i| {
1569 for (func.free_frame_indices.keys(), 0..) |frame_index, free_i| {
15761570 const abi_size = frame_size[@intFromEnum(frame_index)];
15771571 if (abi_size != alloc.abi_size) continue;
15781572 const abi_align = &frame_align[@intFromEnum(frame_index)];
15791573 abi_align.* = abi_align.max(alloc.abi_align);
15801574
1581 _ = self.free_frame_indices.swapRemoveAt(free_i);
1575 _ = func.free_frame_indices.swapRemoveAt(free_i);
15821576 return frame_index;
15831577 }
1584 const frame_index: FrameIndex = @enumFromInt(self.frame_allocs.len);
1585 try self.frame_allocs.append(self.gpa, alloc);
1578 const frame_index: FrameIndex = @enumFromInt(func.frame_allocs.len);
1579 try func.frame_allocs.append(func.gpa, alloc);
15861580 log.debug("allocated frame {}", .{frame_index});
15871581 return frame_index;
15881582}
15891583
15901584/// Use a pointer instruction as the basis for allocating stack memory.
1591fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !FrameIndex {
1592 const zcu = self.bin_file.comp.module.?;
1593 const ptr_ty = self.typeOfIndex(inst);
1585fn allocMemPtr(func: *Func, inst: Air.Inst.Index) !FrameIndex {
1586 const zcu = func.bin_file.comp.module.?;
1587 const ptr_ty = func.typeOfIndex(inst);
15941588 const val_ty = ptr_ty.childType(zcu);
1595 return self.allocFrameIndex(FrameAlloc.init(.{
1589 return func.allocFrameIndex(FrameAlloc.init(.{
15961590 .size = math.cast(u32, val_ty.abiSize(zcu)) orelse {
1597 return self.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(zcu)});
1591 return func.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(zcu)});
15981592 },
15991593 .alignment = ptr_ty.ptrAlignment(zcu).max(.@"1"),
16001594 }));
16011595}
16021596
1603fn typeRegClass(self: *Self, ty: Type) abi.RegisterClass {
1604 const zcu = self.bin_file.comp.module.?;
1597fn typeRegClass(func: *Func, ty: Type) abi.RegisterClass {
1598 const zcu = func.bin_file.comp.module.?;
16051599 return switch (ty.zigTypeTag(zcu)) {
16061600 .Float => .float,
16071601 .Vector => @panic("TODO: typeRegClass for Vectors"),
......@@ -1609,8 +1603,8 @@ fn typeRegClass(self: *Self, ty: Type) abi.RegisterClass {
16091603 };
16101604}
16111605
1612fn regGeneralClassForType(self: *Self, ty: Type) RegisterManager.RegisterBitSet {
1613 const zcu = self.bin_file.comp.module.?;
1606fn regGeneralClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet {
1607 const zcu = func.bin_file.comp.module.?;
16141608 return switch (ty.zigTypeTag(zcu)) {
16151609 .Float => abi.Registers.Float.general_purpose,
16161610 .Vector => @panic("TODO: regGeneralClassForType for Vectors"),
......@@ -1618,8 +1612,8 @@ fn regGeneralClassForType(self: *Self, ty: Type) RegisterManager.RegisterBitSet
16181612 };
16191613}
16201614
1621fn regTempClassForType(self: *Self, ty: Type) RegisterManager.RegisterBitSet {
1622 const zcu = self.bin_file.comp.module.?;
1615fn regTempClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet {
1616 const zcu = func.bin_file.comp.module.?;
16231617 return switch (ty.zigTypeTag(zcu)) {
16241618 .Float => abi.Registers.Float.temporary,
16251619 .Vector => @panic("TODO: regTempClassForType for Vectors"),
......@@ -1627,12 +1621,12 @@ fn regTempClassForType(self: *Self, ty: Type) RegisterManager.RegisterBitSet {
16271621 };
16281622}
16291623
1630fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue {
1631 const zcu = self.bin_file.comp.module.?;
1632 const elem_ty = self.typeOfIndex(inst);
1624fn allocRegOrMem(func: *Func, inst: Air.Inst.Index, reg_ok: bool) !MCValue {
1625 const zcu = func.bin_file.comp.module.?;
1626 const elem_ty = func.typeOfIndex(inst);
16331627
16341628 const abi_size = math.cast(u32, elem_ty.abiSize(zcu)) orelse {
1635 return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(zcu)});
1629 return func.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(zcu)});
16361630 };
16371631
16381632 const min_size: u32 = switch (elem_ty.zigTypeTag(zcu)) {
......@@ -1642,20 +1636,20 @@ fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue {
16421636 };
16431637
16441638 if (reg_ok and abi_size <= min_size) {
1645 if (self.register_manager.tryAllocReg(inst, self.regGeneralClassForType(elem_ty))) |reg| {
1639 if (func.register_manager.tryAllocReg(inst, func.regGeneralClassForType(elem_ty))) |reg| {
16461640 return .{ .register = reg };
16471641 }
16481642 }
16491643
1650 const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(elem_ty, zcu));
1644 const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(elem_ty, zcu));
16511645 return .{ .load_frame = .{ .index = frame_index } };
16521646}
16531647
16541648/// Allocates a register from the general purpose set and returns the Register and the Lock.
16551649///
16561650/// Up to the caller to unlock the register later.
1657fn allocReg(self: *Self, reg_class: abi.RegisterClass) !struct { Register, RegisterLock } {
1658 if (reg_class == .float and !self.hasFeature(.f))
1651fn allocReg(func: *Func, reg_class: abi.RegisterClass) !struct { Register, RegisterLock } {
1652 if (reg_class == .float and !func.hasFeature(.f))
16591653 std.debug.panic("allocReg class == float where F isn't enabled", .{});
16601654
16611655 const class = switch (reg_class) {
......@@ -1663,8 +1657,8 @@ fn allocReg(self: *Self, reg_class: abi.RegisterClass) !struct { Register, Regis
16631657 .float => abi.Registers.Float.general_purpose,
16641658 };
16651659
1666 const reg = try self.register_manager.allocReg(null, class);
1667 const lock = self.register_manager.lockRegAssumeUnused(reg);
1660 const reg = try func.register_manager.allocReg(null, class);
1661 const lock = func.register_manager.lockRegAssumeUnused(reg);
16681662 return .{ reg, lock };
16691663}
16701664
......@@ -1677,8 +1671,8 @@ const PromoteOptions = struct {
16771671
16781672/// Similar to `allocReg` but will copy the MCValue into the Register unless `operand` is already
16791673/// a register, in which case it will return a possible lock to that register.
1680fn promoteReg(self: *Self, ty: Type, operand: MCValue, options: PromoteOptions) !struct { Register, ?RegisterLock } {
1681 const zcu = self.bin_file.comp.module.?;
1674fn promoteReg(func: *Func, ty: Type, operand: MCValue, options: PromoteOptions) !struct { Register, ?RegisterLock } {
1675 const zcu = func.bin_file.comp.module.?;
16821676 const bit_size = ty.bitSize(zcu);
16831677
16841678 if (operand == .register) {
......@@ -1687,7 +1681,7 @@ fn promoteReg(self: *Self, ty: Type, operand: MCValue, options: PromoteOptions)
16871681 // we make sure to emit the truncate manually because binOp will call this function
16881682 // and it could cause an infinite loop
16891683
1690 _ = try self.addInst(.{
1684 _ = try func.addInst(.{
16911685 .tag = .slli,
16921686 .ops = .rri,
16931687 .data = .{
......@@ -1699,7 +1693,7 @@ fn promoteReg(self: *Self, ty: Type, operand: MCValue, options: PromoteOptions)
16991693 },
17001694 });
17011695
1702 _ = try self.addInst(.{
1696 _ = try func.addInst(.{
17031697 .tag = .srli,
17041698 .ops = .rri,
17051699 .data = .{
......@@ -1712,13 +1706,13 @@ fn promoteReg(self: *Self, ty: Type, operand: MCValue, options: PromoteOptions)
17121706 });
17131707 }
17141708
1715 return .{ op_reg, self.register_manager.lockReg(operand.register) };
1709 return .{ op_reg, func.register_manager.lockReg(operand.register) };
17161710 }
17171711
1718 const reg, const lock = try self.allocReg(self.typeRegClass(ty));
1712 const reg, const lock = try func.allocReg(func.typeRegClass(ty));
17191713
17201714 if (options.zero and reg.class() == .int) {
1721 _ = try self.addInst(.{
1715 _ = try func.addInst(.{
17221716 .tag = .pseudo,
17231717 .ops = .pseudo_mv,
17241718 .data = .{ .rr = .{
......@@ -1728,26 +1722,26 @@ fn promoteReg(self: *Self, ty: Type, operand: MCValue, options: PromoteOptions)
17281722 });
17291723 }
17301724
1731 try self.genSetReg(ty, reg, operand);
1725 try func.genSetReg(ty, reg, operand);
17321726 return .{ reg, lock };
17331727}
17341728
1735fn elemOffset(self: *Self, index_ty: Type, index: MCValue, elem_size: u64) !Register {
1729fn elemOffset(func: *Func, index_ty: Type, index: MCValue, elem_size: u64) !Register {
17361730 const reg: Register = blk: {
17371731 switch (index) {
17381732 .immediate => |imm| {
17391733 // Optimisation: if index MCValue is an immediate, we can multiply in `comptime`
17401734 // and set the register directly to the scaled offset as an immediate.
1741 const reg = try self.register_manager.allocReg(null, self.regGeneralClassForType(index_ty));
1742 try self.genSetReg(index_ty, reg, .{ .immediate = imm * elem_size });
1735 const reg = try func.register_manager.allocReg(null, func.regGeneralClassForType(index_ty));
1736 try func.genSetReg(index_ty, reg, .{ .immediate = imm * elem_size });
17431737 break :blk reg;
17441738 },
17451739 else => {
1746 const reg = try self.copyToTmpRegister(index_ty, index);
1747 const lock = self.register_manager.lockRegAssumeUnused(reg);
1748 defer self.register_manager.unlockReg(lock);
1740 const reg = try func.copyToTmpRegister(index_ty, index);
1741 const lock = func.register_manager.lockRegAssumeUnused(reg);
1742 defer func.register_manager.unlockReg(lock);
17491743
1750 const result = try self.binOp(
1744 const result = try func.binOp(
17511745 .mul,
17521746 .{ .register = reg },
17531747 index_ty,
......@@ -1761,72 +1755,72 @@ fn elemOffset(self: *Self, index_ty: Type, index: MCValue, elem_size: u64) !Regi
17611755 return reg;
17621756}
17631757
1764pub fn spillInstruction(self: *Self, reg: Register, inst: Air.Inst.Index) !void {
1765 const tracking = self.inst_tracking.getPtr(inst) orelse return;
1758pub fn spillInstruction(func: *Func, reg: Register, inst: Air.Inst.Index) !void {
1759 const tracking = func.inst_tracking.getPtr(inst) orelse return;
17661760 for (tracking.getRegs()) |tracked_reg| {
17671761 if (tracked_reg.id() == reg.id()) break;
17681762 } else unreachable; // spilled reg not tracked with spilled instruciton
1769 try tracking.spill(self, inst);
1770 try tracking.trackSpill(self, inst);
1763 try tracking.spill(func, inst);
1764 try tracking.trackSpill(func, inst);
17711765}
17721766
17731767/// Copies a value to a register without tracking the register. The register is not considered
17741768/// allocated. A second call to `copyToTmpRegister` may return the same register.
17751769/// This can have a side effect of spilling instructions to the stack to free up a register.
1776fn copyToTmpRegister(self: *Self, ty: Type, mcv: MCValue) !Register {
1777 log.debug("copyToTmpRegister ty: {}", .{ty.fmt(self.bin_file.comp.module.?)});
1778 const reg = try self.register_manager.allocReg(null, self.regTempClassForType(ty));
1779 try self.genSetReg(ty, reg, mcv);
1770fn copyToTmpRegister(func: *Func, ty: Type, mcv: MCValue) !Register {
1771 log.debug("copyToTmpRegister ty: {}", .{ty.fmt(func.bin_file.comp.module.?)});
1772 const reg = try func.register_manager.allocReg(null, func.regTempClassForType(ty));
1773 try func.genSetReg(ty, reg, mcv);
17801774 return reg;
17811775}
17821776
17831777/// Allocates a new register and copies `mcv` into it.
17841778/// `reg_owner` is the instruction that gets associated with the register in the register table.
17851779/// This can have a side effect of spilling instructions to the stack to free up a register.
1786fn copyToNewRegister(self: *Self, reg_owner: Air.Inst.Index, mcv: MCValue) !MCValue {
1787 const ty = self.typeOfIndex(reg_owner);
1788 const reg = try self.register_manager.allocReg(reg_owner, self.regGeneralClassForType(ty));
1789 try self.genSetReg(self.typeOfIndex(reg_owner), reg, mcv);
1780fn copyToNewRegister(func: *Func, reg_owner: Air.Inst.Index, mcv: MCValue) !MCValue {
1781 const ty = func.typeOfIndex(reg_owner);
1782 const reg = try func.register_manager.allocReg(reg_owner, func.regGeneralClassForType(ty));
1783 try func.genSetReg(func.typeOfIndex(reg_owner), reg, mcv);
17901784 return MCValue{ .register = reg };
17911785}
17921786
1793fn airAlloc(self: *Self, inst: Air.Inst.Index) !void {
1794 const result = MCValue{ .lea_frame = .{ .index = try self.allocMemPtr(inst) } };
1795 return self.finishAir(inst, result, .{ .none, .none, .none });
1787fn airAlloc(func: *Func, inst: Air.Inst.Index) !void {
1788 const result = MCValue{ .lea_frame = .{ .index = try func.allocMemPtr(inst) } };
1789 return func.finishAir(inst, result, .{ .none, .none, .none });
17961790}
17971791
1798fn airRetPtr(self: *Self, inst: Air.Inst.Index) !void {
1799 const result: MCValue = switch (self.ret_mcv.long) {
1800 .none => .{ .lea_frame = .{ .index = try self.allocMemPtr(inst) } },
1792fn airRetPtr(func: *Func, inst: Air.Inst.Index) !void {
1793 const result: MCValue = switch (func.ret_mcv.long) {
1794 .none => .{ .lea_frame = .{ .index = try func.allocMemPtr(inst) } },
18011795 .load_frame => .{ .register_offset = .{
1802 .reg = (try self.copyToNewRegister(
1796 .reg = (try func.copyToNewRegister(
18031797 inst,
1804 self.ret_mcv.long,
1798 func.ret_mcv.long,
18051799 )).register,
1806 .off = self.ret_mcv.short.indirect.off,
1800 .off = func.ret_mcv.short.indirect.off,
18071801 } },
1808 else => |t| return self.fail("TODO: airRetPtr {s}", .{@tagName(t)}),
1802 else => |t| return func.fail("TODO: airRetPtr {s}", .{@tagName(t)}),
18091803 };
1810 return self.finishAir(inst, result, .{ .none, .none, .none });
1804 return func.finishAir(inst, result, .{ .none, .none, .none });
18111805}
18121806
1813fn airFptrunc(self: *Self, inst: Air.Inst.Index) !void {
1814 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1815 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airFptrunc for {}", .{self.target.cpu.arch});
1816 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
1807fn airFptrunc(func: *Func, inst: Air.Inst.Index) !void {
1808 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1809 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airFptrunc for {}", .{func.target.cpu.arch});
1810 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
18171811}
18181812
1819fn airFpext(self: *Self, inst: Air.Inst.Index) !void {
1820 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1821 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airFpext for {}", .{self.target.cpu.arch});
1822 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
1813fn airFpext(func: *Func, inst: Air.Inst.Index) !void {
1814 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1815 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airFpext for {}", .{func.target.cpu.arch});
1816 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
18231817}
18241818
1825fn airIntCast(self: *Self, inst: Air.Inst.Index) !void {
1826 const zcu = self.bin_file.comp.module.?;
1827 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1828 const src_ty = self.typeOf(ty_op.operand);
1829 const dst_ty = self.typeOfIndex(inst);
1819fn airIntCast(func: *Func, inst: Air.Inst.Index) !void {
1820 const zcu = func.bin_file.comp.module.?;
1821 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1822 const src_ty = func.typeOf(ty_op.operand);
1823 const dst_ty = func.typeOfIndex(inst);
18301824
18311825 const result: MCValue = result: {
18321826 const src_int_info = src_ty.intInfo(zcu);
......@@ -1834,20 +1828,20 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void {
18341828
18351829 const min_ty = if (dst_int_info.bits < src_int_info.bits) dst_ty else src_ty;
18361830
1837 const src_mcv = try self.resolveInst(ty_op.operand);
1831 const src_mcv = try func.resolveInst(ty_op.operand);
18381832
18391833 const src_storage_bits: u16 = switch (src_mcv) {
18401834 .register => 64,
18411835 .load_frame => src_int_info.bits,
1842 else => return self.fail("airIntCast from {s}", .{@tagName(src_mcv)}),
1836 else => return func.fail("airIntCast from {s}", .{@tagName(src_mcv)}),
18431837 };
18441838
18451839 const dst_mcv = if (dst_int_info.bits <= src_storage_bits and
18461840 math.divCeil(u16, dst_int_info.bits, 64) catch unreachable ==
18471841 math.divCeil(u32, src_storage_bits, 64) catch unreachable and
1848 self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: {
1849 const dst_mcv = try self.allocRegOrMem(inst, true);
1850 try self.genCopy(min_ty, dst_mcv, src_mcv);
1842 func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: {
1843 const dst_mcv = try func.allocRegOrMem(inst, true);
1844 try func.genCopy(min_ty, dst_mcv, src_mcv);
18511845 break :dst dst_mcv;
18521846 };
18531847
......@@ -1858,53 +1852,53 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void {
18581852 break :result null; // TODO
18591853
18601854 break :result dst_mcv;
1861 } orelse return self.fail("TODO implement airIntCast from {} to {}", .{
1855 } orelse return func.fail("TODO implement airIntCast from {} to {}", .{
18621856 src_ty.fmt(zcu), dst_ty.fmt(zcu),
18631857 });
18641858
1865 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
1859 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
18661860}
18671861
1868fn airTrunc(self: *Self, inst: Air.Inst.Index) !void {
1869 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1870 if (self.liveness.isUnused(inst))
1871 return self.finishAir(inst, .unreach, .{ ty_op.operand, .none, .none });
1862fn airTrunc(func: *Func, inst: Air.Inst.Index) !void {
1863 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1864 if (func.liveness.isUnused(inst))
1865 return func.finishAir(inst, .unreach, .{ ty_op.operand, .none, .none });
18721866
1873 const operand = try self.resolveInst(ty_op.operand);
1867 const operand = try func.resolveInst(ty_op.operand);
18741868 _ = operand;
1875 return self.fail("TODO implement trunc for {}", .{self.target.cpu.arch});
1876 // return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
1869 return func.fail("TODO implement trunc for {}", .{func.target.cpu.arch});
1870 // return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
18771871}
18781872
1879fn airIntFromBool(self: *Self, inst: Air.Inst.Index) !void {
1880 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
1881 const operand = try self.resolveInst(un_op);
1882 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else operand;
1883 return self.finishAir(inst, result, .{ un_op, .none, .none });
1873fn airIntFromBool(func: *Func, inst: Air.Inst.Index) !void {
1874 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
1875 const operand = try func.resolveInst(un_op);
1876 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else operand;
1877 return func.finishAir(inst, result, .{ un_op, .none, .none });
18841878}
18851879
1886fn airNot(self: *Self, inst: Air.Inst.Index) !void {
1887 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1888 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
1889 const zcu = self.bin_file.comp.module.?;
1880fn airNot(func: *Func, inst: Air.Inst.Index) !void {
1881 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1882 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
1883 const zcu = func.bin_file.comp.module.?;
18901884
1891 const operand = try self.resolveInst(ty_op.operand);
1892 const ty = self.typeOf(ty_op.operand);
1885 const operand = try func.resolveInst(ty_op.operand);
1886 const ty = func.typeOf(ty_op.operand);
18931887
18941888 switch (ty.zigTypeTag(zcu)) {
18951889 .Bool => {
18961890 const operand_reg = blk: {
18971891 if (operand == .register) break :blk operand.register;
1898 break :blk try self.copyToTmpRegister(ty, operand);
1892 break :blk try func.copyToTmpRegister(ty, operand);
18991893 };
19001894
19011895 const dst_reg: Register =
1902 if (self.reuseOperand(inst, ty_op.operand, 0, operand) and operand == .register)
1896 if (func.reuseOperand(inst, ty_op.operand, 0, operand) and operand == .register)
19031897 operand.register
19041898 else
1905 (try self.allocRegOrMem(inst, true)).register;
1899 (try func.allocRegOrMem(inst, true)).register;
19061900
1907 _ = try self.addInst(.{
1901 _ = try func.addInst(.{
19081902 .tag = .pseudo,
19091903 .ops = .pseudo_not,
19101904 .data = .{
......@@ -1917,59 +1911,59 @@ fn airNot(self: *Self, inst: Air.Inst.Index) !void {
19171911
19181912 break :result .{ .register = dst_reg };
19191913 },
1920 .Int => return self.fail("TODO: airNot ints", .{}),
1914 .Int => return func.fail("TODO: airNot ints", .{}),
19211915 else => unreachable,
19221916 }
19231917 };
1924 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
1918 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
19251919}
19261920
19271921fn airMinMax(
1928 self: *Self,
1922 func: *Func,
19291923 inst: Air.Inst.Index,
19301924 comptime tag: enum {
19311925 max,
19321926 min,
19331927 },
19341928) !void {
1935 const zcu = self.bin_file.comp.module.?;
1936 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
1929 const zcu = func.bin_file.comp.module.?;
1930 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
19371931
1938 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
1939 const lhs = try self.resolveInst(bin_op.lhs);
1940 const rhs = try self.resolveInst(bin_op.rhs);
1941 const lhs_ty = self.typeOf(bin_op.lhs);
1942 const rhs_ty = self.typeOf(bin_op.rhs);
1932 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
1933 const lhs = try func.resolveInst(bin_op.lhs);
1934 const rhs = try func.resolveInst(bin_op.rhs);
1935 const lhs_ty = func.typeOf(bin_op.lhs);
1936 const rhs_ty = func.typeOf(bin_op.rhs);
19431937
19441938 const int_info = lhs_ty.intInfo(zcu);
19451939
1946 if (int_info.bits > 64) return self.fail("TODO: > 64 bit @min", .{});
1940 if (int_info.bits > 64) return func.fail("TODO: > 64 bit @min", .{});
19471941
19481942 const lhs_reg, const lhs_lock = blk: {
1949 if (lhs == .register) break :blk .{ lhs.register, self.register_manager.lockReg(lhs.register) };
1943 if (lhs == .register) break :blk .{ lhs.register, func.register_manager.lockReg(lhs.register) };
19501944
1951 const lhs_reg, const lhs_lock = try self.allocReg(.int);
1952 try self.genSetReg(lhs_ty, lhs_reg, lhs);
1945 const lhs_reg, const lhs_lock = try func.allocReg(.int);
1946 try func.genSetReg(lhs_ty, lhs_reg, lhs);
19531947 break :blk .{ lhs_reg, lhs_lock };
19541948 };
1955 defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock);
1949 defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock);
19561950
19571951 const rhs_reg, const rhs_lock = blk: {
1958 if (rhs == .register) break :blk .{ rhs.register, self.register_manager.lockReg(rhs.register) };
1952 if (rhs == .register) break :blk .{ rhs.register, func.register_manager.lockReg(rhs.register) };
19591953
1960 const rhs_reg, const rhs_lock = try self.allocReg(.int);
1961 try self.genSetReg(rhs_ty, rhs_reg, rhs);
1954 const rhs_reg, const rhs_lock = try func.allocReg(.int);
1955 try func.genSetReg(rhs_ty, rhs_reg, rhs);
19621956 break :blk .{ rhs_reg, rhs_lock };
19631957 };
1964 defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock);
1958 defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock);
19651959
1966 const mask_reg, const mask_lock = try self.allocReg(.int);
1967 defer self.register_manager.unlockReg(mask_lock);
1960 const mask_reg, const mask_lock = try func.allocReg(.int);
1961 defer func.register_manager.unlockReg(mask_lock);
19681962
1969 const result_reg, const result_lock = try self.allocReg(.int);
1970 defer self.register_manager.unlockReg(result_lock);
1963 const result_reg, const result_lock = try func.allocReg(.int);
1964 defer func.register_manager.unlockReg(result_lock);
19711965
1972 _ = try self.addInst(.{
1966 _ = try func.addInst(.{
19731967 .tag = if (int_info.signedness == .unsigned) .sltu else .slt,
19741968 .ops = .rrr,
19751969 .data = .{ .r_type = .{
......@@ -1979,7 +1973,7 @@ fn airMinMax(
19791973 } },
19801974 });
19811975
1982 _ = try self.addInst(.{
1976 _ = try func.addInst(.{
19831977 .tag = .sub,
19841978 .ops = .rrr,
19851979 .data = .{ .r_type = .{
......@@ -1989,7 +1983,7 @@ fn airMinMax(
19891983 } },
19901984 });
19911985
1992 _ = try self.addInst(.{
1986 _ = try func.addInst(.{
19931987 .tag = .xor,
19941988 .ops = .rrr,
19951989 .data = .{ .r_type = .{
......@@ -1999,7 +1993,7 @@ fn airMinMax(
19991993 } },
20001994 });
20011995
2002 _ = try self.addInst(.{
1996 _ = try func.addInst(.{
20031997 .tag = .@"and",
20041998 .ops = .rrr,
20051999 .data = .{ .r_type = .{
......@@ -2009,7 +2003,7 @@ fn airMinMax(
20092003 } },
20102004 });
20112005
2012 _ = try self.addInst(.{
2006 _ = try func.addInst(.{
20132007 .tag = .xor,
20142008 .ops = .rrr,
20152009 .data = .{ .r_type = .{
......@@ -2021,22 +2015,22 @@ fn airMinMax(
20212015
20222016 break :result .{ .register = result_reg };
20232017 };
2024 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2018 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
20252019}
20262020
2027fn airSlice(self: *Self, inst: Air.Inst.Index) !void {
2028 const zcu = self.bin_file.comp.module.?;
2029 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2030 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
2021fn airSlice(func: *Func, inst: Air.Inst.Index) !void {
2022 const zcu = func.bin_file.comp.module.?;
2023 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2024 const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data;
20312025
2032 const slice_ty = self.typeOfIndex(inst);
2033 const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(slice_ty, zcu));
2026 const slice_ty = func.typeOfIndex(inst);
2027 const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(slice_ty, zcu));
20342028
2035 const ptr_ty = self.typeOf(bin_op.lhs);
2036 try self.genSetMem(.{ .frame = frame_index }, 0, ptr_ty, .{ .air_ref = bin_op.lhs });
2029 const ptr_ty = func.typeOf(bin_op.lhs);
2030 try func.genSetMem(.{ .frame = frame_index }, 0, ptr_ty, .{ .air_ref = bin_op.lhs });
20372031
2038 const len_ty = self.typeOf(bin_op.rhs);
2039 try self.genSetMem(
2032 const len_ty = func.typeOf(bin_op.rhs);
2033 try func.genSetMem(
20402034 .{ .frame = frame_index },
20412035 @intCast(ptr_ty.abiSize(zcu)),
20422036 len_ty,
......@@ -2044,31 +2038,31 @@ fn airSlice(self: *Self, inst: Air.Inst.Index) !void {
20442038 );
20452039
20462040 const result = MCValue{ .load_frame = .{ .index = frame_index } };
2047 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2041 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
20482042}
20492043
2050fn airBinOp(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void {
2051 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2052 const lhs = try self.resolveInst(bin_op.lhs);
2053 const rhs = try self.resolveInst(bin_op.rhs);
2054 const lhs_ty = self.typeOf(bin_op.lhs);
2055 const rhs_ty = self.typeOf(bin_op.rhs);
2044fn airBinOp(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void {
2045 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2046 const lhs = try func.resolveInst(bin_op.lhs);
2047 const rhs = try func.resolveInst(bin_op.rhs);
2048 const lhs_ty = func.typeOf(bin_op.lhs);
2049 const rhs_ty = func.typeOf(bin_op.rhs);
20562050
2057 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2058 break :result try self.binOp(tag, lhs, lhs_ty, rhs, rhs_ty);
2051 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2052 break :result try func.binOp(tag, lhs, lhs_ty, rhs, rhs_ty);
20592053 };
2060 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2054 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
20612055}
20622056
20632057fn binOp(
2064 self: *Self,
2058 func: *Func,
20652059 tag: Air.Inst.Tag,
20662060 lhs: MCValue,
20672061 lhs_ty: Type,
20682062 rhs: MCValue,
20692063 rhs_ty: Type,
20702064) InnerError!MCValue {
2071 const zcu = self.bin_file.comp.module.?;
2065 const zcu = func.bin_file.comp.module.?;
20722066
20732067 switch (tag) {
20742068 // Arithmetic operations on integers and floats
......@@ -2087,23 +2081,23 @@ fn binOp(
20872081 switch (lhs_ty.zigTypeTag(zcu)) {
20882082 .Float => {
20892083 const float_bits = lhs_ty.floatBits(zcu.getTarget());
2090 const float_reg_bits: u32 = if (self.hasFeature(.d)) 64 else 32;
2084 const float_reg_bits: u32 = if (func.hasFeature(.d)) 64 else 32;
20912085 if (float_bits <= float_reg_bits) {
2092 return self.binOpFloat(tag, lhs, lhs_ty, rhs, rhs_ty);
2086 return func.binOpFloat(tag, lhs, lhs_ty, rhs, rhs_ty);
20932087 } else {
2094 return self.fail("TODO: binary operations for floats with bits > {d}", .{float_reg_bits});
2088 return func.fail("TODO: binary operations for floats with bits > {d}", .{float_reg_bits});
20952089 }
20962090 },
2097 .Vector => return self.fail("TODO binary operations on vectors", .{}),
2091 .Vector => return func.fail("TODO binary operations on vectors", .{}),
20982092 .Int, .Enum, .ErrorSet => {
20992093 const int_info = lhs_ty.intInfo(zcu);
21002094 if (int_info.bits <= 64) {
2101 return self.binOpRegister(tag, lhs, lhs_ty, rhs, rhs_ty);
2095 return func.binOpRegister(tag, lhs, lhs_ty, rhs, rhs_ty);
21022096 } else {
2103 return self.fail("TODO binary operations on int with bits > 64", .{});
2097 return func.fail("TODO binary operations on int with bits > 64", .{});
21042098 }
21052099 },
2106 else => |x| return self.fail("TOOD: binOp {s}", .{@tagName(x)}),
2100 else => |x| return func.fail("TOOD: binOp {s}", .{@tagName(x)}),
21072101 }
21082102 },
21092103
......@@ -2126,9 +2120,9 @@ fn binOp(
21262120 else => unreachable,
21272121 };
21282122
2129 return try self.binOpRegister(base_tag, lhs, lhs_ty, rhs, rhs_ty);
2123 return try func.binOpRegister(base_tag, lhs, lhs_ty, rhs, rhs_ty);
21302124 } else {
2131 const offset = try self.binOp(
2125 const offset = try func.binOp(
21322126 .mul,
21332127 rhs,
21342128 Type.usize,
......@@ -2136,7 +2130,7 @@ fn binOp(
21362130 Type.usize,
21372131 );
21382132
2139 const addr = try self.binOp(
2133 const addr = try func.binOp(
21402134 tag,
21412135 lhs,
21422136 Type.manyptr_u8,
......@@ -2155,39 +2149,39 @@ fn binOp(
21552149 .shl,
21562150 => {
21572151 switch (lhs_ty.zigTypeTag(zcu)) {
2158 .Float => return self.fail("TODO binary operations on floats", .{}),
2159 .Vector => return self.fail("TODO binary operations on vectors", .{}),
2152 .Float => return func.fail("TODO binary operations on floats", .{}),
2153 .Vector => return func.fail("TODO binary operations on vectors", .{}),
21602154 .Int => {
21612155 const int_info = lhs_ty.intInfo(zcu);
21622156 if (int_info.bits <= 64) {
2163 return self.binOpRegister(tag, lhs, lhs_ty, rhs, rhs_ty);
2157 return func.binOpRegister(tag, lhs, lhs_ty, rhs, rhs_ty);
21642158 } else {
2165 return self.fail("TODO binary operations on int with bits > 64", .{});
2159 return func.fail("TODO binary operations on int with bits > 64", .{});
21662160 }
21672161 },
21682162 else => unreachable,
21692163 }
21702164 },
2171 else => return self.fail("TODO binOp {}", .{tag}),
2165 else => return func.fail("TODO binOp {}", .{tag}),
21722166 }
21732167}
21742168
21752169fn binOpRegister(
2176 self: *Self,
2170 func: *Func,
21772171 tag: Air.Inst.Tag,
21782172 lhs: MCValue,
21792173 lhs_ty: Type,
21802174 rhs: MCValue,
21812175 rhs_ty: Type,
21822176) !MCValue {
2183 const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{ .zero = true });
2184 defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock);
2177 const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{ .zero = true });
2178 defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock);
21852179
2186 const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{ .zero = true });
2187 defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock);
2180 const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{ .zero = true });
2181 defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock);
21882182
2189 const dest_reg, const dest_lock = try self.allocReg(.int);
2190 defer self.register_manager.unlockReg(dest_lock);
2183 const dest_reg, const dest_lock = try func.allocReg(.int);
2184 defer func.register_manager.unlockReg(dest_lock);
21912185
21922186 const mir_tag: Mir.Inst.Tag = switch (tag) {
21932187 .add => .add,
......@@ -2205,7 +2199,7 @@ fn binOpRegister(
22052199 .cmp_lte,
22062200 => .pseudo,
22072201
2208 else => return self.fail("TODO: binOpRegister {s}", .{@tagName(tag)}),
2202 else => return func.fail("TODO: binOpRegister {s}", .{@tagName(tag)}),
22092203 };
22102204
22112205 switch (mir_tag) {
......@@ -2215,7 +2209,7 @@ fn binOpRegister(
22152209 .sllw,
22162210 .srlw,
22172211 => {
2218 _ = try self.addInst(.{
2212 _ = try func.addInst(.{
22192213 .tag = mir_tag,
22202214 .ops = .rrr,
22212215 .data = .{
......@@ -2240,7 +2234,7 @@ fn binOpRegister(
22402234 else => unreachable,
22412235 };
22422236
2243 _ = try self.addInst(.{
2237 _ = try func.addInst(.{
22442238 .tag = .pseudo,
22452239 .ops = pseudo_op,
22462240 .data = .{
......@@ -2257,7 +2251,7 @@ fn binOpRegister(
22572251 .cmp_lte => .lte,
22582252 else => unreachable,
22592253 },
2260 .size = self.memSize(lhs_ty),
2254 .size = func.memSize(lhs_ty),
22612255 },
22622256 },
22632257 });
......@@ -2270,21 +2264,21 @@ fn binOpRegister(
22702264}
22712265
22722266fn binOpFloat(
2273 self: *Self,
2267 func: *Func,
22742268 tag: Air.Inst.Tag,
22752269 lhs: MCValue,
22762270 lhs_ty: Type,
22772271 rhs: MCValue,
22782272 rhs_ty: Type,
22792273) !MCValue {
2280 const zcu = self.bin_file.comp.module.?;
2274 const zcu = func.bin_file.comp.module.?;
22812275 const float_bits = lhs_ty.floatBits(zcu.getTarget());
22822276
2283 const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{});
2284 defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock);
2277 const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{});
2278 defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock);
22852279
2286 const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{});
2287 defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock);
2280 const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{});
2281 defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock);
22882282
22892283 const mir_tag: Mir.Inst.Tag = switch (tag) {
22902284 .add => if (float_bits == 32) .fadds else .faddd,
......@@ -2300,7 +2294,7 @@ fn binOpFloat(
23002294 .cmp_lte,
23012295 => .pseudo,
23022296
2303 else => return self.fail("TODO: binOpFloat mir_tag {s}", .{@tagName(tag)}),
2297 else => return func.fail("TODO: binOpFloat mir_tag {s}", .{@tagName(tag)}),
23042298 };
23052299
23062300 const return_class: abi.RegisterClass = switch (tag) {
......@@ -2320,8 +2314,8 @@ fn binOpFloat(
23202314 else => unreachable,
23212315 };
23222316
2323 const dest_reg, const dest_lock = try self.allocReg(return_class);
2324 defer self.register_manager.unlockReg(dest_lock);
2317 const dest_reg, const dest_lock = try func.allocReg(return_class);
2318 defer func.register_manager.unlockReg(dest_lock);
23252319
23262320 switch (tag) {
23272321 .add,
......@@ -2329,7 +2323,7 @@ fn binOpFloat(
23292323 .mul,
23302324 .div_float,
23312325 => {
2332 _ = try self.addInst(.{
2326 _ = try func.addInst(.{
23332327 .tag = mir_tag,
23342328 .ops = .rrr,
23352329 .data = .{ .r_type = .{
......@@ -2347,7 +2341,7 @@ fn binOpFloat(
23472341 .cmp_lt,
23482342 .cmp_lte,
23492343 => {
2350 _ = try self.addInst(.{
2344 _ = try func.addInst(.{
23512345 .tag = .pseudo,
23522346 .ops = .pseudo_compare,
23532347 .data = .{
......@@ -2364,7 +2358,7 @@ fn binOpFloat(
23642358 .cmp_lte => .lte,
23652359 else => unreachable,
23662360 },
2367 .size = self.memSize(lhs_ty),
2361 .size = func.memSize(lhs_ty),
23682362 },
23692363 },
23702364 });
......@@ -2376,120 +2370,120 @@ fn binOpFloat(
23762370 return MCValue{ .register = dest_reg };
23772371}
23782372
2379fn airPtrArithmetic(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void {
2380 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2381 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
2382 const lhs = try self.resolveInst(bin_op.lhs);
2383 const rhs = try self.resolveInst(bin_op.rhs);
2384 const lhs_ty = self.typeOf(bin_op.lhs);
2385 const rhs_ty = self.typeOf(bin_op.rhs);
2373fn airPtrArithmetic(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void {
2374 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2375 const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data;
2376 const lhs = try func.resolveInst(bin_op.lhs);
2377 const rhs = try func.resolveInst(bin_op.rhs);
2378 const lhs_ty = func.typeOf(bin_op.lhs);
2379 const rhs_ty = func.typeOf(bin_op.rhs);
23862380
2387 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2388 break :result try self.binOp(tag, lhs, lhs_ty, rhs, rhs_ty);
2381 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2382 break :result try func.binOp(tag, lhs, lhs_ty, rhs, rhs_ty);
23892383 };
2390 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2384 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
23912385}
23922386
2393fn airAddWrap(self: *Self, inst: Air.Inst.Index) !void {
2394 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2395 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement addwrap for {}", .{self.target.cpu.arch});
2396 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2387fn airAddWrap(func: *Func, inst: Air.Inst.Index) !void {
2388 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2389 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement addwrap for {}", .{func.target.cpu.arch});
2390 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
23972391}
23982392
2399fn airAddSat(self: *Self, inst: Air.Inst.Index) !void {
2400 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2401 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement add_sat for {}", .{self.target.cpu.arch});
2402 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2393fn airAddSat(func: *Func, inst: Air.Inst.Index) !void {
2394 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2395 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement add_sat for {}", .{func.target.cpu.arch});
2396 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
24032397}
24042398
2405fn airSubWrap(self: *Self, inst: Air.Inst.Index) !void {
2406 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2407 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2399fn airSubWrap(func: *Func, inst: Air.Inst.Index) !void {
2400 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2401 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
24082402 // RISCV arthemtic instructions already wrap, so this is simply a sub binOp with
24092403 // no overflow checks.
2410 const lhs = try self.resolveInst(bin_op.lhs);
2411 const rhs = try self.resolveInst(bin_op.rhs);
2412 const lhs_ty = self.typeOf(bin_op.lhs);
2413 const rhs_ty = self.typeOf(bin_op.rhs);
2404 const lhs = try func.resolveInst(bin_op.lhs);
2405 const rhs = try func.resolveInst(bin_op.rhs);
2406 const lhs_ty = func.typeOf(bin_op.lhs);
2407 const rhs_ty = func.typeOf(bin_op.rhs);
24142408
2415 break :result try self.binOp(.sub, lhs, lhs_ty, rhs, rhs_ty);
2409 break :result try func.binOp(.sub, lhs, lhs_ty, rhs, rhs_ty);
24162410 };
2417 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2411 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
24182412}
24192413
2420fn airSubSat(self: *Self, inst: Air.Inst.Index) !void {
2421 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2422 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement sub_sat for {}", .{self.target.cpu.arch});
2423 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2414fn airSubSat(func: *Func, inst: Air.Inst.Index) !void {
2415 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2416 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement sub_sat for {}", .{func.target.cpu.arch});
2417 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
24242418}
24252419
2426fn airMul(self: *Self, inst: Air.Inst.Index) !void {
2427 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2428 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2429 const lhs = try self.resolveInst(bin_op.lhs);
2430 const rhs = try self.resolveInst(bin_op.rhs);
2431 const lhs_ty = self.typeOf(bin_op.lhs);
2432 const rhs_ty = self.typeOf(bin_op.rhs);
2420fn airMul(func: *Func, inst: Air.Inst.Index) !void {
2421 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2422 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2423 const lhs = try func.resolveInst(bin_op.lhs);
2424 const rhs = try func.resolveInst(bin_op.rhs);
2425 const lhs_ty = func.typeOf(bin_op.lhs);
2426 const rhs_ty = func.typeOf(bin_op.rhs);
24332427
2434 break :result try self.binOp(.mul, lhs, lhs_ty, rhs, rhs_ty);
2428 break :result try func.binOp(.mul, lhs, lhs_ty, rhs, rhs_ty);
24352429 };
2436 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2430 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
24372431}
24382432
2439fn airDiv(self: *Self, inst: Air.Inst.Index) !void {
2440 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2441 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2442 const lhs = try self.resolveInst(bin_op.lhs);
2443 const rhs = try self.resolveInst(bin_op.rhs);
2444 const lhs_ty = self.typeOf(bin_op.lhs);
2445 const rhs_ty = self.typeOf(bin_op.rhs);
2433fn airDiv(func: *Func, inst: Air.Inst.Index) !void {
2434 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2435 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2436 const lhs = try func.resolveInst(bin_op.lhs);
2437 const rhs = try func.resolveInst(bin_op.rhs);
2438 const lhs_ty = func.typeOf(bin_op.lhs);
2439 const rhs_ty = func.typeOf(bin_op.rhs);
24462440
2447 break :result try self.binOp(.div_float, lhs, lhs_ty, rhs, rhs_ty);
2441 break :result try func.binOp(.div_float, lhs, lhs_ty, rhs, rhs_ty);
24482442 };
2449 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2443 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
24502444}
24512445
2452fn airMulWrap(self: *Self, inst: Air.Inst.Index) !void {
2453 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2454 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement mulwrap for {}", .{self.target.cpu.arch});
2455 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2446fn airMulWrap(func: *Func, inst: Air.Inst.Index) !void {
2447 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2448 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement mulwrap for {}", .{func.target.cpu.arch});
2449 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
24562450}
24572451
2458fn airMulSat(self: *Self, inst: Air.Inst.Index) !void {
2459 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2460 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement mul_sat for {}", .{self.target.cpu.arch});
2461 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2452fn airMulSat(func: *Func, inst: Air.Inst.Index) !void {
2453 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2454 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement mul_sat for {}", .{func.target.cpu.arch});
2455 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
24622456}
24632457
2464fn airAddWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
2465 const zcu = self.bin_file.comp.module.?;
2466 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2467 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
2458fn airAddWithOverflow(func: *Func, inst: Air.Inst.Index) !void {
2459 const zcu = func.bin_file.comp.module.?;
2460 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2461 const extra = func.air.extraData(Air.Bin, ty_pl.payload).data;
24682462
2469 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2470 const lhs = try self.resolveInst(extra.lhs);
2471 const rhs = try self.resolveInst(extra.rhs);
2472 const lhs_ty = self.typeOf(extra.lhs);
2473 const rhs_ty = self.typeOf(extra.rhs);
2463 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2464 const lhs = try func.resolveInst(extra.lhs);
2465 const rhs = try func.resolveInst(extra.rhs);
2466 const lhs_ty = func.typeOf(extra.lhs);
2467 const rhs_ty = func.typeOf(extra.rhs);
24742468
24752469 const int_info = lhs_ty.intInfo(zcu);
24762470
2477 const tuple_ty = self.typeOfIndex(inst);
2478 const result_mcv = try self.allocRegOrMem(inst, false);
2471 const tuple_ty = func.typeOfIndex(inst);
2472 const result_mcv = try func.allocRegOrMem(inst, false);
24792473 const offset = result_mcv.load_frame;
24802474
24812475 if (int_info.bits >= 8 and math.isPowerOfTwo(int_info.bits)) {
2482 const add_result = try self.binOp(.add, lhs, lhs_ty, rhs, rhs_ty);
2483 const add_result_reg = try self.copyToTmpRegister(lhs_ty, add_result);
2484 const add_result_reg_lock = self.register_manager.lockRegAssumeUnused(add_result_reg);
2485 defer self.register_manager.unlockReg(add_result_reg_lock);
2476 const add_result = try func.binOp(.add, lhs, lhs_ty, rhs, rhs_ty);
2477 const add_result_reg = try func.copyToTmpRegister(lhs_ty, add_result);
2478 const add_result_reg_lock = func.register_manager.lockRegAssumeUnused(add_result_reg);
2479 defer func.register_manager.unlockReg(add_result_reg_lock);
24862480
24872481 const shift_amount: u6 = @intCast(Type.usize.bitSize(zcu) - int_info.bits);
24882482
2489 const shift_reg, const shift_lock = try self.allocReg(.int);
2490 defer self.register_manager.unlockReg(shift_lock);
2483 const shift_reg, const shift_lock = try func.allocReg(.int);
2484 defer func.register_manager.unlockReg(shift_lock);
24912485
2492 _ = try self.addInst(.{
2486 _ = try func.addInst(.{
24932487 .tag = .slli,
24942488 .ops = .rri,
24952489 .data = .{
......@@ -2501,7 +2495,7 @@ fn airAddWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
25012495 },
25022496 });
25032497
2504 _ = try self.addInst(.{
2498 _ = try func.addInst(.{
25052499 .tag = if (int_info.signedness == .unsigned) .srli else .srai,
25062500 .ops = .rri,
25072501 .data = .{
......@@ -2513,21 +2507,21 @@ fn airAddWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
25132507 },
25142508 });
25152509
2516 try self.genSetMem(
2510 try func.genSetMem(
25172511 .{ .frame = offset.index },
25182512 offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, zcu))),
25192513 lhs_ty,
25202514 add_result,
25212515 );
25222516
2523 const overflow_mcv = try self.binOp(
2517 const overflow_mcv = try func.binOp(
25242518 .cmp_neq,
25252519 .{ .register = shift_reg },
25262520 lhs_ty,
25272521 .{ .register = add_result_reg },
25282522 lhs_ty,
25292523 );
2530 try self.genSetMem(
2524 try func.genSetMem(
25312525 .{ .frame = offset.index },
25322526 offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))),
25332527 Type.u1,
......@@ -2536,50 +2530,50 @@ fn airAddWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
25362530
25372531 break :result result_mcv;
25382532 } else {
2539 return self.fail("TODO: less than 8 bit or non-pow 2 addition", .{});
2533 return func.fail("TODO: less than 8 bit or non-pow 2 addition", .{});
25402534 }
25412535 };
25422536
2543 return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none });
2537 return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none });
25442538}
25452539
2546fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
2547 const zcu = self.bin_file.comp.module.?;
2548 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2549 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
2540fn airSubWithOverflow(func: *Func, inst: Air.Inst.Index) !void {
2541 const zcu = func.bin_file.comp.module.?;
2542 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2543 const extra = func.air.extraData(Air.Bin, ty_pl.payload).data;
25502544
2551 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2552 const lhs = try self.resolveInst(extra.lhs);
2553 const rhs = try self.resolveInst(extra.rhs);
2554 const lhs_ty = self.typeOf(extra.lhs);
2555 const rhs_ty = self.typeOf(extra.rhs);
2545 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2546 const lhs = try func.resolveInst(extra.lhs);
2547 const rhs = try func.resolveInst(extra.rhs);
2548 const lhs_ty = func.typeOf(extra.lhs);
2549 const rhs_ty = func.typeOf(extra.rhs);
25562550
25572551 const int_info = lhs_ty.intInfo(zcu);
25582552
25592553 if (!math.isPowerOfTwo(int_info.bits) or !(int_info.bits >= 8)) {
2560 return self.fail("TODO: airSubWithOverflow non-power of 2 and less than 8 bits", .{});
2554 return func.fail("TODO: airSubWithOverflow non-power of 2 and less than 8 bits", .{});
25612555 }
25622556
2563 const tuple_ty = self.typeOfIndex(inst);
2564 const result_mcv = try self.allocRegOrMem(inst, false);
2557 const tuple_ty = func.typeOfIndex(inst);
2558 const result_mcv = try func.allocRegOrMem(inst, false);
25652559 const offset = result_mcv.load_frame;
25662560
2567 const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{});
2568 defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock);
2561 const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{});
2562 defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock);
25692563
2570 const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{});
2571 defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock);
2564 const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{});
2565 defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock);
25722566
2573 const dest_reg, const dest_lock = try self.allocReg(.int);
2574 defer self.register_manager.unlockReg(dest_lock);
2567 const dest_reg, const dest_lock = try func.allocReg(.int);
2568 defer func.register_manager.unlockReg(dest_lock);
25752569
25762570 switch (int_info.signedness) {
2577 .unsigned => return self.fail("TODO: airSubWithOverflow unsigned", .{}),
2571 .unsigned => return func.fail("TODO: airSubWithOverflow unsigned", .{}),
25782572 .signed => {
25792573 switch (int_info.bits) {
25802574 64 => {
25812575 // result
2582 _ = try self.addInst(.{
2576 _ = try func.addInst(.{
25832577 .tag = .sub,
25842578 .ops = .rrr,
25852579 .data = .{ .r_type = .{
......@@ -2589,7 +2583,7 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
25892583 } },
25902584 });
25912585
2592 try self.genSetMem(
2586 try func.genSetMem(
25932587 .{ .frame = offset.index },
25942588 offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, zcu))),
25952589 lhs_ty,
......@@ -2597,9 +2591,9 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
25972591 );
25982592
25992593 // overflow check
2600 const overflow_reg = try self.copyToTmpRegister(Type.usize, .{ .immediate = 0 });
2594 const overflow_reg = try func.copyToTmpRegister(Type.usize, .{ .immediate = 0 });
26012595
2602 _ = try self.addInst(.{
2596 _ = try func.addInst(.{
26032597 .tag = .slt,
26042598 .ops = .rrr,
26052599 .data = .{ .r_type = .{
......@@ -2609,7 +2603,7 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
26092603 } },
26102604 });
26112605
2612 _ = try self.addInst(.{
2606 _ = try func.addInst(.{
26132607 .tag = .slt,
26142608 .ops = .rrr,
26152609 .data = .{ .r_type = .{
......@@ -2619,7 +2613,7 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
26192613 } },
26202614 });
26212615
2622 _ = try self.addInst(.{
2616 _ = try func.addInst(.{
26232617 .tag = .xor,
26242618 .ops = .rrr,
26252619 .data = .{ .r_type = .{
......@@ -2629,7 +2623,7 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
26292623 } },
26302624 });
26312625
2632 const overflow_mcv = try self.binOp(
2626 const overflow_mcv = try func.binOp(
26332627 .cmp_neq,
26342628 .{ .register = overflow_reg },
26352629 Type.usize,
......@@ -2637,7 +2631,7 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
26372631 Type.usize,
26382632 );
26392633
2640 try self.genSetMem(
2634 try func.genSetMem(
26412635 .{ .frame = offset.index },
26422636 offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))),
26432637 Type.u1,
......@@ -2646,47 +2640,47 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
26462640
26472641 break :result result_mcv;
26482642 },
2649 else => |int_bits| return self.fail("TODO: airSubWithOverflow signed {}", .{int_bits}),
2643 else => |int_bits| return func.fail("TODO: airSubWithOverflow signed {}", .{int_bits}),
26502644 }
26512645 },
26522646 }
26532647 };
26542648
2655 return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none });
2649 return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none });
26562650}
26572651
2658fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
2659 //const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];
2660 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2661 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
2662 const zcu = self.bin_file.comp.module.?;
2663 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2664 const lhs = try self.resolveInst(extra.lhs);
2665 const rhs = try self.resolveInst(extra.rhs);
2666 const lhs_ty = self.typeOf(extra.lhs);
2667 const rhs_ty = self.typeOf(extra.rhs);
2652fn airMulWithOverflow(func: *Func, inst: Air.Inst.Index) !void {
2653 //const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)];
2654 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2655 const extra = func.air.extraData(Air.Bin, ty_pl.payload).data;
2656 const zcu = func.bin_file.comp.module.?;
2657 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2658 const lhs = try func.resolveInst(extra.lhs);
2659 const rhs = try func.resolveInst(extra.rhs);
2660 const lhs_ty = func.typeOf(extra.lhs);
2661 const rhs_ty = func.typeOf(extra.rhs);
26682662
26692663 switch (lhs_ty.zigTypeTag(zcu)) {
2670 else => |x| return self.fail("TODO: airMulWithOverflow {s}", .{@tagName(x)}),
2664 else => |x| return func.fail("TODO: airMulWithOverflow {s}", .{@tagName(x)}),
26712665 .Int => {
26722666 assert(lhs_ty.eql(rhs_ty, zcu));
26732667 const int_info = lhs_ty.intInfo(zcu);
26742668 switch (int_info.bits) {
26752669 1...32 => {
2676 if (self.hasFeature(.m)) {
2677 const dest = try self.binOp(.mul, lhs, lhs_ty, rhs, rhs_ty);
2670 if (func.hasFeature(.m)) {
2671 const dest = try func.binOp(.mul, lhs, lhs_ty, rhs, rhs_ty);
26782672
2679 const add_result_lock = self.register_manager.lockRegAssumeUnused(dest.register);
2680 defer self.register_manager.unlockReg(add_result_lock);
2673 const add_result_lock = func.register_manager.lockRegAssumeUnused(dest.register);
2674 defer func.register_manager.unlockReg(add_result_lock);
26812675
2682 const tuple_ty = self.typeOfIndex(inst);
2676 const tuple_ty = func.typeOfIndex(inst);
26832677
2684 const result_mcv = try self.allocRegOrMem(inst, true);
2678 const result_mcv = try func.allocRegOrMem(inst, true);
26852679
26862680 const result_off: i32 = @intCast(tuple_ty.structFieldOffset(0, zcu));
26872681 const overflow_off: i32 = @intCast(tuple_ty.structFieldOffset(1, zcu));
26882682
2689 try self.genCopy(
2683 try func.genCopy(
26902684 lhs_ty,
26912685 result_mcv.offset(result_off),
26922686 dest,
......@@ -2698,13 +2692,13 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
26982692 1...8 => {
26992693 const max_val = std.math.pow(u16, 2, int_info.bits) - 1;
27002694
2701 const add_reg, const add_lock = try self.promoteReg(lhs_ty, lhs, .{});
2702 defer if (add_lock) |lock| self.register_manager.unlockReg(lock);
2695 const add_reg, const add_lock = try func.promoteReg(lhs_ty, lhs, .{});
2696 defer if (add_lock) |lock| func.register_manager.unlockReg(lock);
27032697
2704 const overflow_reg, const overflow_lock = try self.allocReg(.int);
2705 defer self.register_manager.unlockReg(overflow_lock);
2698 const overflow_reg, const overflow_lock = try func.allocReg(.int);
2699 defer func.register_manager.unlockReg(overflow_lock);
27062700
2707 _ = try self.addInst(.{
2701 _ = try func.addInst(.{
27082702 .tag = .andi,
27092703 .ops = .rri,
27102704 .data = .{ .i_type = .{
......@@ -2714,7 +2708,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
27142708 } },
27152709 });
27162710
2717 const overflow_mcv = try self.binOp(
2711 const overflow_mcv = try func.binOp(
27182712 .cmp_neq,
27192713 .{ .register = overflow_reg },
27202714 lhs_ty,
......@@ -2722,7 +2716,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
27222716 lhs_ty,
27232717 );
27242718
2725 try self.genCopy(
2719 try func.genCopy(
27262720 lhs_ty,
27272721 result_mcv.offset(overflow_off),
27282722 overflow_mcv,
......@@ -2731,63 +2725,63 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
27312725 break :result result_mcv;
27322726 },
27332727
2734 else => return self.fail("TODO: airMulWithOverflow check for size {d}", .{int_info.bits}),
2728 else => return func.fail("TODO: airMulWithOverflow check for size {d}", .{int_info.bits}),
27352729 }
27362730 } else {
2737 return self.fail("TODO: airMulWithOverflow calculate carry for signed addition", .{});
2731 return func.fail("TODO: airMulWithOverflow calculate carry for signed addition", .{});
27382732 }
27392733 } else {
2740 return self.fail("TODO: airMulWithOverflow with < 8 bits or non-pow of 2", .{});
2734 return func.fail("TODO: airMulWithOverflow with < 8 bits or non-pow of 2", .{});
27412735 }
27422736 } else {
2743 return self.fail("TODO: emulate mul for targets without M feature", .{});
2737 return func.fail("TODO: emulate mul for targets without M feature", .{});
27442738 }
27452739 },
2746 else => return self.fail("TODO: airMulWithOverflow larger than 32-bit mul", .{}),
2740 else => return func.fail("TODO: airMulWithOverflow larger than 32-bit mul", .{}),
27472741 }
27482742 },
27492743 }
27502744 };
27512745
2752 return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none });
2746 return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none });
27532747}
27542748
2755fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
2749fn airShlWithOverflow(func: *Func, inst: Air.Inst.Index) !void {
27562750 _ = inst;
2757 return self.fail("TODO implement airShlWithOverflow for {}", .{self.target.cpu.arch});
2751 return func.fail("TODO implement airShlWithOverflow for {}", .{func.target.cpu.arch});
27582752}
27592753
2760fn airRem(self: *Self, inst: Air.Inst.Index) !void {
2761 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2762 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement rem for {}", .{self.target.cpu.arch});
2763 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2754fn airRem(func: *Func, inst: Air.Inst.Index) !void {
2755 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2756 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement rem for {}", .{func.target.cpu.arch});
2757 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
27642758}
27652759
2766fn airMod(self: *Self, inst: Air.Inst.Index) !void {
2767 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2768 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement zcu for {}", .{self.target.cpu.arch});
2769 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2760fn airMod(func: *Func, inst: Air.Inst.Index) !void {
2761 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2762 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement zcu for {}", .{func.target.cpu.arch});
2763 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
27702764}
27712765
2772fn airBitAnd(self: *Self, inst: Air.Inst.Index) !void {
2773 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2774 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2775 const lhs = try self.resolveInst(bin_op.lhs);
2776 const rhs = try self.resolveInst(bin_op.rhs);
2766fn airBitAnd(func: *Func, inst: Air.Inst.Index) !void {
2767 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2768 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2769 const lhs = try func.resolveInst(bin_op.lhs);
2770 const rhs = try func.resolveInst(bin_op.rhs);
27772771
2778 const lhs_ty = self.typeOf(bin_op.lhs);
2779 const rhs_ty = self.typeOf(bin_op.rhs);
2772 const lhs_ty = func.typeOf(bin_op.lhs);
2773 const rhs_ty = func.typeOf(bin_op.rhs);
27802774
2781 const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{});
2782 defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock);
2775 const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{});
2776 defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock);
27832777
2784 const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{});
2785 defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock);
2778 const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{});
2779 defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock);
27862780
2787 const dest_reg, const dest_lock = try self.allocReg(.int);
2788 defer self.register_manager.unlockReg(dest_lock);
2781 const dest_reg, const dest_lock = try func.allocReg(.int);
2782 defer func.register_manager.unlockReg(dest_lock);
27892783
2790 _ = try self.addInst(.{
2784 _ = try func.addInst(.{
27912785 .tag = .@"and",
27922786 .ops = .rrr,
27932787 .data = .{ .r_type = .{
......@@ -2799,28 +2793,28 @@ fn airBitAnd(self: *Self, inst: Air.Inst.Index) !void {
27992793
28002794 break :result .{ .register = dest_reg };
28012795 };
2802 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2796 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
28032797}
28042798
2805fn airBitOr(self: *Self, inst: Air.Inst.Index) !void {
2806 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2807 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2808 const lhs = try self.resolveInst(bin_op.lhs);
2809 const rhs = try self.resolveInst(bin_op.rhs);
2799fn airBitOr(func: *Func, inst: Air.Inst.Index) !void {
2800 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2801 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2802 const lhs = try func.resolveInst(bin_op.lhs);
2803 const rhs = try func.resolveInst(bin_op.rhs);
28102804
2811 const lhs_ty = self.typeOf(bin_op.lhs);
2812 const rhs_ty = self.typeOf(bin_op.rhs);
2805 const lhs_ty = func.typeOf(bin_op.lhs);
2806 const rhs_ty = func.typeOf(bin_op.rhs);
28132807
2814 const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{});
2815 defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock);
2808 const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{});
2809 defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock);
28162810
2817 const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{});
2818 defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock);
2811 const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{});
2812 defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock);
28192813
2820 const dest_reg, const dest_lock = try self.allocReg(.int);
2821 defer self.register_manager.unlockReg(dest_lock);
2814 const dest_reg, const dest_lock = try func.allocReg(.int);
2815 defer func.register_manager.unlockReg(dest_lock);
28222816
2823 _ = try self.addInst(.{
2817 _ = try func.addInst(.{
28242818 .tag = .@"or",
28252819 .ops = .rrr,
28262820 .data = .{ .r_type = .{
......@@ -2832,72 +2826,72 @@ fn airBitOr(self: *Self, inst: Air.Inst.Index) !void {
28322826
28332827 break :result .{ .register = dest_reg };
28342828 };
2835 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2829 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
28362830}
28372831
2838fn airXor(self: *Self, inst: Air.Inst.Index) !void {
2839 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2840 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement xor for {}", .{self.target.cpu.arch});
2841 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2832fn airXor(func: *Func, inst: Air.Inst.Index) !void {
2833 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2834 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement xor for {}", .{func.target.cpu.arch});
2835 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
28422836}
28432837
2844fn airShl(self: *Self, inst: Air.Inst.Index) !void {
2845 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2846 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2847 const lhs = try self.resolveInst(bin_op.lhs);
2848 const rhs = try self.resolveInst(bin_op.rhs);
2849 const lhs_ty = self.typeOf(bin_op.lhs);
2850 const rhs_ty = self.typeOf(bin_op.rhs);
2838fn airShl(func: *Func, inst: Air.Inst.Index) !void {
2839 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2840 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2841 const lhs = try func.resolveInst(bin_op.lhs);
2842 const rhs = try func.resolveInst(bin_op.rhs);
2843 const lhs_ty = func.typeOf(bin_op.lhs);
2844 const rhs_ty = func.typeOf(bin_op.rhs);
28512845
2852 break :result try self.binOp(.shl, lhs, lhs_ty, rhs, rhs_ty);
2846 break :result try func.binOp(.shl, lhs, lhs_ty, rhs, rhs_ty);
28532847 };
2854 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2848 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
28552849}
28562850
2857fn airShlSat(self: *Self, inst: Air.Inst.Index) !void {
2858 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2859 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement shl_sat for {}", .{self.target.cpu.arch});
2860 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2851fn airShlSat(func: *Func, inst: Air.Inst.Index) !void {
2852 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2853 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement shl_sat for {}", .{func.target.cpu.arch});
2854 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
28612855}
28622856
2863fn airShr(self: *Self, inst: Air.Inst.Index) !void {
2864 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2865 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
2866 const lhs = try self.resolveInst(bin_op.lhs);
2867 const rhs = try self.resolveInst(bin_op.rhs);
2868 const lhs_ty = self.typeOf(bin_op.lhs);
2869 const rhs_ty = self.typeOf(bin_op.rhs);
2857fn airShr(func: *Func, inst: Air.Inst.Index) !void {
2858 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2859 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
2860 const lhs = try func.resolveInst(bin_op.lhs);
2861 const rhs = try func.resolveInst(bin_op.rhs);
2862 const lhs_ty = func.typeOf(bin_op.lhs);
2863 const rhs_ty = func.typeOf(bin_op.rhs);
28702864
2871 break :result try self.binOp(.shr, lhs, lhs_ty, rhs, rhs_ty);
2865 break :result try func.binOp(.shr, lhs, lhs_ty, rhs, rhs_ty);
28722866 };
2873 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
2867 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
28742868}
28752869
2876fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) !void {
2877 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2878 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .optional_payload for {}", .{self.target.cpu.arch});
2879 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
2870fn airOptionalPayload(func: *Func, inst: Air.Inst.Index) !void {
2871 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2872 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .optional_payload for {}", .{func.target.cpu.arch});
2873 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
28802874}
28812875
2882fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) !void {
2883 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2884 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .optional_payload_ptr for {}", .{self.target.cpu.arch});
2885 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
2876fn airOptionalPayloadPtr(func: *Func, inst: Air.Inst.Index) !void {
2877 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2878 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .optional_payload_ptr for {}", .{func.target.cpu.arch});
2879 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
28862880}
28872881
2888fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void {
2889 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2890 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .optional_payload_ptr_set for {}", .{self.target.cpu.arch});
2891 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
2882fn airOptionalPayloadPtrSet(func: *Func, inst: Air.Inst.Index) !void {
2883 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2884 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .optional_payload_ptr_set for {}", .{func.target.cpu.arch});
2885 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
28922886}
28932887
2894fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void {
2895 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2896 const zcu = self.bin_file.comp.module.?;
2897 const err_union_ty = self.typeOf(ty_op.operand);
2888fn airUnwrapErrErr(func: *Func, inst: Air.Inst.Index) !void {
2889 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2890 const zcu = func.bin_file.comp.module.?;
2891 const err_union_ty = func.typeOf(ty_op.operand);
28982892 const err_ty = err_union_ty.errorUnionSet(zcu);
28992893 const payload_ty = err_union_ty.errorUnionPayload(zcu);
2900 const operand = try self.resolveInst(ty_op.operand);
2894 const operand = try func.resolveInst(ty_op.operand);
29012895
29022896 const result: MCValue = result: {
29032897 if (err_ty.errorSetIsEmpty(zcu)) {
......@@ -2912,13 +2906,13 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void {
29122906
29132907 switch (operand) {
29142908 .register => |reg| {
2915 const eu_lock = self.register_manager.lockReg(reg);
2916 defer if (eu_lock) |lock| self.register_manager.unlockReg(lock);
2909 const eu_lock = func.register_manager.lockReg(reg);
2910 defer if (eu_lock) |lock| func.register_manager.unlockReg(lock);
29172911
2918 var result = try self.copyToNewRegister(inst, operand);
2912 var result = try func.copyToNewRegister(inst, operand);
29192913
29202914 if (err_off > 0) {
2921 result = try self.binOp(
2915 result = try func.binOp(
29222916 .shr,
29232917 result,
29242918 err_union_ty,
......@@ -2932,27 +2926,27 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void {
29322926 .index = frame_addr.index,
29332927 .off = frame_addr.off + @as(i32, @intCast(err_off)),
29342928 } },
2935 else => return self.fail("TODO implement unwrap_err_err for {}", .{operand}),
2929 else => return func.fail("TODO implement unwrap_err_err for {}", .{operand}),
29362930 }
29372931 };
29382932
2939 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
2933 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
29402934}
29412935
2942fn airUnwrapErrPayload(self: *Self, inst: Air.Inst.Index) !void {
2943 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2944 const operand_ty = self.typeOf(ty_op.operand);
2945 const operand = try self.resolveInst(ty_op.operand);
2946 const result = try self.genUnwrapErrUnionPayloadMir(operand_ty, operand);
2947 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
2936fn airUnwrapErrPayload(func: *Func, inst: Air.Inst.Index) !void {
2937 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2938 const operand_ty = func.typeOf(ty_op.operand);
2939 const operand = try func.resolveInst(ty_op.operand);
2940 const result = try func.genUnwrapErrUnionPayloadMir(operand_ty, operand);
2941 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
29482942}
29492943
29502944fn genUnwrapErrUnionPayloadMir(
2951 self: *Self,
2945 func: *Func,
29522946 err_union_ty: Type,
29532947 err_union: MCValue,
29542948) !MCValue {
2955 const zcu = self.bin_file.comp.module.?;
2949 const zcu = func.bin_file.comp.module.?;
29562950 const payload_ty = err_union_ty.errorUnionPayload(zcu);
29572951
29582952 const result: MCValue = result: {
......@@ -2965,13 +2959,13 @@ fn genUnwrapErrUnionPayloadMir(
29652959 .off = frame_addr.off + payload_off,
29662960 } },
29672961 .register => |reg| {
2968 const eu_lock = self.register_manager.lockReg(reg);
2969 defer if (eu_lock) |lock| self.register_manager.unlockReg(lock);
2962 const eu_lock = func.register_manager.lockReg(reg);
2963 defer if (eu_lock) |lock| func.register_manager.unlockReg(lock);
29702964
2971 var result: MCValue = .{ .register = try self.copyToTmpRegister(err_union_ty, err_union) };
2965 var result: MCValue = .{ .register = try func.copyToTmpRegister(err_union_ty, err_union) };
29722966
29732967 if (payload_off > 0) {
2974 result = try self.binOp(
2968 result = try func.binOp(
29752969 .shr,
29762970 result,
29772971 err_union_ty,
......@@ -2982,7 +2976,7 @@ fn genUnwrapErrUnionPayloadMir(
29822976
29832977 break :result result;
29842978 },
2985 else => return self.fail("TODO implement genUnwrapErrUnionPayloadMir for {}", .{err_union}),
2979 else => return func.fail("TODO implement genUnwrapErrUnionPayloadMir for {}", .{err_union}),
29862980 }
29872981 };
29882982
......@@ -2990,116 +2984,116 @@ fn genUnwrapErrUnionPayloadMir(
29902984}
29912985
29922986// *(E!T) -> E
2993fn airUnwrapErrErrPtr(self: *Self, inst: Air.Inst.Index) !void {
2994 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2995 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement unwrap error union error ptr for {}", .{self.target.cpu.arch});
2996 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
2987fn airUnwrapErrErrPtr(func: *Func, inst: Air.Inst.Index) !void {
2988 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2989 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement unwrap error union error ptr for {}", .{func.target.cpu.arch});
2990 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
29972991}
29982992
29992993// *(E!T) -> *T
3000fn airUnwrapErrPayloadPtr(self: *Self, inst: Air.Inst.Index) !void {
3001 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3002 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement unwrap error union payload ptr for {}", .{self.target.cpu.arch});
3003 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
2994fn airUnwrapErrPayloadPtr(func: *Func, inst: Air.Inst.Index) !void {
2995 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2996 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement unwrap error union payload ptr for {}", .{func.target.cpu.arch});
2997 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
30042998}
30052999
3006fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void {
3007 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3008 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .errunion_payload_ptr_set for {}", .{self.target.cpu.arch});
3009 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3000fn airErrUnionPayloadPtrSet(func: *Func, inst: Air.Inst.Index) !void {
3001 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3002 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .errunion_payload_ptr_set for {}", .{func.target.cpu.arch});
3003 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
30103004}
30113005
3012fn airErrReturnTrace(self: *Self, inst: Air.Inst.Index) !void {
3013 const result: MCValue = if (self.liveness.isUnused(inst))
3006fn airErrReturnTrace(func: *Func, inst: Air.Inst.Index) !void {
3007 const result: MCValue = if (func.liveness.isUnused(inst))
30143008 .unreach
30153009 else
3016 return self.fail("TODO implement airErrReturnTrace for {}", .{self.target.cpu.arch});
3017 return self.finishAir(inst, result, .{ .none, .none, .none });
3010 return func.fail("TODO implement airErrReturnTrace for {}", .{func.target.cpu.arch});
3011 return func.finishAir(inst, result, .{ .none, .none, .none });
30183012}
30193013
3020fn airSetErrReturnTrace(self: *Self, inst: Air.Inst.Index) !void {
3014fn airSetErrReturnTrace(func: *Func, inst: Air.Inst.Index) !void {
30213015 _ = inst;
3022 return self.fail("TODO implement airSetErrReturnTrace for {}", .{self.target.cpu.arch});
3016 return func.fail("TODO implement airSetErrReturnTrace for {}", .{func.target.cpu.arch});
30233017}
30243018
3025fn airSaveErrReturnTraceIndex(self: *Self, inst: Air.Inst.Index) !void {
3019fn airSaveErrReturnTraceIndex(func: *Func, inst: Air.Inst.Index) !void {
30263020 _ = inst;
3027 return self.fail("TODO implement airSaveErrReturnTraceIndex for {}", .{self.target.cpu.arch});
3021 return func.fail("TODO implement airSaveErrReturnTraceIndex for {}", .{func.target.cpu.arch});
30283022}
30293023
3030fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void {
3031 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3032 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
3033 const zcu = self.bin_file.comp.module.?;
3034 const optional_ty = self.typeOfIndex(inst);
3024fn airWrapOptional(func: *Func, inst: Air.Inst.Index) !void {
3025 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3026 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
3027 const zcu = func.bin_file.comp.module.?;
3028 const optional_ty = func.typeOfIndex(inst);
30353029
30363030 // Optional with a zero-bit payload type is just a boolean true
30373031 if (optional_ty.abiSize(zcu) == 1)
30383032 break :result MCValue{ .immediate = 1 };
30393033
3040 return self.fail("TODO implement wrap optional for {}", .{self.target.cpu.arch});
3034 return func.fail("TODO implement wrap optional for {}", .{func.target.cpu.arch});
30413035 };
3042 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3036 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
30433037}
30443038
30453039/// T to E!T
3046fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void {
3047 const zcu = self.bin_file.comp.module.?;
3048 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3040fn airWrapErrUnionPayload(func: *Func, inst: Air.Inst.Index) !void {
3041 const zcu = func.bin_file.comp.module.?;
3042 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
30493043
30503044 const eu_ty = ty_op.ty.toType();
30513045 const pl_ty = eu_ty.errorUnionPayload(zcu);
30523046 const err_ty = eu_ty.errorUnionSet(zcu);
3053 const operand = try self.resolveInst(ty_op.operand);
3047 const operand = try func.resolveInst(ty_op.operand);
30543048
30553049 const result: MCValue = result: {
30563050 if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .{ .immediate = 0 };
30573051
3058 const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu));
3052 const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu));
30593053 const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, zcu));
30603054 const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, zcu));
3061 try self.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, operand);
3062 try self.genSetMem(.{ .frame = frame_index }, err_off, err_ty, .{ .immediate = 0 });
3055 try func.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, operand);
3056 try func.genSetMem(.{ .frame = frame_index }, err_off, err_ty, .{ .immediate = 0 });
30633057 break :result .{ .load_frame = .{ .index = frame_index } };
30643058 };
30653059
3066 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3060 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
30673061}
30683062
30693063/// E to E!T
3070fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void {
3071 const zcu = self.bin_file.comp.module.?;
3072 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3064fn airWrapErrUnionErr(func: *Func, inst: Air.Inst.Index) !void {
3065 const zcu = func.bin_file.comp.module.?;
3066 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
30733067
30743068 const eu_ty = ty_op.ty.toType();
30753069 const pl_ty = eu_ty.errorUnionPayload(zcu);
30763070 const err_ty = eu_ty.errorUnionSet(zcu);
30773071
30783072 const result: MCValue = result: {
3079 if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result try self.resolveInst(ty_op.operand);
3073 if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result try func.resolveInst(ty_op.operand);
30803074
3081 const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu));
3075 const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu));
30823076 const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, zcu));
30833077 const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, zcu));
3084 try self.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, .undef);
3085 const operand = try self.resolveInst(ty_op.operand);
3086 try self.genSetMem(.{ .frame = frame_index }, err_off, err_ty, operand);
3078 try func.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, .undef);
3079 const operand = try func.resolveInst(ty_op.operand);
3080 try func.genSetMem(.{ .frame = frame_index }, err_off, err_ty, operand);
30873081 break :result .{ .load_frame = .{ .index = frame_index } };
30883082 };
3089 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3083 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
30903084}
30913085
3092fn airTry(self: *Self, inst: Air.Inst.Index) !void {
3093 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
3094 const extra = self.air.extraData(Air.Try, pl_op.payload);
3095 const body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len]);
3096 const operand_ty = self.typeOf(pl_op.operand);
3097 const result = try self.genTry(inst, pl_op.operand, body, operand_ty, false);
3098 return self.finishAir(inst, result, .{ .none, .none, .none });
3086fn airTry(func: *Func, inst: Air.Inst.Index) !void {
3087 const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
3088 const extra = func.air.extraData(Air.Try, pl_op.payload);
3089 const body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len]);
3090 const operand_ty = func.typeOf(pl_op.operand);
3091 const result = try func.genTry(inst, pl_op.operand, body, operand_ty, false);
3092 return func.finishAir(inst, result, .{ .none, .none, .none });
30993093}
31003094
31013095fn genTry(
3102 self: *Self,
3096 func: *Func,
31033097 inst: Air.Inst.Index,
31043098 operand: Air.Inst.Ref,
31053099 body: []const Air.Inst.Index,
......@@ -3108,180 +3102,180 @@ fn genTry(
31083102) !MCValue {
31093103 _ = operand_is_ptr;
31103104
3111 const liveness_cond_br = self.liveness.getCondBr(inst);
3105 const liveness_cond_br = func.liveness.getCondBr(inst);
31123106
3113 const operand_mcv = try self.resolveInst(operand);
3114 const is_err_mcv = try self.isErr(null, operand_ty, operand_mcv);
3107 const operand_mcv = try func.resolveInst(operand);
3108 const is_err_mcv = try func.isErr(null, operand_ty, operand_mcv);
31153109
31163110 // A branch to the false section. Uses beq. 1 is the default "true" state.
3117 const reloc = try self.condBr(Type.anyerror, is_err_mcv);
3111 const reloc = try func.condBr(Type.anyerror, is_err_mcv);
31183112
3119 if (self.liveness.operandDies(inst, 0)) {
3120 if (operand.toIndex()) |operand_inst| try self.processDeath(operand_inst);
3113 if (func.liveness.operandDies(inst, 0)) {
3114 if (operand.toIndex()) |operand_inst| try func.processDeath(operand_inst);
31213115 }
31223116
3123 self.scope_generation += 1;
3124 const state = try self.saveState();
3117 func.scope_generation += 1;
3118 const state = try func.saveState();
31253119
3126 for (liveness_cond_br.else_deaths) |death| try self.processDeath(death);
3127 try self.genBody(body);
3128 try self.restoreState(state, &.{}, .{
3120 for (liveness_cond_br.else_deaths) |death| try func.processDeath(death);
3121 try func.genBody(body);
3122 try func.restoreState(state, &.{}, .{
31293123 .emit_instructions = false,
31303124 .update_tracking = true,
31313125 .resurrect = true,
31323126 .close_scope = true,
31333127 });
31343128
3135 self.performReloc(reloc);
3129 func.performReloc(reloc);
31363130
3137 for (liveness_cond_br.then_deaths) |death| try self.processDeath(death);
3131 for (liveness_cond_br.then_deaths) |death| try func.processDeath(death);
31383132
3139 const result = if (self.liveness.isUnused(inst))
3133 const result = if (func.liveness.isUnused(inst))
31403134 .unreach
31413135 else
3142 try self.genUnwrapErrUnionPayloadMir(operand_ty, operand_mcv);
3136 try func.genUnwrapErrUnionPayloadMir(operand_ty, operand_mcv);
31433137
31443138 return result;
31453139}
31463140
3147fn airSlicePtr(self: *Self, inst: Air.Inst.Index) !void {
3148 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3141fn airSlicePtr(func: *Func, inst: Air.Inst.Index) !void {
3142 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
31493143 const result = result: {
3150 const src_mcv = try self.resolveInst(ty_op.operand);
3151 if (self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result src_mcv;
3144 const src_mcv = try func.resolveInst(ty_op.operand);
3145 if (func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result src_mcv;
31523146
3153 const dst_mcv = try self.allocRegOrMem(inst, true);
3154 const dst_ty = self.typeOfIndex(inst);
3155 try self.genCopy(dst_ty, dst_mcv, src_mcv);
3147 const dst_mcv = try func.allocRegOrMem(inst, true);
3148 const dst_ty = func.typeOfIndex(inst);
3149 try func.genCopy(dst_ty, dst_mcv, src_mcv);
31563150 break :result dst_mcv;
31573151 };
3158 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3152 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
31593153}
31603154
3161fn airSliceLen(self: *Self, inst: Air.Inst.Index) !void {
3162 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3163 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
3164 const src_mcv = try self.resolveInst(ty_op.operand);
3155fn airSliceLen(func: *Func, inst: Air.Inst.Index) !void {
3156 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3157 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
3158 const src_mcv = try func.resolveInst(ty_op.operand);
31653159 switch (src_mcv) {
31663160 .load_frame => |frame_addr| {
31673161 const len_mcv: MCValue = .{ .load_frame = .{
31683162 .index = frame_addr.index,
31693163 .off = frame_addr.off + 8,
31703164 } };
3171 if (self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv;
3165 if (func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv;
31723166
3173 const dst_mcv = try self.allocRegOrMem(inst, true);
3174 try self.genCopy(Type.usize, dst_mcv, len_mcv);
3167 const dst_mcv = try func.allocRegOrMem(inst, true);
3168 try func.genCopy(Type.usize, dst_mcv, len_mcv);
31753169 break :result dst_mcv;
31763170 },
31773171 .register_pair => |pair| {
31783172 const len_mcv: MCValue = .{ .register = pair[1] };
31793173
3180 if (self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv;
3174 if (func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv;
31813175
3182 const dst_mcv = try self.allocRegOrMem(inst, true);
3183 try self.genCopy(Type.usize, dst_mcv, len_mcv);
3176 const dst_mcv = try func.allocRegOrMem(inst, true);
3177 try func.genCopy(Type.usize, dst_mcv, len_mcv);
31843178 break :result dst_mcv;
31853179 },
3186 else => return self.fail("TODO airSliceLen for {}", .{src_mcv}),
3180 else => return func.fail("TODO airSliceLen for {}", .{src_mcv}),
31873181 }
31883182 };
3189 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3183 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
31903184}
31913185
3192fn airPtrSliceLenPtr(self: *Self, inst: Air.Inst.Index) !void {
3193 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3194 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement ptr_slice_len_ptr for {}", .{self.target.cpu.arch});
3195 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3186fn airPtrSliceLenPtr(func: *Func, inst: Air.Inst.Index) !void {
3187 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3188 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement ptr_slice_len_ptr for {}", .{func.target.cpu.arch});
3189 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
31963190}
31973191
3198fn airPtrSlicePtrPtr(self: *Self, inst: Air.Inst.Index) !void {
3199 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3200 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement ptr_slice_ptr_ptr for {}", .{self.target.cpu.arch});
3201 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3192fn airPtrSlicePtrPtr(func: *Func, inst: Air.Inst.Index) !void {
3193 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3194 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement ptr_slice_ptr_ptr for {}", .{func.target.cpu.arch});
3195 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
32023196}
32033197
3204fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) !void {
3205 const zcu = self.bin_file.comp.module.?;
3198fn airSliceElemVal(func: *Func, inst: Air.Inst.Index) !void {
3199 const zcu = func.bin_file.comp.module.?;
32063200 const is_volatile = false; // TODO
3207 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3201 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
32083202
3209 if (!is_volatile and self.liveness.isUnused(inst)) return self.finishAir(
3203 if (!is_volatile and func.liveness.isUnused(inst)) return func.finishAir(
32103204 inst,
32113205 .unreach,
32123206 .{ bin_op.lhs, bin_op.rhs, .none },
32133207 );
32143208 const result: MCValue = result: {
3215 const slice_mcv = try self.resolveInst(bin_op.lhs);
3216 const index_mcv = try self.resolveInst(bin_op.rhs);
3209 const slice_mcv = try func.resolveInst(bin_op.lhs);
3210 const index_mcv = try func.resolveInst(bin_op.rhs);
32173211
3218 const slice_ty = self.typeOf(bin_op.lhs);
3212 const slice_ty = func.typeOf(bin_op.lhs);
32193213
32203214 const slice_ptr_field_type = slice_ty.slicePtrFieldType(zcu);
32213215
32223216 const index_lock: ?RegisterLock = if (index_mcv == .register)
3223 self.register_manager.lockRegAssumeUnused(index_mcv.register)
3217 func.register_manager.lockRegAssumeUnused(index_mcv.register)
32243218 else
32253219 null;
3226 defer if (index_lock) |reg| self.register_manager.unlockReg(reg);
3220 defer if (index_lock) |reg| func.register_manager.unlockReg(reg);
32273221
32283222 const base_mcv: MCValue = switch (slice_mcv) {
32293223 .load_frame,
32303224 .load_symbol,
3231 => .{ .register = try self.copyToTmpRegister(slice_ptr_field_type, slice_mcv) },
3232 else => return self.fail("TODO slice_elem_val when slice is {}", .{slice_mcv}),
3225 => .{ .register = try func.copyToTmpRegister(slice_ptr_field_type, slice_mcv) },
3226 else => return func.fail("TODO slice_elem_val when slice is {}", .{slice_mcv}),
32333227 };
32343228
3235 const dest = try self.allocRegOrMem(inst, true);
3236 const addr = try self.binOp(.ptr_add, base_mcv, slice_ptr_field_type, index_mcv, Type.usize);
3237 try self.load(dest, addr, slice_ptr_field_type);
3229 const dest = try func.allocRegOrMem(inst, true);
3230 const addr = try func.binOp(.ptr_add, base_mcv, slice_ptr_field_type, index_mcv, Type.usize);
3231 try func.load(dest, addr, slice_ptr_field_type);
32383232
32393233 break :result dest;
32403234 };
3241 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
3235 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
32423236}
32433237
3244fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) !void {
3245 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3246 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
3247 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement slice_elem_ptr for {}", .{self.target.cpu.arch});
3248 return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none });
3238fn airSliceElemPtr(func: *Func, inst: Air.Inst.Index) !void {
3239 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3240 const extra = func.air.extraData(Air.Bin, ty_pl.payload).data;
3241 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement slice_elem_ptr for {}", .{func.target.cpu.arch});
3242 return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none });
32493243}
32503244
3251fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void {
3252 const zcu = self.bin_file.comp.module.?;
3253 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3254 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
3255 const array_ty = self.typeOf(bin_op.lhs);
3256 const array_mcv = try self.resolveInst(bin_op.lhs);
3245fn airArrayElemVal(func: *Func, inst: Air.Inst.Index) !void {
3246 const zcu = func.bin_file.comp.module.?;
3247 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3248 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
3249 const array_ty = func.typeOf(bin_op.lhs);
3250 const array_mcv = try func.resolveInst(bin_op.lhs);
32573251
3258 const index_mcv = try self.resolveInst(bin_op.rhs);
3259 const index_ty = self.typeOf(bin_op.rhs);
3252 const index_mcv = try func.resolveInst(bin_op.rhs);
3253 const index_ty = func.typeOf(bin_op.rhs);
32603254
32613255 const elem_ty = array_ty.childType(zcu);
32623256 const elem_abi_size = elem_ty.abiSize(zcu);
32633257
3264 const addr_reg, const addr_reg_lock = try self.allocReg(.int);
3265 defer self.register_manager.unlockReg(addr_reg_lock);
3258 const addr_reg, const addr_reg_lock = try func.allocReg(.int);
3259 defer func.register_manager.unlockReg(addr_reg_lock);
32663260
32673261 switch (array_mcv) {
32683262 .register => {
3269 const frame_index = try self.allocFrameIndex(FrameAlloc.initType(array_ty, zcu));
3270 try self.genSetMem(.{ .frame = frame_index }, 0, array_ty, array_mcv);
3271 try self.genSetReg(Type.usize, addr_reg, .{ .lea_frame = .{ .index = frame_index } });
3263 const frame_index = try func.allocFrameIndex(FrameAlloc.initType(array_ty, zcu));
3264 try func.genSetMem(.{ .frame = frame_index }, 0, array_ty, array_mcv);
3265 try func.genSetReg(Type.usize, addr_reg, .{ .lea_frame = .{ .index = frame_index } });
32723266 },
32733267 .load_frame => |frame_addr| {
3274 try self.genSetReg(Type.usize, addr_reg, .{ .lea_frame = frame_addr });
3268 try func.genSetReg(Type.usize, addr_reg, .{ .lea_frame = frame_addr });
32753269 },
3276 else => try self.genSetReg(Type.usize, addr_reg, array_mcv.address()),
3270 else => try func.genSetReg(Type.usize, addr_reg, array_mcv.address()),
32773271 }
32783272
3279 const offset_reg = try self.elemOffset(index_ty, index_mcv, elem_abi_size);
3280 const offset_lock = self.register_manager.lockRegAssumeUnused(offset_reg);
3281 defer self.register_manager.unlockReg(offset_lock);
3273 const offset_reg = try func.elemOffset(index_ty, index_mcv, elem_abi_size);
3274 const offset_lock = func.register_manager.lockRegAssumeUnused(offset_reg);
3275 defer func.register_manager.unlockReg(offset_lock);
32823276
3283 const dst_mcv = try self.allocRegOrMem(inst, false);
3284 _ = try self.addInst(.{
3277 const dst_mcv = try func.allocRegOrMem(inst, false);
3278 _ = try func.addInst(.{
32853279 .tag = .add,
32863280 .ops = .rrr,
32873281 .data = .{ .r_type = .{
......@@ -3290,138 +3284,138 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void {
32903284 .rs2 = addr_reg,
32913285 } },
32923286 });
3293 try self.genCopy(elem_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } });
3287 try func.genCopy(elem_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } });
32943288 break :result dst_mcv;
32953289 };
3296 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
3290 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
32973291}
32983292
3299fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) !void {
3293fn airPtrElemVal(func: *Func, inst: Air.Inst.Index) !void {
33003294 const is_volatile = false; // TODO
3301 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3302 const result: MCValue = if (!is_volatile and self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement ptr_elem_val for {}", .{self.target.cpu.arch});
3303 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
3295 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3296 const result: MCValue = if (!is_volatile and func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement ptr_elem_val for {}", .{func.target.cpu.arch});
3297 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
33043298}
33053299
3306fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) !void {
3307 const zcu = self.bin_file.comp.module.?;
3308 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3309 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
3300fn airPtrElemPtr(func: *Func, inst: Air.Inst.Index) !void {
3301 const zcu = func.bin_file.comp.module.?;
3302 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3303 const extra = func.air.extraData(Air.Bin, ty_pl.payload).data;
33103304
33113305 const result = result: {
3312 const elem_ptr_ty = self.typeOfIndex(inst);
3313 const base_ptr_ty = self.typeOf(extra.lhs);
3306 const elem_ptr_ty = func.typeOfIndex(inst);
3307 const base_ptr_ty = func.typeOf(extra.lhs);
33143308
3315 const base_ptr_mcv = try self.resolveInst(extra.lhs);
3309 const base_ptr_mcv = try func.resolveInst(extra.lhs);
33163310 const base_ptr_lock: ?RegisterLock = switch (base_ptr_mcv) {
3317 .register => |reg| self.register_manager.lockRegAssumeUnused(reg),
3311 .register => |reg| func.register_manager.lockRegAssumeUnused(reg),
33183312 else => null,
33193313 };
3320 defer if (base_ptr_lock) |lock| self.register_manager.unlockReg(lock);
3314 defer if (base_ptr_lock) |lock| func.register_manager.unlockReg(lock);
33213315
33223316 if (elem_ptr_ty.ptrInfo(zcu).flags.vector_index != .none) {
3323 break :result if (self.reuseOperand(inst, extra.lhs, 0, base_ptr_mcv))
3317 break :result if (func.reuseOperand(inst, extra.lhs, 0, base_ptr_mcv))
33243318 base_ptr_mcv
33253319 else
3326 try self.copyToNewRegister(inst, base_ptr_mcv);
3320 try func.copyToNewRegister(inst, base_ptr_mcv);
33273321 }
33283322
33293323 const elem_ty = base_ptr_ty.elemType2(zcu);
33303324 const elem_abi_size = elem_ty.abiSize(zcu);
3331 const index_ty = self.typeOf(extra.rhs);
3332 const index_mcv = try self.resolveInst(extra.rhs);
3325 const index_ty = func.typeOf(extra.rhs);
3326 const index_mcv = try func.resolveInst(extra.rhs);
33333327 const index_lock: ?RegisterLock = switch (index_mcv) {
3334 .register => |reg| self.register_manager.lockRegAssumeUnused(reg),
3328 .register => |reg| func.register_manager.lockRegAssumeUnused(reg),
33353329 else => null,
33363330 };
3337 defer if (index_lock) |lock| self.register_manager.unlockReg(lock);
3331 defer if (index_lock) |lock| func.register_manager.unlockReg(lock);
33383332
3339 const offset_reg = try self.elemOffset(index_ty, index_mcv, elem_abi_size);
3340 const offset_reg_lock = self.register_manager.lockRegAssumeUnused(offset_reg);
3341 defer self.register_manager.unlockReg(offset_reg_lock);
3333 const offset_reg = try func.elemOffset(index_ty, index_mcv, elem_abi_size);
3334 const offset_reg_lock = func.register_manager.lockRegAssumeUnused(offset_reg);
3335 defer func.register_manager.unlockReg(offset_reg_lock);
33423336
3343 break :result try self.binOp(.ptr_add, base_ptr_mcv, base_ptr_ty, .{ .register = offset_reg }, base_ptr_ty);
3337 break :result try func.binOp(.ptr_add, base_ptr_mcv, base_ptr_ty, .{ .register = offset_reg }, base_ptr_ty);
33443338 };
3345 return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none });
3339 return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none });
33463340}
33473341
3348fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) !void {
3349 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3342fn airSetUnionTag(func: *Func, inst: Air.Inst.Index) !void {
3343 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
33503344 _ = bin_op;
3351 return self.fail("TODO implement airSetUnionTag for {}", .{self.target.cpu.arch});
3352 // return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
3345 return func.fail("TODO implement airSetUnionTag for {}", .{func.target.cpu.arch});
3346 // return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
33533347}
33543348
3355fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) !void {
3356 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3357 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airGetUnionTag for {}", .{self.target.cpu.arch});
3358 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3349fn airGetUnionTag(func: *Func, inst: Air.Inst.Index) !void {
3350 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3351 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airGetUnionTag for {}", .{func.target.cpu.arch});
3352 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
33593353}
33603354
3361fn airClz(self: *Self, inst: Air.Inst.Index) !void {
3362 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3363 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airClz for {}", .{self.target.cpu.arch});
3364 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3355fn airClz(func: *Func, inst: Air.Inst.Index) !void {
3356 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3357 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airClz for {}", .{func.target.cpu.arch});
3358 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
33653359}
33663360
3367fn airCtz(self: *Self, inst: Air.Inst.Index) !void {
3368 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3361fn airCtz(func: *Func, inst: Air.Inst.Index) !void {
3362 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
33693363 _ = ty_op;
3370 return self.fail("TODO: finish ctz", .{});
3364 return func.fail("TODO: finish ctz", .{});
33713365}
33723366
3373fn airPopcount(self: *Self, inst: Air.Inst.Index) !void {
3374 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3375 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airPopcount for {}", .{self.target.cpu.arch});
3376 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3367fn airPopcount(func: *Func, inst: Air.Inst.Index) !void {
3368 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3369 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airPopcount for {}", .{func.target.cpu.arch});
3370 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
33773371}
33783372
3379fn airAbs(self: *Self, inst: Air.Inst.Index) !void {
3380 const zcu = self.bin_file.comp.module.?;
3381 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3382 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
3383 const ty = self.typeOf(ty_op.operand);
3373fn airAbs(func: *Func, inst: Air.Inst.Index) !void {
3374 const zcu = func.bin_file.comp.module.?;
3375 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3376 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
3377 const ty = func.typeOf(ty_op.operand);
33843378 const scalar_ty = ty.scalarType(zcu);
3385 const operand = try self.resolveInst(ty_op.operand);
3379 const operand = try func.resolveInst(ty_op.operand);
33863380 _ = operand;
33873381
33883382 switch (scalar_ty.zigTypeTag(zcu)) {
33893383 .Int => if (ty.zigTypeTag(zcu) == .Vector) {
3390 return self.fail("TODO implement airAbs for {}", .{ty.fmt(zcu)});
3384 return func.fail("TODO implement airAbs for {}", .{ty.fmt(zcu)});
33913385 } else {
3392 return self.fail("TODO: implement airAbs for Int", .{});
3386 return func.fail("TODO: implement airAbs for Int", .{});
33933387 },
3394 else => return self.fail("TODO: implement airAbs {}", .{scalar_ty.fmt(zcu)}),
3388 else => return func.fail("TODO: implement airAbs {}", .{scalar_ty.fmt(zcu)}),
33953389 }
33963390
33973391 break :result .{.unreach};
33983392 };
3399 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3393 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
34003394}
34013395
3402fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void {
3403 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3404 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
3405 const zcu = self.bin_file.comp.module.?;
3406 const ty = self.typeOf(ty_op.operand);
3407 const operand = try self.resolveInst(ty_op.operand);
3396fn airByteSwap(func: *Func, inst: Air.Inst.Index) !void {
3397 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3398 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
3399 const zcu = func.bin_file.comp.module.?;
3400 const ty = func.typeOf(ty_op.operand);
3401 const operand = try func.resolveInst(ty_op.operand);
34083402
34093403 const int_bits = ty.intInfo(zcu).bits;
34103404
34113405 // bytes are no-op
3412 if (int_bits == 8 and self.reuseOperand(inst, ty_op.operand, 0, operand)) {
3413 return self.finishAir(inst, operand, .{ ty_op.operand, .none, .none });
3406 if (int_bits == 8 and func.reuseOperand(inst, ty_op.operand, 0, operand)) {
3407 return func.finishAir(inst, operand, .{ ty_op.operand, .none, .none });
34143408 }
34153409
3416 const dest_mcv = try self.copyToNewRegister(inst, operand);
3410 const dest_mcv = try func.copyToNewRegister(inst, operand);
34173411 const dest_reg = dest_mcv.register;
34183412
34193413 switch (int_bits) {
34203414 16 => {
3421 const temp_reg, const temp_lock = try self.allocReg(.int);
3422 defer self.register_manager.unlockReg(temp_lock);
3415 const temp_reg, const temp_lock = try func.allocReg(.int);
3416 defer func.register_manager.unlockReg(temp_lock);
34233417
3424 _ = try self.addInst(.{
3418 _ = try func.addInst(.{
34253419 .tag = .srli,
34263420 .ops = .rri,
34273421 .data = .{ .i_type = .{
......@@ -3431,7 +3425,7 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void {
34313425 } },
34323426 });
34333427
3434 _ = try self.addInst(.{
3428 _ = try func.addInst(.{
34353429 .tag = .slli,
34363430 .ops = .rri,
34373431 .data = .{ .i_type = .{
......@@ -3440,7 +3434,7 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void {
34403434 .rs1 = dest_reg,
34413435 } },
34423436 });
3443 _ = try self.addInst(.{
3437 _ = try func.addInst(.{
34443438 .tag = .@"or",
34453439 .ops = .rri,
34463440 .data = .{ .r_type = .{
......@@ -3450,49 +3444,49 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void {
34503444 } },
34513445 });
34523446 },
3453 else => return self.fail("TODO: {d} bits for airByteSwap", .{int_bits}),
3447 else => return func.fail("TODO: {d} bits for airByteSwap", .{int_bits}),
34543448 }
34553449
34563450 break :result dest_mcv;
34573451 };
3458 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3452 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
34593453}
34603454
3461fn airBitReverse(self: *Self, inst: Air.Inst.Index) !void {
3462 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3463 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airBitReverse for {}", .{self.target.cpu.arch});
3464 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3455fn airBitReverse(func: *Func, inst: Air.Inst.Index) !void {
3456 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3457 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airBitReverse for {}", .{func.target.cpu.arch});
3458 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
34653459}
34663460
3467fn airUnaryMath(self: *Self, inst: Air.Inst.Index) !void {
3468 const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];
3469 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
3470 const result: MCValue = if (self.liveness.isUnused(inst))
3461fn airUnaryMath(func: *Func, inst: Air.Inst.Index) !void {
3462 const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)];
3463 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
3464 const result: MCValue = if (func.liveness.isUnused(inst))
34713465 .unreach
34723466 else
3473 return self.fail("TODO implementairUnaryMath {s} for {}", .{ @tagName(tag), self.target.cpu.arch });
3474 return self.finishAir(inst, result, .{ un_op, .none, .none });
3467 return func.fail("TODO implementairUnaryMath {s} for {}", .{ @tagName(tag), func.target.cpu.arch });
3468 return func.finishAir(inst, result, .{ un_op, .none, .none });
34753469}
34763470
34773471fn reuseOperand(
3478 self: *Self,
3472 func: *Func,
34793473 inst: Air.Inst.Index,
34803474 operand: Air.Inst.Ref,
34813475 op_index: Liveness.OperandInt,
34823476 mcv: MCValue,
34833477) bool {
3484 return self.reuseOperandAdvanced(inst, operand, op_index, mcv, inst);
3478 return func.reuseOperandAdvanced(inst, operand, op_index, mcv, inst);
34853479}
34863480
34873481fn reuseOperandAdvanced(
3488 self: *Self,
3482 func: *Func,
34893483 inst: Air.Inst.Index,
34903484 operand: Air.Inst.Ref,
34913485 op_index: Liveness.OperandInt,
34923486 mcv: MCValue,
34933487 maybe_tracked_inst: ?Air.Inst.Index,
34943488) bool {
3495 if (!self.liveness.operandDies(inst, op_index))
3489 if (!func.liveness.operandDies(inst, op_index))
34963490 return false;
34973491
34983492 switch (mcv) {
......@@ -3502,59 +3496,59 @@ fn reuseOperandAdvanced(
35023496 // If it's in the registers table, need to associate the register(s) with the
35033497 // new instruction.
35043498 if (maybe_tracked_inst) |tracked_inst| {
3505 if (!self.register_manager.isRegFree(reg)) {
3499 if (!func.register_manager.isRegFree(reg)) {
35063500 if (RegisterManager.indexOfRegIntoTracked(reg)) |index| {
3507 self.register_manager.registers[index] = tracked_inst;
3501 func.register_manager.registers[index] = tracked_inst;
35083502 }
35093503 }
3510 } else self.register_manager.freeReg(reg);
3504 } else func.register_manager.freeReg(reg);
35113505 },
35123506 .load_frame => |frame_addr| if (frame_addr.index.isNamed()) return false,
35133507 else => return false,
35143508 }
35153509
35163510 // Prevent the operand deaths processing code from deallocating it.
3517 self.liveness.clearOperandDeath(inst, op_index);
3511 func.liveness.clearOperandDeath(inst, op_index);
35183512 const op_inst = operand.toIndex().?;
3519 self.getResolvedInstValue(op_inst).reuse(self, maybe_tracked_inst, op_inst);
3513 func.getResolvedInstValue(op_inst).reuse(func, maybe_tracked_inst, op_inst);
35203514
35213515 return true;
35223516}
35233517
3524fn airLoad(self: *Self, inst: Air.Inst.Index) !void {
3525 const zcu = self.bin_file.comp.module.?;
3526 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3527 const elem_ty = self.typeOfIndex(inst);
3518fn airLoad(func: *Func, inst: Air.Inst.Index) !void {
3519 const zcu = func.bin_file.comp.module.?;
3520 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3521 const elem_ty = func.typeOfIndex(inst);
35283522
35293523 const result: MCValue = result: {
35303524 if (!elem_ty.hasRuntimeBits(zcu))
35313525 break :result .none;
35323526
3533 const ptr = try self.resolveInst(ty_op.operand);
3534 const is_volatile = self.typeOf(ty_op.operand).isVolatilePtr(zcu);
3535 if (self.liveness.isUnused(inst) and !is_volatile)
3527 const ptr = try func.resolveInst(ty_op.operand);
3528 const is_volatile = func.typeOf(ty_op.operand).isVolatilePtr(zcu);
3529 if (func.liveness.isUnused(inst) and !is_volatile)
35363530 break :result .unreach;
35373531
35383532 const elem_size = elem_ty.abiSize(zcu);
35393533
35403534 const dst_mcv: MCValue = blk: {
35413535 // Pointer is 8 bytes, and if the element is more than that, we cannot reuse it.
3542 if (elem_size <= 8 and self.reuseOperand(inst, ty_op.operand, 0, ptr)) {
3536 if (elem_size <= 8 and func.reuseOperand(inst, ty_op.operand, 0, ptr)) {
35433537 // The MCValue that holds the pointer can be re-used as the value.
35443538 break :blk ptr;
35453539 } else {
3546 break :blk try self.allocRegOrMem(inst, true);
3540 break :blk try func.allocRegOrMem(inst, true);
35473541 }
35483542 };
35493543
3550 try self.load(dst_mcv, ptr, self.typeOf(ty_op.operand));
3544 try func.load(dst_mcv, ptr, func.typeOf(ty_op.operand));
35513545 break :result dst_mcv;
35523546 };
3553 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3547 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
35543548}
35553549
3556fn load(self: *Self, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerError!void {
3557 const zcu = self.bin_file.comp.module.?;
3550fn load(func: *Func, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerError!void {
3551 const zcu = func.bin_file.comp.module.?;
35583552 const dst_ty = ptr_ty.childType(zcu);
35593553
35603554 log.debug("loading {}:{} into {}", .{ ptr_mcv, ptr_ty.fmt(zcu), dst_mcv });
......@@ -3573,43 +3567,43 @@ fn load(self: *Self, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerErro
35733567 .register_offset,
35743568 .lea_frame,
35753569 .lea_symbol,
3576 => try self.genCopy(dst_ty, dst_mcv, ptr_mcv.deref()),
3570 => try func.genCopy(dst_ty, dst_mcv, ptr_mcv.deref()),
35773571
35783572 .memory,
35793573 .indirect,
35803574 .load_symbol,
35813575 .load_frame,
35823576 => {
3583 const addr_reg = try self.copyToTmpRegister(ptr_ty, ptr_mcv);
3584 const addr_lock = self.register_manager.lockRegAssumeUnused(addr_reg);
3585 defer self.register_manager.unlockReg(addr_lock);
3577 const addr_reg = try func.copyToTmpRegister(ptr_ty, ptr_mcv);
3578 const addr_lock = func.register_manager.lockRegAssumeUnused(addr_reg);
3579 defer func.register_manager.unlockReg(addr_lock);
35863580
3587 try self.genCopy(dst_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } });
3581 try func.genCopy(dst_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } });
35883582 },
3589 .air_ref => |ptr_ref| try self.load(dst_mcv, try self.resolveInst(ptr_ref), ptr_ty),
3583 .air_ref => |ptr_ref| try func.load(dst_mcv, try func.resolveInst(ptr_ref), ptr_ty),
35903584 }
35913585}
35923586
3593fn airStore(self: *Self, inst: Air.Inst.Index, safety: bool) !void {
3587fn airStore(func: *Func, inst: Air.Inst.Index, safety: bool) !void {
35943588 if (safety) {
35953589 // TODO if the value is undef, write 0xaa bytes to dest
35963590 } else {
35973591 // TODO if the value is undef, don't lower this instruction
35983592 }
3599 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3600 const ptr = try self.resolveInst(bin_op.lhs);
3601 const value = try self.resolveInst(bin_op.rhs);
3602 const ptr_ty = self.typeOf(bin_op.lhs);
3603 const value_ty = self.typeOf(bin_op.rhs);
3593 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3594 const ptr = try func.resolveInst(bin_op.lhs);
3595 const value = try func.resolveInst(bin_op.rhs);
3596 const ptr_ty = func.typeOf(bin_op.lhs);
3597 const value_ty = func.typeOf(bin_op.rhs);
36043598
3605 try self.store(ptr, value, ptr_ty, value_ty);
3599 try func.store(ptr, value, ptr_ty, value_ty);
36063600
3607 return self.finishAir(inst, .none, .{ bin_op.lhs, bin_op.rhs, .none });
3601 return func.finishAir(inst, .none, .{ bin_op.lhs, bin_op.rhs, .none });
36083602}
36093603
36103604/// Loads `value` into the "payload" of `pointer`.
3611fn store(self: *Self, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type, src_ty: Type) !void {
3612 const zcu = self.bin_file.comp.module.?;
3605fn store(func: *Func, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type, src_ty: Type) !void {
3606 const zcu = func.bin_file.comp.module.?;
36133607
36143608 log.debug("storing {}:{} in {}:{}", .{ src_mcv, src_ty.fmt(zcu), ptr_mcv, ptr_ty.fmt(zcu) });
36153609
......@@ -3626,40 +3620,40 @@ fn store(self: *Self, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type, src_ty:
36263620 .register_offset,
36273621 .lea_symbol,
36283622 .lea_frame,
3629 => try self.genCopy(src_ty, ptr_mcv.deref(), src_mcv),
3623 => try func.genCopy(src_ty, ptr_mcv.deref(), src_mcv),
36303624
36313625 .memory,
36323626 .indirect,
36333627 .load_symbol,
36343628 .load_frame,
36353629 => {
3636 const addr_reg = try self.copyToTmpRegister(ptr_ty, ptr_mcv);
3637 const addr_lock = self.register_manager.lockRegAssumeUnused(addr_reg);
3638 defer self.register_manager.unlockReg(addr_lock);
3630 const addr_reg = try func.copyToTmpRegister(ptr_ty, ptr_mcv);
3631 const addr_lock = func.register_manager.lockRegAssumeUnused(addr_reg);
3632 defer func.register_manager.unlockReg(addr_lock);
36393633
3640 try self.genCopy(src_ty, .{ .indirect = .{ .reg = addr_reg } }, src_mcv);
3634 try func.genCopy(src_ty, .{ .indirect = .{ .reg = addr_reg } }, src_mcv);
36413635 },
3642 .air_ref => |ptr_ref| try self.store(try self.resolveInst(ptr_ref), src_mcv, ptr_ty, src_ty),
3636 .air_ref => |ptr_ref| try func.store(try func.resolveInst(ptr_ref), src_mcv, ptr_ty, src_ty),
36433637 }
36443638}
36453639
3646fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) !void {
3647 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3648 const extra = self.air.extraData(Air.StructField, ty_pl.payload).data;
3649 const result = try self.structFieldPtr(inst, extra.struct_operand, extra.field_index);
3650 return self.finishAir(inst, result, .{ extra.struct_operand, .none, .none });
3640fn airStructFieldPtr(func: *Func, inst: Air.Inst.Index) !void {
3641 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3642 const extra = func.air.extraData(Air.StructField, ty_pl.payload).data;
3643 const result = try func.structFieldPtr(inst, extra.struct_operand, extra.field_index);
3644 return func.finishAir(inst, result, .{ extra.struct_operand, .none, .none });
36513645}
36523646
3653fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void {
3654 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3655 const result = try self.structFieldPtr(inst, ty_op.operand, index);
3656 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
3647fn airStructFieldPtrIndex(func: *Func, inst: Air.Inst.Index, index: u8) !void {
3648 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3649 const result = try func.structFieldPtr(inst, ty_op.operand, index);
3650 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
36573651}
36583652
3659fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue {
3660 const zcu = self.bin_file.comp.module.?;
3661 const ptr_field_ty = self.typeOfIndex(inst);
3662 const ptr_container_ty = self.typeOf(operand);
3653fn structFieldPtr(func: *Func, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue {
3654 const zcu = func.bin_file.comp.module.?;
3655 const ptr_field_ty = func.typeOfIndex(inst);
3656 const ptr_container_ty = func.typeOf(operand);
36633657 const ptr_container_ty_info = ptr_container_ty.ptrInfo(zcu);
36643658 const container_ty = ptr_container_ty.childType(zcu);
36653659
......@@ -3672,27 +3666,27 @@ fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, inde
36723666 else
36733667 @intCast(container_ty.structFieldOffset(index, zcu));
36743668
3675 const src_mcv = try self.resolveInst(operand);
3669 const src_mcv = try func.resolveInst(operand);
36763670 const dst_mcv = if (switch (src_mcv) {
36773671 .immediate, .lea_frame => true,
3678 .register, .register_offset => self.reuseOperand(inst, operand, 0, src_mcv),
3672 .register, .register_offset => func.reuseOperand(inst, operand, 0, src_mcv),
36793673 else => false,
3680 }) src_mcv else try self.copyToNewRegister(inst, src_mcv);
3674 }) src_mcv else try func.copyToNewRegister(inst, src_mcv);
36813675 return dst_mcv.offset(field_offset);
36823676}
36833677
3684fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {
3685 const mod = self.bin_file.comp.module.?;
3678fn airStructFieldVal(func: *Func, inst: Air.Inst.Index) !void {
3679 const mod = func.bin_file.comp.module.?;
36863680
3687 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3688 const extra = self.air.extraData(Air.StructField, ty_pl.payload).data;
3681 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3682 const extra = func.air.extraData(Air.StructField, ty_pl.payload).data;
36893683 const operand = extra.struct_operand;
36903684 const index = extra.field_index;
36913685
3692 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
3693 const zcu = self.bin_file.comp.module.?;
3694 const src_mcv = try self.resolveInst(operand);
3695 const struct_ty = self.typeOf(operand);
3686 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
3687 const zcu = func.bin_file.comp.module.?;
3688 const src_mcv = try func.resolveInst(operand);
3689 const struct_ty = func.typeOf(operand);
36963690 const field_ty = struct_ty.structFieldType(index, zcu);
36973691 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .none;
36983692
......@@ -3707,20 +3701,20 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {
37073701 switch (src_mcv) {
37083702 .dead, .unreach => unreachable,
37093703 .register => |src_reg| {
3710 const src_reg_lock = self.register_manager.lockRegAssumeUnused(src_reg);
3711 defer self.register_manager.unlockReg(src_reg_lock);
3704 const src_reg_lock = func.register_manager.lockRegAssumeUnused(src_reg);
3705 defer func.register_manager.unlockReg(src_reg_lock);
37123706
37133707 const dst_reg = if (field_off == 0)
3714 (try self.copyToNewRegister(inst, src_mcv)).register
3708 (try func.copyToNewRegister(inst, src_mcv)).register
37153709 else
3716 try self.copyToTmpRegister(Type.usize, .{ .register = src_reg });
3710 try func.copyToTmpRegister(Type.usize, .{ .register = src_reg });
37173711
37183712 const dst_mcv: MCValue = .{ .register = dst_reg };
3719 const dst_lock = self.register_manager.lockReg(dst_reg);
3720 defer if (dst_lock) |lock| self.register_manager.unlockReg(lock);
3713 const dst_lock = func.register_manager.lockReg(dst_reg);
3714 defer if (dst_lock) |lock| func.register_manager.unlockReg(lock);
37213715
37223716 if (field_off > 0) {
3723 _ = try self.addInst(.{
3717 _ = try func.addInst(.{
37243718 .tag = .srli,
37253719 .ops = .rri,
37263720 .data = .{ .i_type = .{
......@@ -3731,7 +3725,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {
37313725 });
37323726 }
37333727
3734 break :result if (field_off == 0) dst_mcv else try self.copyToNewRegister(inst, dst_mcv);
3728 break :result if (field_off == 0) dst_mcv else try func.copyToNewRegister(inst, dst_mcv);
37353729 },
37363730 .load_frame => {
37373731 const field_abi_size: u32 = @intCast(field_ty.abiSize(mod));
......@@ -3746,57 +3740,57 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {
37463740 @intCast(field_bit_size),
37473741 );
37483742
3749 const dst_reg, const dst_lock = try self.allocReg(.int);
3743 const dst_reg, const dst_lock = try func.allocReg(.int);
37503744 const dst_mcv = MCValue{ .register = dst_reg };
3751 defer self.register_manager.unlockReg(dst_lock);
3745 defer func.register_manager.unlockReg(dst_lock);
37523746
3753 try self.genCopy(int_ty, dst_mcv, off_mcv);
3754 break :result try self.copyToNewRegister(inst, dst_mcv);
3747 try func.genCopy(int_ty, dst_mcv, off_mcv);
3748 break :result try func.copyToNewRegister(inst, dst_mcv);
37553749 }
37563750
37573751 const container_abi_size: u32 = @intCast(struct_ty.abiSize(mod));
37583752 const dst_mcv = if (field_byte_off + field_abi_size <= container_abi_size and
3759 self.reuseOperand(inst, operand, 0, src_mcv))
3753 func.reuseOperand(inst, operand, 0, src_mcv))
37603754 off_mcv
37613755 else dst: {
3762 const dst_mcv = try self.allocRegOrMem(inst, true);
3763 try self.genCopy(field_ty, dst_mcv, off_mcv);
3756 const dst_mcv = try func.allocRegOrMem(inst, true);
3757 try func.genCopy(field_ty, dst_mcv, off_mcv);
37643758 break :dst dst_mcv;
37653759 };
37663760 if (field_abi_size * 8 > field_bit_size and dst_mcv.isMemory()) {
3767 const tmp_reg, const tmp_lock = try self.allocReg(.int);
3768 defer self.register_manager.unlockReg(tmp_lock);
3761 const tmp_reg, const tmp_lock = try func.allocReg(.int);
3762 defer func.register_manager.unlockReg(tmp_lock);
37693763
37703764 const hi_mcv =
37713765 dst_mcv.address().offset(@intCast(field_bit_size / 64 * 8)).deref();
3772 try self.genSetReg(Type.usize, tmp_reg, hi_mcv);
3773 try self.genCopy(Type.usize, hi_mcv, .{ .register = tmp_reg });
3766 try func.genSetReg(Type.usize, tmp_reg, hi_mcv);
3767 try func.genCopy(Type.usize, hi_mcv, .{ .register = tmp_reg });
37743768 }
37753769 break :result dst_mcv;
37763770 }
37773771
3778 return self.fail("TODO: airStructFieldVal load_frame field_off non multiple of 8", .{});
3772 return func.fail("TODO: airStructFieldVal load_frame field_off non multiple of 8", .{});
37793773 },
3780 else => return self.fail("TODO: airStructField {s}", .{@tagName(src_mcv)}),
3774 else => return func.fail("TODO: airStructField {s}", .{@tagName(src_mcv)}),
37813775 }
37823776 };
37833777
3784 return self.finishAir(inst, result, .{ extra.struct_operand, .none, .none });
3778 return func.finishAir(inst, result, .{ extra.struct_operand, .none, .none });
37853779}
37863780
3787fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void {
3781fn airFieldParentPtr(func: *Func, inst: Air.Inst.Index) !void {
37883782 _ = inst;
3789 return self.fail("TODO implement codegen airFieldParentPtr", .{});
3783 return func.fail("TODO implement codegen airFieldParentPtr", .{});
37903784}
37913785
3792fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void {
3793 const zcu = self.bin_file.comp.module.?;
3794 const arg = self.air.instructions.items(.data)[@intFromEnum(inst)].arg;
3786fn genArgDbgInfo(func: Func, inst: Air.Inst.Index, mcv: MCValue) !void {
3787 const zcu = func.bin_file.comp.module.?;
3788 const arg = func.air.instructions.items(.data)[@intFromEnum(inst)].arg;
37953789 const ty = arg.ty.toType();
3796 const owner_decl = zcu.funcOwnerDeclIndex(self.func_index);
3797 const name = zcu.getParamName(self.func_index, arg.src_index);
3790 const owner_decl = zcu.funcOwnerDeclIndex(func.func_index);
3791 const name = zcu.getParamName(func.func_index, arg.src_index);
37983792
3799 switch (self.debug_output) {
3793 switch (func.debug_output) {
38003794 .dwarf => |dw| switch (mcv) {
38013795 .register => |reg| try dw.genArgDbgInfo(name, ty, owner_decl, .{
38023796 .register = reg.dwarfLocOp(),
......@@ -3809,100 +3803,100 @@ fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void {
38093803 }
38103804}
38113805
3812fn airArg(self: *Self, inst: Air.Inst.Index) !void {
3813 var arg_index = self.arg_index;
3806fn airArg(func: *Func, inst: Air.Inst.Index) !void {
3807 var arg_index = func.arg_index;
38143808
38153809 // we skip over args that have no bits
3816 while (self.args[arg_index] == .none) arg_index += 1;
3817 self.arg_index = arg_index + 1;
3810 while (func.args[arg_index] == .none) arg_index += 1;
3811 func.arg_index = arg_index + 1;
38183812
3819 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
3820 const src_mcv = self.args[arg_index];
3821 const arg_ty = self.typeOfIndex(inst);
3813 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
3814 const src_mcv = func.args[arg_index];
3815 const arg_ty = func.typeOfIndex(inst);
38223816
3823 const dst_mcv = try self.allocRegOrMem(inst, false);
3817 const dst_mcv = try func.allocRegOrMem(inst, false);
38243818
38253819 log.debug("airArg {} -> {}", .{ src_mcv, dst_mcv });
38263820
3827 try self.genCopy(arg_ty, dst_mcv, src_mcv);
3821 try func.genCopy(arg_ty, dst_mcv, src_mcv);
38283822
3829 try self.genArgDbgInfo(inst, src_mcv);
3823 try func.genArgDbgInfo(inst, src_mcv);
38303824 break :result dst_mcv;
38313825 };
38323826
3833 return self.finishAir(inst, result, .{ .none, .none, .none });
3827 return func.finishAir(inst, result, .{ .none, .none, .none });
38343828}
38353829
3836fn airTrap(self: *Self) !void {
3837 _ = try self.addInst(.{
3830fn airTrap(func: *Func) !void {
3831 _ = try func.addInst(.{
38383832 .tag = .unimp,
38393833 .ops = .none,
38403834 .data = undefined,
38413835 });
3842 return self.finishAirBookkeeping();
3836 return func.finishAirBookkeeping();
38433837}
38443838
3845fn airBreakpoint(self: *Self) !void {
3846 _ = try self.addInst(.{
3839fn airBreakpoint(func: *Func) !void {
3840 _ = try func.addInst(.{
38473841 .tag = .ebreak,
38483842 .ops = .none,
38493843 .data = undefined,
38503844 });
3851 return self.finishAirBookkeeping();
3845 return func.finishAirBookkeeping();
38523846}
38533847
3854fn airRetAddr(self: *Self, inst: Air.Inst.Index) !void {
3855 const dst_mcv = try self.allocRegOrMem(inst, true);
3856 try self.genCopy(Type.usize, dst_mcv, .{ .load_frame = .{ .index = .ret_addr } });
3857 return self.finishAir(inst, dst_mcv, .{ .none, .none, .none });
3848fn airRetAddr(func: *Func, inst: Air.Inst.Index) !void {
3849 const dst_mcv = try func.allocRegOrMem(inst, true);
3850 try func.genCopy(Type.usize, dst_mcv, .{ .load_frame = .{ .index = .ret_addr } });
3851 return func.finishAir(inst, dst_mcv, .{ .none, .none, .none });
38583852}
38593853
3860fn airFrameAddress(self: *Self, inst: Air.Inst.Index) !void {
3861 const dst_mcv = try self.allocRegOrMem(inst, true);
3862 try self.genCopy(Type.usize, dst_mcv, .{ .lea_frame = .{ .index = .base_ptr } });
3863 return self.finishAir(inst, dst_mcv, .{ .none, .none, .none });
3854fn airFrameAddress(func: *Func, inst: Air.Inst.Index) !void {
3855 const dst_mcv = try func.allocRegOrMem(inst, true);
3856 try func.genCopy(Type.usize, dst_mcv, .{ .lea_frame = .{ .index = .base_ptr } });
3857 return func.finishAir(inst, dst_mcv, .{ .none, .none, .none });
38643858}
38653859
3866fn airFence(self: *Self) !void {
3867 return self.fail("TODO implement fence() for {}", .{self.target.cpu.arch});
3868 //return self.finishAirBookkeeping();
3860fn airFence(func: *Func) !void {
3861 return func.fail("TODO implement fence() for {}", .{func.target.cpu.arch});
3862 //return func.finishAirBookkeeping();
38693863}
38703864
3871fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !void {
3872 if (modifier == .always_tail) return self.fail("TODO implement tail calls for riscv64", .{});
3873 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
3865fn airCall(func: *Func, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !void {
3866 if (modifier == .always_tail) return func.fail("TODO implement tail calls for riscv64", .{});
3867 const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
38743868 const callee = pl_op.operand;
3875 const extra = self.air.extraData(Air.Call, pl_op.payload);
3876 const arg_refs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]);
3869 const extra = func.air.extraData(Air.Call, pl_op.payload);
3870 const arg_refs: []const Air.Inst.Ref = @ptrCast(func.air.extra[extra.end..][0..extra.data.args_len]);
38773871
38783872 const expected_num_args = 8;
38793873 const ExpectedContents = extern struct {
38803874 vals: [expected_num_args][@sizeOf(MCValue)]u8 align(@alignOf(MCValue)),
38813875 };
38823876 var stack align(@max(@alignOf(ExpectedContents), @alignOf(std.heap.StackFallbackAllocator(0)))) =
3883 std.heap.stackFallback(@sizeOf(ExpectedContents), self.gpa);
3877 std.heap.stackFallback(@sizeOf(ExpectedContents), func.gpa);
38843878 const allocator = stack.get();
38853879
38863880 const arg_tys = try allocator.alloc(Type, arg_refs.len);
38873881 defer allocator.free(arg_tys);
3888 for (arg_tys, arg_refs) |*arg_ty, arg_ref| arg_ty.* = self.typeOf(arg_ref);
3882 for (arg_tys, arg_refs) |*arg_ty, arg_ref| arg_ty.* = func.typeOf(arg_ref);
38893883
38903884 const arg_vals = try allocator.alloc(MCValue, arg_refs.len);
38913885 defer allocator.free(arg_vals);
38923886 for (arg_vals, arg_refs) |*arg_val, arg_ref| arg_val.* = .{ .air_ref = arg_ref };
38933887
3894 const call_ret = try self.genCall(.{ .air = callee }, arg_tys, arg_vals);
3888 const call_ret = try func.genCall(.{ .air = callee }, arg_tys, arg_vals);
38953889
3896 var bt = self.liveness.iterateBigTomb(inst);
3897 try self.feed(&bt, pl_op.operand);
3898 for (arg_refs) |arg_ref| try self.feed(&bt, arg_ref);
3890 var bt = func.liveness.iterateBigTomb(inst);
3891 try func.feed(&bt, pl_op.operand);
3892 for (arg_refs) |arg_ref| try func.feed(&bt, arg_ref);
38993893
3900 const result = if (self.liveness.isUnused(inst)) .unreach else call_ret;
3901 return self.finishAirResult(inst, result);
3894 const result = if (func.liveness.isUnused(inst)) .unreach else call_ret;
3895 return func.finishAirResult(inst, result);
39023896}
39033897
39043898fn genCall(
3905 self: *Self,
3899 func: *Func,
39063900 info: union(enum) {
39073901 air: Air.Inst.Ref,
39083902 lib: struct {
......@@ -3915,11 +3909,11 @@ fn genCall(
39153909 arg_tys: []const Type,
39163910 args: []const MCValue,
39173911) !MCValue {
3918 const zcu = self.bin_file.comp.module.?;
3912 const zcu = func.bin_file.comp.module.?;
39193913
39203914 const fn_ty = switch (info) {
39213915 .air => |callee| fn_info: {
3922 const callee_ty = self.typeOf(callee);
3916 const callee_ty = func.typeOf(callee);
39233917 break :fn_info switch (callee_ty.zigTypeTag(zcu)) {
39243918 .Fn => callee_ty,
39253919 .Pointer => callee_ty.childType(zcu),
......@@ -3935,14 +3929,14 @@ fn genCall(
39353929
39363930 const fn_info = zcu.typeToFunc(fn_ty).?;
39373931
3938 const allocator = self.gpa;
3932 const allocator = func.gpa;
39393933
39403934 const var_args = try allocator.alloc(Type, args.len - fn_info.param_types.len);
39413935 defer allocator.free(var_args);
39423936 for (var_args, arg_tys[fn_info.param_types.len..]) |*var_arg, arg_ty| var_arg.* = arg_ty;
39433937
3944 var call_info = try self.resolveCallingConventionValues(fn_info, var_args);
3945 defer call_info.deinit(self);
3938 var call_info = try func.resolveCallingConventionValues(fn_info, var_args);
3939 defer call_info.deinit(func);
39463940
39473941 // We need a properly aligned and sized call frame to be able to call this function.
39483942 {
......@@ -3950,7 +3944,7 @@ fn genCall(
39503944 .size = call_info.stack_byte_count,
39513945 .alignment = call_info.stack_align,
39523946 });
3953 const frame_allocs_slice = self.frame_allocs.slice();
3947 const frame_allocs_slice = func.frame_allocs.slice();
39543948 const stack_frame_size =
39553949 &frame_allocs_slice.items(.abi_size)[@intFromEnum(FrameIndex.call_frame)];
39563950 stack_frame_size.* = @max(stack_frame_size.*, needed_call_frame.abi_size);
......@@ -3962,34 +3956,34 @@ fn genCall(
39623956 var reg_locks = std.ArrayList(?RegisterLock).init(allocator);
39633957 defer reg_locks.deinit();
39643958 try reg_locks.ensureTotalCapacity(8);
3965 defer for (reg_locks.items) |reg_lock| if (reg_lock) |lock| self.register_manager.unlockReg(lock);
3959 defer for (reg_locks.items) |reg_lock| if (reg_lock) |lock| func.register_manager.unlockReg(lock);
39663960
39673961 const frame_indices = try allocator.alloc(FrameIndex, args.len);
39683962 defer allocator.free(frame_indices);
39693963
39703964 switch (call_info.return_value.long) {
39713965 .none, .unreach => {},
3972 .indirect => |reg_off| try self.register_manager.getReg(reg_off.reg, null),
3966 .indirect => |reg_off| try func.register_manager.getReg(reg_off.reg, null),
39733967 else => unreachable,
39743968 }
39753969 for (call_info.args, args, arg_tys, frame_indices) |dst_arg, src_arg, arg_ty, *frame_index| {
39763970 switch (dst_arg) {
39773971 .none => {},
39783972 .register => |reg| {
3979 try self.register_manager.getReg(reg, null);
3980 try reg_locks.append(self.register_manager.lockReg(reg));
3973 try func.register_manager.getReg(reg, null);
3974 try reg_locks.append(func.register_manager.lockReg(reg));
39813975 },
39823976 .register_pair => |regs| {
3983 for (regs) |reg| try self.register_manager.getReg(reg, null);
3984 try reg_locks.appendSlice(&self.register_manager.lockRegs(2, regs));
3977 for (regs) |reg| try func.register_manager.getReg(reg, null);
3978 try reg_locks.appendSlice(&func.register_manager.lockRegs(2, regs));
39853979 },
39863980 .indirect => |reg_off| {
3987 frame_index.* = try self.allocFrameIndex(FrameAlloc.initType(arg_ty, zcu));
3988 try self.genSetMem(.{ .frame = frame_index.* }, 0, arg_ty, src_arg);
3989 try self.register_manager.getReg(reg_off.reg, null);
3990 try reg_locks.append(self.register_manager.lockReg(reg_off.reg));
3981 frame_index.* = try func.allocFrameIndex(FrameAlloc.initType(arg_ty, zcu));
3982 try func.genSetMem(.{ .frame = frame_index.* }, 0, arg_ty, src_arg);
3983 try func.register_manager.getReg(reg_off.reg, null);
3984 try reg_locks.append(func.register_manager.lockReg(reg_off.reg));
39913985 },
3992 else => return self.fail("TODO: genCall set arg {s}", .{@tagName(dst_arg)}),
3986 else => return func.fail("TODO: genCall set arg {s}", .{@tagName(dst_arg)}),
39933987 }
39943988 }
39953989
......@@ -3997,12 +3991,12 @@ fn genCall(
39973991 .none, .unreach => {},
39983992 .indirect => |reg_off| {
39993993 const ret_ty = Type.fromInterned(fn_info.return_type);
4000 const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(ret_ty, zcu));
4001 try self.genSetReg(Type.usize, reg_off.reg, .{
3994 const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(ret_ty, zcu));
3995 try func.genSetReg(Type.usize, reg_off.reg, .{
40023996 .lea_frame = .{ .index = frame_index, .off = -reg_off.off },
40033997 });
40043998 call_info.return_value.short = .{ .load_frame = .{ .index = frame_index } };
4005 try reg_locks.append(self.register_manager.lockReg(reg_off.reg));
3999 try reg_locks.append(func.register_manager.lockReg(reg_off.reg));
40064000 },
40074001 else => unreachable,
40084002 }
......@@ -4010,16 +4004,16 @@ fn genCall(
40104004 for (call_info.args, arg_tys, args, frame_indices) |dst_arg, arg_ty, src_arg, frame_index| {
40114005 switch (dst_arg) {
40124006 .none, .load_frame => {},
4013 .register_pair => try self.genCopy(arg_ty, dst_arg, src_arg),
4014 .register => |dst_reg| try self.genSetReg(
4007 .register_pair => try func.genCopy(arg_ty, dst_arg, src_arg),
4008 .register => |dst_reg| try func.genSetReg(
40154009 arg_ty,
40164010 dst_reg,
40174011 src_arg,
40184012 ),
4019 .indirect => |reg_off| try self.genSetReg(Type.usize, reg_off.reg, .{
4013 .indirect => |reg_off| try func.genSetReg(Type.usize, reg_off.reg, .{
40204014 .lea_frame = .{ .index = frame_index, .off = -reg_off.off },
40214015 }),
4022 else => return self.fail("TODO: genCall actual set {s}", .{@tagName(dst_arg)}),
4016 else => return func.fail("TODO: genCall actual set {s}", .{@tagName(dst_arg)}),
40234017 }
40244018 }
40254019
......@@ -4027,7 +4021,7 @@ fn genCall(
40274021 // on linking.
40284022 switch (info) {
40294023 .air => |callee| {
4030 if (try self.air.value(callee, zcu)) |func_value| {
4024 if (try func.air.value(callee, zcu)) |func_value| {
40314025 const func_key = zcu.intern_pool.indexToKey(func_value.ip_index);
40324026 switch (switch (func_key) {
40334027 else => func_key,
......@@ -4036,19 +4030,19 @@ fn genCall(
40364030 else => func_key,
40374031 } else func_key,
40384032 }) {
4039 .func => |func| {
4040 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
4041 const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func.owner_decl);
4033 .func => |func_val| {
4034 if (func.bin_file.cast(link.File.Elf)) |elf_file| {
4035 const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func_val.owner_decl);
40424036 const sym = elf_file.symbol(sym_index);
40434037
4044 if (self.mod.pic) {
4045 return self.fail("TODO: genCall pic", .{});
4038 if (func.mod.pic) {
4039 return func.fail("TODO: genCall pic", .{});
40464040 } else {
40474041 _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file);
40484042 const got_addr = sym.zigGotAddress(elf_file);
4049 try self.genSetReg(Type.usize, .ra, .{ .memory = @intCast(got_addr) });
4043 try func.genSetReg(Type.usize, .ra, .{ .memory = @intCast(got_addr) });
40504044
4051 _ = try self.addInst(.{
4045 _ = try func.addInst(.{
40524046 .tag = .jalr,
40534047 .ops = .rri,
40544048 .data = .{ .i_type = .{
......@@ -4064,10 +4058,10 @@ fn genCall(
40644058 const owner_decl = zcu.declPtr(extern_func.decl);
40654059 const lib_name = extern_func.lib_name.toSlice(&zcu.intern_pool);
40664060 const decl_name = owner_decl.name.toSlice(&zcu.intern_pool);
4067 const atom_index = try self.symbolIndex();
4061 const atom_index = try func.symbolIndex();
40684062
4069 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
4070 _ = try self.addInst(.{
4063 if (func.bin_file.cast(link.File.Elf)) |elf_file| {
4064 _ = try func.addInst(.{
40714065 .tag = .pseudo,
40724066 .ops = .pseudo_extern_fn_reloc,
40734067 .data = .{ .reloc = .{
......@@ -4077,15 +4071,15 @@ fn genCall(
40774071 });
40784072 } else unreachable; // not a valid riscv64 format
40794073 },
4080 else => return self.fail("TODO implement calling bitcasted functions", .{}),
4074 else => return func.fail("TODO implement calling bitcasted functions", .{}),
40814075 }
40824076 } else {
4083 assert(self.typeOf(callee).zigTypeTag(zcu) == .Pointer);
4084 const addr_reg, const addr_lock = try self.allocReg(.int);
4085 defer self.register_manager.unlockReg(addr_lock);
4086 try self.genSetReg(Type.usize, addr_reg, .{ .air_ref = callee });
4077 assert(func.typeOf(callee).zigTypeTag(zcu) == .Pointer);
4078 const addr_reg, const addr_lock = try func.allocReg(.int);
4079 defer func.register_manager.unlockReg(addr_lock);
4080 try func.genSetReg(Type.usize, addr_reg, .{ .air_ref = callee });
40874081
4088 _ = try self.addInst(.{
4082 _ = try func.addInst(.{
40894083 .tag = .jalr,
40904084 .ops = .rri,
40914085 .data = .{ .i_type = .{
......@@ -4096,15 +4090,15 @@ fn genCall(
40964090 });
40974091 }
40984092 },
4099 .lib => return self.fail("TODO: lib func calls", .{}),
4093 .lib => return func.fail("TODO: lib func calls", .{}),
41004094 }
41014095
41024096 return call_info.return_value.short;
41034097}
41044098
4105fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void {
4106 const zcu = self.bin_file.comp.module.?;
4107 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4099fn airRet(func: *Func, inst: Air.Inst.Index, safety: bool) !void {
4100 const zcu = func.bin_file.comp.module.?;
4101 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
41084102
41094103 if (safety) {
41104104 // safe
......@@ -4112,19 +4106,19 @@ fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void {
41124106 // not safe
41134107 }
41144108
4115 const ret_ty = self.fn_type.fnReturnType(zcu);
4116 switch (self.ret_mcv.short) {
4109 const ret_ty = func.fn_type.fnReturnType(zcu);
4110 switch (func.ret_mcv.short) {
41174111 .none => {},
41184112 .register,
41194113 .register_pair,
4120 => try self.genCopy(ret_ty, self.ret_mcv.short, .{ .air_ref = un_op }),
4114 => try func.genCopy(ret_ty, func.ret_mcv.short, .{ .air_ref = un_op }),
41214115 .indirect => |reg_off| {
4122 try self.register_manager.getReg(reg_off.reg, null);
4123 const lock = self.register_manager.lockRegAssumeUnused(reg_off.reg);
4124 defer self.register_manager.unlockReg(lock);
4116 try func.register_manager.getReg(reg_off.reg, null);
4117 const lock = func.register_manager.lockRegAssumeUnused(reg_off.reg);
4118 defer func.register_manager.unlockReg(lock);
41254119
4126 try self.genSetReg(Type.usize, reg_off.reg, self.ret_mcv.long);
4127 try self.genSetMem(
4120 try func.genSetReg(Type.usize, reg_off.reg, func.ret_mcv.long);
4121 try func.genSetMem(
41284122 .{ .reg = reg_off.reg },
41294123 reg_off.off,
41304124 ret_ty,
......@@ -4134,52 +4128,52 @@ fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void {
41344128 else => unreachable,
41354129 }
41364130
4137 self.ret_mcv.liveOut(self, inst);
4138 try self.finishAir(inst, .unreach, .{ un_op, .none, .none });
4131 func.ret_mcv.liveOut(func, inst);
4132 try func.finishAir(inst, .unreach, .{ un_op, .none, .none });
41394133
41404134 // Just add space for an instruction, reloced this later
4141 const index = try self.addInst(.{
4135 const index = try func.addInst(.{
41424136 .tag = .pseudo,
41434137 .ops = .pseudo_j,
41444138 .data = .{ .inst = undefined },
41454139 });
41464140
4147 try self.exitlude_jump_relocs.append(self.gpa, index);
4141 try func.exitlude_jump_relocs.append(func.gpa, index);
41484142}
41494143
4150fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void {
4151 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4152 const ptr = try self.resolveInst(un_op);
4144fn airRetLoad(func: *Func, inst: Air.Inst.Index) !void {
4145 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4146 const ptr = try func.resolveInst(un_op);
41534147
4154 const ptr_ty = self.typeOf(un_op);
4155 switch (self.ret_mcv.short) {
4148 const ptr_ty = func.typeOf(un_op);
4149 switch (func.ret_mcv.short) {
41564150 .none => {},
4157 .register, .register_pair => try self.load(self.ret_mcv.short, ptr, ptr_ty),
4158 .indirect => |reg_off| try self.genSetReg(ptr_ty, reg_off.reg, ptr),
4151 .register, .register_pair => try func.load(func.ret_mcv.short, ptr, ptr_ty),
4152 .indirect => |reg_off| try func.genSetReg(ptr_ty, reg_off.reg, ptr),
41594153 else => unreachable,
41604154 }
4161 self.ret_mcv.liveOut(self, inst);
4162 try self.finishAir(inst, .unreach, .{ un_op, .none, .none });
4155 func.ret_mcv.liveOut(func, inst);
4156 try func.finishAir(inst, .unreach, .{ un_op, .none, .none });
41634157
41644158 // Just add space for an instruction, reloced this later
4165 const index = try self.addInst(.{
4159 const index = try func.addInst(.{
41664160 .tag = .pseudo,
41674161 .ops = .pseudo_j,
41684162 .data = .{ .inst = undefined },
41694163 });
41704164
4171 try self.exitlude_jump_relocs.append(self.gpa, index);
4165 try func.exitlude_jump_relocs.append(func.gpa, index);
41724166}
41734167
4174fn airCmp(self: *Self, inst: Air.Inst.Index) !void {
4175 const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];
4176 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
4177 const zcu = self.bin_file.comp.module.?;
4168fn airCmp(func: *Func, inst: Air.Inst.Index) !void {
4169 const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)];
4170 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
4171 const zcu = func.bin_file.comp.module.?;
41784172
4179 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4180 const lhs = try self.resolveInst(bin_op.lhs);
4181 const rhs = try self.resolveInst(bin_op.rhs);
4182 const lhs_ty = self.typeOf(bin_op.lhs);
4173 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
4174 const lhs = try func.resolveInst(bin_op.lhs);
4175 const rhs = try func.resolveInst(bin_op.rhs);
4176 const lhs_ty = func.typeOf(bin_op.lhs);
41834177
41844178 switch (lhs_ty.zigTypeTag(zcu)) {
41854179 .Int,
......@@ -4202,7 +4196,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index) !void {
42024196 } else if (lhs_ty.isPtrLikeOptional(zcu)) {
42034197 break :blk Type.usize;
42044198 } else {
4205 return self.fail("TODO riscv cmp non-pointer optionals", .{});
4199 return func.fail("TODO riscv cmp non-pointer optionals", .{});
42064200 }
42074201 },
42084202 else => unreachable,
......@@ -4210,43 +4204,43 @@ fn airCmp(self: *Self, inst: Air.Inst.Index) !void {
42104204
42114205 const int_info = int_ty.intInfo(zcu);
42124206 if (int_info.bits <= 64) {
4213 break :result try self.binOp(tag, lhs, int_ty, rhs, int_ty);
4207 break :result try func.binOp(tag, lhs, int_ty, rhs, int_ty);
42144208 } else {
4215 return self.fail("TODO riscv cmp for ints > 64 bits", .{});
4209 return func.fail("TODO riscv cmp for ints > 64 bits", .{});
42164210 }
42174211 },
42184212 .Float => {
4219 const float_bits = lhs_ty.floatBits(self.target.*);
4220 const float_reg_size: u32 = if (self.hasFeature(.d)) 64 else 32;
4213 const float_bits = lhs_ty.floatBits(func.target.*);
4214 const float_reg_size: u32 = if (func.hasFeature(.d)) 64 else 32;
42214215 if (float_bits > float_reg_size) {
4222 return self.fail("TODO: airCmp float > 64/32 bits", .{});
4216 return func.fail("TODO: airCmp float > 64/32 bits", .{});
42234217 }
4224 break :result try self.binOpFloat(tag, lhs, lhs_ty, rhs, lhs_ty);
4218 break :result try func.binOpFloat(tag, lhs, lhs_ty, rhs, lhs_ty);
42254219 },
42264220 else => unreachable,
42274221 }
42284222 };
42294223
4230 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
4224 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
42314225}
42324226
4233fn airCmpVector(self: *Self, inst: Air.Inst.Index) !void {
4227fn airCmpVector(func: *Func, inst: Air.Inst.Index) !void {
42344228 _ = inst;
4235 return self.fail("TODO implement airCmpVector for {}", .{self.target.cpu.arch});
4229 return func.fail("TODO implement airCmpVector for {}", .{func.target.cpu.arch});
42364230}
42374231
4238fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) !void {
4239 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4240 const operand = try self.resolveInst(un_op);
4232fn airCmpLtErrorsLen(func: *Func, inst: Air.Inst.Index) !void {
4233 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4234 const operand = try func.resolveInst(un_op);
42414235 _ = operand;
4242 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airCmpLtErrorsLen for {}", .{self.target.cpu.arch});
4243 return self.finishAir(inst, result, .{ un_op, .none, .none });
4236 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airCmpLtErrorsLen for {}", .{func.target.cpu.arch});
4237 return func.finishAir(inst, result, .{ un_op, .none, .none });
42444238}
42454239
4246fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
4247 const dbg_stmt = self.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
4240fn airDbgStmt(func: *Func, inst: Air.Inst.Index) !void {
4241 const dbg_stmt = func.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
42484242
4249 _ = try self.addInst(.{
4243 _ = try func.addInst(.{
42504244 .tag = .pseudo,
42514245 .ops = .pseudo_dbg_line_column,
42524246 .data = .{ .pseudo_dbg_line_column = .{
......@@ -4255,44 +4249,44 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
42554249 } },
42564250 });
42574251
4258 return self.finishAirBookkeeping();
4252 return func.finishAirBookkeeping();
42594253}
42604254
4261fn airDbgInlineBlock(self: *Self, inst: Air.Inst.Index) !void {
4262 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
4263 const extra = self.air.extraData(Air.DbgInlineBlock, ty_pl.payload);
4264 try self.lowerBlock(inst, @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len]));
4255fn airDbgInlineBlock(func: *Func, inst: Air.Inst.Index) !void {
4256 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
4257 const extra = func.air.extraData(Air.DbgInlineBlock, ty_pl.payload);
4258 try func.lowerBlock(inst, @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len]));
42654259}
42664260
4267fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
4268 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
4261fn airDbgVar(func: *Func, inst: Air.Inst.Index) !void {
4262 const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
42694263 const operand = pl_op.operand;
4270 const ty = self.typeOf(operand);
4271 const mcv = try self.resolveInst(operand);
4264 const ty = func.typeOf(operand);
4265 const mcv = try func.resolveInst(operand);
42724266
4273 const name = self.air.nullTerminatedString(pl_op.payload);
4267 const name = func.air.nullTerminatedString(pl_op.payload);
42744268
4275 const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];
4276 try self.genVarDbgInfo(tag, ty, mcv, name);
4269 const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)];
4270 try func.genVarDbgInfo(tag, ty, mcv, name);
42774271
4278 return self.finishAir(inst, .unreach, .{ operand, .none, .none });
4272 return func.finishAir(inst, .unreach, .{ operand, .none, .none });
42794273}
42804274
42814275fn genVarDbgInfo(
4282 self: Self,
4276 func: Func,
42834277 tag: Air.Inst.Tag,
42844278 ty: Type,
42854279 mcv: MCValue,
42864280 name: [:0]const u8,
42874281) !void {
4288 const zcu = self.bin_file.comp.module.?;
4282 const zcu = func.bin_file.comp.module.?;
42894283 const is_ptr = switch (tag) {
42904284 .dbg_var_ptr => true,
42914285 .dbg_var_val => false,
42924286 else => unreachable,
42934287 };
42944288
4295 switch (self.debug_output) {
4289 switch (func.debug_output) {
42964290 .dwarf => |dw| {
42974291 const loc: link.File.Dwarf.DeclState.DbgInfoLoc = switch (mcv) {
42984292 .register => |reg| .{ .register = reg.dwarfLocOp() },
......@@ -4309,47 +4303,47 @@ fn genVarDbgInfo(
43094303 break :blk .nop;
43104304 },
43114305 };
4312 try dw.genVarDbgInfo(name, ty, zcu.funcOwnerDeclIndex(self.func_index), is_ptr, loc);
4306 try dw.genVarDbgInfo(name, ty, zcu.funcOwnerDeclIndex(func.func_index), is_ptr, loc);
43134307 },
43144308 .plan9 => {},
43154309 .none => {},
43164310 }
43174311}
43184312
4319fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
4320 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
4321 const cond = try self.resolveInst(pl_op.operand);
4322 const cond_ty = self.typeOf(pl_op.operand);
4323 const extra = self.air.extraData(Air.CondBr, pl_op.payload);
4324 const then_body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end..][0..extra.data.then_body_len]);
4325 const else_body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]);
4326 const liveness_cond_br = self.liveness.getCondBr(inst);
4313fn airCondBr(func: *Func, inst: Air.Inst.Index) !void {
4314 const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
4315 const cond = try func.resolveInst(pl_op.operand);
4316 const cond_ty = func.typeOf(pl_op.operand);
4317 const extra = func.air.extraData(Air.CondBr, pl_op.payload);
4318 const then_body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end..][0..extra.data.then_body_len]);
4319 const else_body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]);
4320 const liveness_cond_br = func.liveness.getCondBr(inst);
43274321
43284322 // If the condition dies here in this condbr instruction, process
43294323 // that death now instead of later as this has an effect on
43304324 // whether it needs to be spilled in the branches
4331 if (self.liveness.operandDies(inst, 0)) {
4332 if (pl_op.operand.toIndex()) |op_inst| try self.processDeath(op_inst);
4325 if (func.liveness.operandDies(inst, 0)) {
4326 if (pl_op.operand.toIndex()) |op_inst| try func.processDeath(op_inst);
43334327 }
43344328
4335 self.scope_generation += 1;
4336 const state = try self.saveState();
4337 const reloc = try self.condBr(cond_ty, cond);
4329 func.scope_generation += 1;
4330 const state = try func.saveState();
4331 const reloc = try func.condBr(cond_ty, cond);
43384332
4339 for (liveness_cond_br.then_deaths) |death| try self.processDeath(death);
4340 try self.genBody(then_body);
4341 try self.restoreState(state, &.{}, .{
4333 for (liveness_cond_br.then_deaths) |death| try func.processDeath(death);
4334 try func.genBody(then_body);
4335 try func.restoreState(state, &.{}, .{
43424336 .emit_instructions = false,
43434337 .update_tracking = true,
43444338 .resurrect = true,
43454339 .close_scope = true,
43464340 });
43474341
4348 self.performReloc(reloc);
4342 func.performReloc(reloc);
43494343
4350 for (liveness_cond_br.else_deaths) |death| try self.processDeath(death);
4351 try self.genBody(else_body);
4352 try self.restoreState(state, &.{}, .{
4344 for (liveness_cond_br.else_deaths) |death| try func.processDeath(death);
4345 try func.genBody(else_body);
4346 try func.restoreState(state, &.{}, .{
43534347 .emit_instructions = false,
43544348 .update_tracking = true,
43554349 .resurrect = true,
......@@ -4357,13 +4351,13 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
43574351 });
43584352
43594353 // We already took care of pl_op.operand earlier, so there's nothing left to do.
4360 self.finishAirBookkeeping();
4354 func.finishAirBookkeeping();
43614355}
43624356
4363fn condBr(self: *Self, cond_ty: Type, condition: MCValue) !Mir.Inst.Index {
4364 const cond_reg = try self.copyToTmpRegister(cond_ty, condition);
4357fn condBr(func: *Func, cond_ty: Type, condition: MCValue) !Mir.Inst.Index {
4358 const cond_reg = try func.copyToTmpRegister(cond_ty, condition);
43654359
4366 return try self.addInst(.{
4360 return try func.addInst(.{
43674361 .tag = .beq,
43684362 .ops = .rr_inst,
43694363 .data = .{
......@@ -4376,111 +4370,111 @@ fn condBr(self: *Self, cond_ty: Type, condition: MCValue) !Mir.Inst.Index {
43764370 });
43774371}
43784372
4379fn airIsNull(self: *Self, inst: Air.Inst.Index) !void {
4380 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4381 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4382 const operand = try self.resolveInst(un_op);
4383 break :result try self.isNull(operand);
4373fn airIsNull(func: *Func, inst: Air.Inst.Index) !void {
4374 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4375 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
4376 const operand = try func.resolveInst(un_op);
4377 break :result try func.isNull(operand);
43844378 };
4385 return self.finishAir(inst, result, .{ un_op, .none, .none });
4379 return func.finishAir(inst, result, .{ un_op, .none, .none });
43864380}
43874381
4388fn airIsNullPtr(self: *Self, inst: Air.Inst.Index) !void {
4389 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4390 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4391 const operand_ptr = try self.resolveInst(un_op);
4382fn airIsNullPtr(func: *Func, inst: Air.Inst.Index) !void {
4383 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4384 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
4385 const operand_ptr = try func.resolveInst(un_op);
43924386 const operand: MCValue = blk: {
4393 if (self.reuseOperand(inst, un_op, 0, operand_ptr)) {
4387 if (func.reuseOperand(inst, un_op, 0, operand_ptr)) {
43944388 // The MCValue that holds the pointer can be re-used as the value.
43954389 break :blk operand_ptr;
43964390 } else {
4397 break :blk try self.allocRegOrMem(inst, true);
4391 break :blk try func.allocRegOrMem(inst, true);
43984392 }
43994393 };
4400 try self.load(operand, operand_ptr, self.typeOf(un_op));
4401 break :result try self.isNull(operand);
4394 try func.load(operand, operand_ptr, func.typeOf(un_op));
4395 break :result try func.isNull(operand);
44024396 };
4403 return self.finishAir(inst, result, .{ un_op, .none, .none });
4397 return func.finishAir(inst, result, .{ un_op, .none, .none });
44044398}
44054399
4406fn isNull(self: *Self, operand: MCValue) !MCValue {
4400fn isNull(func: *Func, operand: MCValue) !MCValue {
44074401 _ = operand;
44084402 // Here you can specialize this instruction if it makes sense to, otherwise the default
44094403 // will call isNonNull and invert the result.
4410 return self.fail("TODO call isNonNull and invert the result", .{});
4404 return func.fail("TODO call isNonNull and invert the result", .{});
44114405}
44124406
4413fn airIsNonNull(self: *Self, inst: Air.Inst.Index) !void {
4414 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4415 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4416 const operand = try self.resolveInst(un_op);
4417 break :result try self.isNonNull(operand);
4407fn airIsNonNull(func: *Func, inst: Air.Inst.Index) !void {
4408 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4409 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
4410 const operand = try func.resolveInst(un_op);
4411 break :result try func.isNonNull(operand);
44184412 };
4419 return self.finishAir(inst, result, .{ un_op, .none, .none });
4413 return func.finishAir(inst, result, .{ un_op, .none, .none });
44204414}
44214415
4422fn isNonNull(self: *Self, operand: MCValue) !MCValue {
4416fn isNonNull(func: *Func, operand: MCValue) !MCValue {
44234417 _ = operand;
44244418 // Here you can specialize this instruction if it makes sense to, otherwise the default
44254419 // will call isNull and invert the result.
4426 return self.fail("TODO call isNull and invert the result", .{});
4420 return func.fail("TODO call isNull and invert the result", .{});
44274421}
44284422
4429fn airIsNonNullPtr(self: *Self, inst: Air.Inst.Index) !void {
4430 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4431 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4432 const operand_ptr = try self.resolveInst(un_op);
4423fn airIsNonNullPtr(func: *Func, inst: Air.Inst.Index) !void {
4424 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4425 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
4426 const operand_ptr = try func.resolveInst(un_op);
44334427 const operand: MCValue = blk: {
4434 if (self.reuseOperand(inst, un_op, 0, operand_ptr)) {
4428 if (func.reuseOperand(inst, un_op, 0, operand_ptr)) {
44354429 // The MCValue that holds the pointer can be re-used as the value.
44364430 break :blk operand_ptr;
44374431 } else {
4438 break :blk try self.allocRegOrMem(inst, true);
4432 break :blk try func.allocRegOrMem(inst, true);
44394433 }
44404434 };
4441 try self.load(operand, operand_ptr, self.typeOf(un_op));
4442 break :result try self.isNonNull(operand);
4435 try func.load(operand, operand_ptr, func.typeOf(un_op));
4436 break :result try func.isNonNull(operand);
44434437 };
4444 return self.finishAir(inst, result, .{ un_op, .none, .none });
4438 return func.finishAir(inst, result, .{ un_op, .none, .none });
44454439}
44464440
4447fn airIsErr(self: *Self, inst: Air.Inst.Index) !void {
4448 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4449 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4450 const operand = try self.resolveInst(un_op);
4451 const operand_ty = self.typeOf(un_op);
4452 break :result try self.isErr(inst, operand_ty, operand);
4441fn airIsErr(func: *Func, inst: Air.Inst.Index) !void {
4442 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4443 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
4444 const operand = try func.resolveInst(un_op);
4445 const operand_ty = func.typeOf(un_op);
4446 break :result try func.isErr(inst, operand_ty, operand);
44534447 };
4454 return self.finishAir(inst, result, .{ un_op, .none, .none });
4448 return func.finishAir(inst, result, .{ un_op, .none, .none });
44554449}
44564450
4457fn airIsErrPtr(self: *Self, inst: Air.Inst.Index) !void {
4458 const zcu = self.bin_file.comp.module.?;
4459 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4460 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4461 const operand_ptr = try self.resolveInst(un_op);
4451fn airIsErrPtr(func: *Func, inst: Air.Inst.Index) !void {
4452 const zcu = func.bin_file.comp.module.?;
4453 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4454 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
4455 const operand_ptr = try func.resolveInst(un_op);
44624456 const operand: MCValue = blk: {
4463 if (self.reuseOperand(inst, un_op, 0, operand_ptr)) {
4457 if (func.reuseOperand(inst, un_op, 0, operand_ptr)) {
44644458 // The MCValue that holds the pointer can be re-used as the value.
44654459 break :blk operand_ptr;
44664460 } else {
4467 break :blk try self.allocRegOrMem(inst, true);
4461 break :blk try func.allocRegOrMem(inst, true);
44684462 }
44694463 };
4470 try self.load(operand, operand_ptr, self.typeOf(un_op));
4471 const operand_ptr_ty = self.typeOf(un_op);
4464 try func.load(operand, operand_ptr, func.typeOf(un_op));
4465 const operand_ptr_ty = func.typeOf(un_op);
44724466 const operand_ty = operand_ptr_ty.childType(zcu);
44734467
4474 break :result try self.isErr(inst, operand_ty, operand);
4468 break :result try func.isErr(inst, operand_ty, operand);
44754469 };
4476 return self.finishAir(inst, result, .{ un_op, .none, .none });
4470 return func.finishAir(inst, result, .{ un_op, .none, .none });
44774471}
44784472
44794473/// Generates a compare instruction which will indicate if `eu_mcv` is an error.
44804474///
44814475/// Result is in the return register.
4482fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue {
4483 const zcu = self.bin_file.comp.module.?;
4476fn isErr(func: *Func, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue {
4477 const zcu = func.bin_file.comp.module.?;
44844478 const err_ty = eu_ty.errorUnionSet(zcu);
44854479 if (err_ty.errorSetIsEmpty(zcu)) return MCValue{ .immediate = 0 }; // always false
44864480
......@@ -4490,17 +4484,17 @@ fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue)
44904484
44914485 switch (eu_mcv) {
44924486 .register => |reg| {
4493 const eu_lock = self.register_manager.lockReg(reg);
4494 defer if (eu_lock) |lock| self.register_manager.unlockReg(lock);
4487 const eu_lock = func.register_manager.lockReg(reg);
4488 defer if (eu_lock) |lock| func.register_manager.unlockReg(lock);
44954489
4496 const return_reg = try self.copyToTmpRegister(eu_ty, eu_mcv);
4497 const return_lock = self.register_manager.lockRegAssumeUnused(return_reg);
4498 defer self.register_manager.unlockReg(return_lock);
4490 const return_reg = try func.copyToTmpRegister(eu_ty, eu_mcv);
4491 const return_lock = func.register_manager.lockRegAssumeUnused(return_reg);
4492 defer func.register_manager.unlockReg(return_lock);
44994493
45004494 var return_mcv: MCValue = .{ .register = return_reg };
45014495
45024496 if (err_off > 0) {
4503 return_mcv = try self.binOp(
4497 return_mcv = try func.binOp(
45044498 .shr,
45054499 return_mcv,
45064500 eu_ty,
......@@ -4509,7 +4503,7 @@ fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue)
45094503 );
45104504 }
45114505
4512 return try self.binOp(
4506 return try func.binOp(
45134507 .cmp_neq,
45144508 return_mcv,
45154509 Type.u16,
......@@ -4518,7 +4512,7 @@ fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue)
45184512 );
45194513 },
45204514 .load_frame => |frame_addr| {
4521 return self.binOp(
4515 return func.binOp(
45224516 .cmp_neq,
45234517 .{ .load_frame = .{
45244518 .index = frame_addr.index,
......@@ -4529,25 +4523,25 @@ fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue)
45294523 Type.anyerror,
45304524 );
45314525 },
4532 else => return self.fail("TODO implement isErr for {}", .{eu_mcv}),
4526 else => return func.fail("TODO implement isErr for {}", .{eu_mcv}),
45334527 }
45344528}
45354529
4536fn airIsNonErr(self: *Self, inst: Air.Inst.Index) !void {
4537 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4538 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4539 const operand = try self.resolveInst(un_op);
4540 const ty = self.typeOf(un_op);
4541 break :result try self.isNonErr(inst, ty, operand);
4530fn airIsNonErr(func: *Func, inst: Air.Inst.Index) !void {
4531 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4532 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
4533 const operand = try func.resolveInst(un_op);
4534 const ty = func.typeOf(un_op);
4535 break :result try func.isNonErr(inst, ty, operand);
45424536 };
4543 return self.finishAir(inst, result, .{ un_op, .none, .none });
4537 return func.finishAir(inst, result, .{ un_op, .none, .none });
45444538}
45454539
4546fn isNonErr(self: *Self, inst: Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue {
4547 const is_err_res = try self.isErr(inst, eu_ty, eu_mcv);
4540fn isNonErr(func: *Func, inst: Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue {
4541 const is_err_res = try func.isErr(inst, eu_ty, eu_mcv);
45484542 switch (is_err_res) {
45494543 .register => |reg| {
4550 _ = try self.addInst(.{
4544 _ = try func.addInst(.{
45514545 .tag = .pseudo,
45524546 .ops = .pseudo_not,
45534547 .data = .{
......@@ -4568,53 +4562,53 @@ fn isNonErr(self: *Self, inst: Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MC
45684562 }
45694563}
45704564
4571fn airIsNonErrPtr(self: *Self, inst: Air.Inst.Index) !void {
4572 const zcu = self.bin_file.comp.module.?;
4573 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4574 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4575 const operand_ptr = try self.resolveInst(un_op);
4565fn airIsNonErrPtr(func: *Func, inst: Air.Inst.Index) !void {
4566 const zcu = func.bin_file.comp.module.?;
4567 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4568 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
4569 const operand_ptr = try func.resolveInst(un_op);
45764570 const operand: MCValue = blk: {
4577 if (self.reuseOperand(inst, un_op, 0, operand_ptr)) {
4571 if (func.reuseOperand(inst, un_op, 0, operand_ptr)) {
45784572 // The MCValue that holds the pointer can be re-used as the value.
45794573 break :blk operand_ptr;
45804574 } else {
4581 break :blk try self.allocRegOrMem(inst, true);
4575 break :blk try func.allocRegOrMem(inst, true);
45824576 }
45834577 };
4584 const operand_ptr_ty = self.typeOf(un_op);
4578 const operand_ptr_ty = func.typeOf(un_op);
45854579 const operand_ty = operand_ptr_ty.childType(zcu);
45864580
4587 try self.load(operand, operand_ptr, self.typeOf(un_op));
4588 break :result try self.isNonErr(inst, operand_ty, operand);
4581 try func.load(operand, operand_ptr, func.typeOf(un_op));
4582 break :result try func.isNonErr(inst, operand_ty, operand);
45894583 };
4590 return self.finishAir(inst, result, .{ un_op, .none, .none });
4584 return func.finishAir(inst, result, .{ un_op, .none, .none });
45914585}
45924586
4593fn airLoop(self: *Self, inst: Air.Inst.Index) !void {
4587fn airLoop(func: *Func, inst: Air.Inst.Index) !void {
45944588 // A loop is a setup to be able to jump back to the beginning.
4595 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
4596 const loop = self.air.extraData(Air.Block, ty_pl.payload);
4597 const body: []const Air.Inst.Index = @ptrCast(self.air.extra[loop.end..][0..loop.data.body_len]);
4589 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
4590 const loop = func.air.extraData(Air.Block, ty_pl.payload);
4591 const body: []const Air.Inst.Index = @ptrCast(func.air.extra[loop.end..][0..loop.data.body_len]);
45984592
4599 self.scope_generation += 1;
4600 const state = try self.saveState();
4593 func.scope_generation += 1;
4594 const state = try func.saveState();
46014595
4602 const jmp_target: Mir.Inst.Index = @intCast(self.mir_instructions.len);
4603 try self.genBody(body);
4604 try self.restoreState(state, &.{}, .{
4596 const jmp_target: Mir.Inst.Index = @intCast(func.mir_instructions.len);
4597 try func.genBody(body);
4598 try func.restoreState(state, &.{}, .{
46054599 .emit_instructions = true,
46064600 .update_tracking = false,
46074601 .resurrect = false,
46084602 .close_scope = true,
46094603 });
4610 _ = try self.jump(jmp_target);
4604 _ = try func.jump(jmp_target);
46114605
4612 self.finishAirBookkeeping();
4606 func.finishAirBookkeeping();
46134607}
46144608
4615/// Send control flow to the `index` of `self.code`.
4616fn jump(self: *Self, index: Mir.Inst.Index) !Mir.Inst.Index {
4617 return self.addInst(.{
4609/// Send control flow to the `index` of `func.code`.
4610fn jump(func: *Func, index: Mir.Inst.Index) !Mir.Inst.Index {
4611 return func.addInst(.{
46184612 .tag = .pseudo,
46194613 .ops = .pseudo_j,
46204614 .data = .{
......@@ -4623,79 +4617,79 @@ fn jump(self: *Self, index: Mir.Inst.Index) !Mir.Inst.Index {
46234617 });
46244618}
46254619
4626fn airBlock(self: *Self, inst: Air.Inst.Index) !void {
4627 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
4628 const extra = self.air.extraData(Air.Block, ty_pl.payload);
4629 try self.lowerBlock(inst, @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len]));
4620fn airBlock(func: *Func, inst: Air.Inst.Index) !void {
4621 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
4622 const extra = func.air.extraData(Air.Block, ty_pl.payload);
4623 try func.lowerBlock(inst, @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len]));
46304624}
46314625
4632fn lowerBlock(self: *Self, inst: Air.Inst.Index, body: []const Air.Inst.Index) !void {
4626fn lowerBlock(func: *Func, inst: Air.Inst.Index, body: []const Air.Inst.Index) !void {
46334627 // A block is a setup to be able to jump to the end.
4634 const inst_tracking_i = self.inst_tracking.count();
4635 self.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(.unreach));
4628 const inst_tracking_i = func.inst_tracking.count();
4629 func.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(.unreach));
46364630
4637 self.scope_generation += 1;
4638 try self.blocks.putNoClobber(self.gpa, inst, .{ .state = self.initRetroactiveState() });
4639 const liveness = self.liveness.getBlock(inst);
4631 func.scope_generation += 1;
4632 try func.blocks.putNoClobber(func.gpa, inst, .{ .state = func.initRetroactiveState() });
4633 const liveness = func.liveness.getBlock(inst);
46404634
46414635 // TODO emit debug info lexical block
4642 try self.genBody(body);
4636 try func.genBody(body);
46434637
4644 var block_data = self.blocks.fetchRemove(inst).?;
4645 defer block_data.value.deinit(self.gpa);
4638 var block_data = func.blocks.fetchRemove(inst).?;
4639 defer block_data.value.deinit(func.gpa);
46464640 if (block_data.value.relocs.items.len > 0) {
4647 try self.restoreState(block_data.value.state, liveness.deaths, .{
4641 try func.restoreState(block_data.value.state, liveness.deaths, .{
46484642 .emit_instructions = false,
46494643 .update_tracking = true,
46504644 .resurrect = true,
46514645 .close_scope = true,
46524646 });
4653 for (block_data.value.relocs.items) |reloc| self.performReloc(reloc);
4647 for (block_data.value.relocs.items) |reloc| func.performReloc(reloc);
46544648 }
46554649
4656 if (std.debug.runtime_safety) assert(self.inst_tracking.getIndex(inst).? == inst_tracking_i);
4657 const tracking = &self.inst_tracking.values()[inst_tracking_i];
4658 if (self.liveness.isUnused(inst)) try tracking.die(self, inst);
4659 self.getValueIfFree(tracking.short, inst);
4660 self.finishAirBookkeeping();
4650 if (std.debug.runtime_safety) assert(func.inst_tracking.getIndex(inst).? == inst_tracking_i);
4651 const tracking = &func.inst_tracking.values()[inst_tracking_i];
4652 if (func.liveness.isUnused(inst)) try tracking.die(func, inst);
4653 func.getValueIfFree(tracking.short, inst);
4654 func.finishAirBookkeeping();
46614655}
46624656
4663fn airSwitchBr(self: *Self, inst: Air.Inst.Index) !void {
4664 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
4665 const condition = try self.resolveInst(pl_op.operand);
4666 const condition_ty = self.typeOf(pl_op.operand);
4667 const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload);
4657fn airSwitchBr(func: *Func, inst: Air.Inst.Index) !void {
4658 const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
4659 const condition = try func.resolveInst(pl_op.operand);
4660 const condition_ty = func.typeOf(pl_op.operand);
4661 const switch_br = func.air.extraData(Air.SwitchBr, pl_op.payload);
46684662 var extra_index: usize = switch_br.end;
46694663 var case_i: u32 = 0;
4670 const liveness = try self.liveness.getSwitchBr(self.gpa, inst, switch_br.data.cases_len + 1);
4671 defer self.gpa.free(liveness.deaths);
4664 const liveness = try func.liveness.getSwitchBr(func.gpa, inst, switch_br.data.cases_len + 1);
4665 defer func.gpa.free(liveness.deaths);
46724666
46734667 // If the condition dies here in this switch instruction, process
46744668 // that death now instead of later as this has an effect on
46754669 // whether it needs to be spilled in the branches
4676 if (self.liveness.operandDies(inst, 0)) {
4677 if (pl_op.operand.toIndex()) |op_inst| try self.processDeath(op_inst);
4670 if (func.liveness.operandDies(inst, 0)) {
4671 if (pl_op.operand.toIndex()) |op_inst| try func.processDeath(op_inst);
46784672 }
46794673
4680 self.scope_generation += 1;
4681 const state = try self.saveState();
4674 func.scope_generation += 1;
4675 const state = try func.saveState();
46824676
46834677 while (case_i < switch_br.data.cases_len) : (case_i += 1) {
4684 const case = self.air.extraData(Air.SwitchBr.Case, extra_index);
4678 const case = func.air.extraData(Air.SwitchBr.Case, extra_index);
46854679 const items: []const Air.Inst.Ref =
4686 @ptrCast(self.air.extra[case.end..][0..case.data.items_len]);
4680 @ptrCast(func.air.extra[case.end..][0..case.data.items_len]);
46874681 const case_body: []const Air.Inst.Index =
4688 @ptrCast(self.air.extra[case.end + items.len ..][0..case.data.body_len]);
4682 @ptrCast(func.air.extra[case.end + items.len ..][0..case.data.body_len]);
46894683 extra_index = case.end + items.len + case_body.len;
46904684
4691 var relocs = try self.gpa.alloc(Mir.Inst.Index, items.len);
4692 defer self.gpa.free(relocs);
4685 var relocs = try func.gpa.alloc(Mir.Inst.Index, items.len);
4686 defer func.gpa.free(relocs);
46934687
46944688 for (items, relocs, 0..) |item, *reloc, i| {
46954689 // switch branches must be comptime-known, so this is stored in an immediate
4696 const item_mcv = try self.resolveInst(item);
4690 const item_mcv = try func.resolveInst(item);
46974691
4698 const cmp_mcv: MCValue = try self.binOp(
4692 const cmp_mcv: MCValue = try func.binOp(
46994693 .cmp_neq,
47004694 condition,
47014695 condition_ty,
......@@ -4703,10 +4697,10 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) !void {
47034697 condition_ty,
47044698 );
47054699
4706 const cmp_reg = try self.copyToTmpRegister(Type.bool, cmp_mcv);
4700 const cmp_reg = try func.copyToTmpRegister(Type.bool, cmp_mcv);
47074701
47084702 if (!(i < relocs.len - 1)) {
4709 _ = try self.addInst(.{
4703 _ = try func.addInst(.{
47104704 .tag = .pseudo,
47114705 .ops = .pseudo_not,
47124706 .data = .{ .rr = .{
......@@ -4716,32 +4710,32 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) !void {
47164710 });
47174711 }
47184712
4719 reloc.* = try self.condBr(condition_ty, .{ .register = cmp_reg });
4713 reloc.* = try func.condBr(condition_ty, .{ .register = cmp_reg });
47204714 }
47214715
4722 for (liveness.deaths[case_i]) |operand| try self.processDeath(operand);
4716 for (liveness.deaths[case_i]) |operand| try func.processDeath(operand);
47234717
4724 for (relocs[0 .. relocs.len - 1]) |reloc| self.performReloc(reloc);
4725 try self.genBody(case_body);
4726 try self.restoreState(state, &.{}, .{
4718 for (relocs[0 .. relocs.len - 1]) |reloc| func.performReloc(reloc);
4719 try func.genBody(case_body);
4720 try func.restoreState(state, &.{}, .{
47274721 .emit_instructions = false,
47284722 .update_tracking = true,
47294723 .resurrect = true,
47304724 .close_scope = true,
47314725 });
47324726
4733 self.performReloc(relocs[relocs.len - 1]);
4727 func.performReloc(relocs[relocs.len - 1]);
47344728 }
47354729
47364730 if (switch_br.data.else_body_len > 0) {
47374731 const else_body: []const Air.Inst.Index =
4738 @ptrCast(self.air.extra[extra_index..][0..switch_br.data.else_body_len]);
4732 @ptrCast(func.air.extra[extra_index..][0..switch_br.data.else_body_len]);
47394733
47404734 const else_deaths = liveness.deaths.len - 1;
4741 for (liveness.deaths[else_deaths]) |operand| try self.processDeath(operand);
4735 for (liveness.deaths[else_deaths]) |operand| try func.processDeath(operand);
47424736
4743 try self.genBody(else_body);
4744 try self.restoreState(state, &.{}, .{
4737 try func.genBody(else_body);
4738 try func.restoreState(state, &.{}, .{
47454739 .emit_instructions = false,
47464740 .update_tracking = true,
47474741 .resurrect = true,
......@@ -4750,71 +4744,71 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) !void {
47504744 }
47514745
47524746 // We already took care of pl_op.operand earlier, so there's nothing left to do
4753 self.finishAirBookkeeping();
4747 func.finishAirBookkeeping();
47544748}
47554749
4756fn performReloc(self: *Self, inst: Mir.Inst.Index) void {
4757 const tag = self.mir_instructions.items(.tag)[inst];
4758 const ops = self.mir_instructions.items(.ops)[inst];
4759 const target: Mir.Inst.Index = @intCast(self.mir_instructions.len);
4750fn performReloc(func: *Func, inst: Mir.Inst.Index) void {
4751 const tag = func.mir_instructions.items(.tag)[inst];
4752 const ops = func.mir_instructions.items(.ops)[inst];
4753 const target: Mir.Inst.Index = @intCast(func.mir_instructions.len);
47604754
47614755 switch (tag) {
47624756 .bne,
47634757 .beq,
4764 => self.mir_instructions.items(.data)[inst].b_type.inst = target,
4765 .jal => self.mir_instructions.items(.data)[inst].j_type.inst = target,
4758 => func.mir_instructions.items(.data)[inst].b_type.inst = target,
4759 .jal => func.mir_instructions.items(.data)[inst].j_type.inst = target,
47664760 .pseudo => switch (ops) {
4767 .pseudo_j => self.mir_instructions.items(.data)[inst].inst = target,
4761 .pseudo_j => func.mir_instructions.items(.data)[inst].inst = target,
47684762 else => std.debug.panic("TODO: performReloc {s}", .{@tagName(ops)}),
47694763 },
47704764 else => std.debug.panic("TODO: performReloc {s}", .{@tagName(tag)}),
47714765 }
47724766}
47734767
4774fn airBr(self: *Self, inst: Air.Inst.Index) !void {
4775 const mod = self.bin_file.comp.module.?;
4776 const br = self.air.instructions.items(.data)[@intFromEnum(inst)].br;
4768fn airBr(func: *Func, inst: Air.Inst.Index) !void {
4769 const mod = func.bin_file.comp.module.?;
4770 const br = func.air.instructions.items(.data)[@intFromEnum(inst)].br;
47774771
4778 const block_ty = self.typeOfIndex(br.block_inst);
4772 const block_ty = func.typeOfIndex(br.block_inst);
47794773 const block_unused =
4780 !block_ty.hasRuntimeBitsIgnoreComptime(mod) or self.liveness.isUnused(br.block_inst);
4781 const block_tracking = self.inst_tracking.getPtr(br.block_inst).?;
4782 const block_data = self.blocks.getPtr(br.block_inst).?;
4774 !block_ty.hasRuntimeBitsIgnoreComptime(mod) or func.liveness.isUnused(br.block_inst);
4775 const block_tracking = func.inst_tracking.getPtr(br.block_inst).?;
4776 const block_data = func.blocks.getPtr(br.block_inst).?;
47834777 const first_br = block_data.relocs.items.len == 0;
47844778 const block_result = result: {
47854779 if (block_unused) break :result .none;
47864780
4787 if (!first_br) try self.getValue(block_tracking.short, null);
4788 const src_mcv = try self.resolveInst(br.operand);
4781 if (!first_br) try func.getValue(block_tracking.short, null);
4782 const src_mcv = try func.resolveInst(br.operand);
47894783
4790 if (self.reuseOperandAdvanced(inst, br.operand, 0, src_mcv, br.block_inst)) {
4784 if (func.reuseOperandAdvanced(inst, br.operand, 0, src_mcv, br.block_inst)) {
47914785 if (first_br) break :result src_mcv;
47924786
4793 try self.getValue(block_tracking.short, br.block_inst);
4787 try func.getValue(block_tracking.short, br.block_inst);
47944788 // .long = .none to avoid merging operand and block result stack frames.
47954789 const current_tracking: InstTracking = .{ .long = .none, .short = src_mcv };
4796 try current_tracking.materializeUnsafe(self, br.block_inst, block_tracking.*);
4797 for (current_tracking.getRegs()) |src_reg| self.register_manager.freeReg(src_reg);
4790 try current_tracking.materializeUnsafe(func, br.block_inst, block_tracking.*);
4791 for (current_tracking.getRegs()) |src_reg| func.register_manager.freeReg(src_reg);
47984792 break :result block_tracking.short;
47994793 }
48004794
4801 const dst_mcv = if (first_br) try self.allocRegOrMem(br.block_inst, true) else dst: {
4802 try self.getValue(block_tracking.short, br.block_inst);
4795 const dst_mcv = if (first_br) try func.allocRegOrMem(br.block_inst, true) else dst: {
4796 try func.getValue(block_tracking.short, br.block_inst);
48034797 break :dst block_tracking.short;
48044798 };
4805 try self.genCopy(block_ty, dst_mcv, try self.resolveInst(br.operand));
4799 try func.genCopy(block_ty, dst_mcv, try func.resolveInst(br.operand));
48064800 break :result dst_mcv;
48074801 };
48084802
48094803 // Process operand death so that it is properly accounted for in the State below.
4810 if (self.liveness.operandDies(inst, 0)) {
4811 if (br.operand.toIndex()) |op_inst| try self.processDeath(op_inst);
4804 if (func.liveness.operandDies(inst, 0)) {
4805 if (br.operand.toIndex()) |op_inst| try func.processDeath(op_inst);
48124806 }
48134807
48144808 if (first_br) {
48154809 block_tracking.* = InstTracking.init(block_result);
4816 try self.saveRetroactiveState(&block_data.state);
4817 } else try self.restoreState(block_data.state, &.{}, .{
4810 try func.saveRetroactiveState(&block_data.state);
4811 } else try func.restoreState(block_data.state, &.{}, .{
48184812 .emit_instructions = true,
48194813 .update_tracking = false,
48204814 .resurrect = false,
......@@ -4823,35 +4817,35 @@ fn airBr(self: *Self, inst: Air.Inst.Index) !void {
48234817
48244818 // Emit a jump with a relocation. It will be patched up after the block ends.
48254819 // Leave the jump offset undefined
4826 const jmp_reloc = try self.jump(undefined);
4827 try block_data.relocs.append(self.gpa, jmp_reloc);
4820 const jmp_reloc = try func.jump(undefined);
4821 try block_data.relocs.append(func.gpa, jmp_reloc);
48284822
48294823 // Stop tracking block result without forgetting tracking info
4830 try self.freeValue(block_tracking.short);
4824 try func.freeValue(block_tracking.short);
48314825
4832 self.finishAirBookkeeping();
4826 func.finishAirBookkeeping();
48334827}
48344828
4835fn airBoolOp(self: *Self, inst: Air.Inst.Index) !void {
4836 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
4837 const tag: Air.Inst.Tag = self.air.instructions.items(.tag)[@intFromEnum(inst)];
4829fn airBoolOp(func: *Func, inst: Air.Inst.Index) !void {
4830 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
4831 const tag: Air.Inst.Tag = func.air.instructions.items(.tag)[@intFromEnum(inst)];
48384832
4839 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: {
4840 const lhs = try self.resolveInst(bin_op.lhs);
4841 const rhs = try self.resolveInst(bin_op.rhs);
4833 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: {
4834 const lhs = try func.resolveInst(bin_op.lhs);
4835 const rhs = try func.resolveInst(bin_op.rhs);
48424836 const lhs_ty = Type.bool;
48434837 const rhs_ty = Type.bool;
48444838
4845 const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{});
4846 defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock);
4839 const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{});
4840 defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock);
48474841
4848 const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{});
4849 defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock);
4842 const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{});
4843 defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock);
48504844
4851 const result_reg, const result_lock = try self.allocReg(.int);
4852 defer self.register_manager.unlockReg(result_lock);
4845 const result_reg, const result_lock = try func.allocReg(.int);
4846 defer func.register_manager.unlockReg(result_lock);
48534847
4854 _ = try self.addInst(.{
4848 _ = try func.addInst(.{
48554849 .tag = if (tag == .bool_or) .@"or" else .@"and",
48564850 .ops = .rrr,
48574851 .data = .{ .r_type = .{
......@@ -4862,8 +4856,8 @@ fn airBoolOp(self: *Self, inst: Air.Inst.Index) !void {
48624856 });
48634857
48644858 // safety truncate
4865 if (self.wantSafety()) {
4866 _ = try self.addInst(.{
4859 if (func.wantSafety()) {
4860 _ = try func.addInst(.{
48674861 .tag = .andi,
48684862 .ops = .rri,
48694863 .data = .{ .i_type = .{
......@@ -4876,35 +4870,35 @@ fn airBoolOp(self: *Self, inst: Air.Inst.Index) !void {
48764870
48774871 break :result .{ .register = result_reg };
48784872 };
4879 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
4873 return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
48804874}
48814875
4882fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
4883 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
4884 const extra = self.air.extraData(Air.Asm, ty_pl.payload);
4876fn airAsm(func: *Func, inst: Air.Inst.Index) !void {
4877 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
4878 const extra = func.air.extraData(Air.Asm, ty_pl.payload);
48854879 const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0;
48864880 const clobbers_len: u31 = @truncate(extra.data.flags);
48874881 var extra_i: usize = extra.end;
48884882 const outputs: []const Air.Inst.Ref =
4889 @ptrCast(self.air.extra[extra_i..][0..extra.data.outputs_len]);
4883 @ptrCast(func.air.extra[extra_i..][0..extra.data.outputs_len]);
48904884 extra_i += outputs.len;
4891 const inputs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra_i..][0..extra.data.inputs_len]);
4885 const inputs: []const Air.Inst.Ref = @ptrCast(func.air.extra[extra_i..][0..extra.data.inputs_len]);
48924886 extra_i += inputs.len;
48934887
48944888 log.debug("airAsm input: {any}", .{inputs});
48954889
4896 const dead = !is_volatile and self.liveness.isUnused(inst);
4890 const dead = !is_volatile and func.liveness.isUnused(inst);
48974891 const result: MCValue = if (dead) .unreach else result: {
48984892 if (outputs.len > 1) {
4899 return self.fail("TODO implement codegen for asm with more than 1 output", .{});
4893 return func.fail("TODO implement codegen for asm with more than 1 output", .{});
49004894 }
49014895
49024896 const output_constraint: ?[]const u8 = for (outputs) |output| {
49034897 if (output != .none) {
4904 return self.fail("TODO implement codegen for non-expr asm", .{});
4898 return func.fail("TODO implement codegen for non-expr asm", .{});
49054899 }
4906 const extra_bytes = std.mem.sliceAsBytes(self.air.extra[extra_i..]);
4907 const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(self.air.extra[extra_i..]), 0);
4900 const extra_bytes = std.mem.sliceAsBytes(func.air.extra[extra_i..]);
4901 const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(func.air.extra[extra_i..]), 0);
49084902 const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0);
49094903 // This equation accounts for the fact that even if we have exactly 4 bytes
49104904 // for the string, we still use the next u32 for the null terminator.
......@@ -4914,7 +4908,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
49144908 } else null;
49154909
49164910 for (inputs) |input| {
4917 const input_bytes = std.mem.sliceAsBytes(self.air.extra[extra_i..]);
4911 const input_bytes = std.mem.sliceAsBytes(func.air.extra[extra_i..]);
49184912 const constraint = std.mem.sliceTo(input_bytes, 0);
49194913 const name = std.mem.sliceTo(input_bytes[constraint.len + 1 ..], 0);
49204914 // This equation accounts for the fact that even if we have exactly 4 bytes
......@@ -4922,21 +4916,21 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
49224916 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
49234917
49244918 if (constraint.len < 3 or constraint[0] != '{' or constraint[constraint.len - 1] != '}') {
4925 return self.fail("unrecognized asm input constraint: '{s}'", .{constraint});
4919 return func.fail("unrecognized asm input constraint: '{s}'", .{constraint});
49264920 }
49274921 const reg_name = constraint[1 .. constraint.len - 1];
49284922 const reg = parseRegName(reg_name) orelse
4929 return self.fail("unrecognized register: '{s}'", .{reg_name});
4923 return func.fail("unrecognized register: '{s}'", .{reg_name});
49304924
4931 const arg_mcv = try self.resolveInst(input);
4932 try self.register_manager.getReg(reg, null);
4933 try self.genSetReg(self.typeOf(input), reg, arg_mcv);
4925 const arg_mcv = try func.resolveInst(input);
4926 try func.register_manager.getReg(reg, null);
4927 try func.genSetReg(func.typeOf(input), reg, arg_mcv);
49344928 }
49354929
49364930 {
49374931 var clobber_i: u32 = 0;
49384932 while (clobber_i < clobbers_len) : (clobber_i += 1) {
4939 const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(self.air.extra[extra_i..]), 0);
4933 const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(func.air.extra[extra_i..]), 0);
49404934 // This equation accounts for the fact that even if we have exactly 4 bytes
49414935 // for the string, we still use the next u32 for the null terminator.
49424936 extra_i += clobber.len / 4 + 1;
......@@ -4944,31 +4938,31 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
49444938 if (std.mem.eql(u8, clobber, "") or std.mem.eql(u8, clobber, "memory")) {
49454939 // nothing really to do
49464940 } else {
4947 try self.register_manager.getReg(parseRegName(clobber) orelse
4948 return self.fail("invalid clobber: '{s}'", .{clobber}), null);
4941 try func.register_manager.getReg(parseRegName(clobber) orelse
4942 return func.fail("invalid clobber: '{s}'", .{clobber}), null);
49494943 }
49504944 }
49514945 }
49524946
4953 const asm_source = std.mem.sliceAsBytes(self.air.extra[extra_i..])[0..extra.data.source_len];
4947 const asm_source = std.mem.sliceAsBytes(func.air.extra[extra_i..])[0..extra.data.source_len];
49544948
49554949 if (std.meta.stringToEnum(Mir.Inst.Tag, asm_source)) |tag| {
4956 _ = try self.addInst(.{
4950 _ = try func.addInst(.{
49574951 .tag = tag,
49584952 .ops = .none,
49594953 .data = undefined,
49604954 });
49614955 } else {
4962 return self.fail("TODO: asm_source {s}", .{asm_source});
4956 return func.fail("TODO: asm_source {s}", .{asm_source});
49634957 }
49644958
49654959 if (output_constraint) |output| {
49664960 if (output.len < 4 or output[0] != '=' or output[1] != '{' or output[output.len - 1] != '}') {
4967 return self.fail("unrecognized asm output constraint: '{s}'", .{output});
4961 return func.fail("unrecognized asm output constraint: '{s}'", .{output});
49684962 }
49694963 const reg_name = output[2 .. output.len - 1];
49704964 const reg = parseRegName(reg_name) orelse
4971 return self.fail("unrecognized register: '{s}'", .{reg_name});
4965 return func.fail("unrecognized register: '{s}'", .{reg_name});
49724966 break :result .{ .register = reg };
49734967 } else {
49744968 break :result .{ .none = {} };
......@@ -4987,17 +4981,17 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
49874981 }
49884982 if (buf_index + inputs.len > buf.len) break :simple;
49894983 @memcpy(buf[buf_index..][0..inputs.len], inputs);
4990 return self.finishAir(inst, result, buf);
4984 return func.finishAir(inst, result, buf);
49914985 }
4992 var bt = self.liveness.iterateBigTomb(inst);
4993 for (outputs) |output| if (output != .none) try self.feed(&bt, output);
4994 for (inputs) |input| try self.feed(&bt, input);
4995 return self.finishAirResult(inst, result);
4986 var bt = func.liveness.iterateBigTomb(inst);
4987 for (outputs) |output| if (output != .none) try func.feed(&bt, output);
4988 for (inputs) |input| try func.feed(&bt, input);
4989 return func.finishAirResult(inst, result);
49964990}
49974991
49984992/// Sets the value of `dst_mcv` to the value of `src_mcv`.
4999fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void {
5000 const zcu = self.bin_file.comp.module.?;
4993fn genCopy(func: *Func, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void {
4994 const zcu = func.bin_file.comp.module.?;
50014995
50024996 // There isn't anything to store
50034997 if (dst_mcv == .none) return;
......@@ -5008,8 +5002,8 @@ fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void {
50085002 }
50095003
50105004 switch (dst_mcv) {
5011 .register => |reg| return self.genSetReg(ty, reg, src_mcv),
5012 .register_offset => |dst_reg_off| try self.genSetReg(ty, dst_reg_off.reg, switch (src_mcv) {
5005 .register => |reg| return func.genSetReg(ty, reg, src_mcv),
5006 .register_offset => |dst_reg_off| try func.genSetReg(ty, dst_reg_off.reg, switch (src_mcv) {
50135007 .none,
50145008 .unreach,
50155009 .dead,
......@@ -5020,49 +5014,49 @@ fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void {
50205014 .register_offset,
50215015 => src_mcv.offset(-dst_reg_off.off),
50225016 else => .{ .register_offset = .{
5023 .reg = try self.copyToTmpRegister(ty, src_mcv),
5017 .reg = try func.copyToTmpRegister(ty, src_mcv),
50245018 .off = -dst_reg_off.off,
50255019 } },
50265020 }),
5027 .indirect => |reg_off| try self.genSetMem(
5021 .indirect => |reg_off| try func.genSetMem(
50285022 .{ .reg = reg_off.reg },
50295023 reg_off.off,
50305024 ty,
50315025 src_mcv,
50325026 ),
5033 .load_frame => |frame_addr| try self.genSetMem(
5027 .load_frame => |frame_addr| try func.genSetMem(
50345028 .{ .frame = frame_addr.index },
50355029 frame_addr.off,
50365030 ty,
50375031 src_mcv,
50385032 ),
5039 .memory => return self.fail("TODO: genCopy memory", .{}),
5033 .memory => return func.fail("TODO: genCopy memory", .{}),
50405034 .register_pair => |dst_regs| {
50415035 const src_info: ?struct { addr_reg: Register, addr_lock: ?RegisterLock } = switch (src_mcv) {
50425036 .register_pair, .memory, .indirect, .load_frame => null,
50435037 .load_symbol => src: {
5044 const src_addr_reg, const src_addr_lock = try self.promoteReg(Type.usize, src_mcv.address(), .{});
5045 errdefer self.register_manager.unlockReg(src_addr_lock);
5038 const src_addr_reg, const src_addr_lock = try func.promoteReg(Type.usize, src_mcv.address(), .{});
5039 errdefer func.register_manager.unlockReg(src_addr_lock);
50465040
50475041 break :src .{ .addr_reg = src_addr_reg, .addr_lock = src_addr_lock };
50485042 },
5049 .air_ref => |src_ref| return self.genCopy(
5043 .air_ref => |src_ref| return func.genCopy(
50505044 ty,
50515045 dst_mcv,
5052 try self.resolveInst(src_ref),
5046 try func.resolveInst(src_ref),
50535047 ),
50545048 else => unreachable,
50555049 };
50565050
50575051 defer if (src_info) |info| {
50585052 if (info.addr_lock) |lock| {
5059 self.register_manager.unlockReg(lock);
5053 func.register_manager.unlockReg(lock);
50605054 }
50615055 };
50625056
50635057 var part_disp: i32 = 0;
5064 for (dst_regs, try self.splitType(ty), 0..) |dst_reg, dst_ty, part_i| {
5065 try self.genSetReg(dst_ty, dst_reg, switch (src_mcv) {
5058 for (dst_regs, try func.splitType(ty), 0..) |dst_reg, dst_ty, part_i| {
5059 try func.genSetReg(dst_ty, dst_reg, switch (src_mcv) {
50665060 .register_pair => |src_regs| .{ .register = src_regs[part_i] },
50675061 .memory, .indirect, .load_frame => src_mcv.address().offset(part_disp).deref(),
50685062 .load_symbol => .{ .indirect = .{
......@@ -5074,31 +5068,31 @@ fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void {
50745068 part_disp += @intCast(dst_ty.abiSize(zcu));
50755069 }
50765070 },
5077 else => return self.fail("TODO: genCopy to {s} from {s}", .{ @tagName(dst_mcv), @tagName(src_mcv) }),
5071 else => return func.fail("TODO: genCopy to {s} from {s}", .{ @tagName(dst_mcv), @tagName(src_mcv) }),
50785072 }
50795073}
50805074
50815075fn genInlineMemcpy(
5082 self: *Self,
5076 func: *Func,
50835077 dst_ptr: MCValue,
50845078 src_ptr: MCValue,
50855079 len: MCValue,
50865080) !void {
5087 const regs = try self.register_manager.allocRegs(4, .{null} ** 4, abi.Registers.Integer.temporary);
5088 const locks = self.register_manager.lockRegsAssumeUnused(4, regs);
5089 defer for (locks) |lock| self.register_manager.unlockReg(lock);
5081 const regs = try func.register_manager.allocRegs(4, .{null} ** 4, abi.Registers.Integer.temporary);
5082 const locks = func.register_manager.lockRegsAssumeUnused(4, regs);
5083 defer for (locks) |lock| func.register_manager.unlockReg(lock);
50905084
50915085 const count = regs[0];
50925086 const tmp = regs[1];
50935087 const src = regs[2];
50945088 const dst = regs[3];
50955089
5096 try self.genSetReg(Type.usize, count, len);
5097 try self.genSetReg(Type.usize, src, src_ptr);
5098 try self.genSetReg(Type.usize, dst, dst_ptr);
5090 try func.genSetReg(Type.usize, count, len);
5091 try func.genSetReg(Type.usize, src, src_ptr);
5092 try func.genSetReg(Type.usize, dst, dst_ptr);
50995093
51005094 // lb tmp, 0(src)
5101 const first_inst = try self.addInst(.{
5095 const first_inst = try func.addInst(.{
51025096 .tag = .lb,
51035097 .ops = .rri,
51045098 .data = .{
......@@ -5111,7 +5105,7 @@ fn genInlineMemcpy(
51115105 });
51125106
51135107 // sb tmp, 0(dst)
5114 _ = try self.addInst(.{
5108 _ = try func.addInst(.{
51155109 .tag = .sb,
51165110 .ops = .rri,
51175111 .data = .{
......@@ -5124,7 +5118,7 @@ fn genInlineMemcpy(
51245118 });
51255119
51265120 // dec count by 1
5127 _ = try self.addInst(.{
5121 _ = try func.addInst(.{
51285122 .tag = .addi,
51295123 .ops = .rri,
51305124 .data = .{
......@@ -5137,12 +5131,12 @@ fn genInlineMemcpy(
51375131 });
51385132
51395133 // branch if count is 0
5140 _ = try self.addInst(.{
5134 _ = try func.addInst(.{
51415135 .tag = .beq,
51425136 .ops = .rr_inst,
51435137 .data = .{
51445138 .b_type = .{
5145 .inst = @intCast(self.mir_instructions.len + 4), // points after the last inst
5139 .inst = @intCast(func.mir_instructions.len + 4), // points after the last inst
51465140 .rs1 = count,
51475141 .rs2 = .zero,
51485142 },
......@@ -5150,7 +5144,7 @@ fn genInlineMemcpy(
51505144 });
51515145
51525146 // increment the pointers
5153 _ = try self.addInst(.{
5147 _ = try func.addInst(.{
51545148 .tag = .addi,
51555149 .ops = .rri,
51565150 .data = .{
......@@ -5162,7 +5156,7 @@ fn genInlineMemcpy(
51625156 },
51635157 });
51645158
5165 _ = try self.addInst(.{
5159 _ = try func.addInst(.{
51665160 .tag = .addi,
51675161 .ops = .rri,
51685162 .data = .{
......@@ -5175,7 +5169,7 @@ fn genInlineMemcpy(
51755169 });
51765170
51775171 // jump back to start of loop
5178 _ = try self.addInst(.{
5172 _ = try func.addInst(.{
51795173 .tag = .pseudo,
51805174 .ops = .pseudo_j,
51815175 .data = .{ .inst = first_inst },
......@@ -5183,25 +5177,25 @@ fn genInlineMemcpy(
51835177}
51845178
51855179fn genInlineMemset(
5186 self: *Self,
5180 func: *Func,
51875181 dst_ptr: MCValue,
51885182 src_value: MCValue,
51895183 len: MCValue,
51905184) !void {
5191 const regs = try self.register_manager.allocRegs(3, .{null} ** 3, abi.Registers.Integer.temporary);
5192 const locks = self.register_manager.lockRegsAssumeUnused(3, regs);
5193 defer for (locks) |lock| self.register_manager.unlockReg(lock);
5185 const regs = try func.register_manager.allocRegs(3, .{null} ** 3, abi.Registers.Integer.temporary);
5186 const locks = func.register_manager.lockRegsAssumeUnused(3, regs);
5187 defer for (locks) |lock| func.register_manager.unlockReg(lock);
51945188
51955189 const count = regs[0];
51965190 const src = regs[1];
51975191 const dst = regs[2];
51985192
5199 try self.genSetReg(Type.usize, count, len);
5200 try self.genSetReg(Type.usize, src, src_value);
5201 try self.genSetReg(Type.usize, dst, dst_ptr);
5193 try func.genSetReg(Type.usize, count, len);
5194 try func.genSetReg(Type.usize, src, src_value);
5195 try func.genSetReg(Type.usize, dst, dst_ptr);
52025196
52035197 // sb src, 0(dst)
5204 const first_inst = try self.addInst(.{
5198 const first_inst = try func.addInst(.{
52055199 .tag = .sb,
52065200 .ops = .rri,
52075201 .data = .{
......@@ -5214,7 +5208,7 @@ fn genInlineMemset(
52145208 });
52155209
52165210 // dec count by 1
5217 _ = try self.addInst(.{
5211 _ = try func.addInst(.{
52185212 .tag = .addi,
52195213 .ops = .rri,
52205214 .data = .{
......@@ -5227,12 +5221,12 @@ fn genInlineMemset(
52275221 });
52285222
52295223 // branch if count is 0
5230 _ = try self.addInst(.{
5224 _ = try func.addInst(.{
52315225 .tag = .beq,
52325226 .ops = .rr_inst,
52335227 .data = .{
52345228 .b_type = .{
5235 .inst = @intCast(self.mir_instructions.len + 4), // points after the last inst
5229 .inst = @intCast(func.mir_instructions.len + 4), // points after the last inst
52365230 .rs1 = count,
52375231 .rs2 = .zero,
52385232 },
......@@ -5240,7 +5234,7 @@ fn genInlineMemset(
52405234 });
52415235
52425236 // increment the pointers
5243 _ = try self.addInst(.{
5237 _ = try func.addInst(.{
52445238 .tag = .addi,
52455239 .ops = .rri,
52465240 .data = .{
......@@ -5253,7 +5247,7 @@ fn genInlineMemset(
52535247 });
52545248
52555249 // jump back to start of loop
5256 _ = try self.addInst(.{
5250 _ = try func.addInst(.{
52575251 .tag = .pseudo,
52585252 .ops = .pseudo_j,
52595253 .data = .{
......@@ -5263,8 +5257,8 @@ fn genInlineMemset(
52635257}
52645258
52655259/// Sets the value of `src_mcv` into `reg`. Assumes you have a lock on it.
5266fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!void {
5267 const zcu = self.bin_file.comp.module.?;
5260fn genSetReg(func: *Func, ty: Type, reg: Register, src_mcv: MCValue) InnerError!void {
5261 const zcu = func.bin_file.comp.module.?;
52685262 const abi_size: u32 = @intCast(ty.abiSize(zcu));
52695263
52705264 if (abi_size > 8) return std.debug.panic("tried to set reg with size {}", .{abi_size});
......@@ -5275,17 +5269,17 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
52755269 .dead => unreachable,
52765270 .unreach, .none => return, // Nothing to do.
52775271 .undef => {
5278 if (!self.wantSafety())
5272 if (!func.wantSafety())
52795273 return; // The already existing value will do just fine.
52805274 // Write the debug undefined value.
5281 return self.genSetReg(ty, reg, .{ .immediate = 0xaaaaaaaaaaaaaaaa });
5275 return func.genSetReg(ty, reg, .{ .immediate = 0xaaaaaaaaaaaaaaaa });
52825276 },
52835277 .immediate => |unsigned_x| {
52845278 assert(dst_reg_class == .int);
52855279
52865280 const x: i64 = @bitCast(unsigned_x);
52875281 if (math.minInt(i12) <= x and x <= math.maxInt(i12)) {
5288 _ = try self.addInst(.{
5282 _ = try func.addInst(.{
52895283 .tag = .addi,
52905284 .ops = .rri,
52915285 .data = .{ .i_type = .{
......@@ -5299,7 +5293,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
52995293 const carry: i32 = if (lo12 < 0) 1 else 0;
53005294 const hi20: i20 = @truncate((x >> 12) +% carry);
53015295
5302 _ = try self.addInst(.{
5296 _ = try func.addInst(.{
53035297 .tag = .lui,
53045298 .ops = .ri,
53055299 .data = .{ .u_type = .{
......@@ -5307,7 +5301,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
53075301 .imm20 = Immediate.s(hi20),
53085302 } },
53095303 });
5310 _ = try self.addInst(.{
5304 _ = try func.addInst(.{
53115305 .tag = .addi,
53125306 .ops = .rri,
53135307 .data = .{ .i_type = .{
......@@ -5320,17 +5314,17 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
53205314 // TODO: use a more advanced myriad seq to do this without a reg.
53215315 // see: https://github.com/llvm/llvm-project/blob/081a66ffacfe85a37ff775addafcf3371e967328/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp#L224
53225316
5323 const temp, const temp_lock = try self.allocReg(.int);
5324 defer self.register_manager.unlockReg(temp_lock);
5317 const temp, const temp_lock = try func.allocReg(.int);
5318 defer func.register_manager.unlockReg(temp_lock);
53255319
53265320 const lo32: i32 = @truncate(x);
53275321 const carry: i32 = if (lo32 < 0) 1 else 0;
53285322 const hi32: i32 = @truncate((x >> 32) +% carry);
53295323
5330 try self.genSetReg(Type.i32, temp, .{ .immediate = @bitCast(@as(i64, lo32)) });
5331 try self.genSetReg(Type.i32, reg, .{ .immediate = @bitCast(@as(i64, hi32)) });
5324 try func.genSetReg(Type.i32, temp, .{ .immediate = @bitCast(@as(i64, lo32)) });
5325 try func.genSetReg(Type.i32, reg, .{ .immediate = @bitCast(@as(i64, hi32)) });
53325326
5333 _ = try self.addInst(.{
5327 _ = try func.addInst(.{
53345328 .tag = .slli,
53355329 .ops = .rri,
53365330 .data = .{ .i_type = .{
......@@ -5340,7 +5334,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
53405334 } },
53415335 });
53425336
5343 _ = try self.addInst(.{
5337 _ = try func.addInst(.{
53445338 .tag = .add,
53455339 .ops = .rrr,
53465340 .data = .{ .r_type = .{
......@@ -5360,11 +5354,11 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
53605354
53615355 if (src_reg_class == .float and dst_reg_class == .int) {
53625356 // to move from float -> int, we use FMV.X.W
5363 return self.fail("TODO: genSetReg float -> int", .{});
5357 return func.fail("TODO: genSetReg float -> int", .{});
53645358 }
53655359
53665360 // mv reg, src_reg
5367 _ = try self.addInst(.{
5361 _ = try func.addInst(.{
53685362 .tag = .pseudo,
53695363 .ops = .pseudo_mv,
53705364 .data = .{ .rr = .{
......@@ -5373,9 +5367,9 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
53735367 } },
53745368 });
53755369 },
5376 .register_pair => return self.fail("genSetReg should we allow reg -> reg_pair?", .{}),
5370 .register_pair => return func.fail("genSetReg should we allow reg -> reg_pair?", .{}),
53775371 .load_frame => |frame| {
5378 _ = try self.addInst(.{
5372 _ = try func.addInst(.{
53795373 .tag = .pseudo,
53805374 .ops = .pseudo_load_rm,
53815375 .data = .{ .rm = .{
......@@ -5384,7 +5378,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
53845378 .base = .{ .frame = frame.index },
53855379 .mod = .{
53865380 .rm = .{
5387 .size = self.memSize(ty),
5381 .size = func.memSize(ty),
53885382 .disp = frame.off,
53895383 },
53905384 },
......@@ -5393,9 +5387,9 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
53935387 });
53945388 },
53955389 .memory => |addr| {
5396 try self.genSetReg(ty, reg, .{ .immediate = addr });
5390 try func.genSetReg(ty, reg, .{ .immediate = addr });
53975391
5398 _ = try self.addInst(.{
5392 _ = try func.addInst(.{
53995393 .tag = .ld,
54005394 .ops = .rri,
54015395 .data = .{ .i_type = .{
......@@ -5406,7 +5400,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
54065400 });
54075401 },
54085402 .lea_frame, .register_offset => {
5409 _ = try self.addInst(.{
5403 _ = try func.addInst(.{
54105404 .tag = .pseudo,
54115405 .ops = .pseudo_lea_rm,
54125406 .data = .{ .rm = .{
......@@ -5416,7 +5410,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
54165410 .base = .{ .reg = reg_off.reg },
54175411 .mod = .{
54185412 .rm = .{
5419 .size = self.memSize(ty),
5413 .size = func.memSize(ty),
54205414 .disp = reg_off.off,
54215415 },
54225416 },
......@@ -5425,7 +5419,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
54255419 .base = .{ .frame = frame.index },
54265420 .mod = .{
54275421 .rm = .{
5428 .size = self.memSize(ty),
5422 .size = func.memSize(ty),
54295423 .disp = frame.off,
54305424 },
54315425 },
......@@ -5444,7 +5438,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
54445438 else
54455439 .lb,
54465440 2 => if (float_class)
5447 return self.fail("TODO: genSetReg indirect 16-bit float", .{})
5441 return func.fail("TODO: genSetReg indirect 16-bit float", .{})
54485442 else
54495443 .lh,
54505444 4 => if (float_class) .flw else .lw,
......@@ -5452,7 +5446,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
54525446 else => return std.debug.panic("TODO: genSetReg for size {d}", .{abi_size}),
54535447 };
54545448
5455 _ = try self.addInst(.{
5449 _ = try func.addInst(.{
54565450 .tag = load_tag,
54575451 .ops = .rri,
54585452 .data = .{ .i_type = .{
......@@ -5465,12 +5459,12 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
54655459 .lea_symbol => |sym_off| {
54665460 assert(sym_off.off == 0);
54675461
5468 const atom_index = try self.symbolIndex();
5462 const atom_index = try func.symbolIndex();
54695463
5470 _ = try self.addInst(.{
5464 _ = try func.addInst(.{
54715465 .tag = .pseudo,
54725466 .ops = .pseudo_load_symbol,
5473 .data = .{ .payload = try self.addExtra(Mir.LoadSymbolPayload{
5467 .data = .{ .payload = try func.addExtra(Mir.LoadSymbolPayload{
54745468 .register = reg.encodeId(),
54755469 .atom_index = atom_index,
54765470 .sym_index = sym_off.sym,
......@@ -5478,25 +5472,25 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!
54785472 });
54795473 },
54805474 .load_symbol => {
5481 const addr_reg, const addr_lock = try self.allocReg(.int);
5482 defer self.register_manager.unlockReg(addr_lock);
5475 const addr_reg, const addr_lock = try func.allocReg(.int);
5476 defer func.register_manager.unlockReg(addr_lock);
54835477
5484 try self.genSetReg(ty, addr_reg, src_mcv.address());
5485 try self.genSetReg(ty, reg, .{ .indirect = .{ .reg = addr_reg } });
5478 try func.genSetReg(ty, addr_reg, src_mcv.address());
5479 try func.genSetReg(ty, reg, .{ .indirect = .{ .reg = addr_reg } });
54865480 },
5487 .air_ref => |ref| try self.genSetReg(ty, reg, try self.resolveInst(ref)),
5488 else => return self.fail("TODO: genSetReg {s}", .{@tagName(src_mcv)}),
5481 .air_ref => |ref| try func.genSetReg(ty, reg, try func.resolveInst(ref)),
5482 else => return func.fail("TODO: genSetReg {s}", .{@tagName(src_mcv)}),
54895483 }
54905484}
54915485
54925486fn genSetMem(
5493 self: *Self,
5487 func: *Func,
54945488 base: Memory.Base,
54955489 disp: i32,
54965490 ty: Type,
54975491 src_mcv: MCValue,
54985492) InnerError!void {
5499 const mod = self.bin_file.comp.module.?;
5493 const mod = func.bin_file.comp.module.?;
55005494 const abi_size: u32 = @intCast(ty.abiSize(mod));
55015495 const dst_ptr_mcv: MCValue = switch (base) {
55025496 .reg => |base_reg| .{ .register_offset = .{ .reg = base_reg, .off = disp } },
......@@ -5509,7 +5503,7 @@ fn genSetMem(
55095503 .dead,
55105504 .reserved_frame,
55115505 => unreachable,
5512 .undef => try self.genInlineMemset(
5506 .undef => try func.genInlineMemset(
55135507 dst_ptr_mcv,
55145508 src_mcv,
55155509 .{ .immediate = abi_size },
......@@ -5525,13 +5519,13 @@ fn genSetMem(
55255519 0 => {},
55265520 1, 2, 4, 8 => {
55275521 // no matter what type, it should use an integer register
5528 const src_reg = try self.copyToTmpRegister(Type.usize, src_mcv);
5529 const src_lock = self.register_manager.lockRegAssumeUnused(src_reg);
5530 defer self.register_manager.unlockReg(src_lock);
5522 const src_reg = try func.copyToTmpRegister(Type.usize, src_mcv);
5523 const src_lock = func.register_manager.lockRegAssumeUnused(src_reg);
5524 defer func.register_manager.unlockReg(src_lock);
55315525
5532 try self.genSetMem(base, disp, ty, .{ .register = src_reg });
5526 try func.genSetMem(base, disp, ty, .{ .register = src_reg });
55335527 },
5534 else => try self.genInlineMemcpy(
5528 else => try func.genInlineMemcpy(
55355529 dst_ptr_mcv,
55365530 src_mcv.address(),
55375531 .{ .immediate = abi_size },
......@@ -5541,8 +5535,8 @@ fn genSetMem(
55415535 const mem_size = switch (base) {
55425536 .frame => |base_fi| mem_size: {
55435537 assert(disp >= 0);
5544 const frame_abi_size = self.frame_allocs.items(.abi_size)[@intFromEnum(base_fi)];
5545 const frame_spill_pad = self.frame_allocs.items(.spill_pad)[@intFromEnum(base_fi)];
5538 const frame_abi_size = func.frame_allocs.items(.abi_size)[@intFromEnum(base_fi)];
5539 const frame_spill_pad = func.frame_allocs.items(.spill_pad)[@intFromEnum(base_fi)];
55465540 assert(frame_abi_size - frame_spill_pad - disp >= abi_size);
55475541 break :mem_size if (frame_abi_size - frame_spill_pad - disp == abi_size)
55485542 frame_abi_size
......@@ -5554,12 +5548,12 @@ fn genSetMem(
55545548 const src_size = math.ceilPowerOfTwoAssert(u32, abi_size);
55555549 const src_align = Alignment.fromNonzeroByteUnits(math.ceilPowerOfTwoAssert(u32, src_size));
55565550 if (src_size > mem_size) {
5557 const frame_index = try self.allocFrameIndex(FrameAlloc.init(.{
5551 const frame_index = try func.allocFrameIndex(FrameAlloc.init(.{
55585552 .size = src_size,
55595553 .alignment = src_align,
55605554 }));
55615555 const frame_mcv: MCValue = .{ .load_frame = .{ .index = frame_index } };
5562 _ = try self.addInst(.{
5556 _ = try func.addInst(.{
55635557 .tag = .pseudo,
55645558 .ops = .pseudo_store_rm,
55655559 .data = .{ .rm = .{
......@@ -5572,9 +5566,9 @@ fn genSetMem(
55725566 },
55735567 } },
55745568 });
5575 try self.genSetMem(base, disp, ty, frame_mcv);
5576 try self.freeValue(frame_mcv);
5577 } else _ = try self.addInst(.{
5569 try func.genSetMem(base, disp, ty, frame_mcv);
5570 try func.freeValue(frame_mcv);
5571 } else _ = try func.addInst(.{
55785572 .tag = .pseudo,
55795573 .ops = .pseudo_store_rm,
55805574 .data = .{ .rm = .{
......@@ -5582,7 +5576,7 @@ fn genSetMem(
55825576 .m = .{
55835577 .base = base,
55845578 .mod = .{ .rm = .{
5585 .size = self.memSize(ty),
5579 .size = func.memSize(ty),
55865580 .disp = disp,
55875581 } },
55885582 },
......@@ -5591,54 +5585,54 @@ fn genSetMem(
55915585 },
55925586 .register_pair => |src_regs| {
55935587 var part_disp: i32 = disp;
5594 for (try self.splitType(ty), src_regs) |src_ty, src_reg| {
5595 try self.genSetMem(base, part_disp, src_ty, .{ .register = src_reg });
5588 for (try func.splitType(ty), src_regs) |src_ty, src_reg| {
5589 try func.genSetMem(base, part_disp, src_ty, .{ .register = src_reg });
55965590 part_disp += @intCast(src_ty.abiSize(mod));
55975591 }
55985592 },
55995593 .immediate => {
56005594 // TODO: remove this lock in favor of a copyToTmpRegister when we load 64 bit immediates with
56015595 // a register allocation.
5602 const reg, const reg_lock = try self.promoteReg(ty, src_mcv, .{});
5603 defer if (reg_lock) |lock| self.register_manager.unlockReg(lock);
5596 const reg, const reg_lock = try func.promoteReg(ty, src_mcv, .{});
5597 defer if (reg_lock) |lock| func.register_manager.unlockReg(lock);
56045598
5605 return self.genSetMem(base, disp, ty, .{ .register = reg });
5599 return func.genSetMem(base, disp, ty, .{ .register = reg });
56065600 },
5607 .air_ref => |src_ref| try self.genSetMem(base, disp, ty, try self.resolveInst(src_ref)),
5601 .air_ref => |src_ref| try func.genSetMem(base, disp, ty, try func.resolveInst(src_ref)),
56085602 }
56095603}
56105604
5611fn airIntFromPtr(self: *Self, inst: Air.Inst.Index) !void {
5612 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
5605fn airIntFromPtr(func: *Func, inst: Air.Inst.Index) !void {
5606 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
56135607 const result = result: {
5614 const src_mcv = try self.resolveInst(un_op);
5615 if (self.reuseOperand(inst, un_op, 0, src_mcv)) break :result src_mcv;
5608 const src_mcv = try func.resolveInst(un_op);
5609 if (func.reuseOperand(inst, un_op, 0, src_mcv)) break :result src_mcv;
56165610
5617 const dst_mcv = try self.allocRegOrMem(inst, true);
5618 const dst_ty = self.typeOfIndex(inst);
5619 try self.genCopy(dst_ty, dst_mcv, src_mcv);
5611 const dst_mcv = try func.allocRegOrMem(inst, true);
5612 const dst_ty = func.typeOfIndex(inst);
5613 try func.genCopy(dst_ty, dst_mcv, src_mcv);
56205614 break :result dst_mcv;
56215615 };
5622 return self.finishAir(inst, result, .{ un_op, .none, .none });
5616 return func.finishAir(inst, result, .{ un_op, .none, .none });
56235617}
56245618
5625fn airBitCast(self: *Self, inst: Air.Inst.Index) !void {
5626 const zcu = self.bin_file.comp.module.?;
5619fn airBitCast(func: *Func, inst: Air.Inst.Index) !void {
5620 const zcu = func.bin_file.comp.module.?;
56275621
5628 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5629 const result = if (self.liveness.isUnused(inst)) .unreach else result: {
5630 const src_mcv = try self.resolveInst(ty_op.operand);
5622 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5623 const result = if (func.liveness.isUnused(inst)) .unreach else result: {
5624 const src_mcv = try func.resolveInst(ty_op.operand);
56315625
5632 const dst_ty = self.typeOfIndex(inst);
5633 const src_ty = self.typeOf(ty_op.operand);
5626 const dst_ty = func.typeOfIndex(inst);
5627 const src_ty = func.typeOf(ty_op.operand);
56345628
5635 const src_lock = if (src_mcv.getReg()) |reg| self.register_manager.lockReg(reg) else null;
5636 defer if (src_lock) |lock| self.register_manager.unlockReg(lock);
5629 const src_lock = if (src_mcv.getReg()) |reg| func.register_manager.lockReg(reg) else null;
5630 defer if (src_lock) |lock| func.register_manager.unlockReg(lock);
56375631
56385632 const dst_mcv = if (dst_ty.abiSize(zcu) <= src_ty.abiSize(zcu) and
5639 self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: {
5640 const dst_mcv = try self.allocRegOrMem(inst, true);
5641 try self.genCopy(switch (math.order(dst_ty.abiSize(zcu), src_ty.abiSize(zcu))) {
5633 func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: {
5634 const dst_mcv = try func.allocRegOrMem(inst, true);
5635 try func.genCopy(switch (math.order(dst_ty.abiSize(zcu), src_ty.abiSize(zcu))) {
56425636 .lt => dst_ty,
56435637 .eq => if (!dst_mcv.isMemory() or src_mcv.isMemory()) dst_ty else src_ty,
56445638 .gt => src_ty,
......@@ -5653,83 +5647,83 @@ fn airBitCast(self: *Self, inst: Air.Inst.Index) !void {
56535647 const bit_size = dst_ty.bitSize(zcu);
56545648 if (abi_size * 8 <= bit_size) break :result dst_mcv;
56555649
5656 return self.fail("TODO: airBitCast {} to {}", .{ src_ty.fmt(zcu), dst_ty.fmt(zcu) });
5650 return func.fail("TODO: airBitCast {} to {}", .{ src_ty.fmt(zcu), dst_ty.fmt(zcu) });
56575651 };
5658 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
5652 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
56595653}
56605654
5661fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void {
5662 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5663 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airArrayToSlice for {}", .{
5664 self.target.cpu.arch,
5655fn airArrayToSlice(func: *Func, inst: Air.Inst.Index) !void {
5656 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5657 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airArrayToSlice for {}", .{
5658 func.target.cpu.arch,
56655659 });
5666 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
5660 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
56675661}
56685662
5669fn airFloatFromInt(self: *Self, inst: Air.Inst.Index) !void {
5670 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5671 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airFloatFromInt for {}", .{
5672 self.target.cpu.arch,
5663fn airFloatFromInt(func: *Func, inst: Air.Inst.Index) !void {
5664 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5665 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airFloatFromInt for {}", .{
5666 func.target.cpu.arch,
56735667 });
5674 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
5668 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
56755669}
56765670
5677fn airIntFromFloat(self: *Self, inst: Air.Inst.Index) !void {
5678 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5679 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airIntFromFloat for {}", .{
5680 self.target.cpu.arch,
5671fn airIntFromFloat(func: *Func, inst: Air.Inst.Index) !void {
5672 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5673 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airIntFromFloat for {}", .{
5674 func.target.cpu.arch,
56815675 });
5682 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
5676 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
56835677}
56845678
5685fn airCmpxchg(self: *Self, inst: Air.Inst.Index) !void {
5686 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
5687 const extra = self.air.extraData(Air.Block, ty_pl.payload);
5679fn airCmpxchg(func: *Func, inst: Air.Inst.Index) !void {
5680 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
5681 const extra = func.air.extraData(Air.Block, ty_pl.payload);
56885682 _ = extra;
5689 return self.fail("TODO implement airCmpxchg for {}", .{
5690 self.target.cpu.arch,
5683 return func.fail("TODO implement airCmpxchg for {}", .{
5684 func.target.cpu.arch,
56915685 });
5692 // return self.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value });
5686 // return func.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value });
56935687}
56945688
5695fn airAtomicRmw(self: *Self, inst: Air.Inst.Index) !void {
5689fn airAtomicRmw(func: *Func, inst: Air.Inst.Index) !void {
56965690 _ = inst;
5697 return self.fail("TODO implement airCmpxchg for {}", .{self.target.cpu.arch});
5691 return func.fail("TODO implement airCmpxchg for {}", .{func.target.cpu.arch});
56985692}
56995693
5700fn airAtomicLoad(self: *Self, inst: Air.Inst.Index) !void {
5694fn airAtomicLoad(func: *Func, inst: Air.Inst.Index) !void {
57015695 _ = inst;
5702 return self.fail("TODO implement airAtomicLoad for {}", .{self.target.cpu.arch});
5696 return func.fail("TODO implement airAtomicLoad for {}", .{func.target.cpu.arch});
57035697}
57045698
5705fn airAtomicStore(self: *Self, inst: Air.Inst.Index, order: std.builtin.AtomicOrder) !void {
5699fn airAtomicStore(func: *Func, inst: Air.Inst.Index, order: std.builtin.AtomicOrder) !void {
57065700 _ = inst;
57075701 _ = order;
5708 return self.fail("TODO implement airAtomicStore for {}", .{self.target.cpu.arch});
5702 return func.fail("TODO implement airAtomicStore for {}", .{func.target.cpu.arch});
57095703}
57105704
5711fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void {
5712 const zcu = self.bin_file.comp.module.?;
5713 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
5705fn airMemset(func: *Func, inst: Air.Inst.Index, safety: bool) !void {
5706 const zcu = func.bin_file.comp.module.?;
5707 const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
57145708
57155709 result: {
5716 if (!safety and (try self.resolveInst(bin_op.rhs)) == .undef) break :result;
5710 if (!safety and (try func.resolveInst(bin_op.rhs)) == .undef) break :result;
57175711
5718 const dst_ptr = try self.resolveInst(bin_op.lhs);
5719 const dst_ptr_ty = self.typeOf(bin_op.lhs);
5712 const dst_ptr = try func.resolveInst(bin_op.lhs);
5713 const dst_ptr_ty = func.typeOf(bin_op.lhs);
57205714 const dst_ptr_lock: ?RegisterLock = switch (dst_ptr) {
5721 .register => |reg| self.register_manager.lockRegAssumeUnused(reg),
5715 .register => |reg| func.register_manager.lockRegAssumeUnused(reg),
57225716 else => null,
57235717 };
5724 defer if (dst_ptr_lock) |lock| self.register_manager.unlockReg(lock);
5718 defer if (dst_ptr_lock) |lock| func.register_manager.unlockReg(lock);
57255719
5726 const src_val = try self.resolveInst(bin_op.rhs);
5727 const elem_ty = self.typeOf(bin_op.rhs);
5720 const src_val = try func.resolveInst(bin_op.rhs);
5721 const elem_ty = func.typeOf(bin_op.rhs);
57285722 const src_val_lock: ?RegisterLock = switch (src_val) {
5729 .register => |reg| self.register_manager.lockRegAssumeUnused(reg),
5723 .register => |reg| func.register_manager.lockRegAssumeUnused(reg),
57305724 else => null,
57315725 };
5732 defer if (src_val_lock) |lock| self.register_manager.unlockReg(lock);
5726 defer if (src_val_lock) |lock| func.register_manager.unlockReg(lock);
57335727
57345728 const elem_abi_size: u31 = @intCast(elem_ty.abiSize(zcu));
57355729
......@@ -5747,12 +5741,12 @@ fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void {
57475741 .C, .Many => unreachable,
57485742 };
57495743 const len_lock: ?RegisterLock = switch (len) {
5750 .register => |reg| self.register_manager.lockRegAssumeUnused(reg),
5744 .register => |reg| func.register_manager.lockRegAssumeUnused(reg),
57515745 else => null,
57525746 };
5753 defer if (len_lock) |lock| self.register_manager.unlockReg(lock);
5747 defer if (len_lock) |lock| func.register_manager.unlockReg(lock);
57545748
5755 try self.genInlineMemset(ptr, src_val, len);
5749 try func.genInlineMemset(ptr, src_val, len);
57565750 break :result;
57575751 }
57585752
......@@ -5760,87 +5754,87 @@ fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void {
57605754 // Length zero requires a runtime check - so we handle arrays specially
57615755 // here to elide it.
57625756 switch (dst_ptr_ty.ptrSize(zcu)) {
5763 .Slice => return self.fail("TODO: airMemset Slices", .{}),
5757 .Slice => return func.fail("TODO: airMemset Slices", .{}),
57645758 .One => {
57655759 const elem_ptr_ty = try zcu.singleMutPtrType(elem_ty);
57665760
57675761 const len = dst_ptr_ty.childType(zcu).arrayLen(zcu);
57685762
57695763 assert(len != 0); // prevented by Sema
5770 try self.store(dst_ptr, src_val, elem_ptr_ty, elem_ty);
5764 try func.store(dst_ptr, src_val, elem_ptr_ty, elem_ty);
57715765
5772 const second_elem_ptr_reg, const second_elem_ptr_lock = try self.allocReg(.int);
5773 defer self.register_manager.unlockReg(second_elem_ptr_lock);
5766 const second_elem_ptr_reg, const second_elem_ptr_lock = try func.allocReg(.int);
5767 defer func.register_manager.unlockReg(second_elem_ptr_lock);
57745768
57755769 const second_elem_ptr_mcv: MCValue = .{ .register = second_elem_ptr_reg };
57765770
5777 try self.genSetReg(Type.usize, second_elem_ptr_reg, .{ .register_offset = .{
5778 .reg = try self.copyToTmpRegister(Type.usize, dst_ptr),
5771 try func.genSetReg(Type.usize, second_elem_ptr_reg, .{ .register_offset = .{
5772 .reg = try func.copyToTmpRegister(Type.usize, dst_ptr),
57795773 .off = elem_abi_size,
57805774 } });
57815775
57825776 const bytes_to_copy: MCValue = .{ .immediate = elem_abi_size * (len - 1) };
5783 try self.genInlineMemcpy(second_elem_ptr_mcv, dst_ptr, bytes_to_copy);
5777 try func.genInlineMemcpy(second_elem_ptr_mcv, dst_ptr, bytes_to_copy);
57845778 },
57855779 .C, .Many => unreachable,
57865780 }
57875781 }
5788 return self.finishAir(inst, .unreach, .{ bin_op.lhs, bin_op.rhs, .none });
5782 return func.finishAir(inst, .unreach, .{ bin_op.lhs, bin_op.rhs, .none });
57895783}
57905784
5791fn airMemcpy(self: *Self, inst: Air.Inst.Index) !void {
5785fn airMemcpy(func: *Func, inst: Air.Inst.Index) !void {
57925786 _ = inst;
5793 return self.fail("TODO implement airMemcpy for {}", .{self.target.cpu.arch});
5787 return func.fail("TODO implement airMemcpy for {}", .{func.target.cpu.arch});
57945788}
57955789
5796fn airTagName(self: *Self, inst: Air.Inst.Index) !void {
5797 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
5798 const operand = try self.resolveInst(un_op);
5799 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else {
5790fn airTagName(func: *Func, inst: Air.Inst.Index) !void {
5791 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
5792 const operand = try func.resolveInst(un_op);
5793 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else {
58005794 _ = operand;
5801 return self.fail("TODO implement airTagName for riscv64", .{});
5795 return func.fail("TODO implement airTagName for riscv64", .{});
58025796 };
5803 return self.finishAir(inst, result, .{ un_op, .none, .none });
5797 return func.finishAir(inst, result, .{ un_op, .none, .none });
58045798}
58055799
5806fn airErrorName(self: *Self, inst: Air.Inst.Index) !void {
5807 const zcu = self.bin_file.comp.module.?;
5808 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
5800fn airErrorName(func: *Func, inst: Air.Inst.Index) !void {
5801 const zcu = func.bin_file.comp.module.?;
5802 const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
58095803
5810 const err_ty = self.typeOf(un_op);
5811 const err_mcv = try self.resolveInst(un_op);
5804 const err_ty = func.typeOf(un_op);
5805 const err_mcv = try func.resolveInst(un_op);
58125806
5813 const err_reg = try self.copyToTmpRegister(err_ty, err_mcv);
5814 const err_lock = self.register_manager.lockRegAssumeUnused(err_reg);
5815 defer self.register_manager.unlockReg(err_lock);
5807 const err_reg = try func.copyToTmpRegister(err_ty, err_mcv);
5808 const err_lock = func.register_manager.lockRegAssumeUnused(err_reg);
5809 defer func.register_manager.unlockReg(err_lock);
58165810
5817 const addr_reg, const addr_lock = try self.allocReg(.int);
5818 defer self.register_manager.unlockReg(addr_lock);
5811 const addr_reg, const addr_lock = try func.allocReg(.int);
5812 defer func.register_manager.unlockReg(addr_lock);
58195813
58205814 // this is now the base address of the error name table
58215815 const lazy_sym = link.File.LazySymbol.initDecl(.const_data, null, zcu);
5822 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
5816 if (func.bin_file.cast(link.File.Elf)) |elf_file| {
58235817 const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err|
5824 return self.fail("{s} creating lazy symbol", .{@errorName(err)});
5818 return func.fail("{s} creating lazy symbol", .{@errorName(err)});
58255819 const sym = elf_file.symbol(sym_index);
5826 try self.genSetReg(Type.usize, addr_reg, .{ .load_symbol = .{ .sym = sym.esym_index } });
5820 try func.genSetReg(Type.usize, addr_reg, .{ .load_symbol = .{ .sym = sym.esym_index } });
58275821 } else {
5828 return self.fail("TODO: riscv non-elf", .{});
5822 return func.fail("TODO: riscv non-elf", .{});
58295823 }
58305824
5831 const start_reg, const start_lock = try self.allocReg(.int);
5832 defer self.register_manager.unlockReg(start_lock);
5825 const start_reg, const start_lock = try func.allocReg(.int);
5826 defer func.register_manager.unlockReg(start_lock);
58335827
5834 const end_reg, const end_lock = try self.allocReg(.int);
5835 defer self.register_manager.unlockReg(end_lock);
5828 const end_reg, const end_lock = try func.allocReg(.int);
5829 defer func.register_manager.unlockReg(end_lock);
58365830
5837 // const tmp_reg, const tmp_lock = try self.allocReg(.int);
5838 // defer self.register_manager.unlockReg(tmp_lock);
5831 // const tmp_reg, const tmp_lock = try func.allocReg(.int);
5832 // defer func.register_manager.unlockReg(tmp_lock);
58395833
58405834 // we move the base address forward by the following formula: base + (errno * 8)
58415835
58425836 // shifting left by 4 is the same as multiplying by 8
5843 _ = try self.addInst(.{
5837 _ = try func.addInst(.{
58445838 .tag = .slli,
58455839 .ops = .rri,
58465840 .data = .{ .i_type = .{
......@@ -5850,7 +5844,7 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void {
58505844 } },
58515845 });
58525846
5853 _ = try self.addInst(.{
5847 _ = try func.addInst(.{
58545848 .tag = .add,
58555849 .ops = .rrr,
58565850 .data = .{ .r_type = .{
......@@ -5860,7 +5854,7 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void {
58605854 } },
58615855 });
58625856
5863 _ = try self.addInst(.{
5857 _ = try func.addInst(.{
58645858 .tag = .pseudo,
58655859 .ops = .pseudo_load_rm,
58665860 .data = .{
......@@ -5874,7 +5868,7 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void {
58745868 },
58755869 });
58765870
5877 _ = try self.addInst(.{
5871 _ = try func.addInst(.{
58785872 .tag = .pseudo,
58795873 .ops = .pseudo_load_rm,
58805874 .data = .{
......@@ -5888,64 +5882,64 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void {
58885882 },
58895883 });
58905884
5891 const dst_mcv = try self.allocRegOrMem(inst, false);
5885 const dst_mcv = try func.allocRegOrMem(inst, false);
58925886 const frame = dst_mcv.load_frame;
5893 try self.genSetMem(
5887 try func.genSetMem(
58945888 .{ .frame = frame.index },
58955889 frame.off,
58965890 Type.usize,
58975891 .{ .register = start_reg },
58985892 );
58995893
5900 try self.genSetMem(
5894 try func.genSetMem(
59015895 .{ .frame = frame.index },
59025896 frame.off + 8,
59035897 Type.usize,
59045898 .{ .register = end_reg },
59055899 );
59065900
5907 return self.finishAir(inst, dst_mcv, .{ un_op, .none, .none });
5901 return func.finishAir(inst, dst_mcv, .{ un_op, .none, .none });
59085902}
59095903
5910fn airSplat(self: *Self, inst: Air.Inst.Index) !void {
5911 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5912 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airSplat for riscv64", .{});
5913 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
5904fn airSplat(func: *Func, inst: Air.Inst.Index) !void {
5905 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5906 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airSplat for riscv64", .{});
5907 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
59145908}
59155909
5916fn airSelect(self: *Self, inst: Air.Inst.Index) !void {
5917 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
5918 const extra = self.air.extraData(Air.Bin, pl_op.payload).data;
5919 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airSelect for riscv64", .{});
5920 return self.finishAir(inst, result, .{ pl_op.operand, extra.lhs, extra.rhs });
5910fn airSelect(func: *Func, inst: Air.Inst.Index) !void {
5911 const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
5912 const extra = func.air.extraData(Air.Bin, pl_op.payload).data;
5913 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airSelect for riscv64", .{});
5914 return func.finishAir(inst, result, .{ pl_op.operand, extra.lhs, extra.rhs });
59215915}
59225916
5923fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {
5924 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5925 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airShuffle for riscv64", .{});
5926 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
5917fn airShuffle(func: *Func, inst: Air.Inst.Index) !void {
5918 const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5919 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airShuffle for riscv64", .{});
5920 return func.finishAir(inst, result, .{ ty_op.operand, .none, .none });
59275921}
59285922
5929fn airReduce(self: *Self, inst: Air.Inst.Index) !void {
5930 const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce;
5931 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airReduce for riscv64", .{});
5932 return self.finishAir(inst, result, .{ reduce.operand, .none, .none });
5923fn airReduce(func: *Func, inst: Air.Inst.Index) !void {
5924 const reduce = func.air.instructions.items(.data)[@intFromEnum(inst)].reduce;
5925 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airReduce for riscv64", .{});
5926 return func.finishAir(inst, result, .{ reduce.operand, .none, .none });
59335927}
59345928
5935fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
5936 const zcu = self.bin_file.comp.module.?;
5937 const result_ty = self.typeOfIndex(inst);
5929fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void {
5930 const zcu = func.bin_file.comp.module.?;
5931 const result_ty = func.typeOfIndex(inst);
59385932 const len: usize = @intCast(result_ty.arrayLen(zcu));
5939 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
5940 const elements: []const Air.Inst.Ref = @ptrCast(self.air.extra[ty_pl.payload..][0..len]);
5933 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
5934 const elements: []const Air.Inst.Ref = @ptrCast(func.air.extra[ty_pl.payload..][0..len]);
59415935
59425936 const result: MCValue = result: {
59435937 switch (result_ty.zigTypeTag(zcu)) {
59445938 .Struct => {
5945 const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu));
5939 const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu));
59465940 if (result_ty.containerLayout(zcu) == .@"packed") {
59475941 const struct_obj = zcu.typeToStruct(result_ty).?;
5948 try self.genInlineMemset(
5942 try func.genInlineMemset(
59495943 .{ .lea_frame = .{ .index = frame_index } },
59505944 .{ .immediate = 0 },
59515945 .{ .immediate = result_ty.abiSize(zcu) },
......@@ -5958,7 +5952,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
59585952 const elem_ty = result_ty.structFieldType(elem_i, zcu);
59595953 const elem_bit_size: u32 = @intCast(elem_ty.bitSize(zcu));
59605954 if (elem_bit_size > 64) {
5961 return self.fail(
5955 return func.fail(
59625956 "TODO airAggregateInit implement packed structs with large fields",
59635957 .{},
59645958 );
......@@ -5969,109 +5963,109 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
59695963 const elem_off = zcu.structPackedFieldBitOffset(struct_obj, elem_i);
59705964 const elem_byte_off: i32 = @intCast(elem_off / elem_abi_bits * elem_abi_size);
59715965 const elem_bit_off = elem_off % elem_abi_bits;
5972 const elem_mcv = try self.resolveInst(elem);
5966 const elem_mcv = try func.resolveInst(elem);
59735967
59745968 _ = elem_byte_off;
59755969 _ = elem_bit_off;
59765970
59775971 const elem_lock = switch (elem_mcv) {
5978 .register => |reg| self.register_manager.lockReg(reg),
5972 .register => |reg| func.register_manager.lockReg(reg),
59795973 .immediate => |imm| lock: {
59805974 if (imm == 0) continue;
59815975 break :lock null;
59825976 },
59835977 else => null,
59845978 };
5985 defer if (elem_lock) |lock| self.register_manager.unlockReg(lock);
5979 defer if (elem_lock) |lock| func.register_manager.unlockReg(lock);
59865980
5987 return self.fail("TODO: airAggregateInit packed structs", .{});
5981 return func.fail("TODO: airAggregateInit packed structs", .{});
59885982 }
59895983 } else for (elements, 0..) |elem, elem_i| {
59905984 if ((try result_ty.structFieldValueComptime(zcu, elem_i)) != null) continue;
59915985
59925986 const elem_ty = result_ty.structFieldType(elem_i, zcu);
59935987 const elem_off: i32 = @intCast(result_ty.structFieldOffset(elem_i, zcu));
5994 const elem_mcv = try self.resolveInst(elem);
5995 try self.genSetMem(.{ .frame = frame_index }, elem_off, elem_ty, elem_mcv);
5988 const elem_mcv = try func.resolveInst(elem);
5989 try func.genSetMem(.{ .frame = frame_index }, elem_off, elem_ty, elem_mcv);
59965990 }
59975991 break :result .{ .load_frame = .{ .index = frame_index } };
59985992 },
59995993 .Array => {
60005994 const elem_ty = result_ty.childType(zcu);
6001 const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu));
5995 const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu));
60025996 const elem_size: u32 = @intCast(elem_ty.abiSize(zcu));
60035997
60045998 for (elements, 0..) |elem, elem_i| {
6005 const elem_mcv = try self.resolveInst(elem);
5999 const elem_mcv = try func.resolveInst(elem);
60066000 const elem_off: i32 = @intCast(elem_size * elem_i);
6007 try self.genSetMem(
6001 try func.genSetMem(
60086002 .{ .frame = frame_index },
60096003 elem_off,
60106004 elem_ty,
60116005 elem_mcv,
60126006 );
60136007 }
6014 if (result_ty.sentinel(zcu)) |sentinel| try self.genSetMem(
6008 if (result_ty.sentinel(zcu)) |sentinel| try func.genSetMem(
60156009 .{ .frame = frame_index },
60166010 @intCast(elem_size * elements.len),
60176011 elem_ty,
6018 try self.genTypedValue(sentinel),
6012 try func.genTypedValue(sentinel),
60196013 );
60206014 break :result .{ .load_frame = .{ .index = frame_index } };
60216015 },
6022 else => return self.fail("TODO: airAggregate {}", .{result_ty.fmt(zcu)}),
6016 else => return func.fail("TODO: airAggregate {}", .{result_ty.fmt(zcu)}),
60236017 }
60246018 };
60256019
60266020 if (elements.len <= Liveness.bpi - 1) {
60276021 var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1);
60286022 @memcpy(buf[0..elements.len], elements);
6029 return self.finishAir(inst, result, buf);
6023 return func.finishAir(inst, result, buf);
60306024 }
6031 var bt = self.liveness.iterateBigTomb(inst);
6032 for (elements) |elem| try self.feed(&bt, elem);
6033 return self.finishAirResult(inst, result);
6025 var bt = func.liveness.iterateBigTomb(inst);
6026 for (elements) |elem| try func.feed(&bt, elem);
6027 return func.finishAirResult(inst, result);
60346028}
60356029
6036fn airUnionInit(self: *Self, inst: Air.Inst.Index) !void {
6037 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
6038 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;
6030fn airUnionInit(func: *Func, inst: Air.Inst.Index) !void {
6031 const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
6032 const extra = func.air.extraData(Air.UnionInit, ty_pl.payload).data;
60396033 _ = extra;
6040 return self.fail("TODO implement airUnionInit for riscv64", .{});
6041 // return self.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value });
6034 return func.fail("TODO implement airUnionInit for riscv64", .{});
6035 // return func.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value });
60426036}
60436037
6044fn airPrefetch(self: *Self, inst: Air.Inst.Index) !void {
6045 const prefetch = self.air.instructions.items(.data)[@intFromEnum(inst)].prefetch;
6038fn airPrefetch(func: *Func, inst: Air.Inst.Index) !void {
6039 const prefetch = func.air.instructions.items(.data)[@intFromEnum(inst)].prefetch;
60466040 // TODO: RISC-V does have prefetch instruction variants.
60476041 // see here: https://raw.githubusercontent.com/riscv/riscv-CMOs/master/specifications/cmobase-v1.0.1.pdf
6048 return self.finishAir(inst, .unreach, .{ prefetch.ptr, .none, .none });
6042 return func.finishAir(inst, .unreach, .{ prefetch.ptr, .none, .none });
60496043}
60506044
6051fn airMulAdd(self: *Self, inst: Air.Inst.Index) !void {
6052 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
6053 const extra = self.air.extraData(Air.Bin, pl_op.payload).data;
6054 const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else {
6055 return self.fail("TODO implement airMulAdd for riscv64", .{});
6045fn airMulAdd(func: *Func, inst: Air.Inst.Index) !void {
6046 const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
6047 const extra = func.air.extraData(Air.Bin, pl_op.payload).data;
6048 const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else {
6049 return func.fail("TODO implement airMulAdd for riscv64", .{});
60566050 };
6057 return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, pl_op.operand });
6051 return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, pl_op.operand });
60586052}
60596053
6060fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
6061 const zcu = self.bin_file.comp.module.?;
6054fn resolveInst(func: *Func, ref: Air.Inst.Ref) InnerError!MCValue {
6055 const zcu = func.bin_file.comp.module.?;
60626056
60636057 // If the type has no codegen bits, no need to store it.
6064 const inst_ty = self.typeOf(ref);
6058 const inst_ty = func.typeOf(ref);
60656059 if (!inst_ty.hasRuntimeBits(zcu))
60666060 return .none;
60676061
60686062 const mcv = if (ref.toIndex()) |inst| mcv: {
6069 break :mcv self.inst_tracking.getPtr(inst).?.short;
6063 break :mcv func.inst_tracking.getPtr(inst).?.short;
60706064 } else mcv: {
60716065 const ip_index = ref.toInterned().?;
6072 const gop = try self.const_tracking.getOrPut(self.gpa, ip_index);
6066 const gop = try func.const_tracking.getOrPut(func.gpa, ip_index);
60736067 if (!gop.found_existing) gop.value_ptr.* = InstTracking.init(
6074 try self.genTypedValue(Value.fromInterned(ip_index)),
6068 try func.genTypedValue(Value.fromInterned(ip_index)),
60756069 );
60766070 break :mcv gop.value_ptr.short;
60776071 };
......@@ -6079,21 +6073,21 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
60796073 return mcv;
60806074}
60816075
6082fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) *InstTracking {
6083 const tracking = self.inst_tracking.getPtr(inst).?;
6076fn getResolvedInstValue(func: *Func, inst: Air.Inst.Index) *InstTracking {
6077 const tracking = func.inst_tracking.getPtr(inst).?;
60846078 return switch (tracking.short) {
60856079 .none, .unreach, .dead => unreachable,
60866080 else => tracking,
60876081 };
60886082}
60896083
6090fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
6091 const zcu = self.bin_file.comp.module.?;
6084fn genTypedValue(func: *Func, val: Value) InnerError!MCValue {
6085 const zcu = func.bin_file.comp.module.?;
60926086 const result = try codegen.genTypedValue(
6093 self.bin_file,
6094 self.src_loc,
6087 func.bin_file,
6088 func.src_loc,
60956089 val,
6096 zcu.funcOwnerDeclIndex(self.func_index),
6090 zcu.funcOwnerDeclIndex(func.func_index),
60976091 );
60986092 const mcv: MCValue = switch (result) {
60996093 .mcv => |mcv| switch (mcv) {
......@@ -6103,11 +6097,11 @@ fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
61036097 .immediate => |imm| .{ .immediate = imm },
61046098 .memory => |addr| .{ .memory = addr },
61056099 .load_got, .load_direct, .load_tlv => {
6106 return self.fail("TODO: genTypedValue {s}", .{@tagName(mcv)});
6100 return func.fail("TODO: genTypedValue {s}", .{@tagName(mcv)});
61076101 },
61086102 },
61096103 .fail => |msg| {
6110 self.err_msg = msg;
6104 func.err_msg = msg;
61116105 return error.CodegenFail;
61126106 },
61136107 };
......@@ -6120,39 +6114,39 @@ const CallMCValues = struct {
61206114 stack_byte_count: u31,
61216115 stack_align: Alignment,
61226116
6123 fn deinit(self: *CallMCValues, func: *Self) void {
6124 func.gpa.free(self.args);
6125 self.* = undefined;
6117 fn deinit(call: *CallMCValues, func: *Func) void {
6118 func.gpa.free(call.args);
6119 call.* = undefined;
61266120 }
61276121};
61286122
61296123/// Caller must call `CallMCValues.deinit`.
61306124fn resolveCallingConventionValues(
6131 self: *Self,
6125 func: *Func,
61326126 fn_info: InternPool.Key.FuncType,
61336127 var_args: []const Type,
61346128) !CallMCValues {
6135 const zcu = self.bin_file.comp.module.?;
6129 const zcu = func.bin_file.comp.module.?;
61366130 const ip = &zcu.intern_pool;
61376131
6138 const param_types = try self.gpa.alloc(Type, fn_info.param_types.len + var_args.len);
6139 defer self.gpa.free(param_types);
6132 const param_types = try func.gpa.alloc(Type, fn_info.param_types.len + var_args.len);
6133 defer func.gpa.free(param_types);
61406134
61416135 for (param_types[0..fn_info.param_types.len], fn_info.param_types.get(ip)) |*dest, src| {
61426136 dest.* = Type.fromInterned(src);
61436137 }
61446138 for (param_types[fn_info.param_types.len..], var_args) |*param_ty, arg_ty|
6145 param_ty.* = self.promoteVarArg(arg_ty);
6139 param_ty.* = func.promoteVarArg(arg_ty);
61466140
61476141 const cc = fn_info.cc;
61486142 var result: CallMCValues = .{
6149 .args = try self.gpa.alloc(MCValue, param_types.len),
6143 .args = try func.gpa.alloc(MCValue, param_types.len),
61506144 // These undefined values must be populated before returning from this function.
61516145 .return_value = undefined,
61526146 .stack_byte_count = 0,
61536147 .stack_align = undefined,
61546148 };
6155 errdefer self.gpa.free(result.args);
6149 errdefer func.gpa.free(result.args);
61566150
61576151 const ret_ty = Type.fromInterned(fn_info.return_type);
61586152
......@@ -6164,7 +6158,7 @@ fn resolveCallingConventionValues(
61646158 },
61656159 .C, .Unspecified => {
61666160 if (result.args.len > 8) {
6167 return self.fail("RISC-V calling convention does not support more than 8 arguments", .{});
6161 return func.fail("RISC-V calling convention does not support more than 8 arguments", .{});
61686162 }
61696163
61706164 var ret_int_reg_i: u32 = 0;
......@@ -6211,11 +6205,11 @@ fn resolveCallingConventionValues(
62116205 };
62126206 ret_tracking_i += 1;
62136207 },
6214 else => return self.fail("TODO: C calling convention return class {}", .{class}),
6208 else => return func.fail("TODO: C calling convention return class {}", .{class}),
62156209 };
62166210
62176211 result.return_value = switch (ret_tracking_i) {
6218 else => return self.fail("ty {} took {} tracking return indices", .{ ret_ty.fmt(zcu), ret_tracking_i }),
6212 else => return func.fail("ty {} took {} tracking return indices", .{ ret_ty.fmt(zcu), ret_tracking_i }),
62196213 1 => ret_tracking[0],
62206214 2 => InstTracking.init(.{ .register_pair = .{
62216215 ret_tracking[0].short.register, ret_tracking[1].short.register,
......@@ -6267,28 +6261,28 @@ fn resolveCallingConventionValues(
62676261 arg_mcv[arg_mcv_i] = .{ .indirect = .{ .reg = param_int_reg } };
62686262 arg_mcv_i += 1;
62696263 },
6270 else => return self.fail("TODO: C calling convention arg class {}", .{class}),
6264 else => return func.fail("TODO: C calling convention arg class {}", .{class}),
62716265 } else {
62726266 arg.* = switch (arg_mcv_i) {
6273 else => return self.fail("ty {} took {} tracking arg indices", .{ ty.fmt(zcu), arg_mcv_i }),
6267 else => return func.fail("ty {} took {} tracking arg indices", .{ ty.fmt(zcu), arg_mcv_i }),
62746268 1 => arg_mcv[0],
62756269 2 => .{ .register_pair = .{ arg_mcv[0].register, arg_mcv[1].register } },
62766270 };
62776271 continue;
62786272 }
62796273
6280 return self.fail("TODO: pass args by stack", .{});
6274 return func.fail("TODO: pass args by stack", .{});
62816275 }
62826276 },
6283 else => return self.fail("TODO implement function parameters for {} on riscv64", .{cc}),
6277 else => return func.fail("TODO implement function parameters for {} on riscv64", .{cc}),
62846278 }
62856279
62866280 result.stack_byte_count = @intCast(result.stack_align.forward(result.stack_byte_count));
62876281 return result;
62886282}
62896283
6290fn wantSafety(self: *Self) bool {
6291 return switch (self.mod.optimize_mode) {
6284fn wantSafety(func: *Func) bool {
6285 return switch (func.mod.optimize_mode) {
62926286 .Debug => true,
62936287 .ReleaseSafe => true,
62946288 .ReleaseFast => false,
......@@ -6296,39 +6290,36 @@ fn wantSafety(self: *Self) bool {
62966290 };
62976291}
62986292
6299fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError {
6293fn fail(func: *Func, comptime format: []const u8, args: anytype) InnerError {
63006294 @setCold(true);
6301 assert(self.err_msg == null);
6302 self.err_msg = try ErrorMsg.create(self.gpa, self.src_loc, format, args);
6295 assert(func.err_msg == null);
6296 func.err_msg = try ErrorMsg.create(func.gpa, func.src_loc, format, args);
63036297 return error.CodegenFail;
63046298}
63056299
6306fn failSymbol(self: *Self, comptime format: []const u8, args: anytype) InnerError {
6300fn failSymbol(func: *Func, comptime format: []const u8, args: anytype) InnerError {
63076301 @setCold(true);
6308 assert(self.err_msg == null);
6309 self.err_msg = try ErrorMsg.create(self.gpa, self.src_loc, format, args);
6302 assert(func.err_msg == null);
6303 func.err_msg = try ErrorMsg.create(func.gpa, func.src_loc, format, args);
63106304 return error.CodegenFail;
63116305}
63126306
63136307fn parseRegName(name: []const u8) ?Register {
6314 if (@hasDecl(Register, "parseRegName")) {
6315 return Register.parseRegName(name);
6316 }
63176308 return std.meta.stringToEnum(Register, name);
63186309}
63196310
6320fn typeOf(self: *Self, inst: Air.Inst.Ref) Type {
6321 const zcu = self.bin_file.comp.module.?;
6322 return self.air.typeOf(inst, &zcu.intern_pool);
6311fn typeOf(func: *Func, inst: Air.Inst.Ref) Type {
6312 const zcu = func.bin_file.comp.module.?;
6313 return func.air.typeOf(inst, &zcu.intern_pool);
63236314}
63246315
6325fn typeOfIndex(self: *Self, inst: Air.Inst.Index) Type {
6326 const zcu = self.bin_file.comp.module.?;
6327 return self.air.typeOfIndex(inst, &zcu.intern_pool);
6316fn typeOfIndex(func: *Func, inst: Air.Inst.Index) Type {
6317 const zcu = func.bin_file.comp.module.?;
6318 return func.air.typeOfIndex(inst, &zcu.intern_pool);
63286319}
63296320
6330fn hasFeature(self: *Self, feature: Target.riscv.Feature) bool {
6331 return Target.riscv.featureSetHas(self.target.cpu.features, feature);
6321fn hasFeature(func: *Func, feature: Target.riscv.Feature) bool {
6322 return Target.riscv.featureSetHas(func.target.cpu.features, feature);
63326323}
63336324
63346325pub fn errUnionPayloadOffset(payload_ty: Type, zcu: *Module) u64 {
......@@ -6353,8 +6344,8 @@ pub fn errUnionErrorOffset(payload_ty: Type, zcu: *Module) u64 {
63536344 }
63546345}
63556346
6356fn promoteInt(self: *Self, ty: Type) Type {
6357 const mod = self.bin_file.comp.module.?;
6347fn promoteInt(func: *Func, ty: Type) Type {
6348 const mod = func.bin_file.comp.module.?;
63586349 const int_info: InternPool.Key.IntType = switch (ty.toIntern()) {
63596350 .bool_type => .{ .signedness = .unsigned, .bits = 1 },
63606351 else => if (ty.isAbiInt(mod)) ty.intInfo(mod) else return ty,
......@@ -6372,12 +6363,12 @@ fn promoteInt(self: *Self, ty: Type) Type {
63726363 return ty;
63736364}
63746365
6375fn promoteVarArg(self: *Self, ty: Type) Type {
6376 if (!ty.isRuntimeFloat()) return self.promoteInt(ty);
6377 switch (ty.floatBits(self.target.*)) {
6366fn promoteVarArg(func: *Func, ty: Type) Type {
6367 if (!ty.isRuntimeFloat()) return func.promoteInt(ty);
6368 switch (ty.floatBits(func.target.*)) {
63786369 32, 64 => return Type.f64,
63796370 else => |float_bits| {
6380 assert(float_bits == self.target.c_type_bit_size(.longdouble));
6371 assert(float_bits == func.target.c_type_bit_size(.longdouble));
63816372 return Type.c_longdouble;
63826373 },
63836374 }