Skip to content

Commit aeed59e

Browse files
committed
Whitespace, copy-closure-env from reg.
1 parent 685396b commit aeed59e

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

www/notes/loot/compile.rkt

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
[`(if ,e0 ,e1 ,e2) (compile-tail-if e0 e1 e2 c)]
7979
[`(+ ,e0 ,e1) (compile-+ e0 e1 c)]
8080
[`(let ((,x ,e0)) ,e1) (compile-tail-let x e0 e1 c)]
81-
[`(letrec ,bs ,e0) (compile-tail-letrec (map first bs) (map second bs) e0 c)]
81+
[`(letrec ,bs ,e0) (compile-tail-letrec (map first bs) (map second bs) e0 c)]
8282
[`(λ ,xs ',l ,e0) (compile-λ xs l (fvs e) c)]
8383
[`(,e . ,es) (compile-tail-call e es c)]))
8484

@@ -109,14 +109,14 @@
109109
`(;; Save label address
110110
(lea rax (offset ,f 0))
111111
(mov (offset rdi 0) rax)
112-
112+
113113
;; Save the environment
114114
(mov r8 ,(length ys))
115115
(mov (offset rdi 1) r8)
116116
(mov r9 rdi)
117117
(add r9 16)
118118
,@(copy-env-to-heap ys c 0)
119-
119+
120120
;; Return a pointer to the closure
121121
(mov rax rdi)
122122
(or rax ,type-proc)
@@ -153,9 +153,13 @@
153153
(mov rax (offset rsp ,i))
154154
,@assert-proc
155155
(xor rax ,type-proc)
156-
(sub rsp ,stack-size)
157-
,@(copy-closure-env-to-stack (add1 (length es)))
158-
(call (offset rax 0))
156+
(sub rsp ,stack-size)
157+
158+
(mov rcx rsp) ; start of stack in rcx
159+
(add rcx ,(- (* 8 (+ 2 (length es)))))
160+
,@(copy-closure-env-to-stack)
161+
162+
(call (offset rax 0))
159163
(add rsp ,stack-size))))
160164

161165
;; LExpr (Listof LExpr) CEnv -> Asm
@@ -170,9 +174,33 @@
170174
,@(move-args (length es) i)
171175
,@assert-proc
172176
(xor rax ,type-proc)
173-
,@(copy-closure-env-to-stack (length es))
177+
178+
(mov rcx rsp) ; start of stack in rcx
179+
(add rcx ,(- (* 8 (+ 1 (length es)))))
180+
,@(copy-closure-env-to-stack)
181+
182+
;,@(copy-closure-env-to-stack (length es))
174183
(jmp (offset rax 0)))))
175184

185+
;; -> Asm
186+
;; Copy closure's (in rax) env to stack in rcx
187+
(define (copy-closure-env-to-stack)
188+
(let ((copy-loop (gensym 'copy_closure))
189+
(copy-done (gensym 'copy_done)))
190+
`((mov r8 (offset rax 1)) ; length
191+
(mov r9 rax)
192+
(add r9 16) ; start of env
193+
,copy-loop
194+
(cmp r8 0)
195+
(je ,copy-done)
196+
(mov rbx (offset r9 0))
197+
(mov (offset rcx 0) rbx)
198+
(sub r8 1)
199+
(add r9 8)
200+
(sub rcx 8)
201+
(jmp ,copy-loop)
202+
,copy-done)))
203+
#|
176204
;; Natural -> Asm
177205
;; Copy closure's (in rax) env to stack skipping n spots
178206
(define (copy-closure-env-to-stack n)
@@ -185,17 +213,18 @@
185213
(add rcx ,(- (* 8 (add1 n))))
186214
,copy-loop
187215
(cmp r8 0)
188-
(je ,copy-done)
189-
(mov rbx (offset r9 0))
216+
(je ,copy-done)
217+
(mov rbx (offset r9 0))
190218
(mov (offset rcx 0) rbx)
191219
(sub r8 1)
192220
(add r9 8)
193-
(sub rcx 8)
221+
(sub rcx 8)
194222
(jmp ,copy-loop)
195223
,copy-done)))
224+
|#
196225

197226
;; (Listof Variable) (Listof Lambda) Expr CEnv -> Asm
198-
(define (compile-letrec fs ls e c)
227+
(define (compile-letrec fs ls e c)
199228
(let ((c0 (compile-letrec-λs ls c))
200229
(c1 (compile-letrec-init fs ls (append (reverse fs) c)))
201230
(c2 (compile-e e (append (reverse fs) c))))

0 commit comments

Comments
 (0)