Debian Codegraph @

pass/cfg_test.go

raw symbols SCIP
1 package pass
2
3 import (
4 "reflect"
5 "sort"
6 "testing"
7
8 "github.com/mmcloughlin/avo/ir"
9 "github.com/mmcloughlin/avo/operand"
10 )
11
12 func TestLabelTarget(t *testing.T) {
13 expect := map[ir.Label]*ir.Instruction{
14 "lblA": {Opcode: "A"},
15 "lblB": {Opcode: "B"},
16 }
17
18 f := ir.NewFunction("happypath")
19 for lbl, i := range expect {
20 f.AddLabel(lbl)
21 f.AddComment("comments should be ignored")
22 f.AddInstruction(i)
23 f.AddInstruction(&ir.Instruction{Opcode: "IDK"})
24 }
25
26 if err := LabelTarget(f); err != nil {
27 t.Fatal(err)
28 }
29
30 if !reflect.DeepEqual(expect, f.LabelTarget) {
31 t.Fatalf("incorrect LabelTarget value\ngot=%#v\nexpext=%#v\n", f.LabelTarget, expect)
32 }
33 }
34
35 func TestLabelTargetDuplicate(t *testing.T) {
36 f := ir.NewFunction("dupelabel")
37 f.AddLabel(ir.Label("lblA"))
38 f.AddInstruction(&ir.Instruction{Opcode: "A"})
39 f.AddLabel(ir.Label("lblA"))
40 f.AddInstruction(&ir.Instruction{Opcode: "A"})
41
42 err := LabelTarget(f)
43
44 if err == nil || err.Error() != "duplicate label \"lblA\"" {
45 t.Fatalf("expected error on duplcate label; got %v", err)
46 }
47 }
48
49 func TestLabelTargetEndsWithLabel(t *testing.T) {
50 f := ir.NewFunction("endswithlabel")
51 f.AddInstruction(&ir.Instruction{Opcode: "A"})
52 f.AddLabel(ir.Label("theend"))
53
54 err := LabelTarget(f)
55
56 if err == nil || err.Error() != "function ends with label" {
57 t.Fatalf("expected error when function ends with label; got %v", err)
58 }
59 }
60
61 func TestLabelTargetConsecutiveLabels(t *testing.T) {
62 i := &ir.Instruction{Opcode: "A"}
63
64 f := ir.NewFunction("consecutivelabels")
65 f.AddLabel(ir.Label("lblA"))
66 f.AddLabel(ir.Label("lblB"))
67 f.AddInstruction(i)
68
69 expect := map[ir.Label]*ir.Instruction{
70 "lblA": i,
71 "lblB": i,
72 }
73
74 if err := LabelTarget(f); err != nil {
75 t.Fatal(err)
76 }
77
78 if !reflect.DeepEqual(expect, f.LabelTarget) {
79 t.Fatalf("incorrect LabelTarget value\ngot=%#v\nexpext=%#v\n", f.LabelTarget, expect)
80 }
81 }
82
83 func TestCFGSingleBasicBlock(t *testing.T) {
84 f := ir.NewFunction("simple")
85 f.AddInstruction(&ir.Instruction{Opcode: "A"})
86 f.AddInstruction(&ir.Instruction{Opcode: "B"})
87 f.AddInstruction(Terminal("RET"))
88
89 if err := ComputeCFG(t, f); err != nil {
90 t.Fatal(err)
91 }
92
93 AssertSuccessors(t, f, map[string][]string{
94 "A": {"B"},
95 "B": {"RET"},
96 "RET": {},
97 })
98
99 AssertPredecessors(t, f, map[string][]string{
100 "A": {},
101 "B": {"A"},
102 "RET": {"B"},
103 })
104 }
105
106 func TestCFGCondBranch(t *testing.T) {
107 f := ir.NewFunction("condbranch")
108 f.AddInstruction(&ir.Instruction{Opcode: "A"})
109 f.AddLabel(ir.Label("lblB"))
110 f.AddInstruction(&ir.Instruction{Opcode: "B"})
111 f.AddInstruction(&ir.Instruction{Opcode: "C"})
112 f.AddInstruction(CondBranch("J", "lblB"))
113 f.AddInstruction(Terminal("RET"))
114
115 if err := ComputeCFG(t, f); err != nil {
116 t.Fatal(err)
117 }
118
119 AssertSuccessors(t, f, map[string][]string{
120 "A": {"B"},
121 "B": {"C"},
122 "C": {"J"},
123 "J": {"B", "RET"},
124 "RET": {},
125 })
126 }
127
128 func TestCFGUncondBranch(t *testing.T) {
129 f := ir.NewFunction("uncondbranch")
130 f.AddInstruction(&ir.Instruction{Opcode: "A"})
131 f.AddLabel(ir.Label("lblB"))
132 f.AddInstruction(&ir.Instruction{Opcode: "B"})
133 f.AddInstruction(UncondBranch("JMP", "lblB"))
134
135 if err := ComputeCFG(t, f); err != nil {
136 t.Fatal(err)
137 }
138
139 AssertSuccessors(t, f, map[string][]string{
140 "A": {"B"},
141 "B": {"JMP"},
142 "JMP": {"B"},
143 })
144 }
145
146 func TestCFGJumpForward(t *testing.T) {
147 f := ir.NewFunction("forward")
148 f.AddInstruction(&ir.Instruction{Opcode: "A"})
149 f.AddInstruction(CondBranch("J", "done"))
150 f.AddInstruction(&ir.Instruction{Opcode: "B"})
151 f.AddLabel(ir.Label("done"))
152 f.AddInstruction(Terminal("RET"))
153
154 if err := ComputeCFG(t, f); err != nil {
155 t.Fatal(err)
156 }
157
158 AssertSuccessors(t, f, map[string][]string{
159 "A": {"J"},
160 "J": {"B", "RET"},
161 "B": {"RET"},
162 "RET": {},
163 })
164 }
165
166 func TestCFGMultiReturn(t *testing.T) {
167 f := ir.NewFunction("multireturn")
168 f.AddInstruction(&ir.Instruction{Opcode: "A"})
169 f.AddInstruction(CondBranch("J", "fork"))
170 f.AddInstruction(&ir.Instruction{Opcode: "B"})
171 f.AddInstruction(Terminal("RET1"))
172 f.AddLabel(ir.Label("fork"))
173 f.AddInstruction(&ir.Instruction{Opcode: "C"})
174 f.AddInstruction(Terminal("RET2"))
175
176 if err := ComputeCFG(t, f); err != nil {
177 t.Fatal(err)
178 }
179
180 AssertSuccessors(t, f, map[string][]string{
181 "A": {"J"},
182 "J": {"B", "C"},
183 "B": {"RET1"},
184 "RET1": {},
185 "C": {"RET2"},
186 "RET2": {},
187 })
188 }
189
190 func TestCFGShortLoop(t *testing.T) {
191 f := ir.NewFunction("shortloop")
192 f.AddLabel(ir.Label("cycle"))
193 f.AddInstruction(UncondBranch("JMP", "cycle"))
194
195 if err := ComputeCFG(t, f); err != nil {
196 t.Fatal(err)
197 }
198
199 AssertSuccessors(t, f, map[string][]string{
200 "JMP": {"JMP"},
201 })
202 }
203
204 func TestCFGUndefinedLabel(t *testing.T) {
205 f := ir.NewFunction("undeflabel")
206 f.AddInstruction(&ir.Instruction{Opcode: "A"})
207 f.AddInstruction(CondBranch("J", "undef"))
208
209 err := ComputeCFG(t, f)
210
211 if err == nil {
212 t.Fatal("expect error on undefined label")
213 }
214 }
215
216 func TestCFGMissingLabel(t *testing.T) {
217 f := ir.NewFunction("missinglabel")
218 f.AddInstruction(&ir.Instruction{Opcode: "A"})
219 f.AddInstruction(&ir.Instruction{Opcode: "J", IsBranch: true}) // no label operand
220 err := ComputeCFG(t, f)
221 if err == nil {
222 t.Fatal("expect error on missing label")
223 }
224 }
225
226 // Terminal builds a terminal instruction.
227 func Terminal(opcode string) *ir.Instruction {
228 return &ir.Instruction{Opcode: opcode, IsTerminal: true}
229 }
230
231 // CondBranch builds a conditional branch instruction to the given label.
232 func CondBranch(opcode, lbl string) *ir.Instruction {
233 return &ir.Instruction{
234 Opcode: opcode,
235 Operands: []operand.Op{operand.LabelRef(lbl)},
236 IsBranch: true,
237 IsConditional: true,
238 }
239 }
240
241 // UncondBranch builds an unconditional branch instruction to the given label.
242 func UncondBranch(opcode, lbl string) *ir.Instruction {
243 return &ir.Instruction{
244 Opcode: opcode,
245 Operands: []operand.Op{operand.LabelRef(lbl)},
246 IsBranch: true,
247 IsConditional: false,
248 }
249 }
250
251 func ComputeCFG(t *testing.T, f *ir.Function) error {
252 t.Helper()
253 if err := LabelTarget(f); err != nil {
254 t.Fatal(err)
255 }
256 return CFG(f)
257 }
258
259 func AssertSuccessors(t *testing.T, f *ir.Function, expect map[string][]string) {
260 t.Helper()
261 AssertEqual(t, "successors", OpcodeSuccessorGraph(f), expect)
262 }
263
264 func AssertPredecessors(t *testing.T, f *ir.Function, expect map[string][]string) {
265 t.Helper()
266 AssertEqual(t, "predecessors", OpcodePredecessorGraph(f), expect)
267 }
268
269 func AssertEqual(t *testing.T, what string, got, expect any) {
270 t.Helper()
271 t.Logf("%s=%#v\n", what, got)
272 if reflect.DeepEqual(expect, got) {
273 return
274 }
275 t.Fatalf("bad %s; expected=%#v", what, expect)
276 }
277
278 // OpcodeSuccessorGraph builds a map from opcode name to successor opcode names.
279 func OpcodeSuccessorGraph(f *ir.Function) map[string][]string {
280 return OpcodeGraph(f, func(i *ir.Instruction) []*ir.Instruction { return i.Succ })
281 }
282
283 // OpcodePredecessorGraph builds a map from opcode name to predecessor opcode names.
284 func OpcodePredecessorGraph(f *ir.Function) map[string][]string {
285 return OpcodeGraph(f, func(i *ir.Instruction) []*ir.Instruction { return i.Pred })
286 }
287
288 // OpcodeGraph builds a map from opcode name to neighboring opcode names. Each list of neighbors is sorted.
289 func OpcodeGraph(f *ir.Function, neighbors func(*ir.Instruction) []*ir.Instruction) map[string][]string {
290 g := map[string][]string{}
291 for _, i := range f.Instructions() {
292 opcodes := []string{}
293 for _, n := range neighbors(i) {
294 opcode := "<nil>"
295 if n != nil {
296 opcode = n.Opcode
297 }
298 opcodes = append(opcodes, opcode)
299 }
300 sort.Strings(opcodes)
301 g[i.Opcode] = opcodes
302 }
303 return g
304 }