-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
BytecodeDumper.cpp
405 lines (351 loc) · 14.1 KB
/
BytecodeDumper.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
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
/*
* Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com>
* Copyright (C) 2017-2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "BytecodeDumper.h"
#include "BytecodeGenerator.h"
#include "BytecodeGraph.h"
#include "BytecodeStructs.h"
#include "CodeBlock.h"
#include "JSCJSValueInlines.h"
#include "UnlinkedCodeBlockGenerator.h"
#include "UnlinkedMetadataTableInlines.h"
#include "WasmFunctionCodeBlock.h"
#include "WasmGeneratorTraits.h"
#include "WasmModuleInformation.h"
#include "WasmOps.h"
#include "WasmSignatureInlines.h"
namespace JSC {
void BytecodeDumperBase::printLocationAndOp(InstructionStream::Offset location, const char* op)
{
m_currentLocation = location;
m_out.printf("[%4u] %-18s ", location, op);
}
void BytecodeDumperBase::dumpValue(VirtualRegister reg)
{
m_out.printf("%s", registerName(reg).data());
}
template<typename Traits>
void BytecodeDumperBase::dumpValue(GenericBoundLabel<Traits> label)
{
int target = label.target();
if (!target)
target = outOfLineJumpOffset(m_currentLocation);
InstructionStream::Offset targetOffset = target + m_currentLocation;
m_out.print(target, "(->", targetOffset, ")");
}
template void BytecodeDumperBase::dumpValue(GenericBoundLabel<JSGeneratorTraits>);
#if ENABLE(WEBASSEMBLY)
template void BytecodeDumperBase::dumpValue(GenericBoundLabel<Wasm::GeneratorTraits>);
#endif // ENABLE(WEBASSEMBLY)
template<class Block>
CString BytecodeDumper<Block>::registerName(VirtualRegister r) const
{
if (r.isConstant())
return constantName(r);
return toCString(r);
}
template <class Block>
int BytecodeDumper<Block>::outOfLineJumpOffset(InstructionStream::Offset offset) const
{
return m_block->outOfLineJumpOffset(offset);
}
template<class Block>
CString BytecodeDumper<Block>::constantName(VirtualRegister reg) const
{
auto value = block()->getConstant(reg);
return toCString(value, "(", reg, ")");
}
template<class Block>
void BytecodeDumper<Block>::dumpBytecode(const InstructionStream::Ref& it, const ICStatusMap&)
{
::JSC::dumpBytecode(this, it.offset(), it.ptr());
this->m_out.print("\n");
}
template<class Block>
void BytecodeDumper<Block>::dumpBytecode(Block* block, PrintStream& out, const InstructionStream::Ref& it, const ICStatusMap& statusMap)
{
BytecodeDumper dumper(block, out);
dumper.dumpBytecode(it, statusMap);
}
template<class Block>
VM& CodeBlockBytecodeDumper<Block>::vm() const
{
return this->block()->vm();
}
template<class Block>
const Identifier& CodeBlockBytecodeDumper<Block>::identifier(int index) const
{
return this->block()->identifier(index);
}
template<class Block>
void CodeBlockBytecodeDumper<Block>::dumpIdentifiers()
{
if (size_t count = this->block()->numberOfIdentifiers()) {
this->m_out.printf("\nIdentifiers:\n");
size_t i = 0;
do {
this->m_out.print(" id", static_cast<unsigned>(i), " = ", identifier(i), "\n");
++i;
} while (i != count);
}
}
template<class Block>
void CodeBlockBytecodeDumper<Block>::dumpConstants()
{
if (!this->block()->constantRegisters().isEmpty()) {
this->m_out.printf("\nConstants:\n");
size_t i = 0;
for (const auto& constant : this->block()->constantRegisters()) {
const char* sourceCodeRepresentationDescription = nullptr;
switch (this->block()->constantsSourceCodeRepresentation()[i]) {
case SourceCodeRepresentation::Double:
sourceCodeRepresentationDescription = ": in source as double";
break;
case SourceCodeRepresentation::Integer:
sourceCodeRepresentationDescription = ": in source as integer";
break;
case SourceCodeRepresentation::Other:
sourceCodeRepresentationDescription = "";
break;
case SourceCodeRepresentation::LinkTimeConstant:
sourceCodeRepresentationDescription = ": in source as link-time-constant";
break;
}
this->m_out.printf(" k%u = %s%s\n", static_cast<unsigned>(i), toCString(constant.get()).data(), sourceCodeRepresentationDescription);
++i;
}
}
}
template<class Block>
void CodeBlockBytecodeDumper<Block>::dumpExceptionHandlers()
{
if (unsigned count = this->block()->numberOfExceptionHandlers()) {
this->m_out.printf("\nException Handlers:\n");
unsigned i = 0;
do {
const auto& handler = this->block()->exceptionHandler(i);
this->m_out.printf("\t %d: { start: [%4d] end: [%4d] target: [%4d] } %s\n", i + 1, handler.start, handler.end, handler.target, handler.typeName());
++i;
} while (i < count);
}
}
template<class Block>
void CodeBlockBytecodeDumper<Block>::dumpSwitchJumpTables()
{
if (unsigned count = this->block()->numberOfSwitchJumpTables()) {
this->m_out.printf("Switch Jump Tables:\n");
unsigned i = 0;
do {
this->m_out.printf(" %1d = {\n", i);
const auto& switchJumpTable = this->block()->switchJumpTable(i);
int entry = 0;
auto end = switchJumpTable.branchOffsets.end();
for (auto iter = switchJumpTable.branchOffsets.begin(); iter != end; ++iter, ++entry) {
if (!*iter)
continue;
this->m_out.printf("\t\t%4d => %04d\n", entry + switchJumpTable.min, *iter);
}
this->m_out.printf(" }\n");
++i;
} while (i < count);
}
}
template<class Block>
void CodeBlockBytecodeDumper<Block>::dumpStringSwitchJumpTables()
{
if (unsigned count = this->block()->numberOfStringSwitchJumpTables()) {
this->m_out.printf("\nString Switch Jump Tables:\n");
unsigned i = 0;
do {
this->m_out.printf(" %1d = {\n", i);
const auto& stringSwitchJumpTable = this->block()->stringSwitchJumpTable(i);
auto end = stringSwitchJumpTable.offsetTable.end();
for (auto iter = stringSwitchJumpTable.offsetTable.begin(); iter != end; ++iter)
this->m_out.printf("\t\t\"%s\" => %04d\n", iter->key->utf8().data(), iter->value.branchOffset);
this->m_out.printf(" }\n");
++i;
} while (i < count);
}
}
template <typename Block>
static void dumpHeader(Block* block, const InstructionStream& instructions, PrintStream& out)
{
size_t instructionCount = 0;
size_t wide16InstructionCount = 0;
size_t wide32InstructionCount = 0;
size_t instructionWithMetadataCount = 0;
for (const auto& instruction : instructions) {
if (instruction->isWide16())
++wide16InstructionCount;
else if (instruction->isWide32())
++wide32InstructionCount;
if (instruction->hasMetadata())
++instructionWithMetadataCount;
++instructionCount;
}
out.print(*block);
out.printf(
": %lu instructions (%lu 16-bit instructions, %lu 32-bit instructions, %lu instructions with metadata); %lu bytes (%lu metadata bytes); %d parameter(s); %d callee register(s); %d variable(s)",
static_cast<unsigned long>(instructionCount),
static_cast<unsigned long>(wide16InstructionCount),
static_cast<unsigned long>(wide32InstructionCount),
static_cast<unsigned long>(instructionWithMetadataCount),
static_cast<unsigned long>(instructions.sizeInBytes() + block->metadataSizeInBytes()),
static_cast<unsigned long>(block->metadataSizeInBytes()),
block->numParameters(), block->numCalleeLocals(), block->numVars());
out.print("; scope at ", block->scopeRegister());
out.printf("\n");
}
template <typename Dumper>
static void dumpFooter(Dumper& dumper)
{
dumper.dumpIdentifiers();
dumper.dumpConstants();
dumper.dumpExceptionHandlers();
dumper.dumpSwitchJumpTables();
dumper.dumpStringSwitchJumpTables();
}
template<class Block>
void CodeBlockBytecodeDumper<Block>::dumpBlock(Block* block, const InstructionStream& instructions, PrintStream& out, const ICStatusMap& statusMap)
{
dumpHeader(block, instructions, out);
CodeBlockBytecodeDumper<Block> dumper(block, out);
for (const auto& it : instructions)
dumper.dumpBytecode(it, statusMap);
dumpFooter(dumper);
out.printf("\n");
}
template<class Block>
void CodeBlockBytecodeDumper<Block>::dumpGraph(Block* block, const InstructionStream& instructions, BytecodeGraph& graph, PrintStream& out, const ICStatusMap& icStatusMap)
{
dumpHeader(block, instructions, out);
CodeBlockBytecodeDumper<Block> dumper(block, out);
out.printf("\n");
for (BytecodeBasicBlock& block : graph) {
if (block.isEntryBlock() || block.isExitBlock())
continue;
out.print("bb#", block.index(), "\n");
for (unsigned i = 0; i < block.totalLength(); ) {
auto& currentInstruction = instructions.at(i + block.leaderOffset());
dumper.dumpBytecode(currentInstruction, icStatusMap);
i += currentInstruction.ptr()->size();
}
out.print("Successors: [");
for (unsigned successor : block.successors()) {
if (!graph[successor].isExitBlock())
out.print(" #", successor);
}
out.print(" ]\n\n");
}
dumpFooter(dumper);
out.printf("\n");
}
template class BytecodeDumper<CodeBlock>;
template class CodeBlockBytecodeDumper<UnlinkedCodeBlockGenerator>;
template class CodeBlockBytecodeDumper<CodeBlock>;
#if ENABLE(WEBASSEMBLY)
namespace Wasm {
void BytecodeDumper::dumpBlock(FunctionCodeBlock* block, const ModuleInformation& moduleInformation, PrintStream& out)
{
size_t instructionCount = 0;
size_t wide16InstructionCount = 0;
size_t wide32InstructionCount = 0;
for (auto it = block->instructions().begin(); it != block->instructions().end(); it += it->size<WasmOpcodeTraits>()) {
if (it->isWide16())
++wide16InstructionCount;
else if (it->isWide32())
++wide32InstructionCount;
++instructionCount;
}
size_t functionIndexSpace = moduleInformation.importFunctionCount() + block->functionIndex();
out.print(makeString(IndexOrName(functionIndexSpace, moduleInformation.nameSection->get(functionIndexSpace))));
const auto& function = moduleInformation.functions[block->functionIndex()];
SignatureIndex signatureIndex = moduleInformation.internalFunctionSignatureIndices[block->functionIndex()];
const Signature& signature = SignatureInformation::get(signatureIndex);
out.print(" : ", signature, "\n");
out.print("wasm size: ", function.data.size(), " bytes\n");
out.printf(
"bytecode: %lu instructions (%lu 16-bit instructions, %lu 32-bit instructions); %lu bytes; %d parameter(s); %d local(s); %d callee register(s)\n",
static_cast<unsigned long>(instructionCount),
static_cast<unsigned long>(wide16InstructionCount),
static_cast<unsigned long>(wide32InstructionCount),
static_cast<unsigned long>(block->instructions().sizeInBytes()),
block->numArguments(),
block->numVars(),
block->numCalleeLocals());
BytecodeDumper dumper(block, out);
for (auto it = block->instructions().begin(); it != block->instructions().end(); it += it->size<WasmOpcodeTraits>()) {
dumpWasm(&dumper, it.offset(), it.ptr());
out.print("\n");
}
dumper.dumpConstants();
out.printf("\n");
}
void BytecodeDumper::dumpConstants()
{
FunctionCodeBlock* block = this->block();
if (!block->constants().isEmpty()) {
this->m_out.printf("\nConstants:\n");
unsigned i = 0;
for (const auto& constant : block->constants()) {
Type type = block->constantTypes()[i];
this->m_out.print(" const", i, " : ", type, " = ", formatConstant(type, constant), "\n");
++i;
}
}
}
CString BytecodeDumper::constantName(VirtualRegister index) const
{
FunctionCodeBlock* block = this->block();
auto value = formatConstant(block->getConstantType(index), block->getConstant(index));
return toCString(value, "(", VirtualRegister(index), ")");
}
CString BytecodeDumper::formatConstant(Type type, uint64_t constant) const
{
switch (type) {
case Type::I32:
return toCString(static_cast<int32_t>(constant));
case Type::I64:
return toCString(constant);
case Type::F32:
return toCString(bitwise_cast<float>(static_cast<int32_t>(constant)));
break;
case Type::F64:
return toCString(bitwise_cast<double>(constant));
break;
case Type::Externref:
case Type::Funcref:
if (JSValue::decode(constant) == jsNull())
return "null";
return toCString(RawPointer(bitwise_cast<void*>(constant)));
default:
RELEASE_ASSERT_NOT_REACHED();
return "";
}
}
} // namespace Wasm
#endif // ENABLE(WEBASSEMBLY)
}