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
39 (define (expr-size dls e)
40 (if (eqv? (ast-type e) 'stack)
41 (type-size dls (cadr e))
44 (define (on-stack? expr)
49 ; does a movsq for something on the stack
50 ; src points to the start stack index, but not the top of that index
53 ; +------+ <-- to here
55 ; +------+ <-- src (size = 16)
57 ; +------+ <-- start copying from here...
58 (define (emit-stack-copy src dst size)
59 (let ([size-to-copy (- size wordsize)])
60 (emit "leaq ~a(%rbp), %rsi" (- src size-to-copy))
61 (emit "leaq ~a(%rbp), %rdi" (- dst size-to-copy))
62 (emit "movq $~a, %rcx" (/ size wordsize))
66 ; an environment consists of adt layouts in scope,
67 ; and any bound variables.
68 ; bound variables are an assoc list with their stack offset
69 (define make-env list)
70 (define env-data-layouts car)
71 (define env-bindings cadr)
72 (define (env-append-bindings env bindings)
73 (make-env (env-data-layouts env)
74 (append bindings (env-bindings env))))
76 (define (codegen-add xs si env)
79 (emit "movq ~a(%rbp), %rax" si)
83 (emit "addq $~a, ~a(%rbp)" y si)
85 (codegen-expr y (- si wordsize) env)
86 (emit "addq %rax, ~a(%rbp)" si))))
89 ; use si(%rbp) as the accumulator
90 (emit "movq $0, ~a(%rbp)" si)
93 (define (codegen-binop opcode)
95 (codegen-expr b si env)
96 (emit "movq %rax, ~a(%rbp)" si)
97 (codegen-expr a (- si wordsize) env)
98 (emit "~a ~a(%rbp), %rax" opcode si)))
100 (define codegen-sub (codegen-binop "sub"))
102 (define codegen-mul (codegen-binop "imul"))
104 (define (codegen-not x si env)
105 (codegen-expr x si env)
107 (emit "andq $1, %rax"))
109 (define (codegen-eq a b si env)
110 (codegen-expr a si env)
111 (emit "movq %rax, ~a(%rbp)" si)
112 (codegen-expr b (- si wordsize) env)
113 (emit "## ~a = ~b" a b)
114 (emit "cmpq ~a(%rbp), %rax" si)
117 ; 'write file handle addr-string num-bytes
119 (define (codegen-print x si env)
120 (codegen-expr x si env) ; x should be a static-string, producing a label
122 ; make a copy of string address since %rax and %rdi are clobbered
123 (emit "mov %rax, %rbx")
125 ; get the length of the null terminated string
126 (emit "mov %rax, %rdi")
127 (emit "xor %al, %al") ; set %al to 0
128 (emit "mov $-1, %rcx") ; max search length = max int = -1
129 (emit "cld") ; clear direction flag, search up in memory
130 (emit "repne scasb") ; scan string, %rcx = -strlen - 1 - 1
132 (emit "not %rcx") ; -%rcx = strlen + 1
135 (emit "movq %rbx, %rsi") ; string addr
136 (emit "movq %rcx, %rdx") ; num bytes
137 (emit "movq $1, %rdi") ; file handle (stdout)
139 ('darwin (emit "mov $0x2000004, %rax")) ; syscall 4 (write)
140 ('linux (emit "mov $1, %rax"))) ; syscall 1 (write)
143 (define (codegen-let bindings body si env)
145 ; is this a closure that captures itself?
146 ; e.g. (let ([x 3] [f (closure lambda0 (f x))]) (f))
147 (define (self-captive-closure? name expr)
148 (and (eqv? (ast-type expr) 'closure)
149 (memv name (caddr expr))))
151 ;; (define (emit-scc scc env)
152 ;; ; acc is a pair of the env and list of touchups
153 ;; (define (emit-binding acc binding)
154 ;; (let ([binding-name (car binding)]
155 ;; [binding-body (cadr binding)]
157 ;; [other-bindings (filter
158 ;; (lambda (x) (not (eqv? binding-name x)))
160 ;; [mutually-recursives
162 ;; (lambda (other-binding)
163 ;; (memv other-binding (references binding-body)))
166 ;; [new-touchups (append touchups (cdr acc))])
168 ;; ; TODO: assert that the only mutually recursives are closures
171 ;; (when (not (eqv? (ast-type (cadr binding))
174 ;; (cons new-env new-touchups)
177 ;; (fold-left emit-binding (cons env '()) scc))))
178 ; assoc map of binding name to size
181 (define dls (env-data-layouts env))
184 (map (lambda (binding) (cons (car binding) (expr-size dls (cadr binding))))
187 ; assoc map of binding name to offset
188 (define stack-offsets
190 (let* ([totals ; 2 6 8 16 22
191 (reverse (fold-left (lambda (acc x)
194 (cons (+ x (car acc)) acc)))
196 (map cdr stack-sizes)))]
198 [relative-offsets (map - totals (map cdr stack-sizes))]
199 [absolute-offsets (map (lambda (x) (- si x)) relative-offsets)])
200 (map cons (map car stack-sizes) absolute-offsets)))
203 ; the stack index used when codegening binding body and main body
205 ; [stack-offsets | inner-si]
206 [inner-si (- si (fold-left + 0 (map cdr stack-sizes)))]
208 [get-offset (lambda (n) (cdr (assoc n stack-offsets)))]
213 (let* ([scc-binding-offsets
216 (cons (cons name (get-offset name))
220 [scc-env (make-env (env-data-layouts env) scc-binding-offsets)])
223 (let* ([expr (cadr (assoc name bindings))]
224 [size (expr-size dls expr)])
225 (emit "## generating ~a with scc-env ~a" name scc-env)
226 (if (self-captive-closure? name expr)
227 ; if self-captive, insert a flag into the environment to let
228 ; codegen-closure realise this!
232 (env-data-layouts scc-env)
233 (cons (cons name 'self-captive)
234 (env-bindings scc-env))))
235 (codegen-expr expr inner-si scc-env))
238 ; copy over whatevers on the stack
239 (emit-stack-copy inner-si (get-offset name) size)
240 (emit "movq %rax, ~a(%rbp)" (get-offset name)))))
244 (reverse (sccs (graph bindings))))])
246 (for-each (lambda (form)
247 (codegen-expr form inner-si inner-env))
250 (define (codegen-var e si env)
251 (let* ([stack-type (on-stack? e)]
252 [name (if stack-type (caddr e) e)]
254 [dls (env-data-layouts env)]
256 [singleton? (and stack-type
258 (cdr (assoc stack-type dls))))]
260 [stack-offset (assoc name (env-bindings env))])
261 (when (and (not stack-offset) (not singleton?))
262 (error #f (format "Variable ~a is not bound" name)))
266 ; singletons don't need to be in the environment
267 ; just copy over the tag
268 (emit "movq $~a, ~a(%rbp)"
269 (data-sum-tag (env-data-layouts env)
273 (emit-stack-copy (cdr stack-offset) si (type-size dls stack-type)))
274 (emit "movq ~a(%rbp), %rax" (cdr stack-offset)))))
276 (define cur-lambda 0)
277 (define (fresh-lambda)
278 (set! cur-lambda (+ 1 cur-lambda))
279 (format "_lambda~a" (- cur-lambda 1)))
281 ; a closure on the heap looks like:
283 ; addr var1.... var2.... var3....
285 (define (codegen-closure label captured si env)
286 (let* ((heap-offsets (map (lambda (i) (+ 8 (* 8 i)))
287 (range 0 (length captured))))) ; 4, 12, 20, etc.
289 (emit "## creating closure")
291 (emit "movq heap_start@GOTPCREL(%rip), %rbx")
293 (emit "movq (%rbx), %rax") ; %rax = heap addr of closure
296 ; point heap_start to next space
297 (emit "addq $~a, (%rbx)" (+ 8 (* 8 (length captured))))
299 (emit "## storing address to lambda")
300 ; store the address to the lambda code
301 (emit "movq ~a@GOTPCREL(%rip), %rbx" label)
302 (emit "movq %rbx, 0(%rax)")
304 (emit "## storing captives")
305 ; store the captured vars
307 (lambda (var-name heap-offset)
308 (let ([stack-offset (cdr (assoc var-name (env-bindings env)))])
309 (emit "### captive ~a" var-name)
310 (if (eqv? stack-offset 'self-captive)
311 ; captive refers to this closure:
312 ; move heap addr of this closure to stack!
313 (emit "movq %rax, ~a(%rax)" heap-offset)
315 (emit "movq ~a(%rbp), %rbx" stack-offset)
316 (emit "movq %rbx, ~a(%rax)" heap-offset)))))
320 ; for now we can only call closures
321 (define (codegen-call f args si env)
322 (codegen-expr f si env)
324 (emit "## starting call")
326 (emit "movq %rax, ~a(%rbp)" si) ; store address of closure first on stack
328 ; codegen the arguments, store them intermediately
332 (emit "## arg no. ~a" (- i 1))
333 (codegen-expr e (- si (* wordsize i)) env)
334 ; store intermediate result on stack
335 (emit "movq %rax, ~a(%rbp)" (- si (* wordsize i)))))
337 args (range 1 (length args)))
339 ; now that we have everything we need on the stack,
340 ; move them into the param registers
342 (emit "## moving args into place")
344 (lambda (i) (emit "movq ~a(%rbp), ~a"
345 (- si (* wordsize i))
347 (range 1 (length args)))
349 ; todo: can this be made more efficient
350 (emit "movq ~a(%rbp), %rax" si) ; load back pointer to closure
352 (emit "## moving captives into place")
354 ; move captives into first argument
355 (emit "movq %rax, %rbx")
356 (emit "addq $8, %rbx")
357 (emit "movq %rbx, ~a" (param-register 0))
359 (emit "## performing call")
361 (emit "addq $~a, %rsp" si) ; adjust the stack pointer to account all the stuff we put in the env
362 (emit "callq *(%rax)") ; call closure function
363 (emit "subq $~a, %rsp" si))
366 ; 1st param: pointer to captured args
368 ; 3rd param: 2nd arg, etc.
370 (define (codegen-lambda l)
371 (let* ((label (car l))
373 (captives (car stuff))
376 ; params = what actually gets passed
377 (params (append captives args))
379 (stack-offsets (map (lambda (i)
380 (* (- wordsize) (+ 1 i)))
381 (range 0 (length params))))
383 [bindings (map cons params stack-offsets)]
384 [env (make-env '() bindings)])
387 (display "## lambda captives: ")
390 (display "## lambda args: ")
393 (display "## lambda body: ")
397 (emit "push %rbp") ; preserve caller's base pointer
399 (emit "movq %rsp, %rbp") ; set up our own base pointer
401 ; load the captured vars onto the stack
405 (emit "# loading captive ~a" (list-ref captives i))
406 (emit "movq ~a(~a), %rbx" (* wordsize i) (param-register 0))
407 (emit "movq %rbx, ~a(%rbp)" (* (- wordsize) (+ 1 i)))))
408 (range 0 (length captives)))
410 ; load the args onto the stack
414 (emit "movq ~a, %rbx" (param-register (+ 1 i)))
415 (emit "movq %rbx, ~a(%rbp)"
417 (+ 1 (length captives) i)))))
418 (range 0 (length args)))
420 (codegen-expr body (* (- wordsize) (+ 1 (length params))) env)
422 (emit "pop %rbp") ; restore caller's base pointer
426 (define (fresh-label)
427 (set! cur-label (+ 1 cur-label))
428 (format "label~a" (- cur-label 1)))
430 (define (codegen-if cond then else si env)
431 (codegen-expr cond si env)
432 (emit "cmpq $0, %rax")
433 (let ((exit-label (fresh-label))
434 (else-label (fresh-label)))
435 (emit "je ~a" else-label)
436 (codegen-expr then si env)
437 (emit "jmp ~a" exit-label)
438 (emit "~a:" else-label)
439 (codegen-expr else si env)
440 (emit "~a:" exit-label)))
442 (define (codegen-case switch cases si env)
443 (define dls (env-data-layouts env))
444 (define exit-label (fresh-label))
447 ; checks if equal and returns assoc list of bindings
448 (define (check-equal jne-label type inner-offset x)
450 ; TODO: tidy this up! comparibles and binds could be merged
451 ; (foo a 2 (bar x)) -> ((2 Int 1) ((bar x) A 2))
453 (define (comparibles sum)
454 (let ([product-types (cdr (assoc sum (cdr (assoc type dls))))])
455 (if (null? product-types)
457 (filter (lambda (x) (not (eqv? 'var (ast-type (car x)))))
458 (map (lambda (x t i) (list x t i))
461 (range 0 (length product-types)))))))
464 (let ([product-types (cdr (assoc sum (cdr (assoc type dls))))])
465 (if (null? product-types)
467 (filter (lambda (x) (eqv? 'var (ast-type (car x))))
471 (data-product-offset dls type sum i))))
473 (range 0 (length (cdr x))))))))
476 (let ([sums (assoc type dls)])
478 (let* ([sum (if (list? x) (car x) x)] ; can sometimes be a singleton
479 [tag (data-sum-tag dls type sum)])
480 ; the tag is at the top (beginning) of the adt on the stack
481 (emit "cmpq $~a, ~a(%rbp)" tag inner-offset)
482 (emit "jne ~a" jne-label)
486 (lambda (cmp) ; cmp = (x type index)
487 (check-equal jne-label
489 (- inner-offset (data-product-offset dls type sum (caddr cmp)))
492 (if (eqv? 'var (ast-type x))
493 (list (cons x inner-offset))
495 (emit "cmpq $~a, ~a(%rbp)" x inner-offset)
496 (emit "jne ~a" jne-label)
499 (define (codegen-adt-match type case)
500 (let* ([match (car case)]
502 [next-section-label (fresh-label)]
503 [inner-si (- si (type-size dls type))]
504 [new-env (env-append-bindings env
505 (check-equal next-section-label type si match))])
508 (codegen-expr expr inner-si new-env)
509 (emit "jmp ~a" exit-label)
511 (emit "~a:" next-section-label)))
513 (define (codegen-literal-match case)
514 (let ([next-section-label (fresh-label)])
515 (emit "cmpq $~a, %rax" (car case))
516 (emit "jne ~a" next-section-label)
517 (codegen-expr (cadr case) si env)
518 (emit "jmp ~a" exit-label)
519 (emit "~a:" next-section-label)))
521 ; generate the switch
522 ; (and store it on the stack if not a stack value)
523 (codegen-expr switch si env)
525 (if (eqv? 'stack (ast-type switch))
527 (for-each (lambda (x) (codegen-adt-match (cadr switch) x)) cases)
528 (for-each codegen-literal-match cases))
529 (emit "~a:" exit-label))
532 (define (data-tor env e)
533 (if (not (list? e)) #f
534 (assoc (car e) (flat-map data-tors (env-data-layouts env)))))
536 ; returns the internal offset in bytes of a product within an ADT
537 ; given the constructor layout
538 ; constructor-layout: (foo (Int Bool))
539 (define (data-product-offset data-layouts type sum index)
540 (let* ([products (cdr (assoc sum (cdr (assoc type data-layouts))))]
541 [to-traverse (list-head products index)])
543 (lambda (acc t) (+ acc (type-size data-layouts t)))
544 wordsize ; skip the tag in the first word
547 (define (data-sum-tag data-layouts type sum)
549 (define (go acc sums)
550 (when (null? sums) (error #f "data-sum-tag no sum for type" sum type))
551 (if (eqv? sum (car sums))
553 (go (+ 1 acc) (cdr sums))))
554 (let* ([type-sums (cdr (assoc type data-layouts))])
555 (go 0 (map car type-sums))))
557 (define (codegen-data-tor e si env)
559 (define dls (env-data-layouts env))
561 (define (codegen-destructor tor)
562 (let* ([res (codegen-expr (cadr e) si env)]
567 [product-type (cadddr info)]
568 [product-type-size (type-size dls product-type)]
570 [safe-space-offset (- si (type-size dls type))]
572 [inner-offset (- si (data-product-offset dls type sum index))])
574 (when (not (on-stack? (cadr e)))
575 (error #f "trying to destruct something that isn't a stack expression"))
576 (emit "# deconstructing")
578 (if (stack-type? (env-data-layouts env) product-type)
579 ; if copying from the stack, need to first copy
580 ; to a safe space above to avoid overwriting
581 ; the original result on the stack
582 ; this is bad. please remove this in the rewrite.
584 (emit-stack-copy inner-offset safe-space-offset product-type-size)
585 (emit-stack-copy safe-space-offset si product-type-size))
586 (emit "movq ~a(%rbp), %rax" inner-offset))))
588 (define (codegen-constructor tor)
589 (let* ([info (cadr tor)]
592 [constructor (car e)]
596 [tag (data-sum-tag (env-data-layouts env)
600 [inner-si (- si (type-size dls type))]
602 [product-types (cdr (assoc sum (cdr (assoc type dls))))]
605 (lambda (expr i product-type)
607 (- si (data-product-offset dls type sum i))]
608 [product-size (type-size dls product-type)])
609 (codegen-expr expr inner-si env)
611 (emit-stack-copy inner-si dest-offset product-size)
612 (emit "movq %rax, ~a(%rbp)" dest-offset))))])
615 (emit "movq $~a, ~a(%rbp)" tag si)
617 (for-each insert-product args (range 0 (length args)) product-types)))
619 (let* ([tor (data-tor env e)]
620 [constructor (eqv? 'constructor (caddr (cadr tor)))])
622 (codegen-constructor tor)
623 (codegen-destructor tor))))
625 (define (codegen-expr e si env)
628 ('closure (codegen-closure (cadr e) (caddr e) si env))
631 ('+ (codegen-add (cdr e) si env))
632 ('- (codegen-sub (cadr e) (caddr e) si env))
633 ('* (codegen-mul (cadr e) (caddr e) si env))
634 ('! (codegen-not (cadr e) si env))
635 ('= (codegen-eq (cadr e) (caddr e) si env))
636 ('bool->int (codegen-expr (cadr e) si env))
637 ('print (codegen-print (cadr e) si env))
640 (codegen-data-tor e si env)
641 (codegen-call (car e) (cdr e) si env)))))
643 ; this is a builtin being passed around as a variable
644 ; this should have been converted to a closure!
645 ('builtin (error #f "passing about a builtin!" e))
647 ('let (codegen-let (let-bindings e)
652 ('var (codegen-var e si env))
654 ('if (codegen-if (cadr e) (caddr e) (cadddr e) si env))
655 ('case (codegen-case (case-switch e) (case-cases e) si env))
657 ('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
658 ('int-literal (emit "movq $~a, %rax" e))
660 ('static-string (emit "movq ~a@GOTPCREL(%rip), %rax"
663 ('stack (case (ast-type (caddr e))
664 ['var (codegen-var e si env)]
665 [else (codegen-expr (caddr e) si env)]))
667 (else (error #f "don't know how to codegen this")))
668 (emit "# done ~a" e))
670 ; takes in a expr annotated with types and returns a type-less AST
671 ; with stack values wrapped
672 (define (annotate-stack-values data-layouts ann-e)
673 (define (stack-type? type)
674 (assoc type data-layouts))
676 (ast-traverse strip (ann-expr e)))
677 (let* ([e (ann-expr ann-e)]
678 [type (ann-type ann-e)])
679 (if (stack-type? type)
681 ,(ast-traverse (lambda (x) (annotate-stack-values data-layouts x)) e))
682 (ast-traverse (lambda (x)
683 (annotate-stack-values data-layouts x))
686 (define (free-vars prog)
690 ('builtin '()) ; do nothing
691 ('var (if (memv e bound) '() (list e)))
694 (set! bound (append (lambda-args e) bound))
695 (collect (lambda-body e))))
697 ('app (flat-map collect e))
698 ('if (flat-map collect (cdr e)))
700 (let ([bind-fvs (flat-map (lambda (a)
702 (set! bound (cons (car a) bound))
705 (append bind-fvs (flat-map collect (let-body e)))))
709 ; ((lambda (x) (+ x y)) 42) => ((closure lambda1 (y)) 42)
710 ; [(lambda1 . ((y), (x), (+ x y))]
711 ; for builtins, this generates a closure if it is used
712 ; outside of an immediate app
713 ; but only one closure for each builtin
715 (define (extract-lambdas program)
717 (define (add-lambda e)
718 (let* ((label (fresh-lambda))
719 (args (lambda-args e))
720 (captured (free-vars e))
721 (body (extract (lambda-body e)))
722 (new-lambda (cons label (list captured args body))))
723 (set! lambdas (cons new-lambda lambdas))
724 `(closure ,label ,captured))) ; todo: should we string->symbol?
726 (define (find-builtin-lambda e)
727 (let [(l (assq (builtin-name e) lambdas))]
728 (if l `(closure ,(car l) ,(caadr l)) #f)))
730 (define (builtin-name e)
737 ('bool->int "_bool2int")
739 (else (error #f "don't know this builtin"))))
740 (define (builtin-args e)
749 (else (error #f "don't know this builtin"))))
751 (define (add-builtin-lambda e)
752 (let* [(label (builtin-name e))
754 (args (builtin-args e))
756 (new-lambda (cons label (list captured args body)))]
757 (set! lambdas (cons new-lambda lambdas))
758 `(closure ,label ,captured)))
762 ('lambda (add-lambda e))
763 ('let `(let ,(map (lambda (b) `(,(car b) ,@(extract (cdr b)))) (let-bindings e))
764 ,@(map extract (let-body e))))
766 ; if a builtin is used as a function, don't generate lambda
767 (if (eqv? 'builtin (ast-type (car e)))
769 (list (extract (car e))))
770 (map extract (cdr e))))
773 (if (find-builtin-lambda e)
774 (find-builtin-lambda e)
775 (add-builtin-lambda e)))
778 (else (ast-traverse extract e))))
779 (let ((transformed (extract program)))
780 (cons lambdas transformed)))
782 (define (extract-strings program)
784 (strings '())) ; assoc list of labels -> string
785 (define (fresh-string)
786 (set! cur-string (+ cur-string 1))
787 (format "string~a" (- cur-string 1)))
791 (let ((label (fresh-string)))
792 (set! strings (cons (cons label e) strings))
793 `(static-string ,label)))
794 (else (ast-traverse extract e))))
795 (let ((transformed (extract program)))
796 (cons strings transformed))))
798 (define (emit-string-data s)
800 (emit "\t.string \"~a\"" (cdr s)))
803 ; 16(%rbp) mem arg 0 prev frame
804 ; -----------------------
805 ; 8(%rbp) return address cur frame
807 ; -8(%rbp) do what you want
808 ; ... do what you want
809 ; 0(%rsp) do what you want
811 (define (param-register n)
819 (else (error #f "need to test out the below"))
820 (else (format "~a(%rsp)" (- n 6)))))
822 (define (initialize-heap)
825 ('darwin "0x20000c5")
827 ; allocate some heap memory
828 (emit "mov $~a, %rax" mmap) ; mmap
829 (emit "xor %rdi, %rdi") ; addr = null
830 (emit "movq $1024, %rsi") ; length = 1kb
831 (emit "movq $0x3, %rdx") ; prot = read | write = 0x2 | 0x1
832 ; flags = anonymous | private
834 ('darwin (emit "movq $0x1002, %r10")) ; anon = 0x1000, priv = 0x02
835 ('linux (emit "movq $0x22, %r10"))) ; anon = 0x20, priv = 0x02
836 (emit "movq $-1, %r8") ; fd = -1
837 (emit "xor %r9, %r9") ; offset = 0
839 ; %rax now contains pointer to the start of the heap
842 (emit "movq heap_start@GOTPCREL(%rip), %rsi")
843 (emit "movq %rax, (%rsi)")))
845 (define (codegen program)
848 (let* ([data-layouts (program-data-layouts program)]
850 [pattern-matched (expand-pattern-matches program)]
851 [type-annotated (annotate-types pattern-matched)]
852 [stack-annotated (annotate-stack-values data-layouts
855 (strings-res (extract-strings stack-annotated))
856 (strings (car strings-res))
857 (lambdas-res (extract-lambdas (cdr strings-res)))
858 (lambdas (car lambdas-res))
859 (xform-prog (cdr lambdas-res)))
861 (emit "\t.global _start")
863 ; (emit ".p2align 4,,15") is this needed?
865 (for-each codegen-lambda lambdas)
871 (emit "movq %rsp, %rbp") ; set up the base pointer
873 (codegen-expr xform-prog (- wordsize) (make-env data-layouts '()))
876 (emit "mov %rax, %rdi")
878 ('darwin (emit "movq $0x2000001, %rax"))
879 ('linux (emit "mov $60, %rax")))
887 (for-each emit-string-data strings)))
889 (define (compile-to-binary program output t)
891 (when (not (eq? (typecheck program) 'Int)) (error #f "not an Int"))
892 (let ([tmp-path "/tmp/a.s"])
893 (when (file-exists? tmp-path) (delete-file tmp-path))
894 (with-output-to-file tmp-path
895 (lambda () (codegen program)))
899 (system "as /tmp/a.s -o /tmp/a.o")
900 (system (format "ld /tmp/a.o -e _start -macosx_version_min 10.14 -static -o ~a" output)))
902 (system "as /tmp/a.s -o /tmp/a.o")
903 (system (format "ld /tmp/a.o -o ~a" output))))))
906 ; syscalls in linux and darwin use the following arguments for syscall instruction:
915 ; on darwin, unix/posix syscalls are offset by 0x2000000 (syscall classes)
916 ; https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/kern/syscalls.master
917 ; documentation for most syscalls: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys