Skip to content

Commit

Permalink
Make sure that stack frames are aligned to 16 bytes boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Aug 4, 2019
1 parent ed3fc29 commit ee42303
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,22 @@ static 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 .L.call.%d\n", seq);
printf(" mov rax, 0\n");
printf(" call %s\n", node->funcname);
printf(" jmp .L.end.%d\n", seq);
printf(".L.call.%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(".L.end.%d:\n", seq);
printf(" push rax\n");
return;
}
Expand Down

0 comments on commit ee42303

Please sign in to comment.