From 9ee429534c3011b0ed413dc01e81db13f71ac884 Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Tue, 23 Jul 2019 08:57:52 +0100 Subject: [PATCH] Remove C dependency, directly emit _start --- codegen.scm | 14 ++++++++------ rts.c | 11 ----------- tests.scm | 14 +++++++++----- 3 files changed, 17 insertions(+), 22 deletions(-) delete mode 100644 rts.c diff --git a/codegen.scm b/codegen.scm index a19b53d..968a428 100644 --- a/codegen.scm +++ b/codegen.scm @@ -290,12 +290,14 @@ (for-each codegen-lambda lambdas) - (emit ".globl _scheme_entry") - (emit "_scheme_entry:") + (emit ".globl _start") + (emit "_start:") + (codegen-expr xform-prog 0 '()) - - (amd64-abi - (lambda () (codegen-expr xform-prog 0 '()))))) + ; exit syscall + (emit "mov %rax, %rdi") + (emit "mov $60, %rax") + (emit "syscall"))) (define (compile-to-binary program output) (when (not (eq? (typecheck program) 'int)) (error #f "not an int")) @@ -303,4 +305,4 @@ (when (file-exists? tmp-path) (delete-file tmp-path)) (with-output-to-file tmp-path (lambda () (codegen program))) - (system (format "clang -fomit-frame-pointer /tmp/a.s rts.c -o ~a" output)))) + (system (format "clang -nostdlib /tmp/a.s -o ~a" output)))) diff --git a/rts.c b/rts.c deleted file mode 100644 index 0678901..0000000 --- a/rts.c +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include - -__attribute__((__cdecl__)) -extern int scheme_entry(); - -int main(int argc, const char**argv) { - int val = scheme_entry(); - printf("%d\n", val); - return 0; -} diff --git a/tests.scm b/tests.scm index 56762c7..563a6e7 100644 --- a/tests.scm +++ b/tests.scm @@ -16,7 +16,11 @@ result (loop (read-char p) (string-append result (string next)))))))) -(define (test-prog prog output) +(define (test-prog prog exit-code) + (compile-to-binary prog "/tmp/test-prog") + (system "/tmp/test-prog")) + +(define (test-prog-stdout prog output) (compile-to-binary prog "/tmp/test-prog") (system "/tmp/test-prog > /tmp/test-output.txt") (let ((str (read-file "/tmp/test-output.txt"))) @@ -26,11 +30,11 @@ (test (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))) '(abs (abs int (abs int int)) int)) -(test-prog '(+ 1 2) "3") -(test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) "142") +(test-prog '(+ 1 2) 3) +(test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142) (test-prog '(let ((x (+ 1 32)) (y x)) ((lambda (z) (+ 1 z)) (* y x))) - "1090") -(test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) "31") + 1090) +(test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31) -- 2.30.2