aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-01-29 22:26:17 -0500
committerMustafa Quraish <[email protected]>2022-01-29 22:26:57 -0500
commit76b80dd7f4423b37753031a6ef061ef08c570c06 (patch)
treed20e63f18fcf826245d3c9e8c855cd5bb78d0cd9
parentAdd for and while loop support (w/o declarations in `for`) (diff)
downloadcup-76b80dd7f4423b37753031a6ef061ef08c570c06.tar.xz
cup-76b80dd7f4423b37753031a6ef061ef08c570c06.zip
Make the compiler / scripts work on Linux too (yay!)
-rwxr-xr-xassemble.sh17
-rw-r--r--cup/generator.c12
-rw-r--r--tests/common.sh17
3 files changed, 40 insertions, 6 deletions
diff --git a/assemble.sh b/assemble.sh
index 90f945f..ed9b46e 100755
--- a/assemble.sh
+++ b/assemble.sh
@@ -8,7 +8,18 @@ then
exit 1
fi
-set -xe
-nasm -f macho64 -o $1.o $1
-ld -lSystem $1.o
+case "$(uname -s)" in
+ Darwin)
+ set -xe
+ nasm -f macho64 -o $1.o $1
+ ld -lSystem $1.o
+ set +xe
+ ;;
+ Linux)
+ set -xe
+ nasm -f elf64 -o $1.o $1
+ ld $1.o
+ set +xe
+ ;;
+esac \ No newline at end of file
diff --git a/cup/generator.c b/cup/generator.c
index d7b252d..61e8c69 100644
--- a/cup/generator.c
+++ b/cup/generator.c
@@ -307,10 +307,22 @@ void generate_asm(Node *root, FILE *out)
}
// Call `main` from `_main` and return
+#if __APPLE__
fprintf(out, "global _main\n");
fprintf(out, "_main:\n");
+#else
+ fprintf(out, "global _start\n");
+ fprintf(out, "_start:\n");
+#endif
fprintf(out, " call main\n");
+
+#if __APPLE__
fprintf(out, " ret\n");
+#else
+ fprintf(out, " mov rdi, rax\n");
+ fprintf(out, " mov rax, %d\n", SYS_exit);
+ fprintf(out, " syscall\n");
+#endif
// TODO: Add implementations of some primitives?
} \ No newline at end of file
diff --git a/tests/common.sh b/tests/common.sh
index 80e0bea..f863826 100644
--- a/tests/common.sh
+++ b/tests/common.sh
@@ -1,9 +1,20 @@
#!/bin/bash
function assemble() {
- # Note: macOS only for now, abstract this out later
- nasm -f macho64 -o output.o output.nasm
- ld -lSystem output.o
+ case "$(uname -s)" in
+ Darwin)
+ set -e
+ nasm -f macho64 -o output.nasm.o output.nasm
+ ld -lSystem output.nasm.o
+ set +e
+ ;;
+ Linux)
+ set -e
+ nasm -f elf64 -o output.nasm.o output.nasm
+ ld output.nasm.o
+ set +e
+ ;;
+ esac
}
function assert_exit_status() {