5 (define target host-os)
14 (define (stack-type? data-layouts type)
15 (if (assoc type data-layouts) #t #f))
18 (define (type-size data-layouts type)
20 (define (adt-size adt)
23 (fold-left (lambda (acc x) (+ acc (type-size data-layouts x)))
24 wordsize ; one word needed to store tag
33 (let ([adt (assoc type data-layouts)])
36 (error #f "unknown size" type)))]))
38 ; returns the size of an expression's result in bytes
40 (if (eqv? (ast-type e) 'stack)
44 (define (on-stack? expr)
49 ; does a movsq for something on the stack
50 (define (emit-stack-copy src dst size)
51 (emit "leaq ~a(%rbp), %rsi" (- src size))
52 (emit "leaq ~a(%rbp), %rdi" (- dst size))
53 (emit "movq $~a, %rcx" (/ size wordsize))
57 ; an environment consists of adt layouts in scope,
58 ; and any bound variables.
59 ; bound variables are an assoc list with their stack offset
60 (define make-env list)
61 (define env-data-layouts car)
62 (define env-bindings cadr)
64 (define (codegen-add xs si env)
67 (emit "movq ~a(%rbp), %rax" si)
71 (emit "addq $~a, ~a(%rbp)" y si)
73 (codegen-expr y (- si wordsize) env)
74 (emit "addq %rax, ~a(%rbp)" si))))
77 ; use si(%rbp) as the accumulator
78 (emit "movq $0, ~a(%rbp)" si)
81 (define (codegen-binop opcode)
83 (codegen-expr b si env)
84 (emit "movq %rax, ~a(%rbp)" si)
85 (codegen-expr a (- si wordsize) env)
86 (emit "~a ~a(%rbp), %rax" opcode si)))
88 (define codegen-sub (codegen-binop "sub"))
90 (define codegen-mul (codegen-binop "imul"))
92 (define (codegen-not x si env)
93 (codegen-expr x si env)
95 (emit "andq $1, %rax"))
97 (define (codegen-eq a b si env)
98 (codegen-expr a si env)
99 (emit "movq %rax, ~a(%rbp)" si)
100 (codegen-expr b (- si wordsize) env)
101 (emit "## ~a = ~b" a b)
102 (emit "cmpq ~a(%rbp), %rax" si)
105 ; 'write file handle addr-string num-bytes
107 (define (codegen-print x si env)
108 (codegen-expr x si env) ; x should be a static-string, producing a label
110 ; make a copy of string address since %rax and %rdi are clobbered
111 (emit "mov %rax, %rbx")
113 ; get the length of the null terminated string
114 (emit "mov %rax, %rdi")
115 (emit "xor %al, %al") ; set %al to 0
116 (emit "mov $-1, %rcx") ; max search length = max int = -1
117 (emit "cld") ; clear direction flag, search up in memory
118 (emit "repne scasb") ; scan string, %rcx = -strlen - 1 - 1
120 (emit "not %rcx") ; -%rcx = strlen + 1
123 (emit "movq %rbx, %rsi") ; string addr
124 (emit "movq %rcx, %rdx") ; num bytes
125 (emit "movq $1, %rdi") ; file handle (stdout)
127 ('darwin (emit "mov $0x2000004, %rax")) ; syscall 4 (write)
128 ('linux (emit "mov $1, %rax"))) ; syscall 1 (write)
131 (define (codegen-let bindings body si env)
133 ; is this a closure that captures itself?
134 ; e.g. (let ([x 3] [f (closure lambda0 (f x))]) (f))
135 (define (self-captive-closure? name expr)
136 (and (eqv? (ast-type expr) 'closure)
137 (memv name (caddr expr))))
139 ;; (define (emit-scc scc env)
140 ;; ; acc is a pair of the env and list of touchups
141 ;; (define (emit-binding acc binding)
142 ;; (let ([binding-name (car binding)]
143 ;; [binding-body (cadr binding)]
145 ;; [other-bindings (filter
146 ;; (lambda (x) (not (eqv? binding-name x)))
148 ;; [mutually-recursives
150 ;; (lambda (other-binding)
151 ;; (memv other-binding (references binding-body)))
154 ;; [new-touchups (append touchups (cdr acc))])
156 ;; ; TODO: assert that the only mutually recursives are closures
159 ;; (when (not (eqv? (ast-type (cadr binding))
162 ;; (cons new-env new-touchups)
165 ;; (fold-left emit-binding (cons env '()) scc))))
166 ; assoc map of binding name to size
168 (map (lambda (binding) (cons (car binding) (expr-size (cadr binding))))
171 ; assoc map of binding name to offset
172 (define stack-offsets
174 (let* ([totals ; 2 6 8 16 22
175 (reverse (fold-left (lambda (acc x)
178 (cons (+ x (car acc)) acc)))
180 (map cdr stack-sizes)))]
182 [relative-offsets (map - totals (map cdr stack-sizes))]
183 [absolute-offsets (map (lambda (x) (- si x)) relative-offsets)])
184 (map cons (map car stack-sizes) absolute-offsets)))
187 ; the stack index used when codegening binding body and main body
189 ; [stack-offsets | inner-si]
190 [inner-si (- si (fold-left + 0 (map cdr stack-sizes)))]
192 [get-offset (lambda (n) (cdr (assoc n stack-offsets)))]
197 (let* ([scc-binding-offsets
200 (cons (cons name (get-offset name))
204 [scc-env (make-env (env-data-layouts env) scc-binding-offsets)])
207 (let* ([expr (cadr (assoc name bindings))]
208 [size (expr-size expr)])
209 (emit "## generating ~a with scc-env ~a" name scc-env)
210 (if (self-captive-closure? name expr)
211 ; if self-captive, insert a flag into the environment to let
212 ; codegen-closure realise this!
216 (env-data-layouts scc-env)
217 (cons (cons name 'self-captive)
218 (env-bindings scc-env))))
219 (codegen-expr expr inner-si scc-env))
222 ; copy over whatevers on the stack
223 (emit-stack-copy inner-si (get-offset name) size)
224 (emit "movq %rax, ~a(%rbp)" (get-offset name)))))
228 (reverse (sccs (graph bindings))))])
230 (for-each (lambda (form)
231 (codegen-expr form inner-si inner-env))
234 (define (codegen-var e si env)
235 (let* ([stack-size (on-stack? e)]
236 [name (if (on-stack? e) (caddr e) e)]
237 [stack-offset (cdr (assoc name (env-bindings env)))])
238 (when (not stack-offset)
239 (error #f (format "Variable ~a is not bound" name)))
242 (emit-stack-copy stack-offset si stack-size)
243 (emit "movq ~a(%rbp), %rax" stack-offset))))
245 (define cur-lambda 0)
246 (define (fresh-lambda)
247 (set! cur-lambda (+ 1 cur-lambda))
248 (format "_lambda~a" (- cur-lambda 1)))
250 ; a closure on the heap looks like:
252 ; addr var1.... var2.... var3....
254 (define (codegen-closure label captured si env)
255 (let* ((heap-offsets (map (lambda (i) (+ 8 (* 8 i)))
256 (range 0 (length captured))))) ; 4, 12, 20, etc.
258 (emit "## creating closure")
260 (emit "movq heap_start@GOTPCREL(%rip), %rbx")
262 (emit "movq (%rbx), %rax") ; %rax = heap addr of closure
265 ; point heap_start to next space
266 (emit "addq $~a, (%rbx)" (+ 8 (* 8 (length captured))))
268 (emit "## storing address to lambda")
269 ; store the address to the lambda code
270 (emit "movq ~a@GOTPCREL(%rip), %rbx" label)
271 (emit "movq %rbx, 0(%rax)")
273 (emit "## storing captives")
274 ; store the captured vars
276 (lambda (var-name heap-offset)
277 (let ([stack-offset (cdr (assoc var-name (env-bindings env)))])
278 (emit "### captive ~a" var-name)
279 (if (eqv? stack-offset 'self-captive)
280 ; captive refers to this closure:
281 ; move heap addr of this closure to stack!
282 (emit "movq %rax, ~a(%rax)" heap-offset)
284 (emit "movq ~a(%rbp), %rbx" stack-offset)
285 (emit "movq %rbx, ~a(%rax)" heap-offset)))))
289 ; for now we can only call closures
290 (define (codegen-call f args si env)
291 (codegen-expr f si env)
293 (emit "## starting call")
295 (emit "movq %rax, ~a(%rbp)" si) ; store address of closure first on stack
297 ; codegen the arguments, store them intermediately
301 (emit "## arg no. ~a" (- i 1))
302 (codegen-expr e (- si (* wordsize i)) env)
303 ; store intermediate result on stack
304 (emit "movq %rax, ~a(%rbp)" (- si (* wordsize i)))))
306 args (range 1 (length args)))
308 ; now that we have everything we need on the stack,
309 ; move them into the param registers
311 (emit "## moving args into place")
313 (lambda (i) (emit "movq ~a(%rbp), ~a"
314 (- si (* wordsize i))
316 (range 1 (length args)))
318 ; todo: can this be made more efficient
319 (emit "movq ~a(%rbp), %rax" si) ; load back pointer to closure
321 (emit "## moving captives into place")
323 ; move captives into first argument
324 (emit "movq %rax, %rbx")
325 (emit "addq $8, %rbx")
326 (emit "movq %rbx, ~a" (param-register 0))
328 (emit "## performing call")
330 (emit "addq $~a, %rsp" si) ; adjust the stack pointer to account all the stuff we put in the env
331 (emit "callq *(%rax)") ; call closure function
332 (emit "subq $~a, %rsp" si))
335 ; 1st param: pointer to captured args
337 ; 3rd param: 2nd arg, etc.
339 (define (codegen-lambda l)
340 (let* ((label (car l))
342 (captives (car stuff))
345 ; params = what actually gets passed
346 (params (append captives args))
348 (stack-offsets (map (lambda (i)
349 (* (- wordsize) (+ 1 i)))
350 (range 0 (length params))))
352 [bindings (map cons params stack-offsets)]
353 [env (make-env '() bindings)])
356 (display "## lambda captives: ")
359 (display "## lambda args: ")
362 (display "## lambda body: ")
366 (emit "push %rbp") ; preserve caller's base pointer
368 (emit "movq %rsp, %rbp") ; set up our own base pointer
370 ; load the captured vars onto the stack
374 (emit "# loading captive ~a" (list-ref captives i))
375 (emit "movq ~a(~a), %rbx" (* wordsize i) (param-register 0))
376 (emit "movq %rbx, ~a(%rbp)" (* (- wordsize) (+ 1 i)))))
377 (range 0 (length captives)))
379 ; load the args onto the stack
383 (emit "movq ~a, %rbx" (param-register (+ 1 i)))
384 (emit "movq %rbx, ~a(%rbp)"
386 (+ 1 (length captives) i)))))
387 (range 0 (length args)))
389 (codegen-expr body (* (- wordsize) (+ 1 (length params))) env)
391 (emit "pop %rbp") ; restore caller's base pointer
395 (define (fresh-label)
396 (set! cur-label (+ 1 cur-label))
397 (format "label~a" (- cur-label 1)))
399 (define (codegen-if cond then else si env)
400 (codegen-expr cond si env)
401 (emit "cmpq $0, %rax")
402 (let ((exit-label (fresh-label))
403 (else-label (fresh-label)))
404 (emit "je ~a" else-label)
405 (codegen-expr then si env)
406 (emit "jmp ~a" exit-label)
407 (emit "~a:" else-label)
408 (codegen-expr else si env)
409 (emit "~a:" exit-label)))
411 (define (data-tor env e)
412 (if (not (list? e)) #f
413 (assoc (car e) (flat-map data-tors (env-data-layouts env)))))
415 ; returns the internal offset in bytes of a product within an ADT
416 ; given the constructor layout
417 ; constructor-layout: (foo (Int Bool))
418 (define (data-product-offset data-layouts type sum index)
419 (let* ([products (cdr (assoc sum (cdr (assoc type data-layouts))))]
420 [to-traverse (list-head products index)])
422 (lambda (acc t) (+ acc (type-size data-layouts t)))
423 wordsize ; skip the tag in the first word
426 (define (data-sum-tag data-layouts type sum)
428 (define (go acc sums)
429 (when (null? sums) (error #f "data-sum-tag no sum for type" sum type))
430 (if (eqv? sum (car sums))
432 (go (+ 1 acc) (cdr sums))))
433 (let* ([type-sums (cdr (assoc type data-layouts))])
434 (go 0 (map car type-sums))))
436 (define (codegen-data-tor e si env)
438 (define dls (env-data-layouts env))
440 (define (codegen-destructor tor)
441 (let* ([res (codegen-expr (cadr e) si env)]
446 [product-type (cadddr info)]
447 [product-type-size (type-size dls product-type)]
449 [safe-space-offset (- si (type-size dls type))]
451 [inner-offset (- si (data-product-offset dls type sum index))])
453 (when (not (on-stack? (cadr e)))
454 (error #f "trying to destruct something that isn't a stack expression"))
455 (emit "# deconstructing")
457 (if (stack-type? (env-data-layouts env) product-type)
458 ; if copying from the stack, need to first copy
459 ; to a safe space above to avoid overwriting
460 ; the original result on the stack
461 ; this is bad. please remove this in the rewrite.
463 (emit-stack-copy inner-offset safe-space-offset product-type-size)
464 (emit-stack-copy safe-space-offset si product-type-size))
465 (emit "movq ~a(%rbp), %rax" inner-offset))))
467 (define (codegen-constructor tor)
468 (let* ([info (cadr tor)]
471 [constructor (car e)]
475 [tag (data-sum-tag (env-data-layouts env)
479 [inner-si (- si (type-size dls type))]
481 [product-types (cdr (assoc sum (cdr (assoc type dls))))]
484 (lambda (expr i product-type)
486 (- si (data-product-offset dls type sum i))]
487 [product-size (type-size dls product-type)])
488 (codegen-expr expr inner-si env)
490 (emit-stack-copy inner-si dest-offset product-size)
491 (emit "movq %rax, ~a(%rbp)" dest-offset))))])
494 (emit "movq $~a, ~a(%rbp)" tag si)
496 (for-each insert-product args (range 0 (length args)) product-types)))
498 (let* ([tor (data-tor env e)]
499 [constructor (eqv? 'constructor (caddr (cadr tor)))])
501 (codegen-constructor tor)
502 (codegen-destructor tor))))
504 (define (codegen-expr e si env)
507 ('closure (codegen-closure (cadr e) (caddr e) si env))
510 ('+ (codegen-add (cdr e) si env))
511 ('- (codegen-sub (cadr e) (caddr e) si env))
512 ('* (codegen-mul (cadr e) (caddr e) si env))
513 ('! (codegen-not (cadr e) si env))
514 ('= (codegen-eq (cadr e) (caddr e) si env))
515 ('bool->int (codegen-expr (cadr e) si env))
516 ('print (codegen-print (cadr e) si env))
519 (codegen-data-tor e si env)
520 (codegen-call (car e) (cdr e) si env)))))
522 ; this is a builtin being passed around as a variable
523 ; this should have been converted to a closure!
524 ('builtin (error #f "passing about a builtin!" e))
526 ('let (codegen-let (let-bindings e)
531 ('var (codegen-var e si env))
533 ('if (codegen-if (cadr e) (caddr e) (cadddr e) si env))
535 ('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
536 ('int-literal (emit "movq $~a, %rax" e))
538 ('static-string (emit "movq ~a@GOTPCREL(%rip), %rax"
541 ('stack (case (ast-type (caddr e))
542 ['var (codegen-var e si env)]
543 [else (codegen-expr (caddr e) si env)]))
545 (else (error #f "don't know how to codegen this")))
546 (emit "# done ~a" e))
548 ; takes in a expr annotated with types and returns a type-less AST
549 ; with stack values wrapped
550 (define (annotate-stack-values data-layouts ann-e)
551 (define (stack-type? type)
552 (assoc type data-layouts))
554 (ast-traverse strip (ann-expr e)))
555 (let* ([e (ann-expr ann-e)]
556 [type (ann-type ann-e)])
557 (if (stack-type? type)
558 `(stack ,(type-size data-layouts type)
559 ,(ast-traverse (lambda (x) (annotate-stack-values data-layouts x)) e))
560 (ast-traverse (lambda (x)
561 (annotate-stack-values data-layouts x))
564 (define (free-vars prog)
568 ('builtin '()) ; do nothing
569 ('var (if (memv e bound) '() (list e)))
572 (set! bound (append (lambda-args e) bound))
573 (collect (lambda-body e))))
575 ('app (flat-map collect e))
576 ('if (flat-map collect (cdr e)))
578 (let ([bind-fvs (flat-map (lambda (a)
580 (set! bound (cons (car a) bound))
583 (append bind-fvs (flat-map collect (let-body e)))))
587 ; ((lambda (x) (+ x y)) 42) => ((closure lambda1 (y)) 42)
588 ; [(lambda1 . ((y), (x), (+ x y))]
589 ; for builtins, this generates a closure if it is used
590 ; outside of an immediate app
591 ; but only one closure for each builtin
593 (define (extract-lambdas program)
595 (define (add-lambda e)
596 (let* ((label (fresh-lambda))
597 (args (lambda-args e))
598 (captured (free-vars e))
599 (body (extract (lambda-body e)))
600 (new-lambda (cons label (list captured args body))))
601 (set! lambdas (cons new-lambda lambdas))
602 `(closure ,label ,captured))) ; todo: should we string->symbol?
604 (define (find-builtin-lambda e)
605 (let [(l (assq (builtin-name e) lambdas))]
606 (if l `(closure ,(car l) ,(caadr l)) #f)))
608 (define (builtin-name e)
615 ('bool->int "_bool2int")
617 (else (error #f "don't know this builtin"))))
618 (define (builtin-args e)
627 (else (error #f "don't know this builtin"))))
629 (define (add-builtin-lambda e)
630 (let* [(label (builtin-name e))
632 (args (builtin-args e))
634 (new-lambda (cons label (list captured args body)))]
635 (set! lambdas (cons new-lambda lambdas))
636 `(closure ,label ,captured)))
640 ('lambda (add-lambda e))
641 ('let `(let ,(map (lambda (b) `(,(car b) ,@(extract (cdr b)))) (let-bindings e))
642 ,@(map extract (let-body e))))
644 ; if a builtin is used as a function, don't generate lambda
645 (if (eqv? 'builtin (ast-type (car e)))
647 (list (extract (car e))))
648 (map extract (cdr e))))
651 (if (find-builtin-lambda e)
652 (find-builtin-lambda e)
653 (add-builtin-lambda e)))
656 (else (ast-traverse extract e))))
657 (let ((transformed (extract program)))
658 (cons lambdas transformed)))
660 (define (extract-strings program)
662 (strings '())) ; assoc list of labels -> string
663 (define (fresh-string)
664 (set! cur-string (+ cur-string 1))
665 (format "string~a" (- cur-string 1)))
669 (let ((label (fresh-string)))
670 (set! strings (cons (cons label e) strings))
671 `(static-string ,label)))
672 (else (ast-traverse extract e))))
673 (let ((transformed (extract program)))
674 (cons strings transformed))))
676 (define (emit-string-data s)
678 (emit "\t.string \"~a\"" (cdr s)))
681 ; 16(%rbp) mem arg 0 prev frame
682 ; -----------------------
683 ; 8(%rbp) return address cur frame
685 ; -8(%rbp) do what you want
686 ; ... do what you want
687 ; 0(%rsp) do what you want
689 (define (param-register n)
697 (else (error #f "need to test out the below"))
698 (else (format "~a(%rsp)" (- n 6)))))
700 (define (initialize-heap)
703 ('darwin "0x20000c5")
705 ; allocate some heap memory
706 (emit "mov $~a, %rax" mmap) ; mmap
707 (emit "xor %rdi, %rdi") ; addr = null
708 (emit "movq $1024, %rsi") ; length = 1kb
709 (emit "movq $0x3, %rdx") ; prot = read | write = 0x2 | 0x1
710 ; flags = anonymous | private
712 ('darwin (emit "movq $0x1002, %r10")) ; anon = 0x1000, priv = 0x02
713 ('linux (emit "movq $0x22, %r10"))) ; anon = 0x20, priv = 0x02
714 (emit "movq $-1, %r8") ; fd = -1
715 (emit "xor %r9, %r9") ; offset = 0
717 ; %rax now contains pointer to the start of the heap
720 (emit "movq heap_start@GOTPCREL(%rip), %rsi")
721 (emit "movq %rax, (%rsi)")))
723 (define (codegen program)
726 (let* ([data-layouts (program-data-layouts program)]
728 [pattern-matched (expand-pattern-matches program)]
729 [type-annotated (annotate-types pattern-matched)]
730 [stack-annotated (annotate-stack-values data-layouts
733 (strings-res (extract-strings stack-annotated))
734 (strings (car strings-res))
735 (lambdas-res (extract-lambdas (cdr strings-res)))
736 (lambdas (car lambdas-res))
737 (xform-prog (cdr lambdas-res)))
739 (emit "\t.global _start")
741 ; (emit ".p2align 4,,15") is this needed?
743 (for-each codegen-lambda lambdas)
749 (emit "movq %rsp, %rbp") ; set up the base pointer
751 (codegen-expr xform-prog (- wordsize) (make-env data-layouts '()))
754 (emit "mov %rax, %rdi")
756 ('darwin (emit "movq $0x2000001, %rax"))
757 ('linux (emit "mov $60, %rax")))
765 (for-each emit-string-data strings)))
767 (define (compile-to-binary program output t)
769 (when (not (eq? (typecheck program) 'Int)) (error #f "not an Int"))
770 (let ([tmp-path "/tmp/a.s"])
771 (when (file-exists? tmp-path) (delete-file tmp-path))
772 (with-output-to-file tmp-path
773 (lambda () (codegen program)))
777 (system "as /tmp/a.s -o /tmp/a.o")
778 (system (format "ld /tmp/a.o -e _start -macosx_version_min 10.14 -static -o ~a" output)))
780 (system "as /tmp/a.s -o /tmp/a.o")
781 (system (format "ld /tmp/a.o -o ~a" output))))))
784 ; syscalls in linux and darwin use the following arguments for syscall instruction:
793 ; on darwin, unix/posix syscalls are offset by 0x2000000 (syscall classes)
794 ; https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/kern/syscalls.master
795 ; documentation for most syscalls: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys