authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-05-21 19:06:05+02:00
committergravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-05-21 22:15:04+02:00
log9747303d16dfca61316a292d1e05ac901191e3a3
treea2375efaac6a04c5eb43d6bbf09bf8d633ce8d2a
parent1f5b0c11563d9ee0a284b708c7ca092da0a0b9ef

stage2 ARM: Introduce MCValue.cpsr_flags

MCValue.cpsr_flags replaces MCValue.compare_flags_{signed,unsigned}. This simplifies a lot of stuff and enables an MCValue to represent only the overflow bits in the CPU (previously, it was only possible to represent a register + the overflow bits).

1 files changed, 83 insertions(+), 122 deletions(-)

src/arch/arm/CodeGen.zig+83-122
......@@ -96,7 +96,7 @@ register_manager: RegisterManager = .{},
9696/// Maps offset to what is stored there.
9797stack: std.AutoHashMapUnmanaged(u32, StackAllocation) = .{},
9898/// Tracks the current instruction allocated to the compare flags
99compare_flags_inst: ?Air.Inst.Index = null,
99cpsr_flags_inst: ?Air.Inst.Index = null,
100100
101101/// Offset from the stack base, representing the end of the stack frame.
102102max_end_stack: u32 = 0,
......@@ -159,12 +159,11 @@ const MCValue = union(enum) {
159159 /// The value is a pointer to one of the stack variables (payload
160160 /// is stack offset).
161161 ptr_stack_offset: u32,
162 /// The value is in the compare flags assuming an unsigned
163 /// operation, with this operator applied on top of it.
164 compare_flags_unsigned: math.CompareOperator,
165 /// The value is in the compare flags assuming a signed operation,
166 /// with this operator applied on top of it.
167 compare_flags_signed: math.CompareOperator,
162 /// The value resides in the N, Z, C, V flags of the Current
163 /// Program Status Register (CPSR). The value is 1 (if the type is
164 /// u1) or true (if the type in bool) iff the specified condition
165 /// is true.
166 cpsr_flags: Condition,
168167 /// The value is a function argument passed via the stack.
169168 stack_argument_offset: u32,
170169
......@@ -190,8 +189,7 @@ const MCValue = union(enum) {
190189
191190 .immediate,
192191 .memory,
193 .compare_flags_unsigned,
194 .compare_flags_signed,
192 .cpsr_flags,
195193 .ptr_stack_offset,
196194 .undef,
197195 .stack_argument_offset,
......@@ -768,10 +766,10 @@ fn processDeath(self: *Self, inst: Air.Inst.Index) void {
768766 .register_v_flag,
769767 => |reg| {
770768 self.register_manager.freeReg(reg);
771 self.compare_flags_inst = null;
769 self.cpsr_flags_inst = null;
772770 },
773 .compare_flags_signed, .compare_flags_unsigned => {
774 self.compare_flags_inst = null;
771 .cpsr_flags => {
772 self.cpsr_flags_inst = null;
775773 },
776774 else => {}, // TODO process stack allocation death
777775 }
......@@ -903,12 +901,10 @@ pub fn spillInstruction(self: *Self, reg: Register, inst: Air.Inst.Index) !void
903901/// Save the current instruction stored in the compare flags if
904902/// occupied
905903fn spillCompareFlagsIfOccupied(self: *Self) !void {
906 if (self.compare_flags_inst) |inst_to_save| {
904 if (self.cpsr_flags_inst) |inst_to_save| {
907905 const mcv = self.getResolvedInstValue(inst_to_save);
908906 const new_mcv = switch (mcv) {
909 .compare_flags_signed,
910 .compare_flags_unsigned,
911 => try self.allocRegOrMem(inst_to_save, true),
907 .cpsr_flags => try self.allocRegOrMem(inst_to_save, true),
912908 .register_c_flag,
913909 .register_v_flag,
914910 => try self.allocRegOrMem(inst_to_save, false),
......@@ -921,7 +917,7 @@ fn spillCompareFlagsIfOccupied(self: *Self) !void {
921917 const branch = &self.branch_stack.items[self.branch_stack.items.len - 1];
922918 try branch.inst_table.put(self.gpa, inst_to_save, new_mcv);
923919
924 self.compare_flags_inst = null;
920 self.cpsr_flags_inst = null;
925921
926922 // TODO consolidate with register manager and spillInstruction
927923 // this call should really belong in the register manager!
......@@ -1111,32 +1107,7 @@ fn airNot(self: *Self, inst: Air.Inst.Index) !void {
11111107 switch (operand) {
11121108 .dead => unreachable,
11131109 .unreach => unreachable,
1114 .compare_flags_unsigned => |op| {
1115 const r = MCValue{
1116 .compare_flags_unsigned = switch (op) {
1117 .gte => .lt,
1118 .gt => .lte,
1119 .neq => .eq,
1120 .lt => .gte,
1121 .lte => .gt,
1122 .eq => .neq,
1123 },
1124 };
1125 break :result r;
1126 },
1127 .compare_flags_signed => |op| {
1128 const r = MCValue{
1129 .compare_flags_signed = switch (op) {
1130 .gte => .lt,
1131 .gt => .lte,
1132 .neq => .eq,
1133 .lt => .gte,
1134 .lte => .gt,
1135 .eq => .neq,
1136 },
1137 };
1138 break :result r;
1139 },
1110 .cpsr_flags => |cond| break :result MCValue{ .cpsr_flags = cond.negate() },
11401111 else => {
11411112 switch (operand_ty.zigTypeTag()) {
11421113 .Bool => {
......@@ -1425,7 +1396,7 @@ fn airOverflow(self: *Self, inst: Air.Inst.Index) !void {
14251396 const stack_offset = try self.allocMem(inst, tuple_size, tuple_align);
14261397
14271398 try self.spillCompareFlagsIfOccupied();
1428 self.compare_flags_inst = null;
1399 self.cpsr_flags_inst = null;
14291400
14301401 const base_tag: Air.Inst.Tag = switch (tag) {
14311402 .add_with_overflow => .add,
......@@ -1448,7 +1419,7 @@ fn airOverflow(self: *Self, inst: Air.Inst.Index) !void {
14481419 _ = try self.binOp(.cmp_eq, dest, .{ .register = truncated_reg }, Type.usize, Type.usize, null);
14491420
14501421 try self.genSetStack(lhs_ty, stack_offset, .{ .register = truncated_reg });
1451 try self.genSetStack(Type.initTag(.u1), stack_offset - overflow_bit_offset, .{ .compare_flags_unsigned = .neq });
1422 try self.genSetStack(Type.initTag(.u1), stack_offset - overflow_bit_offset, .{ .cpsr_flags = .ne });
14521423
14531424 break :result MCValue{ .stack_offset = stack_offset };
14541425 } else if (int_info.bits == 32) {
......@@ -1474,7 +1445,7 @@ fn airOverflow(self: *Self, inst: Air.Inst.Index) !void {
14741445 };
14751446
14761447 try self.spillCompareFlagsIfOccupied();
1477 self.compare_flags_inst = inst;
1448 self.cpsr_flags_inst = inst;
14781449
14791450 const dest = blk: {
14801451 if (rhs_immediate_ok) {
......@@ -1530,7 +1501,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
15301501 const stack_offset = try self.allocMem(inst, tuple_size, tuple_align);
15311502
15321503 try self.spillCompareFlagsIfOccupied();
1533 self.compare_flags_inst = null;
1504 self.cpsr_flags_inst = null;
15341505
15351506 const base_tag: Mir.Inst.Tag = switch (int_info.signedness) {
15361507 .signed => .smulbb,
......@@ -1553,14 +1524,14 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
15531524 _ = try self.binOp(.cmp_eq, dest, .{ .register = truncated_reg }, Type.usize, Type.usize, null);
15541525
15551526 try self.genSetStack(lhs_ty, stack_offset, .{ .register = truncated_reg });
1556 try self.genSetStack(Type.initTag(.u1), stack_offset - overflow_bit_offset, .{ .compare_flags_unsigned = .neq });
1527 try self.genSetStack(Type.initTag(.u1), stack_offset - overflow_bit_offset, .{ .cpsr_flags = .ne });
15571528
15581529 break :result MCValue{ .stack_offset = stack_offset };
15591530 } else if (int_info.bits <= 32) {
15601531 const stack_offset = try self.allocMem(inst, tuple_size, tuple_align);
15611532
15621533 try self.spillCompareFlagsIfOccupied();
1563 self.compare_flags_inst = null;
1534 self.cpsr_flags_inst = null;
15641535
15651536 const base_tag: Mir.Inst.Tag = switch (int_info.signedness) {
15661537 .signed => .smull,
......@@ -1704,7 +1675,7 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
17041675 defer if (lhs_lock) |reg| self.register_manager.unlockReg(reg);
17051676
17061677 try self.spillCompareFlagsIfOccupied();
1707 self.compare_flags_inst = null;
1678 self.cpsr_flags_inst = null;
17081679
17091680 // lsl dest, lhs, rhs
17101681 const dest = try self.binOp(.shl, lhs, rhs, lhs_ty, rhs_ty, null);
......@@ -1719,7 +1690,7 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void {
17191690 _ = try self.binOp(.cmp_eq, lhs, reconstructed, lhs_ty, lhs_ty, null);
17201691
17211692 try self.genSetStack(lhs_ty, stack_offset, dest);
1722 try self.genSetStack(Type.initTag(.u1), stack_offset - overflow_bit_offset, .{ .compare_flags_unsigned = .neq });
1693 try self.genSetStack(Type.initTag(.u1), stack_offset - overflow_bit_offset, .{ .cpsr_flags = .ne });
17231694
17241695 break :result MCValue{ .stack_offset = stack_offset };
17251696 } else {
......@@ -2213,8 +2184,7 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo
22132184 .undef => unreachable,
22142185 .unreach => unreachable,
22152186 .dead => unreachable,
2216 .compare_flags_unsigned,
2217 .compare_flags_signed,
2187 .cpsr_flags,
22182188 .register_c_flag,
22192189 .register_v_flag,
22202190 => unreachable, // cannot hold an address
......@@ -2227,7 +2197,7 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo
22272197 switch (dst_mcv) {
22282198 .dead => unreachable,
22292199 .undef => unreachable,
2230 .compare_flags_signed, .compare_flags_unsigned => unreachable,
2200 .cpsr_flags => unreachable,
22312201 .register => |dst_reg| {
22322202 try self.genLdrRegister(dst_reg, reg, elem_ty);
22332203 },
......@@ -2314,8 +2284,7 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type
23142284 .undef => unreachable,
23152285 .unreach => unreachable,
23162286 .dead => unreachable,
2317 .compare_flags_unsigned,
2318 .compare_flags_signed,
2287 .cpsr_flags,
23192288 .register_c_flag,
23202289 .register_v_flag,
23212290 => unreachable, // cannot hold an address
......@@ -2484,37 +2453,48 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {
24842453 break :result MCValue{ .register = reg };
24852454 },
24862455 1 => {
2487 // get overflow bit: set register to C flag
2488 // resp. V flag
2489 const dest_reg = try self.register_manager.allocReg(null, gp);
2456 // get overflow bit: return C or V flag
2457 if (self.liveness.operandDies(inst, 0)) {
2458 self.cpsr_flags_inst = inst;
24902459
2491 // mov reg, #0
2492 _ = try self.addInst(.{
2493 .tag = .mov,
2494 .data = .{ .rr_op = .{
2495 .rd = dest_reg,
2496 .rn = .r0,
2497 .op = Instruction.Operand.fromU32(0).?,
2498 } },
2499 });
2500
2501 // C flag: movcs reg, #1
2502 // V flag: movvs reg, #1
2503 _ = try self.addInst(.{
2504 .tag = .mov,
2505 .cond = switch (mcv) {
2460 const cond: Condition = switch (mcv) {
25062461 .register_c_flag => .cs,
25072462 .register_v_flag => .vs,
25082463 else => unreachable,
2509 },
2510 .data = .{ .rr_op = .{
2511 .rd = dest_reg,
2512 .rn = .r0,
2513 .op = Instruction.Operand.fromU32(1).?,
2514 } },
2515 });
2464 };
25162465
2517 break :result MCValue{ .register = dest_reg };
2466 break :result MCValue{ .cpsr_flags = cond };
2467 } else {
2468 const dest_reg = try self.register_manager.allocReg(null, gp);
2469
2470 // mov reg, #0
2471 _ = try self.addInst(.{
2472 .tag = .mov,
2473 .data = .{ .rr_op = .{
2474 .rd = dest_reg,
2475 .rn = .r0,
2476 .op = Instruction.Operand.fromU32(0).?,
2477 } },
2478 });
2479
2480 // C flag: movcs reg, #1
2481 // V flag: movvs reg, #1
2482 _ = try self.addInst(.{
2483 .tag = .mov,
2484 .cond = switch (mcv) {
2485 .register_c_flag => .cs,
2486 .register_v_flag => .vs,
2487 else => unreachable,
2488 },
2489 .data = .{ .rr_op = .{
2490 .rd = dest_reg,
2491 .rn = .r0,
2492 .op = Instruction.Operand.fromU32(1).?,
2493 } },
2494 });
2495
2496 break :result MCValue{ .register = dest_reg };
2497 }
25182498 },
25192499 else => unreachable,
25202500 }
......@@ -3602,7 +3582,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void {
36023582 const int_info = int_ty.intInfo(self.target.*);
36033583 if (int_info.bits <= 32) {
36043584 try self.spillCompareFlagsIfOccupied();
3605 self.compare_flags_inst = inst;
3585 self.cpsr_flags_inst = inst;
36063586
36073587 _ = try self.binOp(.cmp_eq, lhs, rhs, int_ty, int_ty, BinOpMetadata{
36083588 .lhs = bin_op.lhs,
......@@ -3611,8 +3591,8 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void {
36113591 });
36123592
36133593 break :result switch (int_info.signedness) {
3614 .signed => MCValue{ .compare_flags_signed = op },
3615 .unsigned => MCValue{ .compare_flags_unsigned = op },
3594 .signed => MCValue{ .cpsr_flags = Condition.fromCompareOperatorSigned(op) },
3595 .unsigned => MCValue{ .cpsr_flags = Condition.fromCompareOperatorUnsigned(op) },
36163596 };
36173597 } else {
36183598 return self.fail("TODO ARM cmp for ints > 32 bits", .{});
......@@ -3673,28 +3653,19 @@ fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
36733653
36743654fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
36753655 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
3676 const cond = try self.resolveInst(pl_op.operand);
3656 const cond_inst = try self.resolveInst(pl_op.operand);
36773657 const extra = self.air.extraData(Air.CondBr, pl_op.payload);
36783658 const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len];
36793659 const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len];
36803660 const liveness_condbr = self.liveness.getCondBr(inst);
36813661
36823662 const reloc: Mir.Inst.Index = reloc: {
3683 const condition: Condition = switch (cond) {
3684 .compare_flags_signed => |cmp_op| blk: {
3685 // Here we map to the opposite condition because the jump is to the false branch.
3686 const condition = Condition.fromCompareOperatorSigned(cmp_op);
3687 break :blk condition.negate();
3688 },
3689 .compare_flags_unsigned => |cmp_op| blk: {
3690 // Here we map to the opposite condition because the jump is to the false branch.
3691 const condition = Condition.fromCompareOperatorUnsigned(cmp_op);
3692 break :blk condition.negate();
3693 },
3663 const condition: Condition = switch (cond_inst) {
3664 .cpsr_flags => |cond| cond.negate(),
36943665 else => blk: {
3695 const reg = switch (cond) {
3666 const reg = switch (cond_inst) {
36963667 .register => |r| r,
3697 else => try self.copyToTmpRegister(Type.bool, cond),
3668 else => try self.copyToTmpRegister(Type.bool, cond_inst),
36983669 };
36993670
37003671 try self.spillCompareFlagsIfOccupied();
......@@ -3739,7 +3710,7 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
37393710 var parent_stack = try self.stack.clone(self.gpa);
37403711 defer parent_stack.deinit(self.gpa);
37413712 const parent_registers = self.register_manager.registers;
3742 const parent_compare_flags_inst = self.compare_flags_inst;
3713 const parent_cpsr_flags_inst = self.cpsr_flags_inst;
37433714
37443715 try self.branch_stack.append(.{});
37453716 errdefer {
......@@ -3758,7 +3729,7 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
37583729 defer saved_then_branch.deinit(self.gpa);
37593730
37603731 self.register_manager.registers = parent_registers;
3761 self.compare_flags_inst = parent_compare_flags_inst;
3732 self.cpsr_flags_inst = parent_cpsr_flags_inst;
37623733
37633734 self.stack.deinit(self.gpa);
37643735 self.stack = parent_stack;
......@@ -3876,7 +3847,7 @@ fn isNull(self: *Self, ty: Type, operand: MCValue) !MCValue {
38763847 } },
38773848 });
38783849
3879 return MCValue{ .compare_flags_unsigned = .eq };
3850 return MCValue{ .cpsr_flags = .eq };
38803851 } else {
38813852 return self.fail("TODO implement non-pointer optionals", .{});
38823853 }
......@@ -3884,9 +3855,9 @@ fn isNull(self: *Self, ty: Type, operand: MCValue) !MCValue {
38843855
38853856fn isNonNull(self: *Self, ty: Type, operand: MCValue) !MCValue {
38863857 const is_null_result = try self.isNull(ty, operand);
3887 assert(is_null_result.compare_flags_unsigned == .eq);
3858 assert(is_null_result.cpsr_flags == .eq);
38883859
3889 return MCValue{ .compare_flags_unsigned = .neq };
3860 return MCValue{ .cpsr_flags = .ne };
38903861}
38913862
38923863fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
......@@ -3899,15 +3870,15 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
38993870
39003871 const error_mcv = try self.errUnionErr(operand, ty);
39013872 _ = try self.binOp(.cmp_eq, error_mcv, .{ .immediate = 0 }, error_int_type, error_int_type, null);
3902 return MCValue{ .compare_flags_unsigned = .gt };
3873 return MCValue{ .cpsr_flags = .hi };
39033874}
39043875
39053876fn isNonErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
39063877 const is_err_result = try self.isErr(ty, operand);
39073878 switch (is_err_result) {
3908 .compare_flags_unsigned => |op| {
3909 assert(op == .gt);
3910 return MCValue{ .compare_flags_unsigned = .lte };
3879 .cpsr_flags => |cond| {
3880 assert(cond == .hi);
3881 return MCValue{ .cpsr_flags = cond.negate() };
39113882 },
39123883 .immediate => |imm| {
39133884 assert(imm == 0);
......@@ -3921,7 +3892,7 @@ fn airIsNull(self: *Self, inst: Air.Inst.Index) !void {
39213892 const un_op = self.air.instructions.items(.data)[inst].un_op;
39223893
39233894 try self.spillCompareFlagsIfOccupied();
3924 self.compare_flags_inst = inst;
3895 self.cpsr_flags_inst = inst;
39253896
39263897 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
39273898 const operand = try self.resolveInst(un_op);
......@@ -4298,8 +4269,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
42984269 else => return self.fail("TODO implement memset", .{}),
42994270 }
43004271 },
4301 .compare_flags_unsigned,
4302 .compare_flags_signed,
4272 .cpsr_flags,
43034273 .immediate,
43044274 .ptr_stack_offset,
43054275 => {
......@@ -4469,15 +4439,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
44694439 } },
44704440 });
44714441 },
4472 .compare_flags_unsigned,
4473 .compare_flags_signed,
4474 => |op| {
4475 const condition = switch (mcv) {
4476 .compare_flags_unsigned => Condition.fromCompareOperatorUnsigned(op),
4477 .compare_flags_signed => Condition.fromCompareOperatorSigned(op),
4478 else => unreachable,
4479 };
4480
4442 .cpsr_flags => |condition| {
44814443 const zero = Instruction.Operand.imm(0, 0);
44824444 const one = Instruction.Operand.imm(1, 0);
44834445
......@@ -4826,8 +4788,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I
48264788 try self.genInlineMemcpy(src_reg, dst_reg, len_reg, count_reg, tmp_reg);
48274789 }
48284790 },
4829 .compare_flags_unsigned,
4830 .compare_flags_signed,
4791 .cpsr_flags,
48314792 .immediate,
48324793 .ptr_stack_offset,
48334794 => {