6 (define target host-os)
15 (define (stack-type? dls type)
17 (> (type-size dls type) wordsize)
20 (define (adt? data-layouts type) (assoc type data-layouts))
22 (define (type-size data-layouts type)
24 (define (adt-size adt)
27 (fold-left (lambda (acc x) (+ acc (type-size data-layouts x)))
28 wordsize ; one word needed to store tag
36 (let ([adt (assoc type data-layouts)])
39 (error #f "unknown size" type)))]))
41 (define (singletons dls)
45 (filter (lambda (x) (= 1 (length x))) sums)))
48 (define (singleton? dls x)
49 (memv x (singletons dls)))
51 ; returns the size of an expression's result in bytes
52 (define (expr-size dls e)
53 (if (eqv? (ast-type e) 'stack)
54 (type-size dls (cadr e))
57 (define (on-stack? expr)
62 ; does a movsq for something on the stack
63 ; src points to the start stack index, but not the top of that index
66 ; +------+ <-- to here
68 ; +------+ <-- src (size = 16)
70 ; +------+ <-- start copying from here...
71 (define (emit-stack-copy src dst size)
72 (let ([size-to-copy (- size wordsize)])
73 (emit "leaq ~a(%rbp), %rsi" (- src size-to-copy))
74 (emit "leaq ~a(%rbp), %rdi" (- dst size-to-copy))
75 (emit "movq $~a, %rcx" (/ size wordsize))
79 ; an environment consists of adt layouts in scope,
80 ; and any bound variables.
81 ; bound variables are an assoc list with their stack offset
82 (define make-env list)
83 (define env-data-layouts car)
84 (define env-bindings cadr)
85 (define (env-append-bindings env bindings)
86 (make-env (env-data-layouts env)
87 (append bindings (env-bindings env))))
89 (define (codegen-add xs si env)
92 (emit "movq ~a(%rbp), %rax" si)
96 (emit "addq $~a, ~a(%rbp)" y si)
98 (codegen-expr y (- si wordsize) env)
99 (emit "addq %rax, ~a(%rbp)" si))))
102 ; use si(%rbp) as the accumulator
103 (emit "movq $0, ~a(%rbp)" si)
106 (define (codegen-binop opcode)
108 (codegen-expr b si env)
109 (emit "movq %rax, ~a(%rbp)" si)
110 (codegen-expr a (- si wordsize) env)
111 (emit "~a ~a(%rbp), %rax" opcode si)))
113 (define codegen-sub (codegen-binop "sub"))
115 (define codegen-mul (codegen-binop "imul"))
117 (define (codegen-not x si env)
118 (codegen-expr x si env)
120 (emit "andq $1, %rax"))
122 (define (codegen-eq a b si env)
123 (codegen-expr a si env)
124 (emit "movq %rax, ~a(%rbp)" si)
125 (codegen-expr b (- si wordsize) env)
126 (emit "## ~a = ~b" a b)
127 (emit "cmpq ~a(%rbp), %rax" si)
130 ; 'write file handle addr-string num-bytes
132 (define (codegen-print x si env)
133 (codegen-expr x si env) ; x should be a static-string, producing a label
135 ; make a copy of string address since %rax and %rdi are clobbered
136 (emit "mov %rax, %rbx")
138 ; get the length of the null terminated string
139 (emit "mov %rax, %rdi")
140 (emit "xor %al, %al") ; set %al to 0
141 (emit "mov $-1, %rcx") ; max search length = max int = -1
142 (emit "cld") ; clear direction flag, search up in memory
143 (emit "repne scasb") ; scan string, %rcx = -strlen - 1 - 1
145 (emit "not %rcx") ; -%rcx = strlen + 1
148 (emit "movq %rbx, %rsi") ; string addr
149 (emit "movq %rcx, %rdx") ; num bytes
150 (emit "movq $1, %rdi") ; file handle (stdout)
152 ('darwin (emit "mov $0x2000004, %rax")) ; syscall 4 (write)
153 ('linux (emit "mov $1, %rax"))) ; syscall 1 (write)
156 (define (codegen-let bindings body si env)
158 ; is this a closure that captures itself?
159 ; e.g. (let ([x 3] [f (closure lambda0 (f x))]) (f))
160 (define (self-captive-closure? name expr)
161 (and (eqv? (ast-type expr) 'closure)
162 (memv name (caddr expr))))
164 ;; (define (emit-scc scc env)
165 ;; ; acc is a pair of the env and list of touchups
166 ;; (define (emit-binding acc binding)
167 ;; (let ([binding-name (car binding)]
168 ;; [binding-body (cadr binding)]
170 ;; [other-bindings (filter
171 ;; (lambda (x) (not (eqv? binding-name x)))
173 ;; [mutually-recursives
175 ;; (lambda (other-binding)
176 ;; (memv other-binding (references binding-body)))
179 ;; [new-touchups (append touchups (cdr acc))])
181 ;; ; TODO: assert that the only mutually recursives are closures
184 ;; (when (not (eqv? (ast-type (cadr binding))
187 ;; (cons new-env new-touchups)
190 ;; (fold-left emit-binding (cons env '()) scc))))
191 ; assoc map of binding name to size
194 (define dls (env-data-layouts env))
197 (map (lambda (binding) (cons (car binding) (expr-size dls (cadr binding))))
200 ; assoc map of binding name to offset
201 (define stack-offsets
203 (let* ([totals ; 2 6 8 16 22
204 (reverse (fold-left (lambda (acc x)
207 (cons (+ x (car acc)) acc)))
209 (map cdr stack-sizes)))]
211 [relative-offsets (map - totals (map cdr stack-sizes))]
212 [absolute-offsets (map (lambda (x) (- si x)) relative-offsets)])
213 (map cons (map car stack-sizes) absolute-offsets)))
216 ; the stack index used when codegening binding body and main body
218 ; [stack-offsets | inner-si]
219 [inner-si (- si (fold-left + 0 (map cdr stack-sizes)))]
221 [get-offset (lambda (n) (cdr (assoc n stack-offsets)))]
226 (let* ([scc-binding-offsets
229 (cons (cons name (get-offset name))
233 [scc-env (make-env (env-data-layouts env) scc-binding-offsets)])
236 (let* ([expr (cadr (assoc name bindings))]
237 [size (expr-size dls expr)])
238 (emit "## generating ~a with scc-env ~a" name scc-env)
239 (if (self-captive-closure? name expr)
240 ; if self-captive, insert a flag into the environment to let
241 ; codegen-closure realise this!
245 (env-data-layouts scc-env)
246 (cons (cons name 'self-captive)
247 (env-bindings scc-env))))
248 (codegen-expr expr inner-si scc-env))
251 ; copy over whatevers on the stack
252 (emit-stack-copy inner-si (get-offset name) size)
253 (emit "movq %rax, ~a(%rbp)" (get-offset name)))))
257 (reverse (sccs (graph bindings))))])
259 (for-each (lambda (form)
260 (codegen-expr form inner-si inner-env))
263 (define (codegen-var e si env)
264 (let* ([stack-type (on-stack? e)]
265 [name (if stack-type (caddr e) e)]
267 [dls (env-data-layouts env)]
270 [stack-offset (assoc name (env-bindings env))])
272 (when (and (not stack-offset) (not singleton?))
273 (error #f (format "Variable ~a is not bound" name)))
276 [(singleton? dls name)
277 ; singletons don't need to be in the environment
279 (let ([tag (data-sum-tag (env-data-layouts env) name)])
281 (emit "movq $~a, ~a(%rbp)" tag si)
282 (emit "movq $~a, %rax" tag)))]
283 [(on-stack? e) (emit-stack-copy (cdr stack-offset) si (type-size dls stack-type))]
284 [else (emit "movq ~a(%rbp), %rax" (cdr stack-offset))])))
286 (define cur-lambda 0)
287 (define (fresh-lambda)
288 (set! cur-lambda (+ 1 cur-lambda))
289 (format "_lambda~a" (- cur-lambda 1)))
291 ; a closure on the heap looks like:
293 ; addr var1.... var2.... var3....
295 (define (codegen-closure label captured si env)
296 (let* ((heap-offsets (map (lambda (i) (+ 8 (* 8 i)))
297 (range 0 (length captured))))) ; 4, 12, 20, etc.
299 (emit "## creating closure")
301 (emit "movq heap_start@GOTPCREL(%rip), %rbx")
303 (emit "movq (%rbx), %rax") ; %rax = heap addr of closure
306 ; point heap_start to next space
307 (emit "addq $~a, (%rbx)" (+ 8 (* 8 (length captured))))
309 (emit "## storing address to lambda")
310 ; store the address to the lambda code
311 (emit "movq ~a@GOTPCREL(%rip), %rbx" label)
312 (emit "movq %rbx, 0(%rax)")
314 (emit "## storing captives")
315 ; store the captured vars
317 (lambda (var-name heap-offset)
318 (let ([stack-offset (cdr (assoc var-name (env-bindings env)))])
319 (emit "### captive ~a" var-name)
320 (if (eqv? stack-offset 'self-captive)
321 ; captive refers to this closure:
322 ; move heap addr of this closure to stack!
323 (emit "movq %rax, ~a(%rax)" heap-offset)
325 (emit "movq ~a(%rbp), %rbx" stack-offset)
326 (emit "movq %rbx, ~a(%rax)" heap-offset)))))
330 ; for now we can only call closures
331 (define (codegen-call f args si env)
332 (codegen-expr f si env)
334 (emit "## starting call")
336 (emit "movq %rax, ~a(%rbp)" si) ; store address of closure first on stack
338 ; codegen the arguments, store them intermediately
342 (emit "## arg no. ~a" (- i 1))
343 (codegen-expr e (- si (* wordsize i)) env)
344 ; store intermediate result on stack
345 (emit "movq %rax, ~a(%rbp)" (- si (* wordsize i)))))
347 args (range 1 (length args)))
349 ; now that we have everything we need on the stack,
350 ; move them into the param registers
352 (emit "## moving args into place")
354 (lambda (i) (emit "movq ~a(%rbp), ~a"
355 (- si (* wordsize i))
357 (range 1 (length args)))
359 ; todo: can this be made more efficient
360 (emit "movq ~a(%rbp), %rax" si) ; load back pointer to closure
362 (emit "## moving captives into place")
364 ; move captives into first argument
365 (emit "movq %rax, %rbx")
366 (emit "addq $8, %rbx")
367 (emit "movq %rbx, ~a" (param-register 0))
369 (emit "## performing call")
371 (emit "addq $~a, %rsp" si) ; adjust the stack pointer to account all the stuff we put in the env
372 (emit "callq *(%rax)") ; call closure function
373 (emit "subq $~a, %rsp" si))
376 ; 1st param: pointer to captured args
378 ; 3rd param: 2nd arg, etc.
380 (define (codegen-lambda l)
381 (let* ((label (car l))
384 (captives (cadr stuff))
386 (body (cadddr stuff))
387 ; params = what actually gets passed
388 (params (append captives args))
390 (stack-offsets (map (lambda (i)
391 (* (- wordsize) (+ 1 i)))
392 (range 0 (length params))))
394 [bindings (map cons params stack-offsets)]
395 [env (make-env dls bindings)])
398 (display "## lambda captives: ")
401 (display "## lambda args: ")
404 (display "## lambda body: ")
408 (emit "push %rbp") ; preserve caller's base pointer
410 (emit "movq %rsp, %rbp") ; set up our own base pointer
412 ; load the captured vars onto the stack
416 (emit "# loading captive ~a" (list-ref captives i))
417 (emit "movq ~a(~a), %rbx" (* wordsize i) (param-register 0))
418 (emit "movq %rbx, ~a(%rbp)" (* (- wordsize) (+ 1 i)))))
419 (range 0 (length captives)))
421 ; load the args onto the stack
425 (emit "movq ~a, %rbx" (param-register (+ 1 i)))
426 (emit "movq %rbx, ~a(%rbp)"
428 (+ 1 (length captives) i)))))
429 (range 0 (length args)))
431 (codegen-expr body (* (- wordsize) (+ 1 (length params))) env)
433 (emit "pop %rbp") ; restore caller's base pointer
437 (define (fresh-label)
438 (set! cur-label (+ 1 cur-label))
439 (format "label~a" (- cur-label 1)))
441 (define (codegen-if cond then else si env)
442 (codegen-expr cond si env)
443 (emit "cmpq $0, %rax")
444 (let ((exit-label (fresh-label))
445 (else-label (fresh-label)))
446 (emit "je ~a" else-label)
447 (codegen-expr then si env)
448 (emit "jmp ~a" exit-label)
449 (emit "~a:" else-label)
450 (codegen-expr else si env)
451 (emit "~a:" exit-label)))
453 (define (codegen-case switch cases si env)
454 (define dls (env-data-layouts env))
455 (define exit-label (fresh-label))
458 ; checks if equal and returns assoc list of bindings
459 (define (check-equal jne-label type inner-offset x)
461 ; TODO: tidy this up! comparibles and binds could be merged
462 ; (foo a 2 (bar x)) -> ((2 Int 1) ((bar x) A 2))
464 (define (comparibles sum)
465 (let ([product-types (cdr (assoc sum (cdr (assoc type dls))))])
466 (if (null? product-types)
468 (filter (lambda (x) (not (eqv? 'var (ast-type (car x)))))
469 (map (lambda (x t i) (list x t i))
472 (range 0 (length product-types)))))))
475 (let ([product-types (cdr (assoc sum (cdr (assoc type dls))))])
476 (if (null? product-types)
478 (filter (lambda (x) (eqv? 'var (ast-type (car x))))
482 (data-product-offset dls type sum i))))
484 (range 0 (length (cdr x))))))))
488 (let* ([sum (if (list? x) (car x) x)] ; can sometimes be a singleton
489 [tag (data-sum-tag dls sum)])
490 ; the tag is at the top (beginning) of the adt on the stack
491 (emit "cmpq $~a, ~a(%rbp)" tag inner-offset)
492 (emit "jne ~a" jne-label)
496 (lambda (cmp) ; cmp = (x type index)
497 (check-equal jne-label
499 (- inner-offset (data-product-offset dls type sum (caddr cmp)))
502 (if (eqv? 'var (ast-type x))
503 (list (cons x inner-offset))
505 (emit "cmpq $~a, ~a(%rbp)" x inner-offset)
506 (emit "jne ~a" jne-label)
509 (define (codegen-adt-match type case)
510 (let* ([match (car case)]
512 [next-section-label (fresh-label)]
513 [inner-si (- si (type-size dls type))]
514 [new-env (env-append-bindings env
515 (check-equal next-section-label type si match))])
518 (codegen-expr expr inner-si new-env)
519 (emit "jmp ~a" exit-label)
521 (emit "~a:" next-section-label)))
523 (define (codegen-literal-match case)
524 (let* ([next-section-label (fresh-label)]
526 [val (if (singleton? dls match)
527 (data-sum-tag dls match)
529 (emit "cmpq $~a, %rax" val)
530 (emit "jne ~a" next-section-label)
531 (codegen-expr (cadr case) si env)
532 (emit "jmp ~a" exit-label)
533 (emit "~a:" next-section-label)))
535 ; generate the switch
536 ; (and store it on the stack if not a stack value)
537 (codegen-expr switch si env)
539 (if (on-stack? switch)
541 (for-each (lambda (x) (codegen-adt-match (cadr switch) x)) cases)
542 (for-each codegen-literal-match cases))
543 (emit "~a:" exit-label))
546 (define (data-tor env e)
547 (if (not (list? e)) #f
548 (assoc (car e) (flat-map data-tors (env-data-layouts env)))))
550 ; returns the internal offset in bytes of a product within an ADT
551 ; given the constructor layout
552 ; constructor-layout: (foo (Int Bool))
553 (define (data-product-offset data-layouts type sum index)
554 (let* ([products (cdr (assoc sum (cdr (assoc type data-layouts))))]
555 [to-traverse (list-head products index)])
557 (lambda (acc t) (+ acc (type-size data-layouts t)))
558 wordsize ; skip the tag in the first word
561 (define (data-sum-tag data-layouts sum)
563 (define (go acc sums)
564 (when (null? sums) (error #f "data-sum-tag no sum for type" sum type))
565 (if (eqv? sum (car sums))
567 (go (+ 1 acc) (cdr sums))))
568 (let* ([all-sum-names (flat-map (lambda (x) (map (lambda (y) (cons (car y) (car x))) (cdr x))) data-layouts)]
569 [type (cdr (assoc sum all-sum-names))]
570 [type-sums (cdr (assoc type data-layouts))])
571 (go 0 (map car type-sums))))
573 (define (codegen-data-tor e si env)
575 (define dls (env-data-layouts env))
577 (define (codegen-destructor tor)
578 (let* ([res (codegen-expr (cadr e) si env)]
583 [product-type (cadddr info)]
584 [product-type-size (type-size dls product-type)]
586 [safe-space-offset (- si (type-size dls type))]
588 [inner-offset (- si (data-product-offset dls type sum index))])
590 (when (not (on-stack? (cadr e)))
591 (error #f "trying to destruct something that isn't a stack expression"))
592 (emit "# deconstructing")
594 (if (stack-type? (env-data-layouts env) product-type)
595 ; if copying from the stack, need to first copy
596 ; to a safe space above to avoid overwriting
597 ; the original result on the stack
598 ; this is bad. please remove this in the rewrite.
600 (emit-stack-copy inner-offset safe-space-offset product-type-size)
601 (emit-stack-copy safe-space-offset si product-type-size))
602 (emit "movq ~a(%rbp), %rax" inner-offset))))
604 (define (codegen-constructor tor)
605 (let* ([info (cadr tor)]
608 [constructor (car e)]
612 [tag (data-sum-tag (env-data-layouts env)
615 [inner-si (- si (type-size dls type))]
617 [product-types (cdr (assoc sum (cdr (assoc type dls))))]
620 (lambda (expr i product-type)
622 (- si (data-product-offset dls type sum i))]
623 [product-size (type-size dls product-type)])
624 (codegen-expr expr inner-si env)
626 (emit-stack-copy inner-si dest-offset product-size)
627 (emit "movq %rax, ~a(%rbp)" dest-offset))))])
630 (emit "movq $~a, ~a(%rbp)" tag si)
632 (for-each insert-product args (range 0 (length args)) product-types)))
634 (let* ([tor (data-tor env e)]
635 [constructor (eqv? 'constructor (caddr (cadr tor)))])
637 (codegen-constructor tor)
638 (codegen-destructor tor))))
640 (define (codegen-expr e si env)
643 ('closure (codegen-closure (cadr e) (caddr e) si env))
646 ('+ (codegen-add (cdr e) si env))
647 ('- (codegen-sub (cadr e) (caddr e) si env))
648 ('* (codegen-mul (cadr e) (caddr e) si env))
649 ('! (codegen-not (cadr e) si env))
650 ('= (codegen-eq (cadr e) (caddr e) si env))
651 ('bool->int (codegen-expr (cadr e) si env))
652 ('print (codegen-print (cadr e) si env))
655 (codegen-data-tor e si env)
656 (codegen-call (car e) (cdr e) si env)))))
658 ; this is a builtin being passed around as a variable
659 ; this should have been converted to a closure!
660 ('builtin (error #f "passing about a builtin!" e))
662 ('let (codegen-let (let-bindings e)
667 ('var (codegen-var e si env))
669 ('if (codegen-if (cadr e) (caddr e) (cadddr e) si env))
670 ('case (codegen-case (case-switch e) (case-cases e) si env))
672 ('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
673 ('int-literal (emit "movq $~a, %rax" e))
675 ('static-string (emit "movq ~a@GOTPCREL(%rip), %rax"
678 ('stack (case (ast-type (caddr e))
679 ['var (codegen-var e si env)]
680 [else (codegen-expr (caddr e) si env)]))
682 (else (error #f "don't know how to codegen this")))
683 (emit "# done ~a" e))
685 ; takes in a expr annotated with types and returns a type-less AST
686 ; with stack values wrapped
687 (define (annotate-stack-values data-layouts ann-e)
689 (ast-traverse strip (ann-expr e)))
690 (let* ([e (ann-expr ann-e)]
691 [type (ann-type ann-e)])
692 (if (stack-type? data-layouts type)
694 ,(ast-traverse (lambda (x) (annotate-stack-values data-layouts x)) e))
695 (ast-traverse (lambda (x)
696 (annotate-stack-values data-layouts x))
699 (define (free-vars dls prog)
700 (define bound (singletons dls))
703 ('builtin '()) ; do nothing
704 ('var (if (memv e bound) '() (list e)))
707 (set! bound (append (lambda-args e) bound))
708 (collect (lambda-body e))))
710 ('app (flat-map collect e))
711 ('if (flat-map collect (cdr e)))
713 (let ([bind-fvs (flat-map (lambda (a)
715 (set! bound (cons (car a) bound))
718 (append bind-fvs (flat-map collect (let-body e)))))
722 ; ((lambda (x) (+ x y)) 42) => ((closure lambda1 (y)) 42)
723 ; [(lambda1 . ((y), (x), (+ x y))]
724 ; for builtins, this generates a closure if it is used
725 ; outside of an immediate app
726 ; but only one closure for each builtin
728 (define (extract-lambdas dls program)
730 (define (add-lambda e)
731 (let* ((label (fresh-lambda))
732 (args (lambda-args e))
733 (captured (free-vars dls e))
734 (body (extract (lambda-body e)))
735 (new-lambda (cons label (list dls captured args body))))
736 (set! lambdas (cons new-lambda lambdas))
737 `(closure ,label ,captured))) ; todo: should we string->symbol?
739 (define (find-builtin-lambda e)
740 (let [(l (assq (builtin-name e) lambdas))]
741 (if l `(closure ,(car l) ,(caadr l)) #f)))
743 (define (builtin-name e)
750 ('bool->int "_bool2int")
752 (else (error #f "don't know this builtin"))))
753 (define (builtin-args e)
762 (else (error #f "don't know this builtin"))))
764 (define (add-builtin-lambda e)
765 (let* [(label (builtin-name e))
767 (args (builtin-args e))
769 (new-lambda (cons label (list dls captured args body)))]
770 (set! lambdas (cons new-lambda lambdas))
771 `(closure ,label ,captured)))
775 ('lambda (add-lambda e))
776 ('let `(let ,(map (lambda (b) `(,(car b) ,@(extract (cdr b)))) (let-bindings e))
777 ,@(map extract (let-body e))))
779 ; if a builtin is used as a function, don't generate lambda
780 (if (eqv? 'builtin (ast-type (car e)))
782 (list (extract (car e))))
783 (map extract (cdr e))))
786 (if (find-builtin-lambda e)
787 (find-builtin-lambda e)
788 (add-builtin-lambda e)))
791 (else (ast-traverse extract e))))
792 (let ((transformed (extract program)))
793 (cons lambdas transformed)))
795 (define (extract-strings program)
797 (strings '())) ; assoc list of labels -> string
798 (define (fresh-string)
799 (set! cur-string (+ cur-string 1))
800 (format "string~a" (- cur-string 1)))
804 (let ((label (fresh-string)))
805 (set! strings (cons (cons label e) strings))
806 `(static-string ,label)))
807 (else (ast-traverse extract e))))
808 (let ((transformed (extract program)))
809 (cons strings transformed))))
811 (define (emit-string-data s)
813 (emit "\t.string \"~a\"" (cdr s)))
816 ; 16(%rbp) mem arg 0 prev frame
817 ; -----------------------
818 ; 8(%rbp) return address cur frame
820 ; -8(%rbp) do what you want
821 ; ... do what you want
822 ; 0(%rsp) do what you want
824 (define (param-register n)
832 (else (error #f "need to test out the below"))
833 (else (format "~a(%rsp)" (- n 6)))))
835 (define (initialize-heap)
838 ('darwin "0x20000c5")
840 ; allocate some heap memory
841 (emit "mov $~a, %rax" mmap) ; mmap
842 (emit "xor %rdi, %rdi") ; addr = null
843 (emit "movq $1024, %rsi") ; length = 1kb
844 (emit "movq $0x3, %rdx") ; prot = read | write = 0x2 | 0x1
845 ; flags = anonymous | private
847 ('darwin (emit "movq $0x1002, %r10")) ; anon = 0x1000, priv = 0x02
848 ('linux (emit "movq $0x22, %r10"))) ; anon = 0x20, priv = 0x02
849 (emit "movq $-1, %r8") ; fd = -1
850 (emit "xor %r9, %r9") ; offset = 0
852 ; %rax now contains pointer to the start of the heap
855 (emit "movq heap_start@GOTPCREL(%rip), %rsi")
856 (emit "movq %rax, (%rsi)")))
858 (define (codegen program-without-stdlib)
861 (let* ([program (append stdlib program-without-stdlib)]
862 [data-layouts (program-data-layouts program)]
864 [pattern-matched (expand-pattern-matches program)]
865 [type-annotated (annotate-types pattern-matched)]
866 [stack-annotated (annotate-stack-values data-layouts
869 (strings-res (extract-strings stack-annotated))
870 (strings (car strings-res))
871 (lambdas-res (extract-lambdas data-layouts (cdr strings-res)))
872 (lambdas (car lambdas-res))
873 (xform-prog (cdr lambdas-res)))
875 ; verify pattern matches are total
876 (verify-cases data-layouts type-annotated)
878 (emit "\t.global _start")
881 (for-each codegen-lambda lambdas)
887 (emit "movq %rsp, %rbp") ; set up the base pointer
888 (codegen-expr xform-prog (- wordsize) (make-env data-layouts '()))
891 (emit "mov %rax, %rdi")
893 ('darwin (emit "movq $0x2000001, %rax"))
894 ('linux (emit "mov $60, %rax")))
902 (for-each emit-string-data strings)))
904 (define (compile-to-binary program output t)
906 (when (not (eq? (typecheck program) 'Int)) (error #f "not an Int"))
907 (let ([tmp-path "/tmp/a.s"])
908 (when (file-exists? tmp-path) (delete-file tmp-path))
909 (with-output-to-file tmp-path
910 (lambda () (codegen program)))
914 (system "as /tmp/a.s -o /tmp/a.o")
915 (system (format "ld /tmp/a.o -e _start -macosx_version_min 10.14 -static -o ~a" output)))
917 (system "as /tmp/a.s -o /tmp/a.o")
918 (system (format "ld /tmp/a.o -o ~a" output))))))
921 ; syscalls in linux and darwin use the following arguments for syscall instruction:
930 ; on darwin, unix/posix syscalls are offset by 0x2000000 (syscall classes)
931 ; https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/kern/syscalls.master
932 ; documentation for most syscalls: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys