X-Git-Url: http://git.lukelau.me/?a=blobdiff_plain;f=codegen.scm;h=571f1ef8498c0a662f3dfa7254b0cf2f535e4374;hb=a006c661a194672c2d8f5730d3c207dd083ddbf0;hp=5303b4975854be55bfc5d36aedcf5e607ebe8db9;hpb=94d1c48d51bfe32aa1e14d4fb012b283ec9352ef;p=scheme.git diff --git a/codegen.scm b/codegen.scm index 5303b49..571f1ef 100644 --- a/codegen.scm +++ b/codegen.scm @@ -50,6 +50,9 @@ (define (codegen-print x si env) (codegen-expr x si env) ; x should be a static-string, producing a label + ; make a copy of string address since %rax and %rdi are clobbered + (emit "mov %rax, %rbx") + ; get the length of the null terminated string (emit "mov %rax, %rdi") (emit "xor %al, %al") ; set %al to 0 @@ -61,7 +64,7 @@ (emit "dec %rcx") (emit "mov %rcx, %rdx") ; number of bytes - (emit "mov %rax, %rsi") ; addr of string + (emit "mov %rbx, %rsi") ; addr of string (emit "mov $1, %rax") ; file handle 1 (stdout) (emit "mov $1, %rdi") ; syscall 1 (write) (emit "syscall")) @@ -102,8 +105,25 @@ (set! cur-lambda (+ 1 cur-lambda)) (format "_lambda~a" (- cur-lambda 1))) +; a closure on the heap looks like: +; 0-x x+0 x+4 x+12 x+20 +; label #vars var1.... var2.... var3.... +(define (codegen-closure label captured si env) + (let* ((heap-offsets (range 4 (length captured))) ; 4, 12, 20, etc. + (inner-si (- si (* (length captured) wordsize)))) + (emit "movl $~a, (heap_start)") + (emit "add $4, (heap_start)") + (for-each (lambda (var-name new-offset) + (emit "movq ~a(%rbp), ~a(heap_start)" ; todo: do we need to copy this? + (cdr (assoc var-name env)) + new-offset) + (emit "add $8, (heap_start)") + captured + stack-offsets) +) ; for now we can only call closures (define (codegen-call closure args si env) +; (codegen-expr closure si env) (when (not (eq? (ast-type closure) 'closure)) (error #f (format "~a is not a closure" closure))) (let* ((captured (caddr closure)) @@ -118,7 +138,6 @@ (param-register i))) captured (range 0 (length captured))) - ; then codegen the arguments and move them into the next param registers (for-each (lambda (e i) @@ -188,7 +207,7 @@ (define (codegen-expr e si env) (case (ast-type e) ('builtin e) - ('closure e) + ('closure (codegen-closure (cadr e) (caddr e) si env)) ('app (let ((callee (codegen-expr (car e) si env))) (case callee @@ -213,7 +232,7 @@ ('bool-literal (emit "movq $~a, %rax" (if e 1 0))) ('int-literal (emit "movq $~a, %rax" e)) - ('static-string (emit "movq $~a, %rax" (cadr e))) ; move label + ('static-string (emit "lea ~a, %rax" (cadr e))) ; move label (else (error #f "don't know how to codegen this")))) @@ -279,7 +298,7 @@ (let ((transformed (extract program))) (cons strings transformed)))) -(define (codegen-string-data s) +(define (emit-string-data s) (emit "~a:" (car s)) (emit "\t.string \"~a\"" (cdr s))) @@ -340,6 +359,21 @@ (for-each codegen-lambda lambdas) (emit "_start:") + + ; allocate some heap memory + (emit "mov $9, %rax") ; mmap + (emit "xor %rdi, %rdi") ; addr = null + (emit "movq $1024, %rsi") ; length = 1kb + (emit "movq $0x3, %rdx") ; prot = read | write = 0x2 | 0x1 + (emit "movq $0x22, %r10") ; flags = anonymous | private = 0x20 | 0x02 + (emit "movq $-1, %r8") ; fd = -1 + (emit "xor %r9, %r9") ; offset = 0 + (emit "syscall") + + ; %rax now contains pointer to the start of the heap + ; keep track of it + (emit "movq %rax, (heap_start)") + (emit "movq %rsp, %rbp") ; set up the base pointer (codegen-expr xform-prog 0 '()) @@ -348,9 +382,12 @@ (emit "mov $60, %rax") (emit "syscall") - (emit "\t.data") + (emit ".data") - (for-each codegen-string-data strings))) + (emit "heap_start:") + (emit "\t.quad 0") + + (for-each emit-string-data strings))) (define (compile-to-binary program output) (when (not (eq? (typecheck program) 'int)) (error #f "not an int")) @@ -359,3 +396,13 @@ (with-output-to-file tmp-path (lambda () (codegen program))) (system (format "clang -nostdlib /tmp/a.s -o ~a" output)))) + +; NOTES +; syscalls in linux use the following arguments for syscall instruction: +; %rax = syscall # +; %rdi = 1st arg +; %rsi = 2nd arg +; %rdx = 3rd arg +; %r10 = 4th arg +; %r8 = 5th arg +; %r9 = 6th arg