JITDisassembler.cpp 7.0 KB

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