From 650de3195a9631c9f2c777df5ecf1c6ce26c0430 Mon Sep 17 00:00:00 2001 From: lemon Date: Sat, 15 Nov 2025 18:53:50 +0100 Subject: emit: stack alignment edgecases --- amd64/emit.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'amd64') diff --git a/amd64/emit.c b/amd64/emit.c index 30c0b99..d61c1d3 100644 --- a/amd64/emit.c +++ b/amd64/emit.c @@ -1252,17 +1252,21 @@ emitbin(struct function *fn) usebp = 1; /* push rbp; mov rbp, rsp */ DS("\x55\x48\x89\xE5"); - ++npush; } saverestore = calleesave(&npush, pcode, fn); - if (usebp) rbpoff = -(npush - 1)*8; + if (usebp) rbpoff = -npush*8; /* ensure stack is 16-byte aligned for function calls */ - if (!fn->isleaf && ((fn->stksiz + npush*8) & 0xF) != 0x8) { + if (!fn->isleaf && ((fn->stksiz + npush*8) & 0xF) != 0) { assert(usebp); - fn->stksiz += 8; + if ((rbpoff & 0xF) == 0) { + rbpoff -= 16; + fn->stksiz += 24; + } else { + rbpoff -= 8; + fn->stksiz += 8; + } } - rbpoff = alignup(rbpoff, 16); if (fn->stksiz != 0) { /* sub rsp, */ -- cgit v1.2.3