authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2022-03-20 13:48:14+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-22 20:16:05-07:00
log6ac04d8fd7d63dfd4aa611e1564f1d4768baf408
treec4d866f3f6539da2f7b2ae6f6d7fd15fadcee0ed
parente8813b296bc55a13b534bd9b2a03e1f6af366915

stage2 ARM: change semantics of MCValue.stack_offset

A stack_offset will now denote the exact offset applied to the start of the stack frame (=fp when frame pointer is emitted)

1 files changed, 44 insertions(+), 68 deletions(-)

src/arch/arm/CodeGen.zig+44-68
......@@ -396,8 +396,8 @@ fn gen(self: *Self) !void {
396396 // The address of where to store the return value is in
397397 // r0. As this register might get overwritten along the
398398 // way, save the address to the stack.
399 const stack_offset = mem.alignForwardGeneric(u32, self.next_stack_offset, 4);
400 self.next_stack_offset = stack_offset + 4;
399 const stack_offset = mem.alignForwardGeneric(u32, self.next_stack_offset, 4) + 4;
400 self.next_stack_offset = stack_offset;
401401 self.max_end_stack = @maximum(self.max_end_stack, self.next_stack_offset);
402402
403403 try self.genSetStack(Type.usize, stack_offset, MCValue{ .register = .r0 });
......@@ -780,10 +780,9 @@ fn allocMem(self: *Self, inst: Air.Inst.Index, abi_size: u32, abi_align: u32) !u
780780 if (abi_align > self.stack_align)
781781 self.stack_align = abi_align;
782782 // TODO find a free slot instead of always appending
783 const offset = mem.alignForwardGeneric(u32, self.next_stack_offset, abi_align);
784 self.next_stack_offset = offset + abi_size;
785 if (self.next_stack_offset > self.max_end_stack)
786 self.max_end_stack = self.next_stack_offset;
783 const offset = mem.alignForwardGeneric(u32, self.next_stack_offset, abi_align) + abi_size;
784 self.next_stack_offset = offset;
785 self.max_end_stack = @maximum(self.max_end_stack, self.next_stack_offset);
787786 try self.stack.putNoClobber(self.gpa, offset, .{
788787 .inst = inst,
789788 .size = abi_size,
......@@ -797,8 +796,10 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 {
797796
798797 if (!elem_ty.hasRuntimeBits()) {
799798 // As this stack item will never be dereferenced at runtime,
800 // return the current stack offset
801 return self.next_stack_offset;
799 // return the stack offset 0. Stack offset 0 will be where all
800 // zero-sized stack allocations live as non-zero-sized
801 // allocations will always have an offset > 0.
802 return @as(u32, 0);
802803 }
803804
804805 const target = self.target.*;
......@@ -1161,8 +1162,8 @@ fn airSlice(self: *Self, inst: Air.Inst.Index) !void {
11611162 const len_ty = self.air.typeOf(bin_op.rhs);
11621163
11631164 const stack_offset = try self.allocMem(inst, 8, 8);
1164 try self.genSetStack(ptr_ty, stack_offset + 4, ptr);
1165 try self.genSetStack(len_ty, stack_offset, len);
1165 try self.genSetStack(ptr_ty, stack_offset, ptr);
1166 try self.genSetStack(len_ty, stack_offset - 4, len);
11661167 break :result MCValue{ .stack_offset = stack_offset };
11671168 };
11681169 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
......@@ -1180,36 +1181,18 @@ fn airBinOp(self: *Self, inst: Air.Inst.Index) !void {
11801181 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
11811182}
11821183
1183fn airAddWrap(self: *Self, inst: Air.Inst.Index) !void {
1184 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
1185 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement addwrap for {}", .{self.target.cpu.arch});
1186 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
1187}
1188
11891184fn airAddSat(self: *Self, inst: Air.Inst.Index) !void {
11901185 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
11911186 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement add_sat for {}", .{self.target.cpu.arch});
11921187 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
11931188}
11941189
1195fn airSubWrap(self: *Self, inst: Air.Inst.Index) !void {
1196 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
1197 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement subwrap for {}", .{self.target.cpu.arch});
1198 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
1199}
1200
12011190fn airSubSat(self: *Self, inst: Air.Inst.Index) !void {
12021191 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
12031192 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement sub_sat for {}", .{self.target.cpu.arch});
12041193 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
12051194}
12061195
1207fn airMulWrap(self: *Self, inst: Air.Inst.Index) !void {
1208 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
1209 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement mulwrap for {}", .{self.target.cpu.arch});
1210 return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none });
1211}
1212
12131196fn airMulSat(self: *Self, inst: Air.Inst.Index) !void {
12141197 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
12151198 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else return self.fail("TODO implement mul_sat for {}", .{self.target.cpu.arch});
......@@ -1327,12 +1310,14 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void {
13271310 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
13281311 const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: {
13291312 const optional_ty = self.air.typeOfIndex(inst);
1313 const abi_size = @intCast(u32, optional_ty.abiSize(self.target.*));
13301314
13311315 // Optional with a zero-bit payload type is just a boolean true
1332 if (optional_ty.abiSize(self.target.*) == 1)
1316 if (abi_size == 1) {
13331317 break :result MCValue{ .immediate = 1 };
1334
1335 return self.fail("TODO implement wrap optional for {}", .{self.target.cpu.arch});
1318 } else {
1319 return self.fail("TODO implement wrap optional for {}", .{self.target.cpu.arch});
1320 }
13361321 };
13371322 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });
13381323}
......@@ -1366,7 +1351,7 @@ fn slicePtr(self: *Self, mcv: MCValue) !MCValue {
13661351 return MCValue{ .stack_argument_offset = off + 4 };
13671352 },
13681353 .stack_offset => |off| {
1369 return MCValue{ .stack_offset = off + 4 };
1354 return MCValue{ .stack_offset = off };
13701355 },
13711356 .memory => |addr| {
13721357 return MCValue{ .memory = addr };
......@@ -1395,7 +1380,7 @@ fn airSliceLen(self: *Self, inst: Air.Inst.Index) !void {
13951380 break :result MCValue{ .stack_argument_offset = off };
13961381 },
13971382 .stack_offset => |off| {
1398 break :result MCValue{ .stack_offset = off };
1383 break :result MCValue{ .stack_offset = off - 4 };
13991384 },
14001385 .memory => |addr| {
14011386 break :result MCValue{ .memory = addr + 4 };
......@@ -1413,7 +1398,7 @@ fn airPtrSliceLenPtr(self: *Self, inst: Air.Inst.Index) !void {
14131398 switch (mcv) {
14141399 .dead, .unreach => unreachable,
14151400 .ptr_stack_offset => |off| {
1416 break :result MCValue{ .ptr_stack_offset = off };
1401 break :result MCValue{ .ptr_stack_offset = off - 4 };
14171402 },
14181403 else => return self.fail("TODO implement ptr_slice_len_ptr for {}", .{mcv}),
14191404 }
......@@ -1428,7 +1413,7 @@ fn airPtrSlicePtrPtr(self: *Self, inst: Air.Inst.Index) !void {
14281413 switch (mcv) {
14291414 .dead, .unreach => unreachable,
14301415 .ptr_stack_offset => |off| {
1431 break :result MCValue{ .ptr_stack_offset = off + 4 };
1416 break :result MCValue{ .ptr_stack_offset = off };
14321417 },
14331418 else => return self.fail("TODO implement ptr_slice_ptr_ptr for {}", .{mcv}),
14341419 }
......@@ -1860,13 +1845,10 @@ fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, inde
18601845 const mcv = try self.resolveInst(operand);
18611846 const ptr_ty = self.air.typeOf(operand);
18621847 const struct_ty = ptr_ty.childType();
1863 const struct_size = @intCast(u32, struct_ty.abiSize(self.target.*));
18641848 const struct_field_offset = @intCast(u32, struct_ty.structFieldOffset(index, self.target.*));
1865 const struct_field_ty = struct_ty.structFieldType(index);
1866 const struct_field_size = @intCast(u32, struct_field_ty.abiSize(self.target.*));
18671849 switch (mcv) {
18681850 .ptr_stack_offset => |off| {
1869 break :result MCValue{ .ptr_stack_offset = off + struct_size - struct_field_offset - struct_field_size };
1851 break :result MCValue{ .ptr_stack_offset = off - struct_field_offset };
18701852 },
18711853 else => {
18721854 const offset_reg = try self.copyToTmpRegister(ptr_ty, .{
......@@ -1914,7 +1896,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {
19141896 break :result MCValue{ .stack_argument_offset = off + adjusted_field_offset };
19151897 },
19161898 .stack_offset => |off| {
1917 break :result MCValue{ .stack_offset = off + adjusted_field_offset };
1899 break :result MCValue{ .stack_offset = off - struct_field_offset };
19181900 },
19191901 .memory => |addr| {
19201902 break :result MCValue{ .memory = addr + adjusted_field_offset };
......@@ -2871,10 +2853,9 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void {
28712853 if (abi_align > self.stack_align)
28722854 self.stack_align = abi_align;
28732855 // TODO find a free slot instead of always appending
2874 const offset = mem.alignForwardGeneric(u32, self.next_stack_offset, abi_align);
2875 self.next_stack_offset = offset + abi_size;
2876 if (self.next_stack_offset > self.max_end_stack)
2877 self.max_end_stack = self.next_stack_offset;
2856 const offset = mem.alignForwardGeneric(u32, self.next_stack_offset, abi_align) + abi_size;
2857 self.next_stack_offset = offset;
2858 self.max_end_stack = @maximum(self.max_end_stack, self.next_stack_offset);
28782859
28792860 const tmp_mcv = MCValue{ .stack_offset = offset };
28802861 try self.load(tmp_mcv, ptr, ptr_ty);
......@@ -3192,7 +3173,9 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
31923173
31933174 if (!error_type.hasRuntimeBits()) {
31943175 return MCValue{ .immediate = 0 }; // always false
3195 } else if (!payload_type.hasRuntimeBits()) {
3176 }
3177
3178 if (!payload_type.hasRuntimeBits()) {
31963179 if (error_type.abiSize(self.target.*) <= 4) {
31973180 const reg_mcv: MCValue = switch (operand) {
31983181 .register => operand,
......@@ -3620,13 +3603,11 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
36203603 return self.genSetStack(ty, stack_offset, MCValue{ .register = reg });
36213604 },
36223605 .register => |reg| {
3623 const adj_off = stack_offset + abi_size;
3624
36253606 switch (abi_size) {
36263607 1, 4 => {
3627 const offset = if (math.cast(u12, adj_off)) |imm| blk: {
3608 const offset = if (math.cast(u12, stack_offset)) |imm| blk: {
36283609 break :blk Instruction.Offset.imm(imm);
3629 } else |_| Instruction.Offset.reg(try self.copyToTmpRegister(Type.initTag(.u32), MCValue{ .immediate = adj_off }), .none);
3610 } else |_| Instruction.Offset.reg(try self.copyToTmpRegister(Type.initTag(.u32), MCValue{ .immediate = stack_offset }), .none);
36303611
36313612 const tag: Mir.Inst.Tag = switch (abi_size) {
36323613 1 => .strb,
......@@ -3647,9 +3628,9 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro
36473628 });
36483629 },
36493630 2 => {
3650 const offset = if (adj_off <= math.maxInt(u8)) blk: {
3651 break :blk Instruction.ExtraLoadStoreOffset.imm(@intCast(u8, adj_off));
3652 } else Instruction.ExtraLoadStoreOffset.reg(try self.copyToTmpRegister(Type.initTag(.u32), MCValue{ .immediate = adj_off }));
3631 const offset = if (stack_offset <= math.maxInt(u8)) blk: {
3632 break :blk Instruction.ExtraLoadStoreOffset.imm(@intCast(u8, stack_offset));
3633 } else Instruction.ExtraLoadStoreOffset.reg(try self.copyToTmpRegister(Type.initTag(.u32), MCValue{ .immediate = stack_offset }));
36533634
36543635 _ = try self.addInst(.{
36553636 .tag = .strh,
......@@ -3739,13 +3720,9 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
37393720 // Write the debug undefined value.
37403721 return self.genSetReg(ty, reg, .{ .immediate = 0xaaaaaaaa });
37413722 },
3742 .ptr_stack_offset => |unadjusted_off| {
3723 .ptr_stack_offset => |off| {
37433724 // TODO: maybe addressing from sp instead of fp
3744 const elem_ty = ty.childType();
3745 const abi_size = @intCast(u32, elem_ty.abiSize(self.target.*));
3746 const adj_off = unadjusted_off + abi_size;
3747
3748 const op = Instruction.Operand.fromU32(adj_off) orelse
3725 const op = Instruction.Operand.fromU32(off) orelse
37493726 return self.fail("TODO larger stack offsets", .{});
37503727
37513728 _ = try self.addInst(.{
......@@ -3919,10 +3896,9 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
39193896 try self.genSetReg(ty, reg, .{ .immediate = @intCast(u32, addr) });
39203897 try self.genLdrRegister(reg, reg, ty);
39213898 },
3922 .stack_offset => |unadjusted_off| {
3899 .stack_offset => |off| {
39233900 // TODO: maybe addressing from sp instead of fp
39243901 const abi_size = @intCast(u32, ty.abiSize(self.target.*));
3925 const adj_off = unadjusted_off + abi_size;
39263902
39273903 const tag: Mir.Inst.Tag = switch (abi_size) {
39283904 1 => if (ty.isSignedInt()) Mir.Inst.Tag.ldrsb else .ldrb,
......@@ -3939,9 +3915,9 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
39393915 };
39403916
39413917 if (extra_offset) {
3942 const offset = if (adj_off <= math.maxInt(u8)) blk: {
3943 break :blk Instruction.ExtraLoadStoreOffset.imm(@intCast(u8, adj_off));
3944 } else Instruction.ExtraLoadStoreOffset.reg(try self.copyToTmpRegister(Type.initTag(.u32), MCValue{ .immediate = adj_off }));
3918 const offset = if (off <= math.maxInt(u8)) blk: {
3919 break :blk Instruction.ExtraLoadStoreOffset.imm(@intCast(u8, off));
3920 } else Instruction.ExtraLoadStoreOffset.reg(try self.copyToTmpRegister(Type.initTag(.usize), MCValue{ .immediate = off }));
39453921
39463922 _ = try self.addInst(.{
39473923 .tag = tag,
......@@ -3955,9 +3931,9 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void
39553931 } },
39563932 });
39573933 } else {
3958 const offset = if (adj_off <= math.maxInt(u12)) blk: {
3959 break :blk Instruction.Offset.imm(@intCast(u12, adj_off));
3960 } else Instruction.Offset.reg(try self.copyToTmpRegister(Type.initTag(.u32), MCValue{ .immediate = adj_off }), .none);
3934 const offset = if (off <= math.maxInt(u12)) blk: {
3935 break :blk Instruction.Offset.imm(@intCast(u12, off));
3936 } else Instruction.Offset.reg(try self.copyToTmpRegister(Type.initTag(.usize), MCValue{ .immediate = off }), .none);
39613937
39623938 _ = try self.addInst(.{
39633939 .tag = tag,
......@@ -4136,8 +4112,8 @@ fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void {
41364112 const array_len = @intCast(u32, array_ty.arrayLen());
41374113
41384114 const stack_offset = try self.allocMem(inst, 8, 8);
4139 try self.genSetStack(ptr_ty, stack_offset + 4, ptr);
4140 try self.genSetStack(Type.initTag(.usize), stack_offset, .{ .immediate = array_len });
4115 try self.genSetStack(ptr_ty, stack_offset, ptr);
4116 try self.genSetStack(Type.initTag(.usize), stack_offset - 4, .{ .immediate = array_len });
41414117 break :result MCValue{ .stack_offset = stack_offset };
41424118 };
41434119 return self.finishAir(inst, result, .{ ty_op.operand, .none, .none });