Add darwin target support
[scheme.git] / codegen.scm
index a1a0aa71280e66b510be50964d9c7c37893b1a91..04a03fa7af0efa80e0c7f212e988195d4310eeb4 100644 (file)
@@ -1,6 +1,8 @@
 (load "typecheck.scm")
 (load "ast.scm")
 
+(define target 'darwin)
+
 (define (emit . s)
   (begin
     (apply printf s)
 (define (codegen-add xs si env)
   (define (go ys)
     (if (null? ys)
-      (emit "movq ~a(%rsp), %rax" si)
+      (emit "movq ~a(%rbp), %rax" si)
       (begin
        (let ((y (car ys)))
             (if (integer? y)
-                (emit "addq $~a, ~a(%rsp)" y si)
+                (emit "addq $~a, ~a(%rbp)" y si)
                 (begin
                   (codegen-expr y (- si wordsize) env)
-                  (emit "addq %rax, ~a(%rsp)" si))))
+                  (emit "addq %rax, ~a(%rbp)" si))))
        (go (cdr ys)))))
   (begin
-                                       ; use si(%rsp) as the accumulator
-    (emit "movq $0, ~a(%rsp)" si)
+                                       ; use si(%rbp) as the accumulator
+    (emit "movq $0, ~a(%rbp)" si)
     (go xs)))
 
 (define (codegen-binop opcode)
   (lambda (a b si env)
     (codegen-expr b si env)
-    (emit "movq %rax, ~a(%rsp)" si)
+    (emit "movq %rax, ~a(%rbp)" si)
     (codegen-expr a (- si wordsize) env)
-    (emit "~a ~a(%rsp), %rax" opcode si)))
+    (emit "~a ~a(%rbp), %rax" opcode si)))
 
 (define codegen-sub (codegen-binop "sub"))
+
 (define codegen-mul (codegen-binop "imul"))
 
 (define (codegen-not x si env)
 
 (define (codegen-eq a b si env)
   (codegen-expr a si env)
-  (emit "movq %rax, ~a(%rsp)" si)
+  (emit "movq %rax, ~a(%rbp)" si)
   (codegen-expr b (- si wordsize) env)
-  (emit "subq ~a(%rsp), %rax" si)
+  (emit "subq ~a(%rbp), %rax" si)
   (emit "not %rax")
   (emit "andq $1, %rax"))
 
+; 'write file handle addr-string num-bytes
+     
 (define (codegen-print x si env)
   (codegen-expr x si env) ; x should be a static-string, producing a label
 
   (emit "not %rcx")      ; -%rcx = strlen + 1
   (emit "dec %rcx")
 
-  (emit "mov %rcx, %rdx") ; number of bytes
-  (emit "mov %rax, %rsi") ; addr of string
-  (emit "mov $1, %rax") ; file handle 1 (stdout)
-  (emit "mov $1, %rdi") ; syscall 1 (write)
+  (case target
+    ('darwin
+     (emit "movq %rax, %rsi") ; string addr
+     (emit "movq %rcx, %rdx") ; num bytes
+     (emit "movq $1, %rdi")   ; file handle (stdout)
+     (emit "movq $0x2000004, %rax")) ; syscall 4 (write)
+    ('linux
+     (emit "mov %rax, %rsi")  ; string addr
+     (emit "mov %rcx, %rdx")  ; num bytes
+     (emit "mov $1, %rax")    ; file handle (stdout)
+     (emit "mov $1, %rdi"))) ; syscall 1 (write)
   (emit "syscall"))
 
 (define (range s n)
@@ -83,7 +95,7 @@
         (inner-env (fold-left
                     (lambda (env name expr offset)
                       (codegen-expr expr inner-si env)
-                      (emit "movq %rax, ~a(%rsp)" offset)
+                      (emit "movq %rax, ~a(%rbp)" offset)
                       (cons (cons name offset) env))
                     env names exprs stack-offsets)))
     (for-each (lambda (form)
   (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)))
+    (emit "movq ~a(%rbp), %rax" offset)))
 
 (define cur-lambda 0)
 (define (fresh-lambda)
                                        ; first move the captured variables into param registers
     (for-each
      (lambda (e i)
-       (emit "movq ~a(%rsp), ~a"
+       (emit "movq ~a(%rbp), ~a"
             (cdr (assoc e env)) ; offset of the var
             (param-register i)))
      captured (range 0 (length captured)))
         (emit "movq %rax, ~a" (param-register i))))
      args (range argument-start (length args)))
 
