|
24 | 24 | [(Prim p es) (compile-prim p es c g)] |
25 | 25 | [(If e1 e2 e3) (compile-if e1 e2 e3 c g t?)] |
26 | 26 | [(Begin e1 e2) (compile-begin e1 e2 c g t?)] |
27 | | - [(Let x e1 e2) (compile-let x e1 e2 c g t?)] |
| 27 | + [(Let xs es e) (compile-let xs es e c g t?)] |
28 | 28 | [(App e es) (compile-app e es c g t?)] |
29 | 29 | [(Apply e es el) (compile-apply e es el c g t?)] |
30 | 30 | [(Lam _ _ _) (compile-lam e c g)] |
|
37 | 37 | (match (lookup x c) |
38 | 38 | [#f (if (memq x g) |
39 | 39 | (seq (Mov rax (Offset (symbol->label x) 0))) |
40 | | - (error "unbound variable"))] |
| 40 | + (error "unbound variable" x))] |
41 | 41 | [i (seq (Mov rax (Offset rsp i)))])) |
42 | 42 |
|
43 | 43 | ;; Op (Listof Expr) CEnv GEnv -> Asm |
|
65 | 65 | (seq (compile-e e1 c g #f) |
66 | 66 | (compile-e e2 c g t?))) |
67 | 67 |
|
68 | | -;; Id Expr Expr CEnv GEnv Bool -> Asm |
69 | | -(define (compile-let x e1 e2 c g t?) |
70 | | - (seq (compile-e e1 c g #f) |
71 | | - (Push rax) |
72 | | - (compile-e e2 (cons x c) g t?) |
73 | | - (Add rsp 8))) |
| 68 | +;; [Listof Id] [Listof Expr] Expr CEnv GEnv Bool -> Asm |
| 69 | +(define (compile-let xs es e c g t?) |
| 70 | + (seq (compile-es es c g) |
| 71 | + (compile-e e (append (reverse xs) c) g t?) |
| 72 | + (Add rsp (*8 (length xs))))) |
74 | 73 |
|
75 | 74 | ;; Id [Listof Expr] CEnv GEnv Bool -> Asm |
76 | 75 | (define (compile-app f es c g t?) |
|
84 | 83 | (define (compile-app-tail e es c g) |
85 | 84 | (seq (compile-es (cons e es) c g) |
86 | 85 | (move-args (add1 (length es)) (length c)) |
87 | | - (Add rsp (* 8 (length c))) |
88 | | - (Mov rax (Offset rsp (* 8 (length es)))) |
| 86 | + (Add rsp (*8 (length c))) |
| 87 | + (Mov rax (Offset rsp (*8 (length es)))) |
89 | 88 | (assert-proc rax) |
90 | 89 | (Xor rax type-proc) |
91 | 90 | (Mov rax (Offset rax 0)) |
|
96 | 95 | (cond [(zero? off) (seq)] |
97 | 96 | [(zero? i) (seq)] |
98 | 97 | [else |
99 | | - (seq (Mov r8 (Offset rsp (* 8 (sub1 i)))) |
100 | | - (Mov (Offset rsp (* 8 (+ off (sub1 i)))) r8) |
| 98 | + (seq (Mov r8 (Offset rsp (*8 (sub1 i)))) |
| 99 | + (Mov (Offset rsp (*8 (+ off (sub1 i)))) r8) |
101 | 100 | (move-args (sub1 i) off))])) |
102 | 101 |
|
103 | 102 | ;; Expr [Listof Expr] CEnv GEnv -> Asm |
104 | 103 | ;; The return address is placed above the arguments, so callee pops |
105 | 104 | ;; arguments and return address is next frame |
106 | 105 | (define (compile-app-nontail e es c g) |
107 | 106 | (let ((r (gensym 'ret)) |
108 | | - (i (* 8 (length es)))) |
| 107 | + (i (*8 (length es)))) |
109 | 108 | (seq (Lea rax r) |
110 | 109 | (Push rax) |
111 | 110 | (compile-es (cons e es) (cons #f c) g) |
|
126 | 125 | (compile-es (cons e es) (cons #f c) g) |
127 | 126 | (compile-e el (append (make-list (add1 (length es)) #f) (cons #f c)) g #f) |
128 | 127 |
|
129 | | - (Mov r10 (Offset rsp (* 8 (length es)))) |
| 128 | + (Mov r10 (Offset rsp (*8 (length es)))) |
130 | 129 |
|
131 | 130 | (Mov r15 (length es)) |
132 | 131 | (let ((loop (gensym)) |
|
159 | 158 | (free-vars-to-heap fvs c 8) |
160 | 159 | (Mov rax rbx) ; return value |
161 | 160 | (Or rax type-proc) |
162 | | - (Add rbx (* 8 (add1 (length fvs))))))) |
| 161 | + (Add rbx (*8 (add1 (length fvs))))))) |
163 | 162 |
|
164 | 163 | ;; Lambda -> Id |
165 | 164 | (define (lambda-name l) |
|
224 | 223 |
|
225 | 224 | ;; [Listof Pat] [Listof Expr] CEnv GEnv Symbol Bool -> Asm |
226 | 225 | (define (compile-match-clauses ps es c g done t?) |
227 | | - (match* (ps es) |
228 | | - [('() '()) (seq)] |
229 | | - [((cons p ps) (cons e es)) |
| 226 | + (match (cons ps es) |
| 227 | + [(cons '() '()) (seq)] |
| 228 | + [(cons (cons p ps) (cons e es)) |
230 | 229 | (seq (compile-match-clause p e c g done t?) |
231 | 230 | (compile-match-clauses ps es c g done t?))])) |
232 | 231 |
|
|
238 | 237 | (seq (Mov rax (Offset rsp 0)) ; restore value being matched |
239 | 238 | i |
240 | 239 | (compile-e e (append cm c) g t?) |
241 | | - (Add rsp (* 8 (length cm))) |
| 240 | + (Add rsp (*8 (length cm))) |
242 | 241 | (Jmp done) |
243 | 242 | f |
244 | 243 | (Label next))]))) |
|
267 | 266 | (Cmp rax 0) |
268 | 267 | (Jne fail)) |
269 | 268 | (seq (Label fail) |
270 | | - (Add rsp (* 8 (length cm))) |
| 269 | + (Add rsp (*8 (length cm))) |
271 | 270 | (Jmp next)) |
272 | 271 | cm))] |
273 | 272 | [(PSymb s) |
|
276 | 275 | (Cmp rax r9) |
277 | 276 | (Jne fail)) |
278 | 277 | (seq (Label fail) |
279 | | - (Add rsp (* 8 (length cm))) |
| 278 | + (Add rsp (*8 (length cm))) |
280 | 279 | (Jmp next)) |
281 | 280 | cm))] |
282 | 281 | [(PLit l) |
283 | 282 | (let ((fail (gensym))) |
284 | 283 | (list (seq (Cmp rax (imm->bits l)) |
285 | 284 | (Jne fail)) |
286 | 285 | (seq (Label fail) |
287 | | - (Add rsp (* 8 (length cm))) |
| 286 | + (Add rsp (*8 (length cm))) |
288 | 287 | (Jmp next)) |
289 | 288 | cm))] |
290 | 289 | [(PAnd p1 p2) |
|
295 | 294 | (list |
296 | 295 | (seq (Push rax) |
297 | 296 | i1 |
298 | | - (Mov rax (Offset rsp (* 8 (- (sub1 (length cm1)) (length cm))))) |
| 297 | + (Mov rax (Offset rsp (*8 (- (sub1 (length cm1)) (length cm))))) |
299 | 298 | i2) |
300 | 299 | (seq f1 f2) |
301 | 300 | cm2)])])] |
|
313 | 312 | i1) |
314 | 313 | (seq f1 |
315 | 314 | (Label fail) |
316 | | - (Add rsp (* 8 (length cm))) ; haven't pushed anything yet |
| 315 | + (Add rsp (*8 (length cm))) ; haven't pushed anything yet |
317 | 316 | (Jmp next)) |
318 | 317 | cm1))])] |
319 | 318 | [(PCons p1 p2) |
|
332 | 331 | (Push r8) ; push cdr |
333 | 332 | (Mov rax (Offset rax 8)) ; mov rax car |
334 | 333 | i1 |
335 | | - (Mov rax (Offset rsp (* 8 (- (sub1 (length cm1)) (length cm))))) |
| 334 | + (Mov rax (Offset rsp (*8 (- (sub1 (length cm1)) (length cm))))) |
336 | 335 | i2) |
337 | 336 | (seq f1 |
338 | 337 | f2 |
339 | 338 | (Label fail) |
340 | | - (Add rsp (* 8 (length cm))) ; haven't pushed anything yet |
| 339 | + (Add rsp (*8 (length cm))) ; haven't pushed anything yet |
341 | 340 | (Jmp next)) |
342 | 341 | cm2))])])] |
343 | 342 | [(PStruct n ps) |
|
358 | 357 | i) |
359 | 358 | (seq f |
360 | 359 | (Label fail) |
361 | | - (Add rsp (* 8 (length cm))) |
| 360 | + (Add rsp (*8 (length cm))) |
362 | 361 | (Jmp next)) |
363 | 362 | cm))])])) |
364 | 363 |
|
|
372 | 371 | (match (compile-struct-patterns ps cm1 next (add1 i)) |
373 | 372 | [(list is fs cmn) |
374 | 373 | (list |
375 | | - (seq (Mov rax (Offset rax (* 8 i))) |
| 374 | + (seq (Mov rax (Offset rax (*8 i))) |
376 | 375 | i1 |
377 | | - (Mov rax (Offset rsp (* 8 (sub1 (length cm1))))) |
| 376 | + (Mov rax (Offset rsp (*8 (sub1 (length cm1))))) |
378 | 377 | is) |
379 | 378 | (seq f1 fs) |
380 | 379 | cmn)])])])) |
0 commit comments