authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-03-25 19:16:58+01:00
committergravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-03-25 19:21:34+01:00
logf7da4b9bc802b66d409413596204df355604fc67
tree5a5c88e0d6abf5f2303ca5d7b407d79a8cd996ca
parent16e49663329007c7f9c33086f4938c1450b1ebbd
signature Commit is signed but in an unrecognized format.

stage2 AArch64: change semantics of MCValue.stack_offset

Mirrors the changes performed to stack_offset in the ARM backend

1 files changed, 35 insertions(+), 37 deletions(-)

src/arch/aarch64/CodeGen.zig+35-37
......@@ -809,10 +809,9 @@ fn allocMem(self: *Self, inst: Air.Inst.Index, abi_size: u32, abi_align: u32) !u
809809 if (abi_align > self.stack_align)
810810 self.stack_align = abi_align;
811811 // TODO find a free slot instead of always appending
812 const offset = mem.alignForwardGeneric(u32, self.next_stack_offset, abi_align);
813 self.next_stack_offset = offset + abi_size;
814 if (self.next_stack_offset > self.max_end_stack)
815 self.max_end_stack = self.next_stack_offset;
812 const offset = mem.alignForwardGeneric(u32, self.next_stack_offset, abi_align) + abi_size;
813 self.next_stack_offset = offset;
814 self.max_end_stack = @maximum(self.max_end_stack, self.next_stack_offset);
816815 try self.stack.putNoClobber(self.gpa, offset, .{
817816 .inst = inst,
818817 .size = abi_size,
......@@ -825,9 +824,10 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 {
825824 const elem_ty = self.air.typeOfIndex(inst).elemType();
826825
827826 if (!elem_ty.hasRuntimeBits()) {
828 // As this stack item will never be dereferenced at runtime,
829 // return the current stack offset
830 return self.next_stack_offset;
827 // return the stack offset 0. Stack offset 0 will be where all
828 // zero-sized stack allocations live as non-zero-sized
829 // allocations will always have an offset > 0.
830 return @as(u32, 0);
831831 }
832832
833833 const target = self.target.*;
......@@ -1097,8 +1097,8 @@ fn airSlice(self: *Self, inst: Air.Inst.Index) !void {
10971097 const ptr_bytes = @divExact(ptr_bits, 8);
10981098
10991099 const stack_offset = try self.allocMem(inst, ptr_bytes * 2, ptr_bytes * 2);
1100 try self.genSetStack(ptr_ty, stack_offset + ptr_bytes, ptr);
1101 try self.genSetStack(len_ty, stack_offset, len);
1100 try self.genSetStack(ptr_ty, stack_offset, ptr);
1101 try self.genSetStack(len_ty, stack_offset - ptr_bytes, len);
11021102 break :result MCValue{ .stack_offset = stack_offset };
11031103 };
11041104 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
......@@ -1515,7 +1515,13 @@ fn binOp(
15151515 const elem_size = elem_ty.abiSize(self.target.*);
15161516
15171517 if (elem_size == 1) {
1518 return try self.binOpRegister(tag, maybe_inst, lhs, rhs, lhs_ty, rhs_ty);
1518 const base_tag: Air.Inst.Tag = switch (tag) {
1519 .ptr_add => .add,
1520 .ptr_sub => .sub,
1521 else => unreachable,
1522 };
1523
1524 return try self.binOpRegister(base_tag, maybe_inst, lhs, rhs, lhs_ty, rhs_ty);
15191525 } else {
15201526 // convert the offset into a byte offset by
15211527 // multiplying it with elem_size
......@@ -1724,14 +1730,12 @@ fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void {
17241730fn airSlicePtr(self: *Self, inst: Air.Inst.Index) !void {
17251731 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
17261732 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
1727 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
1728 const ptr_bytes = @divExact(ptr_bits, 8);
17291733 const mcv = try self.resolveInst(ty_op.operand);
17301734 switch (mcv) {
17311735 .dead, .unreach, .none => unreachable,
17321736 .register => unreachable, // a slice doesn't fit in one register
17331737 .stack_offset => |off| {
1734 break :result MCValue{ .stack_offset = off + ptr_bytes };
1738 break :result MCValue{ .stack_offset = off };
17351739 },
17361740 .memory => |addr| {
17371741 break :result MCValue{ .memory = addr };
......@@ -1752,7 +1756,7 @@ fn airSliceLen(self: *Self, inst: Air.Inst.Index) !void {
17521756 .dead, .unreach, .none => unreachable,
17531757 .register => unreachable, // a slice doesn't fit in one register
17541758 .stack_offset => |off| {
1755 break :result MCValue{ .stack_offset = off };
1759 break :result MCValue{ .stack_offset = off - ptr_bytes };
17561760 },
17571761 .memory => |addr| {
17581762 break :result MCValue{ .memory = addr + ptr_bytes };
......@@ -1772,7 +1776,7 @@ fn airPtrSliceLenPtr(self: *Self, inst: Air.Inst.Index) !void {
17721776 switch (mcv) {
17731777 .dead, .unreach, .none => unreachable,
17741778 .ptr_stack_offset => |off| {
1775 break :result MCValue{ .ptr_stack_offset = off + ptr_bytes };
1779 break :result MCValue{ .ptr_stack_offset = off - ptr_bytes };
17761780 },
17771781 else => return self.fail("TODO implement ptr_slice_len_ptr for {}", .{mcv}),
17781782 }
......@@ -1819,7 +1823,7 @@ fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) !void {
18191823 defer if (index_is_register) self.register_manager.unfreezeRegs(&.{index_mcv.register});
18201824
18211825 const base_mcv: MCValue = switch (slice_mcv) {
1822 .stack_offset => |off| .{ .register = try self.copyToTmpRegister(slice_ptr_field_type, .{ .stack_offset = off + 8 }) },
1826 .stack_offset => |off| .{ .register = try self.copyToTmpRegister(slice_ptr_field_type, .{ .stack_offset = off }) },
18231827 else => return self.fail("TODO slice_elem_val when slice is {}", .{slice_mcv}),
18241828 };
18251829 self.register_manager.freezeRegs(&.{base_mcv.register});
......@@ -2293,13 +2297,10 @@ fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, inde
22932297 const mcv = try self.resolveInst(operand);
22942298 const ptr_ty = self.air.typeOf(operand);
22952299 const struct_ty = ptr_ty.childType();
2296 const struct_size = @intCast(u32, struct_ty.abiSize(self.target.*));
22972300 const struct_field_offset = @intCast(u32, struct_ty.structFieldOffset(index, self.target.*));
2298 const struct_field_ty = struct_ty.structFieldType(index);
2299 const struct_field_size = @intCast(u32, struct_field_ty.abiSize(self.target.*));
23002301 switch (mcv) {
23012302 .ptr_stack_offset => |off| {
2302 break :result MCValue{ .ptr_stack_offset = off + struct_size - struct_field_offset - struct_field_size };
2303 break :result MCValue{ .ptr_stack_offset = off - struct_field_offset };
23032304 },
23042305 else => {
23052306 const offset_reg = try self.copyToTmpRegister(ptr_ty, .{
......@@ -2621,11 +2622,15 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void {
26212622 .Pointer => Type.usize,
26222623 .ErrorSet => Type.initTag(.u16),
26232624 .Optional => blk: {
2624 if (lhs_ty.isPtrLikeOptional()) {
2625 var opt_buffer: Type.Payload.ElemType = undefined;
2626 const payload_ty = lhs_ty.optionalChild(&opt_buffer);
2627 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
2628 break :blk Type.initTag(.u1);
2629 } else if (lhs_ty.isPtrLikeOptional()) {
26252630 break :blk Type.usize;
2631 } else {
2632 return self.fail("TODO AArch64 cmp non-pointer optionals", .{});
26262633 }
2627
2628 return self.fail("TODO AArch64 cmp optionals", .{});
26292634 },
26302635 .Float => return self.fail("TODO AArch64 cmp floats", .{}),
26312636 else => unreachable,
......@@ -3318,8 +3323,6 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
33183323 return self.genSetStack(ty, stack_offset, MCValue{ .register = reg });
33193324 },
33203325 .register => |reg| {
3321 const adj_off = stack_offset + abi_size;
3322
33233326 switch (abi_size) {
33243327 1, 2, 4, 8 => {
33253328 const tag: Mir.Inst.Tag = switch (abi_size) {
......@@ -3334,7 +3337,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
33343337 .tag = tag,
33353338 .data = .{ .load_store_stack = .{
33363339 .rt = rt,
3337 .offset = @intCast(u32, adj_off),
3340 .offset = @intCast(u32, stack_offset),
33383341 } },
33393342 });
33403343 },
......@@ -3430,13 +3433,9 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
34303433 else => unreachable, // unexpected register size
34313434 }
34323435 },
3433 .ptr_stack_offset => |unadjusted_off| {
3436 .ptr_stack_offset => |off| {
34343437 // TODO: maybe addressing from sp instead of fp
3435 const elem_ty = ty.childType();
3436 const abi_size = elem_ty.abiSize(self.target.*);
3437 const adj_off = unadjusted_off + abi_size;
3438
3439 const imm12 = math.cast(u12, adj_off) catch
3438 const imm12 = math.cast(u12, off) catch
34403439 return self.fail("TODO larger stack offsets", .{});
34413440
34423441 _ = try self.addInst(.{
......@@ -3526,9 +3525,8 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
35263525 try self.genSetReg(ty, reg, .{ .immediate = addr });
35273526 try self.genLdrRegister(reg, reg, ty.abiSize(self.target.*));
35283527 },
3529 .stack_offset => |unadjusted_off| {
3528 .stack_offset => |off| {
35303529 const abi_size = ty.abiSize(self.target.*);
3531 const adj_off = unadjusted_off + abi_size;
35323530
35333531 switch (abi_size) {
35343532 1, 2, 4, 8 => {
......@@ -3548,7 +3546,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
35483546 .tag = tag,
35493547 .data = .{ .load_store_stack = .{
35503548 .rt = rt,
3551 .offset = @intCast(u32, adj_off),
3549 .offset = @intCast(u32, off),
35523550 } },
35533551 });
35543552 },
......@@ -3583,8 +3581,8 @@ fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void {
35833581 const ptr_bytes = @divExact(ptr_bits, 8);
35843582
35853583 const stack_offset = try self.allocMem(inst, ptr_bytes * 2, ptr_bytes * 2);
3586 try self.genSetStack(ptr_ty, stack_offset + ptr_bytes, ptr);
3587 try self.genSetStack(Type.initTag(.usize), stack_offset, .{ .immediate = array_len });
3584 try self.genSetStack(ptr_ty, stack_offset, ptr);
3585 try self.genSetStack(Type.initTag(.usize), stack_offset - ptr_bytes, .{ .immediate = array_len });
35883586 break :result MCValue{ .stack_offset = stack_offset };
35893587 };
35903588 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });