| ... | @@ -13,6 +13,7 @@ const link = @import("../../link.zig"); | ... | @@ -13,6 +13,7 @@ const link = @import("../../link.zig"); |
| 13 | const Module = @import("../../Module.zig"); | 13 | const Module = @import("../../Module.zig"); |
| 14 | const TypedValue = @import("../../TypedValue.zig"); | 14 | const TypedValue = @import("../../TypedValue.zig"); |
| 15 | const ErrorMsg = Module.ErrorMsg; | 15 | const ErrorMsg = Module.ErrorMsg; |
| | 16 | const codegen = @import("../../codegen.zig"); |
| 16 | const Air = @import("../../Air.zig"); | 17 | const Air = @import("../../Air.zig"); |
| 17 | const Mir = @import("Mir.zig"); | 18 | const Mir = @import("Mir.zig"); |
| 18 | const Emit = @import("Emit.zig"); | 19 | const Emit = @import("Emit.zig"); |
| ... | @@ -26,6 +27,8 @@ const build_options = @import("build_options"); | ... | @@ -26,6 +27,8 @@ const build_options = @import("build_options"); |
| 26 | | 27 | |
| 27 | const bits = @import("bits.zig"); | 28 | const bits = @import("bits.zig"); |
| 28 | const abi = @import("abi.zig"); | 29 | const abi = @import("abi.zig"); |
| | 30 | const errUnionPayloadOffset = codegen.errUnionPayloadOffset; |
| | 31 | const errUnionErrorOffset = codegen.errUnionErrorOffset; |
| 29 | const Instruction = bits.Instruction; | 32 | const Instruction = bits.Instruction; |
| 30 | const ShiftWidth = Instruction.ShiftWidth; | 33 | const ShiftWidth = Instruction.ShiftWidth; |
| 31 | const RegisterManager = abi.RegisterManager; | 34 | const RegisterManager = abi.RegisterManager; |
| ... | @@ -93,8 +96,11 @@ register_manager: RegisterManager = .{}, | ... | @@ -93,8 +96,11 @@ register_manager: RegisterManager = .{}, |
| 93 | /// Maps offset to what is stored there. | 96 | /// Maps offset to what is stored there. |
| 94 | stack: std.AutoHashMapUnmanaged(u32, StackAllocation) = .{}, | 97 | stack: std.AutoHashMapUnmanaged(u32, StackAllocation) = .{}, |
| 95 | | 98 | |
| 96 | /// Tracks the current instruction allocated to the compare flags | 99 | /// Tracks the current instruction allocated to the condition flags |
| 97 | compare_flags_inst: ?Air.Inst.Index = null, | 100 | condition_flags_inst: ?Air.Inst.Index = null, |
| | 101 | |
| | 102 | /// Tracks the current instruction allocated to the condition register |
| | 103 | condition_register_inst: ?Air.Inst.Index = null, |
| 98 | | 104 | |
| 99 | /// Offset from the stack base, representing the end of the stack frame. | 105 | /// Offset from the stack base, representing the end of the stack frame. |
| 100 | max_end_stack: u32 = 0, | 106 | max_end_stack: u32 = 0, |
| ... | @@ -141,17 +147,19 @@ const MCValue = union(enum) { | ... | @@ -141,17 +147,19 @@ const MCValue = union(enum) { |
| 141 | stack_offset: u32, | 147 | stack_offset: u32, |
| 142 | /// The value is a pointer to one of the stack variables (payload is stack offset). | 148 | /// The value is a pointer to one of the stack variables (payload is stack offset). |
| 143 | ptr_stack_offset: u32, | 149 | ptr_stack_offset: u32, |
| 144 | /// The value is in the specified CCR assuming an unsigned operation, | 150 | /// The value is in the specified CCR. The value is 1 (if |
| 145 | /// with the operator applied on top of it. | 151 | /// the type is u1) or true (if the type in bool) iff the |
| 146 | compare_flags_unsigned: struct { | 152 | /// specified condition is true. |
| 147 | cmp: math.CompareOperator, | 153 | condition_flags: struct { |
| | 154 | cond: Instruction.Condition, |
| 148 | ccr: Instruction.CCR, | 155 | ccr: Instruction.CCR, |
| 149 | }, | 156 | }, |
| 150 | /// The value is in the specified CCR assuming an signed operation, | 157 | /// The value is in the specified Register. The value is 1 (if |
| 151 | /// with the operator applied on top of it. | 158 | /// the type is u1) or true (if the type in bool) iff the |
| 152 | compare_flags_signed: struct { | 159 | /// specified condition is true. |
| 153 | cmp: math.CompareOperator, | 160 | condition_register: struct { |
| 154 | ccr: Instruction.CCR, | 161 | cond: Instruction.RCondition, |
| | 162 | reg: Register, |
| 155 | }, | 163 | }, |
| 156 | | 164 | |
| 157 | fn isMemory(mcv: MCValue) bool { | 165 | fn isMemory(mcv: MCValue) bool { |
| ... | @@ -176,6 +184,8 @@ const MCValue = union(enum) { | ... | @@ -176,6 +184,8 @@ const MCValue = union(enum) { |
| 176 | | 184 | |
| 177 | .immediate, | 185 | .immediate, |
| 178 | .memory, | 186 | .memory, |
| | 187 | .condition_flags, |
| | 188 | .condition_register, |
| 179 | .ptr_stack_offset, | 189 | .ptr_stack_offset, |
| 180 | .undef, | 190 | .undef, |
| 181 | => false, | 191 | => false, |
| ... | @@ -226,26 +236,12 @@ const CallMCValues = struct { | ... | @@ -226,26 +236,12 @@ const CallMCValues = struct { |
| 226 | const BigTomb = struct { | 236 | const BigTomb = struct { |
| 227 | function: *Self, | 237 | function: *Self, |
| 228 | inst: Air.Inst.Index, | 238 | inst: Air.Inst.Index, |
| 229 | tomb_bits: Liveness.Bpi, | 239 | lbt: Liveness.BigTomb, |
| 230 | big_tomb_bits: u32, | | |
| 231 | bit_index: usize, | | |
| 232 | | 240 | |
| 233 | fn feed(bt: *BigTomb, op_ref: Air.Inst.Ref) void { | 241 | fn feed(bt: *BigTomb, op_ref: Air.Inst.Ref) void { |
| 234 | const this_bit_index = bt.bit_index; | 242 | const dies = bt.lbt.feed(); |
| 235 | bt.bit_index += 1; | 243 | const op_index = Air.refToIndex(op_ref) orelse return; |
| 236 | | 244 | if (!dies) return; |
| 237 | const op_int = @enumToInt(op_ref); | | |
| 238 | if (op_int < Air.Inst.Ref.typed_value_map.len) return; | | |
| 239 | const op_index = @intCast(Air.Inst.Index, op_int - Air.Inst.Ref.typed_value_map.len); | | |
| 240 | | | |
| 241 | if (this_bit_index < Liveness.bpi - 1) { | | |
| 242 | const dies = @truncate(u1, bt.tomb_bits >> @intCast(Liveness.OperandInt, this_bit_index)) != 0; | | |
| 243 | if (!dies) return; | | |
| 244 | } else { | | |
| 245 | const big_bit_index = @intCast(u5, this_bit_index - (Liveness.bpi - 1)); | | |
| 246 | const dies = @truncate(u1, bt.big_tomb_bits >> big_bit_index) != 0; | | |
| 247 | if (!dies) return; | | |
| 248 | } | | |
| 249 | bt.function.processDeath(op_index); | 245 | bt.function.processDeath(op_index); |
| 250 | } | 246 | } |
| 251 | | 247 | |
| ... | @@ -511,8 +507,8 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { | ... | @@ -511,8 +507,8 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 511 | .mul => @panic("TODO try self.airMul(inst)"), | 507 | .mul => @panic("TODO try self.airMul(inst)"), |
| 512 | .mulwrap => @panic("TODO try self.airMulWrap(inst)"), | 508 | .mulwrap => @panic("TODO try self.airMulWrap(inst)"), |
| 513 | .mul_sat => @panic("TODO try self.airMulSat(inst)"), | 509 | .mul_sat => @panic("TODO try self.airMulSat(inst)"), |
| 514 | .rem => @panic("TODO try self.airRem(inst)"), | 510 | .rem => try self.airRem(inst), |
| 515 | .mod => @panic("TODO try self.airMod(inst)"), | 511 | .mod => try self.airMod(inst), |
| 516 | .shl, .shl_exact => @panic("TODO try self.airShl(inst)"), | 512 | .shl, .shl_exact => @panic("TODO try self.airShl(inst)"), |
| 517 | .shl_sat => @panic("TODO try self.airShlSat(inst)"), | 513 | .shl_sat => @panic("TODO try self.airShlSat(inst)"), |
| 518 | .min => @panic("TODO try self.airMin(inst)"), | 514 | .min => @panic("TODO try self.airMin(inst)"), |
| ... | @@ -553,9 +549,9 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { | ... | @@ -553,9 +549,9 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 553 | | 549 | |
| 554 | .bool_and => @panic("TODO try self.airBoolOp(inst)"), | 550 | .bool_and => @panic("TODO try self.airBoolOp(inst)"), |
| 555 | .bool_or => @panic("TODO try self.airBoolOp(inst)"), | 551 | .bool_or => @panic("TODO try self.airBoolOp(inst)"), |
| 556 | .bit_and => @panic("TODO try self.airBitAnd(inst)"), | 552 | .bit_and => try self.airBinOp(inst, .bit_and), |
| 557 | .bit_or => @panic("TODO try self.airBitOr(inst)"), | 553 | .bit_or => try self.airBinOp(inst, .bit_or), |
| 558 | .xor => @panic("TODO try self.airXor(inst)"), | 554 | .xor => try self.airBinOp(inst, .xor), |
| 559 | .shr, .shr_exact => @panic("TODO try self.airShr(inst)"), | 555 | .shr, .shr_exact => @panic("TODO try self.airShr(inst)"), |
| 560 | | 556 | |
| 561 | .alloc => try self.airAlloc(inst), | 557 | .alloc => try self.airAlloc(inst), |
| ... | @@ -568,17 +564,17 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { | ... | @@ -568,17 +564,17 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 568 | .breakpoint => try self.airBreakpoint(), | 564 | .breakpoint => try self.airBreakpoint(), |
| 569 | .ret_addr => @panic("TODO try self.airRetAddr(inst)"), | 565 | .ret_addr => @panic("TODO try self.airRetAddr(inst)"), |
| 570 | .frame_addr => @panic("TODO try self.airFrameAddress(inst)"), | 566 | .frame_addr => @panic("TODO try self.airFrameAddress(inst)"), |
| 571 | .fence => @panic("TODO try self.airFence()"), | 567 | .fence => try self.airFence(inst), |
| 572 | .cond_br => try self.airCondBr(inst), | 568 | .cond_br => try self.airCondBr(inst), |
| 573 | .dbg_stmt => try self.airDbgStmt(inst), | 569 | .dbg_stmt => try self.airDbgStmt(inst), |
| 574 | .fptrunc => @panic("TODO try self.airFptrunc(inst)"), | 570 | .fptrunc => @panic("TODO try self.airFptrunc(inst)"), |
| 575 | .fpext => @panic("TODO try self.airFpext(inst)"), | 571 | .fpext => @panic("TODO try self.airFpext(inst)"), |
| 576 | .intcast => @panic("TODO try self.airIntCast(inst)"), | 572 | .intcast => try self.airIntCast(inst), |
| 577 | .trunc => @panic("TODO try self.airTrunc(inst)"), | 573 | .trunc => @panic("TODO try self.airTrunc(inst)"), |
| 578 | .bool_to_int => @panic("TODO try self.airBoolToInt(inst)"), | 574 | .bool_to_int => try self.airBoolToInt(inst), |
| 579 | .is_non_null => @panic("TODO try self.airIsNonNull(inst)"), | 575 | .is_non_null => try self.airIsNonNull(inst), |
| 580 | .is_non_null_ptr => @panic("TODO try self.airIsNonNullPtr(inst)"), | 576 | .is_non_null_ptr => @panic("TODO try self.airIsNonNullPtr(inst)"), |
| 581 | .is_null => @panic("TODO try self.airIsNull(inst)"), | 577 | .is_null => try self.airIsNull(inst), |
| 582 | .is_null_ptr => @panic("TODO try self.airIsNullPtr(inst)"), | 578 | .is_null_ptr => @panic("TODO try self.airIsNullPtr(inst)"), |
| 583 | .is_non_err => try self.airIsNonErr(inst), | 579 | .is_non_err => try self.airIsNonErr(inst), |
| 584 | .is_non_err_ptr => @panic("TODO try self.airIsNonErrPtr(inst)"), | 580 | .is_non_err_ptr => @panic("TODO try self.airIsNonErrPtr(inst)"), |
| ... | @@ -601,7 +597,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { | ... | @@ -601,7 +597,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 601 | .atomic_rmw => @panic("TODO try self.airAtomicRmw(inst)"), | 597 | .atomic_rmw => @panic("TODO try self.airAtomicRmw(inst)"), |
| 602 | .atomic_load => @panic("TODO try self.airAtomicLoad(inst)"), | 598 | .atomic_load => @panic("TODO try self.airAtomicLoad(inst)"), |
| 603 | .memcpy => @panic("TODO try self.airMemcpy(inst)"), | 599 | .memcpy => @panic("TODO try self.airMemcpy(inst)"), |
| 604 | .memset => @panic("TODO try self.airMemset(inst)"), | 600 | .memset => try self.airMemset(inst), |
| 605 | .set_union_tag => @panic("TODO try self.airSetUnionTag(inst)"), | 601 | .set_union_tag => @panic("TODO try self.airSetUnionTag(inst)"), |
| 606 | .get_union_tag => @panic("TODO try self.airGetUnionTag(inst)"), | 602 | .get_union_tag => @panic("TODO try self.airGetUnionTag(inst)"), |
| 607 | .clz => @panic("TODO try self.airClz(inst)"), | 603 | .clz => @panic("TODO try self.airClz(inst)"), |
| ... | @@ -615,12 +611,12 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { | ... | @@ -615,12 +611,12 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 615 | .select => @panic("TODO try self.airSelect(inst)"), | 611 | .select => @panic("TODO try self.airSelect(inst)"), |
| 616 | .shuffle => @panic("TODO try self.airShuffle(inst)"), | 612 | .shuffle => @panic("TODO try self.airShuffle(inst)"), |
| 617 | .reduce => @panic("TODO try self.airReduce(inst)"), | 613 | .reduce => @panic("TODO try self.airReduce(inst)"), |
| 618 | .aggregate_init => @panic("TODO try self.airAggregateInit(inst)"), | 614 | .aggregate_init => try self.airAggregateInit(inst), |
| 619 | .union_init => @panic("TODO try self.airUnionInit(inst)"), | 615 | .union_init => @panic("TODO try self.airUnionInit(inst)"), |
| 620 | .prefetch => @panic("TODO try self.airPrefetch(inst)"), | 616 | .prefetch => @panic("TODO try self.airPrefetch(inst)"), |
| 621 | .mul_add => @panic("TODO try self.airMulAdd(inst)"), | 617 | .mul_add => @panic("TODO try self.airMulAdd(inst)"), |
| 622 | | 618 | |
| 623 | .@"try" => @panic("TODO try self.airTry(inst)"), | 619 | .@"try" => try self.airTry(inst), |
| 624 | .try_ptr => @panic("TODO try self.airTryPtr(inst)"), | 620 | .try_ptr => @panic("TODO try self.airTryPtr(inst)"), |
| 625 | | 621 | |
| 626 | .dbg_var_ptr, | 622 | .dbg_var_ptr, |
| ... | @@ -659,7 +655,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { | ... | @@ -659,7 +655,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 659 | .ptr_slice_len_ptr => @panic("TODO try self.airPtrSliceLenPtr(inst)"), | 655 | .ptr_slice_len_ptr => @panic("TODO try self.airPtrSliceLenPtr(inst)"), |
| 660 | .ptr_slice_ptr_ptr => @panic("TODO try self.airPtrSlicePtrPtr(inst)"), | 656 | .ptr_slice_ptr_ptr => @panic("TODO try self.airPtrSlicePtrPtr(inst)"), |
| 661 | | 657 | |
| 662 | .array_elem_val => @panic("TODO try self.airArrayElemVal(inst)"), | 658 | .array_elem_val => try self.airArrayElemVal(inst), |
| 663 | .slice_elem_val => try self.airSliceElemVal(inst), | 659 | .slice_elem_val => try self.airSliceElemVal(inst), |
| 664 | .slice_elem_ptr => @panic("TODO try self.airSliceElemPtr(inst)"), | 660 | .slice_elem_ptr => @panic("TODO try self.airSliceElemPtr(inst)"), |
| 665 | .ptr_elem_val => @panic("TODO try self.airPtrElemVal(inst)"), | 661 | .ptr_elem_val => @panic("TODO try self.airPtrElemVal(inst)"), |
| ... | @@ -676,7 +672,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { | ... | @@ -676,7 +672,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 676 | .unwrap_errunion_payload => try self.airUnwrapErrPayload(inst), | 672 | .unwrap_errunion_payload => try self.airUnwrapErrPayload(inst), |
| 677 | .unwrap_errunion_err_ptr => @panic("TODO try self.airUnwrapErrErrPtr(inst)"), | 673 | .unwrap_errunion_err_ptr => @panic("TODO try self.airUnwrapErrErrPtr(inst)"), |
| 678 | .unwrap_errunion_payload_ptr=> @panic("TODO try self.airUnwrapErrPayloadPtr(inst)"), | 674 | .unwrap_errunion_payload_ptr=> @panic("TODO try self.airUnwrapErrPayloadPtr(inst)"), |
| 679 | .errunion_payload_ptr_set => @panic("TODO try self.airErrUnionPayloadPtrSet(inst)"), | 675 | .errunion_payload_ptr_set => try self.airErrUnionPayloadPtrSet(inst), |
| 680 | .err_return_trace => @panic("TODO try self.airErrReturnTrace(inst)"), | 676 | .err_return_trace => @panic("TODO try self.airErrReturnTrace(inst)"), |
| 681 | .set_err_return_trace => @panic("TODO try self.airSetErrReturnTrace(inst)"), | 677 | .set_err_return_trace => @panic("TODO try self.airSetErrReturnTrace(inst)"), |
| 682 | | 678 | |
| ... | @@ -738,8 +734,8 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -738,8 +734,8 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 738 | else => unreachable, | 734 | else => unreachable, |
| 739 | }; | 735 | }; |
| 740 | | 736 | |
| 741 | try self.spillCompareFlagsIfOccupied(); | 737 | try self.spillConditionFlagsIfOccupied(); |
| 742 | self.compare_flags_inst = inst; | 738 | self.condition_flags_inst = inst; |
| 743 | | 739 | |
| 744 | const dest = blk: { | 740 | const dest = blk: { |
| 745 | if (rhs_immediate_ok) { | 741 | if (rhs_immediate_ok) { |
| ... | @@ -781,11 +777,39 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -781,11 +777,39 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 781 | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); | 777 | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 782 | } | 778 | } |
| 783 | | 779 | |
| | 780 | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| | 781 | const vector_ty = self.air.typeOfIndex(inst); |
| | 782 | const len = vector_ty.vectorLen(); |
| | 783 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| | 784 | const elements = @ptrCast([]const Air.Inst.Ref, self.air.extra[ty_pl.payload..][0..len]); |
| | 785 | const result: MCValue = res: { |
| | 786 | if (self.liveness.isUnused(inst)) break :res MCValue.dead; |
| | 787 | return self.fail("TODO implement airAggregateInit for {}", .{self.target.cpu.arch}); |
| | 788 | }; |
| | 789 | |
| | 790 | if (elements.len <= Liveness.bpi - 1) { |
| | 791 | var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); |
| | 792 | std.mem.copy(Air.Inst.Ref, &buf, elements); |
| | 793 | return self.finishAir(inst, result, buf); |
| | 794 | } |
| | 795 | var bt = try self.iterateBigTomb(inst, elements.len); |
| | 796 | for (elements) |elem| { |
| | 797 | bt.feed(elem); |
| | 798 | } |
| | 799 | return bt.finishAir(result); |
| | 800 | } |
| | 801 | |
| 784 | fn airAlloc(self: *Self, inst: Air.Inst.Index) !void { | 802 | fn airAlloc(self: *Self, inst: Air.Inst.Index) !void { |
| 785 | const stack_offset = try self.allocMemPtr(inst); | 803 | const stack_offset = try self.allocMemPtr(inst); |
| 786 | return self.finishAir(inst, .{ .ptr_stack_offset = stack_offset }, .{ .none, .none, .none }); | 804 | return self.finishAir(inst, .{ .ptr_stack_offset = stack_offset }, .{ .none, .none, .none }); |
| 787 | } | 805 | } |
| 788 | | 806 | |
| | 807 | fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| | 808 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| | 809 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement array_elem_val for {}", .{self.target.cpu.arch}); |
| | 810 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| | 811 | } |
| | 812 | |
| 789 | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { | 813 | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 790 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 814 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 791 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { | 815 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| ... | @@ -969,6 +993,13 @@ fn airBinOp(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { | ... | @@ -969,6 +993,13 @@ fn airBinOp(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 969 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); | 993 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 970 | } | 994 | } |
| 971 | | 995 | |
| | 996 | fn airBoolToInt(self: *Self, inst: Air.Inst.Index) !void { |
| | 997 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| | 998 | const operand = try self.resolveInst(un_op); |
| | 999 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else operand; |
| | 1000 | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| | 1001 | } |
| | 1002 | |
| 972 | fn airPtrArithmetic(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { | 1003 | fn airPtrArithmetic(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 973 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 1004 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 974 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; | 1005 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| ... | @@ -1072,7 +1103,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. | ... | @@ -1072,7 +1103,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. |
| 1072 | | 1103 | |
| 1073 | // CCR is volatile across function calls | 1104 | // CCR is volatile across function calls |
| 1074 | // (SCD 2.4.1, page 3P-10) | 1105 | // (SCD 2.4.1, page 3P-10) |
| 1075 | try self.spillCompareFlagsIfOccupied(); | 1106 | try self.spillConditionFlagsIfOccupied(); |
| | 1107 | |
| | 1108 | // Save caller-saved registers, but crucially *after* we save the |
| | 1109 | // compare flags as saving compare flags may require a new |
| | 1110 | // caller-saved register |
| | 1111 | for (abi.caller_preserved_regs) |reg| { |
| | 1112 | try self.register_manager.getReg(reg, null); |
| | 1113 | } |
| 1076 | | 1114 | |
| 1077 | for (info.args) |mc_arg, arg_i| { | 1115 | for (info.args) |mc_arg, arg_i| { |
| 1078 | const arg = args[arg_i]; | 1116 | const arg = args[arg_i]; |
| ... | @@ -1167,7 +1205,12 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. | ... | @@ -1167,7 +1205,12 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. |
| 1167 | return self.finishAir(inst, result, buf); | 1205 | return self.finishAir(inst, result, buf); |
| 1168 | } | 1206 | } |
| 1169 | | 1207 | |
| 1170 | @panic("TODO handle return value with BigTomb"); | 1208 | var bt = try self.iterateBigTomb(inst, 1 + args.len); |
| | 1209 | bt.feed(callee); |
| | 1210 | for (args) |arg| { |
| | 1211 | bt.feed(arg); |
| | 1212 | } |
| | 1213 | return bt.finishAir(result); |
| 1171 | } | 1214 | } |
| 1172 | | 1215 | |
| 1173 | fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { | 1216 | fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| ... | @@ -1208,12 +1251,18 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { | ... | @@ -1208,12 +1251,18 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| 1208 | .inst = inst, | 1251 | .inst = inst, |
| 1209 | }); | 1252 | }); |
| 1210 | | 1253 | |
| 1211 | try self.spillCompareFlagsIfOccupied(); | 1254 | try self.spillConditionFlagsIfOccupied(); |
| 1212 | self.compare_flags_inst = inst; | 1255 | self.condition_flags_inst = inst; |
| 1213 | | 1256 | |
| 1214 | break :result switch (int_info.signedness) { | 1257 | break :result switch (int_info.signedness) { |
| 1215 | .signed => MCValue{ .compare_flags_signed = .{ .cmp = op, .ccr = .xcc } }, | 1258 | .signed => MCValue{ .condition_flags = .{ |
| 1216 | .unsigned => MCValue{ .compare_flags_unsigned = .{ .cmp = op, .ccr = .xcc } }, | 1259 | .cond = .{ .icond = Instruction.ICondition.fromCompareOperatorSigned(op) }, |
| | 1260 | .ccr = .xcc, |
| | 1261 | } }, |
| | 1262 | .unsigned => MCValue{ .condition_flags = .{ |
| | 1263 | .cond = .{ .icond = Instruction.ICondition.fromCompareOperatorUnsigned(op) }, |
| | 1264 | .ccr = .xcc, |
| | 1265 | } }, |
| 1217 | }; | 1266 | }; |
| 1218 | } else { | 1267 | } else { |
| 1219 | return self.fail("TODO SPARCv9 cmp for ints > 64 bits", .{}); | 1268 | return self.fail("TODO SPARCv9 cmp for ints > 64 bits", .{}); |
| ... | @@ -1224,69 +1273,14 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { | ... | @@ -1224,69 +1273,14 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| 1224 | | 1273 | |
| 1225 | fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { | 1274 | fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { |
| 1226 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 1275 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 1227 | const cond = try self.resolveInst(pl_op.operand); | 1276 | const condition = try self.resolveInst(pl_op.operand); |
| 1228 | const extra = self.air.extraData(Air.CondBr, pl_op.payload); | 1277 | const extra = self.air.extraData(Air.CondBr, pl_op.payload); |
| 1229 | const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len]; | 1278 | const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len]; |
| 1230 | const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; | 1279 | const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; |
| 1231 | const liveness_condbr = self.liveness.getCondBr(inst); | 1280 | const liveness_condbr = self.liveness.getCondBr(inst); |
| 1232 | | 1281 | |
| 1233 | // Here we either emit a BPcc for branching on CCR content, | 1282 | // Here we emit a branch to the false section. |
| 1234 | // or emit a BPr to branch on register content. | 1283 | const reloc: Mir.Inst.Index = try self.condBr(condition); |
| 1235 | const reloc: Mir.Inst.Index = switch (cond) { | | |
| 1236 | .compare_flags_signed, | | |
| 1237 | .compare_flags_unsigned, | | |
| 1238 | => try self.addInst(.{ | | |
| 1239 | .tag = .bpcc, | | |
| 1240 | .data = .{ | | |
| 1241 | .branch_predict_int = .{ | | |
| 1242 | .ccr = switch (cond) { | | |
| 1243 | .compare_flags_signed => |cmp_op| cmp_op.ccr, | | |
| 1244 | .compare_flags_unsigned => |cmp_op| cmp_op.ccr, | | |
| 1245 | else => unreachable, | | |
| 1246 | }, | | |
| 1247 | .cond = switch (cond) { | | |
| 1248 | .compare_flags_signed => |cmp_op| blk: { | | |
| 1249 | // Here we map to the opposite condition because the jump is to the false branch. | | |
| 1250 | const condition = Instruction.ICondition.fromCompareOperatorSigned(cmp_op.cmp); | | |
| 1251 | break :blk condition.negate(); | | |
| 1252 | }, | | |
| 1253 | .compare_flags_unsigned => |cmp_op| blk: { | | |
| 1254 | // Here we map to the opposite condition because the jump is to the false branch. | | |
| 1255 | const condition = Instruction.ICondition.fromCompareOperatorUnsigned(cmp_op.cmp); | | |
| 1256 | break :blk condition.negate(); | | |
| 1257 | }, | | |
| 1258 | else => unreachable, | | |
| 1259 | }, | | |
| 1260 | .inst = undefined, // Will be filled by performReloc | | |
| 1261 | }, | | |
| 1262 | }, | | |
| 1263 | }), | | |
| 1264 | else => blk: { | | |
| 1265 | const reg = switch (cond) { | | |
| 1266 | .register => |r| r, | | |
| 1267 | else => try self.copyToTmpRegister(Type.bool, cond), | | |
| 1268 | }; | | |
| 1269 | | | |
| 1270 | break :blk try self.addInst(.{ | | |
| 1271 | .tag = .bpr, | | |
| 1272 | .data = .{ | | |
| 1273 | .branch_predict_reg = .{ | | |
| 1274 | .cond = .eq_zero, | | |
| 1275 | .rs1 = reg, | | |
| 1276 | .inst = undefined, // populated later through performReloc | | |
| 1277 | }, | | |
| 1278 | }, | | |
| 1279 | }); | | |
| 1280 | }, | | |
| 1281 | }; | | |
| 1282 | | | |
| 1283 | // Regardless of the branch type that's emitted, we need to reserve | | |
| 1284 | // a space for the delay slot. | | |
| 1285 | // TODO Find a way to fill this delay slot | | |
| 1286 | _ = try self.addInst(.{ | | |
| 1287 | .tag = .nop, | | |
| 1288 | .data = .{ .nop = {} }, | | |
| 1289 | }); | | |
| 1290 | | 1284 | |
| 1291 | // If the condition dies here in this condbr instruction, process | 1285 | // If the condition dies here in this condbr instruction, process |
| 1292 | // that death now instead of later as this has an effect on | 1286 | // that death now instead of later as this has an effect on |
| ... | @@ -1305,7 +1299,7 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1305,7 +1299,7 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { |
| 1305 | var parent_stack = try self.stack.clone(self.gpa); | 1299 | var parent_stack = try self.stack.clone(self.gpa); |
| 1306 | defer parent_stack.deinit(self.gpa); | 1300 | defer parent_stack.deinit(self.gpa); |
| 1307 | const parent_registers = self.register_manager.registers; | 1301 | const parent_registers = self.register_manager.registers; |
| 1308 | const parent_compare_flags_inst = self.compare_flags_inst; | 1302 | const parent_condition_flags_inst = self.condition_flags_inst; |
| 1309 | | 1303 | |
| 1310 | try self.branch_stack.append(.{}); | 1304 | try self.branch_stack.append(.{}); |
| 1311 | errdefer { | 1305 | errdefer { |
| ... | @@ -1324,7 +1318,7 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1324,7 +1318,7 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { |
| 1324 | defer saved_then_branch.deinit(self.gpa); | 1318 | defer saved_then_branch.deinit(self.gpa); |
| 1325 | | 1319 | |
| 1326 | self.register_manager.registers = parent_registers; | 1320 | self.register_manager.registers = parent_registers; |
| 1327 | self.compare_flags_inst = parent_compare_flags_inst; | 1321 | self.condition_flags_inst = parent_condition_flags_inst; |
| 1328 | | 1322 | |
| 1329 | self.stack.deinit(self.gpa); | 1323 | self.stack.deinit(self.gpa); |
| 1330 | self.stack = parent_stack; | 1324 | self.stack = parent_stack; |
| ... | @@ -1468,6 +1462,53 @@ fn airDiv(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1468,6 +1462,53 @@ fn airDiv(self: *Self, inst: Air.Inst.Index) !void { |
| 1468 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); | 1462 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 1469 | } | 1463 | } |
| 1470 | | 1464 | |
| | 1465 | fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| | 1466 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| | 1467 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement .errunion_payload_ptr_set for {}", .{self.target.cpu.arch}); |
| | 1468 | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| | 1469 | } |
| | 1470 | |
| | 1471 | fn airFence(self: *Self, inst: Air.Inst.Index) !void { |
| | 1472 | // TODO weaken this as needed, currently this implements the strongest membar form |
| | 1473 | const fence = self.air.instructions.items(.data)[inst].fence; |
| | 1474 | _ = fence; |
| | 1475 | |
| | 1476 | // membar #StoreStore | #LoadStore | #StoreLoad | #LoadLoad |
| | 1477 | _ = try self.addInst(.{ |
| | 1478 | .tag = .membar, |
| | 1479 | .data = .{ |
| | 1480 | .membar_mask = .{ |
| | 1481 | .mmask = .{ |
| | 1482 | .store_store = true, |
| | 1483 | .store_load = true, |
| | 1484 | .load_store = true, |
| | 1485 | .load_load = true, |
| | 1486 | }, |
| | 1487 | }, |
| | 1488 | }, |
| | 1489 | }); |
| | 1490 | |
| | 1491 | return self.finishAir(inst, .dead, .{ .none, .none, .none }); |
| | 1492 | } |
| | 1493 | |
| | 1494 | fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| | 1495 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| | 1496 | if (self.liveness.isUnused(inst)) |
| | 1497 | return self.finishAir(inst, .dead, .{ ty_op.operand, .none, .none }); |
| | 1498 | |
| | 1499 | const operand_ty = self.air.typeOf(ty_op.operand); |
| | 1500 | const operand = try self.resolveInst(ty_op.operand); |
| | 1501 | const info_a = operand_ty.intInfo(self.target.*); |
| | 1502 | const info_b = self.air.typeOfIndex(inst).intInfo(self.target.*); |
| | 1503 | if (info_a.signedness != info_b.signedness) |
| | 1504 | return self.fail("TODO gen intcast sign safety in semantic analysis", .{}); |
| | 1505 | |
| | 1506 | if (info_a.bits == info_b.bits) |
| | 1507 | return self.finishAir(inst, operand, .{ ty_op.operand, .none, .none }); |
| | 1508 | |
| | 1509 | return self.fail("TODO implement intCast for {}", .{self.target.cpu.arch}); |
| | 1510 | } |
| | 1511 | |
| 1471 | fn airIsErr(self: *Self, inst: Air.Inst.Index) !void { | 1512 | fn airIsErr(self: *Self, inst: Air.Inst.Index) !void { |
| 1472 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 1513 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 1473 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { | 1514 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| ... | @@ -1488,6 +1529,24 @@ fn airIsNonErr(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1488,6 +1529,24 @@ fn airIsNonErr(self: *Self, inst: Air.Inst.Index) !void { |
| 1488 | return self.finishAir(inst, result, .{ un_op, .none, .none }); | 1529 | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| 1489 | } | 1530 | } |
| 1490 | | 1531 | |
| | 1532 | fn airIsNull(self: *Self, inst: Air.Inst.Index) !void { |
| | 1533 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| | 1534 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| | 1535 | const operand = try self.resolveInst(un_op); |
| | 1536 | break :result try self.isNull(operand); |
| | 1537 | }; |
| | 1538 | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| | 1539 | } |
| | 1540 | |
| | 1541 | fn airIsNonNull(self: *Self, inst: Air.Inst.Index) !void { |
| | 1542 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| | 1543 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| | 1544 | const operand = try self.resolveInst(un_op); |
| | 1545 | break :result try self.isNonNull(operand); |
| | 1546 | }; |
| | 1547 | return self.finishAir(inst, result, .{ un_op, .none, .none }); |
| | 1548 | } |
| | 1549 | |
| 1491 | fn airLoad(self: *Self, inst: Air.Inst.Index) !void { | 1550 | fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 1492 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 1551 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1493 | const elem_ty = self.air.typeOfIndex(inst); | 1552 | const elem_ty = self.air.typeOfIndex(inst); |
| ... | @@ -1529,6 +1588,158 @@ fn airLoop(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1529,6 +1588,158 @@ fn airLoop(self: *Self, inst: Air.Inst.Index) !void { |
| 1529 | return self.finishAirBookkeeping(); | 1588 | return self.finishAirBookkeeping(); |
| 1530 | } | 1589 | } |
| 1531 | | 1590 | |
| | 1591 | fn airMemset(self: *Self, inst: Air.Inst.Index) !void { |
| | 1592 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| | 1593 | const extra = self.air.extraData(Air.Bin, pl_op.payload); |
| | 1594 | |
| | 1595 | const operand = pl_op.operand; |
| | 1596 | const value = extra.data.lhs; |
| | 1597 | const length = extra.data.rhs; |
| | 1598 | _ = operand; |
| | 1599 | _ = value; |
| | 1600 | _ = length; |
| | 1601 | |
| | 1602 | return self.fail("TODO implement airMemset for {}", .{self.target.cpu.arch}); |
| | 1603 | } |
| | 1604 | |
| | 1605 | fn airMod(self: *Self, inst: Air.Inst.Index) !void { |
| | 1606 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| | 1607 | const lhs = try self.resolveInst(bin_op.lhs); |
| | 1608 | const rhs = try self.resolveInst(bin_op.rhs); |
| | 1609 | const lhs_ty = self.air.typeOf(bin_op.lhs); |
| | 1610 | const rhs_ty = self.air.typeOf(bin_op.rhs); |
| | 1611 | assert(lhs_ty.eql(rhs_ty, self.bin_file.options.module.?)); |
| | 1612 | |
| | 1613 | if (self.liveness.isUnused(inst)) |
| | 1614 | return self.finishAir(inst, .dead, .{ bin_op.lhs, bin_op.rhs, .none }); |
| | 1615 | |
| | 1616 | // TODO add safety check |
| | 1617 | |
| | 1618 | // We use manual assembly emission to generate faster code |
| | 1619 | // First, ensure lhs, rhs, rem, and added are in registers |
| | 1620 | |
| | 1621 | const lhs_is_register = lhs == .register; |
| | 1622 | const rhs_is_register = rhs == .register; |
| | 1623 | |
| | 1624 | const lhs_reg = if (lhs_is_register) |
| | 1625 | lhs.register |
| | 1626 | else |
| | 1627 | try self.register_manager.allocReg(null, gp); |
| | 1628 | |
| | 1629 | const lhs_lock = self.register_manager.lockReg(lhs_reg); |
| | 1630 | defer if (lhs_lock) |reg| self.register_manager.unlockReg(reg); |
| | 1631 | |
| | 1632 | const rhs_reg = if (rhs_is_register) |
| | 1633 | rhs.register |
| | 1634 | else |
| | 1635 | try self.register_manager.allocReg(null, gp); |
| | 1636 | const rhs_lock = self.register_manager.lockReg(rhs_reg); |
| | 1637 | defer if (rhs_lock) |reg| self.register_manager.unlockReg(reg); |
| | 1638 | |
| | 1639 | if (!lhs_is_register) try self.genSetReg(lhs_ty, lhs_reg, lhs); |
| | 1640 | if (!rhs_is_register) try self.genSetReg(rhs_ty, rhs_reg, rhs); |
| | 1641 | |
| | 1642 | const regs = try self.register_manager.allocRegs(2, .{ null, null }, gp); |
| | 1643 | const regs_locks = self.register_manager.lockRegsAssumeUnused(2, regs); |
| | 1644 | defer for (regs_locks) |reg| { |
| | 1645 | self.register_manager.unlockReg(reg); |
| | 1646 | }; |
| | 1647 | |
| | 1648 | const add_reg = regs[0]; |
| | 1649 | const mod_reg = regs[1]; |
| | 1650 | |
| | 1651 | // mod_reg = @rem(lhs_reg, rhs_reg) |
| | 1652 | _ = try self.addInst(.{ |
| | 1653 | .tag = .sdivx, |
| | 1654 | .data = .{ |
| | 1655 | .arithmetic_3op = .{ |
| | 1656 | .is_imm = false, |
| | 1657 | .rd = mod_reg, |
| | 1658 | .rs1 = lhs_reg, |
| | 1659 | .rs2_or_imm = .{ .rs2 = rhs_reg }, |
| | 1660 | }, |
| | 1661 | }, |
| | 1662 | }); |
| | 1663 | |
| | 1664 | _ = try self.addInst(.{ |
| | 1665 | .tag = .mulx, |
| | 1666 | .data = .{ |
| | 1667 | .arithmetic_3op = .{ |
| | 1668 | .is_imm = false, |
| | 1669 | .rd = mod_reg, |
| | 1670 | .rs1 = mod_reg, |
| | 1671 | .rs2_or_imm = .{ .rs2 = rhs_reg }, |
| | 1672 | }, |
| | 1673 | }, |
| | 1674 | }); |
| | 1675 | |
| | 1676 | _ = try self.addInst(.{ |
| | 1677 | .tag = .sub, |
| | 1678 | .data = .{ |
| | 1679 | .arithmetic_3op = .{ |
| | 1680 | .is_imm = false, |
| | 1681 | .rd = mod_reg, |
| | 1682 | .rs1 = lhs_reg, |
| | 1683 | .rs2_or_imm = .{ .rs2 = mod_reg }, |
| | 1684 | }, |
| | 1685 | }, |
| | 1686 | }); |
| | 1687 | |
| | 1688 | // add_reg = mod_reg + rhs_reg |
| | 1689 | _ = try self.addInst(.{ |
| | 1690 | .tag = .add, |
| | 1691 | .data = .{ |
| | 1692 | .arithmetic_3op = .{ |
| | 1693 | .is_imm = false, |
| | 1694 | .rd = add_reg, |
| | 1695 | .rs1 = mod_reg, |
| | 1696 | .rs2_or_imm = .{ .rs2 = rhs_reg }, |
| | 1697 | }, |
| | 1698 | }, |
| | 1699 | }); |
| | 1700 | |
| | 1701 | // if (add_reg == rhs_reg) add_reg = 0 |
| | 1702 | _ = try self.addInst(.{ |
| | 1703 | .tag = .cmp, |
| | 1704 | .data = .{ |
| | 1705 | .arithmetic_2op = .{ |
| | 1706 | .is_imm = false, |
| | 1707 | .rs1 = add_reg, |
| | 1708 | .rs2_or_imm = .{ .rs2 = rhs_reg }, |
| | 1709 | }, |
| | 1710 | }, |
| | 1711 | }); |
| | 1712 | |
| | 1713 | _ = try self.addInst(.{ |
| | 1714 | .tag = .movcc, |
| | 1715 | .data = .{ |
| | 1716 | .conditional_move_int = .{ |
| | 1717 | .is_imm = true, |
| | 1718 | .ccr = .xcc, |
| | 1719 | .cond = .{ .icond = .eq }, |
| | 1720 | .rd = add_reg, |
| | 1721 | .rs2_or_imm = .{ .imm = 0 }, |
| | 1722 | }, |
| | 1723 | }, |
| | 1724 | }); |
| | 1725 | |
| | 1726 | // if (lhs_reg < 0) mod_reg = add_reg |
| | 1727 | _ = try self.addInst(.{ |
| | 1728 | .tag = .movr, |
| | 1729 | .data = .{ |
| | 1730 | .conditional_move_reg = .{ |
| | 1731 | .is_imm = false, |
| | 1732 | .cond = .lt_zero, |
| | 1733 | .rd = mod_reg, |
| | 1734 | .rs1 = lhs_reg, |
| | 1735 | .rs2_or_imm = .{ .rs2 = add_reg }, |
| | 1736 | }, |
| | 1737 | }, |
| | 1738 | }); |
| | 1739 | |
| | 1740 | return self.finishAir(inst, .{ .register = mod_reg }, .{ bin_op.lhs, bin_op.rhs, .none }); |
| | 1741 | } |
| | 1742 | |
| 1532 | fn airNot(self: *Self, inst: Air.Inst.Index) !void { | 1743 | fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 1533 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 1744 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1534 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { | 1745 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| ... | @@ -1537,42 +1748,17 @@ fn airNot(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1537,42 +1748,17 @@ fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 1537 | switch (operand) { | 1748 | switch (operand) { |
| 1538 | .dead => unreachable, | 1749 | .dead => unreachable, |
| 1539 | .unreach => unreachable, | 1750 | .unreach => unreachable, |
| 1540 | .compare_flags_unsigned => |op| { | 1751 | .condition_flags => |op| { |
| 1541 | const r = MCValue{ | 1752 | break :result MCValue{ |
| 1542 | .compare_flags_unsigned = .{ | 1753 | .condition_flags = .{ |
| 1543 | .cmp = switch (op.cmp) { | 1754 | .cond = op.cond.negate(), |
| 1544 | .gte => .lt, | | |
| 1545 | .gt => .lte, | | |
| 1546 | .neq => .eq, | | |
| 1547 | .lt => .gte, | | |
| 1548 | .lte => .gt, | | |
| 1549 | .eq => .neq, | | |
| 1550 | }, | | |
| 1551 | .ccr = op.ccr, | | |
| 1552 | }, | | |
| 1553 | }; | | |
| 1554 | break :result r; | | |
| 1555 | }, | | |
| 1556 | .compare_flags_signed => |op| { | | |
| 1557 | const r = MCValue{ | | |
| 1558 | .compare_flags_signed = .{ | | |
| 1559 | .cmp = switch (op.cmp) { | | |
| 1560 | .gte => .lt, | | |
| 1561 | .gt => .lte, | | |
| 1562 | .neq => .eq, | | |
| 1563 | .lt => .gte, | | |
| 1564 | .lte => .gt, | | |
| 1565 | .eq => .neq, | | |
| 1566 | }, | | |
| 1567 | .ccr = op.ccr, | 1755 | .ccr = op.ccr, |
| 1568 | }, | 1756 | }, |
| 1569 | }; | 1757 | }; |
| 1570 | break :result r; | | |
| 1571 | }, | 1758 | }, |
| 1572 | else => { | 1759 | else => { |
| 1573 | switch (operand_ty.zigTypeTag()) { | 1760 | switch (operand_ty.zigTypeTag()) { |
| 1574 | .Bool => { | 1761 | .Bool => { |
| 1575 | // TODO convert this to mvn + and | | |
| 1576 | const op_reg = switch (operand) { | 1762 | const op_reg = switch (operand) { |
| 1577 | .register => |r| r, | 1763 | .register => |r| r, |
| 1578 | else => try self.copyToTmpRegister(operand_ty, operand), | 1764 | else => try self.copyToTmpRegister(operand_ty, operand), |
| ... | @@ -1638,7 +1824,7 @@ fn airNot(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1638,7 +1824,7 @@ fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 1638 | | 1824 | |
| 1639 | break :result MCValue{ .register = dest_reg }; | 1825 | break :result MCValue{ .register = dest_reg }; |
| 1640 | } else { | 1826 | } else { |
| 1641 | return self.fail("TODO AArch64 not on integers > u64/i64", .{}); | 1827 | return self.fail("TODO sparc64 not on integers > u64/i64", .{}); |
| 1642 | } | 1828 | } |
| 1643 | }, | 1829 | }, |
| 1644 | else => unreachable, | 1830 | else => unreachable, |
| ... | @@ -1656,6 +1842,27 @@ fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1656,6 +1842,27 @@ fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 1656 | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); | 1842 | return self.finishAir(inst, result, .{ extra.lhs, extra.rhs, .none }); |
| 1657 | } | 1843 | } |
| 1658 | | 1844 | |
| | 1845 | fn airRem(self: *Self, inst: Air.Inst.Index) !void { |
| | 1846 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| | 1847 | const lhs = try self.resolveInst(bin_op.lhs); |
| | 1848 | const rhs = try self.resolveInst(bin_op.rhs); |
| | 1849 | const lhs_ty = self.air.typeOf(bin_op.lhs); |
| | 1850 | const rhs_ty = self.air.typeOf(bin_op.rhs); |
| | 1851 | |
| | 1852 | // TODO add safety check |
| | 1853 | |
| | 1854 | // result = lhs - @divTrunc(lhs, rhs) * rhs |
| | 1855 | const result: MCValue = if (self.liveness.isUnused(inst)) blk: { |
| | 1856 | break :blk .dead; |
| | 1857 | } else blk: { |
| | 1858 | const tmp0 = try self.binOp(.div_trunc, lhs, rhs, lhs_ty, rhs_ty, null); |
| | 1859 | const tmp1 = try self.binOp(.mul, tmp0, rhs, lhs_ty, rhs_ty, null); |
| | 1860 | break :blk try self.binOp(.sub, lhs, tmp1, lhs_ty, rhs_ty, null); |
| | 1861 | }; |
| | 1862 | |
| | 1863 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| | 1864 | } |
| | 1865 | |
| 1659 | fn airRet(self: *Self, inst: Air.Inst.Index) !void { | 1866 | fn airRet(self: *Self, inst: Air.Inst.Index) !void { |
| 1660 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 1867 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 1661 | const operand = try self.resolveInst(un_op); | 1868 | const operand = try self.resolveInst(un_op); |
| ... | @@ -1826,7 +2033,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1826,7 +2033,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 1826 | _ = try self.addInst(.{ | 2033 | _ = try self.addInst(.{ |
| 1827 | .tag = .movcc, | 2034 | .tag = .movcc, |
| 1828 | .data = .{ | 2035 | .data = .{ |
| 1829 | .conditional_move = .{ | 2036 | .conditional_move_int = .{ |
| 1830 | .ccr = rwo.flag.ccr, | 2037 | .ccr = rwo.flag.ccr, |
| 1831 | .cond = .{ .icond = rwo.flag.cond }, | 2038 | .cond = .{ .icond = rwo.flag.cond }, |
| 1832 | .is_imm = true, | 2039 | .is_imm = true, |
| ... | @@ -1855,6 +2062,24 @@ fn airSwitch(self: *Self, inst: Air.Inst.Index) !void { | ... | @@ -1855,6 +2062,24 @@ fn airSwitch(self: *Self, inst: Air.Inst.Index) !void { |
| 1855 | return self.fail("TODO implement switch for {}", .{self.target.cpu.arch}); | 2062 | return self.fail("TODO implement switch for {}", .{self.target.cpu.arch}); |
| 1856 | } | 2063 | } |
| 1857 | | 2064 | |
| | 2065 | fn airTry(self: *Self, inst: Air.Inst.Index) !void { |
| | 2066 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| | 2067 | const extra = self.air.extraData(Air.Try, pl_op.payload); |
| | 2068 | const body = self.air.extra[extra.end..][0..extra.data.body_len]; |
| | 2069 | const result: MCValue = result: { |
| | 2070 | const error_union_ty = self.air.typeOf(pl_op.operand); |
| | 2071 | const error_union = try self.resolveInst(pl_op.operand); |
| | 2072 | const is_err_result = try self.isErr(error_union_ty, error_union); |
| | 2073 | const reloc = try self.condBr(is_err_result); |
| | 2074 | |
| | 2075 | try self.genBody(body); |
| | 2076 | |
| | 2077 | try self.performReloc(reloc); |
| | 2078 | break :result try self.errUnionPayload(error_union, error_union_ty); |
| | 2079 | }; |
| | 2080 | return self.finishAir(inst, result, .{ pl_op.operand, .none, .none }); |
| | 2081 | } |
| | 2082 | |
| 1858 | fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { | 2083 | fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { |
| 1859 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 2084 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1860 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { | 2085 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| ... | @@ -2011,7 +2236,10 @@ fn binOp( | ... | @@ -2011,7 +2236,10 @@ fn binOp( |
| 2011 | ) InnerError!MCValue { | 2236 | ) InnerError!MCValue { |
| 2012 | const mod = self.bin_file.options.module.?; | 2237 | const mod = self.bin_file.options.module.?; |
| 2013 | switch (tag) { | 2238 | switch (tag) { |
| 2014 | .add, .cmp_eq => { | 2239 | .add, |
| | 2240 | .sub, |
| | 2241 | .cmp_eq, |
| | 2242 | => { |
| 2015 | switch (lhs_ty.zigTypeTag()) { | 2243 | switch (lhs_ty.zigTypeTag()) { |
| 2016 | .Float => return self.fail("TODO binary operations on floats", .{}), | 2244 | .Float => return self.fail("TODO binary operations on floats", .{}), |
| 2017 | .Vector => return self.fail("TODO binary operations on vectors", .{}), | 2245 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| ... | @@ -2037,6 +2265,7 @@ fn binOp( | ... | @@ -2037,6 +2265,7 @@ fn binOp( |
| 2037 | | 2265 | |
| 2038 | const mir_tag: Mir.Inst.Tag = switch (tag) { | 2266 | const mir_tag: Mir.Inst.Tag = switch (tag) { |
| 2039 | .add => .add, | 2267 | .add => .add, |
| | 2268 | .sub => .sub, |
| 2040 | .cmp_eq => .cmp, | 2269 | .cmp_eq => .cmp, |
| 2041 | else => unreachable, | 2270 | else => unreachable, |
| 2042 | }; | 2271 | }; |
| ... | @@ -2058,6 +2287,39 @@ fn binOp( | ... | @@ -2058,6 +2287,39 @@ fn binOp( |
| 2058 | } | 2287 | } |
| 2059 | }, | 2288 | }, |
| 2060 | | 2289 | |
| | 2290 | .div_trunc => { |
| | 2291 | switch (lhs_ty.zigTypeTag()) { |
| | 2292 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| | 2293 | .Int => { |
| | 2294 | assert(lhs_ty.eql(rhs_ty, mod)); |
| | 2295 | const int_info = lhs_ty.intInfo(self.target.*); |
| | 2296 | if (int_info.bits <= 64) { |
| | 2297 | const rhs_immediate_ok = switch (tag) { |
| | 2298 | .div_trunc => rhs == .immediate and rhs.immediate <= std.math.maxInt(u12), |
| | 2299 | else => unreachable, |
| | 2300 | }; |
| | 2301 | |
| | 2302 | const mir_tag: Mir.Inst.Tag = switch (tag) { |
| | 2303 | .div_trunc => switch (int_info.signedness) { |
| | 2304 | .signed => Mir.Inst.Tag.sdivx, |
| | 2305 | .unsigned => Mir.Inst.Tag.udivx, |
| | 2306 | }, |
| | 2307 | else => unreachable, |
| | 2308 | }; |
| | 2309 | |
| | 2310 | if (rhs_immediate_ok) { |
| | 2311 | return try self.binOpImmediate(mir_tag, lhs, rhs, lhs_ty, true, metadata); |
| | 2312 | } else { |
| | 2313 | return try self.binOpRegister(mir_tag, lhs, rhs, lhs_ty, rhs_ty, metadata); |
| | 2314 | } |
| | 2315 | } else { |
| | 2316 | return self.fail("TODO binary operations on int with bits > 64", .{}); |
| | 2317 | } |
| | 2318 | }, |
| | 2319 | else => unreachable, |
| | 2320 | } |
| | 2321 | }, |
| | 2322 | |
| 2061 | .mul => { | 2323 | .mul => { |
| 2062 | switch (lhs_ty.zigTypeTag()) { | 2324 | switch (lhs_ty.zigTypeTag()) { |
| 2063 | .Vector => return self.fail("TODO binary operations on vectors", .{}), | 2325 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| ... | @@ -2131,6 +2393,58 @@ fn binOp( | ... | @@ -2131,6 +2393,58 @@ fn binOp( |
| 2131 | } | 2393 | } |
| 2132 | }, | 2394 | }, |
| 2133 | | 2395 | |
| | 2396 | .bit_and, |
| | 2397 | .bit_or, |
| | 2398 | .xor, |
| | 2399 | => { |
| | 2400 | switch (lhs_ty.zigTypeTag()) { |
| | 2401 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| | 2402 | .Int => { |
| | 2403 | assert(lhs_ty.eql(rhs_ty, mod)); |
| | 2404 | const int_info = lhs_ty.intInfo(self.target.*); |
| | 2405 | if (int_info.bits <= 64) { |
| | 2406 | // Only say yes if the operation is |
| | 2407 | // commutative, i.e. we can swap both of the |
| | 2408 | // operands |
| | 2409 | const lhs_immediate_ok = switch (tag) { |
| | 2410 | .bit_and, |
| | 2411 | .bit_or, |
| | 2412 | .xor, |
| | 2413 | => lhs == .immediate and lhs.immediate <= std.math.maxInt(u13), |
| | 2414 | else => unreachable, |
| | 2415 | }; |
| | 2416 | const rhs_immediate_ok = switch (tag) { |
| | 2417 | .bit_and, |
| | 2418 | .bit_or, |
| | 2419 | .xor, |
| | 2420 | => rhs == .immediate and rhs.immediate <= std.math.maxInt(u13), |
| | 2421 | else => unreachable, |
| | 2422 | }; |
| | 2423 | |
| | 2424 | const mir_tag: Mir.Inst.Tag = switch (tag) { |
| | 2425 | .bit_and => .@"and", |
| | 2426 | .bit_or => .@"or", |
| | 2427 | .xor => .xor, |
| | 2428 | else => unreachable, |
| | 2429 | }; |
| | 2430 | |
| | 2431 | if (rhs_immediate_ok) { |
| | 2432 | return try self.binOpImmediate(mir_tag, lhs, rhs, lhs_ty, false, metadata); |
| | 2433 | } else if (lhs_immediate_ok) { |
| | 2434 | // swap lhs and rhs |
| | 2435 | return try self.binOpImmediate(mir_tag, rhs, lhs, rhs_ty, true, metadata); |
| | 2436 | } else { |
| | 2437 | // TODO convert large immediates to register before adding |
| | 2438 | return try self.binOpRegister(mir_tag, lhs, rhs, lhs_ty, rhs_ty, metadata); |
| | 2439 | } |
| | 2440 | } else { |
| | 2441 | return self.fail("TODO binary operations on int with bits > 64", .{}); |
| | 2442 | } |
| | 2443 | }, |
| | 2444 | else => unreachable, |
| | 2445 | } |
| | 2446 | }, |
| | 2447 | |
| 2134 | .shl => { | 2448 | .shl => { |
| 2135 | const base_tag: Air.Inst.Tag = switch (tag) { | 2449 | const base_tag: Air.Inst.Tag = switch (tag) { |
| 2136 | .shl => .shl_exact, | 2450 | .shl => .shl_exact, |
| ... | @@ -2259,7 +2573,14 @@ fn binOpImmediate( | ... | @@ -2259,7 +2573,14 @@ fn binOpImmediate( |
| 2259 | const mir_data: Mir.Inst.Data = switch (mir_tag) { | 2573 | const mir_data: Mir.Inst.Data = switch (mir_tag) { |
| 2260 | .add, | 2574 | .add, |
| 2261 | .addcc, | 2575 | .addcc, |
| | 2576 | .@"and", |
| | 2577 | .@"or", |
| | 2578 | .xor, |
| | 2579 | .xnor, |
| 2262 | .mulx, | 2580 | .mulx, |
| | 2581 | .sdivx, |
| | 2582 | .udivx, |
| | 2583 | .sub, |
| 2263 | .subcc, | 2584 | .subcc, |
| 2264 | => .{ | 2585 | => .{ |
| 2265 | .arithmetic_3op = .{ | 2586 | .arithmetic_3op = .{ |
| ... | @@ -2272,7 +2593,6 @@ fn binOpImmediate( | ... | @@ -2272,7 +2593,6 @@ fn binOpImmediate( |
| 2272 | .sllx => .{ | 2593 | .sllx => .{ |
| 2273 | .shift = .{ | 2594 | .shift = .{ |
| 2274 | .is_imm = true, | 2595 | .is_imm = true, |
| 2275 | .width = ShiftWidth.shift64, | | |
| 2276 | .rd = dest_reg, | 2596 | .rd = dest_reg, |
| 2277 | .rs1 = lhs_reg, | 2597 | .rs1 = lhs_reg, |
| 2278 | .rs2_or_imm = .{ .imm = @intCast(u6, rhs.immediate) }, | 2598 | .rs2_or_imm = .{ .imm = @intCast(u6, rhs.immediate) }, |
| ... | @@ -2377,7 +2697,14 @@ fn binOpRegister( | ... | @@ -2377,7 +2697,14 @@ fn binOpRegister( |
| 2377 | const mir_data: Mir.Inst.Data = switch (mir_tag) { | 2697 | const mir_data: Mir.Inst.Data = switch (mir_tag) { |
| 2378 | .add, | 2698 | .add, |
| 2379 | .addcc, | 2699 | .addcc, |
| | 2700 | .@"and", |
| | 2701 | .@"or", |
| | 2702 | .xor, |
| | 2703 | .xnor, |
| 2380 | .mulx, | 2704 | .mulx, |
| | 2705 | .sdivx, |
| | 2706 | .udivx, |
| | 2707 | .sub, |
| 2381 | .subcc, | 2708 | .subcc, |
| 2382 | => .{ | 2709 | => .{ |
| 2383 | .arithmetic_3op = .{ | 2710 | .arithmetic_3op = .{ |
| ... | @@ -2390,7 +2717,6 @@ fn binOpRegister( | ... | @@ -2390,7 +2717,6 @@ fn binOpRegister( |
| 2390 | .sllx => .{ | 2717 | .sllx => .{ |
| 2391 | .shift = .{ | 2718 | .shift = .{ |
| 2392 | .is_imm = false, | 2719 | .is_imm = false, |
| 2393 | .width = ShiftWidth.shift64, | | |
| 2394 | .rd = dest_reg, | 2720 | .rd = dest_reg, |
| 2395 | .rs1 = lhs_reg, | 2721 | .rs1 = lhs_reg, |
| 2396 | .rs2_or_imm = .{ .rs2 = rhs_reg }, | 2722 | .rs2_or_imm = .{ .rs2 = rhs_reg }, |
| ... | @@ -2464,6 +2790,62 @@ fn brVoid(self: *Self, block: Air.Inst.Index) !void { | ... | @@ -2464,6 +2790,62 @@ fn brVoid(self: *Self, block: Air.Inst.Index) !void { |
| 2464 | block_data.relocs.appendAssumeCapacity(br_index); | 2790 | block_data.relocs.appendAssumeCapacity(br_index); |
| 2465 | } | 2791 | } |
| 2466 | | 2792 | |
| | 2793 | fn condBr(self: *Self, condition: MCValue) !Mir.Inst.Index { |
| | 2794 | // Here we either emit a BPcc for branching on CCR content, |
| | 2795 | // or emit a BPr to branch on register content. |
| | 2796 | const reloc: Mir.Inst.Index = switch (condition) { |
| | 2797 | .condition_flags => |flags| try self.addInst(.{ |
| | 2798 | .tag = .bpcc, |
| | 2799 | .data = .{ |
| | 2800 | .branch_predict_int = .{ |
| | 2801 | .ccr = flags.ccr, |
| | 2802 | // Here we map to the opposite condition because the jump is to the false branch. |
| | 2803 | .cond = flags.cond.icond.negate(), |
| | 2804 | .inst = undefined, // Will be filled by performReloc |
| | 2805 | }, |
| | 2806 | }, |
| | 2807 | }), |
| | 2808 | .condition_register => |reg| try self.addInst(.{ |
| | 2809 | .tag = .bpr, |
| | 2810 | .data = .{ |
| | 2811 | .branch_predict_reg = .{ |
| | 2812 | .rs1 = reg.reg, |
| | 2813 | // Here we map to the opposite condition because the jump is to the false branch. |
| | 2814 | .cond = reg.cond.negate(), |
| | 2815 | .inst = undefined, // Will be filled by performReloc |
| | 2816 | }, |
| | 2817 | }, |
| | 2818 | }), |
| | 2819 | else => blk: { |
| | 2820 | const reg = switch (condition) { |
| | 2821 | .register => |r| r, |
| | 2822 | else => try self.copyToTmpRegister(Type.bool, condition), |
| | 2823 | }; |
| | 2824 | |
| | 2825 | break :blk try self.addInst(.{ |
| | 2826 | .tag = .bpr, |
| | 2827 | .data = .{ |
| | 2828 | .branch_predict_reg = .{ |
| | 2829 | .cond = .eq_zero, |
| | 2830 | .rs1 = reg, |
| | 2831 | .inst = undefined, // populated later through performReloc |
| | 2832 | }, |
| | 2833 | }, |
| | 2834 | }); |
| | 2835 | }, |
| | 2836 | }; |
| | 2837 | |
| | 2838 | // Regardless of the branch type that's emitted, we need to reserve |
| | 2839 | // a space for the delay slot. |
| | 2840 | // TODO Find a way to fill this delay slot |
| | 2841 | _ = try self.addInst(.{ |
| | 2842 | .tag = .nop, |
| | 2843 | .data = .{ .nop = {} }, |
| | 2844 | }); |
| | 2845 | |
| | 2846 | return reloc; |
| | 2847 | } |
| | 2848 | |
| 2467 | /// Copies a value to a register without tracking the register. The register is not considered | 2849 | /// Copies a value to a register without tracking the register. The register is not considered |
| 2468 | /// allocated. A second call to `copyToTmpRegister` may return the same register. | 2850 | /// allocated. A second call to `copyToTmpRegister` may return the same register. |
| 2469 | /// This can have a side effect of spilling instructions to the stack to free up a register. | 2851 | /// This can have a side effect of spilling instructions to the stack to free up a register. |
| ... | @@ -2478,6 +2860,30 @@ fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void { | ... | @@ -2478,6 +2860,30 @@ fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void { |
| 2478 | try table.ensureUnusedCapacity(self.gpa, additional_count); | 2860 | try table.ensureUnusedCapacity(self.gpa, additional_count); |
| 2479 | } | 2861 | } |
| 2480 | | 2862 | |
| | 2863 | /// Given an error union, returns the payload |
| | 2864 | fn errUnionPayload(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue { |
| | 2865 | const err_ty = error_union_ty.errorUnionSet(); |
| | 2866 | const payload_ty = error_union_ty.errorUnionPayload(); |
| | 2867 | if (err_ty.errorSetIsEmpty()) { |
| | 2868 | return error_union_mcv; |
| | 2869 | } |
| | 2870 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| | 2871 | return MCValue.none; |
| | 2872 | } |
| | 2873 | |
| | 2874 | const payload_offset = @intCast(u32, errUnionPayloadOffset(payload_ty, self.target.*)); |
| | 2875 | switch (error_union_mcv) { |
| | 2876 | .register => return self.fail("TODO errUnionPayload for registers", .{}), |
| | 2877 | .stack_offset => |off| { |
| | 2878 | return MCValue{ .stack_offset = off - payload_offset }; |
| | 2879 | }, |
| | 2880 | .memory => |addr| { |
| | 2881 | return MCValue{ .memory = addr + payload_offset }; |
| | 2882 | }, |
| | 2883 | else => unreachable, // invalid MCValue for an error union |
| | 2884 | } |
| | 2885 | } |
| | 2886 | |
| 2481 | fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { | 2887 | fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { |
| 2482 | @setCold(true); | 2888 | @setCold(true); |
| 2483 | assert(self.err_msg == null); | 2889 | assert(self.err_msg == null); |
| ... | @@ -2667,20 +3073,10 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void | ... | @@ -2667,20 +3073,10 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 2667 | switch (mcv) { | 3073 | switch (mcv) { |
| 2668 | .dead => unreachable, | 3074 | .dead => unreachable, |
| 2669 | .unreach, .none => return, // Nothing to do. | 3075 | .unreach, .none => return, // Nothing to do. |
| 2670 | .compare_flags_signed, | 3076 | .condition_flags => |op| { |
| 2671 | .compare_flags_unsigned, | 3077 | const condition = op.cond; |
| 2672 | => { | 3078 | const ccr = op.ccr; |
| 2673 | const condition = switch (mcv) { | | |
| 2674 | .compare_flags_unsigned => |op| Instruction.ICondition.fromCompareOperatorUnsigned(op.cmp), | | |
| 2675 | .compare_flags_signed => |op| Instruction.ICondition.fromCompareOperatorSigned(op.cmp), | | |
| 2676 | else => unreachable, | | |
| 2677 | }; | | |
| 2678 | | 3079 | |
| 2679 | const ccr = switch (mcv) { | | |
| 2680 | .compare_flags_unsigned => |op| op.ccr, | | |
| 2681 | .compare_flags_signed => |op| op.ccr, | | |
| 2682 | else => unreachable, | | |
| 2683 | }; | | |
| 2684 | // TODO handle floating point CCRs | 3080 | // TODO handle floating point CCRs |
| 2685 | assert(ccr == .xcc or ccr == .icc); | 3081 | assert(ccr == .xcc or ccr == .icc); |
| 2686 | | 3082 | |
| ... | @@ -2698,11 +3094,39 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void | ... | @@ -2698,11 +3094,39 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 2698 | _ = try self.addInst(.{ | 3094 | _ = try self.addInst(.{ |
| 2699 | .tag = .movcc, | 3095 | .tag = .movcc, |
| 2700 | .data = .{ | 3096 | .data = .{ |
| 2701 | .conditional_move = .{ | 3097 | .conditional_move_int = .{ |
| 2702 | .ccr = ccr, | 3098 | .ccr = ccr, |
| 2703 | .cond = .{ .icond = condition }, | 3099 | .cond = condition, |
| | 3100 | .is_imm = true, |
| | 3101 | .rd = reg, |
| | 3102 | .rs2_or_imm = .{ .imm = 1 }, |
| | 3103 | }, |
| | 3104 | }, |
| | 3105 | }); |
| | 3106 | }, |
| | 3107 | .condition_register => |op| { |
| | 3108 | const condition = op.cond; |
| | 3109 | const register = op.reg; |
| | 3110 | |
| | 3111 | _ = try self.addInst(.{ |
| | 3112 | .tag = .mov, |
| | 3113 | .data = .{ |
| | 3114 | .arithmetic_2op = .{ |
| | 3115 | .is_imm = false, |
| | 3116 | .rs1 = reg, |
| | 3117 | .rs2_or_imm = .{ .rs2 = .g0 }, |
| | 3118 | }, |
| | 3119 | }, |
| | 3120 | }); |
| | 3121 | |
| | 3122 | _ = try self.addInst(.{ |
| | 3123 | .tag = .movr, |
| | 3124 | .data = .{ |
| | 3125 | .conditional_move_reg = .{ |
| | 3126 | .cond = condition, |
| 2704 | .is_imm = true, | 3127 | .is_imm = true, |
| 2705 | .rd = reg, | 3128 | .rd = reg, |
| | 3129 | .rs1 = register, |
| 2706 | .rs2_or_imm = .{ .imm = 1 }, | 3130 | .rs2_or_imm = .{ .imm = 1 }, |
| 2707 | }, | 3131 | }, |
| 2708 | }, | 3132 | }, |
| ... | @@ -2773,7 +3197,6 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void | ... | @@ -2773,7 +3197,6 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 2773 | .data = .{ | 3197 | .data = .{ |
| 2774 | .shift = .{ | 3198 | .shift = .{ |
| 2775 | .is_imm = true, | 3199 | .is_imm = true, |
| 2776 | .width = .shift64, | | |
| 2777 | .rd = reg, | 3200 | .rd = reg, |
| 2778 | .rs1 = reg, | 3201 | .rs1 = reg, |
| 2779 | .rs2_or_imm = .{ .imm = 12 }, | 3202 | .rs2_or_imm = .{ .imm = 12 }, |
| ... | @@ -2804,7 +3227,6 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void | ... | @@ -2804,7 +3227,6 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 2804 | .data = .{ | 3227 | .data = .{ |
| 2805 | .shift = .{ | 3228 | .shift = .{ |
| 2806 | .is_imm = true, | 3229 | .is_imm = true, |
| 2807 | .width = .shift64, | | |
| 2808 | .rd = reg, | 3230 | .rd = reg, |
| 2809 | .rs1 = reg, | 3231 | .rs1 = reg, |
| 2810 | .rs2_or_imm = .{ .imm = 32 }, | 3232 | .rs2_or_imm = .{ .imm = 32 }, |
| ... | @@ -2874,8 +3296,8 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro | ... | @@ -2874,8 +3296,8 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 2874 | else => return self.fail("TODO implement memset", .{}), | 3296 | else => return self.fail("TODO implement memset", .{}), |
| 2875 | } | 3297 | } |
| 2876 | }, | 3298 | }, |
| 2877 | .compare_flags_unsigned, | 3299 | .condition_flags, |
| 2878 | .compare_flags_signed, | 3300 | .condition_register, |
| 2879 | .immediate, | 3301 | .immediate, |
| 2880 | .ptr_stack_offset, | 3302 | .ptr_stack_offset, |
| 2881 | => { | 3303 | => { |
| ... | @@ -2916,7 +3338,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro | ... | @@ -2916,7 +3338,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 2916 | _ = try self.addInst(.{ | 3338 | _ = try self.addInst(.{ |
| 2917 | .tag = .movcc, | 3339 | .tag = .movcc, |
| 2918 | .data = .{ | 3340 | .data = .{ |
| 2919 | .conditional_move = .{ | 3341 | .conditional_move_int = .{ |
| 2920 | .ccr = rwo.flag.ccr, | 3342 | .ccr = rwo.flag.ccr, |
| 2921 | .cond = .{ .icond = rwo.flag.cond }, | 3343 | .cond = .{ .icond = rwo.flag.cond }, |
| 2922 | .is_imm = true, | 3344 | .is_imm = true, |
| ... | @@ -3103,7 +3525,7 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue { | ... | @@ -3103,7 +3525,7 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue { |
| 3103 | } }, | 3525 | } }, |
| 3104 | }); | 3526 | }); |
| 3105 | | 3527 | |
| 3106 | return MCValue{ .compare_flags_unsigned = .{ .cmp = .gt, .ccr = .xcc } }; | 3528 | return MCValue{ .condition_flags = .{ .cond = .{ .icond = .gu }, .ccr = .xcc } }; |
| 3107 | } else { | 3529 | } else { |
| 3108 | return self.fail("TODO isErr for errors with size > 8", .{}); | 3530 | return self.fail("TODO isErr for errors with size > 8", .{}); |
| 3109 | } | 3531 | } |
| ... | @@ -3116,9 +3538,30 @@ fn isNonErr(self: *Self, ty: Type, operand: MCValue) !MCValue { | ... | @@ -3116,9 +3538,30 @@ fn isNonErr(self: *Self, ty: Type, operand: MCValue) !MCValue { |
| 3116 | // Call isErr, then negate the result. | 3538 | // Call isErr, then negate the result. |
| 3117 | const is_err_result = try self.isErr(ty, operand); | 3539 | const is_err_result = try self.isErr(ty, operand); |
| 3118 | switch (is_err_result) { | 3540 | switch (is_err_result) { |
| 3119 | .compare_flags_unsigned => |op| { | 3541 | .condition_flags => |op| { |
| 3120 | assert(op.cmp == .gt); | 3542 | return MCValue{ .condition_flags = .{ .cond = op.cond.negate(), .ccr = op.ccr } }; |
| 3121 | return MCValue{ .compare_flags_unsigned = .{ .cmp = .lte, .ccr = op.ccr } }; | 3543 | }, |
| | 3544 | .immediate => |imm| { |
| | 3545 | assert(imm == 0); |
| | 3546 | return MCValue{ .immediate = 1 }; |
| | 3547 | }, |
| | 3548 | else => unreachable, |
| | 3549 | } |
| | 3550 | } |
| | 3551 | |
| | 3552 | fn isNull(self: *Self, operand: MCValue) !MCValue { |
| | 3553 | _ = operand; |
| | 3554 | // Here you can specialize this instruction if it makes sense to, otherwise the default |
| | 3555 | // will call isNonNull and invert the result. |
| | 3556 | return self.fail("TODO call isNonNull and invert the result", .{}); |
| | 3557 | } |
| | 3558 | |
| | 3559 | fn isNonNull(self: *Self, operand: MCValue) !MCValue { |
| | 3560 | // Call isNull, then negate the result. |
| | 3561 | const is_null_result = try self.isNull(operand); |
| | 3562 | switch (is_null_result) { |
| | 3563 | .condition_flags => |op| { |
| | 3564 | return MCValue{ .condition_flags = .{ .cond = op.cond.negate(), .ccr = op.ccr } }; |
| 3122 | }, | 3565 | }, |
| 3123 | .immediate => |imm| { | 3566 | .immediate => |imm| { |
| 3124 | assert(imm == 0); | 3567 | assert(imm == 0); |
| ... | @@ -3133,9 +3576,7 @@ fn iterateBigTomb(self: *Self, inst: Air.Inst.Index, operand_count: usize) !BigT | ... | @@ -3133,9 +3576,7 @@ fn iterateBigTomb(self: *Self, inst: Air.Inst.Index, operand_count: usize) !BigT |
| 3133 | return BigTomb{ | 3576 | return BigTomb{ |
| 3134 | .function = self, | 3577 | .function = self, |
| 3135 | .inst = inst, | 3578 | .inst = inst, |
| 3136 | .tomb_bits = self.liveness.getTombBits(inst), | 3579 | .lbt = self.liveness.iterateBigTomb(inst), |
| 3137 | .big_tomb_bits = self.liveness.special.get(inst) orelse 0, | | |
| 3138 | .bit_index = 0, | | |
| 3139 | }; | 3580 | }; |
| 3140 | } | 3581 | } |
| 3141 | | 3582 | |
| ... | @@ -3168,8 +3609,8 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo | ... | @@ -3168,8 +3609,8 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo |
| 3168 | .undef => unreachable, | 3609 | .undef => unreachable, |
| 3169 | .unreach => unreachable, | 3610 | .unreach => unreachable, |
| 3170 | .dead => unreachable, | 3611 | .dead => unreachable, |
| 3171 | .compare_flags_unsigned, | 3612 | .condition_flags, |
| 3172 | .compare_flags_signed, | 3613 | .condition_register, |
| 3173 | .register_with_overflow, | 3614 | .register_with_overflow, |
| 3174 | => unreachable, // cannot hold an address | 3615 | => unreachable, // cannot hold an address |
| 3175 | .immediate => |imm| try self.setRegOrMem(elem_ty, dst_mcv, .{ .memory = imm }), | 3616 | .immediate => |imm| try self.setRegOrMem(elem_ty, dst_mcv, .{ .memory = imm }), |
| ... | @@ -3181,7 +3622,7 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo | ... | @@ -3181,7 +3622,7 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo |
| 3181 | switch (dst_mcv) { | 3622 | switch (dst_mcv) { |
| 3182 | .dead => unreachable, | 3623 | .dead => unreachable, |
| 3183 | .undef => unreachable, | 3624 | .undef => unreachable, |
| 3184 | .compare_flags_signed, .compare_flags_unsigned => unreachable, | 3625 | .condition_flags => unreachable, |
| 3185 | .register => |dst_reg| { | 3626 | .register => |dst_reg| { |
| 3186 | try self.genLoad(dst_reg, addr_reg, i13, 0, elem_size); | 3627 | try self.genLoad(dst_reg, addr_reg, i13, 0, elem_size); |
| 3187 | }, | 3628 | }, |
| ... | @@ -3277,10 +3718,10 @@ fn processDeath(self: *Self, inst: Air.Inst.Index) void { | ... | @@ -3277,10 +3718,10 @@ fn processDeath(self: *Self, inst: Air.Inst.Index) void { |
| 3277 | }, | 3718 | }, |
| 3278 | .register_with_overflow => |rwo| { | 3719 | .register_with_overflow => |rwo| { |
| 3279 | self.register_manager.freeReg(rwo.reg); | 3720 | self.register_manager.freeReg(rwo.reg); |
| 3280 | self.compare_flags_inst = null; | 3721 | self.condition_flags_inst = null; |
| 3281 | }, | 3722 | }, |
| 3282 | .compare_flags_signed, .compare_flags_unsigned => { | 3723 | .condition_flags => { |
| 3283 | self.compare_flags_inst = null; | 3724 | self.condition_flags_inst = null; |
| 3284 | }, | 3725 | }, |
| 3285 | else => {}, // TODO process stack allocation death | 3726 | else => {}, // TODO process stack allocation death |
| 3286 | } | 3727 | } |
| ... | @@ -3474,15 +3915,13 @@ fn setRegOrMem(self: *Self, ty: Type, loc: MCValue, val: MCValue) !void { | ... | @@ -3474,15 +3915,13 @@ fn setRegOrMem(self: *Self, ty: Type, loc: MCValue, val: MCValue) !void { |
| 3474 | } | 3915 | } |
| 3475 | } | 3916 | } |
| 3476 | | 3917 | |
| 3477 | /// Save the current instruction stored in the compare flags if | 3918 | /// Save the current instruction stored in the condition flags if |
| 3478 | /// occupied | 3919 | /// occupied |
| 3479 | fn spillCompareFlagsIfOccupied(self: *Self) !void { | 3920 | fn spillConditionFlagsIfOccupied(self: *Self) !void { |
| 3480 | if (self.compare_flags_inst) |inst_to_save| { | 3921 | if (self.condition_flags_inst) |inst_to_save| { |
| 3481 | const mcv = self.getResolvedInstValue(inst_to_save); | 3922 | const mcv = self.getResolvedInstValue(inst_to_save); |
| 3482 | const new_mcv = switch (mcv) { | 3923 | const new_mcv = switch (mcv) { |
| 3483 | .compare_flags_signed, | 3924 | .condition_flags => try self.allocRegOrMem(inst_to_save, true), |
| 3484 | .compare_flags_unsigned, | | |
| 3485 | => try self.allocRegOrMem(inst_to_save, true), | | |
| 3486 | .register_with_overflow => try self.allocRegOrMem(inst_to_save, false), | 3925 | .register_with_overflow => try self.allocRegOrMem(inst_to_save, false), |
| 3487 | else => unreachable, // mcv doesn't occupy the compare flags | 3926 | else => unreachable, // mcv doesn't occupy the compare flags |
| 3488 | }; | 3927 | }; |
| ... | @@ -3493,7 +3932,7 @@ fn spillCompareFlagsIfOccupied(self: *Self) !void { | ... | @@ -3493,7 +3932,7 @@ fn spillCompareFlagsIfOccupied(self: *Self) !void { |
| 3493 | const branch = &self.branch_stack.items[self.branch_stack.items.len - 1]; | 3932 | const branch = &self.branch_stack.items[self.branch_stack.items.len - 1]; |
| 3494 | try branch.inst_table.put(self.gpa, inst_to_save, new_mcv); | 3933 | try branch.inst_table.put(self.gpa, inst_to_save, new_mcv); |
| 3495 | | 3934 | |
| 3496 | self.compare_flags_inst = null; | 3935 | self.condition_flags_inst = null; |
| 3497 | | 3936 | |
| 3498 | // TODO consolidate with register manager and spillInstruction | 3937 | // TODO consolidate with register manager and spillInstruction |
| 3499 | // this call should really belong in the register manager! | 3938 | // this call should really belong in the register manager! |
| ... | @@ -3522,8 +3961,8 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type | ... | @@ -3522,8 +3961,8 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type |
| 3522 | .undef => unreachable, | 3961 | .undef => unreachable, |
| 3523 | .unreach => unreachable, | 3962 | .unreach => unreachable, |
| 3524 | .dead => unreachable, | 3963 | .dead => unreachable, |
| 3525 | .compare_flags_unsigned, | 3964 | .condition_flags, |
| 3526 | .compare_flags_signed, | 3965 | .condition_register, |
| 3527 | .register_with_overflow, | 3966 | .register_with_overflow, |
| 3528 | => unreachable, // cannot hold an address | 3967 | => unreachable, // cannot hold an address |
| 3529 | .immediate => |imm| { | 3968 | .immediate => |imm| { |
| ... | @@ -3604,7 +4043,6 @@ fn truncRegister( | ... | @@ -3604,7 +4043,6 @@ fn truncRegister( |
| 3604 | .data = .{ | 4043 | .data = .{ |
| 3605 | .shift = .{ | 4044 | .shift = .{ |
| 3606 | .is_imm = true, | 4045 | .is_imm = true, |
| 3607 | .width = ShiftWidth.shift64, | | |
| 3608 | .rd = dest_reg, | 4046 | .rd = dest_reg, |
| 3609 | .rs1 = operand_reg, | 4047 | .rs1 = operand_reg, |
| 3610 | .rs2_or_imm = .{ .imm = @intCast(u6, 64 - int_bits) }, | 4048 | .rs2_or_imm = .{ .imm = @intCast(u6, 64 - int_bits) }, |
| ... | @@ -3619,7 +4057,6 @@ fn truncRegister( | ... | @@ -3619,7 +4057,6 @@ fn truncRegister( |
| 3619 | .data = .{ | 4057 | .data = .{ |
| 3620 | .shift = .{ | 4058 | .shift = .{ |
| 3621 | .is_imm = true, | 4059 | .is_imm = true, |
| 3622 | .width = ShiftWidth.shift32, | | |
| 3623 | .rd = dest_reg, | 4060 | .rd = dest_reg, |
| 3624 | .rs1 = dest_reg, | 4061 | .rs1 = dest_reg, |
| 3625 | .rs2_or_imm = .{ .imm = @intCast(u6, int_bits) }, | 4062 | .rs2_or_imm = .{ .imm = @intCast(u6, int_bits) }, |
| ... | @@ -3636,7 +4073,6 @@ fn truncRegister( | ... | @@ -3636,7 +4073,6 @@ fn truncRegister( |
| 3636 | .data = .{ | 4073 | .data = .{ |
| 3637 | .shift = .{ | 4074 | .shift = .{ |
| 3638 | .is_imm = true, | 4075 | .is_imm = true, |
| 3639 | .width = ShiftWidth.shift32, | | |
| 3640 | .rd = dest_reg, | 4076 | .rd = dest_reg, |
| 3641 | .rs1 = operand_reg, | 4077 | .rs1 = operand_reg, |
| 3642 | .rs2_or_imm = .{ .imm = 0 }, | 4078 | .rs2_or_imm = .{ .imm = 0 }, |