forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cc
More file actions
343 lines (302 loc) Β· 12.3 KB
/
code.cc
File metadata and controls
343 lines (302 loc) Β· 12.3 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
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/objects/code.h"
#include <iomanip>
#include "src/codegen/assembler-inl.h"
#include "src/codegen/flush-instruction-cache.h"
#include "src/codegen/reloc-info-inl.h"
#include "src/codegen/source-position-table.h"
#include "src/codegen/source-position.h"
#include "src/deoptimizer/deoptimizer.h"
#include "src/objects/code-inl.h"
#ifdef ENABLE_DISASSEMBLER
#include "src/diagnostics/disassembler.h"
#include "src/diagnostics/eh-frame.h"
#endif
namespace v8 {
namespace internal {
Tagged<Object> Code::raw_deoptimization_data_or_interpreter_data() const {
return RawProtectedPointerField(kDeoptimizationDataOrInterpreterDataOffset)
.load();
}
Tagged<Object> Code::raw_position_table() const {
return RawProtectedPointerField(kPositionTableOffset).load();
}
void Code::ClearEmbeddedObjectsAndJSDispatchHandles(Heap* heap) {
DisallowGarbageCollection no_gc;
Tagged<HeapObject> undefined = ReadOnlyRoots(heap).undefined_value();
Tagged<InstructionStream> istream = unchecked_instruction_stream();
int mode_mask = RelocInfo::EmbeddedObjectModeMask();
#ifdef V8_ENABLE_LEAPTIERING
mode_mask |= RelocInfo::JSDispatchHandleModeMask();
#endif
{
WritableJitAllocation jit_allocation = ThreadIsolation::LookupJitAllocation(
istream->address(), istream->Size(),
ThreadIsolation::JitAllocationType::kInstructionStream, true);
for (WritableRelocIterator it(jit_allocation, istream, constant_pool(),
mode_mask);
!it.done(); it.next()) {
const auto mode = it.rinfo()->rmode();
if (RelocInfo::IsEmbeddedObjectMode(mode)) {
it.rinfo()->set_target_object(istream, undefined, SKIP_WRITE_BARRIER);
#ifdef V8_ENABLE_LEAPTIERING
} else {
it.rinfo()->set_js_dispatch_handle(istream, kNullJSDispatchHandle,
SKIP_WRITE_BARRIER);
#endif // V8_ENABLE_LEAPTIERING
}
}
}
set_embedded_objects_cleared(true);
}
void Code::FlushICache() const {
FlushInstructionCache(instruction_start(), instruction_size());
}
int Code::SourcePosition(int offset) const {
CHECK_NE(kind(), CodeKind::BASELINE);
// Subtract one because the current PC is one instruction after the call site.
offset--;
int position = 0;
if (!has_source_position_table()) return position;
for (SourcePositionTableIterator it(
source_position_table(),
SourcePositionTableIterator::kJavaScriptOnly,
SourcePositionTableIterator::kDontSkipFunctionEntry);
!it.done() && it.code_offset() <= offset; it.Advance()) {
position = it.source_position().ScriptOffset();
}
return position;
}
int Code::SourceStatementPosition(int offset) const {
CHECK_NE(kind(), CodeKind::BASELINE);
// Subtract one because the current PC is one instruction after the call site.
offset--;
int position = 0;
if (!has_source_position_table()) return position;
for (SourcePositionTableIterator it(source_position_table());
!it.done() && it.code_offset() <= offset; it.Advance()) {
if (it.is_statement()) {
position = it.source_position().ScriptOffset();
}
}
return position;
}
SafepointEntry Code::GetSafepointEntry(Isolate* isolate, Address pc) {
DCHECK(!is_maglevved());
SafepointTable table(isolate, pc, *this);
return table.FindEntry(pc);
}
MaglevSafepointEntry Code::GetMaglevSafepointEntry(Isolate* isolate,
Address pc) {
DCHECK(is_maglevved());
MaglevSafepointTable table(isolate, pc, *this);
return table.FindEntry(pc);
}
bool Code::IsIsolateIndependent(Isolate* isolate) {
static constexpr int kModeMask =
RelocInfo::AllRealModesMask() &
~RelocInfo::ModeMask(RelocInfo::CONST_POOL) &
~RelocInfo::ModeMask(RelocInfo::OFF_HEAP_TARGET) &
~RelocInfo::ModeMask(RelocInfo::VENEER_POOL) &
~RelocInfo::ModeMask(RelocInfo::WASM_CANONICAL_SIG_ID) &
~RelocInfo::ModeMask(RelocInfo::WASM_CODE_POINTER_TABLE_ENTRY);
static_assert(kModeMask ==
(RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
RelocInfo::ModeMask(RelocInfo::RELATIVE_CODE_TARGET) |
RelocInfo::ModeMask(RelocInfo::COMPRESSED_EMBEDDED_OBJECT) |
RelocInfo::ModeMask(RelocInfo::FULL_EMBEDDED_OBJECT) |
RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE) |
RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE) |
RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE_ENCODED) |
RelocInfo::ModeMask(RelocInfo::JS_DISPATCH_HANDLE) |
RelocInfo::ModeMask(RelocInfo::NEAR_BUILTIN_ENTRY) |
RelocInfo::ModeMask(RelocInfo::WASM_CALL) |
RelocInfo::ModeMask(RelocInfo::WASM_STUB_CALL)));
#if defined(V8_TARGET_ARCH_PPC64) || defined(V8_TARGET_ARCH_MIPS64)
return RelocIterator(*this, kModeMask).done();
#elif defined(V8_TARGET_ARCH_X64) || defined(V8_TARGET_ARCH_ARM64) || \
defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_S390X) || \
defined(V8_TARGET_ARCH_IA32) || defined(V8_TARGET_ARCH_RISCV64) || \
defined(V8_TARGET_ARCH_LOONG64) || defined(V8_TARGET_ARCH_RISCV32)
for (RelocIterator it(*this, kModeMask); !it.done(); it.next()) {
// On these platforms we emit relative builtin-to-builtin
// jumps for isolate independent builtins in the snapshot. They are later
// rewritten as pc-relative jumps to the off-heap instruction stream and are
// thus process-independent. See also: FinalizeEmbeddedCodeTargets.
if (RelocInfo::IsCodeTargetMode(it.rinfo()->rmode())) {
Address target_address = it.rinfo()->target_address();
if (OffHeapInstructionStream::PcIsOffHeap(isolate, target_address))
continue;
Tagged<Code> target = Code::FromTargetAddress(target_address);
if (Builtins::IsIsolateIndependentBuiltin(target)) {
continue;
}
}
return false;
}
return true;
#else
#error Unsupported architecture.
#endif
}
bool Code::Inlines(Tagged<SharedFunctionInfo> sfi) {
// We can only check for inlining for optimized code.
DCHECK(is_optimized_code());
DisallowGarbageCollection no_gc;
Tagged<DeoptimizationData> const data =
Cast<DeoptimizationData>(deoptimization_data());
if (data->length() == 0) return false;
if (data->GetSharedFunctionInfo() == sfi) return true;
Tagged<DeoptimizationLiteralArray> const literals = data->LiteralArray();
int const inlined_count = data->InlinedFunctionCount().value();
for (int i = 0; i < inlined_count; ++i) {
if (Cast<SharedFunctionInfo>(literals->get(i)) == sfi) return true;
}
return false;
}
#ifdef ENABLE_DISASSEMBLER
namespace {
void DisassembleCodeRange(Isolate* isolate, std::ostream& os, Tagged<Code> code,
Address begin, size_t size, Address current_pc,
size_t range_limit = 0) {
Address end = begin + size;
AllowHandleAllocation allow_handles;
DisallowGarbageCollection no_gc;
HandleScope handle_scope(isolate);
Disassembler::Decode(isolate, os, reinterpret_cast<uint8_t*>(begin),
reinterpret_cast<uint8_t*>(end),
CodeReference(handle(code, isolate)), current_pc,
range_limit);
}
void DisassembleOnlyCode(const char* name, std::ostream& os, Isolate* isolate,
Tagged<Code> code, Address current_pc,
size_t range_limit) {
int code_size = code->instruction_size();
DisassembleCodeRange(isolate, os, code, code->instruction_start(), code_size,
current_pc, range_limit);
}
void Disassemble(const char* name, std::ostream& os, Isolate* isolate,
Tagged<Code> code, Address current_pc) {
CodeKind kind = code->kind();
os << "kind = " << CodeKindToString(kind) << "\n";
if (name == nullptr && code->is_builtin()) {
name = Builtins::name(code->builtin_id());
}
if ((name != nullptr) && (name[0] != '\0')) {
os << "name = " << name << "\n";
}
os << "compiler = "
<< (code->is_turbofanned() ? "turbofan"
: code->is_maglevved() ? "maglev"
: kind == CodeKind::BASELINE ? "baseline"
: "unknown")
<< "\n";
os << "address = " << reinterpret_cast<void*>(code.ptr()) << "\n\n";
{
int code_size = code->instruction_size();
os << "Instructions (size = " << code_size << ")\n";
DisassembleCodeRange(isolate, os, code, code->instruction_start(),
code_size, current_pc);
if (int pool_size = code->constant_pool_size()) {
DCHECK_EQ(pool_size & kPointerAlignmentMask, 0);
os << "\nConstant Pool (size = " << pool_size << ")\n";
base::Vector<char> buf = base::Vector<char>::New(50);
intptr_t* ptr = reinterpret_cast<intptr_t*>(code->constant_pool());
for (int i = 0; i < pool_size; i += kSystemPointerSize, ptr++) {
SNPrintF(buf, "%4d %08" V8PRIxPTR, i, *ptr);
os << static_cast<const void*>(ptr) << " " << buf.begin() << "\n";
}
}
}
os << "\n";
// TODO(cbruni): add support for baseline code.
if (code->has_source_position_table()) {
{
SourcePositionTableIterator it(
code->source_position_table(),
SourcePositionTableIterator::kJavaScriptOnly);
if (!it.done()) {
os << "Source positions:\n pc offset position\n";
for (; !it.done(); it.Advance()) {
os << std::setw(10) << std::hex << it.code_offset() << std::dec
<< std::setw(10) << it.source_position().ScriptOffset()
<< (it.is_statement() ? " statement" : "") << "\n";
}
os << "\n";
}
}
{
SourcePositionTableIterator it(
code->source_position_table(),
SourcePositionTableIterator::kExternalOnly);
if (!it.done()) {
os << "External Source positions:\n pc offset fileid line\n";
for (; !it.done(); it.Advance()) {
DCHECK(it.source_position().IsExternal());
os << std::setw(10) << std::hex << it.code_offset() << std::dec
<< std::setw(10) << it.source_position().ExternalFileId()
<< std::setw(10) << it.source_position().ExternalLine() << "\n";
}
os << "\n";
}
}
}
if (code->uses_deoptimization_data()) {
Tagged<DeoptimizationData> data =
Cast<DeoptimizationData>(code->deoptimization_data());
data->PrintDeoptimizationData(os);
}
os << "\n";
if (code->uses_safepoint_table()) {
if (code->is_maglevved()) {
MaglevSafepointTable table(isolate, current_pc, code);
table.Print(os);
} else {
SafepointTable table(isolate, current_pc, code);
table.Print(os);
}
os << "\n";
}
if (code->has_handler_table()) {
HandlerTable table(code);
os << "Handler Table (size = " << table.NumberOfReturnEntries() << ")\n";
if (CodeKindIsOptimizedJSFunction(kind)) {
table.HandlerTableReturnPrint(os);
}
os << "\n";
}
os << "RelocInfo (size = " << code->relocation_size() << ")\n";
if (code->has_instruction_stream()) {
for (RelocIterator it(code); !it.done(); it.next()) {
it.rinfo()->Print(isolate, os);
}
}
os << "\n";
if (code->has_unwinding_info()) {
os << "UnwindingInfo (size = " << code->unwinding_info_size() << ")\n";
EhFrameDisassembler eh_frame_disassembler(
reinterpret_cast<uint8_t*>(code->unwinding_info_start()),
reinterpret_cast<uint8_t*>(code->unwinding_info_end()));
eh_frame_disassembler.DisassembleToStream(os);
os << "\n";
}
}
} // namespace
void Code::Disassemble(const char* name, std::ostream& os, Isolate* isolate,
Address current_pc) {
i::Disassemble(name, os, isolate, *this, current_pc);
}
void Code::DisassembleOnlyCode(const char* name, std::ostream& os,
Isolate* isolate, Address current_pc,
size_t range_limit) {
i::DisassembleOnlyCode(name, os, isolate, *this, current_pc, range_limit);
}
#endif // ENABLE_DISASSEMBLER
void Code::TraceMarkForDeoptimization(Isolate* isolate,
LazyDeoptimizeReason reason) {
Deoptimizer::TraceMarkForDeoptimization(isolate, *this, reason);
}
} // namespace internal
} // namespace v8