-
Notifications
You must be signed in to change notification settings - Fork 122
/
cfg.go
166 lines (139 loc) · 3.68 KB
/
cfg.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
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
package compiler
type CFGraph struct {
Blocks []BasicBlock
}
type BasicBlock struct {
Code []Instr
JmpKind TyJmpKind
JmpTargets []int
JmpCond TyValueID
YieldValue TyValueID
}
type TyJmpKind uint8
const (
JmpUndef TyJmpKind = iota
JmpUncond
JmpEither
JmpTable
JmpReturn
)
func (g *CFGraph) ToInsSeq() []Instr {
out := make([]Instr, 0)
blockRelocs := make([]int, len(g.Blocks))
blockEnds := make([]int, len(g.Blocks))
for i, bb := range g.Blocks {
blockRelocs[i] = len(out)
out = append(out, bb.Code...)
out = append(out, Instr{}) // jmp placeholder
blockEnds[i] = len(out)
}
for i, bb := range g.Blocks {
jmpIns := &out[blockEnds[i]-1]
jmpIns.Immediates = make([]int64, len(bb.JmpTargets))
for j, target := range bb.JmpTargets {
jmpIns.Immediates[j] = int64(blockRelocs[target])
}
switch bb.JmpKind {
case JmpUndef:
panic("got JmpUndef")
case JmpUncond:
jmpIns.Op = "jmp"
jmpIns.Values = []TyValueID{bb.YieldValue}
case JmpEither:
jmpIns.Op = "jmp_either"
jmpIns.Values = []TyValueID{bb.JmpCond, bb.YieldValue}
case JmpTable:
jmpIns.Op = "jmp_table"
jmpIns.Values = []TyValueID{bb.JmpCond, bb.YieldValue}
case JmpReturn:
jmpIns.Op = "return"
if bb.YieldValue != 0 {
jmpIns.Values = []TyValueID{bb.YieldValue}
}
default:
panic("unreachable")
}
}
return out
}
func (c *SSAFunctionCompiler) NewCFGraph() *CFGraph {
g := &CFGraph{}
insLabels := make(map[int]int)
insLabels[0] = 0
nextLabel := 1
for i, ins := range c.Code {
switch ins.Op {
case "jmp", "jmp_if", "jmp_either", "jmp_table":
for _, target := range ins.Immediates {
if _, ok := insLabels[int(target)]; !ok {
insLabels[int(target)] = nextLabel
nextLabel++
}
}
if _, ok := insLabels[i+1]; !ok {
insLabels[i+1] = nextLabel
nextLabel++
}
case "return":
if _, ok := insLabels[i+1]; !ok {
insLabels[i+1] = nextLabel
nextLabel++
}
}
}
g.Blocks = make([]BasicBlock, nextLabel)
var currentBlock *BasicBlock
for i, ins := range c.Code {
if label, ok := insLabels[i]; ok {
if currentBlock != nil {
currentBlock.JmpKind = JmpUncond
currentBlock.JmpTargets = []int{label}
}
currentBlock = &g.Blocks[label]
}
switch ins.Op {
case "jmp":
currentBlock.JmpKind = JmpUncond
currentBlock.JmpTargets = []int{insLabels[int(ins.Immediates[0])]}
currentBlock.YieldValue = ins.Values[0]
currentBlock = nil
case "jmp_if":
currentBlock.JmpKind = JmpEither
currentBlock.JmpTargets = []int{insLabels[int(ins.Immediates[0])], insLabels[i+1]}
currentBlock.JmpCond = ins.Values[0]
currentBlock.YieldValue = ins.Values[1]
currentBlock = nil
case "jmp_either":
currentBlock.JmpKind = JmpEither
currentBlock.JmpTargets = []int{insLabels[int(ins.Immediates[0])], insLabels[int(ins.Immediates[1])]}
currentBlock.JmpCond = ins.Values[0]
currentBlock.YieldValue = ins.Values[1]
currentBlock = nil
case "jmp_table":
currentBlock.JmpKind = JmpTable
currentBlock.JmpTargets = make([]int, len(ins.Immediates))
for j, imm := range ins.Immediates {
currentBlock.JmpTargets[j] = insLabels[int(imm)]
}
currentBlock.JmpCond = ins.Values[0]
currentBlock.YieldValue = ins.Values[1]
currentBlock = nil
case "return":
currentBlock.JmpKind = JmpReturn
if len(ins.Values) > 0 {
currentBlock.YieldValue = ins.Values[0]
}
currentBlock = nil
default:
currentBlock.Code = append(currentBlock.Code, ins)
}
}
if label, ok := insLabels[len(c.Code)]; ok {
lastBlock := &g.Blocks[label]
if lastBlock.JmpKind != JmpUndef {
panic("last block should always have an undefined jump target")
}
lastBlock.JmpKind = JmpReturn
}
return g
}