authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-18 13:02:08+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-18 13:02:08+01:00
log0f0bb7e5ea2aa4216dcbec57086d2d5c7a84625e
treebb845187b51921f44e71347127a2b34b13e1dfd1
parentd74e9b2d98d00b0e9ae0196c0bb3272b2de2b52e

x64: ensure 16byte stack alignment across calls


1 files changed, 7 insertions(+), 17 deletions(-)

src/arch/x86_64/CodeGen.zig+7-17
......@@ -2581,16 +2581,10 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
25812581 self.arg_index += 1;
25822582
25832583 const mcv = self.args[arg_index];
2584 const max_stack = loop: for (self.args) |arg| {
2585 switch (arg) {
2586 .stack_offset => |last| break :loop last,
2587 else => {},
2588 }
2589 } else 0;
25902584 const payload = try self.addExtra(Mir.ArgDbgInfo{
25912585 .air_inst = inst,
25922586 .arg_index = arg_index,
2593 .max_stack = @intCast(u32, max_stack),
2587 .max_stack = self.max_end_stack,
25942588 });
25952589 _ = try self.addInst(.{
25962590 .tag = .arg_dbg_info,
......@@ -2607,7 +2601,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
26072601 break :blk mcv;
26082602 },
26092603 .stack_offset => |off| {
2610 const offset = max_stack - off + 16;
2604 const offset = @intCast(i32, self.max_end_stack) - off + 16;
26112605 break :blk MCValue{ .stack_offset = -offset };
26122606 },
26132607 else => return self.fail("TODO implement arg for {}", .{mcv}),
......@@ -2651,7 +2645,6 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {
26512645 var info = try self.resolveCallingConventionValues(fn_ty);
26522646 defer info.deinit(self);
26532647
2654 var stack_adjustment: ?u32 = null;
26552648 for (args) |arg, arg_i| {
26562649 const mc_arg = info.args[arg_i];
26572650 const arg_ty = self.air.typeOf(arg);
......@@ -2666,9 +2659,6 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {
26662659 },
26672660 .stack_offset => |off| {
26682661 try self.genSetStackArg(arg_ty, off, arg_mcv);
2669 if (stack_adjustment == null) {
2670 stack_adjustment = @intCast(u32, off);
2671 }
26722662 },
26732663 .ptr_stack_offset => {
26742664 return self.fail("TODO implement calling with MCValue.ptr_stack_offset arg", .{});
......@@ -2689,14 +2679,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {
26892679 }
26902680 }
26912681
2692 if (stack_adjustment) |off| {
2682 if (info.stack_byte_count > 0) {
26932683 // Adjust the stack
26942684 _ = try self.addInst(.{
26952685 .tag = .sub,
26962686 .ops = (Mir.Ops{
26972687 .reg1 = .rsp,
26982688 }).encode(),
2699 .data = .{ .imm = off },
2689 .data = .{ .imm = info.stack_byte_count },
27002690 });
27012691 }
27022692
......@@ -2824,14 +2814,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {
28242814 }
28252815 } else unreachable;
28262816
2827 if (stack_adjustment) |off| {
2817 if (info.stack_byte_count > 0) {
28282818 // Readjust the stack
28292819 _ = try self.addInst(.{
28302820 .tag = .add,
28312821 .ops = (Mir.Ops{
28322822 .reg1 = .rsp,
28332823 }).encode(),
2834 .data = .{ .imm = off },
2824 .data = .{ .imm = info.stack_byte_count },
28352825 });
28362826 }
28372827
......@@ -4847,8 +4837,8 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues {
48474837 }
48484838 }
48494839
4850 result.stack_byte_count = next_stack_offset;
48514840 result.stack_align = 16;
4841 result.stack_byte_count = mem.alignForwardGeneric(u32, next_stack_offset, result.stack_align);
48524842 },
48534843 else => return self.fail("TODO implement function parameters for {} on x86_64", .{cc}),
48544844 }