DFGDisassembler.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2012, 2013 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. #include "config.h"
  26. #if !ENABLE(DETACHED_JIT)
  27. #include "DFGDisassembler.h"
  28. #if ENABLE(DFG_JIT)
  29. #include "CodeBlockWithJITType.h"
  30. #include "DFGGraph.h"
  31. namespace JSC { namespace DFG {
  32. Disassembler::Disassembler(Graph& graph)
  33. : m_graph(graph)
  34. {
  35. m_labelForBlockIndex.resize(graph.m_blocks.size());
  36. }
  37. void Disassembler::dump(PrintStream& out, LinkBuffer& linkBuffer)
  38. {
  39. Vector<DumpedOp> ops = createDumpList(linkBuffer);
  40. for (unsigned i = 0; i < ops.size(); ++i)
  41. out.print(ops[i].text);
  42. }
  43. void Disassembler::dump(LinkBuffer& linkBuffer)
  44. {
  45. dump(WTF::dataFile(), linkBuffer);
  46. }
  47. void Disassembler::reportToProfiler(Profiler::Compilation* compilation, LinkBuffer& linkBuffer)
  48. {
  49. Vector<DumpedOp> ops = createDumpList(linkBuffer);
  50. for (unsigned i = 0; i < ops.size(); ++i) {
  51. Profiler::OriginStack stack;
  52. #if ENABLE(DETACHED_JIT)
  53. #pragma message "[SECURE JSCORE] profiler disabled"
  54. RELEASE_ASSERT_NOT_REACHED();
  55. #else
  56. if (ops[i].codeOrigin.isSet())
  57. stack = Profiler::OriginStack(*m_graph.m_vm.m_perBytecodeProfiler, m_graph.m_codeBlock, ops[i].codeOrigin);
  58. #endif
  59. compilation->addDescription(Profiler::CompiledBytecode(stack, ops[i].text));
  60. }
  61. }
  62. void Disassembler::dumpHeader(PrintStream& out, LinkBuffer& linkBuffer)
  63. {
  64. out.print("Generated DFG JIT code for ", CodeBlockWithJITType(m_graph.m_codeBlock, JITCode::DFGJIT), ", instruction count = ", m_graph.m_codeBlock->instructionCount(), ":\n");
  65. out.print(" Optimized with execution counter = ", m_graph.m_profiledBlock->jitExecuteCounter(), "\n");
  66. out.print(" Source: ", m_graph.m_codeBlock->sourceCodeOnOneLine(), "\n");
  67. out.print(" Code at [", RawPointer(linkBuffer.debugAddress()), ", ", RawPointer(static_cast<char*>(linkBuffer.debugAddress()) + linkBuffer.debugSize()), "):\n");
  68. }
  69. void Disassembler::append(Vector<Disassembler::DumpedOp>& result, StringPrintStream& out, CodeOrigin& previousOrigin)
  70. {
  71. result.append(DumpedOp(previousOrigin, out.toCString()));
  72. previousOrigin = CodeOrigin();
  73. out.reset();
  74. }
  75. Vector<Disassembler::DumpedOp> Disassembler::createDumpList(LinkBuffer& linkBuffer)
  76. {
  77. StringPrintStream out;
  78. Vector<DumpedOp> result;
  79. CodeOrigin previousOrigin = CodeOrigin();
  80. dumpHeader(out, linkBuffer);
  81. append(result, out, previousOrigin);
  82. m_graph.m_dominators.computeIfNecessary(m_graph);
  83. const char* prefix = " ";
  84. const char* disassemblyPrefix = " ";
  85. Node* lastNode = 0;
  86. MacroAssembler::Label previousLabel = m_startOfCode;
  87. for (size_t blockIndex = 0; blockIndex < m_graph.m_blocks.size(); ++blockIndex) {
  88. BasicBlock* block = m_graph.m_blocks[blockIndex].get();
  89. if (!block)
  90. continue;
  91. dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, m_labelForBlockIndex[blockIndex], lastNode);
  92. append(result, out, previousOrigin);
  93. m_graph.dumpBlockHeader(out, prefix, blockIndex, Graph::DumpLivePhisOnly);
  94. append(result, out, previousOrigin);
  95. Node* lastNodeForDisassembly = block->at(0);
  96. for (size_t i = 0; i < block->size(); ++i) {
  97. if (!block->at(i)->willHaveCodeGenOrOSR() && !Options::showAllDFGNodes())
  98. continue;
  99. MacroAssembler::Label currentLabel;
  100. HashMap<Node*, MacroAssembler::Label>::iterator iter = m_labelForNode.find(block->at(i));
  101. if (iter != m_labelForNode.end())
  102. currentLabel = iter->value;
  103. else {
  104. // Dump the last instruction by using the first label of the next block
  105. // as the end point. This case is hit either during peephole compare
  106. // optimizations (the Branch won't have its own label) or if we have a
  107. // forced OSR exit.
  108. if (blockIndex + 1 < m_graph.m_blocks.size())
  109. currentLabel = m_labelForBlockIndex[blockIndex + 1];
  110. else
  111. currentLabel = m_endOfMainPath;
  112. }
  113. dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, currentLabel, lastNodeForDisassembly);
  114. append(result, out, previousOrigin);
  115. previousOrigin = block->at(i)->codeOrigin;
  116. if (m_graph.dumpCodeOrigin(out, prefix, lastNode, block->at(i))) {
  117. append(result, out, previousOrigin);
  118. previousOrigin = block->at(i)->codeOrigin;
  119. }
  120. m_graph.dump(out, prefix, block->at(i));
  121. lastNode = block->at(i);
  122. lastNodeForDisassembly = block->at(i);
  123. }
  124. }
  125. dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, m_endOfMainPath, lastNode);
  126. append(result, out, previousOrigin);
  127. out.print(prefix, "(End Of Main Path)\n");
  128. append(result, out, previousOrigin);
  129. dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, m_endOfCode, 0);
  130. append(result, out, previousOrigin);
  131. return result;
  132. }
  133. void Disassembler::dumpDisassembly(PrintStream& out, const char* prefix, LinkBuffer& linkBuffer, MacroAssembler::Label& previousLabel, MacroAssembler::Label currentLabel, Node* context)
  134. {
  135. size_t prefixLength = strlen(prefix);
  136. int amountOfNodeWhiteSpace;
  137. if (!context)
  138. amountOfNodeWhiteSpace = 0;
  139. else
  140. amountOfNodeWhiteSpace = Graph::amountOfNodeWhiteSpace(context);
  141. OwnArrayPtr<char> prefixBuffer = adoptArrayPtr(new char[prefixLength + amountOfNodeWhiteSpace + 1]);
  142. strcpy(prefixBuffer.get(), prefix);
  143. for (int i = 0; i < amountOfNodeWhiteSpace; ++i)
  144. prefixBuffer[i + prefixLength] = ' ';
  145. prefixBuffer[prefixLength + amountOfNodeWhiteSpace] = 0;
  146. CodeLocationLabel start = linkBuffer.locationOf(previousLabel);
  147. CodeLocationLabel end = linkBuffer.locationOf(currentLabel);
  148. previousLabel = currentLabel;
  149. ASSERT(bitwise_cast<uintptr_t>(end.executableAddress()) >= bitwise_cast<uintptr_t>(start.executableAddress()));
  150. disassemble(start, bitwise_cast<uintptr_t>(end.executableAddress()) - bitwise_cast<uintptr_t>(start.executableAddress()), prefixBuffer.get(), out);
  151. }
  152. } } // namespace JSC::DFG
  153. #endif // ENABLE(DFG_JIT)
  154. #endif // #if !ENABLE(DETACHED_JIT)