| ... | ... | @@ -281,9 +281,9 @@ const MCValue = union(enum) { |
| 281 | 281 | const Branch = struct { |
| 282 | 282 | inst_table: std.AutoArrayHashMapUnmanaged(Air.Inst.Index, MCValue) = .{}, |
| 283 | 283 | |
| 284 | | fn deinit(self: *Branch, gpa: Allocator) void { |
| 285 | | self.inst_table.deinit(gpa); |
| 286 | | self.* = undefined; |
| 284 | fn deinit(func: *Branch, gpa: Allocator) void { |
| 285 | func.inst_table.deinit(gpa); |
| 286 | func.* = undefined; |
| 287 | 287 | } |
| 288 | 288 | }; |
| 289 | 289 | |
| ... | ... | @@ -317,33 +317,33 @@ const InstTracking = struct { |
| 317 | 317 | }, .short = result }; |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | | fn getReg(self: InstTracking) ?Register { |
| 321 | | return self.short.getReg(); |
| 320 | fn getReg(func: InstTracking) ?Register { |
| 321 | return func.short.getReg(); |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | | fn getRegs(self: *const InstTracking) []const Register { |
| 325 | | return self.short.getRegs(); |
| 324 | fn getRegs(func: *const InstTracking) []const Register { |
| 325 | return func.short.getRegs(); |
| 326 | 326 | } |
| 327 | 327 | |
| 328 | | fn spill(self: *InstTracking, function: *Self, inst: Air.Inst.Index) !void { |
| 329 | | if (std.meta.eql(self.long, self.short)) return; // Already spilled |
| 328 | fn spill(func: *InstTracking, function: *Func, inst: Air.Inst.Index) !void { |
| 329 | if (std.meta.eql(func.long, func.short)) return; // Already spilled |
| 330 | 330 | // Allocate or reuse frame index |
| 331 | | switch (self.long) { |
| 332 | | .none => self.long = try function.allocRegOrMem(inst, false), |
| 331 | switch (func.long) { |
| 332 | .none => func.long = try function.allocRegOrMem(inst, false), |
| 333 | 333 | .load_frame => {}, |
| 334 | | .reserved_frame => |index| self.long = .{ .load_frame = .{ .index = index } }, |
| 334 | .reserved_frame => |index| func.long = .{ .load_frame = .{ .index = index } }, |
| 335 | 335 | else => unreachable, |
| 336 | 336 | } |
| 337 | | tracking_log.debug("spill %{d} from {} to {}", .{ inst, self.short, self.long }); |
| 338 | | try function.genCopy(function.typeOfIndex(inst), self.long, self.short); |
| 337 | tracking_log.debug("spill %{d} from {} to {}", .{ inst, func.short, func.long }); |
| 338 | try function.genCopy(function.typeOfIndex(inst), func.long, func.short); |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | | fn reuseFrame(self: *InstTracking) void { |
| 342 | | switch (self.long) { |
| 343 | | .reserved_frame => |index| self.long = .{ .load_frame = .{ .index = index } }, |
| 341 | fn reuseFrame(func: *InstTracking) void { |
| 342 | switch (func.long) { |
| 343 | .reserved_frame => |index| func.long = .{ .load_frame = .{ .index = index } }, |
| 344 | 344 | else => {}, |
| 345 | 345 | } |
| 346 | | self.short = switch (self.long) { |
| 346 | func.short = switch (func.long) { |
| 347 | 347 | .none, |
| 348 | 348 | .unreach, |
| 349 | 349 | .undef, |
| ... | ... | @@ -353,7 +353,7 @@ const InstTracking = struct { |
| 353 | 353 | .lea_frame, |
| 354 | 354 | .load_symbol, |
| 355 | 355 | .lea_symbol, |
| 356 | | => self.long, |
| 356 | => func.long, |
| 357 | 357 | .dead, |
| 358 | 358 | .register, |
| 359 | 359 | .register_pair, |
| ... | ... | @@ -365,14 +365,14 @@ const InstTracking = struct { |
| 365 | 365 | }; |
| 366 | 366 | } |
| 367 | 367 | |
| 368 | | fn trackSpill(self: *InstTracking, function: *Self, inst: Air.Inst.Index) !void { |
| 369 | | try function.freeValue(self.short); |
| 370 | | self.reuseFrame(); |
| 371 | | tracking_log.debug("%{d} => {} (spilled)", .{ inst, self.* }); |
| 368 | fn trackSpill(func: *InstTracking, function: *Func, inst: Air.Inst.Index) !void { |
| 369 | try function.freeValue(func.short); |
| 370 | func.reuseFrame(); |
| 371 | tracking_log.debug("%{d} => {} (spilled)", .{ inst, func.* }); |
| 372 | 372 | } |
| 373 | 373 | |
| 374 | | fn verifyMaterialize(self: InstTracking, target: InstTracking) void { |
| 375 | | switch (self.long) { |
| 374 | fn verifyMaterialize(func: InstTracking, target: InstTracking) void { |
| 375 | switch (func.long) { |
| 376 | 376 | .none, |
| 377 | 377 | .unreach, |
| 378 | 378 | .undef, |
| ... | ... | @@ -381,7 +381,7 @@ const InstTracking = struct { |
| 381 | 381 | .lea_frame, |
| 382 | 382 | .load_symbol, |
| 383 | 383 | .lea_symbol, |
| 384 | | => assert(std.meta.eql(self.long, target.long)), |
| 384 | => assert(std.meta.eql(func.long, target.long)), |
| 385 | 385 | .load_frame, |
| 386 | 386 | .reserved_frame, |
| 387 | 387 | => switch (target.long) { |
| ... | ... | @@ -402,73 +402,73 @@ const InstTracking = struct { |
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | fn materialize( |
| 405 | | self: *InstTracking, |
| 406 | | function: *Self, |
| 405 | func: *InstTracking, |
| 406 | function: *Func, |
| 407 | 407 | inst: Air.Inst.Index, |
| 408 | 408 | target: InstTracking, |
| 409 | 409 | ) !void { |
| 410 | | self.verifyMaterialize(target); |
| 411 | | try self.materializeUnsafe(function, inst, target); |
| 410 | func.verifyMaterialize(target); |
| 411 | try func.materializeUnsafe(function, inst, target); |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | fn materializeUnsafe( |
| 415 | | self: InstTracking, |
| 416 | | function: *Self, |
| 415 | func: InstTracking, |
| 416 | function: *Func, |
| 417 | 417 | inst: Air.Inst.Index, |
| 418 | 418 | target: InstTracking, |
| 419 | 419 | ) !void { |
| 420 | 420 | const ty = function.typeOfIndex(inst); |
| 421 | | if ((self.long == .none or self.long == .reserved_frame) and target.long == .load_frame) |
| 422 | | try function.genCopy(ty, target.long, self.short); |
| 423 | | try function.genCopy(ty, target.short, self.short); |
| 421 | if ((func.long == .none or func.long == .reserved_frame) and target.long == .load_frame) |
| 422 | try function.genCopy(ty, target.long, func.short); |
| 423 | try function.genCopy(ty, target.short, func.short); |
| 424 | 424 | } |
| 425 | 425 | |
| 426 | | fn trackMaterialize(self: *InstTracking, inst: Air.Inst.Index, target: InstTracking) void { |
| 427 | | self.verifyMaterialize(target); |
| 426 | fn trackMaterialize(func: *InstTracking, inst: Air.Inst.Index, target: InstTracking) void { |
| 427 | func.verifyMaterialize(target); |
| 428 | 428 | // Don't clobber reserved frame indices |
| 429 | | self.long = if (target.long == .none) switch (self.long) { |
| 429 | func.long = if (target.long == .none) switch (func.long) { |
| 430 | 430 | .load_frame => |addr| .{ .reserved_frame = addr.index }, |
| 431 | | .reserved_frame => self.long, |
| 431 | .reserved_frame => func.long, |
| 432 | 432 | else => target.long, |
| 433 | 433 | } else target.long; |
| 434 | | self.short = target.short; |
| 435 | | tracking_log.debug("%{d} => {} (materialize)", .{ inst, self.* }); |
| 434 | func.short = target.short; |
| 435 | tracking_log.debug("%{d} => {} (materialize)", .{ inst, func.* }); |
| 436 | 436 | } |
| 437 | 437 | |
| 438 | | fn resurrect(self: *InstTracking, inst: Air.Inst.Index, scope_generation: u32) void { |
| 439 | | switch (self.short) { |
| 438 | fn resurrect(func: *InstTracking, inst: Air.Inst.Index, scope_generation: u32) void { |
| 439 | switch (func.short) { |
| 440 | 440 | .dead => |die_generation| if (die_generation >= scope_generation) { |
| 441 | | self.reuseFrame(); |
| 442 | | tracking_log.debug("%{d} => {} (resurrect)", .{ inst, self.* }); |
| 441 | func.reuseFrame(); |
| 442 | tracking_log.debug("%{d} => {} (resurrect)", .{ inst, func.* }); |
| 443 | 443 | }, |
| 444 | 444 | else => {}, |
| 445 | 445 | } |
| 446 | 446 | } |
| 447 | 447 | |
| 448 | | fn die(self: *InstTracking, function: *Self, inst: Air.Inst.Index) !void { |
| 449 | | if (self.short == .dead) return; |
| 450 | | try function.freeValue(self.short); |
| 451 | | self.short = .{ .dead = function.scope_generation }; |
| 452 | | tracking_log.debug("%{d} => {} (death)", .{ inst, self.* }); |
| 448 | fn die(func: *InstTracking, function: *Func, inst: Air.Inst.Index) !void { |
| 449 | if (func.short == .dead) return; |
| 450 | try function.freeValue(func.short); |
| 451 | func.short = .{ .dead = function.scope_generation }; |
| 452 | tracking_log.debug("%{d} => {} (death)", .{ inst, func.* }); |
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | fn reuse( |
| 456 | | self: *InstTracking, |
| 457 | | function: *Self, |
| 456 | func: *InstTracking, |
| 457 | function: *Func, |
| 458 | 458 | new_inst: ?Air.Inst.Index, |
| 459 | 459 | old_inst: Air.Inst.Index, |
| 460 | 460 | ) void { |
| 461 | | self.short = .{ .dead = function.scope_generation }; |
| 461 | func.short = .{ .dead = function.scope_generation }; |
| 462 | 462 | if (new_inst) |inst| |
| 463 | | tracking_log.debug("%{d} => {} (reuse %{d})", .{ inst, self.*, old_inst }) |
| 463 | tracking_log.debug("%{d} => {} (reuse %{d})", .{ inst, func.*, old_inst }) |
| 464 | 464 | else |
| 465 | | tracking_log.debug("tmp => {} (reuse %{d})", .{ self.*, old_inst }); |
| 465 | tracking_log.debug("tmp => {} (reuse %{d})", .{ func.*, old_inst }); |
| 466 | 466 | } |
| 467 | 467 | |
| 468 | | fn liveOut(self: *InstTracking, function: *Self, inst: Air.Inst.Index) void { |
| 469 | | for (self.getRegs()) |reg| { |
| 468 | fn liveOut(func: *InstTracking, function: *Func, inst: Air.Inst.Index) void { |
| 469 | for (func.getRegs()) |reg| { |
| 470 | 470 | if (function.register_manager.isRegFree(reg)) { |
| 471 | | tracking_log.debug("%{d} => {} (live-out)", .{ inst, self.* }); |
| 471 | tracking_log.debug("%{d} => {} (live-out)", .{ inst, func.* }); |
| 472 | 472 | continue; |
| 473 | 473 | } |
| 474 | 474 | |
| ... | ... | @@ -495,18 +495,18 @@ const InstTracking = struct { |
| 495 | 495 | // Perform side-effects of freeValue manually. |
| 496 | 496 | function.register_manager.freeReg(reg); |
| 497 | 497 | |
| 498 | | tracking_log.debug("%{d} => {} (live-out %{d})", .{ inst, self.*, tracked_inst }); |
| 498 | tracking_log.debug("%{d} => {} (live-out %{d})", .{ inst, func.*, tracked_inst }); |
| 499 | 499 | } |
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | pub fn format( |
| 503 | | self: InstTracking, |
| 503 | func: InstTracking, |
| 504 | 504 | comptime _: []const u8, |
| 505 | 505 | _: std.fmt.FormatOptions, |
| 506 | 506 | writer: anytype, |
| 507 | 507 | ) @TypeOf(writer).Error!void { |
| 508 | | if (!std.meta.eql(self.long, self.short)) try writer.print("|{}| ", .{self.long}); |
| 509 | | try writer.print("{}", .{self.short}); |
| 508 | if (!std.meta.eql(func.long, func.short)) try writer.print("|{}| ", .{func.long}); |
| 509 | try writer.print("{}", .{func.short}); |
| 510 | 510 | } |
| 511 | 511 | }; |
| 512 | 512 | |
| ... | ... | @@ -546,19 +546,13 @@ const FrameAlloc = struct { |
| 546 | 546 | } |
| 547 | 547 | }; |
| 548 | 548 | |
| 549 | | const StackAllocation = struct { |
| 550 | | inst: ?Air.Inst.Index, |
| 551 | | /// TODO: make the size inferred from the bits of the inst |
| 552 | | size: u32, |
| 553 | | }; |
| 554 | | |
| 555 | 549 | const BlockData = struct { |
| 556 | 550 | relocs: std.ArrayListUnmanaged(Mir.Inst.Index) = .{}, |
| 557 | 551 | state: State, |
| 558 | 552 | |
| 559 | | fn deinit(self: *BlockData, gpa: Allocator) void { |
| 560 | | self.relocs.deinit(gpa); |
| 561 | | self.* = undefined; |
| 553 | fn deinit(bd: *BlockData, gpa: Allocator) void { |
| 554 | bd.relocs.deinit(gpa); |
| 555 | bd.* = undefined; |
| 562 | 556 | } |
| 563 | 557 | }; |
| 564 | 558 | |
| ... | ... | @@ -570,31 +564,31 @@ const State = struct { |
| 570 | 564 | scope_generation: u32, |
| 571 | 565 | }; |
| 572 | 566 | |
| 573 | | fn initRetroactiveState(self: *Self) State { |
| 567 | fn initRetroactiveState(func: *Func) State { |
| 574 | 568 | var state: State = undefined; |
| 575 | | state.inst_tracking_len = @intCast(self.inst_tracking.count()); |
| 576 | | state.scope_generation = self.scope_generation; |
| 569 | state.inst_tracking_len = @intCast(func.inst_tracking.count()); |
| 570 | state.scope_generation = func.scope_generation; |
| 577 | 571 | return state; |
| 578 | 572 | } |
| 579 | 573 | |
| 580 | | fn saveRetroactiveState(self: *Self, state: *State) !void { |
| 581 | | const free_registers = self.register_manager.free_registers; |
| 574 | fn saveRetroactiveState(func: *Func, state: *State) !void { |
| 575 | const free_registers = func.register_manager.free_registers; |
| 582 | 576 | var it = free_registers.iterator(.{ .kind = .unset }); |
| 583 | 577 | while (it.next()) |index| { |
| 584 | | const tracked_inst = self.register_manager.registers[index]; |
| 578 | const tracked_inst = func.register_manager.registers[index]; |
| 585 | 579 | state.registers[index] = tracked_inst; |
| 586 | | state.reg_tracking[index] = self.inst_tracking.get(tracked_inst).?; |
| 580 | state.reg_tracking[index] = func.inst_tracking.get(tracked_inst).?; |
| 587 | 581 | } |
| 588 | 582 | state.free_registers = free_registers; |
| 589 | 583 | } |
| 590 | 584 | |
| 591 | | fn saveState(self: *Self) !State { |
| 592 | | var state = self.initRetroactiveState(); |
| 593 | | try self.saveRetroactiveState(&state); |
| 585 | fn saveState(func: *Func) !State { |
| 586 | var state = func.initRetroactiveState(); |
| 587 | try func.saveRetroactiveState(&state); |
| 594 | 588 | return state; |
| 595 | 589 | } |
| 596 | 590 | |
| 597 | | fn restoreState(self: *Self, state: State, deaths: []const Air.Inst.Index, comptime opts: struct { |
| 591 | fn restoreState(func: *Func, state: State, deaths: []const Air.Inst.Index, comptime opts: struct { |
| 598 | 592 | emit_instructions: bool, |
| 599 | 593 | update_tracking: bool, |
| 600 | 594 | resurrect: bool, |
| ... | ... | @@ -602,81 +596,81 @@ fn restoreState(self: *Self, state: State, deaths: []const Air.Inst.Index, compt |
| 602 | 596 | }) !void { |
| 603 | 597 | if (opts.close_scope) { |
| 604 | 598 | for ( |
| 605 | | self.inst_tracking.keys()[state.inst_tracking_len..], |
| 606 | | self.inst_tracking.values()[state.inst_tracking_len..], |
| 607 | | ) |inst, *tracking| try tracking.die(self, inst); |
| 608 | | self.inst_tracking.shrinkRetainingCapacity(state.inst_tracking_len); |
| 599 | func.inst_tracking.keys()[state.inst_tracking_len..], |
| 600 | func.inst_tracking.values()[state.inst_tracking_len..], |
| 601 | ) |inst, *tracking| try tracking.die(func, inst); |
| 602 | func.inst_tracking.shrinkRetainingCapacity(state.inst_tracking_len); |
| 609 | 603 | } |
| 610 | 604 | |
| 611 | 605 | if (opts.resurrect) for ( |
| 612 | | self.inst_tracking.keys()[0..state.inst_tracking_len], |
| 613 | | self.inst_tracking.values()[0..state.inst_tracking_len], |
| 606 | func.inst_tracking.keys()[0..state.inst_tracking_len], |
| 607 | func.inst_tracking.values()[0..state.inst_tracking_len], |
| 614 | 608 | ) |inst, *tracking| tracking.resurrect(inst, state.scope_generation); |
| 615 | | for (deaths) |death| try self.processDeath(death); |
| 609 | for (deaths) |death| try func.processDeath(death); |
| 616 | 610 | |
| 617 | 611 | const ExpectedContents = [@typeInfo(RegisterManager.TrackedRegisters).Array.len]RegisterLock; |
| 618 | 612 | var stack align(@max(@alignOf(ExpectedContents), @alignOf(std.heap.StackFallbackAllocator(0)))) = |
| 619 | 613 | if (opts.update_tracking) |
| 620 | | {} else std.heap.stackFallback(@sizeOf(ExpectedContents), self.gpa); |
| 614 | {} else std.heap.stackFallback(@sizeOf(ExpectedContents), func.gpa); |
| 621 | 615 | |
| 622 | 616 | var reg_locks = if (opts.update_tracking) {} else try std.ArrayList(RegisterLock).initCapacity( |
| 623 | 617 | stack.get(), |
| 624 | 618 | @typeInfo(ExpectedContents).Array.len, |
| 625 | 619 | ); |
| 626 | 620 | defer if (!opts.update_tracking) { |
| 627 | | for (reg_locks.items) |lock| self.register_manager.unlockReg(lock); |
| 621 | for (reg_locks.items) |lock| func.register_manager.unlockReg(lock); |
| 628 | 622 | reg_locks.deinit(); |
| 629 | 623 | }; |
| 630 | 624 | |
| 631 | 625 | for (0..state.registers.len) |index| { |
| 632 | | const current_maybe_inst = if (self.register_manager.free_registers.isSet(index)) |
| 626 | const current_maybe_inst = if (func.register_manager.free_registers.isSet(index)) |
| 633 | 627 | null |
| 634 | 628 | else |
| 635 | | self.register_manager.registers[index]; |
| 629 | func.register_manager.registers[index]; |
| 636 | 630 | const target_maybe_inst = if (state.free_registers.isSet(index)) |
| 637 | 631 | null |
| 638 | 632 | else |
| 639 | 633 | state.registers[index]; |
| 640 | 634 | if (std.debug.runtime_safety) if (target_maybe_inst) |target_inst| |
| 641 | | assert(self.inst_tracking.getIndex(target_inst).? < state.inst_tracking_len); |
| 635 | assert(func.inst_tracking.getIndex(target_inst).? < state.inst_tracking_len); |
| 642 | 636 | if (opts.emit_instructions) { |
| 643 | 637 | if (current_maybe_inst) |current_inst| { |
| 644 | | try self.inst_tracking.getPtr(current_inst).?.spill(self, current_inst); |
| 638 | try func.inst_tracking.getPtr(current_inst).?.spill(func, current_inst); |
| 645 | 639 | } |
| 646 | 640 | if (target_maybe_inst) |target_inst| { |
| 647 | | const target_tracking = self.inst_tracking.getPtr(target_inst).?; |
| 648 | | try target_tracking.materialize(self, target_inst, state.reg_tracking[index]); |
| 641 | const target_tracking = func.inst_tracking.getPtr(target_inst).?; |
| 642 | try target_tracking.materialize(func, target_inst, state.reg_tracking[index]); |
| 649 | 643 | } |
| 650 | 644 | } |
| 651 | 645 | if (opts.update_tracking) { |
| 652 | 646 | if (current_maybe_inst) |current_inst| { |
| 653 | | try self.inst_tracking.getPtr(current_inst).?.trackSpill(self, current_inst); |
| 647 | try func.inst_tracking.getPtr(current_inst).?.trackSpill(func, current_inst); |
| 654 | 648 | } |
| 655 | 649 | blk: { |
| 656 | 650 | const inst = target_maybe_inst orelse break :blk; |
| 657 | 651 | const reg = RegisterManager.regAtTrackedIndex(@intCast(index)); |
| 658 | | self.register_manager.freeReg(reg); |
| 659 | | self.register_manager.getRegAssumeFree(reg, inst); |
| 652 | func.register_manager.freeReg(reg); |
| 653 | func.register_manager.getRegAssumeFree(reg, inst); |
| 660 | 654 | } |
| 661 | 655 | if (target_maybe_inst) |target_inst| { |
| 662 | | self.inst_tracking.getPtr(target_inst).?.trackMaterialize( |
| 656 | func.inst_tracking.getPtr(target_inst).?.trackMaterialize( |
| 663 | 657 | target_inst, |
| 664 | 658 | state.reg_tracking[index], |
| 665 | 659 | ); |
| 666 | 660 | } |
| 667 | 661 | } else if (target_maybe_inst) |_| |
| 668 | | try reg_locks.append(self.register_manager.lockRegIndexAssumeUnused(@intCast(index))); |
| 662 | try reg_locks.append(func.register_manager.lockRegIndexAssumeUnused(@intCast(index))); |
| 669 | 663 | } |
| 670 | 664 | |
| 671 | 665 | if (opts.update_tracking and std.debug.runtime_safety) { |
| 672 | | assert(self.register_manager.free_registers.eql(state.free_registers)); |
| 666 | assert(func.register_manager.free_registers.eql(state.free_registers)); |
| 673 | 667 | var used_reg_it = state.free_registers.iterator(.{ .kind = .unset }); |
| 674 | 668 | while (used_reg_it.next()) |index| |
| 675 | | assert(self.register_manager.registers[index] == state.registers[index]); |
| 669 | assert(func.register_manager.registers[index] == state.registers[index]); |
| 676 | 670 | } |
| 677 | 671 | } |
| 678 | 672 | |
| 679 | | const Self = @This(); |
| 673 | const Func = @This(); |
| 680 | 674 | |
| 681 | 675 | const CallView = enum(u1) { |
| 682 | 676 | callee, |
| ... | ... | @@ -712,7 +706,7 @@ pub fn generate( |
| 712 | 706 | } |
| 713 | 707 | try branch_stack.append(.{}); |
| 714 | 708 | |
| 715 | | var function = Self{ |
| 709 | var function = Func{ |
| 716 | 710 | .gpa = gpa, |
| 717 | 711 | .air = air, |
| 718 | 712 | .mod = mod, |
| ... | ... | @@ -852,51 +846,51 @@ pub fn generate( |
| 852 | 846 | } |
| 853 | 847 | } |
| 854 | 848 | |
| 855 | | fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index { |
| 856 | | const gpa = self.gpa; |
| 849 | fn addInst(func: *Func, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index { |
| 850 | const gpa = func.gpa; |
| 857 | 851 | |
| 858 | | try self.mir_instructions.ensureUnusedCapacity(gpa, 1); |
| 852 | try func.mir_instructions.ensureUnusedCapacity(gpa, 1); |
| 859 | 853 | |
| 860 | | const result_index: Mir.Inst.Index = @intCast(self.mir_instructions.len); |
| 861 | | self.mir_instructions.appendAssumeCapacity(inst); |
| 854 | const result_index: Mir.Inst.Index = @intCast(func.mir_instructions.len); |
| 855 | func.mir_instructions.appendAssumeCapacity(inst); |
| 862 | 856 | return result_index; |
| 863 | 857 | } |
| 864 | 858 | |
| 865 | | fn addNop(self: *Self) error{OutOfMemory}!Mir.Inst.Index { |
| 866 | | return self.addInst(.{ |
| 859 | fn addNop(func: *Func) error{OutOfMemory}!Mir.Inst.Index { |
| 860 | return func.addInst(.{ |
| 867 | 861 | .tag = .nop, |
| 868 | 862 | .ops = .none, |
| 869 | 863 | .data = undefined, |
| 870 | 864 | }); |
| 871 | 865 | } |
| 872 | 866 | |
| 873 | | fn addPseudoNone(self: *Self, ops: Mir.Inst.Ops) !void { |
| 874 | | _ = try self.addInst(.{ |
| 867 | fn addPseudoNone(func: *Func, ops: Mir.Inst.Ops) !void { |
| 868 | _ = try func.addInst(.{ |
| 875 | 869 | .tag = .pseudo, |
| 876 | 870 | .ops = ops, |
| 877 | 871 | .data = undefined, |
| 878 | 872 | }); |
| 879 | 873 | } |
| 880 | 874 | |
| 881 | | fn addPseudo(self: *Self, ops: Mir.Inst.Ops) !Mir.Inst.Index { |
| 882 | | return self.addInst(.{ |
| 875 | fn addPseudo(func: *Func, ops: Mir.Inst.Ops) !Mir.Inst.Index { |
| 876 | return func.addInst(.{ |
| 883 | 877 | .tag = .pseudo, |
| 884 | 878 | .ops = ops, |
| 885 | 879 | .data = undefined, |
| 886 | 880 | }); |
| 887 | 881 | } |
| 888 | 882 | |
| 889 | | pub fn addExtra(self: *Self, extra: anytype) Allocator.Error!u32 { |
| 883 | pub fn addExtra(func: *Func, extra: anytype) Allocator.Error!u32 { |
| 890 | 884 | const fields = std.meta.fields(@TypeOf(extra)); |
| 891 | | try self.mir_extra.ensureUnusedCapacity(self.gpa, fields.len); |
| 892 | | return self.addExtraAssumeCapacity(extra); |
| 885 | try func.mir_extra.ensureUnusedCapacity(func.gpa, fields.len); |
| 886 | return func.addExtraAssumeCapacity(extra); |
| 893 | 887 | } |
| 894 | 888 | |
| 895 | | pub fn addExtraAssumeCapacity(self: *Self, extra: anytype) u32 { |
| 889 | pub fn addExtraAssumeCapacity(func: *Func, extra: anytype) u32 { |
| 896 | 890 | const fields = std.meta.fields(@TypeOf(extra)); |
| 897 | | const result: u32 = @intCast(self.mir_extra.items.len); |
| 891 | const result: u32 = @intCast(func.mir_extra.items.len); |
| 898 | 892 | inline for (fields) |field| { |
| 899 | | self.mir_extra.appendAssumeCapacity(switch (field.type) { |
| 893 | func.mir_extra.appendAssumeCapacity(switch (field.type) { |
| 900 | 894 | u32 => @field(extra, field.name), |
| 901 | 895 | i32 => @bitCast(@field(extra, field.name)), |
| 902 | 896 | else => @compileError("bad field type"), |
| ... | ... | @@ -910,13 +904,13 @@ const required_features = [_]Target.riscv.Feature{ |
| 910 | 904 | .m, |
| 911 | 905 | }; |
| 912 | 906 | |
| 913 | | fn gen(self: *Self) !void { |
| 914 | | const mod = self.bin_file.comp.module.?; |
| 915 | | const fn_info = mod.typeToFunc(self.fn_type).?; |
| 907 | fn gen(func: *Func) !void { |
| 908 | const mod = func.bin_file.comp.module.?; |
| 909 | const fn_info = mod.typeToFunc(func.fn_type).?; |
| 916 | 910 | |
| 917 | 911 | inline for (required_features) |feature| { |
| 918 | | if (!self.hasFeature(feature)) { |
| 919 | | return self.fail( |
| 912 | if (!func.hasFeature(feature)) { |
| 913 | return func.fail( |
| 920 | 914 | "target missing required feature {s}", |
| 921 | 915 | .{@tagName(feature)}, |
| 922 | 916 | ); |
| ... | ... | @@ -924,52 +918,52 @@ fn gen(self: *Self) !void { |
| 924 | 918 | } |
| 925 | 919 | |
| 926 | 920 | if (fn_info.cc != .Naked) { |
| 927 | | try self.addPseudoNone(.pseudo_dbg_prologue_end); |
| 921 | try func.addPseudoNone(.pseudo_dbg_prologue_end); |
| 928 | 922 | |
| 929 | | const backpatch_stack_alloc = try self.addPseudo(.pseudo_dead); |
| 930 | | const backpatch_ra_spill = try self.addPseudo(.pseudo_dead); |
| 931 | | const backpatch_fp_spill = try self.addPseudo(.pseudo_dead); |
| 932 | | const backpatch_fp_add = try self.addPseudo(.pseudo_dead); |
| 933 | | const backpatch_spill_callee_preserved_regs = try self.addPseudo(.pseudo_dead); |
| 923 | const backpatch_stack_alloc = try func.addPseudo(.pseudo_dead); |
| 924 | const backpatch_ra_spill = try func.addPseudo(.pseudo_dead); |
| 925 | const backpatch_fp_spill = try func.addPseudo(.pseudo_dead); |
| 926 | const backpatch_fp_add = try func.addPseudo(.pseudo_dead); |
| 927 | const backpatch_spill_callee_preserved_regs = try func.addPseudo(.pseudo_dead); |
| 934 | 928 | |
| 935 | | switch (self.ret_mcv.long) { |
| 929 | switch (func.ret_mcv.long) { |
| 936 | 930 | .none, .unreach => {}, |
| 937 | 931 | .indirect => { |
| 938 | 932 | // The address where to store the return value for the caller is in a |
| 939 | 933 | // register which the callee is free to clobber. Therefore, we purposely |
| 940 | 934 | // spill it to stack immediately. |
| 941 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(Type.usize, mod)); |
| 942 | | try self.genSetMem( |
| 935 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(Type.usize, mod)); |
| 936 | try func.genSetMem( |
| 943 | 937 | .{ .frame = frame_index }, |
| 944 | 938 | 0, |
| 945 | 939 | Type.usize, |
| 946 | | self.ret_mcv.long.address().offset(-self.ret_mcv.short.indirect.off), |
| 940 | func.ret_mcv.long.address().offset(-func.ret_mcv.short.indirect.off), |
| 947 | 941 | ); |
| 948 | | self.ret_mcv.long = .{ .load_frame = .{ .index = frame_index } }; |
| 949 | | tracking_log.debug("spill {} to {}", .{ self.ret_mcv.long, frame_index }); |
| 942 | func.ret_mcv.long = .{ .load_frame = .{ .index = frame_index } }; |
| 943 | tracking_log.debug("spill {} to {}", .{ func.ret_mcv.long, frame_index }); |
| 950 | 944 | }, |
| 951 | 945 | else => unreachable, |
| 952 | 946 | } |
| 953 | 947 | |
| 954 | | try self.genBody(self.air.getMainBody()); |
| 948 | try func.genBody(func.air.getMainBody()); |
| 955 | 949 | |
| 956 | | for (self.exitlude_jump_relocs.items) |jmp_reloc| { |
| 957 | | self.mir_instructions.items(.data)[jmp_reloc].inst = |
| 958 | | @intCast(self.mir_instructions.len); |
| 950 | for (func.exitlude_jump_relocs.items) |jmp_reloc| { |
| 951 | func.mir_instructions.items(.data)[jmp_reloc].inst = |
| 952 | @intCast(func.mir_instructions.len); |
| 959 | 953 | } |
| 960 | 954 | |
| 961 | | try self.addPseudoNone(.pseudo_dbg_epilogue_begin); |
| 955 | try func.addPseudoNone(.pseudo_dbg_epilogue_begin); |
| 962 | 956 | |
| 963 | | const backpatch_restore_callee_preserved_regs = try self.addPseudo(.pseudo_dead); |
| 964 | | const backpatch_ra_restore = try self.addPseudo(.pseudo_dead); |
| 965 | | const backpatch_fp_restore = try self.addPseudo(.pseudo_dead); |
| 966 | | const backpatch_stack_alloc_restore = try self.addPseudo(.pseudo_dead); |
| 967 | | try self.addPseudoNone(.pseudo_ret); |
| 957 | const backpatch_restore_callee_preserved_regs = try func.addPseudo(.pseudo_dead); |
| 958 | const backpatch_ra_restore = try func.addPseudo(.pseudo_dead); |
| 959 | const backpatch_fp_restore = try func.addPseudo(.pseudo_dead); |
| 960 | const backpatch_stack_alloc_restore = try func.addPseudo(.pseudo_dead); |
| 961 | try func.addPseudoNone(.pseudo_ret); |
| 968 | 962 | |
| 969 | | const frame_layout = try self.computeFrameLayout(); |
| 963 | const frame_layout = try func.computeFrameLayout(); |
| 970 | 964 | const need_save_reg = frame_layout.save_reg_list.count() > 0; |
| 971 | 965 | |
| 972 | | self.mir_instructions.set(backpatch_stack_alloc, .{ |
| 966 | func.mir_instructions.set(backpatch_stack_alloc, .{ |
| 973 | 967 | .tag = .addi, |
| 974 | 968 | .ops = .rri, |
| 975 | 969 | .data = .{ .i_type = .{ |
| ... | ... | @@ -978,7 +972,7 @@ fn gen(self: *Self) !void { |
| 978 | 972 | .imm12 = Immediate.s(-@as(i32, @intCast(frame_layout.stack_adjust))), |
| 979 | 973 | } }, |
| 980 | 974 | }); |
| 981 | | self.mir_instructions.set(backpatch_ra_spill, .{ |
| 975 | func.mir_instructions.set(backpatch_ra_spill, .{ |
| 982 | 976 | .tag = .pseudo, |
| 983 | 977 | .ops = .pseudo_store_rm, |
| 984 | 978 | .data = .{ .rm = .{ |
| ... | ... | @@ -989,7 +983,7 @@ fn gen(self: *Self) !void { |
| 989 | 983 | }, |
| 990 | 984 | } }, |
| 991 | 985 | }); |
| 992 | | self.mir_instructions.set(backpatch_ra_restore, .{ |
| 986 | func.mir_instructions.set(backpatch_ra_restore, .{ |
| 993 | 987 | .tag = .pseudo, |
| 994 | 988 | .ops = .pseudo_load_rm, |
| 995 | 989 | .data = .{ .rm = .{ |
| ... | ... | @@ -1000,7 +994,7 @@ fn gen(self: *Self) !void { |
| 1000 | 994 | }, |
| 1001 | 995 | } }, |
| 1002 | 996 | }); |
| 1003 | | self.mir_instructions.set(backpatch_fp_spill, .{ |
| 997 | func.mir_instructions.set(backpatch_fp_spill, .{ |
| 1004 | 998 | .tag = .pseudo, |
| 1005 | 999 | .ops = .pseudo_store_rm, |
| 1006 | 1000 | .data = .{ .rm = .{ |
| ... | ... | @@ -1011,7 +1005,7 @@ fn gen(self: *Self) !void { |
| 1011 | 1005 | }, |
| 1012 | 1006 | } }, |
| 1013 | 1007 | }); |
| 1014 | | self.mir_instructions.set(backpatch_fp_restore, .{ |
| 1008 | func.mir_instructions.set(backpatch_fp_restore, .{ |
| 1015 | 1009 | .tag = .pseudo, |
| 1016 | 1010 | .ops = .pseudo_load_rm, |
| 1017 | 1011 | .data = .{ .rm = .{ |
| ... | ... | @@ -1022,7 +1016,7 @@ fn gen(self: *Self) !void { |
| 1022 | 1016 | }, |
| 1023 | 1017 | } }, |
| 1024 | 1018 | }); |
| 1025 | | self.mir_instructions.set(backpatch_fp_add, .{ |
| 1019 | func.mir_instructions.set(backpatch_fp_add, .{ |
| 1026 | 1020 | .tag = .addi, |
| 1027 | 1021 | .ops = .rri, |
| 1028 | 1022 | .data = .{ .i_type = .{ |
| ... | ... | @@ -1031,7 +1025,7 @@ fn gen(self: *Self) !void { |
| 1031 | 1025 | .imm12 = Immediate.s(@intCast(frame_layout.stack_adjust)), |
| 1032 | 1026 | } }, |
| 1033 | 1027 | }); |
| 1034 | | self.mir_instructions.set(backpatch_stack_alloc_restore, .{ |
| 1028 | func.mir_instructions.set(backpatch_stack_alloc_restore, .{ |
| 1035 | 1029 | .tag = .addi, |
| 1036 | 1030 | .ops = .rri, |
| 1037 | 1031 | .data = .{ .i_type = .{ |
| ... | ... | @@ -1042,72 +1036,72 @@ fn gen(self: *Self) !void { |
| 1042 | 1036 | }); |
| 1043 | 1037 | |
| 1044 | 1038 | if (need_save_reg) { |
| 1045 | | self.mir_instructions.set(backpatch_spill_callee_preserved_regs, .{ |
| 1039 | func.mir_instructions.set(backpatch_spill_callee_preserved_regs, .{ |
| 1046 | 1040 | .tag = .pseudo, |
| 1047 | 1041 | .ops = .pseudo_spill_regs, |
| 1048 | 1042 | .data = .{ .reg_list = frame_layout.save_reg_list }, |
| 1049 | 1043 | }); |
| 1050 | 1044 | |
| 1051 | | self.mir_instructions.set(backpatch_restore_callee_preserved_regs, .{ |
| 1045 | func.mir_instructions.set(backpatch_restore_callee_preserved_regs, .{ |
| 1052 | 1046 | .tag = .pseudo, |
| 1053 | 1047 | .ops = .pseudo_restore_regs, |
| 1054 | 1048 | .data = .{ .reg_list = frame_layout.save_reg_list }, |
| 1055 | 1049 | }); |
| 1056 | 1050 | } |
| 1057 | 1051 | } else { |
| 1058 | | try self.addPseudoNone(.pseudo_dbg_prologue_end); |
| 1059 | | try self.genBody(self.air.getMainBody()); |
| 1060 | | try self.addPseudoNone(.pseudo_dbg_epilogue_begin); |
| 1052 | try func.addPseudoNone(.pseudo_dbg_prologue_end); |
| 1053 | try func.genBody(func.air.getMainBody()); |
| 1054 | try func.addPseudoNone(.pseudo_dbg_epilogue_begin); |
| 1061 | 1055 | } |
| 1062 | 1056 | |
| 1063 | 1057 | // Drop them off at the rbrace. |
| 1064 | | _ = try self.addInst(.{ |
| 1058 | _ = try func.addInst(.{ |
| 1065 | 1059 | .tag = .pseudo, |
| 1066 | 1060 | .ops = .pseudo_dbg_line_column, |
| 1067 | 1061 | .data = .{ .pseudo_dbg_line_column = .{ |
| 1068 | | .line = self.end_di_line, |
| 1069 | | .column = self.end_di_column, |
| 1062 | .line = func.end_di_line, |
| 1063 | .column = func.end_di_column, |
| 1070 | 1064 | } }, |
| 1071 | 1065 | }); |
| 1072 | 1066 | } |
| 1073 | 1067 | |
| 1074 | | fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1075 | | const zcu = self.bin_file.comp.module.?; |
| 1068 | fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void { |
| 1069 | const zcu = func.bin_file.comp.module.?; |
| 1076 | 1070 | const ip = &zcu.intern_pool; |
| 1077 | | const air_tags = self.air.instructions.items(.tag); |
| 1071 | const air_tags = func.air.instructions.items(.tag); |
| 1078 | 1072 | |
| 1079 | 1073 | for (body) |inst| { |
| 1080 | | if (self.liveness.isUnused(inst) and !self.air.mustLower(inst, ip)) continue; |
| 1074 | if (func.liveness.isUnused(inst) and !func.air.mustLower(inst, ip)) continue; |
| 1081 | 1075 | |
| 1082 | | const old_air_bookkeeping = self.air_bookkeeping; |
| 1083 | | try self.inst_tracking.ensureUnusedCapacity(self.gpa, 1); |
| 1076 | const old_air_bookkeeping = func.air_bookkeeping; |
| 1077 | try func.inst_tracking.ensureUnusedCapacity(func.gpa, 1); |
| 1084 | 1078 | switch (air_tags[@intFromEnum(inst)]) { |
| 1085 | 1079 | // zig fmt: off |
| 1086 | | .ptr_add => try self.airPtrArithmetic(inst, .ptr_add), |
| 1087 | | .ptr_sub => try self.airPtrArithmetic(inst, .ptr_sub), |
| 1080 | .ptr_add => try func.airPtrArithmetic(inst, .ptr_add), |
| 1081 | .ptr_sub => try func.airPtrArithmetic(inst, .ptr_sub), |
| 1088 | 1082 | |
| 1089 | | .add => try self.airBinOp(inst, .add), |
| 1090 | | .sub => try self.airBinOp(inst, .sub), |
| 1083 | .add => try func.airBinOp(inst, .add), |
| 1084 | .sub => try func.airBinOp(inst, .sub), |
| 1091 | 1085 | |
| 1092 | 1086 | .add_safe, |
| 1093 | 1087 | .sub_safe, |
| 1094 | 1088 | .mul_safe, |
| 1095 | | => return self.fail("TODO implement safety_checked_instructions", .{}), |
| 1096 | | |
| 1097 | | .add_wrap => try self.airAddWrap(inst), |
| 1098 | | .add_sat => try self.airAddSat(inst), |
| 1099 | | .sub_wrap => try self.airSubWrap(inst), |
| 1100 | | .sub_sat => try self.airSubSat(inst), |
| 1101 | | .mul => try self.airMul(inst), |
| 1102 | | .mul_wrap => try self.airMulWrap(inst), |
| 1103 | | .mul_sat => try self.airMulSat(inst), |
| 1104 | | .rem => try self.airRem(inst), |
| 1105 | | .mod => try self.airMod(inst), |
| 1106 | | .shl, .shl_exact => try self.airShl(inst), |
| 1107 | | .shl_sat => try self.airShlSat(inst), |
| 1108 | | .min => try self.airMinMax(inst, .min), |
| 1109 | | .max => try self.airMinMax(inst, .max), |
| 1110 | | .slice => try self.airSlice(inst), |
| 1089 | => return func.fail("TODO implement safety_checked_instructions", .{}), |
| 1090 | |
| 1091 | .add_wrap => try func.airAddWrap(inst), |
| 1092 | .add_sat => try func.airAddSat(inst), |
| 1093 | .sub_wrap => try func.airSubWrap(inst), |
| 1094 | .sub_sat => try func.airSubSat(inst), |
| 1095 | .mul => try func.airMul(inst), |
| 1096 | .mul_wrap => try func.airMulWrap(inst), |
| 1097 | .mul_sat => try func.airMulSat(inst), |
| 1098 | .rem => try func.airRem(inst), |
| 1099 | .mod => try func.airMod(inst), |
| 1100 | .shl, .shl_exact => try func.airShl(inst), |
| 1101 | .shl_sat => try func.airShlSat(inst), |
| 1102 | .min => try func.airMinMax(inst, .min), |
| 1103 | .max => try func.airMinMax(inst, .max), |
| 1104 | .slice => try func.airSlice(inst), |
| 1111 | 1105 | |
| 1112 | 1106 | .sqrt, |
| 1113 | 1107 | .sin, |
| ... | ... | @@ -1123,157 +1117,157 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1123 | 1117 | .round, |
| 1124 | 1118 | .trunc_float, |
| 1125 | 1119 | .neg, |
| 1126 | | => try self.airUnaryMath(inst), |
| 1127 | | |
| 1128 | | .add_with_overflow => try self.airAddWithOverflow(inst), |
| 1129 | | .sub_with_overflow => try self.airSubWithOverflow(inst), |
| 1130 | | .mul_with_overflow => try self.airMulWithOverflow(inst), |
| 1131 | | .shl_with_overflow => try self.airShlWithOverflow(inst), |
| 1132 | | |
| 1133 | | .div_float, .div_trunc, .div_floor, .div_exact => try self.airDiv(inst), |
| 1134 | | |
| 1135 | | .cmp_lt => try self.airCmp(inst), |
| 1136 | | .cmp_lte => try self.airCmp(inst), |
| 1137 | | .cmp_eq => try self.airCmp(inst), |
| 1138 | | .cmp_gte => try self.airCmp(inst), |
| 1139 | | .cmp_gt => try self.airCmp(inst), |
| 1140 | | .cmp_neq => try self.airCmp(inst), |
| 1141 | | |
| 1142 | | .cmp_vector => try self.airCmpVector(inst), |
| 1143 | | .cmp_lt_errors_len => try self.airCmpLtErrorsLen(inst), |
| 1144 | | |
| 1145 | | .bool_and => try self.airBoolOp(inst), |
| 1146 | | .bool_or => try self.airBoolOp(inst), |
| 1147 | | .bit_and => try self.airBitAnd(inst), |
| 1148 | | .bit_or => try self.airBitOr(inst), |
| 1149 | | .xor => try self.airXor(inst), |
| 1150 | | .shr, .shr_exact => try self.airShr(inst), |
| 1151 | | |
| 1152 | | .alloc => try self.airAlloc(inst), |
| 1153 | | .ret_ptr => try self.airRetPtr(inst), |
| 1154 | | .arg => try self.airArg(inst), |
| 1155 | | .assembly => try self.airAsm(inst), |
| 1156 | | .bitcast => try self.airBitCast(inst), |
| 1157 | | .block => try self.airBlock(inst), |
| 1158 | | .br => try self.airBr(inst), |
| 1159 | | .trap => try self.airTrap(), |
| 1160 | | .breakpoint => try self.airBreakpoint(), |
| 1161 | | .ret_addr => try self.airRetAddr(inst), |
| 1162 | | .frame_addr => try self.airFrameAddress(inst), |
| 1163 | | .fence => try self.airFence(), |
| 1164 | | .cond_br => try self.airCondBr(inst), |
| 1165 | | .dbg_stmt => try self.airDbgStmt(inst), |
| 1166 | | .fptrunc => try self.airFptrunc(inst), |
| 1167 | | .fpext => try self.airFpext(inst), |
| 1168 | | .intcast => try self.airIntCast(inst), |
| 1169 | | .trunc => try self.airTrunc(inst), |
| 1170 | | .int_from_bool => try self.airIntFromBool(inst), |
| 1171 | | .is_non_null => try self.airIsNonNull(inst), |
| 1172 | | .is_non_null_ptr => try self.airIsNonNullPtr(inst), |
| 1173 | | .is_null => try self.airIsNull(inst), |
| 1174 | | .is_null_ptr => try self.airIsNullPtr(inst), |
| 1175 | | .is_non_err => try self.airIsNonErr(inst), |
| 1176 | | .is_non_err_ptr => try self.airIsNonErrPtr(inst), |
| 1177 | | .is_err => try self.airIsErr(inst), |
| 1178 | | .is_err_ptr => try self.airIsErrPtr(inst), |
| 1179 | | .load => try self.airLoad(inst), |
| 1180 | | .loop => try self.airLoop(inst), |
| 1181 | | .not => try self.airNot(inst), |
| 1182 | | .int_from_ptr => try self.airIntFromPtr(inst), |
| 1183 | | .ret => try self.airRet(inst, false), |
| 1184 | | .ret_safe => try self.airRet(inst, true), |
| 1185 | | .ret_load => try self.airRetLoad(inst), |
| 1186 | | .store => try self.airStore(inst, false), |
| 1187 | | .store_safe => try self.airStore(inst, true), |
| 1188 | | .struct_field_ptr=> try self.airStructFieldPtr(inst), |
| 1189 | | .struct_field_val=> try self.airStructFieldVal(inst), |
| 1190 | | .array_to_slice => try self.airArrayToSlice(inst), |
| 1191 | | .float_from_int => try self.airFloatFromInt(inst), |
| 1192 | | .int_from_float => try self.airIntFromFloat(inst), |
| 1193 | | .cmpxchg_strong => try self.airCmpxchg(inst), |
| 1194 | | .cmpxchg_weak => try self.airCmpxchg(inst), |
| 1195 | | .atomic_rmw => try self.airAtomicRmw(inst), |
| 1196 | | .atomic_load => try self.airAtomicLoad(inst), |
| 1197 | | .memcpy => try self.airMemcpy(inst), |
| 1198 | | .memset => try self.airMemset(inst, false), |
| 1199 | | .memset_safe => try self.airMemset(inst, true), |
| 1200 | | .set_union_tag => try self.airSetUnionTag(inst), |
| 1201 | | .get_union_tag => try self.airGetUnionTag(inst), |
| 1202 | | .clz => try self.airClz(inst), |
| 1203 | | .ctz => try self.airCtz(inst), |
| 1204 | | .popcount => try self.airPopcount(inst), |
| 1205 | | .abs => try self.airAbs(inst), |
| 1206 | | .byte_swap => try self.airByteSwap(inst), |
| 1207 | | .bit_reverse => try self.airBitReverse(inst), |
| 1208 | | .tag_name => try self.airTagName(inst), |
| 1209 | | .error_name => try self.airErrorName(inst), |
| 1210 | | .splat => try self.airSplat(inst), |
| 1211 | | .select => try self.airSelect(inst), |
| 1212 | | .shuffle => try self.airShuffle(inst), |
| 1213 | | .reduce => try self.airReduce(inst), |
| 1214 | | .aggregate_init => try self.airAggregateInit(inst), |
| 1215 | | .union_init => try self.airUnionInit(inst), |
| 1216 | | .prefetch => try self.airPrefetch(inst), |
| 1217 | | .mul_add => try self.airMulAdd(inst), |
| 1218 | | .addrspace_cast => return self.fail("TODO: addrspace_cast", .{}), |
| 1219 | | |
| 1220 | | .@"try" => try self.airTry(inst), |
| 1221 | | .try_ptr => return self.fail("TODO: try_ptr", .{}), |
| 1120 | => try func.airUnaryMath(inst), |
| 1121 | |
| 1122 | .add_with_overflow => try func.airAddWithOverflow(inst), |
| 1123 | .sub_with_overflow => try func.airSubWithOverflow(inst), |
| 1124 | .mul_with_overflow => try func.airMulWithOverflow(inst), |
| 1125 | .shl_with_overflow => try func.airShlWithOverflow(inst), |
| 1126 | |
| 1127 | .div_float, .div_trunc, .div_floor, .div_exact => try func.airDiv(inst), |
| 1128 | |
| 1129 | .cmp_lt => try func.airCmp(inst), |
| 1130 | .cmp_lte => try func.airCmp(inst), |
| 1131 | .cmp_eq => try func.airCmp(inst), |
| 1132 | .cmp_gte => try func.airCmp(inst), |
| 1133 | .cmp_gt => try func.airCmp(inst), |
| 1134 | .cmp_neq => try func.airCmp(inst), |
| 1135 | |
| 1136 | .cmp_vector => try func.airCmpVector(inst), |
| 1137 | .cmp_lt_errors_len => try func.airCmpLtErrorsLen(inst), |
| 1138 | |
| 1139 | .bool_and => try func.airBoolOp(inst), |
| 1140 | .bool_or => try func.airBoolOp(inst), |
| 1141 | .bit_and => try func.airBitAnd(inst), |
| 1142 | .bit_or => try func.airBitOr(inst), |
| 1143 | .xor => try func.airXor(inst), |
| 1144 | .shr, .shr_exact => try func.airShr(inst), |
| 1145 | |
| 1146 | .alloc => try func.airAlloc(inst), |
| 1147 | .ret_ptr => try func.airRetPtr(inst), |
| 1148 | .arg => try func.airArg(inst), |
| 1149 | .assembly => try func.airAsm(inst), |
| 1150 | .bitcast => try func.airBitCast(inst), |
| 1151 | .block => try func.airBlock(inst), |
| 1152 | .br => try func.airBr(inst), |
| 1153 | .trap => try func.airTrap(), |
| 1154 | .breakpoint => try func.airBreakpoint(), |
| 1155 | .ret_addr => try func.airRetAddr(inst), |
| 1156 | .frame_addr => try func.airFrameAddress(inst), |
| 1157 | .fence => try func.airFence(), |
| 1158 | .cond_br => try func.airCondBr(inst), |
| 1159 | .dbg_stmt => try func.airDbgStmt(inst), |
| 1160 | .fptrunc => try func.airFptrunc(inst), |
| 1161 | .fpext => try func.airFpext(inst), |
| 1162 | .intcast => try func.airIntCast(inst), |
| 1163 | .trunc => try func.airTrunc(inst), |
| 1164 | .int_from_bool => try func.airIntFromBool(inst), |
| 1165 | .is_non_null => try func.airIsNonNull(inst), |
| 1166 | .is_non_null_ptr => try func.airIsNonNullPtr(inst), |
| 1167 | .is_null => try func.airIsNull(inst), |
| 1168 | .is_null_ptr => try func.airIsNullPtr(inst), |
| 1169 | .is_non_err => try func.airIsNonErr(inst), |
| 1170 | .is_non_err_ptr => try func.airIsNonErrPtr(inst), |
| 1171 | .is_err => try func.airIsErr(inst), |
| 1172 | .is_err_ptr => try func.airIsErrPtr(inst), |
| 1173 | .load => try func.airLoad(inst), |
| 1174 | .loop => try func.airLoop(inst), |
| 1175 | .not => try func.airNot(inst), |
| 1176 | .int_from_ptr => try func.airIntFromPtr(inst), |
| 1177 | .ret => try func.airRet(inst, false), |
| 1178 | .ret_safe => try func.airRet(inst, true), |
| 1179 | .ret_load => try func.airRetLoad(inst), |
| 1180 | .store => try func.airStore(inst, false), |
| 1181 | .store_safe => try func.airStore(inst, true), |
| 1182 | .struct_field_ptr=> try func.airStructFieldPtr(inst), |
| 1183 | .struct_field_val=> try func.airStructFieldVal(inst), |
| 1184 | .array_to_slice => try func.airArrayToSlice(inst), |
| 1185 | .float_from_int => try func.airFloatFromInt(inst), |
| 1186 | .int_from_float => try func.airIntFromFloat(inst), |
| 1187 | .cmpxchg_strong => try func.airCmpxchg(inst), |
| 1188 | .cmpxchg_weak => try func.airCmpxchg(inst), |
| 1189 | .atomic_rmw => try func.airAtomicRmw(inst), |
| 1190 | .atomic_load => try func.airAtomicLoad(inst), |
| 1191 | .memcpy => try func.airMemcpy(inst), |
| 1192 | .memset => try func.airMemset(inst, false), |
| 1193 | .memset_safe => try func.airMemset(inst, true), |
| 1194 | .set_union_tag => try func.airSetUnionTag(inst), |
| 1195 | .get_union_tag => try func.airGetUnionTag(inst), |
| 1196 | .clz => try func.airClz(inst), |
| 1197 | .ctz => try func.airCtz(inst), |
| 1198 | .popcount => try func.airPopcount(inst), |
| 1199 | .abs => try func.airAbs(inst), |
| 1200 | .byte_swap => try func.airByteSwap(inst), |
| 1201 | .bit_reverse => try func.airBitReverse(inst), |
| 1202 | .tag_name => try func.airTagName(inst), |
| 1203 | .error_name => try func.airErrorName(inst), |
| 1204 | .splat => try func.airSplat(inst), |
| 1205 | .select => try func.airSelect(inst), |
| 1206 | .shuffle => try func.airShuffle(inst), |
| 1207 | .reduce => try func.airReduce(inst), |
| 1208 | .aggregate_init => try func.airAggregateInit(inst), |
| 1209 | .union_init => try func.airUnionInit(inst), |
| 1210 | .prefetch => try func.airPrefetch(inst), |
| 1211 | .mul_add => try func.airMulAdd(inst), |
| 1212 | .addrspace_cast => return func.fail("TODO: addrspace_cast", .{}), |
| 1213 | |
| 1214 | .@"try" => try func.airTry(inst), |
| 1215 | .try_ptr => return func.fail("TODO: try_ptr", .{}), |
| 1222 | 1216 | |
| 1223 | 1217 | .dbg_var_ptr, |
| 1224 | 1218 | .dbg_var_val, |
| 1225 | | => try self.airDbgVar(inst), |
| 1219 | => try func.airDbgVar(inst), |
| 1226 | 1220 | |
| 1227 | | .dbg_inline_block => try self.airDbgInlineBlock(inst), |
| 1221 | .dbg_inline_block => try func.airDbgInlineBlock(inst), |
| 1228 | 1222 | |
| 1229 | | .call => try self.airCall(inst, .auto), |
| 1230 | | .call_always_tail => try self.airCall(inst, .always_tail), |
| 1231 | | .call_never_tail => try self.airCall(inst, .never_tail), |
| 1232 | | .call_never_inline => try self.airCall(inst, .never_inline), |
| 1223 | .call => try func.airCall(inst, .auto), |
| 1224 | .call_always_tail => try func.airCall(inst, .always_tail), |
| 1225 | .call_never_tail => try func.airCall(inst, .never_tail), |
| 1226 | .call_never_inline => try func.airCall(inst, .never_inline), |
| 1233 | 1227 | |
| 1234 | | .atomic_store_unordered => try self.airAtomicStore(inst, .unordered), |
| 1235 | | .atomic_store_monotonic => try self.airAtomicStore(inst, .monotonic), |
| 1236 | | .atomic_store_release => try self.airAtomicStore(inst, .release), |
| 1237 | | .atomic_store_seq_cst => try self.airAtomicStore(inst, .seq_cst), |
| 1228 | .atomic_store_unordered => try func.airAtomicStore(inst, .unordered), |
| 1229 | .atomic_store_monotonic => try func.airAtomicStore(inst, .monotonic), |
| 1230 | .atomic_store_release => try func.airAtomicStore(inst, .release), |
| 1231 | .atomic_store_seq_cst => try func.airAtomicStore(inst, .seq_cst), |
| 1238 | 1232 | |
| 1239 | | .struct_field_ptr_index_0 => try self.airStructFieldPtrIndex(inst, 0), |
| 1240 | | .struct_field_ptr_index_1 => try self.airStructFieldPtrIndex(inst, 1), |
| 1241 | | .struct_field_ptr_index_2 => try self.airStructFieldPtrIndex(inst, 2), |
| 1242 | | .struct_field_ptr_index_3 => try self.airStructFieldPtrIndex(inst, 3), |
| 1233 | .struct_field_ptr_index_0 => try func.airStructFieldPtrIndex(inst, 0), |
| 1234 | .struct_field_ptr_index_1 => try func.airStructFieldPtrIndex(inst, 1), |
| 1235 | .struct_field_ptr_index_2 => try func.airStructFieldPtrIndex(inst, 2), |
| 1236 | .struct_field_ptr_index_3 => try func.airStructFieldPtrIndex(inst, 3), |
| 1243 | 1237 | |
| 1244 | | .field_parent_ptr => try self.airFieldParentPtr(inst), |
| 1238 | .field_parent_ptr => try func.airFieldParentPtr(inst), |
| 1245 | 1239 | |
| 1246 | | .switch_br => try self.airSwitchBr(inst), |
| 1247 | | .slice_ptr => try self.airSlicePtr(inst), |
| 1248 | | .slice_len => try self.airSliceLen(inst), |
| 1240 | .switch_br => try func.airSwitchBr(inst), |
| 1241 | .slice_ptr => try func.airSlicePtr(inst), |
| 1242 | .slice_len => try func.airSliceLen(inst), |
| 1249 | 1243 | |
| 1250 | | .ptr_slice_len_ptr => try self.airPtrSliceLenPtr(inst), |
| 1251 | | .ptr_slice_ptr_ptr => try self.airPtrSlicePtrPtr(inst), |
| 1244 | .ptr_slice_len_ptr => try func.airPtrSliceLenPtr(inst), |
| 1245 | .ptr_slice_ptr_ptr => try func.airPtrSlicePtrPtr(inst), |
| 1252 | 1246 | |
| 1253 | | .array_elem_val => try self.airArrayElemVal(inst), |
| 1254 | | .slice_elem_val => try self.airSliceElemVal(inst), |
| 1255 | | .slice_elem_ptr => try self.airSliceElemPtr(inst), |
| 1256 | | .ptr_elem_val => try self.airPtrElemVal(inst), |
| 1257 | | .ptr_elem_ptr => try self.airPtrElemPtr(inst), |
| 1247 | .array_elem_val => try func.airArrayElemVal(inst), |
| 1248 | .slice_elem_val => try func.airSliceElemVal(inst), |
| 1249 | .slice_elem_ptr => try func.airSliceElemPtr(inst), |
| 1250 | .ptr_elem_val => try func.airPtrElemVal(inst), |
| 1251 | .ptr_elem_ptr => try func.airPtrElemPtr(inst), |
| 1258 | 1252 | |
| 1259 | 1253 | .inferred_alloc, .inferred_alloc_comptime => unreachable, |
| 1260 | | .unreach => self.finishAirBookkeeping(), |
| 1261 | | |
| 1262 | | .optional_payload => try self.airOptionalPayload(inst), |
| 1263 | | .optional_payload_ptr => try self.airOptionalPayloadPtr(inst), |
| 1264 | | .optional_payload_ptr_set => try self.airOptionalPayloadPtrSet(inst), |
| 1265 | | .unwrap_errunion_err => try self.airUnwrapErrErr(inst), |
| 1266 | | .unwrap_errunion_payload => try self.airUnwrapErrPayload(inst), |
| 1267 | | .unwrap_errunion_err_ptr => try self.airUnwrapErrErrPtr(inst), |
| 1268 | | .unwrap_errunion_payload_ptr=> try self.airUnwrapErrPayloadPtr(inst), |
| 1269 | | .errunion_payload_ptr_set => try self.airErrUnionPayloadPtrSet(inst), |
| 1270 | | .err_return_trace => try self.airErrReturnTrace(inst), |
| 1271 | | .set_err_return_trace => try self.airSetErrReturnTrace(inst), |
| 1272 | | .save_err_return_trace_index=> try self.airSaveErrReturnTraceIndex(inst), |
| 1273 | | |
| 1274 | | .wrap_optional => try self.airWrapOptional(inst), |
| 1275 | | .wrap_errunion_payload => try self.airWrapErrUnionPayload(inst), |
| 1276 | | .wrap_errunion_err => try self.airWrapErrUnionErr(inst), |
| 1254 | .unreach => func.finishAirBookkeeping(), |
| 1255 | |
| 1256 | .optional_payload => try func.airOptionalPayload(inst), |
| 1257 | .optional_payload_ptr => try func.airOptionalPayloadPtr(inst), |
| 1258 | .optional_payload_ptr_set => try func.airOptionalPayloadPtrSet(inst), |
| 1259 | .unwrap_errunion_err => try func.airUnwrapErrErr(inst), |
| 1260 | .unwrap_errunion_payload => try func.airUnwrapErrPayload(inst), |
| 1261 | .unwrap_errunion_err_ptr => try func.airUnwrapErrErrPtr(inst), |
| 1262 | .unwrap_errunion_payload_ptr=> try func.airUnwrapErrPayloadPtr(inst), |
| 1263 | .errunion_payload_ptr_set => try func.airErrUnionPayloadPtrSet(inst), |
| 1264 | .err_return_trace => try func.airErrReturnTrace(inst), |
| 1265 | .set_err_return_trace => try func.airSetErrReturnTrace(inst), |
| 1266 | .save_err_return_trace_index=> try func.airSaveErrReturnTraceIndex(inst), |
| 1267 | |
| 1268 | .wrap_optional => try func.airWrapOptional(inst), |
| 1269 | .wrap_errunion_payload => try func.airWrapErrUnionPayload(inst), |
| 1270 | .wrap_errunion_err => try func.airWrapErrUnionErr(inst), |
| 1277 | 1271 | |
| 1278 | 1272 | .add_optimized, |
| 1279 | 1273 | .sub_optimized, |
| ... | ... | @@ -1294,16 +1288,16 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1294 | 1288 | .cmp_vector_optimized, |
| 1295 | 1289 | .reduce_optimized, |
| 1296 | 1290 | .int_from_float_optimized, |
| 1297 | | => return self.fail("TODO implement optimized float mode", .{}), |
| 1291 | => return func.fail("TODO implement optimized float mode", .{}), |
| 1298 | 1292 | |
| 1299 | | .is_named_enum_value => return self.fail("TODO implement is_named_enum_value", .{}), |
| 1300 | | .error_set_has_value => return self.fail("TODO implement error_set_has_value", .{}), |
| 1301 | | .vector_store_elem => return self.fail("TODO implement vector_store_elem", .{}), |
| 1293 | .is_named_enum_value => return func.fail("TODO implement is_named_enum_value", .{}), |
| 1294 | .error_set_has_value => return func.fail("TODO implement error_set_has_value", .{}), |
| 1295 | .vector_store_elem => return func.fail("TODO implement vector_store_elem", .{}), |
| 1302 | 1296 | |
| 1303 | | .c_va_arg => return self.fail("TODO implement c_va_arg", .{}), |
| 1304 | | .c_va_copy => return self.fail("TODO implement c_va_copy", .{}), |
| 1305 | | .c_va_end => return self.fail("TODO implement c_va_end", .{}), |
| 1306 | | .c_va_start => return self.fail("TODO implement c_va_start", .{}), |
| 1297 | .c_va_arg => return func.fail("TODO implement c_va_arg", .{}), |
| 1298 | .c_va_copy => return func.fail("TODO implement c_va_copy", .{}), |
| 1299 | .c_va_end => return func.fail("TODO implement c_va_end", .{}), |
| 1300 | .c_va_start => return func.fail("TODO implement c_va_start", .{}), |
| 1307 | 1301 | |
| 1308 | 1302 | .wasm_memory_size => unreachable, |
| 1309 | 1303 | .wasm_memory_grow => unreachable, |
| ... | ... | @@ -1314,19 +1308,19 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1314 | 1308 | // zig fmt: on |
| 1315 | 1309 | } |
| 1316 | 1310 | |
| 1317 | | assert(!self.register_manager.lockedRegsExist()); |
| 1311 | assert(!func.register_manager.lockedRegsExist()); |
| 1318 | 1312 | |
| 1319 | 1313 | if (std.debug.runtime_safety) { |
| 1320 | | if (self.air_bookkeeping < old_air_bookkeeping + 1) { |
| 1314 | if (func.air_bookkeeping < old_air_bookkeeping + 1) { |
| 1321 | 1315 | 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)] }); |
| 1322 | 1316 | } |
| 1323 | 1317 | |
| 1324 | 1318 | { // check consistency of tracked registers |
| 1325 | | var it = self.register_manager.free_registers.iterator(.{ .kind = .unset }); |
| 1319 | var it = func.register_manager.free_registers.iterator(.{ .kind = .unset }); |
| 1326 | 1320 | while (it.next()) |index| { |
| 1327 | | const tracked_inst = self.register_manager.registers[index]; |
| 1321 | const tracked_inst = func.register_manager.registers[index]; |
| 1328 | 1322 | tracking_log.debug("tracked inst: {}", .{tracked_inst}); |
| 1329 | | const tracking = self.getResolvedInstValue(tracked_inst); |
| 1323 | const tracking = func.getResolvedInstValue(tracked_inst); |
| 1330 | 1324 | for (tracking.getRegs()) |reg| { |
| 1331 | 1325 | if (RegisterManager.indexOfRegIntoTracked(reg).? == index) break; |
| 1332 | 1326 | } else return std.debug.panic( |
| ... | ... | @@ -1338,72 +1332,72 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1338 | 1332 | } |
| 1339 | 1333 | } |
| 1340 | 1334 | |
| 1341 | | fn getValue(self: *Self, value: MCValue, inst: ?Air.Inst.Index) !void { |
| 1342 | | for (value.getRegs()) |reg| try self.register_manager.getReg(reg, inst); |
| 1335 | fn getValue(func: *Func, value: MCValue, inst: ?Air.Inst.Index) !void { |
| 1336 | for (value.getRegs()) |reg| try func.register_manager.getReg(reg, inst); |
| 1343 | 1337 | } |
| 1344 | 1338 | |
| 1345 | | fn getValueIfFree(self: *Self, value: MCValue, inst: ?Air.Inst.Index) void { |
| 1346 | | for (value.getRegs()) |reg| if (self.register_manager.isRegFree(reg)) |
| 1347 | | self.register_manager.getRegAssumeFree(reg, inst); |
| 1339 | fn getValueIfFree(func: *Func, value: MCValue, inst: ?Air.Inst.Index) void { |
| 1340 | for (value.getRegs()) |reg| if (func.register_manager.isRegFree(reg)) |
| 1341 | func.register_manager.getRegAssumeFree(reg, inst); |
| 1348 | 1342 | } |
| 1349 | 1343 | |
| 1350 | | fn freeValue(self: *Self, value: MCValue) !void { |
| 1344 | fn freeValue(func: *Func, value: MCValue) !void { |
| 1351 | 1345 | switch (value) { |
| 1352 | | .register => |reg| self.register_manager.freeReg(reg), |
| 1353 | | .register_pair => |regs| for (regs) |reg| self.register_manager.freeReg(reg), |
| 1354 | | .register_offset => |reg_off| self.register_manager.freeReg(reg_off.reg), |
| 1346 | .register => |reg| func.register_manager.freeReg(reg), |
| 1347 | .register_pair => |regs| for (regs) |reg| func.register_manager.freeReg(reg), |
| 1348 | .register_offset => |reg_off| func.register_manager.freeReg(reg_off.reg), |
| 1355 | 1349 | else => {}, // TODO process stack allocation death |
| 1356 | 1350 | } |
| 1357 | 1351 | } |
| 1358 | 1352 | |
| 1359 | | fn feed(self: *Self, bt: *Liveness.BigTomb, operand: Air.Inst.Ref) !void { |
| 1353 | fn feed(func: *Func, bt: *Liveness.BigTomb, operand: Air.Inst.Ref) !void { |
| 1360 | 1354 | if (bt.feed()) if (operand.toIndex()) |inst| { |
| 1361 | 1355 | log.debug("feed inst: %{}", .{inst}); |
| 1362 | | try self.processDeath(inst); |
| 1356 | try func.processDeath(inst); |
| 1363 | 1357 | }; |
| 1364 | 1358 | } |
| 1365 | 1359 | |
| 1366 | 1360 | /// Asserts there is already capacity to insert into top branch inst_table. |
| 1367 | | fn processDeath(self: *Self, inst: Air.Inst.Index) !void { |
| 1368 | | try self.inst_tracking.getPtr(inst).?.die(self, inst); |
| 1361 | fn processDeath(func: *Func, inst: Air.Inst.Index) !void { |
| 1362 | try func.inst_tracking.getPtr(inst).?.die(func, inst); |
| 1369 | 1363 | } |
| 1370 | 1364 | |
| 1371 | 1365 | /// Called when there are no operands, and the instruction is always unreferenced. |
| 1372 | | fn finishAirBookkeeping(self: *Self) void { |
| 1366 | fn finishAirBookkeeping(func: *Func) void { |
| 1373 | 1367 | if (std.debug.runtime_safety) { |
| 1374 | | self.air_bookkeeping += 1; |
| 1368 | func.air_bookkeeping += 1; |
| 1375 | 1369 | } |
| 1376 | 1370 | } |
| 1377 | 1371 | |
| 1378 | | fn finishAirResult(self: *Self, inst: Air.Inst.Index, result: MCValue) void { |
| 1379 | | if (self.liveness.isUnused(inst)) switch (result) { |
| 1372 | fn finishAirResult(func: *Func, inst: Air.Inst.Index, result: MCValue) void { |
| 1373 | if (func.liveness.isUnused(inst)) switch (result) { |
| 1380 | 1374 | .none, .dead, .unreach => {}, |
| 1381 | 1375 | else => unreachable, // Why didn't the result die? |
| 1382 | 1376 | } else { |
| 1383 | 1377 | tracking_log.debug("%{d} => {} (birth)", .{ inst, result }); |
| 1384 | | self.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(result)); |
| 1378 | func.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(result)); |
| 1385 | 1379 | // In some cases, an operand may be reused as the result. |
| 1386 | 1380 | // If that operand died and was a register, it was freed by |
| 1387 | 1381 | // processDeath, so we have to "re-allocate" the register. |
| 1388 | | self.getValueIfFree(result, inst); |
| 1382 | func.getValueIfFree(result, inst); |
| 1389 | 1383 | } |
| 1390 | | self.finishAirBookkeeping(); |
| 1384 | func.finishAirBookkeeping(); |
| 1391 | 1385 | } |
| 1392 | 1386 | |
| 1393 | 1387 | fn finishAir( |
| 1394 | | self: *Self, |
| 1388 | func: *Func, |
| 1395 | 1389 | inst: Air.Inst.Index, |
| 1396 | 1390 | result: MCValue, |
| 1397 | 1391 | operands: [Liveness.bpi - 1]Air.Inst.Ref, |
| 1398 | 1392 | ) !void { |
| 1399 | | var tomb_bits = self.liveness.getTombBits(inst); |
| 1393 | var tomb_bits = func.liveness.getTombBits(inst); |
| 1400 | 1394 | for (operands) |op| { |
| 1401 | 1395 | const dies = @as(u1, @truncate(tomb_bits)) != 0; |
| 1402 | 1396 | tomb_bits >>= 1; |
| 1403 | 1397 | if (!dies) continue; |
| 1404 | | try self.processDeath(op.toIndexAllowNone() orelse continue); |
| 1398 | try func.processDeath(op.toIndexAllowNone() orelse continue); |
| 1405 | 1399 | } |
| 1406 | | self.finishAirResult(inst, result); |
| 1400 | func.finishAirResult(inst, result); |
| 1407 | 1401 | } |
| 1408 | 1402 | |
| 1409 | 1403 | const FrameLayout = struct { |
| ... | ... | @@ -1412,7 +1406,7 @@ const FrameLayout = struct { |
| 1412 | 1406 | }; |
| 1413 | 1407 | |
| 1414 | 1408 | fn setFrameLoc( |
| 1415 | | self: *Self, |
| 1409 | func: *Func, |
| 1416 | 1410 | frame_index: FrameIndex, |
| 1417 | 1411 | base: Register, |
| 1418 | 1412 | offset: *i32, |
| ... | ... | @@ -1420,24 +1414,24 @@ fn setFrameLoc( |
| 1420 | 1414 | ) void { |
| 1421 | 1415 | const frame_i = @intFromEnum(frame_index); |
| 1422 | 1416 | if (aligned) { |
| 1423 | | const alignment: InternPool.Alignment = self.frame_allocs.items(.abi_align)[frame_i]; |
| 1417 | const alignment: InternPool.Alignment = func.frame_allocs.items(.abi_align)[frame_i]; |
| 1424 | 1418 | offset.* = if (math.sign(offset.*) < 0) |
| 1425 | 1419 | -1 * @as(i32, @intCast(alignment.backward(@intCast(@abs(offset.*))))) |
| 1426 | 1420 | else |
| 1427 | 1421 | @intCast(alignment.forward(@intCast(@abs(offset.*)))); |
| 1428 | 1422 | } |
| 1429 | | self.frame_locs.set(frame_i, .{ .base = base, .disp = offset.* }); |
| 1430 | | offset.* += self.frame_allocs.items(.abi_size)[frame_i]; |
| 1423 | func.frame_locs.set(frame_i, .{ .base = base, .disp = offset.* }); |
| 1424 | offset.* += func.frame_allocs.items(.abi_size)[frame_i]; |
| 1431 | 1425 | } |
| 1432 | 1426 | |
| 1433 | | fn computeFrameLayout(self: *Self) !FrameLayout { |
| 1434 | | const frame_allocs_len = self.frame_allocs.len; |
| 1435 | | try self.frame_locs.resize(self.gpa, frame_allocs_len); |
| 1436 | | const stack_frame_order = try self.gpa.alloc(FrameIndex, frame_allocs_len - FrameIndex.named_count); |
| 1437 | | defer self.gpa.free(stack_frame_order); |
| 1427 | fn computeFrameLayout(func: *Func) !FrameLayout { |
| 1428 | const frame_allocs_len = func.frame_allocs.len; |
| 1429 | try func.frame_locs.resize(func.gpa, frame_allocs_len); |
| 1430 | const stack_frame_order = try func.gpa.alloc(FrameIndex, frame_allocs_len - FrameIndex.named_count); |
| 1431 | defer func.gpa.free(stack_frame_order); |
| 1438 | 1432 | |
| 1439 | | const frame_size = self.frame_allocs.items(.abi_size); |
| 1440 | | const frame_align = self.frame_allocs.items(.abi_align); |
| 1433 | const frame_size = func.frame_allocs.items(.abi_size); |
| 1434 | const frame_align = func.frame_allocs.items(.abi_align); |
| 1441 | 1435 | |
| 1442 | 1436 | for (stack_frame_order, FrameIndex.named_count..) |*frame_order, frame_index| |
| 1443 | 1437 | frame_order.* = @enumFromInt(frame_index); |
| ... | ... | @@ -1455,7 +1449,7 @@ fn computeFrameLayout(self: *Self) !FrameLayout { |
| 1455 | 1449 | |
| 1456 | 1450 | var save_reg_list = Mir.RegisterList{}; |
| 1457 | 1451 | for (abi.Registers.all_preserved) |reg| { |
| 1458 | | if (self.register_manager.isRegAllocated(reg)) { |
| 1452 | if (func.register_manager.isRegAllocated(reg)) { |
| 1459 | 1453 | save_reg_list.push(&abi.Registers.all_preserved, reg); |
| 1460 | 1454 | } |
| 1461 | 1455 | } |
| ... | ... | @@ -1489,11 +1483,11 @@ fn computeFrameLayout(self: *Self) !FrameLayout { |
| 1489 | 1483 | |
| 1490 | 1484 | // store the ra at total_size - 8, so it's the very first thing in the stack |
| 1491 | 1485 | // relative to the fp |
| 1492 | | self.frame_locs.set( |
| 1486 | func.frame_locs.set( |
| 1493 | 1487 | @intFromEnum(FrameIndex.ret_addr), |
| 1494 | 1488 | .{ .base = .sp, .disp = acc_frame_size - 8 }, |
| 1495 | 1489 | ); |
| 1496 | | self.frame_locs.set( |
| 1490 | func.frame_locs.set( |
| 1497 | 1491 | @intFromEnum(FrameIndex.base_ptr), |
| 1498 | 1492 | .{ .base = .sp, .disp = acc_frame_size - 16 }, |
| 1499 | 1493 | ); |
| ... | ... | @@ -1502,11 +1496,11 @@ fn computeFrameLayout(self: *Self) !FrameLayout { |
| 1502 | 1496 | // not need to know the size of the first allocation. Stack offsets point at the "bottom" |
| 1503 | 1497 | // of variables. |
| 1504 | 1498 | var s0_offset: i32 = -acc_frame_size; |
| 1505 | | self.setFrameLoc(.stack_frame, .s0, &s0_offset, true); |
| 1506 | | for (stack_frame_order) |frame_index| self.setFrameLoc(frame_index, .s0, &s0_offset, true); |
| 1507 | | self.setFrameLoc(.args_frame, .s0, &s0_offset, true); |
| 1508 | | self.setFrameLoc(.call_frame, .s0, &s0_offset, true); |
| 1509 | | self.setFrameLoc(.spill_frame, .s0, &s0_offset, true); |
| 1499 | func.setFrameLoc(.stack_frame, .s0, &s0_offset, true); |
| 1500 | for (stack_frame_order) |frame_index| func.setFrameLoc(frame_index, .s0, &s0_offset, true); |
| 1501 | func.setFrameLoc(.args_frame, .s0, &s0_offset, true); |
| 1502 | func.setFrameLoc(.call_frame, .s0, &s0_offset, true); |
| 1503 | func.setFrameLoc(.spill_frame, .s0, &s0_offset, true); |
| 1510 | 1504 | |
| 1511 | 1505 | return .{ |
| 1512 | 1506 | .stack_adjust = @intCast(acc_frame_size), |
| ... | ... | @@ -1514,21 +1508,21 @@ fn computeFrameLayout(self: *Self) !FrameLayout { |
| 1514 | 1508 | }; |
| 1515 | 1509 | } |
| 1516 | 1510 | |
| 1517 | | fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void { |
| 1518 | | const table = &self.branch_stack.items[self.branch_stack.items.len - 1].inst_table; |
| 1519 | | try table.ensureUnusedCapacity(self.gpa, additional_count); |
| 1511 | fn ensureProcessDeathCapacity(func: *Func, additional_count: usize) !void { |
| 1512 | const table = &func.branch_stack.items[func.branch_stack.items.len - 1].inst_table; |
| 1513 | try table.ensureUnusedCapacity(func.gpa, additional_count); |
| 1520 | 1514 | } |
| 1521 | 1515 | |
| 1522 | | fn memSize(self: *Self, ty: Type) Memory.Size { |
| 1523 | | const mod = self.bin_file.comp.module.?; |
| 1516 | fn memSize(func: *Func, ty: Type) Memory.Size { |
| 1517 | const mod = func.bin_file.comp.module.?; |
| 1524 | 1518 | return switch (ty.zigTypeTag(mod)) { |
| 1525 | | .Float => Memory.Size.fromBitSize(ty.floatBits(self.target.*)), |
| 1519 | .Float => Memory.Size.fromBitSize(ty.floatBits(func.target.*)), |
| 1526 | 1520 | else => Memory.Size.fromByteSize(ty.abiSize(mod)), |
| 1527 | 1521 | }; |
| 1528 | 1522 | } |
| 1529 | 1523 | |
| 1530 | | fn splitType(self: *Self, ty: Type) ![2]Type { |
| 1531 | | const zcu = self.bin_file.comp.module.?; |
| 1524 | fn splitType(func: *Func, ty: Type) ![2]Type { |
| 1525 | const zcu = func.bin_file.comp.module.?; |
| 1532 | 1526 | const classes = mem.sliceTo(&abi.classifySystem(ty, zcu), .none); |
| 1533 | 1527 | var parts: [2]Type = undefined; |
| 1534 | 1528 | if (classes.len == 2) for (&parts, classes, 0..) |*part, class, part_i| { |
| ... | ... | @@ -1545,63 +1539,63 @@ fn splitType(self: *Self, ty: Type) ![2]Type { |
| 1545 | 1539 | }, |
| 1546 | 1540 | else => unreachable, |
| 1547 | 1541 | }, |
| 1548 | | else => return self.fail("TODO: splitType class {}", .{class}), |
| 1542 | else => return func.fail("TODO: splitType class {}", .{class}), |
| 1549 | 1543 | }; |
| 1550 | 1544 | } else if (parts[0].abiSize(zcu) + parts[1].abiSize(zcu) == ty.abiSize(zcu)) return parts; |
| 1551 | | return self.fail("TODO implement splitType for {}", .{ty.fmt(zcu)}); |
| 1545 | return func.fail("TODO implement splitType for {}", .{ty.fmt(zcu)}); |
| 1552 | 1546 | } |
| 1553 | 1547 | |
| 1554 | | fn symbolIndex(self: *Self) !u32 { |
| 1555 | | const zcu = self.bin_file.comp.module.?; |
| 1556 | | const decl_index = zcu.funcOwnerDeclIndex(self.func_index); |
| 1557 | | return switch (self.bin_file.tag) { |
| 1548 | fn symbolIndex(func: *Func) !u32 { |
| 1549 | const zcu = func.bin_file.comp.module.?; |
| 1550 | const decl_index = zcu.funcOwnerDeclIndex(func.func_index); |
| 1551 | return switch (func.bin_file.tag) { |
| 1558 | 1552 | .elf => blk: { |
| 1559 | | const elf_file = self.bin_file.cast(link.File.Elf).?; |
| 1553 | const elf_file = func.bin_file.cast(link.File.Elf).?; |
| 1560 | 1554 | const atom_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index); |
| 1561 | 1555 | break :blk atom_index; |
| 1562 | 1556 | }, |
| 1563 | | else => return self.fail("TODO symbolIndex {s}", .{@tagName(self.bin_file.tag)}), |
| 1557 | else => return func.fail("TODO symbolIndex {s}", .{@tagName(func.bin_file.tag)}), |
| 1564 | 1558 | }; |
| 1565 | 1559 | } |
| 1566 | 1560 | |
| 1567 | | fn allocFrameIndex(self: *Self, alloc: FrameAlloc) !FrameIndex { |
| 1568 | | const frame_allocs_slice = self.frame_allocs.slice(); |
| 1561 | fn allocFrameIndex(func: *Func, alloc: FrameAlloc) !FrameIndex { |
| 1562 | const frame_allocs_slice = func.frame_allocs.slice(); |
| 1569 | 1563 | const frame_size = frame_allocs_slice.items(.abi_size); |
| 1570 | 1564 | const frame_align = frame_allocs_slice.items(.abi_align); |
| 1571 | 1565 | |
| 1572 | 1566 | const stack_frame_align = &frame_align[@intFromEnum(FrameIndex.stack_frame)]; |
| 1573 | 1567 | stack_frame_align.* = stack_frame_align.max(alloc.abi_align); |
| 1574 | 1568 | |
| 1575 | | for (self.free_frame_indices.keys(), 0..) |frame_index, free_i| { |
| 1569 | for (func.free_frame_indices.keys(), 0..) |frame_index, free_i| { |
| 1576 | 1570 | const abi_size = frame_size[@intFromEnum(frame_index)]; |
| 1577 | 1571 | if (abi_size != alloc.abi_size) continue; |
| 1578 | 1572 | const abi_align = &frame_align[@intFromEnum(frame_index)]; |
| 1579 | 1573 | abi_align.* = abi_align.max(alloc.abi_align); |
| 1580 | 1574 | |
| 1581 | | _ = self.free_frame_indices.swapRemoveAt(free_i); |
| 1575 | _ = func.free_frame_indices.swapRemoveAt(free_i); |
| 1582 | 1576 | return frame_index; |
| 1583 | 1577 | } |
| 1584 | | const frame_index: FrameIndex = @enumFromInt(self.frame_allocs.len); |
| 1585 | | try self.frame_allocs.append(self.gpa, alloc); |
| 1578 | const frame_index: FrameIndex = @enumFromInt(func.frame_allocs.len); |
| 1579 | try func.frame_allocs.append(func.gpa, alloc); |
| 1586 | 1580 | log.debug("allocated frame {}", .{frame_index}); |
| 1587 | 1581 | return frame_index; |
| 1588 | 1582 | } |
| 1589 | 1583 | |
| 1590 | 1584 | /// Use a pointer instruction as the basis for allocating stack memory. |
| 1591 | | fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !FrameIndex { |
| 1592 | | const zcu = self.bin_file.comp.module.?; |
| 1593 | | const ptr_ty = self.typeOfIndex(inst); |
| 1585 | fn allocMemPtr(func: *Func, inst: Air.Inst.Index) !FrameIndex { |
| 1586 | const zcu = func.bin_file.comp.module.?; |
| 1587 | const ptr_ty = func.typeOfIndex(inst); |
| 1594 | 1588 | const val_ty = ptr_ty.childType(zcu); |
| 1595 | | return self.allocFrameIndex(FrameAlloc.init(.{ |
| 1589 | return func.allocFrameIndex(FrameAlloc.init(.{ |
| 1596 | 1590 | .size = math.cast(u32, val_ty.abiSize(zcu)) orelse { |
| 1597 | | return self.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(zcu)}); |
| 1591 | return func.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(zcu)}); |
| 1598 | 1592 | }, |
| 1599 | 1593 | .alignment = ptr_ty.ptrAlignment(zcu).max(.@"1"), |
| 1600 | 1594 | })); |
| 1601 | 1595 | } |
| 1602 | 1596 | |
| 1603 | | fn typeRegClass(self: *Self, ty: Type) abi.RegisterClass { |
| 1604 | | const zcu = self.bin_file.comp.module.?; |
| 1597 | fn typeRegClass(func: *Func, ty: Type) abi.RegisterClass { |
| 1598 | const zcu = func.bin_file.comp.module.?; |
| 1605 | 1599 | return switch (ty.zigTypeTag(zcu)) { |
| 1606 | 1600 | .Float => .float, |
| 1607 | 1601 | .Vector => @panic("TODO: typeRegClass for Vectors"), |
| ... | ... | @@ -1609,8 +1603,8 @@ fn typeRegClass(self: *Self, ty: Type) abi.RegisterClass { |
| 1609 | 1603 | }; |
| 1610 | 1604 | } |
| 1611 | 1605 | |
| 1612 | | fn regGeneralClassForType(self: *Self, ty: Type) RegisterManager.RegisterBitSet { |
| 1613 | | const zcu = self.bin_file.comp.module.?; |
| 1606 | fn regGeneralClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet { |
| 1607 | const zcu = func.bin_file.comp.module.?; |
| 1614 | 1608 | return switch (ty.zigTypeTag(zcu)) { |
| 1615 | 1609 | .Float => abi.Registers.Float.general_purpose, |
| 1616 | 1610 | .Vector => @panic("TODO: regGeneralClassForType for Vectors"), |
| ... | ... | @@ -1618,8 +1612,8 @@ fn regGeneralClassForType(self: *Self, ty: Type) RegisterManager.RegisterBitSet |
| 1618 | 1612 | }; |
| 1619 | 1613 | } |
| 1620 | 1614 | |
| 1621 | | fn regTempClassForType(self: *Self, ty: Type) RegisterManager.RegisterBitSet { |
| 1622 | | const zcu = self.bin_file.comp.module.?; |
| 1615 | fn regTempClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet { |
| 1616 | const zcu = func.bin_file.comp.module.?; |
| 1623 | 1617 | return switch (ty.zigTypeTag(zcu)) { |
| 1624 | 1618 | .Float => abi.Registers.Float.temporary, |
| 1625 | 1619 | .Vector => @panic("TODO: regTempClassForType for Vectors"), |
| ... | ... | @@ -1627,12 +1621,12 @@ fn regTempClassForType(self: *Self, ty: Type) RegisterManager.RegisterBitSet { |
| 1627 | 1621 | }; |
| 1628 | 1622 | } |
| 1629 | 1623 | |
| 1630 | | fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue { |
| 1631 | | const zcu = self.bin_file.comp.module.?; |
| 1632 | | const elem_ty = self.typeOfIndex(inst); |
| 1624 | fn allocRegOrMem(func: *Func, inst: Air.Inst.Index, reg_ok: bool) !MCValue { |
| 1625 | const zcu = func.bin_file.comp.module.?; |
| 1626 | const elem_ty = func.typeOfIndex(inst); |
| 1633 | 1627 | |
| 1634 | 1628 | const abi_size = math.cast(u32, elem_ty.abiSize(zcu)) orelse { |
| 1635 | | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(zcu)}); |
| 1629 | return func.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(zcu)}); |
| 1636 | 1630 | }; |
| 1637 | 1631 | |
| 1638 | 1632 | const min_size: u32 = switch (elem_ty.zigTypeTag(zcu)) { |
| ... | ... | @@ -1642,20 +1636,20 @@ fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue { |
| 1642 | 1636 | }; |
| 1643 | 1637 | |
| 1644 | 1638 | if (reg_ok and abi_size <= min_size) { |
| 1645 | | if (self.register_manager.tryAllocReg(inst, self.regGeneralClassForType(elem_ty))) |reg| { |
| 1639 | if (func.register_manager.tryAllocReg(inst, func.regGeneralClassForType(elem_ty))) |reg| { |
| 1646 | 1640 | return .{ .register = reg }; |
| 1647 | 1641 | } |
| 1648 | 1642 | } |
| 1649 | 1643 | |
| 1650 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(elem_ty, zcu)); |
| 1644 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(elem_ty, zcu)); |
| 1651 | 1645 | return .{ .load_frame = .{ .index = frame_index } }; |
| 1652 | 1646 | } |
| 1653 | 1647 | |
| 1654 | 1648 | /// Allocates a register from the general purpose set and returns the Register and the Lock. |
| 1655 | 1649 | /// |
| 1656 | 1650 | /// Up to the caller to unlock the register later. |
| 1657 | | fn allocReg(self: *Self, reg_class: abi.RegisterClass) !struct { Register, RegisterLock } { |
| 1658 | | if (reg_class == .float and !self.hasFeature(.f)) |
| 1651 | fn allocReg(func: *Func, reg_class: abi.RegisterClass) !struct { Register, RegisterLock } { |
| 1652 | if (reg_class == .float and !func.hasFeature(.f)) |
| 1659 | 1653 | std.debug.panic("allocReg class == float where F isn't enabled", .{}); |
| 1660 | 1654 | |
| 1661 | 1655 | const class = switch (reg_class) { |
| ... | ... | @@ -1663,8 +1657,8 @@ fn allocReg(self: *Self, reg_class: abi.RegisterClass) !struct { Register, Regis |
| 1663 | 1657 | .float => abi.Registers.Float.general_purpose, |
| 1664 | 1658 | }; |
| 1665 | 1659 | |
| 1666 | | const reg = try self.register_manager.allocReg(null, class); |
| 1667 | | const lock = self.register_manager.lockRegAssumeUnused(reg); |
| 1660 | const reg = try func.register_manager.allocReg(null, class); |
| 1661 | const lock = func.register_manager.lockRegAssumeUnused(reg); |
| 1668 | 1662 | return .{ reg, lock }; |
| 1669 | 1663 | } |
| 1670 | 1664 | |
| ... | ... | @@ -1677,8 +1671,8 @@ const PromoteOptions = struct { |
| 1677 | 1671 | |
| 1678 | 1672 | /// Similar to `allocReg` but will copy the MCValue into the Register unless `operand` is already |
| 1679 | 1673 | /// a register, in which case it will return a possible lock to that register. |
| 1680 | | fn promoteReg(self: *Self, ty: Type, operand: MCValue, options: PromoteOptions) !struct { Register, ?RegisterLock } { |
| 1681 | | const zcu = self.bin_file.comp.module.?; |
| 1674 | fn promoteReg(func: *Func, ty: Type, operand: MCValue, options: PromoteOptions) !struct { Register, ?RegisterLock } { |
| 1675 | const zcu = func.bin_file.comp.module.?; |
| 1682 | 1676 | const bit_size = ty.bitSize(zcu); |
| 1683 | 1677 | |
| 1684 | 1678 | if (operand == .register) { |
| ... | ... | @@ -1687,7 +1681,7 @@ fn promoteReg(self: *Self, ty: Type, operand: MCValue, options: PromoteOptions) |
| 1687 | 1681 | // we make sure to emit the truncate manually because binOp will call this function |
| 1688 | 1682 | // and it could cause an infinite loop |
| 1689 | 1683 | |
| 1690 | | _ = try self.addInst(.{ |
| 1684 | _ = try func.addInst(.{ |
| 1691 | 1685 | .tag = .slli, |
| 1692 | 1686 | .ops = .rri, |
| 1693 | 1687 | .data = .{ |
| ... | ... | @@ -1699,7 +1693,7 @@ fn promoteReg(self: *Self, ty: Type, operand: MCValue, options: PromoteOptions) |
| 1699 | 1693 | }, |
| 1700 | 1694 | }); |
| 1701 | 1695 | |
| 1702 | | _ = try self.addInst(.{ |
| 1696 | _ = try func.addInst(.{ |
| 1703 | 1697 | .tag = .srli, |
| 1704 | 1698 | .ops = .rri, |
| 1705 | 1699 | .data = .{ |
| ... | ... | @@ -1712,13 +1706,13 @@ fn promoteReg(self: *Self, ty: Type, operand: MCValue, options: PromoteOptions) |
| 1712 | 1706 | }); |
| 1713 | 1707 | } |
| 1714 | 1708 | |
| 1715 | | return .{ op_reg, self.register_manager.lockReg(operand.register) }; |
| 1709 | return .{ op_reg, func.register_manager.lockReg(operand.register) }; |
| 1716 | 1710 | } |
| 1717 | 1711 | |
| 1718 | | const reg, const lock = try self.allocReg(self.typeRegClass(ty)); |
| 1712 | const reg, const lock = try func.allocReg(func.typeRegClass(ty)); |
| 1719 | 1713 | |
| 1720 | 1714 | if (options.zero and reg.class() == .int) { |
| 1721 | | _ = try self.addInst(.{ |
| 1715 | _ = try func.addInst(.{ |
| 1722 | 1716 | .tag = .pseudo, |
| 1723 | 1717 | .ops = .pseudo_mv, |
| 1724 | 1718 | .data = .{ .rr = .{ |
| ... | ... | @@ -1728,26 +1722,26 @@ fn promoteReg(self: *Self, ty: Type, operand: MCValue, options: PromoteOptions) |
| 1728 | 1722 | }); |
| 1729 | 1723 | } |
| 1730 | 1724 | |
| 1731 | | try self.genSetReg(ty, reg, operand); |
| 1725 | try func.genSetReg(ty, reg, operand); |
| 1732 | 1726 | return .{ reg, lock }; |
| 1733 | 1727 | } |
| 1734 | 1728 | |
| 1735 | | fn elemOffset(self: *Self, index_ty: Type, index: MCValue, elem_size: u64) !Register { |
| 1729 | fn elemOffset(func: *Func, index_ty: Type, index: MCValue, elem_size: u64) !Register { |
| 1736 | 1730 | const reg: Register = blk: { |
| 1737 | 1731 | switch (index) { |
| 1738 | 1732 | .immediate => |imm| { |
| 1739 | 1733 | // Optimisation: if index MCValue is an immediate, we can multiply in `comptime` |
| 1740 | 1734 | // and set the register directly to the scaled offset as an immediate. |
| 1741 | | const reg = try self.register_manager.allocReg(null, self.regGeneralClassForType(index_ty)); |
| 1742 | | try self.genSetReg(index_ty, reg, .{ .immediate = imm * elem_size }); |
| 1735 | const reg = try func.register_manager.allocReg(null, func.regGeneralClassForType(index_ty)); |
| 1736 | try func.genSetReg(index_ty, reg, .{ .immediate = imm * elem_size }); |
| 1743 | 1737 | break :blk reg; |
| 1744 | 1738 | }, |
| 1745 | 1739 | else => { |
| 1746 | | const reg = try self.copyToTmpRegister(index_ty, index); |
| 1747 | | const lock = self.register_manager.lockRegAssumeUnused(reg); |
| 1748 | | defer self.register_manager.unlockReg(lock); |
| 1740 | const reg = try func.copyToTmpRegister(index_ty, index); |
| 1741 | const lock = func.register_manager.lockRegAssumeUnused(reg); |
| 1742 | defer func.register_manager.unlockReg(lock); |
| 1749 | 1743 | |
| 1750 | | const result = try self.binOp( |
| 1744 | const result = try func.binOp( |
| 1751 | 1745 | .mul, |
| 1752 | 1746 | .{ .register = reg }, |
| 1753 | 1747 | index_ty, |
| ... | ... | @@ -1761,72 +1755,72 @@ fn elemOffset(self: *Self, index_ty: Type, index: MCValue, elem_size: u64) !Regi |
| 1761 | 1755 | return reg; |
| 1762 | 1756 | } |
| 1763 | 1757 | |
| 1764 | | pub fn spillInstruction(self: *Self, reg: Register, inst: Air.Inst.Index) !void { |
| 1765 | | const tracking = self.inst_tracking.getPtr(inst) orelse return; |
| 1758 | pub fn spillInstruction(func: *Func, reg: Register, inst: Air.Inst.Index) !void { |
| 1759 | const tracking = func.inst_tracking.getPtr(inst) orelse return; |
| 1766 | 1760 | for (tracking.getRegs()) |tracked_reg| { |
| 1767 | 1761 | if (tracked_reg.id() == reg.id()) break; |
| 1768 | 1762 | } else unreachable; // spilled reg not tracked with spilled instruciton |
| 1769 | | try tracking.spill(self, inst); |
| 1770 | | try tracking.trackSpill(self, inst); |
| 1763 | try tracking.spill(func, inst); |
| 1764 | try tracking.trackSpill(func, inst); |
| 1771 | 1765 | } |
| 1772 | 1766 | |
| 1773 | 1767 | /// Copies a value to a register without tracking the register. The register is not considered |
| 1774 | 1768 | /// allocated. A second call to `copyToTmpRegister` may return the same register. |
| 1775 | 1769 | /// This can have a side effect of spilling instructions to the stack to free up a register. |
| 1776 | | fn copyToTmpRegister(self: *Self, ty: Type, mcv: MCValue) !Register { |
| 1777 | | log.debug("copyToTmpRegister ty: {}", .{ty.fmt(self.bin_file.comp.module.?)}); |
| 1778 | | const reg = try self.register_manager.allocReg(null, self.regTempClassForType(ty)); |
| 1779 | | try self.genSetReg(ty, reg, mcv); |
| 1770 | fn copyToTmpRegister(func: *Func, ty: Type, mcv: MCValue) !Register { |
| 1771 | log.debug("copyToTmpRegister ty: {}", .{ty.fmt(func.bin_file.comp.module.?)}); |
| 1772 | const reg = try func.register_manager.allocReg(null, func.regTempClassForType(ty)); |
| 1773 | try func.genSetReg(ty, reg, mcv); |
| 1780 | 1774 | return reg; |
| 1781 | 1775 | } |
| 1782 | 1776 | |
| 1783 | 1777 | /// Allocates a new register and copies `mcv` into it. |
| 1784 | 1778 | /// `reg_owner` is the instruction that gets associated with the register in the register table. |
| 1785 | 1779 | /// This can have a side effect of spilling instructions to the stack to free up a register. |
| 1786 | | fn copyToNewRegister(self: *Self, reg_owner: Air.Inst.Index, mcv: MCValue) !MCValue { |
| 1787 | | const ty = self.typeOfIndex(reg_owner); |
| 1788 | | const reg = try self.register_manager.allocReg(reg_owner, self.regGeneralClassForType(ty)); |
| 1789 | | try self.genSetReg(self.typeOfIndex(reg_owner), reg, mcv); |
| 1780 | fn copyToNewRegister(func: *Func, reg_owner: Air.Inst.Index, mcv: MCValue) !MCValue { |
| 1781 | const ty = func.typeOfIndex(reg_owner); |
| 1782 | const reg = try func.register_manager.allocReg(reg_owner, func.regGeneralClassForType(ty)); |
| 1783 | try func.genSetReg(func.typeOfIndex(reg_owner), reg, mcv); |
| 1790 | 1784 | return MCValue{ .register = reg }; |
| 1791 | 1785 | } |
| 1792 | 1786 | |
| 1793 | | fn airAlloc(self: *Self, inst: Air.Inst.Index) !void { |
| 1794 | | const result = MCValue{ .lea_frame = .{ .index = try self.allocMemPtr(inst) } }; |
| 1795 | | return self.finishAir(inst, result, .{ .none, .none, .none }); |
| 1787 | fn airAlloc(func: *Func, inst: Air.Inst.Index) !void { |
| 1788 | const result = MCValue{ .lea_frame = .{ .index = try func.allocMemPtr(inst) } }; |
| 1789 | return func.finishAir(inst, result, .{ .none, .none, .none }); |
| 1796 | 1790 | } |
| 1797 | 1791 | |
| 1798 | | fn airRetPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 1799 | | const result: MCValue = switch (self.ret_mcv.long) { |
| 1800 | | .none => .{ .lea_frame = .{ .index = try self.allocMemPtr(inst) } }, |
| 1792 | fn airRetPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 1793 | const result: MCValue = switch (func.ret_mcv.long) { |
| 1794 | .none => .{ .lea_frame = .{ .index = try func.allocMemPtr(inst) } }, |
| 1801 | 1795 | .load_frame => .{ .register_offset = .{ |
| 1802 | | .reg = (try self.copyToNewRegister( |
| 1796 | .reg = (try func.copyToNewRegister( |
| 1803 | 1797 | inst, |
| 1804 | | self.ret_mcv.long, |
| 1798 | func.ret_mcv.long, |
| 1805 | 1799 | )).register, |
| 1806 | | .off = self.ret_mcv.short.indirect.off, |
| 1800 | .off = func.ret_mcv.short.indirect.off, |
| 1807 | 1801 | } }, |
| 1808 | | else => |t| return self.fail("TODO: airRetPtr {s}", .{@tagName(t)}), |
| 1802 | else => |t| return func.fail("TODO: airRetPtr {s}", .{@tagName(t)}), |
| 1809 | 1803 | }; |
| 1810 | | return self.finishAir(inst, result, .{ .none, .none, .none }); |
| 1804 | return func.finishAir(inst, result, .{ .none, .none, .none }); |
| 1811 | 1805 | } |
| 1812 | 1806 | |
| 1813 | | fn airFptrunc(self: *Self, inst: Air.Inst.Index) !void { |
| 1814 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1815 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airFptrunc for {}", .{self.target.cpu.arch}); |
| 1816 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1807 | fn airFptrunc(func: *Func, inst: Air.Inst.Index) !void { |
| 1808 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1809 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airFptrunc for {}", .{func.target.cpu.arch}); |
| 1810 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1817 | 1811 | } |
| 1818 | 1812 | |
| 1819 | | fn airFpext(self: *Self, inst: Air.Inst.Index) !void { |
| 1820 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1821 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airFpext for {}", .{self.target.cpu.arch}); |
| 1822 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1813 | fn airFpext(func: *Func, inst: Air.Inst.Index) !void { |
| 1814 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1815 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airFpext for {}", .{func.target.cpu.arch}); |
| 1816 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1823 | 1817 | } |
| 1824 | 1818 | |
| 1825 | | fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 1826 | | const zcu = self.bin_file.comp.module.?; |
| 1827 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1828 | | const src_ty = self.typeOf(ty_op.operand); |
| 1829 | | const dst_ty = self.typeOfIndex(inst); |
| 1819 | fn airIntCast(func: *Func, inst: Air.Inst.Index) !void { |
| 1820 | const zcu = func.bin_file.comp.module.?; |
| 1821 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1822 | const src_ty = func.typeOf(ty_op.operand); |
| 1823 | const dst_ty = func.typeOfIndex(inst); |
| 1830 | 1824 | |
| 1831 | 1825 | const result: MCValue = result: { |
| 1832 | 1826 | const src_int_info = src_ty.intInfo(zcu); |
| ... | ... | @@ -1834,20 +1828,20 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 1834 | 1828 | |
| 1835 | 1829 | const min_ty = if (dst_int_info.bits < src_int_info.bits) dst_ty else src_ty; |
| 1836 | 1830 | |
| 1837 | | const src_mcv = try self.resolveInst(ty_op.operand); |
| 1831 | const src_mcv = try func.resolveInst(ty_op.operand); |
| 1838 | 1832 | |
| 1839 | 1833 | const src_storage_bits: u16 = switch (src_mcv) { |
| 1840 | 1834 | .register => 64, |
| 1841 | 1835 | .load_frame => src_int_info.bits, |
| 1842 | | else => return self.fail("airIntCast from {s}", .{@tagName(src_mcv)}), |
| 1836 | else => return func.fail("airIntCast from {s}", .{@tagName(src_mcv)}), |
| 1843 | 1837 | }; |
| 1844 | 1838 | |
| 1845 | 1839 | const dst_mcv = if (dst_int_info.bits <= src_storage_bits and |
| 1846 | 1840 | math.divCeil(u16, dst_int_info.bits, 64) catch unreachable == |
| 1847 | 1841 | math.divCeil(u32, src_storage_bits, 64) catch unreachable and |
| 1848 | | self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: { |
| 1849 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 1850 | | try self.genCopy(min_ty, dst_mcv, src_mcv); |
| 1842 | func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: { |
| 1843 | const dst_mcv = try func.allocRegOrMem(inst, true); |
| 1844 | try func.genCopy(min_ty, dst_mcv, src_mcv); |
| 1851 | 1845 | break :dst dst_mcv; |
| 1852 | 1846 | }; |
| 1853 | 1847 | |
| ... | ... | @@ -1858,53 +1852,53 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 1858 | 1852 | break :result null; // TODO |
| 1859 | 1853 | |
| 1860 | 1854 | break :result dst_mcv; |
| 1861 | | } orelse return self.fail("TODO implement airIntCast from {} to {}", .{ |
| 1855 | } orelse return func.fail("TODO implement airIntCast from {} to {}", .{ |
| 1862 | 1856 | src_ty.fmt(zcu), dst_ty.fmt(zcu), |
| 1863 | 1857 | }); |
| 1864 | 1858 | |
| 1865 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1859 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1866 | 1860 | } |
| 1867 | 1861 | |
| 1868 | | fn airTrunc(self: *Self, inst: Air.Inst.Index) !void { |
| 1869 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1870 | | if (self.liveness.isUnused(inst)) |
| 1871 | | return self.finishAir(inst, .unreach, .{ ty_op.operand, .none, .none }); |
| 1862 | fn airTrunc(func: *Func, inst: Air.Inst.Index) !void { |
| 1863 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1864 | if (func.liveness.isUnused(inst)) |
| 1865 | return func.finishAir(inst, .unreach, .{ ty_op.operand, .none, .none }); |
| 1872 | 1866 | |
| 1873 | | const operand = try self.resolveInst(ty_op.operand); |
| 1867 | const operand = try func.resolveInst(ty_op.operand); |
| 1874 | 1868 | _ = operand; |
| 1875 | | return self.fail("TODO implement trunc for {}", .{self.target.cpu.arch}); |
| 1876 | | // return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1869 | return func.fail("TODO implement trunc for {}", .{func.target.cpu.arch}); |
| 1870 | // return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1877 | 1871 | } |
| 1878 | 1872 | |
| 1879 | | fn airIntFromBool(self: *Self, inst: Air.Inst.Index) !void { |
| 1880 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 1881 | | const operand = try self.resolveInst(un_op); |
| 1882 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else operand; |
| 1883 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 1873 | fn airIntFromBool(func: *Func, inst: Air.Inst.Index) !void { |
| 1874 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 1875 | const operand = try func.resolveInst(un_op); |
| 1876 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else operand; |
| 1877 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 1884 | 1878 | } |
| 1885 | 1879 | |
| 1886 | | fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 1887 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1888 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 1889 | | const zcu = self.bin_file.comp.module.?; |
| 1880 | fn airNot(func: *Func, inst: Air.Inst.Index) !void { |
| 1881 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1882 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 1883 | const zcu = func.bin_file.comp.module.?; |
| 1890 | 1884 | |
| 1891 | | const operand = try self.resolveInst(ty_op.operand); |
| 1892 | | const ty = self.typeOf(ty_op.operand); |
| 1885 | const operand = try func.resolveInst(ty_op.operand); |
| 1886 | const ty = func.typeOf(ty_op.operand); |
| 1893 | 1887 | |
| 1894 | 1888 | switch (ty.zigTypeTag(zcu)) { |
| 1895 | 1889 | .Bool => { |
| 1896 | 1890 | const operand_reg = blk: { |
| 1897 | 1891 | if (operand == .register) break :blk operand.register; |
| 1898 | | break :blk try self.copyToTmpRegister(ty, operand); |
| 1892 | break :blk try func.copyToTmpRegister(ty, operand); |
| 1899 | 1893 | }; |
| 1900 | 1894 | |
| 1901 | 1895 | const dst_reg: Register = |
| 1902 | | if (self.reuseOperand(inst, ty_op.operand, 0, operand) and operand == .register) |
| 1896 | if (func.reuseOperand(inst, ty_op.operand, 0, operand) and operand == .register) |
| 1903 | 1897 | operand.register |
| 1904 | 1898 | else |
| 1905 | | (try self.allocRegOrMem(inst, true)).register; |
| 1899 | (try func.allocRegOrMem(inst, true)).register; |
| 1906 | 1900 | |
| 1907 | | _ = try self.addInst(.{ |
| 1901 | _ = try func.addInst(.{ |
| 1908 | 1902 | .tag = .pseudo, |
| 1909 | 1903 | .ops = .pseudo_not, |
| 1910 | 1904 | .data = .{ |
| ... | ... | @@ -1917,59 +1911,59 @@ fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 1917 | 1911 | |
| 1918 | 1912 | break :result .{ .register = dst_reg }; |
| 1919 | 1913 | }, |
| 1920 | | .Int => return self.fail("TODO: airNot ints", .{}), |
| 1914 | .Int => return func.fail("TODO: airNot ints", .{}), |
| 1921 | 1915 | else => unreachable, |
| 1922 | 1916 | } |
| 1923 | 1917 | }; |
| 1924 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1918 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1925 | 1919 | } |
| 1926 | 1920 | |
| 1927 | 1921 | fn airMinMax( |
| 1928 | | self: *Self, |
| 1922 | func: *Func, |
| 1929 | 1923 | inst: Air.Inst.Index, |
| 1930 | 1924 | comptime tag: enum { |
| 1931 | 1925 | max, |
| 1932 | 1926 | min, |
| 1933 | 1927 | }, |
| 1934 | 1928 | ) !void { |
| 1935 | | const zcu = self.bin_file.comp.module.?; |
| 1936 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 1929 | const zcu = func.bin_file.comp.module.?; |
| 1930 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 1937 | 1931 | |
| 1938 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 1939 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 1940 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 1941 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 1942 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 1932 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 1933 | const lhs = try func.resolveInst(bin_op.lhs); |
| 1934 | const rhs = try func.resolveInst(bin_op.rhs); |
| 1935 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 1936 | const rhs_ty = func.typeOf(bin_op.rhs); |
| 1943 | 1937 | |
| 1944 | 1938 | const int_info = lhs_ty.intInfo(zcu); |
| 1945 | 1939 | |
| 1946 | | if (int_info.bits > 64) return self.fail("TODO: > 64 bit @min", .{}); |
| 1940 | if (int_info.bits > 64) return func.fail("TODO: > 64 bit @min", .{}); |
| 1947 | 1941 | |
| 1948 | 1942 | const lhs_reg, const lhs_lock = blk: { |
| 1949 | | if (lhs == .register) break :blk .{ lhs.register, self.register_manager.lockReg(lhs.register) }; |
| 1943 | if (lhs == .register) break :blk .{ lhs.register, func.register_manager.lockReg(lhs.register) }; |
| 1950 | 1944 | |
| 1951 | | const lhs_reg, const lhs_lock = try self.allocReg(.int); |
| 1952 | | try self.genSetReg(lhs_ty, lhs_reg, lhs); |
| 1945 | const lhs_reg, const lhs_lock = try func.allocReg(.int); |
| 1946 | try func.genSetReg(lhs_ty, lhs_reg, lhs); |
| 1953 | 1947 | break :blk .{ lhs_reg, lhs_lock }; |
| 1954 | 1948 | }; |
| 1955 | | defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 1949 | defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 1956 | 1950 | |
| 1957 | 1951 | const rhs_reg, const rhs_lock = blk: { |
| 1958 | | if (rhs == .register) break :blk .{ rhs.register, self.register_manager.lockReg(rhs.register) }; |
| 1952 | if (rhs == .register) break :blk .{ rhs.register, func.register_manager.lockReg(rhs.register) }; |
| 1959 | 1953 | |
| 1960 | | const rhs_reg, const rhs_lock = try self.allocReg(.int); |
| 1961 | | try self.genSetReg(rhs_ty, rhs_reg, rhs); |
| 1954 | const rhs_reg, const rhs_lock = try func.allocReg(.int); |
| 1955 | try func.genSetReg(rhs_ty, rhs_reg, rhs); |
| 1962 | 1956 | break :blk .{ rhs_reg, rhs_lock }; |
| 1963 | 1957 | }; |
| 1964 | | defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 1958 | defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 1965 | 1959 | |
| 1966 | | const mask_reg, const mask_lock = try self.allocReg(.int); |
| 1967 | | defer self.register_manager.unlockReg(mask_lock); |
| 1960 | const mask_reg, const mask_lock = try func.allocReg(.int); |
| 1961 | defer func.register_manager.unlockReg(mask_lock); |
| 1968 | 1962 | |
| 1969 | | const result_reg, const result_lock = try self.allocReg(.int); |
| 1970 | | defer self.register_manager.unlockReg(result_lock); |
| 1963 | const result_reg, const result_lock = try func.allocReg(.int); |
| 1964 | defer func.register_manager.unlockReg(result_lock); |
| 1971 | 1965 | |
| 1972 | | _ = try self.addInst(.{ |
| 1966 | _ = try func.addInst(.{ |
| 1973 | 1967 | .tag = if (int_info.signedness == .unsigned) .sltu else .slt, |
| 1974 | 1968 | .ops = .rrr, |
| 1975 | 1969 | .data = .{ .r_type = .{ |
| ... | ... | @@ -1979,7 +1973,7 @@ fn airMinMax( |
| 1979 | 1973 | } }, |
| 1980 | 1974 | }); |
| 1981 | 1975 | |
| 1982 | | _ = try self.addInst(.{ |
| 1976 | _ = try func.addInst(.{ |
| 1983 | 1977 | .tag = .sub, |
| 1984 | 1978 | .ops = .rrr, |
| 1985 | 1979 | .data = .{ .r_type = .{ |
| ... | ... | @@ -1989,7 +1983,7 @@ fn airMinMax( |
| 1989 | 1983 | } }, |
| 1990 | 1984 | }); |
| 1991 | 1985 | |
| 1992 | | _ = try self.addInst(.{ |
| 1986 | _ = try func.addInst(.{ |
| 1993 | 1987 | .tag = .xor, |
| 1994 | 1988 | .ops = .rrr, |
| 1995 | 1989 | .data = .{ .r_type = .{ |
| ... | ... | @@ -1999,7 +1993,7 @@ fn airMinMax( |
| 1999 | 1993 | } }, |
| 2000 | 1994 | }); |
| 2001 | 1995 | |
| 2002 | | _ = try self.addInst(.{ |
| 1996 | _ = try func.addInst(.{ |
| 2003 | 1997 | .tag = .@"and", |
| 2004 | 1998 | .ops = .rrr, |
| 2005 | 1999 | .data = .{ .r_type = .{ |
| ... | ... | @@ -2009,7 +2003,7 @@ fn airMinMax( |
| 2009 | 2003 | } }, |
| 2010 | 2004 | }); |
| 2011 | 2005 | |
| 2012 | | _ = try self.addInst(.{ |
| 2006 | _ = try func.addInst(.{ |
| 2013 | 2007 | .tag = .xor, |
| 2014 | 2008 | .ops = .rrr, |
| 2015 | 2009 | .data = .{ .r_type = .{ |
| ... | ... | @@ -2021,22 +2015,22 @@ fn airMinMax( |
| 2021 | 2015 | |
| 2022 | 2016 | break :result .{ .register = result_reg }; |
| 2023 | 2017 | }; |
| 2024 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2018 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2025 | 2019 | } |
| 2026 | 2020 | |
| 2027 | | fn airSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 2028 | | const zcu = self.bin_file.comp.module.?; |
| 2029 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2030 | | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2021 | fn airSlice(func: *Func, inst: Air.Inst.Index) !void { |
| 2022 | const zcu = func.bin_file.comp.module.?; |
| 2023 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2024 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2031 | 2025 | |
| 2032 | | const slice_ty = self.typeOfIndex(inst); |
| 2033 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(slice_ty, zcu)); |
| 2026 | const slice_ty = func.typeOfIndex(inst); |
| 2027 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(slice_ty, zcu)); |
| 2034 | 2028 | |
| 2035 | | const ptr_ty = self.typeOf(bin_op.lhs); |
| 2036 | | try self.genSetMem(.{ .frame = frame_index }, 0, ptr_ty, .{ .air_ref = bin_op.lhs }); |
| 2029 | const ptr_ty = func.typeOf(bin_op.lhs); |
| 2030 | try func.genSetMem(.{ .frame = frame_index }, 0, ptr_ty, .{ .air_ref = bin_op.lhs }); |
| 2037 | 2031 | |
| 2038 | | const len_ty = self.typeOf(bin_op.rhs); |
| 2039 | | try self.genSetMem( |
| 2032 | const len_ty = func.typeOf(bin_op.rhs); |
| 2033 | try func.genSetMem( |
| 2040 | 2034 | .{ .frame = frame_index }, |
| 2041 | 2035 | @intCast(ptr_ty.abiSize(zcu)), |
| 2042 | 2036 | len_ty, |
| ... | ... | @@ -2044,31 +2038,31 @@ fn airSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 2044 | 2038 | ); |
| 2045 | 2039 | |
| 2046 | 2040 | const result = MCValue{ .load_frame = .{ .index = frame_index } }; |
| 2047 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2041 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2048 | 2042 | } |
| 2049 | 2043 | |
| 2050 | | fn airBinOp(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 2051 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2052 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2053 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2054 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2055 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2044 | fn airBinOp(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 2045 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2046 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2047 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2048 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 2049 | const rhs_ty = func.typeOf(bin_op.rhs); |
| 2056 | 2050 | |
| 2057 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2058 | | break :result try self.binOp(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2051 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2052 | break :result try func.binOp(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2059 | 2053 | }; |
| 2060 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2054 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2061 | 2055 | } |
| 2062 | 2056 | |
| 2063 | 2057 | fn binOp( |
| 2064 | | self: *Self, |
| 2058 | func: *Func, |
| 2065 | 2059 | tag: Air.Inst.Tag, |
| 2066 | 2060 | lhs: MCValue, |
| 2067 | 2061 | lhs_ty: Type, |
| 2068 | 2062 | rhs: MCValue, |
| 2069 | 2063 | rhs_ty: Type, |
| 2070 | 2064 | ) InnerError!MCValue { |
| 2071 | | const zcu = self.bin_file.comp.module.?; |
| 2065 | const zcu = func.bin_file.comp.module.?; |
| 2072 | 2066 | |
| 2073 | 2067 | switch (tag) { |
| 2074 | 2068 | // Arithmetic operations on integers and floats |
| ... | ... | @@ -2087,23 +2081,23 @@ fn binOp( |
| 2087 | 2081 | switch (lhs_ty.zigTypeTag(zcu)) { |
| 2088 | 2082 | .Float => { |
| 2089 | 2083 | const float_bits = lhs_ty.floatBits(zcu.getTarget()); |
| 2090 | | const float_reg_bits: u32 = if (self.hasFeature(.d)) 64 else 32; |
| 2084 | const float_reg_bits: u32 = if (func.hasFeature(.d)) 64 else 32; |
| 2091 | 2085 | if (float_bits <= float_reg_bits) { |
| 2092 | | return self.binOpFloat(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2086 | return func.binOpFloat(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2093 | 2087 | } else { |
| 2094 | | return self.fail("TODO: binary operations for floats with bits > {d}", .{float_reg_bits}); |
| 2088 | return func.fail("TODO: binary operations for floats with bits > {d}", .{float_reg_bits}); |
| 2095 | 2089 | } |
| 2096 | 2090 | }, |
| 2097 | | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 2091 | .Vector => return func.fail("TODO binary operations on vectors", .{}), |
| 2098 | 2092 | .Int, .Enum, .ErrorSet => { |
| 2099 | 2093 | const int_info = lhs_ty.intInfo(zcu); |
| 2100 | 2094 | if (int_info.bits <= 64) { |
| 2101 | | return self.binOpRegister(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2095 | return func.binOpRegister(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2102 | 2096 | } else { |
| 2103 | | return self.fail("TODO binary operations on int with bits > 64", .{}); |
| 2097 | return func.fail("TODO binary operations on int with bits > 64", .{}); |
| 2104 | 2098 | } |
| 2105 | 2099 | }, |
| 2106 | | else => |x| return self.fail("TOOD: binOp {s}", .{@tagName(x)}), |
| 2100 | else => |x| return func.fail("TOOD: binOp {s}", .{@tagName(x)}), |
| 2107 | 2101 | } |
| 2108 | 2102 | }, |
| 2109 | 2103 | |
| ... | ... | @@ -2126,9 +2120,9 @@ fn binOp( |
| 2126 | 2120 | else => unreachable, |
| 2127 | 2121 | }; |
| 2128 | 2122 | |
| 2129 | | return try self.binOpRegister(base_tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2123 | return try func.binOpRegister(base_tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2130 | 2124 | } else { |
| 2131 | | const offset = try self.binOp( |
| 2125 | const offset = try func.binOp( |
| 2132 | 2126 | .mul, |
| 2133 | 2127 | rhs, |
| 2134 | 2128 | Type.usize, |
| ... | ... | @@ -2136,7 +2130,7 @@ fn binOp( |
| 2136 | 2130 | Type.usize, |
| 2137 | 2131 | ); |
| 2138 | 2132 | |
| 2139 | | const addr = try self.binOp( |
| 2133 | const addr = try func.binOp( |
| 2140 | 2134 | tag, |
| 2141 | 2135 | lhs, |
| 2142 | 2136 | Type.manyptr_u8, |
| ... | ... | @@ -2155,39 +2149,39 @@ fn binOp( |
| 2155 | 2149 | .shl, |
| 2156 | 2150 | => { |
| 2157 | 2151 | switch (lhs_ty.zigTypeTag(zcu)) { |
| 2158 | | .Float => return self.fail("TODO binary operations on floats", .{}), |
| 2159 | | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 2152 | .Float => return func.fail("TODO binary operations on floats", .{}), |
| 2153 | .Vector => return func.fail("TODO binary operations on vectors", .{}), |
| 2160 | 2154 | .Int => { |
| 2161 | 2155 | const int_info = lhs_ty.intInfo(zcu); |
| 2162 | 2156 | if (int_info.bits <= 64) { |
| 2163 | | return self.binOpRegister(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2157 | return func.binOpRegister(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2164 | 2158 | } else { |
| 2165 | | return self.fail("TODO binary operations on int with bits > 64", .{}); |
| 2159 | return func.fail("TODO binary operations on int with bits > 64", .{}); |
| 2166 | 2160 | } |
| 2167 | 2161 | }, |
| 2168 | 2162 | else => unreachable, |
| 2169 | 2163 | } |
| 2170 | 2164 | }, |
| 2171 | | else => return self.fail("TODO binOp {}", .{tag}), |
| 2165 | else => return func.fail("TODO binOp {}", .{tag}), |
| 2172 | 2166 | } |
| 2173 | 2167 | } |
| 2174 | 2168 | |
| 2175 | 2169 | fn binOpRegister( |
| 2176 | | self: *Self, |
| 2170 | func: *Func, |
| 2177 | 2171 | tag: Air.Inst.Tag, |
| 2178 | 2172 | lhs: MCValue, |
| 2179 | 2173 | lhs_ty: Type, |
| 2180 | 2174 | rhs: MCValue, |
| 2181 | 2175 | rhs_ty: Type, |
| 2182 | 2176 | ) !MCValue { |
| 2183 | | const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{ .zero = true }); |
| 2184 | | defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2177 | const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{ .zero = true }); |
| 2178 | defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2185 | 2179 | |
| 2186 | | const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{ .zero = true }); |
| 2187 | | defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2180 | const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{ .zero = true }); |
| 2181 | defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2188 | 2182 | |
| 2189 | | const dest_reg, const dest_lock = try self.allocReg(.int); |
| 2190 | | defer self.register_manager.unlockReg(dest_lock); |
| 2183 | const dest_reg, const dest_lock = try func.allocReg(.int); |
| 2184 | defer func.register_manager.unlockReg(dest_lock); |
| 2191 | 2185 | |
| 2192 | 2186 | const mir_tag: Mir.Inst.Tag = switch (tag) { |
| 2193 | 2187 | .add => .add, |
| ... | ... | @@ -2205,7 +2199,7 @@ fn binOpRegister( |
| 2205 | 2199 | .cmp_lte, |
| 2206 | 2200 | => .pseudo, |
| 2207 | 2201 | |
| 2208 | | else => return self.fail("TODO: binOpRegister {s}", .{@tagName(tag)}), |
| 2202 | else => return func.fail("TODO: binOpRegister {s}", .{@tagName(tag)}), |
| 2209 | 2203 | }; |
| 2210 | 2204 | |
| 2211 | 2205 | switch (mir_tag) { |
| ... | ... | @@ -2215,7 +2209,7 @@ fn binOpRegister( |
| 2215 | 2209 | .sllw, |
| 2216 | 2210 | .srlw, |
| 2217 | 2211 | => { |
| 2218 | | _ = try self.addInst(.{ |
| 2212 | _ = try func.addInst(.{ |
| 2219 | 2213 | .tag = mir_tag, |
| 2220 | 2214 | .ops = .rrr, |
| 2221 | 2215 | .data = .{ |
| ... | ... | @@ -2240,7 +2234,7 @@ fn binOpRegister( |
| 2240 | 2234 | else => unreachable, |
| 2241 | 2235 | }; |
| 2242 | 2236 | |
| 2243 | | _ = try self.addInst(.{ |
| 2237 | _ = try func.addInst(.{ |
| 2244 | 2238 | .tag = .pseudo, |
| 2245 | 2239 | .ops = pseudo_op, |
| 2246 | 2240 | .data = .{ |
| ... | ... | @@ -2257,7 +2251,7 @@ fn binOpRegister( |
| 2257 | 2251 | .cmp_lte => .lte, |
| 2258 | 2252 | else => unreachable, |
| 2259 | 2253 | }, |
| 2260 | | .size = self.memSize(lhs_ty), |
| 2254 | .size = func.memSize(lhs_ty), |
| 2261 | 2255 | }, |
| 2262 | 2256 | }, |
| 2263 | 2257 | }); |
| ... | ... | @@ -2270,21 +2264,21 @@ fn binOpRegister( |
| 2270 | 2264 | } |
| 2271 | 2265 | |
| 2272 | 2266 | fn binOpFloat( |
| 2273 | | self: *Self, |
| 2267 | func: *Func, |
| 2274 | 2268 | tag: Air.Inst.Tag, |
| 2275 | 2269 | lhs: MCValue, |
| 2276 | 2270 | lhs_ty: Type, |
| 2277 | 2271 | rhs: MCValue, |
| 2278 | 2272 | rhs_ty: Type, |
| 2279 | 2273 | ) !MCValue { |
| 2280 | | const zcu = self.bin_file.comp.module.?; |
| 2274 | const zcu = func.bin_file.comp.module.?; |
| 2281 | 2275 | const float_bits = lhs_ty.floatBits(zcu.getTarget()); |
| 2282 | 2276 | |
| 2283 | | const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{}); |
| 2284 | | defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2277 | const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{}); |
| 2278 | defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2285 | 2279 | |
| 2286 | | const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{}); |
| 2287 | | defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2280 | const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{}); |
| 2281 | defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2288 | 2282 | |
| 2289 | 2283 | const mir_tag: Mir.Inst.Tag = switch (tag) { |
| 2290 | 2284 | .add => if (float_bits == 32) .fadds else .faddd, |
| ... | ... | @@ -2300,7 +2294,7 @@ fn binOpFloat( |
| 2300 | 2294 | .cmp_lte, |
| 2301 | 2295 | => .pseudo, |
| 2302 | 2296 | |
| 2303 | | else => return self.fail("TODO: binOpFloat mir_tag {s}", .{@tagName(tag)}), |
| 2297 | else => return func.fail("TODO: binOpFloat mir_tag {s}", .{@tagName(tag)}), |
| 2304 | 2298 | }; |
| 2305 | 2299 | |
| 2306 | 2300 | const return_class: abi.RegisterClass = switch (tag) { |
| ... | ... | @@ -2320,8 +2314,8 @@ fn binOpFloat( |
| 2320 | 2314 | else => unreachable, |
| 2321 | 2315 | }; |
| 2322 | 2316 | |
| 2323 | | const dest_reg, const dest_lock = try self.allocReg(return_class); |
| 2324 | | defer self.register_manager.unlockReg(dest_lock); |
| 2317 | const dest_reg, const dest_lock = try func.allocReg(return_class); |
| 2318 | defer func.register_manager.unlockReg(dest_lock); |
| 2325 | 2319 | |
| 2326 | 2320 | switch (tag) { |
| 2327 | 2321 | .add, |
| ... | ... | @@ -2329,7 +2323,7 @@ fn binOpFloat( |
| 2329 | 2323 | .mul, |
| 2330 | 2324 | .div_float, |
| 2331 | 2325 | => { |
| 2332 | | _ = try self.addInst(.{ |
| 2326 | _ = try func.addInst(.{ |
| 2333 | 2327 | .tag = mir_tag, |
| 2334 | 2328 | .ops = .rrr, |
| 2335 | 2329 | .data = .{ .r_type = .{ |
| ... | ... | @@ -2347,7 +2341,7 @@ fn binOpFloat( |
| 2347 | 2341 | .cmp_lt, |
| 2348 | 2342 | .cmp_lte, |
| 2349 | 2343 | => { |
| 2350 | | _ = try self.addInst(.{ |
| 2344 | _ = try func.addInst(.{ |
| 2351 | 2345 | .tag = .pseudo, |
| 2352 | 2346 | .ops = .pseudo_compare, |
| 2353 | 2347 | .data = .{ |
| ... | ... | @@ -2364,7 +2358,7 @@ fn binOpFloat( |
| 2364 | 2358 | .cmp_lte => .lte, |
| 2365 | 2359 | else => unreachable, |
| 2366 | 2360 | }, |
| 2367 | | .size = self.memSize(lhs_ty), |
| 2361 | .size = func.memSize(lhs_ty), |
| 2368 | 2362 | }, |
| 2369 | 2363 | }, |
| 2370 | 2364 | }); |
| ... | ... | @@ -2376,120 +2370,120 @@ fn binOpFloat( |
| 2376 | 2370 | return MCValue{ .register = dest_reg }; |
| 2377 | 2371 | } |
| 2378 | 2372 | |
| 2379 | | fn airPtrArithmetic(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 2380 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2381 | | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2382 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2383 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2384 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2385 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2373 | fn airPtrArithmetic(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 2374 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2375 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2376 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2377 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2378 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 2379 | const rhs_ty = func.typeOf(bin_op.rhs); |
| 2386 | 2380 | |
| 2387 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2388 | | break :result try self.binOp(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2381 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2382 | break :result try func.binOp(tag, lhs, lhs_ty, rhs, rhs_ty); |
| 2389 | 2383 | }; |
| 2390 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2384 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2391 | 2385 | } |
| 2392 | 2386 | |
| 2393 | | fn airAddWrap(self: *Self, inst: Air.Inst.Index) !void { |
| 2394 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2395 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement addwrap for {}", .{self.target.cpu.arch}); |
| 2396 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2387 | fn airAddWrap(func: *Func, inst: Air.Inst.Index) !void { |
| 2388 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2389 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement addwrap for {}", .{func.target.cpu.arch}); |
| 2390 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2397 | 2391 | } |
| 2398 | 2392 | |
| 2399 | | fn airAddSat(self: *Self, inst: Air.Inst.Index) !void { |
| 2400 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2401 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement add_sat for {}", .{self.target.cpu.arch}); |
| 2402 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2393 | fn airAddSat(func: *Func, inst: Air.Inst.Index) !void { |
| 2394 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2395 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement add_sat for {}", .{func.target.cpu.arch}); |
| 2396 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2403 | 2397 | } |
| 2404 | 2398 | |
| 2405 | | fn airSubWrap(self: *Self, inst: Air.Inst.Index) !void { |
| 2406 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2407 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2399 | fn airSubWrap(func: *Func, inst: Air.Inst.Index) !void { |
| 2400 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2401 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2408 | 2402 | // RISCV arthemtic instructions already wrap, so this is simply a sub binOp with |
| 2409 | 2403 | // no overflow checks. |
| 2410 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2411 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2412 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2413 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2404 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2405 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2406 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 2407 | const rhs_ty = func.typeOf(bin_op.rhs); |
| 2414 | 2408 | |
| 2415 | | break :result try self.binOp(.sub, lhs, lhs_ty, rhs, rhs_ty); |
| 2409 | break :result try func.binOp(.sub, lhs, lhs_ty, rhs, rhs_ty); |
| 2416 | 2410 | }; |
| 2417 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2411 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2418 | 2412 | } |
| 2419 | 2413 | |
| 2420 | | fn airSubSat(self: *Self, inst: Air.Inst.Index) !void { |
| 2421 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2422 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement sub_sat for {}", .{self.target.cpu.arch}); |
| 2423 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2414 | fn airSubSat(func: *Func, inst: Air.Inst.Index) !void { |
| 2415 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2416 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement sub_sat for {}", .{func.target.cpu.arch}); |
| 2417 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2424 | 2418 | } |
| 2425 | 2419 | |
| 2426 | | fn airMul(self: *Self, inst: Air.Inst.Index) !void { |
| 2427 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2428 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2429 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2430 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2431 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2432 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2420 | fn airMul(func: *Func, inst: Air.Inst.Index) !void { |
| 2421 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2422 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2423 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2424 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2425 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 2426 | const rhs_ty = func.typeOf(bin_op.rhs); |
| 2433 | 2427 | |
| 2434 | | break :result try self.binOp(.mul, lhs, lhs_ty, rhs, rhs_ty); |
| 2428 | break :result try func.binOp(.mul, lhs, lhs_ty, rhs, rhs_ty); |
| 2435 | 2429 | }; |
| 2436 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2430 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2437 | 2431 | } |
| 2438 | 2432 | |
| 2439 | | fn airDiv(self: *Self, inst: Air.Inst.Index) !void { |
| 2440 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2441 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2442 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2443 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2444 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2445 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2433 | fn airDiv(func: *Func, inst: Air.Inst.Index) !void { |
| 2434 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2435 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2436 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2437 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2438 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 2439 | const rhs_ty = func.typeOf(bin_op.rhs); |
| 2446 | 2440 | |
| 2447 | | break :result try self.binOp(.div_float, lhs, lhs_ty, rhs, rhs_ty); |
| 2441 | break :result try func.binOp(.div_float, lhs, lhs_ty, rhs, rhs_ty); |
| 2448 | 2442 | }; |
| 2449 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2443 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2450 | 2444 | } |
| 2451 | 2445 | |
| 2452 | | fn airMulWrap(self: *Self, inst: Air.Inst.Index) !void { |
| 2453 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2454 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement mulwrap for {}", .{self.target.cpu.arch}); |
| 2455 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2446 | fn airMulWrap(func: *Func, inst: Air.Inst.Index) !void { |
| 2447 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2448 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement mulwrap for {}", .{func.target.cpu.arch}); |
| 2449 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2456 | 2450 | } |
| 2457 | 2451 | |
| 2458 | | fn airMulSat(self: *Self, inst: Air.Inst.Index) !void { |
| 2459 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2460 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement mul_sat for {}", .{self.target.cpu.arch}); |
| 2461 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2452 | fn airMulSat(func: *Func, inst: Air.Inst.Index) !void { |
| 2453 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2454 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement mul_sat for {}", .{func.target.cpu.arch}); |
| 2455 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2462 | 2456 | } |
| 2463 | 2457 | |
| 2464 | | fn airAddWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2465 | | const zcu = self.bin_file.comp.module.?; |
| 2466 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2467 | | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2458 | fn airAddWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2459 | const zcu = func.bin_file.comp.module.?; |
| 2460 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2461 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2468 | 2462 | |
| 2469 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2470 | | const lhs = try self.resolveInst(extra.lhs); |
| 2471 | | const rhs = try self.resolveInst(extra.rhs); |
| 2472 | | const lhs_ty = self.typeOf(extra.lhs); |
| 2473 | | const rhs_ty = self.typeOf(extra.rhs); |
| 2463 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2464 | const lhs = try func.resolveInst(extra.lhs); |
| 2465 | const rhs = try func.resolveInst(extra.rhs); |
| 2466 | const lhs_ty = func.typeOf(extra.lhs); |
| 2467 | const rhs_ty = func.typeOf(extra.rhs); |
| 2474 | 2468 | |
| 2475 | 2469 | const int_info = lhs_ty.intInfo(zcu); |
| 2476 | 2470 | |
| 2477 | | const tuple_ty = self.typeOfIndex(inst); |
| 2478 | | const result_mcv = try self.allocRegOrMem(inst, false); |
| 2471 | const tuple_ty = func.typeOfIndex(inst); |
| 2472 | const result_mcv = try func.allocRegOrMem(inst, false); |
| 2479 | 2473 | const offset = result_mcv.load_frame; |
| 2480 | 2474 | |
| 2481 | 2475 | if (int_info.bits >= 8 and math.isPowerOfTwo(int_info.bits)) { |
| 2482 | | const add_result = try self.binOp(.add, lhs, lhs_ty, rhs, rhs_ty); |
| 2483 | | const add_result_reg = try self.copyToTmpRegister(lhs_ty, add_result); |
| 2484 | | const add_result_reg_lock = self.register_manager.lockRegAssumeUnused(add_result_reg); |
| 2485 | | defer self.register_manager.unlockReg(add_result_reg_lock); |
| 2476 | const add_result = try func.binOp(.add, lhs, lhs_ty, rhs, rhs_ty); |
| 2477 | const add_result_reg = try func.copyToTmpRegister(lhs_ty, add_result); |
| 2478 | const add_result_reg_lock = func.register_manager.lockRegAssumeUnused(add_result_reg); |
| 2479 | defer func.register_manager.unlockReg(add_result_reg_lock); |
| 2486 | 2480 | |
| 2487 | 2481 | const shift_amount: u6 = @intCast(Type.usize.bitSize(zcu) - int_info.bits); |
| 2488 | 2482 | |
| 2489 | | const shift_reg, const shift_lock = try self.allocReg(.int); |
| 2490 | | defer self.register_manager.unlockReg(shift_lock); |
| 2483 | const shift_reg, const shift_lock = try func.allocReg(.int); |
| 2484 | defer func.register_manager.unlockReg(shift_lock); |
| 2491 | 2485 | |
| 2492 | | _ = try self.addInst(.{ |
| 2486 | _ = try func.addInst(.{ |
| 2493 | 2487 | .tag = .slli, |
| 2494 | 2488 | .ops = .rri, |
| 2495 | 2489 | .data = .{ |
| ... | ... | @@ -2501,7 +2495,7 @@ fn airAddWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2501 | 2495 | }, |
| 2502 | 2496 | }); |
| 2503 | 2497 | |
| 2504 | | _ = try self.addInst(.{ |
| 2498 | _ = try func.addInst(.{ |
| 2505 | 2499 | .tag = if (int_info.signedness == .unsigned) .srli else .srai, |
| 2506 | 2500 | .ops = .rri, |
| 2507 | 2501 | .data = .{ |
| ... | ... | @@ -2513,21 +2507,21 @@ fn airAddWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2513 | 2507 | }, |
| 2514 | 2508 | }); |
| 2515 | 2509 | |
| 2516 | | try self.genSetMem( |
| 2510 | try func.genSetMem( |
| 2517 | 2511 | .{ .frame = offset.index }, |
| 2518 | 2512 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, zcu))), |
| 2519 | 2513 | lhs_ty, |
| 2520 | 2514 | add_result, |
| 2521 | 2515 | ); |
| 2522 | 2516 | |
| 2523 | | const overflow_mcv = try self.binOp( |
| 2517 | const overflow_mcv = try func.binOp( |
| 2524 | 2518 | .cmp_neq, |
| 2525 | 2519 | .{ .register = shift_reg }, |
| 2526 | 2520 | lhs_ty, |
| 2527 | 2521 | .{ .register = add_result_reg }, |
| 2528 | 2522 | lhs_ty, |
| 2529 | 2523 | ); |
| 2530 | | try self.genSetMem( |
| 2524 | try func.genSetMem( |
| 2531 | 2525 | .{ .frame = offset.index }, |
| 2532 | 2526 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))), |
| 2533 | 2527 | Type.u1, |
| ... | ... | @@ -2536,50 +2530,50 @@ fn airAddWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2536 | 2530 | |
| 2537 | 2531 | break :result result_mcv; |
| 2538 | 2532 | } else { |
| 2539 | | return self.fail("TODO: less than 8 bit or non-pow 2 addition", .{}); |
| 2533 | return func.fail("TODO: less than 8 bit or non-pow 2 addition", .{}); |
| 2540 | 2534 | } |
| 2541 | 2535 | }; |
| 2542 | 2536 | |
| 2543 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2537 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2544 | 2538 | } |
| 2545 | 2539 | |
| 2546 | | fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2547 | | const zcu = self.bin_file.comp.module.?; |
| 2548 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2549 | | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2540 | fn airSubWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2541 | const zcu = func.bin_file.comp.module.?; |
| 2542 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2543 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2550 | 2544 | |
| 2551 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2552 | | const lhs = try self.resolveInst(extra.lhs); |
| 2553 | | const rhs = try self.resolveInst(extra.rhs); |
| 2554 | | const lhs_ty = self.typeOf(extra.lhs); |
| 2555 | | const rhs_ty = self.typeOf(extra.rhs); |
| 2545 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2546 | const lhs = try func.resolveInst(extra.lhs); |
| 2547 | const rhs = try func.resolveInst(extra.rhs); |
| 2548 | const lhs_ty = func.typeOf(extra.lhs); |
| 2549 | const rhs_ty = func.typeOf(extra.rhs); |
| 2556 | 2550 | |
| 2557 | 2551 | const int_info = lhs_ty.intInfo(zcu); |
| 2558 | 2552 | |
| 2559 | 2553 | if (!math.isPowerOfTwo(int_info.bits) or !(int_info.bits >= 8)) { |
| 2560 | | return self.fail("TODO: airSubWithOverflow non-power of 2 and less than 8 bits", .{}); |
| 2554 | return func.fail("TODO: airSubWithOverflow non-power of 2 and less than 8 bits", .{}); |
| 2561 | 2555 | } |
| 2562 | 2556 | |
| 2563 | | const tuple_ty = self.typeOfIndex(inst); |
| 2564 | | const result_mcv = try self.allocRegOrMem(inst, false); |
| 2557 | const tuple_ty = func.typeOfIndex(inst); |
| 2558 | const result_mcv = try func.allocRegOrMem(inst, false); |
| 2565 | 2559 | const offset = result_mcv.load_frame; |
| 2566 | 2560 | |
| 2567 | | const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{}); |
| 2568 | | defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2561 | const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{}); |
| 2562 | defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2569 | 2563 | |
| 2570 | | const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{}); |
| 2571 | | defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2564 | const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{}); |
| 2565 | defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2572 | 2566 | |
| 2573 | | const dest_reg, const dest_lock = try self.allocReg(.int); |
| 2574 | | defer self.register_manager.unlockReg(dest_lock); |
| 2567 | const dest_reg, const dest_lock = try func.allocReg(.int); |
| 2568 | defer func.register_manager.unlockReg(dest_lock); |
| 2575 | 2569 | |
| 2576 | 2570 | switch (int_info.signedness) { |
| 2577 | | .unsigned => return self.fail("TODO: airSubWithOverflow unsigned", .{}), |
| 2571 | .unsigned => return func.fail("TODO: airSubWithOverflow unsigned", .{}), |
| 2578 | 2572 | .signed => { |
| 2579 | 2573 | switch (int_info.bits) { |
| 2580 | 2574 | 64 => { |
| 2581 | 2575 | // result |
| 2582 | | _ = try self.addInst(.{ |
| 2576 | _ = try func.addInst(.{ |
| 2583 | 2577 | .tag = .sub, |
| 2584 | 2578 | .ops = .rrr, |
| 2585 | 2579 | .data = .{ .r_type = .{ |
| ... | ... | @@ -2589,7 +2583,7 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2589 | 2583 | } }, |
| 2590 | 2584 | }); |
| 2591 | 2585 | |
| 2592 | | try self.genSetMem( |
| 2586 | try func.genSetMem( |
| 2593 | 2587 | .{ .frame = offset.index }, |
| 2594 | 2588 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, zcu))), |
| 2595 | 2589 | lhs_ty, |
| ... | ... | @@ -2597,9 +2591,9 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2597 | 2591 | ); |
| 2598 | 2592 | |
| 2599 | 2593 | // overflow check |
| 2600 | | const overflow_reg = try self.copyToTmpRegister(Type.usize, .{ .immediate = 0 }); |
| 2594 | const overflow_reg = try func.copyToTmpRegister(Type.usize, .{ .immediate = 0 }); |
| 2601 | 2595 | |
| 2602 | | _ = try self.addInst(.{ |
| 2596 | _ = try func.addInst(.{ |
| 2603 | 2597 | .tag = .slt, |
| 2604 | 2598 | .ops = .rrr, |
| 2605 | 2599 | .data = .{ .r_type = .{ |
| ... | ... | @@ -2609,7 +2603,7 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2609 | 2603 | } }, |
| 2610 | 2604 | }); |
| 2611 | 2605 | |
| 2612 | | _ = try self.addInst(.{ |
| 2606 | _ = try func.addInst(.{ |
| 2613 | 2607 | .tag = .slt, |
| 2614 | 2608 | .ops = .rrr, |
| 2615 | 2609 | .data = .{ .r_type = .{ |
| ... | ... | @@ -2619,7 +2613,7 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2619 | 2613 | } }, |
| 2620 | 2614 | }); |
| 2621 | 2615 | |
| 2622 | | _ = try self.addInst(.{ |
| 2616 | _ = try func.addInst(.{ |
| 2623 | 2617 | .tag = .xor, |
| 2624 | 2618 | .ops = .rrr, |
| 2625 | 2619 | .data = .{ .r_type = .{ |
| ... | ... | @@ -2629,7 +2623,7 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2629 | 2623 | } }, |
| 2630 | 2624 | }); |
| 2631 | 2625 | |
| 2632 | | const overflow_mcv = try self.binOp( |
| 2626 | const overflow_mcv = try func.binOp( |
| 2633 | 2627 | .cmp_neq, |
| 2634 | 2628 | .{ .register = overflow_reg }, |
| 2635 | 2629 | Type.usize, |
| ... | ... | @@ -2637,7 +2631,7 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2637 | 2631 | Type.usize, |
| 2638 | 2632 | ); |
| 2639 | 2633 | |
| 2640 | | try self.genSetMem( |
| 2634 | try func.genSetMem( |
| 2641 | 2635 | .{ .frame = offset.index }, |
| 2642 | 2636 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))), |
| 2643 | 2637 | Type.u1, |
| ... | ... | @@ -2646,47 +2640,47 @@ fn airSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2646 | 2640 | |
| 2647 | 2641 | break :result result_mcv; |
| 2648 | 2642 | }, |
| 2649 | | else => |int_bits| return self.fail("TODO: airSubWithOverflow signed {}", .{int_bits}), |
| 2643 | else => |int_bits| return func.fail("TODO: airSubWithOverflow signed {}", .{int_bits}), |
| 2650 | 2644 | } |
| 2651 | 2645 | }, |
| 2652 | 2646 | } |
| 2653 | 2647 | }; |
| 2654 | 2648 | |
| 2655 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2649 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2656 | 2650 | } |
| 2657 | 2651 | |
| 2658 | | fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2659 | | //const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 2660 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2661 | | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2662 | | const zcu = self.bin_file.comp.module.?; |
| 2663 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2664 | | const lhs = try self.resolveInst(extra.lhs); |
| 2665 | | const rhs = try self.resolveInst(extra.rhs); |
| 2666 | | const lhs_ty = self.typeOf(extra.lhs); |
| 2667 | | const rhs_ty = self.typeOf(extra.rhs); |
| 2652 | fn airMulWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2653 | //const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 2654 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2655 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2656 | const zcu = func.bin_file.comp.module.?; |
| 2657 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2658 | const lhs = try func.resolveInst(extra.lhs); |
| 2659 | const rhs = try func.resolveInst(extra.rhs); |
| 2660 | const lhs_ty = func.typeOf(extra.lhs); |
| 2661 | const rhs_ty = func.typeOf(extra.rhs); |
| 2668 | 2662 | |
| 2669 | 2663 | switch (lhs_ty.zigTypeTag(zcu)) { |
| 2670 | | else => |x| return self.fail("TODO: airMulWithOverflow {s}", .{@tagName(x)}), |
| 2664 | else => |x| return func.fail("TODO: airMulWithOverflow {s}", .{@tagName(x)}), |
| 2671 | 2665 | .Int => { |
| 2672 | 2666 | assert(lhs_ty.eql(rhs_ty, zcu)); |
| 2673 | 2667 | const int_info = lhs_ty.intInfo(zcu); |
| 2674 | 2668 | switch (int_info.bits) { |
| 2675 | 2669 | 1...32 => { |
| 2676 | | if (self.hasFeature(.m)) { |
| 2677 | | const dest = try self.binOp(.mul, lhs, lhs_ty, rhs, rhs_ty); |
| 2670 | if (func.hasFeature(.m)) { |
| 2671 | const dest = try func.binOp(.mul, lhs, lhs_ty, rhs, rhs_ty); |
| 2678 | 2672 | |
| 2679 | | const add_result_lock = self.register_manager.lockRegAssumeUnused(dest.register); |
| 2680 | | defer self.register_manager.unlockReg(add_result_lock); |
| 2673 | const add_result_lock = func.register_manager.lockRegAssumeUnused(dest.register); |
| 2674 | defer func.register_manager.unlockReg(add_result_lock); |
| 2681 | 2675 | |
| 2682 | | const tuple_ty = self.typeOfIndex(inst); |
| 2676 | const tuple_ty = func.typeOfIndex(inst); |
| 2683 | 2677 | |
| 2684 | | const result_mcv = try self.allocRegOrMem(inst, true); |
| 2678 | const result_mcv = try func.allocRegOrMem(inst, true); |
| 2685 | 2679 | |
| 2686 | 2680 | const result_off: i32 = @intCast(tuple_ty.structFieldOffset(0, zcu)); |
| 2687 | 2681 | const overflow_off: i32 = @intCast(tuple_ty.structFieldOffset(1, zcu)); |
| 2688 | 2682 | |
| 2689 | | try self.genCopy( |
| 2683 | try func.genCopy( |
| 2690 | 2684 | lhs_ty, |
| 2691 | 2685 | result_mcv.offset(result_off), |
| 2692 | 2686 | dest, |
| ... | ... | @@ -2698,13 +2692,13 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2698 | 2692 | 1...8 => { |
| 2699 | 2693 | const max_val = std.math.pow(u16, 2, int_info.bits) - 1; |
| 2700 | 2694 | |
| 2701 | | const add_reg, const add_lock = try self.promoteReg(lhs_ty, lhs, .{}); |
| 2702 | | defer if (add_lock) |lock| self.register_manager.unlockReg(lock); |
| 2695 | const add_reg, const add_lock = try func.promoteReg(lhs_ty, lhs, .{}); |
| 2696 | defer if (add_lock) |lock| func.register_manager.unlockReg(lock); |
| 2703 | 2697 | |
| 2704 | | const overflow_reg, const overflow_lock = try self.allocReg(.int); |
| 2705 | | defer self.register_manager.unlockReg(overflow_lock); |
| 2698 | const overflow_reg, const overflow_lock = try func.allocReg(.int); |
| 2699 | defer func.register_manager.unlockReg(overflow_lock); |
| 2706 | 2700 | |
| 2707 | | _ = try self.addInst(.{ |
| 2701 | _ = try func.addInst(.{ |
| 2708 | 2702 | .tag = .andi, |
| 2709 | 2703 | .ops = .rri, |
| 2710 | 2704 | .data = .{ .i_type = .{ |
| ... | ... | @@ -2714,7 +2708,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2714 | 2708 | } }, |
| 2715 | 2709 | }); |
| 2716 | 2710 | |
| 2717 | | const overflow_mcv = try self.binOp( |
| 2711 | const overflow_mcv = try func.binOp( |
| 2718 | 2712 | .cmp_neq, |
| 2719 | 2713 | .{ .register = overflow_reg }, |
| 2720 | 2714 | lhs_ty, |
| ... | ... | @@ -2722,7 +2716,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2722 | 2716 | lhs_ty, |
| 2723 | 2717 | ); |
| 2724 | 2718 | |
| 2725 | | try self.genCopy( |
| 2719 | try func.genCopy( |
| 2726 | 2720 | lhs_ty, |
| 2727 | 2721 | result_mcv.offset(overflow_off), |
| 2728 | 2722 | overflow_mcv, |
| ... | ... | @@ -2731,63 +2725,63 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2731 | 2725 | break :result result_mcv; |
| 2732 | 2726 | }, |
| 2733 | 2727 | |
| 2734 | | else => return self.fail("TODO: airMulWithOverflow check for size {d}", .{int_info.bits}), |
| 2728 | else => return func.fail("TODO: airMulWithOverflow check for size {d}", .{int_info.bits}), |
| 2735 | 2729 | } |
| 2736 | 2730 | } else { |
| 2737 | | return self.fail("TODO: airMulWithOverflow calculate carry for signed addition", .{}); |
| 2731 | return func.fail("TODO: airMulWithOverflow calculate carry for signed addition", .{}); |
| 2738 | 2732 | } |
| 2739 | 2733 | } else { |
| 2740 | | return self.fail("TODO: airMulWithOverflow with < 8 bits or non-pow of 2", .{}); |
| 2734 | return func.fail("TODO: airMulWithOverflow with < 8 bits or non-pow of 2", .{}); |
| 2741 | 2735 | } |
| 2742 | 2736 | } else { |
| 2743 | | return self.fail("TODO: emulate mul for targets without M feature", .{}); |
| 2737 | return func.fail("TODO: emulate mul for targets without M feature", .{}); |
| 2744 | 2738 | } |
| 2745 | 2739 | }, |
| 2746 | | else => return self.fail("TODO: airMulWithOverflow larger than 32-bit mul", .{}), |
| 2740 | else => return func.fail("TODO: airMulWithOverflow larger than 32-bit mul", .{}), |
| 2747 | 2741 | } |
| 2748 | 2742 | }, |
| 2749 | 2743 | } |
| 2750 | 2744 | }; |
| 2751 | 2745 | |
| 2752 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2746 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 2753 | 2747 | } |
| 2754 | 2748 | |
| 2755 | | fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2749 | fn airShlWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2756 | 2750 | _ = inst; |
| 2757 | | return self.fail("TODO implement airShlWithOverflow for {}", .{self.target.cpu.arch}); |
| 2751 | return func.fail("TODO implement airShlWithOverflow for {}", .{func.target.cpu.arch}); |
| 2758 | 2752 | } |
| 2759 | 2753 | |
| 2760 | | fn airRem(self: *Self, inst: Air.Inst.Index) !void { |
| 2761 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2762 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement rem for {}", .{self.target.cpu.arch}); |
| 2763 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2754 | fn airRem(func: *Func, inst: Air.Inst.Index) !void { |
| 2755 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2756 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement rem for {}", .{func.target.cpu.arch}); |
| 2757 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2764 | 2758 | } |
| 2765 | 2759 | |
| 2766 | | fn airMod(self: *Self, inst: Air.Inst.Index) !void { |
| 2767 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2768 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement zcu for {}", .{self.target.cpu.arch}); |
| 2769 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2760 | fn airMod(func: *Func, inst: Air.Inst.Index) !void { |
| 2761 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2762 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement zcu for {}", .{func.target.cpu.arch}); |
| 2763 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2770 | 2764 | } |
| 2771 | 2765 | |
| 2772 | | fn airBitAnd(self: *Self, inst: Air.Inst.Index) !void { |
| 2773 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2774 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2775 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2776 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2766 | fn airBitAnd(func: *Func, inst: Air.Inst.Index) !void { |
| 2767 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2768 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2769 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2770 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2777 | 2771 | |
| 2778 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2779 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2772 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 2773 | const rhs_ty = func.typeOf(bin_op.rhs); |
| 2780 | 2774 | |
| 2781 | | const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{}); |
| 2782 | | defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2775 | const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{}); |
| 2776 | defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2783 | 2777 | |
| 2784 | | const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{}); |
| 2785 | | defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2778 | const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{}); |
| 2779 | defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2786 | 2780 | |
| 2787 | | const dest_reg, const dest_lock = try self.allocReg(.int); |
| 2788 | | defer self.register_manager.unlockReg(dest_lock); |
| 2781 | const dest_reg, const dest_lock = try func.allocReg(.int); |
| 2782 | defer func.register_manager.unlockReg(dest_lock); |
| 2789 | 2783 | |
| 2790 | | _ = try self.addInst(.{ |
| 2784 | _ = try func.addInst(.{ |
| 2791 | 2785 | .tag = .@"and", |
| 2792 | 2786 | .ops = .rrr, |
| 2793 | 2787 | .data = .{ .r_type = .{ |
| ... | ... | @@ -2799,28 +2793,28 @@ fn airBitAnd(self: *Self, inst: Air.Inst.Index) !void { |
| 2799 | 2793 | |
| 2800 | 2794 | break :result .{ .register = dest_reg }; |
| 2801 | 2795 | }; |
| 2802 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2796 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2803 | 2797 | } |
| 2804 | 2798 | |
| 2805 | | fn airBitOr(self: *Self, inst: Air.Inst.Index) !void { |
| 2806 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2807 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2808 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2809 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2799 | fn airBitOr(func: *Func, inst: Air.Inst.Index) !void { |
| 2800 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2801 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2802 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2803 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2810 | 2804 | |
| 2811 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2812 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2805 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 2806 | const rhs_ty = func.typeOf(bin_op.rhs); |
| 2813 | 2807 | |
| 2814 | | const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{}); |
| 2815 | | defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2808 | const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{}); |
| 2809 | defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2816 | 2810 | |
| 2817 | | const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{}); |
| 2818 | | defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 2811 | const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{}); |
| 2812 | defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 2819 | 2813 | |
| 2820 | | const dest_reg, const dest_lock = try self.allocReg(.int); |
| 2821 | | defer self.register_manager.unlockReg(dest_lock); |
| 2814 | const dest_reg, const dest_lock = try func.allocReg(.int); |
| 2815 | defer func.register_manager.unlockReg(dest_lock); |
| 2822 | 2816 | |
| 2823 | | _ = try self.addInst(.{ |
| 2817 | _ = try func.addInst(.{ |
| 2824 | 2818 | .tag = .@"or", |
| 2825 | 2819 | .ops = .rrr, |
| 2826 | 2820 | .data = .{ .r_type = .{ |
| ... | ... | @@ -2832,72 +2826,72 @@ fn airBitOr(self: *Self, inst: Air.Inst.Index) !void { |
| 2832 | 2826 | |
| 2833 | 2827 | break :result .{ .register = dest_reg }; |
| 2834 | 2828 | }; |
| 2835 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2829 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2836 | 2830 | } |
| 2837 | 2831 | |
| 2838 | | fn airXor(self: *Self, inst: Air.Inst.Index) !void { |
| 2839 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2840 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement xor for {}", .{self.target.cpu.arch}); |
| 2841 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2832 | fn airXor(func: *Func, inst: Air.Inst.Index) !void { |
| 2833 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2834 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement xor for {}", .{func.target.cpu.arch}); |
| 2835 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2842 | 2836 | } |
| 2843 | 2837 | |
| 2844 | | fn airShl(self: *Self, inst: Air.Inst.Index) !void { |
| 2845 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2846 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2847 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2848 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2849 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2850 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2838 | fn airShl(func: *Func, inst: Air.Inst.Index) !void { |
| 2839 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2840 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2841 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2842 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2843 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 2844 | const rhs_ty = func.typeOf(bin_op.rhs); |
| 2851 | 2845 | |
| 2852 | | break :result try self.binOp(.shl, lhs, lhs_ty, rhs, rhs_ty); |
| 2846 | break :result try func.binOp(.shl, lhs, lhs_ty, rhs, rhs_ty); |
| 2853 | 2847 | }; |
| 2854 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2848 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2855 | 2849 | } |
| 2856 | 2850 | |
| 2857 | | fn airShlSat(self: *Self, inst: Air.Inst.Index) !void { |
| 2858 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2859 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement shl_sat for {}", .{self.target.cpu.arch}); |
| 2860 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2851 | fn airShlSat(func: *Func, inst: Air.Inst.Index) !void { |
| 2852 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2853 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement shl_sat for {}", .{func.target.cpu.arch}); |
| 2854 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2861 | 2855 | } |
| 2862 | 2856 | |
| 2863 | | fn airShr(self: *Self, inst: Air.Inst.Index) !void { |
| 2864 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2865 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 2866 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 2867 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 2868 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 2869 | | const rhs_ty = self.typeOf(bin_op.rhs); |
| 2857 | fn airShr(func: *Func, inst: Air.Inst.Index) !void { |
| 2858 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2859 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2860 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2861 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2862 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 2863 | const rhs_ty = func.typeOf(bin_op.rhs); |
| 2870 | 2864 | |
| 2871 | | break :result try self.binOp(.shr, lhs, lhs_ty, rhs, rhs_ty); |
| 2865 | break :result try func.binOp(.shr, lhs, lhs_ty, rhs, rhs_ty); |
| 2872 | 2866 | }; |
| 2873 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2867 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 2874 | 2868 | } |
| 2875 | 2869 | |
| 2876 | | fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 2877 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2878 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .optional_payload for {}", .{self.target.cpu.arch}); |
| 2879 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2870 | fn airOptionalPayload(func: *Func, inst: Air.Inst.Index) !void { |
| 2871 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2872 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .optional_payload for {}", .{func.target.cpu.arch}); |
| 2873 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2880 | 2874 | } |
| 2881 | 2875 | |
| 2882 | | fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2883 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2884 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .optional_payload_ptr for {}", .{self.target.cpu.arch}); |
| 2885 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2876 | fn airOptionalPayloadPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 2877 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2878 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .optional_payload_ptr for {}", .{func.target.cpu.arch}); |
| 2879 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2886 | 2880 | } |
| 2887 | 2881 | |
| 2888 | | fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 2889 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2890 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .optional_payload_ptr_set for {}", .{self.target.cpu.arch}); |
| 2891 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2882 | fn airOptionalPayloadPtrSet(func: *Func, inst: Air.Inst.Index) !void { |
| 2883 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2884 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .optional_payload_ptr_set for {}", .{func.target.cpu.arch}); |
| 2885 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2892 | 2886 | } |
| 2893 | 2887 | |
| 2894 | | fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { |
| 2895 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2896 | | const zcu = self.bin_file.comp.module.?; |
| 2897 | | const err_union_ty = self.typeOf(ty_op.operand); |
| 2888 | fn airUnwrapErrErr(func: *Func, inst: Air.Inst.Index) !void { |
| 2889 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2890 | const zcu = func.bin_file.comp.module.?; |
| 2891 | const err_union_ty = func.typeOf(ty_op.operand); |
| 2898 | 2892 | const err_ty = err_union_ty.errorUnionSet(zcu); |
| 2899 | 2893 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 2900 | | const operand = try self.resolveInst(ty_op.operand); |
| 2894 | const operand = try func.resolveInst(ty_op.operand); |
| 2901 | 2895 | |
| 2902 | 2896 | const result: MCValue = result: { |
| 2903 | 2897 | if (err_ty.errorSetIsEmpty(zcu)) { |
| ... | ... | @@ -2912,13 +2906,13 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { |
| 2912 | 2906 | |
| 2913 | 2907 | switch (operand) { |
| 2914 | 2908 | .register => |reg| { |
| 2915 | | const eu_lock = self.register_manager.lockReg(reg); |
| 2916 | | defer if (eu_lock) |lock| self.register_manager.unlockReg(lock); |
| 2909 | const eu_lock = func.register_manager.lockReg(reg); |
| 2910 | defer if (eu_lock) |lock| func.register_manager.unlockReg(lock); |
| 2917 | 2911 | |
| 2918 | | var result = try self.copyToNewRegister(inst, operand); |
| 2912 | var result = try func.copyToNewRegister(inst, operand); |
| 2919 | 2913 | |
| 2920 | 2914 | if (err_off > 0) { |
| 2921 | | result = try self.binOp( |
| 2915 | result = try func.binOp( |
| 2922 | 2916 | .shr, |
| 2923 | 2917 | result, |
| 2924 | 2918 | err_union_ty, |
| ... | ... | @@ -2932,27 +2926,27 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { |
| 2932 | 2926 | .index = frame_addr.index, |
| 2933 | 2927 | .off = frame_addr.off + @as(i32, @intCast(err_off)), |
| 2934 | 2928 | } }, |
| 2935 | | else => return self.fail("TODO implement unwrap_err_err for {}", .{operand}), |
| 2929 | else => return func.fail("TODO implement unwrap_err_err for {}", .{operand}), |
| 2936 | 2930 | } |
| 2937 | 2931 | }; |
| 2938 | 2932 | |
| 2939 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2933 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2940 | 2934 | } |
| 2941 | 2935 | |
| 2942 | | fn airUnwrapErrPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 2943 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2944 | | const operand_ty = self.typeOf(ty_op.operand); |
| 2945 | | const operand = try self.resolveInst(ty_op.operand); |
| 2946 | | const result = try self.genUnwrapErrUnionPayloadMir(operand_ty, operand); |
| 2947 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2936 | fn airUnwrapErrPayload(func: *Func, inst: Air.Inst.Index) !void { |
| 2937 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2938 | const operand_ty = func.typeOf(ty_op.operand); |
| 2939 | const operand = try func.resolveInst(ty_op.operand); |
| 2940 | const result = try func.genUnwrapErrUnionPayloadMir(operand_ty, operand); |
| 2941 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2948 | 2942 | } |
| 2949 | 2943 | |
| 2950 | 2944 | fn genUnwrapErrUnionPayloadMir( |
| 2951 | | self: *Self, |
| 2945 | func: *Func, |
| 2952 | 2946 | err_union_ty: Type, |
| 2953 | 2947 | err_union: MCValue, |
| 2954 | 2948 | ) !MCValue { |
| 2955 | | const zcu = self.bin_file.comp.module.?; |
| 2949 | const zcu = func.bin_file.comp.module.?; |
| 2956 | 2950 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 2957 | 2951 | |
| 2958 | 2952 | const result: MCValue = result: { |
| ... | ... | @@ -2965,13 +2959,13 @@ fn genUnwrapErrUnionPayloadMir( |
| 2965 | 2959 | .off = frame_addr.off + payload_off, |
| 2966 | 2960 | } }, |
| 2967 | 2961 | .register => |reg| { |
| 2968 | | const eu_lock = self.register_manager.lockReg(reg); |
| 2969 | | defer if (eu_lock) |lock| self.register_manager.unlockReg(lock); |
| 2962 | const eu_lock = func.register_manager.lockReg(reg); |
| 2963 | defer if (eu_lock) |lock| func.register_manager.unlockReg(lock); |
| 2970 | 2964 | |
| 2971 | | var result: MCValue = .{ .register = try self.copyToTmpRegister(err_union_ty, err_union) }; |
| 2965 | var result: MCValue = .{ .register = try func.copyToTmpRegister(err_union_ty, err_union) }; |
| 2972 | 2966 | |
| 2973 | 2967 | if (payload_off > 0) { |
| 2974 | | result = try self.binOp( |
| 2968 | result = try func.binOp( |
| 2975 | 2969 | .shr, |
| 2976 | 2970 | result, |
| 2977 | 2971 | err_union_ty, |
| ... | ... | @@ -2982,7 +2976,7 @@ fn genUnwrapErrUnionPayloadMir( |
| 2982 | 2976 | |
| 2983 | 2977 | break :result result; |
| 2984 | 2978 | }, |
| 2985 | | else => return self.fail("TODO implement genUnwrapErrUnionPayloadMir for {}", .{err_union}), |
| 2979 | else => return func.fail("TODO implement genUnwrapErrUnionPayloadMir for {}", .{err_union}), |
| 2986 | 2980 | } |
| 2987 | 2981 | }; |
| 2988 | 2982 | |
| ... | ... | @@ -2990,116 +2984,116 @@ fn genUnwrapErrUnionPayloadMir( |
| 2990 | 2984 | } |
| 2991 | 2985 | |
| 2992 | 2986 | // *(E!T) -> E |
| 2993 | | fn airUnwrapErrErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2994 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2995 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement unwrap error union error ptr for {}", .{self.target.cpu.arch}); |
| 2996 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2987 | fn airUnwrapErrErrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 2988 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2989 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement unwrap error union error ptr for {}", .{func.target.cpu.arch}); |
| 2990 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2997 | 2991 | } |
| 2998 | 2992 | |
| 2999 | 2993 | // *(E!T) -> *T |
| 3000 | | fn airUnwrapErrPayloadPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3001 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3002 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement unwrap error union payload ptr for {}", .{self.target.cpu.arch}); |
| 3003 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2994 | fn airUnwrapErrPayloadPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 2995 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2996 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement unwrap error union payload ptr for {}", .{func.target.cpu.arch}); |
| 2997 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3004 | 2998 | } |
| 3005 | 2999 | |
| 3006 | | fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 3007 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3008 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement .errunion_payload_ptr_set for {}", .{self.target.cpu.arch}); |
| 3009 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3000 | fn airErrUnionPayloadPtrSet(func: *Func, inst: Air.Inst.Index) !void { |
| 3001 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3002 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement .errunion_payload_ptr_set for {}", .{func.target.cpu.arch}); |
| 3003 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3010 | 3004 | } |
| 3011 | 3005 | |
| 3012 | | fn airErrReturnTrace(self: *Self, inst: Air.Inst.Index) !void { |
| 3013 | | const result: MCValue = if (self.liveness.isUnused(inst)) |
| 3006 | fn airErrReturnTrace(func: *Func, inst: Air.Inst.Index) !void { |
| 3007 | const result: MCValue = if (func.liveness.isUnused(inst)) |
| 3014 | 3008 | .unreach |
| 3015 | 3009 | else |
| 3016 | | return self.fail("TODO implement airErrReturnTrace for {}", .{self.target.cpu.arch}); |
| 3017 | | return self.finishAir(inst, result, .{ .none, .none, .none }); |
| 3010 | return func.fail("TODO implement airErrReturnTrace for {}", .{func.target.cpu.arch}); |
| 3011 | return func.finishAir(inst, result, .{ .none, .none, .none }); |
| 3018 | 3012 | } |
| 3019 | 3013 | |
| 3020 | | fn airSetErrReturnTrace(self: *Self, inst: Air.Inst.Index) !void { |
| 3014 | fn airSetErrReturnTrace(func: *Func, inst: Air.Inst.Index) !void { |
| 3021 | 3015 | _ = inst; |
| 3022 | | return self.fail("TODO implement airSetErrReturnTrace for {}", .{self.target.cpu.arch}); |
| 3016 | return func.fail("TODO implement airSetErrReturnTrace for {}", .{func.target.cpu.arch}); |
| 3023 | 3017 | } |
| 3024 | 3018 | |
| 3025 | | fn airSaveErrReturnTraceIndex(self: *Self, inst: Air.Inst.Index) !void { |
| 3019 | fn airSaveErrReturnTraceIndex(func: *Func, inst: Air.Inst.Index) !void { |
| 3026 | 3020 | _ = inst; |
| 3027 | | return self.fail("TODO implement airSaveErrReturnTraceIndex for {}", .{self.target.cpu.arch}); |
| 3021 | return func.fail("TODO implement airSaveErrReturnTraceIndex for {}", .{func.target.cpu.arch}); |
| 3028 | 3022 | } |
| 3029 | 3023 | |
| 3030 | | fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 3031 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3032 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3033 | | const zcu = self.bin_file.comp.module.?; |
| 3034 | | const optional_ty = self.typeOfIndex(inst); |
| 3024 | fn airWrapOptional(func: *Func, inst: Air.Inst.Index) !void { |
| 3025 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3026 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3027 | const zcu = func.bin_file.comp.module.?; |
| 3028 | const optional_ty = func.typeOfIndex(inst); |
| 3035 | 3029 | |
| 3036 | 3030 | // Optional with a zero-bit payload type is just a boolean true |
| 3037 | 3031 | if (optional_ty.abiSize(zcu) == 1) |
| 3038 | 3032 | break :result MCValue{ .immediate = 1 }; |
| 3039 | 3033 | |
| 3040 | | return self.fail("TODO implement wrap optional for {}", .{self.target.cpu.arch}); |
| 3034 | return func.fail("TODO implement wrap optional for {}", .{func.target.cpu.arch}); |
| 3041 | 3035 | }; |
| 3042 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3036 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3043 | 3037 | } |
| 3044 | 3038 | |
| 3045 | 3039 | /// T to E!T |
| 3046 | | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 3047 | | const zcu = self.bin_file.comp.module.?; |
| 3048 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3040 | fn airWrapErrUnionPayload(func: *Func, inst: Air.Inst.Index) !void { |
| 3041 | const zcu = func.bin_file.comp.module.?; |
| 3042 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3049 | 3043 | |
| 3050 | 3044 | const eu_ty = ty_op.ty.toType(); |
| 3051 | 3045 | const pl_ty = eu_ty.errorUnionPayload(zcu); |
| 3052 | 3046 | const err_ty = eu_ty.errorUnionSet(zcu); |
| 3053 | | const operand = try self.resolveInst(ty_op.operand); |
| 3047 | const operand = try func.resolveInst(ty_op.operand); |
| 3054 | 3048 | |
| 3055 | 3049 | const result: MCValue = result: { |
| 3056 | 3050 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .{ .immediate = 0 }; |
| 3057 | 3051 | |
| 3058 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu)); |
| 3052 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu)); |
| 3059 | 3053 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, zcu)); |
| 3060 | 3054 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, zcu)); |
| 3061 | | try self.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, operand); |
| 3062 | | try self.genSetMem(.{ .frame = frame_index }, err_off, err_ty, .{ .immediate = 0 }); |
| 3055 | try func.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, operand); |
| 3056 | try func.genSetMem(.{ .frame = frame_index }, err_off, err_ty, .{ .immediate = 0 }); |
| 3063 | 3057 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 3064 | 3058 | }; |
| 3065 | 3059 | |
| 3066 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3060 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3067 | 3061 | } |
| 3068 | 3062 | |
| 3069 | 3063 | /// E to E!T |
| 3070 | | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { |
| 3071 | | const zcu = self.bin_file.comp.module.?; |
| 3072 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3064 | fn airWrapErrUnionErr(func: *Func, inst: Air.Inst.Index) !void { |
| 3065 | const zcu = func.bin_file.comp.module.?; |
| 3066 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3073 | 3067 | |
| 3074 | 3068 | const eu_ty = ty_op.ty.toType(); |
| 3075 | 3069 | const pl_ty = eu_ty.errorUnionPayload(zcu); |
| 3076 | 3070 | const err_ty = eu_ty.errorUnionSet(zcu); |
| 3077 | 3071 | |
| 3078 | 3072 | const result: MCValue = result: { |
| 3079 | | if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result try self.resolveInst(ty_op.operand); |
| 3073 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result try func.resolveInst(ty_op.operand); |
| 3080 | 3074 | |
| 3081 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu)); |
| 3075 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu)); |
| 3082 | 3076 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, zcu)); |
| 3083 | 3077 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, zcu)); |
| 3084 | | try self.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, .undef); |
| 3085 | | const operand = try self.resolveInst(ty_op.operand); |
| 3086 | | try self.genSetMem(.{ .frame = frame_index }, err_off, err_ty, operand); |
| 3078 | try func.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, .undef); |
| 3079 | const operand = try func.resolveInst(ty_op.operand); |
| 3080 | try func.genSetMem(.{ .frame = frame_index }, err_off, err_ty, operand); |
| 3087 | 3081 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 3088 | 3082 | }; |
| 3089 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3083 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3090 | 3084 | } |
| 3091 | 3085 | |
| 3092 | | fn airTry(self: *Self, inst: Air.Inst.Index) !void { |
| 3093 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 3094 | | const extra = self.air.extraData(Air.Try, pl_op.payload); |
| 3095 | | const body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len]); |
| 3096 | | const operand_ty = self.typeOf(pl_op.operand); |
| 3097 | | const result = try self.genTry(inst, pl_op.operand, body, operand_ty, false); |
| 3098 | | return self.finishAir(inst, result, .{ .none, .none, .none }); |
| 3086 | fn airTry(func: *Func, inst: Air.Inst.Index) !void { |
| 3087 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 3088 | const extra = func.air.extraData(Air.Try, pl_op.payload); |
| 3089 | const body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len]); |
| 3090 | const operand_ty = func.typeOf(pl_op.operand); |
| 3091 | const result = try func.genTry(inst, pl_op.operand, body, operand_ty, false); |
| 3092 | return func.finishAir(inst, result, .{ .none, .none, .none }); |
| 3099 | 3093 | } |
| 3100 | 3094 | |
| 3101 | 3095 | fn genTry( |
| 3102 | | self: *Self, |
| 3096 | func: *Func, |
| 3103 | 3097 | inst: Air.Inst.Index, |
| 3104 | 3098 | operand: Air.Inst.Ref, |
| 3105 | 3099 | body: []const Air.Inst.Index, |
| ... | ... | @@ -3108,180 +3102,180 @@ fn genTry( |
| 3108 | 3102 | ) !MCValue { |
| 3109 | 3103 | _ = operand_is_ptr; |
| 3110 | 3104 | |
| 3111 | | const liveness_cond_br = self.liveness.getCondBr(inst); |
| 3105 | const liveness_cond_br = func.liveness.getCondBr(inst); |
| 3112 | 3106 | |
| 3113 | | const operand_mcv = try self.resolveInst(operand); |
| 3114 | | const is_err_mcv = try self.isErr(null, operand_ty, operand_mcv); |
| 3107 | const operand_mcv = try func.resolveInst(operand); |
| 3108 | const is_err_mcv = try func.isErr(null, operand_ty, operand_mcv); |
| 3115 | 3109 | |
| 3116 | 3110 | // A branch to the false section. Uses beq. 1 is the default "true" state. |
| 3117 | | const reloc = try self.condBr(Type.anyerror, is_err_mcv); |
| 3111 | const reloc = try func.condBr(Type.anyerror, is_err_mcv); |
| 3118 | 3112 | |
| 3119 | | if (self.liveness.operandDies(inst, 0)) { |
| 3120 | | if (operand.toIndex()) |operand_inst| try self.processDeath(operand_inst); |
| 3113 | if (func.liveness.operandDies(inst, 0)) { |
| 3114 | if (operand.toIndex()) |operand_inst| try func.processDeath(operand_inst); |
| 3121 | 3115 | } |
| 3122 | 3116 | |
| 3123 | | self.scope_generation += 1; |
| 3124 | | const state = try self.saveState(); |
| 3117 | func.scope_generation += 1; |
| 3118 | const state = try func.saveState(); |
| 3125 | 3119 | |
| 3126 | | for (liveness_cond_br.else_deaths) |death| try self.processDeath(death); |
| 3127 | | try self.genBody(body); |
| 3128 | | try self.restoreState(state, &.{}, .{ |
| 3120 | for (liveness_cond_br.else_deaths) |death| try func.processDeath(death); |
| 3121 | try func.genBody(body); |
| 3122 | try func.restoreState(state, &.{}, .{ |
| 3129 | 3123 | .emit_instructions = false, |
| 3130 | 3124 | .update_tracking = true, |
| 3131 | 3125 | .resurrect = true, |
| 3132 | 3126 | .close_scope = true, |
| 3133 | 3127 | }); |
| 3134 | 3128 | |
| 3135 | | self.performReloc(reloc); |
| 3129 | func.performReloc(reloc); |
| 3136 | 3130 | |
| 3137 | | for (liveness_cond_br.then_deaths) |death| try self.processDeath(death); |
| 3131 | for (liveness_cond_br.then_deaths) |death| try func.processDeath(death); |
| 3138 | 3132 | |
| 3139 | | const result = if (self.liveness.isUnused(inst)) |
| 3133 | const result = if (func.liveness.isUnused(inst)) |
| 3140 | 3134 | .unreach |
| 3141 | 3135 | else |
| 3142 | | try self.genUnwrapErrUnionPayloadMir(operand_ty, operand_mcv); |
| 3136 | try func.genUnwrapErrUnionPayloadMir(operand_ty, operand_mcv); |
| 3143 | 3137 | |
| 3144 | 3138 | return result; |
| 3145 | 3139 | } |
| 3146 | 3140 | |
| 3147 | | fn airSlicePtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3148 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3141 | fn airSlicePtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3142 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3149 | 3143 | const result = result: { |
| 3150 | | const src_mcv = try self.resolveInst(ty_op.operand); |
| 3151 | | if (self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result src_mcv; |
| 3144 | const src_mcv = try func.resolveInst(ty_op.operand); |
| 3145 | if (func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result src_mcv; |
| 3152 | 3146 | |
| 3153 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 3154 | | const dst_ty = self.typeOfIndex(inst); |
| 3155 | | try self.genCopy(dst_ty, dst_mcv, src_mcv); |
| 3147 | const dst_mcv = try func.allocRegOrMem(inst, true); |
| 3148 | const dst_ty = func.typeOfIndex(inst); |
| 3149 | try func.genCopy(dst_ty, dst_mcv, src_mcv); |
| 3156 | 3150 | break :result dst_mcv; |
| 3157 | 3151 | }; |
| 3158 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3152 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3159 | 3153 | } |
| 3160 | 3154 | |
| 3161 | | fn airSliceLen(self: *Self, inst: Air.Inst.Index) !void { |
| 3162 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3163 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3164 | | const src_mcv = try self.resolveInst(ty_op.operand); |
| 3155 | fn airSliceLen(func: *Func, inst: Air.Inst.Index) !void { |
| 3156 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3157 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3158 | const src_mcv = try func.resolveInst(ty_op.operand); |
| 3165 | 3159 | switch (src_mcv) { |
| 3166 | 3160 | .load_frame => |frame_addr| { |
| 3167 | 3161 | const len_mcv: MCValue = .{ .load_frame = .{ |
| 3168 | 3162 | .index = frame_addr.index, |
| 3169 | 3163 | .off = frame_addr.off + 8, |
| 3170 | 3164 | } }; |
| 3171 | | if (self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv; |
| 3165 | if (func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv; |
| 3172 | 3166 | |
| 3173 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 3174 | | try self.genCopy(Type.usize, dst_mcv, len_mcv); |
| 3167 | const dst_mcv = try func.allocRegOrMem(inst, true); |
| 3168 | try func.genCopy(Type.usize, dst_mcv, len_mcv); |
| 3175 | 3169 | break :result dst_mcv; |
| 3176 | 3170 | }, |
| 3177 | 3171 | .register_pair => |pair| { |
| 3178 | 3172 | const len_mcv: MCValue = .{ .register = pair[1] }; |
| 3179 | 3173 | |
| 3180 | | if (self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv; |
| 3174 | if (func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) break :result len_mcv; |
| 3181 | 3175 | |
| 3182 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 3183 | | try self.genCopy(Type.usize, dst_mcv, len_mcv); |
| 3176 | const dst_mcv = try func.allocRegOrMem(inst, true); |
| 3177 | try func.genCopy(Type.usize, dst_mcv, len_mcv); |
| 3184 | 3178 | break :result dst_mcv; |
| 3185 | 3179 | }, |
| 3186 | | else => return self.fail("TODO airSliceLen for {}", .{src_mcv}), |
| 3180 | else => return func.fail("TODO airSliceLen for {}", .{src_mcv}), |
| 3187 | 3181 | } |
| 3188 | 3182 | }; |
| 3189 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3183 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3190 | 3184 | } |
| 3191 | 3185 | |
| 3192 | | fn airPtrSliceLenPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3193 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3194 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement ptr_slice_len_ptr for {}", .{self.target.cpu.arch}); |
| 3195 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3186 | fn airPtrSliceLenPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3187 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3188 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement ptr_slice_len_ptr for {}", .{func.target.cpu.arch}); |
| 3189 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3196 | 3190 | } |
| 3197 | 3191 | |
| 3198 | | fn airPtrSlicePtrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3199 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3200 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement ptr_slice_ptr_ptr for {}", .{self.target.cpu.arch}); |
| 3201 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3192 | fn airPtrSlicePtrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3193 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3194 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement ptr_slice_ptr_ptr for {}", .{func.target.cpu.arch}); |
| 3195 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3202 | 3196 | } |
| 3203 | 3197 | |
| 3204 | | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3205 | | const zcu = self.bin_file.comp.module.?; |
| 3198 | fn airSliceElemVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3199 | const zcu = func.bin_file.comp.module.?; |
| 3206 | 3200 | const is_volatile = false; // TODO |
| 3207 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3201 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3208 | 3202 | |
| 3209 | | if (!is_volatile and self.liveness.isUnused(inst)) return self.finishAir( |
| 3203 | if (!is_volatile and func.liveness.isUnused(inst)) return func.finishAir( |
| 3210 | 3204 | inst, |
| 3211 | 3205 | .unreach, |
| 3212 | 3206 | .{ bin_op.lhs, bin_op.rhs, .none }, |
| 3213 | 3207 | ); |
| 3214 | 3208 | const result: MCValue = result: { |
| 3215 | | const slice_mcv = try self.resolveInst(bin_op.lhs); |
| 3216 | | const index_mcv = try self.resolveInst(bin_op.rhs); |
| 3209 | const slice_mcv = try func.resolveInst(bin_op.lhs); |
| 3210 | const index_mcv = try func.resolveInst(bin_op.rhs); |
| 3217 | 3211 | |
| 3218 | | const slice_ty = self.typeOf(bin_op.lhs); |
| 3212 | const slice_ty = func.typeOf(bin_op.lhs); |
| 3219 | 3213 | |
| 3220 | 3214 | const slice_ptr_field_type = slice_ty.slicePtrFieldType(zcu); |
| 3221 | 3215 | |
| 3222 | 3216 | const index_lock: ?RegisterLock = if (index_mcv == .register) |
| 3223 | | self.register_manager.lockRegAssumeUnused(index_mcv.register) |
| 3217 | func.register_manager.lockRegAssumeUnused(index_mcv.register) |
| 3224 | 3218 | else |
| 3225 | 3219 | null; |
| 3226 | | defer if (index_lock) |reg| self.register_manager.unlockReg(reg); |
| 3220 | defer if (index_lock) |reg| func.register_manager.unlockReg(reg); |
| 3227 | 3221 | |
| 3228 | 3222 | const base_mcv: MCValue = switch (slice_mcv) { |
| 3229 | 3223 | .load_frame, |
| 3230 | 3224 | .load_symbol, |
| 3231 | | => .{ .register = try self.copyToTmpRegister(slice_ptr_field_type, slice_mcv) }, |
| 3232 | | else => return self.fail("TODO slice_elem_val when slice is {}", .{slice_mcv}), |
| 3225 | => .{ .register = try func.copyToTmpRegister(slice_ptr_field_type, slice_mcv) }, |
| 3226 | else => return func.fail("TODO slice_elem_val when slice is {}", .{slice_mcv}), |
| 3233 | 3227 | }; |
| 3234 | 3228 | |
| 3235 | | const dest = try self.allocRegOrMem(inst, true); |
| 3236 | | const addr = try self.binOp(.ptr_add, base_mcv, slice_ptr_field_type, index_mcv, Type.usize); |
| 3237 | | try self.load(dest, addr, slice_ptr_field_type); |
| 3229 | const dest = try func.allocRegOrMem(inst, true); |
| 3230 | const addr = try func.binOp(.ptr_add, base_mcv, slice_ptr_field_type, index_mcv, Type.usize); |
| 3231 | try func.load(dest, addr, slice_ptr_field_type); |
| 3238 | 3232 | |
| 3239 | 3233 | break :result dest; |
| 3240 | 3234 | }; |
| 3241 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3235 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3242 | 3236 | } |
| 3243 | 3237 | |
| 3244 | | fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3245 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3246 | | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3247 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement slice_elem_ptr for {}", .{self.target.cpu.arch}); |
| 3248 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 3238 | fn airSliceElemPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3239 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3240 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3241 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement slice_elem_ptr for {}", .{func.target.cpu.arch}); |
| 3242 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 3249 | 3243 | } |
| 3250 | 3244 | |
| 3251 | | fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3252 | | const zcu = self.bin_file.comp.module.?; |
| 3253 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3254 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3255 | | const array_ty = self.typeOf(bin_op.lhs); |
| 3256 | | const array_mcv = try self.resolveInst(bin_op.lhs); |
| 3245 | fn airArrayElemVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3246 | const zcu = func.bin_file.comp.module.?; |
| 3247 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3248 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3249 | const array_ty = func.typeOf(bin_op.lhs); |
| 3250 | const array_mcv = try func.resolveInst(bin_op.lhs); |
| 3257 | 3251 | |
| 3258 | | const index_mcv = try self.resolveInst(bin_op.rhs); |
| 3259 | | const index_ty = self.typeOf(bin_op.rhs); |
| 3252 | const index_mcv = try func.resolveInst(bin_op.rhs); |
| 3253 | const index_ty = func.typeOf(bin_op.rhs); |
| 3260 | 3254 | |
| 3261 | 3255 | const elem_ty = array_ty.childType(zcu); |
| 3262 | 3256 | const elem_abi_size = elem_ty.abiSize(zcu); |
| 3263 | 3257 | |
| 3264 | | const addr_reg, const addr_reg_lock = try self.allocReg(.int); |
| 3265 | | defer self.register_manager.unlockReg(addr_reg_lock); |
| 3258 | const addr_reg, const addr_reg_lock = try func.allocReg(.int); |
| 3259 | defer func.register_manager.unlockReg(addr_reg_lock); |
| 3266 | 3260 | |
| 3267 | 3261 | switch (array_mcv) { |
| 3268 | 3262 | .register => { |
| 3269 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initType(array_ty, zcu)); |
| 3270 | | try self.genSetMem(.{ .frame = frame_index }, 0, array_ty, array_mcv); |
| 3271 | | try self.genSetReg(Type.usize, addr_reg, .{ .lea_frame = .{ .index = frame_index } }); |
| 3263 | const frame_index = try func.allocFrameIndex(FrameAlloc.initType(array_ty, zcu)); |
| 3264 | try func.genSetMem(.{ .frame = frame_index }, 0, array_ty, array_mcv); |
| 3265 | try func.genSetReg(Type.usize, addr_reg, .{ .lea_frame = .{ .index = frame_index } }); |
| 3272 | 3266 | }, |
| 3273 | 3267 | .load_frame => |frame_addr| { |
| 3274 | | try self.genSetReg(Type.usize, addr_reg, .{ .lea_frame = frame_addr }); |
| 3268 | try func.genSetReg(Type.usize, addr_reg, .{ .lea_frame = frame_addr }); |
| 3275 | 3269 | }, |
| 3276 | | else => try self.genSetReg(Type.usize, addr_reg, array_mcv.address()), |
| 3270 | else => try func.genSetReg(Type.usize, addr_reg, array_mcv.address()), |
| 3277 | 3271 | } |
| 3278 | 3272 | |
| 3279 | | const offset_reg = try self.elemOffset(index_ty, index_mcv, elem_abi_size); |
| 3280 | | const offset_lock = self.register_manager.lockRegAssumeUnused(offset_reg); |
| 3281 | | defer self.register_manager.unlockReg(offset_lock); |
| 3273 | const offset_reg = try func.elemOffset(index_ty, index_mcv, elem_abi_size); |
| 3274 | const offset_lock = func.register_manager.lockRegAssumeUnused(offset_reg); |
| 3275 | defer func.register_manager.unlockReg(offset_lock); |
| 3282 | 3276 | |
| 3283 | | const dst_mcv = try self.allocRegOrMem(inst, false); |
| 3284 | | _ = try self.addInst(.{ |
| 3277 | const dst_mcv = try func.allocRegOrMem(inst, false); |
| 3278 | _ = try func.addInst(.{ |
| 3285 | 3279 | .tag = .add, |
| 3286 | 3280 | .ops = .rrr, |
| 3287 | 3281 | .data = .{ .r_type = .{ |
| ... | ... | @@ -3290,138 +3284,138 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3290 | 3284 | .rs2 = addr_reg, |
| 3291 | 3285 | } }, |
| 3292 | 3286 | }); |
| 3293 | | try self.genCopy(elem_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } }); |
| 3287 | try func.genCopy(elem_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } }); |
| 3294 | 3288 | break :result dst_mcv; |
| 3295 | 3289 | }; |
| 3296 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3290 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3297 | 3291 | } |
| 3298 | 3292 | |
| 3299 | | fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3293 | fn airPtrElemVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3300 | 3294 | const is_volatile = false; // TODO |
| 3301 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3302 | | const result: MCValue = if (!is_volatile and self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement ptr_elem_val for {}", .{self.target.cpu.arch}); |
| 3303 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3295 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3296 | const result: MCValue = if (!is_volatile and func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement ptr_elem_val for {}", .{func.target.cpu.arch}); |
| 3297 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3304 | 3298 | } |
| 3305 | 3299 | |
| 3306 | | fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3307 | | const zcu = self.bin_file.comp.module.?; |
| 3308 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3309 | | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3300 | fn airPtrElemPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3301 | const zcu = func.bin_file.comp.module.?; |
| 3302 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3303 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3310 | 3304 | |
| 3311 | 3305 | const result = result: { |
| 3312 | | const elem_ptr_ty = self.typeOfIndex(inst); |
| 3313 | | const base_ptr_ty = self.typeOf(extra.lhs); |
| 3306 | const elem_ptr_ty = func.typeOfIndex(inst); |
| 3307 | const base_ptr_ty = func.typeOf(extra.lhs); |
| 3314 | 3308 | |
| 3315 | | const base_ptr_mcv = try self.resolveInst(extra.lhs); |
| 3309 | const base_ptr_mcv = try func.resolveInst(extra.lhs); |
| 3316 | 3310 | const base_ptr_lock: ?RegisterLock = switch (base_ptr_mcv) { |
| 3317 | | .register => |reg| self.register_manager.lockRegAssumeUnused(reg), |
| 3311 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 3318 | 3312 | else => null, |
| 3319 | 3313 | }; |
| 3320 | | defer if (base_ptr_lock) |lock| self.register_manager.unlockReg(lock); |
| 3314 | defer if (base_ptr_lock) |lock| func.register_manager.unlockReg(lock); |
| 3321 | 3315 | |
| 3322 | 3316 | if (elem_ptr_ty.ptrInfo(zcu).flags.vector_index != .none) { |
| 3323 | | break :result if (self.reuseOperand(inst, extra.lhs, 0, base_ptr_mcv)) |
| 3317 | break :result if (func.reuseOperand(inst, extra.lhs, 0, base_ptr_mcv)) |
| 3324 | 3318 | base_ptr_mcv |
| 3325 | 3319 | else |
| 3326 | | try self.copyToNewRegister(inst, base_ptr_mcv); |
| 3320 | try func.copyToNewRegister(inst, base_ptr_mcv); |
| 3327 | 3321 | } |
| 3328 | 3322 | |
| 3329 | 3323 | const elem_ty = base_ptr_ty.elemType2(zcu); |
| 3330 | 3324 | const elem_abi_size = elem_ty.abiSize(zcu); |
| 3331 | | const index_ty = self.typeOf(extra.rhs); |
| 3332 | | const index_mcv = try self.resolveInst(extra.rhs); |
| 3325 | const index_ty = func.typeOf(extra.rhs); |
| 3326 | const index_mcv = try func.resolveInst(extra.rhs); |
| 3333 | 3327 | const index_lock: ?RegisterLock = switch (index_mcv) { |
| 3334 | | .register => |reg| self.register_manager.lockRegAssumeUnused(reg), |
| 3328 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 3335 | 3329 | else => null, |
| 3336 | 3330 | }; |
| 3337 | | defer if (index_lock) |lock| self.register_manager.unlockReg(lock); |
| 3331 | defer if (index_lock) |lock| func.register_manager.unlockReg(lock); |
| 3338 | 3332 | |
| 3339 | | const offset_reg = try self.elemOffset(index_ty, index_mcv, elem_abi_size); |
| 3340 | | const offset_reg_lock = self.register_manager.lockRegAssumeUnused(offset_reg); |
| 3341 | | defer self.register_manager.unlockReg(offset_reg_lock); |
| 3333 | const offset_reg = try func.elemOffset(index_ty, index_mcv, elem_abi_size); |
| 3334 | const offset_reg_lock = func.register_manager.lockRegAssumeUnused(offset_reg); |
| 3335 | defer func.register_manager.unlockReg(offset_reg_lock); |
| 3342 | 3336 | |
| 3343 | | break :result try self.binOp(.ptr_add, base_ptr_mcv, base_ptr_ty, .{ .register = offset_reg }, base_ptr_ty); |
| 3337 | break :result try func.binOp(.ptr_add, base_ptr_mcv, base_ptr_ty, .{ .register = offset_reg }, base_ptr_ty); |
| 3344 | 3338 | }; |
| 3345 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 3339 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 3346 | 3340 | } |
| 3347 | 3341 | |
| 3348 | | fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) !void { |
| 3349 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3342 | fn airSetUnionTag(func: *Func, inst: Air.Inst.Index) !void { |
| 3343 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3350 | 3344 | _ = bin_op; |
| 3351 | | return self.fail("TODO implement airSetUnionTag for {}", .{self.target.cpu.arch}); |
| 3352 | | // return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3345 | return func.fail("TODO implement airSetUnionTag for {}", .{func.target.cpu.arch}); |
| 3346 | // return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3353 | 3347 | } |
| 3354 | 3348 | |
| 3355 | | fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) !void { |
| 3356 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3357 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airGetUnionTag for {}", .{self.target.cpu.arch}); |
| 3358 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3349 | fn airGetUnionTag(func: *Func, inst: Air.Inst.Index) !void { |
| 3350 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3351 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airGetUnionTag for {}", .{func.target.cpu.arch}); |
| 3352 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3359 | 3353 | } |
| 3360 | 3354 | |
| 3361 | | fn airClz(self: *Self, inst: Air.Inst.Index) !void { |
| 3362 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3363 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airClz for {}", .{self.target.cpu.arch}); |
| 3364 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3355 | fn airClz(func: *Func, inst: Air.Inst.Index) !void { |
| 3356 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3357 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airClz for {}", .{func.target.cpu.arch}); |
| 3358 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3365 | 3359 | } |
| 3366 | 3360 | |
| 3367 | | fn airCtz(self: *Self, inst: Air.Inst.Index) !void { |
| 3368 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3361 | fn airCtz(func: *Func, inst: Air.Inst.Index) !void { |
| 3362 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3369 | 3363 | _ = ty_op; |
| 3370 | | return self.fail("TODO: finish ctz", .{}); |
| 3364 | return func.fail("TODO: finish ctz", .{}); |
| 3371 | 3365 | } |
| 3372 | 3366 | |
| 3373 | | fn airPopcount(self: *Self, inst: Air.Inst.Index) !void { |
| 3374 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3375 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airPopcount for {}", .{self.target.cpu.arch}); |
| 3376 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3367 | fn airPopcount(func: *Func, inst: Air.Inst.Index) !void { |
| 3368 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3369 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airPopcount for {}", .{func.target.cpu.arch}); |
| 3370 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3377 | 3371 | } |
| 3378 | 3372 | |
| 3379 | | fn airAbs(self: *Self, inst: Air.Inst.Index) !void { |
| 3380 | | const zcu = self.bin_file.comp.module.?; |
| 3381 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3382 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3383 | | const ty = self.typeOf(ty_op.operand); |
| 3373 | fn airAbs(func: *Func, inst: Air.Inst.Index) !void { |
| 3374 | const zcu = func.bin_file.comp.module.?; |
| 3375 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3376 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3377 | const ty = func.typeOf(ty_op.operand); |
| 3384 | 3378 | const scalar_ty = ty.scalarType(zcu); |
| 3385 | | const operand = try self.resolveInst(ty_op.operand); |
| 3379 | const operand = try func.resolveInst(ty_op.operand); |
| 3386 | 3380 | _ = operand; |
| 3387 | 3381 | |
| 3388 | 3382 | switch (scalar_ty.zigTypeTag(zcu)) { |
| 3389 | 3383 | .Int => if (ty.zigTypeTag(zcu) == .Vector) { |
| 3390 | | return self.fail("TODO implement airAbs for {}", .{ty.fmt(zcu)}); |
| 3384 | return func.fail("TODO implement airAbs for {}", .{ty.fmt(zcu)}); |
| 3391 | 3385 | } else { |
| 3392 | | return self.fail("TODO: implement airAbs for Int", .{}); |
| 3386 | return func.fail("TODO: implement airAbs for Int", .{}); |
| 3393 | 3387 | }, |
| 3394 | | else => return self.fail("TODO: implement airAbs {}", .{scalar_ty.fmt(zcu)}), |
| 3388 | else => return func.fail("TODO: implement airAbs {}", .{scalar_ty.fmt(zcu)}), |
| 3395 | 3389 | } |
| 3396 | 3390 | |
| 3397 | 3391 | break :result .{.unreach}; |
| 3398 | 3392 | }; |
| 3399 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3393 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3400 | 3394 | } |
| 3401 | 3395 | |
| 3402 | | fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 3403 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3404 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3405 | | const zcu = self.bin_file.comp.module.?; |
| 3406 | | const ty = self.typeOf(ty_op.operand); |
| 3407 | | const operand = try self.resolveInst(ty_op.operand); |
| 3396 | fn airByteSwap(func: *Func, inst: Air.Inst.Index) !void { |
| 3397 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3398 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3399 | const zcu = func.bin_file.comp.module.?; |
| 3400 | const ty = func.typeOf(ty_op.operand); |
| 3401 | const operand = try func.resolveInst(ty_op.operand); |
| 3408 | 3402 | |
| 3409 | 3403 | const int_bits = ty.intInfo(zcu).bits; |
| 3410 | 3404 | |
| 3411 | 3405 | // bytes are no-op |
| 3412 | | if (int_bits == 8 and self.reuseOperand(inst, ty_op.operand, 0, operand)) { |
| 3413 | | return self.finishAir(inst, operand, .{ ty_op.operand, .none, .none }); |
| 3406 | if (int_bits == 8 and func.reuseOperand(inst, ty_op.operand, 0, operand)) { |
| 3407 | return func.finishAir(inst, operand, .{ ty_op.operand, .none, .none }); |
| 3414 | 3408 | } |
| 3415 | 3409 | |
| 3416 | | const dest_mcv = try self.copyToNewRegister(inst, operand); |
| 3410 | const dest_mcv = try func.copyToNewRegister(inst, operand); |
| 3417 | 3411 | const dest_reg = dest_mcv.register; |
| 3418 | 3412 | |
| 3419 | 3413 | switch (int_bits) { |
| 3420 | 3414 | 16 => { |
| 3421 | | const temp_reg, const temp_lock = try self.allocReg(.int); |
| 3422 | | defer self.register_manager.unlockReg(temp_lock); |
| 3415 | const temp_reg, const temp_lock = try func.allocReg(.int); |
| 3416 | defer func.register_manager.unlockReg(temp_lock); |
| 3423 | 3417 | |
| 3424 | | _ = try self.addInst(.{ |
| 3418 | _ = try func.addInst(.{ |
| 3425 | 3419 | .tag = .srli, |
| 3426 | 3420 | .ops = .rri, |
| 3427 | 3421 | .data = .{ .i_type = .{ |
| ... | ... | @@ -3431,7 +3425,7 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 3431 | 3425 | } }, |
| 3432 | 3426 | }); |
| 3433 | 3427 | |
| 3434 | | _ = try self.addInst(.{ |
| 3428 | _ = try func.addInst(.{ |
| 3435 | 3429 | .tag = .slli, |
| 3436 | 3430 | .ops = .rri, |
| 3437 | 3431 | .data = .{ .i_type = .{ |
| ... | ... | @@ -3440,7 +3434,7 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 3440 | 3434 | .rs1 = dest_reg, |
| 3441 | 3435 | } }, |
| 3442 | 3436 | }); |
| 3443 | | _ = try self.addInst(.{ |
| 3437 | _ = try func.addInst(.{ |
| 3444 | 3438 | .tag = .@"or", |
| 3445 | 3439 | .ops = .rri, |
| 3446 | 3440 | .data = .{ .r_type = .{ |
| ... | ... | @@ -3450,49 +3444,49 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 3450 | 3444 | } }, |
| 3451 | 3445 | }); |
| 3452 | 3446 | }, |
| 3453 | | else => return self.fail("TODO: {d} bits for airByteSwap", .{int_bits}), |
| 3447 | else => return func.fail("TODO: {d} bits for airByteSwap", .{int_bits}), |
| 3454 | 3448 | } |
| 3455 | 3449 | |
| 3456 | 3450 | break :result dest_mcv; |
| 3457 | 3451 | }; |
| 3458 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3452 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3459 | 3453 | } |
| 3460 | 3454 | |
| 3461 | | fn airBitReverse(self: *Self, inst: Air.Inst.Index) !void { |
| 3462 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3463 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airBitReverse for {}", .{self.target.cpu.arch}); |
| 3464 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3455 | fn airBitReverse(func: *Func, inst: Air.Inst.Index) !void { |
| 3456 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3457 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airBitReverse for {}", .{func.target.cpu.arch}); |
| 3458 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3465 | 3459 | } |
| 3466 | 3460 | |
| 3467 | | fn airUnaryMath(self: *Self, inst: Air.Inst.Index) !void { |
| 3468 | | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 3469 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3470 | | const result: MCValue = if (self.liveness.isUnused(inst)) |
| 3461 | fn airUnaryMath(func: *Func, inst: Air.Inst.Index) !void { |
| 3462 | const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 3463 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3464 | const result: MCValue = if (func.liveness.isUnused(inst)) |
| 3471 | 3465 | .unreach |
| 3472 | 3466 | else |
| 3473 | | return self.fail("TODO implementairUnaryMath {s} for {}", .{ @tagName(tag), self.target.cpu.arch }); |
| 3474 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 3467 | return func.fail("TODO implementairUnaryMath {s} for {}", .{ @tagName(tag), func.target.cpu.arch }); |
| 3468 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 3475 | 3469 | } |
| 3476 | 3470 | |
| 3477 | 3471 | fn reuseOperand( |
| 3478 | | self: *Self, |
| 3472 | func: *Func, |
| 3479 | 3473 | inst: Air.Inst.Index, |
| 3480 | 3474 | operand: Air.Inst.Ref, |
| 3481 | 3475 | op_index: Liveness.OperandInt, |
| 3482 | 3476 | mcv: MCValue, |
| 3483 | 3477 | ) bool { |
| 3484 | | return self.reuseOperandAdvanced(inst, operand, op_index, mcv, inst); |
| 3478 | return func.reuseOperandAdvanced(inst, operand, op_index, mcv, inst); |
| 3485 | 3479 | } |
| 3486 | 3480 | |
| 3487 | 3481 | fn reuseOperandAdvanced( |
| 3488 | | self: *Self, |
| 3482 | func: *Func, |
| 3489 | 3483 | inst: Air.Inst.Index, |
| 3490 | 3484 | operand: Air.Inst.Ref, |
| 3491 | 3485 | op_index: Liveness.OperandInt, |
| 3492 | 3486 | mcv: MCValue, |
| 3493 | 3487 | maybe_tracked_inst: ?Air.Inst.Index, |
| 3494 | 3488 | ) bool { |
| 3495 | | if (!self.liveness.operandDies(inst, op_index)) |
| 3489 | if (!func.liveness.operandDies(inst, op_index)) |
| 3496 | 3490 | return false; |
| 3497 | 3491 | |
| 3498 | 3492 | switch (mcv) { |
| ... | ... | @@ -3502,59 +3496,59 @@ fn reuseOperandAdvanced( |
| 3502 | 3496 | // If it's in the registers table, need to associate the register(s) with the |
| 3503 | 3497 | // new instruction. |
| 3504 | 3498 | if (maybe_tracked_inst) |tracked_inst| { |
| 3505 | | if (!self.register_manager.isRegFree(reg)) { |
| 3499 | if (!func.register_manager.isRegFree(reg)) { |
| 3506 | 3500 | if (RegisterManager.indexOfRegIntoTracked(reg)) |index| { |
| 3507 | | self.register_manager.registers[index] = tracked_inst; |
| 3501 | func.register_manager.registers[index] = tracked_inst; |
| 3508 | 3502 | } |
| 3509 | 3503 | } |
| 3510 | | } else self.register_manager.freeReg(reg); |
| 3504 | } else func.register_manager.freeReg(reg); |
| 3511 | 3505 | }, |
| 3512 | 3506 | .load_frame => |frame_addr| if (frame_addr.index.isNamed()) return false, |
| 3513 | 3507 | else => return false, |
| 3514 | 3508 | } |
| 3515 | 3509 | |
| 3516 | 3510 | // Prevent the operand deaths processing code from deallocating it. |
| 3517 | | self.liveness.clearOperandDeath(inst, op_index); |
| 3511 | func.liveness.clearOperandDeath(inst, op_index); |
| 3518 | 3512 | const op_inst = operand.toIndex().?; |
| 3519 | | self.getResolvedInstValue(op_inst).reuse(self, maybe_tracked_inst, op_inst); |
| 3513 | func.getResolvedInstValue(op_inst).reuse(func, maybe_tracked_inst, op_inst); |
| 3520 | 3514 | |
| 3521 | 3515 | return true; |
| 3522 | 3516 | } |
| 3523 | 3517 | |
| 3524 | | fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 3525 | | const zcu = self.bin_file.comp.module.?; |
| 3526 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3527 | | const elem_ty = self.typeOfIndex(inst); |
| 3518 | fn airLoad(func: *Func, inst: Air.Inst.Index) !void { |
| 3519 | const zcu = func.bin_file.comp.module.?; |
| 3520 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3521 | const elem_ty = func.typeOfIndex(inst); |
| 3528 | 3522 | |
| 3529 | 3523 | const result: MCValue = result: { |
| 3530 | 3524 | if (!elem_ty.hasRuntimeBits(zcu)) |
| 3531 | 3525 | break :result .none; |
| 3532 | 3526 | |
| 3533 | | const ptr = try self.resolveInst(ty_op.operand); |
| 3534 | | const is_volatile = self.typeOf(ty_op.operand).isVolatilePtr(zcu); |
| 3535 | | if (self.liveness.isUnused(inst) and !is_volatile) |
| 3527 | const ptr = try func.resolveInst(ty_op.operand); |
| 3528 | const is_volatile = func.typeOf(ty_op.operand).isVolatilePtr(zcu); |
| 3529 | if (func.liveness.isUnused(inst) and !is_volatile) |
| 3536 | 3530 | break :result .unreach; |
| 3537 | 3531 | |
| 3538 | 3532 | const elem_size = elem_ty.abiSize(zcu); |
| 3539 | 3533 | |
| 3540 | 3534 | const dst_mcv: MCValue = blk: { |
| 3541 | 3535 | // Pointer is 8 bytes, and if the element is more than that, we cannot reuse it. |
| 3542 | | if (elem_size <= 8 and self.reuseOperand(inst, ty_op.operand, 0, ptr)) { |
| 3536 | if (elem_size <= 8 and func.reuseOperand(inst, ty_op.operand, 0, ptr)) { |
| 3543 | 3537 | // The MCValue that holds the pointer can be re-used as the value. |
| 3544 | 3538 | break :blk ptr; |
| 3545 | 3539 | } else { |
| 3546 | | break :blk try self.allocRegOrMem(inst, true); |
| 3540 | break :blk try func.allocRegOrMem(inst, true); |
| 3547 | 3541 | } |
| 3548 | 3542 | }; |
| 3549 | 3543 | |
| 3550 | | try self.load(dst_mcv, ptr, self.typeOf(ty_op.operand)); |
| 3544 | try func.load(dst_mcv, ptr, func.typeOf(ty_op.operand)); |
| 3551 | 3545 | break :result dst_mcv; |
| 3552 | 3546 | }; |
| 3553 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3547 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3554 | 3548 | } |
| 3555 | 3549 | |
| 3556 | | fn load(self: *Self, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerError!void { |
| 3557 | | const zcu = self.bin_file.comp.module.?; |
| 3550 | fn load(func: *Func, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerError!void { |
| 3551 | const zcu = func.bin_file.comp.module.?; |
| 3558 | 3552 | const dst_ty = ptr_ty.childType(zcu); |
| 3559 | 3553 | |
| 3560 | 3554 | log.debug("loading {}:{} into {}", .{ ptr_mcv, ptr_ty.fmt(zcu), dst_mcv }); |
| ... | ... | @@ -3573,43 +3567,43 @@ fn load(self: *Self, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerErro |
| 3573 | 3567 | .register_offset, |
| 3574 | 3568 | .lea_frame, |
| 3575 | 3569 | .lea_symbol, |
| 3576 | | => try self.genCopy(dst_ty, dst_mcv, ptr_mcv.deref()), |
| 3570 | => try func.genCopy(dst_ty, dst_mcv, ptr_mcv.deref()), |
| 3577 | 3571 | |
| 3578 | 3572 | .memory, |
| 3579 | 3573 | .indirect, |
| 3580 | 3574 | .load_symbol, |
| 3581 | 3575 | .load_frame, |
| 3582 | 3576 | => { |
| 3583 | | const addr_reg = try self.copyToTmpRegister(ptr_ty, ptr_mcv); |
| 3584 | | const addr_lock = self.register_manager.lockRegAssumeUnused(addr_reg); |
| 3585 | | defer self.register_manager.unlockReg(addr_lock); |
| 3577 | const addr_reg = try func.copyToTmpRegister(ptr_ty, ptr_mcv); |
| 3578 | const addr_lock = func.register_manager.lockRegAssumeUnused(addr_reg); |
| 3579 | defer func.register_manager.unlockReg(addr_lock); |
| 3586 | 3580 | |
| 3587 | | try self.genCopy(dst_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } }); |
| 3581 | try func.genCopy(dst_ty, dst_mcv, .{ .indirect = .{ .reg = addr_reg } }); |
| 3588 | 3582 | }, |
| 3589 | | .air_ref => |ptr_ref| try self.load(dst_mcv, try self.resolveInst(ptr_ref), ptr_ty), |
| 3583 | .air_ref => |ptr_ref| try func.load(dst_mcv, try func.resolveInst(ptr_ref), ptr_ty), |
| 3590 | 3584 | } |
| 3591 | 3585 | } |
| 3592 | 3586 | |
| 3593 | | fn airStore(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 3587 | fn airStore(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 3594 | 3588 | if (safety) { |
| 3595 | 3589 | // TODO if the value is undef, write 0xaa bytes to dest |
| 3596 | 3590 | } else { |
| 3597 | 3591 | // TODO if the value is undef, don't lower this instruction |
| 3598 | 3592 | } |
| 3599 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3600 | | const ptr = try self.resolveInst(bin_op.lhs); |
| 3601 | | const value = try self.resolveInst(bin_op.rhs); |
| 3602 | | const ptr_ty = self.typeOf(bin_op.lhs); |
| 3603 | | const value_ty = self.typeOf(bin_op.rhs); |
| 3593 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3594 | const ptr = try func.resolveInst(bin_op.lhs); |
| 3595 | const value = try func.resolveInst(bin_op.rhs); |
| 3596 | const ptr_ty = func.typeOf(bin_op.lhs); |
| 3597 | const value_ty = func.typeOf(bin_op.rhs); |
| 3604 | 3598 | |
| 3605 | | try self.store(ptr, value, ptr_ty, value_ty); |
| 3599 | try func.store(ptr, value, ptr_ty, value_ty); |
| 3606 | 3600 | |
| 3607 | | return self.finishAir(inst, .none, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3601 | return func.finishAir(inst, .none, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 3608 | 3602 | } |
| 3609 | 3603 | |
| 3610 | 3604 | /// Loads `value` into the "payload" of `pointer`. |
| 3611 | | fn store(self: *Self, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type, src_ty: Type) !void { |
| 3612 | | const zcu = self.bin_file.comp.module.?; |
| 3605 | fn store(func: *Func, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type, src_ty: Type) !void { |
| 3606 | const zcu = func.bin_file.comp.module.?; |
| 3613 | 3607 | |
| 3614 | 3608 | log.debug("storing {}:{} in {}:{}", .{ src_mcv, src_ty.fmt(zcu), ptr_mcv, ptr_ty.fmt(zcu) }); |
| 3615 | 3609 | |
| ... | ... | @@ -3626,40 +3620,40 @@ fn store(self: *Self, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type, src_ty: |
| 3626 | 3620 | .register_offset, |
| 3627 | 3621 | .lea_symbol, |
| 3628 | 3622 | .lea_frame, |
| 3629 | | => try self.genCopy(src_ty, ptr_mcv.deref(), src_mcv), |
| 3623 | => try func.genCopy(src_ty, ptr_mcv.deref(), src_mcv), |
| 3630 | 3624 | |
| 3631 | 3625 | .memory, |
| 3632 | 3626 | .indirect, |
| 3633 | 3627 | .load_symbol, |
| 3634 | 3628 | .load_frame, |
| 3635 | 3629 | => { |
| 3636 | | const addr_reg = try self.copyToTmpRegister(ptr_ty, ptr_mcv); |
| 3637 | | const addr_lock = self.register_manager.lockRegAssumeUnused(addr_reg); |
| 3638 | | defer self.register_manager.unlockReg(addr_lock); |
| 3630 | const addr_reg = try func.copyToTmpRegister(ptr_ty, ptr_mcv); |
| 3631 | const addr_lock = func.register_manager.lockRegAssumeUnused(addr_reg); |
| 3632 | defer func.register_manager.unlockReg(addr_lock); |
| 3639 | 3633 | |
| 3640 | | try self.genCopy(src_ty, .{ .indirect = .{ .reg = addr_reg } }, src_mcv); |
| 3634 | try func.genCopy(src_ty, .{ .indirect = .{ .reg = addr_reg } }, src_mcv); |
| 3641 | 3635 | }, |
| 3642 | | .air_ref => |ptr_ref| try self.store(try self.resolveInst(ptr_ref), src_mcv, ptr_ty, src_ty), |
| 3636 | .air_ref => |ptr_ref| try func.store(try func.resolveInst(ptr_ref), src_mcv, ptr_ty, src_ty), |
| 3643 | 3637 | } |
| 3644 | 3638 | } |
| 3645 | 3639 | |
| 3646 | | fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3647 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3648 | | const extra = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| 3649 | | const result = try self.structFieldPtr(inst, extra.struct_operand, extra.field_index); |
| 3650 | | return self.finishAir(inst, result, .{ extra.struct_operand, .none, .none }); |
| 3640 | fn airStructFieldPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3641 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3642 | const extra = func.air.extraData(Air.StructField, ty_pl.payload).data; |
| 3643 | const result = try func.structFieldPtr(inst, extra.struct_operand, extra.field_index); |
| 3644 | return func.finishAir(inst, result, .{ extra.struct_operand, .none, .none }); |
| 3651 | 3645 | } |
| 3652 | 3646 | |
| 3653 | | fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void { |
| 3654 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3655 | | const result = try self.structFieldPtr(inst, ty_op.operand, index); |
| 3656 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3647 | fn airStructFieldPtrIndex(func: *Func, inst: Air.Inst.Index, index: u8) !void { |
| 3648 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3649 | const result = try func.structFieldPtr(inst, ty_op.operand, index); |
| 3650 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3657 | 3651 | } |
| 3658 | 3652 | |
| 3659 | | fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue { |
| 3660 | | const zcu = self.bin_file.comp.module.?; |
| 3661 | | const ptr_field_ty = self.typeOfIndex(inst); |
| 3662 | | const ptr_container_ty = self.typeOf(operand); |
| 3653 | fn structFieldPtr(func: *Func, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue { |
| 3654 | const zcu = func.bin_file.comp.module.?; |
| 3655 | const ptr_field_ty = func.typeOfIndex(inst); |
| 3656 | const ptr_container_ty = func.typeOf(operand); |
| 3663 | 3657 | const ptr_container_ty_info = ptr_container_ty.ptrInfo(zcu); |
| 3664 | 3658 | const container_ty = ptr_container_ty.childType(zcu); |
| 3665 | 3659 | |
| ... | ... | @@ -3672,27 +3666,27 @@ fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, inde |
| 3672 | 3666 | else |
| 3673 | 3667 | @intCast(container_ty.structFieldOffset(index, zcu)); |
| 3674 | 3668 | |
| 3675 | | const src_mcv = try self.resolveInst(operand); |
| 3669 | const src_mcv = try func.resolveInst(operand); |
| 3676 | 3670 | const dst_mcv = if (switch (src_mcv) { |
| 3677 | 3671 | .immediate, .lea_frame => true, |
| 3678 | | .register, .register_offset => self.reuseOperand(inst, operand, 0, src_mcv), |
| 3672 | .register, .register_offset => func.reuseOperand(inst, operand, 0, src_mcv), |
| 3679 | 3673 | else => false, |
| 3680 | | }) src_mcv else try self.copyToNewRegister(inst, src_mcv); |
| 3674 | }) src_mcv else try func.copyToNewRegister(inst, src_mcv); |
| 3681 | 3675 | return dst_mcv.offset(field_offset); |
| 3682 | 3676 | } |
| 3683 | 3677 | |
| 3684 | | fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3685 | | const mod = self.bin_file.comp.module.?; |
| 3678 | fn airStructFieldVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3679 | const mod = func.bin_file.comp.module.?; |
| 3686 | 3680 | |
| 3687 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3688 | | const extra = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| 3681 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3682 | const extra = func.air.extraData(Air.StructField, ty_pl.payload).data; |
| 3689 | 3683 | const operand = extra.struct_operand; |
| 3690 | 3684 | const index = extra.field_index; |
| 3691 | 3685 | |
| 3692 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3693 | | const zcu = self.bin_file.comp.module.?; |
| 3694 | | const src_mcv = try self.resolveInst(operand); |
| 3695 | | const struct_ty = self.typeOf(operand); |
| 3686 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3687 | const zcu = func.bin_file.comp.module.?; |
| 3688 | const src_mcv = try func.resolveInst(operand); |
| 3689 | const struct_ty = func.typeOf(operand); |
| 3696 | 3690 | const field_ty = struct_ty.structFieldType(index, zcu); |
| 3697 | 3691 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .none; |
| 3698 | 3692 | |
| ... | ... | @@ -3707,20 +3701,20 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3707 | 3701 | switch (src_mcv) { |
| 3708 | 3702 | .dead, .unreach => unreachable, |
| 3709 | 3703 | .register => |src_reg| { |
| 3710 | | const src_reg_lock = self.register_manager.lockRegAssumeUnused(src_reg); |
| 3711 | | defer self.register_manager.unlockReg(src_reg_lock); |
| 3704 | const src_reg_lock = func.register_manager.lockRegAssumeUnused(src_reg); |
| 3705 | defer func.register_manager.unlockReg(src_reg_lock); |
| 3712 | 3706 | |
| 3713 | 3707 | const dst_reg = if (field_off == 0) |
| 3714 | | (try self.copyToNewRegister(inst, src_mcv)).register |
| 3708 | (try func.copyToNewRegister(inst, src_mcv)).register |
| 3715 | 3709 | else |
| 3716 | | try self.copyToTmpRegister(Type.usize, .{ .register = src_reg }); |
| 3710 | try func.copyToTmpRegister(Type.usize, .{ .register = src_reg }); |
| 3717 | 3711 | |
| 3718 | 3712 | const dst_mcv: MCValue = .{ .register = dst_reg }; |
| 3719 | | const dst_lock = self.register_manager.lockReg(dst_reg); |
| 3720 | | defer if (dst_lock) |lock| self.register_manager.unlockReg(lock); |
| 3713 | const dst_lock = func.register_manager.lockReg(dst_reg); |
| 3714 | defer if (dst_lock) |lock| func.register_manager.unlockReg(lock); |
| 3721 | 3715 | |
| 3722 | 3716 | if (field_off > 0) { |
| 3723 | | _ = try self.addInst(.{ |
| 3717 | _ = try func.addInst(.{ |
| 3724 | 3718 | .tag = .srli, |
| 3725 | 3719 | .ops = .rri, |
| 3726 | 3720 | .data = .{ .i_type = .{ |
| ... | ... | @@ -3731,7 +3725,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3731 | 3725 | }); |
| 3732 | 3726 | } |
| 3733 | 3727 | |
| 3734 | | break :result if (field_off == 0) dst_mcv else try self.copyToNewRegister(inst, dst_mcv); |
| 3728 | break :result if (field_off == 0) dst_mcv else try func.copyToNewRegister(inst, dst_mcv); |
| 3735 | 3729 | }, |
| 3736 | 3730 | .load_frame => { |
| 3737 | 3731 | const field_abi_size: u32 = @intCast(field_ty.abiSize(mod)); |
| ... | ... | @@ -3746,57 +3740,57 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3746 | 3740 | @intCast(field_bit_size), |
| 3747 | 3741 | ); |
| 3748 | 3742 | |
| 3749 | | const dst_reg, const dst_lock = try self.allocReg(.int); |
| 3743 | const dst_reg, const dst_lock = try func.allocReg(.int); |
| 3750 | 3744 | const dst_mcv = MCValue{ .register = dst_reg }; |
| 3751 | | defer self.register_manager.unlockReg(dst_lock); |
| 3745 | defer func.register_manager.unlockReg(dst_lock); |
| 3752 | 3746 | |
| 3753 | | try self.genCopy(int_ty, dst_mcv, off_mcv); |
| 3754 | | break :result try self.copyToNewRegister(inst, dst_mcv); |
| 3747 | try func.genCopy(int_ty, dst_mcv, off_mcv); |
| 3748 | break :result try func.copyToNewRegister(inst, dst_mcv); |
| 3755 | 3749 | } |
| 3756 | 3750 | |
| 3757 | 3751 | const container_abi_size: u32 = @intCast(struct_ty.abiSize(mod)); |
| 3758 | 3752 | const dst_mcv = if (field_byte_off + field_abi_size <= container_abi_size and |
| 3759 | | self.reuseOperand(inst, operand, 0, src_mcv)) |
| 3753 | func.reuseOperand(inst, operand, 0, src_mcv)) |
| 3760 | 3754 | off_mcv |
| 3761 | 3755 | else dst: { |
| 3762 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 3763 | | try self.genCopy(field_ty, dst_mcv, off_mcv); |
| 3756 | const dst_mcv = try func.allocRegOrMem(inst, true); |
| 3757 | try func.genCopy(field_ty, dst_mcv, off_mcv); |
| 3764 | 3758 | break :dst dst_mcv; |
| 3765 | 3759 | }; |
| 3766 | 3760 | if (field_abi_size * 8 > field_bit_size and dst_mcv.isMemory()) { |
| 3767 | | const tmp_reg, const tmp_lock = try self.allocReg(.int); |
| 3768 | | defer self.register_manager.unlockReg(tmp_lock); |
| 3761 | const tmp_reg, const tmp_lock = try func.allocReg(.int); |
| 3762 | defer func.register_manager.unlockReg(tmp_lock); |
| 3769 | 3763 | |
| 3770 | 3764 | const hi_mcv = |
| 3771 | 3765 | dst_mcv.address().offset(@intCast(field_bit_size / 64 * 8)).deref(); |
| 3772 | | try self.genSetReg(Type.usize, tmp_reg, hi_mcv); |
| 3773 | | try self.genCopy(Type.usize, hi_mcv, .{ .register = tmp_reg }); |
| 3766 | try func.genSetReg(Type.usize, tmp_reg, hi_mcv); |
| 3767 | try func.genCopy(Type.usize, hi_mcv, .{ .register = tmp_reg }); |
| 3774 | 3768 | } |
| 3775 | 3769 | break :result dst_mcv; |
| 3776 | 3770 | } |
| 3777 | 3771 | |
| 3778 | | return self.fail("TODO: airStructFieldVal load_frame field_off non multiple of 8", .{}); |
| 3772 | return func.fail("TODO: airStructFieldVal load_frame field_off non multiple of 8", .{}); |
| 3779 | 3773 | }, |
| 3780 | | else => return self.fail("TODO: airStructField {s}", .{@tagName(src_mcv)}), |
| 3774 | else => return func.fail("TODO: airStructField {s}", .{@tagName(src_mcv)}), |
| 3781 | 3775 | } |
| 3782 | 3776 | }; |
| 3783 | 3777 | |
| 3784 | | return self.finishAir(inst, result, .{ extra.struct_operand, .none, .none }); |
| 3778 | return func.finishAir(inst, result, .{ extra.struct_operand, .none, .none }); |
| 3785 | 3779 | } |
| 3786 | 3780 | |
| 3787 | | fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3781 | fn airFieldParentPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3788 | 3782 | _ = inst; |
| 3789 | | return self.fail("TODO implement codegen airFieldParentPtr", .{}); |
| 3783 | return func.fail("TODO implement codegen airFieldParentPtr", .{}); |
| 3790 | 3784 | } |
| 3791 | 3785 | |
| 3792 | | fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 3793 | | const zcu = self.bin_file.comp.module.?; |
| 3794 | | const arg = self.air.instructions.items(.data)[@intFromEnum(inst)].arg; |
| 3786 | fn genArgDbgInfo(func: Func, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 3787 | const zcu = func.bin_file.comp.module.?; |
| 3788 | const arg = func.air.instructions.items(.data)[@intFromEnum(inst)].arg; |
| 3795 | 3789 | const ty = arg.ty.toType(); |
| 3796 | | const owner_decl = zcu.funcOwnerDeclIndex(self.func_index); |
| 3797 | | const name = zcu.getParamName(self.func_index, arg.src_index); |
| 3790 | const owner_decl = zcu.funcOwnerDeclIndex(func.func_index); |
| 3791 | const name = zcu.getParamName(func.func_index, arg.src_index); |
| 3798 | 3792 | |
| 3799 | | switch (self.debug_output) { |
| 3793 | switch (func.debug_output) { |
| 3800 | 3794 | .dwarf => |dw| switch (mcv) { |
| 3801 | 3795 | .register => |reg| try dw.genArgDbgInfo(name, ty, owner_decl, .{ |
| 3802 | 3796 | .register = reg.dwarfLocOp(), |
| ... | ... | @@ -3809,100 +3803,100 @@ fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 3809 | 3803 | } |
| 3810 | 3804 | } |
| 3811 | 3805 | |
| 3812 | | fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 3813 | | var arg_index = self.arg_index; |
| 3806 | fn airArg(func: *Func, inst: Air.Inst.Index) !void { |
| 3807 | var arg_index = func.arg_index; |
| 3814 | 3808 | |
| 3815 | 3809 | // we skip over args that have no bits |
| 3816 | | while (self.args[arg_index] == .none) arg_index += 1; |
| 3817 | | self.arg_index = arg_index + 1; |
| 3810 | while (func.args[arg_index] == .none) arg_index += 1; |
| 3811 | func.arg_index = arg_index + 1; |
| 3818 | 3812 | |
| 3819 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 3820 | | const src_mcv = self.args[arg_index]; |
| 3821 | | const arg_ty = self.typeOfIndex(inst); |
| 3813 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3814 | const src_mcv = func.args[arg_index]; |
| 3815 | const arg_ty = func.typeOfIndex(inst); |
| 3822 | 3816 | |
| 3823 | | const dst_mcv = try self.allocRegOrMem(inst, false); |
| 3817 | const dst_mcv = try func.allocRegOrMem(inst, false); |
| 3824 | 3818 | |
| 3825 | 3819 | log.debug("airArg {} -> {}", .{ src_mcv, dst_mcv }); |
| 3826 | 3820 | |
| 3827 | | try self.genCopy(arg_ty, dst_mcv, src_mcv); |
| 3821 | try func.genCopy(arg_ty, dst_mcv, src_mcv); |
| 3828 | 3822 | |
| 3829 | | try self.genArgDbgInfo(inst, src_mcv); |
| 3823 | try func.genArgDbgInfo(inst, src_mcv); |
| 3830 | 3824 | break :result dst_mcv; |
| 3831 | 3825 | }; |
| 3832 | 3826 | |
| 3833 | | return self.finishAir(inst, result, .{ .none, .none, .none }); |
| 3827 | return func.finishAir(inst, result, .{ .none, .none, .none }); |
| 3834 | 3828 | } |
| 3835 | 3829 | |
| 3836 | | fn airTrap(self: *Self) !void { |
| 3837 | | _ = try self.addInst(.{ |
| 3830 | fn airTrap(func: *Func) !void { |
| 3831 | _ = try func.addInst(.{ |
| 3838 | 3832 | .tag = .unimp, |
| 3839 | 3833 | .ops = .none, |
| 3840 | 3834 | .data = undefined, |
| 3841 | 3835 | }); |
| 3842 | | return self.finishAirBookkeeping(); |
| 3836 | return func.finishAirBookkeeping(); |
| 3843 | 3837 | } |
| 3844 | 3838 | |
| 3845 | | fn airBreakpoint(self: *Self) !void { |
| 3846 | | _ = try self.addInst(.{ |
| 3839 | fn airBreakpoint(func: *Func) !void { |
| 3840 | _ = try func.addInst(.{ |
| 3847 | 3841 | .tag = .ebreak, |
| 3848 | 3842 | .ops = .none, |
| 3849 | 3843 | .data = undefined, |
| 3850 | 3844 | }); |
| 3851 | | return self.finishAirBookkeeping(); |
| 3845 | return func.finishAirBookkeeping(); |
| 3852 | 3846 | } |
| 3853 | 3847 | |
| 3854 | | fn airRetAddr(self: *Self, inst: Air.Inst.Index) !void { |
| 3855 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 3856 | | try self.genCopy(Type.usize, dst_mcv, .{ .load_frame = .{ .index = .ret_addr } }); |
| 3857 | | return self.finishAir(inst, dst_mcv, .{ .none, .none, .none }); |
| 3848 | fn airRetAddr(func: *Func, inst: Air.Inst.Index) !void { |
| 3849 | const dst_mcv = try func.allocRegOrMem(inst, true); |
| 3850 | try func.genCopy(Type.usize, dst_mcv, .{ .load_frame = .{ .index = .ret_addr } }); |
| 3851 | return func.finishAir(inst, dst_mcv, .{ .none, .none, .none }); |
| 3858 | 3852 | } |
| 3859 | 3853 | |
| 3860 | | fn airFrameAddress(self: *Self, inst: Air.Inst.Index) !void { |
| 3861 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 3862 | | try self.genCopy(Type.usize, dst_mcv, .{ .lea_frame = .{ .index = .base_ptr } }); |
| 3863 | | return self.finishAir(inst, dst_mcv, .{ .none, .none, .none }); |
| 3854 | fn airFrameAddress(func: *Func, inst: Air.Inst.Index) !void { |
| 3855 | const dst_mcv = try func.allocRegOrMem(inst, true); |
| 3856 | try func.genCopy(Type.usize, dst_mcv, .{ .lea_frame = .{ .index = .base_ptr } }); |
| 3857 | return func.finishAir(inst, dst_mcv, .{ .none, .none, .none }); |
| 3864 | 3858 | } |
| 3865 | 3859 | |
| 3866 | | fn airFence(self: *Self) !void { |
| 3867 | | return self.fail("TODO implement fence() for {}", .{self.target.cpu.arch}); |
| 3868 | | //return self.finishAirBookkeeping(); |
| 3860 | fn airFence(func: *Func) !void { |
| 3861 | return func.fail("TODO implement fence() for {}", .{func.target.cpu.arch}); |
| 3862 | //return func.finishAirBookkeeping(); |
| 3869 | 3863 | } |
| 3870 | 3864 | |
| 3871 | | fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !void { |
| 3872 | | if (modifier == .always_tail) return self.fail("TODO implement tail calls for riscv64", .{}); |
| 3873 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 3865 | fn airCall(func: *Func, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !void { |
| 3866 | if (modifier == .always_tail) return func.fail("TODO implement tail calls for riscv64", .{}); |
| 3867 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 3874 | 3868 | const callee = pl_op.operand; |
| 3875 | | const extra = self.air.extraData(Air.Call, pl_op.payload); |
| 3876 | | const arg_refs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]); |
| 3869 | const extra = func.air.extraData(Air.Call, pl_op.payload); |
| 3870 | const arg_refs: []const Air.Inst.Ref = @ptrCast(func.air.extra[extra.end..][0..extra.data.args_len]); |
| 3877 | 3871 | |
| 3878 | 3872 | const expected_num_args = 8; |
| 3879 | 3873 | const ExpectedContents = extern struct { |
| 3880 | 3874 | vals: [expected_num_args][@sizeOf(MCValue)]u8 align(@alignOf(MCValue)), |
| 3881 | 3875 | }; |
| 3882 | 3876 | var stack align(@max(@alignOf(ExpectedContents), @alignOf(std.heap.StackFallbackAllocator(0)))) = |
| 3883 | | std.heap.stackFallback(@sizeOf(ExpectedContents), self.gpa); |
| 3877 | std.heap.stackFallback(@sizeOf(ExpectedContents), func.gpa); |
| 3884 | 3878 | const allocator = stack.get(); |
| 3885 | 3879 | |
| 3886 | 3880 | const arg_tys = try allocator.alloc(Type, arg_refs.len); |
| 3887 | 3881 | defer allocator.free(arg_tys); |
| 3888 | | for (arg_tys, arg_refs) |*arg_ty, arg_ref| arg_ty.* = self.typeOf(arg_ref); |
| 3882 | for (arg_tys, arg_refs) |*arg_ty, arg_ref| arg_ty.* = func.typeOf(arg_ref); |
| 3889 | 3883 | |
| 3890 | 3884 | const arg_vals = try allocator.alloc(MCValue, arg_refs.len); |
| 3891 | 3885 | defer allocator.free(arg_vals); |
| 3892 | 3886 | for (arg_vals, arg_refs) |*arg_val, arg_ref| arg_val.* = .{ .air_ref = arg_ref }; |
| 3893 | 3887 | |
| 3894 | | const call_ret = try self.genCall(.{ .air = callee }, arg_tys, arg_vals); |
| 3888 | const call_ret = try func.genCall(.{ .air = callee }, arg_tys, arg_vals); |
| 3895 | 3889 | |
| 3896 | | var bt = self.liveness.iterateBigTomb(inst); |
| 3897 | | try self.feed(&bt, pl_op.operand); |
| 3898 | | for (arg_refs) |arg_ref| try self.feed(&bt, arg_ref); |
| 3890 | var bt = func.liveness.iterateBigTomb(inst); |
| 3891 | try func.feed(&bt, pl_op.operand); |
| 3892 | for (arg_refs) |arg_ref| try func.feed(&bt, arg_ref); |
| 3899 | 3893 | |
| 3900 | | const result = if (self.liveness.isUnused(inst)) .unreach else call_ret; |
| 3901 | | return self.finishAirResult(inst, result); |
| 3894 | const result = if (func.liveness.isUnused(inst)) .unreach else call_ret; |
| 3895 | return func.finishAirResult(inst, result); |
| 3902 | 3896 | } |
| 3903 | 3897 | |
| 3904 | 3898 | fn genCall( |
| 3905 | | self: *Self, |
| 3899 | func: *Func, |
| 3906 | 3900 | info: union(enum) { |
| 3907 | 3901 | air: Air.Inst.Ref, |
| 3908 | 3902 | lib: struct { |
| ... | ... | @@ -3915,11 +3909,11 @@ fn genCall( |
| 3915 | 3909 | arg_tys: []const Type, |
| 3916 | 3910 | args: []const MCValue, |
| 3917 | 3911 | ) !MCValue { |
| 3918 | | const zcu = self.bin_file.comp.module.?; |
| 3912 | const zcu = func.bin_file.comp.module.?; |
| 3919 | 3913 | |
| 3920 | 3914 | const fn_ty = switch (info) { |
| 3921 | 3915 | .air => |callee| fn_info: { |
| 3922 | | const callee_ty = self.typeOf(callee); |
| 3916 | const callee_ty = func.typeOf(callee); |
| 3923 | 3917 | break :fn_info switch (callee_ty.zigTypeTag(zcu)) { |
| 3924 | 3918 | .Fn => callee_ty, |
| 3925 | 3919 | .Pointer => callee_ty.childType(zcu), |
| ... | ... | @@ -3935,14 +3929,14 @@ fn genCall( |
| 3935 | 3929 | |
| 3936 | 3930 | const fn_info = zcu.typeToFunc(fn_ty).?; |
| 3937 | 3931 | |
| 3938 | | const allocator = self.gpa; |
| 3932 | const allocator = func.gpa; |
| 3939 | 3933 | |
| 3940 | 3934 | const var_args = try allocator.alloc(Type, args.len - fn_info.param_types.len); |
| 3941 | 3935 | defer allocator.free(var_args); |
| 3942 | 3936 | for (var_args, arg_tys[fn_info.param_types.len..]) |*var_arg, arg_ty| var_arg.* = arg_ty; |
| 3943 | 3937 | |
| 3944 | | var call_info = try self.resolveCallingConventionValues(fn_info, var_args); |
| 3945 | | defer call_info.deinit(self); |
| 3938 | var call_info = try func.resolveCallingConventionValues(fn_info, var_args); |
| 3939 | defer call_info.deinit(func); |
| 3946 | 3940 | |
| 3947 | 3941 | // We need a properly aligned and sized call frame to be able to call this function. |
| 3948 | 3942 | { |
| ... | ... | @@ -3950,7 +3944,7 @@ fn genCall( |
| 3950 | 3944 | .size = call_info.stack_byte_count, |
| 3951 | 3945 | .alignment = call_info.stack_align, |
| 3952 | 3946 | }); |
| 3953 | | const frame_allocs_slice = self.frame_allocs.slice(); |
| 3947 | const frame_allocs_slice = func.frame_allocs.slice(); |
| 3954 | 3948 | const stack_frame_size = |
| 3955 | 3949 | &frame_allocs_slice.items(.abi_size)[@intFromEnum(FrameIndex.call_frame)]; |
| 3956 | 3950 | stack_frame_size.* = @max(stack_frame_size.*, needed_call_frame.abi_size); |
| ... | ... | @@ -3962,34 +3956,34 @@ fn genCall( |
| 3962 | 3956 | var reg_locks = std.ArrayList(?RegisterLock).init(allocator); |
| 3963 | 3957 | defer reg_locks.deinit(); |
| 3964 | 3958 | try reg_locks.ensureTotalCapacity(8); |
| 3965 | | defer for (reg_locks.items) |reg_lock| if (reg_lock) |lock| self.register_manager.unlockReg(lock); |
| 3959 | defer for (reg_locks.items) |reg_lock| if (reg_lock) |lock| func.register_manager.unlockReg(lock); |
| 3966 | 3960 | |
| 3967 | 3961 | const frame_indices = try allocator.alloc(FrameIndex, args.len); |
| 3968 | 3962 | defer allocator.free(frame_indices); |
| 3969 | 3963 | |
| 3970 | 3964 | switch (call_info.return_value.long) { |
| 3971 | 3965 | .none, .unreach => {}, |
| 3972 | | .indirect => |reg_off| try self.register_manager.getReg(reg_off.reg, null), |
| 3966 | .indirect => |reg_off| try func.register_manager.getReg(reg_off.reg, null), |
| 3973 | 3967 | else => unreachable, |
| 3974 | 3968 | } |
| 3975 | 3969 | for (call_info.args, args, arg_tys, frame_indices) |dst_arg, src_arg, arg_ty, *frame_index| { |
| 3976 | 3970 | switch (dst_arg) { |
| 3977 | 3971 | .none => {}, |
| 3978 | 3972 | .register => |reg| { |
| 3979 | | try self.register_manager.getReg(reg, null); |
| 3980 | | try reg_locks.append(self.register_manager.lockReg(reg)); |
| 3973 | try func.register_manager.getReg(reg, null); |
| 3974 | try reg_locks.append(func.register_manager.lockReg(reg)); |
| 3981 | 3975 | }, |
| 3982 | 3976 | .register_pair => |regs| { |
| 3983 | | for (regs) |reg| try self.register_manager.getReg(reg, null); |
| 3984 | | try reg_locks.appendSlice(&self.register_manager.lockRegs(2, regs)); |
| 3977 | for (regs) |reg| try func.register_manager.getReg(reg, null); |
| 3978 | try reg_locks.appendSlice(&func.register_manager.lockRegs(2, regs)); |
| 3985 | 3979 | }, |
| 3986 | 3980 | .indirect => |reg_off| { |
| 3987 | | frame_index.* = try self.allocFrameIndex(FrameAlloc.initType(arg_ty, zcu)); |
| 3988 | | try self.genSetMem(.{ .frame = frame_index.* }, 0, arg_ty, src_arg); |
| 3989 | | try self.register_manager.getReg(reg_off.reg, null); |
| 3990 | | try reg_locks.append(self.register_manager.lockReg(reg_off.reg)); |
| 3981 | frame_index.* = try func.allocFrameIndex(FrameAlloc.initType(arg_ty, zcu)); |
| 3982 | try func.genSetMem(.{ .frame = frame_index.* }, 0, arg_ty, src_arg); |
| 3983 | try func.register_manager.getReg(reg_off.reg, null); |
| 3984 | try reg_locks.append(func.register_manager.lockReg(reg_off.reg)); |
| 3991 | 3985 | }, |
| 3992 | | else => return self.fail("TODO: genCall set arg {s}", .{@tagName(dst_arg)}), |
| 3986 | else => return func.fail("TODO: genCall set arg {s}", .{@tagName(dst_arg)}), |
| 3993 | 3987 | } |
| 3994 | 3988 | } |
| 3995 | 3989 | |
| ... | ... | @@ -3997,12 +3991,12 @@ fn genCall( |
| 3997 | 3991 | .none, .unreach => {}, |
| 3998 | 3992 | .indirect => |reg_off| { |
| 3999 | 3993 | const ret_ty = Type.fromInterned(fn_info.return_type); |
| 4000 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(ret_ty, zcu)); |
| 4001 | | try self.genSetReg(Type.usize, reg_off.reg, .{ |
| 3994 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(ret_ty, zcu)); |
| 3995 | try func.genSetReg(Type.usize, reg_off.reg, .{ |
| 4002 | 3996 | .lea_frame = .{ .index = frame_index, .off = -reg_off.off }, |
| 4003 | 3997 | }); |
| 4004 | 3998 | call_info.return_value.short = .{ .load_frame = .{ .index = frame_index } }; |
| 4005 | | try reg_locks.append(self.register_manager.lockReg(reg_off.reg)); |
| 3999 | try reg_locks.append(func.register_manager.lockReg(reg_off.reg)); |
| 4006 | 4000 | }, |
| 4007 | 4001 | else => unreachable, |
| 4008 | 4002 | } |
| ... | ... | @@ -4010,16 +4004,16 @@ fn genCall( |
| 4010 | 4004 | for (call_info.args, arg_tys, args, frame_indices) |dst_arg, arg_ty, src_arg, frame_index| { |
| 4011 | 4005 | switch (dst_arg) { |
| 4012 | 4006 | .none, .load_frame => {}, |
| 4013 | | .register_pair => try self.genCopy(arg_ty, dst_arg, src_arg), |
| 4014 | | .register => |dst_reg| try self.genSetReg( |
| 4007 | .register_pair => try func.genCopy(arg_ty, dst_arg, src_arg), |
| 4008 | .register => |dst_reg| try func.genSetReg( |
| 4015 | 4009 | arg_ty, |
| 4016 | 4010 | dst_reg, |
| 4017 | 4011 | src_arg, |
| 4018 | 4012 | ), |
| 4019 | | .indirect => |reg_off| try self.genSetReg(Type.usize, reg_off.reg, .{ |
| 4013 | .indirect => |reg_off| try func.genSetReg(Type.usize, reg_off.reg, .{ |
| 4020 | 4014 | .lea_frame = .{ .index = frame_index, .off = -reg_off.off }, |
| 4021 | 4015 | }), |
| 4022 | | else => return self.fail("TODO: genCall actual set {s}", .{@tagName(dst_arg)}), |
| 4016 | else => return func.fail("TODO: genCall actual set {s}", .{@tagName(dst_arg)}), |
| 4023 | 4017 | } |
| 4024 | 4018 | } |
| 4025 | 4019 | |
| ... | ... | @@ -4027,7 +4021,7 @@ fn genCall( |
| 4027 | 4021 | // on linking. |
| 4028 | 4022 | switch (info) { |
| 4029 | 4023 | .air => |callee| { |
| 4030 | | if (try self.air.value(callee, zcu)) |func_value| { |
| 4024 | if (try func.air.value(callee, zcu)) |func_value| { |
| 4031 | 4025 | const func_key = zcu.intern_pool.indexToKey(func_value.ip_index); |
| 4032 | 4026 | switch (switch (func_key) { |
| 4033 | 4027 | else => func_key, |
| ... | ... | @@ -4036,19 +4030,19 @@ fn genCall( |
| 4036 | 4030 | else => func_key, |
| 4037 | 4031 | } else func_key, |
| 4038 | 4032 | }) { |
| 4039 | | .func => |func| { |
| 4040 | | if (self.bin_file.cast(link.File.Elf)) |elf_file| { |
| 4041 | | const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func.owner_decl); |
| 4033 | .func => |func_val| { |
| 4034 | if (func.bin_file.cast(link.File.Elf)) |elf_file| { |
| 4035 | const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func_val.owner_decl); |
| 4042 | 4036 | const sym = elf_file.symbol(sym_index); |
| 4043 | 4037 | |
| 4044 | | if (self.mod.pic) { |
| 4045 | | return self.fail("TODO: genCall pic", .{}); |
| 4038 | if (func.mod.pic) { |
| 4039 | return func.fail("TODO: genCall pic", .{}); |
| 4046 | 4040 | } else { |
| 4047 | 4041 | _ = try sym.getOrCreateZigGotEntry(sym_index, elf_file); |
| 4048 | 4042 | const got_addr = sym.zigGotAddress(elf_file); |
| 4049 | | try self.genSetReg(Type.usize, .ra, .{ .memory = @intCast(got_addr) }); |
| 4043 | try func.genSetReg(Type.usize, .ra, .{ .memory = @intCast(got_addr) }); |
| 4050 | 4044 | |
| 4051 | | _ = try self.addInst(.{ |
| 4045 | _ = try func.addInst(.{ |
| 4052 | 4046 | .tag = .jalr, |
| 4053 | 4047 | .ops = .rri, |
| 4054 | 4048 | .data = .{ .i_type = .{ |
| ... | ... | @@ -4064,10 +4058,10 @@ fn genCall( |
| 4064 | 4058 | const owner_decl = zcu.declPtr(extern_func.decl); |
| 4065 | 4059 | const lib_name = extern_func.lib_name.toSlice(&zcu.intern_pool); |
| 4066 | 4060 | const decl_name = owner_decl.name.toSlice(&zcu.intern_pool); |
| 4067 | | const atom_index = try self.symbolIndex(); |
| 4061 | const atom_index = try func.symbolIndex(); |
| 4068 | 4062 | |
| 4069 | | if (self.bin_file.cast(link.File.Elf)) |elf_file| { |
| 4070 | | _ = try self.addInst(.{ |
| 4063 | if (func.bin_file.cast(link.File.Elf)) |elf_file| { |
| 4064 | _ = try func.addInst(.{ |
| 4071 | 4065 | .tag = .pseudo, |
| 4072 | 4066 | .ops = .pseudo_extern_fn_reloc, |
| 4073 | 4067 | .data = .{ .reloc = .{ |
| ... | ... | @@ -4077,15 +4071,15 @@ fn genCall( |
| 4077 | 4071 | }); |
| 4078 | 4072 | } else unreachable; // not a valid riscv64 format |
| 4079 | 4073 | }, |
| 4080 | | else => return self.fail("TODO implement calling bitcasted functions", .{}), |
| 4074 | else => return func.fail("TODO implement calling bitcasted functions", .{}), |
| 4081 | 4075 | } |
| 4082 | 4076 | } else { |
| 4083 | | assert(self.typeOf(callee).zigTypeTag(zcu) == .Pointer); |
| 4084 | | const addr_reg, const addr_lock = try self.allocReg(.int); |
| 4085 | | defer self.register_manager.unlockReg(addr_lock); |
| 4086 | | try self.genSetReg(Type.usize, addr_reg, .{ .air_ref = callee }); |
| 4077 | assert(func.typeOf(callee).zigTypeTag(zcu) == .Pointer); |
| 4078 | const addr_reg, const addr_lock = try func.allocReg(.int); |
| 4079 | defer func.register_manager.unlockReg(addr_lock); |
| 4080 | try func.genSetReg(Type.usize, addr_reg, .{ .air_ref = callee }); |
| 4087 | 4081 | |
| 4088 | | _ = try self.addInst(.{ |
| 4082 | _ = try func.addInst(.{ |
| 4089 | 4083 | .tag = .jalr, |
| 4090 | 4084 | .ops = .rri, |
| 4091 | 4085 | .data = .{ .i_type = .{ |
| ... | ... | @@ -4096,15 +4090,15 @@ fn genCall( |
| 4096 | 4090 | }); |
| 4097 | 4091 | } |
| 4098 | 4092 | }, |
| 4099 | | .lib => return self.fail("TODO: lib func calls", .{}), |
| 4093 | .lib => return func.fail("TODO: lib func calls", .{}), |
| 4100 | 4094 | } |
| 4101 | 4095 | |
| 4102 | 4096 | return call_info.return_value.short; |
| 4103 | 4097 | } |
| 4104 | 4098 | |
| 4105 | | fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 4106 | | const zcu = self.bin_file.comp.module.?; |
| 4107 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4099 | fn airRet(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 4100 | const zcu = func.bin_file.comp.module.?; |
| 4101 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4108 | 4102 | |
| 4109 | 4103 | if (safety) { |
| 4110 | 4104 | // safe |
| ... | ... | @@ -4112,19 +4106,19 @@ fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 4112 | 4106 | // not safe |
| 4113 | 4107 | } |
| 4114 | 4108 | |
| 4115 | | const ret_ty = self.fn_type.fnReturnType(zcu); |
| 4116 | | switch (self.ret_mcv.short) { |
| 4109 | const ret_ty = func.fn_type.fnReturnType(zcu); |
| 4110 | switch (func.ret_mcv.short) { |
| 4117 | 4111 | .none => {}, |
| 4118 | 4112 | .register, |
| 4119 | 4113 | .register_pair, |
| 4120 | | => try self.genCopy(ret_ty, self.ret_mcv.short, .{ .air_ref = un_op }), |
| 4114 | => try func.genCopy(ret_ty, func.ret_mcv.short, .{ .air_ref = un_op }), |
| 4121 | 4115 | .indirect => |reg_off| { |
| 4122 | | try self.register_manager.getReg(reg_off.reg, null); |
| 4123 | | const lock = self.register_manager.lockRegAssumeUnused(reg_off.reg); |
| 4124 | | defer self.register_manager.unlockReg(lock); |
| 4116 | try func.register_manager.getReg(reg_off.reg, null); |
| 4117 | const lock = func.register_manager.lockRegAssumeUnused(reg_off.reg); |
| 4118 | defer func.register_manager.unlockReg(lock); |
| 4125 | 4119 | |
| 4126 | | try self.genSetReg(Type.usize, reg_off.reg, self.ret_mcv.long); |
| 4127 | | try self.genSetMem( |
| 4120 | try func.genSetReg(Type.usize, reg_off.reg, func.ret_mcv.long); |
| 4121 | try func.genSetMem( |
| 4128 | 4122 | .{ .reg = reg_off.reg }, |
| 4129 | 4123 | reg_off.off, |
| 4130 | 4124 | ret_ty, |
| ... | ... | @@ -4134,52 +4128,52 @@ fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 4134 | 4128 | else => unreachable, |
| 4135 | 4129 | } |
| 4136 | 4130 | |
| 4137 | | self.ret_mcv.liveOut(self, inst); |
| 4138 | | try self.finishAir(inst, .unreach, .{ un_op, .none, .none }); |
| 4131 | func.ret_mcv.liveOut(func, inst); |
| 4132 | try func.finishAir(inst, .unreach, .{ un_op, .none, .none }); |
| 4139 | 4133 | |
| 4140 | 4134 | // Just add space for an instruction, reloced this later |
| 4141 | | const index = try self.addInst(.{ |
| 4135 | const index = try func.addInst(.{ |
| 4142 | 4136 | .tag = .pseudo, |
| 4143 | 4137 | .ops = .pseudo_j, |
| 4144 | 4138 | .data = .{ .inst = undefined }, |
| 4145 | 4139 | }); |
| 4146 | 4140 | |
| 4147 | | try self.exitlude_jump_relocs.append(self.gpa, index); |
| 4141 | try func.exitlude_jump_relocs.append(func.gpa, index); |
| 4148 | 4142 | } |
| 4149 | 4143 | |
| 4150 | | fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 4151 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4152 | | const ptr = try self.resolveInst(un_op); |
| 4144 | fn airRetLoad(func: *Func, inst: Air.Inst.Index) !void { |
| 4145 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4146 | const ptr = try func.resolveInst(un_op); |
| 4153 | 4147 | |
| 4154 | | const ptr_ty = self.typeOf(un_op); |
| 4155 | | switch (self.ret_mcv.short) { |
| 4148 | const ptr_ty = func.typeOf(un_op); |
| 4149 | switch (func.ret_mcv.short) { |
| 4156 | 4150 | .none => {}, |
| 4157 | | .register, .register_pair => try self.load(self.ret_mcv.short, ptr, ptr_ty), |
| 4158 | | .indirect => |reg_off| try self.genSetReg(ptr_ty, reg_off.reg, ptr), |
| 4151 | .register, .register_pair => try func.load(func.ret_mcv.short, ptr, ptr_ty), |
| 4152 | .indirect => |reg_off| try func.genSetReg(ptr_ty, reg_off.reg, ptr), |
| 4159 | 4153 | else => unreachable, |
| 4160 | 4154 | } |
| 4161 | | self.ret_mcv.liveOut(self, inst); |
| 4162 | | try self.finishAir(inst, .unreach, .{ un_op, .none, .none }); |
| 4155 | func.ret_mcv.liveOut(func, inst); |
| 4156 | try func.finishAir(inst, .unreach, .{ un_op, .none, .none }); |
| 4163 | 4157 | |
| 4164 | 4158 | // Just add space for an instruction, reloced this later |
| 4165 | | const index = try self.addInst(.{ |
| 4159 | const index = try func.addInst(.{ |
| 4166 | 4160 | .tag = .pseudo, |
| 4167 | 4161 | .ops = .pseudo_j, |
| 4168 | 4162 | .data = .{ .inst = undefined }, |
| 4169 | 4163 | }); |
| 4170 | 4164 | |
| 4171 | | try self.exitlude_jump_relocs.append(self.gpa, index); |
| 4165 | try func.exitlude_jump_relocs.append(func.gpa, index); |
| 4172 | 4166 | } |
| 4173 | 4167 | |
| 4174 | | fn airCmp(self: *Self, inst: Air.Inst.Index) !void { |
| 4175 | | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4176 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4177 | | const zcu = self.bin_file.comp.module.?; |
| 4168 | fn airCmp(func: *Func, inst: Air.Inst.Index) !void { |
| 4169 | const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4170 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4171 | const zcu = func.bin_file.comp.module.?; |
| 4178 | 4172 | |
| 4179 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4180 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 4181 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 4182 | | const lhs_ty = self.typeOf(bin_op.lhs); |
| 4173 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4174 | const lhs = try func.resolveInst(bin_op.lhs); |
| 4175 | const rhs = try func.resolveInst(bin_op.rhs); |
| 4176 | const lhs_ty = func.typeOf(bin_op.lhs); |
| 4183 | 4177 | |
| 4184 | 4178 | switch (lhs_ty.zigTypeTag(zcu)) { |
| 4185 | 4179 | .Int, |
| ... | ... | @@ -4202,7 +4196,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index) !void { |
| 4202 | 4196 | } else if (lhs_ty.isPtrLikeOptional(zcu)) { |
| 4203 | 4197 | break :blk Type.usize; |
| 4204 | 4198 | } else { |
| 4205 | | return self.fail("TODO riscv cmp non-pointer optionals", .{}); |
| 4199 | return func.fail("TODO riscv cmp non-pointer optionals", .{}); |
| 4206 | 4200 | } |
| 4207 | 4201 | }, |
| 4208 | 4202 | else => unreachable, |
| ... | ... | @@ -4210,43 +4204,43 @@ fn airCmp(self: *Self, inst: Air.Inst.Index) !void { |
| 4210 | 4204 | |
| 4211 | 4205 | const int_info = int_ty.intInfo(zcu); |
| 4212 | 4206 | if (int_info.bits <= 64) { |
| 4213 | | break :result try self.binOp(tag, lhs, int_ty, rhs, int_ty); |
| 4207 | break :result try func.binOp(tag, lhs, int_ty, rhs, int_ty); |
| 4214 | 4208 | } else { |
| 4215 | | return self.fail("TODO riscv cmp for ints > 64 bits", .{}); |
| 4209 | return func.fail("TODO riscv cmp for ints > 64 bits", .{}); |
| 4216 | 4210 | } |
| 4217 | 4211 | }, |
| 4218 | 4212 | .Float => { |
| 4219 | | const float_bits = lhs_ty.floatBits(self.target.*); |
| 4220 | | const float_reg_size: u32 = if (self.hasFeature(.d)) 64 else 32; |
| 4213 | const float_bits = lhs_ty.floatBits(func.target.*); |
| 4214 | const float_reg_size: u32 = if (func.hasFeature(.d)) 64 else 32; |
| 4221 | 4215 | if (float_bits > float_reg_size) { |
| 4222 | | return self.fail("TODO: airCmp float > 64/32 bits", .{}); |
| 4216 | return func.fail("TODO: airCmp float > 64/32 bits", .{}); |
| 4223 | 4217 | } |
| 4224 | | break :result try self.binOpFloat(tag, lhs, lhs_ty, rhs, lhs_ty); |
| 4218 | break :result try func.binOpFloat(tag, lhs, lhs_ty, rhs, lhs_ty); |
| 4225 | 4219 | }, |
| 4226 | 4220 | else => unreachable, |
| 4227 | 4221 | } |
| 4228 | 4222 | }; |
| 4229 | 4223 | |
| 4230 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 4224 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 4231 | 4225 | } |
| 4232 | 4226 | |
| 4233 | | fn airCmpVector(self: *Self, inst: Air.Inst.Index) !void { |
| 4227 | fn airCmpVector(func: *Func, inst: Air.Inst.Index) !void { |
| 4234 | 4228 | _ = inst; |
| 4235 | | return self.fail("TODO implement airCmpVector for {}", .{self.target.cpu.arch}); |
| 4229 | return func.fail("TODO implement airCmpVector for {}", .{func.target.cpu.arch}); |
| 4236 | 4230 | } |
| 4237 | 4231 | |
| 4238 | | fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) !void { |
| 4239 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4240 | | const operand = try self.resolveInst(un_op); |
| 4232 | fn airCmpLtErrorsLen(func: *Func, inst: Air.Inst.Index) !void { |
| 4233 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4234 | const operand = try func.resolveInst(un_op); |
| 4241 | 4235 | _ = operand; |
| 4242 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airCmpLtErrorsLen for {}", .{self.target.cpu.arch}); |
| 4243 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4236 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airCmpLtErrorsLen for {}", .{func.target.cpu.arch}); |
| 4237 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4244 | 4238 | } |
| 4245 | 4239 | |
| 4246 | | fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void { |
| 4247 | | const dbg_stmt = self.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; |
| 4240 | fn airDbgStmt(func: *Func, inst: Air.Inst.Index) !void { |
| 4241 | const dbg_stmt = func.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; |
| 4248 | 4242 | |
| 4249 | | _ = try self.addInst(.{ |
| 4243 | _ = try func.addInst(.{ |
| 4250 | 4244 | .tag = .pseudo, |
| 4251 | 4245 | .ops = .pseudo_dbg_line_column, |
| 4252 | 4246 | .data = .{ .pseudo_dbg_line_column = .{ |
| ... | ... | @@ -4255,44 +4249,44 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void { |
| 4255 | 4249 | } }, |
| 4256 | 4250 | }); |
| 4257 | 4251 | |
| 4258 | | return self.finishAirBookkeeping(); |
| 4252 | return func.finishAirBookkeeping(); |
| 4259 | 4253 | } |
| 4260 | 4254 | |
| 4261 | | fn airDbgInlineBlock(self: *Self, inst: Air.Inst.Index) !void { |
| 4262 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4263 | | const extra = self.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 4264 | | try self.lowerBlock(inst, @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len])); |
| 4255 | fn airDbgInlineBlock(func: *Func, inst: Air.Inst.Index) !void { |
| 4256 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4257 | const extra = func.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 4258 | try func.lowerBlock(inst, @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len])); |
| 4265 | 4259 | } |
| 4266 | 4260 | |
| 4267 | | fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void { |
| 4268 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4261 | fn airDbgVar(func: *Func, inst: Air.Inst.Index) !void { |
| 4262 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4269 | 4263 | const operand = pl_op.operand; |
| 4270 | | const ty = self.typeOf(operand); |
| 4271 | | const mcv = try self.resolveInst(operand); |
| 4264 | const ty = func.typeOf(operand); |
| 4265 | const mcv = try func.resolveInst(operand); |
| 4272 | 4266 | |
| 4273 | | const name = self.air.nullTerminatedString(pl_op.payload); |
| 4267 | const name = func.air.nullTerminatedString(pl_op.payload); |
| 4274 | 4268 | |
| 4275 | | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4276 | | try self.genVarDbgInfo(tag, ty, mcv, name); |
| 4269 | const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4270 | try func.genVarDbgInfo(tag, ty, mcv, name); |
| 4277 | 4271 | |
| 4278 | | return self.finishAir(inst, .unreach, .{ operand, .none, .none }); |
| 4272 | return func.finishAir(inst, .unreach, .{ operand, .none, .none }); |
| 4279 | 4273 | } |
| 4280 | 4274 | |
| 4281 | 4275 | fn genVarDbgInfo( |
| 4282 | | self: Self, |
| 4276 | func: Func, |
| 4283 | 4277 | tag: Air.Inst.Tag, |
| 4284 | 4278 | ty: Type, |
| 4285 | 4279 | mcv: MCValue, |
| 4286 | 4280 | name: [:0]const u8, |
| 4287 | 4281 | ) !void { |
| 4288 | | const zcu = self.bin_file.comp.module.?; |
| 4282 | const zcu = func.bin_file.comp.module.?; |
| 4289 | 4283 | const is_ptr = switch (tag) { |
| 4290 | 4284 | .dbg_var_ptr => true, |
| 4291 | 4285 | .dbg_var_val => false, |
| 4292 | 4286 | else => unreachable, |
| 4293 | 4287 | }; |
| 4294 | 4288 | |
| 4295 | | switch (self.debug_output) { |
| 4289 | switch (func.debug_output) { |
| 4296 | 4290 | .dwarf => |dw| { |
| 4297 | 4291 | const loc: link.File.Dwarf.DeclState.DbgInfoLoc = switch (mcv) { |
| 4298 | 4292 | .register => |reg| .{ .register = reg.dwarfLocOp() }, |
| ... | ... | @@ -4309,47 +4303,47 @@ fn genVarDbgInfo( |
| 4309 | 4303 | break :blk .nop; |
| 4310 | 4304 | }, |
| 4311 | 4305 | }; |
| 4312 | | try dw.genVarDbgInfo(name, ty, zcu.funcOwnerDeclIndex(self.func_index), is_ptr, loc); |
| 4306 | try dw.genVarDbgInfo(name, ty, zcu.funcOwnerDeclIndex(func.func_index), is_ptr, loc); |
| 4313 | 4307 | }, |
| 4314 | 4308 | .plan9 => {}, |
| 4315 | 4309 | .none => {}, |
| 4316 | 4310 | } |
| 4317 | 4311 | } |
| 4318 | 4312 | |
| 4319 | | fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4320 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4321 | | const cond = try self.resolveInst(pl_op.operand); |
| 4322 | | const cond_ty = self.typeOf(pl_op.operand); |
| 4323 | | const extra = self.air.extraData(Air.CondBr, pl_op.payload); |
| 4324 | | const then_body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end..][0..extra.data.then_body_len]); |
| 4325 | | const else_body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]); |
| 4326 | | const liveness_cond_br = self.liveness.getCondBr(inst); |
| 4313 | fn airCondBr(func: *Func, inst: Air.Inst.Index) !void { |
| 4314 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4315 | const cond = try func.resolveInst(pl_op.operand); |
| 4316 | const cond_ty = func.typeOf(pl_op.operand); |
| 4317 | const extra = func.air.extraData(Air.CondBr, pl_op.payload); |
| 4318 | const then_body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end..][0..extra.data.then_body_len]); |
| 4319 | const else_body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]); |
| 4320 | const liveness_cond_br = func.liveness.getCondBr(inst); |
| 4327 | 4321 | |
| 4328 | 4322 | // If the condition dies here in this condbr instruction, process |
| 4329 | 4323 | // that death now instead of later as this has an effect on |
| 4330 | 4324 | // whether it needs to be spilled in the branches |
| 4331 | | if (self.liveness.operandDies(inst, 0)) { |
| 4332 | | if (pl_op.operand.toIndex()) |op_inst| try self.processDeath(op_inst); |
| 4325 | if (func.liveness.operandDies(inst, 0)) { |
| 4326 | if (pl_op.operand.toIndex()) |op_inst| try func.processDeath(op_inst); |
| 4333 | 4327 | } |
| 4334 | 4328 | |
| 4335 | | self.scope_generation += 1; |
| 4336 | | const state = try self.saveState(); |
| 4337 | | const reloc = try self.condBr(cond_ty, cond); |
| 4329 | func.scope_generation += 1; |
| 4330 | const state = try func.saveState(); |
| 4331 | const reloc = try func.condBr(cond_ty, cond); |
| 4338 | 4332 | |
| 4339 | | for (liveness_cond_br.then_deaths) |death| try self.processDeath(death); |
| 4340 | | try self.genBody(then_body); |
| 4341 | | try self.restoreState(state, &.{}, .{ |
| 4333 | for (liveness_cond_br.then_deaths) |death| try func.processDeath(death); |
| 4334 | try func.genBody(then_body); |
| 4335 | try func.restoreState(state, &.{}, .{ |
| 4342 | 4336 | .emit_instructions = false, |
| 4343 | 4337 | .update_tracking = true, |
| 4344 | 4338 | .resurrect = true, |
| 4345 | 4339 | .close_scope = true, |
| 4346 | 4340 | }); |
| 4347 | 4341 | |
| 4348 | | self.performReloc(reloc); |
| 4342 | func.performReloc(reloc); |
| 4349 | 4343 | |
| 4350 | | for (liveness_cond_br.else_deaths) |death| try self.processDeath(death); |
| 4351 | | try self.genBody(else_body); |
| 4352 | | try self.restoreState(state, &.{}, .{ |
| 4344 | for (liveness_cond_br.else_deaths) |death| try func.processDeath(death); |
| 4345 | try func.genBody(else_body); |
| 4346 | try func.restoreState(state, &.{}, .{ |
| 4353 | 4347 | .emit_instructions = false, |
| 4354 | 4348 | .update_tracking = true, |
| 4355 | 4349 | .resurrect = true, |
| ... | ... | @@ -4357,13 +4351,13 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4357 | 4351 | }); |
| 4358 | 4352 | |
| 4359 | 4353 | // We already took care of pl_op.operand earlier, so there's nothing left to do. |
| 4360 | | self.finishAirBookkeeping(); |
| 4354 | func.finishAirBookkeeping(); |
| 4361 | 4355 | } |
| 4362 | 4356 | |
| 4363 | | fn condBr(self: *Self, cond_ty: Type, condition: MCValue) !Mir.Inst.Index { |
| 4364 | | const cond_reg = try self.copyToTmpRegister(cond_ty, condition); |
| 4357 | fn condBr(func: *Func, cond_ty: Type, condition: MCValue) !Mir.Inst.Index { |
| 4358 | const cond_reg = try func.copyToTmpRegister(cond_ty, condition); |
| 4365 | 4359 | |
| 4366 | | return try self.addInst(.{ |
| 4360 | return try func.addInst(.{ |
| 4367 | 4361 | .tag = .beq, |
| 4368 | 4362 | .ops = .rr_inst, |
| 4369 | 4363 | .data = .{ |
| ... | ... | @@ -4376,111 +4370,111 @@ fn condBr(self: *Self, cond_ty: Type, condition: MCValue) !Mir.Inst.Index { |
| 4376 | 4370 | }); |
| 4377 | 4371 | } |
| 4378 | 4372 | |
| 4379 | | fn airIsNull(self: *Self, inst: Air.Inst.Index) !void { |
| 4380 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4381 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4382 | | const operand = try self.resolveInst(un_op); |
| 4383 | | break :result try self.isNull(operand); |
| 4373 | fn airIsNull(func: *Func, inst: Air.Inst.Index) !void { |
| 4374 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4375 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4376 | const operand = try func.resolveInst(un_op); |
| 4377 | break :result try func.isNull(operand); |
| 4384 | 4378 | }; |
| 4385 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4379 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4386 | 4380 | } |
| 4387 | 4381 | |
| 4388 | | fn airIsNullPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4389 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4390 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4391 | | const operand_ptr = try self.resolveInst(un_op); |
| 4382 | fn airIsNullPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4383 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4384 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4385 | const operand_ptr = try func.resolveInst(un_op); |
| 4392 | 4386 | const operand: MCValue = blk: { |
| 4393 | | if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4387 | if (func.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4394 | 4388 | // The MCValue that holds the pointer can be re-used as the value. |
| 4395 | 4389 | break :blk operand_ptr; |
| 4396 | 4390 | } else { |
| 4397 | | break :blk try self.allocRegOrMem(inst, true); |
| 4391 | break :blk try func.allocRegOrMem(inst, true); |
| 4398 | 4392 | } |
| 4399 | 4393 | }; |
| 4400 | | try self.load(operand, operand_ptr, self.typeOf(un_op)); |
| 4401 | | break :result try self.isNull(operand); |
| 4394 | try func.load(operand, operand_ptr, func.typeOf(un_op)); |
| 4395 | break :result try func.isNull(operand); |
| 4402 | 4396 | }; |
| 4403 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4397 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4404 | 4398 | } |
| 4405 | 4399 | |
| 4406 | | fn isNull(self: *Self, operand: MCValue) !MCValue { |
| 4400 | fn isNull(func: *Func, operand: MCValue) !MCValue { |
| 4407 | 4401 | _ = operand; |
| 4408 | 4402 | // Here you can specialize this instruction if it makes sense to, otherwise the default |
| 4409 | 4403 | // will call isNonNull and invert the result. |
| 4410 | | return self.fail("TODO call isNonNull and invert the result", .{}); |
| 4404 | return func.fail("TODO call isNonNull and invert the result", .{}); |
| 4411 | 4405 | } |
| 4412 | 4406 | |
| 4413 | | fn airIsNonNull(self: *Self, inst: Air.Inst.Index) !void { |
| 4414 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4415 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4416 | | const operand = try self.resolveInst(un_op); |
| 4417 | | break :result try self.isNonNull(operand); |
| 4407 | fn airIsNonNull(func: *Func, inst: Air.Inst.Index) !void { |
| 4408 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4409 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4410 | const operand = try func.resolveInst(un_op); |
| 4411 | break :result try func.isNonNull(operand); |
| 4418 | 4412 | }; |
| 4419 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4413 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4420 | 4414 | } |
| 4421 | 4415 | |
| 4422 | | fn isNonNull(self: *Self, operand: MCValue) !MCValue { |
| 4416 | fn isNonNull(func: *Func, operand: MCValue) !MCValue { |
| 4423 | 4417 | _ = operand; |
| 4424 | 4418 | // Here you can specialize this instruction if it makes sense to, otherwise the default |
| 4425 | 4419 | // will call isNull and invert the result. |
| 4426 | | return self.fail("TODO call isNull and invert the result", .{}); |
| 4420 | return func.fail("TODO call isNull and invert the result", .{}); |
| 4427 | 4421 | } |
| 4428 | 4422 | |
| 4429 | | fn airIsNonNullPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4430 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4431 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4432 | | const operand_ptr = try self.resolveInst(un_op); |
| 4423 | fn airIsNonNullPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4424 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4425 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4426 | const operand_ptr = try func.resolveInst(un_op); |
| 4433 | 4427 | const operand: MCValue = blk: { |
| 4434 | | if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4428 | if (func.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4435 | 4429 | // The MCValue that holds the pointer can be re-used as the value. |
| 4436 | 4430 | break :blk operand_ptr; |
| 4437 | 4431 | } else { |
| 4438 | | break :blk try self.allocRegOrMem(inst, true); |
| 4432 | break :blk try func.allocRegOrMem(inst, true); |
| 4439 | 4433 | } |
| 4440 | 4434 | }; |
| 4441 | | try self.load(operand, operand_ptr, self.typeOf(un_op)); |
| 4442 | | break :result try self.isNonNull(operand); |
| 4435 | try func.load(operand, operand_ptr, func.typeOf(un_op)); |
| 4436 | break :result try func.isNonNull(operand); |
| 4443 | 4437 | }; |
| 4444 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4438 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4445 | 4439 | } |
| 4446 | 4440 | |
| 4447 | | fn airIsErr(self: *Self, inst: Air.Inst.Index) !void { |
| 4448 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4449 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4450 | | const operand = try self.resolveInst(un_op); |
| 4451 | | const operand_ty = self.typeOf(un_op); |
| 4452 | | break :result try self.isErr(inst, operand_ty, operand); |
| 4441 | fn airIsErr(func: *Func, inst: Air.Inst.Index) !void { |
| 4442 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4443 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4444 | const operand = try func.resolveInst(un_op); |
| 4445 | const operand_ty = func.typeOf(un_op); |
| 4446 | break :result try func.isErr(inst, operand_ty, operand); |
| 4453 | 4447 | }; |
| 4454 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4448 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4455 | 4449 | } |
| 4456 | 4450 | |
| 4457 | | fn airIsErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4458 | | const zcu = self.bin_file.comp.module.?; |
| 4459 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4460 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4461 | | const operand_ptr = try self.resolveInst(un_op); |
| 4451 | fn airIsErrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4452 | const zcu = func.bin_file.comp.module.?; |
| 4453 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4454 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4455 | const operand_ptr = try func.resolveInst(un_op); |
| 4462 | 4456 | const operand: MCValue = blk: { |
| 4463 | | if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4457 | if (func.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4464 | 4458 | // The MCValue that holds the pointer can be re-used as the value. |
| 4465 | 4459 | break :blk operand_ptr; |
| 4466 | 4460 | } else { |
| 4467 | | break :blk try self.allocRegOrMem(inst, true); |
| 4461 | break :blk try func.allocRegOrMem(inst, true); |
| 4468 | 4462 | } |
| 4469 | 4463 | }; |
| 4470 | | try self.load(operand, operand_ptr, self.typeOf(un_op)); |
| 4471 | | const operand_ptr_ty = self.typeOf(un_op); |
| 4464 | try func.load(operand, operand_ptr, func.typeOf(un_op)); |
| 4465 | const operand_ptr_ty = func.typeOf(un_op); |
| 4472 | 4466 | const operand_ty = operand_ptr_ty.childType(zcu); |
| 4473 | 4467 | |
| 4474 | | break :result try self.isErr(inst, operand_ty, operand); |
| 4468 | break :result try func.isErr(inst, operand_ty, operand); |
| 4475 | 4469 | }; |
| 4476 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4470 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4477 | 4471 | } |
| 4478 | 4472 | |
| 4479 | 4473 | /// Generates a compare instruction which will indicate if `eu_mcv` is an error. |
| 4480 | 4474 | /// |
| 4481 | 4475 | /// Result is in the return register. |
| 4482 | | fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue { |
| 4483 | | const zcu = self.bin_file.comp.module.?; |
| 4476 | fn isErr(func: *Func, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue { |
| 4477 | const zcu = func.bin_file.comp.module.?; |
| 4484 | 4478 | const err_ty = eu_ty.errorUnionSet(zcu); |
| 4485 | 4479 | if (err_ty.errorSetIsEmpty(zcu)) return MCValue{ .immediate = 0 }; // always false |
| 4486 | 4480 | |
| ... | ... | @@ -4490,17 +4484,17 @@ fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) |
| 4490 | 4484 | |
| 4491 | 4485 | switch (eu_mcv) { |
| 4492 | 4486 | .register => |reg| { |
| 4493 | | const eu_lock = self.register_manager.lockReg(reg); |
| 4494 | | defer if (eu_lock) |lock| self.register_manager.unlockReg(lock); |
| 4487 | const eu_lock = func.register_manager.lockReg(reg); |
| 4488 | defer if (eu_lock) |lock| func.register_manager.unlockReg(lock); |
| 4495 | 4489 | |
| 4496 | | const return_reg = try self.copyToTmpRegister(eu_ty, eu_mcv); |
| 4497 | | const return_lock = self.register_manager.lockRegAssumeUnused(return_reg); |
| 4498 | | defer self.register_manager.unlockReg(return_lock); |
| 4490 | const return_reg = try func.copyToTmpRegister(eu_ty, eu_mcv); |
| 4491 | const return_lock = func.register_manager.lockRegAssumeUnused(return_reg); |
| 4492 | defer func.register_manager.unlockReg(return_lock); |
| 4499 | 4493 | |
| 4500 | 4494 | var return_mcv: MCValue = .{ .register = return_reg }; |
| 4501 | 4495 | |
| 4502 | 4496 | if (err_off > 0) { |
| 4503 | | return_mcv = try self.binOp( |
| 4497 | return_mcv = try func.binOp( |
| 4504 | 4498 | .shr, |
| 4505 | 4499 | return_mcv, |
| 4506 | 4500 | eu_ty, |
| ... | ... | @@ -4509,7 +4503,7 @@ fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) |
| 4509 | 4503 | ); |
| 4510 | 4504 | } |
| 4511 | 4505 | |
| 4512 | | return try self.binOp( |
| 4506 | return try func.binOp( |
| 4513 | 4507 | .cmp_neq, |
| 4514 | 4508 | return_mcv, |
| 4515 | 4509 | Type.u16, |
| ... | ... | @@ -4518,7 +4512,7 @@ fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) |
| 4518 | 4512 | ); |
| 4519 | 4513 | }, |
| 4520 | 4514 | .load_frame => |frame_addr| { |
| 4521 | | return self.binOp( |
| 4515 | return func.binOp( |
| 4522 | 4516 | .cmp_neq, |
| 4523 | 4517 | .{ .load_frame = .{ |
| 4524 | 4518 | .index = frame_addr.index, |
| ... | ... | @@ -4529,25 +4523,25 @@ fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) |
| 4529 | 4523 | Type.anyerror, |
| 4530 | 4524 | ); |
| 4531 | 4525 | }, |
| 4532 | | else => return self.fail("TODO implement isErr for {}", .{eu_mcv}), |
| 4526 | else => return func.fail("TODO implement isErr for {}", .{eu_mcv}), |
| 4533 | 4527 | } |
| 4534 | 4528 | } |
| 4535 | 4529 | |
| 4536 | | fn airIsNonErr(self: *Self, inst: Air.Inst.Index) !void { |
| 4537 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4538 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4539 | | const operand = try self.resolveInst(un_op); |
| 4540 | | const ty = self.typeOf(un_op); |
| 4541 | | break :result try self.isNonErr(inst, ty, operand); |
| 4530 | fn airIsNonErr(func: *Func, inst: Air.Inst.Index) !void { |
| 4531 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4532 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4533 | const operand = try func.resolveInst(un_op); |
| 4534 | const ty = func.typeOf(un_op); |
| 4535 | break :result try func.isNonErr(inst, ty, operand); |
| 4542 | 4536 | }; |
| 4543 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4537 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4544 | 4538 | } |
| 4545 | 4539 | |
| 4546 | | fn isNonErr(self: *Self, inst: Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue { |
| 4547 | | const is_err_res = try self.isErr(inst, eu_ty, eu_mcv); |
| 4540 | fn isNonErr(func: *Func, inst: Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue { |
| 4541 | const is_err_res = try func.isErr(inst, eu_ty, eu_mcv); |
| 4548 | 4542 | switch (is_err_res) { |
| 4549 | 4543 | .register => |reg| { |
| 4550 | | _ = try self.addInst(.{ |
| 4544 | _ = try func.addInst(.{ |
| 4551 | 4545 | .tag = .pseudo, |
| 4552 | 4546 | .ops = .pseudo_not, |
| 4553 | 4547 | .data = .{ |
| ... | ... | @@ -4568,53 +4562,53 @@ fn isNonErr(self: *Self, inst: Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MC |
| 4568 | 4562 | } |
| 4569 | 4563 | } |
| 4570 | 4564 | |
| 4571 | | fn airIsNonErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4572 | | const zcu = self.bin_file.comp.module.?; |
| 4573 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4574 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4575 | | const operand_ptr = try self.resolveInst(un_op); |
| 4565 | fn airIsNonErrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4566 | const zcu = func.bin_file.comp.module.?; |
| 4567 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4568 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4569 | const operand_ptr = try func.resolveInst(un_op); |
| 4576 | 4570 | const operand: MCValue = blk: { |
| 4577 | | if (self.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4571 | if (func.reuseOperand(inst, un_op, 0, operand_ptr)) { |
| 4578 | 4572 | // The MCValue that holds the pointer can be re-used as the value. |
| 4579 | 4573 | break :blk operand_ptr; |
| 4580 | 4574 | } else { |
| 4581 | | break :blk try self.allocRegOrMem(inst, true); |
| 4575 | break :blk try func.allocRegOrMem(inst, true); |
| 4582 | 4576 | } |
| 4583 | 4577 | }; |
| 4584 | | const operand_ptr_ty = self.typeOf(un_op); |
| 4578 | const operand_ptr_ty = func.typeOf(un_op); |
| 4585 | 4579 | const operand_ty = operand_ptr_ty.childType(zcu); |
| 4586 | 4580 | |
| 4587 | | try self.load(operand, operand_ptr, self.typeOf(un_op)); |
| 4588 | | break :result try self.isNonErr(inst, operand_ty, operand); |
| 4581 | try func.load(operand, operand_ptr, func.typeOf(un_op)); |
| 4582 | break :result try func.isNonErr(inst, operand_ty, operand); |
| 4589 | 4583 | }; |
| 4590 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4584 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 4591 | 4585 | } |
| 4592 | 4586 | |
| 4593 | | fn airLoop(self: *Self, inst: Air.Inst.Index) !void { |
| 4587 | fn airLoop(func: *Func, inst: Air.Inst.Index) !void { |
| 4594 | 4588 | // A loop is a setup to be able to jump back to the beginning. |
| 4595 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4596 | | const loop = self.air.extraData(Air.Block, ty_pl.payload); |
| 4597 | | const body: []const Air.Inst.Index = @ptrCast(self.air.extra[loop.end..][0..loop.data.body_len]); |
| 4589 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4590 | const loop = func.air.extraData(Air.Block, ty_pl.payload); |
| 4591 | const body: []const Air.Inst.Index = @ptrCast(func.air.extra[loop.end..][0..loop.data.body_len]); |
| 4598 | 4592 | |
| 4599 | | self.scope_generation += 1; |
| 4600 | | const state = try self.saveState(); |
| 4593 | func.scope_generation += 1; |
| 4594 | const state = try func.saveState(); |
| 4601 | 4595 | |
| 4602 | | const jmp_target: Mir.Inst.Index = @intCast(self.mir_instructions.len); |
| 4603 | | try self.genBody(body); |
| 4604 | | try self.restoreState(state, &.{}, .{ |
| 4596 | const jmp_target: Mir.Inst.Index = @intCast(func.mir_instructions.len); |
| 4597 | try func.genBody(body); |
| 4598 | try func.restoreState(state, &.{}, .{ |
| 4605 | 4599 | .emit_instructions = true, |
| 4606 | 4600 | .update_tracking = false, |
| 4607 | 4601 | .resurrect = false, |
| 4608 | 4602 | .close_scope = true, |
| 4609 | 4603 | }); |
| 4610 | | _ = try self.jump(jmp_target); |
| 4604 | _ = try func.jump(jmp_target); |
| 4611 | 4605 | |
| 4612 | | self.finishAirBookkeeping(); |
| 4606 | func.finishAirBookkeeping(); |
| 4613 | 4607 | } |
| 4614 | 4608 | |
| 4615 | | /// Send control flow to the `index` of `self.code`. |
| 4616 | | fn jump(self: *Self, index: Mir.Inst.Index) !Mir.Inst.Index { |
| 4617 | | return self.addInst(.{ |
| 4609 | /// Send control flow to the `index` of `func.code`. |
| 4610 | fn jump(func: *Func, index: Mir.Inst.Index) !Mir.Inst.Index { |
| 4611 | return func.addInst(.{ |
| 4618 | 4612 | .tag = .pseudo, |
| 4619 | 4613 | .ops = .pseudo_j, |
| 4620 | 4614 | .data = .{ |
| ... | ... | @@ -4623,79 +4617,79 @@ fn jump(self: *Self, index: Mir.Inst.Index) !Mir.Inst.Index { |
| 4623 | 4617 | }); |
| 4624 | 4618 | } |
| 4625 | 4619 | |
| 4626 | | fn airBlock(self: *Self, inst: Air.Inst.Index) !void { |
| 4627 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4628 | | const extra = self.air.extraData(Air.Block, ty_pl.payload); |
| 4629 | | try self.lowerBlock(inst, @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len])); |
| 4620 | fn airBlock(func: *Func, inst: Air.Inst.Index) !void { |
| 4621 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4622 | const extra = func.air.extraData(Air.Block, ty_pl.payload); |
| 4623 | try func.lowerBlock(inst, @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len])); |
| 4630 | 4624 | } |
| 4631 | 4625 | |
| 4632 | | fn lowerBlock(self: *Self, inst: Air.Inst.Index, body: []const Air.Inst.Index) !void { |
| 4626 | fn lowerBlock(func: *Func, inst: Air.Inst.Index, body: []const Air.Inst.Index) !void { |
| 4633 | 4627 | // A block is a setup to be able to jump to the end. |
| 4634 | | const inst_tracking_i = self.inst_tracking.count(); |
| 4635 | | self.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(.unreach)); |
| 4628 | const inst_tracking_i = func.inst_tracking.count(); |
| 4629 | func.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(.unreach)); |
| 4636 | 4630 | |
| 4637 | | self.scope_generation += 1; |
| 4638 | | try self.blocks.putNoClobber(self.gpa, inst, .{ .state = self.initRetroactiveState() }); |
| 4639 | | const liveness = self.liveness.getBlock(inst); |
| 4631 | func.scope_generation += 1; |
| 4632 | try func.blocks.putNoClobber(func.gpa, inst, .{ .state = func.initRetroactiveState() }); |
| 4633 | const liveness = func.liveness.getBlock(inst); |
| 4640 | 4634 | |
| 4641 | 4635 | // TODO emit debug info lexical block |
| 4642 | | try self.genBody(body); |
| 4636 | try func.genBody(body); |
| 4643 | 4637 | |
| 4644 | | var block_data = self.blocks.fetchRemove(inst).?; |
| 4645 | | defer block_data.value.deinit(self.gpa); |
| 4638 | var block_data = func.blocks.fetchRemove(inst).?; |
| 4639 | defer block_data.value.deinit(func.gpa); |
| 4646 | 4640 | if (block_data.value.relocs.items.len > 0) { |
| 4647 | | try self.restoreState(block_data.value.state, liveness.deaths, .{ |
| 4641 | try func.restoreState(block_data.value.state, liveness.deaths, .{ |
| 4648 | 4642 | .emit_instructions = false, |
| 4649 | 4643 | .update_tracking = true, |
| 4650 | 4644 | .resurrect = true, |
| 4651 | 4645 | .close_scope = true, |
| 4652 | 4646 | }); |
| 4653 | | for (block_data.value.relocs.items) |reloc| self.performReloc(reloc); |
| 4647 | for (block_data.value.relocs.items) |reloc| func.performReloc(reloc); |
| 4654 | 4648 | } |
| 4655 | 4649 | |
| 4656 | | if (std.debug.runtime_safety) assert(self.inst_tracking.getIndex(inst).? == inst_tracking_i); |
| 4657 | | const tracking = &self.inst_tracking.values()[inst_tracking_i]; |
| 4658 | | if (self.liveness.isUnused(inst)) try tracking.die(self, inst); |
| 4659 | | self.getValueIfFree(tracking.short, inst); |
| 4660 | | self.finishAirBookkeeping(); |
| 4650 | if (std.debug.runtime_safety) assert(func.inst_tracking.getIndex(inst).? == inst_tracking_i); |
| 4651 | const tracking = &func.inst_tracking.values()[inst_tracking_i]; |
| 4652 | if (func.liveness.isUnused(inst)) try tracking.die(func, inst); |
| 4653 | func.getValueIfFree(tracking.short, inst); |
| 4654 | func.finishAirBookkeeping(); |
| 4661 | 4655 | } |
| 4662 | 4656 | |
| 4663 | | fn airSwitchBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4664 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4665 | | const condition = try self.resolveInst(pl_op.operand); |
| 4666 | | const condition_ty = self.typeOf(pl_op.operand); |
| 4667 | | const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload); |
| 4657 | fn airSwitchBr(func: *Func, inst: Air.Inst.Index) !void { |
| 4658 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4659 | const condition = try func.resolveInst(pl_op.operand); |
| 4660 | const condition_ty = func.typeOf(pl_op.operand); |
| 4661 | const switch_br = func.air.extraData(Air.SwitchBr, pl_op.payload); |
| 4668 | 4662 | var extra_index: usize = switch_br.end; |
| 4669 | 4663 | var case_i: u32 = 0; |
| 4670 | | const liveness = try self.liveness.getSwitchBr(self.gpa, inst, switch_br.data.cases_len + 1); |
| 4671 | | defer self.gpa.free(liveness.deaths); |
| 4664 | const liveness = try func.liveness.getSwitchBr(func.gpa, inst, switch_br.data.cases_len + 1); |
| 4665 | defer func.gpa.free(liveness.deaths); |
| 4672 | 4666 | |
| 4673 | 4667 | // If the condition dies here in this switch instruction, process |
| 4674 | 4668 | // that death now instead of later as this has an effect on |
| 4675 | 4669 | // whether it needs to be spilled in the branches |
| 4676 | | if (self.liveness.operandDies(inst, 0)) { |
| 4677 | | if (pl_op.operand.toIndex()) |op_inst| try self.processDeath(op_inst); |
| 4670 | if (func.liveness.operandDies(inst, 0)) { |
| 4671 | if (pl_op.operand.toIndex()) |op_inst| try func.processDeath(op_inst); |
| 4678 | 4672 | } |
| 4679 | 4673 | |
| 4680 | | self.scope_generation += 1; |
| 4681 | | const state = try self.saveState(); |
| 4674 | func.scope_generation += 1; |
| 4675 | const state = try func.saveState(); |
| 4682 | 4676 | |
| 4683 | 4677 | while (case_i < switch_br.data.cases_len) : (case_i += 1) { |
| 4684 | | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); |
| 4678 | const case = func.air.extraData(Air.SwitchBr.Case, extra_index); |
| 4685 | 4679 | const items: []const Air.Inst.Ref = |
| 4686 | | @ptrCast(self.air.extra[case.end..][0..case.data.items_len]); |
| 4680 | @ptrCast(func.air.extra[case.end..][0..case.data.items_len]); |
| 4687 | 4681 | const case_body: []const Air.Inst.Index = |
| 4688 | | @ptrCast(self.air.extra[case.end + items.len ..][0..case.data.body_len]); |
| 4682 | @ptrCast(func.air.extra[case.end + items.len ..][0..case.data.body_len]); |
| 4689 | 4683 | extra_index = case.end + items.len + case_body.len; |
| 4690 | 4684 | |
| 4691 | | var relocs = try self.gpa.alloc(Mir.Inst.Index, items.len); |
| 4692 | | defer self.gpa.free(relocs); |
| 4685 | var relocs = try func.gpa.alloc(Mir.Inst.Index, items.len); |
| 4686 | defer func.gpa.free(relocs); |
| 4693 | 4687 | |
| 4694 | 4688 | for (items, relocs, 0..) |item, *reloc, i| { |
| 4695 | 4689 | // switch branches must be comptime-known, so this is stored in an immediate |
| 4696 | | const item_mcv = try self.resolveInst(item); |
| 4690 | const item_mcv = try func.resolveInst(item); |
| 4697 | 4691 | |
| 4698 | | const cmp_mcv: MCValue = try self.binOp( |
| 4692 | const cmp_mcv: MCValue = try func.binOp( |
| 4699 | 4693 | .cmp_neq, |
| 4700 | 4694 | condition, |
| 4701 | 4695 | condition_ty, |
| ... | ... | @@ -4703,10 +4697,10 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4703 | 4697 | condition_ty, |
| 4704 | 4698 | ); |
| 4705 | 4699 | |
| 4706 | | const cmp_reg = try self.copyToTmpRegister(Type.bool, cmp_mcv); |
| 4700 | const cmp_reg = try func.copyToTmpRegister(Type.bool, cmp_mcv); |
| 4707 | 4701 | |
| 4708 | 4702 | if (!(i < relocs.len - 1)) { |
| 4709 | | _ = try self.addInst(.{ |
| 4703 | _ = try func.addInst(.{ |
| 4710 | 4704 | .tag = .pseudo, |
| 4711 | 4705 | .ops = .pseudo_not, |
| 4712 | 4706 | .data = .{ .rr = .{ |
| ... | ... | @@ -4716,32 +4710,32 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4716 | 4710 | }); |
| 4717 | 4711 | } |
| 4718 | 4712 | |
| 4719 | | reloc.* = try self.condBr(condition_ty, .{ .register = cmp_reg }); |
| 4713 | reloc.* = try func.condBr(condition_ty, .{ .register = cmp_reg }); |
| 4720 | 4714 | } |
| 4721 | 4715 | |
| 4722 | | for (liveness.deaths[case_i]) |operand| try self.processDeath(operand); |
| 4716 | for (liveness.deaths[case_i]) |operand| try func.processDeath(operand); |
| 4723 | 4717 | |
| 4724 | | for (relocs[0 .. relocs.len - 1]) |reloc| self.performReloc(reloc); |
| 4725 | | try self.genBody(case_body); |
| 4726 | | try self.restoreState(state, &.{}, .{ |
| 4718 | for (relocs[0 .. relocs.len - 1]) |reloc| func.performReloc(reloc); |
| 4719 | try func.genBody(case_body); |
| 4720 | try func.restoreState(state, &.{}, .{ |
| 4727 | 4721 | .emit_instructions = false, |
| 4728 | 4722 | .update_tracking = true, |
| 4729 | 4723 | .resurrect = true, |
| 4730 | 4724 | .close_scope = true, |
| 4731 | 4725 | }); |
| 4732 | 4726 | |
| 4733 | | self.performReloc(relocs[relocs.len - 1]); |
| 4727 | func.performReloc(relocs[relocs.len - 1]); |
| 4734 | 4728 | } |
| 4735 | 4729 | |
| 4736 | 4730 | if (switch_br.data.else_body_len > 0) { |
| 4737 | 4731 | const else_body: []const Air.Inst.Index = |
| 4738 | | @ptrCast(self.air.extra[extra_index..][0..switch_br.data.else_body_len]); |
| 4732 | @ptrCast(func.air.extra[extra_index..][0..switch_br.data.else_body_len]); |
| 4739 | 4733 | |
| 4740 | 4734 | const else_deaths = liveness.deaths.len - 1; |
| 4741 | | for (liveness.deaths[else_deaths]) |operand| try self.processDeath(operand); |
| 4735 | for (liveness.deaths[else_deaths]) |operand| try func.processDeath(operand); |
| 4742 | 4736 | |
| 4743 | | try self.genBody(else_body); |
| 4744 | | try self.restoreState(state, &.{}, .{ |
| 4737 | try func.genBody(else_body); |
| 4738 | try func.restoreState(state, &.{}, .{ |
| 4745 | 4739 | .emit_instructions = false, |
| 4746 | 4740 | .update_tracking = true, |
| 4747 | 4741 | .resurrect = true, |
| ... | ... | @@ -4750,71 +4744,71 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4750 | 4744 | } |
| 4751 | 4745 | |
| 4752 | 4746 | // We already took care of pl_op.operand earlier, so there's nothing left to do |
| 4753 | | self.finishAirBookkeeping(); |
| 4747 | func.finishAirBookkeeping(); |
| 4754 | 4748 | } |
| 4755 | 4749 | |
| 4756 | | fn performReloc(self: *Self, inst: Mir.Inst.Index) void { |
| 4757 | | const tag = self.mir_instructions.items(.tag)[inst]; |
| 4758 | | const ops = self.mir_instructions.items(.ops)[inst]; |
| 4759 | | const target: Mir.Inst.Index = @intCast(self.mir_instructions.len); |
| 4750 | fn performReloc(func: *Func, inst: Mir.Inst.Index) void { |
| 4751 | const tag = func.mir_instructions.items(.tag)[inst]; |
| 4752 | const ops = func.mir_instructions.items(.ops)[inst]; |
| 4753 | const target: Mir.Inst.Index = @intCast(func.mir_instructions.len); |
| 4760 | 4754 | |
| 4761 | 4755 | switch (tag) { |
| 4762 | 4756 | .bne, |
| 4763 | 4757 | .beq, |
| 4764 | | => self.mir_instructions.items(.data)[inst].b_type.inst = target, |
| 4765 | | .jal => self.mir_instructions.items(.data)[inst].j_type.inst = target, |
| 4758 | => func.mir_instructions.items(.data)[inst].b_type.inst = target, |
| 4759 | .jal => func.mir_instructions.items(.data)[inst].j_type.inst = target, |
| 4766 | 4760 | .pseudo => switch (ops) { |
| 4767 | | .pseudo_j => self.mir_instructions.items(.data)[inst].inst = target, |
| 4761 | .pseudo_j => func.mir_instructions.items(.data)[inst].inst = target, |
| 4768 | 4762 | else => std.debug.panic("TODO: performReloc {s}", .{@tagName(ops)}), |
| 4769 | 4763 | }, |
| 4770 | 4764 | else => std.debug.panic("TODO: performReloc {s}", .{@tagName(tag)}), |
| 4771 | 4765 | } |
| 4772 | 4766 | } |
| 4773 | 4767 | |
| 4774 | | fn airBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4775 | | const mod = self.bin_file.comp.module.?; |
| 4776 | | const br = self.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 4768 | fn airBr(func: *Func, inst: Air.Inst.Index) !void { |
| 4769 | const mod = func.bin_file.comp.module.?; |
| 4770 | const br = func.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 4777 | 4771 | |
| 4778 | | const block_ty = self.typeOfIndex(br.block_inst); |
| 4772 | const block_ty = func.typeOfIndex(br.block_inst); |
| 4779 | 4773 | const block_unused = |
| 4780 | | !block_ty.hasRuntimeBitsIgnoreComptime(mod) or self.liveness.isUnused(br.block_inst); |
| 4781 | | const block_tracking = self.inst_tracking.getPtr(br.block_inst).?; |
| 4782 | | const block_data = self.blocks.getPtr(br.block_inst).?; |
| 4774 | !block_ty.hasRuntimeBitsIgnoreComptime(mod) or func.liveness.isUnused(br.block_inst); |
| 4775 | const block_tracking = func.inst_tracking.getPtr(br.block_inst).?; |
| 4776 | const block_data = func.blocks.getPtr(br.block_inst).?; |
| 4783 | 4777 | const first_br = block_data.relocs.items.len == 0; |
| 4784 | 4778 | const block_result = result: { |
| 4785 | 4779 | if (block_unused) break :result .none; |
| 4786 | 4780 | |
| 4787 | | if (!first_br) try self.getValue(block_tracking.short, null); |
| 4788 | | const src_mcv = try self.resolveInst(br.operand); |
| 4781 | if (!first_br) try func.getValue(block_tracking.short, null); |
| 4782 | const src_mcv = try func.resolveInst(br.operand); |
| 4789 | 4783 | |
| 4790 | | if (self.reuseOperandAdvanced(inst, br.operand, 0, src_mcv, br.block_inst)) { |
| 4784 | if (func.reuseOperandAdvanced(inst, br.operand, 0, src_mcv, br.block_inst)) { |
| 4791 | 4785 | if (first_br) break :result src_mcv; |
| 4792 | 4786 | |
| 4793 | | try self.getValue(block_tracking.short, br.block_inst); |
| 4787 | try func.getValue(block_tracking.short, br.block_inst); |
| 4794 | 4788 | // .long = .none to avoid merging operand and block result stack frames. |
| 4795 | 4789 | const current_tracking: InstTracking = .{ .long = .none, .short = src_mcv }; |
| 4796 | | try current_tracking.materializeUnsafe(self, br.block_inst, block_tracking.*); |
| 4797 | | for (current_tracking.getRegs()) |src_reg| self.register_manager.freeReg(src_reg); |
| 4790 | try current_tracking.materializeUnsafe(func, br.block_inst, block_tracking.*); |
| 4791 | for (current_tracking.getRegs()) |src_reg| func.register_manager.freeReg(src_reg); |
| 4798 | 4792 | break :result block_tracking.short; |
| 4799 | 4793 | } |
| 4800 | 4794 | |
| 4801 | | const dst_mcv = if (first_br) try self.allocRegOrMem(br.block_inst, true) else dst: { |
| 4802 | | try self.getValue(block_tracking.short, br.block_inst); |
| 4795 | const dst_mcv = if (first_br) try func.allocRegOrMem(br.block_inst, true) else dst: { |
| 4796 | try func.getValue(block_tracking.short, br.block_inst); |
| 4803 | 4797 | break :dst block_tracking.short; |
| 4804 | 4798 | }; |
| 4805 | | try self.genCopy(block_ty, dst_mcv, try self.resolveInst(br.operand)); |
| 4799 | try func.genCopy(block_ty, dst_mcv, try func.resolveInst(br.operand)); |
| 4806 | 4800 | break :result dst_mcv; |
| 4807 | 4801 | }; |
| 4808 | 4802 | |
| 4809 | 4803 | // Process operand death so that it is properly accounted for in the State below. |
| 4810 | | if (self.liveness.operandDies(inst, 0)) { |
| 4811 | | if (br.operand.toIndex()) |op_inst| try self.processDeath(op_inst); |
| 4804 | if (func.liveness.operandDies(inst, 0)) { |
| 4805 | if (br.operand.toIndex()) |op_inst| try func.processDeath(op_inst); |
| 4812 | 4806 | } |
| 4813 | 4807 | |
| 4814 | 4808 | if (first_br) { |
| 4815 | 4809 | block_tracking.* = InstTracking.init(block_result); |
| 4816 | | try self.saveRetroactiveState(&block_data.state); |
| 4817 | | } else try self.restoreState(block_data.state, &.{}, .{ |
| 4810 | try func.saveRetroactiveState(&block_data.state); |
| 4811 | } else try func.restoreState(block_data.state, &.{}, .{ |
| 4818 | 4812 | .emit_instructions = true, |
| 4819 | 4813 | .update_tracking = false, |
| 4820 | 4814 | .resurrect = false, |
| ... | ... | @@ -4823,35 +4817,35 @@ fn airBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4823 | 4817 | |
| 4824 | 4818 | // Emit a jump with a relocation. It will be patched up after the block ends. |
| 4825 | 4819 | // Leave the jump offset undefined |
| 4826 | | const jmp_reloc = try self.jump(undefined); |
| 4827 | | try block_data.relocs.append(self.gpa, jmp_reloc); |
| 4820 | const jmp_reloc = try func.jump(undefined); |
| 4821 | try block_data.relocs.append(func.gpa, jmp_reloc); |
| 4828 | 4822 | |
| 4829 | 4823 | // Stop tracking block result without forgetting tracking info |
| 4830 | | try self.freeValue(block_tracking.short); |
| 4824 | try func.freeValue(block_tracking.short); |
| 4831 | 4825 | |
| 4832 | | self.finishAirBookkeeping(); |
| 4826 | func.finishAirBookkeeping(); |
| 4833 | 4827 | } |
| 4834 | 4828 | |
| 4835 | | fn airBoolOp(self: *Self, inst: Air.Inst.Index) !void { |
| 4836 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4837 | | const tag: Air.Inst.Tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4829 | fn airBoolOp(func: *Func, inst: Air.Inst.Index) !void { |
| 4830 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4831 | const tag: Air.Inst.Tag = func.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4838 | 4832 | |
| 4839 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 4840 | | const lhs = try self.resolveInst(bin_op.lhs); |
| 4841 | | const rhs = try self.resolveInst(bin_op.rhs); |
| 4833 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4834 | const lhs = try func.resolveInst(bin_op.lhs); |
| 4835 | const rhs = try func.resolveInst(bin_op.rhs); |
| 4842 | 4836 | const lhs_ty = Type.bool; |
| 4843 | 4837 | const rhs_ty = Type.bool; |
| 4844 | 4838 | |
| 4845 | | const lhs_reg, const lhs_lock = try self.promoteReg(lhs_ty, lhs, .{}); |
| 4846 | | defer if (lhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 4839 | const lhs_reg, const lhs_lock = try func.promoteReg(lhs_ty, lhs, .{}); |
| 4840 | defer if (lhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 4847 | 4841 | |
| 4848 | | const rhs_reg, const rhs_lock = try self.promoteReg(rhs_ty, rhs, .{}); |
| 4849 | | defer if (rhs_lock) |lock| self.register_manager.unlockReg(lock); |
| 4842 | const rhs_reg, const rhs_lock = try func.promoteReg(rhs_ty, rhs, .{}); |
| 4843 | defer if (rhs_lock) |lock| func.register_manager.unlockReg(lock); |
| 4850 | 4844 | |
| 4851 | | const result_reg, const result_lock = try self.allocReg(.int); |
| 4852 | | defer self.register_manager.unlockReg(result_lock); |
| 4845 | const result_reg, const result_lock = try func.allocReg(.int); |
| 4846 | defer func.register_manager.unlockReg(result_lock); |
| 4853 | 4847 | |
| 4854 | | _ = try self.addInst(.{ |
| 4848 | _ = try func.addInst(.{ |
| 4855 | 4849 | .tag = if (tag == .bool_or) .@"or" else .@"and", |
| 4856 | 4850 | .ops = .rrr, |
| 4857 | 4851 | .data = .{ .r_type = .{ |
| ... | ... | @@ -4862,8 +4856,8 @@ fn airBoolOp(self: *Self, inst: Air.Inst.Index) !void { |
| 4862 | 4856 | }); |
| 4863 | 4857 | |
| 4864 | 4858 | // safety truncate |
| 4865 | | if (self.wantSafety()) { |
| 4866 | | _ = try self.addInst(.{ |
| 4859 | if (func.wantSafety()) { |
| 4860 | _ = try func.addInst(.{ |
| 4867 | 4861 | .tag = .andi, |
| 4868 | 4862 | .ops = .rri, |
| 4869 | 4863 | .data = .{ .i_type = .{ |
| ... | ... | @@ -4876,35 +4870,35 @@ fn airBoolOp(self: *Self, inst: Air.Inst.Index) !void { |
| 4876 | 4870 | |
| 4877 | 4871 | break :result .{ .register = result_reg }; |
| 4878 | 4872 | }; |
| 4879 | | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 4873 | return func.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 4880 | 4874 | } |
| 4881 | 4875 | |
| 4882 | | fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 4883 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4884 | | const extra = self.air.extraData(Air.Asm, ty_pl.payload); |
| 4876 | fn airAsm(func: *Func, inst: Air.Inst.Index) !void { |
| 4877 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4878 | const extra = func.air.extraData(Air.Asm, ty_pl.payload); |
| 4885 | 4879 | const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0; |
| 4886 | 4880 | const clobbers_len: u31 = @truncate(extra.data.flags); |
| 4887 | 4881 | var extra_i: usize = extra.end; |
| 4888 | 4882 | const outputs: []const Air.Inst.Ref = |
| 4889 | | @ptrCast(self.air.extra[extra_i..][0..extra.data.outputs_len]); |
| 4883 | @ptrCast(func.air.extra[extra_i..][0..extra.data.outputs_len]); |
| 4890 | 4884 | extra_i += outputs.len; |
| 4891 | | const inputs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra_i..][0..extra.data.inputs_len]); |
| 4885 | const inputs: []const Air.Inst.Ref = @ptrCast(func.air.extra[extra_i..][0..extra.data.inputs_len]); |
| 4892 | 4886 | extra_i += inputs.len; |
| 4893 | 4887 | |
| 4894 | 4888 | log.debug("airAsm input: {any}", .{inputs}); |
| 4895 | 4889 | |
| 4896 | | const dead = !is_volatile and self.liveness.isUnused(inst); |
| 4890 | const dead = !is_volatile and func.liveness.isUnused(inst); |
| 4897 | 4891 | const result: MCValue = if (dead) .unreach else result: { |
| 4898 | 4892 | if (outputs.len > 1) { |
| 4899 | | return self.fail("TODO implement codegen for asm with more than 1 output", .{}); |
| 4893 | return func.fail("TODO implement codegen for asm with more than 1 output", .{}); |
| 4900 | 4894 | } |
| 4901 | 4895 | |
| 4902 | 4896 | const output_constraint: ?[]const u8 = for (outputs) |output| { |
| 4903 | 4897 | if (output != .none) { |
| 4904 | | return self.fail("TODO implement codegen for non-expr asm", .{}); |
| 4898 | return func.fail("TODO implement codegen for non-expr asm", .{}); |
| 4905 | 4899 | } |
| 4906 | | const extra_bytes = std.mem.sliceAsBytes(self.air.extra[extra_i..]); |
| 4907 | | const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(self.air.extra[extra_i..]), 0); |
| 4900 | const extra_bytes = std.mem.sliceAsBytes(func.air.extra[extra_i..]); |
| 4901 | const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(func.air.extra[extra_i..]), 0); |
| 4908 | 4902 | const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); |
| 4909 | 4903 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| 4910 | 4904 | // for the string, we still use the next u32 for the null terminator. |
| ... | ... | @@ -4914,7 +4908,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 4914 | 4908 | } else null; |
| 4915 | 4909 | |
| 4916 | 4910 | for (inputs) |input| { |
| 4917 | | const input_bytes = std.mem.sliceAsBytes(self.air.extra[extra_i..]); |
| 4911 | const input_bytes = std.mem.sliceAsBytes(func.air.extra[extra_i..]); |
| 4918 | 4912 | const constraint = std.mem.sliceTo(input_bytes, 0); |
| 4919 | 4913 | const name = std.mem.sliceTo(input_bytes[constraint.len + 1 ..], 0); |
| 4920 | 4914 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| ... | ... | @@ -4922,21 +4916,21 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 4922 | 4916 | extra_i += (constraint.len + name.len + (2 + 3)) / 4; |
| 4923 | 4917 | |
| 4924 | 4918 | if (constraint.len < 3 or constraint[0] != '{' or constraint[constraint.len - 1] != '}') { |
| 4925 | | return self.fail("unrecognized asm input constraint: '{s}'", .{constraint}); |
| 4919 | return func.fail("unrecognized asm input constraint: '{s}'", .{constraint}); |
| 4926 | 4920 | } |
| 4927 | 4921 | const reg_name = constraint[1 .. constraint.len - 1]; |
| 4928 | 4922 | const reg = parseRegName(reg_name) orelse |
| 4929 | | return self.fail("unrecognized register: '{s}'", .{reg_name}); |
| 4923 | return func.fail("unrecognized register: '{s}'", .{reg_name}); |
| 4930 | 4924 | |
| 4931 | | const arg_mcv = try self.resolveInst(input); |
| 4932 | | try self.register_manager.getReg(reg, null); |
| 4933 | | try self.genSetReg(self.typeOf(input), reg, arg_mcv); |
| 4925 | const arg_mcv = try func.resolveInst(input); |
| 4926 | try func.register_manager.getReg(reg, null); |
| 4927 | try func.genSetReg(func.typeOf(input), reg, arg_mcv); |
| 4934 | 4928 | } |
| 4935 | 4929 | |
| 4936 | 4930 | { |
| 4937 | 4931 | var clobber_i: u32 = 0; |
| 4938 | 4932 | while (clobber_i < clobbers_len) : (clobber_i += 1) { |
| 4939 | | const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(self.air.extra[extra_i..]), 0); |
| 4933 | const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(func.air.extra[extra_i..]), 0); |
| 4940 | 4934 | // This equation accounts for the fact that even if we have exactly 4 bytes |
| 4941 | 4935 | // for the string, we still use the next u32 for the null terminator. |
| 4942 | 4936 | extra_i += clobber.len / 4 + 1; |
| ... | ... | @@ -4944,31 +4938,31 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 4944 | 4938 | if (std.mem.eql(u8, clobber, "") or std.mem.eql(u8, clobber, "memory")) { |
| 4945 | 4939 | // nothing really to do |
| 4946 | 4940 | } else { |
| 4947 | | try self.register_manager.getReg(parseRegName(clobber) orelse |
| 4948 | | return self.fail("invalid clobber: '{s}'", .{clobber}), null); |
| 4941 | try func.register_manager.getReg(parseRegName(clobber) orelse |
| 4942 | return func.fail("invalid clobber: '{s}'", .{clobber}), null); |
| 4949 | 4943 | } |
| 4950 | 4944 | } |
| 4951 | 4945 | } |
| 4952 | 4946 | |
| 4953 | | const asm_source = std.mem.sliceAsBytes(self.air.extra[extra_i..])[0..extra.data.source_len]; |
| 4947 | const asm_source = std.mem.sliceAsBytes(func.air.extra[extra_i..])[0..extra.data.source_len]; |
| 4954 | 4948 | |
| 4955 | 4949 | if (std.meta.stringToEnum(Mir.Inst.Tag, asm_source)) |tag| { |
| 4956 | | _ = try self.addInst(.{ |
| 4950 | _ = try func.addInst(.{ |
| 4957 | 4951 | .tag = tag, |
| 4958 | 4952 | .ops = .none, |
| 4959 | 4953 | .data = undefined, |
| 4960 | 4954 | }); |
| 4961 | 4955 | } else { |
| 4962 | | return self.fail("TODO: asm_source {s}", .{asm_source}); |
| 4956 | return func.fail("TODO: asm_source {s}", .{asm_source}); |
| 4963 | 4957 | } |
| 4964 | 4958 | |
| 4965 | 4959 | if (output_constraint) |output| { |
| 4966 | 4960 | if (output.len < 4 or output[0] != '=' or output[1] != '{' or output[output.len - 1] != '}') { |
| 4967 | | return self.fail("unrecognized asm output constraint: '{s}'", .{output}); |
| 4961 | return func.fail("unrecognized asm output constraint: '{s}'", .{output}); |
| 4968 | 4962 | } |
| 4969 | 4963 | const reg_name = output[2 .. output.len - 1]; |
| 4970 | 4964 | const reg = parseRegName(reg_name) orelse |
| 4971 | | return self.fail("unrecognized register: '{s}'", .{reg_name}); |
| 4965 | return func.fail("unrecognized register: '{s}'", .{reg_name}); |
| 4972 | 4966 | break :result .{ .register = reg }; |
| 4973 | 4967 | } else { |
| 4974 | 4968 | break :result .{ .none = {} }; |
| ... | ... | @@ -4987,17 +4981,17 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 4987 | 4981 | } |
| 4988 | 4982 | if (buf_index + inputs.len > buf.len) break :simple; |
| 4989 | 4983 | @memcpy(buf[buf_index..][0..inputs.len], inputs); |
| 4990 | | return self.finishAir(inst, result, buf); |
| 4984 | return func.finishAir(inst, result, buf); |
| 4991 | 4985 | } |
| 4992 | | var bt = self.liveness.iterateBigTomb(inst); |
| 4993 | | for (outputs) |output| if (output != .none) try self.feed(&bt, output); |
| 4994 | | for (inputs) |input| try self.feed(&bt, input); |
| 4995 | | return self.finishAirResult(inst, result); |
| 4986 | var bt = func.liveness.iterateBigTomb(inst); |
| 4987 | for (outputs) |output| if (output != .none) try func.feed(&bt, output); |
| 4988 | for (inputs) |input| try func.feed(&bt, input); |
| 4989 | return func.finishAirResult(inst, result); |
| 4996 | 4990 | } |
| 4997 | 4991 | |
| 4998 | 4992 | /// Sets the value of `dst_mcv` to the value of `src_mcv`. |
| 4999 | | fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { |
| 5000 | | const zcu = self.bin_file.comp.module.?; |
| 4993 | fn genCopy(func: *Func, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { |
| 4994 | const zcu = func.bin_file.comp.module.?; |
| 5001 | 4995 | |
| 5002 | 4996 | // There isn't anything to store |
| 5003 | 4997 | if (dst_mcv == .none) return; |
| ... | ... | @@ -5008,8 +5002,8 @@ fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { |
| 5008 | 5002 | } |
| 5009 | 5003 | |
| 5010 | 5004 | switch (dst_mcv) { |
| 5011 | | .register => |reg| return self.genSetReg(ty, reg, src_mcv), |
| 5012 | | .register_offset => |dst_reg_off| try self.genSetReg(ty, dst_reg_off.reg, switch (src_mcv) { |
| 5005 | .register => |reg| return func.genSetReg(ty, reg, src_mcv), |
| 5006 | .register_offset => |dst_reg_off| try func.genSetReg(ty, dst_reg_off.reg, switch (src_mcv) { |
| 5013 | 5007 | .none, |
| 5014 | 5008 | .unreach, |
| 5015 | 5009 | .dead, |
| ... | ... | @@ -5020,49 +5014,49 @@ fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { |
| 5020 | 5014 | .register_offset, |
| 5021 | 5015 | => src_mcv.offset(-dst_reg_off.off), |
| 5022 | 5016 | else => .{ .register_offset = .{ |
| 5023 | | .reg = try self.copyToTmpRegister(ty, src_mcv), |
| 5017 | .reg = try func.copyToTmpRegister(ty, src_mcv), |
| 5024 | 5018 | .off = -dst_reg_off.off, |
| 5025 | 5019 | } }, |
| 5026 | 5020 | }), |
| 5027 | | .indirect => |reg_off| try self.genSetMem( |
| 5021 | .indirect => |reg_off| try func.genSetMem( |
| 5028 | 5022 | .{ .reg = reg_off.reg }, |
| 5029 | 5023 | reg_off.off, |
| 5030 | 5024 | ty, |
| 5031 | 5025 | src_mcv, |
| 5032 | 5026 | ), |
| 5033 | | .load_frame => |frame_addr| try self.genSetMem( |
| 5027 | .load_frame => |frame_addr| try func.genSetMem( |
| 5034 | 5028 | .{ .frame = frame_addr.index }, |
| 5035 | 5029 | frame_addr.off, |
| 5036 | 5030 | ty, |
| 5037 | 5031 | src_mcv, |
| 5038 | 5032 | ), |
| 5039 | | .memory => return self.fail("TODO: genCopy memory", .{}), |
| 5033 | .memory => return func.fail("TODO: genCopy memory", .{}), |
| 5040 | 5034 | .register_pair => |dst_regs| { |
| 5041 | 5035 | const src_info: ?struct { addr_reg: Register, addr_lock: ?RegisterLock } = switch (src_mcv) { |
| 5042 | 5036 | .register_pair, .memory, .indirect, .load_frame => null, |
| 5043 | 5037 | .load_symbol => src: { |
| 5044 | | const src_addr_reg, const src_addr_lock = try self.promoteReg(Type.usize, src_mcv.address(), .{}); |
| 5045 | | errdefer self.register_manager.unlockReg(src_addr_lock); |
| 5038 | const src_addr_reg, const src_addr_lock = try func.promoteReg(Type.usize, src_mcv.address(), .{}); |
| 5039 | errdefer func.register_manager.unlockReg(src_addr_lock); |
| 5046 | 5040 | |
| 5047 | 5041 | break :src .{ .addr_reg = src_addr_reg, .addr_lock = src_addr_lock }; |
| 5048 | 5042 | }, |
| 5049 | | .air_ref => |src_ref| return self.genCopy( |
| 5043 | .air_ref => |src_ref| return func.genCopy( |
| 5050 | 5044 | ty, |
| 5051 | 5045 | dst_mcv, |
| 5052 | | try self.resolveInst(src_ref), |
| 5046 | try func.resolveInst(src_ref), |
| 5053 | 5047 | ), |
| 5054 | 5048 | else => unreachable, |
| 5055 | 5049 | }; |
| 5056 | 5050 | |
| 5057 | 5051 | defer if (src_info) |info| { |
| 5058 | 5052 | if (info.addr_lock) |lock| { |
| 5059 | | self.register_manager.unlockReg(lock); |
| 5053 | func.register_manager.unlockReg(lock); |
| 5060 | 5054 | } |
| 5061 | 5055 | }; |
| 5062 | 5056 | |
| 5063 | 5057 | var part_disp: i32 = 0; |
| 5064 | | for (dst_regs, try self.splitType(ty), 0..) |dst_reg, dst_ty, part_i| { |
| 5065 | | try self.genSetReg(dst_ty, dst_reg, switch (src_mcv) { |
| 5058 | for (dst_regs, try func.splitType(ty), 0..) |dst_reg, dst_ty, part_i| { |
| 5059 | try func.genSetReg(dst_ty, dst_reg, switch (src_mcv) { |
| 5066 | 5060 | .register_pair => |src_regs| .{ .register = src_regs[part_i] }, |
| 5067 | 5061 | .memory, .indirect, .load_frame => src_mcv.address().offset(part_disp).deref(), |
| 5068 | 5062 | .load_symbol => .{ .indirect = .{ |
| ... | ... | @@ -5074,31 +5068,31 @@ fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { |
| 5074 | 5068 | part_disp += @intCast(dst_ty.abiSize(zcu)); |
| 5075 | 5069 | } |
| 5076 | 5070 | }, |
| 5077 | | else => return self.fail("TODO: genCopy to {s} from {s}", .{ @tagName(dst_mcv), @tagName(src_mcv) }), |
| 5071 | else => return func.fail("TODO: genCopy to {s} from {s}", .{ @tagName(dst_mcv), @tagName(src_mcv) }), |
| 5078 | 5072 | } |
| 5079 | 5073 | } |
| 5080 | 5074 | |
| 5081 | 5075 | fn genInlineMemcpy( |
| 5082 | | self: *Self, |
| 5076 | func: *Func, |
| 5083 | 5077 | dst_ptr: MCValue, |
| 5084 | 5078 | src_ptr: MCValue, |
| 5085 | 5079 | len: MCValue, |
| 5086 | 5080 | ) !void { |
| 5087 | | const regs = try self.register_manager.allocRegs(4, .{null} ** 4, abi.Registers.Integer.temporary); |
| 5088 | | const locks = self.register_manager.lockRegsAssumeUnused(4, regs); |
| 5089 | | defer for (locks) |lock| self.register_manager.unlockReg(lock); |
| 5081 | const regs = try func.register_manager.allocRegs(4, .{null} ** 4, abi.Registers.Integer.temporary); |
| 5082 | const locks = func.register_manager.lockRegsAssumeUnused(4, regs); |
| 5083 | defer for (locks) |lock| func.register_manager.unlockReg(lock); |
| 5090 | 5084 | |
| 5091 | 5085 | const count = regs[0]; |
| 5092 | 5086 | const tmp = regs[1]; |
| 5093 | 5087 | const src = regs[2]; |
| 5094 | 5088 | const dst = regs[3]; |
| 5095 | 5089 | |
| 5096 | | try self.genSetReg(Type.usize, count, len); |
| 5097 | | try self.genSetReg(Type.usize, src, src_ptr); |
| 5098 | | try self.genSetReg(Type.usize, dst, dst_ptr); |
| 5090 | try func.genSetReg(Type.usize, count, len); |
| 5091 | try func.genSetReg(Type.usize, src, src_ptr); |
| 5092 | try func.genSetReg(Type.usize, dst, dst_ptr); |
| 5099 | 5093 | |
| 5100 | 5094 | // lb tmp, 0(src) |
| 5101 | | const first_inst = try self.addInst(.{ |
| 5095 | const first_inst = try func.addInst(.{ |
| 5102 | 5096 | .tag = .lb, |
| 5103 | 5097 | .ops = .rri, |
| 5104 | 5098 | .data = .{ |
| ... | ... | @@ -5111,7 +5105,7 @@ fn genInlineMemcpy( |
| 5111 | 5105 | }); |
| 5112 | 5106 | |
| 5113 | 5107 | // sb tmp, 0(dst) |
| 5114 | | _ = try self.addInst(.{ |
| 5108 | _ = try func.addInst(.{ |
| 5115 | 5109 | .tag = .sb, |
| 5116 | 5110 | .ops = .rri, |
| 5117 | 5111 | .data = .{ |
| ... | ... | @@ -5124,7 +5118,7 @@ fn genInlineMemcpy( |
| 5124 | 5118 | }); |
| 5125 | 5119 | |
| 5126 | 5120 | // dec count by 1 |
| 5127 | | _ = try self.addInst(.{ |
| 5121 | _ = try func.addInst(.{ |
| 5128 | 5122 | .tag = .addi, |
| 5129 | 5123 | .ops = .rri, |
| 5130 | 5124 | .data = .{ |
| ... | ... | @@ -5137,12 +5131,12 @@ fn genInlineMemcpy( |
| 5137 | 5131 | }); |
| 5138 | 5132 | |
| 5139 | 5133 | // branch if count is 0 |
| 5140 | | _ = try self.addInst(.{ |
| 5134 | _ = try func.addInst(.{ |
| 5141 | 5135 | .tag = .beq, |
| 5142 | 5136 | .ops = .rr_inst, |
| 5143 | 5137 | .data = .{ |
| 5144 | 5138 | .b_type = .{ |
| 5145 | | .inst = @intCast(self.mir_instructions.len + 4), // points after the last inst |
| 5139 | .inst = @intCast(func.mir_instructions.len + 4), // points after the last inst |
| 5146 | 5140 | .rs1 = count, |
| 5147 | 5141 | .rs2 = .zero, |
| 5148 | 5142 | }, |
| ... | ... | @@ -5150,7 +5144,7 @@ fn genInlineMemcpy( |
| 5150 | 5144 | }); |
| 5151 | 5145 | |
| 5152 | 5146 | // increment the pointers |
| 5153 | | _ = try self.addInst(.{ |
| 5147 | _ = try func.addInst(.{ |
| 5154 | 5148 | .tag = .addi, |
| 5155 | 5149 | .ops = .rri, |
| 5156 | 5150 | .data = .{ |
| ... | ... | @@ -5162,7 +5156,7 @@ fn genInlineMemcpy( |
| 5162 | 5156 | }, |
| 5163 | 5157 | }); |
| 5164 | 5158 | |
| 5165 | | _ = try self.addInst(.{ |
| 5159 | _ = try func.addInst(.{ |
| 5166 | 5160 | .tag = .addi, |
| 5167 | 5161 | .ops = .rri, |
| 5168 | 5162 | .data = .{ |
| ... | ... | @@ -5175,7 +5169,7 @@ fn genInlineMemcpy( |
| 5175 | 5169 | }); |
| 5176 | 5170 | |
| 5177 | 5171 | // jump back to start of loop |
| 5178 | | _ = try self.addInst(.{ |
| 5172 | _ = try func.addInst(.{ |
| 5179 | 5173 | .tag = .pseudo, |
| 5180 | 5174 | .ops = .pseudo_j, |
| 5181 | 5175 | .data = .{ .inst = first_inst }, |
| ... | ... | @@ -5183,25 +5177,25 @@ fn genInlineMemcpy( |
| 5183 | 5177 | } |
| 5184 | 5178 | |
| 5185 | 5179 | fn genInlineMemset( |
| 5186 | | self: *Self, |
| 5180 | func: *Func, |
| 5187 | 5181 | dst_ptr: MCValue, |
| 5188 | 5182 | src_value: MCValue, |
| 5189 | 5183 | len: MCValue, |
| 5190 | 5184 | ) !void { |
| 5191 | | const regs = try self.register_manager.allocRegs(3, .{null} ** 3, abi.Registers.Integer.temporary); |
| 5192 | | const locks = self.register_manager.lockRegsAssumeUnused(3, regs); |
| 5193 | | defer for (locks) |lock| self.register_manager.unlockReg(lock); |
| 5185 | const regs = try func.register_manager.allocRegs(3, .{null} ** 3, abi.Registers.Integer.temporary); |
| 5186 | const locks = func.register_manager.lockRegsAssumeUnused(3, regs); |
| 5187 | defer for (locks) |lock| func.register_manager.unlockReg(lock); |
| 5194 | 5188 | |
| 5195 | 5189 | const count = regs[0]; |
| 5196 | 5190 | const src = regs[1]; |
| 5197 | 5191 | const dst = regs[2]; |
| 5198 | 5192 | |
| 5199 | | try self.genSetReg(Type.usize, count, len); |
| 5200 | | try self.genSetReg(Type.usize, src, src_value); |
| 5201 | | try self.genSetReg(Type.usize, dst, dst_ptr); |
| 5193 | try func.genSetReg(Type.usize, count, len); |
| 5194 | try func.genSetReg(Type.usize, src, src_value); |
| 5195 | try func.genSetReg(Type.usize, dst, dst_ptr); |
| 5202 | 5196 | |
| 5203 | 5197 | // sb src, 0(dst) |
| 5204 | | const first_inst = try self.addInst(.{ |
| 5198 | const first_inst = try func.addInst(.{ |
| 5205 | 5199 | .tag = .sb, |
| 5206 | 5200 | .ops = .rri, |
| 5207 | 5201 | .data = .{ |
| ... | ... | @@ -5214,7 +5208,7 @@ fn genInlineMemset( |
| 5214 | 5208 | }); |
| 5215 | 5209 | |
| 5216 | 5210 | // dec count by 1 |
| 5217 | | _ = try self.addInst(.{ |
| 5211 | _ = try func.addInst(.{ |
| 5218 | 5212 | .tag = .addi, |
| 5219 | 5213 | .ops = .rri, |
| 5220 | 5214 | .data = .{ |
| ... | ... | @@ -5227,12 +5221,12 @@ fn genInlineMemset( |
| 5227 | 5221 | }); |
| 5228 | 5222 | |
| 5229 | 5223 | // branch if count is 0 |
| 5230 | | _ = try self.addInst(.{ |
| 5224 | _ = try func.addInst(.{ |
| 5231 | 5225 | .tag = .beq, |
| 5232 | 5226 | .ops = .rr_inst, |
| 5233 | 5227 | .data = .{ |
| 5234 | 5228 | .b_type = .{ |
| 5235 | | .inst = @intCast(self.mir_instructions.len + 4), // points after the last inst |
| 5229 | .inst = @intCast(func.mir_instructions.len + 4), // points after the last inst |
| 5236 | 5230 | .rs1 = count, |
| 5237 | 5231 | .rs2 = .zero, |
| 5238 | 5232 | }, |
| ... | ... | @@ -5240,7 +5234,7 @@ fn genInlineMemset( |
| 5240 | 5234 | }); |
| 5241 | 5235 | |
| 5242 | 5236 | // increment the pointers |
| 5243 | | _ = try self.addInst(.{ |
| 5237 | _ = try func.addInst(.{ |
| 5244 | 5238 | .tag = .addi, |
| 5245 | 5239 | .ops = .rri, |
| 5246 | 5240 | .data = .{ |
| ... | ... | @@ -5253,7 +5247,7 @@ fn genInlineMemset( |
| 5253 | 5247 | }); |
| 5254 | 5248 | |
| 5255 | 5249 | // jump back to start of loop |
| 5256 | | _ = try self.addInst(.{ |
| 5250 | _ = try func.addInst(.{ |
| 5257 | 5251 | .tag = .pseudo, |
| 5258 | 5252 | .ops = .pseudo_j, |
| 5259 | 5253 | .data = .{ |
| ... | ... | @@ -5263,8 +5257,8 @@ fn genInlineMemset( |
| 5263 | 5257 | } |
| 5264 | 5258 | |
| 5265 | 5259 | /// Sets the value of `src_mcv` into `reg`. Assumes you have a lock on it. |
| 5266 | | fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError!void { |
| 5267 | | const zcu = self.bin_file.comp.module.?; |
| 5260 | fn genSetReg(func: *Func, ty: Type, reg: Register, src_mcv: MCValue) InnerError!void { |
| 5261 | const zcu = func.bin_file.comp.module.?; |
| 5268 | 5262 | const abi_size: u32 = @intCast(ty.abiSize(zcu)); |
| 5269 | 5263 | |
| 5270 | 5264 | if (abi_size > 8) return std.debug.panic("tried to set reg with size {}", .{abi_size}); |
| ... | ... | @@ -5275,17 +5269,17 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5275 | 5269 | .dead => unreachable, |
| 5276 | 5270 | .unreach, .none => return, // Nothing to do. |
| 5277 | 5271 | .undef => { |
| 5278 | | if (!self.wantSafety()) |
| 5272 | if (!func.wantSafety()) |
| 5279 | 5273 | return; // The already existing value will do just fine. |
| 5280 | 5274 | // Write the debug undefined value. |
| 5281 | | return self.genSetReg(ty, reg, .{ .immediate = 0xaaaaaaaaaaaaaaaa }); |
| 5275 | return func.genSetReg(ty, reg, .{ .immediate = 0xaaaaaaaaaaaaaaaa }); |
| 5282 | 5276 | }, |
| 5283 | 5277 | .immediate => |unsigned_x| { |
| 5284 | 5278 | assert(dst_reg_class == .int); |
| 5285 | 5279 | |
| 5286 | 5280 | const x: i64 = @bitCast(unsigned_x); |
| 5287 | 5281 | if (math.minInt(i12) <= x and x <= math.maxInt(i12)) { |
| 5288 | | _ = try self.addInst(.{ |
| 5282 | _ = try func.addInst(.{ |
| 5289 | 5283 | .tag = .addi, |
| 5290 | 5284 | .ops = .rri, |
| 5291 | 5285 | .data = .{ .i_type = .{ |
| ... | ... | @@ -5299,7 +5293,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5299 | 5293 | const carry: i32 = if (lo12 < 0) 1 else 0; |
| 5300 | 5294 | const hi20: i20 = @truncate((x >> 12) +% carry); |
| 5301 | 5295 | |
| 5302 | | _ = try self.addInst(.{ |
| 5296 | _ = try func.addInst(.{ |
| 5303 | 5297 | .tag = .lui, |
| 5304 | 5298 | .ops = .ri, |
| 5305 | 5299 | .data = .{ .u_type = .{ |
| ... | ... | @@ -5307,7 +5301,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5307 | 5301 | .imm20 = Immediate.s(hi20), |
| 5308 | 5302 | } }, |
| 5309 | 5303 | }); |
| 5310 | | _ = try self.addInst(.{ |
| 5304 | _ = try func.addInst(.{ |
| 5311 | 5305 | .tag = .addi, |
| 5312 | 5306 | .ops = .rri, |
| 5313 | 5307 | .data = .{ .i_type = .{ |
| ... | ... | @@ -5320,17 +5314,17 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5320 | 5314 | // TODO: use a more advanced myriad seq to do this without a reg. |
| 5321 | 5315 | // see: https://github.com/llvm/llvm-project/blob/081a66ffacfe85a37ff775addafcf3371e967328/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp#L224 |
| 5322 | 5316 | |
| 5323 | | const temp, const temp_lock = try self.allocReg(.int); |
| 5324 | | defer self.register_manager.unlockReg(temp_lock); |
| 5317 | const temp, const temp_lock = try func.allocReg(.int); |
| 5318 | defer func.register_manager.unlockReg(temp_lock); |
| 5325 | 5319 | |
| 5326 | 5320 | const lo32: i32 = @truncate(x); |
| 5327 | 5321 | const carry: i32 = if (lo32 < 0) 1 else 0; |
| 5328 | 5322 | const hi32: i32 = @truncate((x >> 32) +% carry); |
| 5329 | 5323 | |
| 5330 | | try self.genSetReg(Type.i32, temp, .{ .immediate = @bitCast(@as(i64, lo32)) }); |
| 5331 | | try self.genSetReg(Type.i32, reg, .{ .immediate = @bitCast(@as(i64, hi32)) }); |
| 5324 | try func.genSetReg(Type.i32, temp, .{ .immediate = @bitCast(@as(i64, lo32)) }); |
| 5325 | try func.genSetReg(Type.i32, reg, .{ .immediate = @bitCast(@as(i64, hi32)) }); |
| 5332 | 5326 | |
| 5333 | | _ = try self.addInst(.{ |
| 5327 | _ = try func.addInst(.{ |
| 5334 | 5328 | .tag = .slli, |
| 5335 | 5329 | .ops = .rri, |
| 5336 | 5330 | .data = .{ .i_type = .{ |
| ... | ... | @@ -5340,7 +5334,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5340 | 5334 | } }, |
| 5341 | 5335 | }); |
| 5342 | 5336 | |
| 5343 | | _ = try self.addInst(.{ |
| 5337 | _ = try func.addInst(.{ |
| 5344 | 5338 | .tag = .add, |
| 5345 | 5339 | .ops = .rrr, |
| 5346 | 5340 | .data = .{ .r_type = .{ |
| ... | ... | @@ -5360,11 +5354,11 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5360 | 5354 | |
| 5361 | 5355 | if (src_reg_class == .float and dst_reg_class == .int) { |
| 5362 | 5356 | // to move from float -> int, we use FMV.X.W |
| 5363 | | return self.fail("TODO: genSetReg float -> int", .{}); |
| 5357 | return func.fail("TODO: genSetReg float -> int", .{}); |
| 5364 | 5358 | } |
| 5365 | 5359 | |
| 5366 | 5360 | // mv reg, src_reg |
| 5367 | | _ = try self.addInst(.{ |
| 5361 | _ = try func.addInst(.{ |
| 5368 | 5362 | .tag = .pseudo, |
| 5369 | 5363 | .ops = .pseudo_mv, |
| 5370 | 5364 | .data = .{ .rr = .{ |
| ... | ... | @@ -5373,9 +5367,9 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5373 | 5367 | } }, |
| 5374 | 5368 | }); |
| 5375 | 5369 | }, |
| 5376 | | .register_pair => return self.fail("genSetReg should we allow reg -> reg_pair?", .{}), |
| 5370 | .register_pair => return func.fail("genSetReg should we allow reg -> reg_pair?", .{}), |
| 5377 | 5371 | .load_frame => |frame| { |
| 5378 | | _ = try self.addInst(.{ |
| 5372 | _ = try func.addInst(.{ |
| 5379 | 5373 | .tag = .pseudo, |
| 5380 | 5374 | .ops = .pseudo_load_rm, |
| 5381 | 5375 | .data = .{ .rm = .{ |
| ... | ... | @@ -5384,7 +5378,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5384 | 5378 | .base = .{ .frame = frame.index }, |
| 5385 | 5379 | .mod = .{ |
| 5386 | 5380 | .rm = .{ |
| 5387 | | .size = self.memSize(ty), |
| 5381 | .size = func.memSize(ty), |
| 5388 | 5382 | .disp = frame.off, |
| 5389 | 5383 | }, |
| 5390 | 5384 | }, |
| ... | ... | @@ -5393,9 +5387,9 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5393 | 5387 | }); |
| 5394 | 5388 | }, |
| 5395 | 5389 | .memory => |addr| { |
| 5396 | | try self.genSetReg(ty, reg, .{ .immediate = addr }); |
| 5390 | try func.genSetReg(ty, reg, .{ .immediate = addr }); |
| 5397 | 5391 | |
| 5398 | | _ = try self.addInst(.{ |
| 5392 | _ = try func.addInst(.{ |
| 5399 | 5393 | .tag = .ld, |
| 5400 | 5394 | .ops = .rri, |
| 5401 | 5395 | .data = .{ .i_type = .{ |
| ... | ... | @@ -5406,7 +5400,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5406 | 5400 | }); |
| 5407 | 5401 | }, |
| 5408 | 5402 | .lea_frame, .register_offset => { |
| 5409 | | _ = try self.addInst(.{ |
| 5403 | _ = try func.addInst(.{ |
| 5410 | 5404 | .tag = .pseudo, |
| 5411 | 5405 | .ops = .pseudo_lea_rm, |
| 5412 | 5406 | .data = .{ .rm = .{ |
| ... | ... | @@ -5416,7 +5410,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5416 | 5410 | .base = .{ .reg = reg_off.reg }, |
| 5417 | 5411 | .mod = .{ |
| 5418 | 5412 | .rm = .{ |
| 5419 | | .size = self.memSize(ty), |
| 5413 | .size = func.memSize(ty), |
| 5420 | 5414 | .disp = reg_off.off, |
| 5421 | 5415 | }, |
| 5422 | 5416 | }, |
| ... | ... | @@ -5425,7 +5419,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5425 | 5419 | .base = .{ .frame = frame.index }, |
| 5426 | 5420 | .mod = .{ |
| 5427 | 5421 | .rm = .{ |
| 5428 | | .size = self.memSize(ty), |
| 5422 | .size = func.memSize(ty), |
| 5429 | 5423 | .disp = frame.off, |
| 5430 | 5424 | }, |
| 5431 | 5425 | }, |
| ... | ... | @@ -5444,7 +5438,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5444 | 5438 | else |
| 5445 | 5439 | .lb, |
| 5446 | 5440 | 2 => if (float_class) |
| 5447 | | return self.fail("TODO: genSetReg indirect 16-bit float", .{}) |
| 5441 | return func.fail("TODO: genSetReg indirect 16-bit float", .{}) |
| 5448 | 5442 | else |
| 5449 | 5443 | .lh, |
| 5450 | 5444 | 4 => if (float_class) .flw else .lw, |
| ... | ... | @@ -5452,7 +5446,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5452 | 5446 | else => return std.debug.panic("TODO: genSetReg for size {d}", .{abi_size}), |
| 5453 | 5447 | }; |
| 5454 | 5448 | |
| 5455 | | _ = try self.addInst(.{ |
| 5449 | _ = try func.addInst(.{ |
| 5456 | 5450 | .tag = load_tag, |
| 5457 | 5451 | .ops = .rri, |
| 5458 | 5452 | .data = .{ .i_type = .{ |
| ... | ... | @@ -5465,12 +5459,12 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5465 | 5459 | .lea_symbol => |sym_off| { |
| 5466 | 5460 | assert(sym_off.off == 0); |
| 5467 | 5461 | |
| 5468 | | const atom_index = try self.symbolIndex(); |
| 5462 | const atom_index = try func.symbolIndex(); |
| 5469 | 5463 | |
| 5470 | | _ = try self.addInst(.{ |
| 5464 | _ = try func.addInst(.{ |
| 5471 | 5465 | .tag = .pseudo, |
| 5472 | 5466 | .ops = .pseudo_load_symbol, |
| 5473 | | .data = .{ .payload = try self.addExtra(Mir.LoadSymbolPayload{ |
| 5467 | .data = .{ .payload = try func.addExtra(Mir.LoadSymbolPayload{ |
| 5474 | 5468 | .register = reg.encodeId(), |
| 5475 | 5469 | .atom_index = atom_index, |
| 5476 | 5470 | .sym_index = sym_off.sym, |
| ... | ... | @@ -5478,25 +5472,25 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, src_mcv: MCValue) InnerError! |
| 5478 | 5472 | }); |
| 5479 | 5473 | }, |
| 5480 | 5474 | .load_symbol => { |
| 5481 | | const addr_reg, const addr_lock = try self.allocReg(.int); |
| 5482 | | defer self.register_manager.unlockReg(addr_lock); |
| 5475 | const addr_reg, const addr_lock = try func.allocReg(.int); |
| 5476 | defer func.register_manager.unlockReg(addr_lock); |
| 5483 | 5477 | |
| 5484 | | try self.genSetReg(ty, addr_reg, src_mcv.address()); |
| 5485 | | try self.genSetReg(ty, reg, .{ .indirect = .{ .reg = addr_reg } }); |
| 5478 | try func.genSetReg(ty, addr_reg, src_mcv.address()); |
| 5479 | try func.genSetReg(ty, reg, .{ .indirect = .{ .reg = addr_reg } }); |
| 5486 | 5480 | }, |
| 5487 | | .air_ref => |ref| try self.genSetReg(ty, reg, try self.resolveInst(ref)), |
| 5488 | | else => return self.fail("TODO: genSetReg {s}", .{@tagName(src_mcv)}), |
| 5481 | .air_ref => |ref| try func.genSetReg(ty, reg, try func.resolveInst(ref)), |
| 5482 | else => return func.fail("TODO: genSetReg {s}", .{@tagName(src_mcv)}), |
| 5489 | 5483 | } |
| 5490 | 5484 | } |
| 5491 | 5485 | |
| 5492 | 5486 | fn genSetMem( |
| 5493 | | self: *Self, |
| 5487 | func: *Func, |
| 5494 | 5488 | base: Memory.Base, |
| 5495 | 5489 | disp: i32, |
| 5496 | 5490 | ty: Type, |
| 5497 | 5491 | src_mcv: MCValue, |
| 5498 | 5492 | ) InnerError!void { |
| 5499 | | const mod = self.bin_file.comp.module.?; |
| 5493 | const mod = func.bin_file.comp.module.?; |
| 5500 | 5494 | const abi_size: u32 = @intCast(ty.abiSize(mod)); |
| 5501 | 5495 | const dst_ptr_mcv: MCValue = switch (base) { |
| 5502 | 5496 | .reg => |base_reg| .{ .register_offset = .{ .reg = base_reg, .off = disp } }, |
| ... | ... | @@ -5509,7 +5503,7 @@ fn genSetMem( |
| 5509 | 5503 | .dead, |
| 5510 | 5504 | .reserved_frame, |
| 5511 | 5505 | => unreachable, |
| 5512 | | .undef => try self.genInlineMemset( |
| 5506 | .undef => try func.genInlineMemset( |
| 5513 | 5507 | dst_ptr_mcv, |
| 5514 | 5508 | src_mcv, |
| 5515 | 5509 | .{ .immediate = abi_size }, |
| ... | ... | @@ -5525,13 +5519,13 @@ fn genSetMem( |
| 5525 | 5519 | 0 => {}, |
| 5526 | 5520 | 1, 2, 4, 8 => { |
| 5527 | 5521 | // no matter what type, it should use an integer register |
| 5528 | | const src_reg = try self.copyToTmpRegister(Type.usize, src_mcv); |
| 5529 | | const src_lock = self.register_manager.lockRegAssumeUnused(src_reg); |
| 5530 | | defer self.register_manager.unlockReg(src_lock); |
| 5522 | const src_reg = try func.copyToTmpRegister(Type.usize, src_mcv); |
| 5523 | const src_lock = func.register_manager.lockRegAssumeUnused(src_reg); |
| 5524 | defer func.register_manager.unlockReg(src_lock); |
| 5531 | 5525 | |
| 5532 | | try self.genSetMem(base, disp, ty, .{ .register = src_reg }); |
| 5526 | try func.genSetMem(base, disp, ty, .{ .register = src_reg }); |
| 5533 | 5527 | }, |
| 5534 | | else => try self.genInlineMemcpy( |
| 5528 | else => try func.genInlineMemcpy( |
| 5535 | 5529 | dst_ptr_mcv, |
| 5536 | 5530 | src_mcv.address(), |
| 5537 | 5531 | .{ .immediate = abi_size }, |
| ... | ... | @@ -5541,8 +5535,8 @@ fn genSetMem( |
| 5541 | 5535 | const mem_size = switch (base) { |
| 5542 | 5536 | .frame => |base_fi| mem_size: { |
| 5543 | 5537 | assert(disp >= 0); |
| 5544 | | const frame_abi_size = self.frame_allocs.items(.abi_size)[@intFromEnum(base_fi)]; |
| 5545 | | const frame_spill_pad = self.frame_allocs.items(.spill_pad)[@intFromEnum(base_fi)]; |
| 5538 | const frame_abi_size = func.frame_allocs.items(.abi_size)[@intFromEnum(base_fi)]; |
| 5539 | const frame_spill_pad = func.frame_allocs.items(.spill_pad)[@intFromEnum(base_fi)]; |
| 5546 | 5540 | assert(frame_abi_size - frame_spill_pad - disp >= abi_size); |
| 5547 | 5541 | break :mem_size if (frame_abi_size - frame_spill_pad - disp == abi_size) |
| 5548 | 5542 | frame_abi_size |
| ... | ... | @@ -5554,12 +5548,12 @@ fn genSetMem( |
| 5554 | 5548 | const src_size = math.ceilPowerOfTwoAssert(u32, abi_size); |
| 5555 | 5549 | const src_align = Alignment.fromNonzeroByteUnits(math.ceilPowerOfTwoAssert(u32, src_size)); |
| 5556 | 5550 | if (src_size > mem_size) { |
| 5557 | | const frame_index = try self.allocFrameIndex(FrameAlloc.init(.{ |
| 5551 | const frame_index = try func.allocFrameIndex(FrameAlloc.init(.{ |
| 5558 | 5552 | .size = src_size, |
| 5559 | 5553 | .alignment = src_align, |
| 5560 | 5554 | })); |
| 5561 | 5555 | const frame_mcv: MCValue = .{ .load_frame = .{ .index = frame_index } }; |
| 5562 | | _ = try self.addInst(.{ |
| 5556 | _ = try func.addInst(.{ |
| 5563 | 5557 | .tag = .pseudo, |
| 5564 | 5558 | .ops = .pseudo_store_rm, |
| 5565 | 5559 | .data = .{ .rm = .{ |
| ... | ... | @@ -5572,9 +5566,9 @@ fn genSetMem( |
| 5572 | 5566 | }, |
| 5573 | 5567 | } }, |
| 5574 | 5568 | }); |
| 5575 | | try self.genSetMem(base, disp, ty, frame_mcv); |
| 5576 | | try self.freeValue(frame_mcv); |
| 5577 | | } else _ = try self.addInst(.{ |
| 5569 | try func.genSetMem(base, disp, ty, frame_mcv); |
| 5570 | try func.freeValue(frame_mcv); |
| 5571 | } else _ = try func.addInst(.{ |
| 5578 | 5572 | .tag = .pseudo, |
| 5579 | 5573 | .ops = .pseudo_store_rm, |
| 5580 | 5574 | .data = .{ .rm = .{ |
| ... | ... | @@ -5582,7 +5576,7 @@ fn genSetMem( |
| 5582 | 5576 | .m = .{ |
| 5583 | 5577 | .base = base, |
| 5584 | 5578 | .mod = .{ .rm = .{ |
| 5585 | | .size = self.memSize(ty), |
| 5579 | .size = func.memSize(ty), |
| 5586 | 5580 | .disp = disp, |
| 5587 | 5581 | } }, |
| 5588 | 5582 | }, |
| ... | ... | @@ -5591,54 +5585,54 @@ fn genSetMem( |
| 5591 | 5585 | }, |
| 5592 | 5586 | .register_pair => |src_regs| { |
| 5593 | 5587 | var part_disp: i32 = disp; |
| 5594 | | for (try self.splitType(ty), src_regs) |src_ty, src_reg| { |
| 5595 | | try self.genSetMem(base, part_disp, src_ty, .{ .register = src_reg }); |
| 5588 | for (try func.splitType(ty), src_regs) |src_ty, src_reg| { |
| 5589 | try func.genSetMem(base, part_disp, src_ty, .{ .register = src_reg }); |
| 5596 | 5590 | part_disp += @intCast(src_ty.abiSize(mod)); |
| 5597 | 5591 | } |
| 5598 | 5592 | }, |
| 5599 | 5593 | .immediate => { |
| 5600 | 5594 | // TODO: remove this lock in favor of a copyToTmpRegister when we load 64 bit immediates with |
| 5601 | 5595 | // a register allocation. |
| 5602 | | const reg, const reg_lock = try self.promoteReg(ty, src_mcv, .{}); |
| 5603 | | defer if (reg_lock) |lock| self.register_manager.unlockReg(lock); |
| 5596 | const reg, const reg_lock = try func.promoteReg(ty, src_mcv, .{}); |
| 5597 | defer if (reg_lock) |lock| func.register_manager.unlockReg(lock); |
| 5604 | 5598 | |
| 5605 | | return self.genSetMem(base, disp, ty, .{ .register = reg }); |
| 5599 | return func.genSetMem(base, disp, ty, .{ .register = reg }); |
| 5606 | 5600 | }, |
| 5607 | | .air_ref => |src_ref| try self.genSetMem(base, disp, ty, try self.resolveInst(src_ref)), |
| 5601 | .air_ref => |src_ref| try func.genSetMem(base, disp, ty, try func.resolveInst(src_ref)), |
| 5608 | 5602 | } |
| 5609 | 5603 | } |
| 5610 | 5604 | |
| 5611 | | fn airIntFromPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5612 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5605 | fn airIntFromPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 5606 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5613 | 5607 | const result = result: { |
| 5614 | | const src_mcv = try self.resolveInst(un_op); |
| 5615 | | if (self.reuseOperand(inst, un_op, 0, src_mcv)) break :result src_mcv; |
| 5608 | const src_mcv = try func.resolveInst(un_op); |
| 5609 | if (func.reuseOperand(inst, un_op, 0, src_mcv)) break :result src_mcv; |
| 5616 | 5610 | |
| 5617 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 5618 | | const dst_ty = self.typeOfIndex(inst); |
| 5619 | | try self.genCopy(dst_ty, dst_mcv, src_mcv); |
| 5611 | const dst_mcv = try func.allocRegOrMem(inst, true); |
| 5612 | const dst_ty = func.typeOfIndex(inst); |
| 5613 | try func.genCopy(dst_ty, dst_mcv, src_mcv); |
| 5620 | 5614 | break :result dst_mcv; |
| 5621 | 5615 | }; |
| 5622 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 5616 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 5623 | 5617 | } |
| 5624 | 5618 | |
| 5625 | | fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { |
| 5626 | | const zcu = self.bin_file.comp.module.?; |
| 5619 | fn airBitCast(func: *Func, inst: Air.Inst.Index) !void { |
| 5620 | const zcu = func.bin_file.comp.module.?; |
| 5627 | 5621 | |
| 5628 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5629 | | const result = if (self.liveness.isUnused(inst)) .unreach else result: { |
| 5630 | | const src_mcv = try self.resolveInst(ty_op.operand); |
| 5622 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5623 | const result = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 5624 | const src_mcv = try func.resolveInst(ty_op.operand); |
| 5631 | 5625 | |
| 5632 | | const dst_ty = self.typeOfIndex(inst); |
| 5633 | | const src_ty = self.typeOf(ty_op.operand); |
| 5626 | const dst_ty = func.typeOfIndex(inst); |
| 5627 | const src_ty = func.typeOf(ty_op.operand); |
| 5634 | 5628 | |
| 5635 | | const src_lock = if (src_mcv.getReg()) |reg| self.register_manager.lockReg(reg) else null; |
| 5636 | | defer if (src_lock) |lock| self.register_manager.unlockReg(lock); |
| 5629 | const src_lock = if (src_mcv.getReg()) |reg| func.register_manager.lockReg(reg) else null; |
| 5630 | defer if (src_lock) |lock| func.register_manager.unlockReg(lock); |
| 5637 | 5631 | |
| 5638 | 5632 | const dst_mcv = if (dst_ty.abiSize(zcu) <= src_ty.abiSize(zcu) and |
| 5639 | | self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: { |
| 5640 | | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 5641 | | try self.genCopy(switch (math.order(dst_ty.abiSize(zcu), src_ty.abiSize(zcu))) { |
| 5633 | func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: { |
| 5634 | const dst_mcv = try func.allocRegOrMem(inst, true); |
| 5635 | try func.genCopy(switch (math.order(dst_ty.abiSize(zcu), src_ty.abiSize(zcu))) { |
| 5642 | 5636 | .lt => dst_ty, |
| 5643 | 5637 | .eq => if (!dst_mcv.isMemory() or src_mcv.isMemory()) dst_ty else src_ty, |
| 5644 | 5638 | .gt => src_ty, |
| ... | ... | @@ -5653,83 +5647,83 @@ fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { |
| 5653 | 5647 | const bit_size = dst_ty.bitSize(zcu); |
| 5654 | 5648 | if (abi_size * 8 <= bit_size) break :result dst_mcv; |
| 5655 | 5649 | |
| 5656 | | return self.fail("TODO: airBitCast {} to {}", .{ src_ty.fmt(zcu), dst_ty.fmt(zcu) }); |
| 5650 | return func.fail("TODO: airBitCast {} to {}", .{ src_ty.fmt(zcu), dst_ty.fmt(zcu) }); |
| 5657 | 5651 | }; |
| 5658 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5652 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5659 | 5653 | } |
| 5660 | 5654 | |
| 5661 | | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 5662 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5663 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airArrayToSlice for {}", .{ |
| 5664 | | self.target.cpu.arch, |
| 5655 | fn airArrayToSlice(func: *Func, inst: Air.Inst.Index) !void { |
| 5656 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5657 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airArrayToSlice for {}", .{ |
| 5658 | func.target.cpu.arch, |
| 5665 | 5659 | }); |
| 5666 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5660 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5667 | 5661 | } |
| 5668 | 5662 | |
| 5669 | | fn airFloatFromInt(self: *Self, inst: Air.Inst.Index) !void { |
| 5670 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5671 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airFloatFromInt for {}", .{ |
| 5672 | | self.target.cpu.arch, |
| 5663 | fn airFloatFromInt(func: *Func, inst: Air.Inst.Index) !void { |
| 5664 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5665 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airFloatFromInt for {}", .{ |
| 5666 | func.target.cpu.arch, |
| 5673 | 5667 | }); |
| 5674 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5668 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5675 | 5669 | } |
| 5676 | 5670 | |
| 5677 | | fn airIntFromFloat(self: *Self, inst: Air.Inst.Index) !void { |
| 5678 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5679 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airIntFromFloat for {}", .{ |
| 5680 | | self.target.cpu.arch, |
| 5671 | fn airIntFromFloat(func: *Func, inst: Air.Inst.Index) !void { |
| 5672 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5673 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airIntFromFloat for {}", .{ |
| 5674 | func.target.cpu.arch, |
| 5681 | 5675 | }); |
| 5682 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5676 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5683 | 5677 | } |
| 5684 | 5678 | |
| 5685 | | fn airCmpxchg(self: *Self, inst: Air.Inst.Index) !void { |
| 5686 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5687 | | const extra = self.air.extraData(Air.Block, ty_pl.payload); |
| 5679 | fn airCmpxchg(func: *Func, inst: Air.Inst.Index) !void { |
| 5680 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5681 | const extra = func.air.extraData(Air.Block, ty_pl.payload); |
| 5688 | 5682 | _ = extra; |
| 5689 | | return self.fail("TODO implement airCmpxchg for {}", .{ |
| 5690 | | self.target.cpu.arch, |
| 5683 | return func.fail("TODO implement airCmpxchg for {}", .{ |
| 5684 | func.target.cpu.arch, |
| 5691 | 5685 | }); |
| 5692 | | // return self.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value }); |
| 5686 | // return func.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value }); |
| 5693 | 5687 | } |
| 5694 | 5688 | |
| 5695 | | fn airAtomicRmw(self: *Self, inst: Air.Inst.Index) !void { |
| 5689 | fn airAtomicRmw(func: *Func, inst: Air.Inst.Index) !void { |
| 5696 | 5690 | _ = inst; |
| 5697 | | return self.fail("TODO implement airCmpxchg for {}", .{self.target.cpu.arch}); |
| 5691 | return func.fail("TODO implement airCmpxchg for {}", .{func.target.cpu.arch}); |
| 5698 | 5692 | } |
| 5699 | 5693 | |
| 5700 | | fn airAtomicLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 5694 | fn airAtomicLoad(func: *Func, inst: Air.Inst.Index) !void { |
| 5701 | 5695 | _ = inst; |
| 5702 | | return self.fail("TODO implement airAtomicLoad for {}", .{self.target.cpu.arch}); |
| 5696 | return func.fail("TODO implement airAtomicLoad for {}", .{func.target.cpu.arch}); |
| 5703 | 5697 | } |
| 5704 | 5698 | |
| 5705 | | fn airAtomicStore(self: *Self, inst: Air.Inst.Index, order: std.builtin.AtomicOrder) !void { |
| 5699 | fn airAtomicStore(func: *Func, inst: Air.Inst.Index, order: std.builtin.AtomicOrder) !void { |
| 5706 | 5700 | _ = inst; |
| 5707 | 5701 | _ = order; |
| 5708 | | return self.fail("TODO implement airAtomicStore for {}", .{self.target.cpu.arch}); |
| 5702 | return func.fail("TODO implement airAtomicStore for {}", .{func.target.cpu.arch}); |
| 5709 | 5703 | } |
| 5710 | 5704 | |
| 5711 | | fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 5712 | | const zcu = self.bin_file.comp.module.?; |
| 5713 | | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5705 | fn airMemset(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 5706 | const zcu = func.bin_file.comp.module.?; |
| 5707 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5714 | 5708 | |
| 5715 | 5709 | result: { |
| 5716 | | if (!safety and (try self.resolveInst(bin_op.rhs)) == .undef) break :result; |
| 5710 | if (!safety and (try func.resolveInst(bin_op.rhs)) == .undef) break :result; |
| 5717 | 5711 | |
| 5718 | | const dst_ptr = try self.resolveInst(bin_op.lhs); |
| 5719 | | const dst_ptr_ty = self.typeOf(bin_op.lhs); |
| 5712 | const dst_ptr = try func.resolveInst(bin_op.lhs); |
| 5713 | const dst_ptr_ty = func.typeOf(bin_op.lhs); |
| 5720 | 5714 | const dst_ptr_lock: ?RegisterLock = switch (dst_ptr) { |
| 5721 | | .register => |reg| self.register_manager.lockRegAssumeUnused(reg), |
| 5715 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 5722 | 5716 | else => null, |
| 5723 | 5717 | }; |
| 5724 | | defer if (dst_ptr_lock) |lock| self.register_manager.unlockReg(lock); |
| 5718 | defer if (dst_ptr_lock) |lock| func.register_manager.unlockReg(lock); |
| 5725 | 5719 | |
| 5726 | | const src_val = try self.resolveInst(bin_op.rhs); |
| 5727 | | const elem_ty = self.typeOf(bin_op.rhs); |
| 5720 | const src_val = try func.resolveInst(bin_op.rhs); |
| 5721 | const elem_ty = func.typeOf(bin_op.rhs); |
| 5728 | 5722 | const src_val_lock: ?RegisterLock = switch (src_val) { |
| 5729 | | .register => |reg| self.register_manager.lockRegAssumeUnused(reg), |
| 5723 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 5730 | 5724 | else => null, |
| 5731 | 5725 | }; |
| 5732 | | defer if (src_val_lock) |lock| self.register_manager.unlockReg(lock); |
| 5726 | defer if (src_val_lock) |lock| func.register_manager.unlockReg(lock); |
| 5733 | 5727 | |
| 5734 | 5728 | const elem_abi_size: u31 = @intCast(elem_ty.abiSize(zcu)); |
| 5735 | 5729 | |
| ... | ... | @@ -5747,12 +5741,12 @@ fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 5747 | 5741 | .C, .Many => unreachable, |
| 5748 | 5742 | }; |
| 5749 | 5743 | const len_lock: ?RegisterLock = switch (len) { |
| 5750 | | .register => |reg| self.register_manager.lockRegAssumeUnused(reg), |
| 5744 | .register => |reg| func.register_manager.lockRegAssumeUnused(reg), |
| 5751 | 5745 | else => null, |
| 5752 | 5746 | }; |
| 5753 | | defer if (len_lock) |lock| self.register_manager.unlockReg(lock); |
| 5747 | defer if (len_lock) |lock| func.register_manager.unlockReg(lock); |
| 5754 | 5748 | |
| 5755 | | try self.genInlineMemset(ptr, src_val, len); |
| 5749 | try func.genInlineMemset(ptr, src_val, len); |
| 5756 | 5750 | break :result; |
| 5757 | 5751 | } |
| 5758 | 5752 | |
| ... | ... | @@ -5760,87 +5754,87 @@ fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 5760 | 5754 | // Length zero requires a runtime check - so we handle arrays specially |
| 5761 | 5755 | // here to elide it. |
| 5762 | 5756 | switch (dst_ptr_ty.ptrSize(zcu)) { |
| 5763 | | .Slice => return self.fail("TODO: airMemset Slices", .{}), |
| 5757 | .Slice => return func.fail("TODO: airMemset Slices", .{}), |
| 5764 | 5758 | .One => { |
| 5765 | 5759 | const elem_ptr_ty = try zcu.singleMutPtrType(elem_ty); |
| 5766 | 5760 | |
| 5767 | 5761 | const len = dst_ptr_ty.childType(zcu).arrayLen(zcu); |
| 5768 | 5762 | |
| 5769 | 5763 | assert(len != 0); // prevented by Sema |
| 5770 | | try self.store(dst_ptr, src_val, elem_ptr_ty, elem_ty); |
| 5764 | try func.store(dst_ptr, src_val, elem_ptr_ty, elem_ty); |
| 5771 | 5765 | |
| 5772 | | const second_elem_ptr_reg, const second_elem_ptr_lock = try self.allocReg(.int); |
| 5773 | | defer self.register_manager.unlockReg(second_elem_ptr_lock); |
| 5766 | const second_elem_ptr_reg, const second_elem_ptr_lock = try func.allocReg(.int); |
| 5767 | defer func.register_manager.unlockReg(second_elem_ptr_lock); |
| 5774 | 5768 | |
| 5775 | 5769 | const second_elem_ptr_mcv: MCValue = .{ .register = second_elem_ptr_reg }; |
| 5776 | 5770 | |
| 5777 | | try self.genSetReg(Type.usize, second_elem_ptr_reg, .{ .register_offset = .{ |
| 5778 | | .reg = try self.copyToTmpRegister(Type.usize, dst_ptr), |
| 5771 | try func.genSetReg(Type.usize, second_elem_ptr_reg, .{ .register_offset = .{ |
| 5772 | .reg = try func.copyToTmpRegister(Type.usize, dst_ptr), |
| 5779 | 5773 | .off = elem_abi_size, |
| 5780 | 5774 | } }); |
| 5781 | 5775 | |
| 5782 | 5776 | const bytes_to_copy: MCValue = .{ .immediate = elem_abi_size * (len - 1) }; |
| 5783 | | try self.genInlineMemcpy(second_elem_ptr_mcv, dst_ptr, bytes_to_copy); |
| 5777 | try func.genInlineMemcpy(second_elem_ptr_mcv, dst_ptr, bytes_to_copy); |
| 5784 | 5778 | }, |
| 5785 | 5779 | .C, .Many => unreachable, |
| 5786 | 5780 | } |
| 5787 | 5781 | } |
| 5788 | | return self.finishAir(inst, .unreach, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 5782 | return func.finishAir(inst, .unreach, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 5789 | 5783 | } |
| 5790 | 5784 | |
| 5791 | | fn airMemcpy(self: *Self, inst: Air.Inst.Index) !void { |
| 5785 | fn airMemcpy(func: *Func, inst: Air.Inst.Index) !void { |
| 5792 | 5786 | _ = inst; |
| 5793 | | return self.fail("TODO implement airMemcpy for {}", .{self.target.cpu.arch}); |
| 5787 | return func.fail("TODO implement airMemcpy for {}", .{func.target.cpu.arch}); |
| 5794 | 5788 | } |
| 5795 | 5789 | |
| 5796 | | fn airTagName(self: *Self, inst: Air.Inst.Index) !void { |
| 5797 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5798 | | const operand = try self.resolveInst(un_op); |
| 5799 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else { |
| 5790 | fn airTagName(func: *Func, inst: Air.Inst.Index) !void { |
| 5791 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5792 | const operand = try func.resolveInst(un_op); |
| 5793 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else { |
| 5800 | 5794 | _ = operand; |
| 5801 | | return self.fail("TODO implement airTagName for riscv64", .{}); |
| 5795 | return func.fail("TODO implement airTagName for riscv64", .{}); |
| 5802 | 5796 | }; |
| 5803 | | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 5797 | return func.finishAir(inst, result, .{ un_op, .none, .none }); |
| 5804 | 5798 | } |
| 5805 | 5799 | |
| 5806 | | fn airErrorName(self: *Self, inst: Air.Inst.Index) !void { |
| 5807 | | const zcu = self.bin_file.comp.module.?; |
| 5808 | | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5800 | fn airErrorName(func: *Func, inst: Air.Inst.Index) !void { |
| 5801 | const zcu = func.bin_file.comp.module.?; |
| 5802 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5809 | 5803 | |
| 5810 | | const err_ty = self.typeOf(un_op); |
| 5811 | | const err_mcv = try self.resolveInst(un_op); |
| 5804 | const err_ty = func.typeOf(un_op); |
| 5805 | const err_mcv = try func.resolveInst(un_op); |
| 5812 | 5806 | |
| 5813 | | const err_reg = try self.copyToTmpRegister(err_ty, err_mcv); |
| 5814 | | const err_lock = self.register_manager.lockRegAssumeUnused(err_reg); |
| 5815 | | defer self.register_manager.unlockReg(err_lock); |
| 5807 | const err_reg = try func.copyToTmpRegister(err_ty, err_mcv); |
| 5808 | const err_lock = func.register_manager.lockRegAssumeUnused(err_reg); |
| 5809 | defer func.register_manager.unlockReg(err_lock); |
| 5816 | 5810 | |
| 5817 | | const addr_reg, const addr_lock = try self.allocReg(.int); |
| 5818 | | defer self.register_manager.unlockReg(addr_lock); |
| 5811 | const addr_reg, const addr_lock = try func.allocReg(.int); |
| 5812 | defer func.register_manager.unlockReg(addr_lock); |
| 5819 | 5813 | |
| 5820 | 5814 | // this is now the base address of the error name table |
| 5821 | 5815 | const lazy_sym = link.File.LazySymbol.initDecl(.const_data, null, zcu); |
| 5822 | | if (self.bin_file.cast(link.File.Elf)) |elf_file| { |
| 5816 | if (func.bin_file.cast(link.File.Elf)) |elf_file| { |
| 5823 | 5817 | const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err| |
| 5824 | | return self.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 5818 | return func.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 5825 | 5819 | const sym = elf_file.symbol(sym_index); |
| 5826 | | try self.genSetReg(Type.usize, addr_reg, .{ .load_symbol = .{ .sym = sym.esym_index } }); |
| 5820 | try func.genSetReg(Type.usize, addr_reg, .{ .load_symbol = .{ .sym = sym.esym_index } }); |
| 5827 | 5821 | } else { |
| 5828 | | return self.fail("TODO: riscv non-elf", .{}); |
| 5822 | return func.fail("TODO: riscv non-elf", .{}); |
| 5829 | 5823 | } |
| 5830 | 5824 | |
| 5831 | | const start_reg, const start_lock = try self.allocReg(.int); |
| 5832 | | defer self.register_manager.unlockReg(start_lock); |
| 5825 | const start_reg, const start_lock = try func.allocReg(.int); |
| 5826 | defer func.register_manager.unlockReg(start_lock); |
| 5833 | 5827 | |
| 5834 | | const end_reg, const end_lock = try self.allocReg(.int); |
| 5835 | | defer self.register_manager.unlockReg(end_lock); |
| 5828 | const end_reg, const end_lock = try func.allocReg(.int); |
| 5829 | defer func.register_manager.unlockReg(end_lock); |
| 5836 | 5830 | |
| 5837 | | // const tmp_reg, const tmp_lock = try self.allocReg(.int); |
| 5838 | | // defer self.register_manager.unlockReg(tmp_lock); |
| 5831 | // const tmp_reg, const tmp_lock = try func.allocReg(.int); |
| 5832 | // defer func.register_manager.unlockReg(tmp_lock); |
| 5839 | 5833 | |
| 5840 | 5834 | // we move the base address forward by the following formula: base + (errno * 8) |
| 5841 | 5835 | |
| 5842 | 5836 | // shifting left by 4 is the same as multiplying by 8 |
| 5843 | | _ = try self.addInst(.{ |
| 5837 | _ = try func.addInst(.{ |
| 5844 | 5838 | .tag = .slli, |
| 5845 | 5839 | .ops = .rri, |
| 5846 | 5840 | .data = .{ .i_type = .{ |
| ... | ... | @@ -5850,7 +5844,7 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void { |
| 5850 | 5844 | } }, |
| 5851 | 5845 | }); |
| 5852 | 5846 | |
| 5853 | | _ = try self.addInst(.{ |
| 5847 | _ = try func.addInst(.{ |
| 5854 | 5848 | .tag = .add, |
| 5855 | 5849 | .ops = .rrr, |
| 5856 | 5850 | .data = .{ .r_type = .{ |
| ... | ... | @@ -5860,7 +5854,7 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void { |
| 5860 | 5854 | } }, |
| 5861 | 5855 | }); |
| 5862 | 5856 | |
| 5863 | | _ = try self.addInst(.{ |
| 5857 | _ = try func.addInst(.{ |
| 5864 | 5858 | .tag = .pseudo, |
| 5865 | 5859 | .ops = .pseudo_load_rm, |
| 5866 | 5860 | .data = .{ |
| ... | ... | @@ -5874,7 +5868,7 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void { |
| 5874 | 5868 | }, |
| 5875 | 5869 | }); |
| 5876 | 5870 | |
| 5877 | | _ = try self.addInst(.{ |
| 5871 | _ = try func.addInst(.{ |
| 5878 | 5872 | .tag = .pseudo, |
| 5879 | 5873 | .ops = .pseudo_load_rm, |
| 5880 | 5874 | .data = .{ |
| ... | ... | @@ -5888,64 +5882,64 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void { |
| 5888 | 5882 | }, |
| 5889 | 5883 | }); |
| 5890 | 5884 | |
| 5891 | | const dst_mcv = try self.allocRegOrMem(inst, false); |
| 5885 | const dst_mcv = try func.allocRegOrMem(inst, false); |
| 5892 | 5886 | const frame = dst_mcv.load_frame; |
| 5893 | | try self.genSetMem( |
| 5887 | try func.genSetMem( |
| 5894 | 5888 | .{ .frame = frame.index }, |
| 5895 | 5889 | frame.off, |
| 5896 | 5890 | Type.usize, |
| 5897 | 5891 | .{ .register = start_reg }, |
| 5898 | 5892 | ); |
| 5899 | 5893 | |
| 5900 | | try self.genSetMem( |
| 5894 | try func.genSetMem( |
| 5901 | 5895 | .{ .frame = frame.index }, |
| 5902 | 5896 | frame.off + 8, |
| 5903 | 5897 | Type.usize, |
| 5904 | 5898 | .{ .register = end_reg }, |
| 5905 | 5899 | ); |
| 5906 | 5900 | |
| 5907 | | return self.finishAir(inst, dst_mcv, .{ un_op, .none, .none }); |
| 5901 | return func.finishAir(inst, dst_mcv, .{ un_op, .none, .none }); |
| 5908 | 5902 | } |
| 5909 | 5903 | |
| 5910 | | fn airSplat(self: *Self, inst: Air.Inst.Index) !void { |
| 5911 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5912 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airSplat for riscv64", .{}); |
| 5913 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5904 | fn airSplat(func: *Func, inst: Air.Inst.Index) !void { |
| 5905 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5906 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airSplat for riscv64", .{}); |
| 5907 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5914 | 5908 | } |
| 5915 | 5909 | |
| 5916 | | fn airSelect(self: *Self, inst: Air.Inst.Index) !void { |
| 5917 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 5918 | | const extra = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 5919 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airSelect for riscv64", .{}); |
| 5920 | | return self.finishAir(inst, result, .{ pl_op.operand, extra.lhs, extra.rhs }); |
| 5910 | fn airSelect(func: *Func, inst: Air.Inst.Index) !void { |
| 5911 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 5912 | const extra = func.air.extraData(Air.Bin, pl_op.payload).data; |
| 5913 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airSelect for riscv64", .{}); |
| 5914 | return func.finishAir(inst, result, .{ pl_op.operand, extra.lhs, extra.rhs }); |
| 5921 | 5915 | } |
| 5922 | 5916 | |
| 5923 | | fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 5924 | | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5925 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airShuffle for riscv64", .{}); |
| 5926 | | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5917 | fn airShuffle(func: *Func, inst: Air.Inst.Index) !void { |
| 5918 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5919 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airShuffle for riscv64", .{}); |
| 5920 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5927 | 5921 | } |
| 5928 | 5922 | |
| 5929 | | fn airReduce(self: *Self, inst: Air.Inst.Index) !void { |
| 5930 | | const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 5931 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else return self.fail("TODO implement airReduce for riscv64", .{}); |
| 5932 | | return self.finishAir(inst, result, .{ reduce.operand, .none, .none }); |
| 5923 | fn airReduce(func: *Func, inst: Air.Inst.Index) !void { |
| 5924 | const reduce = func.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 5925 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else return func.fail("TODO implement airReduce for riscv64", .{}); |
| 5926 | return func.finishAir(inst, result, .{ reduce.operand, .none, .none }); |
| 5933 | 5927 | } |
| 5934 | 5928 | |
| 5935 | | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 5936 | | const zcu = self.bin_file.comp.module.?; |
| 5937 | | const result_ty = self.typeOfIndex(inst); |
| 5929 | fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void { |
| 5930 | const zcu = func.bin_file.comp.module.?; |
| 5931 | const result_ty = func.typeOfIndex(inst); |
| 5938 | 5932 | const len: usize = @intCast(result_ty.arrayLen(zcu)); |
| 5939 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5940 | | const elements: []const Air.Inst.Ref = @ptrCast(self.air.extra[ty_pl.payload..][0..len]); |
| 5933 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5934 | const elements: []const Air.Inst.Ref = @ptrCast(func.air.extra[ty_pl.payload..][0..len]); |
| 5941 | 5935 | |
| 5942 | 5936 | const result: MCValue = result: { |
| 5943 | 5937 | switch (result_ty.zigTypeTag(zcu)) { |
| 5944 | 5938 | .Struct => { |
| 5945 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu)); |
| 5939 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu)); |
| 5946 | 5940 | if (result_ty.containerLayout(zcu) == .@"packed") { |
| 5947 | 5941 | const struct_obj = zcu.typeToStruct(result_ty).?; |
| 5948 | | try self.genInlineMemset( |
| 5942 | try func.genInlineMemset( |
| 5949 | 5943 | .{ .lea_frame = .{ .index = frame_index } }, |
| 5950 | 5944 | .{ .immediate = 0 }, |
| 5951 | 5945 | .{ .immediate = result_ty.abiSize(zcu) }, |
| ... | ... | @@ -5958,7 +5952,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 5958 | 5952 | const elem_ty = result_ty.structFieldType(elem_i, zcu); |
| 5959 | 5953 | const elem_bit_size: u32 = @intCast(elem_ty.bitSize(zcu)); |
| 5960 | 5954 | if (elem_bit_size > 64) { |
| 5961 | | return self.fail( |
| 5955 | return func.fail( |
| 5962 | 5956 | "TODO airAggregateInit implement packed structs with large fields", |
| 5963 | 5957 | .{}, |
| 5964 | 5958 | ); |
| ... | ... | @@ -5969,109 +5963,109 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 5969 | 5963 | const elem_off = zcu.structPackedFieldBitOffset(struct_obj, elem_i); |
| 5970 | 5964 | const elem_byte_off: i32 = @intCast(elem_off / elem_abi_bits * elem_abi_size); |
| 5971 | 5965 | const elem_bit_off = elem_off % elem_abi_bits; |
| 5972 | | const elem_mcv = try self.resolveInst(elem); |
| 5966 | const elem_mcv = try func.resolveInst(elem); |
| 5973 | 5967 | |
| 5974 | 5968 | _ = elem_byte_off; |
| 5975 | 5969 | _ = elem_bit_off; |
| 5976 | 5970 | |
| 5977 | 5971 | const elem_lock = switch (elem_mcv) { |
| 5978 | | .register => |reg| self.register_manager.lockReg(reg), |
| 5972 | .register => |reg| func.register_manager.lockReg(reg), |
| 5979 | 5973 | .immediate => |imm| lock: { |
| 5980 | 5974 | if (imm == 0) continue; |
| 5981 | 5975 | break :lock null; |
| 5982 | 5976 | }, |
| 5983 | 5977 | else => null, |
| 5984 | 5978 | }; |
| 5985 | | defer if (elem_lock) |lock| self.register_manager.unlockReg(lock); |
| 5979 | defer if (elem_lock) |lock| func.register_manager.unlockReg(lock); |
| 5986 | 5980 | |
| 5987 | | return self.fail("TODO: airAggregateInit packed structs", .{}); |
| 5981 | return func.fail("TODO: airAggregateInit packed structs", .{}); |
| 5988 | 5982 | } |
| 5989 | 5983 | } else for (elements, 0..) |elem, elem_i| { |
| 5990 | 5984 | if ((try result_ty.structFieldValueComptime(zcu, elem_i)) != null) continue; |
| 5991 | 5985 | |
| 5992 | 5986 | const elem_ty = result_ty.structFieldType(elem_i, zcu); |
| 5993 | 5987 | const elem_off: i32 = @intCast(result_ty.structFieldOffset(elem_i, zcu)); |
| 5994 | | const elem_mcv = try self.resolveInst(elem); |
| 5995 | | try self.genSetMem(.{ .frame = frame_index }, elem_off, elem_ty, elem_mcv); |
| 5988 | const elem_mcv = try func.resolveInst(elem); |
| 5989 | try func.genSetMem(.{ .frame = frame_index }, elem_off, elem_ty, elem_mcv); |
| 5996 | 5990 | } |
| 5997 | 5991 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 5998 | 5992 | }, |
| 5999 | 5993 | .Array => { |
| 6000 | 5994 | const elem_ty = result_ty.childType(zcu); |
| 6001 | | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu)); |
| 5995 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu)); |
| 6002 | 5996 | const elem_size: u32 = @intCast(elem_ty.abiSize(zcu)); |
| 6003 | 5997 | |
| 6004 | 5998 | for (elements, 0..) |elem, elem_i| { |
| 6005 | | const elem_mcv = try self.resolveInst(elem); |
| 5999 | const elem_mcv = try func.resolveInst(elem); |
| 6006 | 6000 | const elem_off: i32 = @intCast(elem_size * elem_i); |
| 6007 | | try self.genSetMem( |
| 6001 | try func.genSetMem( |
| 6008 | 6002 | .{ .frame = frame_index }, |
| 6009 | 6003 | elem_off, |
| 6010 | 6004 | elem_ty, |
| 6011 | 6005 | elem_mcv, |
| 6012 | 6006 | ); |
| 6013 | 6007 | } |
| 6014 | | if (result_ty.sentinel(zcu)) |sentinel| try self.genSetMem( |
| 6008 | if (result_ty.sentinel(zcu)) |sentinel| try func.genSetMem( |
| 6015 | 6009 | .{ .frame = frame_index }, |
| 6016 | 6010 | @intCast(elem_size * elements.len), |
| 6017 | 6011 | elem_ty, |
| 6018 | | try self.genTypedValue(sentinel), |
| 6012 | try func.genTypedValue(sentinel), |
| 6019 | 6013 | ); |
| 6020 | 6014 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 6021 | 6015 | }, |
| 6022 | | else => return self.fail("TODO: airAggregate {}", .{result_ty.fmt(zcu)}), |
| 6016 | else => return func.fail("TODO: airAggregate {}", .{result_ty.fmt(zcu)}), |
| 6023 | 6017 | } |
| 6024 | 6018 | }; |
| 6025 | 6019 | |
| 6026 | 6020 | if (elements.len <= Liveness.bpi - 1) { |
| 6027 | 6021 | var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); |
| 6028 | 6022 | @memcpy(buf[0..elements.len], elements); |
| 6029 | | return self.finishAir(inst, result, buf); |
| 6023 | return func.finishAir(inst, result, buf); |
| 6030 | 6024 | } |
| 6031 | | var bt = self.liveness.iterateBigTomb(inst); |
| 6032 | | for (elements) |elem| try self.feed(&bt, elem); |
| 6033 | | return self.finishAirResult(inst, result); |
| 6025 | var bt = func.liveness.iterateBigTomb(inst); |
| 6026 | for (elements) |elem| try func.feed(&bt, elem); |
| 6027 | return func.finishAirResult(inst, result); |
| 6034 | 6028 | } |
| 6035 | 6029 | |
| 6036 | | fn airUnionInit(self: *Self, inst: Air.Inst.Index) !void { |
| 6037 | | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6038 | | const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 6030 | fn airUnionInit(func: *Func, inst: Air.Inst.Index) !void { |
| 6031 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6032 | const extra = func.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 6039 | 6033 | _ = extra; |
| 6040 | | return self.fail("TODO implement airUnionInit for riscv64", .{}); |
| 6041 | | // return self.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value }); |
| 6034 | return func.fail("TODO implement airUnionInit for riscv64", .{}); |
| 6035 | // return func.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value }); |
| 6042 | 6036 | } |
| 6043 | 6037 | |
| 6044 | | fn airPrefetch(self: *Self, inst: Air.Inst.Index) !void { |
| 6045 | | const prefetch = self.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; |
| 6038 | fn airPrefetch(func: *Func, inst: Air.Inst.Index) !void { |
| 6039 | const prefetch = func.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; |
| 6046 | 6040 | // TODO: RISC-V does have prefetch instruction variants. |
| 6047 | 6041 | // see here: https://raw.githubusercontent.com/riscv/riscv-CMOs/master/specifications/cmobase-v1.0.1.pdf |
| 6048 | | return self.finishAir(inst, .unreach, .{ prefetch.ptr, .none, .none }); |
| 6042 | return func.finishAir(inst, .unreach, .{ prefetch.ptr, .none, .none }); |
| 6049 | 6043 | } |
| 6050 | 6044 | |
| 6051 | | fn airMulAdd(self: *Self, inst: Air.Inst.Index) !void { |
| 6052 | | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6053 | | const extra = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 6054 | | const result: MCValue = if (self.liveness.isUnused(inst)) .unreach else { |
| 6055 | | return self.fail("TODO implement airMulAdd for riscv64", .{}); |
| 6045 | fn airMulAdd(func: *Func, inst: Air.Inst.Index) !void { |
| 6046 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6047 | const extra = func.air.extraData(Air.Bin, pl_op.payload).data; |
| 6048 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else { |
| 6049 | return func.fail("TODO implement airMulAdd for riscv64", .{}); |
| 6056 | 6050 | }; |
| 6057 | | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, pl_op.operand }); |
| 6051 | return func.finishAir(inst, result, .{ extra.lhs, extra.rhs, pl_op.operand }); |
| 6058 | 6052 | } |
| 6059 | 6053 | |
| 6060 | | fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue { |
| 6061 | | const zcu = self.bin_file.comp.module.?; |
| 6054 | fn resolveInst(func: *Func, ref: Air.Inst.Ref) InnerError!MCValue { |
| 6055 | const zcu = func.bin_file.comp.module.?; |
| 6062 | 6056 | |
| 6063 | 6057 | // If the type has no codegen bits, no need to store it. |
| 6064 | | const inst_ty = self.typeOf(ref); |
| 6058 | const inst_ty = func.typeOf(ref); |
| 6065 | 6059 | if (!inst_ty.hasRuntimeBits(zcu)) |
| 6066 | 6060 | return .none; |
| 6067 | 6061 | |
| 6068 | 6062 | const mcv = if (ref.toIndex()) |inst| mcv: { |
| 6069 | | break :mcv self.inst_tracking.getPtr(inst).?.short; |
| 6063 | break :mcv func.inst_tracking.getPtr(inst).?.short; |
| 6070 | 6064 | } else mcv: { |
| 6071 | 6065 | const ip_index = ref.toInterned().?; |
| 6072 | | const gop = try self.const_tracking.getOrPut(self.gpa, ip_index); |
| 6066 | const gop = try func.const_tracking.getOrPut(func.gpa, ip_index); |
| 6073 | 6067 | if (!gop.found_existing) gop.value_ptr.* = InstTracking.init( |
| 6074 | | try self.genTypedValue(Value.fromInterned(ip_index)), |
| 6068 | try func.genTypedValue(Value.fromInterned(ip_index)), |
| 6075 | 6069 | ); |
| 6076 | 6070 | break :mcv gop.value_ptr.short; |
| 6077 | 6071 | }; |
| ... | ... | @@ -6079,21 +6073,21 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue { |
| 6079 | 6073 | return mcv; |
| 6080 | 6074 | } |
| 6081 | 6075 | |
| 6082 | | fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) *InstTracking { |
| 6083 | | const tracking = self.inst_tracking.getPtr(inst).?; |
| 6076 | fn getResolvedInstValue(func: *Func, inst: Air.Inst.Index) *InstTracking { |
| 6077 | const tracking = func.inst_tracking.getPtr(inst).?; |
| 6084 | 6078 | return switch (tracking.short) { |
| 6085 | 6079 | .none, .unreach, .dead => unreachable, |
| 6086 | 6080 | else => tracking, |
| 6087 | 6081 | }; |
| 6088 | 6082 | } |
| 6089 | 6083 | |
| 6090 | | fn genTypedValue(self: *Self, val: Value) InnerError!MCValue { |
| 6091 | | const zcu = self.bin_file.comp.module.?; |
| 6084 | fn genTypedValue(func: *Func, val: Value) InnerError!MCValue { |
| 6085 | const zcu = func.bin_file.comp.module.?; |
| 6092 | 6086 | const result = try codegen.genTypedValue( |
| 6093 | | self.bin_file, |
| 6094 | | self.src_loc, |
| 6087 | func.bin_file, |
| 6088 | func.src_loc, |
| 6095 | 6089 | val, |
| 6096 | | zcu.funcOwnerDeclIndex(self.func_index), |
| 6090 | zcu.funcOwnerDeclIndex(func.func_index), |
| 6097 | 6091 | ); |
| 6098 | 6092 | const mcv: MCValue = switch (result) { |
| 6099 | 6093 | .mcv => |mcv| switch (mcv) { |
| ... | ... | @@ -6103,11 +6097,11 @@ fn genTypedValue(self: *Self, val: Value) InnerError!MCValue { |
| 6103 | 6097 | .immediate => |imm| .{ .immediate = imm }, |
| 6104 | 6098 | .memory => |addr| .{ .memory = addr }, |
| 6105 | 6099 | .load_got, .load_direct, .load_tlv => { |
| 6106 | | return self.fail("TODO: genTypedValue {s}", .{@tagName(mcv)}); |
| 6100 | return func.fail("TODO: genTypedValue {s}", .{@tagName(mcv)}); |
| 6107 | 6101 | }, |
| 6108 | 6102 | }, |
| 6109 | 6103 | .fail => |msg| { |
| 6110 | | self.err_msg = msg; |
| 6104 | func.err_msg = msg; |
| 6111 | 6105 | return error.CodegenFail; |
| 6112 | 6106 | }, |
| 6113 | 6107 | }; |
| ... | ... | @@ -6120,39 +6114,39 @@ const CallMCValues = struct { |
| 6120 | 6114 | stack_byte_count: u31, |
| 6121 | 6115 | stack_align: Alignment, |
| 6122 | 6116 | |
| 6123 | | fn deinit(self: *CallMCValues, func: *Self) void { |
| 6124 | | func.gpa.free(self.args); |
| 6125 | | self.* = undefined; |
| 6117 | fn deinit(call: *CallMCValues, func: *Func) void { |
| 6118 | func.gpa.free(call.args); |
| 6119 | call.* = undefined; |
| 6126 | 6120 | } |
| 6127 | 6121 | }; |
| 6128 | 6122 | |
| 6129 | 6123 | /// Caller must call `CallMCValues.deinit`. |
| 6130 | 6124 | fn resolveCallingConventionValues( |
| 6131 | | self: *Self, |
| 6125 | func: *Func, |
| 6132 | 6126 | fn_info: InternPool.Key.FuncType, |
| 6133 | 6127 | var_args: []const Type, |
| 6134 | 6128 | ) !CallMCValues { |
| 6135 | | const zcu = self.bin_file.comp.module.?; |
| 6129 | const zcu = func.bin_file.comp.module.?; |
| 6136 | 6130 | const ip = &zcu.intern_pool; |
| 6137 | 6131 | |
| 6138 | | const param_types = try self.gpa.alloc(Type, fn_info.param_types.len + var_args.len); |
| 6139 | | defer self.gpa.free(param_types); |
| 6132 | const param_types = try func.gpa.alloc(Type, fn_info.param_types.len + var_args.len); |
| 6133 | defer func.gpa.free(param_types); |
| 6140 | 6134 | |
| 6141 | 6135 | for (param_types[0..fn_info.param_types.len], fn_info.param_types.get(ip)) |*dest, src| { |
| 6142 | 6136 | dest.* = Type.fromInterned(src); |
| 6143 | 6137 | } |
| 6144 | 6138 | for (param_types[fn_info.param_types.len..], var_args) |*param_ty, arg_ty| |
| 6145 | | param_ty.* = self.promoteVarArg(arg_ty); |
| 6139 | param_ty.* = func.promoteVarArg(arg_ty); |
| 6146 | 6140 | |
| 6147 | 6141 | const cc = fn_info.cc; |
| 6148 | 6142 | var result: CallMCValues = .{ |
| 6149 | | .args = try self.gpa.alloc(MCValue, param_types.len), |
| 6143 | .args = try func.gpa.alloc(MCValue, param_types.len), |
| 6150 | 6144 | // These undefined values must be populated before returning from this function. |
| 6151 | 6145 | .return_value = undefined, |
| 6152 | 6146 | .stack_byte_count = 0, |
| 6153 | 6147 | .stack_align = undefined, |
| 6154 | 6148 | }; |
| 6155 | | errdefer self.gpa.free(result.args); |
| 6149 | errdefer func.gpa.free(result.args); |
| 6156 | 6150 | |
| 6157 | 6151 | const ret_ty = Type.fromInterned(fn_info.return_type); |
| 6158 | 6152 | |
| ... | ... | @@ -6164,7 +6158,7 @@ fn resolveCallingConventionValues( |
| 6164 | 6158 | }, |
| 6165 | 6159 | .C, .Unspecified => { |
| 6166 | 6160 | if (result.args.len > 8) { |
| 6167 | | return self.fail("RISC-V calling convention does not support more than 8 arguments", .{}); |
| 6161 | return func.fail("RISC-V calling convention does not support more than 8 arguments", .{}); |
| 6168 | 6162 | } |
| 6169 | 6163 | |
| 6170 | 6164 | var ret_int_reg_i: u32 = 0; |
| ... | ... | @@ -6211,11 +6205,11 @@ fn resolveCallingConventionValues( |
| 6211 | 6205 | }; |
| 6212 | 6206 | ret_tracking_i += 1; |
| 6213 | 6207 | }, |
| 6214 | | else => return self.fail("TODO: C calling convention return class {}", .{class}), |
| 6208 | else => return func.fail("TODO: C calling convention return class {}", .{class}), |
| 6215 | 6209 | }; |
| 6216 | 6210 | |
| 6217 | 6211 | result.return_value = switch (ret_tracking_i) { |
| 6218 | | else => return self.fail("ty {} took {} tracking return indices", .{ ret_ty.fmt(zcu), ret_tracking_i }), |
| 6212 | else => return func.fail("ty {} took {} tracking return indices", .{ ret_ty.fmt(zcu), ret_tracking_i }), |
| 6219 | 6213 | 1 => ret_tracking[0], |
| 6220 | 6214 | 2 => InstTracking.init(.{ .register_pair = .{ |
| 6221 | 6215 | ret_tracking[0].short.register, ret_tracking[1].short.register, |
| ... | ... | @@ -6267,28 +6261,28 @@ fn resolveCallingConventionValues( |
| 6267 | 6261 | arg_mcv[arg_mcv_i] = .{ .indirect = .{ .reg = param_int_reg } }; |
| 6268 | 6262 | arg_mcv_i += 1; |
| 6269 | 6263 | }, |
| 6270 | | else => return self.fail("TODO: C calling convention arg class {}", .{class}), |
| 6264 | else => return func.fail("TODO: C calling convention arg class {}", .{class}), |
| 6271 | 6265 | } else { |
| 6272 | 6266 | arg.* = switch (arg_mcv_i) { |
| 6273 | | else => return self.fail("ty {} took {} tracking arg indices", .{ ty.fmt(zcu), arg_mcv_i }), |
| 6267 | else => return func.fail("ty {} took {} tracking arg indices", .{ ty.fmt(zcu), arg_mcv_i }), |
| 6274 | 6268 | 1 => arg_mcv[0], |
| 6275 | 6269 | 2 => .{ .register_pair = .{ arg_mcv[0].register, arg_mcv[1].register } }, |
| 6276 | 6270 | }; |
| 6277 | 6271 | continue; |
| 6278 | 6272 | } |
| 6279 | 6273 | |
| 6280 | | return self.fail("TODO: pass args by stack", .{}); |
| 6274 | return func.fail("TODO: pass args by stack", .{}); |
| 6281 | 6275 | } |
| 6282 | 6276 | }, |
| 6283 | | else => return self.fail("TODO implement function parameters for {} on riscv64", .{cc}), |
| 6277 | else => return func.fail("TODO implement function parameters for {} on riscv64", .{cc}), |
| 6284 | 6278 | } |
| 6285 | 6279 | |
| 6286 | 6280 | result.stack_byte_count = @intCast(result.stack_align.forward(result.stack_byte_count)); |
| 6287 | 6281 | return result; |
| 6288 | 6282 | } |
| 6289 | 6283 | |
| 6290 | | fn wantSafety(self: *Self) bool { |
| 6291 | | return switch (self.mod.optimize_mode) { |
| 6284 | fn wantSafety(func: *Func) bool { |
| 6285 | return switch (func.mod.optimize_mode) { |
| 6292 | 6286 | .Debug => true, |
| 6293 | 6287 | .ReleaseSafe => true, |
| 6294 | 6288 | .ReleaseFast => false, |
| ... | ... | @@ -6296,39 +6290,36 @@ fn wantSafety(self: *Self) bool { |
| 6296 | 6290 | }; |
| 6297 | 6291 | } |
| 6298 | 6292 | |
| 6299 | | fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { |
| 6293 | fn fail(func: *Func, comptime format: []const u8, args: anytype) InnerError { |
| 6300 | 6294 | @setCold(true); |
| 6301 | | assert(self.err_msg == null); |
| 6302 | | self.err_msg = try ErrorMsg.create(self.gpa, self.src_loc, format, args); |
| 6295 | assert(func.err_msg == null); |
| 6296 | func.err_msg = try ErrorMsg.create(func.gpa, func.src_loc, format, args); |
| 6303 | 6297 | return error.CodegenFail; |
| 6304 | 6298 | } |
| 6305 | 6299 | |
| 6306 | | fn failSymbol(self: *Self, comptime format: []const u8, args: anytype) InnerError { |
| 6300 | fn failSymbol(func: *Func, comptime format: []const u8, args: anytype) InnerError { |
| 6307 | 6301 | @setCold(true); |
| 6308 | | assert(self.err_msg == null); |
| 6309 | | self.err_msg = try ErrorMsg.create(self.gpa, self.src_loc, format, args); |
| 6302 | assert(func.err_msg == null); |
| 6303 | func.err_msg = try ErrorMsg.create(func.gpa, func.src_loc, format, args); |
| 6310 | 6304 | return error.CodegenFail; |
| 6311 | 6305 | } |
| 6312 | 6306 | |
| 6313 | 6307 | fn parseRegName(name: []const u8) ?Register { |
| 6314 | | if (@hasDecl(Register, "parseRegName")) { |
| 6315 | | return Register.parseRegName(name); |
| 6316 | | } |
| 6317 | 6308 | return std.meta.stringToEnum(Register, name); |
| 6318 | 6309 | } |
| 6319 | 6310 | |
| 6320 | | fn typeOf(self: *Self, inst: Air.Inst.Ref) Type { |
| 6321 | | const zcu = self.bin_file.comp.module.?; |
| 6322 | | return self.air.typeOf(inst, &zcu.intern_pool); |
| 6311 | fn typeOf(func: *Func, inst: Air.Inst.Ref) Type { |
| 6312 | const zcu = func.bin_file.comp.module.?; |
| 6313 | return func.air.typeOf(inst, &zcu.intern_pool); |
| 6323 | 6314 | } |
| 6324 | 6315 | |
| 6325 | | fn typeOfIndex(self: *Self, inst: Air.Inst.Index) Type { |
| 6326 | | const zcu = self.bin_file.comp.module.?; |
| 6327 | | return self.air.typeOfIndex(inst, &zcu.intern_pool); |
| 6316 | fn typeOfIndex(func: *Func, inst: Air.Inst.Index) Type { |
| 6317 | const zcu = func.bin_file.comp.module.?; |
| 6318 | return func.air.typeOfIndex(inst, &zcu.intern_pool); |
| 6328 | 6319 | } |
| 6329 | 6320 | |
| 6330 | | fn hasFeature(self: *Self, feature: Target.riscv.Feature) bool { |
| 6331 | | return Target.riscv.featureSetHas(self.target.cpu.features, feature); |
| 6321 | fn hasFeature(func: *Func, feature: Target.riscv.Feature) bool { |
| 6322 | return Target.riscv.featureSetHas(func.target.cpu.features, feature); |
| 6332 | 6323 | } |
| 6333 | 6324 | |
| 6334 | 6325 | pub fn errUnionPayloadOffset(payload_ty: Type, zcu: *Module) u64 { |
| ... | ... | @@ -6353,8 +6344,8 @@ pub fn errUnionErrorOffset(payload_ty: Type, zcu: *Module) u64 { |
| 6353 | 6344 | } |
| 6354 | 6345 | } |
| 6355 | 6346 | |
| 6356 | | fn promoteInt(self: *Self, ty: Type) Type { |
| 6357 | | const mod = self.bin_file.comp.module.?; |
| 6347 | fn promoteInt(func: *Func, ty: Type) Type { |
| 6348 | const mod = func.bin_file.comp.module.?; |
| 6358 | 6349 | const int_info: InternPool.Key.IntType = switch (ty.toIntern()) { |
| 6359 | 6350 | .bool_type => .{ .signedness = .unsigned, .bits = 1 }, |
| 6360 | 6351 | else => if (ty.isAbiInt(mod)) ty.intInfo(mod) else return ty, |
| ... | ... | @@ -6372,12 +6363,12 @@ fn promoteInt(self: *Self, ty: Type) Type { |
| 6372 | 6363 | return ty; |
| 6373 | 6364 | } |
| 6374 | 6365 | |
| 6375 | | fn promoteVarArg(self: *Self, ty: Type) Type { |
| 6376 | | if (!ty.isRuntimeFloat()) return self.promoteInt(ty); |
| 6377 | | switch (ty.floatBits(self.target.*)) { |
| 6366 | fn promoteVarArg(func: *Func, ty: Type) Type { |
| 6367 | if (!ty.isRuntimeFloat()) return func.promoteInt(ty); |
| 6368 | switch (ty.floatBits(func.target.*)) { |
| 6378 | 6369 | 32, 64 => return Type.f64, |
| 6379 | 6370 | else => |float_bits| { |
| 6380 | | assert(float_bits == self.target.c_type_bit_size(.longdouble)); |
| 6371 | assert(float_bits == func.target.c_type_bit_size(.longdouble)); |
| 6381 | 6372 | return Type.c_longdouble; |
| 6382 | 6373 | }, |
| 6383 | 6374 | } |