-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathir.rkt
370 lines (352 loc) · 14.2 KB
/
ir.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
#lang racket
(require racket/trace)
(require "types.rkt")
(require "environ.rkt")
(require "lc2k.rkt")
(require "util.rkt")
(provide gen-ir ir-dest ir-sources register-num label-cleanup)
(define (reg-ref n)
(list 'register n))
(define reg0 (reg-ref 0))
(define/match (register-num r)
[((list 'register n)) n])
(define (gen-pred-code pred true-label false-label next-label)
;;
;; (gen-sequence conditions init-continue targets): return a code
;; list for a list of conditional expressions.
;;
;; conditions: list of conditional expressions.
;;
;; targets: a function which, given the following label in sequence,
;; or #f for end-of sequence, will return two values, the true
;; and false branch targets. For (and ...) these should be
;; (following false-label); for (or ...) they should be
;; (true-label following).
;;
(define (gen-sequence conditions targets)
(let-values ([(labels fol code)
(for/fold ([following false-label]
[cond-after #f]
[code-after empty])
([cond (reverse conditions)])
(let*-values
([(cond-label) (internal-label)]
[(true-l false-l) (targets cond-after)]
[(cond-code) (gen-pred-code cond
true-l
false-l
following)])
(values cond-label
cond-label
(append (list (list 'label cond-label))
cond-code
code-after))))])
(cdr code)))
(match pred
[(list 'primcall %eq? pa0 pa1)
;;(list (list 'cjump 'eq pa0 pa1 true-label false-label))
(if (eqv? false-label next-label)
(list (list 'beq pa0 pa1 true-label))
(list (list 'beq pa0 pa1 true-label)
(list 'beq reg0 reg0 false-label)))]
[(list 'primcall %zero? pa0)
(if (eqv? false-label next-label)
(list (list 'beq reg0 pa0 true-label))
(list (list 'beq reg0 pa0 true-label)
(list 'beq reg0 reg0 false-label)))]
[(list 'not cond)
(gen-pred-code cond false-label true-label next-label)]
[(list-rest 'and conditions)
(gen-sequence conditions
(lambda (following)
(values (or following true-label)
false-label)))]
[(list-rest 'or conditions)
(gen-sequence conditions
(lambda (following)
(values true-label
(or following false-label))))]))
;; simple stack frame handling for now
(define *frame-size* 16)
(define *debug-codegen* #f)
(define (gen-ir code-exp entry-label (fun-name "<anon>"))
(match-define (list 'code (list formals ...) body-exp)
code-exp)
(let* ([env (make-env (global-env))]
[post-prologue (internal-label)]
[insns empty]
[n-temp 0])
(define (emit! . args)
(if (list? (car args))
(apply emit! (car args))
(set! insns (cons args insns))))
(define (emit-all! . l)
(for-each (lambda (argl) (apply emit! argl)) l))
(define (alloc-temp)
(begin0
(list 'temp n-temp)
(set! n-temp (add1 n-temp))))
;;
;; cost-estimate: rough relative measure of the local cost of
;; function args, in order to evaluate the most expensive ones
;; first to minimize temp lifetimes.
;;
(define/match (cost-estimate exp)
[((? immed-const?)) 1]
[((? symbol?))
(match (env-lookup env exp)
;; needs to match all environment entry types
;; as defined in environ.rkt
[(list 'local name) 0]
[(list 'immediate val) 1]
[(struct proc _) 1])]
[((list 'primcall _ ...)) 2]
[((list 'call _ ...)) 6]
[((list 'if _ ...)) 3])
(define (no-code? exp)
(and (symbol? exp)
(tagged-list? 'local (env-lookup env exp))))
(define (cg exp dd cd next-label)
(define (gen-args args)
(let ([arg-temp-alist
(for/list ([arg (sort args >
#:key cost-estimate
#:cache-keys? #t)])
(cons arg (cg arg #f #f #f)))])
(map (compose cdr (curryr assq arg-temp-alist))
args)))
(define (gen-children cl cr body-label)
;; XXX: need to improve this
(cond
[(or (no-code? cl) (no-code? cr))
(list (cg cl #f body-label body-label)
(cg cr #f body-label body-label))]
[else
(let* ([l-label (internal-label)]
[r-reg (cg cr #f l-label l-label)])
(emit! 'label l-label)
(list (cg cl #f body-label body-label)
r-reg))]))
(define (gen-code)
(match exp
;; immediate constant reference
[(? immed-const?)
(if (and dd (not (eq? cd 'return)))
(begin
(emit! 'lw 0 dd (const-label exp)
(format "; ~a = ~a" dd exp))
dd)
(list 'constant exp exp))]
;; register reference
[(list 'register n)
exp]
;; symbol
[(? symbol?)
(let* ([referent (env-lookup env exp)])
(match referent
;; constant, from the constant pool
[(list 'immediate val)
(if (and dd (not (eq? cd 'return)))
(begin
(emit! 'lw 0 dd (const-label val)
(format "; ~a = ~a" dd exp))
dd)
(list 'constant val exp))]
;; register variable (formal param)
[(list 'local name)
referent]
;; procedure; take a pointer to it
[(struct proc _)
(let* ([reg (or dd (alloc-temp))])
(emit! 'lw 0 reg (proc-ptr-label referent)
(format "; ~a = &~a" reg (proc-name referent)))
reg)]))]
;; unary primitive
[(list 'primcall prim arg)
(let* ([op-label (internal-label)]
[dest (or dd (alloc-temp))]
[arg-reg (cg arg #f op-label op-label)])
(emit! 'label op-label)
(match prim
['%bnot (emit! 'nand arg-reg arg-reg dest)])
dest)]
;; binary primitive
[(list 'primcall prim arg1 arg2)
(let* ([op-label (internal-label)]
[child-regs (gen-children arg1 arg2 op-label)]
[t1 (car child-regs)]
[t2 (cadr child-regs)]
[dest (or dd (alloc-temp))])
(emit! 'label op-label)
(match prim
['%add (emit! 'add t1 t2 dest
(format "~a = ~a + ~a" dest t1 t2))]
['%nand (emit! 'nand t1 t2 dest
(format "~a = ~a NAND ~a" dest t1 t2))]
['%band (emit! 'nand t1 t2 dest)
(emit! 'nand dest dest dest
(format "~a = ~a & ~a" dest t1 t2))])
dest)]
;; function call
[(list 'call (? symbol? sym) args ...)
;; marshal arguments
(let* ([next-label (internal-label)]
[arg-temps (gen-args args)]
[dest-reg (or dd (alloc-temp))]
[target-entry (env-lookup env sym)]
[target-label (and (proc? target-entry)
(proc-label target-entry))])
(if (eq? cd 'return)
(begin
(apply emit!
'tail-call
(if (and target-label
(equal? entry-label target-label))
;; self tail call, skip epilogue and prologue
post-prologue
;; tail call elsewhere
target-entry)
'return-addr arg-temps)
'tail-call)
(begin
(match target-entry
[(struct proc _)
(apply emit! 'labelcall dest-reg target-entry arg-temps)]
[(list 'local var-name)
(apply emit! 'proc-call dest-reg target-entry arg-temps)])
dest-reg)))]
;; conditional
[(list 'if if-pred if-then if-else)
(let* ([true-label (internal-label)]
[false-label (internal-label)]
[branch-label (internal-label)]
[dest (or dd (alloc-temp))])
(for ([stmt (gen-pred-code if-pred
true-label false-label false-label)])
(match stmt
[(list 'label (? string?))
(apply emit! stmt)]
[(list 'beq arg0 arg1 target-label)
(let* ([branch-label (internal-label)]
[registers (gen-children arg0 arg1 branch-label)])
(emit! 'label branch-label)
(emit! 'beq (car registers) (cadr registers) target-label))]))
(emit! 'label false-label)
(cg if-else dest cd true-label)
(emit! 'label true-label)
(cg if-then dest cd next-label))]))
(let ([result (gen-code)])
(cond
[(tagged-list? 'if exp) #f]
;; generated a tail call
[(eq? result 'tail-call) #f]
;; otherwise shouldn't generate a control transfer
[(not cd) #f]
;; expression in tail position, return
[(eq? cd 'return)
(emit! 'return result 'return-addr)]
;; continue directly to next label (or whatever)
;; when next-label is #f or the same as cd
[(or (not next-label) (eq? cd next-label)) #t]
;; jump to next label
[else (emit! 'beq 0 0 cd)])
result))
(when (tracing? 'cg)
(trace cg))
(emit! 'noop (format "; [~a] ~v" fun-name code-exp))
;; function entry point
(emit! 'label entry-label)
(emit! 'prologue)
;; branch target for self tail calls
(emit! 'label post-prologue)
;; tell the register allocator where return-addr is
(emit! 'bind 'return-addr 6 '(frame 0))
(for ([formal formals]
[reg (in-range 1 4)])
(let ([var-ref (list 'local formal)])
(env-define env formal var-ref)
(emit! 'bind var-ref reg #f)))
(let ([temp (alloc-temp)])
(cg body-exp temp 'return #f))
(reverse insns)))
(define (ir-dest stmt)
(let* ([tag (car stmt)]
[dest
(case tag
[(add nand) (fourth stmt)]
[(lw) (third stmt)]
[(labelcall proc-call) (second stmt)]
[(bind) (second stmt)]
[(sw beq noop label prologue return tail-call) #f]
[else (error "unhandled tag: " tag)])])
(if (or (eq? dest 0) (equal? dest '(register 0)))
#f
dest)))
(define (ir-sources stmt)
(let ([tag (car stmt)])
(filter-not (lambda (e)
(or (eq? 0 e)
(tagged-list? 'constant e)
(equal? '(register 0) e)))
(case tag
[(add nand) (list (second stmt) (third stmt))]
[(lw) (list (second stmt))]
[(sw) (list (second stmt) (third stmt))]
[(labelcall) (cdddr stmt)]
[(proc-call) (cddr stmt)]
[(tail-call)
(match stmt
[(list 'tail-call (and target (list 'local _)) _ ...)
(cdr stmt)]
[else
(cddr stmt)])]
[(beq) (list (second stmt) (third stmt))]
[(return) (list (second stmt) (third stmt))]
[(noop label bind prologue) empty]
[else (error "unhandled tag: " tag)]))))
(define (internal-label? name)
(and (string? name)
(eqv? (string-ref name 0) #\I)))
;; Remove internal labels which are not referenced, and combine
;; sequences of label declarations, rewriting references.
(define (label-cleanup sasm)
(let*-values ([(_ canonical used)
(for/fold ([cur-label #f]
;; label => canonical label name
[int-labels empty]
[labels-used (set)])
([stmt sasm])
(match stmt
;; internal label
[(list 'label (? internal-label? name))
(if cur-label
;; later label, to alias
(values cur-label
(dict-set int-labels name cur-label)
labels-used)
;; first label in a group
(values name
(dict-set int-labels name name)
labels-used))]
;; branch to internal label
[(list 'beq a b (? internal-label? target))
(values #f int-labels (set-add labels-used target))]
;; self-tail-call
[(list 'tail-call (? string? target) _ ...)
(values #f int-labels (set-add labels-used target))]
;; non-code-generating directive
[(list 'bind args ...)
(values cur-label int-labels labels-used)]
;; anything else
[else
(values #f int-labels labels-used)]))]
[(to-emit)
(list->set (map (curry dict-ref canonical) (set->list used)))])
(filter (lambda (stmt)
(match stmt
[(list 'label name)
(or (not (internal-label? name))
(set-member? to-emit name))]
[else
#t]))
sasm)))