| ... | ... | @@ -22,6 +22,8 @@ const DW = std.dwarf; |
| 22 | 22 | const leb128 = std.leb; |
| 23 | 23 | const log = std.log.scoped(.riscv_codegen); |
| 24 | 24 | const tracking_log = std.log.scoped(.tracking); |
| 25 | const verbose_tracking_log = std.log.scoped(.verbose_tracking); |
| 26 | const wip_mir_log = std.log.scoped(.wip_mir); |
| 25 | 27 | const build_options = @import("build_options"); |
| 26 | 28 | const codegen = @import("../../codegen.zig"); |
| 27 | 29 | const Alignment = InternPool.Alignment; |
| ... | ... | @@ -32,29 +34,17 @@ const DebugInfoOutput = codegen.DebugInfoOutput; |
| 32 | 34 | |
| 33 | 35 | const bits = @import("bits.zig"); |
| 34 | 36 | const abi = @import("abi.zig"); |
| 37 | const Lower = @import("Lower.zig"); |
| 38 | |
| 35 | 39 | const Register = bits.Register; |
| 36 | 40 | const Immediate = bits.Immediate; |
| 37 | 41 | const Memory = bits.Memory; |
| 38 | 42 | const FrameIndex = bits.FrameIndex; |
| 39 | 43 | const RegisterManager = abi.RegisterManager; |
| 40 | 44 | const RegisterLock = RegisterManager.RegisterLock; |
| 41 | | const callee_preserved_regs = abi.callee_preserved_regs; |
| 42 | | /// General Purpose |
| 43 | | const gp = abi.RegisterClass.gp; |
| 44 | | /// Function Args |
| 45 | | const fa = abi.RegisterClass.fa; |
| 46 | | /// Function Returns |
| 47 | | const fr = abi.RegisterClass.fr; |
| 48 | | /// Temporary Use |
| 49 | | const tp = abi.RegisterClass.tp; |
| 50 | 45 | |
| 51 | 46 | const InnerError = CodeGenError || error{OutOfRegisters}; |
| 52 | 47 | |
| 53 | | const RegisterView = enum(u1) { |
| 54 | | caller, |
| 55 | | callee, |
| 56 | | }; |
| 57 | | |
| 58 | 48 | gpa: Allocator, |
| 59 | 49 | air: Air, |
| 60 | 50 | mod: *Package.Module, |
| ... | ... | @@ -172,6 +162,14 @@ const MCValue = union(enum) { |
| 172 | 162 | }; |
| 173 | 163 | } |
| 174 | 164 | |
| 165 | fn isRegister(mcv: MCValue) bool { |
| 166 | return switch (mcv) { |
| 167 | .register => true, |
| 168 | .register_offset => |reg_off| return reg_off.off == 0, |
| 169 | else => false, |
| 170 | }; |
| 171 | } |
| 172 | |
| 175 | 173 | fn isMutable(mcv: MCValue) bool { |
| 176 | 174 | return switch (mcv) { |
| 177 | 175 | .none => unreachable, |
| ... | ... | @@ -295,14 +293,15 @@ const MCValue = union(enum) { |
| 295 | 293 | const Branch = struct { |
| 296 | 294 | inst_table: std.AutoArrayHashMapUnmanaged(Air.Inst.Index, MCValue) = .{}, |
| 297 | 295 | |
| 298 | | fn deinit(self: *Branch, gpa: Allocator) void { |
| 299 | | self.inst_table.deinit(gpa); |
| 300 | | self.* = undefined; |
| 296 | fn deinit(func: *Branch, gpa: Allocator) void { |
| 297 | func.inst_table.deinit(gpa); |
| 298 | func.* = undefined; |
| 301 | 299 | } |
| 302 | 300 | }; |
| 303 | 301 | |
| 304 | 302 | const InstTrackingMap = std.AutoArrayHashMapUnmanaged(Air.Inst.Index, InstTracking); |
| 305 | 303 | const ConstTrackingMap = std.AutoArrayHashMapUnmanaged(InternPool.Index, InstTracking); |
| 304 | |
| 306 | 305 | const InstTracking = struct { |
| 307 | 306 | long: MCValue, |
| 308 | 307 | short: MCValue, |
| ... | ... | @@ -331,33 +330,37 @@ const InstTracking = struct { |
| 331 | 330 | }, .short = result }; |
| 332 | 331 | } |
| 333 | 332 | |
| 334 | | fn getReg(self: InstTracking) ?Register { |
| 335 | | return self.short.getReg(); |
| 333 | fn getReg(inst_tracking: InstTracking) ?Register { |
| 334 | return inst_tracking.short.getReg(); |
| 336 | 335 | } |
| 337 | 336 | |
| 338 | | fn getRegs(self: *const InstTracking) []const Register { |
| 339 | | return self.short.getRegs(); |
| 337 | fn getRegs(inst_tracking: *const InstTracking) []const Register { |
| 338 | return inst_tracking.short.getRegs(); |
| 340 | 339 | } |
| 341 | 340 | |
| 342 | | fn spill(self: *InstTracking, function: *Self, inst: Air.Inst.Index) !void { |
| 343 | | if (std.meta.eql(self.long, self.short)) return; // Already spilled |
| 341 | fn spill(inst_tracking: *InstTracking, function: *Func, inst: Air.Inst.Index) !void { |
| 342 | if (std.meta.eql(inst_tracking.long, inst_tracking.short)) return; // Already spilled |
| 344 | 343 | // Allocate or reuse frame index |
| 345 | | switch (self.long) { |
| 346 | | .none => self.long = try function.allocRegOrMem(inst, false), |
| 344 | switch (inst_tracking.long) { |
| 345 | .none => inst_tracking.long = try function.allocRegOrMem( |
| 346 | function.typeOfIndex(inst), |
| 347 | inst, |
| 348 | false, |
| 349 | ), |
| 347 | 350 | .load_frame => {}, |
| 348 | | .reserved_frame => |index| self.long = .{ .load_frame = .{ .index = index } }, |
| 351 | .reserved_frame => |index| inst_tracking.long = .{ .load_frame = .{ .index = index } }, |
| 349 | 352 | else => unreachable, |
| 350 | 353 | } |
| 351 | | tracking_log.debug("spill %{d} from {} to {}", .{ inst, self.short, self.long }); |
| 352 | | try function.genCopy(function.typeOfIndex(inst), self.long, self.short); |
| 354 | tracking_log.debug("spill %{d} from {} to {}", .{ inst, inst_tracking.short, inst_tracking.long }); |
| 355 | try function.genCopy(function.typeOfIndex(inst), inst_tracking.long, inst_tracking.short); |
| 353 | 356 | } |
| 354 | 357 | |
| 355 | | fn reuseFrame(self: *InstTracking) void { |
| 356 | | switch (self.long) { |
| 357 | | .reserved_frame => |index| self.long = .{ .load_frame = .{ .index = index } }, |
| 358 | fn reuseFrame(inst_tracking: *InstTracking) void { |
| 359 | switch (inst_tracking.long) { |
| 360 | .reserved_frame => |index| inst_tracking.long = .{ .load_frame = .{ .index = index } }, |
| 358 | 361 | else => {}, |
| 359 | 362 | } |
| 360 | | self.short = switch (self.long) { |
| 363 | inst_tracking.short = switch (inst_tracking.long) { |
| 361 | 364 | .none, |
| 362 | 365 | .unreach, |
| 363 | 366 | .undef, |
| ... | ... | @@ -367,7 +370,7 @@ const InstTracking = struct { |
| 367 | 370 | .lea_frame, |
| 368 | 371 | .load_symbol, |
| 369 | 372 | .lea_symbol, |
| 370 | | => self.long, |
| 373 | => inst_tracking.long, |
| 371 | 374 | .dead, |
| 372 | 375 | .register, |
| 373 | 376 | .register_pair, |
| ... | ... | @@ -379,14 +382,14 @@ const InstTracking = struct { |
| 379 | 382 | }; |
| 380 | 383 | } |
| 381 | 384 | |
| 382 | | fn trackSpill(self: *InstTracking, function: *Self, inst: Air.Inst.Index) !void { |
| 383 | | try function.freeValue(self.short); |
| 384 | | self.reuseFrame(); |
| 385 | | tracking_log.debug("%{d} => {} (spilled)", .{ inst, self.* }); |
| 385 | fn trackSpill(inst_tracking: *InstTracking, function: *Func, inst: Air.Inst.Index) !void { |
| 386 | try function.freeValue(inst_tracking.short); |
| 387 | inst_tracking.reuseFrame(); |
| 388 | tracking_log.debug("%{d} => {} (spilled)", .{ inst, inst_tracking.* }); |
| 386 | 389 | } |
| 387 | 390 | |
| 388 | | fn verifyMaterialize(self: InstTracking, target: InstTracking) void { |
| 389 | | switch (self.long) { |
| 391 | fn verifyMaterialize(inst_tracking: InstTracking, target: InstTracking) void { |
| 392 | switch (inst_tracking.long) { |
| 390 | 393 | .none, |
| 391 | 394 | .unreach, |
| 392 | 395 | .undef, |
| ... | ... | @@ -395,7 +398,7 @@ const InstTracking = struct { |
| 395 | 398 | .lea_frame, |
| 396 | 399 | .load_symbol, |
| 397 | 400 | .lea_symbol, |
| 398 | | => assert(std.meta.eql(self.long, target.long)), |
| 401 | => assert(std.meta.eql(inst_tracking.long, target.long)), |
| 399 | 402 | .load_frame, |
| 400 | 403 | .reserved_frame, |
| 401 | 404 | => switch (target.long) { |
| ... | ... | @@ -416,73 +419,73 @@ const InstTracking = struct { |
| 416 | 419 | } |
| 417 | 420 | |
| 418 | 421 | fn materialize( |
| 419 | | self: *InstTracking, |
| 420 | | function: *Self, |
| 422 | inst_tracking: *InstTracking, |
| 423 | function: *Func, |
| 421 | 424 | inst: Air.Inst.Index, |
| 422 | 425 | target: InstTracking, |
| 423 | 426 | ) !void { |
| 424 | | self.verifyMaterialize(target); |
| 425 | | try self.materializeUnsafe(function, inst, target); |
| 427 | inst_tracking.verifyMaterialize(target); |
| 428 | try inst_tracking.materializeUnsafe(function, inst, target); |
| 426 | 429 | } |
| 427 | 430 | |
| 428 | 431 | fn materializeUnsafe( |
| 429 | | self: InstTracking, |
| 430 | | function: *Self, |
| 432 | inst_tracking: InstTracking, |
| 433 | function: *Func, |
| 431 | 434 | inst: Air.Inst.Index, |
| 432 | 435 | target: InstTracking, |
| 433 | 436 | ) !void { |
| 434 | 437 | const ty = function.typeOfIndex(inst); |
| 435 | | if ((self.long == .none or self.long == .reserved_frame) and target.long == .load_frame) |
| 436 | | try function.genCopy(ty, target.long, self.short); |
| 437 | | try function.genCopy(ty, target.short, self.short); |
| 438 | if ((inst_tracking.long == .none or inst_tracking.long == .reserved_frame) and target.long == .load_frame) |
| 439 | try function.genCopy(ty, target.long, inst_tracking.short); |
| 440 | try function.genCopy(ty, target.short, inst_tracking.short); |
| 438 | 441 | } |
| 439 | 442 | |
| 440 | | fn trackMaterialize(self: *InstTracking, inst: Air.Inst.Index, target: InstTracking) void { |
| 441 | | self.verifyMaterialize(target); |
| 443 | fn trackMaterialize(inst_tracking: *InstTracking, inst: Air.Inst.Index, target: InstTracking) void { |
| 444 | inst_tracking.verifyMaterialize(target); |
| 442 | 445 | // Don't clobber reserved frame indices |
| 443 | | self.long = if (target.long == .none) switch (self.long) { |
| 446 | inst_tracking.long = if (target.long == .none) switch (inst_tracking.long) { |
| 444 | 447 | .load_frame => |addr| .{ .reserved_frame = addr.index }, |
| 445 | | .reserved_frame => self.long, |
| 448 | .reserved_frame => inst_tracking.long, |
| 446 | 449 | else => target.long, |
| 447 | 450 | } else target.long; |
| 448 | | self.short = target.short; |
| 449 | | tracking_log.debug("%{d} => {} (materialize)", .{ inst, self.* }); |
| 451 | inst_tracking.short = target.short; |
| 452 | tracking_log.debug("%{d} => {} (materialize)", .{ inst, inst_tracking.* }); |
| 450 | 453 | } |
| 451 | 454 | |
| 452 | | fn resurrect(self: *InstTracking, inst: Air.Inst.Index, scope_generation: u32) void { |
| 453 | | switch (self.short) { |
| 455 | fn resurrect(inst_tracking: *InstTracking, inst: Air.Inst.Index, scope_generation: u32) void { |
| 456 | switch (inst_tracking.short) { |
| 454 | 457 | .dead => |die_generation| if (die_generation >= scope_generation) { |
| 455 | | self.reuseFrame(); |
| 456 | | tracking_log.debug("%{d} => {} (resurrect)", .{ inst, self.* }); |
| 458 | inst_tracking.reuseFrame(); |
| 459 | tracking_log.debug("%{d} => {} (resurrect)", .{ inst, inst_tracking.* }); |
| 457 | 460 | }, |
| 458 | 461 | else => {}, |
| 459 | 462 | } |
| 460 | 463 | } |
| 461 | 464 | |
| 462 | | fn die(self: *InstTracking, function: *Self, inst: Air.Inst.Index) !void { |
| 463 | | if (self.short == .dead) return; |
| 464 | | try function.freeValue(self.short); |
| 465 | | self.short = .{ .dead = function.scope_generation }; |
| 466 | | tracking_log.debug("%{d} => {} (death)", .{ inst, self.* }); |
| 465 | fn die(inst_tracking: *InstTracking, function: *Func, inst: Air.Inst.Index) !void { |
| 466 | if (inst_tracking.short == .dead) return; |
| 467 | try function.freeValue(inst_tracking.short); |
| 468 | inst_tracking.short = .{ .dead = function.scope_generation }; |
| 469 | tracking_log.debug("%{d} => {} (death)", .{ inst, inst_tracking.* }); |
| 467 | 470 | } |
| 468 | 471 | |
| 469 | 472 | fn reuse( |
| 470 | | self: *InstTracking, |
| 471 | | function: *Self, |
| 473 | inst_tracking: *InstTracking, |
| 474 | function: *Func, |
| 472 | 475 | new_inst: ?Air.Inst.Index, |
| 473 | 476 | old_inst: Air.Inst.Index, |
| 474 | 477 | ) void { |
| 475 | | self.short = .{ .dead = function.scope_generation }; |
| 478 | inst_tracking.short = .{ .dead = function.scope_generation }; |
| 476 | 479 | if (new_inst) |inst| |
| 477 | | tracking_log.debug("%{d} => {} (reuse %{d})", .{ inst, self.*, old_inst }) |
| 480 | tracking_log.debug("%{d} => {} (reuse %{d})", .{ inst, inst_tracking.*, old_inst }) |
| 478 | 481 | else |
| 479 | | tracking_log.debug("tmp => {} (reuse %{d})", .{ self.*, old_inst }); |
| 482 | tracking_log.debug("tmp => {} (reuse %{d})", .{ inst_tracking.*, old_inst }); |
| 480 | 483 | } |
| 481 | 484 | |
| 482 | | fn liveOut(self: *InstTracking, function: *Self, inst: Air.Inst.Index) void { |
| 483 | | for (self.getRegs()) |reg| { |
| 485 | fn liveOut(inst_tracking: *InstTracking, function: *Func, inst: Air.Inst.Index) void { |
| 486 | for (inst_tracking.getRegs()) |reg| { |
| 484 | 487 | if (function.register_manager.isRegFree(reg)) { |
| 485 | | tracking_log.debug("%{d} => {} (live-out)", .{ inst, self.* }); |
| 488 | tracking_log.debug("%{d} => {} (live-out)", .{ inst, inst_tracking.* }); |
| 486 | 489 | continue; |
| 487 | 490 | } |
| 488 | 491 | |
| ... | ... | @@ -509,18 +512,18 @@ const InstTracking = struct { |
| 509 | 512 | // Perform side-effects of freeValue manually. |
| 510 | 513 | function.register_manager.freeReg(reg); |
| 511 | 514 | |
| 512 | | tracking_log.debug("%{d} => {} (live-out %{d})", .{ inst, self.*, tracked_inst }); |
| 515 | tracking_log.debug("%{d} => {} (live-out %{d})", .{ inst, inst_tracking.*, tracked_inst }); |
| 513 | 516 | } |
| 514 | 517 | } |
| 515 | 518 | |
| 516 | 519 | pub fn format( |
| 517 | | self: InstTracking, |
| 520 | inst_tracking: InstTracking, |
| 518 | 521 | comptime _: []const u8, |
| 519 | 522 | _: std.fmt.FormatOptions, |
| 520 | 523 | writer: anytype, |
| 521 | 524 | ) @TypeOf(writer).Error!void { |
| 522 | | if (!std.meta.eql(self.long, self.short)) try writer.print("|{}| ", .{self.long}); |
| 523 | | try writer.print("{}", .{self.short}); |
| 525 | if (!std.meta.eql(inst_tracking.long, inst_tracking.short)) try writer.print("|{}| ", .{inst_tracking.long}); |
| 526 | try writer.print("{}", .{inst_tracking.short}); |
| 524 | 527 | } |
| 525 | 528 | }; |
| 526 | 529 | |
| ... | ... | @@ -560,19 +563,13 @@ const FrameAlloc = struct { |
| 560 | 563 | } |
| 561 | 564 | }; |
| 562 | 565 | |
| 563 | | const StackAllocation = struct { |
| 564 | | inst: ?Air.Inst.Index, |
| 565 | | /// TODO: make the size inferred from the bits of the inst |
| 566 | | size: u32, |
| 567 | | }; |
| 568 | | |
| 569 | 566 | const BlockData = struct { |
| 570 | 567 | relocs: std.ArrayListUnmanaged(Mir.Inst.Index) = .{}, |
| 571 | 568 | state: State, |
| 572 | 569 | |
| 573 | | fn deinit(self: *BlockData, gpa: Allocator) void { |
| 574 | | self.relocs.deinit(gpa); |
| 575 | | self.* = undefined; |
| 570 | fn deinit(bd: *BlockData, gpa: Allocator) void { |
| 571 | bd.relocs.deinit(gpa); |
| 572 | bd.* = undefined; |
| 576 | 573 | } |
| 577 | 574 | }; |
| 578 | 575 | |
| ... | ... | @@ -584,31 +581,31 @@ const State = struct { |
| 584 | 581 | scope_generation: u32, |
| 585 | 582 | }; |
| 586 | 583 | |
| 587 | | fn initRetroactiveState(self: *Self) State { |
| 584 | fn initRetroactiveState(func: *Func) State { |
| 588 | 585 | var state: State = undefined; |
| 589 | | state.inst_tracking_len = @intCast(self.inst_tracking.count()); |
| 590 | | state.scope_generation = self.scope_generation; |
| 586 | state.inst_tracking_len = @intCast(func.inst_tracking.count()); |
| 587 | state.scope_generation = func.scope_generation; |
| 591 | 588 | return state; |
| 592 | 589 | } |
| 593 | 590 | |
| 594 | | fn saveRetroactiveState(self: *Self, state: *State) !void { |
| 595 | | const free_registers = self.register_manager.free_registers; |
| 591 | fn saveRetroactiveState(func: *Func, state: *State) !void { |
| 592 | const free_registers = func.register_manager.free_registers; |
| 596 | 593 | var it = free_registers.iterator(.{ .kind = .unset }); |
| 597 | 594 | while (it.next()) |index| { |
| 598 | | const tracked_inst = self.register_manager.registers[index]; |
| 595 | const tracked_inst = func.register_manager.registers[index]; |
| 599 | 596 | state.registers[index] = tracked_inst; |
| 600 | | state.reg_tracking[index] = self.inst_tracking.get(tracked_inst).?; |
| 597 | state.reg_tracking[index] = func.inst_tracking.get(tracked_inst).?; |
| 601 | 598 | } |
| 602 | 599 | state.free_registers = free_registers; |
| 603 | 600 | } |
| 604 | 601 | |
| 605 | | fn saveState(self: *Self) !State { |
| 606 | | var state = self.initRetroactiveState(); |
| 607 | | try self.saveRetroactiveState(&state); |
| 602 | fn saveState(func: *Func) !State { |
| 603 | var state = func.initRetroactiveState(); |
| 604 | try func.saveRetroactiveState(&state); |
| 608 | 605 | return state; |
| 609 | 606 | } |
| 610 | 607 | |
| 611 | | fn restoreState(self: *Self, state: State, deaths: []const Air.Inst.Index, comptime opts: struct { |
| 608 | fn restoreState(func: *Func, state: State, deaths: []const Air.Inst.Index, comptime opts: struct { |
| 612 | 609 | emit_instructions: bool, |
| 613 | 610 | update_tracking: bool, |
| 614 | 611 | resurrect: bool, |
| ... | ... | @@ -616,80 +613,81 @@ fn restoreState(self: *Self, state: State, deaths: []const Air.Inst.Index, compt |
| 616 | 613 | }) !void { |
| 617 | 614 | if (opts.close_scope) { |
| 618 | 615 | for ( |
| 619 | | self.inst_tracking.keys()[state.inst_tracking_len..], |
| 620 | | self.inst_tracking.values()[state.inst_tracking_len..], |
| 621 | | ) |inst, *tracking| try tracking.die(self, inst); |
| 622 | | self.inst_tracking.shrinkRetainingCapacity(state.inst_tracking_len); |
| 616 | func.inst_tracking.keys()[state.inst_tracking_len..], |
| 617 | func.inst_tracking.values()[state.inst_tracking_len..], |
| 618 | ) |inst, *tracking| try tracking.die(func, inst); |
| 619 | func.inst_tracking.shrinkRetainingCapacity(state.inst_tracking_len); |
| 623 | 620 | } |
| 624 | 621 | |
| 625 | 622 | if (opts.resurrect) for ( |
| 626 | | self.inst_tracking.keys()[0..state.inst_tracking_len], |
| 627 | | self.inst_tracking.values()[0..state.inst_tracking_len], |
| 623 | func.inst_tracking.keys()[0..state.inst_tracking_len], |
| 624 | func.inst_tracking.values()[0..state.inst_tracking_len], |
| 628 | 625 | ) |inst, *tracking| tracking.resurrect(inst, state.scope_generation); |
| 629 | | for (deaths) |death| try self.processDeath(death); |
| 626 | for (deaths) |death| try func.processDeath(death); |
| 630 | 627 | |
| 631 | 628 | const ExpectedContents = [@typeInfo(RegisterManager.TrackedRegisters).Array.len]RegisterLock; |
| 632 | 629 | var stack align(@max(@alignOf(ExpectedContents), @alignOf(std.heap.StackFallbackAllocator(0)))) = |
| 633 | 630 | if (opts.update_tracking) |
| 634 | | {} else std.heap.stackFallback(@sizeOf(ExpectedContents), self.gpa); |
| 631 | {} else std.heap.stackFallback(@sizeOf(ExpectedContents), func.gpa); |
| 635 | 632 | |
| 636 | 633 | var reg_locks = if (opts.update_tracking) {} else try std.ArrayList(RegisterLock).initCapacity( |
| 637 | 634 | stack.get(), |
| 638 | 635 | @typeInfo(ExpectedContents).Array.len, |
| 639 | 636 | ); |
| 640 | 637 | defer if (!opts.update_tracking) { |
| 641 | | for (reg_locks.items) |lock| self.register_manager.unlockReg(lock); |
| 638 | for (reg_locks.items) |lock| func.register_manager.unlockReg(lock); |
| 642 | 639 | reg_locks.deinit(); |
| 643 | 640 | }; |
| 644 | 641 | |
| 645 | 642 | for (0..state.registers.len) |index| { |
| 646 | | const current_maybe_inst = if (self.register_manager.free_registers.isSet(index)) |
| 643 | const current_maybe_inst = if (func.register_manager.free_registers.isSet(index)) |
| 647 | 644 | null |
| 648 | 645 | else |
| 649 | | self.register_manager.registers[index]; |
| 646 | func.register_manager.registers[index]; |
| 650 | 647 | const target_maybe_inst = if (state.free_registers.isSet(index)) |
| 651 | 648 | null |
| 652 | 649 | else |
| 653 | 650 | state.registers[index]; |
| 654 | 651 | if (std.debug.runtime_safety) if (target_maybe_inst) |target_inst| |
| 655 | | assert(self.inst_tracking.getIndex(target_inst).? < state.inst_tracking_len); |
| 652 | assert(func.inst_tracking.getIndex(target_inst).? < state.inst_tracking_len); |
| 656 | 653 | if (opts.emit_instructions) { |
| 657 | 654 | if (current_maybe_inst) |current_inst| { |
| 658 | | try self.inst_tracking.getPtr(current_inst).?.spill(self, current_inst); |
| 655 | try func.inst_tracking.getPtr(current_inst).?.spill(func, current_inst); |
| 659 | 656 | } |
| 660 | 657 | if (target_maybe_inst) |target_inst| { |
| 661 | | const target_tracking = self.inst_tracking.getPtr(target_inst).?; |
| 662 | | try target_tracking.materialize(self, target_inst, state.reg_tracking[index]); |
| 658 | const target_tracking = func.inst_tracking.getPtr(target_inst).?; |
| 659 | try target_tracking.materialize(func, target_inst, state.reg_tracking[index]); |
| 663 | 660 | } |
| 664 | 661 | } |
| 665 | 662 | if (opts.update_tracking) { |
| 666 | 663 | if (current_maybe_inst) |current_inst| { |
| 667 | | try self.inst_tracking.getPtr(current_inst).?.trackSpill(self, current_inst); |
| 664 | try func.inst_tracking.getPtr(current_inst).?.trackSpill(func, current_inst); |
| 668 | 665 | } |
| 669 | | { |
| 666 | blk: { |
| 667 | const inst = target_maybe_inst orelse break :blk; |
| 670 | 668 | const reg = RegisterManager.regAtTrackedIndex(@intCast(index)); |
| 671 | | self.register_manager.freeReg(reg); |
| 672 | | self.register_manager.getRegAssumeFree(reg, target_maybe_inst); |
| 669 | func.register_manager.freeReg(reg); |
| 670 | func.register_manager.getRegAssumeFree(reg, inst); |
| 673 | 671 | } |
| 674 | 672 | if (target_maybe_inst) |target_inst| { |
| 675 | | self.inst_tracking.getPtr(target_inst).?.trackMaterialize( |
| 673 | func.inst_tracking.getPtr(target_inst).?.trackMaterialize( |
| 676 | 674 | target_inst, |
| 677 | 675 | state.reg_tracking[index], |
| 678 | 676 | ); |
| 679 | 677 | } |
| 680 | 678 | } else if (target_maybe_inst) |_| |
| 681 | | try reg_locks.append(self.register_manager.lockRegIndexAssumeUnused(@intCast(index))); |
| 679 | try reg_locks.append(func.register_manager.lockRegIndexAssumeUnused(@intCast(index))); |
| 682 | 680 | } |
| 683 | 681 | |
| 684 | 682 | if (opts.update_tracking and std.debug.runtime_safety) { |
| 685 | | assert(self.register_manager.free_registers.eql(state.free_registers)); |
| 683 | assert(func.register_manager.free_registers.eql(state.free_registers)); |
| 686 | 684 | var used_reg_it = state.free_registers.iterator(.{ .kind = .unset }); |
| 687 | 685 | while (used_reg_it.next()) |index| |
| 688 | | assert(self.register_manager.registers[index] == state.registers[index]); |
| 686 | assert(func.register_manager.registers[index] == state.registers[index]); |
| 689 | 687 | } |
| 690 | 688 | } |
| 691 | 689 | |
| 692 | | const Self = @This(); |
| 690 | const Func = @This(); |
| 693 | 691 | |
| 694 | 692 | const CallView = enum(u1) { |
| 695 | 693 | callee, |
| ... | ... | @@ -725,7 +723,7 @@ pub fn generate( |
| 725 | 723 | } |
| 726 | 724 | try branch_stack.append(.{}); |
| 727 | 725 | |
| 728 | | var function = Self{ |
| 726 | var function = Func{ |
| 729 | 727 | .gpa = gpa, |
| 730 | 728 | .air = air, |
| 731 | 729 | .mod = mod, |
| ... | ... | @@ -760,6 +758,8 @@ pub fn generate( |
| 760 | 758 | function.mir_extra.deinit(gpa); |
| 761 | 759 | } |
| 762 | 760 | |
| 761 | wip_mir_log.debug("{}:", .{function.fmtDecl(func.owner_decl)}); |
| 762 | |
| 763 | 763 | try function.frame_allocs.resize(gpa, FrameIndex.named_count); |
| 764 | 764 | function.frame_allocs.set( |
| 765 | 765 | @intFromEnum(FrameIndex.stack_frame), |
| ... | ... | @@ -774,7 +774,7 @@ pub fn generate( |
| 774 | 774 | ); |
| 775 | 775 | |
| 776 | 776 | const fn_info = zcu.typeToFunc(fn_type).?; |
| 777 | | var call_info = function.resolveCallingConventionValues(fn_info) catch |err| switch (err) { |
| 777 | var call_info = function.resolveCallingConventionValues(fn_info, &.{}) catch |err| switch (err) { |
| 778 | 778 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, |
| 779 | 779 | error.OutOfRegisters => return Result{ |
| 780 | 780 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), |
| ... | ... | @@ -865,51 +865,171 @@ pub fn generate( |
| 865 | 865 | } |
| 866 | 866 | } |
| 867 | 867 | |
| 868 | | fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index { |
| 869 | | const gpa = self.gpa; |
| 868 | const FormatWipMirData = struct { |
| 869 | func: *Func, |
| 870 | inst: Mir.Inst.Index, |
| 871 | }; |
| 872 | fn formatWipMir( |
| 873 | data: FormatWipMirData, |
| 874 | comptime _: []const u8, |
| 875 | _: std.fmt.FormatOptions, |
| 876 | writer: anytype, |
| 877 | ) @TypeOf(writer).Error!void { |
| 878 | const comp = data.func.bin_file.comp; |
| 879 | const mod = comp.root_mod; |
| 880 | var lower = Lower{ |
| 881 | .bin_file = data.func.bin_file, |
| 882 | .allocator = data.func.gpa, |
| 883 | .mir = .{ |
| 884 | .instructions = data.func.mir_instructions.slice(), |
| 885 | .extra = data.func.mir_extra.items, |
| 886 | .frame_locs = data.func.frame_locs.slice(), |
| 887 | }, |
| 888 | .cc = .Unspecified, |
| 889 | .src_loc = data.func.src_loc, |
| 890 | .output_mode = comp.config.output_mode, |
| 891 | .link_mode = comp.config.link_mode, |
| 892 | .pic = mod.pic, |
| 893 | }; |
| 894 | var first = true; |
| 895 | for ((lower.lowerMir(data.inst) catch |err| switch (err) { |
| 896 | error.LowerFail => { |
| 897 | defer { |
| 898 | lower.err_msg.?.deinit(data.func.gpa); |
| 899 | lower.err_msg = null; |
| 900 | } |
| 901 | try writer.writeAll(lower.err_msg.?.msg); |
| 902 | return; |
| 903 | }, |
| 904 | error.OutOfMemory, error.InvalidInstruction => |e| { |
| 905 | try writer.writeAll(switch (e) { |
| 906 | error.OutOfMemory => "Out of memory", |
| 907 | error.InvalidInstruction => "CodeGen failed to find a viable instruction.", |
| 908 | }); |
| 909 | return; |
| 910 | }, |
| 911 | else => |e| return e, |
| 912 | }).insts) |lowered_inst| { |
| 913 | if (!first) try writer.writeAll("\ndebug(wip_mir): "); |
| 914 | try writer.print(" | {}", .{lowered_inst}); |
| 915 | first = false; |
| 916 | } |
| 917 | } |
| 918 | fn fmtWipMir(func: *Func, inst: Mir.Inst.Index) std.fmt.Formatter(formatWipMir) { |
| 919 | return .{ .data = .{ .func = func, .inst = inst } }; |
| 920 | } |
| 870 | 921 | |
| 871 | | try self.mir_instructions.ensureUnusedCapacity(gpa, 1); |
| 922 | const FormatDeclData = struct { |
| 923 | mod: *Module, |
| 924 | decl_index: InternPool.DeclIndex, |
| 925 | }; |
| 926 | fn formatDecl( |
| 927 | data: FormatDeclData, |
| 928 | comptime _: []const u8, |
| 929 | _: std.fmt.FormatOptions, |
| 930 | writer: anytype, |
| 931 | ) @TypeOf(writer).Error!void { |
| 932 | try data.mod.declPtr(data.decl_index).renderFullyQualifiedName(data.mod, writer); |
| 933 | } |
| 934 | fn fmtDecl(func: *Func, decl_index: InternPool.DeclIndex) std.fmt.Formatter(formatDecl) { |
| 935 | return .{ .data = .{ |
| 936 | .mod = func.bin_file.comp.module.?, |
| 937 | .decl_index = decl_index, |
| 938 | } }; |
| 939 | } |
| 940 | |
| 941 | const FormatAirData = struct { |
| 942 | func: *Func, |
| 943 | inst: Air.Inst.Index, |
| 944 | }; |
| 945 | fn formatAir( |
| 946 | data: FormatAirData, |
| 947 | comptime _: []const u8, |
| 948 | _: std.fmt.FormatOptions, |
| 949 | writer: anytype, |
| 950 | ) @TypeOf(writer).Error!void { |
| 951 | @import("../../print_air.zig").dumpInst( |
| 952 | data.inst, |
| 953 | data.func.bin_file.comp.module.?, |
| 954 | data.func.air, |
| 955 | data.func.liveness, |
| 956 | ); |
| 957 | } |
| 958 | fn fmtAir(func: *Func, inst: Air.Inst.Index) std.fmt.Formatter(formatAir) { |
| 959 | return .{ .data = .{ .func = func, .inst = inst } }; |
| 960 | } |
| 872 | 961 | |
| 873 | | const result_index: Mir.Inst.Index = @intCast(self.mir_instructions.len); |
| 874 | | self.mir_instructions.appendAssumeCapacity(inst); |
| 962 | const FormatTrackingData = struct { |
| 963 | func: *Func, |
| 964 | }; |
| 965 | fn formatTracking( |
| 966 | data: FormatTrackingData, |
| 967 | comptime _: []const u8, |
| 968 | _: std.fmt.FormatOptions, |
| 969 | writer: anytype, |
| 970 | ) @TypeOf(writer).Error!void { |
| 971 | var it = data.func.inst_tracking.iterator(); |
| 972 | while (it.next()) |entry| try writer.print("\n%{d} = {}", .{ entry.key_ptr.*, entry.value_ptr.* }); |
| 973 | } |
| 974 | fn fmtTracking(func: *Func) std.fmt.Formatter(formatTracking) { |
| 975 | return .{ .data = .{ .func = func } }; |
| 976 | } |
| 977 | |
| 978 | fn addInst(func: *Func, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index { |
| 979 | const gpa = func.gpa; |
| 980 | try func.mir_instructions.ensureUnusedCapacity(gpa, 1); |
| 981 | const result_index: Mir.Inst.Index = @intCast(func.mir_instructions.len); |
| 982 | func.mir_instructions.appendAssumeCapacity(inst); |
| 983 | if (inst.tag != .pseudo or switch (inst.ops) { |
| 984 | else => true, |
| 985 | .pseudo_dbg_prologue_end, |
| 986 | .pseudo_dbg_line_column, |
| 987 | .pseudo_dbg_epilogue_begin, |
| 988 | .pseudo_store_rm, |
| 989 | .pseudo_load_rm, |
| 990 | .pseudo_lea_rm, |
| 991 | .pseudo_mv, |
| 992 | .pseudo_dead, |
| 993 | => false, |
| 994 | }) wip_mir_log.debug("{}", .{func.fmtWipMir(result_index)}) else wip_mir_log.debug(" | uses-mem", .{}); |
| 875 | 995 | return result_index; |
| 876 | 996 | } |
| 877 | 997 | |
| 878 | | fn addNop(self: *Self) error{OutOfMemory}!Mir.Inst.Index { |
| 879 | | return self.addInst(.{ |
| 998 | fn addNop(func: *Func) error{OutOfMemory}!Mir.Inst.Index { |
| 999 | return func.addInst(.{ |
| 880 | 1000 | .tag = .nop, |
| 881 | 1001 | .ops = .none, |
| 882 | 1002 | .data = undefined, |
| 883 | 1003 | }); |
| 884 | 1004 | } |
| 885 | 1005 | |
| 886 | | fn addPseudoNone(self: *Self, ops: Mir.Inst.Ops) !void { |
| 887 | | _ = try self.addInst(.{ |
| 1006 | fn addPseudoNone(func: *Func, ops: Mir.Inst.Ops) !void { |
| 1007 | _ = try func.addInst(.{ |
| 888 | 1008 | .tag = .pseudo, |
| 889 | 1009 | .ops = ops, |
| 890 | 1010 | .data = undefined, |
| 891 | 1011 | }); |
| 892 | 1012 | } |
| 893 | 1013 | |
| 894 | | fn addPseudo(self: *Self, ops: Mir.Inst.Ops) !Mir.Inst.Index { |
| 895 | | return self.addInst(.{ |
| 1014 | fn addPseudo(func: *Func, ops: Mir.Inst.Ops) !Mir.Inst.Index { |
| 1015 | return func.addInst(.{ |
| 896 | 1016 | .tag = .pseudo, |
| 897 | 1017 | .ops = ops, |
| 898 | 1018 | .data = undefined, |
| 899 | 1019 | }); |
| 900 | 1020 | } |
| 901 | 1021 | |
| 902 | | pub fn addExtra(self: *Self, extra: anytype) Allocator.Error!u32 { |
| 1022 | pub fn addExtra(func: *Func, extra: anytype) Allocator.Error!u32 { |
| 903 | 1023 | const fields = std.meta.fields(@TypeOf(extra)); |
| 904 | | try self.mir_extra.ensureUnusedCapacity(self.gpa, fields.len); |
| 905 | | return self.addExtraAssumeCapacity(extra); |
| 1024 | try func.mir_extra.ensureUnusedCapacity(func.gpa, fields.len); |
| 1025 | return func.addExtraAssumeCapacity(extra); |
| 906 | 1026 | } |
| 907 | 1027 | |
| 908 | | pub fn addExtraAssumeCapacity(self: *Self, extra: anytype) u32 { |
| 1028 | pub fn addExtraAssumeCapacity(func: *Func, extra: anytype) u32 { |
| 909 | 1029 | const fields = std.meta.fields(@TypeOf(extra)); |
| 910 | | const result: u32 = @intCast(self.mir_extra.items.len); |
| 1030 | const result: u32 = @intCast(func.mir_extra.items.len); |
| 911 | 1031 | inline for (fields) |field| { |
| 912 | | self.mir_extra.appendAssumeCapacity(switch (field.type) { |
| 1032 | func.mir_extra.appendAssumeCapacity(switch (field.type) { |
| 913 | 1033 | u32 => @field(extra, field.name), |
| 914 | 1034 | i32 => @bitCast(@field(extra, field.name)), |
| 915 | 1035 | else => @compileError("bad field type"), |
| ... | ... | @@ -918,38 +1038,71 @@ pub fn addExtraAssumeCapacity(self: *Self, extra: anytype) u32 { |
| 918 | 1038 | return result; |
| 919 | 1039 | } |
| 920 | 1040 | |
| 921 | | fn gen(self: *Self) !void { |
| 922 | | const mod = self.bin_file.comp.module.?; |
| 923 | | const fn_info = mod.typeToFunc(self.fn_type).?; |
| 1041 | const required_features = [_]Target.riscv.Feature{ |
| 1042 | .d, |
| 1043 | .m, |
| 1044 | }; |
| 1045 | |
| 1046 | fn gen(func: *Func) !void { |
| 1047 | const mod = func.bin_file.comp.module.?; |
| 1048 | const fn_info = mod.typeToFunc(func.fn_type).?; |
| 924 | 1049 | |
| 925 | | if (fn_info.cc != .Naked) { |
| 926 | | try self.addPseudoNone(.pseudo_dbg_prologue_end); |
| 1050 | inline for (required_features) |feature| { |
| 1051 | if (!func.hasFeature(feature)) { |
| 1052 | return func.fail( |
| 1053 | "target missing required feature {s}", |
| 1054 | .{@tagName(feature)}, |
| 1055 | ); |
| 1056 | } |
| 1057 | } |
| 927 | 1058 | |
| 928 | | const backpatch_stack_alloc = try self.addPseudo(.pseudo_dead); |
| 929 | | const backpatch_ra_spill = try self.addPseudo(.pseudo_dead); |
| 930 | | const backpatch_fp_spill = try self.addPseudo(.pseudo_dead); |
| 931 | | const backpatch_fp_add = try self.addPseudo(.pseudo_dead); |
| 932 | | const backpatch_spill_callee_preserved_regs = try self.addPseudo(.pseudo_dead); |
| 1059 | if (fn_info.cc != .Naked) { |
| 1060 | try func.addPseudoNone(.pseudo_dbg_prologue_end); |
| 1061 | |
| 1062 | const backpatch_stack_alloc = try func.addPseudo(.pseudo_dead); |
| 1063 | const backpatch_ra_spill = try func.addPseudo(.pseudo_dead); |
| 1064 | const backpatch_fp_spill = try func.addPseudo(.pseudo_dead); |
| 1065 | const backpatch_fp_add = try func.addPseudo(.pseudo_dead); |
| 1066 | const backpatch_spill_callee_preserved_regs = try func.addPseudo(.pseudo_dead); |
| 1067 | |
| 1068 | switch (func.ret_mcv.long) { |
| 1069 | .none, .unreach => {}, |
| 1070 | .indirect => { |
| 1071 | // The address where to store the return value for the caller is in a |
| 1072 | // register which the callee is free to clobber. Therefore, we purposely |
| 1073 | // spill it to stack immediately. |
| 1074 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(Type.usize, mod)); |
| 1075 | try func.genSetMem( |
| 1076 | .{ .frame = frame_index }, |
| 1077 | 0, |
| 1078 | Type.usize, |
| 1079 | func.ret_mcv.long.address().offset(-func.ret_mcv.short.indirect.off), |
| 1080 | ); |
| 1081 | func.ret_mcv.long = .{ .load_frame = .{ .index = frame_index } }; |
| 1082 | tracking_log.debug("spill {} to {}", .{ func.ret_mcv.long, frame_index }); |
| 1083 | }, |
| 1084 | else => unreachable, |
| 1085 | } |
| 933 | 1086 | |
| 934 | | try self.genBody(self.air.getMainBody()); |
| 1087 | try func.genBody(func.air.getMainBody()); |
| 935 | 1088 | |
| 936 | | for (self.exitlude_jump_relocs.items) |jmp_reloc| { |
| 937 | | self.mir_instructions.items(.data)[jmp_reloc].inst = |
| 938 | | @intCast(self.mir_instructions.len); |
| 1089 | for (func.exitlude_jump_relocs.items) |jmp_reloc| { |
| 1090 | func.mir_instructions.items(.data)[jmp_reloc].inst = |
| 1091 | @intCast(func.mir_instructions.len); |
| 939 | 1092 | } |
| 940 | 1093 | |
| 941 | | try self.addPseudoNone(.pseudo_dbg_epilogue_begin); |
| 1094 | try func.addPseudoNone(.pseudo_dbg_epilogue_begin); |
| 942 | 1095 | |
| 943 | | const backpatch_restore_callee_preserved_regs = try self.addPseudo(.pseudo_dead); |
| 944 | | const backpatch_ra_restore = try self.addPseudo(.pseudo_dead); |
| 945 | | const backpatch_fp_restore = try self.addPseudo(.pseudo_dead); |
| 946 | | const backpatch_stack_alloc_restore = try self.addPseudo(.pseudo_dead); |
| 947 | | try self.addPseudoNone(.pseudo_ret); |
| 1096 | const backpatch_restore_callee_preserved_regs = try func.addPseudo(.pseudo_dead); |
| 1097 | const backpatch_ra_restore = try func.addPseudo(.pseudo_dead); |
| 1098 | const backpatch_fp_restore = try func.addPseudo(.pseudo_dead); |
| 1099 | const backpatch_stack_alloc_restore = try func.addPseudo(.pseudo_dead); |
| 1100 | try func.addPseudoNone(.pseudo_ret); |
| 948 | 1101 | |
| 949 | | const frame_layout = try self.computeFrameLayout(); |
| 1102 | const frame_layout = try func.computeFrameLayout(); |
| 950 | 1103 | const need_save_reg = frame_layout.save_reg_list.count() > 0; |
| 951 | 1104 | |
| 952 | | self.mir_instructions.set(backpatch_stack_alloc, .{ |
| 1105 | func.mir_instructions.set(backpatch_stack_alloc, .{ |
| 953 | 1106 | .tag = .addi, |
| 954 | 1107 | .ops = .rri, |
| 955 | 1108 | .data = .{ .i_type = .{ |
| ... | ... | @@ -958,51 +1111,51 @@ fn gen(self: *Self) !void { |
| 958 | 1111 | .imm12 = Immediate.s(-@as(i32, @intCast(frame_layout.stack_adjust))), |
| 959 | 1112 | } }, |
| 960 | 1113 | }); |
| 961 | | self.mir_instructions.set(backpatch_ra_spill, .{ |
| 1114 | func.mir_instructions.set(backpatch_ra_spill, .{ |
| 962 | 1115 | .tag = .pseudo, |
| 963 | 1116 | .ops = .pseudo_store_rm, |
| 964 | 1117 | .data = .{ .rm = .{ |
| 965 | 1118 | .r = .ra, |
| 966 | 1119 | .m = .{ |
| 967 | 1120 | .base = .{ .frame = .ret_addr }, |
| 968 | | .mod = .{ .rm = .{ .size = .dword } }, |
| 1121 | .mod = .{ .size = .dword, .unsigned = false }, |
| 969 | 1122 | }, |
| 970 | 1123 | } }, |
| 971 | 1124 | }); |
| 972 | | self.mir_instructions.set(backpatch_ra_restore, .{ |
| 1125 | func.mir_instructions.set(backpatch_ra_restore, .{ |
| 973 | 1126 | .tag = .pseudo, |
| 974 | 1127 | .ops = .pseudo_load_rm, |
| 975 | 1128 | .data = .{ .rm = .{ |
| 976 | 1129 | .r = .ra, |
| 977 | 1130 | .m = .{ |
| 978 | 1131 | .base = .{ .frame = .ret_addr }, |
| 979 | | .mod = .{ .rm = .{ .size = .dword } }, |
| 1132 | .mod = .{ .size = .dword, .unsigned = false }, |
| 980 | 1133 | }, |
| 981 | 1134 | } }, |
| 982 | 1135 | }); |
| 983 | | self.mir_instructions.set(backpatch_fp_spill, .{ |
| 1136 | func.mir_instructions.set(backpatch_fp_spill, .{ |
| 984 | 1137 | .tag = .pseudo, |
| 985 | 1138 | .ops = .pseudo_store_rm, |
| 986 | 1139 | .data = .{ .rm = .{ |
| 987 | 1140 | .r = .s0, |
| 988 | 1141 | .m = .{ |
| 989 | 1142 | .base = .{ .frame = .base_ptr }, |
| 990 | | .mod = .{ .rm = .{ .size = .dword } }, |
| 1143 | .mod = .{ .size = .dword, .unsigned = false }, |
| 991 | 1144 | }, |
| 992 | 1145 | } }, |
| 993 | 1146 | }); |
| 994 | | self.mir_instructions.set(backpatch_fp_restore, .{ |
| 1147 | func.mir_instructions.set(backpatch_fp_restore, .{ |
| 995 | 1148 | .tag = .pseudo, |
| 996 | 1149 | .ops = .pseudo_load_rm, |
| 997 | 1150 | .data = .{ .rm = .{ |
| 998 | 1151 | .r = .s0, |
| 999 | 1152 | .m = .{ |
| 1000 | 1153 | .base = .{ .frame = .base_ptr }, |
| 1001 | | .mod = .{ .rm = .{ .size = .dword } }, |
| 1154 | .mod = .{ .size = .dword, .unsigned = false }, |
| 1002 | 1155 | }, |
| 1003 | 1156 | } }, |
| 1004 | 1157 | }); |
| 1005 | | self.mir_instructions.set(backpatch_fp_add, .{ |
| 1158 | func.mir_instructions.set(backpatch_fp_add, .{ |
| 1006 | 1159 | .tag = .addi, |
| 1007 | 1160 | .ops = .rri, |
| 1008 | 1161 | .data = .{ .i_type = .{ |
| ... | ... | @@ -1011,7 +1164,7 @@ fn gen(self: *Self) !void { |
| 1011 | 1164 | .imm12 = Immediate.s(@intCast(frame_layout.stack_adjust)), |
| 1012 | 1165 | } }, |
| 1013 | 1166 | }); |
| 1014 | | self.mir_instructions.set(backpatch_stack_alloc_restore, .{ |
| 1167 | func.mir_instructions.set(backpatch_stack_alloc_restore, .{ |
| 1015 | 1168 | .tag = .addi, |
| 1016 | 1169 | .ops = .rri, |
| 1017 | 1170 | .data = .{ .i_type = .{ |
| ... | ... | @@ -1022,72 +1175,83 @@ fn gen(self: *Self) !void { |
| 1022 | 1175 | }); |
| 1023 | 1176 | |
| 1024 | 1177 | if (need_save_reg) { |
| 1025 | | self.mir_instructions.set(backpatch_spill_callee_preserved_regs, .{ |
| 1178 | func.mir_instructions.set(backpatch_spill_callee_preserved_regs, .{ |
| 1026 | 1179 | .tag = .pseudo, |
| 1027 | 1180 | .ops = .pseudo_spill_regs, |
| 1028 | 1181 | .data = .{ .reg_list = frame_layout.save_reg_list }, |
| 1029 | 1182 | }); |
| 1030 | 1183 | |
| 1031 | | self.mir_instructions.set(backpatch_restore_callee_preserved_regs, .{ |
| 1184 | func.mir_instructions.set(backpatch_restore_callee_preserved_regs, .{ |
| 1032 | 1185 | .tag = .pseudo, |
| 1033 | 1186 | .ops = .pseudo_restore_regs, |
| 1034 | 1187 | .data = .{ .reg_list = frame_layout.save_reg_list }, |
| 1035 | 1188 | }); |
| 1036 | 1189 | } |
| 1037 | 1190 | } else { |
| 1038 | | try self.addPseudoNone(.pseudo_dbg_prologue_end); |
| 1039 | | try self.genBody(self.air.getMainBody()); |
| 1040 | | try self.addPseudoNone(.pseudo_dbg_epilogue_begin); |
| 1191 | try func.addPseudoNone(.pseudo_dbg_prologue_end); |
| 1192 | try func.genBody(func.air.getMainBody()); |
| 1193 | try func.addPseudoNone(.pseudo_dbg_epilogue_begin); |
| 1041 | 1194 | } |
| 1042 | 1195 | |
| 1043 | 1196 | // Drop them off at the rbrace. |
| 1044 | | _ = try self.addInst(.{ |
| 1197 | _ = try func.addInst(.{ |
| 1045 | 1198 | .tag = .pseudo, |
| 1046 | 1199 | .ops = .pseudo_dbg_line_column, |
| 1047 | 1200 | .data = .{ .pseudo_dbg_line_column = .{ |
| 1048 | | .line = self.end_di_line, |
| 1049 | | .column = self.end_di_column, |
| 1201 | .line = func.end_di_line, |
| 1202 | .column = func.end_di_column, |
| 1050 | 1203 | } }, |
| 1051 | 1204 | }); |
| 1052 | 1205 | } |
| 1053 | 1206 | |
| 1054 | | fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1055 | | const zcu = self.bin_file.comp.module.?; |
| 1207 | fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void { |
| 1208 | const zcu = func.bin_file.comp.module.?; |
| 1056 | 1209 | const ip = &zcu.intern_pool; |
| 1057 | | const air_tags = self.air.instructions.items(.tag); |
| 1210 | const air_tags = func.air.instructions.items(.tag); |
| 1058 | 1211 | |
| 1059 | 1212 | for (body) |inst| { |
| 1060 | | if (self.liveness.isUnused(inst) and !self.air.mustLower(inst, ip)) continue; |
| 1061 | | |
| 1062 | | const old_air_bookkeeping = self.air_bookkeeping; |
| 1063 | | try self.inst_tracking.ensureUnusedCapacity(self.gpa, 1); |
| 1064 | | switch (air_tags[@intFromEnum(inst)]) { |
| 1213 | if (func.liveness.isUnused(inst) and !func.air.mustLower(inst, ip)) continue; |
| 1214 | wip_mir_log.debug("{}", .{func.fmtAir(inst)}); |
| 1215 | verbose_tracking_log.debug("{}", .{func.fmtTracking()}); |
| 1216 | |
| 1217 | const old_air_bookkeeping = func.air_bookkeeping; |
| 1218 | try func.inst_tracking.ensureUnusedCapacity(func.gpa, 1); |
| 1219 | const tag: Air.Inst.Tag = air_tags[@intFromEnum(inst)]; |
| 1220 | switch (tag) { |
| 1065 | 1221 | // zig fmt: off |
| 1066 | | .ptr_add => try self.airPtrArithmetic(inst, .ptr_add), |
| 1067 | | .ptr_sub => try self.airPtrArithmetic(inst, .ptr_sub), |
| 1222 | .add, |
| 1223 | .add_wrap, |
| 1224 | .sub, |
| 1225 | .sub_wrap, |
| 1068 | 1226 | |
| 1069 | | .add => try self.airBinOp(inst, .add), |
| 1070 | | .sub => try self.airBinOp(inst, .sub), |
| 1227 | .mul, |
| 1228 | .mul_wrap, |
| 1229 | .div_trunc, |
| 1071 | 1230 | |
| 1072 | | .add_safe, |
| 1073 | | .sub_safe, |
| 1074 | | .mul_safe, |
| 1075 | | => return self.fail("TODO implement safety_checked_instructions", .{}), |
| 1076 | | |
| 1077 | | .add_wrap => try self.airAddWrap(inst), |
| 1078 | | .add_sat => try self.airAddSat(inst), |
| 1079 | | .sub_wrap => try self.airSubWrap(inst), |
| 1080 | | .sub_sat => try self.airSubSat(inst), |
| 1081 | | .mul => try self.airMul(inst), |
| 1082 | | .mul_wrap => try self.airMulWrap(inst), |
| 1083 | | .mul_sat => try self.airMulSat(inst), |
| 1084 | | .rem => try self.airRem(inst), |
| 1085 | | .mod => try self.airMod(inst), |
| 1086 | | .shl, .shl_exact => try self.airShl(inst), |
| 1087 | | .shl_sat => try self.airShlSat(inst), |
| 1088 | | .min => try self.airMinMax(inst, .min), |
| 1089 | | .max => try self.airMinMax(inst, .max), |
| 1090 | | .slice => try self.airSlice(inst), |
| 1231 | .shl, .shl_exact, |
| 1232 | .shr, .shr_exact, |
| 1233 | |
| 1234 | .bool_and, |
| 1235 | .bool_or, |
| 1236 | .bit_and, |
| 1237 | .bit_or, |
| 1238 | |
| 1239 | .xor, |
| 1240 | |
| 1241 | .min, |
| 1242 | .max, |
| 1243 | => try func.airBinOp(inst, tag), |
| 1244 | |
| 1245 | |
| 1246 | .ptr_add, |
| 1247 | .ptr_sub => try func.airPtrArithmetic(inst, tag), |
| 1248 | |
| 1249 | .rem, |
| 1250 | .mod, |
| 1251 | .div_float, |
| 1252 | .div_floor, |
| 1253 | .div_exact, |
| 1254 | => return func.fail("TODO: {s}", .{@tagName(tag)}), |
| 1091 | 1255 | |
| 1092 | 1256 | .sqrt, |
| 1093 | 1257 | .sin, |
| ... | ... | @@ -1103,157 +1267,164 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1103 | 1267 | .round, |
| 1104 | 1268 | .trunc_float, |
| 1105 | 1269 | .neg, |
| 1106 | | => try self.airUnaryMath(inst), |
| 1107 | | |
| 1108 | | .add_with_overflow => try self.airAddWithOverflow(inst), |
| 1109 | | .sub_with_overflow => try self.airSubWithOverflow(inst), |
| 1110 | | .mul_with_overflow => try self.airMulWithOverflow(inst), |
| 1111 | | .shl_with_overflow => try self.airShlWithOverflow(inst), |
| 1112 | | |
| 1113 | | .div_float, .div_trunc, .div_floor, .div_exact => try self.airDiv(inst), |
| 1114 | | |
| 1115 | | .cmp_lt => try self.airCmp(inst), |
| 1116 | | .cmp_lte => try self.airCmp(inst), |
| 1117 | | .cmp_eq => try self.airCmp(inst), |
| 1118 | | .cmp_gte => try self.airCmp(inst), |
| 1119 | | .cmp_gt => try self.airCmp(inst), |
| 1120 | | .cmp_neq => try self.airCmp(inst), |
| 1121 | | |
| 1122 | | .cmp_vector => try self.airCmpVector(inst), |
| 1123 | | .cmp_lt_errors_len => try self.airCmpLtErrorsLen(inst), |
| 1124 | | |
| 1125 | | .bool_and => try self.airBoolOp(inst), |
| 1126 | | .bool_or => try self.airBoolOp(inst), |
| 1127 | | .bit_and => try self.airBitAnd(inst), |
| 1128 | | .bit_or => try self.airBitOr(inst), |
| 1129 | | .xor => try self.airXor(inst), |
| 1130 | | .shr, .shr_exact => try self.airShr(inst), |
| 1131 | | |
| 1132 | | .alloc => try self.airAlloc(inst), |
| 1133 | | .ret_ptr => try self.airRetPtr(inst), |
| 1134 | | .arg => try self.airArg(inst), |
| 1135 | | .assembly => try self.airAsm(inst), |
| 1136 | | .bitcast => try self.airBitCast(inst), |
| 1137 | | .block => try self.airBlock(inst), |
| 1138 | | .br => try self.airBr(inst), |
| 1139 | | .trap => try self.airTrap(), |
| 1140 | | .breakpoint => try self.airBreakpoint(), |
| 1141 | | .ret_addr => try self.airRetAddr(inst), |
| 1142 | | .frame_addr => try self.airFrameAddress(inst), |
| 1143 | | .fence => try self.airFence(), |
| 1144 | | .cond_br => try self.airCondBr(inst), |
| 1145 | | .dbg_stmt => try self.airDbgStmt(inst), |
| 1146 | | .fptrunc => try self.airFptrunc(inst), |
| 1147 | | .fpext => try self.airFpext(inst), |
| 1148 | | .intcast => try self.airIntCast(inst), |
| 1149 | | .trunc => try self.airTrunc(inst), |
| 1150 | | .int_from_bool => try self.airIntFromBool(inst), |
| 1151 | | .is_non_null => try self.airIsNonNull(inst), |
| 1152 | | .is_non_null_ptr => try self.airIsNonNullPtr(inst), |
| 1153 | | .is_null => try self.airIsNull(inst), |
| 1154 | | .is_null_ptr => try self.airIsNullPtr(inst), |
| 1155 | | .is_non_err => try self.airIsNonErr(inst), |
| 1156 | | .is_non_err_ptr => try self.airIsNonErrPtr(inst), |
| 1157 | | .is_err => try self.airIsErr(inst), |
| 1158 | | .is_err_ptr => try self.airIsErrPtr(inst), |
| 1159 | | .load => try self.airLoad(inst), |
| 1160 | | .loop => try self.airLoop(inst), |
| 1161 | | .not => try self.airNot(inst), |
| 1162 | | .int_from_ptr => try self.airIntFromPtr(inst), |
| 1163 | | .ret => try self.airRet(inst, false), |
| 1164 | | .ret_safe => try self.airRet(inst, true), |
| 1165 | | .ret_load => try self.airRetLoad(inst), |
| 1166 | | .store => try self.airStore(inst, false), |
| 1167 | | .store_safe => try self.airStore(inst, true), |
| 1168 | | .struct_field_ptr=> try self.airStructFieldPtr(inst), |
| 1169 | | .struct_field_val=> try self.airStructFieldVal(inst), |
| 1170 | | .array_to_slice => try self.airArrayToSlice(inst), |
| 1171 | | .float_from_int => try self.airFloatFromInt(inst), |
| 1172 | | .int_from_float => try self.airIntFromFloat(inst), |
| 1173 | | .cmpxchg_strong => try self.airCmpxchg(inst), |
| 1174 | | .cmpxchg_weak => try self.airCmpxchg(inst), |
| 1175 | | .atomic_rmw => try self.airAtomicRmw(inst), |
| 1176 | | .atomic_load => try self.airAtomicLoad(inst), |
| 1177 | | .memcpy => try self.airMemcpy(inst), |
| 1178 | | .memset => try self.airMemset(inst, false), |
| 1179 | | .memset_safe => try self.airMemset(inst, true), |
| 1180 | | .set_union_tag => try self.airSetUnionTag(inst), |
| 1181 | | .get_union_tag => try self.airGetUnionTag(inst), |
| 1182 | | .clz => try self.airClz(inst), |
| 1183 | | .ctz => try self.airCtz(inst), |
| 1184 | | .popcount => try self.airPopcount(inst), |
| 1185 | | .abs => try self.airAbs(inst), |
| 1186 | | .byte_swap => try self.airByteSwap(inst), |
| 1187 | | .bit_reverse => try self.airBitReverse(inst), |
| 1188 | | .tag_name => try self.airTagName(inst), |
| 1189 | | .error_name => try self.airErrorName(inst), |
| 1190 | | .splat => try self.airSplat(inst), |
| 1191 | | .select => try self.airSelect(inst), |
| 1192 | | .shuffle => try self.airShuffle(inst), |
| 1193 | | .reduce => try self.airReduce(inst), |
| 1194 | | .aggregate_init => try self.airAggregateInit(inst), |
| 1195 | | .union_init => try self.airUnionInit(inst), |
| 1196 | | .prefetch => try self.airPrefetch(inst), |
| 1197 | | .mul_add => try self.airMulAdd(inst), |
| 1198 | | .addrspace_cast => return self.fail("TODO: addrspace_cast", .{}), |
| 1199 | | |
| 1200 | | .@"try" => try self.airTry(inst), |
| 1201 | | .try_ptr => return self.fail("TODO: try_ptr", .{}), |
| 1270 | => try func.airUnaryMath(inst), |
| 1271 | |
| 1272 | .add_with_overflow => try func.airAddWithOverflow(inst), |
| 1273 | .sub_with_overflow => try func.airSubWithOverflow(inst), |
| 1274 | .mul_with_overflow => try func.airMulWithOverflow(inst), |
| 1275 | .shl_with_overflow => try func.airShlWithOverflow(inst), |
| 1276 | |
| 1277 | |
| 1278 | .add_sat => try func.airAddSat(inst), |
| 1279 | .sub_sat => try func.airSubSat(inst), |
| 1280 | .mul_sat => try func.airMulSat(inst), |
| 1281 | .shl_sat => try func.airShlSat(inst), |
| 1282 | |
| 1283 | .add_safe, |
| 1284 | .sub_safe, |
| 1285 | .mul_safe, |
| 1286 | => return func.fail("TODO implement safety_checked_instructions", .{}), |
| 1287 | |
| 1288 | .cmp_lt, |
| 1289 | .cmp_lte, |
| 1290 | .cmp_eq, |
| 1291 | .cmp_gte, |
| 1292 | .cmp_gt, |
| 1293 | .cmp_neq, |
| 1294 | => try func.airCmp(inst, tag), |
| 1295 | |
| 1296 | .cmp_vector => try func.airCmpVector(inst), |
| 1297 | .cmp_lt_errors_len => try func.airCmpLtErrorsLen(inst), |
| 1298 | |
| 1299 | .slice => try func.airSlice(inst), |
| 1300 | .array_to_slice => try func.airArrayToSlice(inst), |
| 1301 | |
| 1302 | .slice_ptr => try func.airSlicePtr(inst), |
| 1303 | .slice_len => try func.airSliceLen(inst), |
| 1304 | |
| 1305 | .alloc => try func.airAlloc(inst), |
| 1306 | .ret_ptr => try func.airRetPtr(inst), |
| 1307 | .arg => try func.airArg(inst), |
| 1308 | .assembly => try func.airAsm(inst), |
| 1309 | .bitcast => try func.airBitCast(inst), |
| 1310 | .block => try func.airBlock(inst), |
| 1311 | .br => try func.airBr(inst), |
| 1312 | .trap => try func.airTrap(), |
| 1313 | .breakpoint => try func.airBreakpoint(), |
| 1314 | .ret_addr => try func.airRetAddr(inst), |
| 1315 | .frame_addr => try func.airFrameAddress(inst), |
| 1316 | .fence => try func.airFence(), |
| 1317 | .cond_br => try func.airCondBr(inst), |
| 1318 | .dbg_stmt => try func.airDbgStmt(inst), |
| 1319 | .fptrunc => try func.airFptrunc(inst), |
| 1320 | .fpext => try func.airFpext(inst), |
| 1321 | .intcast => try func.airIntCast(inst), |
| 1322 | .trunc => try func.airTrunc(inst), |
| 1323 | .int_from_bool => try func.airIntFromBool(inst), |
| 1324 | .is_non_null => try func.airIsNonNull(inst), |
| 1325 | .is_non_null_ptr => try func.airIsNonNullPtr(inst), |
| 1326 | .is_null => try func.airIsNull(inst), |
| 1327 | .is_null_ptr => try func.airIsNullPtr(inst), |
| 1328 | .is_non_err => try func.airIsNonErr(inst), |
| 1329 | .is_non_err_ptr => try func.airIsNonErrPtr(inst), |
| 1330 | .is_err => try func.airIsErr(inst), |
| 1331 | .is_err_ptr => try func.airIsErrPtr(inst), |
| 1332 | .load => try func.airLoad(inst), |
| 1333 | .loop => try func.airLoop(inst), |
| 1334 | .not => try func.airNot(inst), |
| 1335 | .int_from_ptr => try func.airIntFromPtr(inst), |
| 1336 | .ret => try func.airRet(inst, false), |
| 1337 | .ret_safe => try func.airRet(inst, true), |
| 1338 | .ret_load => try func.airRetLoad(inst), |
| 1339 | .store => try func.airStore(inst, false), |
| 1340 | .store_safe => try func.airStore(inst, true), |
| 1341 | .struct_field_ptr=> try func.airStructFieldPtr(inst), |
| 1342 | .struct_field_val=> try func.airStructFieldVal(inst), |
| 1343 | .float_from_int => try func.airFloatFromInt(inst), |
| 1344 | .int_from_float => try func.airIntFromFloat(inst), |
| 1345 | .cmpxchg_strong => try func.airCmpxchg(inst), |
| 1346 | .cmpxchg_weak => try func.airCmpxchg(inst), |
| 1347 | .atomic_rmw => try func.airAtomicRmw(inst), |
| 1348 | .atomic_load => try func.airAtomicLoad(inst), |
| 1349 | .memcpy => try func.airMemcpy(inst), |
| 1350 | .memset => try func.airMemset(inst, false), |
| 1351 | .memset_safe => try func.airMemset(inst, true), |
| 1352 | .set_union_tag => try func.airSetUnionTag(inst), |
| 1353 | .get_union_tag => try func.airGetUnionTag(inst), |
| 1354 | .clz => try func.airClz(inst), |
| 1355 | .ctz => try func.airCtz(inst), |
| 1356 | .popcount => try func.airPopcount(inst), |
| 1357 | .abs => try func.airAbs(inst), |
| 1358 | .byte_swap => try func.airByteSwap(inst), |
| 1359 | .bit_reverse => try func.airBitReverse(inst), |
| 1360 | .tag_name => try func.airTagName(inst), |
| 1361 | .error_name => try func.airErrorName(inst), |
| 1362 | .splat => try func.airSplat(inst), |
| 1363 | .select => try func.airSelect(inst), |
| 1364 | .shuffle => try func.airShuffle(inst), |
| 1365 | .reduce => try func.airReduce(inst), |
| 1366 | .aggregate_init => try func.airAggregateInit(inst), |
| 1367 | .union_init => try func.airUnionInit(inst), |
| 1368 | .prefetch => try func.airPrefetch(inst), |
| 1369 | .mul_add => try func.airMulAdd(inst), |
| 1370 | .addrspace_cast => return func.fail("TODO: addrspace_cast", .{}), |
| 1371 | |
| 1372 | .@"try" => try func.airTry(inst), |
| 1373 | .try_ptr => return func.fail("TODO: try_ptr", .{}), |
| 1202 | 1374 | |
| 1203 | 1375 | .dbg_var_ptr, |
| 1204 | 1376 | .dbg_var_val, |
| 1205 | | => try self.airDbgVar(inst), |
| 1377 | => try func.airDbgVar(inst), |
| 1206 | 1378 | |
| 1207 | | .dbg_inline_block => try self.airDbgInlineBlock(inst), |
| 1379 | .dbg_inline_block => try func.airDbgInlineBlock(inst), |
| 1208 | 1380 | |
| 1209 | | .call => try self.airCall(inst, .auto), |
| 1210 | | .call_always_tail => try self.airCall(inst, .always_tail), |
| 1211 | | .call_never_tail => try self.airCall(inst, .never_tail), |
| 1212 | | .call_never_inline => try self.airCall(inst, .never_inline), |
| 1381 | .call => try func.airCall(inst, .auto), |
| 1382 | .call_always_tail => try func.airCall(inst, .always_tail), |
| 1383 | .call_never_tail => try func.airCall(inst, .never_tail), |
| 1384 | .call_never_inline => try func.airCall(inst, .never_inline), |
| 1213 | 1385 | |
| 1214 | | .atomic_store_unordered => try self.airAtomicStore(inst, .unordered), |
| 1215 | | .atomic_store_monotonic => try self.airAtomicStore(inst, .monotonic), |
| 1216 | | .atomic_store_release => try self.airAtomicStore(inst, .release), |
| 1217 | | .atomic_store_seq_cst => try self.airAtomicStore(inst, .seq_cst), |
| 1386 | .atomic_store_unordered => try func.airAtomicStore(inst, .unordered), |
| 1387 | .atomic_store_monotonic => try func.airAtomicStore(inst, .monotonic), |
| 1388 | .atomic_store_release => try func.airAtomicStore(inst, .release), |
| 1389 | .atomic_store_seq_cst => try func.airAtomicStore(inst, .seq_cst), |
| 1390 | .struct_field_ptr_index_0 => try func.airStructFieldPtrIndex(inst, 0), |
| 1391 | .struct_field_ptr_index_1 => try func.airStructFieldPtrIndex(inst, 1), |
| 1392 | .struct_field_ptr_index_2 => try func.airStructFieldPtrIndex(inst, 2), |
| 1393 | .struct_field_ptr_index_3 => try func.airStructFieldPtrIndex(inst, 3), |
| 1218 | 1394 | |
| 1219 | | .struct_field_ptr_index_0 => try self.airStructFieldPtrIndex(inst, 0), |
| 1220 | | .struct_field_ptr_index_1 => try self.airStructFieldPtrIndex(inst, 1), |
| 1221 | | .struct_field_ptr_index_2 => try self.airStructFieldPtrIndex(inst, 2), |
| 1222 | | .struct_field_ptr_index_3 => try self.airStructFieldPtrIndex(inst, 3), |
| 1395 | .field_parent_ptr => try func.airFieldParentPtr(inst), |
| 1223 | 1396 | |
| 1224 | | .field_parent_ptr => try self.airFieldParentPtr(inst), |
| 1397 | .switch_br => try func.airSwitchBr(inst), |
| 1225 | 1398 | |
| 1226 | | .switch_br => try self.airSwitch(inst), |
| 1227 | | .slice_ptr => try self.airSlicePtr(inst), |
| 1228 | | .slice_len => try self.airSliceLen(inst), |
| 1399 | .ptr_slice_len_ptr => try func.airPtrSliceLenPtr(inst), |
| 1400 | .ptr_slice_ptr_ptr => try func.airPtrSlicePtrPtr(inst), |
| 1229 | 1401 | |
| 1230 | | .ptr_slice_len_ptr => try self.airPtrSliceLenPtr(inst), |
| 1231 | | .ptr_slice_ptr_ptr => try self.airPtrSlicePtrPtr(inst), |
| 1402 | .array_elem_val => try func.airArrayElemVal(inst), |
| 1403 | |
| 1404 | .slice_elem_val => try func.airSliceElemVal(inst), |
| 1405 | .slice_elem_ptr => try func.airSliceElemPtr(inst), |
| 1232 | 1406 | |
| 1233 | | .array_elem_val => try self.airArrayElemVal(inst), |
| 1234 | | .slice_elem_val => try self.airSliceElemVal(inst), |
| 1235 | | .slice_elem_ptr => try self.airSliceElemPtr(inst), |
| 1236 | | .ptr_elem_val => try self.airPtrElemVal(inst), |
| 1237 | | .ptr_elem_ptr => try self.airPtrElemPtr(inst), |
| 1407 | .ptr_elem_val => try func.airPtrElemVal(inst), |
| 1408 | .ptr_elem_ptr => try func.airPtrElemPtr(inst), |
| 1238 | 1409 | |
| 1239 | 1410 | .inferred_alloc, .inferred_alloc_comptime => unreachable, |
| 1240 | | .unreach => self.finishAirBookkeeping(), |
| 1241 | | |
| 1242 | | .optional_payload => try self.airOptionalPayload(inst), |
| 1243 | | .optional_payload_ptr => try self.airOptionalPayloadPtr(inst), |
| 1244 | | .optional_payload_ptr_set => try self.airOptionalPayloadPtrSet(inst), |
| 1245 | | .unwrap_errunion_err => try self.airUnwrapErrErr(inst), |
| 1246 | | .unwrap_errunion_payload => try self.airUnwrapErrPayload(inst), |
| 1247 | | .unwrap_errunion_err_ptr => try self.airUnwrapErrErrPtr(inst), |
| 1248 | | .unwrap_errunion_payload_ptr=> try self.airUnwrapErrPayloadPtr(inst), |
| 1249 | | .errunion_payload_ptr_set => try self.airErrUnionPayloadPtrSet(inst), |
| 1250 | | .err_return_trace => try self.airErrReturnTrace(inst), |
| 1251 | | .set_err_return_trace => try self.airSetErrReturnTrace(inst), |
| 1252 | | .save_err_return_trace_index=> try self.airSaveErrReturnTraceIndex(inst), |
| 1253 | | |
| 1254 | | .wrap_optional => try self.airWrapOptional(inst), |
| 1255 | | .wrap_errunion_payload => try self.airWrapErrUnionPayload(inst), |
| 1256 | | .wrap_errunion_err => try self.airWrapErrUnionErr(inst), |
| 1411 | .unreach => func.finishAirBookkeeping(), |
| 1412 | |
| 1413 | .optional_payload => try func.airOptionalPayload(inst), |
| 1414 | .optional_payload_ptr => try func.airOptionalPayloadPtr(inst), |
| 1415 | .optional_payload_ptr_set => try func.airOptionalPayloadPtrSet(inst), |
| 1416 | .unwrap_errunion_err => try func.airUnwrapErrErr(inst), |
| 1417 | .unwrap_errunion_payload => try func.airUnwrapErrPayload(inst), |
| 1418 | .unwrap_errunion_err_ptr => try func.airUnwrapErrErrPtr(inst), |
| 1419 | .unwrap_errunion_payload_ptr=> try func.airUnwrapErrPayloadPtr(inst), |
| 1420 | .errunion_payload_ptr_set => try func.airErrUnionPayloadPtrSet(inst), |
| 1421 | .err_return_trace => try func.airErrReturnTrace(inst), |
| 1422 | .set_err_return_trace => try func.airSetErrReturnTrace(inst), |
| 1423 | .save_err_return_trace_index=> try func.airSaveErrReturnTraceIndex(inst), |
| 1424 | |
| 1425 | .wrap_optional => try func.airWrapOptional(inst), |
| 1426 | .wrap_errunion_payload => try func.airWrapErrUnionPayload(inst), |
| 1427 | .wrap_errunion_err => try func.airWrapErrUnionErr(inst), |
| 1257 | 1428 | |
| 1258 | 1429 | .add_optimized, |
| 1259 | 1430 | .sub_optimized, |
| ... | ... | @@ -1274,16 +1445,16 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1274 | 1445 | .cmp_vector_optimized, |
| 1275 | 1446 | .reduce_optimized, |
| 1276 | 1447 | .int_from_float_optimized, |
| 1277 | | => return self.fail("TODO implement optimized float mode", .{}), |
| 1448 | => return func.fail("TODO implement optimized float mode", .{}), |
| 1278 | 1449 | |
| 1279 | | .is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}), |
| 1280 | | .error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}), |
| 1281 | | .vector_store_elem => return self.fail("TODO implement vector_store_elem", .{}), |
| 1450 | .is_named_enum_value => return func.fail("TODO implement is_named_enum_value", .{}), |
| 1451 | .error_set_has_value => return func.fail("TODO implement error_set_has_value", .{}), |
| 1452 | .vector_store_elem => return func.fail("TODO implement vector_store_elem", .{}), |
| 1282 | 1453 | |
| 1283 | | .c_va_arg => return self.fail("TODO implement c_va_arg", .{}), |
| 1284 | | .c_va_copy => return self.fail("TODO implement c_va_copy", .{}), |
| 1285 | | .c_va_end => return self.fail("TODO implement c_va_end", .{}), |
| 1286 | | .c_va_start => return self.fail("TODO implement c_va_start", .{}), |
| 1454 | .c_va_arg => return func.fail("TODO implement c_va_arg", .{}), |
| 1455 | .c_va_copy => return func.fail("TODO implement c_va_copy", .{}), |
| 1456 | .c_va_end => return func.fail("TODO implement c_va_end", .{}), |
| 1457 | .c_va_start => return func.fail("TODO implement c_va_start", .{}), |
| 1287 | 1458 | |
| 1288 | 1459 | .wasm_memory_size => unreachable, |
| 1289 | 1460 | .wasm_memory_grow => unreachable, |
| ... | ... | @@ -1294,95 +1465,97 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1294 | 1465 | // zig fmt: on |
| 1295 | 1466 | } |
| 1296 | 1467 | |
| 1297 | | assert(!self.register_manager.lockedRegsExist()); |
| 1468 | assert(!func.register_manager.lockedRegsExist()); |
| 1298 | 1469 | |
| 1299 | 1470 | if (std.debug.runtime_safety) { |
| 1300 | | if (self.air_bookkeeping < old_air_bookkeeping + 1) { |
| 1471 | if (func.air_bookkeeping < old_air_bookkeeping + 1) { |
| 1301 | 1472 | 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)] }); |
| 1302 | 1473 | } |
| 1303 | 1474 | |
| 1304 | 1475 | { // check consistency of tracked registers |
| 1305 | | var it = self.register_manager.free_registers.iterator(.{ .kind = .unset }); |
| 1476 | var it = func.register_manager.free_registers.iterator(.{ .kind = .unset }); |
| 1306 | 1477 | while (it.next()) |index| { |
| 1307 | | const tracked_inst = self.register_manager.registers[index]; |
| 1308 | | const tracking = self.getResolvedInstValue(tracked_inst); |
| 1478 | const tracked_inst = func.register_manager.registers[index]; |
| 1479 | tracking_log.debug("tracked inst: {}", .{tracked_inst}); |
| 1480 | const tracking = func.getResolvedInstValue(tracked_inst); |
| 1309 | 1481 | for (tracking.getRegs()) |reg| { |
| 1310 | 1482 | if (RegisterManager.indexOfRegIntoTracked(reg).? == index) break; |
| 1311 | | } else return self.fail( |
| 1312 | | \\%{} takes up these regs: {any}, however those regs don't use it |
| 1313 | | , .{ index, tracking.getRegs() }); |
| 1483 | } else return std.debug.panic( |
| 1484 | \\%{} takes up these regs: {any}, however these regs {any}, don't use it |
| 1485 | , .{ tracked_inst, tracking.getRegs(), RegisterManager.regAtTrackedIndex(@intCast(index)) }); |
| 1314 | 1486 | } |
| 1315 | 1487 | } |
| 1316 | 1488 | } |
| 1317 | 1489 | } |
| 1490 | verbose_tracking_log.debug("{}", .{func.fmtTracking()}); |
| 1318 | 1491 | } |
| 1319 | 1492 | |
| 1320 | | fn getValue(self: *Self, value: MCValue, inst: ?Air.Inst.Index) !void { |
| 1321 | | for (value.getRegs()) |reg| try self.register_manager.getReg(reg, inst); |
| 1493 | fn getValue(func: *Func, value: MCValue, inst: ?Air.Inst.Index) !void { |
| 1494 | for (value.getRegs()) |reg| try func.register_manager.getReg(reg, inst); |
| 1322 | 1495 | } |
| 1323 | 1496 | |
| 1324 | | fn getValueIfFree(self: *Self, value: MCValue, inst: ?Air.Inst.Index) void { |
| 1325 | | for (value.getRegs()) |reg| if (self.register_manager.isRegFree(reg)) |
| 1326 | | self.register_manager.getRegAssumeFree(reg, inst); |
| 1497 | fn getValueIfFree(func: *Func, value: MCValue, inst: ?Air.Inst.Index) void { |
| 1498 | for (value.getRegs()) |reg| if (func.register_manager.isRegFree(reg)) |
| 1499 | func.register_manager.getRegAssumeFree(reg, inst); |
| 1327 | 1500 | } |
| 1328 | 1501 | |
| 1329 | | fn freeValue(self: *Self, value: MCValue) !void { |
| 1502 | fn freeValue(func: *Func, value: MCValue) !void { |
| 1330 | 1503 | switch (value) { |
| 1331 | | .register => |reg| self.register_manager.freeReg(reg), |
| 1332 | | .register_pair => |regs| for (regs) |reg| self.register_manager.freeReg(reg), |
| 1333 | | .register_offset => |reg_off| self.register_manager.freeReg(reg_off.reg), |
| 1504 | .register => |reg| func.register_manager.freeReg(reg), |
| 1505 | .register_pair => |regs| for (regs) |reg| func.register_manager.freeReg(reg), |
| 1506 | .register_offset => |reg_off| func.register_manager.freeReg(reg_off.reg), |
| 1334 | 1507 | else => {}, // TODO process stack allocation death |
| 1335 | 1508 | } |
| 1336 | 1509 | } |
| 1337 | 1510 | |
| 1338 | | fn feed(self: *Self, bt: *Liveness.BigTomb, operand: Air.Inst.Ref) !void { |
| 1511 | fn feed(func: *Func, bt: *Liveness.BigTomb, operand: Air.Inst.Ref) !void { |
| 1339 | 1512 | if (bt.feed()) if (operand.toIndex()) |inst| { |
| 1340 | 1513 | log.debug("feed inst: %{}", .{inst}); |
| 1341 | | try self.processDeath(inst); |
| 1514 | try func.processDeath(inst); |
| 1342 | 1515 | }; |
| 1343 | 1516 | } |
| 1344 | 1517 | |
| 1345 | 1518 | /// Asserts there is already capacity to insert into top branch inst_table. |
| 1346 | | fn processDeath(self: *Self, inst: Air.Inst.Index) !void { |
| 1347 | | try self.inst_tracking.getPtr(inst).?.die(self, inst); |
| 1519 | fn processDeath(func: *Func, inst: Air.Inst.Index) !void { |
| 1520 | try func.inst_tracking.getPtr(inst).?.die(func, inst); |
| 1348 | 1521 | } |
| 1349 | 1522 | |
| 1350 | 1523 | /// Called when there are no operands, and the instruction is always unreferenced. |
| 1351 | | fn finishAirBookkeeping(self: *Self) void { |
| 1524 | fn finishAirBookkeeping(func: *Func) void { |
| 1352 | 1525 | if (std.debug.runtime_safety) { |
| 1353 | | self.air_bookkeeping += 1; |
| 1526 | func.air_bookkeeping += 1; |
| 1354 | 1527 | } |
| 1355 | 1528 | } |
| 1356 | 1529 | |
| 1357 | | fn finishAirResult(self: *Self, inst: Air.Inst.Index, result: MCValue) void { |
| 1358 | | if (self.liveness.isUnused(inst)) switch (result) { |
| 1530 | fn finishAirResult(func: *Func, inst: Air.Inst.Index, result: MCValue) void { |
| 1531 | if (func.liveness.isUnused(inst)) switch (result) { |
| 1359 | 1532 | .none, .dead, .unreach => {}, |
| 1360 | 1533 | else => unreachable, // Why didn't the result die? |
| 1361 | 1534 | } else { |
| 1362 | 1535 | tracking_log.debug("%{d} => {} (birth)", .{ inst, result }); |
| 1363 | | self.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(result)); |
| 1536 | func.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(result)); |
| 1364 | 1537 | // In some cases, an operand may be reused as the result. |
| 1365 | 1538 | // If that operand died and was a register, it was freed by |
| 1366 | 1539 | // processDeath, so we have to "re-allocate" the register. |
| 1367 | | self.getValueIfFree(result, inst); |
| 1540 | func.getValueIfFree(result, inst); |
| 1368 | 1541 | } |
| 1369 | | self.finishAirBookkeeping(); |
| 1542 | func.finishAirBookkeeping(); |
| 1370 | 1543 | } |
| 1371 | 1544 | |
| 1372 | 1545 | fn finishAir( |
| 1373 | | self: *Self, |
| 1546 | func: *Func, |
| 1374 | 1547 | inst: Air.Inst.Index, |
| 1375 | 1548 | result: MCValue, |
| 1376 | 1549 | operands: [Liveness.bpi - 1]Air.Inst.Ref, |
| 1377 | 1550 | ) !void { |
| 1378 | | var tomb_bits = self.liveness.getTombBits(inst); |
| 1551 | var tomb_bits = func.liveness.getTombBits(inst); |
| 1379 | 1552 | for (operands) |op| { |
| 1380 | 1553 | const dies = @as(u1, @truncate(tomb_bits)) != 0; |
| 1381 | 1554 | tomb_bits >>= 1; |
| 1382 | 1555 | if (!dies) continue; |
| 1383 | | try self.processDeath(op.toIndexAllowNone() orelse continue); |
| 1556 | try func.processDeath(op.toIndexAllowNone() orelse continue); |
| 1384 | 1557 | } |
| 1385 | | self.finishAirResult(inst, result); |
| 1558 | func.finishAirResult(inst, result); |
| 1386 | 1559 | } |
| 1387 | 1560 | |
| 1388 | 1561 | const FrameLayout = struct { |
| ... | ... | @@ -1391,7 +1564,7 @@ const FrameLayout = struct { |
| 1391 | 1564 | }; |
| 1392 | 1565 | |
| 1393 | 1566 | fn setFrameLoc( |
| 1394 | | self: *Self, |
| 1567 | func: *Func, |
| 1395 | 1568 | frame_index: FrameIndex, |
| 1396 | 1569 | base: Register, |
| 1397 | 1570 | offset: *i32, |
| ... | ... | @@ -1399,24 +1572,24 @@ fn setFrameLoc( |
| 1399 | 1572 | ) void { |
| 1400 | 1573 | const frame_i = @intFromEnum(frame_index); |
| 1401 | 1574 | if (aligned) { |
| 1402 | | const alignment: InternPool.Alignment = self.frame_allocs.items(.abi_align)[frame_i]; |
| 1575 | const alignment: InternPool.Alignment = func.frame_allocs.items(.abi_align)[frame_i]; |
| 1403 | 1576 | offset.* = if (math.sign(offset.*) < 0) |
| 1404 | 1577 | -1 * @as(i32, @intCast(alignment.backward(@intCast(@abs(offset.*))))) |
| 1405 | 1578 | else |
| 1406 | 1579 | @intCast(alignment.forward(@intCast(@abs(offset.*)))); |
| 1407 | 1580 | } |
| 1408 | | self.frame_locs.set(frame_i, .{ .base = base, .disp = offset.* }); |
| 1409 | | offset.* += self.frame_allocs.items(.abi_size)[frame_i]; |
| 1581 | func.frame_locs.set(frame_i, .{ .base = base, .disp = offset.* }); |
| 1582 | offset.* += func.frame_allocs.items(.abi_size)[frame_i]; |
| 1410 | 1583 | } |
| 1411 | 1584 | |
| 1412 | | fn computeFrameLayout(self: *Self) !FrameLayout { |
| 1413 | | const frame_allocs_len = self.frame_allocs.len; |
| 1414 | | try self.frame_locs.resize(self.gpa, frame_allocs_len); |
| 1415 | | const stack_frame_order = try self.gpa.alloc(FrameIndex, frame_allocs_len - FrameIndex.named_count); |
| 1416 | | defer self.gpa.free(stack_frame_order); |
| 1585 | fn computeFrameLayout(func: *Func) !FrameLayout { |
| 1586 | const frame_allocs_len = func.frame_allocs.len; |
| 1587 | try func.frame_locs.resize(func.gpa, frame_allocs_len); |
| 1588 | const stack_frame_order = try func.gpa.alloc(FrameIndex, frame_allocs_len - FrameIndex.named_count); |
| 1589 | defer func.gpa.free(stack_frame_order); |
| 1417 | 1590 | |
| 1418 | | const frame_size = self.frame_allocs.items(.abi_size); |
| 1419 | | const frame_align = self.frame_allocs.items(.abi_align); |
| 1591 | const frame_size = func.frame_allocs.items(.abi_size); |
| 1592 | const frame_align = func.frame_allocs.items(.abi_align); |
| 1420 | 1593 | |
| 1421 | 1594 | for (stack_frame_order, FrameIndex.named_count..) |*frame_order, frame_index| |
| 1422 | 1595 | frame_order.* = @enumFromInt(frame_index); |
| ... | ... | @@ -1433,9 +1606,9 @@ fn computeFrameLayout(self: *Self) !FrameLayout { |
| 1433 | 1606 | } |
| 1434 | 1607 | |
| 1435 | 1608 | var save_reg_list = Mir.RegisterList{}; |
| 1436 | | for (callee_preserved_regs) |reg| { |
| 1437 | | if (self.register_manager.isRegAllocated(reg)) { |
| 1438 | | save_reg_list.push(&callee_preserved_regs, reg); |
| 1609 | for (abi.Registers.all_preserved) |reg| { |
| 1610 | if (func.register_manager.isRegAllocated(reg)) { |
| 1611 | save_reg_list.push(&abi.Registers.all_preserved, reg); |
| 1439 | 1612 | } |
| 1440 | 1613 | } |
| 1441 | 1614 | |
| ... | ... | @@ -1468,11 +1641,11 @@ fn computeFrameLayout(self: *Self) !FrameLayout { |
| 1468 | 1641 | |
| 1469 | 1642 | // store the ra at total_size - 8, so it's the very first thing in the stack |
| 1470 | 1643 | // relative to the fp |
| 1471 | | self.frame_locs.set( |
| 1644 | func.frame_locs.set( |
| 1472 | 1645 | @intFromEnum(FrameIndex.ret_addr), |
| 1473 | 1646 | .{ .base = .sp, .disp = acc_frame_size - 8 }, |
| 1474 | 1647 | ); |
| 1475 | | self.frame_locs.set( |
| 1648 | func.frame_locs.set( |
| 1476 | 1649 | @intFromEnum(FrameIndex.base_ptr), |
| 1477 | 1650 | .{ .base = .sp, .disp = acc_frame_size - 16 }, |
| 1478 | 1651 | ); |
| ... | ... | @@ -1481,11 +1654,11 @@ fn computeFrameLayout(self: *Self) !FrameLayout { |
| 1481 | 1654 | // not need to know the size of the first allocation. Stack offsets point at the "bottom" |
| 1482 | 1655 | // of variables. |
| 1483 | 1656 | var s0_offset: i32 = -acc_frame_size; |
| 1484 | | self.setFrameLoc(.stack_frame, .s0, &s0_offset, true); |
| 1485 | | for (stack_frame_order) |frame_index| self.setFrameLoc(frame_index, .s0, &s0_offset, true); |
| 1486 | | self.setFrameLoc(.args_frame, .s0, &s0_offset, true); |
| 1487 | | self.setFrameLoc(.call_frame, .s0, &s0_offset, true); |
| 1488 | | self.setFrameLoc(.spill_frame, .s0, &s0_offset, true); |
| 1657 | func.setFrameLoc(.stack_frame, .s0, &s0_offset, true); |
| 1658 | for (stack_frame_order) |frame_index| func.setFrameLoc(frame_index, .s0, &s0_offset, true); |
| 1659 | func.setFrameLoc(.args_frame, .s0, &s0_offset, true); |
| 1660 | func.setFrameLoc(.call_frame, .s0, &s0_offset, true); |
| 1661 | func.setFrameLoc(.spill_frame, .s0, &s0_offset, true); |
| 1489 | 1662 | |
| 1490 | 1663 | return .{ |
| 1491 | 1664 | .stack_adjust = @intCast(acc_frame_size), |
| ... | ... | @@ -1493,21 +1666,21 @@ fn computeFrameLayout(self: *Self) !FrameLayout { |
| 1493 | 1666 | }; |
| 1494 | 1667 | } |
| 1495 | 1668 | |
| 1496 | | fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void { |
| 1497 | | const table = &self.branch_stack.items[self.branch_stack.items.len - 1].inst_table; |
| 1498 | | try table.ensureUnusedCapacity(self.gpa, additional_count); |
| 1669 | fn ensureProcessDeathCapacity(func: *Func, additional_count: usize) !void { |
| 1670 | const table = &func.branch_stack.items[func.branch_stack.items.len - 1].inst_table; |
| 1671 | try table.ensureUnusedCapacity(func.gpa, additional_count); |
| 1499 | 1672 | } |
| 1500 | 1673 | |
| 1501 | | fn memSize(self: *Self, ty: Type) Memory.Size { |
| 1502 | | const mod = self.bin_file.comp.module.?; |
| 1674 | fn memSize(func: *Func, ty: Type) Memory.Size { |
| 1675 | const mod = func.bin_file.comp.module.?; |
| 1503 | 1676 | return switch (ty.zigTypeTag(mod)) { |
| 1504 | | .Float => Memory.Size.fromBitSize(ty.floatBits(self.target.*)), |
| 1677 | .Float => Memory.Size.fromBitSize(ty.floatBits(func.target.*)), |
| 1505 | 1678 | else => Memory.Size.fromByteSize(ty.abiSize(mod)), |
| 1506 | 1679 | }; |
| 1507 | 1680 | } |
| 1508 | 1681 | |
| 1509 | | fn splitType(self: *Self, ty: Type) ![2]Type { |
| 1510 | | const zcu = self.bin_file.comp.module.?; |
| 1682 | fn splitType(func: *Func, ty: Type) ![2]Type { |
| 1683 | const zcu = func.bin_file.comp.module.?; |
| 1511 | 1684 | const classes = mem.sliceTo(&abi.classifySystem(ty, zcu), .none); |
| 1512 | 1685 | var parts: [2]Type = undefined; |
| 1513 | 1686 | if (classes.len == 2) for (&parts, classes, 0..) |*part, class, part_i| { |
| ... | ... | @@ -1524,186 +1697,317 @@ fn splitType(self: *Self, ty: Type) ![2]Type { |
| 1524 | 1697 | }, |
| 1525 | 1698 | else => unreachable, |
| 1526 | 1699 | }, |
| 1527 | | else => return self.fail("TODO: splitType class {}", .{class}), |
| 1700 | else => return func.fail("TODO: splitType class {}", .{class}), |
| 1528 | 1701 | }; |
| 1529 | 1702 | } else if (parts[0].abiSize(zcu) + parts[1].abiSize(zcu) == ty.abiSize(zcu)) return parts; |
| 1530 | | return self.fail("TODO implement splitType for {}", .{ty.fmt(zcu)}); |
| 1703 | return func.fail("TODO implement splitType for {}", .{ty.fmt(zcu)}); |
| 1704 | } |
| 1705 | |
| 1706 | /// Truncates the value in the register in place. |
| 1707 | /// Clobbers any remaining bits. |
| 1708 | fn truncateRegister(func: *Func, ty: Type, reg: Register) !void { |
| 1709 | const mod = func.bin_file.comp.module.?; |
| 1710 | const int_info = if (ty.isAbiInt(mod)) ty.intInfo(mod) else std.builtin.Type.Int{ |
| 1711 | .signedness = .unsigned, |
| 1712 | .bits = @intCast(ty.bitSize(mod)), |
| 1713 | }; |
| 1714 | const shift = math.cast(u6, 64 - int_info.bits % 64) orelse return; |
| 1715 | switch (int_info.signedness) { |
| 1716 | .signed => { |
| 1717 | _ = try func.addInst(.{ |
| 1718 | .tag = .slli, |
| 1719 | .ops = .rri, |
| 1720 | .data = .{ |
| 1721 | .i_type = .{ |
| 1722 | .rd = reg, |
| 1723 | .rs1 = reg, |
| 1724 | .imm12 = Immediate.u(shift), |
| 1725 | }, |
| 1726 | }, |
| 1727 | }); |
| 1728 | _ = try func.addInst(.{ |
| 1729 | .tag = .srai, |
| 1730 | .ops = .rri, |
| 1731 | .data = .{ |
| 1732 | .i_type = .{ |
| 1733 | .rd = reg, |
| 1734 | .rs1 = reg, |
| 1735 | .imm12 = Immediate.u(shift), |
| 1736 | }, |
| 1737 | }, |
| 1738 | }); |
| 1739 | }, |
| 1740 | .unsigned => { |
| 1741 | const mask = ~@as(u64, 0) >> shift; |
| 1742 | if (mask < 256) { |
| 1743 | _ = try func.addInst(.{ |
| 1744 | .tag = .andi, |
| 1745 | .ops = .rri, |
| 1746 | .data = .{ |
| 1747 | .i_type = .{ |
| 1748 | .rd = reg, |
| 1749 | .rs1 = reg, |
| 1750 | .imm12 = Immediate.u(@intCast(mask)), |
| 1751 | }, |
| 1752 | }, |
| 1753 | }); |
| 1754 | } else { |
| 1755 | _ = try func.addInst(.{ |
| 1756 | .tag = .slli, |
| 1757 | .ops = .rri, |
| 1758 | .data = .{ |
| 1759 | .i_type = .{ |
| 1760 | .rd = reg, |
| 1761 | .rs1 = reg, |
| 1762 | .imm12 = Immediate.u(shift), |
| 1763 | }, |
| 1764 | }, |
| 1765 | }); |
| 1766 | _ = try func.addInst(.{ |
| 1767 | .tag = .srli, |
| 1768 | .ops = .rri, |
| 1769 | .data = .{ |
| 1770 | .i_type = .{ |
| 1771 | .rd = reg, |
| 1772 | .rs1 = reg, |
| 1773 | .imm12 = Immediate.u(shift), |
| 1774 | }, |
| 1775 | }, |
| 1776 | }); |
| 1777 | } |
| 1778 | }, |
| 1779 | } |
| 1531 | 1780 | } |
| 1532 | 1781 | |
| 1533 | | fn symbolIndex(self: *Self) !u32 { |
| 1534 | | const zcu = self.bin_file.comp.module.?; |
| 1535 | | const decl_index = zcu.funcOwnerDeclIndex(self.func_index); |
| 1536 | | return switch (self.bin_file.tag) { |
| 1782 | fn symbolIndex(func: *Func) !u32 { |
| 1783 | const zcu = func.bin_file.comp.module.?; |
| 1784 | const decl_index = zcu.funcOwnerDeclIndex(func.func_index); |
| 1785 | return switch (func.bin_file.tag) { |
| 1537 | 1786 | .elf => blk: { |
| 1538 | | const elf_file = self.bin_file.cast(link.File.Elf).?; |
| 1787 | const elf_file = func.bin_file.cast(link.File.Elf).?; |
| 1539 | 1788 | const atom_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index); |
| 1540 | 1789 | break :blk atom_index; |
| 1541 | 1790 | }, |
| 1542 | | else => return self.fail("TODO genSetReg load_symbol for {s}", .{@tagName(self.bin_file.tag)}), |
| 1791 | else => return func.fail("TODO symbolIndex {s}", .{@tagName(func.bin_file.tag)}), |
| 1543 | 1792 | }; |
| 1544 | 1793 | } |
| 1545 | 1794 | |
| 1546 | | fn allocFrameIndex(self: *Self, alloc: FrameAlloc) !FrameIndex { |
| 1547 | | const frame_allocs_slice = self.frame_allocs.slice(); |
| 1795 | fn allocFrameIndex(func: *Func, alloc: FrameAlloc) !FrameIndex { |
| 1796 | const frame_allocs_slice = func.frame_allocs.slice(); |
| 1548 | 1797 | const frame_size = frame_allocs_slice.items(.abi_size); |
| 1549 | 1798 | const frame_align = frame_allocs_slice.items(.abi_align); |
| 1550 | 1799 | |
| 1551 | 1800 | const stack_frame_align = &frame_align[@intFromEnum(FrameIndex.stack_frame)]; |
| 1552 | 1801 | stack_frame_align.* = stack_frame_align.max(alloc.abi_align); |
| 1553 | 1802 | |
| 1554 | | for (self.free_frame_indices.keys(), 0..) |frame_index, free_i| { |
| 1803 | for (func.free_frame_indices.keys(), 0..) |frame_index, free_i| { |
| 1555 | 1804 | const abi_size = frame_size[@intFromEnum(frame_index)]; |
| 1556 | 1805 | if (abi_size != alloc.abi_size) continue; |
| 1557 | 1806 | const abi_align = &frame_align[@intFromEnum(frame_index)]; |
| 1558 | 1807 | abi_align.* = abi_align.max(alloc.abi_align); |
| 1559 | 1808 | |
| 1560 | | _ = self.free_frame_indices.swapRemoveAt(free_i); |
| 1809 | _ = func.free_frame_indices.swapRemoveAt(free_i); |
| 1561 | 1810 | return frame_index; |
| 1562 | 1811 | } |
| 1563 | | const frame_index: FrameIndex = @enumFromInt(self.frame_allocs.len); |
| 1564 | | try self.frame_allocs.append(self.gpa, alloc); |
| 1812 | const frame_index: FrameIndex = @enumFromInt(func.frame_allocs.len); |
| 1813 | try func.frame_allocs.append(func.gpa, alloc); |
| 1565 | 1814 | log.debug("allocated frame {}", .{frame_index}); |
| 1566 | 1815 | return frame_index; |
| 1567 | 1816 | } |
| 1568 | 1817 | |
| 1569 | 1818 | /// Use a pointer instruction as the basis for allocating stack memory. |
| 1570 | | fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !FrameIndex { |
| 1571 | | const zcu = self.bin_file.comp.module.?; |
| 1572 | | const ptr_ty = self.typeOfIndex(inst); |
| 1819 | fn allocMemPtr(func: *Func, inst: Air.Inst.Index) !FrameIndex { |
| 1820 | const zcu = func.bin_file.comp.module.?; |
| 1821 | const ptr_ty = func.typeOfIndex(inst); |
| 1573 | 1822 | const val_ty = ptr_ty.childType(zcu); |
| 1574 | | return self.allocFrameIndex(FrameAlloc.init(.{ |
| 1823 | return func.allocFrameIndex(FrameAlloc.init(.{ |
| 1575 | 1824 | .size = math.cast(u32, val_ty.abiSize(zcu)) orelse { |
| 1576 | | return self.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(zcu)}); |
| 1825 | return func.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(zcu)}); |
| 1577 | 1826 | }, |
| 1578 | 1827 | .alignment = ptr_ty.ptrAlignment(zcu).max(.@"1"), |
| 1579 | 1828 | })); |
| 1580 | 1829 | } |
| 1581 | 1830 | |
| 1582 | | fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue { |
| 1583 | | const zcu = self.bin_file.comp.module.?; |
| 1584 | | const elem_ty = self.typeOfIndex(inst); |
| 1831 | fn typeRegClass(func: *Func, ty: Type) abi.RegisterClass { |
| 1832 | const zcu = func.bin_file.comp.module.?; |
| 1833 | return switch (ty.zigTypeTag(zcu)) { |
| 1834 | .Float => .float, |
| 1835 | .Vector => @panic("TODO: typeRegClass for Vectors"), |
| 1836 | inline else => .int, |
| 1837 | }; |
| 1838 | } |
| 1839 | |
| 1840 | fn regGeneralClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet { |
| 1841 | const zcu = func.bin_file.comp.module.?; |
| 1842 | return switch (ty.zigTypeTag(zcu)) { |
| 1843 | .Float => abi.Registers.Float.general_purpose, |
| 1844 | .Vector => @panic("TODO: regGeneralClassForType for Vectors"), |
| 1845 | else => abi.Registers.Integer.general_purpose, |
| 1846 | }; |
| 1847 | } |
| 1848 | |
| 1849 | fn regTempClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet { |
| 1850 | const zcu = func.bin_file.comp.module.?; |
| 1851 | return switch (ty.zigTypeTag(zcu)) { |
| 1852 | .Float => abi.Registers.Float.temporary, |
| 1853 | .Vector => @panic("TODO: regTempClassForType for Vectors"), |
| 1854 | else => abi.Registers.Integer.temporary, |
| 1855 | }; |
| 1856 | } |
| 1857 | |
| 1858 | fn allocRegOrMem(func: *Func, elem_ty: Type, inst: ?Air.Inst.Index, reg_ok: bool) !MCValue { |
| 1859 | const zcu = func.bin_file.comp.module.?; |
| 1585 | 1860 | |
| 1586 | 1861 | const abi_size = math.cast(u32, elem_ty.abiSize(zcu)) orelse { |
| 1587 | | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(zcu)}); |
| 1862 | return func.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(zcu)}); |
| 1588 | 1863 | }; |
| 1589 | 1864 | |
| 1590 | | if (reg_ok) { |
| 1591 | | // Make sure the type can fit in a register before we try to allocate one. |
| 1592 | | const ptr_bits = self.target.ptrBitWidth(); |
| 1593 | | const ptr_bytes: u64 = @divExact(ptr_bits, 8); |
| 1594 | | if (abi_size <= ptr_bytes) { |
| 1595 | | if (self.register_manager.tryAllocReg(inst, gp)) |reg| { |
| 1596 | | return .{ .register = reg }; |
| 1597 | | } |
| 1865 | const min_size: u32 = switch (elem_ty.zigTypeTag(zcu)) { |
| 1866 | .Float => 4, |
| 1867 | .Vector => @panic("allocRegOrMem Vector"), |
| 1868 | else => 8, |
| 1869 | }; |
| 1870 | |
| 1871 | if (reg_ok and abi_size <= min_size) { |
| 1872 | if (func.register_manager.tryAllocReg(inst, func.regGeneralClassForType(elem_ty))) |reg| { |
| 1873 | return .{ .register = reg }; |
| 1598 | 1874 | } |
| 1599 | 1875 | } |
| 1600 | 1876 | |
| 1601 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(elem_ty, zcu)); |
| 1877 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(elem_ty, zcu)); |
| 1602 | 1878 | return .{ .load_frame = .{ .index = frame_index } }; |
| 1603 | 1879 | } |
| 1604 | 1880 | |
| 1605 | 1881 | /// Allocates a register from the general purpose set and returns the Register and the Lock. |
| 1606 | 1882 | /// |
| 1607 | | /// Up to the user to unlock the register later. |
| 1608 | | fn allocReg(self: *Self) !struct { Register, RegisterLock } { |
| 1609 | | const reg = try self.register_manager.allocReg(null, gp); |
| 1610 | | const lock = self.register_manager.lockRegAssumeUnused(reg); |
| 1883 | /// Up to the caller to unlock the register later. |
| 1884 | fn allocReg(func: *Func, reg_class: abi.RegisterClass) !struct { Register, RegisterLock } { |
| 1885 | if (reg_class == .float and !func.hasFeature(.f)) |
| 1886 | std.debug.panic("allocReg class == float where F isn't enabled", .{}); |
| 1887 | |
| 1888 | const class = switch (reg_class) { |
| 1889 | .int => abi.Registers.Integer.general_purpose, |
| 1890 | .float => abi.Registers.Float.general_purpose, |
| 1891 | }; |
| 1892 | |
| 1893 | const reg = try func.register_manager.allocReg(null, class); |
| 1894 | const lock = func.register_manager.lockRegAssumeUnused(reg); |
| 1611 | 1895 | return .{ reg, lock }; |
| 1612 | 1896 | } |
| 1613 | 1897 | |
| 1614 | | fn elemOffset(self: *Self, index_ty: Type, index: MCValue, elem_size: u64) !Register { |
| 1898 | /// Similar to `allocReg` but will copy the MCValue into the Register unless `operand` is already |
| 1899 | /// a register, in which case it will return a possible lock to that register. |
| 1900 | fn promoteReg(func: *Func, ty: Type, operand: MCValue) !struct { Register, ?RegisterLock } { |
| 1901 | if (operand == .register) { |
| 1902 | const op_reg = operand.register; |
| 1903 | return .{ op_reg, func.register_manager.lockReg(operand.register) }; |
| 1904 | } |
| 1905 | |
| 1906 | const reg, const lock = try func.allocReg(func.typeRegClass(ty)); |
| 1907 | try func.genSetReg(ty, reg, operand); |
| 1908 | return .{ reg, lock }; |
| 1909 | } |
| 1910 | |
| 1911 | fn elemOffset(func: *Func, index_ty: Type, index: MCValue, elem_size: u64) !Register { |
| 1615 | 1912 | const reg: Register = blk: { |
| 1616 | 1913 | switch (index) { |
| 1617 | 1914 | .immediate => |imm| { |
| 1618 | 1915 | // Optimisation: if index MCValue is an immediate, we can multiply in `comptime` |
| 1619 | 1916 | // and set the register directly to the scaled offset as an immediate. |
| 1620 | | const reg = try self.register_manager.allocReg(null, gp); |
| 1621 | | try self.genSetReg(index_ty, reg, .{ .immediate = imm * elem_size }); |
| 1917 | const reg = try func.register_manager.allocReg(null, func.regGeneralClassForType(index_ty)); |
| 1918 | try func.genSetReg(index_ty, reg, .{ .immediate = imm * elem_size }); |
| 1622 | 1919 | break :blk reg; |
| 1623 | 1920 | }, |
| 1624 | 1921 | else => { |
| 1625 | | const reg = try self.copyToTmpRegister(index_ty, index); |
| 1626 | | const lock = self.register_manager.lockRegAssumeUnused(reg); |
| 1627 | | defer self.register_manager.unlockReg(lock); |
| 1922 | const reg = try func.copyToTmpRegister(index_ty, index); |
| 1923 | const lock = func.register_manager.lockRegAssumeUnused(reg); |
| 1924 | defer func.register_manager.unlockReg(lock); |
| 1925 | |
| 1926 | const result_reg, const result_lock = try func.allocReg(.int); |
| 1927 | defer func.register_manager.unlockReg(result_lock); |
| 1628 | 1928 | |
| 1629 | | const result = try self.binOp( |
| 1929 | try func.genBinOp( |
| 1630 | 1930 | .mul, |
| 1631 | 1931 | .{ .register = reg }, |
| 1632 | 1932 | index_ty, |
| 1633 | 1933 | .{ .immediate = elem_size }, |
| 1634 | 1934 | index_ty, |
| 1935 | result_reg, |
| 1635 | 1936 | ); |
| 1636 | | break :blk result.register; |
| 1937 | |
| 1938 | break :blk result_reg; |
| 1637 | 1939 | }, |
| 1638 | 1940 | } |
| 1639 | 1941 | }; |
| 1640 | 1942 | return reg; |
| 1641 | 1943 | } |
| 1642 | 1944 | |
| 1643 | | pub fn spillInstruction(self: *Self, reg: Register, inst: Air.Inst.Index) !void { |
| 1644 | | const tracking = self.inst_tracking.getPtr(inst) orelse return; |
| 1945 | pub fn spillInstruction(func: *Func, reg: Register, inst: Air.Inst.Index) !void { |
| 1946 | const tracking = func.inst_tracking.getPtr(inst) orelse return; |
| 1645 | 1947 | for (tracking.getRegs()) |tracked_reg| { |
| 1646 | 1948 | if (tracked_reg.id() == reg.id()) break; |
| 1647 | 1949 | } else unreachable; // spilled reg not tracked with spilled instruciton |
| 1648 | | try tracking.spill(self, inst); |
| 1649 | | try tracking.trackSpill(self, inst); |
| 1950 | try tracking.spill(func, inst); |
| 1951 | try tracking.trackSpill(func, inst); |
| 1650 | 1952 | } |
| 1651 | 1953 | |
| 1652 | 1954 | /// Copies a value to a register without tracking the register. The register is not considered |
| 1653 | 1955 | /// allocated. A second call to `copyToTmpRegister` may return the same register. |
| 1654 | 1956 | /// This can have a side effect of spilling instructions to the stack to free up a register. |
| 1655 | | fn copyToTmpRegister(self: *Self, ty: Type, mcv: MCValue) !Register { |
| 1656 | | const reg = try self.register_manager.allocReg(null, tp); |
| 1657 | | try self.genSetReg(ty, reg, mcv); |
| 1957 | fn copyToTmpRegister(func: *Func, ty: Type, mcv: MCValue) !Register { |
| 1958 | log.debug("copyToTmpRegister ty: {}", .{ty.fmt(func.bin_file.comp.module.?)}); |
| 1959 | const reg = try func.register_manager.allocReg(null, func.regTempClassForType(ty)); |
| 1960 | try func.genSetReg(ty, reg, mcv); |
| 1658 | 1961 | return reg; |
| 1659 | 1962 | } |
| 1660 | 1963 | |
| 1661 | 1964 | /// Allocates a new register and copies `mcv` into it. |
| 1662 | 1965 | /// `reg_owner` is the instruction that gets associated with the register in the register table. |
| 1663 | 1966 | /// This can have a side effect of spilling instructions to the stack to free up a register. |
| 1664 | | fn copyToNewRegister(self: *Self, reg_owner: Air.Inst.Index, mcv: MCValue) !MCValue { |
| 1665 | | const reg = try self.register_manager.allocReg(reg_owner, gp); |
| 1666 | | try self.genSetReg(self.typeOfIndex(reg_owner), reg, mcv); |
| 1967 | fn copyToNewRegister(func: *Func, reg_owner: Air.Inst.Index, mcv: MCValue) !MCValue { |
| 1968 | const ty = func.typeOfIndex(reg_owner); |
| 1969 | const reg = try func.register_manager.allocReg(reg_owner, func.regGeneralClassForType(ty)); |
| 1970 | try func.genSetReg(func.typeOfIndex(reg_owner), reg, mcv); |
| 1667 | 1971 | return MCValue{ .register = reg }; |
| 1668 | 1972 | } |
| 1669 | 1973 | |
| 1670 | | fn airAlloc(self: *Self, inst: Air.Inst.Index) !void { |
| 1671 | | const result = MCValue{ .lea_frame = .{ .index = try self.allocMemPtr(inst) } }; |
| 1672 | | return self.finishAir(inst, result, .{ .none, .none, .none }); |
| 1974 | fn airAlloc(func: *Func, inst: Air.Inst.Index) !void { |
| 1975 | const result = MCValue{ .lea_frame = .{ .index = try func.allocMemPtr(inst) } }; |
| 1976 | return func.finishAir(inst, result, .{ .none, .none, .none }); |
| 1673 | 1977 | } |
| 1674 | 1978 | |
| 1675 | | fn airRetPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 1676 | | const result: MCValue = switch (self.ret_mcv.long) { |
| 1677 | | .none => .{ .lea_frame = .{ .index = try self.allocMemPtr(inst) } }, |
| 1979 | fn airRetPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 1980 | const result: MCValue = switch (func.ret_mcv.long) { |
| 1981 | .none => .{ .lea_frame = .{ .index = try func.allocMemPtr(inst) } }, |
| 1678 | 1982 | .load_frame => .{ .register_offset = .{ |
| 1679 | | .reg = (try self.copyToNewRegister( |
| 1983 | .reg = (try func.copyToNewRegister( |
| 1680 | 1984 | inst, |
| 1681 | | self.ret_mcv.long, |
| 1985 | func.ret_mcv.long, |
| 1682 | 1986 | )).register, |
| 1683 | | .off = self.ret_mcv.short.indirect.off, |
| 1987 | .off = func.ret_mcv.short.indirect.off, |
| 1684 | 1988 | } }, |
| 1685 | | else => |t| return self.fail("TODO: airRetPtr {s}", .{@tagName(t)}), |
| 1989 | else => |t| return func.fail("TODO: airRetPtr {s}", .{@tagName(t)}), |
| 1686 | 1990 | }; |
| 1687 | | return self.finishAir(inst, result, .{ .none, .none, .none }); |
| 1991 | return func.finishAir(inst, result, .{ .none, .none, .none }); |
| 1688 | 1992 | } |
| 1689 | 1993 | |
| 1690 | | fn airFptrunc(self: *Self, inst: Air.Inst.Index) !void { |
| 1691 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1692 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airFptrunc for {}", .{self.target.cpu.arch}); |
| 1693 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1994 | fn airFptrunc(func: *Func, inst: Air.Inst.Index) !void { |
| 1995 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1996 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airFptrunc for {}", .{func.target.cpu.arch}); |
| 1997 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1694 | 1998 | } |
| 1695 | 1999 | |
| 1696 | | fn airFpext(self: *Self, inst: Air.Inst.Index) !void { |
| 1697 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1698 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airFpext for {}", .{self.target.cpu.arch}); |
| 1699 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2000 | fn airFpext(func: *Func, inst: Air.Inst.Index) !void { |
| 2001 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2002 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airFpext for {}", .{func.target.cpu.arch}); |
| 2003 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1700 | 2004 | } |
| 1701 | 2005 | |
| 1702 | | fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 1703 | | const zcu = self.bin_file.comp.module.?; |
| 1704 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1705 | | const src_ty = self.typeOf(ty_op.operand); |
| 1706 | | const dst_ty = self.typeOfIndex(inst); |
| 2006 | fn airIntCast(func: *Func, inst: Air.Inst.Index) !void { |
| 2007 | const zcu = func.bin_file.comp.module.?; |
| 2008 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2009 | const src_ty = func.typeOf(ty_op.operand); |
| 2010 | const dst_ty = func.typeOfIndex(inst); |
| 1707 | 2011 | |
| 1708 | 2012 | const result: MCValue = result: { |
| 1709 | 2013 | const src_int_info = src_ty.intInfo(zcu); |
| ... | ... | @@ -1711,20 +2015,20 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 1711 | 2015 | |
| 1712 | 2016 | const min_ty = if (dst_int_info.bits < src_int_info.bits) dst_ty else src_ty; |
| 1713 | 2017 | |
| 1714 | | const src_mcv = try self.resolveInst(ty_op.operand); |
| 2018 | const src_mcv = try func.resolveInst(ty_op.operand); |
| 1715 | 2019 | |
| 1716 | 2020 | const src_storage_bits: u16 = switch (src_mcv) { |
| 1717 | 2021 | .register => 64, |
| 1718 | 2022 | .load_frame => src_int_info.bits, |
| 1719 | | else => return self.fail("airIntCast from {s}", .{@tagName(src_mcv)}), |
| 2023 | else => return func.fail("airIntCast from {s}", .{@tagName(src_mcv)}), |
| 1720 | 2024 | }; |
| 1721 | 2025 | |
| 1722 | 2026 | const dst_mcv = if (dst_int_info.bits <= src_storage_bits and |
| 1723 | 2027 | math.divCeil(u16, dst_int_info.bits, 64) catch unreachable == |
| 1724 | 2028 | math.divCeil(u32, src_storage_bits, 64) catch unreachable and |
| 1725 | | self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: { |
| 1726 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 1727 | | try self.genCopy(min_ty, dst_mcv, src_mcv); |
| 2029 | func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: { |
| 2030 | const dst_mcv = try func.allocRegOrMem(dst_ty, inst, true); |
| 2031 | try func.genCopy(min_ty, dst_mcv, src_mcv); |
| 1728 | 2032 | break :dst dst_mcv; |
| 1729 | 2033 | }; |
| 1730 | 2034 | |
| ... | ... | @@ -1735,53 +2039,53 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 1735 | 2039 | break :result null; // TODO |
| 1736 | 2040 | |
| 1737 | 2041 | break :result dst_mcv; |
| 1738 | | } orelse return self.fail("TODO implement airIntCast from {} to {}", .{ |
| 2042 | } orelse return func.fail("TODO: implement airIntCast from {} to {}", .{ |
| 1739 | 2043 | src_ty.fmt(zcu), dst_ty.fmt(zcu), |
| 1740 | 2044 | }); |
| 1741 | 2045 | |
| 1742 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2046 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1743 | 2047 | } |
| 1744 | 2048 | |
| 1745 | | fn airTrunc(self: *Self, inst: Air.Inst.Index) !void { |
| 1746 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1747 | | if (self.liveness.isUnused(inst)) |
| 1748 | | return self.finishAir(inst, .unreach, .{ ty_op.operand, .none, .none }); |
| 2049 | fn airTrunc(func: *Func, inst: Air.Inst.Index) !void { |
| 2050 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2051 | if (func.liveness.isUnused(inst)) |
| 2052 | return func.finishAir(inst, .unreach, .{ ty_op.operand, .none, .none }); |
| 1749 | 2053 | |
| 1750 | | const operand = try self.resolveInst(ty_op.operand); |
| 2054 | const operand = try func.resolveInst(ty_op.operand); |
| 1751 | 2055 | _ = operand; |
| 1752 | | return self.fail("TODO implement trunc for {}", .{self.target.cpu.arch}); |
| 1753 | | // return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2056 | return func.fail("TODO implement trunc for {}", .{func.target.cpu.arch}); |
| 2057 | // return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1754 | 2058 | } |
| 1755 | 2059 | |
| 1756 | | fn airIntFromBool(self: *Self, inst: Air.Inst.Index) !void { |
| 1757 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 1758 | | const operand = try self.resolveInst(un_op); |
| 1759 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else operand; |
| 1760 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 2060 | fn airIntFromBool(func: *Func, inst: Air.Inst.Index) !void { |
| 2061 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 2062 | const operand = try func.resolveInst(un_op); |
| 2063 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else operand; |
| 2064 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 1761 | 2065 | } |
| 1762 | 2066 | |
| 1763 | | fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 1764 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1765 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 1766 | | const zcu = self.bin_file.comp.module.?; |
| 2067 | fn airNot(func: *Func, inst: Air.Inst.Index) !void { |
| 2068 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2069 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2070 | const zcu = func.bin_file.comp.module.?; |
| 1767 | 2071 | |
| 1768 | | const operand = try self.resolveInst(ty_op.operand); |
| 1769 | | const ty = self.typeOf(ty_op.operand); |
| 2072 | const operand = try func.resolveInst(ty_op.operand); |
| 2073 | const ty = func.typeOf(ty_op.operand); |
| 1770 | 2074 | |
| 1771 | 2075 | switch (ty.zigTypeTag(zcu)) { |
| 1772 | 2076 | .Bool => { |
| 1773 | 2077 | const operand_reg = blk: { |
| 1774 | 2078 | if (operand == .register) break :blk operand.register; |
| 1775 | | break :blk try self.copyToTmpRegister(ty, operand); |
| 2079 | break :blk try func.copyToTmpRegister(ty, operand); |
| 1776 | 2080 | }; |
| 1777 | 2081 | |
| 1778 | 2082 | const dst_reg: Register = |
| 1779 | | if (self.reuseOperand(inst, ty_op.operand, 0, operand) and operand == .register) |
| 2083 | if (func.reuseOperand(inst, ty_op.operand, 0, operand) and operand == .register) |
| 1780 | 2084 | operand.register |
| 1781 | 2085 | else |
| 1782 | | try self.register_manager.allocReg(inst, gp); |
| 2086 | (try func.allocRegOrMem(func.typeOfIndex(inst), inst, true)).register; |
| 1783 | 2087 | |
| 1784 | | _ = try self.addInst(.{ |
| 2088 | _ = try func.addInst(.{ |
| 1785 | 2089 | .tag = .pseudo, |
| 1786 | 2090 | .ops = .pseudo_not, |
| 1787 | 2091 | .data = .{ |
| ... | ... | @@ -1794,718 +2098,906 @@ fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 1794 | 2098 | |
| 1795 | 2099 | break :result .{ .register = dst_reg }; |
| 1796 | 2100 | }, |
| 1797 | | .Int => return self.fail("TODO: airNot ints", .{}), |
| 2101 | .Int => return func.fail("TODO: airNot ints", .{}), |
| 1798 | 2102 | else => unreachable, |
| 1799 | 2103 | } |
| 1800 | 2104 | }; |
| 1801 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2105 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1802 | 2106 | } |
| 1803 | 2107 | |
| 1804 | | fn airMinMax( |
| 1805 | | self: *Self, |
| 1806 | | inst: Air.Inst.Index, |
| 1807 | | comptime tag: enum { |
| 1808 | | max, |
| 1809 | | min, |
| 1810 | | }, |
| 1811 | | ) !void { |
| 1812 | | const zcu = self.bin_file.comp.module.?; |
| 1813 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 1814 | | |
| 1815 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 1816 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 1817 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 1818 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 1819 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 1820 | | |
| 1821 | | const int_info = lhs_ty.intInfo(zcu); |
| 2108 | fn airSlice(func: *Func, inst: Air.Inst.Index) !void { |
| 2109 | const zcu = func.bin_file.comp.module.?; |
| 2110 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2111 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 1822 | 2112 | |
| 1823 | | if (int_info.bits > 64) return self.fail("TODO: > 64 bit @min", .{}); |
| 2113 | const slice_ty = func.typeOfIndex(inst); |
| 2114 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(slice_ty, zcu)); |
| 1824 | 2115 | |
| 1825 | | const lhs_reg, const lhs_lock = blk: { |
| 1826 | | if (lhs == .register) break :blk .{ lhs.register, null }; |
| 2116 | const ptr_ty = func.typeOf(bin_op.lhs); |
| 2117 | try func.genSetMem(.{ .frame = frame_index }, 0, ptr_ty, .{ .air_ref = bin_op.lhs }); |
| 1827 | 2118 | |
| 1828 | | const lhs_reg, const lhs_lock = try self.allocReg(); |
| 1829 | | try self.genSetReg(lhs_ty, lhs_reg, lhs); |
| 1830 | | break :blk .{ lhs_reg, lhs_lock }; |
| 1831 | | }; |
| 1832 | | defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2119 | const len_ty = func.typeOf(bin_op.rhs); |
| 2120 | try func.genSetMem( |
| 2121 | .{ .frame = frame_index }, |
| 2122 | @intCast(ptr_ty.abiSize(zcu)), |
| 2123 | len_ty, |
| 2124 | .{ .air_ref = bin_op.rhs }, |
| 2125 | ); |
| 1833 | 2126 | |
| 1834 | | const rhs_reg, const rhs_lock = blk: { |
| 1835 | | if (rhs == .register) break :blk .{ rhs.register, null }; |
| 2127 | const result = MCValue{ .load_frame = .{ .index = frame_index } }; |
| 2128 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2129 | } |
| 1836 | 2130 | |
| 1837 | | const rhs_reg, const rhs_lock = try self.allocReg(); |
| 1838 | | try self.genSetReg(rhs_ty, rhs_reg, rhs); |
| 1839 | | break :blk .{ rhs_reg, rhs_lock }; |
| 1840 | | }; |
| 1841 | | defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2131 | fn airBinOp(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 2132 | const zcu = func.bin_file.comp.module.?; |
| 2133 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2134 | const dst_mcv = try func.binOp(inst, tag, bin_op.lhs, bin_op.rhs); |
| 1842 | 2135 | |
| 1843 | | const mask_reg, const mask_lock = try self.allocReg(); |
| 1844 | | defer self.register_manager.unlockReg(mask_lock); |
| 2136 | const dst_ty = func.typeOfIndex(inst); |
| 2137 | if (dst_ty.isAbiInt(zcu)) { |
| 2138 | const abi_size: u32 = @intCast(dst_ty.abiSize(zcu)); |
| 2139 | const bit_size: u32 = @intCast(dst_ty.bitSize(zcu)); |
| 2140 | if (abi_size * 8 > bit_size) { |
| 2141 | const dst_lock = switch (dst_mcv) { |
| 2142 | .register => |dst_reg| func.register_manager.lockRegAssumeUnused(dst_reg), |
| 2143 | else => null, |
| 2144 | }; |
| 2145 | defer if (dst_lock) |lock| func.register_manager.unlockReg(lock); |
| 1845 | 2146 | |
| 1846 | | const result_reg, const result_lock = try self.allocReg(); |
| 1847 | | defer self.register_manager.unlockReg(result_lock); |
| 2147 | if (dst_mcv.isRegister()) { |
| 2148 | try func.truncateRegister(dst_ty, dst_mcv.getReg().?); |
| 2149 | } else { |
| 2150 | const tmp_reg, const tmp_lock = try func.allocReg(.int); |
| 2151 | defer func.register_manager.unlockReg(tmp_lock); |
| 2152 | |
| 2153 | const hi_ty = try zcu.intType(.unsigned, @intCast((dst_ty.bitSize(zcu) - 1) % 64 + 1)); |
| 2154 | const hi_mcv = dst_mcv.address().offset(@intCast(bit_size / 64 * 8)).deref(); |
| 2155 | try func.genSetReg(hi_ty, tmp_reg, hi_mcv); |
| 2156 | try func.truncateRegister(dst_ty, tmp_reg); |
| 2157 | try func.genCopy(hi_ty, hi_mcv, .{ .register = tmp_reg }); |
| 2158 | } |
| 2159 | } |
| 2160 | } |
| 1848 | 2161 | |
| 1849 | | _ = try self.addInst(.{ |
| 1850 | | .tag = if (int_info.signedness == .unsigned) .sltu else .slt, |
| 1851 | | .ops = .rrr, |
| 1852 | | .data = .{ .r_type = .{ |
| 1853 | | .rd = mask_reg, |
| 1854 | | .rs1 = lhs_reg, |
| 1855 | | .rs2 = rhs_reg, |
| 1856 | | } }, |
| 1857 | | }); |
| 2162 | return func.finishAir(inst, dst_mcv, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2163 | } |
| 1858 | 2164 | |
| 1859 | | _ = try self.addInst(.{ |
| 1860 | | .tag = .sub, |
| 1861 | | .ops = .rrr, |
| 1862 | | .data = .{ .r_type = .{ |
| 1863 | | .rd = mask_reg, |
| 1864 | | .rs1 = .zero, |
| 1865 | | .rs2 = mask_reg, |
| 1866 | | } }, |
| 1867 | | }); |
| 2165 | fn binOp( |
| 2166 | func: *Func, |
| 2167 | maybe_inst: ?Air.Inst.Index, |
| 2168 | air_tag: Air.Inst.Tag, |
| 2169 | lhs_air: Air.Inst.Ref, |
| 2170 | rhs_air: Air.Inst.Ref, |
| 2171 | ) !MCValue { |
| 2172 | _ = maybe_inst; |
| 2173 | const zcu = func.bin_file.comp.module.?; |
| 2174 | const lhs_ty = func.typeOf(lhs_air); |
| 2175 | const rhs_ty = func.typeOf(rhs_air); |
| 2176 | |
| 2177 | if (lhs_ty.isRuntimeFloat()) libcall: { |
| 2178 | const float_bits = lhs_ty.floatBits(func.target.*); |
| 2179 | const type_needs_libcall = switch (float_bits) { |
| 2180 | 16 => true, |
| 2181 | 32, 64 => false, |
| 2182 | 80, 128 => true, |
| 2183 | else => unreachable, |
| 2184 | }; |
| 2185 | switch (air_tag) { |
| 2186 | .rem, .mod => {}, |
| 2187 | else => if (!type_needs_libcall) break :libcall, |
| 2188 | } |
| 2189 | return func.fail("binOp libcall runtime-float ops", .{}); |
| 2190 | } |
| 1868 | 2191 | |
| 1869 | | _ = try self.addInst(.{ |
| 1870 | | .tag = .xor, |
| 1871 | | .ops = .rrr, |
| 1872 | | .data = .{ .r_type = .{ |
| 1873 | | .rd = result_reg, |
| 1874 | | .rs1 = lhs_reg, |
| 1875 | | .rs2 = rhs_reg, |
| 1876 | | } }, |
| 1877 | | }); |
| 2192 | if (lhs_ty.bitSize(zcu) > 64) return func.fail("TODO: binOp >= 64 bits", .{}); |
| 1878 | 2193 | |
| 1879 | | _ = try self.addInst(.{ |
| 1880 | | .tag = .@"and", |
| 1881 | | .ops = .rrr, |
| 1882 | | .data = .{ .r_type = .{ |
| 1883 | | .rd = mask_reg, |
| 1884 | | .rs1 = result_reg, |
| 1885 | | .rs2 = mask_reg, |
| 1886 | | } }, |
| 1887 | | }); |
| 2194 | const lhs_mcv = try func.resolveInst(lhs_air); |
| 2195 | const rhs_mcv = try func.resolveInst(rhs_air); |
| 1888 | 2196 | |
| 1889 | | _ = try self.addInst(.{ |
| 1890 | | .tag = .xor, |
| 1891 | | .ops = .rrr, |
| 1892 | | .data = .{ .r_type = .{ |
| 1893 | | .rd = result_reg, |
| 1894 | | .rs1 = if (tag == .min) rhs_reg else lhs_reg, |
| 1895 | | .rs2 = mask_reg, |
| 1896 | | } }, |
| 1897 | | }); |
| 2197 | const class_for_dst_ty: abi.RegisterClass = switch (air_tag) { |
| 2198 | // will always return int register no matter the input |
| 2199 | .cmp_eq, |
| 2200 | .cmp_neq, |
| 2201 | .cmp_lt, |
| 2202 | .cmp_lte, |
| 2203 | .cmp_gt, |
| 2204 | .cmp_gte, |
| 2205 | => .int, |
| 1898 | 2206 | |
| 1899 | | break :result .{ .register = result_reg }; |
| 2207 | else => func.typeRegClass(lhs_ty), |
| 1900 | 2208 | }; |
| 1901 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 1902 | | } |
| 1903 | 2209 | |
| 1904 | | fn airSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 1905 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 1906 | | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 1907 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement slice for {}", .{self.target.cpu.arch}); |
| 1908 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 1909 | | } |
| 2210 | const dst_reg, const dst_lock = try func.allocReg(class_for_dst_ty); |
| 2211 | defer func.register_manager.unlockReg(dst_lock); |
| 1910 | 2212 | |
| 1911 | | fn airBinOp(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 1912 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 1913 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 1914 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 1915 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 1916 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2213 | try func.genBinOp( |
| 2214 | air_tag, |
| 2215 | lhs_mcv, |
| 2216 | lhs_ty, |
| 2217 | rhs_mcv, |
| 2218 | rhs_ty, |
| 2219 | dst_reg, |
| 2220 | ); |
| 1917 | 2221 | |
| 1918 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 1919 | | break :result try self.binOp(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 1920 | | }; |
| 1921 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2222 | return .{ .register = dst_reg }; |
| 1922 | 2223 | } |
| 1923 | 2224 | |
| 1924 | | /// For all your binary operation needs, this function will generate |
| 1925 | | /// the corresponding Mir instruction(s). Returns the location of the |
| 1926 | | /// result. |
| 1927 | | /// |
| 1928 | | /// If the binary operation itself happens to be an Air instruction, |
| 1929 | | /// pass the corresponding index in the inst parameter. That helps |
| 1930 | | /// this function do stuff like reusing operands. |
| 2225 | /// Does the same thing as binOp however is meant to be used internally to the backend. |
| 1931 | 2226 | /// |
| 1932 | | /// This function does not do any lowering to Mir itself, but instead |
| 1933 | | /// looks at the lhs and rhs and determines which kind of lowering |
| 1934 | | /// would be best suitable and then delegates the lowering to other |
| 1935 | | /// functions. |
| 2227 | /// The `dst_reg` argument is meant to be caller-locked. Asserts that the binOp result can be |
| 2228 | /// fit into the register. |
| 1936 | 2229 | /// |
| 1937 | | /// `maybe_inst` **needs** to be a bin_op, make sure of that. |
| 1938 | | fn binOp( |
| 1939 | | self: *Self, |
| 2230 | /// Assumes that the `dst_reg` class is correct. |
| 2231 | fn genBinOp( |
| 2232 | func: *Func, |
| 1940 | 2233 | tag: Air.Inst.Tag, |
| 1941 | | lhs: MCValue, |
| 2234 | lhs_mcv: MCValue, |
| 1942 | 2235 | lhs_ty: Type, |
| 1943 | | rhs: MCValue, |
| 2236 | rhs_mcv: MCValue, |
| 1944 | 2237 | rhs_ty: Type, |
| 1945 | | ) InnerError!MCValue { |
| 1946 | | const zcu = self.bin_file.comp.module.?; |
| 2238 | dst_reg: Register, |
| 2239 | ) !void { |
| 2240 | const zcu = func.bin_file.comp.module.?; |
| 2241 | const bit_size = lhs_ty.bitSize(zcu); |
| 2242 | assert(bit_size <= 64); |
| 2243 | |
| 2244 | const is_unsigned = lhs_ty.isUnsignedInt(zcu); |
| 2245 | |
| 2246 | const lhs_reg, const maybe_lhs_lock = try func.promoteReg(lhs_ty, lhs_mcv); |
| 2247 | const rhs_reg, const maybe_rhs_lock = try func.promoteReg(rhs_ty, rhs_mcv); |
| 2248 | |
| 2249 | defer if (maybe_lhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2250 | defer if (maybe_rhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 1947 | 2251 | |
| 1948 | 2252 | switch (tag) { |
| 1949 | | // Arithmetic operations on integers and floats |
| 1950 | 2253 | .add, |
| 2254 | .add_wrap, |
| 1951 | 2255 | .sub, |
| 2256 | .sub_wrap, |
| 1952 | 2257 | .mul, |
| 1953 | | .cmp_eq, |
| 1954 | | .cmp_neq, |
| 1955 | | .cmp_gt, |
| 1956 | | .cmp_gte, |
| 1957 | | .cmp_lt, |
| 1958 | | .cmp_lte, |
| 2258 | .mul_wrap, |
| 1959 | 2259 | => { |
| 2260 | if (!math.isPowerOfTwo(bit_size)) |
| 2261 | return func.fail( |
| 2262 | "TODO: genBinOp {s} non-pow 2, found {}", |
| 2263 | .{ @tagName(tag), bit_size }, |
| 2264 | ); |
| 2265 | |
| 1960 | 2266 | switch (lhs_ty.zigTypeTag(zcu)) { |
| 1961 | | .Float => return self.fail("TODO binary operations on floats", .{}), |
| 1962 | | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 1963 | 2267 | .Int => { |
| 1964 | | assert(lhs_ty.eql(rhs_ty, zcu)); |
| 1965 | | const int_info = lhs_ty.intInfo(zcu); |
| 1966 | | if (int_info.bits <= 64) { |
| 1967 | | return self.binOpRegister(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 1968 | | } else { |
| 1969 | | return self.fail("TODO binary operations on int with bits > 64", .{}); |
| 2268 | const mir_tag: Mir.Inst.Tag = switch (tag) { |
| 2269 | .add, .add_wrap => switch (bit_size) { |
| 2270 | 8, 16, 64 => .add, |
| 2271 | 32 => .addw, |
| 2272 | else => unreachable, |
| 2273 | }, |
| 2274 | .sub, .sub_wrap => switch (bit_size) { |
| 2275 | 8, 16, 32 => .subw, |
| 2276 | 64 => .sub, |
| 2277 | else => unreachable, |
| 2278 | }, |
| 2279 | .mul, .mul_wrap => switch (bit_size) { |
| 2280 | 8, 16, 64 => .mul, |
| 2281 | 32 => .mulw, |
| 2282 | else => unreachable, |
| 2283 | }, |
| 2284 | else => unreachable, |
| 2285 | }; |
| 2286 | |
| 2287 | _ = try func.addInst(.{ |
| 2288 | .tag = mir_tag, |
| 2289 | .ops = .rrr, |
| 2290 | .data = .{ |
| 2291 | .r_type = .{ |
| 2292 | .rd = dst_reg, |
| 2293 | .rs1 = lhs_reg, |
| 2294 | .rs2 = rhs_reg, |
| 2295 | }, |
| 2296 | }, |
| 2297 | }); |
| 2298 | |
| 2299 | // truncate when the instruction is larger than the bit size. |
| 2300 | switch (bit_size) { |
| 2301 | 8, 16 => try func.truncateRegister(lhs_ty, dst_reg), |
| 2302 | 32 => {}, // addw/subw affects the first 32-bits |
| 2303 | 64 => {}, // add/sub affects the entire register |
| 2304 | else => unreachable, |
| 1970 | 2305 | } |
| 1971 | 2306 | }, |
| 1972 | | else => |x| return self.fail("TOOD: binOp {s}", .{@tagName(x)}), |
| 2307 | .Float => { |
| 2308 | const mir_tag: Mir.Inst.Tag = switch (tag) { |
| 2309 | .add => switch (bit_size) { |
| 2310 | 32 => .fadds, |
| 2311 | 64 => .faddd, |
| 2312 | else => unreachable, |
| 2313 | }, |
| 2314 | .sub => switch (bit_size) { |
| 2315 | 32 => .fsubs, |
| 2316 | 64 => .fsubd, |
| 2317 | else => unreachable, |
| 2318 | }, |
| 2319 | .mul => switch (bit_size) { |
| 2320 | 32 => .fmuls, |
| 2321 | 64 => .fmuld, |
| 2322 | else => unreachable, |
| 2323 | }, |
| 2324 | else => unreachable, |
| 2325 | }; |
| 2326 | |
| 2327 | _ = try func.addInst(.{ |
| 2328 | .tag = mir_tag, |
| 2329 | .ops = .rrr, |
| 2330 | .data = .{ |
| 2331 | .r_type = .{ |
| 2332 | .rd = dst_reg, |
| 2333 | .rs1 = lhs_reg, |
| 2334 | .rs2 = rhs_reg, |
| 2335 | }, |
| 2336 | }, |
| 2337 | }); |
| 2338 | }, |
| 2339 | else => unreachable, |
| 1973 | 2340 | } |
| 1974 | 2341 | }, |
| 1975 | 2342 | |
| 1976 | 2343 | .ptr_add, |
| 1977 | 2344 | .ptr_sub, |
| 1978 | 2345 | => { |
| 1979 | | switch (lhs_ty.zigTypeTag(zcu)) { |
| 1980 | | .Pointer => { |
| 1981 | | const ptr_ty = lhs_ty; |
| 1982 | | const elem_ty = switch (ptr_ty.ptrSize(zcu)) { |
| 1983 | | .One => ptr_ty.childType(zcu).childType(zcu), // ptr to array, so get array element type |
| 1984 | | else => ptr_ty.childType(zcu), |
| 1985 | | }; |
| 1986 | | const elem_size = elem_ty.abiSize(zcu); |
| 1987 | | |
| 1988 | | if (elem_size == 1) { |
| 1989 | | const base_tag: Air.Inst.Tag = switch (tag) { |
| 1990 | | .ptr_add => .add, |
| 1991 | | .ptr_sub => .sub, |
| 1992 | | else => unreachable, |
| 1993 | | }; |
| 2346 | const tmp_reg = try func.copyToTmpRegister(rhs_ty, .{ .register = rhs_reg }); |
| 2347 | const tmp_mcv = MCValue{ .register = tmp_reg }; |
| 2348 | const tmp_lock = func.register_manager.lockRegAssumeUnused(tmp_reg); |
| 2349 | defer func.register_manager.unlockReg(tmp_lock); |
| 2350 | |
| 2351 | // RISC-V has no immediate mul, so we copy the size to a temporary register |
| 2352 | const elem_size = lhs_ty.elemType2(zcu).abiSize(zcu); |
| 2353 | const elem_size_reg = try func.copyToTmpRegister(Type.usize, .{ .immediate = elem_size }); |
| 2354 | |
| 2355 | try func.genBinOp( |
| 2356 | .mul, |
| 2357 | tmp_mcv, |
| 2358 | rhs_ty, |
| 2359 | .{ .register = elem_size_reg }, |
| 2360 | Type.usize, |
| 2361 | tmp_reg, |
| 2362 | ); |
| 1994 | 2363 | |
| 1995 | | return try self.binOpRegister(base_tag, lhs, lhs_ty, rhs, rhs_ty); |
| 1996 | | } else { |
| 1997 | | const offset = try self.binOp( |
| 1998 | | .mul, |
| 1999 | | rhs, |
| 2000 | | Type.usize, |
| 2001 | | .{ .immediate = elem_size }, |
| 2002 | | Type.usize, |
| 2003 | | ); |
| 2364 | try func.genBinOp( |
| 2365 | switch (tag) { |
| 2366 | .ptr_add => .add, |
| 2367 | .ptr_sub => .sub, |
| 2368 | else => unreachable, |
| 2369 | }, |
| 2370 | lhs_mcv, |
| 2371 | Type.usize, // we know it's a pointer, so it'll be usize. |
| 2372 | tmp_mcv, |
| 2373 | Type.usize, |
| 2374 | dst_reg, |
| 2375 | ); |
| 2376 | }, |
| 2004 | 2377 | |
| 2005 | | const addr = try self.binOp( |
| 2006 | | tag, |
| 2007 | | lhs, |
| 2008 | | Type.manyptr_u8, |
| 2009 | | offset, |
| 2010 | | Type.usize, |
| 2011 | | ); |
| 2012 | | return addr; |
| 2013 | | } |
| 2378 | .bit_and, |
| 2379 | .bit_or, |
| 2380 | .bool_and, |
| 2381 | .bool_or, |
| 2382 | => { |
| 2383 | _ = try func.addInst(.{ |
| 2384 | .tag = switch (tag) { |
| 2385 | .bit_and, .bool_and => .@"and", |
| 2386 | .bit_or, .bool_or => .@"or", |
| 2387 | else => unreachable, |
| 2014 | 2388 | }, |
| 2015 | | else => unreachable, |
| 2389 | .ops = .rrr, |
| 2390 | .data = .{ |
| 2391 | .r_type = .{ |
| 2392 | .rd = dst_reg, |
| 2393 | .rs1 = lhs_reg, |
| 2394 | .rs2 = rhs_reg, |
| 2395 | }, |
| 2396 | }, |
| 2397 | }); |
| 2398 | |
| 2399 | switch (tag) { |
| 2400 | .bool_and, |
| 2401 | .bool_or, |
| 2402 | => try func.truncateRegister(Type.bool, dst_reg), |
| 2403 | else => {}, |
| 2016 | 2404 | } |
| 2017 | 2405 | }, |
| 2018 | 2406 | |
| 2019 | | // These instructions have unsymteric bit sizes on RHS and LHS. |
| 2020 | | .shr, |
| 2021 | | .shl, |
| 2407 | .div_trunc, |
| 2022 | 2408 | => { |
| 2023 | | switch (lhs_ty.zigTypeTag(zcu)) { |
| 2024 | | .Float => return self.fail("TODO binary operations on floats", .{}), |
| 2025 | | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 2026 | | .Int => { |
| 2027 | | const int_info = lhs_ty.intInfo(zcu); |
| 2028 | | if (int_info.bits <= 64) { |
| 2029 | | return self.binOpRegister(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2030 | | } else { |
| 2031 | | return self.fail("TODO binary operations on int with bits > 64", .{}); |
| 2032 | | } |
| 2409 | if (!math.isPowerOfTwo(bit_size)) |
| 2410 | return func.fail( |
| 2411 | "TODO: genBinOp {s} non-pow 2, found {}", |
| 2412 | .{ @tagName(tag), bit_size }, |
| 2413 | ); |
| 2414 | |
| 2415 | const mir_tag: Mir.Inst.Tag = switch (tag) { |
| 2416 | .div_trunc => switch (bit_size) { |
| 2417 | 8, 16, 32 => if (is_unsigned) .divuw else .divw, |
| 2418 | 64 => if (is_unsigned) .divu else .div, |
| 2419 | else => unreachable, |
| 2033 | 2420 | }, |
| 2034 | 2421 | else => unreachable, |
| 2035 | | } |
| 2036 | | }, |
| 2037 | | else => return self.fail("TODO binOp {}", .{tag}), |
| 2038 | | } |
| 2039 | | } |
| 2040 | | /// Don't call this function directly. Use binOp instead. |
| 2041 | | /// |
| 2042 | | /// Calling this function signals an intention to generate a Mir |
| 2043 | | /// instruction of the form |
| 2044 | | /// |
| 2045 | | /// op dest, lhs, rhs |
| 2046 | | /// |
| 2047 | | /// Asserts that generating an instruction of that form is possible. |
| 2048 | | fn binOpRegister( |
| 2049 | | self: *Self, |
| 2050 | | tag: Air.Inst.Tag, |
| 2051 | | lhs: MCValue, |
| 2052 | | lhs_ty: Type, |
| 2053 | | rhs: MCValue, |
| 2054 | | rhs_ty: Type, |
| 2055 | | ) !MCValue { |
| 2056 | | const lhs_reg, const lhs_lock = blk: { |
| 2057 | | if (lhs == .register) break :blk .{ lhs.register, null }; |
| 2058 | | |
| 2059 | | const lhs_reg, const lhs_lock = try self.allocReg(); |
| 2060 | | try self.genSetReg(lhs_ty, lhs_reg, lhs); |
| 2061 | | break :blk .{ lhs_reg, lhs_lock }; |
| 2062 | | }; |
| 2063 | | defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2064 | | |
| 2065 | | const rhs_reg, const rhs_lock = blk: { |
| 2066 | | if (rhs == .register) break :blk .{ rhs.register, null }; |
| 2067 | | |
| 2068 | | const rhs_reg, const rhs_lock = try self.allocReg(); |
| 2069 | | try self.genSetReg(rhs_ty, rhs_reg, rhs); |
| 2070 | | break :blk .{ rhs_reg, rhs_lock }; |
| 2071 | | }; |
| 2072 | | defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2073 | | |
| 2074 | | const dest_reg, const dest_lock = try self.allocReg(); |
| 2075 | | defer self.register_manager.unlockReg(dest_lock); |
| 2076 | | |
| 2077 | | const mir_tag: Mir.Inst.Tag = switch (tag) { |
| 2078 | | .add => .add, |
| 2079 | | .sub => .sub, |
| 2080 | | .mul => .mul, |
| 2081 | | |
| 2082 | | .shl => .sllw, |
| 2083 | | .shr => .srlw, |
| 2084 | | |
| 2085 | | .cmp_eq, |
| 2086 | | .cmp_neq, |
| 2087 | | .cmp_gt, |
| 2088 | | .cmp_gte, |
| 2089 | | .cmp_lt, |
| 2090 | | .cmp_lte, |
| 2091 | | => .pseudo, |
| 2092 | | |
| 2093 | | else => return self.fail("TODO: binOpRegister {s}", .{@tagName(tag)}), |
| 2094 | | }; |
| 2422 | }; |
| 2095 | 2423 | |
| 2096 | | switch (mir_tag) { |
| 2097 | | .add, |
| 2098 | | .sub, |
| 2099 | | .mul, |
| 2100 | | .sllw, |
| 2101 | | .srlw, |
| 2102 | | => { |
| 2103 | | _ = try self.addInst(.{ |
| 2424 | _ = try func.addInst(.{ |
| 2104 | 2425 | .tag = mir_tag, |
| 2105 | 2426 | .ops = .rrr, |
| 2106 | 2427 | .data = .{ |
| 2107 | 2428 | .r_type = .{ |
| 2108 | | .rd = dest_reg, |
| 2429 | .rd = dst_reg, |
| 2109 | 2430 | .rs1 = lhs_reg, |
| 2110 | 2431 | .rs2 = rhs_reg, |
| 2111 | 2432 | }, |
| 2112 | 2433 | }, |
| 2113 | 2434 | }); |
| 2435 | |
| 2436 | if (!is_unsigned) { |
| 2437 | // truncate when the instruction is larger than the bit size. |
| 2438 | switch (bit_size) { |
| 2439 | 8, 16 => try func.truncateRegister(lhs_ty, dst_reg), |
| 2440 | 32 => {}, // divw affects the first 32-bits |
| 2441 | 64 => {}, // div affects the entire register |
| 2442 | else => unreachable, |
| 2443 | } |
| 2444 | } |
| 2114 | 2445 | }, |
| 2115 | 2446 | |
| 2116 | | .pseudo => { |
| 2117 | | const pseudo_op = switch (tag) { |
| 2118 | | .cmp_eq, |
| 2119 | | .cmp_neq, |
| 2120 | | .cmp_gt, |
| 2121 | | .cmp_gte, |
| 2122 | | .cmp_lt, |
| 2123 | | .cmp_lte, |
| 2124 | | => .pseudo_compare, |
| 2447 | .shr, |
| 2448 | .shr_exact, |
| 2449 | .shl, |
| 2450 | .shl_exact, |
| 2451 | => { |
| 2452 | if (!math.isPowerOfTwo(bit_size)) |
| 2453 | return func.fail( |
| 2454 | "TODO: genBinOp {s} non-pow 2, found {}", |
| 2455 | .{ @tagName(tag), bit_size }, |
| 2456 | ); |
| 2457 | |
| 2458 | // it's important that the shift amount is exact |
| 2459 | try func.truncateRegister(rhs_ty, rhs_reg); |
| 2460 | |
| 2461 | const mir_tag: Mir.Inst.Tag = switch (tag) { |
| 2462 | .shl, .shl_exact => switch (bit_size) { |
| 2463 | 8, 16, 64 => .sll, |
| 2464 | 32 => .sllw, |
| 2465 | else => unreachable, |
| 2466 | }, |
| 2467 | .shr, .shr_exact => switch (bit_size) { |
| 2468 | 8, 16, 64 => .srl, |
| 2469 | 32 => .srlw, |
| 2470 | else => unreachable, |
| 2471 | }, |
| 2125 | 2472 | else => unreachable, |
| 2126 | 2473 | }; |
| 2127 | 2474 | |
| 2128 | | _ = try self.addInst(.{ |
| 2475 | _ = try func.addInst(.{ |
| 2476 | .tag = mir_tag, |
| 2477 | .ops = .rrr, |
| 2478 | .data = .{ .r_type = .{ |
| 2479 | .rd = dst_reg, |
| 2480 | .rs1 = lhs_reg, |
| 2481 | .rs2 = rhs_reg, |
| 2482 | } }, |
| 2483 | }); |
| 2484 | |
| 2485 | switch (bit_size) { |
| 2486 | 8, 16 => try func.truncateRegister(lhs_ty, dst_reg), |
| 2487 | 32 => {}, |
| 2488 | 64 => {}, |
| 2489 | else => unreachable, |
| 2490 | } |
| 2491 | }, |
| 2492 | |
| 2493 | // TODO: move the isel logic out of lower and into here. |
| 2494 | .cmp_eq, |
| 2495 | .cmp_neq, |
| 2496 | .cmp_lt, |
| 2497 | .cmp_lte, |
| 2498 | .cmp_gt, |
| 2499 | .cmp_gte, |
| 2500 | => { |
| 2501 | _ = try func.addInst(.{ |
| 2129 | 2502 | .tag = .pseudo, |
| 2130 | | .ops = pseudo_op, |
| 2503 | .ops = .pseudo_compare, |
| 2131 | 2504 | .data = .{ |
| 2132 | 2505 | .compare = .{ |
| 2133 | | .rd = dest_reg, |
| 2134 | | .rs1 = lhs_reg, |
| 2135 | | .rs2 = rhs_reg, |
| 2136 | 2506 | .op = switch (tag) { |
| 2137 | 2507 | .cmp_eq => .eq, |
| 2138 | 2508 | .cmp_neq => .neq, |
| 2139 | | .cmp_gt => .gt, |
| 2140 | | .cmp_gte => .gte, |
| 2141 | 2509 | .cmp_lt => .lt, |
| 2142 | 2510 | .cmp_lte => .lte, |
| 2511 | .cmp_gt => .gt, |
| 2512 | .cmp_gte => .gte, |
| 2143 | 2513 | else => unreachable, |
| 2144 | 2514 | }, |
| 2515 | .rd = dst_reg, |
| 2516 | .rs1 = lhs_reg, |
| 2517 | .rs2 = rhs_reg, |
| 2518 | .ty = lhs_ty, |
| 2145 | 2519 | }, |
| 2146 | 2520 | }, |
| 2147 | 2521 | }); |
| 2148 | 2522 | }, |
| 2149 | 2523 | |
| 2150 | | else => unreachable, |
| 2151 | | } |
| 2152 | | |
| 2153 | | // generate the struct for OF checks |
| 2154 | | |
| 2155 | | return MCValue{ .register = dest_reg }; |
| 2156 | | } |
| 2157 | | |
| 2158 | | fn airPtrArithmetic(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 2159 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2160 | | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2161 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2162 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2163 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2164 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2165 | | |
| 2166 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2167 | | break :result try self.binOp(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2168 | | }; |
| 2169 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2170 | | } |
| 2171 | | |
| 2172 | | fn airAddWrap(self: *Self, inst: Air.Inst.Index) !void { |
| 2173 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2174 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement addwrap for {}", .{self.target.cpu.arch}); |
| 2175 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2176 | | } |
| 2177 | | |
| 2178 | | fn airAddSat(self: *Self, inst: Air.Inst.Index) !void { |
| 2179 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2180 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement add_sat for {}", .{self.target.cpu.arch}); |
| 2181 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2182 | | } |
| 2183 | | |
| 2184 | | fn airSubWrap(self: *Self, inst: Air.Inst.Index) !void { |
| 2185 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2186 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2187 | | // RISCV arthemtic instructions already wrap, so this is simply a sub binOp with |
| 2188 | | // no overflow checks. |
| 2189 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2190 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2191 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2192 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2524 | // A branchless @min/@max sequence. |
| 2525 | // |
| 2526 | // Assume that a0 and a1 are the lhs and rhs respectively. |
| 2527 | // Also assume that a2 is the destination register. |
| 2528 | // |
| 2529 | // Algorithm: |
| 2530 | // slt s0, a0, a1 |
| 2531 | // sub s0, zero, s0 |
| 2532 | // xor a2, a0, a1 |
| 2533 | // and s0, a2, s0 |
| 2534 | // xor a2, a0, s0 # a0 is @min, a1 is @max |
| 2535 | // |
| 2536 | // "slt s0, a0, a1" will set s0 to 1 if a0 is less than a1, and 1 otherwise. |
| 2537 | // |
| 2538 | // "sub s0, zero, s0" will set all the bits of s0 to 1 if it was 1, otherwise it'll remain at 0. |
| 2539 | // |
| 2540 | // "xor a2, a0, a1" stores the bitwise XOR of a0 and a1 in a2. Effectively getting the difference between them. |
| 2541 | // |
| 2542 | // "and a0, a2, s0" here we mask the result of the XOR with the negated s0. If a0 < a1, s0 is -1, which |
| 2543 | // doesn't change the bits of a2. If a0 >= a1, s0 is 0, nullifying a2. |
| 2544 | // |
| 2545 | // "xor a2, a0, s0" the final XOR operation adjusts a2 to be the minimum value of a0 and a1. If a0 was less than |
| 2546 | // a1, s0 was -1, flipping all the bits in a2 and effectively restoring a0. If a0 was greater than or equal to a1, |
| 2547 | // s0 was 0, leaving a2 unchanged as a0. |
| 2548 | .min, .max => { |
| 2549 | const int_info = lhs_ty.intInfo(zcu); |
| 2550 | |
| 2551 | const mask_reg, const mask_lock = try func.allocReg(.int); |
| 2552 | defer func.register_manager.unlockReg(mask_lock); |
| 2553 | |
| 2554 | _ = try func.addInst(.{ |
| 2555 | .tag = if (int_info.signedness == .unsigned) .sltu else .slt, |
| 2556 | .ops = .rrr, |
| 2557 | .data = .{ .r_type = .{ |
| 2558 | .rd = mask_reg, |
| 2559 | .rs1 = lhs_reg, |
| 2560 | .rs2 = rhs_reg, |
| 2561 | } }, |
| 2562 | }); |
| 2193 | 2563 | |
| 2194 | | break :result try self.binOp(.sub, lhs, lhs_ty, rhs, rhs_ty); |
| 2195 | | }; |
| 2196 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2197 | | } |
| 2564 | _ = try func.addInst(.{ |
| 2565 | .tag = .sub, |
| 2566 | .ops = .rrr, |
| 2567 | .data = .{ .r_type = .{ |
| 2568 | .rd = mask_reg, |
| 2569 | .rs1 = .zero, |
| 2570 | .rs2 = mask_reg, |
| 2571 | } }, |
| 2572 | }); |
| 2198 | 2573 | |
| 2199 | | fn airSubSat(self: *Self, inst: Air.Inst.Index) !void { |
| 2200 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2201 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement sub_sat for {}", .{self.target.cpu.arch}); |
| 2202 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2203 | | } |
| 2574 | _ = try func.addInst(.{ |
| 2575 | .tag = .xor, |
| 2576 | .ops = .rrr, |
| 2577 | .data = .{ .r_type = .{ |
| 2578 | .rd = dst_reg, |
| 2579 | .rs1 = lhs_reg, |
| 2580 | .rs2 = rhs_reg, |
| 2581 | } }, |
| 2582 | }); |
| 2204 | 2583 | |
| 2205 | | fn airMul(self: *Self, inst: Air.Inst.Index) !void { |
| 2206 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2207 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement mul for {}", .{self.target.cpu.arch}); |
| 2208 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2209 | | } |
| 2584 | _ = try func.addInst(.{ |
| 2585 | .tag = .@"and", |
| 2586 | .ops = .rrr, |
| 2587 | .data = .{ .r_type = .{ |
| 2588 | .rd = mask_reg, |
| 2589 | .rs1 = dst_reg, |
| 2590 | .rs2 = mask_reg, |
| 2591 | } }, |
| 2592 | }); |
| 2210 | 2593 | |
| 2211 | | fn airMulWrap(self: *Self, inst: Air.Inst.Index) !void { |
| 2212 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2213 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement mulwrap for {}", .{self.target.cpu.arch}); |
| 2214 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2594 | _ = try func.addInst(.{ |
| 2595 | .tag = .xor, |
| 2596 | .ops = .rrr, |
| 2597 | .data = .{ .r_type = .{ |
| 2598 | .rd = dst_reg, |
| 2599 | .rs1 = if (tag == .min) rhs_reg else lhs_reg, |
| 2600 | .rs2 = mask_reg, |
| 2601 | } }, |
| 2602 | }); |
| 2603 | }, |
| 2604 | else => return func.fail("TODO: genBinOp {}", .{tag}), |
| 2605 | } |
| 2215 | 2606 | } |
| 2216 | 2607 | |
| 2217 | | fn airMulSat(self: *Self, inst: Air.Inst.Index) !void { |
| 2218 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2219 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement mul_sat for {}", .{self.target.cpu.arch}); |
| 2220 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2608 | fn airPtrArithmetic(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 2609 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2610 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2611 | const dst_mcv = try func.binOp(inst, tag, bin_op.lhs, bin_op.rhs); |
| 2612 | return func.finishAir(inst, dst_mcv, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2221 | 2613 | } |
| 2222 | 2614 | |
| 2223 | | fn airAddWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2224 | | const zcu = self.bin_file.comp.module.?; |
| 2225 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2226 | | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2615 | fn airAddWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2616 | const zcu = func.bin_file.comp.module.?; |
| 2617 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2618 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2227 | 2619 | |
| 2228 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2229 | | const lhs = try self.resolveInst(extra.lhs); |
| 2230 | | const rhs = try self.resolveInst(extra.rhs); |
| 2231 | | const lhs_ty = self.typeOf(extra.lhs); |
| 2232 | | const rhs_ty = self.typeOf(extra.rhs); |
| 2620 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2621 | const lhs_ty = func.typeOf(extra.lhs); |
| 2233 | 2622 | |
| 2234 | 2623 | const int_info = lhs_ty.intInfo(zcu); |
| 2235 | 2624 | |
| 2236 | | const tuple_ty = self.typeOfIndex(inst); |
| 2237 | | const result_mcv = try self.allocRegOrMem(inst, false); |
| 2625 | const tuple_ty = func.typeOfIndex(inst); |
| 2626 | const result_mcv = try func.allocRegOrMem(tuple_ty, inst, false); |
| 2238 | 2627 | const offset = result_mcv.load_frame; |
| 2239 | 2628 | |
| 2240 | 2629 | if (int_info.bits >= 8 and math.isPowerOfTwo(int_info.bits)) { |
| 2241 | | const add_result = try self.binOp(.add, lhs, lhs_ty, rhs, rhs_ty); |
| 2242 | | const add_result_reg = try self.copyToTmpRegister(lhs_ty, add_result); |
| 2243 | | const add_result_reg_lock = self.register_manager.lockRegAssumeUnused(add_result_reg); |
| 2244 | | defer self.register_manager.unlockReg(add_result_reg_lock); |
| 2630 | const add_result = try func.binOp(null, .add, extra.lhs, extra.rhs); |
| 2631 | const add_result_reg = try func.copyToTmpRegister(lhs_ty, add_result); |
| 2632 | const add_result_reg_lock = func.register_manager.lockRegAssumeUnused(add_result_reg); |
| 2633 | defer func.register_manager.unlockReg(add_result_reg_lock); |
| 2245 | 2634 | |
| 2246 | 2635 | const shift_amount: u6 = @intCast(Type.usize.bitSize(zcu) - int_info.bits); |
| 2247 | 2636 | |
| 2248 | | const shift_reg, const shift_lock = try self.allocReg(); |
| 2249 | | defer self.register_manager.unlockReg(shift_lock); |
| 2637 | const shift_reg, const shift_lock = try func.allocReg(.int); |
| 2638 | defer func.register_manager.unlockReg(shift_lock); |
| 2250 | 2639 | |
| 2251 | | _ = try self.addInst(.{ |
| 2640 | _ = try func.addInst(.{ |
| 2252 | 2641 | .tag = .slli, |
| 2253 | 2642 | .ops = .rri, |
| 2254 | 2643 | .data = .{ |
| 2255 | 2644 | .i_type = .{ |
| 2256 | 2645 | .rd = shift_reg, |
| 2257 | 2646 | .rs1 = add_result_reg, |
| 2258 | | .imm12 = Immediate.s(shift_amount), |
| 2647 | .imm12 = Immediate.u(shift_amount), |
| 2259 | 2648 | }, |
| 2260 | 2649 | }, |
| 2261 | 2650 | }); |
| 2262 | 2651 | |
| 2263 | | _ = try self.addInst(.{ |
| 2652 | _ = try func.addInst(.{ |
| 2264 | 2653 | .tag = if (int_info.signedness == .unsigned) .srli else .srai, |
| 2265 | 2654 | .ops = .rri, |
| 2266 | 2655 | .data = .{ |
| 2267 | 2656 | .i_type = .{ |
| 2268 | 2657 | .rd = shift_reg, |
| 2269 | 2658 | .rs1 = shift_reg, |
| 2270 | | .imm12 = Immediate.s(shift_amount), |
| 2659 | .imm12 = Immediate.u(shift_amount), |
| 2271 | 2660 | }, |
| 2272 | 2661 | }, |
| 2273 | 2662 | }); |
| 2274 | 2663 | |
| 2275 | | const add_result_frame: FrameAddr = .{ |
| 2276 | | .index = offset.index, |
| 2277 | | .off = offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, zcu))), |
| 2278 | | }; |
| 2279 | | try self.genSetStack( |
| 2664 | try func.genSetMem( |
| 2665 | .{ .frame = offset.index }, |
| 2666 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, zcu))), |
| 2280 | 2667 | lhs_ty, |
| 2281 | | add_result_frame, |
| 2282 | 2668 | add_result, |
| 2283 | 2669 | ); |
| 2284 | 2670 | |
| 2285 | | const overflow_mcv = try self.binOp( |
| 2671 | const overflow_reg, const overflow_lock = try func.allocReg(.int); |
| 2672 | defer func.register_manager.unlockReg(overflow_lock); |
| 2673 | |
| 2674 | try func.genBinOp( |
| 2286 | 2675 | .cmp_neq, |
| 2287 | 2676 | .{ .register = shift_reg }, |
| 2288 | 2677 | lhs_ty, |
| 2289 | 2678 | .{ .register = add_result_reg }, |
| 2290 | 2679 | lhs_ty, |
| 2680 | overflow_reg, |
| 2291 | 2681 | ); |
| 2292 | 2682 | |
| 2293 | | const overflow_frame: FrameAddr = .{ |
| 2294 | | .index = offset.index, |
| 2295 | | .off = offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))), |
| 2296 | | }; |
| 2297 | | try self.genSetStack( |
| 2683 | try func.genSetMem( |
| 2684 | .{ .frame = offset.index }, |
| 2685 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))), |
| 2298 | 2686 | Type.u1, |
| 2299 | | overflow_frame, |
| 2300 | | overflow_mcv, |
| 2687 | .{ .register = overflow_reg }, |
| 2301 | 2688 | ); |
| 2302 | 2689 | |
| 2303 | 2690 | break :result result_mcv; |
| 2304 | 2691 | } else { |
| 2305 | | return self.fail("TODO: less than 8 bit or non-pow 2 addition", .{}); |
| 2692 | return func.fail("TODO: less than 8 bit or non-pow 2 addition", .{}); |
| 2306 | 2693 | } |
| 2307 | 2694 | }; |
| 2308 | 2695 | |
| 2309 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2696 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2310 | 2697 | } |
| 2311 | 2698 | |
| 2312 | | fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2313 | | _ = inst; |
| 2314 | | return self.fail("TODO implement airSubWithOverflow for {}", .{self.target.cpu.arch}); |
| 2699 | fn airSubWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2700 | const zcu = func.bin_file.comp.module.?; |
| 2701 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2702 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2703 | |
| 2704 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2705 | const lhs = try func.resolveInst(extra.lhs); |
| 2706 | const rhs = try func.resolveInst(extra.rhs); |
| 2707 | const lhs_ty = func.typeOf(extra.lhs); |
| 2708 | const rhs_ty = func.typeOf(extra.rhs); |
| 2709 | |
| 2710 | const int_info = lhs_ty.intInfo(zcu); |
| 2711 | |
| 2712 | if (!math.isPowerOfTwo(int_info.bits) or int_info.bits < 8) { |
| 2713 | return func.fail("TODO: airSubWithOverflow non-power of 2 and less than 8 bits", .{}); |
| 2714 | } |
| 2715 | |
| 2716 | if (int_info.bits > 64) { |
| 2717 | return func.fail("TODO: airSubWithOverflow > 64 bits", .{}); |
| 2718 | } |
| 2719 | |
| 2720 | const tuple_ty = func.typeOfIndex(inst); |
| 2721 | const result_mcv = try func.allocRegOrMem(tuple_ty, inst, false); |
| 2722 | const offset = result_mcv.load_frame; |
| 2723 | |
| 2724 | const dest_mcv = try func.binOp(null, .sub, extra.lhs, extra.rhs); |
| 2725 | assert(dest_mcv == .register); |
| 2726 | const dest_reg = dest_mcv.register; |
| 2727 | |
| 2728 | try func.genSetMem( |
| 2729 | .{ .frame = offset.index }, |
| 2730 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, zcu))), |
| 2731 | lhs_ty, |
| 2732 | .{ .register = dest_reg }, |
| 2733 | ); |
| 2734 | |
| 2735 | const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs); |
| 2736 | defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2737 | |
| 2738 | const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs); |
| 2739 | defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2740 | |
| 2741 | const overflow_reg = try func.copyToTmpRegister(Type.usize, .{ .immediate = 0 }); |
| 2742 | |
| 2743 | const overflow_lock = func.register_manager.lockRegAssumeUnused(overflow_reg); |
| 2744 | defer func.register_manager.unlockReg(overflow_lock); |
| 2745 | |
| 2746 | switch (int_info.signedness) { |
| 2747 | .unsigned => { |
| 2748 | _ = try func.addInst(.{ |
| 2749 | .tag = .sltu, |
| 2750 | .ops = .rrr, |
| 2751 | .data = .{ .r_type = .{ |
| 2752 | .rd = overflow_reg, |
| 2753 | .rs1 = lhs_reg, |
| 2754 | .rs2 = rhs_reg, |
| 2755 | } }, |
| 2756 | }); |
| 2757 | |
| 2758 | try func.genSetMem( |
| 2759 | .{ .frame = offset.index }, |
| 2760 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))), |
| 2761 | Type.u1, |
| 2762 | .{ .register = overflow_reg }, |
| 2763 | ); |
| 2764 | |
| 2765 | break :result result_mcv; |
| 2766 | }, |
| 2767 | .signed => { |
| 2768 | switch (int_info.bits) { |
| 2769 | 64 => { |
| 2770 | _ = try func.addInst(.{ |
| 2771 | .tag = .slt, |
| 2772 | .ops = .rrr, |
| 2773 | .data = .{ .r_type = .{ |
| 2774 | .rd = overflow_reg, |
| 2775 | .rs1 = overflow_reg, |
| 2776 | .rs2 = rhs_reg, |
| 2777 | } }, |
| 2778 | }); |
| 2779 | |
| 2780 | _ = try func.addInst(.{ |
| 2781 | .tag = .slt, |
| 2782 | .ops = .rrr, |
| 2783 | .data = .{ .r_type = .{ |
| 2784 | .rd = rhs_reg, |
| 2785 | .rs1 = rhs_reg, |
| 2786 | .rs2 = lhs_reg, |
| 2787 | } }, |
| 2788 | }); |
| 2789 | |
| 2790 | _ = try func.addInst(.{ |
| 2791 | .tag = .xor, |
| 2792 | .ops = .rrr, |
| 2793 | .data = .{ .r_type = .{ |
| 2794 | .rd = lhs_reg, |
| 2795 | .rs1 = overflow_reg, |
| 2796 | .rs2 = rhs_reg, |
| 2797 | } }, |
| 2798 | }); |
| 2799 | |
| 2800 | try func.genBinOp( |
| 2801 | .cmp_neq, |
| 2802 | .{ .register = overflow_reg }, |
| 2803 | Type.usize, |
| 2804 | .{ .register = rhs_reg }, |
| 2805 | Type.usize, |
| 2806 | overflow_reg, |
| 2807 | ); |
| 2808 | |
| 2809 | try func.genSetMem( |
| 2810 | .{ .frame = offset.index }, |
| 2811 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))), |
| 2812 | Type.u1, |
| 2813 | .{ .register = overflow_reg }, |
| 2814 | ); |
| 2815 | |
| 2816 | break :result result_mcv; |
| 2817 | }, |
| 2818 | else => |int_bits| return func.fail("TODO: airSubWithOverflow signed {}", .{int_bits}), |
| 2819 | } |
| 2820 | }, |
| 2821 | } |
| 2822 | }; |
| 2823 | |
| 2824 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2315 | 2825 | } |
| 2316 | 2826 | |
| 2317 | | fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2318 | | //const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 2319 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2320 | | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2321 | | const zcu = self.bin_file.comp.module.?; |
| 2322 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2323 | | const lhs = try self.resolveInst(extra.lhs); |
| 2324 | | const rhs = try self.resolveInst(extra.rhs); |
| 2325 | | const lhs_ty = self.typeOf(extra.lhs); |
| 2326 | | const rhs_ty = self.typeOf(extra.rhs); |
| 2827 | fn airMulWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2828 | const zcu = func.bin_file.comp.module.?; |
| 2829 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2830 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2831 | |
| 2832 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2833 | const lhs = try func.resolveInst(extra.lhs); |
| 2834 | const rhs = try func.resolveInst(extra.rhs); |
| 2835 | const lhs_ty = func.typeOf(extra.lhs); |
| 2836 | const rhs_ty = func.typeOf(extra.rhs); |
| 2837 | |
| 2838 | const tuple_ty = func.typeOfIndex(inst); |
| 2839 | |
| 2840 | // genSetReg needs to support register_offset src_mcv for this to be true. |
| 2841 | const result_mcv = try func.allocRegOrMem(tuple_ty, inst, false); |
| 2842 | |
| 2843 | const result_off: i32 = @intCast(tuple_ty.structFieldOffset(0, zcu)); |
| 2844 | const overflow_off: i32 = @intCast(tuple_ty.structFieldOffset(1, zcu)); |
| 2845 | |
| 2846 | const dest_reg, const dest_lock = try func.allocReg(.int); |
| 2847 | defer func.register_manager.unlockReg(dest_lock); |
| 2848 | |
| 2849 | try func.genBinOp( |
| 2850 | .mul, |
| 2851 | lhs, |
| 2852 | lhs_ty, |
| 2853 | rhs, |
| 2854 | rhs_ty, |
| 2855 | dest_reg, |
| 2856 | ); |
| 2857 | |
| 2858 | try func.genCopy( |
| 2859 | lhs_ty, |
| 2860 | result_mcv.offset(result_off), |
| 2861 | .{ .register = dest_reg }, |
| 2862 | ); |
| 2327 | 2863 | |
| 2328 | 2864 | switch (lhs_ty.zigTypeTag(zcu)) { |
| 2329 | | else => |x| return self.fail("TODO: airMulWithOverflow {s}", .{@tagName(x)}), |
| 2865 | else => |x| return func.fail("TODO: airMulWithOverflow {s}", .{@tagName(x)}), |
| 2330 | 2866 | .Int => { |
| 2331 | 2867 | assert(lhs_ty.eql(rhs_ty, zcu)); |
| 2332 | 2868 | const int_info = lhs_ty.intInfo(zcu); |
| 2333 | 2869 | switch (int_info.bits) { |
| 2334 | 2870 | 1...32 => { |
| 2335 | | if (self.hasFeature(.m)) { |
| 2336 | | const dest = try self.binOp(.mul, lhs, lhs_ty, rhs, rhs_ty); |
| 2337 | | |
| 2338 | | const add_result_lock = self.register_manager.lockRegAssumeUnused(dest.register); |
| 2339 | | defer self.register_manager.unlockReg(add_result_lock); |
| 2340 | | |
| 2341 | | const tuple_ty = self.typeOfIndex(inst); |
| 2342 | | |
| 2343 | | // TODO: optimization, set this to true. needs the other struct access stuff to support |
| 2344 | | // accessing registers. |
| 2345 | | const result_mcv = try self.allocRegOrMem(inst, false); |
| 2346 | | |
| 2347 | | const result_off: i32 = @intCast(tuple_ty.structFieldOffset(0, zcu)); |
| 2348 | | const overflow_off: i32 = @intCast(tuple_ty.structFieldOffset(1, zcu)); |
| 2349 | | |
| 2350 | | try self.genSetStack(lhs_ty, result_mcv.offset(result_off).load_frame, dest); |
| 2351 | | |
| 2352 | | if (int_info.bits >= 8 and math.isPowerOfTwo(int_info.bits)) { |
| 2353 | | if (int_info.signedness == .unsigned) { |
| 2354 | | switch (int_info.bits) { |
| 2355 | | 1...8 => { |
| 2356 | | const max_val = std.math.pow(u16, 2, int_info.bits) - 1; |
| 2357 | | |
| 2358 | | const overflow_reg, const overflow_lock = try self.allocReg(); |
| 2359 | | defer self.register_manager.unlockReg(overflow_lock); |
| 2360 | | |
| 2361 | | const add_reg, const add_lock = blk: { |
| 2362 | | if (dest == .register) break :blk .{ dest.register, null }; |
| 2363 | | |
| 2364 | | const add_reg, const add_lock = try self.allocReg(); |
| 2365 | | try self.genSetReg(lhs_ty, add_reg, dest); |
| 2366 | | break :blk .{ add_reg, add_lock }; |
| 2367 | | }; |
| 2368 | | defer if (add_lock) |lock| self.register_manager.unlockReg(lock); |
| 2369 | | |
| 2370 | | _ = try self.addInst(.{ |
| 2371 | | .tag = .andi, |
| 2372 | | .ops = .rri, |
| 2373 | | .data = .{ .i_type = .{ |
| 2374 | | .rd = overflow_reg, |
| 2375 | | .rs1 = add_reg, |
| 2376 | | .imm12 = Immediate.s(max_val), |
| 2377 | | } }, |
| 2378 | | }); |
| 2379 | | |
| 2380 | | const overflow_mcv = try self.binOp( |
| 2381 | | .cmp_neq, |
| 2382 | | .{ .register = overflow_reg }, |
| 2383 | | lhs_ty, |
| 2384 | | .{ .register = add_reg }, |
| 2385 | | lhs_ty, |
| 2386 | | ); |
| 2387 | | |
| 2388 | | try self.genSetStack( |
| 2389 | | lhs_ty, |
| 2390 | | result_mcv.offset(overflow_off).load_frame, |
| 2391 | | overflow_mcv, |
| 2392 | | ); |
| 2393 | | |
| 2394 | | break :result result_mcv; |
| 2395 | | }, |
| 2396 | | |
| 2397 | | else => return self.fail("TODO: airMulWithOverflow check for size {d}", .{int_info.bits}), |
| 2398 | | } |
| 2399 | | } else { |
| 2400 | | return self.fail("TODO: airMulWithOverflow calculate carry for signed addition", .{}); |
| 2871 | if (int_info.bits >= 8 and math.isPowerOfTwo(int_info.bits)) { |
| 2872 | if (int_info.signedness == .unsigned) { |
| 2873 | switch (int_info.bits) { |
| 2874 | 1...8 => { |
| 2875 | const max_val = std.math.pow(u16, 2, int_info.bits) - 1; |
| 2876 | |
| 2877 | const add_reg, const add_lock = try func.promoteReg(lhs_ty, lhs); |
| 2878 | defer if (add_lock) |lock| func.register_manager.unlockReg(lock); |
| 2879 | |
| 2880 | const overflow_reg, const overflow_lock = try func.allocReg(.int); |
| 2881 | defer func.register_manager.unlockReg(overflow_lock); |
| 2882 | |
| 2883 | _ = try func.addInst(.{ |
| 2884 | .tag = .andi, |
| 2885 | .ops = .rri, |
| 2886 | .data = .{ .i_type = .{ |
| 2887 | .rd = overflow_reg, |
| 2888 | .rs1 = add_reg, |
| 2889 | .imm12 = Immediate.s(max_val), |
| 2890 | } }, |
| 2891 | }); |
| 2892 | |
| 2893 | try func.genBinOp( |
| 2894 | .cmp_neq, |
| 2895 | .{ .register = overflow_reg }, |
| 2896 | lhs_ty, |
| 2897 | .{ .register = add_reg }, |
| 2898 | lhs_ty, |
| 2899 | overflow_reg, |
| 2900 | ); |
| 2901 | |
| 2902 | try func.genCopy( |
| 2903 | lhs_ty, |
| 2904 | result_mcv.offset(overflow_off), |
| 2905 | .{ .register = overflow_reg }, |
| 2906 | ); |
| 2907 | |
| 2908 | break :result result_mcv; |
| 2909 | }, |
| 2910 | |
| 2911 | else => return func.fail("TODO: airMulWithOverflow check for size {d}", .{int_info.bits}), |
| 2401 | 2912 | } |
| 2402 | 2913 | } else { |
| 2403 | | return self.fail("TODO: airMulWithOverflow with < 8 bits or non-pow of 2", .{}); |
| 2914 | return func.fail("TODO: airMulWithOverflow calculate carry for signed addition", .{}); |
| 2404 | 2915 | } |
| 2405 | 2916 | } else { |
| 2406 | | return self.fail("TODO: emulate mul for targets without M feature", .{}); |
| 2917 | return func.fail("TODO: airMulWithOverflow with < 8 bits or non-pow of 2", .{}); |
| 2407 | 2918 | } |
| 2408 | 2919 | }, |
| 2409 | | else => return self.fail("TODO: airMulWithOverflow larger than 32-bit mul", .{}), |
| 2920 | else => return func.fail("TODO: airMulWithOverflow larger than 32-bit mul", .{}), |
| 2410 | 2921 | } |
| 2411 | 2922 | }, |
| 2412 | 2923 | } |
| 2413 | 2924 | }; |
| 2414 | 2925 | |
| 2415 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2926 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2416 | 2927 | } |
| 2417 | 2928 | |
| 2418 | | fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2419 | | _ = inst; |
| 2420 | | return self.fail("TODO implement airShlWithOverflow for {}", .{self.target.cpu.arch}); |
| 2421 | | } |
| 2422 | | |
| 2423 | | fn airDiv(self: *Self, inst: Air.Inst.Index) !void { |
| 2424 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2425 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement div for {}", .{self.target.cpu.arch}); |
| 2426 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2427 | | } |
| 2428 | | |
| 2429 | | fn airRem(self: *Self, inst: Air.Inst.Index) !void { |
| 2430 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2431 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement rem for {}", .{self.target.cpu.arch}); |
| 2432 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2929 | fn airShlWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2930 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2931 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airShlWithOverflow", .{}); |
| 2932 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2433 | 2933 | } |
| 2434 | 2934 | |
| 2435 | | fn airMod(self: *Self, inst: Air.Inst.Index) !void { |
| 2436 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2437 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement zcu for {}", .{self.target.cpu.arch}); |
| 2438 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2935 | fn airAddSat(func: *Func, inst: Air.Inst.Index) !void { |
| 2936 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2937 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airAddSat", .{}); |
| 2938 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2439 | 2939 | } |
| 2440 | 2940 | |
| 2441 | | fn airBitAnd(self: *Self, inst: Air.Inst.Index) !void { |
| 2442 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2443 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement bitwise and for {}", .{self.target.cpu.arch}); |
| 2444 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2941 | fn airSubSat(func: *Func, inst: Air.Inst.Index) !void { |
| 2942 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2943 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airSubSat", .{}); |
| 2944 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2445 | 2945 | } |
| 2446 | 2946 | |
| 2447 | | fn airBitOr(self: *Self, inst: Air.Inst.Index) !void { |
| 2448 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2449 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement bitwise or for {}", .{self.target.cpu.arch}); |
| 2450 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2947 | fn airMulSat(func: *Func, inst: Air.Inst.Index) !void { |
| 2948 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2949 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airMulSat", .{}); |
| 2950 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2451 | 2951 | } |
| 2452 | 2952 | |
| 2453 | | fn airXor(self: *Self, inst: Air.Inst.Index) !void { |
| 2454 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2455 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement xor for {}", .{self.target.cpu.arch}); |
| 2456 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2953 | fn airShlSat(func: *Func, inst: Air.Inst.Index) !void { |
| 2954 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2955 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airShlSat", .{}); |
| 2956 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2457 | 2957 | } |
| 2458 | 2958 | |
| 2459 | | fn airShl(self: *Self, inst: Air.Inst.Index) !void { |
| 2460 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2461 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2462 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2463 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2464 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2465 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2959 | fn airOptionalPayload(func: *Func, inst: Air.Inst.Index) !void { |
| 2960 | const zcu = func.bin_file.comp.module.?; |
| 2961 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2962 | const result: MCValue = result: { |
| 2963 | const pl_ty = func.typeOfIndex(inst); |
| 2964 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .none; |
| 2965 | |
| 2966 | const opt_mcv = try func.resolveInst(ty_op.operand); |
| 2967 | if (func.reuseOperand(inst, ty_op.operand, 0, opt_mcv)) { |
| 2968 | switch (opt_mcv) { |
| 2969 | .register => |pl_reg| try func.truncateRegister(pl_ty, pl_reg), |
| 2970 | else => {}, |
| 2971 | } |
| 2972 | break :result opt_mcv; |
| 2973 | } |
| 2466 | 2974 | |
| 2467 | | break :result try self.binOp(.shl, lhs, lhs_ty, rhs, rhs_ty); |
| 2975 | const pl_mcv = try func.allocRegOrMem(pl_ty, inst, true); |
| 2976 | try func.genCopy(pl_ty, pl_mcv, opt_mcv); |
| 2977 | break :result pl_mcv; |
| 2468 | 2978 | }; |
| 2469 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2470 | | } |
| 2471 | | |
| 2472 | | fn airShlSat(self: *Self, inst: Air.Inst.Index) !void { |
| 2473 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2474 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement shl_sat for {}", .{self.target.cpu.arch}); |
| 2475 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2476 | | } |
| 2477 | | |
| 2478 | | fn airShr(self: *Self, inst: Air.Inst.Index) !void { |
| 2479 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2480 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement shr for {}", .{self.target.cpu.arch}); |
| 2481 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2482 | | } |
| 2483 | | |
| 2484 | | fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 2485 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2486 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .optional_payload for {}", .{self.target.cpu.arch}); |
| 2487 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2979 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2488 | 2980 | } |
| 2489 | 2981 | |
| 2490 | | fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2491 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2492 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .optional_payload_ptr for {}", .{self.target.cpu.arch}); |
| 2493 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2982 | fn airOptionalPayloadPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 2983 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2984 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .optional_payload_ptr for {}", .{func.target.cpu.arch}); |
| 2985 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2494 | 2986 | } |
| 2495 | 2987 | |
| 2496 | | fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 2497 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2498 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .optional_payload_ptr_set for {}", .{self.target.cpu.arch}); |
| 2499 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2988 | fn airOptionalPayloadPtrSet(func: *Func, inst: Air.Inst.Index) !void { |
| 2989 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2990 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .optional_payload_ptr_set for {}", .{func.target.cpu.arch}); |
| 2991 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2500 | 2992 | } |
| 2501 | 2993 | |
| 2502 | | fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { |
| 2503 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2504 | | const zcu = self.bin_file.comp.module.?; |
| 2505 | | const err_union_ty = self.typeOf(ty_op.operand); |
| 2994 | fn airUnwrapErrErr(func: *Func, inst: Air.Inst.Index) !void { |
| 2995 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2996 | const zcu = func.bin_file.comp.module.?; |
| 2997 | const err_union_ty = func.typeOf(ty_op.operand); |
| 2506 | 2998 | const err_ty = err_union_ty.errorUnionSet(zcu); |
| 2507 | 2999 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 2508 | | const operand = try self.resolveInst(ty_op.operand); |
| 3000 | const operand = try func.resolveInst(ty_op.operand); |
| 2509 | 3001 | |
| 2510 | 3002 | const result: MCValue = result: { |
| 2511 | 3003 | if (err_ty.errorSetIsEmpty(zcu)) { |
| ... | ... | @@ -2520,43 +3012,47 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { |
| 2520 | 3012 | |
| 2521 | 3013 | switch (operand) { |
| 2522 | 3014 | .register => |reg| { |
| 2523 | | const eu_lock = self.register_manager.lockReg(reg); |
| 2524 | | defer if (eu_lock) |lock| self.register_manager.unlockReg(lock); |
| 2525 | | |
| 2526 | | var result = try self.copyToNewRegister(inst, operand); |
| 3015 | const eu_lock = func.register_manager.lockReg(reg); |
| 3016 | defer if (eu_lock) |lock| func.register_manager.unlockReg(lock); |
| 2527 | 3017 | |
| 3018 | const result = try func.copyToNewRegister(inst, operand); |
| 2528 | 3019 | if (err_off > 0) { |
| 2529 | | result = try self.binOp( |
| 3020 | try func.genBinOp( |
| 2530 | 3021 | .shr, |
| 2531 | 3022 | result, |
| 2532 | 3023 | err_union_ty, |
| 2533 | 3024 | .{ .immediate = @as(u6, @intCast(err_off * 8)) }, |
| 2534 | 3025 | Type.u8, |
| 3026 | result.register, |
| 2535 | 3027 | ); |
| 2536 | 3028 | } |
| 2537 | 3029 | break :result result; |
| 2538 | 3030 | }, |
| 2539 | | else => return self.fail("TODO implement unwrap_err_err for {}", .{operand}), |
| 3031 | .load_frame => |frame_addr| break :result .{ .load_frame = .{ |
| 3032 | .index = frame_addr.index, |
| 3033 | .off = frame_addr.off + @as(i32, @intCast(err_off)), |
| 3034 | } }, |
| 3035 | else => return func.fail("TODO implement unwrap_err_err for {}", .{operand}), |
| 2540 | 3036 | } |
| 2541 | 3037 | }; |
| 2542 | 3038 | |
| 2543 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3039 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2544 | 3040 | } |
| 2545 | 3041 | |
| 2546 | | fn airUnwrapErrPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 2547 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2548 | | const operand_ty = self.typeOf(ty_op.operand); |
| 2549 | | const operand = try self.resolveInst(ty_op.operand); |
| 2550 | | const result = try self.genUnwrapErrUnionPayloadMir(operand_ty, operand); |
| 2551 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3042 | fn airUnwrapErrPayload(func: *Func, inst: Air.Inst.Index) !void { |
| 3043 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3044 | const operand_ty = func.typeOf(ty_op.operand); |
| 3045 | const operand = try func.resolveInst(ty_op.operand); |
| 3046 | const result = try func.genUnwrapErrUnionPayloadMir(operand_ty, operand); |
| 3047 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2552 | 3048 | } |
| 2553 | 3049 | |
| 2554 | 3050 | fn genUnwrapErrUnionPayloadMir( |
| 2555 | | self: *Self, |
| 3051 | func: *Func, |
| 2556 | 3052 | err_union_ty: Type, |
| 2557 | 3053 | err_union: MCValue, |
| 2558 | 3054 | ) !MCValue { |
| 2559 | | const zcu = self.bin_file.comp.module.?; |
| 3055 | const zcu = func.bin_file.comp.module.?; |
| 2560 | 3056 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 2561 | 3057 | |
| 2562 | 3058 | const result: MCValue = result: { |
| ... | ... | @@ -2569,24 +3065,23 @@ fn genUnwrapErrUnionPayloadMir( |
| 2569 | 3065 | .off = frame_addr.off + payload_off, |
| 2570 | 3066 | } }, |
| 2571 | 3067 | .register => |reg| { |
| 2572 | | const eu_lock = self.register_manager.lockReg(reg); |
| 2573 | | defer if (eu_lock) |lock| self.register_manager.unlockReg(lock); |
| 2574 | | |
| 2575 | | var result: MCValue = .{ .register = try self.copyToTmpRegister(err_union_ty, err_union) }; |
| 3068 | const eu_lock = func.register_manager.lockReg(reg); |
| 3069 | defer if (eu_lock) |lock| func.register_manager.unlockReg(lock); |
| 2576 | 3070 | |
| 3071 | const result_reg = try func.copyToTmpRegister(err_union_ty, err_union); |
| 2577 | 3072 | if (payload_off > 0) { |
| 2578 | | result = try self.binOp( |
| 3073 | try func.genBinOp( |
| 2579 | 3074 | .shr, |
| 2580 | | result, |
| 3075 | .{ .register = result_reg }, |
| 2581 | 3076 | err_union_ty, |
| 2582 | 3077 | .{ .immediate = @as(u6, @intCast(payload_off * 8)) }, |
| 2583 | 3078 | Type.u8, |
| 3079 | result_reg, |
| 2584 | 3080 | ); |
| 2585 | 3081 | } |
| 2586 | | |
| 2587 | | break :result result; |
| 3082 | break :result .{ .register = result_reg }; |
| 2588 | 3083 | }, |
| 2589 | | else => return self.fail("TODO implement genUnwrapErrUnionPayloadMir for {}", .{err_union}), |
| 3084 | else => return func.fail("TODO implement genUnwrapErrUnionPayloadMir for {}", .{err_union}), |
| 2590 | 3085 | } |
| 2591 | 3086 | }; |
| 2592 | 3087 | |
| ... | ... | @@ -2594,99 +3089,140 @@ fn genUnwrapErrUnionPayloadMir( |
| 2594 | 3089 | } |
| 2595 | 3090 | |
| 2596 | 3091 | // *(E!T) -> E |
| 2597 | | fn airUnwrapErrErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2598 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2599 | | 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}); |
| 2600 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3092 | fn airUnwrapErrErrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3093 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3094 | 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}); |
| 3095 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2601 | 3096 | } |
| 2602 | 3097 | |
| 2603 | 3098 | // *(E!T) -> *T |
| 2604 | | fn airUnwrapErrPayloadPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2605 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2606 | | 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}); |
| 2607 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3099 | fn airUnwrapErrPayloadPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3100 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3101 | 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}); |
| 3102 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2608 | 3103 | } |
| 2609 | 3104 | |
| 2610 | | fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 2611 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2612 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .errunion_payload_ptr_set for {}", .{self.target.cpu.arch}); |
| 2613 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3105 | fn airErrUnionPayloadPtrSet(func: *Func, inst: Air.Inst.Index) !void { |
| 3106 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3107 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .errunion_payload_ptr_set for {}", .{func.target.cpu.arch}); |
| 3108 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2614 | 3109 | } |
| 2615 | 3110 | |
| 2616 | | fn airErrReturnTrace(self: *Self, inst: Air.Inst.Index) !void { |
| 2617 | | const result: MCValue = if (self.liveness.isUnused(inst)) |
| 3111 | fn airErrReturnTrace(func: *Func, inst: Air.Inst.Index) !void { |
| 3112 | const result: MCValue = if (func.liveness.isUnused(inst)) |
| 2618 | 3113 | .unreach |
| 2619 | 3114 | else |
| 2620 | | return self.fail("TODO implement airErrReturnTrace for {}", .{self.target.cpu.arch}); |
| 2621 | | return self.finishAir(inst, result, .{ .none, .none, .none }); |
| 3115 | return func.fail("TODO implement airErrReturnTrace for {}", .{func.target.cpu.arch}); |
| 3116 | return func.finishAir(inst, result, .{ .none, .none, .none }); |
| 2622 | 3117 | } |
| 2623 | 3118 | |
| 2624 | | fn airSetErrReturnTrace(self: *Self, inst: Air.Inst.Index) !void { |
| 3119 | fn airSetErrReturnTrace(func: *Func, inst: Air.Inst.Index) !void { |
| 2625 | 3120 | _ = inst; |
| 2626 | | return self.fail("TODO implement airSetErrReturnTrace for {}", .{self.target.cpu.arch}); |
| 3121 | return func.fail("TODO implement airSetErrReturnTrace for {}", .{func.target.cpu.arch}); |
| 2627 | 3122 | } |
| 2628 | 3123 | |
| 2629 | | fn airSaveErrReturnTraceIndex(self: *Self, inst: Air.Inst.Index) !void { |
| 3124 | fn airSaveErrReturnTraceIndex(func: *Func, inst: Air.Inst.Index) !void { |
| 2630 | 3125 | _ = inst; |
| 2631 | | return self.fail("TODO implement airSaveErrReturnTraceIndex for {}", .{self.target.cpu.arch}); |
| 3126 | return func.fail("TODO implement airSaveErrReturnTraceIndex for {}", .{func.target.cpu.arch}); |
| 2632 | 3127 | } |
| 2633 | 3128 | |
| 2634 | | fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 2635 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2636 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2637 | | const zcu = self.bin_file.comp.module.?; |
| 2638 | | const optional_ty = self.typeOfIndex(inst); |
| 3129 | fn airWrapOptional(func: *Func, inst: Air.Inst.Index) !void { |
| 3130 | const zcu = func.bin_file.comp.module.?; |
| 3131 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3132 | const result: MCValue = result: { |
| 3133 | const pl_ty = func.typeOf(ty_op.operand); |
| 3134 | if (!pl_ty.hasRuntimeBits(zcu)) break :result .{ .immediate = 1 }; |
| 3135 | |
| 3136 | const opt_ty = func.typeOfIndex(inst); |
| 3137 | const pl_mcv = try func.resolveInst(ty_op.operand); |
| 3138 | const same_repr = opt_ty.optionalReprIsPayload(zcu); |
| 3139 | if (same_repr and func.reuseOperand(inst, ty_op.operand, 0, pl_mcv)) break :result pl_mcv; |
| 3140 | |
| 3141 | const pl_lock: ?RegisterLock = switch (pl_mcv) { |
| 3142 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 3143 | else => null, |
| 3144 | }; |
| 3145 | defer if (pl_lock) |lock| func.register_manager.unlockReg(lock); |
| 2639 | 3146 | |
| 2640 | | // Optional with a zero-bit payload type is just a boolean true |
| 2641 | | if (optional_ty.abiSize(zcu) == 1) |
| 2642 | | break :result MCValue{ .immediate = 1 }; |
| 3147 | const opt_mcv = try func.allocRegOrMem(opt_ty, inst, true); |
| 3148 | try func.genCopy(pl_ty, opt_mcv, pl_mcv); |
| 2643 | 3149 | |
| 2644 | | return self.fail("TODO implement wrap optional for {}", .{self.target.cpu.arch}); |
| 3150 | if (!same_repr) { |
| 3151 | const pl_abi_size: i32 = @intCast(pl_ty.abiSize(zcu)); |
| 3152 | switch (opt_mcv) { |
| 3153 | .load_frame => |frame_addr| try func.genSetMem( |
| 3154 | .{ .frame = frame_addr.index }, |
| 3155 | frame_addr.off + pl_abi_size, |
| 3156 | Type.u8, |
| 3157 | .{ .immediate = 1 }, |
| 3158 | ), |
| 3159 | .register => return func.fail("TODO: airWrapOption opt_mcv register", .{}), |
| 3160 | else => unreachable, |
| 3161 | } |
| 3162 | } |
| 3163 | break :result opt_mcv; |
| 2645 | 3164 | }; |
| 2646 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3165 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2647 | 3166 | } |
| 2648 | 3167 | |
| 2649 | 3168 | /// T to E!T |
| 2650 | | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 2651 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2652 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement wrap errunion payload for {}", .{self.target.cpu.arch}); |
| 2653 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3169 | fn airWrapErrUnionPayload(func: *Func, inst: Air.Inst.Index) !void { |
| 3170 | const zcu = func.bin_file.comp.module.?; |
| 3171 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3172 | |
| 3173 | const eu_ty = ty_op.ty.toType(); |
| 3174 | const pl_ty = eu_ty.errorUnionPayload(zcu); |
| 3175 | const err_ty = eu_ty.errorUnionSet(zcu); |
| 3176 | const operand = try func.resolveInst(ty_op.operand); |
| 3177 | |
| 3178 | const result: MCValue = result: { |
| 3179 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .{ .immediate = 0 }; |
| 3180 | |
| 3181 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu)); |
| 3182 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, zcu)); |
| 3183 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, zcu)); |
| 3184 | try func.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, operand); |
| 3185 | try func.genSetMem(.{ .frame = frame_index }, err_off, err_ty, .{ .immediate = 0 }); |
| 3186 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 3187 | }; |
| 3188 | |
| 3189 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2654 | 3190 | } |
| 2655 | 3191 | |
| 2656 | 3192 | /// E to E!T |
| 2657 | | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { |
| 2658 | | const zcu = self.bin_file.comp.module.?; |
| 2659 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3193 | fn airWrapErrUnionErr(func: *Func, inst: Air.Inst.Index) !void { |
| 3194 | const zcu = func.bin_file.comp.module.?; |
| 3195 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2660 | 3196 | |
| 2661 | 3197 | const eu_ty = ty_op.ty.toType(); |
| 2662 | 3198 | const pl_ty = eu_ty.errorUnionPayload(zcu); |
| 2663 | 3199 | const err_ty = eu_ty.errorUnionSet(zcu); |
| 2664 | 3200 | |
| 2665 | 3201 | const result: MCValue = result: { |
| 2666 | | if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result try self.resolveInst(ty_op.operand); |
| 3202 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result try func.resolveInst(ty_op.operand); |
| 2667 | 3203 | |
| 2668 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu)); |
| 3204 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu)); |
| 2669 | 3205 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, zcu)); |
| 2670 | 3206 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, zcu)); |
| 2671 | | try self.genSetStack(pl_ty, .{ .index = frame_index, .off = pl_off }, .undef); |
| 2672 | | const operand = try self.resolveInst(ty_op.operand); |
| 2673 | | try self.genSetStack(err_ty, .{ .index = frame_index, .off = err_off }, operand); |
| 3207 | try func.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, .undef); |
| 3208 | const operand = try func.resolveInst(ty_op.operand); |
| 3209 | try func.genSetMem(.{ .frame = frame_index }, err_off, err_ty, operand); |
| 2674 | 3210 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 2675 | 3211 | }; |
| 2676 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3212 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2677 | 3213 | } |
| 2678 | 3214 | |
| 2679 | | fn airTry(self: *Self, inst: Air.Inst.Index) !void { |
| 2680 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 2681 | | const extra = self.air.extraData(Air.Try, pl_op.payload); |
| 2682 | | const body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len]); |
| 2683 | | const operand_ty = self.typeOf(pl_op.operand); |
| 2684 | | const result = try self.genTry(inst, pl_op.operand, body, operand_ty, false); |
| 2685 | | return self.finishAir(inst, result, .{ .none, .none, .none }); |
| 3215 | fn airTry(func: *Func, inst: Air.Inst.Index) !void { |
| 3216 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 3217 | const extra = func.air.extraData(Air.Try, pl_op.payload); |
| 3218 | const body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len]); |
| 3219 | const operand_ty = func.typeOf(pl_op.operand); |
| 3220 | const result = try func.genTry(inst, pl_op.operand, body, operand_ty, false); |
| 3221 | return func.finishAir(inst, result, .{ .none, .none, .none }); |
| 2686 | 3222 | } |
| 2687 | 3223 | |
| 2688 | 3224 | fn genTry( |
| 2689 | | self: *Self, |
| 3225 | func: *Func, |
| 2690 | 3226 | inst: Air.Inst.Index, |
| 2691 | 3227 | operand: Air.Inst.Ref, |
| 2692 | 3228 | body: []const Air.Inst.Index, |
| ... | ... | @@ -2695,180 +3231,204 @@ fn genTry( |
| 2695 | 3231 | ) !MCValue { |
| 2696 | 3232 | _ = operand_is_ptr; |
| 2697 | 3233 | |
| 2698 | | const liveness_cond_br = self.liveness.getCondBr(inst); |
| 3234 | const liveness_cond_br = func.liveness.getCondBr(inst); |
| 2699 | 3235 | |
| 2700 | | const operand_mcv = try self.resolveInst(operand); |
| 2701 | | const is_err_mcv = try self.isErr(null, operand_ty, operand_mcv); |
| 3236 | const operand_mcv = try func.resolveInst(operand); |
| 3237 | const is_err_mcv = try func.isErr(null, operand_ty, operand_mcv); |
| 2702 | 3238 | |
| 2703 | 3239 | // A branch to the false section. Uses beq. 1 is the default "true" state. |
| 2704 | | const reloc = try self.condBr(Type.anyerror, is_err_mcv); |
| 3240 | const reloc = try func.condBr(Type.anyerror, is_err_mcv); |
| 2705 | 3241 | |
| 2706 | | if (self.liveness.operandDies(inst, 0)) { |
| 2707 | | if (operand.toIndex()) |operand_inst| try self.processDeath(operand_inst); |
| 3242 | if (func.liveness.operandDies(inst, 0)) { |
| 3243 | if (operand.toIndex()) |operand_inst| try func.processDeath(operand_inst); |
| 2708 | 3244 | } |
| 2709 | 3245 | |
| 2710 | | self.scope_generation += 1; |
| 2711 | | const state = try self.saveState(); |
| 3246 | func.scope_generation += 1; |
| 3247 | const state = try func.saveState(); |
| 2712 | 3248 | |
| 2713 | | for (liveness_cond_br.else_deaths) |death| try self.processDeath(death); |
| 2714 | | try self.genBody(body); |
| 2715 | | try self.restoreState(state, &.{}, .{ |
| 3249 | for (liveness_cond_br.else_deaths) |death| try func.processDeath(death); |
| 3250 | try func.genBody(body); |
| 3251 | try func.restoreState(state, &.{}, .{ |
| 2716 | 3252 | .emit_instructions = false, |
| 2717 | 3253 | .update_tracking = true, |
| 2718 | 3254 | .resurrect = true, |
| 2719 | 3255 | .close_scope = true, |
| 2720 | 3256 | }); |
| 2721 | 3257 | |
| 2722 | | self.performReloc(reloc); |
| 3258 | func.performReloc(reloc); |
| 2723 | 3259 | |
| 2724 | | for (liveness_cond_br.then_deaths) |death| try self.processDeath(death); |
| 3260 | for (liveness_cond_br.then_deaths) |death| try func.processDeath(death); |
| 2725 | 3261 | |
| 2726 | | const result = if (self.liveness.isUnused(inst)) |
| 3262 | const result = if (func.liveness.isUnused(inst)) |
| 2727 | 3263 | .unreach |
| 2728 | 3264 | else |
| 2729 | | try self.genUnwrapErrUnionPayloadMir(operand_ty, operand_mcv); |
| 3265 | try func.genUnwrapErrUnionPayloadMir(operand_ty, operand_mcv); |
| 2730 | 3266 | |
| 2731 | 3267 | return result; |
| 2732 | 3268 | } |
| 2733 | 3269 | |
| 2734 | | fn airSlicePtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2735 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3270 | fn airSlicePtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3271 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2736 | 3272 | const result = result: { |
| 2737 | | const src_mcv = try self.resolveInst(ty_op.operand); |
| 2738 | | if (self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result src_mcv; |
| 3273 | const src_mcv = try func.resolveInst(ty_op.operand); |
| 3274 | if (func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result src_mcv; |
| 2739 | 3275 | |
| 2740 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 2741 | | const dst_ty = self.typeOfIndex(inst); |
| 2742 | | try self.genCopy(dst_ty, dst_mcv, src_mcv); |
| 3276 | const dst_mcv = try func.allocRegOrMem(func.typeOfIndex(inst), inst, true); |
| 3277 | const dst_ty = func.typeOfIndex(inst); |
| 3278 | try func.genCopy(dst_ty, dst_mcv, src_mcv); |
| 2743 | 3279 | break :result dst_mcv; |
| 2744 | 3280 | }; |
| 2745 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3281 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2746 | 3282 | } |
| 2747 | 3283 | |
| 2748 | | fn airSliceLen(self: *Self, inst: Air.Inst.Index) !void { |
| 2749 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2750 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2751 | | const src_mcv = try self.resolveInst(ty_op.operand); |
| 3284 | fn airSliceLen(func: *Func, inst: Air.Inst.Index) !void { |
| 3285 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3286 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3287 | const src_mcv = try func.resolveInst(ty_op.operand); |
| 3288 | const ty = func.typeOfIndex(inst); |
| 3289 | |
| 2752 | 3290 | switch (src_mcv) { |
| 2753 | 3291 | .load_frame => |frame_addr| { |
| 2754 | 3292 | const len_mcv: MCValue = .{ .load_frame = .{ |
| 2755 | 3293 | .index = frame_addr.index, |
| 2756 | 3294 | .off = frame_addr.off + 8, |
| 2757 | 3295 | } }; |
| 2758 | | if (self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv; |
| 3296 | if (func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv; |
| 2759 | 3297 | |
| 2760 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 2761 | | try self.genCopy(Type.usize, dst_mcv, len_mcv); |
| 3298 | const dst_mcv = try func.allocRegOrMem(ty, inst, true); |
| 3299 | try func.genCopy(Type.usize, dst_mcv, len_mcv); |
| 2762 | 3300 | break :result dst_mcv; |
| 2763 | 3301 | }, |
| 2764 | 3302 | .register_pair => |pair| { |
| 2765 | 3303 | const len_mcv: MCValue = .{ .register = pair[1] }; |
| 2766 | 3304 | |
| 2767 | | if (self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv; |
| 3305 | if (func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv; |
| 2768 | 3306 | |
| 2769 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 2770 | | try self.genCopy(Type.usize, dst_mcv, len_mcv); |
| 3307 | const dst_mcv = try func.allocRegOrMem(ty, inst, true); |
| 3308 | try func.genCopy(Type.usize, dst_mcv, len_mcv); |
| 2771 | 3309 | break :result dst_mcv; |
| 2772 | 3310 | }, |
| 2773 | | else => return self.fail("TODO airSliceLen for {}", .{src_mcv}), |
| 3311 | else => return func.fail("TODO airSliceLen for {}", .{src_mcv}), |
| 2774 | 3312 | } |
| 2775 | 3313 | }; |
| 2776 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3314 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2777 | 3315 | } |
| 2778 | 3316 | |
| 2779 | | fn airPtrSliceLenPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2780 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2781 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement ptr_slice_len_ptr for {}", .{self.target.cpu.arch}); |
| 2782 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3317 | fn airPtrSliceLenPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3318 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3319 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement ptr_slice_len_ptr for {}", .{func.target.cpu.arch}); |
| 3320 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2783 | 3321 | } |
| 2784 | 3322 | |
| 2785 | | fn airPtrSlicePtrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2786 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2787 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement ptr_slice_ptr_ptr for {}", .{self.target.cpu.arch}); |
| 2788 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3323 | fn airPtrSlicePtrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3324 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3325 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement ptr_slice_ptr_ptr for {}", .{func.target.cpu.arch}); |
| 3326 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2789 | 3327 | } |
| 2790 | 3328 | |
| 2791 | | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2792 | | const zcu = self.bin_file.comp.module.?; |
| 2793 | | const is_volatile = false; // TODO |
| 2794 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3329 | fn airSliceElemVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3330 | const mod = func.bin_file.comp.module.?; |
| 3331 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2795 | 3332 | |
| 2796 | | if (!is_volatile and self.liveness.isUnused(inst)) return self.finishAir( |
| 2797 | | inst, |
| 2798 | | .unreach, |
| 2799 | | .{ bin_op.lhs, bin_op.rhs, .none }, |
| 2800 | | ); |
| 2801 | 3333 | const result: MCValue = result: { |
| 2802 | | const slice_mcv = try self.resolveInst(bin_op.lhs); |
| 2803 | | const index_mcv = try self.resolveInst(bin_op.rhs); |
| 2804 | | |
| 2805 | | const slice_ty = self.typeOf(bin_op.lhs); |
| 2806 | | |
| 2807 | | const slice_ptr_field_type = slice_ty.slicePtrFieldType(zcu); |
| 3334 | const elem_ty = func.typeOfIndex(inst); |
| 3335 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result .none; |
| 3336 | |
| 3337 | const slice_ty = func.typeOf(bin_op.lhs); |
| 3338 | const slice_ptr_field_type = slice_ty.slicePtrFieldType(mod); |
| 3339 | const elem_ptr = try func.genSliceElemPtr(bin_op.lhs, bin_op.rhs); |
| 3340 | const dst_mcv = try func.allocRegOrMem(elem_ty, inst, false); |
| 3341 | try func.load(dst_mcv, elem_ptr, slice_ptr_field_type); |
| 3342 | break :result dst_mcv; |
| 3343 | }; |
| 3344 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3345 | } |
| 2808 | 3346 | |
| 2809 | | const index_lock: ?RegisterLock = if (index_mcv == .register) |
| 2810 | | self.register_manager.lockRegAssumeUnused(index_mcv.register) |
| 2811 | | else |
| 2812 | | null; |
| 2813 | | defer if (index_lock) |reg| self.register_manager.unlockReg(reg); |
| 3347 | fn airSliceElemPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3348 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3349 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3350 | const dst_mcv = try func.genSliceElemPtr(extra.lhs, extra.rhs); |
| 3351 | return func.finishAir(inst, dst_mcv, .{ extra.lhs, extra.rhs, .none }); |
| 3352 | } |
| 2814 | 3353 | |
| 2815 | | const base_mcv: MCValue = switch (slice_mcv) { |
| 2816 | | .load_frame, |
| 2817 | | .load_symbol, |
| 2818 | | => .{ .register = try self.copyToTmpRegister(slice_ptr_field_type, slice_mcv) }, |
| 2819 | | else => return self.fail("TODO slice_elem_val when slice is {}", .{slice_mcv}), |
| 2820 | | }; |
| 3354 | fn genSliceElemPtr(func: *Func, lhs: Air.Inst.Ref, rhs: Air.Inst.Ref) !MCValue { |
| 3355 | const zcu = func.bin_file.comp.module.?; |
| 3356 | const slice_ty = func.typeOf(lhs); |
| 3357 | const slice_mcv = try func.resolveInst(lhs); |
| 3358 | const slice_mcv_lock: ?RegisterLock = switch (slice_mcv) { |
| 3359 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 3360 | else => null, |
| 3361 | }; |
| 3362 | defer if (slice_mcv_lock) |lock| func.register_manager.unlockReg(lock); |
| 2821 | 3363 | |
| 2822 | | const dest = try self.allocRegOrMem(inst, true); |
| 2823 | | const addr = try self.binOp(.ptr_add, base_mcv, slice_ptr_field_type, index_mcv, Type.usize); |
| 2824 | | try self.load(dest, addr, slice_ptr_field_type); |
| 3364 | const elem_ty = slice_ty.childType(zcu); |
| 3365 | const elem_size = elem_ty.abiSize(zcu); |
| 2825 | 3366 | |
| 2826 | | break :result dest; |
| 3367 | const index_ty = func.typeOf(rhs); |
| 3368 | const index_mcv = try func.resolveInst(rhs); |
| 3369 | const index_mcv_lock: ?RegisterLock = switch (index_mcv) { |
| 3370 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 3371 | else => null, |
| 2827 | 3372 | }; |
| 2828 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2829 | | } |
| 3373 | defer if (index_mcv_lock) |lock| func.register_manager.unlockReg(lock); |
| 3374 | |
| 3375 | const offset_reg = try func.elemOffset(index_ty, index_mcv, elem_size); |
| 3376 | const offset_reg_lock = func.register_manager.lockRegAssumeUnused(offset_reg); |
| 3377 | defer func.register_manager.unlockReg(offset_reg_lock); |
| 3378 | |
| 3379 | const addr_reg, const addr_lock = try func.allocReg(.int); |
| 3380 | defer func.register_manager.unlockReg(addr_lock); |
| 3381 | try func.genSetReg(Type.usize, addr_reg, slice_mcv); |
| 3382 | |
| 3383 | _ = try func.addInst(.{ |
| 3384 | .tag = .add, |
| 3385 | .ops = .rrr, |
| 3386 | .data = .{ .r_type = .{ |
| 3387 | .rd = addr_reg, |
| 3388 | .rs1 = addr_reg, |
| 3389 | .rs2 = offset_reg, |
| 3390 | } }, |
| 3391 | }); |
| 2830 | 3392 | |
| 2831 | | fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2832 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2833 | | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2834 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement slice_elem_ptr for {}", .{self.target.cpu.arch}); |
| 2835 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 3393 | return .{ .register = addr_reg }; |
| 2836 | 3394 | } |
| 2837 | 3395 | |
| 2838 | | fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2839 | | const zcu = self.bin_file.comp.module.?; |
| 2840 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2841 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2842 | | const array_ty = self.typeOf(bin_op.lhs); |
| 2843 | | const array_mcv = try self.resolveInst(bin_op.lhs); |
| 3396 | fn airArrayElemVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3397 | const zcu = func.bin_file.comp.module.?; |
| 3398 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3399 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3400 | const result_ty = func.typeOfIndex(inst); |
| 3401 | |
| 3402 | const array_ty = func.typeOf(bin_op.lhs); |
| 3403 | const array_mcv = try func.resolveInst(bin_op.lhs); |
| 2844 | 3404 | |
| 2845 | | const index_mcv = try self.resolveInst(bin_op.rhs); |
| 2846 | | const index_ty = self.typeOf(bin_op.rhs); |
| 3405 | const index_mcv = try func.resolveInst(bin_op.rhs); |
| 3406 | const index_ty = func.typeOf(bin_op.rhs); |
| 2847 | 3407 | |
| 2848 | 3408 | const elem_ty = array_ty.childType(zcu); |
| 2849 | 3409 | const elem_abi_size = elem_ty.abiSize(zcu); |
| 2850 | 3410 | |
| 2851 | | const addr_reg, const addr_reg_lock = try self.allocReg(); |
| 2852 | | defer self.register_manager.unlockReg(addr_reg_lock); |
| 3411 | const addr_reg, const addr_reg_lock = try func.allocReg(.int); |
| 3412 | defer func.register_manager.unlockReg(addr_reg_lock); |
| 2853 | 3413 | |
| 2854 | 3414 | switch (array_mcv) { |
| 2855 | 3415 | .register => { |
| 2856 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initType(array_ty, zcu)); |
| 2857 | | try self.genSetStack(array_ty, .{ .index = frame_index }, array_mcv); |
| 2858 | | try self.genSetReg(Type.usize, addr_reg, .{ .lea_frame = .{ .index = frame_index } }); |
| 3416 | const frame_index = try func.allocFrameIndex(FrameAlloc.initType(array_ty, zcu)); |
| 3417 | try func.genSetMem(.{ .frame = frame_index }, 0, array_ty, array_mcv); |
| 3418 | try func.genSetReg(Type.usize, addr_reg, .{ .lea_frame = .{ .index = frame_index } }); |
| 2859 | 3419 | }, |
| 2860 | 3420 | .load_frame => |frame_addr| { |
| 2861 | | try self.genSetReg(Type.usize, addr_reg, .{ .lea_frame = frame_addr }); |
| 3421 | try func.genSetReg(Type.usize, addr_reg, .{ .lea_frame = frame_addr }); |
| 2862 | 3422 | }, |
| 2863 | | else => try self.genSetReg(Type.usize, addr_reg, array_mcv.address()), |
| 3423 | else => try func.genSetReg(Type.usize, addr_reg, array_mcv.address()), |
| 2864 | 3424 | } |
| 2865 | 3425 | |
| 2866 | | const offset_reg = try self.elemOffset(index_ty, index_mcv, elem_abi_size); |
| 2867 | | const offset_lock = self.register_manager.lockRegAssumeUnused(offset_reg); |
| 2868 | | defer self.register_manager.unlockReg(offset_lock); |
| 3426 | const offset_reg = try func.elemOffset(index_ty, index_mcv, elem_abi_size); |
| 3427 | const offset_lock = func.register_manager.lockRegAssumeUnused(offset_reg); |
| 3428 | defer func.register_manager.unlockReg(offset_lock); |
| 2869 | 3429 | |
| 2870 | | const dst_mcv = try self.allocRegOrMem(inst, false); |
| 2871 | | _ = try self.addInst(.{ |
| 3430 | const dst_mcv = try func.allocRegOrMem(result_ty, inst, false); |
| 3431 | _ = try func.addInst(.{ |
| 2872 | 3432 | .tag = .add, |
| 2873 | 3433 | .ops = .rrr, |
| 2874 | 3434 | .data = .{ .r_type = .{ |
| ... | ... | @@ -2877,163 +3437,225 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2877 | 3437 | .rs2 = addr_reg, |
| 2878 | 3438 | } }, |
| 2879 | 3439 | }); |
| 2880 | | try self.genCopy(elem_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } }); |
| 3440 | try func.genCopy(elem_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } }); |
| 2881 | 3441 | break :result dst_mcv; |
| 2882 | 3442 | }; |
| 2883 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3443 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2884 | 3444 | } |
| 2885 | 3445 | |
| 2886 | | fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3446 | fn airPtrElemVal(func: *Func, inst: Air.Inst.Index) !void { |
| 2887 | 3447 | const is_volatile = false; // TODO |
| 2888 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2889 | | 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}); |
| 2890 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3448 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3449 | 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}); |
| 3450 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2891 | 3451 | } |
| 2892 | 3452 | |
| 2893 | | fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2894 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2895 | | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2896 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement ptr_elem_ptr for {}", .{self.target.cpu.arch}); |
| 2897 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2898 | | } |
| 2899 | | |
| 2900 | | fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) !void { |
| 2901 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2902 | | _ = bin_op; |
| 2903 | | return self.fail("TODO implement airSetUnionTag for {}", .{self.target.cpu.arch}); |
| 2904 | | // return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2905 | | } |
| 2906 | | |
| 2907 | | fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) !void { |
| 2908 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2909 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airGetUnionTag for {}", .{self.target.cpu.arch}); |
| 2910 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2911 | | } |
| 2912 | | |
| 2913 | | fn airClz(self: *Self, inst: Air.Inst.Index) !void { |
| 2914 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2915 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airClz for {}", .{self.target.cpu.arch}); |
| 2916 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2917 | | } |
| 2918 | | |
| 2919 | | fn airCtz(self: *Self, inst: Air.Inst.Index) !void { |
| 2920 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2921 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2922 | | const operand = try self.resolveInst(ty_op.operand); |
| 2923 | | const operand_ty = self.typeOf(ty_op.operand); |
| 3453 | fn airPtrElemPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3454 | const zcu = func.bin_file.comp.module.?; |
| 3455 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3456 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2924 | 3457 | |
| 2925 | | const dest_reg = try self.register_manager.allocReg(inst, gp); |
| 2926 | | |
| 2927 | | const source_reg, const source_lock = blk: { |
| 2928 | | if (operand == .register) break :blk .{ operand.register, null }; |
| 3458 | const result = result: { |
| 3459 | const elem_ptr_ty = func.typeOfIndex(inst); |
| 3460 | const base_ptr_ty = func.typeOf(extra.lhs); |
| 2929 | 3461 | |
| 2930 | | const source_reg, const source_lock = try self.allocReg(); |
| 2931 | | try self.genSetReg(operand_ty, source_reg, operand); |
| 2932 | | break :blk .{ source_reg, source_lock }; |
| 3462 | const base_ptr_mcv = try func.resolveInst(extra.lhs); |
| 3463 | const base_ptr_lock: ?RegisterLock = switch (base_ptr_mcv) { |
| 3464 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 3465 | else => null, |
| 2933 | 3466 | }; |
| 2934 | | defer if (source_lock) |lock| self.register_manager.unlockReg(lock); |
| 3467 | defer if (base_ptr_lock) |lock| func.register_manager.unlockReg(lock); |
| 2935 | 3468 | |
| 2936 | | // TODO: the B extension for RISCV should have the ctz instruction, and we should use it. |
| 3469 | if (elem_ptr_ty.ptrInfo(zcu).flags.vector_index != .none) { |
| 3470 | break :result if (func.reuseOperand(inst, extra.lhs, 0, base_ptr_mcv)) |
| 3471 | base_ptr_mcv |
| 3472 | else |
| 3473 | try func.copyToNewRegister(inst, base_ptr_mcv); |
| 3474 | } |
| 2937 | 3475 | |
| 2938 | | try self.ctz(source_reg, dest_reg, operand_ty); |
| 3476 | const elem_ty = base_ptr_ty.elemType2(zcu); |
| 3477 | const elem_abi_size = elem_ty.abiSize(zcu); |
| 3478 | const index_ty = func.typeOf(extra.rhs); |
| 3479 | const index_mcv = try func.resolveInst(extra.rhs); |
| 3480 | const index_lock: ?RegisterLock = switch (index_mcv) { |
| 3481 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 3482 | else => null, |
| 3483 | }; |
| 3484 | defer if (index_lock) |lock| func.register_manager.unlockReg(lock); |
| 3485 | |
| 3486 | const offset_reg = try func.elemOffset(index_ty, index_mcv, elem_abi_size); |
| 3487 | const offset_reg_lock = func.register_manager.lockRegAssumeUnused(offset_reg); |
| 3488 | defer func.register_manager.unlockReg(offset_reg_lock); |
| 3489 | |
| 3490 | const result_reg, const result_lock = try func.allocReg(.int); |
| 3491 | defer func.register_manager.unlockReg(result_lock); |
| 3492 | |
| 3493 | try func.genBinOp( |
| 3494 | .ptr_add, |
| 3495 | base_ptr_mcv, |
| 3496 | base_ptr_ty, |
| 3497 | .{ .register = offset_reg }, |
| 3498 | Type.usize, |
| 3499 | result_reg, |
| 3500 | ); |
| 2939 | 3501 | |
| 2940 | | break :result .{ .register = dest_reg }; |
| 3502 | break :result MCValue{ .register = result_reg }; |
| 2941 | 3503 | }; |
| 2942 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3504 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2943 | 3505 | } |
| 2944 | 3506 | |
| 2945 | | fn ctz(self: *Self, src: Register, dst: Register, ty: Type) !void { |
| 2946 | | const zcu = self.bin_file.comp.module.?; |
| 2947 | | const length = (ty.abiSize(zcu) * 8) - 1; |
| 2948 | | |
| 2949 | | const count_reg, const count_lock = try self.allocReg(); |
| 2950 | | defer self.register_manager.unlockReg(count_lock); |
| 2951 | | |
| 2952 | | const len_reg, const len_lock = try self.allocReg(); |
| 2953 | | defer self.register_manager.unlockReg(len_lock); |
| 3507 | fn airSetUnionTag(func: *Func, inst: Air.Inst.Index) !void { |
| 3508 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3509 | _ = bin_op; |
| 3510 | return func.fail("TODO implement airSetUnionTag for {}", .{func.target.cpu.arch}); |
| 3511 | // return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3512 | } |
| 2954 | 3513 | |
| 2955 | | try self.genSetReg(Type.usize, count_reg, .{ .immediate = 0 }); |
| 2956 | | try self.genSetReg(Type.usize, len_reg, .{ .immediate = length }); |
| 3514 | fn airGetUnionTag(func: *Func, inst: Air.Inst.Index) !void { |
| 3515 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3516 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airGetUnionTag for {}", .{func.target.cpu.arch}); |
| 3517 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3518 | } |
| 2957 | 3519 | |
| 2958 | | _ = src; |
| 2959 | | _ = dst; |
| 3520 | fn airClz(func: *Func, inst: Air.Inst.Index) !void { |
| 3521 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3522 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airClz for {}", .{func.target.cpu.arch}); |
| 3523 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3524 | } |
| 2960 | 3525 | |
| 2961 | | return self.fail("TODO: finish ctz", .{}); |
| 3526 | fn airCtz(func: *Func, inst: Air.Inst.Index) !void { |
| 3527 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3528 | _ = ty_op; |
| 3529 | return func.fail("TODO: finish ctz", .{}); |
| 2962 | 3530 | } |
| 2963 | 3531 | |
| 2964 | | fn airPopcount(self: *Self, inst: Air.Inst.Index) !void { |
| 2965 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2966 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airPopcount for {}", .{self.target.cpu.arch}); |
| 2967 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3532 | fn airPopcount(func: *Func, inst: Air.Inst.Index) !void { |
| 3533 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3534 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airPopcount for {}", .{func.target.cpu.arch}); |
| 3535 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2968 | 3536 | } |
| 2969 | 3537 | |
| 2970 | | fn airAbs(self: *Self, inst: Air.Inst.Index) !void { |
| 2971 | | const zcu = self.bin_file.comp.module.?; |
| 2972 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2973 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2974 | | const ty = self.typeOf(ty_op.operand); |
| 3538 | fn airAbs(func: *Func, inst: Air.Inst.Index) !void { |
| 3539 | const zcu = func.bin_file.comp.module.?; |
| 3540 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3541 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3542 | const ty = func.typeOf(ty_op.operand); |
| 2975 | 3543 | const scalar_ty = ty.scalarType(zcu); |
| 2976 | | const operand = try self.resolveInst(ty_op.operand); |
| 3544 | const operand = try func.resolveInst(ty_op.operand); |
| 2977 | 3545 | |
| 2978 | 3546 | switch (scalar_ty.zigTypeTag(zcu)) { |
| 2979 | 3547 | .Int => if (ty.zigTypeTag(zcu) == .Vector) { |
| 2980 | | return self.fail("TODO implement airAbs for {}", .{ty.fmt(zcu)}); |
| 3548 | return func.fail("TODO implement airAbs for {}", .{ty.fmt(zcu)}); |
| 2981 | 3549 | } else { |
| 2982 | | const int_bits = ty.intInfo(zcu).bits; |
| 3550 | const return_mcv = try func.copyToNewRegister(inst, operand); |
| 3551 | const operand_reg = return_mcv.register; |
| 3552 | |
| 3553 | const temp_reg, const temp_lock = try func.allocReg(.int); |
| 3554 | defer func.register_manager.unlockReg(temp_lock); |
| 3555 | |
| 3556 | _ = try func.addInst(.{ |
| 3557 | .tag = .srai, |
| 3558 | .ops = .rri, |
| 3559 | .data = .{ .i_type = .{ |
| 3560 | .rd = temp_reg, |
| 3561 | .rs1 = operand_reg, |
| 3562 | .imm12 = Immediate.u(63), |
| 3563 | } }, |
| 3564 | }); |
| 2983 | 3565 | |
| 2984 | | if (int_bits > 32) { |
| 2985 | | return self.fail("TODO: airAbs for larger than 32 bits", .{}); |
| 3566 | _ = try func.addInst(.{ |
| 3567 | .tag = .xor, |
| 3568 | .ops = .rrr, |
| 3569 | .data = .{ .r_type = .{ |
| 3570 | .rd = operand_reg, |
| 3571 | .rs1 = operand_reg, |
| 3572 | .rs2 = temp_reg, |
| 3573 | } }, |
| 3574 | }); |
| 3575 | |
| 3576 | _ = try func.addInst(.{ |
| 3577 | .tag = .sub, |
| 3578 | .ops = .rrr, |
| 3579 | .data = .{ .r_type = .{ |
| 3580 | .rd = operand_reg, |
| 3581 | .rs1 = operand_reg, |
| 3582 | .rs2 = temp_reg, |
| 3583 | } }, |
| 3584 | }); |
| 3585 | |
| 3586 | break :result return_mcv; |
| 3587 | }, |
| 3588 | .Float => { |
| 3589 | const float_bits = scalar_ty.floatBits(zcu.getTarget()); |
| 3590 | switch (float_bits) { |
| 3591 | 16 => return func.fail("TODO: airAbs 16-bit float", .{}), |
| 3592 | 32 => {}, |
| 3593 | 64 => {}, |
| 3594 | 80 => return func.fail("TODO: airAbs 80-bit float", .{}), |
| 3595 | 128 => return func.fail("TODO: airAbs 128-bit float", .{}), |
| 3596 | else => unreachable, |
| 2986 | 3597 | } |
| 2987 | 3598 | |
| 2988 | | // promote the src into a register |
| 2989 | | const src_mcv = try self.copyToNewRegister(inst, operand); |
| 2990 | | // temp register for shift |
| 2991 | | const temp_reg = try self.register_manager.allocReg(inst, gp); |
| 3599 | const return_mcv = try func.copyToNewRegister(inst, operand); |
| 3600 | const operand_reg = return_mcv.register; |
| 2992 | 3601 | |
| 2993 | | _ = try self.addInst(.{ |
| 2994 | | .tag = .abs, |
| 2995 | | .ops = .rri, |
| 3602 | assert(operand_reg.class() == .float); |
| 3603 | |
| 3604 | _ = try func.addInst(.{ |
| 3605 | .tag = .pseudo, |
| 3606 | .ops = .pseudo_fabs, |
| 2996 | 3607 | .data = .{ |
| 2997 | | .i_type = .{ |
| 2998 | | .rs1 = src_mcv.register, |
| 2999 | | .rd = temp_reg, |
| 3000 | | .imm12 = Immediate.s(int_bits - 1), |
| 3608 | .fabs = .{ |
| 3609 | .rd = operand_reg, |
| 3610 | .rs = operand_reg, |
| 3611 | .bits = float_bits, |
| 3001 | 3612 | }, |
| 3002 | 3613 | }, |
| 3003 | 3614 | }); |
| 3004 | 3615 | |
| 3005 | | break :result src_mcv; |
| 3616 | break :result return_mcv; |
| 3006 | 3617 | }, |
| 3007 | | else => return self.fail("TODO: implement airAbs {}", .{scalar_ty.fmt(zcu)}), |
| 3618 | else => return func.fail("TODO: implement airAbs {}", .{scalar_ty.fmt(zcu)}), |
| 3008 | 3619 | } |
| 3620 | |
| 3621 | break :result .unreach; |
| 3009 | 3622 | }; |
| 3010 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3623 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3011 | 3624 | } |
| 3012 | 3625 | |
| 3013 | | fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 3014 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3015 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3016 | | const zcu = self.bin_file.comp.module.?; |
| 3017 | | const ty = self.typeOf(ty_op.operand); |
| 3018 | | const operand = try self.resolveInst(ty_op.operand); |
| 3626 | fn airByteSwap(func: *Func, inst: Air.Inst.Index) !void { |
| 3627 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3628 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3629 | const zcu = func.bin_file.comp.module.?; |
| 3630 | const ty = func.typeOf(ty_op.operand); |
| 3631 | const operand = try func.resolveInst(ty_op.operand); |
| 3019 | 3632 | |
| 3020 | 3633 | const int_bits = ty.intInfo(zcu).bits; |
| 3021 | 3634 | |
| 3022 | 3635 | // bytes are no-op |
| 3023 | | if (int_bits == 8 and self.reuseOperand(inst, ty_op.operand, 0, operand)) { |
| 3024 | | return self.finishAir(inst, operand, .{ ty_op.operand, .none, .none }); |
| 3636 | if (int_bits == 8 and func.reuseOperand(inst, ty_op.operand, 0, operand)) { |
| 3637 | return func.finishAir(inst, operand, .{ ty_op.operand, .none, .none }); |
| 3025 | 3638 | } |
| 3026 | 3639 | |
| 3027 | | const dest_reg = try self.register_manager.allocReg(null, gp); |
| 3028 | | try self.genSetReg(ty, dest_reg, operand); |
| 3640 | const dest_mcv = try func.copyToNewRegister(inst, operand); |
| 3641 | const dest_reg = dest_mcv.register; |
| 3642 | |
| 3643 | switch (int_bits) { |
| 3644 | 16 => { |
| 3645 | const temp_reg, const temp_lock = try func.allocReg(.int); |
| 3646 | defer func.register_manager.unlockReg(temp_lock); |
| 3029 | 3647 | |
| 3030 | | const dest_mcv: MCValue = .{ .register = dest_reg }; |
| 3648 | _ = try func.addInst(.{ |
| 3649 | .tag = .srli, |
| 3650 | .ops = .rri, |
| 3651 | .data = .{ .i_type = .{ |
| 3652 | .imm12 = Immediate.s(8), |
| 3653 | .rd = temp_reg, |
| 3654 | .rs1 = dest_reg, |
| 3655 | } }, |
| 3656 | }); |
| 3031 | 3657 | |
| 3032 | | switch (int_bits) { |
| 3033 | | 16 => { |
| 3034 | | const temp = try self.binOp(.shr, dest_mcv, ty, .{ .immediate = 8 }, Type.u8); |
| 3035 | | assert(temp == .register); |
| 3036 | | _ = try self.addInst(.{ |
| 3658 | _ = try func.addInst(.{ |
| 3037 | 3659 | .tag = .slli, |
| 3038 | 3660 | .ops = .rri, |
| 3039 | 3661 | .data = .{ .i_type = .{ |
| ... | ... | @@ -3042,58 +3664,59 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 3042 | 3664 | .rs1 = dest_reg, |
| 3043 | 3665 | } }, |
| 3044 | 3666 | }); |
| 3045 | | _ = try self.addInst(.{ |
| 3667 | _ = try func.addInst(.{ |
| 3046 | 3668 | .tag = .@"or", |
| 3047 | 3669 | .ops = .rri, |
| 3048 | 3670 | .data = .{ .r_type = .{ |
| 3049 | 3671 | .rd = dest_reg, |
| 3050 | 3672 | .rs1 = dest_reg, |
| 3051 | | .rs2 = temp.register, |
| 3673 | .rs2 = temp_reg, |
| 3052 | 3674 | } }, |
| 3053 | 3675 | }); |
| 3054 | 3676 | }, |
| 3055 | | else => return self.fail("TODO: {d} bits for airByteSwap", .{int_bits}), |
| 3677 | else => return func.fail("TODO: {d} bits for airByteSwap", .{int_bits}), |
| 3056 | 3678 | } |
| 3057 | 3679 | |
| 3058 | 3680 | break :result dest_mcv; |
| 3059 | 3681 | }; |
| 3060 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3682 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3061 | 3683 | } |
| 3062 | 3684 | |
| 3063 | | fn airBitReverse(self: *Self, inst: Air.Inst.Index) !void { |
| 3064 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3065 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airBitReverse for {}", .{self.target.cpu.arch}); |
| 3066 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3685 | fn airBitReverse(func: *Func, inst: Air.Inst.Index) !void { |
| 3686 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3687 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airBitReverse for {}", .{func.target.cpu.arch}); |
| 3688 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3067 | 3689 | } |
| 3068 | 3690 | |
| 3069 | | fn airUnaryMath(self: *Self, inst: Air.Inst.Index) !void { |
| 3070 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3071 | | const result: MCValue = if (self.liveness.isUnused(inst)) |
| 3691 | fn airUnaryMath(func: *Func, inst: Air.Inst.Index) !void { |
| 3692 | const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 3693 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3694 | const result: MCValue = if (func.liveness.isUnused(inst)) |
| 3072 | 3695 | .unreach |
| 3073 | 3696 | else |
| 3074 | | return self.fail("TODO implement airUnaryMath for {}", .{self.target.cpu.arch}); |
| 3075 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 3697 | return func.fail("TODO implementairUnaryMath {s} for {}", .{ @tagName(tag), func.target.cpu.arch }); |
| 3698 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 3076 | 3699 | } |
| 3077 | 3700 | |
| 3078 | 3701 | fn reuseOperand( |
| 3079 | | self: *Self, |
| 3702 | func: *Func, |
| 3080 | 3703 | inst: Air.Inst.Index, |
| 3081 | 3704 | operand: Air.Inst.Ref, |
| 3082 | 3705 | op_index: Liveness.OperandInt, |
| 3083 | 3706 | mcv: MCValue, |
| 3084 | 3707 | ) bool { |
| 3085 | | return self.reuseOperandAdvanced(inst, operand, op_index, mcv, inst); |
| 3708 | return func.reuseOperandAdvanced(inst, operand, op_index, mcv, inst); |
| 3086 | 3709 | } |
| 3087 | 3710 | |
| 3088 | 3711 | fn reuseOperandAdvanced( |
| 3089 | | self: *Self, |
| 3712 | func: *Func, |
| 3090 | 3713 | inst: Air.Inst.Index, |
| 3091 | 3714 | operand: Air.Inst.Ref, |
| 3092 | 3715 | op_index: Liveness.OperandInt, |
| 3093 | 3716 | mcv: MCValue, |
| 3094 | 3717 | maybe_tracked_inst: ?Air.Inst.Index, |
| 3095 | 3718 | ) bool { |
| 3096 | | if (!self.liveness.operandDies(inst, op_index)) |
| 3719 | if (!func.liveness.operandDies(inst, op_index)) |
| 3097 | 3720 | return false; |
| 3098 | 3721 | |
| 3099 | 3722 | switch (mcv) { |
| ... | ... | @@ -3103,55 +3726,59 @@ fn reuseOperandAdvanced( |
| 3103 | 3726 | // If it's in the registers table, need to associate the register(s) with the |
| 3104 | 3727 | // new instruction. |
| 3105 | 3728 | if (maybe_tracked_inst) |tracked_inst| { |
| 3106 | | if (!self.register_manager.isRegFree(reg)) { |
| 3729 | if (!func.register_manager.isRegFree(reg)) { |
| 3107 | 3730 | if (RegisterManager.indexOfRegIntoTracked(reg)) |index| { |
| 3108 | | self.register_manager.registers[index] = tracked_inst; |
| 3731 | func.register_manager.registers[index] = tracked_inst; |
| 3109 | 3732 | } |
| 3110 | 3733 | } |
| 3111 | | } else self.register_manager.freeReg(reg); |
| 3734 | } else func.register_manager.freeReg(reg); |
| 3112 | 3735 | }, |
| 3113 | 3736 | .load_frame => |frame_addr| if (frame_addr.index.isNamed()) return false, |
| 3114 | 3737 | else => return false, |
| 3115 | 3738 | } |
| 3116 | 3739 | |
| 3117 | 3740 | // Prevent the operand deaths processing code from deallocating it. |
| 3118 | | self.liveness.clearOperandDeath(inst, op_index); |
| 3741 | func.liveness.clearOperandDeath(inst, op_index); |
| 3119 | 3742 | const op_inst = operand.toIndex().?; |
| 3120 | | self.getResolvedInstValue(op_inst).reuse(self, maybe_tracked_inst, op_inst); |
| 3743 | func.getResolvedInstValue(op_inst).reuse(func, maybe_tracked_inst, op_inst); |
| 3121 | 3744 | |
| 3122 | 3745 | return true; |
| 3123 | 3746 | } |
| 3124 | 3747 | |
| 3125 | | fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 3126 | | const zcu = self.bin_file.comp.module.?; |
| 3127 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3128 | | const elem_ty = self.typeOfIndex(inst); |
| 3748 | fn airLoad(func: *Func, inst: Air.Inst.Index) !void { |
| 3749 | const zcu = func.bin_file.comp.module.?; |
| 3750 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3751 | const elem_ty = func.typeOfIndex(inst); |
| 3752 | |
| 3129 | 3753 | const result: MCValue = result: { |
| 3130 | 3754 | if (!elem_ty.hasRuntimeBits(zcu)) |
| 3131 | 3755 | break :result .none; |
| 3132 | 3756 | |
| 3133 | | const ptr = try self.resolveInst(ty_op.operand); |
| 3134 | | const is_volatile = self.typeOf(ty_op.operand).isVolatilePtr(zcu); |
| 3135 | | if (self.liveness.isUnused(inst) and !is_volatile) |
| 3757 | const ptr = try func.resolveInst(ty_op.operand); |
| 3758 | const is_volatile = func.typeOf(ty_op.operand).isVolatilePtr(zcu); |
| 3759 | if (func.liveness.isUnused(inst) and !is_volatile) |
| 3136 | 3760 | break :result .unreach; |
| 3137 | 3761 | |
| 3762 | const elem_size = elem_ty.abiSize(zcu); |
| 3763 | |
| 3138 | 3764 | const dst_mcv: MCValue = blk: { |
| 3139 | | if (self.reuseOperand(inst, ty_op.operand, 0, ptr)) { |
| 3765 | // Pointer is 8 bytes, and if the element is more than that, we cannot reuse it. |
| 3766 | if (elem_size <= 8 and func.reuseOperand(inst, ty_op.operand, 0, ptr)) { |
| 3140 | 3767 | // The MCValue that holds the pointer can be re-used as the value. |
| 3141 | 3768 | break :blk ptr; |
| 3142 | 3769 | } else { |
| 3143 | | break :blk try self.allocRegOrMem(inst, true); |
| 3770 | break :blk try func.allocRegOrMem(elem_ty, inst, true); |
| 3144 | 3771 | } |
| 3145 | 3772 | }; |
| 3146 | 3773 | |
| 3147 | | try self.load(dst_mcv, ptr, self.typeOf(ty_op.operand)); |
| 3774 | try func.load(dst_mcv, ptr, func.typeOf(ty_op.operand)); |
| 3148 | 3775 | break :result dst_mcv; |
| 3149 | 3776 | }; |
| 3150 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3777 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3151 | 3778 | } |
| 3152 | 3779 | |
| 3153 | | fn load(self: *Self, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerError!void { |
| 3154 | | const zcu = self.bin_file.comp.module.?; |
| 3780 | fn load(func: *Func, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerError!void { |
| 3781 | const zcu = func.bin_file.comp.module.?; |
| 3155 | 3782 | const dst_ty = ptr_ty.childType(zcu); |
| 3156 | 3783 | |
| 3157 | 3784 | log.debug("loading {}:{} into {}", .{ ptr_mcv, ptr_ty.fmt(zcu), dst_mcv }); |
| ... | ... | @@ -3170,43 +3797,43 @@ fn load(self: *Self, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerErro |
| 3170 | 3797 | .register_offset, |
| 3171 | 3798 | .lea_frame, |
| 3172 | 3799 | .lea_symbol, |
| 3173 | | => try self.genCopy(dst_ty, dst_mcv, ptr_mcv.deref()), |
| 3800 | => try func.genCopy(dst_ty, dst_mcv, ptr_mcv.deref()), |
| 3174 | 3801 | |
| 3175 | 3802 | .memory, |
| 3176 | 3803 | .indirect, |
| 3177 | 3804 | .load_symbol, |
| 3178 | 3805 | .load_frame, |
| 3179 | 3806 | => { |
| 3180 | | const addr_reg = try self.copyToTmpRegister(ptr_ty, ptr_mcv); |
| 3181 | | const addr_lock = self.register_manager.lockRegAssumeUnused(addr_reg); |
| 3182 | | defer self.register_manager.unlockReg(addr_lock); |
| 3807 | const addr_reg = try func.copyToTmpRegister(ptr_ty, ptr_mcv); |
| 3808 | const addr_lock = func.register_manager.lockRegAssumeUnused(addr_reg); |
| 3809 | defer func.register_manager.unlockReg(addr_lock); |
| 3183 | 3810 | |
| 3184 | | try self.genCopy(dst_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } }); |
| 3811 | try func.genCopy(dst_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } }); |
| 3185 | 3812 | }, |
| 3186 | | .air_ref => |ptr_ref| try self.load(dst_mcv, try self.resolveInst(ptr_ref), ptr_ty), |
| 3813 | .air_ref => |ptr_ref| try func.load(dst_mcv, try func.resolveInst(ptr_ref), ptr_ty), |
| 3187 | 3814 | } |
| 3188 | 3815 | } |
| 3189 | 3816 | |
| 3190 | | fn airStore(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 3817 | fn airStore(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 3191 | 3818 | if (safety) { |
| 3192 | 3819 | // TODO if the value is undef, write 0xaa bytes to dest |
| 3193 | 3820 | } else { |
| 3194 | 3821 | // TODO if the value is undef, don't lower this instruction |
| 3195 | 3822 | } |
| 3196 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3197 | | const ptr = try self.resolveInst(bin_op.lhs); |
| 3198 | | const value = try self.resolveInst(bin_op.rhs); |
| 3199 | | const ptr_ty = self.typeOf(bin_op.lhs); |
| 3200 | | const value_ty = self.typeOf(bin_op.rhs); |
| 3823 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3824 | const ptr = try func.resolveInst(bin_op.lhs); |
| 3825 | const value = try func.resolveInst(bin_op.rhs); |
| 3826 | const ptr_ty = func.typeOf(bin_op.lhs); |
| 3827 | const value_ty = func.typeOf(bin_op.rhs); |
| 3201 | 3828 | |
| 3202 | | try self.store(ptr, value, ptr_ty, value_ty); |
| 3829 | try func.store(ptr, value, ptr_ty, value_ty); |
| 3203 | 3830 | |
| 3204 | | return self.finishAir(inst, .none, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3831 | return func.finishAir(inst, .none, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3205 | 3832 | } |
| 3206 | 3833 | |
| 3207 | 3834 | /// Loads `value` into the "payload" of `pointer`. |
| 3208 | | fn store(self: *Self, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type, src_ty: Type) !void { |
| 3209 | | const zcu = self.bin_file.comp.module.?; |
| 3835 | fn store(func: *Func, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type, src_ty: Type) !void { |
| 3836 | const zcu = func.bin_file.comp.module.?; |
| 3210 | 3837 | |
| 3211 | 3838 | log.debug("storing {}:{} in {}:{}", .{ src_mcv, src_ty.fmt(zcu), ptr_mcv, ptr_ty.fmt(zcu) }); |
| 3212 | 3839 | |
| ... | ... | @@ -3223,40 +3850,40 @@ fn store(self: *Self, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type, src_ty: |
| 3223 | 3850 | .register_offset, |
| 3224 | 3851 | .lea_symbol, |
| 3225 | 3852 | .lea_frame, |
| 3226 | | => try self.genCopy(src_ty, ptr_mcv.deref(), src_mcv), |
| 3853 | => try func.genCopy(src_ty, ptr_mcv.deref(), src_mcv), |
| 3227 | 3854 | |
| 3228 | 3855 | .memory, |
| 3229 | 3856 | .indirect, |
| 3230 | 3857 | .load_symbol, |
| 3231 | 3858 | .load_frame, |
| 3232 | 3859 | => { |
| 3233 | | const addr_reg = try self.copyToTmpRegister(ptr_ty, ptr_mcv); |
| 3234 | | const addr_lock = self.register_manager.lockRegAssumeUnused(addr_reg); |
| 3235 | | defer self.register_manager.unlockReg(addr_lock); |
| 3860 | const addr_reg = try func.copyToTmpRegister(ptr_ty, ptr_mcv); |
| 3861 | const addr_lock = func.register_manager.lockRegAssumeUnused(addr_reg); |
| 3862 | defer func.register_manager.unlockReg(addr_lock); |
| 3236 | 3863 | |
| 3237 | | try self.genCopy(src_ty, .{ .indirect = .{ .reg = addr_reg } }, src_mcv); |
| 3864 | try func.genCopy(src_ty, .{ .indirect = .{ .reg = addr_reg } }, src_mcv); |
| 3238 | 3865 | }, |
| 3239 | | .air_ref => |ptr_ref| try self.store(try self.resolveInst(ptr_ref), src_mcv, ptr_ty, src_ty), |
| 3866 | .air_ref => |ptr_ref| try func.store(try func.resolveInst(ptr_ref), src_mcv, ptr_ty, src_ty), |
| 3240 | 3867 | } |
| 3241 | 3868 | } |
| 3242 | 3869 | |
| 3243 | | fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3244 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3245 | | const extra = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| 3246 | | const result = try self.structFieldPtr(inst, extra.struct_operand, extra.field_index); |
| 3247 | | return self.finishAir(inst, result, .{ extra.struct_operand, .none, .none }); |
| 3870 | fn airStructFieldPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3871 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3872 | const extra = func.air.extraData(Air.StructField, ty_pl.payload).data; |
| 3873 | const result = try func.structFieldPtr(inst, extra.struct_operand, extra.field_index); |
| 3874 | return func.finishAir(inst, result, .{ extra.struct_operand, .none, .none }); |
| 3248 | 3875 | } |
| 3249 | 3876 | |
| 3250 | | fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void { |
| 3251 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3252 | | const result = try self.structFieldPtr(inst, ty_op.operand, index); |
| 3253 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3877 | fn airStructFieldPtrIndex(func: *Func, inst: Air.Inst.Index, index: u8) !void { |
| 3878 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3879 | const result = try func.structFieldPtr(inst, ty_op.operand, index); |
| 3880 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3254 | 3881 | } |
| 3255 | 3882 | |
| 3256 | | fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue { |
| 3257 | | const zcu = self.bin_file.comp.module.?; |
| 3258 | | const ptr_field_ty = self.typeOfIndex(inst); |
| 3259 | | const ptr_container_ty = self.typeOf(operand); |
| 3883 | fn structFieldPtr(func: *Func, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue { |
| 3884 | const zcu = func.bin_file.comp.module.?; |
| 3885 | const ptr_field_ty = func.typeOfIndex(inst); |
| 3886 | const ptr_container_ty = func.typeOf(operand); |
| 3260 | 3887 | const ptr_container_ty_info = ptr_container_ty.ptrInfo(zcu); |
| 3261 | 3888 | const container_ty = ptr_container_ty.childType(zcu); |
| 3262 | 3889 | |
| ... | ... | @@ -3269,27 +3896,27 @@ fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, inde |
| 3269 | 3896 | else |
| 3270 | 3897 | @intCast(container_ty.structFieldOffset(index, zcu)); |
| 3271 | 3898 | |
| 3272 | | const src_mcv = try self.resolveInst(operand); |
| 3899 | const src_mcv = try func.resolveInst(operand); |
| 3273 | 3900 | const dst_mcv = if (switch (src_mcv) { |
| 3274 | 3901 | .immediate, .lea_frame => true, |
| 3275 | | .register, .register_offset => self.reuseOperand(inst, operand, 0, src_mcv), |
| 3902 | .register, .register_offset => func.reuseOperand(inst, operand, 0, src_mcv), |
| 3276 | 3903 | else => false, |
| 3277 | | }) src_mcv else try self.copyToNewRegister(inst, src_mcv); |
| 3904 | }) src_mcv else try func.copyToNewRegister(inst, src_mcv); |
| 3278 | 3905 | return dst_mcv.offset(field_offset); |
| 3279 | 3906 | } |
| 3280 | 3907 | |
| 3281 | | fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3282 | | const mod = self.bin_file.comp.module.?; |
| 3908 | fn airStructFieldVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3909 | const mod = func.bin_file.comp.module.?; |
| 3283 | 3910 | |
| 3284 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3285 | | const extra = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| 3911 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3912 | const extra = func.air.extraData(Air.StructField, ty_pl.payload).data; |
| 3286 | 3913 | const operand = extra.struct_operand; |
| 3287 | 3914 | const index = extra.field_index; |
| 3288 | 3915 | |
| 3289 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3290 | | const zcu = self.bin_file.comp.module.?; |
| 3291 | | const src_mcv = try self.resolveInst(operand); |
| 3292 | | const struct_ty = self.typeOf(operand); |
| 3916 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3917 | const zcu = func.bin_file.comp.module.?; |
| 3918 | const src_mcv = try func.resolveInst(operand); |
| 3919 | const struct_ty = func.typeOf(operand); |
| 3293 | 3920 | const field_ty = struct_ty.structFieldType(index, zcu); |
| 3294 | 3921 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .none; |
| 3295 | 3922 | |
| ... | ... | @@ -3304,33 +3931,31 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3304 | 3931 | switch (src_mcv) { |
| 3305 | 3932 | .dead, .unreach => unreachable, |
| 3306 | 3933 | .register => |src_reg| { |
| 3307 | | const src_reg_lock = self.register_manager.lockRegAssumeUnused(src_reg); |
| 3308 | | defer self.register_manager.unlockReg(src_reg_lock); |
| 3934 | const src_reg_lock = func.register_manager.lockRegAssumeUnused(src_reg); |
| 3935 | defer func.register_manager.unlockReg(src_reg_lock); |
| 3309 | 3936 | |
| 3310 | 3937 | const dst_reg = if (field_off == 0) |
| 3311 | | (try self.copyToNewRegister(inst, src_mcv)).register |
| 3938 | (try func.copyToNewRegister(inst, src_mcv)).register |
| 3312 | 3939 | else |
| 3313 | | try self.copyToTmpRegister(Type.usize, .{ .register = src_reg }); |
| 3940 | try func.copyToTmpRegister(Type.usize, .{ .register = src_reg }); |
| 3314 | 3941 | |
| 3315 | 3942 | const dst_mcv: MCValue = .{ .register = dst_reg }; |
| 3316 | | const dst_lock = self.register_manager.lockReg(dst_reg); |
| 3317 | | defer if (dst_lock) |lock| self.register_manager.unlockReg(lock); |
| 3943 | const dst_lock = func.register_manager.lockReg(dst_reg); |
| 3944 | defer if (dst_lock) |lock| func.register_manager.unlockReg(lock); |
| 3318 | 3945 | |
| 3319 | 3946 | if (field_off > 0) { |
| 3320 | | _ = try self.addInst(.{ |
| 3947 | _ = try func.addInst(.{ |
| 3321 | 3948 | .tag = .srli, |
| 3322 | 3949 | .ops = .rri, |
| 3323 | 3950 | .data = .{ .i_type = .{ |
| 3324 | | .imm12 = Immediate.s(@intCast(field_off)), |
| 3951 | .imm12 = Immediate.u(@intCast(field_off)), |
| 3325 | 3952 | .rd = dst_reg, |
| 3326 | 3953 | .rs1 = dst_reg, |
| 3327 | 3954 | } }, |
| 3328 | 3955 | }); |
| 3329 | | |
| 3330 | | return self.fail("TODO: airStructFieldVal register with field_off > 0", .{}); |
| 3331 | 3956 | } |
| 3332 | 3957 | |
| 3333 | | break :result if (field_off == 0) dst_mcv else try self.copyToNewRegister(inst, dst_mcv); |
| 3958 | break :result if (field_off == 0) dst_mcv else try func.copyToNewRegister(inst, dst_mcv); |
| 3334 | 3959 | }, |
| 3335 | 3960 | .load_frame => { |
| 3336 | 3961 | const field_abi_size: u32 = @intCast(field_ty.abiSize(mod)); |
| ... | ... | @@ -3345,57 +3970,57 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3345 | 3970 | @intCast(field_bit_size), |
| 3346 | 3971 | ); |
| 3347 | 3972 | |
| 3348 | | const dst_reg, const dst_lock = try self.allocReg(); |
| 3973 | const dst_reg, const dst_lock = try func.allocReg(.int); |
| 3349 | 3974 | const dst_mcv = MCValue{ .register = dst_reg }; |
| 3350 | | defer self.register_manager.unlockReg(dst_lock); |
| 3975 | defer func.register_manager.unlockReg(dst_lock); |
| 3351 | 3976 | |
| 3352 | | try self.genCopy(int_ty, dst_mcv, off_mcv); |
| 3353 | | break :result try self.copyToNewRegister(inst, dst_mcv); |
| 3977 | try func.genCopy(int_ty, dst_mcv, off_mcv); |
| 3978 | break :result try func.copyToNewRegister(inst, dst_mcv); |
| 3354 | 3979 | } |
| 3355 | 3980 | |
| 3356 | 3981 | const container_abi_size: u32 = @intCast(struct_ty.abiSize(mod)); |
| 3357 | 3982 | const dst_mcv = if (field_byte_off + field_abi_size <= container_abi_size and |
| 3358 | | self.reuseOperand(inst, operand, 0, src_mcv)) |
| 3983 | func.reuseOperand(inst, operand, 0, src_mcv)) |
| 3359 | 3984 | off_mcv |
| 3360 | 3985 | else dst: { |
| 3361 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 3362 | | try self.genCopy(field_ty, dst_mcv, off_mcv); |
| 3986 | const dst_mcv = try func.allocRegOrMem(func.typeOfIndex(inst), inst, true); |
| 3987 | try func.genCopy(field_ty, dst_mcv, off_mcv); |
| 3363 | 3988 | break :dst dst_mcv; |
| 3364 | 3989 | }; |
| 3365 | 3990 | if (field_abi_size * 8 > field_bit_size and dst_mcv.isMemory()) { |
| 3366 | | const tmp_reg, const tmp_lock = try self.allocReg(); |
| 3367 | | defer self.register_manager.unlockReg(tmp_lock); |
| 3991 | const tmp_reg, const tmp_lock = try func.allocReg(.int); |
| 3992 | defer func.register_manager.unlockReg(tmp_lock); |
| 3368 | 3993 | |
| 3369 | 3994 | const hi_mcv = |
| 3370 | 3995 | dst_mcv.address().offset(@intCast(field_bit_size / 64 * 8)).deref(); |
| 3371 | | try self.genSetReg(Type.usize, tmp_reg, hi_mcv); |
| 3372 | | try self.genCopy(Type.usize, hi_mcv, .{ .register = tmp_reg }); |
| 3996 | try func.genSetReg(Type.usize, tmp_reg, hi_mcv); |
| 3997 | try func.genCopy(Type.usize, hi_mcv, .{ .register = tmp_reg }); |
| 3373 | 3998 | } |
| 3374 | 3999 | break :result dst_mcv; |
| 3375 | 4000 | } |
| 3376 | 4001 | |
| 3377 | | return self.fail("TODO: airStructFieldVal load_frame field_off non multiple of 8", .{}); |
| 4002 | return func.fail("TODO: airStructFieldVal load_frame field_off non multiple of 8", .{}); |
| 3378 | 4003 | }, |
| 3379 | | else => return self.fail("TODO: airStructField {s}", .{@tagName(src_mcv)}), |
| 4004 | else => return func.fail("TODO: airStructField {s}", .{@tagName(src_mcv)}), |
| 3380 | 4005 | } |
| 3381 | 4006 | }; |
| 3382 | 4007 | |
| 3383 | | return self.finishAir(inst, result, .{ extra.struct_operand, .none, .none }); |
| 4008 | return func.finishAir(inst, result, .{ extra.struct_operand, .none, .none }); |
| 3384 | 4009 | } |
| 3385 | 4010 | |
| 3386 | | fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4011 | fn airFieldParentPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3387 | 4012 | _ = inst; |
| 3388 | | return self.fail("TODO implement codegen airFieldParentPtr", .{}); |
| 4013 | return func.fail("TODO implement codegen airFieldParentPtr", .{}); |
| 3389 | 4014 | } |
| 3390 | 4015 | |
| 3391 | | fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 3392 | | const zcu = self.bin_file.comp.module.?; |
| 3393 | | const arg = self.air.instructions.items(.data)[@intFromEnum(inst)].arg; |
| 4016 | fn genArgDbgInfo(func: Func, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 4017 | const zcu = func.bin_file.comp.module.?; |
| 4018 | const arg = func.air.instructions.items(.data)[@intFromEnum(inst)].arg; |
| 3394 | 4019 | const ty = arg.ty.toType(); |
| 3395 | | const owner_decl = zcu.funcOwnerDeclIndex(self.func_index); |
| 3396 | | const name = zcu.getParamName(self.func_index, arg.src_index); |
| 4020 | const owner_decl = zcu.funcOwnerDeclIndex(func.func_index); |
| 4021 | const name = zcu.getParamName(func.func_index, arg.src_index); |
| 3397 | 4022 | |
| 3398 | | switch (self.debug_output) { |
| 4023 | switch (func.debug_output) { |
| 3399 | 4024 | .dwarf => |dw| switch (mcv) { |
| 3400 | 4025 | .register => |reg| try dw.genArgDbgInfo(name, ty, owner_decl, .{ |
| 3401 | 4026 | .register = reg.dwarfLocOp(), |
| ... | ... | @@ -3408,119 +4033,100 @@ fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 3408 | 4033 | } |
| 3409 | 4034 | } |
| 3410 | 4035 | |
| 3411 | | fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 3412 | | const zcu = self.bin_file.comp.module.?; |
| 3413 | | var arg_index = self.arg_index; |
| 4036 | fn airArg(func: *Func, inst: Air.Inst.Index) !void { |
| 4037 | var arg_index = func.arg_index; |
| 3414 | 4038 | |
| 3415 | 4039 | // we skip over args that have no bits |
| 3416 | | while (self.args[arg_index] == .none) arg_index += 1; |
| 3417 | | self.arg_index = arg_index + 1; |
| 4040 | while (func.args[arg_index] == .none) arg_index += 1; |
| 4041 | func.arg_index = arg_index + 1; |
| 3418 | 4042 | |
| 3419 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3420 | | const src_mcv = self.args[arg_index]; |
| 4043 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4044 | const src_mcv = func.args[arg_index]; |
| 4045 | const arg_ty = func.typeOfIndex(inst); |
| 3421 | 4046 | |
| 3422 | | const arg_ty = self.typeOfIndex(inst); |
| 4047 | const dst_mcv = try func.allocRegOrMem(arg_ty, inst, false); |
| 3423 | 4048 | |
| 3424 | | const dst_mcv = switch (src_mcv) { |
| 3425 | | .register => dst: { |
| 3426 | | const frame = try self.allocFrameIndex(FrameAlloc.init(.{ |
| 3427 | | .size = Type.usize.abiSize(zcu), |
| 3428 | | .alignment = Type.usize.abiAlignment(zcu), |
| 3429 | | })); |
| 3430 | | const dst_mcv: MCValue = .{ .load_frame = .{ .index = frame } }; |
| 3431 | | try self.genCopy(Type.usize, dst_mcv, src_mcv); |
| 3432 | | break :dst dst_mcv; |
| 3433 | | }, |
| 3434 | | .register_pair => dst: { |
| 3435 | | const frame = try self.allocFrameIndex(FrameAlloc.init(.{ |
| 3436 | | .size = Type.usize.abiSize(zcu) * 2, |
| 3437 | | .alignment = Type.usize.abiAlignment(zcu), |
| 3438 | | })); |
| 3439 | | const dst_mcv: MCValue = .{ .load_frame = .{ .index = frame } }; |
| 3440 | | try self.genCopy(arg_ty, dst_mcv, src_mcv); |
| 3441 | | break :dst dst_mcv; |
| 3442 | | }, |
| 3443 | | .load_frame => src_mcv, |
| 3444 | | else => return self.fail("TODO: airArg {s}", .{@tagName(src_mcv)}), |
| 3445 | | }; |
| 4049 | log.debug("airArg {} -> {}", .{ src_mcv, dst_mcv }); |
| 3446 | 4050 | |
| 3447 | | try self.genArgDbgInfo(inst, src_mcv); |
| 4051 | try func.genCopy(arg_ty, dst_mcv, src_mcv); |
| 4052 | |
| 4053 | try func.genArgDbgInfo(inst, src_mcv); |
| 3448 | 4054 | break :result dst_mcv; |
| 3449 | 4055 | }; |
| 3450 | 4056 | |
| 3451 | | return self.finishAir(inst, result, .{ .none, .none, .none }); |
| 4057 | return func.finishAir(inst, result, .{ .none, .none, .none }); |
| 3452 | 4058 | } |
| 3453 | 4059 | |
| 3454 | | fn airTrap(self: *Self) !void { |
| 3455 | | _ = try self.addInst(.{ |
| 4060 | fn airTrap(func: *Func) !void { |
| 4061 | _ = try func.addInst(.{ |
| 3456 | 4062 | .tag = .unimp, |
| 3457 | 4063 | .ops = .none, |
| 3458 | 4064 | .data = undefined, |
| 3459 | 4065 | }); |
| 3460 | | return self.finishAirBookkeeping(); |
| 4066 | return func.finishAirBookkeeping(); |
| 3461 | 4067 | } |
| 3462 | 4068 | |
| 3463 | | fn airBreakpoint(self: *Self) !void { |
| 3464 | | _ = try self.addInst(.{ |
| 4069 | fn airBreakpoint(func: *Func) !void { |
| 4070 | _ = try func.addInst(.{ |
| 3465 | 4071 | .tag = .ebreak, |
| 3466 | 4072 | .ops = .none, |
| 3467 | 4073 | .data = undefined, |
| 3468 | 4074 | }); |
| 3469 | | return self.finishAirBookkeeping(); |
| 4075 | return func.finishAirBookkeeping(); |
| 3470 | 4076 | } |
| 3471 | 4077 | |
| 3472 | | fn airRetAddr(self: *Self, inst: Air.Inst.Index) !void { |
| 3473 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 3474 | | try self.genCopy(Type.usize, dst_mcv, .{ .load_frame = .{ .index = .ret_addr } }); |
| 3475 | | return self.finishAir(inst, dst_mcv, .{ .none, .none, .none }); |
| 4078 | fn airRetAddr(func: *Func, inst: Air.Inst.Index) !void { |
| 4079 | const dst_mcv = try func.allocRegOrMem(func.typeOfIndex(inst), inst, true); |
| 4080 | try func.genCopy(Type.usize, dst_mcv, .{ .load_frame = .{ .index = .ret_addr } }); |
| 4081 | return func.finishAir(inst, dst_mcv, .{ .none, .none, .none }); |
| 3476 | 4082 | } |
| 3477 | 4083 | |
| 3478 | | fn airFrameAddress(self: *Self, inst: Air.Inst.Index) !void { |
| 3479 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 3480 | | try self.genCopy(Type.usize, dst_mcv, .{ .lea_frame = .{ .index = .base_ptr } }); |
| 3481 | | return self.finishAir(inst, dst_mcv, .{ .none, .none, .none }); |
| 4084 | fn airFrameAddress(func: *Func, inst: Air.Inst.Index) !void { |
| 4085 | const dst_mcv = try func.allocRegOrMem(func.typeOfIndex(inst), inst, true); |
| 4086 | try func.genCopy(Type.usize, dst_mcv, .{ .lea_frame = .{ .index = .base_ptr } }); |
| 4087 | return func.finishAir(inst, dst_mcv, .{ .none, .none, .none }); |
| 3482 | 4088 | } |
| 3483 | 4089 | |
| 3484 | | fn airFence(self: *Self) !void { |
| 3485 | | return self.fail("TODO implement fence() for {}", .{self.target.cpu.arch}); |
| 3486 | | //return self.finishAirBookkeeping(); |
| 4090 | fn airFence(func: *Func) !void { |
| 4091 | return func.fail("TODO implement fence() for {}", .{func.target.cpu.arch}); |
| 4092 | //return func.finishAirBookkeeping(); |
| 3487 | 4093 | } |
| 3488 | 4094 | |
| 3489 | | fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !void { |
| 3490 | | if (modifier == .always_tail) return self.fail("TODO implement tail calls for riscv64", .{}); |
| 3491 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4095 | fn airCall(func: *Func, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !void { |
| 4096 | if (modifier == .always_tail) return func.fail("TODO implement tail calls for riscv64", .{}); |
| 4097 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 3492 | 4098 | const callee = pl_op.operand; |
| 3493 | | const extra = self.air.extraData(Air.Call, pl_op.payload); |
| 3494 | | const arg_refs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]); |
| 4099 | const extra = func.air.extraData(Air.Call, pl_op.payload); |
| 4100 | const arg_refs: []const Air.Inst.Ref = @ptrCast(func.air.extra[extra.end..][0..extra.data.args_len]); |
| 3495 | 4101 | |
| 3496 | 4102 | const expected_num_args = 8; |
| 3497 | 4103 | const ExpectedContents = extern struct { |
| 3498 | 4104 | vals: [expected_num_args][@sizeOf(MCValue)]u8 align(@alignOf(MCValue)), |
| 3499 | 4105 | }; |
| 3500 | 4106 | var stack align(@max(@alignOf(ExpectedContents), @alignOf(std.heap.StackFallbackAllocator(0)))) = |
| 3501 | | std.heap.stackFallback(@sizeOf(ExpectedContents), self.gpa); |
| 4107 | std.heap.stackFallback(@sizeOf(ExpectedContents), func.gpa); |
| 3502 | 4108 | const allocator = stack.get(); |
| 3503 | 4109 | |
| 3504 | 4110 | const arg_tys = try allocator.alloc(Type, arg_refs.len); |
| 3505 | 4111 | defer allocator.free(arg_tys); |
| 3506 | | for (arg_tys, arg_refs) |*arg_ty, arg_ref| arg_ty.* = self.typeOf(arg_ref); |
| 4112 | for (arg_tys, arg_refs) |*arg_ty, arg_ref| arg_ty.* = func.typeOf(arg_ref); |
| 3507 | 4113 | |
| 3508 | 4114 | const arg_vals = try allocator.alloc(MCValue, arg_refs.len); |
| 3509 | 4115 | defer allocator.free(arg_vals); |
| 3510 | 4116 | for (arg_vals, arg_refs) |*arg_val, arg_ref| arg_val.* = .{ .air_ref = arg_ref }; |
| 3511 | 4117 | |
| 3512 | | const call_ret = try self.genCall(.{ .air = callee }, arg_tys, arg_vals); |
| 4118 | const call_ret = try func.genCall(.{ .air = callee }, arg_tys, arg_vals); |
| 3513 | 4119 | |
| 3514 | | var bt = self.liveness.iterateBigTomb(inst); |
| 3515 | | try self.feed(&bt, pl_op.operand); |
| 3516 | | for (arg_refs) |arg_ref| try self.feed(&bt, arg_ref); |
| 4120 | var bt = func.liveness.iterateBigTomb(inst); |
| 4121 | try func.feed(&bt, pl_op.operand); |
| 4122 | for (arg_refs) |arg_ref| try func.feed(&bt, arg_ref); |
| 3517 | 4123 | |
| 3518 | | const result = if (self.liveness.isUnused(inst)) .unreach else call_ret; |
| 3519 | | return self.finishAirResult(inst, result); |
| 4124 | const result = if (func.liveness.isUnused(inst)) .unreach else call_ret; |
| 4125 | return func.finishAirResult(inst, result); |
| 3520 | 4126 | } |
| 3521 | 4127 | |
| 3522 | 4128 | fn genCall( |
| 3523 | | self: *Self, |
| 4129 | func: *Func, |
| 3524 | 4130 | info: union(enum) { |
| 3525 | 4131 | air: Air.Inst.Ref, |
| 3526 | 4132 | lib: struct { |
| ... | ... | @@ -3533,11 +4139,11 @@ fn genCall( |
| 3533 | 4139 | arg_tys: []const Type, |
| 3534 | 4140 | args: []const MCValue, |
| 3535 | 4141 | ) !MCValue { |
| 3536 | | const zcu = self.bin_file.comp.module.?; |
| 4142 | const zcu = func.bin_file.comp.module.?; |
| 3537 | 4143 | |
| 3538 | 4144 | const fn_ty = switch (info) { |
| 3539 | 4145 | .air => |callee| fn_info: { |
| 3540 | | const callee_ty = self.typeOf(callee); |
| 4146 | const callee_ty = func.typeOf(callee); |
| 3541 | 4147 | break :fn_info switch (callee_ty.zigTypeTag(zcu)) { |
| 3542 | 4148 | .Fn => callee_ty, |
| 3543 | 4149 | .Pointer => callee_ty.childType(zcu), |
| ... | ... | @@ -3552,8 +4158,15 @@ fn genCall( |
| 3552 | 4158 | }; |
| 3553 | 4159 | |
| 3554 | 4160 | const fn_info = zcu.typeToFunc(fn_ty).?; |
| 3555 | | var call_info = try self.resolveCallingConventionValues(fn_info); |
| 3556 | | defer call_info.deinit(self); |
| 4161 | |
| 4162 | const allocator = func.gpa; |
| 4163 | |
| 4164 | const var_args = try allocator.alloc(Type, args.len - fn_info.param_types.len); |
| 4165 | defer allocator.free(var_args); |
| 4166 | for (var_args, arg_tys[fn_info.param_types.len..]) |*var_arg, arg_ty| var_arg.* = arg_ty; |
| 4167 | |
| 4168 | var call_info = try func.resolveCallingConventionValues(fn_info, var_args); |
| 4169 | defer call_info.deinit(func); |
| 3557 | 4170 | |
| 3558 | 4171 | // We need a properly aligned and sized call frame to be able to call this function. |
| 3559 | 4172 | { |
| ... | ... | @@ -3561,7 +4174,7 @@ fn genCall( |
| 3561 | 4174 | .size = call_info.stack_byte_count, |
| 3562 | 4175 | .alignment = call_info.stack_align, |
| 3563 | 4176 | }); |
| 3564 | | const frame_allocs_slice = self.frame_allocs.slice(); |
| 4177 | const frame_allocs_slice = func.frame_allocs.slice(); |
| 3565 | 4178 | const stack_frame_size = |
| 3566 | 4179 | &frame_allocs_slice.items(.abi_size)[@intFromEnum(FrameIndex.call_frame)]; |
| 3567 | 4180 | stack_frame_size.* = @max(stack_frame_size.*, needed_call_frame.abi_size); |
| ... | ... | @@ -3570,13 +4183,75 @@ fn genCall( |
| 3570 | 4183 | stack_frame_align.* = stack_frame_align.max(needed_call_frame.abi_align); |
| 3571 | 4184 | } |
| 3572 | 4185 | |
| 3573 | | for (call_info.args, 0..) |mc_arg, arg_i| try self.genCopy(arg_tys[arg_i], mc_arg, args[arg_i]); |
| 4186 | var reg_locks = std.ArrayList(?RegisterLock).init(allocator); |
| 4187 | defer reg_locks.deinit(); |
| 4188 | try reg_locks.ensureTotalCapacity(8); |
| 4189 | defer for (reg_locks.items) |reg_lock| if (reg_lock) |lock| func.register_manager.unlockReg(lock); |
| 4190 | |
| 4191 | const frame_indices = try allocator.alloc(FrameIndex, args.len); |
| 4192 | defer allocator.free(frame_indices); |
| 4193 | |
| 4194 | switch (call_info.return_value.long) { |
| 4195 | .none, .unreach => {}, |
| 4196 | .indirect => |reg_off| try func.register_manager.getReg(reg_off.reg, null), |
| 4197 | else => unreachable, |
| 4198 | } |
| 4199 | for (call_info.args, args, arg_tys, frame_indices) |dst_arg, src_arg, arg_ty, *frame_index| { |
| 4200 | switch (dst_arg) { |
| 4201 | .none => {}, |
| 4202 | .register => |reg| { |
| 4203 | try func.register_manager.getReg(reg, null); |
| 4204 | try reg_locks.append(func.register_manager.lockReg(reg)); |
| 4205 | }, |
| 4206 | .register_pair => |regs| { |
| 4207 | for (regs) |reg| try func.register_manager.getReg(reg, null); |
| 4208 | try reg_locks.appendSlice(&func.register_manager.lockRegs(2, regs)); |
| 4209 | }, |
| 4210 | .indirect => |reg_off| { |
| 4211 | frame_index.* = try func.allocFrameIndex(FrameAlloc.initType(arg_ty, zcu)); |
| 4212 | try func.genSetMem(.{ .frame = frame_index.* }, 0, arg_ty, src_arg); |
| 4213 | try func.register_manager.getReg(reg_off.reg, null); |
| 4214 | try reg_locks.append(func.register_manager.lockReg(reg_off.reg)); |
| 4215 | }, |
| 4216 | else => return func.fail("TODO: genCall set arg {s}", .{@tagName(dst_arg)}), |
| 4217 | } |
| 4218 | } |
| 4219 | |
| 4220 | switch (call_info.return_value.long) { |
| 4221 | .none, .unreach => {}, |
| 4222 | .indirect => |reg_off| { |
| 4223 | const ret_ty = Type.fromInterned(fn_info.return_type); |
| 4224 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(ret_ty, zcu)); |
| 4225 | try func.genSetReg(Type.usize, reg_off.reg, .{ |
| 4226 | .lea_frame = .{ .index = frame_index, .off = -reg_off.off }, |
| 4227 | }); |
| 4228 | call_info.return_value.short = .{ .load_frame = .{ .index = frame_index } }; |
| 4229 | try reg_locks.append(func.register_manager.lockReg(reg_off.reg)); |
| 4230 | }, |
| 4231 | else => unreachable, |
| 4232 | } |
| 4233 | |
| 4234 | for (call_info.args, arg_tys, args, frame_indices) |dst_arg, arg_ty, src_arg, frame_index| { |
| 4235 | switch (dst_arg) { |
| 4236 | .none, .load_frame => {}, |
| 4237 | .register_pair => try func.genCopy(arg_ty, dst_arg, src_arg), |
| 4238 | .register => |dst_reg| try func.genSetReg( |
| 4239 | arg_ty, |
| 4240 | dst_reg, |
| 4241 | src_arg, |
| 4242 | ), |
| 4243 | .indirect => |reg_off| try func.genSetReg(Type.usize, reg_off.reg, .{ |
| 4244 | .lea_frame = .{ .index = frame_index, .off = -reg_off.off }, |
| 4245 | }), |
| 4246 | else => return func.fail("TODO: genCall actual set {s}", .{@tagName(dst_arg)}), |
| 4247 | } |
| 4248 | } |
| 3574 | 4249 | |
| 3575 | 4250 | // Due to incremental compilation, how function calls are generated depends |
| 3576 | 4251 | // on linking. |
| 3577 | 4252 | switch (info) { |
| 3578 | 4253 | .air => |callee| { |
| 3579 | | if (try self.air.value(callee, zcu)) |func_value| { |
| 4254 | if (try func.air.value(callee, zcu)) |func_value| { |
| 3580 | 4255 | const func_key = zcu.intern_pool.indexToKey(func_value.ip_index); |
| 3581 | 4256 | switch (switch (func_key) { |
| 3582 | 4257 | else => func_key, |
| ... | ... | @@ -3585,35 +4260,53 @@ fn genCall( |
| 3585 | 4260 | else => func_key, |
| 3586 | 4261 | } else func_key, |
| 3587 | 4262 | }) { |
| 3588 | | .func => |func| { |
| 3589 | | if (self.bin_file.cast(link.File.Elf)) |elf_file| { |
| 3590 | | const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func.owner_decl); |
| 4263 | .func => |func_val| { |
| 4264 | if (func.bin_file.cast(link.File.Elf)) |elf_file| { |
| 4265 | const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func_val.owner_decl); |
| 3591 | 4266 | const sym = elf_file.symbol(sym_index); |
| 3592 | 4267 | |
| 3593 | | _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file); |
| 3594 | | const got_addr = sym.zigGotAddress(elf_file); |
| 3595 | | try self.genSetReg(Type.usize, .ra, .{ .memory = @intCast(got_addr) }); |
| 3596 | | |
| 3597 | | _ = try self.addInst(.{ |
| 3598 | | .tag = .jalr, |
| 3599 | | .ops = .rri, |
| 3600 | | .data = .{ .i_type = .{ |
| 3601 | | .rd = .ra, |
| 3602 | | .rs1 = .ra, |
| 3603 | | .imm12 = Immediate.s(0), |
| 4268 | if (func.mod.pic) { |
| 4269 | return func.fail("TODO: genCall pic", .{}); |
| 4270 | } else { |
| 4271 | try func.genSetReg(Type.usize, .ra, .{ .load_symbol = .{ .sym = sym.esym_index } }); |
| 4272 | _ = try func.addInst(.{ |
| 4273 | .tag = .jalr, |
| 4274 | .ops = .rri, |
| 4275 | .data = .{ .i_type = .{ |
| 4276 | .rd = .ra, |
| 4277 | .rs1 = .ra, |
| 4278 | .imm12 = Immediate.s(0), |
| 4279 | } }, |
| 4280 | }); |
| 4281 | } |
| 4282 | } else unreachable; // not a valid riscv64 format |
| 4283 | }, |
| 4284 | .extern_func => |extern_func| { |
| 4285 | const owner_decl = zcu.declPtr(extern_func.decl); |
| 4286 | const lib_name = extern_func.lib_name.toSlice(&zcu.intern_pool); |
| 4287 | const decl_name = owner_decl.name.toSlice(&zcu.intern_pool); |
| 4288 | const atom_index = try func.symbolIndex(); |
| 4289 | |
| 4290 | if (func.bin_file.cast(link.File.Elf)) |elf_file| { |
| 4291 | _ = try func.addInst(.{ |
| 4292 | .tag = .pseudo, |
| 4293 | .ops = .pseudo_extern_fn_reloc, |
| 4294 | .data = .{ .reloc = .{ |
| 4295 | .atom_index = atom_index, |
| 4296 | .sym_index = try elf_file.getGlobalSymbol(decl_name, lib_name), |
| 3604 | 4297 | } }, |
| 3605 | 4298 | }); |
| 3606 | | } else unreachable; |
| 4299 | } else unreachable; // not a valid riscv64 format |
| 3607 | 4300 | }, |
| 3608 | | .extern_func => return self.fail("TODO: extern func calls", .{}), |
| 3609 | | else => return self.fail("TODO implement calling bitcasted functions", .{}), |
| 4301 | else => return func.fail("TODO implement calling bitcasted functions", .{}), |
| 3610 | 4302 | } |
| 3611 | 4303 | } else { |
| 3612 | | assert(self.typeOf(callee).zigTypeTag(zcu) == .Pointer); |
| 3613 | | const addr_reg, const addr_lock = try self.allocReg(); |
| 3614 | | defer self.register_manager.unlockReg(addr_lock); |
| 3615 | | try self.genSetReg(Type.usize, addr_reg, .{ .air_ref = callee }); |
| 3616 | | _ = try self.addInst(.{ |
| 4304 | assert(func.typeOf(callee).zigTypeTag(zcu) == .Pointer); |
| 4305 | const addr_reg, const addr_lock = try func.allocReg(.int); |
| 4306 | defer func.register_manager.unlockReg(addr_lock); |
| 4307 | try func.genSetReg(Type.usize, addr_reg, .{ .air_ref = callee }); |
| 4308 | |
| 4309 | _ = try func.addInst(.{ |
| 3617 | 4310 | .tag = .jalr, |
| 3618 | 4311 | .ops = .rri, |
| 3619 | 4312 | .data = .{ .i_type = .{ |
| ... | ... | @@ -3624,15 +4317,15 @@ fn genCall( |
| 3624 | 4317 | }); |
| 3625 | 4318 | } |
| 3626 | 4319 | }, |
| 3627 | | .lib => return self.fail("TODO: lib func calls", .{}), |
| 4320 | .lib => return func.fail("TODO: lib func calls", .{}), |
| 3628 | 4321 | } |
| 3629 | 4322 | |
| 3630 | 4323 | return call_info.return_value.short; |
| 3631 | 4324 | } |
| 3632 | 4325 | |
| 3633 | | fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 3634 | | const zcu = self.bin_file.comp.module.?; |
| 3635 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4326 | fn airRet(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 4327 | const zcu = func.bin_file.comp.module.?; |
| 4328 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3636 | 4329 | |
| 3637 | 4330 | if (safety) { |
| 3638 | 4331 | // safe |
| ... | ... | @@ -3640,123 +4333,138 @@ fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 3640 | 4333 | // not safe |
| 3641 | 4334 | } |
| 3642 | 4335 | |
| 3643 | | const ret_ty = self.fn_type.fnReturnType(zcu); |
| 3644 | | switch (self.ret_mcv.short) { |
| 4336 | const ret_ty = func.fn_type.fnReturnType(zcu); |
| 4337 | switch (func.ret_mcv.short) { |
| 3645 | 4338 | .none => {}, |
| 3646 | 4339 | .register, |
| 3647 | 4340 | .register_pair, |
| 3648 | | => try self.genCopy(ret_ty, self.ret_mcv.short, .{ .air_ref = un_op }), |
| 4341 | => try func.genCopy(ret_ty, func.ret_mcv.short, .{ .air_ref = un_op }), |
| 3649 | 4342 | .indirect => |reg_off| { |
| 3650 | | try self.register_manager.getReg(reg_off.reg, null); |
| 3651 | | const lock = self.register_manager.lockRegAssumeUnused(reg_off.reg); |
| 3652 | | defer self.register_manager.unlockReg(lock); |
| 3653 | | |
| 3654 | | try self.genSetReg(Type.usize, reg_off.reg, self.ret_mcv.long); |
| 3655 | | try self.genCopy( |
| 4343 | try func.register_manager.getReg(reg_off.reg, null); |
| 4344 | const lock = func.register_manager.lockRegAssumeUnused(reg_off.reg); |
| 4345 | defer func.register_manager.unlockReg(lock); |
| 4346 | |
| 4347 | try func.genSetReg(Type.usize, reg_off.reg, func.ret_mcv.long); |
| 4348 | try func.genSetMem( |
| 4349 | .{ .reg = reg_off.reg }, |
| 4350 | reg_off.off, |
| 3656 | 4351 | ret_ty, |
| 3657 | | .{ .register_offset = reg_off }, |
| 3658 | 4352 | .{ .air_ref = un_op }, |
| 3659 | 4353 | ); |
| 3660 | 4354 | }, |
| 3661 | 4355 | else => unreachable, |
| 3662 | 4356 | } |
| 3663 | 4357 | |
| 3664 | | self.ret_mcv.liveOut(self, inst); |
| 3665 | | try self.finishAir(inst, .unreach, .{ un_op, .none, .none }); |
| 4358 | func.ret_mcv.liveOut(func, inst); |
| 4359 | try func.finishAir(inst, .unreach, .{ un_op, .none, .none }); |
| 3666 | 4360 | |
| 3667 | 4361 | // Just add space for an instruction, reloced this later |
| 3668 | | const index = try self.addInst(.{ |
| 4362 | const index = try func.addInst(.{ |
| 3669 | 4363 | .tag = .pseudo, |
| 3670 | 4364 | .ops = .pseudo_j, |
| 3671 | 4365 | .data = .{ .inst = undefined }, |
| 3672 | 4366 | }); |
| 3673 | 4367 | |
| 3674 | | try self.exitlude_jump_relocs.append(self.gpa, index); |
| 4368 | try func.exitlude_jump_relocs.append(func.gpa, index); |
| 3675 | 4369 | } |
| 3676 | 4370 | |
| 3677 | | fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 3678 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3679 | | const ptr = try self.resolveInst(un_op); |
| 4371 | fn airRetLoad(func: *Func, inst: Air.Inst.Index) !void { |
| 4372 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4373 | const ptr = try func.resolveInst(un_op); |
| 3680 | 4374 | |
| 3681 | | const ptr_ty = self.typeOf(un_op); |
| 3682 | | switch (self.ret_mcv.short) { |
| 4375 | const ptr_ty = func.typeOf(un_op); |
| 4376 | switch (func.ret_mcv.short) { |
| 3683 | 4377 | .none => {}, |
| 3684 | | .register, .register_pair => try self.load(self.ret_mcv.short, ptr, ptr_ty), |
| 3685 | | .indirect => |reg_off| try self.genSetReg(ptr_ty, reg_off.reg, ptr), |
| 4378 | .register, .register_pair => try func.load(func.ret_mcv.short, ptr, ptr_ty), |
| 4379 | .indirect => |reg_off| try func.genSetReg(ptr_ty, reg_off.reg, ptr), |
| 3686 | 4380 | else => unreachable, |
| 3687 | 4381 | } |
| 3688 | | self.ret_mcv.liveOut(self, inst); |
| 3689 | | try self.finishAir(inst, .unreach, .{ un_op, .none, .none }); |
| 4382 | func.ret_mcv.liveOut(func, inst); |
| 4383 | try func.finishAir(inst, .unreach, .{ un_op, .none, .none }); |
| 3690 | 4384 | |
| 3691 | 4385 | // Just add space for an instruction, reloced this later |
| 3692 | | const index = try self.addInst(.{ |
| 4386 | const index = try func.addInst(.{ |
| 3693 | 4387 | .tag = .pseudo, |
| 3694 | 4388 | .ops = .pseudo_j, |
| 3695 | 4389 | .data = .{ .inst = undefined }, |
| 3696 | 4390 | }); |
| 3697 | 4391 | |
| 3698 | | try self.exitlude_jump_relocs.append(self.gpa, index); |
| 3699 | | } |
| 3700 | | |
| 3701 | | fn airCmp(self: *Self, inst: Air.Inst.Index) !void { |
| 3702 | | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 3703 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3704 | | const zcu = self.bin_file.comp.module.?; |
| 3705 | | |
| 3706 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3707 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 3708 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 3709 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 3710 | | |
| 3711 | | const int_ty = switch (lhs_ty.zigTypeTag(zcu)) { |
| 3712 | | .Vector => unreachable, // Handled by cmp_vector. |
| 3713 | | .Enum => lhs_ty.intTagType(zcu), |
| 3714 | | .Int => lhs_ty, |
| 3715 | | .Bool => Type.u1, |
| 3716 | | .Pointer => Type.usize, |
| 3717 | | .ErrorSet => Type.u16, |
| 3718 | | .Optional => blk: { |
| 3719 | | const payload_ty = lhs_ty.optionalChild(zcu); |
| 3720 | | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 3721 | | break :blk Type.u1; |
| 3722 | | } else if (lhs_ty.isPtrLikeOptional(zcu)) { |
| 3723 | | break :blk Type.usize; |
| 4392 | try func.exitlude_jump_relocs.append(func.gpa, index); |
| 4393 | } |
| 4394 | |
| 4395 | fn airCmp(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 4396 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4397 | const zcu = func.bin_file.comp.module.?; |
| 4398 | |
| 4399 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4400 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 4401 | |
| 4402 | switch (lhs_ty.zigTypeTag(zcu)) { |
| 4403 | .Int, |
| 4404 | .Enum, |
| 4405 | .Bool, |
| 4406 | .Pointer, |
| 4407 | .ErrorSet, |
| 4408 | .Optional, |
| 4409 | => { |
| 4410 | const int_ty = switch (lhs_ty.zigTypeTag(zcu)) { |
| 4411 | .Enum => lhs_ty.intTagType(zcu), |
| 4412 | .Int => lhs_ty, |
| 4413 | .Bool => Type.u1, |
| 4414 | .Pointer => Type.usize, |
| 4415 | .ErrorSet => Type.anyerror, |
| 4416 | .Optional => blk: { |
| 4417 | const payload_ty = lhs_ty.optionalChild(zcu); |
| 4418 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 4419 | break :blk Type.u1; |
| 4420 | } else if (lhs_ty.isPtrLikeOptional(zcu)) { |
| 4421 | break :blk Type.usize; |
| 4422 | } else { |
| 4423 | return func.fail("TODO riscv cmp non-pointer optionals", .{}); |
| 4424 | } |
| 4425 | }, |
| 4426 | else => unreachable, |
| 4427 | }; |
| 4428 | |
| 4429 | const int_info = int_ty.intInfo(zcu); |
| 4430 | if (int_info.bits <= 64) { |
| 4431 | break :result try func.binOp(inst, tag, bin_op.lhs, bin_op.rhs); |
| 3724 | 4432 | } else { |
| 3725 | | return self.fail("TODO riscv cmp non-pointer optionals", .{}); |
| 4433 | return func.fail("TODO riscv cmp for ints > 64 bits", .{}); |
| 4434 | } |
| 4435 | }, |
| 4436 | .Float => { |
| 4437 | const float_bits = lhs_ty.floatBits(func.target.*); |
| 4438 | const float_reg_size: u32 = if (func.hasFeature(.d)) 64 else 32; |
| 4439 | if (float_bits > float_reg_size) { |
| 4440 | return func.fail("TODO: airCmp float > 64/32 bits", .{}); |
| 3726 | 4441 | } |
| 4442 | break :result try func.binOp(inst, tag, bin_op.lhs, bin_op.rhs); |
| 3727 | 4443 | }, |
| 3728 | | .Float => return self.fail("TODO riscv cmp floats", .{}), |
| 3729 | 4444 | else => unreachable, |
| 3730 | | }; |
| 3731 | | |
| 3732 | | const int_info = int_ty.intInfo(zcu); |
| 3733 | | if (int_info.bits <= 64) { |
| 3734 | | break :result try self.binOp(tag, lhs, int_ty, rhs, int_ty); |
| 3735 | | } else { |
| 3736 | | return self.fail("TODO riscv cmp for ints > 64 bits", .{}); |
| 3737 | 4445 | } |
| 3738 | 4446 | }; |
| 3739 | 4447 | |
| 3740 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 4448 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3741 | 4449 | } |
| 3742 | 4450 | |
| 3743 | | fn airCmpVector(self: *Self, inst: Air.Inst.Index) !void { |
| 4451 | fn airCmpVector(func: *Func, inst: Air.Inst.Index) !void { |
| 3744 | 4452 | _ = inst; |
| 3745 | | return self.fail("TODO implement airCmpVector for {}", .{self.target.cpu.arch}); |
| 4453 | return func.fail("TODO implement airCmpVector for {}", .{func.target.cpu.arch}); |
| 3746 | 4454 | } |
| 3747 | 4455 | |
| 3748 | | fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) !void { |
| 3749 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3750 | | const operand = try self.resolveInst(un_op); |
| 4456 | fn airCmpLtErrorsLen(func: *Func, inst: Air.Inst.Index) !void { |
| 4457 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4458 | const operand = try func.resolveInst(un_op); |
| 3751 | 4459 | _ = operand; |
| 3752 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airCmpLtErrorsLen for {}", .{self.target.cpu.arch}); |
| 3753 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4460 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airCmpLtErrorsLen for {}", .{func.target.cpu.arch}); |
| 4461 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 3754 | 4462 | } |
| 3755 | 4463 | |
| 3756 | | fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void { |
| 3757 | | const dbg_stmt = self.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; |
| 4464 | fn airDbgStmt(func: *Func, inst: Air.Inst.Index) !void { |
| 4465 | const dbg_stmt = func.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; |
| 3758 | 4466 | |
| 3759 | | _ = try self.addInst(.{ |
| 4467 | _ = try func.addInst(.{ |
| 3760 | 4468 | .tag = .pseudo, |
| 3761 | 4469 | .ops = .pseudo_dbg_line_column, |
| 3762 | 4470 | .data = .{ .pseudo_dbg_line_column = .{ |
| ... | ... | @@ -3765,44 +4473,44 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void { |
| 3765 | 4473 | } }, |
| 3766 | 4474 | }); |
| 3767 | 4475 | |
| 3768 | | return self.finishAirBookkeeping(); |
| 4476 | return func.finishAirBookkeeping(); |
| 3769 | 4477 | } |
| 3770 | 4478 | |
| 3771 | | fn airDbgInlineBlock(self: *Self, inst: Air.Inst.Index) !void { |
| 3772 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3773 | | const extra = self.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 3774 | | try self.lowerBlock(inst, @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len])); |
| 4479 | fn airDbgInlineBlock(func: *Func, inst: Air.Inst.Index) !void { |
| 4480 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4481 | const extra = func.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 4482 | try func.lowerBlock(inst, @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len])); |
| 3775 | 4483 | } |
| 3776 | 4484 | |
| 3777 | | fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void { |
| 3778 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4485 | fn airDbgVar(func: *Func, inst: Air.Inst.Index) !void { |
| 4486 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 3779 | 4487 | const operand = pl_op.operand; |
| 3780 | | const ty = self.typeOf(operand); |
| 3781 | | const mcv = try self.resolveInst(operand); |
| 4488 | const ty = func.typeOf(operand); |
| 4489 | const mcv = try func.resolveInst(operand); |
| 3782 | 4490 | |
| 3783 | | const name = self.air.nullTerminatedString(pl_op.payload); |
| 4491 | const name = func.air.nullTerminatedString(pl_op.payload); |
| 3784 | 4492 | |
| 3785 | | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 3786 | | try self.genVarDbgInfo(tag, ty, mcv, name); |
| 4493 | const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4494 | try func.genVarDbgInfo(tag, ty, mcv, name); |
| 3787 | 4495 | |
| 3788 | | return self.finishAir(inst, .unreach, .{ operand, .none, .none }); |
| 4496 | return func.finishAir(inst, .unreach, .{ operand, .none, .none }); |
| 3789 | 4497 | } |
| 3790 | 4498 | |
| 3791 | 4499 | fn genVarDbgInfo( |
| 3792 | | self: Self, |
| 4500 | func: Func, |
| 3793 | 4501 | tag: Air.Inst.Tag, |
| 3794 | 4502 | ty: Type, |
| 3795 | 4503 | mcv: MCValue, |
| 3796 | 4504 | name: [:0]const u8, |
| 3797 | 4505 | ) !void { |
| 3798 | | const zcu = self.bin_file.comp.module.?; |
| 4506 | const zcu = func.bin_file.comp.module.?; |
| 3799 | 4507 | const is_ptr = switch (tag) { |
| 3800 | 4508 | .dbg_var_ptr => true, |
| 3801 | 4509 | .dbg_var_val => false, |
| 3802 | 4510 | else => unreachable, |
| 3803 | 4511 | }; |
| 3804 | 4512 | |
| 3805 | | switch (self.debug_output) { |
| 4513 | switch (func.debug_output) { |
| 3806 | 4514 | .dwarf => |dw| { |
| 3807 | 4515 | const loc: link.File.Dwarf.DeclState.DbgInfoLoc = switch (mcv) { |
| 3808 | 4516 | .register => |reg| .{ .register = reg.dwarfLocOp() }, |
| ... | ... | @@ -3819,47 +4527,47 @@ fn genVarDbgInfo( |
| 3819 | 4527 | break :blk .nop; |
| 3820 | 4528 | }, |
| 3821 | 4529 | }; |
| 3822 | | try dw.genVarDbgInfo(name, ty, zcu.funcOwnerDeclIndex(self.func_index), is_ptr, loc); |
| 4530 | try dw.genVarDbgInfo(name, ty, zcu.funcOwnerDeclIndex(func.func_index), is_ptr, loc); |
| 3823 | 4531 | }, |
| 3824 | 4532 | .plan9 => {}, |
| 3825 | 4533 | .none => {}, |
| 3826 | 4534 | } |
| 3827 | 4535 | } |
| 3828 | 4536 | |
| 3829 | | fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { |
| 3830 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 3831 | | const cond = try self.resolveInst(pl_op.operand); |
| 3832 | | const cond_ty = self.typeOf(pl_op.operand); |
| 3833 | | const extra = self.air.extraData(Air.CondBr, pl_op.payload); |
| 3834 | | const then_body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end..][0..extra.data.then_body_len]); |
| 3835 | | const else_body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]); |
| 3836 | | const liveness_cond_br = self.liveness.getCondBr(inst); |
| 4537 | fn airCondBr(func: *Func, inst: Air.Inst.Index) !void { |
| 4538 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4539 | const cond = try func.resolveInst(pl_op.operand); |
| 4540 | const cond_ty = func.typeOf(pl_op.operand); |
| 4541 | const extra = func.air.extraData(Air.CondBr, pl_op.payload); |
| 4542 | const then_body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end..][0..extra.data.then_body_len]); |
| 4543 | const else_body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]); |
| 4544 | const liveness_cond_br = func.liveness.getCondBr(inst); |
| 3837 | 4545 | |
| 3838 | 4546 | // If the condition dies here in this condbr instruction, process |
| 3839 | 4547 | // that death now instead of later as this has an effect on |
| 3840 | 4548 | // whether it needs to be spilled in the branches |
| 3841 | | if (self.liveness.operandDies(inst, 0)) { |
| 3842 | | if (pl_op.operand.toIndex()) |op_inst| try self.processDeath(op_inst); |
| 4549 | if (func.liveness.operandDies(inst, 0)) { |
| 4550 | if (pl_op.operand.toIndex()) |op_inst| try func.processDeath(op_inst); |
| 3843 | 4551 | } |
| 3844 | 4552 | |
| 3845 | | self.scope_generation += 1; |
| 3846 | | const state = try self.saveState(); |
| 3847 | | const reloc = try self.condBr(cond_ty, cond); |
| 4553 | func.scope_generation += 1; |
| 4554 | const state = try func.saveState(); |
| 4555 | const reloc = try func.condBr(cond_ty, cond); |
| 3848 | 4556 | |
| 3849 | | for (liveness_cond_br.then_deaths) |death| try self.processDeath(death); |
| 3850 | | try self.genBody(then_body); |
| 3851 | | try self.restoreState(state, &.{}, .{ |
| 4557 | for (liveness_cond_br.then_deaths) |death| try func.processDeath(death); |
| 4558 | try func.genBody(then_body); |
| 4559 | try func.restoreState(state, &.{}, .{ |
| 3852 | 4560 | .emit_instructions = false, |
| 3853 | 4561 | .update_tracking = true, |
| 3854 | 4562 | .resurrect = true, |
| 3855 | 4563 | .close_scope = true, |
| 3856 | 4564 | }); |
| 3857 | 4565 | |
| 3858 | | self.performReloc(reloc); |
| 4566 | func.performReloc(reloc); |
| 3859 | 4567 | |
| 3860 | | for (liveness_cond_br.else_deaths) |death| try self.processDeath(death); |
| 3861 | | try self.genBody(else_body); |
| 3862 | | try self.restoreState(state, &.{}, .{ |
| 4568 | for (liveness_cond_br.else_deaths) |death| try func.processDeath(death); |
| 4569 | try func.genBody(else_body); |
| 4570 | try func.restoreState(state, &.{}, .{ |
| 3863 | 4571 | .emit_instructions = false, |
| 3864 | 4572 | .update_tracking = true, |
| 3865 | 4573 | .resurrect = true, |
| ... | ... | @@ -3867,13 +4575,13 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { |
| 3867 | 4575 | }); |
| 3868 | 4576 | |
| 3869 | 4577 | // We already took care of pl_op.operand earlier, so there's nothing left to do. |
| 3870 | | self.finishAirBookkeeping(); |
| 4578 | func.finishAirBookkeeping(); |
| 3871 | 4579 | } |
| 3872 | 4580 | |
| 3873 | | fn condBr(self: *Self, cond_ty: Type, condition: MCValue) !Mir.Inst.Index { |
| 3874 | | const cond_reg = try self.copyToTmpRegister(cond_ty, condition); |
| 4581 | fn condBr(func: *Func, cond_ty: Type, condition: MCValue) !Mir.Inst.Index { |
| 4582 | const cond_reg = try func.copyToTmpRegister(cond_ty, condition); |
| 3875 | 4583 | |
| 3876 | | return try self.addInst(.{ |
| 4584 | return try func.addInst(.{ |
| 3877 | 4585 | .tag = .beq, |
| 3878 | 4586 | .ops = .rr_inst, |
| 3879 | 4587 | .data = .{ |
| ... | ... | @@ -3886,168 +4594,249 @@ fn condBr(self: *Self, cond_ty: Type, condition: MCValue) !Mir.Inst.Index { |
| 3886 | 4594 | }); |
| 3887 | 4595 | } |
| 3888 | 4596 | |
| 3889 | | fn airIsNull(self: *Self, inst: Air.Inst.Index) !void { |
| 3890 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3891 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3892 | | const operand = try self.resolveInst(un_op); |
| 3893 | | break :result try self.isNull(operand); |
| 3894 | | }; |
| 3895 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 3896 | | } |
| 4597 | fn isNull(func: *Func, inst: Air.Inst.Index, opt_ty: Type, opt_mcv: MCValue) !MCValue { |
| 4598 | const zcu = func.bin_file.comp.module.?; |
| 4599 | const pl_ty = opt_ty.optionalChild(zcu); |
| 3897 | 4600 | |
| 3898 | | fn airIsNullPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3899 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3900 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3901 | | const operand_ptr = try self.resolveInst(un_op); |
| 3902 | | const operand: MCValue = blk: { |
| 3903 | | if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 3904 | | // The MCValue that holds the pointer can be re-used as the value. |
| 3905 | | break :blk operand_ptr; |
| 3906 | | } else { |
| 3907 | | break :blk try self.allocRegOrMem(inst, true); |
| 4601 | const some_info: struct { off: i32, ty: Type } = if (opt_ty.optionalReprIsPayload(zcu)) |
| 4602 | .{ .off = 0, .ty = if (pl_ty.isSlice(zcu)) pl_ty.slicePtrFieldType(zcu) else pl_ty } |
| 4603 | else |
| 4604 | .{ .off = @intCast(pl_ty.abiSize(zcu)), .ty = Type.bool }; |
| 4605 | |
| 4606 | const return_mcv = try func.allocRegOrMem(func.typeOfIndex(inst), inst, true); |
| 4607 | assert(return_mcv == .register); // should not be larger 8 bytes |
| 4608 | const return_reg = return_mcv.register; |
| 4609 | |
| 4610 | switch (opt_mcv) { |
| 4611 | .none, |
| 4612 | .unreach, |
| 4613 | .dead, |
| 4614 | .undef, |
| 4615 | .immediate, |
| 4616 | .register_pair, |
| 4617 | .register_offset, |
| 4618 | .lea_frame, |
| 4619 | .lea_symbol, |
| 4620 | .reserved_frame, |
| 4621 | .air_ref, |
| 4622 | => return func.fail("TODO: hmm {}", .{opt_mcv}), |
| 4623 | |
| 4624 | .register => |opt_reg| { |
| 4625 | if (some_info.off == 0) { |
| 4626 | _ = try func.addInst(.{ |
| 4627 | .tag = .pseudo, |
| 4628 | .ops = .pseudo_compare, |
| 4629 | .data = .{ |
| 4630 | .compare = .{ |
| 4631 | .op = .eq, |
| 4632 | .rd = return_reg, |
| 4633 | .rs1 = opt_reg, |
| 4634 | .rs2 = try func.copyToTmpRegister( |
| 4635 | some_info.ty, |
| 4636 | .{ .immediate = 0 }, |
| 4637 | ), |
| 4638 | .ty = Type.bool, |
| 4639 | }, |
| 4640 | }, |
| 4641 | }); |
| 4642 | return return_mcv; |
| 3908 | 4643 | } |
| 3909 | | }; |
| 3910 | | try self.load(operand, operand_ptr, self.typeOf(un_op)); |
| 3911 | | break :result try self.isNull(operand); |
| 3912 | | }; |
| 3913 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4644 | assert(some_info.ty.ip_index == .bool_type); |
| 4645 | const opt_abi_size: u32 = @intCast(opt_ty.abiSize(zcu)); |
| 4646 | _ = opt_abi_size; |
| 4647 | return func.fail("TODO: isNull some_info.off != 0 register", .{}); |
| 4648 | }, |
| 4649 | |
| 4650 | .load_frame => { |
| 4651 | const opt_reg = try func.copyToTmpRegister( |
| 4652 | some_info.ty, |
| 4653 | opt_mcv.address().offset(some_info.off).deref(), |
| 4654 | ); |
| 4655 | const opt_reg_lock = func.register_manager.lockRegAssumeUnused(opt_reg); |
| 4656 | defer func.register_manager.unlockReg(opt_reg_lock); |
| 4657 | |
| 4658 | _ = try func.addInst(.{ |
| 4659 | .tag = .pseudo, |
| 4660 | .ops = .pseudo_compare, |
| 4661 | .data = .{ |
| 4662 | .compare = .{ |
| 4663 | .op = .eq, |
| 4664 | .rd = return_reg, |
| 4665 | .rs1 = opt_reg, |
| 4666 | .rs2 = try func.copyToTmpRegister( |
| 4667 | some_info.ty, |
| 4668 | .{ .immediate = 0 }, |
| 4669 | ), |
| 4670 | .ty = Type.bool, |
| 4671 | }, |
| 4672 | }, |
| 4673 | }); |
| 4674 | return return_mcv; |
| 4675 | }, |
| 4676 | |
| 4677 | else => return func.fail("TODO: isNull {}", .{opt_mcv}), |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | fn airIsNull(func: *Func, inst: Air.Inst.Index) !void { |
| 4682 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4683 | const operand = try func.resolveInst(un_op); |
| 4684 | const ty = func.typeOf(un_op); |
| 4685 | const result = try func.isNull(inst, ty, operand); |
| 4686 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 3914 | 4687 | } |
| 3915 | 4688 | |
| 3916 | | fn isNull(self: *Self, operand: MCValue) !MCValue { |
| 4689 | fn airIsNullPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4690 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4691 | const operand = try func.resolveInst(un_op); |
| 3917 | 4692 | _ = operand; |
| 3918 | | // Here you can specialize this instruction if it makes sense to, otherwise the default |
| 3919 | | // will call isNonNull and invert the result. |
| 3920 | | return self.fail("TODO call isNonNull and invert the result", .{}); |
| 4693 | const ty = func.typeOf(un_op); |
| 4694 | _ = ty; |
| 4695 | |
| 4696 | if (true) return func.fail("TODO: airIsNullPtr", .{}); |
| 4697 | |
| 4698 | return func.finishAir(inst, .unreach, .{ un_op, .none, .none }); |
| 3921 | 4699 | } |
| 3922 | 4700 | |
| 3923 | | fn airIsNonNull(self: *Self, inst: Air.Inst.Index) !void { |
| 3924 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3925 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3926 | | const operand = try self.resolveInst(un_op); |
| 3927 | | break :result try self.isNonNull(operand); |
| 3928 | | }; |
| 3929 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4701 | fn airIsNonNull(func: *Func, inst: Air.Inst.Index) !void { |
| 4702 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4703 | const operand = try func.resolveInst(un_op); |
| 4704 | const ty = func.typeOf(un_op); |
| 4705 | const result = try func.isNull(inst, ty, operand); |
| 4706 | assert(result == .register); |
| 4707 | |
| 4708 | _ = try func.addInst(.{ |
| 4709 | .tag = .pseudo, |
| 4710 | .ops = .pseudo_not, |
| 4711 | .data = .{ |
| 4712 | .rr = .{ |
| 4713 | .rd = result.register, |
| 4714 | .rs = result.register, |
| 4715 | }, |
| 4716 | }, |
| 4717 | }); |
| 4718 | |
| 4719 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 3930 | 4720 | } |
| 3931 | 4721 | |
| 3932 | | fn isNonNull(self: *Self, operand: MCValue) !MCValue { |
| 4722 | fn airIsNonNullPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4723 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4724 | const operand = try func.resolveInst(un_op); |
| 3933 | 4725 | _ = operand; |
| 3934 | | // Here you can specialize this instruction if it makes sense to, otherwise the default |
| 3935 | | // will call isNull and invert the result. |
| 3936 | | return self.fail("TODO call isNull and invert the result", .{}); |
| 3937 | | } |
| 4726 | const ty = func.typeOf(un_op); |
| 4727 | _ = ty; |
| 3938 | 4728 | |
| 3939 | | fn airIsNonNullPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3940 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3941 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3942 | | const operand_ptr = try self.resolveInst(un_op); |
| 3943 | | const operand: MCValue = blk: { |
| 3944 | | if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 3945 | | // The MCValue that holds the pointer can be re-used as the value. |
| 3946 | | break :blk operand_ptr; |
| 3947 | | } else { |
| 3948 | | break :blk try self.allocRegOrMem(inst, true); |
| 3949 | | } |
| 3950 | | }; |
| 3951 | | try self.load(operand, operand_ptr, self.typeOf(un_op)); |
| 3952 | | break :result try self.isNonNull(operand); |
| 3953 | | }; |
| 3954 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4729 | if (true) return func.fail("TODO: airIsNonNullPtr", .{}); |
| 4730 | |
| 4731 | return func.finishAir(inst, .unreach, .{ un_op, .none, .none }); |
| 3955 | 4732 | } |
| 3956 | 4733 | |
| 3957 | | fn airIsErr(self: *Self, inst: Air.Inst.Index) !void { |
| 3958 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3959 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3960 | | const operand = try self.resolveInst(un_op); |
| 3961 | | const operand_ty = self.typeOf(un_op); |
| 3962 | | break :result try self.isErr(inst, operand_ty, operand); |
| 4734 | fn airIsErr(func: *Func, inst: Air.Inst.Index) !void { |
| 4735 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4736 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4737 | const operand = try func.resolveInst(un_op); |
| 4738 | const operand_ty = func.typeOf(un_op); |
| 4739 | break :result try func.isErr(inst, operand_ty, operand); |
| 3963 | 4740 | }; |
| 3964 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4741 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 3965 | 4742 | } |
| 3966 | 4743 | |
| 3967 | | fn airIsErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3968 | | const zcu = self.bin_file.comp.module.?; |
| 3969 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3970 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3971 | | const operand_ptr = try self.resolveInst(un_op); |
| 4744 | fn airIsErrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4745 | const zcu = func.bin_file.comp.module.?; |
| 4746 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4747 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4748 | const operand_ptr = try func.resolveInst(un_op); |
| 3972 | 4749 | const operand: MCValue = blk: { |
| 3973 | | if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4750 | if (func.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 3974 | 4751 | // The MCValue that holds the pointer can be re-used as the value. |
| 3975 | 4752 | break :blk operand_ptr; |
| 3976 | 4753 | } else { |
| 3977 | | break :blk try self.allocRegOrMem(inst, true); |
| 4754 | break :blk try func.allocRegOrMem(func.typeOfIndex(inst), inst, true); |
| 3978 | 4755 | } |
| 3979 | 4756 | }; |
| 3980 | | try self.load(operand, operand_ptr, self.typeOf(un_op)); |
| 3981 | | const operand_ptr_ty = self.typeOf(un_op); |
| 4757 | try func.load(operand, operand_ptr, func.typeOf(un_op)); |
| 4758 | const operand_ptr_ty = func.typeOf(un_op); |
| 3982 | 4759 | const operand_ty = operand_ptr_ty.childType(zcu); |
| 3983 | 4760 | |
| 3984 | | break :result try self.isErr(inst, operand_ty, operand); |
| 4761 | break :result try func.isErr(inst, operand_ty, operand); |
| 3985 | 4762 | }; |
| 3986 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4763 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 3987 | 4764 | } |
| 3988 | 4765 | |
| 3989 | 4766 | /// Generates a compare instruction which will indicate if `eu_mcv` is an error. |
| 3990 | 4767 | /// |
| 3991 | 4768 | /// Result is in the return register. |
| 3992 | | fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue { |
| 3993 | | const zcu = self.bin_file.comp.module.?; |
| 4769 | fn isErr(func: *Func, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue { |
| 4770 | _ = maybe_inst; |
| 4771 | const zcu = func.bin_file.comp.module.?; |
| 3994 | 4772 | const err_ty = eu_ty.errorUnionSet(zcu); |
| 3995 | 4773 | if (err_ty.errorSetIsEmpty(zcu)) return MCValue{ .immediate = 0 }; // always false |
| 4774 | const err_off: u31 = @intCast(errUnionErrorOffset(eu_ty.errorUnionPayload(zcu), zcu)); |
| 3996 | 4775 | |
| 3997 | | _ = maybe_inst; |
| 3998 | | |
| 3999 | | const err_off = errUnionErrorOffset(eu_ty.errorUnionPayload(zcu), zcu); |
| 4776 | const return_reg, const return_lock = try func.allocReg(.int); |
| 4777 | defer func.register_manager.unlockReg(return_lock); |
| 4000 | 4778 | |
| 4001 | 4779 | switch (eu_mcv) { |
| 4002 | 4780 | .register => |reg| { |
| 4003 | | const eu_lock = self.register_manager.lockReg(reg); |
| 4004 | | defer if (eu_lock) |lock| self.register_manager.unlockReg(lock); |
| 4005 | | |
| 4006 | | const return_reg = try self.copyToTmpRegister(eu_ty, eu_mcv); |
| 4007 | | const return_lock = self.register_manager.lockRegAssumeUnused(return_reg); |
| 4008 | | defer self.register_manager.unlockReg(return_lock); |
| 4781 | const eu_lock = func.register_manager.lockReg(reg); |
| 4782 | defer if (eu_lock) |lock| func.register_manager.unlockReg(lock); |
| 4009 | 4783 | |
| 4010 | | var return_mcv: MCValue = .{ .register = return_reg }; |
| 4784 | try func.genCopy(eu_ty, .{ .register = return_reg }, eu_mcv); |
| 4011 | 4785 | |
| 4012 | 4786 | if (err_off > 0) { |
| 4013 | | return_mcv = try self.binOp( |
| 4787 | try func.genBinOp( |
| 4014 | 4788 | .shr, |
| 4015 | | return_mcv, |
| 4789 | .{ .register = return_reg }, |
| 4016 | 4790 | eu_ty, |
| 4017 | 4791 | .{ .immediate = @as(u6, @intCast(err_off * 8)) }, |
| 4018 | 4792 | Type.u8, |
| 4793 | return_reg, |
| 4019 | 4794 | ); |
| 4020 | 4795 | } |
| 4021 | 4796 | |
| 4022 | | return_mcv = try self.binOp( |
| 4797 | try func.genBinOp( |
| 4023 | 4798 | .cmp_neq, |
| 4024 | | return_mcv, |
| 4025 | | Type.u16, |
| 4799 | .{ .register = return_reg }, |
| 4800 | Type.anyerror, |
| 4026 | 4801 | .{ .immediate = 0 }, |
| 4027 | | Type.u16, |
| 4802 | Type.u8, |
| 4803 | return_reg, |
| 4028 | 4804 | ); |
| 4029 | | |
| 4030 | | return return_mcv; |
| 4031 | 4805 | }, |
| 4032 | | else => return self.fail("TODO implement isErr for {}", .{eu_mcv}), |
| 4806 | .load_frame => |frame_addr| { |
| 4807 | try func.genBinOp( |
| 4808 | .cmp_neq, |
| 4809 | .{ .load_frame = .{ |
| 4810 | .index = frame_addr.index, |
| 4811 | .off = frame_addr.off + err_off, |
| 4812 | } }, |
| 4813 | Type.anyerror, |
| 4814 | .{ .immediate = 0 }, |
| 4815 | Type.anyerror, |
| 4816 | return_reg, |
| 4817 | ); |
| 4818 | }, |
| 4819 | else => return func.fail("TODO implement isErr for {}", .{eu_mcv}), |
| 4033 | 4820 | } |
| 4821 | |
| 4822 | return .{ .register = return_reg }; |
| 4034 | 4823 | } |
| 4035 | 4824 | |
| 4036 | | fn airIsNonErr(self: *Self, inst: Air.Inst.Index) !void { |
| 4037 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4038 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4039 | | const operand = try self.resolveInst(un_op); |
| 4040 | | const ty = self.typeOf(un_op); |
| 4041 | | break :result try self.isNonErr(inst, ty, operand); |
| 4825 | fn airIsNonErr(func: *Func, inst: Air.Inst.Index) !void { |
| 4826 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4827 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4828 | const operand = try func.resolveInst(un_op); |
| 4829 | const ty = func.typeOf(un_op); |
| 4830 | break :result try func.isNonErr(inst, ty, operand); |
| 4042 | 4831 | }; |
| 4043 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4832 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4044 | 4833 | } |
| 4045 | 4834 | |
| 4046 | | fn isNonErr(self: *Self, inst: Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue { |
| 4047 | | const is_err_res = try self.isErr(inst, eu_ty, eu_mcv); |
| 4835 | fn isNonErr(func: *Func, inst: Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue { |
| 4836 | const is_err_res = try func.isErr(inst, eu_ty, eu_mcv); |
| 4048 | 4837 | switch (is_err_res) { |
| 4049 | 4838 | .register => |reg| { |
| 4050 | | _ = try self.addInst(.{ |
| 4839 | _ = try func.addInst(.{ |
| 4051 | 4840 | .tag = .pseudo, |
| 4052 | 4841 | .ops = .pseudo_not, |
| 4053 | 4842 | .data = .{ |
| ... | ... | @@ -4068,53 +4857,53 @@ fn isNonErr(self: *Self, inst: Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MC |
| 4068 | 4857 | } |
| 4069 | 4858 | } |
| 4070 | 4859 | |
| 4071 | | fn airIsNonErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4072 | | const zcu = self.bin_file.comp.module.?; |
| 4073 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4074 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4075 | | const operand_ptr = try self.resolveInst(un_op); |
| 4860 | fn airIsNonErrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4861 | const zcu = func.bin_file.comp.module.?; |
| 4862 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4863 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4864 | const operand_ptr = try func.resolveInst(un_op); |
| 4076 | 4865 | const operand: MCValue = blk: { |
| 4077 | | if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4866 | if (func.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4078 | 4867 | // The MCValue that holds the pointer can be re-used as the value. |
| 4079 | 4868 | break :blk operand_ptr; |
| 4080 | 4869 | } else { |
| 4081 | | break :blk try self.allocRegOrMem(inst, true); |
| 4870 | break :blk try func.allocRegOrMem(func.typeOfIndex(inst), inst, true); |
| 4082 | 4871 | } |
| 4083 | 4872 | }; |
| 4084 | | const operand_ptr_ty = self.typeOf(un_op); |
| 4873 | const operand_ptr_ty = func.typeOf(un_op); |
| 4085 | 4874 | const operand_ty = operand_ptr_ty.childType(zcu); |
| 4086 | 4875 | |
| 4087 | | try self.load(operand, operand_ptr, self.typeOf(un_op)); |
| 4088 | | break :result try self.isNonErr(inst, operand_ty, operand); |
| 4876 | try func.load(operand, operand_ptr, func.typeOf(un_op)); |
| 4877 | break :result try func.isNonErr(inst, operand_ty, operand); |
| 4089 | 4878 | }; |
| 4090 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4879 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4091 | 4880 | } |
| 4092 | 4881 | |
| 4093 | | fn airLoop(self: *Self, inst: Air.Inst.Index) !void { |
| 4882 | fn airLoop(func: *Func, inst: Air.Inst.Index) !void { |
| 4094 | 4883 | // A loop is a setup to be able to jump back to the beginning. |
| 4095 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4096 | | const loop = self.air.extraData(Air.Block, ty_pl.payload); |
| 4097 | | const body: []const Air.Inst.Index = @ptrCast(self.air.extra[loop.end..][0..loop.data.body_len]); |
| 4884 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4885 | const loop = func.air.extraData(Air.Block, ty_pl.payload); |
| 4886 | const body: []const Air.Inst.Index = @ptrCast(func.air.extra[loop.end..][0..loop.data.body_len]); |
| 4098 | 4887 | |
| 4099 | | self.scope_generation += 1; |
| 4100 | | const state = try self.saveState(); |
| 4888 | func.scope_generation += 1; |
| 4889 | const state = try func.saveState(); |
| 4101 | 4890 | |
| 4102 | | const jmp_target: Mir.Inst.Index = @intCast(self.mir_instructions.len); |
| 4103 | | try self.genBody(body); |
| 4104 | | try self.restoreState(state, &.{}, .{ |
| 4891 | const jmp_target: Mir.Inst.Index = @intCast(func.mir_instructions.len); |
| 4892 | try func.genBody(body); |
| 4893 | try func.restoreState(state, &.{}, .{ |
| 4105 | 4894 | .emit_instructions = true, |
| 4106 | 4895 | .update_tracking = false, |
| 4107 | 4896 | .resurrect = false, |
| 4108 | 4897 | .close_scope = true, |
| 4109 | 4898 | }); |
| 4110 | | _ = try self.jump(jmp_target); |
| 4899 | _ = try func.jump(jmp_target); |
| 4111 | 4900 | |
| 4112 | | self.finishAirBookkeeping(); |
| 4901 | func.finishAirBookkeeping(); |
| 4113 | 4902 | } |
| 4114 | 4903 | |
| 4115 | | /// Send control flow to the `index` of `self.code`. |
| 4116 | | fn jump(self: *Self, index: Mir.Inst.Index) !Mir.Inst.Index { |
| 4117 | | return self.addInst(.{ |
| 4904 | /// Send control flow to the `index` of `func.code`. |
| 4905 | fn jump(func: *Func, index: Mir.Inst.Index) !Mir.Inst.Index { |
| 4906 | return func.addInst(.{ |
| 4118 | 4907 | .tag = .pseudo, |
| 4119 | 4908 | .ops = .pseudo_j, |
| 4120 | 4909 | .data = .{ |
| ... | ... | @@ -4123,113 +4912,200 @@ fn jump(self: *Self, index: Mir.Inst.Index) !Mir.Inst.Index { |
| 4123 | 4912 | }); |
| 4124 | 4913 | } |
| 4125 | 4914 | |
| 4126 | | fn airBlock(self: *Self, inst: Air.Inst.Index) !void { |
| 4127 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4128 | | const extra = self.air.extraData(Air.Block, ty_pl.payload); |
| 4129 | | try self.lowerBlock(inst, @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len])); |
| 4915 | fn airBlock(func: *Func, inst: Air.Inst.Index) !void { |
| 4916 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4917 | const extra = func.air.extraData(Air.Block, ty_pl.payload); |
| 4918 | try func.lowerBlock(inst, @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len])); |
| 4130 | 4919 | } |
| 4131 | 4920 | |
| 4132 | | fn lowerBlock(self: *Self, inst: Air.Inst.Index, body: []const Air.Inst.Index) !void { |
| 4921 | fn lowerBlock(func: *Func, inst: Air.Inst.Index, body: []const Air.Inst.Index) !void { |
| 4133 | 4922 | // A block is a setup to be able to jump to the end. |
| 4134 | | const inst_tracking_i = self.inst_tracking.count(); |
| 4135 | | self.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(.unreach)); |
| 4923 | const inst_tracking_i = func.inst_tracking.count(); |
| 4924 | func.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(.unreach)); |
| 4136 | 4925 | |
| 4137 | | self.scope_generation += 1; |
| 4138 | | try self.blocks.putNoClobber(self.gpa, inst, .{ .state = self.initRetroactiveState() }); |
| 4139 | | const liveness = self.liveness.getBlock(inst); |
| 4926 | func.scope_generation += 1; |
| 4927 | try func.blocks.putNoClobber(func.gpa, inst, .{ .state = func.initRetroactiveState() }); |
| 4928 | const liveness = func.liveness.getBlock(inst); |
| 4140 | 4929 | |
| 4141 | 4930 | // TODO emit debug info lexical block |
| 4142 | | try self.genBody(body); |
| 4931 | try func.genBody(body); |
| 4143 | 4932 | |
| 4144 | | var block_data = self.blocks.fetchRemove(inst).?; |
| 4145 | | defer block_data.value.deinit(self.gpa); |
| 4933 | var block_data = func.blocks.fetchRemove(inst).?; |
| 4934 | defer block_data.value.deinit(func.gpa); |
| 4146 | 4935 | if (block_data.value.relocs.items.len > 0) { |
| 4147 | | try self.restoreState(block_data.value.state, liveness.deaths, .{ |
| 4936 | try func.restoreState(block_data.value.state, liveness.deaths, .{ |
| 4148 | 4937 | .emit_instructions = false, |
| 4149 | 4938 | .update_tracking = true, |
| 4150 | 4939 | .resurrect = true, |
| 4151 | 4940 | .close_scope = true, |
| 4152 | 4941 | }); |
| 4153 | | for (block_data.value.relocs.items) |reloc| self.performReloc(reloc); |
| 4942 | for (block_data.value.relocs.items) |reloc| func.performReloc(reloc); |
| 4154 | 4943 | } |
| 4155 | 4944 | |
| 4156 | | if (std.debug.runtime_safety) assert(self.inst_tracking.getIndex(inst).? == inst_tracking_i); |
| 4157 | | const tracking = &self.inst_tracking.values()[inst_tracking_i]; |
| 4158 | | if (self.liveness.isUnused(inst)) try tracking.die(self, inst); |
| 4159 | | self.getValueIfFree(tracking.short, inst); |
| 4160 | | self.finishAirBookkeeping(); |
| 4945 | if (std.debug.runtime_safety) assert(func.inst_tracking.getIndex(inst).? == inst_tracking_i); |
| 4946 | const tracking = &func.inst_tracking.values()[inst_tracking_i]; |
| 4947 | if (func.liveness.isUnused(inst)) try tracking.die(func, inst); |
| 4948 | func.getValueIfFree(tracking.short, inst); |
| 4949 | func.finishAirBookkeeping(); |
| 4161 | 4950 | } |
| 4162 | 4951 | |
| 4163 | | fn airSwitch(self: *Self, inst: Air.Inst.Index) !void { |
| 4164 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4165 | | const condition = pl_op.operand; |
| 4166 | | _ = condition; |
| 4167 | | return self.fail("TODO airSwitch for {}", .{self.target.cpu.arch}); |
| 4168 | | // return self.finishAir(inst, .dead, .{ condition, .none, .none }); |
| 4952 | fn airSwitchBr(func: *Func, inst: Air.Inst.Index) !void { |
| 4953 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4954 | const condition = try func.resolveInst(pl_op.operand); |
| 4955 | const condition_ty = func.typeOf(pl_op.operand); |
| 4956 | const switch_br = func.air.extraData(Air.SwitchBr, pl_op.payload); |
| 4957 | var extra_index: usize = switch_br.end; |
| 4958 | var case_i: u32 = 0; |
| 4959 | const liveness = try func.liveness.getSwitchBr(func.gpa, inst, switch_br.data.cases_len + 1); |
| 4960 | defer func.gpa.free(liveness.deaths); |
| 4961 | |
| 4962 | // If the condition dies here in this switch instruction, process |
| 4963 | // that death now instead of later as this has an effect on |
| 4964 | // whether it needs to be spilled in the branches |
| 4965 | if (func.liveness.operandDies(inst, 0)) { |
| 4966 | if (pl_op.operand.toIndex()) |op_inst| try func.processDeath(op_inst); |
| 4967 | } |
| 4968 | |
| 4969 | func.scope_generation += 1; |
| 4970 | const state = try func.saveState(); |
| 4971 | |
| 4972 | while (case_i < switch_br.data.cases_len) : (case_i += 1) { |
| 4973 | const case = func.air.extraData(Air.SwitchBr.Case, extra_index); |
| 4974 | const items: []const Air.Inst.Ref = |
| 4975 | @ptrCast(func.air.extra[case.end..][0..case.data.items_len]); |
| 4976 | const case_body: []const Air.Inst.Index = |
| 4977 | @ptrCast(func.air.extra[case.end + items.len ..][0..case.data.body_len]); |
| 4978 | extra_index = case.end + items.len + case_body.len; |
| 4979 | |
| 4980 | var relocs = try func.gpa.alloc(Mir.Inst.Index, items.len); |
| 4981 | defer func.gpa.free(relocs); |
| 4982 | |
| 4983 | for (items, relocs, 0..) |item, *reloc, i| { |
| 4984 | // switch branches must be comptime-known, so this is stored in an immediate |
| 4985 | const item_mcv = try func.resolveInst(item); |
| 4986 | |
| 4987 | const cmp_reg, const cmp_lock = try func.allocReg(.int); |
| 4988 | defer func.register_manager.unlockReg(cmp_lock); |
| 4989 | |
| 4990 | try func.genBinOp( |
| 4991 | .cmp_neq, |
| 4992 | condition, |
| 4993 | condition_ty, |
| 4994 | item_mcv, |
| 4995 | condition_ty, |
| 4996 | cmp_reg, |
| 4997 | ); |
| 4998 | |
| 4999 | if (!(i < relocs.len - 1)) { |
| 5000 | _ = try func.addInst(.{ |
| 5001 | .tag = .pseudo, |
| 5002 | .ops = .pseudo_not, |
| 5003 | .data = .{ .rr = .{ |
| 5004 | .rd = cmp_reg, |
| 5005 | .rs = cmp_reg, |
| 5006 | } }, |
| 5007 | }); |
| 5008 | } |
| 5009 | |
| 5010 | reloc.* = try func.condBr(condition_ty, .{ .register = cmp_reg }); |
| 5011 | } |
| 5012 | |
| 5013 | for (liveness.deaths[case_i]) |operand| try func.processDeath(operand); |
| 5014 | |
| 5015 | for (relocs[0 .. relocs.len - 1]) |reloc| func.performReloc(reloc); |
| 5016 | try func.genBody(case_body); |
| 5017 | try func.restoreState(state, &.{}, .{ |
| 5018 | .emit_instructions = false, |
| 5019 | .update_tracking = true, |
| 5020 | .resurrect = true, |
| 5021 | .close_scope = true, |
| 5022 | }); |
| 5023 | |
| 5024 | func.performReloc(relocs[relocs.len - 1]); |
| 5025 | } |
| 5026 | |
| 5027 | if (switch_br.data.else_body_len > 0) { |
| 5028 | const else_body: []const Air.Inst.Index = |
| 5029 | @ptrCast(func.air.extra[extra_index..][0..switch_br.data.else_body_len]); |
| 5030 | |
| 5031 | const else_deaths = liveness.deaths.len - 1; |
| 5032 | for (liveness.deaths[else_deaths]) |operand| try func.processDeath(operand); |
| 5033 | |
| 5034 | try func.genBody(else_body); |
| 5035 | try func.restoreState(state, &.{}, .{ |
| 5036 | .emit_instructions = false, |
| 5037 | .update_tracking = true, |
| 5038 | .resurrect = true, |
| 5039 | .close_scope = true, |
| 5040 | }); |
| 5041 | } |
| 5042 | |
| 5043 | // We already took care of pl_op.operand earlier, so there's nothing left to do |
| 5044 | func.finishAirBookkeeping(); |
| 4169 | 5045 | } |
| 4170 | 5046 | |
| 4171 | | fn performReloc(self: *Self, inst: Mir.Inst.Index) void { |
| 4172 | | const tag = self.mir_instructions.items(.tag)[inst]; |
| 4173 | | const ops = self.mir_instructions.items(.ops)[inst]; |
| 4174 | | const target: Mir.Inst.Index = @intCast(self.mir_instructions.len); |
| 5047 | fn performReloc(func: *Func, inst: Mir.Inst.Index) void { |
| 5048 | const tag = func.mir_instructions.items(.tag)[inst]; |
| 5049 | const ops = func.mir_instructions.items(.ops)[inst]; |
| 5050 | const target: Mir.Inst.Index = @intCast(func.mir_instructions.len); |
| 4175 | 5051 | |
| 4176 | 5052 | switch (tag) { |
| 4177 | 5053 | .bne, |
| 4178 | 5054 | .beq, |
| 4179 | | => self.mir_instructions.items(.data)[inst].b_type.inst = target, |
| 4180 | | .jal => self.mir_instructions.items(.data)[inst].j_type.inst = target, |
| 5055 | => func.mir_instructions.items(.data)[inst].b_type.inst = target, |
| 5056 | .jal => func.mir_instructions.items(.data)[inst].j_type.inst = target, |
| 4181 | 5057 | .pseudo => switch (ops) { |
| 4182 | | .pseudo_j => self.mir_instructions.items(.data)[inst].inst = target, |
| 5058 | .pseudo_j => func.mir_instructions.items(.data)[inst].inst = target, |
| 4183 | 5059 | else => std.debug.panic("TODO: performReloc {s}", .{@tagName(ops)}), |
| 4184 | 5060 | }, |
| 4185 | 5061 | else => std.debug.panic("TODO: performReloc {s}", .{@tagName(tag)}), |
| 4186 | 5062 | } |
| 4187 | 5063 | } |
| 4188 | 5064 | |
| 4189 | | fn airBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4190 | | const mod = self.bin_file.comp.module.?; |
| 4191 | | const br = self.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 5065 | fn airBr(func: *Func, inst: Air.Inst.Index) !void { |
| 5066 | const mod = func.bin_file.comp.module.?; |
| 5067 | const br = func.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 4192 | 5068 | |
| 4193 | | const block_ty = self.typeOfIndex(br.block_inst); |
| 5069 | const block_ty = func.typeOfIndex(br.block_inst); |
| 4194 | 5070 | const block_unused = |
| 4195 | | !block_ty.hasRuntimeBitsIgnoreComptime(mod) or self.liveness.isUnused(br.block_inst); |
| 4196 | | const block_tracking = self.inst_tracking.getPtr(br.block_inst).?; |
| 4197 | | const block_data = self.blocks.getPtr(br.block_inst).?; |
| 5071 | !block_ty.hasRuntimeBitsIgnoreComptime(mod) or func.liveness.isUnused(br.block_inst); |
| 5072 | const block_tracking = func.inst_tracking.getPtr(br.block_inst).?; |
| 5073 | const block_data = func.blocks.getPtr(br.block_inst).?; |
| 4198 | 5074 | const first_br = block_data.relocs.items.len == 0; |
| 4199 | 5075 | const block_result = result: { |
| 4200 | 5076 | if (block_unused) break :result .none; |
| 4201 | 5077 | |
| 4202 | | if (!first_br) try self.getValue(block_tracking.short, null); |
| 4203 | | const src_mcv = try self.resolveInst(br.operand); |
| 5078 | if (!first_br) try func.getValue(block_tracking.short, null); |
| 5079 | const src_mcv = try func.resolveInst(br.operand); |
| 4204 | 5080 | |
| 4205 | | if (self.reuseOperandAdvanced(inst, br.operand, 0, src_mcv, br.block_inst)) { |
| 5081 | if (func.reuseOperandAdvanced(inst, br.operand, 0, src_mcv, br.block_inst)) { |
| 4206 | 5082 | if (first_br) break :result src_mcv; |
| 4207 | 5083 | |
| 4208 | | try self.getValue(block_tracking.short, br.block_inst); |
| 5084 | try func.getValue(block_tracking.short, br.block_inst); |
| 4209 | 5085 | // .long = .none to avoid merging operand and block result stack frames. |
| 4210 | 5086 | const current_tracking: InstTracking = .{ .long = .none, .short = src_mcv }; |
| 4211 | | try current_tracking.materializeUnsafe(self, br.block_inst, block_tracking.*); |
| 4212 | | for (current_tracking.getRegs()) |src_reg| self.register_manager.freeReg(src_reg); |
| 5087 | try current_tracking.materializeUnsafe(func, br.block_inst, block_tracking.*); |
| 5088 | for (current_tracking.getRegs()) |src_reg| func.register_manager.freeReg(src_reg); |
| 4213 | 5089 | break :result block_tracking.short; |
| 4214 | 5090 | } |
| 4215 | 5091 | |
| 4216 | | const dst_mcv = if (first_br) try self.allocRegOrMem(br.block_inst, true) else dst: { |
| 4217 | | try self.getValue(block_tracking.short, br.block_inst); |
| 5092 | const dst_mcv = if (first_br) try func.allocRegOrMem(block_ty, br.block_inst, true) else dst: { |
| 5093 | try func.getValue(block_tracking.short, br.block_inst); |
| 4218 | 5094 | break :dst block_tracking.short; |
| 4219 | 5095 | }; |
| 4220 | | try self.genCopy(block_ty, dst_mcv, try self.resolveInst(br.operand)); |
| 5096 | try func.genCopy(block_ty, dst_mcv, try func.resolveInst(br.operand)); |
| 4221 | 5097 | break :result dst_mcv; |
| 4222 | 5098 | }; |
| 4223 | 5099 | |
| 4224 | 5100 | // Process operand death so that it is properly accounted for in the State below. |
| 4225 | | if (self.liveness.operandDies(inst, 0)) { |
| 4226 | | if (br.operand.toIndex()) |op_inst| try self.processDeath(op_inst); |
| 5101 | if (func.liveness.operandDies(inst, 0)) { |
| 5102 | if (br.operand.toIndex()) |op_inst| try func.processDeath(op_inst); |
| 4227 | 5103 | } |
| 4228 | 5104 | |
| 4229 | 5105 | if (first_br) { |
| 4230 | 5106 | block_tracking.* = InstTracking.init(block_result); |
| 4231 | | try self.saveRetroactiveState(&block_data.state); |
| 4232 | | } else try self.restoreState(block_data.state, &.{}, .{ |
| 5107 | try func.saveRetroactiveState(&block_data.state); |
| 5108 | } else try func.restoreState(block_data.state, &.{}, .{ |
| 4233 | 5109 | .emit_instructions = true, |
| 4234 | 5110 | .update_tracking = false, |
| 4235 | 5111 | .resurrect = false, |
| ... | ... | @@ -4238,49 +5114,88 @@ fn airBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4238 | 5114 | |
| 4239 | 5115 | // Emit a jump with a relocation. It will be patched up after the block ends. |
| 4240 | 5116 | // Leave the jump offset undefined |
| 4241 | | const jmp_reloc = try self.jump(undefined); |
| 4242 | | try block_data.relocs.append(self.gpa, jmp_reloc); |
| 5117 | const jmp_reloc = try func.jump(undefined); |
| 5118 | try block_data.relocs.append(func.gpa, jmp_reloc); |
| 4243 | 5119 | |
| 4244 | 5120 | // Stop tracking block result without forgetting tracking info |
| 4245 | | try self.freeValue(block_tracking.short); |
| 5121 | try func.freeValue(block_tracking.short); |
| 4246 | 5122 | |
| 4247 | | self.finishAirBookkeeping(); |
| 5123 | func.finishAirBookkeeping(); |
| 4248 | 5124 | } |
| 4249 | 5125 | |
| 4250 | | fn airBoolOp(self: *Self, inst: Air.Inst.Index) !void { |
| 4251 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4252 | | const air_tags = self.air.instructions.items(.tag); |
| 4253 | | _ = air_tags; |
| 4254 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement boolean operations for {}", .{self.target.cpu.arch}); |
| 4255 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 5126 | fn airBoolOp(func: *Func, inst: Air.Inst.Index) !void { |
| 5127 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5128 | const tag: Air.Inst.Tag = func.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 5129 | |
| 5130 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 5131 | const lhs = try func.resolveInst(bin_op.lhs); |
| 5132 | const rhs = try func.resolveInst(bin_op.rhs); |
| 5133 | const lhs_ty = Type.bool; |
| 5134 | const rhs_ty = Type.bool; |
| 5135 | |
| 5136 | const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs); |
| 5137 | defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 5138 | |
| 5139 | const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs); |
| 5140 | defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 5141 | |
| 5142 | const result_reg, const result_lock = try func.allocReg(.int); |
| 5143 | defer func.register_manager.unlockReg(result_lock); |
| 5144 | |
| 5145 | _ = try func.addInst(.{ |
| 5146 | .tag = if (tag == .bool_or) .@"or" else .@"and", |
| 5147 | .ops = .rrr, |
| 5148 | .data = .{ .r_type = .{ |
| 5149 | .rd = result_reg, |
| 5150 | .rs1 = lhs_reg, |
| 5151 | .rs2 = rhs_reg, |
| 5152 | } }, |
| 5153 | }); |
| 5154 | |
| 5155 | // safety truncate |
| 5156 | if (func.wantSafety()) { |
| 5157 | _ = try func.addInst(.{ |
| 5158 | .tag = .andi, |
| 5159 | .ops = .rri, |
| 5160 | .data = .{ .i_type = .{ |
| 5161 | .rd = result_reg, |
| 5162 | .rs1 = result_reg, |
| 5163 | .imm12 = Immediate.s(1), |
| 5164 | } }, |
| 5165 | }); |
| 5166 | } |
| 5167 | |
| 5168 | break :result .{ .register = result_reg }; |
| 5169 | }; |
| 5170 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 4256 | 5171 | } |
| 4257 | 5172 | |
| 4258 | | fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 4259 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4260 | | const extra = self.air.extraData(Air.Asm, ty_pl.payload); |
| 5173 | fn airAsm(func: *Func, inst: Air.Inst.Index) !void { |
| 5174 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5175 | const extra = func.air.extraData(Air.Asm, ty_pl.payload); |
| 4261 | 5176 | const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0; |
| 4262 | 5177 | const clobbers_len: u31 = @truncate(extra.data.flags); |
| 4263 | 5178 | var extra_i: usize = extra.end; |
| 4264 | 5179 | const outputs: []const Air.Inst.Ref = |
| 4265 | | @ptrCast(self.air.extra[extra_i..][0..extra.data.outputs_len]); |
| 5180 | @ptrCast(func.air.extra[extra_i..][0..extra.data.outputs_len]); |
| 4266 | 5181 | extra_i += outputs.len; |
| 4267 | | const inputs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra_i..][0..extra.data.inputs_len]); |
| 5182 | const inputs: []const Air.Inst.Ref = @ptrCast(func.air.extra[extra_i..][0..extra.data.inputs_len]); |
| 4268 | 5183 | extra_i += inputs.len; |
| 4269 | 5184 | |
| 4270 | 5185 | log.debug("airAsm input: {any}", .{inputs}); |
| 4271 | 5186 | |
| 4272 | | const dead = !is_volatile and self.liveness.isUnused(inst); |
| 5187 | const dead = !is_volatile and func.liveness.isUnused(inst); |
| 4273 | 5188 | const result: MCValue = if (dead) .unreach else result: { |
| 4274 | 5189 | if (outputs.len > 1) { |
| 4275 | | return self.fail("TODO implement codegen for asm with more than 1 output", .{}); |
| 5190 | return func.fail("TODO implement codegen for asm with more than 1 output", .{}); |
| 4276 | 5191 | } |
| 4277 | 5192 | |
| 4278 | 5193 | const output_constraint: ?[]const u8 = for (outputs) |output| { |
| 4279 | 5194 | if (output != .none) { |
| 4280 | | return self.fail("TODO implement codegen for non-expr asm", .{}); |
| 5195 | return func.fail("TODO implement codegen for non-expr asm", .{}); |
| 4281 | 5196 | } |
| 4282 | | const extra_bytes = std.mem.sliceAsBytes(self.air.extra[extra_i..]); |
| 4283 | | const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(self.air.extra[extra_i..]), 0); |
| 5197 | const extra_bytes = std.mem.sliceAsBytes(func.air.extra[extra_i..]); |
| 5198 | const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(func.air.extra[extra_i..]), 0); |
| 4284 | 5199 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); |
| 4285 | 5200 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| 4286 | 5201 | // for the string, we still use the next u32 for the null terminator. |
| ... | ... | @@ -4290,7 +5205,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 4290 | 5205 | } else null; |
| 4291 | 5206 | |
| 4292 | 5207 | for (inputs) |input| { |
| 4293 | | const input_bytes = std.mem.sliceAsBytes(self.air.extra[extra_i..]); |
| 5208 | const input_bytes = std.mem.sliceAsBytes(func.air.extra[extra_i..]); |
| 4294 | 5209 | const constraint = std.mem.sliceTo(input_bytes, 0); |
| 4295 | 5210 | const name = std.mem.sliceTo(input_bytes[constraint.len + 1 ..], 0); |
| 4296 | 5211 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| ... | ... | @@ -4298,21 +5213,21 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 4298 | 5213 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; |
| 4299 | 5214 | |
| 4300 | 5215 | if (constraint.len < 3 or constraint[0] != '{' or constraint[constraint.len - 1] != '}') { |
| 4301 | | return self.fail("unrecognized asm input constraint: '{s}'", .{constraint}); |
| 5216 | return func.fail("unrecognized asm input constraint: '{s}'", .{constraint}); |
| 4302 | 5217 | } |
| 4303 | 5218 | const reg_name = constraint[1 .. constraint.len - 1]; |
| 4304 | 5219 | const reg = parseRegName(reg_name) orelse |
| 4305 | | return self.fail("unrecognized register: '{s}'", .{reg_name}); |
| 5220 | return func.fail("unrecognized register: '{s}'", .{reg_name}); |
| 4306 | 5221 | |
| 4307 | | const arg_mcv = try self.resolveInst(input); |
| 4308 | | try self.register_manager.getReg(reg, null); |
| 4309 | | try self.genSetReg(self.typeOf(input), reg, arg_mcv); |
| 5222 | const arg_mcv = try func.resolveInst(input); |
| 5223 | try func.register_manager.getReg(reg, null); |
| 5224 | try func.genSetReg(func.typeOf(input), reg, arg_mcv); |
| 4310 | 5225 | } |
| 4311 | 5226 | |
| 4312 | 5227 | { |
| 4313 | 5228 | var clobber_i: u32 = 0; |
| 4314 | 5229 | while (clobber_i < clobbers_len) : (clobber_i += 1) { |
| 4315 | | const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(self.air.extra[extra_i..]), 0); |
| 5230 | const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(func.air.extra[extra_i..]), 0); |
| 4316 | 5231 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| 4317 | 5232 | // for the string, we still use the next u32 for the null terminator. |
| 4318 | 5233 | extra_i += clobber.len / 4 + 1; |
| ... | ... | @@ -4320,31 +5235,31 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 4320 | 5235 | if (std.mem.eql(u8, clobber, "") or std.mem.eql(u8, clobber, "memory")) { |
| 4321 | 5236 | // nothing really to do |
| 4322 | 5237 | } else { |
| 4323 | | try self.register_manager.getReg(parseRegName(clobber) orelse |
| 4324 | | return self.fail("invalid clobber: '{s}'", .{clobber}), null); |
| 5238 | try func.register_manager.getReg(parseRegName(clobber) orelse |
| 5239 | return func.fail("invalid clobber: '{s}'", .{clobber}), null); |
| 4325 | 5240 | } |
| 4326 | 5241 | } |
| 4327 | 5242 | } |
| 4328 | 5243 | |
| 4329 | | const asm_source = std.mem.sliceAsBytes(self.air.extra[extra_i..])[0..extra.data.source_len]; |
| 5244 | const asm_source = std.mem.sliceAsBytes(func.air.extra[extra_i..])[0..extra.data.source_len]; |
| 4330 | 5245 | |
| 4331 | 5246 | if (std.meta.stringToEnum(Mir.Inst.Tag, asm_source)) |tag| { |
| 4332 | | _ = try self.addInst(.{ |
| 5247 | _ = try func.addInst(.{ |
| 4333 | 5248 | .tag = tag, |
| 4334 | 5249 | .ops = .none, |
| 4335 | 5250 | .data = undefined, |
| 4336 | 5251 | }); |
| 4337 | 5252 | } else { |
| 4338 | | return self.fail("TODO: asm_source {s}", .{asm_source}); |
| 5253 | return func.fail("TODO: asm_source {s}", .{asm_source}); |
| 4339 | 5254 | } |
| 4340 | 5255 | |
| 4341 | 5256 | if (output_constraint) |output| { |
| 4342 | 5257 | if (output.len < 4 or output[0] != '=' or output[1] != '{' or output[output.len - 1] != '}') { |
| 4343 | | return self.fail("unrecognized asm output constraint: '{s}'", .{output}); |
| 5258 | return func.fail("unrecognized asm output constraint: '{s}'", .{output}); |
| 4344 | 5259 | } |
| 4345 | 5260 | const reg_name = output[2 .. output.len - 1]; |
| 4346 | 5261 | const reg = parseRegName(reg_name) orelse |
| 4347 | | return self.fail("unrecognized register: '{s}'", .{reg_name}); |
| 5262 | return func.fail("unrecognized register: '{s}'", .{reg_name}); |
| 4348 | 5263 | break :result .{ .register = reg }; |
| 4349 | 5264 | } else { |
| 4350 | 5265 | break :result .{ .none = {} }; |
| ... | ... | @@ -4363,29 +5278,29 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 4363 | 5278 | } |
| 4364 | 5279 | if (buf_index + inputs.len > buf.len) break :simple; |
| 4365 | 5280 | @memcpy(buf[buf_index..][0..inputs.len], inputs); |
| 4366 | | return self.finishAir(inst, result, buf); |
| 5281 | return func.finishAir(inst, result, buf); |
| 4367 | 5282 | } |
| 4368 | | var bt = self.liveness.iterateBigTomb(inst); |
| 4369 | | for (outputs) |output| if (output != .none) try self.feed(&bt, output); |
| 4370 | | for (inputs) |input| try self.feed(&bt, input); |
| 4371 | | return self.finishAirResult(inst, result); |
| 5283 | var bt = func.liveness.iterateBigTomb(inst); |
| 5284 | for (outputs) |output| if (output != .none) try func.feed(&bt, output); |
| 5285 | for (inputs) |input| try func.feed(&bt, input); |
| 5286 | return func.finishAirResult(inst, result); |
| 4372 | 5287 | } |
| 4373 | 5288 | |
| 4374 | | /// Sets the value without any modifications to register allocation metadata or stack allocation metadata. |
| 4375 | | fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { |
| 4376 | | const zcu = self.bin_file.comp.module.?; |
| 5289 | /// Sets the value of `dst_mcv` to the value of `src_mcv`. |
| 5290 | fn genCopy(func: *Func, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { |
| 5291 | const zcu = func.bin_file.comp.module.?; |
| 4377 | 5292 | |
| 4378 | 5293 | // There isn't anything to store |
| 4379 | 5294 | if (dst_mcv == .none) return; |
| 4380 | 5295 | |
| 4381 | 5296 | if (!dst_mcv.isMutable()) { |
| 4382 | 5297 | // panic so we can see the trace |
| 4383 | | return self.fail("tried to genCopy immutable: {s}", .{@tagName(dst_mcv)}); |
| 5298 | return std.debug.panic("tried to genCopy immutable: {s}", .{@tagName(dst_mcv)}); |
| 4384 | 5299 | } |
| 4385 | 5300 | |
| 4386 | 5301 | switch (dst_mcv) { |
| 4387 | | .register => |reg| return self.genSetReg(ty, reg, src_mcv), |
| 4388 | | .register_offset => |dst_reg_off| try self.genSetReg(ty, dst_reg_off.reg, switch (src_mcv) { |
| 5302 | .register => |reg| return func.genSetReg(ty, reg, src_mcv), |
| 5303 | .register_offset => |dst_reg_off| try func.genSetReg(ty, dst_reg_off.reg, switch (src_mcv) { |
| 4389 | 5304 | .none, |
| 4390 | 5305 | .unreach, |
| 4391 | 5306 | .dead, |
| ... | ... | @@ -4396,167 +5311,85 @@ fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { |
| 4396 | 5311 | .register_offset, |
| 4397 | 5312 | => src_mcv.offset(-dst_reg_off.off), |
| 4398 | 5313 | else => .{ .register_offset = .{ |
| 4399 | | .reg = try self.copyToTmpRegister(ty, src_mcv), |
| 5314 | .reg = try func.copyToTmpRegister(ty, src_mcv), |
| 4400 | 5315 | .off = -dst_reg_off.off, |
| 4401 | 5316 | } }, |
| 4402 | 5317 | }), |
| 4403 | | .indirect => |ro| { |
| 4404 | | const src_reg = try self.copyToTmpRegister(ty, src_mcv); |
| 4405 | | |
| 4406 | | _ = try self.addInst(.{ |
| 4407 | | .tag = .pseudo, |
| 4408 | | .ops = .pseudo_store_rm, |
| 4409 | | .data = .{ .rm = .{ |
| 4410 | | .r = src_reg, |
| 4411 | | .m = .{ |
| 4412 | | .base = .{ .reg = ro.reg }, |
| 4413 | | .mod = .{ .rm = .{ .disp = ro.off, .size = self.memSize(ty) } }, |
| 4414 | | }, |
| 4415 | | } }, |
| 4416 | | }); |
| 4417 | | }, |
| 4418 | | .load_frame => |frame| return self.genSetStack(ty, frame, src_mcv), |
| 4419 | | .memory => return self.fail("TODO: genCopy memory", .{}), |
| 5318 | .indirect => |reg_off| try func.genSetMem( |
| 5319 | .{ .reg = reg_off.reg }, |
| 5320 | reg_off.off, |
| 5321 | ty, |
| 5322 | src_mcv, |
| 5323 | ), |
| 5324 | .load_frame => |frame_addr| try func.genSetMem( |
| 5325 | .{ .frame = frame_addr.index }, |
| 5326 | frame_addr.off, |
| 5327 | ty, |
| 5328 | src_mcv, |
| 5329 | ), |
| 5330 | .memory => return func.fail("TODO: genCopy memory", .{}), |
| 4420 | 5331 | .register_pair => |dst_regs| { |
| 4421 | | const src_info: ?struct { addr_reg: Register, addr_lock: RegisterLock } = switch (src_mcv) { |
| 5332 | const src_info: ?struct { addr_reg: Register, addr_lock: ?RegisterLock } = switch (src_mcv) { |
| 4422 | 5333 | .register_pair, .memory, .indirect, .load_frame => null, |
| 4423 | 5334 | .load_symbol => src: { |
| 4424 | | const src_addr_reg, const src_addr_lock = try self.allocReg(); |
| 4425 | | errdefer self.register_manager.unlockReg(src_addr_lock); |
| 5335 | const src_addr_reg, const src_addr_lock = try func.promoteReg(Type.usize, src_mcv.address()); |
| 5336 | errdefer func.register_manager.unlockReg(src_addr_lock); |
| 4426 | 5337 | |
| 4427 | | try self.genSetReg(Type.usize, src_addr_reg, src_mcv.address()); |
| 4428 | 5338 | break :src .{ .addr_reg = src_addr_reg, .addr_lock = src_addr_lock }; |
| 4429 | 5339 | }, |
| 4430 | | .air_ref => |src_ref| return self.genCopy( |
| 5340 | .air_ref => |src_ref| return func.genCopy( |
| 4431 | 5341 | ty, |
| 4432 | 5342 | dst_mcv, |
| 4433 | | try self.resolveInst(src_ref), |
| 5343 | try func.resolveInst(src_ref), |
| 4434 | 5344 | ), |
| 4435 | | else => unreachable, |
| 5345 | else => return func.fail("genCopy register_pair src: {}", .{src_mcv}), |
| 4436 | 5346 | }; |
| 4437 | | defer if (src_info) |info| self.register_manager.unlockReg(info.addr_lock); |
| 4438 | | |
| 4439 | | var part_disp: i32 = 0; |
| 4440 | | for (dst_regs, try self.splitType(ty), 0..) |dst_reg, dst_ty, part_i| { |
| 4441 | | try self.genSetReg(dst_ty, dst_reg, switch (src_mcv) { |
| 4442 | | .register_pair => |src_regs| .{ .register = src_regs[part_i] }, |
| 4443 | | .memory, .indirect, .load_frame => src_mcv.address().offset(part_disp).deref(), |
| 4444 | | .load_symbol => .{ .indirect = .{ |
| 4445 | | .reg = src_info.?.addr_reg, |
| 4446 | | .off = part_disp, |
| 4447 | | } }, |
| 4448 | | else => unreachable, |
| 4449 | | }); |
| 4450 | | part_disp += @intCast(dst_ty.abiSize(zcu)); |
| 4451 | | } |
| 4452 | | }, |
| 4453 | | else => return self.fail("TODO: genCopy to {s} from {s}", .{ @tagName(dst_mcv), @tagName(src_mcv) }), |
| 4454 | | } |
| 4455 | | } |
| 4456 | | |
| 4457 | | fn genSetStack( |
| 4458 | | self: *Self, |
| 4459 | | ty: Type, |
| 4460 | | frame: FrameAddr, |
| 4461 | | src_mcv: MCValue, |
| 4462 | | ) InnerError!void { |
| 4463 | | const zcu = self.bin_file.comp.module.?; |
| 4464 | | const abi_size: u32 = @intCast(ty.abiSize(zcu)); |
| 4465 | | |
| 4466 | | switch (src_mcv) { |
| 4467 | | .none => return, |
| 4468 | | .dead => unreachable, |
| 4469 | | .undef => { |
| 4470 | | if (!self.wantSafety()) return; |
| 4471 | | try self.genSetStack(ty, frame, .{ .immediate = 0xaaaaaaaaaaaaaaaa }); |
| 4472 | | }, |
| 4473 | | .immediate, |
| 4474 | | .lea_frame, |
| 4475 | | => { |
| 4476 | | // TODO: remove this lock in favor of a copyToTmpRegister when we load 64 bit immediates with |
| 4477 | | // a register allocation. |
| 4478 | | const reg, const reg_lock = try self.allocReg(); |
| 4479 | | defer self.register_manager.unlockReg(reg_lock); |
| 4480 | 5347 | |
| 4481 | | try self.genSetReg(ty, reg, src_mcv); |
| 5348 | defer if (src_info) |info| { |
| 5349 | if (info.addr_lock) |lock| { |
| 5350 | func.register_manager.unlockReg(lock); |
| 5351 | } |
| 5352 | }; |
| 4482 | 5353 | |
| 4483 | | return self.genSetStack(ty, frame, .{ .register = reg }); |
| 4484 | | }, |
| 4485 | | .register => |reg| { |
| 4486 | | switch (abi_size) { |
| 4487 | | 1, 2, 4, 8 => { |
| 4488 | | _ = try self.addInst(.{ |
| 4489 | | .tag = .pseudo, |
| 4490 | | .ops = .pseudo_store_rm, |
| 4491 | | .data = .{ .rm = .{ |
| 4492 | | .r = reg, |
| 4493 | | .m = .{ |
| 4494 | | .base = .{ .frame = frame.index }, |
| 4495 | | .mod = .{ |
| 4496 | | .rm = .{ |
| 4497 | | .size = self.memSize(ty), |
| 4498 | | .disp = frame.off, |
| 4499 | | }, |
| 4500 | | }, |
| 4501 | | }, |
| 4502 | | } }, |
| 4503 | | }); |
| 4504 | | }, |
| 4505 | | else => unreachable, // register can hold a max of 8 bytes |
| 4506 | | } |
| 4507 | | }, |
| 4508 | | .register_pair => |pair| { |
| 4509 | | var part_disp: i32 = frame.off; |
| 4510 | | for (try self.splitType(ty), pair) |src_ty, src_reg| { |
| 4511 | | try self.genSetStack( |
| 4512 | | src_ty, |
| 4513 | | .{ .index = frame.index, .off = part_disp }, |
| 4514 | | .{ .register = src_reg }, |
| 4515 | | ); |
| 4516 | | part_disp += @intCast(src_ty.abiSize(zcu)); |
| 4517 | | } |
| 4518 | | }, |
| 4519 | | .load_frame, |
| 4520 | | .indirect, |
| 4521 | | .load_symbol, |
| 4522 | | => { |
| 4523 | | if (abi_size <= 8) { |
| 4524 | | const reg = try self.copyToTmpRegister(ty, src_mcv); |
| 4525 | | return self.genSetStack(ty, frame, .{ .register = reg }); |
| 5354 | var part_disp: i32 = 0; |
| 5355 | for (dst_regs, try func.splitType(ty), 0..) |dst_reg, dst_ty, part_i| { |
| 5356 | try func.genSetReg(dst_ty, dst_reg, switch (src_mcv) { |
| 5357 | .register_pair => |src_regs| .{ .register = src_regs[part_i] }, |
| 5358 | .memory, .indirect, .load_frame => src_mcv.address().offset(part_disp).deref(), |
| 5359 | .load_symbol => .{ .indirect = .{ |
| 5360 | .reg = src_info.?.addr_reg, |
| 5361 | .off = part_disp, |
| 5362 | } }, |
| 5363 | else => unreachable, |
| 5364 | }); |
| 5365 | part_disp += @intCast(dst_ty.abiSize(zcu)); |
| 4526 | 5366 | } |
| 4527 | | |
| 4528 | | try self.genInlineMemcpy( |
| 4529 | | .{ .lea_frame = frame }, |
| 4530 | | src_mcv.address(), |
| 4531 | | .{ .immediate = abi_size }, |
| 4532 | | ); |
| 4533 | 5367 | }, |
| 4534 | | .air_ref => |ref| try self.genSetStack(ty, frame, try self.resolveInst(ref)), |
| 4535 | | else => return self.fail("TODO: genSetStack {s}", .{@tagName(src_mcv)}), |
| 5368 | else => return func.fail("TODO: genCopy to {s} from {s}", .{ @tagName(dst_mcv), @tagName(src_mcv) }), |
| 4536 | 5369 | } |
| 4537 | 5370 | } |
| 4538 | 5371 | |
| 4539 | 5372 | fn genInlineMemcpy( |
| 4540 | | self: *Self, |
| 5373 | func: *Func, |
| 4541 | 5374 | dst_ptr: MCValue, |
| 4542 | 5375 | src_ptr: MCValue, |
| 4543 | 5376 | len: MCValue, |
| 4544 | 5377 | ) !void { |
| 4545 | | const regs = try self.register_manager.allocRegs(4, .{null} ** 4, tp); |
| 4546 | | const locks = self.register_manager.lockRegsAssumeUnused(4, regs); |
| 4547 | | defer for (locks) |lock| self.register_manager.unlockReg(lock); |
| 5378 | const regs = try func.register_manager.allocRegs(4, .{null} ** 4, abi.Registers.Integer.temporary); |
| 5379 | const locks = func.register_manager.lockRegsAssumeUnused(4, regs); |
| 5380 | defer for (locks) |lock| func.register_manager.unlockReg(lock); |
| 4548 | 5381 | |
| 4549 | 5382 | const count = regs[0]; |
| 4550 | 5383 | const tmp = regs[1]; |
| 4551 | 5384 | const src = regs[2]; |
| 4552 | 5385 | const dst = regs[3]; |
| 4553 | 5386 | |
| 4554 | | try self.genSetReg(Type.usize, count, len); |
| 4555 | | try self.genSetReg(Type.usize, src, src_ptr); |
| 4556 | | try self.genSetReg(Type.usize, dst, dst_ptr); |
| 5387 | try func.genSetReg(Type.usize, count, len); |
| 5388 | try func.genSetReg(Type.usize, src, src_ptr); |
| 5389 | try func.genSetReg(Type.usize, dst, dst_ptr); |
| 4557 | 5390 | |
| 4558 | 5391 | // lb tmp, 0(src) |
| 4559 | | const first_inst = try self.addInst(.{ |
| 5392 | const first_inst = try func.addInst(.{ |
| 4560 | 5393 | .tag = .lb, |
| 4561 | 5394 | .ops = .rri, |
| 4562 | 5395 | .data = .{ |
| ... | ... | @@ -4569,7 +5402,7 @@ fn genInlineMemcpy( |
| 4569 | 5402 | }); |
| 4570 | 5403 | |
| 4571 | 5404 | // sb tmp, 0(dst) |
| 4572 | | _ = try self.addInst(.{ |
| 5405 | _ = try func.addInst(.{ |
| 4573 | 5406 | .tag = .sb, |
| 4574 | 5407 | .ops = .rri, |
| 4575 | 5408 | .data = .{ |
| ... | ... | @@ -4582,7 +5415,7 @@ fn genInlineMemcpy( |
| 4582 | 5415 | }); |
| 4583 | 5416 | |
| 4584 | 5417 | // dec count by 1 |
| 4585 | | _ = try self.addInst(.{ |
| 5418 | _ = try func.addInst(.{ |
| 4586 | 5419 | .tag = .addi, |
| 4587 | 5420 | .ops = .rri, |
| 4588 | 5421 | .data = .{ |
| ... | ... | @@ -4595,12 +5428,12 @@ fn genInlineMemcpy( |
| 4595 | 5428 | }); |
| 4596 | 5429 | |
| 4597 | 5430 | // branch if count is 0 |
| 4598 | | _ = try self.addInst(.{ |
| 5431 | _ = try func.addInst(.{ |
| 4599 | 5432 | .tag = .beq, |
| 4600 | 5433 | .ops = .rr_inst, |
| 4601 | 5434 | .data = .{ |
| 4602 | 5435 | .b_type = .{ |
| 4603 | | .inst = @intCast(self.mir_instructions.len + 4), // points after the last inst |
| 5436 | .inst = @intCast(func.mir_instructions.len + 4), // points after the last inst |
| 4604 | 5437 | .rs1 = count, |
| 4605 | 5438 | .rs2 = .zero, |
| 4606 | 5439 | }, |
| ... | ... | @@ -4608,7 +5441,7 @@ fn genInlineMemcpy( |
| 4608 | 5441 | }); |
| 4609 | 5442 | |
| 4610 | 5443 | // increment the pointers |
| 4611 | | _ = try self.addInst(.{ |
| 5444 | _ = try func.addInst(.{ |
| 4612 | 5445 | .tag = .addi, |
| 4613 | 5446 | .ops = .rri, |
| 4614 | 5447 | .data = .{ |
| ... | ... | @@ -4620,7 +5453,85 @@ fn genInlineMemcpy( |
| 4620 | 5453 | }, |
| 4621 | 5454 | }); |
| 4622 | 5455 | |
| 4623 | | _ = try self.addInst(.{ |
| 5456 | _ = try func.addInst(.{ |
| 5457 | .tag = .addi, |
| 5458 | .ops = .rri, |
| 5459 | .data = .{ |
| 5460 | .i_type = .{ |
| 5461 | .rd = dst, |
| 5462 | .rs1 = dst, |
| 5463 | .imm12 = Immediate.s(1), |
| 5464 | }, |
| 5465 | }, |
| 5466 | }); |
| 5467 | |
| 5468 | // jump back to start of loop |
| 5469 | _ = try func.addInst(.{ |
| 5470 | .tag = .pseudo, |
| 5471 | .ops = .pseudo_j, |
| 5472 | .data = .{ .inst = first_inst }, |
| 5473 | }); |
| 5474 | } |
| 5475 | |
| 5476 | fn genInlineMemset( |
| 5477 | func: *Func, |
| 5478 | dst_ptr: MCValue, |
| 5479 | src_value: MCValue, |
| 5480 | len: MCValue, |
| 5481 | ) !void { |
| 5482 | const regs = try func.register_manager.allocRegs(3, .{null} ** 3, abi.Registers.Integer.temporary); |
| 5483 | const locks = func.register_manager.lockRegsAssumeUnused(3, regs); |
| 5484 | defer for (locks) |lock| func.register_manager.unlockReg(lock); |
| 5485 | |
| 5486 | const count = regs[0]; |
| 5487 | const src = regs[1]; |
| 5488 | const dst = regs[2]; |
| 5489 | |
| 5490 | try func.genSetReg(Type.usize, count, len); |
| 5491 | try func.genSetReg(Type.usize, src, src_value); |
| 5492 | try func.genSetReg(Type.usize, dst, dst_ptr); |
| 5493 | |
| 5494 | // sb src, 0(dst) |
| 5495 | const first_inst = try func.addInst(.{ |
| 5496 | .tag = .sb, |
| 5497 | .ops = .rri, |
| 5498 | .data = .{ |
| 5499 | .i_type = .{ |
| 5500 | .rd = dst, |
| 5501 | .rs1 = src, |
| 5502 | .imm12 = Immediate.s(0), |
| 5503 | }, |
| 5504 | }, |
| 5505 | }); |
| 5506 | |
| 5507 | // dec count by 1 |
| 5508 | _ = try func.addInst(.{ |
| 5509 | .tag = .addi, |
| 5510 | .ops = .rri, |
| 5511 | .data = .{ |
| 5512 | .i_type = .{ |
| 5513 | .rd = count, |
| 5514 | .rs1 = count, |
| 5515 | .imm12 = Immediate.s(-1), |
| 5516 | }, |
| 5517 | }, |
| 5518 | }); |
| 5519 | |
| 5520 | // branch if count is 0 |
| 5521 | _ = try func.addInst(.{ |
| 5522 | .tag = .beq, |
| 5523 | .ops = .rr_inst, |
| 5524 | .data = .{ |
| 5525 | .b_type = .{ |
| 5526 | .inst = @intCast(func.mir_instructions.len + 4), // points after the last inst |
| 5527 | .rs1 = count, |
| 5528 | .rs2 = .zero, |
| 5529 | }, |
| 5530 | }, |
| 5531 | }); |
| 5532 | |
| 5533 | // increment the pointers |
| 5534 | _ = try func.addInst(.{ |
| 4624 | 5535 | .tag = .addi, |
| 4625 | 5536 | .ops = .rri, |
| 4626 | 5537 | .data = .{ |
| ... | ... | @@ -4633,7 +5544,7 @@ fn genInlineMemcpy( |
| 4633 | 5544 | }); |
| 4634 | 5545 | |
| 4635 | 5546 | // jump back to start of loop |
| 4636 | | _ = try self.addInst(.{ |
| 5547 | _ = try func.addInst(.{ |
| 4637 | 5548 | .tag = .pseudo, |
| 4638 | 5549 | .ops = .pseudo_j, |
| 4639 | 5550 | .data = .{ |
| ... | ... | @@ -4643,25 +5554,29 @@ fn genInlineMemcpy( |
| 4643 | 5554 | } |
| 4644 | 5555 | |
| 4645 | 5556 | /// Sets the value of `src_mcv` into `reg`. Assumes you have a lock on it. |
| 4646 | | fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!void { |
| 4647 | | const zcu = self.bin_file.comp.module.?; |
| 5557 | fn genSetReg(func: *Func, ty: Type, reg: Register, src_mcv: MCValue) InnerError!void { |
| 5558 | const zcu = func.bin_file.comp.module.?; |
| 4648 | 5559 | const abi_size: u32 = @intCast(ty.abiSize(zcu)); |
| 4649 | 5560 | |
| 4650 | | if (abi_size > 8) return self.fail("tried to set reg with size {}", .{abi_size}); |
| 5561 | if (abi_size > 8) return std.debug.panic("tried to set reg with size {}", .{abi_size}); |
| 5562 | |
| 5563 | const dst_reg_class = reg.class(); |
| 4651 | 5564 | |
| 4652 | 5565 | switch (src_mcv) { |
| 4653 | 5566 | .dead => unreachable, |
| 4654 | 5567 | .unreach, .none => return, // Nothing to do. |
| 4655 | 5568 | .undef => { |
| 4656 | | if (!self.wantSafety()) |
| 5569 | if (!func.wantSafety()) |
| 4657 | 5570 | return; // The already existing value will do just fine. |
| 4658 | 5571 | // Write the debug undefined value. |
| 4659 | | return self.genSetReg(ty, reg, .{ .immediate = 0xaaaaaaaaaaaaaaaa }); |
| 5572 | return func.genSetReg(ty, reg, .{ .immediate = 0xaaaaaaaaaaaaaaaa }); |
| 4660 | 5573 | }, |
| 4661 | 5574 | .immediate => |unsigned_x| { |
| 5575 | assert(dst_reg_class == .int); |
| 5576 | |
| 4662 | 5577 | const x: i64 = @bitCast(unsigned_x); |
| 4663 | 5578 | if (math.minInt(i12) <= x and x <= math.maxInt(i12)) { |
| 4664 | | _ = try self.addInst(.{ |
| 5579 | _ = try func.addInst(.{ |
| 4665 | 5580 | .tag = .addi, |
| 4666 | 5581 | .ops = .rri, |
| 4667 | 5582 | .data = .{ .i_type = .{ |
| ... | ... | @@ -4675,7 +5590,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 4675 | 5590 | const carry: i32 = if (lo12 < 0) 1 else 0; |
| 4676 | 5591 | const hi20: i20 = @truncate((x >> 12) +% carry); |
| 4677 | 5592 | |
| 4678 | | _ = try self.addInst(.{ |
| 5593 | _ = try func.addInst(.{ |
| 4679 | 5594 | .tag = .lui, |
| 4680 | 5595 | .ops = .ri, |
| 4681 | 5596 | .data = .{ .u_type = .{ |
| ... | ... | @@ -4683,7 +5598,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 4683 | 5598 | .imm20 = Immediate.s(hi20), |
| 4684 | 5599 | } }, |
| 4685 | 5600 | }); |
| 4686 | | _ = try self.addInst(.{ |
| 5601 | _ = try func.addInst(.{ |
| 4687 | 5602 | .tag = .addi, |
| 4688 | 5603 | .ops = .rri, |
| 4689 | 5604 | .data = .{ .i_type = .{ |
| ... | ... | @@ -4696,27 +5611,27 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 4696 | 5611 | // TODO: use a more advanced myriad seq to do this without a reg. |
| 4697 | 5612 | // see: https://github.com/llvm/llvm-project/blob/081a66ffacfe85a37ff775addafcf3371e967328/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp#L224 |
| 4698 | 5613 | |
| 4699 | | const temp, const temp_lock = try self.allocReg(); |
| 4700 | | defer self.register_manager.unlockReg(temp_lock); |
| 5614 | const temp, const temp_lock = try func.allocReg(.int); |
| 5615 | defer func.register_manager.unlockReg(temp_lock); |
| 4701 | 5616 | |
| 4702 | 5617 | const lo32: i32 = @truncate(x); |
| 4703 | 5618 | const carry: i32 = if (lo32 < 0) 1 else 0; |
| 4704 | 5619 | const hi32: i32 = @truncate((x >> 32) +% carry); |
| 4705 | 5620 | |
| 4706 | | try self.genSetReg(Type.i32, temp, .{ .immediate = @bitCast(@as(i64, lo32)) }); |
| 4707 | | try self.genSetReg(Type.i32, reg, .{ .immediate = @bitCast(@as(i64, hi32)) }); |
| 5621 | try func.genSetReg(Type.i32, temp, .{ .immediate = @bitCast(@as(i64, lo32)) }); |
| 5622 | try func.genSetReg(Type.i32, reg, .{ .immediate = @bitCast(@as(i64, hi32)) }); |
| 4708 | 5623 | |
| 4709 | | _ = try self.addInst(.{ |
| 5624 | _ = try func.addInst(.{ |
| 4710 | 5625 | .tag = .slli, |
| 4711 | 5626 | .ops = .rri, |
| 4712 | 5627 | .data = .{ .i_type = .{ |
| 4713 | 5628 | .rd = reg, |
| 4714 | 5629 | .rs1 = reg, |
| 4715 | | .imm12 = Immediate.s(32), |
| 5630 | .imm12 = Immediate.u(32), |
| 4716 | 5631 | } }, |
| 4717 | 5632 | }); |
| 4718 | 5633 | |
| 4719 | | _ = try self.addInst(.{ |
| 5634 | _ = try func.addInst(.{ |
| 4720 | 5635 | .tag = .add, |
| 4721 | 5636 | .ops = .rrr, |
| 4722 | 5637 | .data = .{ .r_type = .{ |
| ... | ... | @@ -4732,8 +5647,15 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 4732 | 5647 | if (src_reg.id() == reg.id()) |
| 4733 | 5648 | return; |
| 4734 | 5649 | |
| 4735 | | // mov reg, src_reg |
| 4736 | | _ = try self.addInst(.{ |
| 5650 | const src_reg_class = src_reg.class(); |
| 5651 | |
| 5652 | if (src_reg_class == .float and dst_reg_class == .int) { |
| 5653 | // to move from float -> int, we use FMV.X.W |
| 5654 | return func.fail("TODO: genSetReg float -> int", .{}); |
| 5655 | } |
| 5656 | |
| 5657 | // mv reg, src_reg |
| 5658 | _ = try func.addInst(.{ |
| 4737 | 5659 | .tag = .pseudo, |
| 4738 | 5660 | .ops = .pseudo_mv, |
| 4739 | 5661 | .data = .{ .rr = .{ |
| ... | ... | @@ -4742,22 +5664,9 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 4742 | 5664 | } }, |
| 4743 | 5665 | }); |
| 4744 | 5666 | }, |
| 4745 | | .register_pair => |pair| try self.genSetReg(ty, reg, .{ .register = pair[0] }), |
| 4746 | | .memory => |addr| { |
| 4747 | | try self.genSetReg(ty, reg, .{ .immediate = addr }); |
| 4748 | | |
| 4749 | | _ = try self.addInst(.{ |
| 4750 | | .tag = .ld, |
| 4751 | | .ops = .rri, |
| 4752 | | .data = .{ .i_type = .{ |
| 4753 | | .rd = reg, |
| 4754 | | .rs1 = reg, |
| 4755 | | .imm12 = Immediate.s(0), |
| 4756 | | } }, |
| 4757 | | }); |
| 4758 | | }, |
| 5667 | .register_pair => return func.fail("genSetReg should we allow reg -> reg_pair?", .{}), |
| 4759 | 5668 | .load_frame => |frame| { |
| 4760 | | _ = try self.addInst(.{ |
| 5669 | _ = try func.addInst(.{ |
| 4761 | 5670 | .tag = .pseudo, |
| 4762 | 5671 | .ops = .pseudo_load_rm, |
| 4763 | 5672 | .data = .{ .rm = .{ |
| ... | ... | @@ -4765,47 +5674,73 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 4765 | 5674 | .m = .{ |
| 4766 | 5675 | .base = .{ .frame = frame.index }, |
| 4767 | 5676 | .mod = .{ |
| 4768 | | .rm = .{ |
| 4769 | | .size = self.memSize(ty), |
| 4770 | | .disp = frame.off, |
| 4771 | | }, |
| 5677 | .size = func.memSize(ty), |
| 5678 | .unsigned = ty.isUnsignedInt(zcu), |
| 5679 | .disp = frame.off, |
| 4772 | 5680 | }, |
| 4773 | 5681 | }, |
| 4774 | 5682 | } }, |
| 4775 | 5683 | }); |
| 4776 | 5684 | }, |
| 4777 | | .lea_frame => |frame| { |
| 4778 | | _ = try self.addInst(.{ |
| 5685 | .memory => |addr| { |
| 5686 | try func.genSetReg(ty, reg, .{ .immediate = addr }); |
| 5687 | |
| 5688 | _ = try func.addInst(.{ |
| 5689 | .tag = .ld, |
| 5690 | .ops = .rri, |
| 5691 | .data = .{ .i_type = .{ |
| 5692 | .rd = reg, |
| 5693 | .rs1 = reg, |
| 5694 | .imm12 = Immediate.u(0), |
| 5695 | } }, |
| 5696 | }); |
| 5697 | }, |
| 5698 | .lea_frame, .register_offset => { |
| 5699 | _ = try func.addInst(.{ |
| 4779 | 5700 | .tag = .pseudo, |
| 4780 | 5701 | .ops = .pseudo_lea_rm, |
| 4781 | 5702 | .data = .{ .rm = .{ |
| 4782 | 5703 | .r = reg, |
| 4783 | | .m = .{ |
| 4784 | | .base = .{ .frame = frame.index }, |
| 4785 | | .mod = .{ |
| 4786 | | .rm = .{ |
| 4787 | | .size = self.memSize(ty), |
| 5704 | .m = switch (src_mcv) { |
| 5705 | .register_offset => |reg_off| .{ |
| 5706 | .base = .{ .reg = reg_off.reg }, |
| 5707 | .mod = .{ |
| 5708 | .size = func.memSize(ty), |
| 5709 | .disp = reg_off.off, |
| 5710 | .unsigned = false, |
| 5711 | }, |
| 5712 | }, |
| 5713 | .lea_frame => |frame| .{ |
| 5714 | .base = .{ .frame = frame.index }, |
| 5715 | .mod = .{ |
| 5716 | .size = func.memSize(ty), |
| 4788 | 5717 | .disp = frame.off, |
| 5718 | .unsigned = false, |
| 4789 | 5719 | }, |
| 4790 | 5720 | }, |
| 5721 | else => unreachable, |
| 4791 | 5722 | }, |
| 4792 | 5723 | } }, |
| 4793 | 5724 | }); |
| 4794 | 5725 | }, |
| 4795 | | .load_symbol => { |
| 4796 | | try self.genSetReg(ty, reg, src_mcv.address()); |
| 4797 | | try self.genSetReg(ty, reg, .{ .indirect = .{ .reg = reg } }); |
| 4798 | | }, |
| 4799 | 5726 | .indirect => |reg_off| { |
| 5727 | const float_class = dst_reg_class == .float; |
| 5728 | |
| 4800 | 5729 | const load_tag: Mir.Inst.Tag = switch (abi_size) { |
| 4801 | | 1 => .lb, |
| 4802 | | 2 => .lh, |
| 4803 | | 4 => .lw, |
| 4804 | | 8 => .ld, |
| 4805 | | else => return self.fail("TODO: genSetReg for size {d}", .{abi_size}), |
| 5730 | 1 => if (float_class) |
| 5731 | unreachable // Zig does not support 8-bit floats |
| 5732 | else |
| 5733 | .lb, |
| 5734 | 2 => if (float_class) |
| 5735 | return func.fail("TODO: genSetReg indirect 16-bit float", .{}) |
| 5736 | else |
| 5737 | .lh, |
| 5738 | 4 => if (float_class) .flw else .lw, |
| 5739 | 8 => if (float_class) .fld else .ld, |
| 5740 | else => return std.debug.panic("TODO: genSetReg for size {d}", .{abi_size}), |
| 4806 | 5741 | }; |
| 4807 | 5742 | |
| 4808 | | _ = try self.addInst(.{ |
| 5743 | _ = try func.addInst(.{ |
| 4809 | 5744 | .tag = load_tag, |
| 4810 | 5745 | .ops = .rri, |
| 4811 | 5746 | .data = .{ .i_type = .{ |
| ... | ... | @@ -4818,54 +5753,183 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 4818 | 5753 | .lea_symbol => |sym_off| { |
| 4819 | 5754 | assert(sym_off.off == 0); |
| 4820 | 5755 | |
| 4821 | | const atom_index = try self.symbolIndex(); |
| 5756 | const atom_index = try func.symbolIndex(); |
| 4822 | 5757 | |
| 4823 | | _ = try self.addInst(.{ |
| 5758 | _ = try func.addInst(.{ |
| 4824 | 5759 | .tag = .pseudo, |
| 4825 | 5760 | .ops = .pseudo_load_symbol, |
| 4826 | | .data = .{ .payload = try self.addExtra(Mir.LoadSymbolPayload{ |
| 4827 | | .register = reg.id(), |
| 5761 | .data = .{ .payload = try func.addExtra(Mir.LoadSymbolPayload{ |
| 5762 | .register = reg.encodeId(), |
| 4828 | 5763 | .atom_index = atom_index, |
| 4829 | 5764 | .sym_index = sym_off.sym, |
| 4830 | 5765 | }) }, |
| 4831 | 5766 | }); |
| 4832 | 5767 | }, |
| 4833 | | .air_ref => |ref| try self.genSetReg(ty, reg, try self.resolveInst(ref)), |
| 4834 | | else => return self.fail("TODO: genSetReg {s}", .{@tagName(src_mcv)}), |
| 5768 | .load_symbol => { |
| 5769 | const addr_reg, const addr_lock = try func.allocReg(.int); |
| 5770 | defer func.register_manager.unlockReg(addr_lock); |
| 5771 | |
| 5772 | try func.genSetReg(ty, addr_reg, src_mcv.address()); |
| 5773 | try func.genSetReg(ty, reg, .{ .indirect = .{ .reg = addr_reg } }); |
| 5774 | }, |
| 5775 | .air_ref => |ref| try func.genSetReg(ty, reg, try func.resolveInst(ref)), |
| 5776 | else => return func.fail("TODO: genSetReg {s}", .{@tagName(src_mcv)}), |
| 5777 | } |
| 5778 | } |
| 5779 | |
| 5780 | fn genSetMem( |
| 5781 | func: *Func, |
| 5782 | base: Memory.Base, |
| 5783 | disp: i32, |
| 5784 | ty: Type, |
| 5785 | src_mcv: MCValue, |
| 5786 | ) InnerError!void { |
| 5787 | const mod = func.bin_file.comp.module.?; |
| 5788 | const abi_size: u32 = @intCast(ty.abiSize(mod)); |
| 5789 | const dst_ptr_mcv: MCValue = switch (base) { |
| 5790 | .reg => |base_reg| .{ .register_offset = .{ .reg = base_reg, .off = disp } }, |
| 5791 | .frame => |base_frame_index| .{ .lea_frame = .{ .index = base_frame_index, .off = disp } }, |
| 5792 | .reloc => |base_symbol| .{ .lea_symbol = .{ .sym = base_symbol.sym_index, .off = disp } }, |
| 5793 | }; |
| 5794 | switch (src_mcv) { |
| 5795 | .none, |
| 5796 | .unreach, |
| 5797 | .dead, |
| 5798 | .reserved_frame, |
| 5799 | => unreachable, |
| 5800 | .undef => try func.genInlineMemset( |
| 5801 | dst_ptr_mcv, |
| 5802 | src_mcv, |
| 5803 | .{ .immediate = abi_size }, |
| 5804 | ), |
| 5805 | .register_offset, |
| 5806 | .memory, |
| 5807 | .indirect, |
| 5808 | .load_frame, |
| 5809 | .lea_frame, |
| 5810 | .load_symbol, |
| 5811 | .lea_symbol, |
| 5812 | => switch (abi_size) { |
| 5813 | 0 => {}, |
| 5814 | 1, 2, 4, 8 => { |
| 5815 | // no matter what type, it should use an integer register |
| 5816 | const src_reg = try func.copyToTmpRegister(Type.usize, src_mcv); |
| 5817 | const src_lock = func.register_manager.lockRegAssumeUnused(src_reg); |
| 5818 | defer func.register_manager.unlockReg(src_lock); |
| 5819 | |
| 5820 | try func.genSetMem(base, disp, ty, .{ .register = src_reg }); |
| 5821 | }, |
| 5822 | else => try func.genInlineMemcpy( |
| 5823 | dst_ptr_mcv, |
| 5824 | src_mcv.address(), |
| 5825 | .{ .immediate = abi_size }, |
| 5826 | ), |
| 5827 | }, |
| 5828 | .register => |reg| { |
| 5829 | const mem_size = switch (base) { |
| 5830 | .frame => |base_fi| mem_size: { |
| 5831 | assert(disp >= 0); |
| 5832 | const frame_abi_size = func.frame_allocs.items(.abi_size)[@intFromEnum(base_fi)]; |
| 5833 | const frame_spill_pad = func.frame_allocs.items(.spill_pad)[@intFromEnum(base_fi)]; |
| 5834 | assert(frame_abi_size - frame_spill_pad - disp >= abi_size); |
| 5835 | break :mem_size if (frame_abi_size - frame_spill_pad - disp == abi_size) |
| 5836 | frame_abi_size |
| 5837 | else |
| 5838 | abi_size; |
| 5839 | }, |
| 5840 | else => abi_size, |
| 5841 | }; |
| 5842 | const src_size = math.ceilPowerOfTwoAssert(u32, abi_size); |
| 5843 | const src_align = Alignment.fromNonzeroByteUnits(math.ceilPowerOfTwoAssert(u32, src_size)); |
| 5844 | if (src_size > mem_size) { |
| 5845 | const frame_index = try func.allocFrameIndex(FrameAlloc.init(.{ |
| 5846 | .size = src_size, |
| 5847 | .alignment = src_align, |
| 5848 | })); |
| 5849 | const frame_mcv: MCValue = .{ .load_frame = .{ .index = frame_index } }; |
| 5850 | _ = try func.addInst(.{ |
| 5851 | .tag = .pseudo, |
| 5852 | .ops = .pseudo_store_rm, |
| 5853 | .data = .{ .rm = .{ |
| 5854 | .r = reg, |
| 5855 | .m = .{ |
| 5856 | .base = .{ .frame = frame_index }, |
| 5857 | .mod = .{ |
| 5858 | .size = Memory.Size.fromByteSize(src_size), |
| 5859 | .unsigned = false, |
| 5860 | }, |
| 5861 | }, |
| 5862 | } }, |
| 5863 | }); |
| 5864 | try func.genSetMem(base, disp, ty, frame_mcv); |
| 5865 | try func.freeValue(frame_mcv); |
| 5866 | } else _ = try func.addInst(.{ |
| 5867 | .tag = .pseudo, |
| 5868 | .ops = .pseudo_store_rm, |
| 5869 | .data = .{ .rm = .{ |
| 5870 | .r = reg, |
| 5871 | .m = .{ |
| 5872 | .base = base, |
| 5873 | .mod = .{ |
| 5874 | .size = func.memSize(ty), |
| 5875 | .disp = disp, |
| 5876 | .unsigned = false, |
| 5877 | }, |
| 5878 | }, |
| 5879 | } }, |
| 5880 | }); |
| 5881 | }, |
| 5882 | .register_pair => |src_regs| { |
| 5883 | var part_disp: i32 = disp; |
| 5884 | for (try func.splitType(ty), src_regs) |src_ty, src_reg| { |
| 5885 | try func.genSetMem(base, part_disp, src_ty, .{ .register = src_reg }); |
| 5886 | part_disp += @intCast(src_ty.abiSize(mod)); |
| 5887 | } |
| 5888 | }, |
| 5889 | .immediate => { |
| 5890 | // TODO: remove this lock in favor of a copyToTmpRegister when we load 64 bit immediates with |
| 5891 | // a register allocation. |
| 5892 | const reg, const reg_lock = try func.promoteReg(ty, src_mcv); |
| 5893 | defer if (reg_lock) |lock| func.register_manager.unlockReg(lock); |
| 5894 | |
| 5895 | return func.genSetMem(base, disp, ty, .{ .register = reg }); |
| 5896 | }, |
| 5897 | .air_ref => |src_ref| try func.genSetMem(base, disp, ty, try func.resolveInst(src_ref)), |
| 4835 | 5898 | } |
| 4836 | 5899 | } |
| 4837 | 5900 | |
| 4838 | | fn airIntFromPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4839 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5901 | fn airIntFromPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 5902 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4840 | 5903 | const result = result: { |
| 4841 | | const src_mcv = try self.resolveInst(un_op); |
| 4842 | | if (self.reuseOperand(inst, un_op, 0, src_mcv)) break :result src_mcv; |
| 5904 | const src_mcv = try func.resolveInst(un_op); |
| 5905 | const src_ty = func.typeOfIndex(inst); |
| 5906 | if (func.reuseOperand(inst, un_op, 0, src_mcv)) break :result src_mcv; |
| 4843 | 5907 | |
| 4844 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 4845 | | const dst_ty = self.typeOfIndex(inst); |
| 4846 | | try self.genCopy(dst_ty, dst_mcv, src_mcv); |
| 5908 | const dst_mcv = try func.allocRegOrMem(src_ty, inst, true); |
| 5909 | const dst_ty = func.typeOfIndex(inst); |
| 5910 | try func.genCopy(dst_ty, dst_mcv, src_mcv); |
| 4847 | 5911 | break :result dst_mcv; |
| 4848 | 5912 | }; |
| 4849 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 5913 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4850 | 5914 | } |
| 4851 | 5915 | |
| 4852 | | fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { |
| 4853 | | const zcu = self.bin_file.comp.module.?; |
| 5916 | fn airBitCast(func: *Func, inst: Air.Inst.Index) !void { |
| 5917 | const zcu = func.bin_file.comp.module.?; |
| 4854 | 5918 | |
| 4855 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4856 | | const result = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4857 | | const src_mcv = try self.resolveInst(ty_op.operand); |
| 5919 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5920 | const result = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 5921 | const src_mcv = try func.resolveInst(ty_op.operand); |
| 4858 | 5922 | |
| 4859 | | const dst_ty = self.typeOfIndex(inst); |
| 4860 | | const src_ty = self.typeOf(ty_op.operand); |
| 5923 | const dst_ty = func.typeOfIndex(inst); |
| 5924 | const src_ty = func.typeOf(ty_op.operand); |
| 4861 | 5925 | |
| 4862 | | const src_lock = if (src_mcv.getReg()) |reg| self.register_manager.lockReg(reg) else null; |
| 4863 | | defer if (src_lock) |lock| self.register_manager.unlockReg(lock); |
| 5926 | const src_lock = if (src_mcv.getReg()) |reg| func.register_manager.lockReg(reg) else null; |
| 5927 | defer if (src_lock) |lock| func.register_manager.unlockReg(lock); |
| 4864 | 5928 | |
| 4865 | 5929 | const dst_mcv = if (dst_ty.abiSize(zcu) <= src_ty.abiSize(zcu) and |
| 4866 | | self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: { |
| 4867 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 4868 | | try self.genCopy(switch (math.order(dst_ty.abiSize(zcu), src_ty.abiSize(zcu))) { |
| 5930 | func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: { |
| 5931 | const dst_mcv = try func.allocRegOrMem(dst_ty, inst, true); |
| 5932 | try func.genCopy(switch (math.order(dst_ty.abiSize(zcu), src_ty.abiSize(zcu))) { |
| 4869 | 5933 | .lt => dst_ty, |
| 4870 | 5934 | .eq => if (!dst_mcv.isMemory() or src_mcv.isMemory()) dst_ty else src_ty, |
| 4871 | 5935 | .gt => src_ty, |
| ... | ... | @@ -4880,230 +5944,440 @@ fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { |
| 4880 | 5944 | const bit_size = dst_ty.bitSize(zcu); |
| 4881 | 5945 | if (abi_size * 8 <= bit_size) break :result dst_mcv; |
| 4882 | 5946 | |
| 4883 | | return self.fail("TODO: airBitCast {} to {}", .{ src_ty.fmt(zcu), dst_ty.fmt(zcu) }); |
| 5947 | return func.fail("TODO: airBitCast {} to {}", .{ src_ty.fmt(zcu), dst_ty.fmt(zcu) }); |
| 4884 | 5948 | }; |
| 4885 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 4886 | | } |
| 5949 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5950 | } |
| 5951 | |
| 5952 | fn airArrayToSlice(func: *Func, inst: Air.Inst.Index) !void { |
| 5953 | const zcu = func.bin_file.comp.module.?; |
| 5954 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5955 | |
| 5956 | const slice_ty = func.typeOfIndex(inst); |
| 5957 | const ptr_ty = func.typeOf(ty_op.operand); |
| 5958 | const ptr = try func.resolveInst(ty_op.operand); |
| 5959 | const array_ty = ptr_ty.childType(zcu); |
| 5960 | const array_len = array_ty.arrayLen(zcu); |
| 5961 | |
| 5962 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(slice_ty, zcu)); |
| 5963 | try func.genSetMem(.{ .frame = frame_index }, 0, ptr_ty, ptr); |
| 5964 | try func.genSetMem( |
| 5965 | .{ .frame = frame_index }, |
| 5966 | @intCast(ptr_ty.abiSize(zcu)), |
| 5967 | Type.usize, |
| 5968 | .{ .immediate = array_len }, |
| 5969 | ); |
| 4887 | 5970 | |
| 4888 | | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 4889 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4890 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airArrayToSlice for {}", .{ |
| 4891 | | self.target.cpu.arch, |
| 4892 | | }); |
| 4893 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5971 | const result = MCValue{ .load_frame = .{ .index = frame_index } }; |
| 5972 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 4894 | 5973 | } |
| 4895 | 5974 | |
| 4896 | | fn airFloatFromInt(self: *Self, inst: Air.Inst.Index) !void { |
| 4897 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4898 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airFloatFromInt for {}", .{ |
| 4899 | | self.target.cpu.arch, |
| 5975 | fn airFloatFromInt(func: *Func, inst: Air.Inst.Index) !void { |
| 5976 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5977 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airFloatFromInt for {}", .{ |
| 5978 | func.target.cpu.arch, |
| 4900 | 5979 | }); |
| 4901 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5980 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 4902 | 5981 | } |
| 4903 | 5982 | |
| 4904 | | fn airIntFromFloat(self: *Self, inst: Air.Inst.Index) !void { |
| 4905 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4906 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airIntFromFloat for {}", .{ |
| 4907 | | self.target.cpu.arch, |
| 5983 | fn airIntFromFloat(func: *Func, inst: Air.Inst.Index) !void { |
| 5984 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5985 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airIntFromFloat for {}", .{ |
| 5986 | func.target.cpu.arch, |
| 4908 | 5987 | }); |
| 4909 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5988 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 4910 | 5989 | } |
| 4911 | 5990 | |
| 4912 | | fn airCmpxchg(self: *Self, inst: Air.Inst.Index) !void { |
| 4913 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4914 | | const extra = self.air.extraData(Air.Block, ty_pl.payload); |
| 5991 | fn airCmpxchg(func: *Func, inst: Air.Inst.Index) !void { |
| 5992 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5993 | const extra = func.air.extraData(Air.Block, ty_pl.payload); |
| 4915 | 5994 | _ = extra; |
| 4916 | | return self.fail("TODO implement airCmpxchg for {}", .{ |
| 4917 | | self.target.cpu.arch, |
| 5995 | return func.fail("TODO implement airCmpxchg for {}", .{ |
| 5996 | func.target.cpu.arch, |
| 4918 | 5997 | }); |
| 4919 | | // return self.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value }); |
| 5998 | // return func.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value }); |
| 4920 | 5999 | } |
| 4921 | 6000 | |
| 4922 | | fn airAtomicRmw(self: *Self, inst: Air.Inst.Index) !void { |
| 6001 | fn airAtomicRmw(func: *Func, inst: Air.Inst.Index) !void { |
| 4923 | 6002 | _ = inst; |
| 4924 | | return self.fail("TODO implement airCmpxchg for {}", .{self.target.cpu.arch}); |
| 6003 | return func.fail("TODO implement airCmpxchg for {}", .{func.target.cpu.arch}); |
| 4925 | 6004 | } |
| 4926 | 6005 | |
| 4927 | | fn airAtomicLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 6006 | fn airAtomicLoad(func: *Func, inst: Air.Inst.Index) !void { |
| 4928 | 6007 | _ = inst; |
| 4929 | | return self.fail("TODO implement airAtomicLoad for {}", .{self.target.cpu.arch}); |
| 6008 | return func.fail("TODO implement airAtomicLoad for {}", .{func.target.cpu.arch}); |
| 4930 | 6009 | } |
| 4931 | 6010 | |
| 4932 | | fn airAtomicStore(self: *Self, inst: Air.Inst.Index, order: std.builtin.AtomicOrder) !void { |
| 6011 | fn airAtomicStore(func: *Func, inst: Air.Inst.Index, order: std.builtin.AtomicOrder) !void { |
| 4933 | 6012 | _ = inst; |
| 4934 | 6013 | _ = order; |
| 4935 | | return self.fail("TODO implement airAtomicStore for {}", .{self.target.cpu.arch}); |
| 6014 | return func.fail("TODO implement airAtomicStore for {}", .{func.target.cpu.arch}); |
| 4936 | 6015 | } |
| 4937 | 6016 | |
| 4938 | | fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 4939 | | _ = inst; |
| 4940 | | if (safety) { |
| 4941 | | // TODO if the value is undef, write 0xaa bytes to dest |
| 4942 | | } else { |
| 4943 | | // TODO if the value is undef, don't lower this instruction |
| 6017 | fn airMemset(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 6018 | const zcu = func.bin_file.comp.module.?; |
| 6019 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6020 | |
| 6021 | result: { |
| 6022 | if (!safety and (try func.resolveInst(bin_op.rhs)) == .undef) break :result; |
| 6023 | |
| 6024 | const dst_ptr = try func.resolveInst(bin_op.lhs); |
| 6025 | const dst_ptr_ty = func.typeOf(bin_op.lhs); |
| 6026 | const dst_ptr_lock: ?RegisterLock = switch (dst_ptr) { |
| 6027 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 6028 | else => null, |
| 6029 | }; |
| 6030 | defer if (dst_ptr_lock) |lock| func.register_manager.unlockReg(lock); |
| 6031 | |
| 6032 | const src_val = try func.resolveInst(bin_op.rhs); |
| 6033 | const elem_ty = func.typeOf(bin_op.rhs); |
| 6034 | const src_val_lock: ?RegisterLock = switch (src_val) { |
| 6035 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 6036 | else => null, |
| 6037 | }; |
| 6038 | defer if (src_val_lock) |lock| func.register_manager.unlockReg(lock); |
| 6039 | |
| 6040 | const elem_abi_size: u31 = @intCast(elem_ty.abiSize(zcu)); |
| 6041 | |
| 6042 | if (elem_abi_size == 1) { |
| 6043 | const ptr: MCValue = switch (dst_ptr_ty.ptrSize(zcu)) { |
| 6044 | // TODO: this only handles slices stored in the stack |
| 6045 | .Slice => dst_ptr, |
| 6046 | .One => dst_ptr, |
| 6047 | .C, .Many => unreachable, |
| 6048 | }; |
| 6049 | const len: MCValue = switch (dst_ptr_ty.ptrSize(zcu)) { |
| 6050 | // TODO: this only handles slices stored in the stack |
| 6051 | .Slice => dst_ptr.address().offset(8).deref(), |
| 6052 | .One => .{ .immediate = dst_ptr_ty.childType(zcu).arrayLen(zcu) }, |
| 6053 | .C, .Many => unreachable, |
| 6054 | }; |
| 6055 | const len_lock: ?RegisterLock = switch (len) { |
| 6056 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 6057 | else => null, |
| 6058 | }; |
| 6059 | defer if (len_lock) |lock| func.register_manager.unlockReg(lock); |
| 6060 | |
| 6061 | try func.genInlineMemset(ptr, src_val, len); |
| 6062 | break :result; |
| 6063 | } |
| 6064 | |
| 6065 | // Store the first element, and then rely on memcpy copying forwards. |
| 6066 | // Length zero requires a runtime check - so we handle arrays specially |
| 6067 | // here to elide it. |
| 6068 | switch (dst_ptr_ty.ptrSize(zcu)) { |
| 6069 | .Slice => return func.fail("TODO: airMemset Slices", .{}), |
| 6070 | .One => { |
| 6071 | const elem_ptr_ty = try zcu.singleMutPtrType(elem_ty); |
| 6072 | |
| 6073 | const len = dst_ptr_ty.childType(zcu).arrayLen(zcu); |
| 6074 | |
| 6075 | assert(len != 0); // prevented by Sema |
| 6076 | try func.store(dst_ptr, src_val, elem_ptr_ty, elem_ty); |
| 6077 | |
| 6078 | const second_elem_ptr_reg, const second_elem_ptr_lock = try func.allocReg(.int); |
| 6079 | defer func.register_manager.unlockReg(second_elem_ptr_lock); |
| 6080 | |
| 6081 | const second_elem_ptr_mcv: MCValue = .{ .register = second_elem_ptr_reg }; |
| 6082 | |
| 6083 | try func.genSetReg(Type.usize, second_elem_ptr_reg, .{ .register_offset = .{ |
| 6084 | .reg = try func.copyToTmpRegister(Type.usize, dst_ptr), |
| 6085 | .off = elem_abi_size, |
| 6086 | } }); |
| 6087 | |
| 6088 | const bytes_to_copy: MCValue = .{ .immediate = elem_abi_size * (len - 1) }; |
| 6089 | try func.genInlineMemcpy(second_elem_ptr_mcv, dst_ptr, bytes_to_copy); |
| 6090 | }, |
| 6091 | .C, .Many => unreachable, |
| 6092 | } |
| 4944 | 6093 | } |
| 4945 | | return self.fail("TODO implement airMemset for {}", .{self.target.cpu.arch}); |
| 6094 | return func.finishAir(inst, .unreach, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 4946 | 6095 | } |
| 4947 | 6096 | |
| 4948 | | fn airMemcpy(self: *Self, inst: Air.Inst.Index) !void { |
| 6097 | fn airMemcpy(func: *Func, inst: Air.Inst.Index) !void { |
| 4949 | 6098 | _ = inst; |
| 4950 | | return self.fail("TODO implement airMemcpy for {}", .{self.target.cpu.arch}); |
| 6099 | return func.fail("TODO implement airMemcpy for {}", .{func.target.cpu.arch}); |
| 4951 | 6100 | } |
| 4952 | 6101 | |
| 4953 | | fn airTagName(self: *Self, inst: Air.Inst.Index) !void { |
| 4954 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4955 | | const operand = try self.resolveInst(un_op); |
| 4956 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else { |
| 6102 | fn airTagName(func: *Func, inst: Air.Inst.Index) !void { |
| 6103 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6104 | const operand = try func.resolveInst(un_op); |
| 6105 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else { |
| 4957 | 6106 | _ = operand; |
| 4958 | | return self.fail("TODO implement airTagName for riscv64", .{}); |
| 6107 | return func.fail("TODO implement airTagName for riscv64", .{}); |
| 4959 | 6108 | }; |
| 4960 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 6109 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4961 | 6110 | } |
| 4962 | 6111 | |
| 4963 | | fn airErrorName(self: *Self, inst: Air.Inst.Index) !void { |
| 4964 | | const zcu = self.bin_file.comp.module.?; |
| 4965 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6112 | fn airErrorName(func: *Func, inst: Air.Inst.Index) !void { |
| 6113 | const zcu = func.bin_file.comp.module.?; |
| 6114 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4966 | 6115 | |
| 4967 | | const err_ty = self.typeOf(un_op); |
| 4968 | | const err_mcv = try self.resolveInst(un_op); |
| 6116 | const err_ty = func.typeOf(un_op); |
| 6117 | const err_mcv = try func.resolveInst(un_op); |
| 4969 | 6118 | |
| 4970 | | const err_reg = try self.copyToTmpRegister(err_ty, err_mcv); |
| 4971 | | const err_lock = self.register_manager.lockRegAssumeUnused(err_reg); |
| 4972 | | defer self.register_manager.unlockReg(err_lock); |
| 6119 | const err_reg = try func.copyToTmpRegister(err_ty, err_mcv); |
| 6120 | const err_lock = func.register_manager.lockRegAssumeUnused(err_reg); |
| 6121 | defer func.register_manager.unlockReg(err_lock); |
| 4973 | 6122 | |
| 4974 | | const addr_reg, const addr_lock = try self.allocReg(); |
| 4975 | | defer self.register_manager.unlockReg(addr_lock); |
| 6123 | const addr_reg, const addr_lock = try func.allocReg(.int); |
| 6124 | defer func.register_manager.unlockReg(addr_lock); |
| 4976 | 6125 | |
| 6126 | // this is now the base address of the error name table |
| 4977 | 6127 | const lazy_sym = link.File.LazySymbol.initDecl(.const_data, null, zcu); |
| 4978 | | if (self.bin_file.cast(link.File.Elf)) |elf_file| { |
| 6128 | if (func.bin_file.cast(link.File.Elf)) |elf_file| { |
| 4979 | 6129 | const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err| |
| 4980 | | return self.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 6130 | return func.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 4981 | 6131 | const sym = elf_file.symbol(sym_index); |
| 4982 | | try self.genSetReg(Type.usize, addr_reg, .{ .load_symbol = .{ .sym = sym.esym_index } }); |
| 6132 | try func.genSetReg(Type.usize, addr_reg, .{ .load_symbol = .{ .sym = sym.esym_index } }); |
| 4983 | 6133 | } else { |
| 4984 | | return self.fail("TODO: riscv non-elf", .{}); |
| 6134 | return func.fail("TODO: riscv non-elf", .{}); |
| 4985 | 6135 | } |
| 4986 | 6136 | |
| 4987 | | const start_reg, const start_lock = try self.allocReg(); |
| 4988 | | defer self.register_manager.unlockReg(start_lock); |
| 6137 | const start_reg, const start_lock = try func.allocReg(.int); |
| 6138 | defer func.register_manager.unlockReg(start_lock); |
| 6139 | |
| 6140 | const end_reg, const end_lock = try func.allocReg(.int); |
| 6141 | defer func.register_manager.unlockReg(end_lock); |
| 6142 | |
| 6143 | // const tmp_reg, const tmp_lock = try func.allocReg(.int); |
| 6144 | // defer func.register_manager.unlockReg(tmp_lock); |
| 6145 | |
| 6146 | // we move the base address forward by the following formula: base + (errno * 8) |
| 6147 | |
| 6148 | // shifting left by 4 is the same as multiplying by 8 |
| 6149 | _ = try func.addInst(.{ |
| 6150 | .tag = .slli, |
| 6151 | .ops = .rri, |
| 6152 | .data = .{ .i_type = .{ |
| 6153 | .imm12 = Immediate.u(4), |
| 6154 | .rd = err_reg, |
| 6155 | .rs1 = err_reg, |
| 6156 | } }, |
| 6157 | }); |
| 6158 | |
| 6159 | _ = try func.addInst(.{ |
| 6160 | .tag = .add, |
| 6161 | .ops = .rrr, |
| 6162 | .data = .{ .r_type = .{ |
| 6163 | .rd = addr_reg, |
| 6164 | .rs1 = addr_reg, |
| 6165 | .rs2 = err_reg, |
| 6166 | } }, |
| 6167 | }); |
| 6168 | |
| 6169 | _ = try func.addInst(.{ |
| 6170 | .tag = .pseudo, |
| 6171 | .ops = .pseudo_load_rm, |
| 6172 | .data = .{ |
| 6173 | .rm = .{ |
| 6174 | .r = start_reg, |
| 6175 | .m = .{ |
| 6176 | .base = .{ .reg = addr_reg }, |
| 6177 | .mod = .{ .size = .dword, .unsigned = true }, |
| 6178 | }, |
| 6179 | }, |
| 6180 | }, |
| 6181 | }); |
| 6182 | |
| 6183 | _ = try func.addInst(.{ |
| 6184 | .tag = .pseudo, |
| 6185 | .ops = .pseudo_load_rm, |
| 6186 | .data = .{ |
| 6187 | .rm = .{ |
| 6188 | .r = end_reg, |
| 6189 | .m = .{ |
| 6190 | .base = .{ .reg = addr_reg }, |
| 6191 | .mod = .{ .size = .dword, .unsigned = true }, |
| 6192 | }, |
| 6193 | }, |
| 6194 | }, |
| 6195 | }); |
| 4989 | 6196 | |
| 4990 | | const end_reg, const end_lock = try self.allocReg(); |
| 4991 | | defer self.register_manager.unlockReg(end_lock); |
| 6197 | const dst_mcv = try func.allocRegOrMem(func.typeOfIndex(inst), inst, false); |
| 6198 | const frame = dst_mcv.load_frame; |
| 6199 | try func.genSetMem( |
| 6200 | .{ .frame = frame.index }, |
| 6201 | frame.off, |
| 6202 | Type.usize, |
| 6203 | .{ .register = start_reg }, |
| 6204 | ); |
| 4992 | 6205 | |
| 4993 | | _ = start_reg; |
| 4994 | | _ = end_reg; |
| 6206 | try func.genSetMem( |
| 6207 | .{ .frame = frame.index }, |
| 6208 | frame.off + 8, |
| 6209 | Type.usize, |
| 6210 | .{ .register = end_reg }, |
| 6211 | ); |
| 4995 | 6212 | |
| 4996 | | return self.fail("TODO: airErrorName", .{}); |
| 6213 | return func.finishAir(inst, dst_mcv, .{ un_op, .none, .none }); |
| 4997 | 6214 | } |
| 4998 | 6215 | |
| 4999 | | fn airSplat(self: *Self, inst: Air.Inst.Index) !void { |
| 5000 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5001 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airSplat for riscv64", .{}); |
| 5002 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 6216 | fn airSplat(func: *Func, inst: Air.Inst.Index) !void { |
| 6217 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6218 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airSplat for riscv64", .{}); |
| 6219 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5003 | 6220 | } |
| 5004 | 6221 | |
| 5005 | | fn airSelect(self: *Self, inst: Air.Inst.Index) !void { |
| 5006 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 5007 | | const extra = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 5008 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airSelect for riscv64", .{}); |
| 5009 | | return self.finishAir(inst, result, .{ pl_op.operand, extra.lhs, extra.rhs }); |
| 6222 | fn airSelect(func: *Func, inst: Air.Inst.Index) !void { |
| 6223 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6224 | const extra = func.air.extraData(Air.Bin, pl_op.payload).data; |
| 6225 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airSelect for riscv64", .{}); |
| 6226 | return func.finishAir(inst, result, .{ pl_op.operand, extra.lhs, extra.rhs }); |
| 5010 | 6227 | } |
| 5011 | 6228 | |
| 5012 | | fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 5013 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5014 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airShuffle for riscv64", .{}); |
| 5015 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 6229 | fn airShuffle(func: *Func, inst: Air.Inst.Index) !void { |
| 6230 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6231 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airShuffle for riscv64", .{}); |
| 6232 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5016 | 6233 | } |
| 5017 | 6234 | |
| 5018 | | fn airReduce(self: *Self, inst: Air.Inst.Index) !void { |
| 5019 | | const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 5020 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airReduce for riscv64", .{}); |
| 5021 | | return self.finishAir(inst, result, .{ reduce.operand, .none, .none }); |
| 6235 | fn airReduce(func: *Func, inst: Air.Inst.Index) !void { |
| 6236 | const reduce = func.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 6237 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airReduce for riscv64", .{}); |
| 6238 | return func.finishAir(inst, result, .{ reduce.operand, .none, .none }); |
| 5022 | 6239 | } |
| 5023 | 6240 | |
| 5024 | | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 5025 | | const zcu = self.bin_file.comp.module.?; |
| 5026 | | const result_ty = self.typeOfIndex(inst); |
| 6241 | fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void { |
| 6242 | const zcu = func.bin_file.comp.module.?; |
| 6243 | const result_ty = func.typeOfIndex(inst); |
| 5027 | 6244 | const len: usize = @intCast(result_ty.arrayLen(zcu)); |
| 5028 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5029 | | const elements: []const Air.Inst.Ref = @ptrCast(self.air.extra[ty_pl.payload..][0..len]); |
| 6245 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6246 | const elements: []const Air.Inst.Ref = @ptrCast(func.air.extra[ty_pl.payload..][0..len]); |
| 6247 | |
| 5030 | 6248 | const result: MCValue = result: { |
| 5031 | 6249 | switch (result_ty.zigTypeTag(zcu)) { |
| 5032 | 6250 | .Struct => { |
| 5033 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu)); |
| 6251 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu)); |
| 6252 | if (result_ty.containerLayout(zcu) == .@"packed") { |
| 6253 | const struct_obj = zcu.typeToStruct(result_ty).?; |
| 6254 | try func.genInlineMemset( |
| 6255 | .{ .lea_frame = .{ .index = frame_index } }, |
| 6256 | .{ .immediate = 0 }, |
| 6257 | .{ .immediate = result_ty.abiSize(zcu) }, |
| 6258 | ); |
| 5034 | 6259 | |
| 5035 | | if (result_ty.containerLayout(zcu) == .@"packed") {} else for (elements, 0..) |elem, elem_i| { |
| 6260 | for (elements, 0..) |elem, elem_i_usize| { |
| 6261 | const elem_i: u32 = @intCast(elem_i_usize); |
| 6262 | if ((try result_ty.structFieldValueComptime(zcu, elem_i)) != null) continue; |
| 6263 | |
| 6264 | const elem_ty = result_ty.structFieldType(elem_i, zcu); |
| 6265 | const elem_bit_size: u32 = @intCast(elem_ty.bitSize(zcu)); |
| 6266 | if (elem_bit_size > 64) { |
| 6267 | return func.fail( |
| 6268 | "TODO airAggregateInit implement packed structs with large fields", |
| 6269 | .{}, |
| 6270 | ); |
| 6271 | } |
| 6272 | |
| 6273 | const elem_abi_size: u32 = @intCast(elem_ty.abiSize(zcu)); |
| 6274 | const elem_abi_bits = elem_abi_size * 8; |
| 6275 | const elem_off = zcu.structPackedFieldBitOffset(struct_obj, elem_i); |
| 6276 | const elem_byte_off: i32 = @intCast(elem_off / elem_abi_bits * elem_abi_size); |
| 6277 | const elem_bit_off = elem_off % elem_abi_bits; |
| 6278 | const elem_mcv = try func.resolveInst(elem); |
| 6279 | |
| 6280 | _ = elem_byte_off; |
| 6281 | _ = elem_bit_off; |
| 6282 | |
| 6283 | const elem_lock = switch (elem_mcv) { |
| 6284 | .register => |reg| func.register_manager.lockReg(reg), |
| 6285 | .immediate => |imm| lock: { |
| 6286 | if (imm == 0) continue; |
| 6287 | break :lock null; |
| 6288 | }, |
| 6289 | else => null, |
| 6290 | }; |
| 6291 | defer if (elem_lock) |lock| func.register_manager.unlockReg(lock); |
| 6292 | |
| 6293 | return func.fail("TODO: airAggregateInit packed structs", .{}); |
| 6294 | } |
| 6295 | } else for (elements, 0..) |elem, elem_i| { |
| 5036 | 6296 | if ((try result_ty.structFieldValueComptime(zcu, elem_i)) != null) continue; |
| 5037 | 6297 | |
| 5038 | 6298 | const elem_ty = result_ty.structFieldType(elem_i, zcu); |
| 5039 | 6299 | const elem_off: i32 = @intCast(result_ty.structFieldOffset(elem_i, zcu)); |
| 5040 | | const elem_mcv = try self.resolveInst(elem); |
| 5041 | | |
| 5042 | | const elem_frame: FrameAddr = .{ |
| 5043 | | .index = frame_index, |
| 5044 | | .off = elem_off, |
| 5045 | | }; |
| 5046 | | try self.genSetStack( |
| 6300 | const elem_mcv = try func.resolveInst(elem); |
| 6301 | try func.genSetMem(.{ .frame = frame_index }, elem_off, elem_ty, elem_mcv); |
| 6302 | } |
| 6303 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 6304 | }, |
| 6305 | .Array => { |
| 6306 | const elem_ty = result_ty.childType(zcu); |
| 6307 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu)); |
| 6308 | const elem_size: u32 = @intCast(elem_ty.abiSize(zcu)); |
| 6309 | |
| 6310 | for (elements, 0..) |elem, elem_i| { |
| 6311 | const elem_mcv = try func.resolveInst(elem); |
| 6312 | const elem_off: i32 = @intCast(elem_size * elem_i); |
| 6313 | try func.genSetMem( |
| 6314 | .{ .frame = frame_index }, |
| 6315 | elem_off, |
| 5047 | 6316 | elem_ty, |
| 5048 | | elem_frame, |
| 5049 | 6317 | elem_mcv, |
| 5050 | 6318 | ); |
| 5051 | 6319 | } |
| 6320 | if (result_ty.sentinel(zcu)) |sentinel| try func.genSetMem( |
| 6321 | .{ .frame = frame_index }, |
| 6322 | @intCast(elem_size * elements.len), |
| 6323 | elem_ty, |
| 6324 | try func.genTypedValue(sentinel), |
| 6325 | ); |
| 6326 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 5052 | 6327 | }, |
| 5053 | | else => return self.fail("TODO: airAggregateInit {}", .{result_ty.fmt(zcu)}), |
| 6328 | else => return func.fail("TODO: airAggregate {}", .{result_ty.fmt(zcu)}), |
| 5054 | 6329 | } |
| 5055 | | break :result .{ .register = .zero }; |
| 5056 | 6330 | }; |
| 5057 | 6331 | |
| 5058 | 6332 | if (elements.len <= Liveness.bpi - 1) { |
| 5059 | 6333 | var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); |
| 5060 | 6334 | @memcpy(buf[0..elements.len], elements); |
| 5061 | | return self.finishAir(inst, result, buf); |
| 6335 | return func.finishAir(inst, result, buf); |
| 5062 | 6336 | } |
| 5063 | | var bt = self.liveness.iterateBigTomb(inst); |
| 5064 | | for (elements) |elem| try self.feed(&bt, elem); |
| 5065 | | return self.finishAirResult(inst, result); |
| 6337 | var bt = func.liveness.iterateBigTomb(inst); |
| 6338 | for (elements) |elem| try func.feed(&bt, elem); |
| 6339 | return func.finishAirResult(inst, result); |
| 5066 | 6340 | } |
| 5067 | 6341 | |
| 5068 | | fn airUnionInit(self: *Self, inst: Air.Inst.Index) !void { |
| 5069 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5070 | | const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 6342 | fn airUnionInit(func: *Func, inst: Air.Inst.Index) !void { |
| 6343 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6344 | const extra = func.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 5071 | 6345 | _ = extra; |
| 5072 | | return self.fail("TODO implement airUnionInit for riscv64", .{}); |
| 5073 | | // return self.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value }); |
| 6346 | return func.fail("TODO implement airUnionInit for riscv64", .{}); |
| 6347 | // return func.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value }); |
| 5074 | 6348 | } |
| 5075 | 6349 | |
| 5076 | | fn airPrefetch(self: *Self, inst: Air.Inst.Index) !void { |
| 5077 | | const prefetch = self.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; |
| 6350 | fn airPrefetch(func: *Func, inst: Air.Inst.Index) !void { |
| 6351 | const prefetch = func.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; |
| 5078 | 6352 | // TODO: RISC-V does have prefetch instruction variants. |
| 5079 | 6353 | // see here: https://raw.githubusercontent.com/riscv/riscv-CMOs/master/specifications/cmobase-v1.0.1.pdf |
| 5080 | | return self.finishAir(inst, .unreach, .{ prefetch.ptr, .none, .none }); |
| 6354 | return func.finishAir(inst, .unreach, .{ prefetch.ptr, .none, .none }); |
| 5081 | 6355 | } |
| 5082 | 6356 | |
| 5083 | | fn airMulAdd(self: *Self, inst: Air.Inst.Index) !void { |
| 5084 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 5085 | | const extra = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 5086 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else { |
| 5087 | | return self.fail("TODO implement airMulAdd for riscv64", .{}); |
| 6357 | fn airMulAdd(func: *Func, inst: Air.Inst.Index) !void { |
| 6358 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6359 | const extra = func.air.extraData(Air.Bin, pl_op.payload).data; |
| 6360 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else { |
| 6361 | return func.fail("TODO implement airMulAdd for riscv64", .{}); |
| 5088 | 6362 | }; |
| 5089 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, pl_op.operand }); |
| 6363 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, pl_op.operand }); |
| 5090 | 6364 | } |
| 5091 | 6365 | |
| 5092 | | fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue { |
| 5093 | | const zcu = self.bin_file.comp.module.?; |
| 6366 | fn resolveInst(func: *Func, ref: Air.Inst.Ref) InnerError!MCValue { |
| 6367 | const zcu = func.bin_file.comp.module.?; |
| 5094 | 6368 | |
| 5095 | 6369 | // If the type has no codegen bits, no need to store it. |
| 5096 | | const inst_ty = self.typeOf(ref); |
| 6370 | const inst_ty = func.typeOf(ref); |
| 5097 | 6371 | if (!inst_ty.hasRuntimeBits(zcu)) |
| 5098 | 6372 | return .none; |
| 5099 | 6373 | |
| 5100 | 6374 | const mcv = if (ref.toIndex()) |inst| mcv: { |
| 5101 | | break :mcv self.inst_tracking.getPtr(inst).?.short; |
| 6375 | break :mcv func.inst_tracking.getPtr(inst).?.short; |
| 5102 | 6376 | } else mcv: { |
| 5103 | 6377 | const ip_index = ref.toInterned().?; |
| 5104 | | const gop = try self.const_tracking.getOrPut(self.gpa, ip_index); |
| 6378 | const gop = try func.const_tracking.getOrPut(func.gpa, ip_index); |
| 5105 | 6379 | if (!gop.found_existing) gop.value_ptr.* = InstTracking.init( |
| 5106 | | try self.genTypedValue(Value.fromInterned(ip_index)), |
| 6380 | try func.genTypedValue(Value.fromInterned(ip_index)), |
| 5107 | 6381 | ); |
| 5108 | 6382 | break :mcv gop.value_ptr.short; |
| 5109 | 6383 | }; |
| ... | ... | @@ -5111,21 +6385,21 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue { |
| 5111 | 6385 | return mcv; |
| 5112 | 6386 | } |
| 5113 | 6387 | |
| 5114 | | fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) *InstTracking { |
| 5115 | | const tracking = self.inst_tracking.getPtr(inst).?; |
| 6388 | fn getResolvedInstValue(func: *Func, inst: Air.Inst.Index) *InstTracking { |
| 6389 | const tracking = func.inst_tracking.getPtr(inst).?; |
| 5116 | 6390 | return switch (tracking.short) { |
| 5117 | 6391 | .none, .unreach, .dead => unreachable, |
| 5118 | 6392 | else => tracking, |
| 5119 | 6393 | }; |
| 5120 | 6394 | } |
| 5121 | 6395 | |
| 5122 | | fn genTypedValue(self: *Self, val: Value) InnerError!MCValue { |
| 5123 | | const zcu = self.bin_file.comp.module.?; |
| 6396 | fn genTypedValue(func: *Func, val: Value) InnerError!MCValue { |
| 6397 | const zcu = func.bin_file.comp.module.?; |
| 5124 | 6398 | const result = try codegen.genTypedValue( |
| 5125 | | self.bin_file, |
| 5126 | | self.src_loc, |
| 6399 | func.bin_file, |
| 6400 | func.src_loc, |
| 5127 | 6401 | val, |
| 5128 | | zcu.funcOwnerDeclIndex(self.func_index), |
| 6402 | zcu.funcOwnerDeclIndex(func.func_index), |
| 5129 | 6403 | ); |
| 5130 | 6404 | const mcv: MCValue = switch (result) { |
| 5131 | 6405 | .mcv => |mcv| switch (mcv) { |
| ... | ... | @@ -5135,11 +6409,11 @@ fn genTypedValue(self: *Self, val: Value) InnerError!MCValue { |
| 5135 | 6409 | .immediate => |imm| .{ .immediate = imm }, |
| 5136 | 6410 | .memory => |addr| .{ .memory = addr }, |
| 5137 | 6411 | .load_got, .load_direct, .load_tlv => { |
| 5138 | | return self.fail("TODO: genTypedValue {s}", .{@tagName(mcv)}); |
| 6412 | return func.fail("TODO: genTypedValue {s}", .{@tagName(mcv)}); |
| 5139 | 6413 | }, |
| 5140 | 6414 | }, |
| 5141 | 6415 | .fail => |msg| { |
| 5142 | | self.err_msg = msg; |
| 6416 | func.err_msg = msg; |
| 5143 | 6417 | return error.CodegenFail; |
| 5144 | 6418 | }, |
| 5145 | 6419 | }; |
| ... | ... | @@ -5152,36 +6426,39 @@ const CallMCValues = struct { |
| 5152 | 6426 | stack_byte_count: u31, |
| 5153 | 6427 | stack_align: Alignment, |
| 5154 | 6428 | |
| 5155 | | fn deinit(self: *CallMCValues, func: *Self) void { |
| 5156 | | func.gpa.free(self.args); |
| 5157 | | self.* = undefined; |
| 6429 | fn deinit(call: *CallMCValues, func: *Func) void { |
| 6430 | func.gpa.free(call.args); |
| 6431 | call.* = undefined; |
| 5158 | 6432 | } |
| 5159 | 6433 | }; |
| 5160 | 6434 | |
| 5161 | 6435 | /// Caller must call `CallMCValues.deinit`. |
| 5162 | 6436 | fn resolveCallingConventionValues( |
| 5163 | | self: *Self, |
| 6437 | func: *Func, |
| 5164 | 6438 | fn_info: InternPool.Key.FuncType, |
| 6439 | var_args: []const Type, |
| 5165 | 6440 | ) !CallMCValues { |
| 5166 | | const zcu = self.bin_file.comp.module.?; |
| 6441 | const zcu = func.bin_file.comp.module.?; |
| 5167 | 6442 | const ip = &zcu.intern_pool; |
| 5168 | 6443 | |
| 5169 | | const param_types = try self.gpa.alloc(Type, fn_info.param_types.len); |
| 5170 | | defer self.gpa.free(param_types); |
| 6444 | const param_types = try func.gpa.alloc(Type, fn_info.param_types.len + var_args.len); |
| 6445 | defer func.gpa.free(param_types); |
| 5171 | 6446 | |
| 5172 | 6447 | for (param_types[0..fn_info.param_types.len], fn_info.param_types.get(ip)) |*dest, src| { |
| 5173 | 6448 | dest.* = Type.fromInterned(src); |
| 5174 | 6449 | } |
| 6450 | for (param_types[fn_info.param_types.len..], var_args) |*param_ty, arg_ty| |
| 6451 | param_ty.* = func.promoteVarArg(arg_ty); |
| 5175 | 6452 | |
| 5176 | 6453 | const cc = fn_info.cc; |
| 5177 | 6454 | var result: CallMCValues = .{ |
| 5178 | | .args = try self.gpa.alloc(MCValue, param_types.len), |
| 6455 | .args = try func.gpa.alloc(MCValue, param_types.len), |
| 5179 | 6456 | // These undefined values must be populated before returning from this function. |
| 5180 | 6457 | .return_value = undefined, |
| 5181 | 6458 | .stack_byte_count = 0, |
| 5182 | 6459 | .stack_align = undefined, |
| 5183 | 6460 | }; |
| 5184 | | errdefer self.gpa.free(result.args); |
| 6461 | errdefer func.gpa.free(result.args); |
| 5185 | 6462 | |
| 5186 | 6463 | const ret_ty = Type.fromInterned(fn_info.return_type); |
| 5187 | 6464 | |
| ... | ... | @@ -5193,7 +6470,7 @@ fn resolveCallingConventionValues( |
| 5193 | 6470 | }, |
| 5194 | 6471 | .C, .Unspecified => { |
| 5195 | 6472 | if (result.args.len > 8) { |
| 5196 | | return self.fail("RISC-V calling convention does not support more than 8 arguments", .{}); |
| 6473 | return func.fail("RISC-V calling convention does not support more than 8 arguments", .{}); |
| 5197 | 6474 | } |
| 5198 | 6475 | |
| 5199 | 6476 | var ret_int_reg_i: u32 = 0; |
| ... | ... | @@ -5209,21 +6486,29 @@ fn resolveCallingConventionValues( |
| 5209 | 6486 | } else { |
| 5210 | 6487 | var ret_tracking: [2]InstTracking = undefined; |
| 5211 | 6488 | var ret_tracking_i: usize = 0; |
| 6489 | var ret_float_reg_i: usize = 0; |
| 5212 | 6490 | |
| 5213 | 6491 | const classes = mem.sliceTo(&abi.classifySystem(ret_ty, zcu), .none); |
| 5214 | 6492 | |
| 5215 | 6493 | for (classes) |class| switch (class) { |
| 5216 | 6494 | .integer => { |
| 5217 | | const ret_int_reg = abi.function_ret_regs[ret_int_reg_i]; |
| 6495 | const ret_int_reg = abi.Registers.Integer.function_ret_regs[ret_int_reg_i]; |
| 5218 | 6496 | ret_int_reg_i += 1; |
| 5219 | 6497 | |
| 5220 | 6498 | ret_tracking[ret_tracking_i] = InstTracking.init(.{ .register = ret_int_reg }); |
| 5221 | 6499 | ret_tracking_i += 1; |
| 5222 | 6500 | }, |
| 6501 | .float => { |
| 6502 | const ret_float_reg = abi.Registers.Float.function_ret_regs[ret_float_reg_i]; |
| 6503 | ret_float_reg_i += 1; |
| 6504 | |
| 6505 | ret_tracking[ret_tracking_i] = InstTracking.init(.{ .register = ret_float_reg }); |
| 6506 | ret_tracking_i += 1; |
| 6507 | }, |
| 5223 | 6508 | .memory => { |
| 5224 | | const ret_int_reg = abi.function_ret_regs[ret_int_reg_i]; |
| 6509 | const ret_int_reg = abi.Registers.Integer.function_ret_regs[ret_int_reg_i]; |
| 5225 | 6510 | ret_int_reg_i += 1; |
| 5226 | | const ret_indirect_reg = abi.function_arg_regs[param_int_reg_i]; |
| 6511 | const ret_indirect_reg = abi.Registers.Integer.function_arg_regs[param_int_reg_i]; |
| 5227 | 6512 | param_int_reg_i += 1; |
| 5228 | 6513 | |
| 5229 | 6514 | ret_tracking[ret_tracking_i] = .{ |
| ... | ... | @@ -5232,11 +6517,11 @@ fn resolveCallingConventionValues( |
| 5232 | 6517 | }; |
| 5233 | 6518 | ret_tracking_i += 1; |
| 5234 | 6519 | }, |
| 5235 | | else => return self.fail("TODO: C calling convention return class {}", .{class}), |
| 6520 | else => return func.fail("TODO: C calling convention return class {}", .{class}), |
| 5236 | 6521 | }; |
| 5237 | 6522 | |
| 5238 | 6523 | result.return_value = switch (ret_tracking_i) { |
| 5239 | | else => return self.fail("ty {} took {} tracking return indices", .{ ret_ty.fmt(zcu), ret_tracking_i }), |
| 6524 | else => return func.fail("ty {} took {} tracking return indices", .{ ret_ty.fmt(zcu), ret_tracking_i }), |
| 5240 | 6525 | 1 => ret_tracking[0], |
| 5241 | 6526 | 2 => InstTracking.init(.{ .register_pair = .{ |
| 5242 | 6527 | ret_tracking[0].short.register, ret_tracking[1].short.register, |
| ... | ... | @@ -5244,8 +6529,14 @@ fn resolveCallingConventionValues( |
| 5244 | 6529 | }; |
| 5245 | 6530 | } |
| 5246 | 6531 | |
| 6532 | var param_float_reg_i: usize = 0; |
| 6533 | |
| 5247 | 6534 | for (param_types, result.args) |ty, *arg| { |
| 5248 | | assert(ty.hasRuntimeBitsIgnoreComptime(zcu)); |
| 6535 | if (!ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 6536 | assert(cc == .Unspecified); |
| 6537 | arg.* = .none; |
| 6538 | continue; |
| 6539 | } |
| 5249 | 6540 | |
| 5250 | 6541 | var arg_mcv: [2]MCValue = undefined; |
| 5251 | 6542 | var arg_mcv_i: usize = 0; |
| ... | ... | @@ -5254,7 +6545,7 @@ fn resolveCallingConventionValues( |
| 5254 | 6545 | |
| 5255 | 6546 | for (classes) |class| switch (class) { |
| 5256 | 6547 | .integer => { |
| 5257 | | const param_int_regs = abi.function_arg_regs; |
| 6548 | const param_int_regs = abi.Registers.Integer.function_arg_regs; |
| 5258 | 6549 | if (param_int_reg_i >= param_int_regs.len) break; |
| 5259 | 6550 | |
| 5260 | 6551 | const param_int_reg = param_int_regs[param_int_reg_i]; |
| ... | ... | @@ -5263,36 +6554,47 @@ fn resolveCallingConventionValues( |
| 5263 | 6554 | arg_mcv[arg_mcv_i] = .{ .register = param_int_reg }; |
| 5264 | 6555 | arg_mcv_i += 1; |
| 5265 | 6556 | }, |
| 6557 | .float => { |
| 6558 | const param_float_regs = abi.Registers.Float.function_arg_regs; |
| 6559 | if (param_float_reg_i >= param_float_regs.len) break; |
| 6560 | |
| 6561 | const param_float_reg = param_float_regs[param_float_reg_i]; |
| 6562 | param_float_reg_i += 1; |
| 6563 | |
| 6564 | arg_mcv[arg_mcv_i] = .{ .register = param_float_reg }; |
| 6565 | arg_mcv_i += 1; |
| 6566 | }, |
| 5266 | 6567 | .memory => { |
| 5267 | | const param_int_regs = abi.function_arg_regs; |
| 6568 | const param_int_regs = abi.Registers.Integer.function_arg_regs; |
| 6569 | |
| 5268 | 6570 | const param_int_reg = param_int_regs[param_int_reg_i]; |
| 6571 | param_int_reg_i += 1; |
| 5269 | 6572 | |
| 5270 | 6573 | arg_mcv[arg_mcv_i] = .{ .indirect = .{ .reg = param_int_reg } }; |
| 5271 | 6574 | arg_mcv_i += 1; |
| 5272 | 6575 | }, |
| 5273 | | else => return self.fail("TODO: C calling convention arg class {}", .{class}), |
| 6576 | else => return func.fail("TODO: C calling convention arg class {}", .{class}), |
| 5274 | 6577 | } else { |
| 5275 | 6578 | arg.* = switch (arg_mcv_i) { |
| 5276 | | else => return self.fail("ty {} took {} tracking arg indices", .{ ty.fmt(zcu), arg_mcv_i }), |
| 6579 | else => return func.fail("ty {} took {} tracking arg indices", .{ ty.fmt(zcu), arg_mcv_i }), |
| 5277 | 6580 | 1 => arg_mcv[0], |
| 5278 | 6581 | 2 => .{ .register_pair = .{ arg_mcv[0].register, arg_mcv[1].register } }, |
| 5279 | 6582 | }; |
| 5280 | 6583 | continue; |
| 5281 | 6584 | } |
| 5282 | 6585 | |
| 5283 | | return self.fail("TODO: pass args by stack", .{}); |
| 6586 | return func.fail("TODO: pass args by stack", .{}); |
| 5284 | 6587 | } |
| 5285 | 6588 | }, |
| 5286 | | else => return self.fail("TODO implement function parameters for {} on riscv64", .{cc}), |
| 6589 | else => return func.fail("TODO implement function parameters for {} on riscv64", .{cc}), |
| 5287 | 6590 | } |
| 5288 | 6591 | |
| 5289 | 6592 | result.stack_byte_count = @intCast(result.stack_align.forward(result.stack_byte_count)); |
| 5290 | 6593 | return result; |
| 5291 | 6594 | } |
| 5292 | 6595 | |
| 5293 | | /// TODO support scope overrides. Also note this logic is duplicated with `Module.wantSafety`. |
| 5294 | | fn wantSafety(self: *Self) bool { |
| 5295 | | return switch (self.bin_file.comp.root_mod.optimize_mode) { |
| 6596 | fn wantSafety(func: *Func) bool { |
| 6597 | return switch (func.mod.optimize_mode) { |
| 5296 | 6598 | .Debug => true, |
| 5297 | 6599 | .ReleaseSafe => true, |
| 5298 | 6600 | .ReleaseFast => false, |
| ... | ... | @@ -5300,39 +6602,36 @@ fn wantSafety(self: *Self) bool { |
| 5300 | 6602 | }; |
| 5301 | 6603 | } |
| 5302 | 6604 | |
| 5303 | | fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { |
| 6605 | fn fail(func: *Func, comptime format: []const u8, args: anytype) InnerError { |
| 5304 | 6606 | @setCold(true); |
| 5305 | | assert(self.err_msg == null); |
| 5306 | | self.err_msg = try ErrorMsg.create(self.gpa, self.src_loc, format, args); |
| 6607 | assert(func.err_msg == null); |
| 6608 | func.err_msg = try ErrorMsg.create(func.gpa, func.src_loc, format, args); |
| 5307 | 6609 | return error.CodegenFail; |
| 5308 | 6610 | } |
| 5309 | 6611 | |
| 5310 | | fn failSymbol(self: *Self, comptime format: []const u8, args: anytype) InnerError { |
| 6612 | fn failSymbol(func: *Func, comptime format: []const u8, args: anytype) InnerError { |
| 5311 | 6613 | @setCold(true); |
| 5312 | | assert(self.err_msg == null); |
| 5313 | | self.err_msg = try ErrorMsg.create(self.gpa, self.src_loc, format, args); |
| 6614 | assert(func.err_msg == null); |
| 6615 | func.err_msg = try ErrorMsg.create(func.gpa, func.src_loc, format, args); |
| 5314 | 6616 | return error.CodegenFail; |
| 5315 | 6617 | } |
| 5316 | 6618 | |
| 5317 | 6619 | fn parseRegName(name: []const u8) ?Register { |
| 5318 | | if (@hasDecl(Register, "parseRegName")) { |
| 5319 | | return Register.parseRegName(name); |
| 5320 | | } |
| 5321 | 6620 | return std.meta.stringToEnum(Register, name); |
| 5322 | 6621 | } |
| 5323 | 6622 | |
| 5324 | | fn typeOf(self: *Self, inst: Air.Inst.Ref) Type { |
| 5325 | | const zcu = self.bin_file.comp.module.?; |
| 5326 | | return self.air.typeOf(inst, &zcu.intern_pool); |
| 6623 | fn typeOf(func: *Func, inst: Air.Inst.Ref) Type { |
| 6624 | const zcu = func.bin_file.comp.module.?; |
| 6625 | return func.air.typeOf(inst, &zcu.intern_pool); |
| 5327 | 6626 | } |
| 5328 | 6627 | |
| 5329 | | fn typeOfIndex(self: *Self, inst: Air.Inst.Index) Type { |
| 5330 | | const zcu = self.bin_file.comp.module.?; |
| 5331 | | return self.air.typeOfIndex(inst, &zcu.intern_pool); |
| 6628 | fn typeOfIndex(func: *Func, inst: Air.Inst.Index) Type { |
| 6629 | const zcu = func.bin_file.comp.module.?; |
| 6630 | return func.air.typeOfIndex(inst, &zcu.intern_pool); |
| 5332 | 6631 | } |
| 5333 | 6632 | |
| 5334 | | fn hasFeature(self: *Self, feature: Target.riscv.Feature) bool { |
| 5335 | | return Target.riscv.featureSetHas(self.target.cpu.features, feature); |
| 6633 | fn hasFeature(func: *Func, feature: Target.riscv.Feature) bool { |
| 6634 | return Target.riscv.featureSetHas(func.target.cpu.features, feature); |
| 5336 | 6635 | } |
| 5337 | 6636 | |
| 5338 | 6637 | pub fn errUnionPayloadOffset(payload_ty: Type, zcu: *Module) u64 { |
| ... | ... | @@ -5356,3 +6655,33 @@ pub fn errUnionErrorOffset(payload_ty: Type, zcu: *Module) u64 { |
| 5356 | 6655 | return 0; |
| 5357 | 6656 | } |
| 5358 | 6657 | } |
| 6658 | |
| 6659 | fn promoteInt(func: *Func, ty: Type) Type { |
| 6660 | const mod = func.bin_file.comp.module.?; |
| 6661 | const int_info: InternPool.Key.IntType = switch (ty.toIntern()) { |
| 6662 | .bool_type => .{ .signedness = .unsigned, .bits = 1 }, |
| 6663 | else => if (ty.isAbiInt(mod)) ty.intInfo(mod) else return ty, |
| 6664 | }; |
| 6665 | for ([_]Type{ |
| 6666 | Type.c_int, Type.c_uint, |
| 6667 | Type.c_long, Type.c_ulong, |
| 6668 | Type.c_longlong, Type.c_ulonglong, |
| 6669 | }) |promote_ty| { |
| 6670 | const promote_info = promote_ty.intInfo(mod); |
| 6671 | if (int_info.signedness == .signed and promote_info.signedness == .unsigned) continue; |
| 6672 | if (int_info.bits + @intFromBool(int_info.signedness == .unsigned and |
| 6673 | promote_info.signedness == .signed) <= promote_info.bits) return promote_ty; |
| 6674 | } |
| 6675 | return ty; |
| 6676 | } |
| 6677 | |
| 6678 | fn promoteVarArg(func: *Func, ty: Type) Type { |
| 6679 | if (!ty.isRuntimeFloat()) return func.promoteInt(ty); |
| 6680 | switch (ty.floatBits(func.target.*)) { |
| 6681 | 32, 64 => return Type.f64, |
| 6682 | else => |float_bits| { |
| 6683 | assert(float_bits == func.target.c_type_bit_size(.longdouble)); |
| 6684 | return Type.c_longdouble; |
| 6685 | }, |
| 6686 | } |
| 6687 | } |