-
Notifications
You must be signed in to change notification settings - Fork 39
/
cfg.go
157 lines (121 loc) · 2.96 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
package util
import (
"fmt"
"github.com/system-pclub/GCatch/GCatch/tools/go/ssa"
)
func GetFirstInst(bb * ssa.BasicBlock) ssa.Instruction {
if len(bb.Instrs) == 0 {
return nil
}
return bb.Instrs[0]
}
func GetLastInst(bb * ssa.BasicBlock) ssa.Instruction {
if len(bb.Instrs) == 0 {
return nil
}
return bb.Instrs[len(bb.Instrs) -1]
}
func IsFnBegin(ii ssa.Instruction) bool {
bb := *ii.Parent().Blocks[0]
return bb.Instrs[0] == ii
}
func IsFnEnd(ii ssa.Instruction) bool {
fn := ii.Parent()
if len(fn.Blocks) == 0 {
return false
}
for _, bb := range fn.Blocks {
if len(bb.Succs) == 0 {
if ii == GetLastInst(bb) {
return true
}
}
}
return false
}
// according to the annotation of ssa.Function, a function only has one normal entry BB and one optional recover BB.
// There is no need to analyze the recover BB. No BB can reach it and it reaches no BB
func GetEntryInsts(fn *ssa.Function) [] ssa.Instruction {
vecResult := make([] ssa.Instruction, 0)
for _, bb := range fn.Blocks {
if bb.Comment == "recover" {
continue
}
if len(bb.Preds) == 0 {
if len(bb.Instrs) > 0 {
vecResult = append(vecResult, bb.Instrs[0])
break
}
}
}
return vecResult
}
func GetExitInsts(fn * ssa.Function) [] ssa.Instruction {
vecResult := make([] ssa.Instruction, 0)
for _, bb := range fn.Blocks {
if len(bb.Succs) == 0 {
if len(bb.Instrs) > 0 {
vecResult = append(vecResult, bb.Instrs[len(bb.Instrs)-1])
}
}
}
return vecResult
}
func GetPrevInsts(inputInst ssa.Instruction) [] ssa.Instruction {
vecResult := make([] ssa.Instruction, 0)
if IsFnBegin(inputInst) {
return vecResult
}
for _, bb := range inputInst.Parent().Blocks {
if inputInst == bb.Instrs[0] {
for _, pred := range bb.Preds {
vecResult = append(vecResult, GetLastInst(pred))
}
return vecResult
}
}
for _, bb := range inputInst.Parent().Blocks {
for index, _ := range bb.Instrs {
if inputInst == bb.Instrs[index] {
vecResult = append(vecResult, bb.Instrs[index-1])
return vecResult
}
}
}
fmt.Println("Error when calculating previous insts for inst:",inputInst)
panic(inputInst)
}
func GetSuccInsts(inputInst ssa.Instruction) [] ssa.Instruction {
vecResult := make([] ssa.Instruction, 0)
if IsFnEnd(inputInst) {
return vecResult
}
for _, bb := range inputInst.Parent().Blocks {
if inputInst == GetLastInst(bb) {
for _, succ := range bb.Succs {
vecResult = append(vecResult, GetFirstInst(succ))
}
return vecResult
}
}
for _, bb := range inputInst.Parent().Blocks {
for index, _ := range bb.Instrs {
if inputInst == bb.Instrs[index] {
vecResult = append(vecResult, bb.Instrs[index+1])
return vecResult
}
}
}
fmt.Println("Error when calculating previous insts for inst:", inputInst)
panic(inputInst)
}
func GetIIndexBB(II ssa.Instruction) int {
index := 0
for index < len(II.Block().Instrs) {
if II.Block().Instrs[index] == II {
return index
}
index = index + 1
}
return -1
}