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 "DFGDisassembler.h" |
28 | |
29 | #if ENABLE(DFG_JIT) |
30 | |
31 | #include "CodeBlockWithJITType.h" |
32 | #include "DFGGraph.h" |
33 | #include "DFGJITCode.h" |
34 | #include "Disassembler.h" |
35 | #include "JSCInlines.h" |
36 | #include "LinkBuffer.h" |
37 | #include "ProfilerDatabase.h" |
38 | #include <wtf/StdLibExtras.h> |
39 | |
40 | namespace JSC { namespace DFG { |
41 | |
42 | Disassembler::Disassembler(Graph& graph) |
43 | : m_graph(graph) |
44 | { |
45 | m_dumpContext.graph = &m_graph; |
46 | m_labelForBlockIndex.grow(graph.numBlocks()); |
47 | } |
48 | |
49 | void Disassembler::dump(PrintStream& out, LinkBuffer& linkBuffer) |
50 | { |
51 | Vector<DumpedOp> ops = createDumpList(linkBuffer); |
52 | for (unsigned i = 0; i < ops.size(); ++i) |
53 | out.print(ops[i].text); |
54 | } |
55 | |
56 | void Disassembler::dump(LinkBuffer& linkBuffer) |
57 | { |
58 | dump(WTF::dataFile(), linkBuffer); |
59 | } |
60 | |
61 | void Disassembler::reportToProfiler(Profiler::Compilation* compilation, LinkBuffer& linkBuffer) |
62 | { |
63 | Vector<DumpedOp> ops = createDumpList(linkBuffer); |
64 | |
65 | for (unsigned i = 0; i < ops.size(); ++i) { |
66 | Profiler::OriginStack stack; |
67 | |
68 | if (ops[i].codeOrigin.isSet()) |
69 | stack = Profiler::OriginStack(*m_graph.m_vm.m_perBytecodeProfiler, m_graph.m_codeBlock, ops[i].codeOrigin); |
70 | |
71 | compilation->addDescription(Profiler::CompiledBytecode(stack, ops[i].text)); |
72 | } |
73 | } |
74 | |
75 | void Disassembler::(PrintStream& out, LinkBuffer& linkBuffer) |
76 | { |
77 | out.print("Generated DFG JIT code for " , CodeBlockWithJITType(m_graph.m_codeBlock, JITType::DFGJIT), ", instructions size = " , m_graph.m_codeBlock->instructionsSize(), ":\n" ); |
78 | out.print(" Optimized with execution counter = " , m_graph.m_profiledBlock->jitExecuteCounter(), "\n" ); |
79 | out.print(" Code at [" , RawPointer(linkBuffer.debugAddress()), ", " , RawPointer(static_cast<char*>(linkBuffer.debugAddress()) + linkBuffer.size()), "):\n" ); |
80 | } |
81 | |
82 | void Disassembler::append(Vector<Disassembler::DumpedOp>& result, StringPrintStream& out, CodeOrigin& previousOrigin) |
83 | { |
84 | result.append(DumpedOp(previousOrigin, out.toCString())); |
85 | previousOrigin = CodeOrigin(); |
86 | out.reset(); |
87 | } |
88 | |
89 | Vector<Disassembler::DumpedOp> Disassembler::createDumpList(LinkBuffer& linkBuffer) |
90 | { |
91 | StringPrintStream out; |
92 | Vector<DumpedOp> result; |
93 | |
94 | CodeOrigin previousOrigin = CodeOrigin(); |
95 | dumpHeader(out, linkBuffer); |
96 | append(result, out, previousOrigin); |
97 | |
98 | m_graph.ensureCPSDominators(); |
99 | m_graph.ensureCPSNaturalLoops(); |
100 | |
101 | const char* prefix = " " ; |
102 | const char* disassemblyPrefix = " " ; |
103 | |
104 | Node* lastNode = 0; |
105 | MacroAssembler::Label previousLabel = m_startOfCode; |
106 | for (size_t blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex) { |
107 | BasicBlock* block = m_graph.block(blockIndex); |
108 | if (!block) |
109 | continue; |
110 | dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, m_labelForBlockIndex[blockIndex], lastNode); |
111 | append(result, out, previousOrigin); |
112 | m_graph.dumpBlockHeader(out, prefix, block, Graph::DumpLivePhisOnly, &m_dumpContext); |
113 | append(result, out, previousOrigin); |
114 | Node* lastNodeForDisassembly = block->at(0); |
115 | for (size_t i = 0; i < block->size(); ++i) { |
116 | MacroAssembler::Label currentLabel; |
117 | HashMap<Node*, MacroAssembler::Label>::iterator iter = m_labelForNode.find(block->at(i)); |
118 | if (iter != m_labelForNode.end()) |
119 | currentLabel = iter->value; |
120 | else { |
121 | // Dump the last instruction by using the first label of the next block |
122 | // as the end point. This case is hit either during peephole compare |
123 | // optimizations (the Branch won't have its own label) or if we have a |
124 | // forced OSR exit. |
125 | if (blockIndex + 1 < m_graph.numBlocks()) |
126 | currentLabel = m_labelForBlockIndex[blockIndex + 1]; |
127 | else |
128 | currentLabel = m_endOfMainPath; |
129 | } |
130 | dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, currentLabel, lastNodeForDisassembly); |
131 | append(result, out, previousOrigin); |
132 | previousOrigin = block->at(i)->origin.semantic; |
133 | if (m_graph.dumpCodeOrigin(out, prefix, lastNode, block->at(i), &m_dumpContext)) { |
134 | append(result, out, previousOrigin); |
135 | previousOrigin = block->at(i)->origin.semantic; |
136 | } |
137 | m_graph.dump(out, prefix, block->at(i), &m_dumpContext); |
138 | lastNode = block->at(i); |
139 | lastNodeForDisassembly = block->at(i); |
140 | } |
141 | } |
142 | dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, m_endOfMainPath, lastNode); |
143 | append(result, out, previousOrigin); |
144 | out.print(prefix, "(End Of Main Path)\n" ); |
145 | append(result, out, previousOrigin); |
146 | dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, m_endOfCode, 0); |
147 | append(result, out, previousOrigin); |
148 | m_dumpContext.dump(out, prefix); |
149 | append(result, out, previousOrigin); |
150 | |
151 | return result; |
152 | } |
153 | |
154 | void Disassembler::dumpDisassembly(PrintStream& out, const char* prefix, LinkBuffer& linkBuffer, MacroAssembler::Label& previousLabel, MacroAssembler::Label currentLabel, Node* context) |
155 | { |
156 | size_t prefixLength = strlen(prefix); |
157 | int amountOfNodeWhiteSpace; |
158 | if (!context) |
159 | amountOfNodeWhiteSpace = 0; |
160 | else |
161 | amountOfNodeWhiteSpace = Graph::amountOfNodeWhiteSpace(context); |
162 | Vector<char> prefixBuffer(prefixLength + amountOfNodeWhiteSpace + 1); |
163 | memcpy(prefixBuffer.data(), prefix, prefixLength); |
164 | for (int i = 0; i < amountOfNodeWhiteSpace; ++i) |
165 | prefixBuffer[i + prefixLength] = ' '; |
166 | prefixBuffer[prefixLength + amountOfNodeWhiteSpace] = 0; |
167 | |
168 | CodeLocationLabel<DisassemblyPtrTag> start = linkBuffer.locationOf<DisassemblyPtrTag>(previousLabel); |
169 | CodeLocationLabel<DisassemblyPtrTag> end = linkBuffer.locationOf<DisassemblyPtrTag>(currentLabel); |
170 | previousLabel = currentLabel; |
171 | ASSERT(end.dataLocation<uintptr_t>() >= start.dataLocation<uintptr_t>()); |
172 | disassemble(start, end.dataLocation<uintptr_t>() - start.dataLocation<uintptr_t>(), prefixBuffer.data(), out); |
173 | } |
174 | |
175 | } } // namespace JSC::DFG |
176 | |
177 | #endif // ENABLE(DFG_JIT) |
178 | |