aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-05 02:08:13 -0500
committerMustafa Quraish <[email protected]>2022-02-05 08:56:15 -0500
commit20d6e4f5710c3260eaab6a9b836acef64a9f9611 (patch)
treeac9e14d174ade66236ac8352446ec4e146f50799
parentAdd `OS_IS_MACOS` and `OS_IS_LINUX` constants as builtins (diff)
downloadcup-20d6e4f5710c3260eaab6a9b836acef64a9f9611.tar.xz
cup-20d6e4f5710c3260eaab6a9b836acef64a9f9611.zip
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`
-rw-r--r--src/generator.c17
1 files changed, 13 insertions, 4 deletions
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++) {