-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.rkt
580 lines (551 loc) · 19.1 KB
/
compiler.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#lang racket
(require racket/set racket/stream)
(require racket/fixnum)
(require graph)
(require "interp.rkt")
(require "interp-Lwhile.rkt")
(require "type-check-Lwhile.rkt")
(require "interp-Cwhile.rkt")
(require "type-check-Cwhile.rkt")
(require "utilities.rkt")
(require "priority_queue.rkt")
;pass modules
(require "shrink.rkt")
(require "uniquify.rkt")
(require "uncover-get.rkt")
(require "remove_complex_operands.rkt")
(require "explicate-control.rkt")
(require "select-instructions.rkt")
(require "uncover-live.rkt")
(require "build-interference.rkt")
(require "allocate-registers.rkt")
(provide (all-defined-out))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lint examples
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The following compiler pass is just a silly one that doesn't change
;; anything important, but is nevertheless an example of a pass. It
;; flips the arguments of +. -Jeremy
(define (flip-exp e)
(match e
[(Var x) e]
[(Prim 'read '()) (Prim 'read '())]
[(Prim '- (list e1)) (Prim '- (list (flip-exp e1)))]
[(Prim '+ (list e1 e2)) (Prim '+ (list (flip-exp e2) (flip-exp e1)))]))
(define (flip-Lint e)
(match e
[(Program info e) (Program info (flip-exp e))]))
;; Next we have the partial evaluation pass described in the book.
(define (pe-neg r)
(match r
[(Int n) (Int (fx- 0 n))]
[else (Prim '- (list r))]))
(define (pe-add r1 r2)
(match* (r1 r2)
[((Int n1) (Int n2)) (Int (fx+ n1 n2))]
[(_ _) (Prim '+ (list r1 r2))]))
(define (pe-exp e)
(match e
[(Int n) (Int n)]
[(Prim 'read '()) (Prim 'read '())]
[(Prim '- (list e1)) (pe-neg (pe-exp e1))]
[(Prim '+ (list e1 e2)) (pe-add (pe-exp e1) (pe-exp e2))]))
(define (pe-Lint p)
(match p
[(Program info e) (Program info (pe-exp e))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; HW1 Passes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (define (uniquify-exp env)
;; (lambda (e)
;; (match e
;; [(Var x)
;; (Var (dict-ref env x))]
;; [(Int n) (Int n)]
;; [(Let x e body)
;; (define x2 (gensym x))
;; (define env2 (dict-set env x x2))
;; (Let x2 ((uniquify-exp env) e) ((uniquify-exp env2) body))]
;; [(Prim op es)
;; (Prim op (for/list ([e es]) ((uniquify-exp env) e)))])))
;;
;; ;; uniquify : Lvar -> Lvar
;; (define (uniquify p)
;; (match p
;; [(Program info e) (Program info ((uniquify-exp '()) e))]))
;; remove-complex-opera* : Lvar -> Lvar^mon
;; (define (remove-complex-opera* p)
;;
;; ;; the argument is already atomic, so we leave it unchanged
;; (struct atomic-arg (e))
;;
;; ;; bind the (non-atomic) argument to a variable so that we can use the (atomic) variable in its place
;; (struct bind-arg (x e))
;;
;; (define (get-arg a)
;; (match a
;; [(atomic-arg e) e]
;; [(bind-arg x e) (Var x)]))
;;
;; ; given bindings=[(x1 . e1) (x2 . e2) ... (xn . en)],
;; ; return an expression (Let x1 e1 (Let x2 e2 ...body)...)
;; (define (multi-let bindings body)
;; (match bindings
;; [(cons (cons x e) rest) (Let x e (multi-let rest body))]
;; [(list) body]))
;;
;; (define (rco_atom e)
;; (match e
;; [(Var x) (atomic-arg (Var x))]
;; [(Int n) (atomic-arg (Int n))]
;; [(Let x e body)
;; (define x2 (gensym "x"))
;; (bind-arg x2 (Let x (rco_exp e) (rco_exp body)))]
;; [(Prim op es)
;; (define x2 (gensym "x"))
;; (define arg_results (for/list ([e es]) (rco_atom e)))
;; (define args (map get-arg arg_results))
;; (define bindings
;; (for/list ([res arg_results] #:when (bind-arg? res))
;; (cons (bind-arg-x res) (bind-arg-e res))))
;; (bind-arg x2 (multi-let bindings (Prim op args)))]))
;;
;; (define (rco_exp e)
;; (match e
;; [(Var x) (Var x)]
;; [(Int n) (Int n)]
;; [(Let x e body) (Let x (rco_exp e) (rco_exp body))]
;; [(Prim op es)
;; ;todo: is there a standard library "unzip" function? use that here.
;; (define arg_results (for/list ([e es]) (rco_atom e)))
;; (define args
;; (for/list ([res arg_results])
;; (match res
;; [(atomic-arg e) e]
;; [(bind-arg x e) (Var x)])))
;; (define bindings
;; (for/list ([res arg_results] #:when (bind-arg? res))
;; (cons (bind-arg-x res) (bind-arg-e res))))
;; (multi-let bindings (Prim op args))]))
;;
;; (match p
;; [(Program info e) (Program info (rco_exp e))]))
;;
;; explicate-control : Lvar^mon -> Cvar
;; (define (explicate-control p)
;;
;; (define (explicate_tail e)
;; (match e
;; [(Var x) (Return (Var x))]
;; [(Int n) (Return (Int n))]
;; [(Let x rhs body) (explicate_assign rhs x (explicate_tail body))]
;; [(Prim op es)
;; (define x (gensym "x"))
;; (Seq (Assign (Var x) (Prim op es)) (Return (Var x)))]
;; [else (error "explicate_tail unhandled case" e)]))
;;
;; (define (explicate_assign e x cont)
;; (match e
;; [(Var y) (Seq (Assign (Var x) (Var y)) cont)]
;; [(Int n) (Seq (Assign (Var x) (Int n)) cont)]
;; [(Let y rhs body)
;; (explicate_assign rhs y (explicate_assign body x cont))]
;; [(Prim op es) (Seq (Assign (Var x) (Prim op es)) cont)]
;; [else (error "eplicate_assign unhandled case" e)]))
;;
;; (match p
;; [(Program info body)
;; (define labels `((start . ,(explicate_tail body))))
;; (CProgram info labels)]))
;; select-instructions : Cvar -> x86var
;; (define (select-instructions p)
;; (define (atm a)
;; (match a
;; [(Var x) (Var x)]
;; [(Int n) (Imm n)]))
;;
;; ; instruction syntax on pg 22
;;
;; (define (stmt s) ; returns a list of instructions
;; (match s
;; [(Assign (Var x) (Prim '+ (list (Var x) a2)))
;; (list
;; (Instr 'addq (list (atm a2) (Var x)))
;; )
;; ]
;; [(Assign (Var x) (Prim '+ (list a1 (Var x))))
;; (list
;; (Instr 'addq (list (atm a1) (Var x)))
;; )
;; ]
;; [(Assign (Var x) (Prim '+ (list a1 a2)))
;; (list
;; (Instr 'movq (list (atm a1) (Var x)))
;; (Instr 'addq (list (atm a2) (Var x)))
;; )
;; ]
;; [(Assign (Var x) (Prim '- (list (Var x) a2)))
;; (list
;; (Instr 'subq ((atm a2) (Var x)))
;; )
;; ]
;; [(Assign (Var x) (Prim '- (list a1 a2)))
;; (list
;; (Instr 'movq (list (atm a1) (Var x)))
;; (Instr 'subq (list (atm a2) (Var x)))
;; )
;; ]
;; [(Assign (Var x) (Prim '- (list (Var x))))
;; (list
;; (Instr 'negq ((Var x)))
;; )
;; ]
;; [(Assign (Var x) (Prim '- (list a)))
;; (list
;; (Instr 'movq (list (atm a) (Var x)))
;; (Instr 'negq (list (Var x)))
;; )
;; ]
;; [(Assign (Var x) (Prim 'read (list)))
;; (list
;; (Callq 'read_int 0)
;; (Instr 'movq (list (Reg 'rax) (Var x)))
;; )
;; ]
;; [(Assign (Var x) (Int n))
;; (list
;; (Instr 'movq (list (Imm n) (Var x)))
;; )
;; ]
;; [(Assign (Var x) (Var x))
;; (list)
;; ]
;; [(Assign (Var x) (Var y))
;; (list
;; (Instr 'movq (list (Var y) (Var x)))
;; )
;; ]
;; ))
;;
;; (define (tail t) ; returns a list of instructions
;; (match t
;; [(Return e)
;; (list
;; (Instr 'movq (list e (Reg 'rax)))
;; (Jmp 'conclusion)
;; )
;; ]
;; [(Seq s t)
;; (append (stmt s) (tail t))]))
;;
;; (define (make-block label/tail)
;; (define l (car label/tail))
;; (define t (cdr label/tail))
;; (cons l (Block '() (tail t))))
;;
;; (match p
;; [(CProgram info label/tails)
;; (X86Program info (map make-block label/tails))]))
;; (define (locs-from-arg arg)
;; (match arg
;; [(Imm n) (set)]
;; [(Var x) (set (Var x))]
;; [(Reg r) (set (Reg r))]))
;;
;; (define (locs-read instr)
;; (match instr
;; [(Instr 'addq (list a1 a2))
;; (set-union (locs-from-arg a1) (locs-from-arg a2))]
;; [(Instr 'subq (list a1 a2))
;; (set-union (locs-from-arg a1) (locs-from-arg a2))]
;; [(Instr 'negq (list a))
;; (locs-from-arg a)]
;; [(Instr 'movq (list a1 a2))
;; (locs-from-arg a1)]
;; [(Instr 'pushq (list a))
;; (locs-from-arg a)]
;; [(Instr 'popq (list a))
;; (set)]
;; [(Callq label arity)
;; (match arity
;; [0 (set)]
;; [1 (set (Reg 'rdi))]
;; [2 (set (Reg 'rdi) (Reg 'rsi))]
;; [3 (set (Reg 'rdi) (Reg 'rsi) (Reg 'rdx))]
;; [4 (set (Reg 'rdi) (Reg 'rsi) (Reg 'rdx) (Reg 'rcx))]
;; [5 (set (Reg 'rdi) (Reg 'rsi) (Reg 'rdx) (Reg 'rcx) (Reg 'r8))]
;; [else (set (Reg 'rdi) (Reg 'rsi) (Reg 'rdx) (Reg 'rcx) (Reg 'r8) (Reg 'r9))])]
;; [(Retq)
;; (set)]
;; [(Jmp label)
;; (set)]))
;;
;; (define (locs-write instr)
;; (match instr
;; [(Instr 'addq (list a1 a2))
;; (locs-from-arg a2)]
;; [(Instr 'subq (list a1 a2))
;; (locs-from-arg a2)]
;; [(Instr 'negq (list a))
;; (locs-from-arg a)]
;; [(Instr 'movq (list a1 a2))
;; (locs-from-arg a2)]
;; [(Instr 'pushq (list a))
;; (set)]
;; [(Instr 'popq (list a))
;; (locs-from-arg a)]
;; [(Callq label arity)
;; (set (Reg 'rax) (Reg 'rcx) (Reg 'rdx) (Reg 'rsi) (Reg 'rdi) (Reg 'r8) (Reg 'r9) (Reg 'r10) (Reg 'r11))]
;; [(Retq)
;; (set)]
;; [(Jmp label)
;; (set)]))
;; (define (build-interference p)
;; (define (fold-instr instr live-vars edges)
;; (define write-locations (locs-write instr))
;; (for*/fold ([e edges] #:result e)
;; ([v (in-immutable-set live-vars)]
;; [w (in-immutable-set write-locations)]
;; #:when (not (equal? v w)))
;; (match instr
;; [(Instr 'movq (list a b))
;; #:when (equal? a v)
;; e]
;; [else
;; (match (list v w)
;; [(list (Var x) (Var y))
;; (cons (list x y) e)]
;; [else e])])))
;;
;; (define (fold-block block edges)
;; (match block
;; [(cons label (Block info instrs))
;; (define live-sets (dict-ref info 'live-sets))
;; (foldl fold-instr edges instrs (rest live-sets))]))
;;
;; (match p
;; [(X86Program info blocks)
;; (define edges (foldl fold-block '() blocks))
;; (define conflict-graph (undirected-graph edges))
;; (for [(v (in-dict-keys (dict-ref info 'locals-types)))]
;; (add-vertex! conflict-graph v))
;; (define info2 (dict-set info 'conflicts conflict-graph))
;; ; (define info3 (dict-set info2 'conflicts-dbg (graphviz conflict-graph)))
;; (X86Program info2 blocks)]))
;; (define (allocate-registers p)
;;
;; ; returns (var->color, max-color) pair, where var->color is a dictionary from variables to colors
;; ; and max-color is the maximum color used
;; (define (color-graph conflicts vars)
;;
;; ; why do we need these?
;; ; (define non-alloc-regs (list (Reg 'rax) (Reg 'rsp) (Reg 'rbp) (Reg 'r11) (Reg 'r15)))
;; ; (define all-locations (append vars non-alloc-regs))
;;
;; (define var->handle (make-hash))
;; (define var->saturation (make-hash))
;; (define var->color (make-hash))
;; (define max-color 0)
;;
;; (define var-pqueue
;; (make-pqueue
;; (λ (v w)
;; (>
;; (set-count (dict-ref var->saturation v))
;; (set-count (dict-ref var->saturation w))))))
;;
;; (define (set-loc-color! var color)
;; (dict-set! var->color var color)
;; (for [(w (in-neighbors conflicts var))]
;; (cond
;; [(dict-has-key? var->handle w)
;; (pqueue-decrease-key! var-pqueue (dict-ref var->handle w))
;; (set-add! (dict-ref var->saturation w) color)])))
;;
;; (define (get-color var)
;; (define sat (dict-ref var->saturation var))
;; (define attempt-color 0)
;; (while (set-member? sat attempt-color)
;; (set! attempt-color (+ attempt-color 1)))
;; (set! max-color (max attempt-color max-color))
;; attempt-color)
;;
;; (for [(v vars)]
;; (dict-set! var->saturation v (mutable-set))
;; (dict-set! var->handle v (pqueue-push! var-pqueue v)))
;;
;; (while (not (= (pqueue-count var-pqueue) 0))
;; (define v (pqueue-pop! var-pqueue))
;; (dict-remove! var->handle v)
;; (set-loc-color! v (get-color v)))
;;
;; (values var->color max-color))
;;
;; (match p
;; [(X86Program info blocks)
;; (define vars (sequence->list (in-dict-keys (dict-ref info 'locals-types))))
;; (let-values ([(var->color max-color)
;; (color-graph
;; (dict-ref info 'conflicts)
;; vars)])
;;
;; (define (var-to-loc var)
;; (match (dict-ref var->color var)
;; [0 (Reg 'rcx)]
;; [1 (Reg 'rdx)]
;; [2 (Reg 'rsi)]
;; [3 (Reg 'rdi)]
;; [4 (Reg 'r8)]
;; [5 (Reg 'r9)]
;; [6 (Reg 'r10)]
;; [7 (Reg 'rbx)]
;; [8 (Reg 'r12)]
;; [9 (Reg 'r13)]
;; [10 (Reg 'r14)]
;; [else (Deref 'rbp (- (dict-ref var->color var) 10))]))
;;
;; (define locs (map var-to-loc vars))
;; (define callee-save-locs (filter (λ (x) (set-member? callee-save-regs x)) locs))
;; (define num-callee-save-locs (length callee-save-locs))
;;
;; (define (fix-deref loc)
;; (match loc
;; [(Deref 'rbp i)
;; (Deref 'rbp (* -8 (+ num-callee-save-locs i)))]
;; [else loc]))
;;
;; (define (map-arg arg)
;; (match arg
;; [(Var x) (fix-deref (var-to-loc x))]
;; [else arg]))
;;
;; (define (map-instr instr)
;; (match instr
;; [(Instr 'addq (list a1 a2))
;; (Instr 'addq (list (map-arg a1) (map-arg a2)))]
;; [(Instr 'subq (list a1 a2))
;; (Instr 'subq (list (map-arg a1) (map-arg a2)))]
;; [(Instr 'negq (list a))
;; (Instr 'negq (list (map-arg a)))]
;; [(Instr 'movq (list a1 a2))
;; (Instr 'movq (list (map-arg a1) (map-arg a2)))]
;; [(Instr 'pushq (list a))
;; (Instr 'pushq (list (map-arg a)))]
;; [(Instr 'popq (list a))
;; (Instr 'popq (list (map-arg a)))]
;; [(Callq label arity)
;; (Callq label arity)]
;; [(Retq)
;; (Retq)]
;; [(Jmp label)
;; (Jmp label)]))
;;
;; (define (map-block block)
;; (match block
;; [(cons label (Block info instrs))
;; (cons label (Block info (map map-instr instrs)))]))
;;
;; (define spill-space
;; (cond
;; [(< max-color 11) 0]
;; [else
;; (define num-stack-vars (- max-color 10))
;; (* num-stack-vars 8)]))
;;
;; (define info2 (cons (cons 'spill-space spill-space) info))
;; (define info3 (cons (cons 'used-callee callee-save-locs) info2))
;; (X86Program info3 (map map-block blocks)))
;; ]
;; ))
;; patch-instructions : x86var -> x86int
(define (patch-instructions p)
(define (instr1 i)
(match i
[(Instr op (list (Imm n) (Deref r m))) #:when (> n 65536)
(list
(Instr 'movq (list (Imm n) (Reg 'rax)))
(Instr op (list (Reg 'rax) (Deref r m))))
]
[(Instr op (list (Deref r m) (Imm n))) #:when (> n 65536)
(list
(Instr 'movq (list (Imm n) (Reg 'rax)))
(Instr op (list (Deref r m) (Reg 'rax))))
]
[else i]))
(define (instr2 i)
(match i
[(Instr op (list (Deref r1 n1) (Deref r2 n2)))
(list
(Instr 'movq (list (Deref r1 n1) (Reg 'rax)))
(Instr op (list (Reg 'rax) (Deref r2 n2))))]
[else i]))
(define (instr3 i)
(match i
[(Instr 'cmpq (list a (Imm n)))
(list
(Instr 'movq (list (Imm n) (Reg 'rax)))
(Instr 'cmpq (list a (Reg 'rax))))]
[(Instr 'movzbq (list byte-reg (Reg r))) i]
[(Instr 'movzbq (list byte-reg dest))
(list
(Instr 'movzbq (list byte-reg (Reg 'rax)))
(Instr 'movq (list (Reg 'rax) dest)))]
[else i]))
(define (block b)
(match b
[(cons label (Block info instrs))
(define instrs1 (flatten (map instr1 instrs)))
(define instrs2 (flatten (map instr2 instrs1)))
(define instrs3 (flatten (map instr3 instrs2)))
(cons label (Block info instrs3))]))
(match p
[(X86Program info blocks)
(X86Program info (map block blocks))]))
;; prelude-and-conclusion : x86int -> x86int
(define (prelude-and-conclusion p)
(match p
[(X86Program info blocks)
(define used-callee (dict-ref info 'used-callee))
(define callee-save-space (* 8 (length used-callee)))
(define spill-space (dict-ref info 'spill-space))
(define (align x) (if (= (remainder x 16) 0) x (+ x 8)))
(define all-space (- (align (+ spill-space callee-save-space)) callee-save-space))
(define blocks2
(dict-set blocks 'main
(Block '()
(append
(list
(Instr 'pushq (list (Reg 'rbp)))
(Instr 'movq (list (Reg 'rsp) (Reg 'rbp))) )
(map (λ (x) (Instr 'pushq (list x))) used-callee)
(list
(Instr 'subq (list (Imm all-space) (Reg 'rsp)))
(Jmp 'start))))))
(define blocks3
(dict-set blocks2 'conclusion
(Block '()
(append
(list (Instr 'addq (list (Imm all-space) (Reg 'rsp))))
(map (λ (x) (Instr 'popq (list x))) (reverse used-callee))
(list
(Instr 'popq (list (Reg 'rbp)))
(Retq))))))
(X86Program info blocks3)]))
;; Define the compiler passes to be used by interp-tests and the grader
;; Note that your compiler file (the file that defines the passes)
;; must be named "compiler.rkt"
(define compiler-passes
`(
;; Uncomment the following passes as you finish them.
("shrink", shrink, interp-Lwhile, type-check-Lwhile)
("uniquify" ,uniquify ,interp-Lwhile ,type-check-Lwhile)
("uncover-get",uncover-get,interp-Lwhile, type-check-Lwhile)
("remove complex opera*" ,remove-complex-operands ,interp-Lwhile ,type-check-Lwhile)
("explicate control" ,explicate-control ,interp-Cwhile ,type-check-Cwhile)
("instruction selection" ,select-instructions ,interp-pseudo-x86-1)
("uncover live", uncover-live, interp-pseudo-x86-1)
("build interference", build-interference, interp-pseudo-x86-1)
("allocate registers", allocate-registers ,interp-x86-1)
("patch instructions" ,patch-instructions ,interp-x86-1)
("prelude-and-conclusion" ,prelude-and-conclusion ,interp-x86-1)
))