(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))))
+++ /dev/null
-#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;
-}
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)