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