From 20d6e4f5710c3260eaab6a9b836acef64a9f9611 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Sat, 5 Feb 2022 02:08:13 -0500 Subject: Handle command-line arguments properly on linux Turns out they're supposed to be accessed on the stack there. For macOS, because 16 byte-alignment is "required", we don't know the exact position of the arguments and that they're also passed in through `rdi` and `rsi` --- src/generator.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/generator.c b/src/generator.c index 23cc315..82c492b 100644 --- a/src/generator.c +++ b/src/generator.c @@ -477,16 +477,25 @@ void generate_asm(Node *root, FILE *out) #if __APPLE__ fprintf(out, "global _main\n"); fprintf(out, "_main:\n"); -#else - fprintf(out, "global _start\n"); - fprintf(out, "_start:\n"); -#endif // Push argv fprintf(out, " mov rax, rsi\n"); fprintf(out, " push rax\n"); // Push argc fprintf(out, " mov rax, rdi\n"); fprintf(out, " push rax\n"); +#else + fprintf(out, "global _start\n"); + fprintf(out, "_start:\n"); + + fprintf(out, " mov rbp, rsp\n"); + // // Push argv + fprintf(out, " mov rax, rbp\n"); + fprintf(out, " add rax, 8\n"); + fprintf(out, " push rax\n"); + // Push argc + fprintf(out, " mov rax, [rbp]\n"); + fprintf(out, " push rax\n"); +#endif // Initialize global variables for (int i = 0; i < root->block.num_children; i++) { -- cgit v1.2.3