From aedbf56c3af4914e3f183223ff879734683bec73 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 5 Aug 2019 08:35:49 +0900 Subject: [PATCH] Make sure that stack frames are aligned to 16 bytes boundaries --- codegen.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/codegen.c b/codegen.c index db24cd1..737a771 100644 --- a/codegen.c +++ b/codegen.c @@ -112,7 +112,22 @@ void gen(Node *node) { for (int i = nargs - 1; i >= 0; i--) printf(" pop %s\n", argreg[i]); + // We need to align RSP to a 16 byte boundary before + // calling a function because it is an ABI requirement. + // RAX is set to 0 for variadic function. + int seq = labelseq++; + printf(" mov rax, rsp\n"); + printf(" and rax, 15\n"); + printf(" jnz .Lcall%d\n", seq); + printf(" mov rax, 0\n"); + printf(" call %s\n", node->funcname); + printf(" jmp .Lend%d\n", seq); + printf(".Lcall%d:\n", seq); + printf(" sub rsp, 8\n"); + printf(" mov rax, 0\n"); printf(" call %s\n", node->funcname); + printf(" add rsp, 8\n"); + printf(".Lend%d:\n", seq); printf(" push rax\n"); return; } -- GitLab