5 (define target host-os)
14 (define (type-size data-layouts type)
16 (define (adt-size adt)
19 (fold-left (lambda (acc x) (+ acc (type-size data-layouts x)))
20 wordsize ; one word needed to store tag
29 (let ([adt (assoc type data-layouts)])
32 (error #f "unknown size" type)))]))
34 ; returns the size of an expression's result in bytes
36 (if (eqv? (ast-type e) 'stack)
40 (define (on-stack? expr)
45 ; an environment consists of adt layouts in scope,
46 ; and any bound variables.
47 ; bound variables are an assoc list with their stack offset
48 (define make-env list)
49 (define env-data-layouts car)
50 (define env-bindings cadr)
52 (define (codegen-add xs si env)
55 (emit "movq ~a(%rbp), %rax" si)
59 (emit "addq $~a, ~a(%rbp)" y si)
61 (codegen-expr y (- si wordsize) env)
62 (emit "addq %rax, ~a(%rbp)" si))))
65 ; use si(%rbp) as the accumulator
66 (emit "movq $0, ~a(%rbp)" si)
69 (define (codegen-binop opcode)
71 (codegen-expr b si env)
72 (emit "movq %rax, ~a(%rbp)" si)
73 (codegen-expr a (- si wordsize) env)
74 (emit "~a ~a(%rbp), %rax" opcode si)))
76 (define codegen-sub (codegen-binop "sub"))
78 (define codegen-mul (codegen-binop "imul"))
80 (define (codegen-not x si env)
81 (codegen-expr x si env)
83 (emit "andq $1, %rax"))
85 (define (codegen-eq a b si env)
86 (codegen-expr a si env)
87 (emit "movq %rax, ~a(%rbp)" si)
88 (codegen-expr b (- si wordsize) env)
89 (emit "## ~a = ~b" a b)
90 (emit "cmpq ~a(%rbp), %rax" si)
93 ; 'write file handle addr-string num-bytes
95 (define (codegen-print x si env)
96 (codegen-expr x si env) ; x should be a static-string, producing a label
98 ; make a copy of string address since %rax and %rdi are clobbered
99 (emit "mov %rax, %rbx")
101 ; get the length of the null terminated string
102 (emit "mov %rax, %rdi")
103 (emit "xor %al, %al") ; set %al to 0
104 (emit "mov $-1, %rcx") ; max search length = max int = -1
105 (emit "cld") ; clear direction flag, search up in memory
106 (emit "repne scasb") ; scan string, %rcx = -strlen - 1 - 1
108 (emit "not %rcx") ; -%rcx = strlen + 1
111 (emit "movq %rbx, %rsi") ; string addr
112 (emit "movq %rcx, %rdx") ; num bytes
113 (emit "movq $1, %rdi") ; file handle (stdout)
115 ('darwin (emit "mov $0x2000004, %rax")) ; syscall 4 (write)
116 ('linux (emit "mov $1, %rax"))) ; syscall 1 (write)
119 (define (codegen-let bindings body si env)
121 ; is this a closure that captures itself?
122 ; e.g. (let ([x 3] [f (closure lambda0 (f x))]) (f))
123 (define (self-captive-closure? name expr)
124 (and (eqv? (ast-type expr) 'closure)
125 (memv name (caddr expr))))
127 ;; (define (emit-scc scc env)
128 ;; ; acc is a pair of the env and list of touchups
129 ;; (define (emit-binding acc binding)
130 ;; (let ([binding-name (car binding)]
131 ;; [binding-body (cadr binding)]
133 ;; [other-bindings (filter
134 ;; (lambda (x) (not (eqv? binding-name x)))
136 ;; [mutually-recursives
138 ;; (lambda (other-binding)
139 ;; (memv other-binding (references binding-body)))
142 ;; [new-touchups (append touchups (cdr acc))])
144 ;; ; TODO: assert that the only mutually recursives are closures
147 ;; (when (not (eqv? (ast-type (cadr binding))
150 ;; (cons new-env new-touchups)
153 ;; (fold-left emit-binding (cons env '()) scc))))
154 ; assoc map of binding name to size
156 (map (lambda (binding) (cons (car binding) (expr-size (cadr binding))))
159 ; assoc map of binding name to offset
160 (define stack-offsets
162 (let* ([totals ; 2 6 8 16 22
163 (reverse (fold-left (lambda (acc x)
166 (cons (+ x (car acc)) acc)))
168 (map cdr stack-sizes)))]
170 [relative-offsets (map - totals (map cdr stack-sizes))]
171 [absolute-offsets (map (lambda (x) (- si x)) relative-offsets)])
172 (map cons (map car stack-sizes) absolute-offsets)))
175 ; the stack index used when codegening binding body and main body
177 ; [stack-offsets | inner-si]
178 [inner-si (- si (fold-left + 0 (map cdr stack-sizes)))]
180 [get-offset (lambda (n) (cdr (assoc n stack-offsets)))]
185 (let* ([scc-binding-offsets
188 (cons (cons name (get-offset name))
192 [scc-env (make-env (env-data-layouts env) scc-binding-offsets)])
195 (let* ([expr (cadr (assoc name bindings))]
196 [size (expr-size expr)])
197 (emit "## generating ~a with scc-env ~a" name scc-env)
198 (if (self-captive-closure? name expr)
199 ; if self-captive, insert a flag into the environment to let
200 ; codegen-closure realise this!
204 (env-data-layouts scc-env)
205 (cons (cons name 'self-captive)
206 (env-bindings scc-env))))
207 (codegen-expr expr inner-si scc-env))
211 ; copy over whatevers on the stack
212 (emit "leaq ~a(%rbp), %rsi" (- inner-si size))
213 (emit "leaq ~a(%rbp), %rdi" (- (get-offset name) size))
214 (emit "movq $~a, %rcx" (/ size wordsize))
217 (emit "movq %rax, ~a(%rbp)" (get-offset name)))))
221 (reverse (sccs (graph bindings))))])
223 (for-each (lambda (form)
224 (codegen-expr form inner-si inner-env))
227 (define (codegen-var e si env)
228 (let* ([stack-size (on-stack? e)]
229 [name (if (on-stack? e) (caddr e) e)]
230 [stack-offset (cdr (assoc name (env-bindings env)))])
231 (when (not stack-offset)
232 (error #f (format "Variable ~a is not bound" name)))
236 (emit "leaq ~a(%rbp), %rsi" (- stack-offset stack-size))
237 (emit "leaq ~a(%rbp), %rdi" (- si stack-size))
238 (emit "movq $~a, %rcx" (/ stack-size wordsize))
240 (emit "movq ~a(%rbp), %rax" stack-offset))))
242 (define cur-lambda 0)
243 (define (fresh-lambda)
244 (set! cur-lambda (+ 1 cur-lambda))
245 (format "_lambda~a" (- cur-lambda 1)))
247 ; a closure on the heap looks like:
249 ; addr var1.... var2.... var3....
251 (define (codegen-closure label captured si env)
252 (let* ((heap-offsets (map (lambda (i) (+ 8 (* 8 i)))
253 (range 0 (length captured))))) ; 4, 12, 20, etc.
255 (emit "## creating closure")
257 (emit "movq heap_start@GOTPCREL(%rip), %rbx")
259 (emit "movq (%rbx), %rax") ; %rax = heap addr of closure
262 ; point heap_start to next space
263 (emit "addq $~a, (%rbx)" (+ 8 (* 8 (length captured))))
265 (emit "## storing address to lambda")
266 ; store the address to the lambda code
267 (emit "movq ~a@GOTPCREL(%rip), %rbx" label)
268 (emit "movq %rbx, 0(%rax)")
270 (emit "## storing captives")
271 ; store the captured vars
273 (lambda (var-name heap-offset)
274 (let ([stack-offset (cdr (assoc var-name (env-bindings env)))])
275 (emit "### captive ~a" var-name)
276 (if (eqv? stack-offset 'self-captive)
277 ; captive refers to this closure:
278 ; move heap addr of this closure to stack!
279 (emit "movq %rax, ~a(%rax)" heap-offset)
281 (emit "movq ~a(%rbp), %rbx" stack-offset)
282 (emit "movq %rbx, ~a(%rax)" heap-offset)))))
286 ; for now we can only call closures
287 (define (codegen-call f args si env)
288 (codegen-expr f si env)
290 (emit "## starting call")
292 (emit "movq %rax, ~a(%rbp)" si) ; store address of closure first on stack
294 ; codegen the arguments, store them intermediately
298 (emit "## arg no. ~a" (- i 1))
299 (codegen-expr e (- si (* wordsize i)) env)
300 ; store intermediate result on stack
301 (emit "movq %rax, ~a(%rbp)" (- si (* wordsize i)))))
303 args (range 1 (length args)))
305 ; now that we have everything we need on the stack,
306 ; move them into the param registers
308 (emit "## moving args into place")
310 (lambda (i) (emit "movq ~a(%rbp), ~a"
311 (- si (* wordsize i))
313 (range 1 (length args)))
315 ; todo: can this be made more efficient
316 (emit "movq ~a(%rbp), %rax" si) ; load back pointer to closure
318 (emit "## moving captives into place")
320 ; move captives into first argument
321 (emit "movq %rax, %rbx")
322 (emit "addq $8, %rbx")
323 (emit "movq %rbx, ~a" (param-register 0))
325 (emit "## performing call")
327 (emit "addq $~a, %rsp" si) ; adjust the stack pointer to account all the stuff we put in the env
328 (emit "callq *(%rax)") ; call closure function
329 (emit "subq $~a, %rsp" si))
332 ; 1st param: pointer to captured args
334 ; 3rd param: 2nd arg, etc.
336 (define (codegen-lambda l)
337 (let* ((label (car l))
339 (captives (car stuff))
342 ; params = what actually gets passed
343 (params (append captives args))
345 (stack-offsets (map (lambda (i)
346 (* (- wordsize) (+ 1 i)))
347 (range 0 (length params))))
349 [bindings (map cons params stack-offsets)]
350 [env (make-env '() bindings)])
353 (display "## lambda captives: ")
356 (display "## lambda args: ")
359 (display "## lambda body: ")
363 (emit "push %rbp") ; preserve caller's base pointer
365 (emit "movq %rsp, %rbp") ; set up our own base pointer
367 ; load the captured vars onto the stack
371 (emit "# loading captive ~a" (list-ref captives i))
372 (emit "movq ~a(~a), %rbx" (* wordsize i) (param-register 0))
373 (emit "movq %rbx, ~a(%rbp)" (* (- wordsize) (+ 1 i)))))
374 (range 0 (length captives)))
376 ; load the args onto the stack
380 (emit "movq ~a, %rbx" (param-register (+ 1 i)))
381 (emit "movq %rbx, ~a(%rbp)"
383 (+ 1 (length captives) i)))))
384 (range 0 (length args)))
386 (codegen-expr body (* (- wordsize) (+ 1 (length params))) env)
388 (emit "pop %rbp") ; restore caller's base pointer
392 (define (fresh-label)
393 (set! cur-label (+ 1 cur-label))
394 (format "label~a" (- cur-label 1)))
396 (define (codegen-if cond then else si env)
397 (codegen-expr cond si env)
398 (emit "cmpq $0, %rax")
399 (let ((exit-label (fresh-label))
400 (else-label (fresh-label)))
401 (emit "je ~a" else-label)
402 (codegen-expr then si env)
403 (emit "jmp ~a" exit-label)
404 (emit "~a:" else-label)
405 (codegen-expr else si env)
406 (emit "~a:" exit-label)))
408 (define (data-tor env e)
409 (if (not (list? e)) #f
410 (assoc (car e) (flat-map data-tors (env-data-layouts env)))))
412 ; returns the internal offset in bytes of a product within an ADT
413 ; given the constructor layout
414 ; constructor-layout: (foo (Int Bool))
415 (define (data-product-offset data-layouts type sum index)
416 (let* ([products (cdr (assoc sum (cdr (assoc type data-layouts))))]
417 [to-traverse (list-head products index)])
419 (lambda (acc t) (+ acc (type-size data-layouts t)))
420 wordsize ; skip the tag in the first word
423 (define (data-sum-tag data-layouts type sum)
425 (define (go acc sums)
426 (when (null? sums) (error #f "data-sum-tag no sum for type" sum type))
427 (if (eqv? sum (car sums))
429 (go (+ 1 acc) (cdr sums))))
430 (let* ([type-sums (cdr (assoc type data-layouts))])
431 (go 0 (map car type-sums))))
433 (define (codegen-data-tor e si env)
435 (define (codegen-destructor tor)
436 (let* ([res (codegen-expr (cadr e) si env)]
441 (when (not (on-stack? (cadr e)))
442 (error #f "trying to destruct something that isn't a stack expression"))
443 (emit "# deconstructing")
444 (emit "movq ~a(%rbp), %rax"
445 (- si (data-product-offset (env-data-layouts env) type sum index)))))
447 (define (codegen-constructor tor)
448 (let* ([info (cadr tor)]
451 [constructor (car e)]
455 [tag (data-sum-tag (env-data-layouts env)
461 (let ([res (codegen-expr expr si env)]
462 [stack-offset (- si (data-product-offset (env-data-layouts env)
466 (error #f "todo: handle stack-exprs in stack exprs")
467 (emit "movq %rax, ~a(%rbp)" stack-offset))))])
470 (emit "movq $~a, ~a(%rbp)" tag si)
472 (for-each insert-product args (range 0 (length args)))
473 (type-size (env-data-layouts env) type)))
475 (let* ([tor (data-tor env e)]
476 [constructor (eqv? 'constructor (caddr (cadr tor)))])
478 (codegen-constructor tor)
479 (codegen-destructor tor))))
481 (define (codegen-expr e si env)
484 ('closure (codegen-closure (cadr e) (caddr e) si env))
487 ('+ (codegen-add (cdr e) si env))
488 ('- (codegen-sub (cadr e) (caddr e) si env))
489 ('* (codegen-mul (cadr e) (caddr e) si env))
490 ('! (codegen-not (cadr e) si env))
491 ('= (codegen-eq (cadr e) (caddr e) si env))
492 ('bool->int (codegen-expr (cadr e) si env))
493 ('print (codegen-print (cadr e) si env))
496 (codegen-data-tor e si env)
497 (codegen-call (car e) (cdr e) si env)))))
499 ; this is a builtin being passed around as a variable
500 ; this should have been converted to a closure!
501 ('builtin (error #f "passing about a builtin!" e))
503 ('let (codegen-let (let-bindings e)
508 ('var (codegen-var e si env))
510 ('if (codegen-if (cadr e) (caddr e) (cadddr e) si env))
512 ('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
513 ('int-literal (emit "movq $~a, %rax" e))
515 ('static-string (emit "movq ~a@GOTPCREL(%rip), %rax"
518 ('stack (case (ast-type (caddr e))
519 ['var (codegen-var e si env)]
520 [else (codegen-expr (caddr e) si env)]))
522 (else (error #f "don't know how to codegen this"))))
524 ; takes in a expr annotated with types and returns a type-less AST
525 ; with stack values wrapped
526 (define (annotate-stack-values data-layouts ann-e)
527 (define (stack-type? type)
528 (assoc type data-layouts))
530 (ast-traverse strip (ann-expr e)))
531 (let* ([e (ann-expr ann-e)]
532 [type (ann-type ann-e)])
533 (if (stack-type? type)
534 `(stack ,(type-size data-layouts type) ,(ast-traverse strip e))
535 (ast-traverse (lambda (x)
536 (annotate-stack-values data-layouts x))
539 (define (free-vars prog)
543 ('builtin '()) ; do nothing
544 ('var (if (memv e bound) '() (list e)))
547 (set! bound (append (lambda-args e) bound))
548 (collect (lambda-body e))))
550 ('app (flat-map collect e))
551 ('if (flat-map collect (cdr e)))
553 (let ([bind-fvs (flat-map (lambda (a)
555 (set! bound (cons (car a) bound))
558 (append bind-fvs (flat-map collect (let-body e)))))
562 ; ((lambda (x) (+ x y)) 42) => ((closure lambda1 (y)) 42)
563 ; [(lambda1 . ((y), (x), (+ x y))]
564 ; for builtins, this generates a closure if it is used
565 ; outside of an immediate app
566 ; but only one closure for each builtin
568 (define (extract-lambdas program)
570 (define (add-lambda e)
571 (let* ((label (fresh-lambda))
572 (args (lambda-args e))
573 (captured (free-vars e))
574 (body (extract (lambda-body e)))
575 (new-lambda (cons label (list captured args body))))
576 (set! lambdas (cons new-lambda lambdas))
577 `(closure ,label ,captured))) ; todo: should we string->symbol?
579 (define (find-builtin-lambda e)
580 (let [(l (assq (builtin-name e) lambdas))]
581 (if l `(closure ,(car l) ,(caadr l)) #f)))
583 (define (builtin-name e)
590 ('bool->int "_bool2int")
592 (else (error #f "don't know this builtin"))))
593 (define (builtin-args e)
602 (else (error #f "don't know this builtin"))))
604 (define (add-builtin-lambda e)
605 (let* [(label (builtin-name e))
607 (args (builtin-args e))
609 (new-lambda (cons label (list captured args body)))]
610 (set! lambdas (cons new-lambda lambdas))
611 `(closure ,label ,captured)))
615 ('lambda (add-lambda e))
616 ('let `(let ,(map (lambda (b) `(,(car b) ,@(extract (cdr b)))) (let-bindings e))
617 ,@(map extract (let-body e))))
619 ; if a builtin is used as a function, don't generate lambda
620 (if (eqv? 'builtin (ast-type (car e)))
622 (list (extract (car e))))
623 (map extract (cdr e))))
626 (if (find-builtin-lambda e)
627 (find-builtin-lambda e)
628 (add-builtin-lambda e)))
631 (else (ast-traverse extract e))))
632 (let ((transformed (extract program)))
633 (cons lambdas transformed)))
635 (define (extract-strings program)
637 (strings '())) ; assoc list of labels -> string
638 (define (fresh-string)
639 (set! cur-string (+ cur-string 1))
640 (format "string~a" (- cur-string 1)))
644 (let ((label (fresh-string)))
645 (set! strings (cons (cons label e) strings))
646 `(static-string ,label)))
647 (else (ast-traverse extract e))))
648 (let ((transformed (extract program)))
649 (cons strings transformed))))
651 (define (emit-string-data s)
653 (emit "\t.string \"~a\"" (cdr s)))
656 ; 16(%rbp) mem arg 0 prev frame
657 ; -----------------------
658 ; 8(%rbp) return address cur frame
660 ; -8(%rbp) do what you want
661 ; ... do what you want
662 ; 0(%rsp) do what you want
664 (define (param-register n)
672 (else (error #f "need to test out the below"))
673 (else (format "~a(%rsp)" (- n 6)))))
675 (define (initialize-heap)
678 ('darwin "0x20000c5")
680 ; allocate some heap memory
681 (emit "mov $~a, %rax" mmap) ; mmap
682 (emit "xor %rdi, %rdi") ; addr = null
683 (emit "movq $1024, %rsi") ; length = 1kb
684 (emit "movq $0x3, %rdx") ; prot = read | write = 0x2 | 0x1
685 ; flags = anonymous | private
687 ('darwin (emit "movq $0x1002, %r10")) ; anon = 0x1000, priv = 0x02
688 ('linux (emit "movq $0x22, %r10"))) ; anon = 0x20, priv = 0x02
689 (emit "movq $-1, %r8") ; fd = -1
690 (emit "xor %r9, %r9") ; offset = 0
692 ; %rax now contains pointer to the start of the heap
695 (emit "movq heap_start@GOTPCREL(%rip), %rsi")
696 (emit "movq %rax, (%rsi)")))
698 (define (codegen program)
701 (let* ([data-layouts (program-data-layouts program)]
703 [pattern-matched (expand-pattern-matches program)]
704 [type-annotated (annotate-types pattern-matched)]
705 [stack-annotated (annotate-stack-values data-layouts
708 (strings-res (extract-strings stack-annotated))
709 (strings (car strings-res))
710 (lambdas-res (extract-lambdas (cdr strings-res)))
711 (lambdas (car lambdas-res))
712 (xform-prog (cdr lambdas-res)))
714 (emit "\t.global _start")
716 ; (emit ".p2align 4,,15") is this needed?
718 (for-each codegen-lambda lambdas)
724 (emit "movq %rsp, %rbp") ; set up the base pointer
726 (codegen-expr xform-prog (- wordsize) (make-env data-layouts '()))
729 (emit "mov %rax, %rdi")
731 ('darwin (emit "movq $0x2000001, %rax"))
732 ('linux (emit "mov $60, %rax")))
740 (for-each emit-string-data strings)))
742 (define (compile-to-binary program output t)
744 (when (not (eq? (typecheck program) 'Int)) (error #f "not an Int"))
745 (let ([tmp-path "/tmp/a.s"])
746 (when (file-exists? tmp-path) (delete-file tmp-path))
747 (with-output-to-file tmp-path
748 (lambda () (codegen program)))
752 (system "as /tmp/a.s -o /tmp/a.o")
753 (system (format "ld /tmp/a.o -e _start -macosx_version_min 10.14 -static -o ~a" output)))
755 (system "as /tmp/a.s -o /tmp/a.o")
756 (system (format "ld /tmp/a.o -o ~a" output))))))
759 ; syscalls in linux and darwin use the following arguments for syscall instruction:
768 ; on darwin, unix/posix syscalls are offset by 0x2000000 (syscall classes)
769 ; https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/kern/syscalls.master
770 ; documentation for most syscalls: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys