Remove C dependency, directly emit _start
authorLuke Lau <luke.lau@intel.com>
Tue, 23 Jul 2019 07:57:52 +0000 (08:57 +0100)
committerLuke Lau <luke.lau@intel.com>
Tue, 23 Jul 2019 07:57:52 +0000 (08:57 +0100)
codegen.scm
rts.c [deleted file]
tests.scm

index a19b53df53fa239bc700a5d06cccfd86298d7601..968a428cd2f442bf820ea37c0a01a3e92047e4b5 100644 (file)
 
     (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"))
     (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 (file)
index 0678901..0000000
--- a/rts.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-__attribute__((__cdecl__))
-extern int scheme_entry();
-
-int main(int argc, const char**argv) {
-  int val = scheme_entry();
-  printf("%d\n", val);
-  return 0;
-}
index 56762c72124ee6c0f9bdbd3dd47754a5b73a8dd3..563a6e708e79a2abef0eb54b6c68b093be3ab333 100644 (file)
--- a/tests.scm
+++ b/tests.scm
             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")))
 (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)