1 | /*
|
---|
2 | * Copyright (C) 2012-2022 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
---|
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
---|
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "config.h"
|
---|
27 | #include "JITDisassembler.h"
|
---|
28 |
|
---|
29 | #if ENABLE(JIT)
|
---|
30 |
|
---|
31 | #include "CodeBlock.h"
|
---|
32 | #include "CodeBlockWithJITType.h"
|
---|
33 | #include "Disassembler.h"
|
---|
34 | #include "JSCellInlines.h"
|
---|
35 | #include "LinkBuffer.h"
|
---|
36 | #include "ProfilerCompilation.h"
|
---|
37 | #include <wtf/StringPrintStream.h>
|
---|
38 |
|
---|
39 | namespace JSC {
|
---|
40 |
|
---|
41 | JITDisassembler::JITDisassembler(CodeBlock *codeBlock)
|
---|
42 | : m_codeBlock(codeBlock)
|
---|
43 | , m_labelForBytecodeIndexInMainPath(codeBlock->instructions().size())
|
---|
44 | , m_labelForBytecodeIndexInSlowPath(codeBlock->instructions().size())
|
---|
45 | {
|
---|
46 | }
|
---|
47 |
|
---|
48 | JITDisassembler::~JITDisassembler()
|
---|
49 | {
|
---|
50 | }
|
---|
51 |
|
---|
52 | void JITDisassembler::dump(PrintStream& out, LinkBuffer& linkBuffer)
|
---|
53 | {
|
---|
54 | m_codeStart = linkBuffer.entrypoint<DisassemblyPtrTag>().untaggedExecutableAddress();
|
---|
55 | m_codeEnd = bitwise_cast<uint8_t*>(m_codeStart) + linkBuffer.size();
|
---|
56 |
|
---|
57 | dumpHeader(out, linkBuffer);
|
---|
58 | dumpDisassembly(out, linkBuffer, m_startOfCode, m_labelForBytecodeIndexInMainPath[0]);
|
---|
59 |
|
---|
60 | dumpForInstructions(out, linkBuffer, " ", m_labelForBytecodeIndexInMainPath, firstSlowLabel());
|
---|
61 | out.print(" (End Of Main Path)\n");
|
---|
62 | dumpForInstructions(out, linkBuffer, " (S) ", m_labelForBytecodeIndexInSlowPath, m_endOfSlowPath);
|
---|
63 | out.print(" (End Of Slow Path)\n");
|
---|
64 |
|
---|
65 | dumpDisassembly(out, linkBuffer, m_endOfSlowPath, m_endOfCode);
|
---|
66 | }
|
---|
67 |
|
---|
68 | void JITDisassembler::dump(LinkBuffer& linkBuffer)
|
---|
69 | {
|
---|
70 | dump(WTF::dataFile(), linkBuffer);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void JITDisassembler::reportToProfiler(Profiler::Compilation* compilation, LinkBuffer& linkBuffer)
|
---|
74 | {
|
---|
75 | StringPrintStream out;
|
---|
76 |
|
---|
77 | dumpHeader(out, linkBuffer);
|
---|
78 | compilation->addDescription(Profiler::CompiledBytecode(Profiler::OriginStack(), out.toCString()));
|
---|
79 | out.reset();
|
---|
80 | dumpDisassembly(out, linkBuffer, m_startOfCode, m_labelForBytecodeIndexInMainPath[0]);
|
---|
81 | compilation->addDescription(Profiler::CompiledBytecode(Profiler::OriginStack(), out.toCString()));
|
---|
82 |
|
---|
83 | reportInstructions(compilation, linkBuffer, " ", m_labelForBytecodeIndexInMainPath, firstSlowLabel());
|
---|
84 | compilation->addDescription(Profiler::CompiledBytecode(Profiler::OriginStack(), " (End Of Main Path)\n"));
|
---|
85 | reportInstructions(compilation, linkBuffer, " (S) ", m_labelForBytecodeIndexInSlowPath, m_endOfSlowPath);
|
---|
86 | compilation->addDescription(Profiler::CompiledBytecode(Profiler::OriginStack(), " (End Of Slow Path)\n"));
|
---|
87 | out.reset();
|
---|
88 | dumpDisassembly(out, linkBuffer, m_endOfSlowPath, m_endOfCode);
|
---|
89 | compilation->addDescription(Profiler::CompiledBytecode(Profiler::OriginStack(), out.toCString()));
|
---|
90 | }
|
---|
91 |
|
---|
92 | void JITDisassembler::dumpHeader(PrintStream& out, LinkBuffer& linkBuffer)
|
---|
93 | {
|
---|
94 | out.print("Generated Baseline JIT code for ", CodeBlockWithJITType(m_codeBlock, JITType::BaselineJIT), ", instructions size = ", m_codeBlock->instructionsSize(), "\n");
|
---|
95 | out.print(" Source: ", m_codeBlock->sourceCodeOnOneLine(), "\n");
|
---|
96 | out.print(" Code at [", RawPointer(linkBuffer.debugAddress()), ", ", RawPointer(static_cast<char*>(linkBuffer.debugAddress()) + linkBuffer.size()), "):\n");
|
---|
97 | }
|
---|
98 |
|
---|
99 | MacroAssembler::Label JITDisassembler::firstSlowLabel()
|
---|
100 | {
|
---|
101 | MacroAssembler::Label firstSlowLabel;
|
---|
102 | for (unsigned i = 0; i < m_labelForBytecodeIndexInSlowPath.size(); ++i) {
|
---|
103 | if (m_labelForBytecodeIndexInSlowPath[i].isSet()) {
|
---|
104 | firstSlowLabel = m_labelForBytecodeIndexInSlowPath[i];
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | return firstSlowLabel.isSet() ? firstSlowLabel : m_endOfSlowPath;
|
---|
109 | }
|
---|
110 |
|
---|
111 | Vector<JITDisassembler::DumpedOp> JITDisassembler::dumpVectorForInstructions(LinkBuffer& linkBuffer, const char* prefix, Vector<MacroAssembler::Label>& labels, MacroAssembler::Label endLabel)
|
---|
112 | {
|
---|
113 | StringPrintStream out;
|
---|
114 | Vector<DumpedOp> result;
|
---|
115 |
|
---|
116 | for (unsigned i = 0; i < labels.size();) {
|
---|
117 | if (!labels[i].isSet()) {
|
---|
118 | i++;
|
---|
119 | continue;
|
---|
120 | }
|
---|
121 | out.reset();
|
---|
122 | result.append(DumpedOp());
|
---|
123 | result.last().bytecodeIndex = BytecodeIndex(i);
|
---|
124 | out.print(prefix);
|
---|
125 | m_codeBlock->dumpBytecode(out, i);
|
---|
126 | for (unsigned nextIndex = i + 1; ; nextIndex++) {
|
---|
127 | if (nextIndex >= labels.size()) {
|
---|
128 | dumpDisassembly(out, linkBuffer, labels[i], endLabel);
|
---|
129 | result.last().disassembly = out.toCString();
|
---|
130 | return result;
|
---|
131 | }
|
---|
132 | if (labels[nextIndex].isSet()) {
|
---|
133 | dumpDisassembly(out, linkBuffer, labels[i], labels[nextIndex]);
|
---|
134 | result.last().disassembly = out.toCString();
|
---|
135 | i = nextIndex;
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | return result;
|
---|
142 | }
|
---|
143 |
|
---|
144 | void JITDisassembler::dumpForInstructions(PrintStream& out, LinkBuffer& linkBuffer, const char* prefix, Vector<MacroAssembler::Label>& labels, MacroAssembler::Label endLabel)
|
---|
145 | {
|
---|
146 | Vector<DumpedOp> dumpedOps = dumpVectorForInstructions(linkBuffer, prefix, labels, endLabel);
|
---|
147 |
|
---|
148 | for (unsigned i = 0; i < dumpedOps.size(); ++i)
|
---|
149 | out.print(dumpedOps[i].disassembly);
|
---|
150 | }
|
---|
151 |
|
---|
152 | void JITDisassembler::reportInstructions(Profiler::Compilation* compilation, LinkBuffer& linkBuffer, const char* prefix, Vector<MacroAssembler::Label>& labels, MacroAssembler::Label endLabel)
|
---|
153 | {
|
---|
154 | Vector<DumpedOp> dumpedOps = dumpVectorForInstructions(linkBuffer, prefix, labels, endLabel);
|
---|
155 |
|
---|
156 | for (unsigned i = 0; i < dumpedOps.size(); ++i) {
|
---|
157 | compilation->addDescription(
|
---|
158 | Profiler::CompiledBytecode(
|
---|
159 | Profiler::OriginStack(Profiler::Origin(compilation->bytecodes(), dumpedOps[i].bytecodeIndex)),
|
---|
160 | dumpedOps[i].disassembly));
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | void JITDisassembler::dumpDisassembly(PrintStream& out, LinkBuffer& linkBuffer, MacroAssembler::Label from, MacroAssembler::Label to)
|
---|
165 | {
|
---|
166 | CodeLocationLabel<DisassemblyPtrTag> fromLocation = linkBuffer.locationOf<DisassemblyPtrTag>(from);
|
---|
167 | CodeLocationLabel<DisassemblyPtrTag> toLocation = linkBuffer.locationOf<DisassemblyPtrTag>(to);
|
---|
168 | disassemble(fromLocation, toLocation.dataLocation<uintptr_t>() - fromLocation.dataLocation<uintptr_t>(), m_codeStart, m_codeEnd, " ", out);
|
---|
169 | }
|
---|
170 |
|
---|
171 | } // namespace JSC
|
---|
172 |
|
---|
173 | #endif // ENABLE(JIT)
|
---|
174 |
|
---|