forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDumper.cpp
More file actions
460 lines (401 loc) · 12.4 KB
/
Dumper.cpp
File metadata and controls
460 lines (401 loc) · 12.4 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <cctype>
#include <string>
#include "llvh/ADT/DenseMap.h"
#include "llvh/ADT/DenseSet.h"
#include "llvh/ADT/SmallVector.h"
#include "llvh/Support/Casting.h"
#include "llvh/Support/ErrorHandling.h"
#include "llvh/Support/GraphWriter.h"
#include "llvh/Support/raw_ostream.h"
#include "hermes/AST/Context.h"
#include "hermes/FrontEndDefs/Builtins.h"
#include "hermes/IR/CFG.h"
#include "hermes/IR/IR.h"
#include "hermes/IR/IRVisitor.h"
#include "hermes/IR/Instrs.h"
#include "hermes/Support/OSCompat.h"
#include "hermes/Support/Statistic.h"
#include "hermes/Utils/Dumper.h"
using namespace hermes;
using hermes::oscompat::to_string;
using llvh::cast;
using llvh::dyn_cast;
using llvh::isa;
using llvh::raw_ostream;
namespace hermes {
std::string IRPrinter::escapeStr(StringRef name) {
std::string s = name.str();
std::string out;
out += getQuoteSign();
for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
unsigned char c = *i;
if (std::isprint(c) && c != '\\' && c != '"') {
out += c;
} else {
out += "\\\\";
switch (c) {
case '"':
out += "\\\"";
break;
case '\\':
out += "\\\\";
break;
case '\t':
out += 't';
break;
case '\r':
out += 'r';
break;
case '\n':
out += 'n';
break;
default:
char const *const hexdig = "0123456789ABCDEF";
out += 'x';
out += hexdig[c >> 4];
out += hexdig[c & 0xF];
}
}
}
out += getQuoteSign();
return out;
}
std::string IRPrinter::quoteStr(StringRef name) {
if (name.count(" ") || name.empty()) {
return getQuoteSign() + name.str() + getQuoteSign();
}
return name.str();
}
void InstructionNamer::clear() {
Counter = 0;
InstrMap.clear();
}
unsigned InstructionNamer::getNumber(Value *T) {
auto It = InstrMap.find(T);
if (It != InstrMap.end()) {
return It->second;
}
InstrMap[T] = Counter;
return Counter++;
}
void IRPrinter::printTypeLabel(Type T) {
// We don't print type annotations for unknown types.
if (T.isAnyType())
return;
os << " : " << T;
}
void IRPrinter::printValueLabel(Instruction *I, Value *V, unsigned opIndex) {
auto &ctx = I->getContext();
if (isa<CallBuiltinInst>(I) && opIndex == 0) {
os << "["
<< getBuiltinMethodName(cast<CallBuiltinInst>(I)->getBuiltinIndex())
<< "]";
} else if (auto LS = dyn_cast<LiteralString>(V)) {
os << escapeStr(ctx.toString(LS->getValue()));
} else if (auto LB = dyn_cast<LiteralBool>(V)) {
os << (LB->getValue() ? "true" : "false");
} else if (auto LN = dyn_cast<LiteralNumber>(V)) {
const auto Num = LN->getValue();
if (Num == 0 && std::signbit(Num)) {
// Ensure we output -0 correctly
os << "-0";
} else {
char buf[NUMBER_TO_STRING_BUF_SIZE];
numberToString(LN->getValue(), buf, sizeof(buf));
os << buf;
}
} else if (isa<LiteralNull>(V)) {
os << "null";
} else if (isa<LiteralUndefined>(V)) {
os << "undefined";
} else if (isa<GlobalObject>(V)) {
os << "globalObject";
} else if (isa<EmptySentinel>(V)) {
os << "empty";
} else if (isa<Instruction>(V)) {
os << "%" << InstNamer.getNumber(V);
} else if (isa<BasicBlock>(V)) {
os << "%BB" << BBNamer.getNumber(V);
} else if (auto L = dyn_cast<Label>(V)) {
auto Name = L->get();
os << "$" << quoteStr(ctx.toString(Name));
} else if (auto P = dyn_cast<Parameter>(V)) {
auto Name = P->getName();
os << "%" << ctx.toString(Name);
} else if (auto F = dyn_cast<Function>(V)) {
os << "%" << quoteStr(ctx.toString(F->getInternalName())) << "()";
} else if (auto VS = dyn_cast<VariableScope>(V)) {
os << "%" << quoteStr(ctx.toString(VS->getFunction()->getInternalName()))
<< "()";
} else if (auto VR = dyn_cast<Variable>(V)) {
os << "[" << quoteStr(ctx.toString(VR->getName()));
if (I->getParent()->getParent() != VR->getParent()->getFunction()) {
StringRef scopeName =
VR->getParent()->getFunction()->getInternalNameStr();
os << "@" << quoteStr(scopeName);
}
os << "]";
} else {
llvm_unreachable("Invalid value");
}
printTypeLabel(V->getType());
}
void IRPrinter::printFunctionHeader(Function *F) {
bool first = true;
auto &Ctx = F->getContext();
std::string defKindStr = F->getDefinitionKindStr(false);
os << defKindStr << " " << quoteStr(Ctx.toString(F->getInternalName()))
<< "(";
for (auto P : F->getParameters()) {
if (!first) {
os << ", ";
}
os << Ctx.toString(P->getName());
printTypeLabel(P->getType());
first = false;
}
os << ")";
printTypeLabel(F->getType());
}
void IRPrinter::printFunctionVariables(Function *F) {
bool first = true;
auto &Ctx = F->getContext();
os << "frame = [";
for (auto V : F->getFunctionScope()->getVariables()) {
if (!first) {
os << ", ";
}
os << Ctx.toString(V->getName());
printTypeLabel(V->getType());
first = false;
}
os << "]";
if (F->isGlobalScope()) {
bool first2 = true;
for (auto *GP : F->getParent()->getGlobalProperties()) {
if (!GP->isDeclared())
continue;
if (first2) {
os << ", globals = [";
} else {
os << ", ";
}
os << Ctx.toString(GP->getName()->getValue());
first2 = false;
}
if (!first2)
os << "]";
}
}
void IRPrinter::printInstructionDestination(Instruction *I) {
os << "%" << InstNamer.getNumber(I);
}
void IRPrinter::printInstruction(Instruction *I) {
printInstructionDestination(I);
os << " = ";
os << I->getName();
bool first = true;
if (auto *binop = dyn_cast<BinaryOperatorInst>(I)) {
os << " '" << binop->getOperatorStr() << "'";
first = false;
} else if (auto *cmpbr = dyn_cast<CompareBranchInst>(I)) {
os << " '" << cmpbr->getOperatorStr() << "'";
first = false;
} else if (auto *unop = dyn_cast<UnaryOperatorInst>(I)) {
os << " '" << unop->getOperatorStr() << "'";
first = false;
}
for (int i = 0, e = I->getNumOperands(); i < e; i++) {
os << (first ? " " : ", ");
printValueLabel(I, I->getOperand(i), i);
first = false;
}
auto codeGenOpts = I->getContext().getCodeGenerationSettings();
// Print the use list if there is any user for the instruction.
if (!codeGenOpts.dumpUseList || I->getUsers().empty())
return;
llvh::DenseSet<Instruction *> Visited;
os << " // users:";
for (auto &U : I->getUsers()) {
auto *II = cast<Instruction>(U);
assert(II && "Expecting user to be an Instruction");
if (Visited.find(II) != Visited.end())
continue;
Visited.insert(II);
os << " %" << InstNamer.getNumber(II);
}
}
void IRPrinter::printSourceLocation(SMLoc loc) {
SourceErrorManager::SourceCoords coords;
if (!sm_.findBufferLineAndLoc(loc, coords))
return;
os << sm_.getSourceUrl(coords.bufId) << ":" << coords.line << ":"
<< coords.col;
}
void IRPrinter::printSourceLocation(SMRange rng) {
SourceErrorManager::SourceCoords start, end;
if (!sm_.findBufferLineAndLoc(rng.Start, start) ||
!sm_.findBufferLineAndLoc(rng.End, end))
return;
os << "[" << sm_.getSourceUrl(start.bufId) << ":" << start.line << ":"
<< start.col << " ... " << sm_.getSourceUrl(end.bufId) << ":" << end.line
<< ":" << end.col << ")";
}
void IRPrinter::visitModule(const Module &M) {
// Use IRVisitor dispatch to visit each individual function.
for (auto &F : M)
visit(F);
}
void IRPrinter::visitFunction(const Function &F) {
auto *UF = const_cast<Function *>(&F);
os.indent(Indent);
BBNamer.clear();
InstNamer.clear();
// Number all instructions sequentially.
for (auto &BB : *UF)
for (auto &I : BB)
InstNamer.getNumber(&I);
printFunctionHeader(UF);
os << "\n";
printFunctionVariables(UF);
os << "\n";
auto codeGenOpts = F.getContext().getCodeGenerationSettings();
if (codeGenOpts.dumpSourceLocation) {
os << "source location: ";
printSourceLocation(F.getSourceRange());
os << "\n";
}
// Use IRVisitor dispatch to visit the basic blocks.
for (auto &BB : F) {
visit(BB);
}
os.indent(Indent);
os << "function_end"
<< "\n";
os << "\n";
}
void IRPrinter::visitBasicBlock(const BasicBlock &BB) {
auto *UBB = const_cast<BasicBlock *>(&BB);
os.indent(Indent);
os << "%BB" << BBNamer.getNumber(UBB) << ":\n";
Indent += 2;
// Use IRVisitor dispatch to visit the instructions.
for (auto &I : BB)
visit(I);
Indent -= 2;
}
void IRPrinter::visitInstruction(const Instruction &I) {
auto *UII = const_cast<Instruction *>(&I);
auto codeGenOpts = I.getContext().getCodeGenerationSettings();
if (codeGenOpts.dumpSourceLocation) {
os << "; ";
printSourceLocation(UII->getLocation());
os << "\n";
}
os.indent(Indent);
printInstruction(UII);
os << "\n";
}
} // namespace hermes
namespace {
/// This class prints Functions into dotty graphs. This struct inherits the
/// IRVisitor and reimplement the visitFunction and visitBasicBlock function.
struct DottyPrinter : public IRVisitor<DottyPrinter, void> {
llvh::raw_ostream &os;
llvh::SmallVector<std::pair<std::string, std::string>, 4> Edges;
IRPrinter Printer;
explicit DottyPrinter(Context &ctx, llvh::raw_ostream &ost, StringRef Title)
: os(ost), Printer(ctx, ost, /* escape output */ true) {
os << " digraph g {\n graph [ rankdir = \"TD\" ];\n";
os << "labelloc=\"t\"; ";
os << " node [ fontsize = \"16\" shape = \"record\" ]; edge [ ];\n";
}
~DottyPrinter() {
os << "\n";
// Print the edges between the blocks.
for (auto T : Edges) {
os << "" << T.first << " ->";
os << "" << T.second << ";\n";
}
os << "\n}\n";
}
/// Convert pointers into unique textual ids.
static std::string toString(BasicBlock *ptr) {
auto Num = (size_t)ptr;
return to_string(Num);
}
/// Reimplement the visitFunction in IRVisitor.
void visitFunction(const Function &F) {
os << "label=\"";
Printer.printFunctionHeader(const_cast<Function *>(&F));
os << "\";\n";
// Pre-assign every instruction a number, by doing this we avoid
// assigning non-consecutive numbers to consecutive instructions if we
// are dumping user list. Its also not a bad practice to initialize the
// InstNamer table before we dump all the instructions.
for (auto &BB : F) {
for (auto &II : BB) {
(void)Printer.InstNamer.getNumber(const_cast<Instruction *>(&II));
}
}
// Use IRVisitor dispatch to visit the basic blocks.
for (auto &BB : F) {
visit(BB);
}
}
/// Reimplement the visitBasicBlock in the IRVisitor.
/// Visit all of the basic blocks in the program and render them into dotty
/// records. Save edges between basic blocks and print them when we finish
/// visiting all of the blocks in the program.
void visitBasicBlock(const BasicBlock &V) {
auto *BB = const_cast<BasicBlock *>(&V);
os << "\"" << toString(BB) << "\"";
os << "[ label = \"{ ";
os << " { <self> | <head> \\<\\<%BB" << Printer.BBNamer.getNumber(BB)
<< "\\>\\> | } ";
int counter = 0;
// For each instruction in the basic block:
for (auto &I : *BB) {
counter++;
os << " | ";
// Print the instruction.
os << "<L" << counter << ">";
Printer.printInstruction(&I);
}
for (auto I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
auto From = toString(BB) + ":L" + to_string(counter);
Edges.push_back({From, toString(*I) + (*I == BB ? ":self" : ":head")});
}
// Emit the record:
os << "}\" shape = \"record\" ]; \n";
}
};
} // anonymous namespace
void hermes::viewGraph(Function *F) {
#ifndef NDEBUG
auto &Ctx = F->getContext();
StringRef Name = Ctx.toString(F->getInternalName());
int FD;
// Windows can't always handle long paths, so limit the length of the name.
std::string N = Name.str();
N = N.substr(0, std::min<std::size_t>(N.size(), 140));
std::string Filename = llvh::createGraphFilename(N, FD);
{
llvh::raw_fd_ostream O(FD, /*shouldClose=*/true);
if (FD == -1) {
llvh::errs() << "error opening file '" << Filename << "' for writing!\n";
return;
}
DottyPrinter D(F->getContext(), O, Name);
D.visitFunction(*F);
}
llvh::DisplayGraph(Filename);
llvh::errs() << " done. \n";
#endif
}