/
dependency_pass.go
224 lines (202 loc) · 5.25 KB
/
dependency_pass.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
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
// Dependency pass analysis
//
// Use the individual accesses in the Access Pass to classify each variable as
// WriteFirst, ReadOnly or ReadWrite
package main
import (
"bytes"
"go/ast"
)
type DependencyPass struct {
BasePass
}
type DependencyType uint
const (
Unknown DependencyType = iota
ReadOnly
WriteFirst
ReadWrite
)
var dependencyTypeString = map[DependencyType]string{
Unknown: "\x1b[31mUnknown\x1b[0m",
ReadOnly: "\x1b[32mReadOnly\x1b[0m",
WriteFirst: "\x1b[33mWriteFirst\x1b[0m",
ReadWrite: "\x1b[35mReadWrite\x1b[0m",
}
type DependencyLevel struct {
id string
dependency DependencyType
isIndexed bool // is the children map by sub-expression (x.y) or index (x[y])
children map[string]*DependencyLevel
}
func NewDependencyLevel() *DependencyLevel {
return &DependencyLevel{
children: make(map[string]*DependencyLevel),
}
}
// Store all dependencies for this block
type Dependency struct {
group []Identifier
depType DependencyType
goType Type
}
func (d *Dependency) String() string {
var buffer bytes.Buffer
for _, i := range d.group {
buffer.WriteString(i.id)
if i.isIndexed {
buffer.WriteString("[")
buffer.WriteString(i.index)
buffer.WriteString("]")
}
buffer.WriteString(".")
}
buffer.WriteString(dependencyTypeString[d.depType])
if d.goType != nil {
buffer.WriteString(" ")
buffer.WriteString(d.goType.String())
}
return buffer.String()
}
// Make a new AST node that represents this dependency identifier group
func (d *Dependency) MakeNode() ast.Expr {
var root ast.Expr
// ((a.b)[2]).c
for _, i := range d.group {
if root == nil {
root = &ast.Ident{Name: i.id}
} else {
root = &ast.SelectorExpr{X: root, Sel: &ast.Ident{Name: i.id}}
}
if i.isIndexed {
root = &ast.IndexExpr{X: root, Index: &ast.Ident{Name: i.index}}
}
}
return root
}
func ClassifyAccess(prev DependencyType, t AccessType) DependencyType {
if prev != Unknown {
// upgrade the previous access
if prev == ReadOnly && t == WriteAccess {
return ReadWrite
}
return prev
} else {
if t == ReadAccess {
return ReadOnly
} else if t == WriteAccess {
return WriteFirst
}
return Unknown
}
return Unknown
}
type DependencyPassData struct {
deps []Dependency
}
func NewDependencyPassData() *DependencyPassData {
return &DependencyPassData{
deps: make([]Dependency, 0),
}
}
func NewDependencyPass() *DependencyPass {
return &DependencyPass{
BasePass: NewBasePass(),
}
}
func (pass *DependencyPass) GetPassType() PassType {
return DependencyPassType
}
func (pass *DependencyPass) GetPassMode() PassMode {
return BasicBlockPassMode
}
func (pass *DependencyPass) GetDependencies() []PassType {
return []PassType{AccessPassFuncPropogateType}
}
func (pass *DependencyPass) RunBasicBlockPass(block *BasicBlock, p *Package) BasicBlockVisitor {
dataBlock := block.Get(AccessPassType).(*AccessPassData)
dependencyData := NewDependencyPassData()
block.Set(DependencyPassType, dependencyData)
if *verbose {
for _, access := range dataBlock.accesses {
block.Printf(access.String())
}
}
for _, access := range dataBlock.accesses {
// propogate the access to potentially multiple dependency entries for
// subaccesses, arrays, etc
//
// Check the dep list for sub or super accesses (a.b or a.b.c.d for a.b.c)
// Super and exact accesses (dep = a.b, access = a.b.c or a.b[idx])
//
// a.b = ReadOnly (dep)
// a.b.c = WriteAccess (access)
// a.b -> ReadWrite
for idx, dep := range dependencyData.deps {
if len(access.group) < len(dep.group) {
// sub-access
for i := 0; i < len(access.group); i++ {
if !access.group[i].Equals(dep.group[i]) {
break
}
if i == len(access.group)-1 {
// at the end, everything matches up to here
dependencyData.deps[idx].depType = ClassifyAccess(dep.depType, access.t)
if *verbose {
block.Printf("Sub-access %s < %s", dep.String(), access.String())
block.Printf(" => %s", dependencyData.deps[idx].String())
}
}
}
} else {
// super-access
for i := 0; i < len(dep.group); i++ {
if !dep.group[i].Equals(access.group[i]) {
break
}
if i == len(dep.group)-1 {
// at the end, everything matches up to here
dependencyData.deps[idx].depType = ClassifyAccess(dep.depType, access.t)
if *verbose {
block.Printf("Super-access %s >= %s", dep.String(), access.String())
block.Printf(" => %s", dependencyData.deps[idx].String())
}
}
}
}
}
// Add this access if it's unique (a.b[idx])
var matched bool
for _, dep := range dependencyData.deps {
if len(dep.group) != len(access.group) {
continue // not an exact match
}
for i := 0; i < len(dep.group); i++ {
if !dep.group[i].Equals(access.group[i]) {
break
}
// if exact match
if i == len(dep.group)-1 {
matched = true
}
}
}
if !matched {
dep := Dependency{
group: access.group,
depType: ClassifyAccess(Unknown, access.t),
}
dependencyData.deps = append(dependencyData.deps, dep)
if *verbose {
block.Printf("Added dep %s", dep.String())
}
}
}
if *verbose {
block.Print("== Dependencies ==")
for i := range dependencyData.deps {
block.Print(dependencyData.deps[i].String())
}
}
return DefaultBasicBlockVisitor{}
}