-                                       ; now call
-    (emit "callq ~a" label)))
-
+    (emit "addq $~a, %rsp" si) ; adjust the stack pointer to account all the stuff we put in the env
+    (emit "callq ~a" label)
+    (emit "subq $~a, %rsp" si)))
 
 (define (codegen-lambda l)
   (let* ((label (car l))
         (args (cadr l))
         (captured (caddr l))
         (body (cadddr l))
-        ; captured, then args
-        (vars (append captured args))
+                                       ; params = what actually gets passed
+        (params (append captured args))
 
         (param-registers (map param-register
-                            (range 0 (length vars))))
+                              (range 0 (length params))))
         (stack-offsets (map (lambda (i)
                               (* (- wordsize) i))
-                            (range 0 (length vars))))
+                            (range 1 (length params))))
 
         (copy-insts (map (lambda (r o)
-                           (format "movq ~a, ~a(%rsp)"
-                                   r o))
+                           (format "movq ~a, ~a(%rbp)" r o))
                          param-registers stack-offsets))
 
-        (env (map cons vars stack-offsets)))
+        (env (map cons params stack-offsets)))
     (emit "~a:" label)
     (display "## lambda body: ")
     (display body)
     (display "## environment: ")
     (display env)
     (newline)
-    (amd64-abi
-     (lambda ()
+
+    (emit "push %rbp") ; preserve caller's base pointer
+    (emit "movq %rsp, %rbp") ; set up our own base pointer
+    
     (for-each emit copy-insts)
-       (codegen-expr body (* (- wordsize) (length vars)) env)
-       ))))                    ; move args and capture vars to stack
+    (codegen-expr body (* (- wordsize) (+ 1 (length params))) env)
+
+    (emit "pop %rbp") ; restore caller's base pointer
+    (emit "ret")))
+
+(define (codegen-string label)
+  (case target
+    ('darwin (emit "movq ~a@GOTPCREL(%rip), %rax" label))
+    ('linux  (emit "movq $~a, %rax" label))))
 
 (define cur-label 0)
 (define (fresh-label)
     ('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
     ('int-literal (emit "movq $~a, %rax" e))
     
-    ('static-string (emit "movq $~a, %rax" (cadr e))) ; move label
+    ('static-string (codegen-string (cadr e)))
 
     (else (error #f "don't know how to codegen this"))))
 
   (emit "~a:" (car s))
   (emit "\t.string \"~a\"" (cdr s)))
 
-(define (amd64-abi f)
-                                       ; preserve registers
-  (emit "push %rbp")
-  (emit "push %rbx")
-  (for-each (lambda (i)
-             (emit (string-append
-                    "push %r"
-                    (number->string i))))
-           '(12 13 14 15))
-
-  (f) ; call stuff
-                                       ; restore preserved registers
-  (for-each (lambda (i)
-             (emit (string-append
-                    "pop %r"
-                    (number->string i))))
-           '(15 14 13 12))
-  (emit "pop %rbx")
-  (emit "pop %rbp")
-  (emit "ret"))
+;; (define (amd64-abi f)
+;;                                     ; preserve registers
+;;   (emit "push %rbp")
+;;   ;; (emit "push %rbx")
+;;   ;; (for-each (lambda (i)
+;;   ;;              (emit (string-append
+;;   ;;                     "push %r"
+;;   ;;                     (number->string i))))
+;;   ;;            '(12 13 14 15))
+
+;;   (emit "movq %rsp, %rbp")              ; set up the base pointer
+
+;;   (f) ; call stuff
+;;                                     ; restore preserved registers
+;;   ;; (for-each (lambda (i)
+;;   ;;              (emit (string-append
+;;   ;;                     "pop %r"
+;;   ;;                     (number->string i))))
+;;   ;;            '(15 14 13 12))
+;;   ;; (emit "pop %rbx")
+;;   (emit "pop %rbp")
+;;   (emit "ret"))
 
                                        ; 24(%rbp) mem arg 1
                                        ; 16(%rbp) mem arg 0          prev frame
         (lambdas (car extract-res-1))
         (xform-prog (cdr extract-res-1)))
 
-    (emit "\t.globl _start")
+    (emit "\t.global _start")
     (emit "\t.text")
                                        ;    (emit ".p2align 4,,15") is this needed?
 
     (for-each codegen-lambda lambdas)
 
-
     (emit "_start:")
+    (emit "movq %rsp, %rbp")            ; set up the base pointer
     (codegen-expr xform-prog 0 '())
 
                                        ; exit syscall
     (emit "mov %rax, %rdi")
-    (emit "mov $60, %rax")
+    (case target
+      ('darwin (emit "movq $0x2000001, %rax"))
+      ('linux (emit "mov $60, %rax")))
     (emit "syscall")
 
     (emit "\t.data")
 
     (for-each codegen-string-data strings)))
 
-(define (compile-to-binary program output)
+(define (compile-to-binary program output t)
+  (set! target t)
   (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 (format "clang -nostdlib /tmp/a.s -o ~a" output))))
+
+    (case target
+      ('darwin
+       (system "as /tmp/a.s -o /tmp/a.o")
+       (system (format "ld /tmp/a.o -e _start -macosx_version_min 10.14 -static -o ~a" output)))
+      ('linux
+       (system "as /tmp/a.s -o /tmp/a.o")
+       (system (format "ld /tmp/a.o -o ~a" output))))))