Move utils into its onw file
[scheme.git] / codegen.scm
index 52df26ece76527068fd89a7c397c3ca632aeaef4..dededbec01e0856b19703d8945e096e49b316aa5 100644 (file)
@@ -9,6 +9,40 @@
     (apply printf s)
     (display "\n")))
 
+(define wordsize 8)
+
+(define (type-size data-layouts type)
+
+  (define (adt-size adt)
+    (let ([sizes
+          (map (lambda (sum)
+                 (fold-left (lambda (acc x) (+ acc (type-size data-layouts x)))
+                            wordsize ; one word needed to store tag
+                            (cdr sum)))
+               (cdr adt))])
+      (apply max sizes)))
+  
+  (case type
+    ['Int wordsize]
+    ['Bool wordsize]
+    [else
+     (let ([adt (assoc type data-layouts)])
+       (if adt
+          (adt-size adt)
+          (error #f "unknown size" type)))]))
+
+(define (on-stack? expr)
+  (case (ast-type expr)
+    ['stack (cadr expr)]
+    [else #f]))
+
+                                       ; an environment consists of adt layouts in scope,
+                                       ; and any bound variables.
+                                       ; bound variables are an assoc list with their stack offset
+(define make-env list)
+(define env-data-layouts car)
+(define env-bindings cadr)
+
 (define (codegen-add xs si env)
   (define (go ys)
     (if (null? ys)
     ('linux  (emit "mov $1, %rax"))) ; syscall 1 (write)
   (emit "syscall"))
 
-(define (range s n)
-  (if (= 0 n) '()
-      (append (range s (- n 1))
-             (list (+ s (- n 1))))))
-
-(define wordsize 8)
-
 (define (codegen-let bindings body si env)
 
                                        ; is this a closure that captures itself?
     (and (eqv? (ast-type expr) 'closure)
         (memv name (caddr expr))))
 
-  (let* ((stack-offsets (map (lambda (name x) ; assoc map of binding name to offset
+
+  ;; (define (emit-scc scc env)
+  ;;   ; acc is a pair of the env and list of touchups
+  ;;   (define (emit-binding acc binding)
+  ;;     (let ([binding-name (car binding)]
+  ;;       [binding-body (cadr binding)]
+
+  ;;       [other-bindings (filter
+  ;;                        (lambda (x) (not (eqv? binding-name x)))
+  ;;                        scc)]
+  ;;       [mutually-recursives
+  ;;        (filter
+  ;;         (lambda (other-binding)
+  ;;           (memv other-binding (references binding-body)))
+  ;;         other-bindings)]
+
+  ;;       [new-touchups (append touchups (cdr acc))])
+
+  ;;                                   ; TODO: assert that the only mutually recursives are closures
+  ;;   (for-each
+  ;;    (lambda (binding)
+  ;;      (when (not (eqv? (ast-type (cadr binding))
+       
+  ;;   (emit "asdf")
+  ;;   (cons new-env new-touchups)
+  ;;   ))
+
+  ;;   (fold-left emit-binding (cons env '()) scc))))
+  
+  (let* ([stack-offsets (map (lambda (name x) ; assoc map of binding name to offset
                               (cons name (- si (* x wordsize))))
                             (map car bindings)
-                            (range 0 (length bindings))))
-        (inner-si (- si (* (length bindings) wordsize)))
+                            (range 0 (length bindings)))]
+        [inner-si (- si (* (length bindings) wordsize))]
 
-        (get-offset (lambda (n) (cdr (assoc n stack-offsets))))
+        [get-offset (lambda (n) (cdr (assoc n stack-offsets)))]
         
         [inner-env
          (fold-left
           (lambda (env comps)
-            (let ([scc-env
+            (let* ([scc-binding-offsets
                     (fold-left
                      (lambda (acc name)
                        (cons (cons name (get-offset name))
                              acc))
-                    env
-                    comps)])
+                     (env-bindings env)
+                     comps)]
+                   [scc-env (make-env (env-data-layouts env) scc-binding-offsets)])
               (for-each 
                (lambda (name)
                  (let ([expr (cadr (assoc name bindings))])
                                        ; codegen-closure realise this!
                        (codegen-expr expr
                                      inner-si
+                                     (make-env
+                                      (env-data-layouts scc-env)
                                       (cons (cons name 'self-captive)
-                                           scc-env))
+                                            (env-bindings scc-env))))
                        (codegen-expr expr inner-si scc-env))
                    (emit "movq %rax, ~a(%rbp)" (get-offset name))))
                comps)
               scc-env))
-          env (reverse (sccs (graph bindings))))])
+          env
+          (reverse (sccs (graph bindings))))])
     
     (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(%rbp), %rax" offset)))
+  (let ([binding (assoc name (env-bindings env))])
+    (if (not binding)
+       (error #f (format "Variable ~a is not bound" name))
+       (emit "movq ~a(%rbp), %rax" (cdr binding)))))
 
 (define cur-lambda 0)
 (define (fresh-lambda)
                                        ; store the captured vars
     (for-each
      (lambda (var-name heap-offset)
-       (let ([stack-offset (cdr (assoc var-name env))])
+       (let ([stack-offset (cdr (assoc var-name (env-bindings env)))])
         (emit "### captive ~a" var-name)
         (if (eqv? stack-offset 'self-captive)
                                        ; captive refers to this closure:
                               (* (- wordsize) (+ 1 i)))
                             (range 0 (length params))))
 
-        (env (map cons params stack-offsets)))
+        [bindings (map cons params stack-offsets)]
+        [env (make-env '() bindings)])
     (emit "~a:" label)
 
     (display "## lambda captives: ")
     (codegen-expr else si env)
     (emit "~a:" exit-label)))
 
+(define (data-tor env e)
+  (if (not (list? e)) #f    
+      (assoc (car e) (flat-map data-tors (env-data-layouts env)))))
+
+                                       ; returns the internal offset in bytes of a product within an ADT
+                                       ; given the constructor layout
+                                       ; constructor-layout: (foo (Int Bool))
+(define (data-product-offset data-layouts type sum index)
+  (let* ([products (cdr (assoc sum (cdr (assoc type data-layouts))))]
+        [to-traverse (list-head products index)])
+    (fold-left
+     (lambda (acc t) (+ acc (type-size data-layouts t)))
+     wordsize ; skip the tag in the first word
+     to-traverse)))
+
+(define (data-sum-tag data-layouts type sum)
+
+  (define (go acc sums)
+    (when (null? sums) (error #f "data-sum-tag no sum for type" sum type))
+    (if (eqv? sum (car sums))
+       acc
+       (go (+ 1 acc) (cdr sums))))
+  (let* ([type-sums (cdr (assoc type data-layouts))])
+    (go 0 (map car type-sums))))
+
+(define (codegen-data-tor e si env)
+
+  (define (codegen-destructor tor)
+    (let* ([res (codegen-expr (cadr e) si env)]
+          [info (cadr tor)]
+          [index (caddr info)]
+          [type (car info)]
+          [sum (cadr info)])
+      (when (not (stack-expr? res))
+       (error #f "codegened something that wasn't a stack expression"))
+                                       ;TODO handle stack types
+      (emit "movq ~a(%rbp), %rax"
+           (- si (data-product-offset (env-data-layouts env) type sum index)))))                                     
+
+  (define (codegen-constructor tor)
+    (let* ([info (cadr tor)]
+          [type (car info)]
+          [sum (cadr info)]
+          [constructor (car e)]
+
+          [args (cdr e)]
+
+          [tag (data-sum-tag (env-data-layouts env)
+                             type
+                             sum)]
+          
+          [insert-product
+           (lambda (expr i)
+             (let ([res (codegen-expr expr si env)]
+                   [stack-offset (- si (data-product-offset (env-data-layouts env)
+                                                            type sum
+                                                            i))])
+               (if (stack-expr? res)
+                   (error #f "todo: handle stack-exprs in stack exprs")
+                   (emit "movq %rax, ~a(%rbp)" stack-offset))))])
+
+                                       ; emit the tag
+      (emit "movq $~a, ~a(%rbp)" tag si)
+      
+      (for-each insert-product args (range 0 (length args)))
+      (type-size (env-data-layouts env) type)))
+  
+  (let* ([tor (data-tor env e)]
+        [constructor (eqv? 'constructor (caddr (cadr tor)))])
+    (if constructor
+       (codegen-constructor tor)
+       (codegen-destructor tor))))
+
+(define stack-expr? number?)
+
+                                       ; returns a number if result was stored on stack 
 (define (codegen-expr e si env)
   (emit "# ~a" e)
   (case (ast-type e)
        ('= (codegen-eq  (cadr e) (caddr e) si env))
        ('bool->int (codegen-expr (cadr e) si env))
        ('print (codegen-print (cadr e) si env))
-       (else (codegen-call (car e) (cdr e) si env))))
+       (else
+       (if (data-tor env e)
+           (codegen-data-tor e si env)
+           (codegen-call (car e) (cdr e) si env)))))
 
                                        ; this is a builtin being passed around as a variable
-    ('builtin (emit "movq $~a, %rax" (builtin-id e)))
+                                       ; this should have been converted to a closure!
+    ('builtin (error #f "passing about a builtin!" e))
 
     ('let (codegen-let (let-bindings e)
                       (let-body e)
     ('static-string (emit "movq ~a@GOTPCREL(%rip), %rax"
                          (cadr e)))
 
-    (else (error #f "don't know how to codegen this"))))
+    ('stack (codegen-expr (caddr e) si env))
 
+    (else (error #f "don't know how to codegen this"))))
 
+                                       ; takes in a expr annotated with types and returns a type-less AST
+                                       ; with stack values wrapped
+(define (annotate-stack-values data-layout ann-e)
+  (define (stack-type? type)
+    (assoc type data-layout))
+  (define (strip e)
+    (ast-traverse strip (ann-expr e)))
+  (let* ([e (ann-expr ann-e)]
+        [type (ann-type ann-e)])
+    (if (stack-type? type)
+       `(stack ,type ,(ast-traverse strip e))
+       (ast-traverse (lambda (x)
+                       (annotate-stack-values data-layout x))
+                     e))))
 
 (define (free-vars prog)
   (define bound '())
            (set! bound (append (lambda-args e) bound))
            (collect (lambda-body e))))
 
-      ('app (fold-map collect e))
-      ('if (fold-map collect (cdr e)))
+      ('app (flat-map collect e))
+      ('if (flat-map collect (cdr e)))
       ('let
-         (let ([bind-fvs (fold-map (lambda (a)
+         (let ([bind-fvs (flat-map (lambda (a)
                                      (begin
                                        (set! bound (cons (car a) bound))
                                        (collect (cdr a))))
                                    (let-bindings e))])
-           (append bind-fvs (fold-map collect (let-body e)))))
+           (append bind-fvs (flat-map collect (let-body e)))))
       (else '())))
   (collect prog))
 
   (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))
-
-;;   (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
                                        ; -----------------------
 (define (codegen program)
   (set! cur-label 0)
   (set! cur-lambda 0)
-  (let* ((extract-res-0 (extract-strings program))
-        (strings (car extract-res-0))
-        (extract-res-1 (extract-lambdas (cdr extract-res-0)))
-        (lambdas (car extract-res-1))
-        (xform-prog (cdr extract-res-1)))
+  (let* ([data-layouts (program-data-layouts program)]
+
+        [pattern-matched (expand-pattern-matches program)]
+        [type-annotated (annotate-types pattern-matched)]
+        [stack-annotated (annotate-stack-values data-layouts
+                                                type-annotated)]
+        
+        (strings-res (extract-strings stack-annotated))
+        (strings (car strings-res))
+        (lambdas-res (extract-lambdas (cdr strings-res)))
+        (lambdas (car lambdas-res))
+        (xform-prog (cdr lambdas-res)))
 
     (emit "\t.global _start")
     (emit "\t.text")
 
     (emit "movq %rsp, %rbp")            ; set up the base pointer
     
-    (codegen-expr xform-prog wordsize '())
+    (codegen-expr xform-prog (- wordsize) (make-env data-layouts '()))
 
                                        ; exit syscall
     (emit "mov %rax, %rdi")
 
 (define (compile-to-binary program output t)
   (set! target t)
-  (when (not (eq? (typecheck program) 'int)) (error #f "not an int"))
+  (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
 ; %r8  = 5th arg
 ; %r9  = 6th arg
 
-; on darwin, the syscall is offset by 0x2000000
+; on darwin, unix/posix syscalls are offset by 0x2000000 (syscall classes)
 ; https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/kern/syscalls.master
 ; documentation for most syscalls: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys