-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.scm
588 lines (445 loc) · 13.1 KB
/
tree.scm
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
581
582
583
584
585
(declare (unit tree)
(uses extras utils))
(use srfi-1)
(use srfi-13)
(use matchable)
(import-for-syntax matchable)
(include "struct-syntax")
(define-syntax assertp
(syntax-rules ()
((assertp pred x)
(assert (pred x) "invalid type"))))
;; Selection language
(define-struct tree-module (functions))
(define-struct tree-function (name args entry module))
(define-struct tree-block (name head tail pred succ function))
(define-struct tree-instr (op mode in1 in2 in3 next prev block attrs))
(define-struct tree-temp (name))
(define-struct tree-label (name))
(define-struct tree-constant (size value))
(define-struct tree-data (size name))
(define *tree-tag*
'(function block instr temp label const))
(define *tree-type*
'(i8 i16 i32 i64))
(define tree-make-function make-tree-function)
(define tree-make-block make-tree-block)
(define tree-make-temp make-tree-temp)
(define tree-make-label make-tree-label)
(define tree-make-constant make-tree-constant)
;; constant pool
(define *tree-i1-pool* '())
(define *tree-i8-pool* '())
(define *tree-i16-pool* '())
(define *tree-i32-pool* '())
(define *tree-i64-pool* '())
(define (tree-constant-get type value)
(cond
((eq? type 'i1)
(cond
((assq value *tree-i1-pool*) => cdr)
(else
(let ((const (tree-make-constant type value)))
(set! *tree-i1-pool* (cons (cons value const) *tree-i1-pool*))
const))))
((eq? type 'i8)
(cond
((assq value *tree-i8-pool*) => cdr)
(else
(let ((const (tree-make-constant type value)))
(set! *tree-i8-pool* (cons (cons value const) *tree-i8-pool*))
const))))
((eq? type 'i16)
(cond
((assq value *tree-i16-pool*) => cdr)
(else
(let ((const (tree-make-constant type value)))
(set! *tree-i16-pool* (cons (cons value const) *tree-i16-pool*))
const))))
((eq? type 'i32)
(cond
((assq value *tree-i32-pool*) => cdr)
(else
(let ((const (tree-make-constant type value)))
(set! *tree-i32-pool* (cons (cons value const) *tree-i32-pool*))
const))))
((eq? type 'i64)
(cond
((assq value *tree-i64-pool*) => cdr)
(else
(let ((const (tree-make-constant type value)))
(set! *tree-i64-pool* (cons (cons value const) *tree-i64-pool*))
const))))))
;; temp pool
(define *tree-temp-pool* '())
(define (tree-temp-get name)
(cond
((assq name *tree-temp-pool*) => cdr)
(else
(let ((temp (tree-make-temp name)))
(set! *tree-temp-pool* (cons (cons name temp) *tree-temp-pool*))
temp))))
;; label pool
(define *tree-label-pool* '())
(define (tree-label-get name)
(cond
((assq name *tree-label-pool*) => cdr)
(else
(let ((label (tree-make-label name)))
(set! *tree-label-pool* (cons (cons name label) *tree-label-pool*))
label))))
;; base constructor
(define-syntax tree-make-instr
(lambda (e r c)
(define defaults '(op mode in1 in2 in3 next prev block attrs))
(match e
(('tree-make-instr pair* ...)
`(make-tree-instr ,@(map (lambda (field)
(cond
((assq field pair*)
=> cadr)
(else ''())))
defaults))))))
;; constructors for instructions
(define (tree-make-assign name value)
(let ((node
(tree-make-instr
(op 'assign)
(in1 name)
(in2 value))))
node))
(define (tree-make-binop op mode x y)
(let ((node
(tree-make-instr
(op op)
(mode mode)
(in1 x)
(in2 y))))
node))
(define (tree-make-load mode addr)
(let ((node
(tree-make-instr
(op 'load)
(mode mode)
(in1 addr))))
node))
(define (tree-make-store mode value addr)
(let ((node
(tree-make-instr
(op 'store)
(mode mode)
(in1 value)
(in2 addr))))
node))
(define (tree-make-call callconv target args)
(let ((node
(tree-make-instr
(op 'call)
(in1 target)
(in2 args)
(attrs `((callconv . ,callconv))))))
node))
(define (tree-make-return value)
(let ((node
(tree-make-instr
(op 'return)
(in1 value))))
node))
(define (tree-make-br label)
(let ((node
(tree-make-instr
(op 'br)
(in1 label))))
node))
(define (tree-make-brc cond labelx labely)
(let ((node
(tree-make-instr
(op 'brc)
(in1 cond)
(in2 labelx)
(in3 labely))))
node))
(define (tree-make-cmp test x y)
(let ((node
(tree-make-instr
(op 'cmp)
(mode 'i64)
(in1 test)
(in2 x)
(in3 y))))
node))
;; node attributes
(define (tree-instr-attr x attr)
(cond
((assq attr (tree-instr-attrs x))
=> cdr)
(else #f)))
(define (tree-instr-attr-set! x attr value)
(cond
((assq attr (tree-instr-attrs x))
=> (lambda (cell)
(set-cdr! cell value)))
(else
(tree-instr-attrs-set! x (cons (cons attr value) (tree-instr-attrs x))))))
;; builders
(define tree-build-block tree-make-block)
(define tree-build-load tree-make-load)
(define tree-build-store tree-make-store)
(define tree-build-call tree-make-call)
(define tree-build-return tree-make-return)
(define tree-build-cmp tree-make-cmp)
(define tree-build-br tree-make-br)
(define tree-build-brc tree-make-brc)
(define tree-build-assign tree-make-assign)
(define (tree-build-add mode x y)
(tree-make-binop 'add mode x y))
(define (tree-build-sub mode x y)
(tree-make-binop 'sub mode x y))
(define (tree-build-mul mode x y)
(tree-make-binop 'mul mode x y))
(define (tree-build-and mode x y)
(tree-make-binop 'and mode x y))
(define (tree-build-ior mode x y)
(tree-make-binop 'ior mode x y))
(define (tree-build-xor mode x y)
(tree-make-binop 'xor mode x y))
(define (tree-build-shr mode x y)
(tree-make-binop 'shr mode x y))
(define (tree-build-shl mode x y)
(tree-make-binop 'shl mode x y))
;; OPS
;; basic
(define (tree-instr-print x port)
(fprintf port " ~a\n" (tree-instr-format x)))
;; module
(define (tree-make-module)
(make-tree-module '()))
(define (tree-module-add-function! mod fun)
(tree-module-functions-set! mod (cons fun (tree-module-functions mod))))
(define (tree-module-print mod port)
(for-each
(lambda (fun)
(tree-function-print fun port)
(fprintf port "\n"))
(tree-module-functions mod)))
;; function
(define (tree-function-add-arg! x arg)
(assertp tree-function? x)
(tree-function-args-set! x (cons arg (tree-function-args x))))
(define (tree-function-print x port)
(define (format-arg-list args)
(string-join
(map (lambda (arg)
(format "~a" (tree-instr-format arg)))
args)
" "))
(define (print-declaration name args port)
(fprintf port "(~a (~a)\n" name (format-arg-list args)))
(define (print-body x port)
(tree-for-each-block
(lambda (block)
(tree-block-print block port))
x)
(fprintf port ")"))
(print-declaration
(tree-function-name x)
(tree-function-args x)
port)
(print-body x port))
;; block
(define (tree-block-add-succ! x block)
(assertp tree-block? x)
(tree-block-succ-set! x (cons block (tree-block-succ x))))
(define (tree-block-add-statement! x stm)
(cond
((and (null? (tree-block-head x)) (null? (tree-block-tail x)))
(tree-block-head-set! x stm)
(tree-block-tail-set! x stm))
(else
(tree-instr-next-set! (tree-block-tail x) stm)
(tree-instr-prev-set! stm (tree-block-tail x))
(tree-block-tail-set! x stm)))
x)
(define (tree-block-print x port)
(fprintf port " (~a\n" (tree-block-name x))
(tree-for-each-statement
(lambda (instr)
(tree-instr-print instr port))
x))
;; binops
(define (tree-binop-left x)
(assertp tree-instr? x)
(tree-instr-in1 x))
(define (tree-binop-left-set! x v)
(assertp tree-instr? x)
(tree-instr-in1-set! x v))
(define (tree-binop-right x)
(assertp tree-instr? x)
(tree-instr-in2 x))
(define (tree-binop-right-set! x v)
(assertp tree-instr? x)
(tree-instr-in2-set! x v))
(define (tree-binop-format node)
(format "(~a ~a ~a ~a)"
(tree-instr-op node)
(tree-instr-mode node)
(tree-instr-format (tree-binop-left node))
(tree-instr-format (tree-binop-right node))))
;; call
(define (tree-call-target x)
(tree-instr-in1 x))
(define (tree-call-target-set! x v)
(tree-instr-in1-set! x v))
(define (tree-call-args x)
(tree-instr-in2 x))
(define (tree-call-args-set! x v)
(tree-instr-in2 x v))
(define (tree-call-format node)
(sprintf "(call ~a (~a))"
(tree-instr-format (tree-call-target node))
(string-join
(map (lambda (arg)
(tree-instr-format arg))
(tree-call-args node))
" ")))
;; ret
(define (tree-ret-value x)
(tree-instr-in1 x))
(define (tree-ret-value-set! x)
(tree-instr-in1 x))
(define (tree-ret-format node)
(format "(ret ~a)"
(tree-instr-format (tree-ret-value node))))
;; load
(define (tree-load-addr x)
(tree-instr-in1 x))
(define (tree-load-addr-set! x v)
(tree-instr-in1-set! x v))
(define (tree-load-format node)
(format "(load ~a ~a)"
(tree-instr-mode node)
(tree-instr-format (tree-load-addr node))))
;; store
(define (tree-store-value x)
(tree-instr-in1 x))
(define (tree-store-value-set! x v)
(tree-instr-in1-set! x v))
(define (tree-store-addr x)
(tree-instr-in2 x))
(define (tree-store-addr-set! x v)
(tree-instr-in2 x v))
(define (tree-store-format node)
(format "(store ~a ~a ~a)"
(tree-instr-mode node)
(tree-instr-format (tree-store-value node))
(tree-instr-format (tree-store-addr node))))
;; conditional branch
(define (tree-brc-cond x)
(tree-instr-in1 x))
(define (tree-brc-cond-set! x v)
(tree-instr-in1-set! x v))
(define (tree-brc-labelx x)
(tree-instr-in2 x))
(define (tree-brc-labelx-set! x v)
(tree-instr-in2-set! x v))
(define (tree-brc-labely x)
(tree-instr-in3 x))
(define (tree-brc-labely-set! x v)
(tree-instr-in3-set! x v))
(define (tree-brc-format node)
(format "(brc ~a ~a ~a)"
(tree-instr-format (tree-brc-cond node))
(tree-instr-format (tree-brc-labelx node))
(tree-instr-format (tree-brc-labely node))))
;; unconditional branch
(define (tree-br-label x)
(tree-instr-in1 x))
(define (tree-br-label-set! x v)
(tree-instr-in1-set! x v))
(define (tree-br-format node)
(format "(br ~a)"
(tree-instr-format (tree-br-label x))))
;; cmp
(define (tree-cmp-test x)
(tree-instr-in1 x))
(define (tree-cmp-test-set! x test)
(tree-instr-in1-set! x test))
(define (tree-cmp-x x)
(tree-instr-in2 x))
(define (tree-cmp-x-set! x v)
(tree-instr-in2-set! x v))
(define (tree-cmp-y x)
(tree-instr-in3 x))
(define (tree-cmp-y-set! x v)
(tree-instr-in3-set! x v))
(define (tree-cmp-format x)
(format "(cmp ~a ~a ~a ~a)"
(tree-instr-mode x)
(tree-cmp-test x)
(tree-instr-format (tree-cmp-x x))
(tree-instr-format (tree-cmp-y x))))
;; assign
(define (tree-assign-name x)
(tree-instr-in1 x))
(define (tree-assign-value x)
(tree-instr-in2 x))
(define (tree-assign-format x)
(format "(assign ~a ~a)"
(tree-assign-name x)
(tree-instr-format (tree-assign-value x))))
;; function traversal
(define (tree-for-each-function f mod)
(let ((funcs (tree-module-functions f)))
(for-each f funcs)))
;; block traversal
(define (tree-for-each-block f fun)
(define (visit-block block f)
(let ((succ (tree-block-succ block)))
(f block)
(for-each (lambda (succ)
(visit-block succ f))
succ)))
(visit-block (tree-function-entry fun) f))
(define (tree-for-each-block-succ f block)
(for-each f (tree-block-succ block)))
(define (tree-for-each-block-pred f block)
(f (tree-block-pred block)))
;; instruction traversal operations
(define (tree-for-each-statement f block)
(let ((head (tree-block-head block)))
(let walk ((x head))
(cond
((not (null? x))
(f x)
(walk (tree-instr-next x)))))))
;; formatting
(define (tree-instr-format x)
(cond
;; atoms
((tree-constant? x)
(format "(~a ~a)" (tree-constant-size x) (tree-constant-value x)))
((tree-temp? x)
(format "$~a" (tree-temp-name x)))
((tree-label? x)
(format "@~a" (tree-label-name x)))
((tree-instr? x)
(case (tree-instr-op x)
((add sub mul and ior shl shr)
(tree-binop-format x))
((call)
(tree-call-format x))
((return)
(tree-ret-format x))
((br)
(tree-br-format x))
((brc)
(tree-brc-format x))
((load)
(tree-load-format x))
((store)
(tree-store-format x))
((cmp)
(tree-cmp-format x))
((assign)
(tree-assign-format x))))
(else (assert-not-reached))))