-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAvailableExpressionAnalysisFlow.cpp
More file actions
executable file
·229 lines (195 loc) · 6.38 KB
/
AvailableExpressionAnalysisFlow.cpp
File metadata and controls
executable file
·229 lines (195 loc) · 6.38 KB
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
225
226
227
228
229
/*
* Flow.cpp
*
* Created by Costas Zarifis on 22/05/2014.
*/
#include "AvailableExpressionAnalysisFlow.h"
/*
* Flows are equal if their values are equal
*/
bool AvailableExpressionAnalysisFlow::equals(Flow* otherSuper) {
AvailableExpressionAnalysisFlow* other =
static_cast<AvailableExpressionAnalysisFlow*>(otherSuper);
if (this->isBasic() || other->isBasic())
return this->basicEquals(other);
if (other->value.size() != this->value.size())
return false;
for (map<string, string>::const_iterator it = this->value.begin();
it != this->value.end(); it++) {
string key = it->first;
string thisVal = it->second;
//Check if key is found in other
if (other->value.find(key) == other->value.end())
return false;
string otherVal = other->value.find(key)->second;
if (otherVal != thisVal)
return false;
}
return true;
}
//Represents a constant propagation analysis value as a JSON string.
string AvailableExpressionAnalysisFlow::jsonString() {
if (value.size() == 0)
return "\"" + basic + "\"";
//Value has something inside
stringstream ss;
map<string, string>::const_iterator it = this->value.begin();
/*ss << "{\"" << it->first << "\" : [ ";
set<string>::iterator its=it->second.begin();
ss << *its << " "; its++;
for (; its != it->second.end() ; its++) {
ss << ", " << *its;
}
ss << " ] ";
*/
int counter = 0;
for (; it != this->value.end(); it++) {
if (counter == 0) {
ss << "\"" << it->first << "\" : ";
string v = it->second;
ss << v << " ";
} else {
ss << ",\"" << it->first << "\" : ";
string v = it->second;
ss << v << " ";
}
counter++;
}
ss << "}";
// errs() << "After jSonString()...\n";
return ss.str();
/*
*
stringstream ss;
map<string, set<string> >::const_iterator it = this->value.begin();
ss << "{\"" << it->first << "\" : [ ";
set<string>::iterator its=it->second.begin();
ss << *its << " "; its++;
for (; its != it->second.end() ; its++) {
ss << ", " << *its;
}
ss << " ] ";
for (; it != this->value.end() ; it++) {
ss << "\"" << it->first << "\" : [ ";
its=it->second.begin();
ss << *its << " "; its++;
for (; its != it->second.end() ; its++) {
ss << ", " << *its;
}
ss << "] ";
}
ss << "}";
return ss.str();
*
*
*
*/
}
void AvailableExpressionAnalysisFlow::copy(Flow* rhs) {
AvailableExpressionAnalysisFlow* f = static_cast<AvailableExpressionAnalysisFlow*>(rhs);
this->basic = f->basic;
this->value = f->value;
}
AvailableExpressionAnalysisFlow::AvailableExpressionAnalysisFlow() :
Flow() {
}
AvailableExpressionAnalysisFlow::AvailableExpressionAnalysisFlow(string input) :
Flow(input) {
}
AvailableExpressionAnalysisFlow::AvailableExpressionAnalysisFlow(
AvailableExpressionAnalysisFlow *flow) :
Flow(flow->basic) {
this->value = flow->value;
}
// TODO : Fix the join... See the commented out stuff
//Merges flow together.
Flow* AvailableExpressionAnalysisFlow::join(Flow* otherSuper) {
//join bottom-bottom gives you bottom. Anything else gives you top.
AvailableExpressionAnalysisFlow* other =
static_cast<AvailableExpressionAnalysisFlow*>(otherSuper);
// errs()<< "I just entered into the sublcassed join... \n";
if (this->basic == BOTTOM && other->basic == BOTTOM)
return new AvailableExpressionAnalysisFlow(BOTTOM);
//Anything joined with a bottom will just be itself.
if (this->basic == BOTTOM) {
AvailableExpressionAnalysisFlow* f = new AvailableExpressionAnalysisFlow();
f->copy(other);
return f;
}
if (other->basic == BOTTOM) {
AvailableExpressionAnalysisFlow* f = new AvailableExpressionAnalysisFlow();
f->copy(this);
return f;
}
//Join anything with top will give you top.
if (this->basic == TOP || other->basic == TOP)
return new AvailableExpressionAnalysisFlow(TOP);
//Merge the input from both.
AvailableExpressionAnalysisFlow* f = new AvailableExpressionAnalysisFlow();
//f = other;
for (map<string, string>::iterator it = this->value.begin();
it != this->value.end(); it++) {
if (other->value.find(it->first) == other->value.end()) {
// They don't have the same key! We're good!
f->value[it->first] = this->value.find(it->first)->second;
} else {
// Oh no! They do have the same key! We need to check if they have
// the same values! if they do then we're good
string otherVal = other->value.find(it->first)->second;
string thisVal = this->value.find(it->first)->second;
if (otherVal == thisVal)
// OK, we can replicate the value since both branches
// had the same value for this variable
f->value[it->first] = otherVal;
//else
// Nope! They have different values
// we need to omit this variable for
// the (implicit) "set"
}
}
for (map<string, string>::iterator it = other->value.begin();
it != other->value.end(); it++) {
if (this->value.find(it->first) == this->value.end()) {
// They don't have the same key! We're good!
f->value[it->first] = other->value.find(it->first)->second;
} else {
// Oh no! They do have the same key! We need to check if they have
// the same values! if they do then we're good
string thisVal = this->value.find(it->first)->second;
string otherVal = other->value.find(it->first)->second;
if (otherVal == thisVal)
// OK, we can replicate the value since both branches
// had the same value for this variable
f->value[it->first] = otherVal;
//else
// Nope! They have different values
// we need to omit this variable for
// the (implicit) "set"
}
}
// errs()<< "JOINING!\n";
// for (map<string, float>::iterator it = f->value.begin();
// it != f->value.end(); it++) {
// errs()<< it->first << " -> " << it->second << "\n";
// }
//Get all keys
/*set<string> keys;
for (map<string, set<string> >::iterator it = this->value.begin() ; it != this->value.end() ; it++)
keys.insert(it->first);
for (map<string, set<string> >::iterator it = other->value.begin() ; it != other->value.end() ; it++)
keys.insert(it->first);
for (set<string>::iterator it = keys.begin() ; it != keys.end() ; it++) {
string key = *it;
set<string> values;
for (set<string>::iterator j = this->value[key].begin(); j != this->value[key].end() ; j++)
values.insert(*j);
for (set<string>::iterator j = other->value[key].begin(); j != other->value[key].end() ; j++)
values.insert(*j);
f->value[key] = values;
}
*/
return f;
}
AvailableExpressionAnalysisFlow::~AvailableExpressionAnalysisFlow() {
//Nothing for basic static analysis
}