forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRInstrument.cpp
More file actions
128 lines (108 loc) · 3.48 KB
/
IRInstrument.cpp
File metadata and controls
128 lines (108 loc) · 3.48 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
/*
* 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 "IRInstrument.h"
namespace hermes {
#ifdef HERMES_ENABLE_IR_INSTRUMENTATION
IRInstrument::IRInstrument(hermes::Module *M, hermes::IRBuilder &builder)
: builder_(builder),
globalName_(builder.getLiteralString("__instrument")),
M_(M),
enabled_(M->getContext().getCodeGenerationSettings().instrumentIR) {}
IRInstrument::~IRInstrument() = default;
Value *IRInstrument::getIID(ESTree::Node *node) {
auto start = node->getStartLoc();
if (!start.isValid())
return builder_.getLiteralUndefined();
auto &sem = M_->getContext().getSourceErrorManager();
auto *buffer = sem.findBufferForLoc(start);
uint64_t bufferId = sem.findBufferIdForLoc(start);
uint64_t offset = (uint64_t)(
(uintptr_t)start.getPointer() - (uintptr_t)buffer->getBufferStart());
double id = (double)((bufferId << 32) | offset);
return builder_.getLiteralNumber(id);
}
Value *IRInstrument::invokeHook(
llvh::StringRef name,
llvh::ArrayRef<Value *> args) {
TryLoadGlobalPropertyInst *instrument =
builder_.createTryLoadGlobalPropertyInst(globalName_);
auto *hook = builder_.createLoadPropertyInst(instrument, name);
return builder_.createCallInst(hook, instrument, args);
}
Value *IRInstrument::preBinaryExpression(
ESTree::BinaryExpressionNode *node,
Value *left,
Value *right) {
if (!enabled_)
return nullptr;
auto *opStr = builder_.getLiteralString(node->_operator->str());
return invokeHook("preBinary", {getIID(node), opStr, left, right});
}
Value *IRInstrument::postBinaryExpression(
ESTree::BinaryExpressionNode *node,
Value *cookie,
Value *result,
Value *left,
Value *right) {
if (!enabled_)
return result;
auto *opStr = builder_.getLiteralString(node->_operator->str());
return invokeHook(
"postBinary",
{getIID(node), undefinedIfNull(cookie), opStr, result, left, right});
return result;
}
Value *IRInstrument::preUnaryExpression(
ESTree::UnaryExpressionNode *node,
Value *operand) {
if (!enabled_)
return nullptr;
auto *opStr = builder_.getLiteralString(node->_operator->str());
return invokeHook("preUnary", {getIID(node), opStr, operand});
}
Value *IRInstrument::postUnaryExpression(
ESTree::UnaryExpressionNode *node,
Value *cookie,
Value *result,
Value *operand) {
if (!enabled_)
return result;
auto *opStr = builder_.getLiteralString(node->_operator->str());
return invokeHook(
"postUnary",
{getIID(node), undefinedIfNull(cookie), opStr, result, operand});
}
Value *IRInstrument::preAssignment(
ESTree::AssignmentExpressionNode *node,
Value *left,
Value *right) {
if (!enabled_)
return nullptr;
auto *opStr = builder_.getLiteralString(node->_operator->str());
return invokeHook(
"preAssignment", {getIID(node), opStr, undefinedIfNull(left), right});
}
Value *IRInstrument::postAssignment(
ESTree::AssignmentExpressionNode *node,
Value *cookie,
Value *result,
Value *left,
Value *right) {
if (!enabled_)
return result;
auto *opStr = builder_.getLiteralString(node->_operator->str());
return invokeHook(
"postAssignment",
{getIID(node),
undefinedIfNull(cookie),
opStr,
result,
undefinedIfNull(left),
right});
}
#endif // HERMES_ENABLE_IR_INSTRUMENTATION
} // namespace hermes