From 6f699d1ddbb1cd33ec3095a0e6e9a6eee157d708 Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Wed, 24 Jul 2019 14:30:34 +0100 Subject: [PATCH] Allocate heap at the start --- codegen.scm | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/codegen.scm b/codegen.scm index 4848d4d..9b3878a 100644 --- a/codegen.scm +++ b/codegen.scm @@ -121,7 +121,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) @@ -343,6 +342,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 '()) @@ -351,7 +365,10 @@ (emit "mov $60, %rax") (emit "syscall") - (emit "\t.data") + (emit ".data") + + (emit "heap_start:") + (emit "\t.quad 0") (for-each emit-string-data strings))) @@ -362,3 +379,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 -- 2.30.2