From 76b80dd7f4423b37753031a6ef061ef08c570c06 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Sat, 29 Jan 2022 22:26:17 -0500 Subject: Make the compiler / scripts work on Linux too (yay!) --- assemble.sh | 17 ++++++++++++++--- cup/generator.c | 12 ++++++++++++ tests/common.sh | 17 ++++++++++++++--- 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() { -- cgit v1.2.3