-
Notifications
You must be signed in to change notification settings - Fork 2
/
xform.go
71 lines (58 loc) · 1.11 KB
/
xform.go
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
package xform
import (
"github.com/rj45/nanogo/ir"
)
//go:generate go run github.com/dmarkham/enumer -type=Pass
type Pass int
const (
Elaboration Pass = iota
Simplification
Lowering
Legalize
CleanUp
)
type xformerDef struct {
tags []Tag
fn func(*ir.Value) int
}
var passes [][]xformerDef
func addToPass(pass Pass, fn func(*ir.Value) int, tags ...Tag) int {
for int(pass) >= len(passes) {
passes = append(passes, nil)
}
passes[pass] = append(passes[pass], xformerDef{tags: tags, fn: fn})
return 0
}
func Transform(pass Pass, fn *ir.Func) {
var xforms []func(*ir.Value) int
next:
for _, xf := range passes[pass] {
for _, tag := range xf.tags {
if !activeTags[tag] {
continue next
}
}
xforms = append(xforms, xf.fn)
}
changes := 1
tries := 0
nextchange:
for changes > 0 {
changes = 0
tries++
if tries > 1000 {
panic("too many tries")
}
for _, blk := range fn.Blocks() {
for i := 0; i < blk.NumInstrs(); i++ {
instr := blk.Instr(i)
for _, xform := range xforms {
changes += xform(instr)
if changes > 0 {
continue nextchange
}
}
}
}
}
}