(apply printf s)
(display "\n")))
+(define wordsize 8)
+
+(define (stack-type? data-layouts type)
+ (if (assoc type data-layouts) #t #f))
+
+
+(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)))]))
+
+ ; returns the size of an expression's result in bytes
+(define (expr-size dls e)
+ (if (eqv? (ast-type e) 'stack)
+ (type-size dls (cadr e))
+ wordsize))
+
+(define (on-stack? expr)
+ (case (ast-type expr)
+ ['stack (cadr expr)]
+ [else #f]))
+
+ ; does a movsq for something on the stack
+ ; src points to the start stack index, but not the top of that index
+ ; likewise for dst
+ ; | ... |
+ ; +------+ <-- to here
+ ; | tag0 |
+ ; +------+ <-- src (size = 16)
+ ; | 42 |
+ ; +------+ <-- start copying from here...
+(define (emit-stack-copy src dst size)
+ (let ([size-to-copy (- size wordsize)])
+ (emit "leaq ~a(%rbp), %rsi" (- src size-to-copy))
+ (emit "leaq ~a(%rbp), %rdi" (- dst size-to-copy))
+ (emit "movq $~a, %rcx" (/ size wordsize))
+ (emit "rep movsq")))
+
+
+ ; 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 (env-append-bindings env bindings)
+ (make-env (env-data-layouts env)
+ (append bindings (env-bindings env))))
+
(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 (codegen-let bindings body si env)
-(define wordsize 8)
+ ; is this a closure that captures itself?
+ ; e.g. (let ([x 3] [f (closure lambda0 (f x))]) (f))
+ (define (self-captive-closure? name expr)
+ (and (eqv? (ast-type expr) 'closure)
+ (memv name (caddr expr))))
+
+ ;; (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))))
+ ; assoc map of binding name to size
+
+
+ (define dls (env-data-layouts env))
+
+ (define stack-sizes
+ (map (lambda (binding) (cons (car binding) (expr-size dls (cadr binding))))
+ bindings))
+
+ ; assoc map of binding name to offset
+ (define stack-offsets
+ ; 2 4 2 8 6
+ (let* ([totals ; 2 6 8 16 22
+ (reverse (fold-left (lambda (acc x)
+ (if (null? acc)
+ (list x)
+ (cons (+ x (car acc)) acc)))
+ '()
+ (map cdr stack-sizes)))]
+ ; 0 2 6 8 16
+ [relative-offsets (map - totals (map cdr stack-sizes))]
+ [absolute-offsets (map (lambda (x) (- si x)) relative-offsets)])
+ (map cons (map car stack-sizes) absolute-offsets)))
+
+ (let* (
+ ; the stack index used when codegening binding body and main body
+ ; -> stack ->
+ ; [stack-offsets | inner-si]
+ [inner-si (- si (fold-left + 0 (map cdr stack-sizes)))]
+
+ [get-offset (lambda (n) (cdr (assoc n stack-offsets)))]
+
+ [inner-env
+ (fold-left
+ (lambda (env comps)
+ (let* ([scc-binding-offsets
+ (fold-left
+ (lambda (acc name)
+ (cons (cons name (get-offset name))
+ acc))
+ (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))]
+ [size (expr-size dls expr)])
+ (emit "## generating ~a with scc-env ~a" name scc-env)
+ (if (self-captive-closure? name expr)
+ ; if self-captive, insert a flag into the environment to let
+ ; codegen-closure realise this!
+ (codegen-expr expr
+ inner-si
+ (make-env
+ (env-data-layouts scc-env)
+ (cons (cons name 'self-captive)
+ (env-bindings scc-env))))
+ (codegen-expr expr inner-si scc-env))
+
+ (if (on-stack? expr)
+ ; copy over whatevers on the stack
+ (emit-stack-copy inner-si (get-offset name) size)
+ (emit "movq %rax, ~a(%rbp)" (get-offset name)))))
+ comps)
+ scc-env))
+ env
+ (reverse (sccs (graph bindings))))])
-(define (codegen-let bindings body si env)
- (let* ((stack-offsets (map (lambda (x) (- si (* x wordsize)))
- (range 0 (length bindings))))
- (inner-si (- si (* (length bindings) wordsize)))
- (names (map car bindings))
- (exprs (map cadr bindings))
-
- ; recursive let bindings: build environment as we go
- (inner-env (fold-left
- (lambda (env name expr offset)
- (codegen-expr expr inner-si env)
- (emit "movq %rax, ~a(%rbp)" offset)
- (cons (cons name offset) env))
- env names exprs stack-offsets)))
(for-each (lambda (form)
(codegen-expr form inner-si inner-env))
body)))
-(define (codegen-var name si env)
- (when (not (assoc name env))
+(define (codegen-var e si env)
+ (let* ([stack-type (on-stack? e)]
+ [name (if stack-type (caddr e) e)]
+
+ [dls (env-data-layouts env)]
+
+ [singleton? (and stack-type
+ (assoc name
+ (cdr (assoc stack-type dls))))]
+
+ [stack-offset (assoc name (env-bindings env))])
+ (when (and (not stack-offset) (not singleton?))
(error #f (format "Variable ~a is not bound" name)))
- (let ((offset (cdr (assoc name env))))
- (emit "movq ~a(%rbp), %rax" offset)))
+
+ (if (on-stack? e)
+ (if singleton?
+ ; singletons don't need to be in the environment
+ ; just copy over the tag
+ (emit "movq $~a, ~a(%rbp)"
+ (data-sum-tag (env-data-layouts env)
+ stack-type
+ name)
+ si)
+ (emit-stack-copy (cdr stack-offset) si (type-size dls stack-type)))
+ (emit "movq ~a(%rbp), %rax" (cdr stack-offset)))))
(define cur-lambda 0)
(define (fresh-lambda)
(emit "## storing captives")
; store the captured vars
(for-each
- (lambda (var-name new-offset)
+ (lambda (var-name heap-offset)
+ (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:
+ ; move heap addr of this closure to stack!
+ (emit "movq %rax, ~a(%rax)" heap-offset)
(begin
- (emit "movq ~a(%rbp), %rbx" (cdr (assoc var-name env)))
- (emit "movq %rbx, ~a(%rax)" new-offset)))
+ (emit "movq ~a(%rbp), %rbx" stack-offset)
+ (emit "movq %rbx, ~a(%rax)" heap-offset)))))
captured
heap-offsets)))
(define (codegen-lambda l)
(let* ((label (car l))
(stuff (cdr l))
- (captured (car stuff))
+ (captives (car stuff))
(args (cadr stuff))
(body (caddr stuff))
; params = what actually gets passed
- (params (append captured args))
+ (params (append captives args))
(stack-offsets (map (lambda (i)
- (* (- wordsize) i))
+ (* (- 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: ")
- (display captured)
+ (display captives)
(newline)
(display "## lambda args: ")
(display args)
(emit "push %rbp") ; preserve caller's base pointer
(emit "movq %rsp, %rbp") ; set up our own base pointer
- (emit "subq $8, %rbp")
; load the captured vars onto the stack
(for-each
(lambda (i)
(begin
- (emit "movq ~a(~a), %rbx" i (param-register 0))
- (emit "movq %rbx, ~a(%rbp)" (* (- wordsize) i))))
- (range 0 (length captured)))
+ (emit "# loading captive ~a" (list-ref captives i))
+ (emit "movq ~a(~a), %rbx" (* wordsize i) (param-register 0))
+ (emit "movq %rbx, ~a(%rbp)" (* (- wordsize) (+ 1 i)))))
+ (range 0 (length captives)))
; load the args onto the stack
(for-each
(emit "movq ~a, %rbx" (param-register (+ 1 i)))
(emit "movq %rbx, ~a(%rbp)"
(* (- wordsize)
- (+ (length captured) i)))))
+ (+ 1 (length captives) i)))))
(range 0 (length args)))
(codegen-expr body (* (- wordsize) (+ 1 (length params))) env)
(codegen-expr else si env)
(emit "~a:" exit-label)))
+(define (codegen-case switch cases si env)
+ (define dls (env-data-layouts env))
+ (define exit-label (fresh-label))
+
+
+ ; checks if equal and returns assoc list of bindings
+ (define (check-equal jne-label type inner-offset x)
+
+ ; TODO: tidy this up! comparibles and binds could be merged
+ ; (foo a 2 (bar x)) -> ((2 Int 1) ((bar x) A 2))
+ ; sum: foo
+ (define (comparibles sum)
+ (let ([product-types (cdr (assoc sum (cdr (assoc type dls))))])
+ (if (null? product-types)
+ '()
+ (filter (lambda (x) (not (eqv? 'var (ast-type (car x)))))
+ (map (lambda (x t i) (list x t i))
+ (cdr x)
+ product-types
+ (range 0 (length product-types)))))))
+
+ (define (binds sum)
+ (let ([product-types (cdr (assoc sum (cdr (assoc type dls))))])
+ (if (null? product-types)
+ '()
+ (filter (lambda (x) (eqv? 'var (ast-type (car x))))
+ (map (lambda (x i)
+ (cons x
+ (- inner-offset
+ (data-product-offset dls type sum i))))
+ (cdr x)
+ (range 0 (length (cdr x))))))))
+
+
+ (let ([sums (assoc type dls)])
+ (if sums
+ (let* ([sum (if (list? x) (car x) x)] ; can sometimes be a singleton
+ [tag (data-sum-tag dls type sum)])
+ ; the tag is at the top (beginning) of the adt on the stack
+ (emit "cmpq $~a, ~a(%rbp)" tag inner-offset)
+ (emit "jne ~a" jne-label)
+
+ (append (binds sum)
+ (flat-map
+ (lambda (cmp) ; cmp = (x type index)
+ (check-equal jne-label
+ (cadr cmp)
+ (- inner-offset (data-product-offset dls type sum (caddr cmp)))
+ (car cmp)))
+ (comparibles sum))))
+ (if (eqv? 'var (ast-type x))
+ (list (cons x inner-offset))
+ (begin
+ (emit "cmpq $~a, ~a(%rbp)" x inner-offset)
+ (emit "jne ~a" jne-label)
+ '() )))))
+
+ (define (codegen-adt-match type case)
+ (let* ([match (car case)]
+ [expr (cadr case)]
+ [next-section-label (fresh-label)]
+ [inner-si (- si (type-size dls type))]
+ [new-env (env-append-bindings env
+ (check-equal next-section-label type si match))])
+
+
+ (codegen-expr expr inner-si new-env)
+ (emit "jmp ~a" exit-label)
+
+ (emit "~a:" next-section-label)))
+
+ (define (codegen-literal-match case)
+ (let ([next-section-label (fresh-label)])
+ (emit "cmpq $~a, %rax" (car case))
+ (emit "jne ~a" next-section-label)
+ (codegen-expr (cadr case) si env)
+ (emit "jmp ~a" exit-label)
+ (emit "~a:" next-section-label)))
+
+ ; generate the switch
+ ; (and store it on the stack if not a stack value)
+ (codegen-expr switch si env)
+
+ (if (eqv? 'stack (ast-type switch))
+ ; adt pattern match
+ (for-each (lambda (x) (codegen-adt-match (cadr switch) x)) cases)
+ (for-each codegen-literal-match cases))
+ (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 dls (env-data-layouts env))
+
+ (define (codegen-destructor tor)
+ (let* ([res (codegen-expr (cadr e) si env)]
+ [info (cadr tor)]
+ [type (car info)]
+ [sum (cadr info)]
+ [index (caddr info)]
+ [product-type (cadddr info)]
+ [product-type-size (type-size dls product-type)]
+
+ [safe-space-offset (- si (type-size dls type))]
+
+ [inner-offset (- si (data-product-offset dls type sum index))])
+
+ (when (not (on-stack? (cadr e)))
+ (error #f "trying to destruct something that isn't a stack expression"))
+ (emit "# deconstructing")
+
+ (if (stack-type? (env-data-layouts env) product-type)
+ ; if copying from the stack, need to first copy
+ ; to a safe space above to avoid overwriting
+ ; the original result on the stack
+ ; this is bad. please remove this in the rewrite.
+ (begin
+ (emit-stack-copy inner-offset safe-space-offset product-type-size)
+ (emit-stack-copy safe-space-offset si product-type-size))
+ (emit "movq ~a(%rbp), %rax" inner-offset))))
+
+ (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)]
+
+ [inner-si (- si (type-size dls type))]
+
+ [product-types (cdr (assoc sum (cdr (assoc type dls))))]
+
+ [insert-product
+ (lambda (expr i product-type)
+ (let ([dest-offset
+ (- si (data-product-offset dls type sum i))]
+ [product-size (type-size dls product-type)])
+ (codegen-expr expr inner-si env)
+ (if (on-stack? expr)
+ (emit-stack-copy inner-si dest-offset product-size)
+ (emit "movq %rax, ~a(%rbp)" dest-offset))))])
+
+ ; emit the tag
+ (emit "movq $~a, ~a(%rbp)" tag si)
+ ; generate products
+ (for-each insert-product args (range 0 (length args)) product-types)))
+
+ (let* ([tor (data-tor env e)]
+ [constructor (eqv? 'constructor (caddr (cadr tor)))])
+ (if constructor
+ (codegen-constructor tor)
+ (codegen-destructor tor))))
+
(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)
('var (codegen-var e si env))
('if (codegen-if (cadr e) (caddr e) (cadddr e) si env))
+ ('case (codegen-case (case-switch e) (case-cases e) si env))
('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
('int-literal (emit "movq $~a, %rax" e))
('static-string (emit "movq ~a@GOTPCREL(%rip), %rax"
(cadr e)))
- (else (error #f "don't know how to codegen this"))))
-
-
+ ('stack (case (ast-type (caddr e))
+ ['var (codegen-var e si env)]
+ [else (codegen-expr (caddr e) si env)]))
+
+ (else (error #f "don't know how to codegen this")))
+ (emit "# done ~a" e))
+
+ ; takes in a expr annotated with types and returns a type-less AST
+ ; with stack values wrapped
+(define (annotate-stack-values data-layouts ann-e)
+ (define (stack-type? type)
+ (assoc type data-layouts))
+ (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 (lambda (x) (annotate-stack-values data-layouts x)) e))
+ (ast-traverse (lambda (x)
+ (annotate-stack-values data-layouts x))
+ e))))
(define (free-vars prog)
(define bound '())
(define (collect e)
(case (ast-type e)
('builtin '()) ; do nothing
- ('var (if (memq e bound) '() (list e)))
+ ('var (if (memv e bound) '() (list e)))
('lambda
+ (begin
(set! bound (append (lambda-args e) bound))
- (collect (lambda-body e)))
+ (collect (lambda-body e))))
- ('app (fold-map collect e))
+ ('app (flat-map collect e))
+ ('if (flat-map collect (cdr e)))
('let
- (let ((bind-fvs (fold-map (lambda (a)
- ((set! bound (cons (car a) bound))
+ (let ([bind-fvs (flat-map (lambda (a)
+ (begin
+ (set! bound (cons (car a) bound))
(collect (cdr a))))
- (let-bindings cadr)))
- (body-fvs (fold-map collect (let-body e))))
- (append bind-fvs body-fvs)))
+ (let-bindings e))])
+ (append bind-fvs (flat-map collect (let-body e)))))
(else '())))
(collect prog))
(define (extract e)
(case (ast-type e)
('lambda (add-lambda e))
- ('let `(let ,(map extract (let-bindings e))
+ ('let `(let ,(map (lambda (b) `(,(car b) ,@(extract (cdr b)))) (let-bindings e))
,@(map extract (let-body e))))
('app (append
; if a builtin is used as a function, don't generate lambda
(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")
(initialize-heap)
(emit "movq %rsp, %rbp") ; set up the base pointer
- (codegen-expr xform-prog 0 '())
+
+ (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