-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.rkt
531 lines (479 loc) · 14.7 KB
/
compile.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#lang racket
(provide (all-defined-out))
(require "ast.rkt" "types.rkt" a86/ast)
;; Registers used
(define rax 'rax) ; return
(define rbx 'rbx) ; heap
(define rcx 'rcx) ; scratch
(define rdx 'rdx) ; return, 2
(define r8 'r8) ; scratch in +, -
(define r9 'r9) ; scratch in assert-type and tail-calls
(define rsp 'rsp) ; stack
(define rdi 'rdi) ; arg
;; type CEnv = [Listof Variable]
;; Expr -> Asm
(define (compile p)
(match (label-λ (desugar p)) ; <-- changed!
[(Prog '() e)
(prog (Extern 'peek_byte)
(Extern 'read_byte)
(Extern 'write_byte)
(Extern 'raise_error)
(Label 'entry)
(Mov rbx rdi)
(compile-e e '(#f))
(Mov rdx rbx)
(Ret)
(compile-λ-definitions (λs e)))])) ; <-- changed!
;; [Listof Defn] -> Asm
(define (compile-λ-definitions ds)
(seq
(match ds
['() (seq)]
[(cons d ds)
(seq (compile-λ-definition d)
(compile-λ-definitions ds))])))
;; This is the code generation for the lambdas themselves.
;; It's not very different from generating code for user-defined functions,
;; because lambdas _are_ user defined functions, they just don't have a name
;;
;; Defn -> Asm
(define (compile-λ-definition l)
(match l
[(Lam '() xs e) (error "Lambdas must be labelled before code gen (contact your compiler writer)")]
[(Lam f xs e)
(let* ((free (remq* xs (fvs e)))
; leave space for RIP
(env (parity (cons #f (cons (length free) (reverse (append xs free)))))))
(seq (Label (symbol->label f))
; we need the #args on the frame, not the length of the entire
; env (which may have padding)
(compile-e e env)
(Ret)))]))
(define (parity c)
(if (even? (length c))
(append c (list #f))
c))
;; Expr CEnv -> Asm
(define (compile-e e c)
(seq
(match e
[(? imm? i) (compile-value (get-imm i))]
[(Var x) (compile-variable x c)]
[(App f es) (compile-call f es c)]
[(Lam l xs e0) (compile-λ xs l (fvs e) c)] ; why do we ignore e0?
[(Prim0 p) (compile-prim0 p c)]
[(Prim1 p e) (compile-prim1 p e c)]
[(Prim2 p e1 e2) (compile-prim2 p e1 e2 c)]
[(If e1 e2 e3) (compile-if e1 e2 e3 c)]
[(Begin e1 e2) (compile-begin e1 e2 c)]
[(LetRec bs e1) (compile-letrec (map car bs) (map cadr bs) e1 c)]
[(Let x e1 e2) (compile-let x e1 e2 c)])))
;; Value -> Asm
(define (compile-value v)
(seq (Mov rax (imm->bits v))))
;; Id CEnv -> Asm
(define (compile-variable x c)
(let ((i (lookup x c)))
(seq (Mov rax (Offset rsp i)))))
;; (Listof Variable) Label (Listof Variable) CEnv -> Asm
(define (compile-λ xs f ys c)
(seq
; Save label address
(Lea rax (symbol->label f))
(Mov (Offset rbx 0) rax)
; Save the environment
(%% "Begin saving the env")
(Mov r8 (length ys))
(Mov (Offset rbx 8) r8)
(Mov r9 rbx)
(Add r9 16)
(copy-env-to-heap ys c 0)
(%% "end saving the env")
; Return a pointer to the closure
(Mov rax rbx)
(Or rax type-proc)
(Add rbx (* 8 (+ 2 (length ys))))))
;; (Listof Variable) CEnv Natural -> Asm
;; Pointer to beginning of environment in r9
(define (copy-env-to-heap fvs c i)
(match fvs
['() (seq)]
[(cons x fvs)
(seq
; Move the stack item in question to a temp register
(Mov r8 (Offset rsp (lookup x c)))
; Put the iterm in the heap
(Mov (Offset r9 i) r8)
; Do it again for the rest of the items, incrementing how
; far away from r9 the next item should be
(copy-env-to-heap fvs c (+ 8 i)))]))
;; Id CEnv -> Asm
(define (compile-fun f)
; Load the address of the label into rax
(seq (Lea rax (symbol->label f))
; Copy the value onto the heap
(Mov (Offset rbx 0) rax)
; Copy the heap address into rax
(Mov rax rbx)
; Tag the value as a proc
(Or rax type-proc)
; Bump the heap pointer
(Add rbx 8)))
;; Op0 CEnv -> Asm
(define (compile-prim0 p c)
(match p
['void (seq (Mov rax val-void))]
['read-byte (seq (pad-stack c)
(Call 'read_byte)
(unpad-stack c))]
['peek-byte (seq (pad-stack c)
(Call 'peek_byte)
(unpad-stack c))]))
;; Op1 Expr CEnv -> Asm
(define (compile-prim1 p e c)
(seq (compile-e e c)
(match p
['add1
(seq (assert-integer rax)
(Add rax (imm->bits 1)))]
['sub1
(seq (assert-integer rax)
(Sub rax (imm->bits 1)))]
['zero?
(let ((l1 (gensym)))
(seq (assert-integer rax)
(Cmp rax 0)
(Mov rax val-true)
(Je l1)
(Mov rax val-false)
(Label l1)))]
['char?
(let ((l1 (gensym)))
(seq (And rax mask-char)
(Xor rax type-char)
(Cmp rax 0)
(Mov rax val-true)
(Je l1)
(Mov rax val-false)
(Label l1)))]
['char->integer
(seq (assert-char rax)
(Sar rax char-shift)
(Sal rax int-shift))]
['integer->char
(seq assert-codepoint
(Sar rax int-shift)
(Sal rax char-shift)
(Xor rax type-char))]
['eof-object? (eq-imm val-eof)]
['write-byte
(seq assert-byte
(pad-stack c)
(Mov rdi rax)
(Call 'write_byte)
(unpad-stack c)
(Mov rax val-void))]
['box
(seq (Mov (Offset rbx 0) rax)
(Mov rax rbx)
(Or rax type-box)
(Add rbx 8))]
['unbox
(seq (assert-box rax)
(Xor rax type-box)
(Mov rax (Offset rax 0)))]
['car
(seq (assert-cons rax)
(Xor rax type-cons)
(Mov rax (Offset rax 8)))]
['cdr
(seq (assert-cons rax)
(Xor rax type-cons)
(Mov rax (Offset rax 0)))]
['empty? (eq-imm val-empty)]
['procedure-arity
;; TODO
(seq)
]
)))
;; Op2 Expr Expr CEnv -> Asm
(define (compile-prim2 p e1 e2 c)
(seq (compile-e e1 c)
(Push rax)
(compile-e e2 (cons #f c))
(match p
['+
(seq (Pop r8)
(assert-integer r8)
(assert-integer rax)
(Add rax r8))]
['-
(seq (Pop r8)
(assert-integer r8)
(assert-integer rax)
(Sub r8 rax)
(Mov rax r8))]
['eq?
(let ((l (gensym)))
(seq (Cmp rax (Offset rsp 0))
(Sub rsp 8)
(Mov rax val-true)
(Je l)
(Mov rax val-false)
(Label l)))]
['cons
(seq (Mov (Offset rbx 0) rax)
(Pop rax)
(Mov (Offset rbx 8) rax)
(Mov rax rbx)
(Or rax type-cons)
(Add rbx 16))])))
;; Id [Listof Expr] CEnv -> Asm
;; Here's why this code is so gross: you have to align the stack for the call
;; but you have to do it *before* evaluating the arguments es, because you need
;; es's values to be just above 'rsp when the call is made. But if you push
;; a frame in order to align the call, you've got to compile es in a static
;; environment that accounts for that frame, hence:
(define (compile-call f es c)
(let* ((cnt (length es))
(aligned (even? (+ cnt (length c))))
(i (if aligned 1 2))
(c+ (if aligned
c
(cons #f c)))
(c++ (cons #f c+)))
(seq
(%% (~a "Begin compile-call: aligned = " aligned " function: " f))
; Adjust the stack for alignment, if necessary
(if aligned
(seq)
(Sub rsp 8))
; Generate the code for the thing being called
; and push the result on the stack
(compile-e f c+)
(%% "Push function on stack")
(Push rax)
(%% (~a "Begin compile-es: es = " es))
; Generate the code for the arguments
; all results will be put on the stack (compile-es does this)
(compile-es es c++)
; Get the function being called off the stack
; Ensure it's a proc and remove the tag
; Remember it points to the _closure_
(%% "Get function off stack")
(Mov rax (Offset rsp (* 8 cnt)))
(assert-proc rax)
(Xor rax type-proc)
(%% "Get closure env")
(copy-closure-env-to-stack)
(%% "finish closure env")
; get the size of the env and save it on the stack
(Mov rcx (Offset rax 8))
(Push rcx)
; Actually call the function
(Mov rax (Offset rax 0))
(Call rax)
; Get the size of the env off the stack
(Pop rcx)
(Sal rcx 3)
; pop args
; First the number of arguments + alignment + the closure
; then captured values
(Add rsp (* 8 (+ i cnt)))
(Add rsp rcx))))
;; -> Asm
;; Copy closure's (in rax) env to stack in rcx
(define (copy-closure-env-to-stack)
(let ((copy-loop (symbol->label (gensym 'copy_closure)))
(copy-done (symbol->label (gensym 'copy_done))))
(seq
(Mov r8 (Offset rax 8)) ; length
(Mov r9 rax)
(Add r9 16) ; start of env
(Label copy-loop)
(Cmp r8 0)
(Je copy-done)
(Mov rcx (Offset r9 0))
(Push rcx) ; Move val onto stack
(Sub r8 1)
(Add r9 8)
(Jmp copy-loop)
(Label copy-done))))
;; [Listof Expr] CEnv -> Asm
(define (compile-es es c)
(match es
['() '()]
[(cons e es)
(seq (compile-e e c)
(Push rax)
(compile-es es (cons #f c)))]))
;; Imm -> Asm
(define (eq-imm imm)
(let ((l1 (gensym)))
(seq (Cmp rax imm)
(Mov rax val-true)
(Je l1)
(Mov rax val-false)
(Label l1))))
;; Expr Expr Expr CEnv -> Asm
(define (compile-if e1 e2 e3 c)
(let ((l1 (gensym 'if))
(l2 (gensym 'if)))
(seq (compile-e e1 c)
(Cmp rax val-false)
(Je l1)
(%% (~a "Compiling then: " e2))
(compile-e e2 c)
(Jmp l2)
(Label l1)
(%% (~a "Compiling else: " e3))
(compile-e e3 c)
(Label l2))))
;; Expr Expr CEnv -> Asm
(define (compile-begin e1 e2 c)
(seq (compile-e e1 c)
(compile-e e2 c)))
;; Id Expr Expr CEnv -> Asm
(define (compile-let x e1 e2 c)
(seq (compile-e e1 c)
(Push rax)
(compile-e e2 (cons x c))
(Add rsp 8)))
;; (Listof Variable) (Listof Lambda) Expr CEnv -> Asm
(define (compile-letrec fs ls e c)
(seq
(%% (~a "Start compile letrec with" fs))
(compile-letrec-λs ls c)
(%% "letrec-init follows")
(compile-letrec-init fs ls (append (reverse fs) c))
(%% "Finish compile-letrec-init")
(compile-e e (append (reverse fs) c))
(Add rsp (* 8 (length fs)))))
;; (Listof Lambda) CEnv -> Asm
;; Create a bunch of uninitialized closures and push them on the stack
(define (compile-letrec-λs ls c)
(match ls
['() (seq)]
[(cons l ls)
(match l
[(Lam lab as body)
(let ((ys (fvs l)))
(seq
(Lea rax (symbol->label lab))
(Mov (Offset rbx 0) rax)
(Mov rax (length ys))
(Mov (Offset rbx 8) rax)
(Mov rax rbx)
(Or rax type-proc)
(%% (~a "The fvs of " lab " are " ys))
(Add rbx (* 8 (+ 2 (length ys))))
(Push rax)
(compile-letrec-λs ls (cons #f c))))])]))
;; (Listof Variable) (Listof Lambda) CEnv -> Asm
(define (compile-letrec-init fs ls c)
(match fs
['() (seq)]
[(cons f fs)
(let ((ys (fvs (first ls))))
(seq
(Mov r9 (Offset rsp (lookup f c)))
(Xor r9 type-proc)
(Add r9 16) ; move past label and length
(copy-env-to-heap ys c 0)
(compile-letrec-init fs (rest ls) c)))]))
;; CEnv -> Asm
;; Pad the stack to be aligned for a call with stack arguments
(define (pad-stack-call c i)
(match (even? (+ (length c) i))
[#f (seq (Sub rsp 8) (% "padding stack"))]
[#t (seq)]))
;; CEnv -> Asm
;; Pad the stack to be aligned for a call
(define (pad-stack c)
(pad-stack-call c 0))
;; CEnv -> Asm
;; Undo the stack alignment after a call
(define (unpad-stack-call c i)
(match (even? (+ (length c) i))
[#f (seq (Add rsp 8) (% "unpadding"))]
[#t (seq)]))
;; CEnv -> Asm
;; Undo the stack alignment after a call
(define (unpad-stack c)
(unpad-stack-call c 0))
;; Id CEnv -> Integer
(define (lookup x cenv)
(match cenv
['() (error "undefined variable:" x " Env: " cenv)]
[(cons y rest)
(match (eq? x y)
[#t 0]
[#f (+ 8 (lookup x rest))])]))
(define (in-frame cenv)
(match cenv
['() 0]
[(cons #f rest) 0]
[(cons y rest) (+ 1 (in-frame rest))]))
(define (assert-type mask type)
(λ (arg)
(seq (%% "Begin Assert")
(Mov r9 arg)
(And r9 mask)
(Cmp r9 type)
(Jne 'raise_error)
(%% "End Assert"))))
(define (type-pred mask type)
(let ((l (gensym)))
(seq (And rax mask)
(Cmp rax type)
(Mov rax (imm->bits #t))
(Je l)
(Mov rax (imm->bits #f))
(Label l))))
(define assert-integer
(assert-type mask-int type-int))
(define assert-char
(assert-type mask-char type-char))
(define assert-box
(assert-type ptr-mask type-box))
(define assert-cons
(assert-type ptr-mask type-cons))
(define assert-proc
(assert-type ptr-mask type-proc))
(define assert-codepoint
(let ((ok (gensym)))
(seq (assert-integer rax)
(Cmp rax (imm->bits 0))
(Jl 'raise_error)
(Cmp rax (imm->bits 1114111))
(Jg 'raise_error)
(Cmp rax (imm->bits 55295))
(Jl ok)
(Cmp rax (imm->bits 57344))
(Jg ok)
(Jmp 'raise_error)
(Label ok))))
(define assert-byte
(seq (assert-integer rax)
(Cmp rax (imm->bits 0))
(Jl 'raise_error)
(Cmp rax (imm->bits 255))
(Jg 'raise_error)))
;; Symbol -> Label
;; Produce a symbol that is a valid Nasm label
(define (symbol->label s)
(string->symbol
(string-append
"label_"
(list->string
(map (λ (c)
(if (or (char<=? #\a c #\z)
(char<=? #\A c #\Z)
(char<=? #\0 c #\9)
(memq c '(#\_ #\$ #\# #\@ #\~ #\. #\?)))
c
#\_))
(string->list (symbol->string s))))
"_"
(number->string (eq-hash-code s) 16))))