X-Git-Url: https://git.lukelau.me/?a=blobdiff_plain;f=codegen.scm;h=c73677325f899e064200b678182e6290d9fe6b2d;hb=6cad928374127b2fae65d7023fd31a725f4bd1d9;hp=4e05bcf797b31d175a9629aac4b75868bbd9c72e;hpb=d6c23652adf6bf2e9494cdedfed853c288b16f3f;p=scheme.git diff --git a/codegen.scm b/codegen.scm index 4e05bcf..c736773 100644 --- a/codegen.scm +++ b/codegen.scm @@ -1,7 +1,8 @@ (load "typecheck.scm") (load "ast.scm") +(load "platform.scm") -(define target 'darwin) +(define target host-os) (define (emit . s) (begin @@ -45,9 +46,9 @@ (codegen-expr a si env) (emit "movq %rax, ~a(%rbp)" si) (codegen-expr b (- si wordsize) env) - (emit "subq ~a(%rbp), %rax" si) - (emit "not %rax") - (emit "andq $1, %rax")) + (emit "## ~a = ~b" a b) + (emit "cmpq ~a(%rbp), %rax" si) + (emit "sete %al")) ; 'write file handle addr-string num-bytes @@ -67,17 +68,12 @@ (emit "not %rcx") ; -%rcx = strlen + 1 (emit "dec %rcx") - (case target - ('darwin (emit "movq %rbx, %rsi") ; string addr (emit "movq %rcx, %rdx") ; num bytes (emit "movq $1, %rdi") ; file handle (stdout) - (emit "movq $0x2000004, %rax")) ; syscall 4 (write) - ('linux - (emit "mov %rbx, %rsi") ; string addr - (emit "mov %rcx, %rdx") ; num bytes - (emit "mov $1, %rax") ; file handle (stdout) - (emit "mov $1, %rdi"))) ; syscall 1 (write) + (case target + ('darwin (emit "mov $0x2000004, %rax")) ; syscall 4 (write) + ('linux (emit "mov $1, %rax"))) ; syscall 1 (write) (emit "syscall")) (define (range s n) @@ -253,11 +249,6 @@ (emit "pop %rbp") ; restore caller's base pointer (emit "ret"))) -(define (codegen-string label) - (case target - ('darwin (emit "movq ~a@GOTPCREL(%rip), %rax" label)) - ('linux (emit "lea $~a, %rax" label)))) - (define cur-label 0) (define (fresh-label) (set! cur-label (+ 1 cur-label)) @@ -304,12 +295,12 @@ ('bool-literal (emit "movq $~a, %rax" (if e 1 0))) ('int-literal (emit "movq $~a, %rax" e)) - ('static-string (codegen-string (cadr e))) + ('static-string (emit "movq ~a@GOTPCREL(%rip), %rax" + (cadr e))) (else (error #f "don't know how to codegen this")))) -(define (fold-map f x) (fold-left append '() (map f x))) (define (free-vars prog) (define bound '())