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 {...@@ -2581,16 +2581,10 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
2581 self.arg_index += 1;2581 self.arg_index += 1;
25822582
2583 const mcv = self.args[arg_index];2583 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;
2590 const payload = try self.addExtra(Mir.ArgDbgInfo{2584 const payload = try self.addExtra(Mir.ArgDbgInfo{
2591 .air_inst = inst,2585 .air_inst = inst,
2592 .arg_index = arg_index,2586 .arg_index = arg_index,
2593 .max_stack = @intCast(u32, max_stack),2587 .max_stack = self.max_end_stack,
2594 });2588 });
2595 _ = try self.addInst(.{2589 _ = try self.addInst(.{
2596 .tag = .arg_dbg_info,2590 .tag = .arg_dbg_info,
...@@ -2607,7 +2601,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {...@@ -2607,7 +2601,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
2607 break :blk mcv;2601 break :blk mcv;
2608 },2602 },
2609 .stack_offset => |off| {2603 .stack_offset => |off| {
2610 const offset = max_stack - off + 16;2604 const offset = @intCast(i32, self.max_end_stack) - off + 16;
2611 break :blk MCValue{ .stack_offset = -offset };2605 break :blk MCValue{ .stack_offset = -offset };
2612 },2606 },
2613 else => return self.fail("TODO implement arg for {}", .{mcv}),2607 else => return self.fail("TODO implement arg for {}", .{mcv}),
...@@ -2651,7 +2645,6 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {...@@ -2651,7 +2645,6 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {
2651 var info = try self.resolveCallingConventionValues(fn_ty);2645 var info = try self.resolveCallingConventionValues(fn_ty);
2652 defer info.deinit(self);2646 defer info.deinit(self);
26532647
2654 var stack_adjustment: ?u32 = null;
2655 for (args) |arg, arg_i| {2648 for (args) |arg, arg_i| {
2656 const mc_arg = info.args[arg_i];2649 const mc_arg = info.args[arg_i];
2657 const arg_ty = self.air.typeOf(arg);2650 const arg_ty = self.air.typeOf(arg);
...@@ -2666,9 +2659,6 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {...@@ -2666,9 +2659,6 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {
2666 },2659 },
2667 .stack_offset => |off| {2660 .stack_offset => |off| {
2668 try self.genSetStackArg(arg_ty, off, arg_mcv);2661 try self.genSetStackArg(arg_ty, off, arg_mcv);
2669 if (stack_adjustment == null) {
2670 stack_adjustment = @intCast(u32, off);
2671 }
2672 },2662 },
2673 .ptr_stack_offset => {2663 .ptr_stack_offset => {
2674 return self.fail("TODO implement calling with MCValue.ptr_stack_offset arg", .{});2664 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 {...@@ -2689,14 +2679,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {
2689 }2679 }
2690 }2680 }
26912681
2692 if (stack_adjustment) |off| {2682 if (info.stack_byte_count > 0) {
2693 // Adjust the stack2683 // Adjust the stack
2694 _ = try self.addInst(.{2684 _ = try self.addInst(.{
2695 .tag = .sub,2685 .tag = .sub,
2696 .ops = (Mir.Ops{2686 .ops = (Mir.Ops{
2697 .reg1 = .rsp,2687 .reg1 = .rsp,
2698 }).encode(),2688 }).encode(),
2699 .data = .{ .imm = off },2689 .data = .{ .imm = info.stack_byte_count },
2700 });2690 });
2701 }2691 }
27022692
...@@ -2824,14 +2814,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {...@@ -2824,14 +2814,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void {
2824 }2814 }
2825 } else unreachable;2815 } else unreachable;
28262816
2827 if (stack_adjustment) |off| {2817 if (info.stack_byte_count > 0) {
2828 // Readjust the stack2818 // Readjust the stack
2829 _ = try self.addInst(.{2819 _ = try self.addInst(.{
2830 .tag = .add,2820 .tag = .add,
2831 .ops = (Mir.Ops{2821 .ops = (Mir.Ops{
2832 .reg1 = .rsp,2822 .reg1 = .rsp,
2833 }).encode(),2823 }).encode(),
2834 .data = .{ .imm = off },2824 .data = .{ .imm = info.stack_byte_count },
2835 });2825 });
2836 }2826 }
28372827
...@@ -4847,8 +4837,8 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues {...@@ -4847,8 +4837,8 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues {
4847 }4837 }
4848 }4838 }
48494839
4850 result.stack_byte_count = next_stack_offset;
4851 result.stack_align = 16;4840 result.stack_align = 16;
4841 result.stack_byte_count = mem.alignForwardGeneric(u32, next_stack_offset, result.stack_align);
4852 },4842 },
4853 else => return self.fail("TODO implement function parameters for {} on x86_64", .{cc}),4843 else => return self.fail("TODO implement function parameters for {} on x86_64", .{cc}),
4854 }4844 }