5 (define target host-os)
14 (define (type-size type env)
16 (define (adt-size adt)
19 (fold-left (lambda (acc x) (+ acc (type-size x)))
20 wordsize ; one word needed to store tag
29 (let ([adt (assoc type (env-adts env))])
32 (error #f "unknown size" type)))]))
34 ; an environment consists of adt layouts in scope,
35 ; and any bound variables.
36 ; bound variables are an assoc list with their stack offset
37 (define make-env list)
39 (define env-bindings cadr)
41 (define (codegen-add xs si env)
44 (emit "movq ~a(%rbp), %rax" si)
48 (emit "addq $~a, ~a(%rbp)" y si)
50 (codegen-expr y (- si wordsize) env)
51 (emit "addq %rax, ~a(%rbp)" si))))
54 ; use si(%rbp) as the accumulator
55 (emit "movq $0, ~a(%rbp)" si)
58 (define (codegen-binop opcode)
60 (codegen-expr b si env)
61 (emit "movq %rax, ~a(%rbp)" si)
62 (codegen-expr a (- si wordsize) env)
63 (emit "~a ~a(%rbp), %rax" opcode si)))
65 (define codegen-sub (codegen-binop "sub"))
67 (define codegen-mul (codegen-binop "imul"))
69 (define (codegen-not x si env)
70 (codegen-expr x si env)
72 (emit "andq $1, %rax"))
74 (define (codegen-eq a b si env)
75 (codegen-expr a si env)
76 (emit "movq %rax, ~a(%rbp)" si)
77 (codegen-expr b (- si wordsize) env)
78 (emit "## ~a = ~b" a b)
79 (emit "cmpq ~a(%rbp), %rax" si)
82 ; 'write file handle addr-string num-bytes
84 (define (codegen-print x si env)
85 (codegen-expr x si env) ; x should be a static-string, producing a label
87 ; make a copy of string address since %rax and %rdi are clobbered
88 (emit "mov %rax, %rbx")
90 ; get the length of the null terminated string
91 (emit "mov %rax, %rdi")
92 (emit "xor %al, %al") ; set %al to 0
93 (emit "mov $-1, %rcx") ; max search length = max int = -1
94 (emit "cld") ; clear direction flag, search up in memory
95 (emit "repne scasb") ; scan string, %rcx = -strlen - 1 - 1
97 (emit "not %rcx") ; -%rcx = strlen + 1
100 (emit "movq %rbx, %rsi") ; string addr
101 (emit "movq %rcx, %rdx") ; num bytes
102 (emit "movq $1, %rdi") ; file handle (stdout)
104 ('darwin (emit "mov $0x2000004, %rax")) ; syscall 4 (write)
105 ('linux (emit "mov $1, %rax"))) ; syscall 1 (write)
108 (define (codegen-let bindings body si env)
110 ; is this a closure that captures itself?
111 ; e.g. (let ([x 3] [f (closure lambda0 (f x))]) (f))
112 (define (self-captive-closure? name expr)
113 (and (eqv? (ast-type expr) 'closure)
114 (memv name (caddr expr))))
117 ;; (define (emit-scc scc env)
118 ;; ; acc is a pair of the env and list of touchups
119 ;; (define (emit-binding acc binding)
120 ;; (let ([binding-name (car binding)]
121 ;; [binding-body (cadr binding)]
123 ;; [other-bindings (filter
124 ;; (lambda (x) (not (eqv? binding-name x)))
126 ;; [mutually-recursives
128 ;; (lambda (other-binding)
129 ;; (memv other-binding (references binding-body)))
132 ;; [new-touchups (append touchups (cdr acc))])
134 ;; ; TODO: assert that the only mutually recursives are closures
137 ;; (when (not (eqv? (ast-type (cadr binding))
140 ;; (cons new-env new-touchups)
143 ;; (fold-left emit-binding (cons env '()) scc))))
145 (let* ([stack-offsets (map (lambda (name x) ; assoc map of binding name to offset
146 (cons name (- si (* x wordsize))))
148 (range 0 (length bindings)))]
149 [inner-si (- si (* (length bindings) wordsize))]
151 [get-offset (lambda (n) (cdr (assoc n stack-offsets)))]
156 (let* ([scc-binding-offsets
159 (cons (cons name (get-offset name))
163 [scc-env (make-env (env-adts env) scc-binding-offsets)])
166 (let ([expr (cadr (assoc name bindings))])
167 (emit "## generating ~a with scc-env ~a" name scc-env)
168 (if (self-captive-closure? name expr)
169 ; if self-captive, insert a flag into the environment to let
170 ; codegen-closure realise this!
175 (cons (cons name 'self-captive)
176 (env-bindings scc-env))))
177 (codegen-expr expr inner-si scc-env))
178 (emit "movq %rax, ~a(%rbp)" (get-offset name))))
182 (reverse (sccs (graph bindings))))])
184 (for-each (lambda (form)
185 (codegen-expr form inner-si inner-env))
188 (define (codegen-var name si env)
189 (let ([binding (assoc name (env-bindings env))])
191 (error #f (format "Variable ~a is not bound" name))
192 (emit "movq ~a(%rbp), %rax" (cdr binding)))))
194 (define cur-lambda 0)
195 (define (fresh-lambda)
196 (set! cur-lambda (+ 1 cur-lambda))
197 (format "_lambda~a" (- cur-lambda 1)))
199 ; a closure on the heap looks like:
201 ; addr var1.... var2.... var3....
203 (define (codegen-closure label captured si env)
204 (let* ((heap-offsets (map (lambda (i) (+ 8 (* 8 i)))
205 (range 0 (length captured))))) ; 4, 12, 20, etc.
207 (emit "## creating closure")
209 (emit "movq heap_start@GOTPCREL(%rip), %rbx")
211 (emit "movq (%rbx), %rax") ; %rax = heap addr of closure
214 ; point heap_start to next space
215 (emit "addq $~a, (%rbx)" (+ 8 (* 8 (length captured))))
217 (emit "## storing address to lambda")
218 ; store the address to the lambda code
219 (emit "movq ~a@GOTPCREL(%rip), %rbx" label)
220 (emit "movq %rbx, 0(%rax)")
222 (emit "## storing captives")
223 ; store the captured vars
225 (lambda (var-name heap-offset)
226 (let ([stack-offset (cdr (assoc var-name (env-bindings env)))])
227 (emit "### captive ~a" var-name)
228 (if (eqv? stack-offset 'self-captive)
229 ; captive refers to this closure:
230 ; move heap addr of this closure to stack!
231 (emit "movq %rax, ~a(%rax)" heap-offset)
233 (emit "movq ~a(%rbp), %rbx" stack-offset)
234 (emit "movq %rbx, ~a(%rax)" heap-offset)))))
238 ; for now we can only call closures
239 (define (codegen-call f args si env)
240 (codegen-expr f si env)
242 (emit "## starting call")
244 (emit "movq %rax, ~a(%rbp)" si) ; store address of closure first on stack
246 ; codegen the arguments, store them intermediately
250 (emit "## arg no. ~a" (- i 1))
251 (codegen-expr e (- si (* wordsize i)) env)
252 ; store intermediate result on stack
253 (emit "movq %rax, ~a(%rbp)" (- si (* wordsize i)))))
255 args (range 1 (length args)))
257 ; now that we have everything we need on the stack,
258 ; move them into the param registers
260 (emit "## moving args into place")
262 (lambda (i) (emit "movq ~a(%rbp), ~a"
263 (- si (* wordsize i))
265 (range 1 (length args)))
267 ; todo: can this be made more efficient
268 (emit "movq ~a(%rbp), %rax" si) ; load back pointer to closure
270 (emit "## moving captives into place")
272 ; move captives into first argument
273 (emit "movq %rax, %rbx")
274 (emit "addq $8, %rbx")
275 (emit "movq %rbx, ~a" (param-register 0))
277 (emit "## performing call")
279 (emit "addq $~a, %rsp" si) ; adjust the stack pointer to account all the stuff we put in the env
280 (emit "callq *(%rax)") ; call closure function
281 (emit "subq $~a, %rsp" si))
284 ; 1st param: pointer to captured args
286 ; 3rd param: 2nd arg, etc.
288 (define (codegen-lambda l)
289 (let* ((label (car l))
291 (captives (car stuff))
294 ; params = what actually gets passed
295 (params (append captives args))
297 (stack-offsets (map (lambda (i)
298 (* (- wordsize) (+ 1 i)))
299 (range 0 (length params))))
301 [bindings (map cons params stack-offsets)]
302 [env (make-env '() bindings)])
305 (display "## lambda captives: ")
308 (display "## lambda args: ")
311 (display "## lambda body: ")
315 (emit "push %rbp") ; preserve caller's base pointer
317 (emit "movq %rsp, %rbp") ; set up our own base pointer
319 ; load the captured vars onto the stack
323 (emit "# loading captive ~a" (list-ref captives i))
324 (emit "movq ~a(~a), %rbx" (* wordsize i) (param-register 0))
325 (emit "movq %rbx, ~a(%rbp)" (* (- wordsize) (+ 1 i)))))
326 (range 0 (length captives)))
328 ; load the args onto the stack
332 (emit "movq ~a, %rbx" (param-register (+ 1 i)))
333 (emit "movq %rbx, ~a(%rbp)"
335 (+ 1 (length captives) i)))))
336 (range 0 (length args)))
338 (codegen-expr body (* (- wordsize) (+ 1 (length params))) env)
340 (emit "pop %rbp") ; restore caller's base pointer
344 (define (fresh-label)
345 (set! cur-label (+ 1 cur-label))
346 (format "label~a" (- cur-label 1)))
348 (define (codegen-if cond then else si env)
349 (codegen-expr cond si env)
350 (emit "cmpq $0, %rax")
351 (let ((exit-label (fresh-label))
352 (else-label (fresh-label)))
353 (emit "je ~a" else-label)
354 (codegen-expr then si env)
355 (emit "jmp ~a" exit-label)
356 (emit "~a:" else-label)
357 (codegen-expr else si env)
358 (emit "~a:" exit-label)))
360 (define (data-tor env e)
362 (assoc (car e) (flat-map data-tors (env-adts env)))))
364 (define (codegen-data-tor e si env)
366 (define (codegen-destructor tor)
367 (codegen-expr (cadr e) si env)
368 (let ([index (cadr tor)]
370 [to-traverse (list-head products index)]
372 (lambda (acc t) (+ acc (type-size t)))
373 wordsize ; skip tag in first word
378 (let ([tor (data-tor env e)]
379 [constructor (eqv? 'constructor (cadr tor))])
381 (codegen-constructor tor)
382 (codegen-destructor tor))))
384 (define (codegen-expr e si env)
387 ('closure (codegen-closure (cadr e) (caddr e) si env))
390 ('+ (codegen-add (cdr e) si env))
391 ('- (codegen-sub (cadr e) (caddr e) si env))
392 ('* (codegen-mul (cadr e) (caddr e) si env))
393 ('! (codegen-not (cadr e) si env))
394 ('= (codegen-eq (cadr e) (caddr e) si env))
395 ('bool->int (codegen-expr (cadr e) si env))
396 ('print (codegen-print (cadr e) si env))
399 (codegen-data-tor e si env)
400 (codegen-call (car e) (cdr e) si env)))))
402 ; this is a builtin being passed around as a variable
403 ('builtin (emit "movq $~a, %rax" (builtin-id e)))
405 ('let (codegen-let (let-bindings e)
410 ('var (codegen-var e si env))
412 ('if (codegen-if (cadr e) (caddr e) (cadddr e) si env))
414 ('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
415 ('int-literal (emit "movq $~a, %rax" e))
417 ('static-string (emit "movq ~a@GOTPCREL(%rip), %rax"
420 (else (error #f "don't know how to codegen this"))))
424 (define (free-vars prog)
428 ('builtin '()) ; do nothing
429 ('var (if (memv e bound) '() (list e)))
432 (set! bound (append (lambda-args e) bound))
433 (collect (lambda-body e))))
435 ('app (flat-map collect e))
436 ('if (flat-map collect (cdr e)))
438 (let ([bind-fvs (flat-map (lambda (a)
440 (set! bound (cons (car a) bound))
443 (append bind-fvs (flat-map collect (let-body e)))))
447 ; ((lambda (x) (+ x y)) 42) => ((closure lambda1 (y)) 42)
448 ; [(lambda1 . ((y), (x), (+ x y))]
449 ; for builtins, this generates a closure if it is used
450 ; outside of an immediate app
451 ; but only one closure for each builtin
453 (define (extract-lambdas program)
455 (define (add-lambda e)
456 (let* ((label (fresh-lambda))
457 (args (lambda-args e))
458 (captured (free-vars e))
459 (body (extract (lambda-body e)))
460 (new-lambda (cons label (list captured args body))))
461 (set! lambdas (cons new-lambda lambdas))
462 `(closure ,label ,captured))) ; todo: should we string->symbol?
464 (define (find-builtin-lambda e)
465 (let [(l (assq (builtin-name e) lambdas))]
466 (if l `(closure ,(car l) ,(caadr l)) #f)))
468 (define (builtin-name e)
475 ('bool->int "_bool2int")
477 (else (error #f "don't know this builtin"))))
478 (define (builtin-args e)
487 (else (error #f "don't know this builtin"))))
489 (define (add-builtin-lambda e)
490 (let* [(label (builtin-name e))
492 (args (builtin-args e))
494 (new-lambda (cons label (list captured args body)))]
495 (set! lambdas (cons new-lambda lambdas))
496 `(closure ,label ,captured)))
500 ('lambda (add-lambda e))
501 ('let `(let ,(map (lambda (b) `(,(car b) ,@(extract (cdr b)))) (let-bindings e))
502 ,@(map extract (let-body e))))
504 ; if a builtin is used as a function, don't generate lambda
505 (if (eqv? 'builtin (ast-type (car e)))
507 (list (extract (car e))))
508 (map extract (cdr e))))
511 (if (find-builtin-lambda e)
512 (find-builtin-lambda e)
513 (add-builtin-lambda e)))
516 (else (ast-traverse extract e))))
517 (let ((transformed (extract program)))
518 (cons lambdas transformed)))
520 (define (extract-strings program)
522 (strings '())) ; assoc list of labels -> string
523 (define (fresh-string)
524 (set! cur-string (+ cur-string 1))
525 (format "string~a" (- cur-string 1)))
529 (let ((label (fresh-string)))
530 (set! strings (cons (cons label e) strings))
531 `(static-string ,label)))
532 (else (ast-traverse extract e))))
533 (let ((transformed (extract program)))
534 (cons strings transformed))))
536 (define (emit-string-data s)
538 (emit "\t.string \"~a\"" (cdr s)))
541 ; 16(%rbp) mem arg 0 prev frame
542 ; -----------------------
543 ; 8(%rbp) return address cur frame
545 ; -8(%rbp) do what you want
546 ; ... do what you want
547 ; 0(%rsp) do what you want
549 (define (param-register n)
557 (else (error #f "need to test out the below"))
558 (else (format "~a(%rsp)" (- n 6)))))
560 (define (initialize-heap)
563 ('darwin "0x20000c5")
565 ; allocate some heap memory
566 (emit "mov $~a, %rax" mmap) ; mmap
567 (emit "xor %rdi, %rdi") ; addr = null
568 (emit "movq $1024, %rsi") ; length = 1kb
569 (emit "movq $0x3, %rdx") ; prot = read | write = 0x2 | 0x1
570 ; flags = anonymous | private
572 ('darwin (emit "movq $0x1002, %r10")) ; anon = 0x1000, priv = 0x02
573 ('linux (emit "movq $0x22, %r10"))) ; anon = 0x20, priv = 0x02
574 (emit "movq $-1, %r8") ; fd = -1
575 (emit "xor %r9, %r9") ; offset = 0
577 ; %rax now contains pointer to the start of the heap
580 (emit "movq heap_start@GOTPCREL(%rip), %rsi")
581 (emit "movq %rax, (%rsi)")))
583 (define (codegen program)
586 (let* ([body (program-body program)]
588 [data-layouts (map data-layout (program-datas program))]
590 (extract-res-0 (extract-strings body))
591 (strings (car extract-res-0))
592 (extract-res-1 (extract-lambdas (cdr extract-res-0)))
593 (lambdas (car extract-res-1))
594 (xform-prog (cdr extract-res-1)))
596 (emit "\t.global _start")
598 ; (emit ".p2align 4,,15") is this needed?
600 (for-each codegen-lambda lambdas)
606 (emit "movq %rsp, %rbp") ; set up the base pointer
608 (codegen-expr xform-prog (- wordsize) (make-env data-layouts '()))
611 (emit "mov %rax, %rdi")
613 ('darwin (emit "movq $0x2000001, %rax"))
614 ('linux (emit "mov $60, %rax")))
622 (for-each emit-string-data strings)))
624 (define (compile-to-binary program output t)
626 (when (not (eq? (typecheck program) 'Int)) (error #f "not an Int"))
627 (let ([tmp-path "/tmp/a.s"])
628 (when (file-exists? tmp-path) (delete-file tmp-path))
629 (with-output-to-file tmp-path
630 (lambda () (codegen program)))
634 (system "as /tmp/a.s -o /tmp/a.o")
635 (system (format "ld /tmp/a.o -e _start -macosx_version_min 10.14 -static -o ~a" output)))
637 (system "as /tmp/a.s -o /tmp/a.o")
638 (system (format "ld /tmp/a.o -o ~a" output))))))
641 ; syscalls in linux and darwin use the following arguments for syscall instruction:
650 ; on darwin, the syscall is offset by 0x2000000
651 ; https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/kern/syscalls.master
652 ; documentation for most syscalls: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys