1 | /* |
2 | * Copyright (C) 2012-2018 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 "JIT.h" |
35 | #include "JSCInlines.h" |
36 | #include "LinkBuffer.h" |
37 | #include "ProfilerCompilation.h" |
38 | #include <wtf/StringPrintStream.h> |
39 | |
40 | namespace JSC { |
41 | |
42 | JITDisassembler::JITDisassembler(CodeBlock *codeBlock) |
43 | : m_codeBlock(codeBlock) |
44 | , m_labelForBytecodeIndexInMainPath(codeBlock->instructions().size()) |
45 | , m_labelForBytecodeIndexInSlowPath(codeBlock->instructions().size()) |
46 | { |
47 | } |
48 | |
49 | JITDisassembler::~JITDisassembler() |
50 | { |
51 | } |
52 | |
53 | void JITDisassembler::dump(PrintStream& out, LinkBuffer& linkBuffer) |
54 | { |
55 | dumpHeader(out, linkBuffer); |
56 | dumpDisassembly(out, linkBuffer, m_startOfCode, m_labelForBytecodeIndexInMainPath[0]); |
57 | |
58 | dumpForInstructions(out, linkBuffer, " " , m_labelForBytecodeIndexInMainPath, firstSlowLabel()); |
59 | out.print(" (End Of Main Path)\n" ); |
60 | dumpForInstructions(out, linkBuffer, " (S) " , m_labelForBytecodeIndexInSlowPath, m_endOfSlowPath); |
61 | out.print(" (End Of Slow Path)\n" ); |
62 | |
63 | dumpDisassembly(out, linkBuffer, m_endOfSlowPath, m_endOfCode); |
64 | } |
65 | |
66 | void JITDisassembler::dump(LinkBuffer& linkBuffer) |
67 | { |
68 | dump(WTF::dataFile(), linkBuffer); |
69 | } |
70 | |
71 | void JITDisassembler::reportToProfiler(Profiler::Compilation* compilation, LinkBuffer& linkBuffer) |
72 | { |
73 | StringPrintStream out; |
74 | |
75 | dumpHeader(out, linkBuffer); |
76 | compilation->addDescription(Profiler::CompiledBytecode(Profiler::OriginStack(), out.toCString())); |
77 | out.reset(); |
78 | dumpDisassembly(out, linkBuffer, m_startOfCode, m_labelForBytecodeIndexInMainPath[0]); |
79 | compilation->addDescription(Profiler::CompiledBytecode(Profiler::OriginStack(), out.toCString())); |
80 | |
81 | reportInstructions(compilation, linkBuffer, " " , m_labelForBytecodeIndexInMainPath, firstSlowLabel()); |
82 | compilation->addDescription(Profiler::CompiledBytecode(Profiler::OriginStack(), " (End Of Main Path)\n" )); |
83 | reportInstructions(compilation, linkBuffer, " (S) " , m_labelForBytecodeIndexInSlowPath, m_endOfSlowPath); |
84 | compilation->addDescription(Profiler::CompiledBytecode(Profiler::OriginStack(), " (End Of Slow Path)\n" )); |
85 | out.reset(); |
86 | dumpDisassembly(out, linkBuffer, m_endOfSlowPath, m_endOfCode); |
87 | compilation->addDescription(Profiler::CompiledBytecode(Profiler::OriginStack(), out.toCString())); |
88 | } |
89 | |
90 | void JITDisassembler::(PrintStream& out, LinkBuffer& linkBuffer) |
91 | { |
92 | out.print("Generated Baseline JIT code for " , CodeBlockWithJITType(m_codeBlock, JITType::BaselineJIT), ", instructions size = " , m_codeBlock->instructionsSize(), "\n" ); |
93 | out.print(" Source: " , m_codeBlock->sourceCodeOnOneLine(), "\n" ); |
94 | out.print(" Code at [" , RawPointer(linkBuffer.debugAddress()), ", " , RawPointer(static_cast<char*>(linkBuffer.debugAddress()) + linkBuffer.size()), "):\n" ); |
95 | } |
96 | |
97 | MacroAssembler::Label JITDisassembler::firstSlowLabel() |
98 | { |
99 | MacroAssembler::Label firstSlowLabel; |
100 | for (unsigned i = 0; i < m_labelForBytecodeIndexInSlowPath.size(); ++i) { |
101 | if (m_labelForBytecodeIndexInSlowPath[i].isSet()) { |
102 | firstSlowLabel = m_labelForBytecodeIndexInSlowPath[i]; |
103 | break; |
104 | } |
105 | } |
106 | return firstSlowLabel.isSet() ? firstSlowLabel : m_endOfSlowPath; |
107 | } |
108 | |
109 | Vector<JITDisassembler::DumpedOp> JITDisassembler::dumpVectorForInstructions(LinkBuffer& linkBuffer, const char* prefix, Vector<MacroAssembler::Label>& labels, MacroAssembler::Label endLabel) |
110 | { |
111 | StringPrintStream out; |
112 | Vector<DumpedOp> result; |
113 | |
114 | for (unsigned i = 0; i < labels.size();) { |
115 | if (!labels[i].isSet()) { |
116 | i++; |
117 | continue; |
118 | } |
119 | out.reset(); |
120 | result.append(DumpedOp()); |
121 | result.last().bytecodeIndex = BytecodeIndex(i); |
122 | out.print(prefix); |
123 | m_codeBlock->dumpBytecode(out, i); |
124 | for (unsigned nextIndex = i + 1; ; nextIndex++) { |
125 | if (nextIndex >= labels.size()) { |
126 | dumpDisassembly(out, linkBuffer, labels[i], endLabel); |
127 | result.last().disassembly = out.toCString(); |
128 | return result; |
129 | } |
130 | if (labels[nextIndex].isSet()) { |
131 | dumpDisassembly(out, linkBuffer, labels[i], labels[nextIndex]); |
132 | result.last().disassembly = out.toCString(); |
133 | i = nextIndex; |
134 | break; |
135 | } |
136 | } |
137 | } |
138 | |
139 | return result; |
140 | } |
141 | |
142 | void JITDisassembler::dumpForInstructions(PrintStream& out, LinkBuffer& linkBuffer, const char* prefix, Vector<MacroAssembler::Label>& labels, MacroAssembler::Label endLabel) |
143 | { |
144 | Vector<DumpedOp> dumpedOps = dumpVectorForInstructions(linkBuffer, prefix, labels, endLabel); |
145 | |
146 | for (unsigned i = 0; i < dumpedOps.size(); ++i) |
147 | out.print(dumpedOps[i].disassembly); |
148 | } |
149 | |
150 | void JITDisassembler::reportInstructions(Profiler::Compilation* compilation, LinkBuffer& linkBuffer, const char* prefix, Vector<MacroAssembler::Label>& labels, MacroAssembler::Label endLabel) |
151 | { |
152 | Vector<DumpedOp> dumpedOps = dumpVectorForInstructions(linkBuffer, prefix, labels, endLabel); |
153 | |
154 | for (unsigned i = 0; i < dumpedOps.size(); ++i) { |
155 | compilation->addDescription( |
156 | Profiler::CompiledBytecode( |
157 | Profiler::OriginStack(Profiler::Origin(compilation->bytecodes(), dumpedOps[i].bytecodeIndex)), |
158 | dumpedOps[i].disassembly)); |
159 | } |
160 | } |
161 | |
162 | void JITDisassembler::dumpDisassembly(PrintStream& out, LinkBuffer& linkBuffer, MacroAssembler::Label from, MacroAssembler::Label to) |
163 | { |
164 | CodeLocationLabel<DisassemblyPtrTag> fromLocation = linkBuffer.locationOf<DisassemblyPtrTag>(from); |
165 | CodeLocationLabel<DisassemblyPtrTag> toLocation = linkBuffer.locationOf<DisassemblyPtrTag>(to); |
166 | disassemble(fromLocation, toLocation.dataLocation<uintptr_t>() - fromLocation.dataLocation<uintptr_t>(), " " , out); |
167 | } |
168 | |
169 | } // namespace JSC |
170 | |
171 | #endif // ENABLE(JIT) |
172 | |
173 | |