-
Notifications
You must be signed in to change notification settings - Fork 0
/
Atomic.cpp
227 lines (211 loc) · 6.93 KB
/
Atomic.cpp
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
#include "Atomic.h"
void AtomBlock::add_succ(float block_id, bool reach) {
TMPPC tmp(reach, block_id);
this->tmp_succs.push_back(tmp);
}
void AtomBlock::add_pred(float block_id, bool reach) {
TMPPC tmp(reach, block_id);
this->tmp_preds.push_back(tmp);
}
BlockVec AtomBlock::get_preds() { return this->preds; }
BlockVec AtomBlock::get_succs() { return this->succs; }
void AtomBlock::add_all_preds() {
for (auto &&b : this->parent->atom_blocks) {
for (auto &&p : this->tmp_preds) {
if (b.atom_id == p.id) {
this->preds.push_back(&b);
}
}
}
}
void AtomBlock::add_all_succs() {
for (auto &&b : this->parent->atom_blocks) {
for (auto &&s : this->tmp_succs) {
if (b.atom_id == s.id) {
this->succs.push_back(&b);
}
}
}
}
void AtomicCFG::generate_atomic_cfg() {
unsigned max = 0, scale = 10;
for (auto *const blk : *this->raw_cfg)
if (auto size = blk->size(); size > max)
max = size;
while (max /= 10)
scale *= 10;
for (auto *const blk : *this->raw_cfg) {
unsigned bID = blk->getBlockID();
unsigned size = blk->size();
clang::Stmt *statement = nullptr;
if (size > 1) {
for (unsigned i = 0; i < size; i++) {
statement = (clang::Stmt *)(*blk)[size - i - 1].castAs<clang::CFGStmt>().getStmt();
AtomBlock tmp(this, blk, bID * scale + i, statement);
if (i == size - 1) {
tmp.add_succ(bID * scale + i - 1, true);
for (auto it = blk->pred_begin(); it != blk->pred_end(); it++) {
bool flag;
unsigned _id;
if (it->isReachable()) {
_id = (*it)->getBlockID();
flag = true;
} else {
_id = it->getPossiblyUnreachableBlock()->getBlockID();
flag = false;
}
_id *= scale;
tmp.add_pred(_id, flag);
}
} else if (i == 0) {
tmp.add_pred(bID * scale + i + 1, true);
for (auto it = blk->succ_begin(); it != blk->succ_end(); it++) {
if (it->isReachable()) {
auto size = (*it)->size(),
last = (size > 0) ? size - 1 : size;
tmp.add_succ((*it)->getBlockID() * scale + last, true);
} else {
auto size = it->getPossiblyUnreachableBlock()->size(),
last = (size > 0) ? size - 1 : size,
bID = it->getPossiblyUnreachableBlock()->getBlockID();
tmp.add_succ(bID * scale + last, false);
}
}
} else {
tmp.add_pred(bID * scale + i + 1, true);
tmp.add_succ(bID * scale + i - 1, true);
}
this->atom_blocks.push_back(tmp);
}
} else {
if (size != 0)
statement = (clang::Stmt *)(*blk)[0].castAs<clang::CFGStmt>().getStmt();
else
statement = nullptr;
AtomBlock tmp(this, blk, bID * scale, statement);
for (auto it = blk->pred_begin(); it != blk->pred_end(); it++) {
if (it->isReachable()) {
tmp.add_pred((*it)->getBlockID() * scale, true);
} else {
auto bID = it->getPossiblyUnreachableBlock()->getBlockID();
tmp.add_pred(bID * scale, false);
}
}
for (auto it = blk->succ_begin(); it != blk->succ_end(); it++) {
if (it->isReachable()) {
auto size = (*it)->size(),
last = (size > 0) ? size - 1 : size;
tmp.add_succ((*it)->getBlockID() * scale + last, true);
} else if (auto b = it->getPossiblyUnreachableBlock()){
auto size = b->size(),
last = (size > 0) ? size - 1 : size,
bID = b->getBlockID();
tmp.add_succ(bID * scale + last, false);
}
}
this->atom_blocks.push_back(tmp);
}
}
for (auto &&b : this->atom_blocks) {
b.add_all_preds();
b.add_all_succs();
}
}
bool AtomBlock::operator==(AtomBlock &a) { return a.atom_id == this->atom_id; }
void AtomicCFG::prettyPrint(std::ostream& ofile) {
auto pp = clang::PrintingPolicy(Ctx->getLangOpts());
auto jStr = std::string("[\n");
for (auto const &AB : atom_blocks) {
// auto AB = db.atom_block;
std::string stmtStream;
llvm::raw_string_ostream ross(stmtStream);
if (AB.statement)
AB.statement->printPretty(ross, nullptr, pp, 0U, "\n", Ctx);
jStr += " {\"BlockID\":" + std::to_string(AB.atom_id) + ",";
jStr += "\"Stmt\":" + AB.JsonFormat(llvm::StringRef(ross.str()), true) +
",";
jStr += "\"CFG Preds\":[";
for (auto const &pred : AB.preds)
jStr += std::to_string(pred->atom_id) + ",";
if (!AB.preds.empty())
jStr.erase(jStr.end() - 1);
jStr += "]},";
}
if (!atom_blocks.empty())
jStr.erase(jStr.end() - 1);
jStr += "]";
ofile << jStr;
}
std::string AtomBlock::JsonFormat(clang::StringRef RawSR, bool AddQuotes) {
if (RawSR.empty())
return "\"null\"";
// Trim special characters.
std::string Str = RawSR.trim().str();
size_t Pos = 0;
// Escape backslashes.
while (true) {
Pos = Str.find('\\', Pos);
if (Pos == std::string::npos)
break;
// Prevent bad conversions.
size_t TempPos = (Pos != 0) ? Pos - 1 : 0;
// See whether the current backslash is not escaped.
if (TempPos != Str.find("\\\\", Pos)) {
Str.insert(Pos, "\\");
++Pos; // As we insert the backslash move plus one.
}
++Pos;
}
// Escape double quotes.
Pos = 0;
while (true) {
Pos = Str.find('\"', Pos);
if (Pos == std::string::npos)
break;
// Prevent bad conversions.
size_t TempPos = (Pos != 0) ? Pos - 1 : 0;
// See whether the current double quote is not escaped.
if (TempPos != Str.find("\\\"", Pos)) {
Str.insert(Pos, "\\");
++Pos; // As we insert the escape-character move plus one.
}
++Pos;
}
// Remove new-lines.
Str.erase(std::remove(Str.begin(), Str.end(), '\n'), Str.end());
if (!AddQuotes)
return Str;
return '\"' + Str + '\"';
}
void AtomBlock::printSourceLocationAsJson(llvm::raw_ostream &Out, clang::SourceLocation Loc, const clang::SourceManager &SM, bool AddBraces) {
// Mostly copy-pasted from SourceLocation::print.
if (!Loc.isValid()) {
Out << "null";
return;
}
if (Loc.isFileID()) {
clang::PresumedLoc PLoc = SM.getPresumedLoc(Loc);
if (PLoc.isInvalid()) {
Out << "null";
return;
}
// The macro expansion and spelling pos is identical for file locs.
if (AddBraces)
Out << "{ ";
Out << "\"line\": " << PLoc.getLine()
<< ", \"column\": " << PLoc.getColumn()
<< ", \"ID\": " << Loc.getRawEncoding();
// Loc.dump(SM);
if (AddBraces)
Out << " }";
return;
}
// We want 'location: { ..., spelling: { ... }}' but not
// 'location: { ... }, spelling: { ... }', hence the dance
// with braces.
Out << "{ ";
printSourceLocationAsJson(Out, SM.getExpansionLoc(Loc), SM, false);
Out << ", \"spelling\": ";
printSourceLocationAsJson(Out, SM.getSpellingLoc(Loc), SM, true);
Out << " }";
}