(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
(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"))
('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"))))
(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)))
(emit "\t.data")
- (for-each codegen-string-data strings)))
+ (for-each emit-string-data strings)))
(define (compile-to-binary program output)
(when (not (eq? (typecheck program) 'int)) (error #f "not an int"))