forked from keyz/p423-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source-grammar.ss
481 lines (441 loc) · 11.7 KB
/
source-grammar.ss
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
;; P423 / P523
;; Week 14 grammars
;;
;; Passes:
;; verify-scheme l-01 -> l-01
;; * convert-complex-datum l-01 -> l-02
;; * uncover-assigned l-02 -> l-03
;; * purify-letrec l-03 -> l-04
;; * convert-assignments l-04 -> l-05
;; optimize-direct-call l-05 -> l-06
;; remove-anonymous-lambda l-06 -> l-07
;; sanitize-binding-forms l-07 -> l-08
;; uncover-free l-08 -> l-09
;; convert-closures l-09 -> l-10
;; optimize-known-call l-10 -> l-11
;; introduce-procedure-primitives l-10 -> l-15
;; lift-letrec l-01 -> l-17
;; normalize-context l-17 -> l-18
;; specify-representation l-18 -> l-19
;; uncover-locals l-19 -> l-20
;; remove-let l-20 -> l-21
;; verify-uil l-22 -> l-22
;; remove-complex-opera* l-22 -> l-23
;; flatten-set! l-23 -> l-24
;; impose-calling-conventions l-24 -> l-25
;; uncover-frame-conflict l-25 -> l-27
;; pre-assign-frame l-27 -> l-28
;; assign-new-frame l-28 -> l-29
;; finalize-frame-locations l-29 -> l-30
;; select-instructions l-30 -> l-30
;; uncover-register-conflict l-30 -> l-32
;; assign-registers l-32 -> l-33
;; everybody-home? l-33 -> bool
;; assign-frame l-33 -> l-29
;; discard-call-live l-33 -> l-35
;; finalize-locations l-35 -> l-36
;; expose-frame-var l-36 -> l-37
;; expose-basic-blocks l-37 -> l-39
;; optimize-jumps l-39 -> l-39
;; flatten-program l-39 -> l-41
;; generate-x86-64 l-41 -> ()
(p423-grammars
;; This is the INPUT to parse-scheme.
(l00-verify-scheme
(start Prog)
(Prog Expr)
(Expr
Immediate
(quote Datum)
(let ([UVar Expr] *) Body *)
(letrec ([UVar Expr] *) Body *)
(lambda (UVar *) Body *)
(and Expr *)
(or Expr *)
(not Expr)
(if Expr Expr)
(if Expr Expr Expr)
(begin Expr * Expr)
(set! UVar Expr)
(ValPrim Expr *)
(EffectPrim Expr *)
(PredPrim Expr *)
(Expr Expr *)
UVar
)
(Body Expr)
)
;; This is the OUTPUT of parse-scheme
(l01-parse-scheme
(%remove Expr)
(%add
(Expr
(quote Immediate)
(let ([UVar Expr] *) Body)
(letrec ([UVar Expr] *) Body)
(lambda (UVar *) Body)
(if Expr Expr Expr)
(begin Expr * Expr)
(set! UVar Expr)
(ValPrim Expr *)
(EffectPrim Expr *)
(PredPrim Expr *)
(Expr Expr *)
UVar
)))
(l02-convert-complex-datum
(%remove (Expr quote))
(%add (Expr (quote Immediate)))
)
(l03-uncover-assigned
(%remove Body)
(%add (Body (assigned (UVar *) Expr)))
)
(l04-purify-letrec
(%remove (Expr letrec lambda))
(%add
(Expr (letrec ([UVar Lamb] *) Body))
(Lamb (lambda (UVar *) Body))
))
(l05-convert-assignments
(%remove (Expr set! letrec let)
Lamb Body)
(%add
(Expr (let ([UVar Expr] *) Expr)
(letrec ([UVar Lamb] *) Expr))
(Lamb (lambda (UVar *) Expr))
))
;; No l06-optimize-direct-call, grammar does not change.
(l07-remove-anonymous-lambda
(%remove (Expr Lamb let))
(%add (Expr (let ([UVar LambdaOrExpr]*) Expr))
(LambdaOrExpr Lamb Expr)))
(l08-sanitize-bindings
(%remove Lamb LambdaOrExpr (Expr let letrec))
(%add (Expr (let ([UVar Expr]*) Expr)
(letrec ([UVar (lambda (UVar *) Expr)] *) Expr))))
(l09-uncover-free
(%remove (Expr letrec))
(%add
(Expr
(letrec ((UVar (lambda (UVar *) (free (UVar *) Expr))) *) Expr)
)
)
)
(l10-convert-closures
(%remove (Expr letrec))
(%add
(Expr
(letrec ((Label (lambda (UVar *) (bind-free (UVar *) Expr))) *) (closures ((UVar Label UVar *) *) Expr))
Label
)
)
)
(l15-introduce-procedure-primitives
(%remove (Expr letrec))
(%add
(Expr
(letrec ((Label (lambda (UVar *) Expr)) *) Expr)
)
)
)
(l17-lift-letrec
(%remove Prog (Expr letrec)) ;; Remove ALL. Start fresh.
(%add
(Prog (letrec ((Label (lambda (UVar *) Expr)) *) Expr)))
)
(l18-normalize-context
(%remove Prog Expr) ;; Remove ALL. Start fresh.
(%add
(Prog
(letrec ((Label (lambda (UVar *) Value)) *) Value))
(Pred
(let ([UVar Value]*) Pred)
(true)
(false)
(if Pred Pred Pred)
(begin Effect * Pred)
(PredPrim Value *))
(Effect
(let ([UVar Value]*) Effect)
(nop)
(if Pred Effect Effect)
(begin Effect * Effect)
(EffectPrim Value *)
(Value Value *))
(Value
(quote Immediate)
(let ([UVar Value]*) Value)
(if Pred Value Value)
(begin Effect * Value)
(ValPrim Value *)
(Value Value *)
UVar Label))
)
(l19-specify-representation
(%remove Prog Pred Effect Value) ;; Remove ALL. Start fresh.
(%add
(Prog
(letrec ((Label (lambda (UVar *) Tail)) *) Tail))
(Tail
(let ([UVar Value]*) Tail)
(if Pred Tail Tail)
(begin Effect * Tail)
(alloc Value)
(mref Value Value)
(Binop Value Value)
(Value Value *)
Triv)
(Pred
(let ([UVar Value]*) Pred)
(true)
(false)
(if Pred Pred Pred)
(begin Effect * Pred)
(Relop Value Value))
(Effect
(let ([UVar Value]*) Effect)
(nop)
(mset! Value Value Value)
(if Pred Effect Effect)
(begin Effect * Effect)
(Value Value *))
(Value
(let ([UVar Value]*) Value)
(if Pred Value Value)
(begin Effect * Value)
(alloc Value)
(mref Value Value)
(Binop Value Value)
(Value Value *)
Triv)
(Triv
UVar
Integer
Label)))
(l20-uncover-locals
(%remove Prog)
(%add
(Prog (letrec ((Label (lambda (UVar *) Body)) *) Body))
(Body (locals (UVar *) Tail))))
; (l21-remove-let
(l22-verify-uil
(%remove
(Tail let)
(Pred let)
(Effect let)
(Value let))
(%add (Effect (set! UVar Value))))
; (l22-verify-uil)
;; Replace Value with Triv in arguments of procedure calls and primitive application.
(l23-remove-complex-opera
(%remove
(Tail Binop Value alloc mref)
(Pred Relop)
(Value alloc mref Binop Value)
(Effect Value mset!))
(%add
(Tail
(alloc Triv)
(mref Triv Triv)
(Binop Triv Triv)
(Triv Triv *))
(Pred (Relop Triv Triv))
(Value (alloc Triv)
(mref Triv Triv)
(Binop Triv Triv)
(Triv Triv *))
(Effect
(mset! Triv Triv Triv)
(Triv Triv *)
)))
;; Remove Value, set! rhs may only be Triv or Binop.
;; We could treat mref's as Binops and save a bit in the grammar, but
;; then we wouldn't be able to verify that they are removed later.
(l24-flatten-set
(%remove
Value
(Effect set! UVar (alloc Triv)))
(%add
(Effect
(set! UVar Triv)
(set! UVar (Binop Triv Triv))
(set! UVar (Triv Triv *))
(set! UVar (alloc Triv))
(set! UVar (mref Triv Triv))
)))
;; alloc/mref will only occur on the RHS of set! after this pass:
(l25-impose-calling-conventions
(%remove
(Prog letrec)
(Tail Triv Binop alloc mref)
(Effect set! Triv)
(Triv UVar)
(Body locals))
(%add
(Prog (letrec ((Label (lambda () Body)) *) Body))
(Body (locals (UVar *)
(new-frames (Frame *)
Tail)))
;; Operands can include new frame vars (NFVs) which are
;; technically UVars even though they are used as locations:
(Tail (Triv Var *)) ;; Note 'Var' not 'Loc'.
(Effect
(set! Var Triv)
(set! Var (Binop Triv Triv))
(set! Var (alloc Triv))
(set! Var (mref Triv Triv))
(return-point Label Tail))
(Loc
Reg
FVar)
(Var
UVar
Loc)
(Triv Var)
(Frame (UVar *))))
;; After this point, alloc is gone and we reference the
;; allocation-pointer-register directly.
(l26-expose-allocation-pointer
(%remove (Effect set!))
(%add
(Effect (set! Var Triv)
(set! Var (Binop Triv Triv))
;; Remove alloc!
(set! Var (mref Triv Triv))
)))
(l26-expose-allocation-pointer
(%remove
(Effect set!)
)
(%add
(Effect
(set! Var Triv)
; (set! Var (alloc Triv))
(set! Var (Binop Triv Triv))
(set! Var (mref Triv Triv))
)
)
)
(l27-uncover-frame-conflict
(%remove
(Body locals))
(%add
(Body
(locals (UVar *)
(new-frames (Frame *)
(spills (UVar *)
(frame-conflict ((UVar Var *) *)
(call-live (UFVar *) Tail))))))
(UFVar UVar FVar)))
(l28-pre-assign-frame
(%remove
(Body locals))
(%add
(Body
;; Eliminate 'spills' only:
(locals (UVar *)
(new-frames (Frame *)
(locate ((UVar FVar) *)
(frame-conflict ((UVar Var *) *)
(call-live (UFVar *) Tail)))))
)))
;; This is an important grammar. Its the one that is used at the top
;; and end of every iteration of the register-allocation loop.
(l29-assign-new-frame
(%remove
(UFVar UVar FVar) ; No need for this after this point [Z]
(Body locals)
Frame)
(%add
(Body
;; Add ulocals, remove new-frames, call-live:
(locals (UVar *)
(ulocals (UVar *)
(locate ((UVar FVar) *)
(frame-conflict ((UVar Var *) *)
Tail))))
;; Add "finished" locate form, as this grammar is used in iterative register allocation.
(locate ((UVar Loc) *) Tail)
)))
;; Resolve NFVs into frame vars.
(l30-finalize-frame-locations
(%remove (Tail Triv))
(%add (Tail (Triv Loc *))))
;; Adds register-conflict to the deeply nested Body forms.
(l32-uncover-register-conflict
(%remove
(Body locals))
(%add
;; Ignore conflicts with frame vars:
(Conflict Reg UVar)
(Body
(locals (UVar *)
(ulocals (UVar *)
(locate ((UVar FVar) *)
(frame-conflict ((UVar Var *) *)
(register-conflict ((UVar Conflict *) *)
Tail))))))))
;; Adds the 'spill' form.
(l33-assign-registers
(%remove
(Body locals)
(Conflict))
(%add
(Body
(locals (UVar *)
(ulocals (UVar *)
(spills (UVar *)
(locate ((UVar FVar) *)
(frame-conflict ((UVar Var *) *)
Tail))))))))
; assign-frame: This is the same as l29-assign-new-frame
(l35-discard-call-live
(%remove
(Body locals)
(Tail Triv))
(%add
(Tail (Triv))))
(l36-finalize-locations
(%remove
(Body locate)
UVar
Var)
(%rename
(Body -> Tail)
(Var -> Loc)))
(l37-expose-frame-var
(%remove UFVar Loc)
(%add (Loc Reg Disp)))
(l38-expose-memory-operands
(%remove (Tail mref)
(Effect mset! set!))
(%add
(Loc Ind)
(Effect
(set! Loc Triv)
(set! Loc (Binop Triv Triv)))))
(l39-expose-basic-blocks
(%remove
(Tail if)
Pred
(Effect nop if begin return-point))
(%add
(Tail
(if (Relop Triv Triv) (Label) (Label)))))
;; No l40, optimize-jumps uses l39.
(l41-flatten-program
(%remove
Prog
Tail)
(%rename
(Effect -> Statement))
(%add
(Prog
(code Statement * Statement))
(Statement
(if (Relop Triv Triv) (jump Label))
(if (not (Relop Triv Triv)) (jump Label))
(jump Triv)
Label)))
)