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 (define (on-stack? expr)
39 ; an environment consists of adt layouts in scope,
40 ; and any bound variables.
41 ; bound variables are an assoc list with their stack offset
42 (define make-env list)
43 (define env-data-layouts car)
44 (define env-bindings cadr)
46 (define (codegen-add xs si env)
49 (emit "movq ~a(%rbp), %rax" si)
53 (emit "addq $~a, ~a(%rbp)" y si)
55 (codegen-expr y (- si wordsize) env)
56 (emit "addq %rax, ~a(%rbp)" si))))
59 ; use si(%rbp) as the accumulator
60 (emit "movq $0, ~a(%rbp)" si)
63 (define (codegen-binop opcode)
65 (codegen-expr b si env)
66 (emit "movq %rax, ~a(%rbp)" si)
67 (codegen-expr a (- si wordsize) env)
68 (emit "~a ~a(%rbp), %rax" opcode si)))
70 (define codegen-sub (codegen-binop "sub"))
72 (define codegen-mul (codegen-binop "imul"))
74 (define (codegen-not x si env)
75 (codegen-expr x si env)
77 (emit "andq $1, %rax"))
79 (define (codegen-eq a b si env)
80 (codegen-expr a si env)
81 (emit "movq %rax, ~a(%rbp)" si)
82 (codegen-expr b (- si wordsize) env)
83 (emit "## ~a = ~b" a b)
84 (emit "cmpq ~a(%rbp), %rax" si)
87 ; 'write file handle addr-string num-bytes
89 (define (codegen-print x si env)
90 (codegen-expr x si env) ; x should be a static-string, producing a label
92 ; make a copy of string address since %rax and %rdi are clobbered
93 (emit "mov %rax, %rbx")
95 ; get the length of the null terminated string
96 (emit "mov %rax, %rdi")
97 (emit "xor %al, %al") ; set %al to 0
98 (emit "mov $-1, %rcx") ; max search length = max int = -1
99 (emit "cld") ; clear direction flag, search up in memory
100 (emit "repne scasb") ; scan string, %rcx = -strlen - 1 - 1
102 (emit "not %rcx") ; -%rcx = strlen + 1
105 (emit "movq %rbx, %rsi") ; string addr
106 (emit "movq %rcx, %rdx") ; num bytes
107 (emit "movq $1, %rdi") ; file handle (stdout)
109 ('darwin (emit "mov $0x2000004, %rax")) ; syscall 4 (write)
110 ('linux (emit "mov $1, %rax"))) ; syscall 1 (write)
113 (define (codegen-let bindings body si env)
115 ; is this a closure that captures itself?
116 ; e.g. (let ([x 3] [f (closure lambda0 (f x))]) (f))
117 (define (self-captive-closure? name expr)
118 (and (eqv? (ast-type expr) 'closure)
119 (memv name (caddr expr))))
122 ;; (define (emit-scc scc env)
123 ;; ; acc is a pair of the env and list of touchups
124 ;; (define (emit-binding acc binding)
125 ;; (let ([binding-name (car binding)]
126 ;; [binding-body (cadr binding)]
128 ;; [other-bindings (filter
129 ;; (lambda (x) (not (eqv? binding-name x)))
131 ;; [mutually-recursives
133 ;; (lambda (other-binding)
134 ;; (memv other-binding (references binding-body)))
137 ;; [new-touchups (append touchups (cdr acc))])
139 ;; ; TODO: assert that the only mutually recursives are closures
142 ;; (when (not (eqv? (ast-type (cadr binding))
145 ;; (cons new-env new-touchups)
148 ;; (fold-left emit-binding (cons env '()) scc))))
150 (let* ([stack-offsets (map (lambda (name x) ; assoc map of binding name to offset
151 (cons name (- si (* x wordsize))))
153 (range 0 (length bindings)))]
154 [inner-si (- si (* (length bindings) wordsize))]
156 [get-offset (lambda (n) (cdr (assoc n stack-offsets)))]
161 (let* ([scc-binding-offsets
164 (cons (cons name (get-offset name))
168 [scc-env (make-env (env-data-layouts env) scc-binding-offsets)])
171 (let ([expr (cadr (assoc name bindings))])
172 (emit "## generating ~a with scc-env ~a" name scc-env)
173 (if (self-captive-closure? name expr)
174 ; if self-captive, insert a flag into the environment to let
175 ; codegen-closure realise this!
179 (env-data-layouts scc-env)
180 (cons (cons name 'self-captive)
181 (env-bindings scc-env))))
182 (codegen-expr expr inner-si scc-env))
183 (emit "movq %rax, ~a(%rbp)" (get-offset name))))
187 (reverse (sccs (graph bindings))))])
189 (for-each (lambda (form)
190 (codegen-expr form inner-si inner-env))
193 (define (codegen-var name si env)
194 (let ([binding (assoc name (env-bindings env))])
196 (error #f (format "Variable ~a is not bound" name))
197 (emit "movq ~a(%rbp), %rax" (cdr binding)))))
199 (define cur-lambda 0)
200 (define (fresh-lambda)
201 (set! cur-lambda (+ 1 cur-lambda))
202 (format "_lambda~a" (- cur-lambda 1)))
204 ; a closure on the heap looks like:
206 ; addr var1.... var2.... var3....
208 (define (codegen-closure label captured si env)
209 (let* ((heap-offsets (map (lambda (i) (+ 8 (* 8 i)))
210 (range 0 (length captured))))) ; 4, 12, 20, etc.
212 (emit "## creating closure")
214 (emit "movq heap_start@GOTPCREL(%rip), %rbx")
216 (emit "movq (%rbx), %rax") ; %rax = heap addr of closure
219 ; point heap_start to next space
220 (emit "addq $~a, (%rbx)" (+ 8 (* 8 (length captured))))
222 (emit "## storing address to lambda")
223 ; store the address to the lambda code
224 (emit "movq ~a@GOTPCREL(%rip), %rbx" label)
225 (emit "movq %rbx, 0(%rax)")
227 (emit "## storing captives")
228 ; store the captured vars
230 (lambda (var-name heap-offset)
231 (let ([stack-offset (cdr (assoc var-name (env-bindings env)))])
232 (emit "### captive ~a" var-name)
233 (if (eqv? stack-offset 'self-captive)
234 ; captive refers to this closure:
235 ; move heap addr of this closure to stack!
236 (emit "movq %rax, ~a(%rax)" heap-offset)
238 (emit "movq ~a(%rbp), %rbx" stack-offset)
239 (emit "movq %rbx, ~a(%rax)" heap-offset)))))
243 ; for now we can only call closures
244 (define (codegen-call f args si env)
245 (codegen-expr f si env)
247 (emit "## starting call")
249 (emit "movq %rax, ~a(%rbp)" si) ; store address of closure first on stack
251 ; codegen the arguments, store them intermediately
255 (emit "## arg no. ~a" (- i 1))
256 (codegen-expr e (- si (* wordsize i)) env)
257 ; store intermediate result on stack
258 (emit "movq %rax, ~a(%rbp)" (- si (* wordsize i)))))
260 args (range 1 (length args)))
262 ; now that we have everything we need on the stack,
263 ; move them into the param registers
265 (emit "## moving args into place")
267 (lambda (i) (emit "movq ~a(%rbp), ~a"
268 (- si (* wordsize i))
270 (range 1 (length args)))
272 ; todo: can this be made more efficient
273 (emit "movq ~a(%rbp), %rax" si) ; load back pointer to closure
275 (emit "## moving captives into place")
277 ; move captives into first argument
278 (emit "movq %rax, %rbx")
279 (emit "addq $8, %rbx")
280 (emit "movq %rbx, ~a" (param-register 0))
282 (emit "## performing call")
284 (emit "addq $~a, %rsp" si) ; adjust the stack pointer to account all the stuff we put in the env
285 (emit "callq *(%rax)") ; call closure function
286 (emit "subq $~a, %rsp" si))
289 ; 1st param: pointer to captured args
291 ; 3rd param: 2nd arg, etc.
293 (define (codegen-lambda l)
294 (let* ((label (car l))
296 (captives (car stuff))
299 ; params = what actually gets passed
300 (params (append captives args))
302 (stack-offsets (map (lambda (i)
303 (* (- wordsize) (+ 1 i)))
304 (range 0 (length params))))
306 [bindings (map cons params stack-offsets)]
307 [env (make-env '() bindings)])
310 (display "## lambda captives: ")
313 (display "## lambda args: ")
316 (display "## lambda body: ")
320 (emit "push %rbp") ; preserve caller's base pointer
322 (emit "movq %rsp, %rbp") ; set up our own base pointer
324 ; load the captured vars onto the stack
328 (emit "# loading captive ~a" (list-ref captives i))
329 (emit "movq ~a(~a), %rbx" (* wordsize i) (param-register 0))
330 (emit "movq %rbx, ~a(%rbp)" (* (- wordsize) (+ 1 i)))))
331 (range 0 (length captives)))
333 ; load the args onto the stack
337 (emit "movq ~a, %rbx" (param-register (+ 1 i)))
338 (emit "movq %rbx, ~a(%rbp)"
340 (+ 1 (length captives) i)))))
341 (range 0 (length args)))
343 (codegen-expr body (* (- wordsize) (+ 1 (length params))) env)
345 (emit "pop %rbp") ; restore caller's base pointer
349 (define (fresh-label)
350 (set! cur-label (+ 1 cur-label))
351 (format "label~a" (- cur-label 1)))
353 (define (codegen-if cond then else si env)
354 (codegen-expr cond si env)
355 (emit "cmpq $0, %rax")
356 (let ((exit-label (fresh-label))
357 (else-label (fresh-label)))
358 (emit "je ~a" else-label)
359 (codegen-expr then si env)
360 (emit "jmp ~a" exit-label)
361 (emit "~a:" else-label)
362 (codegen-expr else si env)
363 (emit "~a:" exit-label)))
365 (define (data-tor env e)
366 (if (not (list? e)) #f
367 (assoc (car e) (flat-map data-tors (env-data-layouts env)))))
369 ; returns the internal offset in bytes of a product within an ADT
370 ; given the constructor layout
371 ; constructor-layout: (foo (Int Bool))
372 (define (data-product-offset data-layouts type sum index)
373 (let* ([products (cdr (assoc sum (cdr (assoc type data-layouts))))]
374 [to-traverse (list-head products index)])
376 (lambda (acc t) (+ acc (type-size data-layouts t)))
377 wordsize ; skip the tag in the first word
380 (define (data-sum-tag data-layouts type sum)
382 (define (go acc sums)
383 (when (null? sums) (error #f "data-sum-tag no sum for type" sum type))
384 (if (eqv? sum (car sums))
386 (go (+ 1 acc) (cdr sums))))
387 (let* ([type-sums (cdr (assoc type data-layouts))])
388 (go 0 (map car type-sums))))
390 (define (codegen-data-tor e si env)
392 (define (codegen-destructor tor)
393 (let* ([res (codegen-expr (cadr e) si env)]
398 (when (not (stack-expr? res))
399 (error #f "codegened something that wasn't a stack expression"))
400 ;TODO handle stack types
401 (emit "movq ~a(%rbp), %rax"
402 (- si (data-product-offset (env-data-layouts env) type sum index)))))
404 (define (codegen-constructor tor)
405 (let* ([info (cadr tor)]
408 [constructor (car e)]
412 [tag (data-sum-tag (env-data-layouts env)
418 (let ([res (codegen-expr expr si env)]
419 [stack-offset (- si (data-product-offset (env-data-layouts env)
422 (if (stack-expr? res)
423 (error #f "todo: handle stack-exprs in stack exprs")
424 (emit "movq %rax, ~a(%rbp)" stack-offset))))])
427 (emit "movq $~a, ~a(%rbp)" tag si)
429 (for-each insert-product args (range 0 (length args)))
430 (type-size (env-data-layouts env) type)))
432 (let* ([tor (data-tor env e)]
433 [constructor (eqv? 'constructor (caddr (cadr tor)))])
435 (codegen-constructor tor)
436 (codegen-destructor tor))))
438 (define stack-expr? number?)
440 ; returns a number if result was stored on stack
441 (define (codegen-expr e si env)
444 ('closure (codegen-closure (cadr e) (caddr e) si env))
447 ('+ (codegen-add (cdr e) si env))
448 ('- (codegen-sub (cadr e) (caddr e) si env))
449 ('* (codegen-mul (cadr e) (caddr e) si env))
450 ('! (codegen-not (cadr e) si env))
451 ('= (codegen-eq (cadr e) (caddr e) si env))
452 ('bool->int (codegen-expr (cadr e) si env))
453 ('print (codegen-print (cadr e) si env))
456 (codegen-data-tor e si env)
457 (codegen-call (car e) (cdr e) si env)))))
459 ; this is a builtin being passed around as a variable
460 ; this should have been converted to a closure!
461 ('builtin (error #f "passing about a builtin!" e))
463 ('let (codegen-let (let-bindings e)
468 ('var (codegen-var e si env))
470 ('if (codegen-if (cadr e) (caddr e) (cadddr e) si env))
472 ('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
473 ('int-literal (emit "movq $~a, %rax" e))
475 ('static-string (emit "movq ~a@GOTPCREL(%rip), %rax"
478 ('stack (codegen-expr (caddr e) si env))
480 (else (error #f "don't know how to codegen this"))))
482 ; takes in a expr annotated with types and returns a type-less AST
483 ; with stack values wrapped
484 (define (annotate-stack-values data-layout ann-e)
485 (define (stack-type? type)
486 (assoc type data-layout))
488 (ast-traverse strip (ann-expr e)))
489 (let* ([e (ann-expr ann-e)]
490 [type (ann-type ann-e)])
491 (if (stack-type? type)
492 `(stack ,type ,(ast-traverse strip e))
493 (ast-traverse (lambda (x)
494 (annotate-stack-values data-layout x))
497 (define (free-vars prog)
501 ('builtin '()) ; do nothing
502 ('var (if (memv e bound) '() (list e)))
505 (set! bound (append (lambda-args e) bound))
506 (collect (lambda-body e))))
508 ('app (flat-map collect e))
509 ('if (flat-map collect (cdr e)))
511 (let ([bind-fvs (flat-map (lambda (a)
513 (set! bound (cons (car a) bound))
516 (append bind-fvs (flat-map collect (let-body e)))))
520 ; ((lambda (x) (+ x y)) 42) => ((closure lambda1 (y)) 42)
521 ; [(lambda1 . ((y), (x), (+ x y))]
522 ; for builtins, this generates a closure if it is used
523 ; outside of an immediate app
524 ; but only one closure for each builtin
526 (define (extract-lambdas program)
528 (define (add-lambda e)
529 (let* ((label (fresh-lambda))
530 (args (lambda-args e))
531 (captured (free-vars e))
532 (body (extract (lambda-body e)))
533 (new-lambda (cons label (list captured args body))))
534 (set! lambdas (cons new-lambda lambdas))
535 `(closure ,label ,captured))) ; todo: should we string->symbol?
537 (define (find-builtin-lambda e)
538 (let [(l (assq (builtin-name e) lambdas))]
539 (if l `(closure ,(car l) ,(caadr l)) #f)))
541 (define (builtin-name e)
548 ('bool->int "_bool2int")
550 (else (error #f "don't know this builtin"))))
551 (define (builtin-args e)
560 (else (error #f "don't know this builtin"))))
562 (define (add-builtin-lambda e)
563 (let* [(label (builtin-name e))
565 (args (builtin-args e))
567 (new-lambda (cons label (list captured args body)))]
568 (set! lambdas (cons new-lambda lambdas))
569 `(closure ,label ,captured)))
573 ('lambda (add-lambda e))
574 ('let `(let ,(map (lambda (b) `(,(car b) ,@(extract (cdr b)))) (let-bindings e))
575 ,@(map extract (let-body e))))
577 ; if a builtin is used as a function, don't generate lambda
578 (if (eqv? 'builtin (ast-type (car e)))
580 (list (extract (car e))))
581 (map extract (cdr e))))
584 (if (find-builtin-lambda e)
585 (find-builtin-lambda e)
586 (add-builtin-lambda e)))
589 (else (ast-traverse extract e))))
590 (let ((transformed (extract program)))
591 (cons lambdas transformed)))
593 (define (extract-strings program)
595 (strings '())) ; assoc list of labels -> string
596 (define (fresh-string)
597 (set! cur-string (+ cur-string 1))
598 (format "string~a" (- cur-string 1)))
602 (let ((label (fresh-string)))
603 (set! strings (cons (cons label e) strings))
604 `(static-string ,label)))
605 (else (ast-traverse extract e))))
606 (let ((transformed (extract program)))
607 (cons strings transformed))))
609 (define (emit-string-data s)
611 (emit "\t.string \"~a\"" (cdr s)))
614 ; 16(%rbp) mem arg 0 prev frame
615 ; -----------------------
616 ; 8(%rbp) return address cur frame
618 ; -8(%rbp) do what you want
619 ; ... do what you want
620 ; 0(%rsp) do what you want
622 (define (param-register n)
630 (else (error #f "need to test out the below"))
631 (else (format "~a(%rsp)" (- n 6)))))
633 (define (initialize-heap)
636 ('darwin "0x20000c5")
638 ; allocate some heap memory
639 (emit "mov $~a, %rax" mmap) ; mmap
640 (emit "xor %rdi, %rdi") ; addr = null
641 (emit "movq $1024, %rsi") ; length = 1kb
642 (emit "movq $0x3, %rdx") ; prot = read | write = 0x2 | 0x1
643 ; flags = anonymous | private
645 ('darwin (emit "movq $0x1002, %r10")) ; anon = 0x1000, priv = 0x02
646 ('linux (emit "movq $0x22, %r10"))) ; anon = 0x20, priv = 0x02
647 (emit "movq $-1, %r8") ; fd = -1
648 (emit "xor %r9, %r9") ; offset = 0
650 ; %rax now contains pointer to the start of the heap
653 (emit "movq heap_start@GOTPCREL(%rip), %rsi")
654 (emit "movq %rax, (%rsi)")))
656 (define (codegen program)
659 (let* ([data-layouts (program-data-layouts program)]
661 [pattern-matched (expand-pattern-matches program)]
662 [type-annotated (annotate-types pattern-matched)]
663 [stack-annotated (annotate-stack-values data-layouts
666 (strings-res (extract-strings stack-annotated))
667 (strings (car strings-res))
668 (lambdas-res (extract-lambdas (cdr strings-res)))
669 (lambdas (car lambdas-res))
670 (xform-prog (cdr lambdas-res)))
672 (emit "\t.global _start")
674 ; (emit ".p2align 4,,15") is this needed?
676 (for-each codegen-lambda lambdas)
682 (emit "movq %rsp, %rbp") ; set up the base pointer
684 (codegen-expr xform-prog (- wordsize) (make-env data-layouts '()))
687 (emit "mov %rax, %rdi")
689 ('darwin (emit "movq $0x2000001, %rax"))
690 ('linux (emit "mov $60, %rax")))
698 (for-each emit-string-data strings)))
700 (define (compile-to-binary program output t)
702 (when (not (eq? (typecheck program) 'Int)) (error #f "not an Int"))
703 (let ([tmp-path "/tmp/a.s"])
704 (when (file-exists? tmp-path) (delete-file tmp-path))
705 (with-output-to-file tmp-path
706 (lambda () (codegen program)))
710 (system "as /tmp/a.s -o /tmp/a.o")
711 (system (format "ld /tmp/a.o -e _start -macosx_version_min 10.14 -static -o ~a" output)))
713 (system "as /tmp/a.s -o /tmp/a.o")
714 (system (format "ld /tmp/a.o -o ~a" output))))))
717 ; syscalls in linux and darwin use the following arguments for syscall instruction:
726 ; on darwin, unix/posix syscalls are offset by 0x2000000 (syscall classes)
727 ; https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/kern/syscalls.master
728 ; documentation for most syscalls: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys