(load "typecheck.scm")
(load "ast.scm")
+(load "platform.scm")
-(define target 'darwin)
+(define target host-os)
(define (emit . s)
(begin
(codegen-expr a si env)
(emit "movq %rax, ~a(%rbp)" si)
(codegen-expr b (- si wordsize) env)
- (emit "subq ~a(%rbp), %rax" si)
- (emit "not %rax")
- (emit "andq $1, %rax"))
+ (emit "## ~a = ~b" a b)
+ (emit "cmpq ~a(%rbp), %rax" si)
+ (emit "sete %al"))
; 'write file handle addr-string num-bytes
(emit "not %rcx") ; -%rcx = strlen + 1
(emit "dec %rcx")
- (case target
- ('darwin
(emit "movq %rbx, %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 %rbx, %rsi") ; string addr
- (emit "mov %rcx, %rdx") ; num bytes
- (emit "mov $1, %rax") ; file handle (stdout)
- (emit "mov $1, %rdi"))) ; syscall 1 (write)
+ (case target
+ ('darwin (emit "mov $0x2000004, %rax")) ; syscall 4 (write)
+ ('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)
- (let* ((stack-offsets (map (lambda (x) (- si (* x wordsize)))
+
+ ; 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))))
+
+ (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)))
- (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)))
+
+ (get-offset (lambda (n) (cdr (assoc n stack-offsets))))
+
+ [inner-env
+ (fold-left
+ (lambda (env comps)
+ (let ([scc-env
+ (fold-left
+ (lambda (acc name)
+ (cons (cons name (get-offset name))
+ acc))
+ env
+ comps)])
+ (for-each
+ (lambda (name)
+ (let ([expr (cadr (assoc name bindings))])
+ (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
+ (cons (cons name 'self-captive)
+ 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))))])
+
(for-each (lambda (form)
(codegen-expr form inner-si inner-env))
body)))
(let* ((heap-offsets (map (lambda (i) (+ 8 (* 8 i)))
(range 0 (length captured))))) ; 4, 12, 20, etc.
+ (emit "## creating closure")
+
(emit "movq heap_start@GOTPCREL(%rip), %rbx")
(emit "movq (%rbx), %rax") ; %rax = heap addr of closure
; point heap_start to next space
(emit "addq $~a, (%rbx)" (+ 8 (* 8 (length captured))))
+ (emit "## storing address to lambda")
; store the address to the lambda code
(emit "movq ~a@GOTPCREL(%rip), %rbx" label)
(emit "movq %rbx, 0(%rax)")
+ (emit "## storing captives")
; store the captured vars
(for-each
- (lambda (var-name new-offset)
- (emit "movq ~a(%rbp), ~a(rax)"
- (cdr (assoc var-name env))
- new-offset))
+ (lambda (var-name heap-offset)
+ (let ([stack-offset (cdr (assoc var-name 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" 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))
- (range 1 (length params))))
+ (* (- wordsize) (+ 1 i)))
+ (range 0 (length params))))
(env (map cons params stack-offsets)))
(emit "~a:" label)
(display "## lambda captives: ")
- (display captured)
+ (display captives)
(newline)
(display "## lambda args: ")
(display args)
(newline)
(emit "push %rbp") ; preserve caller's base pointer
+
(emit "movq %rsp, %rbp") ; set up our own base pointer
; load the captured vars onto the stack
(for-each
(lambda (i)
- (emit "movq ~a(~a), ~a(%rbp)"
- i (param-register 0) (* (- wordsize) i)))
- (range 0 (length captured)))
+ (begin
+ (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
(lambda (i)
- (emit "movq ~a, ~a(%rbp)"
- (param-register i) (* (- wordsize) i)))
- (range 1 (length args)))
+ (begin
+ (emit "movq ~a, %rbx" (param-register (+ 1 i)))
+ (emit "movq %rbx, ~a(%rbp)"
+ (* (- wordsize)
+ (+ 1 (length captives) i)))))
+ (range 0 (length args)))
(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 "lea $~a, %rax" label))))
-
(define cur-label 0)
(define (fresh-label)
(set! cur-label (+ 1 cur-label))
(emit "~a:" exit-label)))
(define (codegen-expr e si env)
+ (emit "# ~a" e)
(case (ast-type e)
('closure (codegen-closure (cadr e) (caddr e) si env))
('app
('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
('int-literal (emit "movq $~a, %rax" e))
- ('static-string (codegen-string (cadr e)))
+ ('static-string (emit "movq ~a@GOTPCREL(%rip), %rax"
+ (cadr e)))
(else (error #f "don't know how to codegen this"))))
-(define (fold-map f x) (fold-left append '() (map f x)))
(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))
('+ "_add")
('- "_sub")
('* "_mul")
+ ('! "_not")
+ ('= "_eq")
('bool->int "_bool2int")
- (else (error #f "fill this out"))))
+ ('print "_print")
+ (else (error #f "don't know this builtin"))))
(define (builtin-args e)
(case e
('+ '(x y))
('- '(x y))
('* '(x y))
+ ('! '(x))
+ ('= '(x y))
('bool->int '(x))
- (else (error #f "fill this out"))))
+ ('print '(x))
+ (else (error #f "don't know this builtin"))))
(define (add-builtin-lambda e)
(let* [(label (builtin-name e))
(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
(if (eqv? 'builtin (ast-type (car e)))
- '()
+ (list (car e))
(list (extract (car e))))
(map extract (cdr e))))
(emit "movq %rax, (%rsi)")))
(define (codegen program)
- (let* ((extract-res-0 (extract-strings program))
+ (set! cur-label 0)
+ (set! cur-lambda 0)
+ (let* ([body (program-body program)]
+
+ (extract-res-0 (extract-strings body))
(strings (car extract-res-0))
(extract-res-1 (extract-lambdas (cdr extract-res-0)))
(lambdas (car extract-res-1))
(initialize-heap)
(emit "movq %rsp, %rbp") ; set up the base pointer
- (codegen-expr xform-prog 0 '())
+
+ (codegen-expr xform-prog (- wordsize) '())
; 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