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)
73 (define (codegen-add xs si env)
76 (emit "movq ~a(%rbp), %rax" si)
80 (emit "addq $~a, ~a(%rbp)" y si)
82 (codegen-expr y (- si wordsize) env)
83 (emit "addq %rax, ~a(%rbp)" si))))
86 ; use si(%rbp) as the accumulator
87 (emit "movq $0, ~a(%rbp)" si)
90 (define (codegen-binop opcode)
92 (codegen-expr b si env)
93 (emit "movq %rax, ~a(%rbp)" si)
94 (codegen-expr a (- si wordsize) env)
95 (emit "~a ~a(%rbp), %rax" opcode si)))
97 (define codegen-sub (codegen-binop "sub"))
99 (define codegen-mul (codegen-binop "imul"))
101 (define (codegen-not x si env)
102 (codegen-expr x si env)
104 (emit "andq $1, %rax"))
106 (define (codegen-eq a b si env)
107 (codegen-expr a si env)
108 (emit "movq %rax, ~a(%rbp)" si)
109 (codegen-expr b (- si wordsize) env)
110 (emit "## ~a = ~b" a b)
111 (emit "cmpq ~a(%rbp), %rax" si)
114 ; 'write file handle addr-string num-bytes
116 (define (codegen-print x si env)
117 (codegen-expr x si env) ; x should be a static-string, producing a label
119 ; make a copy of string address since %rax and %rdi are clobbered
120 (emit "mov %rax, %rbx")
122 ; get the length of the null terminated string
123 (emit "mov %rax, %rdi")
124 (emit "xor %al, %al") ; set %al to 0
125 (emit "mov $-1, %rcx") ; max search length = max int = -1
126 (emit "cld") ; clear direction flag, search up in memory
127 (emit "repne scasb") ; scan string, %rcx = -strlen - 1 - 1
129 (emit "not %rcx") ; -%rcx = strlen + 1
132 (emit "movq %rbx, %rsi") ; string addr
133 (emit "movq %rcx, %rdx") ; num bytes
134 (emit "movq $1, %rdi") ; file handle (stdout)
136 ('darwin (emit "mov $0x2000004, %rax")) ; syscall 4 (write)
137 ('linux (emit "mov $1, %rax"))) ; syscall 1 (write)
140 (define (codegen-let bindings body si env)
142 ; is this a closure that captures itself?
143 ; e.g. (let ([x 3] [f (closure lambda0 (f x))]) (f))
144 (define (self-captive-closure? name expr)
145 (and (eqv? (ast-type expr) 'closure)
146 (memv name (caddr expr))))
148 ;; (define (emit-scc scc env)
149 ;; ; acc is a pair of the env and list of touchups
150 ;; (define (emit-binding acc binding)
151 ;; (let ([binding-name (car binding)]
152 ;; [binding-body (cadr binding)]
154 ;; [other-bindings (filter
155 ;; (lambda (x) (not (eqv? binding-name x)))
157 ;; [mutually-recursives
159 ;; (lambda (other-binding)
160 ;; (memv other-binding (references binding-body)))
163 ;; [new-touchups (append touchups (cdr acc))])
165 ;; ; TODO: assert that the only mutually recursives are closures
168 ;; (when (not (eqv? (ast-type (cadr binding))
171 ;; (cons new-env new-touchups)
174 ;; (fold-left emit-binding (cons env '()) scc))))
175 ; assoc map of binding name to size
178 (define dls (env-data-layouts env))
181 (map (lambda (binding) (cons (car binding) (expr-size dls (cadr binding))))
184 ; assoc map of binding name to offset
185 (define stack-offsets
187 (let* ([totals ; 2 6 8 16 22
188 (reverse (fold-left (lambda (acc x)
191 (cons (+ x (car acc)) acc)))
193 (map cdr stack-sizes)))]
195 [relative-offsets (map - totals (map cdr stack-sizes))]
196 [absolute-offsets (map (lambda (x) (- si x)) relative-offsets)])
197 (map cons (map car stack-sizes) absolute-offsets)))
200 ; the stack index used when codegening binding body and main body
202 ; [stack-offsets | inner-si]
203 [inner-si (- si (fold-left + 0 (map cdr stack-sizes)))]
205 [get-offset (lambda (n) (cdr (assoc n stack-offsets)))]
210 (let* ([scc-binding-offsets
213 (cons (cons name (get-offset name))
217 [scc-env (make-env (env-data-layouts env) scc-binding-offsets)])
220 (let* ([expr (cadr (assoc name bindings))]
221 [size (expr-size dls expr)])
222 (emit "## generating ~a with scc-env ~a" name scc-env)
223 (if (self-captive-closure? name expr)
224 ; if self-captive, insert a flag into the environment to let
225 ; codegen-closure realise this!
229 (env-data-layouts scc-env)
230 (cons (cons name 'self-captive)
231 (env-bindings scc-env))))
232 (codegen-expr expr inner-si scc-env))
235 ; copy over whatevers on the stack
236 (emit-stack-copy inner-si (get-offset name) size)
237 (emit "movq %rax, ~a(%rbp)" (get-offset name)))))
241 (reverse (sccs (graph bindings))))])
243 (for-each (lambda (form)
244 (codegen-expr form inner-si inner-env))
247 (define (codegen-var e si env)
248 (let* ([stack-type (on-stack? e)]
249 [name (if stack-type (caddr e) e)]
251 [dls (env-data-layouts env)]
253 [singleton? (and stack-type
255 (cdr (assoc stack-type dls))))]
257 [stack-offset (assoc name (env-bindings env))])
258 (when (and (not stack-offset) (not singleton?))
259 (error #f (format "Variable ~a is not bound" name)))
263 ; singletons don't need to be in the environment
264 ; just copy over the tag
265 (emit "movq $~a, ~a(%rbp)"
266 (data-sum-tag (env-data-layouts env)
270 (emit-stack-copy (cdr stack-offset) si (type-size dls stack-type)))
271 (emit "movq ~a(%rbp), %rax" (cdr stack-offset)))))
273 (define cur-lambda 0)
274 (define (fresh-lambda)
275 (set! cur-lambda (+ 1 cur-lambda))
276 (format "_lambda~a" (- cur-lambda 1)))
278 ; a closure on the heap looks like:
280 ; addr var1.... var2.... var3....
282 (define (codegen-closure label captured si env)
283 (let* ((heap-offsets (map (lambda (i) (+ 8 (* 8 i)))
284 (range 0 (length captured))))) ; 4, 12, 20, etc.
286 (emit "## creating closure")
288 (emit "movq heap_start@GOTPCREL(%rip), %rbx")
290 (emit "movq (%rbx), %rax") ; %rax = heap addr of closure
293 ; point heap_start to next space
294 (emit "addq $~a, (%rbx)" (+ 8 (* 8 (length captured))))
296 (emit "## storing address to lambda")
297 ; store the address to the lambda code
298 (emit "movq ~a@GOTPCREL(%rip), %rbx" label)
299 (emit "movq %rbx, 0(%rax)")
301 (emit "## storing captives")
302 ; store the captured vars
304 (lambda (var-name heap-offset)
305 (let ([stack-offset (cdr (assoc var-name (env-bindings env)))])
306 (emit "### captive ~a" var-name)
307 (if (eqv? stack-offset 'self-captive)
308 ; captive refers to this closure:
309 ; move heap addr of this closure to stack!
310 (emit "movq %rax, ~a(%rax)" heap-offset)
312 (emit "movq ~a(%rbp), %rbx" stack-offset)
313 (emit "movq %rbx, ~a(%rax)" heap-offset)))))
317 ; for now we can only call closures
318 (define (codegen-call f args si env)
319 (codegen-expr f si env)
321 (emit "## starting call")
323 (emit "movq %rax, ~a(%rbp)" si) ; store address of closure first on stack
325 ; codegen the arguments, store them intermediately
329 (emit "## arg no. ~a" (- i 1))
330 (codegen-expr e (- si (* wordsize i)) env)
331 ; store intermediate result on stack
332 (emit "movq %rax, ~a(%rbp)" (- si (* wordsize i)))))
334 args (range 1 (length args)))
336 ; now that we have everything we need on the stack,
337 ; move them into the param registers
339 (emit "## moving args into place")
341 (lambda (i) (emit "movq ~a(%rbp), ~a"
342 (- si (* wordsize i))
344 (range 1 (length args)))
346 ; todo: can this be made more efficient
347 (emit "movq ~a(%rbp), %rax" si) ; load back pointer to closure
349 (emit "## moving captives into place")
351 ; move captives into first argument
352 (emit "movq %rax, %rbx")
353 (emit "addq $8, %rbx")
354 (emit "movq %rbx, ~a" (param-register 0))
356 (emit "## performing call")
358 (emit "addq $~a, %rsp" si) ; adjust the stack pointer to account all the stuff we put in the env
359 (emit "callq *(%rax)") ; call closure function
360 (emit "subq $~a, %rsp" si))
363 ; 1st param: pointer to captured args
365 ; 3rd param: 2nd arg, etc.
367 (define (codegen-lambda l)
368 (let* ((label (car l))
370 (captives (car stuff))
373 ; params = what actually gets passed
374 (params (append captives args))
376 (stack-offsets (map (lambda (i)
377 (* (- wordsize) (+ 1 i)))
378 (range 0 (length params))))
380 [bindings (map cons params stack-offsets)]
381 [env (make-env '() bindings)])
384 (display "## lambda captives: ")
387 (display "## lambda args: ")
390 (display "## lambda body: ")
394 (emit "push %rbp") ; preserve caller's base pointer
396 (emit "movq %rsp, %rbp") ; set up our own base pointer
398 ; load the captured vars onto the stack
402 (emit "# loading captive ~a" (list-ref captives i))
403 (emit "movq ~a(~a), %rbx" (* wordsize i) (param-register 0))
404 (emit "movq %rbx, ~a(%rbp)" (* (- wordsize) (+ 1 i)))))
405 (range 0 (length captives)))
407 ; load the args onto the stack
411 (emit "movq ~a, %rbx" (param-register (+ 1 i)))
412 (emit "movq %rbx, ~a(%rbp)"
414 (+ 1 (length captives) i)))))
415 (range 0 (length args)))
417 (codegen-expr body (* (- wordsize) (+ 1 (length params))) env)
419 (emit "pop %rbp") ; restore caller's base pointer
423 (define (fresh-label)
424 (set! cur-label (+ 1 cur-label))
425 (format "label~a" (- cur-label 1)))
427 (define (codegen-if cond then else si env)
428 (codegen-expr cond si env)
429 (emit "cmpq $0, %rax")
430 (let ((exit-label (fresh-label))
431 (else-label (fresh-label)))
432 (emit "je ~a" else-label)
433 (codegen-expr then si env)
434 (emit "jmp ~a" exit-label)
435 (emit "~a:" else-label)
436 (codegen-expr else si env)
437 (emit "~a:" exit-label)))
439 (define (codegen-case switch cases si env)
440 (define dls (env-data-layouts env))
441 (define exit-label (fresh-label))
444 ; checks if equal and returns assoc list of bindings
445 (define (check-equal jne-label type inner-offset x)
447 ; (foo a 2 (bar x)) -> ((2 Int 1) ((bar x) A 2))
449 (define (comparibles sum)
450 (if (not (list? sum))
452 (let ([product-types (cdr (assoc sum (cdr (assoc type dls))))])
453 (filter (lambda (x) (not (eqv? 'var (ast-type (car x)))))
454 (map (lambda (x t i) (list x t i))
457 (range 0 (length product-types)))))))
459 (let ([sums (assoc type dls)])
461 (let* ([sum (if (list? x) (car x) x)] ; can sometimes be a singleton
462 [tag (data-sum-tag dls type sum)])
463 ; the tag is at the top (beginning) of the adt on the stack
464 (emit "cmpq $~a, ~a(%rbp)" tag inner-offset)
465 (emit "jne ~a" jne-label)
467 (lambda (cmpx cmpt cmpi)
468 (check-equal jne-label
470 (- inner-offset (data-product-offset dls type sum i))
473 (if (eqv? 'var (ast-type x))
474 (list (cons x inner-offset))
476 (emit "cmp $~a, ~a(%rbp)" x inner-offset)
477 (emit "jne ~a" jne-label)
480 (define (codegen-adt-match type case)
481 (let* ([match (car case)]
483 [next-section-label (fresh-label)]
484 [inner-si (- si (type-size dls type))]
485 [new-env (append (check-equal next-section-label type si match)
488 (codegen-expr expr inner-si new-env)
489 (emit "jmp ~a" exit-label)
491 (emit "~a:" next-section-label)))
493 (define (codegen-literal-match case)
494 (let ([next-section-label (fresh-label)])
495 (emit "cmpq $~a, %rax" (car case))
496 (emit "jne ~a" next-section-label)
497 (codegen-expr (cadr case) si env)
498 (emit "jmp ~a" exit-label)
499 (emit "~a:" next-section-label)))
501 ; generate the switch
502 ; (and store it on the stack if not a stack value)
503 (codegen-expr switch si env)
505 (if (eqv? 'stack (ast-type switch))
507 (for-each (lambda (x) (codegen-adt-match (cadr switch) x)) cases)
508 (for-each codegen-literal-match cases))
509 (emit "~a:" exit-label))
512 (define (data-tor env e)
513 (if (not (list? e)) #f
514 (assoc (car e) (flat-map data-tors (env-data-layouts env)))))
516 ; returns the internal offset in bytes of a product within an ADT
517 ; given the constructor layout
518 ; constructor-layout: (foo (Int Bool))
519 (define (data-product-offset data-layouts type sum index)
520 (let* ([products (cdr (assoc sum (cdr (assoc type data-layouts))))]
521 [to-traverse (list-head products index)])
523 (lambda (acc t) (+ acc (type-size data-layouts t)))
524 wordsize ; skip the tag in the first word
527 (define (data-sum-tag data-layouts type sum)
529 (define (go acc sums)
530 (when (null? sums) (error #f "data-sum-tag no sum for type" sum type))
531 (if (eqv? sum (car sums))
533 (go (+ 1 acc) (cdr sums))))
534 (let* ([type-sums (cdr (assoc type data-layouts))])
535 (go 0 (map car type-sums))))
537 (define (codegen-data-tor e si env)
539 (define dls (env-data-layouts env))
541 (define (codegen-destructor tor)
542 (let* ([res (codegen-expr (cadr e) si env)]
547 [product-type (cadddr info)]
548 [product-type-size (type-size dls product-type)]
550 [safe-space-offset (- si (type-size dls type))]
552 [inner-offset (- si (data-product-offset dls type sum index))])
554 (when (not (on-stack? (cadr e)))
555 (error #f "trying to destruct something that isn't a stack expression"))
556 (emit "# deconstructing")
558 (if (stack-type? (env-data-layouts env) product-type)
559 ; if copying from the stack, need to first copy
560 ; to a safe space above to avoid overwriting
561 ; the original result on the stack
562 ; this is bad. please remove this in the rewrite.
564 (emit-stack-copy inner-offset safe-space-offset product-type-size)
565 (emit-stack-copy safe-space-offset si product-type-size))
566 (emit "movq ~a(%rbp), %rax" inner-offset))))
568 (define (codegen-constructor tor)
569 (let* ([info (cadr tor)]
572 [constructor (car e)]
576 [tag (data-sum-tag (env-data-layouts env)
580 [inner-si (- si (type-size dls type))]
582 [product-types (cdr (assoc sum (cdr (assoc type dls))))]
585 (lambda (expr i product-type)
587 (- si (data-product-offset dls type sum i))]
588 [product-size (type-size dls product-type)])
589 (codegen-expr expr inner-si env)
591 (emit-stack-copy inner-si dest-offset product-size)
592 (emit "movq %rax, ~a(%rbp)" dest-offset))))])
595 (emit "movq $~a, ~a(%rbp)" tag si)
597 (for-each insert-product args (range 0 (length args)) product-types)))
599 (let* ([tor (data-tor env e)]
600 [constructor (eqv? 'constructor (caddr (cadr tor)))])
602 (codegen-constructor tor)
603 (codegen-destructor tor))))
605 (define (codegen-expr e si env)
608 ('closure (codegen-closure (cadr e) (caddr e) si env))
611 ('+ (codegen-add (cdr e) si env))
612 ('- (codegen-sub (cadr e) (caddr e) si env))
613 ('* (codegen-mul (cadr e) (caddr e) si env))
614 ('! (codegen-not (cadr e) si env))
615 ('= (codegen-eq (cadr e) (caddr e) si env))
616 ('bool->int (codegen-expr (cadr e) si env))
617 ('print (codegen-print (cadr e) si env))
620 (codegen-data-tor e si env)
621 (codegen-call (car e) (cdr e) si env)))))
623 ; this is a builtin being passed around as a variable
624 ; this should have been converted to a closure!
625 ('builtin (error #f "passing about a builtin!" e))
627 ('let (codegen-let (let-bindings e)
632 ('var (codegen-var e si env))
634 ('if (codegen-if (cadr e) (caddr e) (cadddr e) si env))
635 ('case (codegen-case (case-switch e) (case-cases e) si env))
637 ('bool-literal (emit "movq $~a, %rax" (if e 1 0)))
638 ('int-literal (emit "movq $~a, %rax" e))
640 ('static-string (emit "movq ~a@GOTPCREL(%rip), %rax"
643 ('stack (case (ast-type (caddr e))
644 ['var (codegen-var e si env)]
645 [else (codegen-expr (caddr e) si env)]))
647 (else (error #f "don't know how to codegen this")))
648 (emit "# done ~a" e))
650 ; takes in a expr annotated with types and returns a type-less AST
651 ; with stack values wrapped
652 (define (annotate-stack-values data-layouts ann-e)
653 (define (stack-type? type)
654 (assoc type data-layouts))
656 (ast-traverse strip (ann-expr e)))
657 (let* ([e (ann-expr ann-e)]
658 [type (ann-type ann-e)])
659 (if (stack-type? type)
661 ,(ast-traverse (lambda (x) (annotate-stack-values data-layouts x)) e))
662 (ast-traverse (lambda (x)
663 (annotate-stack-values data-layouts x))
666 (define (free-vars prog)
670 ('builtin '()) ; do nothing
671 ('var (if (memv e bound) '() (list e)))
674 (set! bound (append (lambda-args e) bound))
675 (collect (lambda-body e))))
677 ('app (flat-map collect e))
678 ('if (flat-map collect (cdr e)))
680 (let ([bind-fvs (flat-map (lambda (a)
682 (set! bound (cons (car a) bound))
685 (append bind-fvs (flat-map collect (let-body e)))))
689 ; ((lambda (x) (+ x y)) 42) => ((closure lambda1 (y)) 42)
690 ; [(lambda1 . ((y), (x), (+ x y))]
691 ; for builtins, this generates a closure if it is used
692 ; outside of an immediate app
693 ; but only one closure for each builtin
695 (define (extract-lambdas program)
697 (define (add-lambda e)
698 (let* ((label (fresh-lambda))
699 (args (lambda-args e))
700 (captured (free-vars e))
701 (body (extract (lambda-body e)))
702 (new-lambda (cons label (list captured args body))))
703 (set! lambdas (cons new-lambda lambdas))
704 `(closure ,label ,captured))) ; todo: should we string->symbol?
706 (define (find-builtin-lambda e)
707 (let [(l (assq (builtin-name e) lambdas))]
708 (if l `(closure ,(car l) ,(caadr l)) #f)))
710 (define (builtin-name e)
717 ('bool->int "_bool2int")
719 (else (error #f "don't know this builtin"))))
720 (define (builtin-args e)
729 (else (error #f "don't know this builtin"))))
731 (define (add-builtin-lambda e)
732 (let* [(label (builtin-name e))
734 (args (builtin-args e))
736 (new-lambda (cons label (list captured args body)))]
737 (set! lambdas (cons new-lambda lambdas))
738 `(closure ,label ,captured)))
742 ('lambda (add-lambda e))
743 ('let `(let ,(map (lambda (b) `(,(car b) ,@(extract (cdr b)))) (let-bindings e))
744 ,@(map extract (let-body e))))
746 ; if a builtin is used as a function, don't generate lambda
747 (if (eqv? 'builtin (ast-type (car e)))
749 (list (extract (car e))))
750 (map extract (cdr e))))
753 (if (find-builtin-lambda e)
754 (find-builtin-lambda e)
755 (add-builtin-lambda e)))
758 (else (ast-traverse extract e))))
759 (let ((transformed (extract program)))
760 (cons lambdas transformed)))
762 (define (extract-strings program)
764 (strings '())) ; assoc list of labels -> string
765 (define (fresh-string)
766 (set! cur-string (+ cur-string 1))
767 (format "string~a" (- cur-string 1)))
771 (let ((label (fresh-string)))
772 (set! strings (cons (cons label e) strings))
773 `(static-string ,label)))
774 (else (ast-traverse extract e))))
775 (let ((transformed (extract program)))
776 (cons strings transformed))))
778 (define (emit-string-data s)
780 (emit "\t.string \"~a\"" (cdr s)))
783 ; 16(%rbp) mem arg 0 prev frame
784 ; -----------------------
785 ; 8(%rbp) return address cur frame
787 ; -8(%rbp) do what you want
788 ; ... do what you want
789 ; 0(%rsp) do what you want
791 (define (param-register n)
799 (else (error #f "need to test out the below"))
800 (else (format "~a(%rsp)" (- n 6)))))
802 (define (initialize-heap)
805 ('darwin "0x20000c5")
807 ; allocate some heap memory
808 (emit "mov $~a, %rax" mmap) ; mmap
809 (emit "xor %rdi, %rdi") ; addr = null
810 (emit "movq $1024, %rsi") ; length = 1kb
811 (emit "movq $0x3, %rdx") ; prot = read | write = 0x2 | 0x1
812 ; flags = anonymous | private
814 ('darwin (emit "movq $0x1002, %r10")) ; anon = 0x1000, priv = 0x02
815 ('linux (emit "movq $0x22, %r10"))) ; anon = 0x20, priv = 0x02
816 (emit "movq $-1, %r8") ; fd = -1
817 (emit "xor %r9, %r9") ; offset = 0
819 ; %rax now contains pointer to the start of the heap
822 (emit "movq heap_start@GOTPCREL(%rip), %rsi")
823 (emit "movq %rax, (%rsi)")))
825 (define (codegen program)
828 (let* ([data-layouts (program-data-layouts program)]
830 [pattern-matched (expand-pattern-matches program)]
831 [type-annotated (annotate-types pattern-matched)]
832 [stack-annotated (annotate-stack-values data-layouts
835 (strings-res (extract-strings stack-annotated))
836 (strings (car strings-res))
837 (lambdas-res (extract-lambdas (cdr strings-res)))
838 (lambdas (car lambdas-res))
839 (xform-prog (cdr lambdas-res)))
841 (emit "\t.global _start")
843 ; (emit ".p2align 4,,15") is this needed?
845 (for-each codegen-lambda lambdas)
851 (emit "movq %rsp, %rbp") ; set up the base pointer
853 (codegen-expr xform-prog (- wordsize) (make-env data-layouts '()))
856 (emit "mov %rax, %rdi")
858 ('darwin (emit "movq $0x2000001, %rax"))
859 ('linux (emit "mov $60, %rax")))
867 (for-each emit-string-data strings)))
869 (define (compile-to-binary program output t)
871 (when (not (eq? (typecheck program) 'Int)) (error #f "not an Int"))
872 (let ([tmp-path "/tmp/a.s"])
873 (when (file-exists? tmp-path) (delete-file tmp-path))
874 (with-output-to-file tmp-path
875 (lambda () (codegen program)))
879 (system "as /tmp/a.s -o /tmp/a.o")
880 (system (format "ld /tmp/a.o -e _start -macosx_version_min 10.14 -static -o ~a" output)))
882 (system "as /tmp/a.s -o /tmp/a.o")
883 (system (format "ld /tmp/a.o -o ~a" output))))))
886 ; syscalls in linux and darwin use the following arguments for syscall instruction:
895 ; on darwin, unix/posix syscalls are offset by 0x2000000 (syscall classes)
896 ; https://opensource.apple.com/source/xnu/xnu-2782.20.48/bsd/kern/syscalls.master
897 ; documentation for most syscalls: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys