Fix some normalization issues, add codegen tests
[scheme.git] / codegen.scm
index 04816ab75d533b6b950ff7fe17813cfce1880383..d30adc54e61a144097c3b0bb541a5835af8c028a 100644 (file)
@@ -59,6 +59,8 @@
   (for-each (lambda (form) (codegen-expr form inner-si inner-env)) body)))
 
 (define (codegen-var name si env)
+  (when (not (assoc name env))
+    (error #f (format "Variable ~a is not bound" name)))
   (let ((offset (cdr (assoc name env))))
     (emit "movq ~a(%rsp), %rax" offset)))
 
     (amd64-abi
      (lambda () (codegen-expr xform-prog 0 '())))))
 
-(define (compile-to-binary program)
+(define (compile-to-binary program output)
   (when (not (eq? (typecheck program) 'int)) (error #f "not an int"))
   (let ([tmp-path "/tmp/a.s"])
     (when (file-exists? tmp-path) (delete-file tmp-path))
     (with-output-to-file tmp-path
       (lambda () (codegen program)))
-    (system "clang -fomit-frame-pointer /tmp/a.s rts.c")))
+    (system (format "clang -fomit-frame-pointer /tmp/a.s rts.c -o ~a" output))))