1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*
* Copyright (C) 2012-2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "DFGDisassembler.h"
#if ENABLE(DFG_JIT)
#include "CodeBlockWithJITType.h"
#include "DFGGraph.h"
#include "Disassembler.h"
#include "JSCJSValueInlines.h"
#include "LinkBuffer.h"
#include <wtf/TZoneMallocInlines.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC { namespace DFG {
WTF_MAKE_TZONE_ALLOCATED_IMPL(Disassembler);
Disassembler::Disassembler(Graph& graph)
: m_graph(graph)
{
m_dumpContext.graph = &m_graph;
m_labelForBlockIndex.grow(graph.numBlocks());
}
void Disassembler::dump(PrintStream& out, LinkBuffer& linkBuffer)
{
Vector<DumpedOp> ops = createDumpList(linkBuffer);
for (unsigned i = 0; i < ops.size(); ++i)
out.print(ops[i].text);
}
void Disassembler::dump(LinkBuffer& linkBuffer)
{
dump(WTF::dataFile(), linkBuffer);
}
void Disassembler::reportToProfiler(Profiler::Compilation* compilation, LinkBuffer& linkBuffer)
{
Vector<DumpedOp> ops = createDumpList(linkBuffer);
for (unsigned i = 0; i < ops.size(); ++i) {
Profiler::OriginStack stack;
if (ops[i].codeOrigin.isSet())
stack = Profiler::OriginStack(*m_graph.m_vm.m_perBytecodeProfiler, m_graph.m_codeBlock, ops[i].codeOrigin);
compilation->addDescription(Profiler::CompiledBytecode(stack, ops[i].text));
}
}
void Disassembler::dumpHeader(PrintStream& out, LinkBuffer& linkBuffer)
{
out.print("Generated DFG JIT code for ", CodeBlockWithJITType(m_graph.m_codeBlock, JITType::DFGJIT), ", instructions size = ", m_graph.m_codeBlock->instructionsSize(), ":\n");
out.print(" Optimized with execution counter = ", m_graph.m_profiledBlock->baselineExecuteCounter(), "\n");
out.print(" Code at [", RawPointer(linkBuffer.debugAddress()), ", ", RawPointer(static_cast<char*>(linkBuffer.debugAddress()) + linkBuffer.size()), "):\n");
}
void Disassembler::append(Vector<Disassembler::DumpedOp>& result, StringPrintStream& out, CodeOrigin& previousOrigin)
{
result.append(DumpedOp(previousOrigin, out.toCString()));
previousOrigin = CodeOrigin();
out.reset();
}
Vector<Disassembler::DumpedOp> Disassembler::createDumpList(LinkBuffer& linkBuffer)
{
StringPrintStream out;
Vector<DumpedOp> result;
CodeOrigin previousOrigin = CodeOrigin();
dumpHeader(out, linkBuffer);
append(result, out, previousOrigin);
m_graph.ensureCPSDominators();
m_graph.ensureCPSNaturalLoops();
const char* prefix = " ";
const char* disassemblyPrefix = " ";
Node* lastNode = nullptr;
MacroAssembler::Label previousLabel = m_startOfCode;
for (size_t blockIndex = 0; blockIndex < m_graph.numBlocks(); ++blockIndex) {
BasicBlock* block = m_graph.block(blockIndex);
if (!block)
continue;
dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, m_labelForBlockIndex[blockIndex], lastNode);
append(result, out, previousOrigin);
m_graph.dumpBlockHeader(out, prefix, block, Graph::DumpLivePhisOnly, &m_dumpContext);
append(result, out, previousOrigin);
Node* lastNodeForDisassembly = block->at(0);
for (size_t i = 0; i < block->size(); ++i) {
MacroAssembler::Label currentLabel;
UncheckedKeyHashMap<Node*, MacroAssembler::Label>::iterator iter = m_labelForNode.find(block->at(i));
if (iter != m_labelForNode.end())
currentLabel = iter->value;
else {
// Dump the last instruction by using the first label of the next block
// as the end point. This case is hit either during peephole compare
// optimizations (the Branch won't have its own label) or if we have a
// forced OSR exit.
if (blockIndex + 1 < m_graph.numBlocks())
currentLabel = m_labelForBlockIndex[blockIndex + 1];
else
currentLabel = m_endOfMainPath;
}
dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, currentLabel, lastNodeForDisassembly);
append(result, out, previousOrigin);
previousOrigin = block->at(i)->origin.semantic;
if (m_graph.dumpCodeOrigin(out, prefix, lastNode, block->at(i), &m_dumpContext)) {
append(result, out, previousOrigin);
previousOrigin = block->at(i)->origin.semantic;
}
m_graph.dump(out, prefix, block->at(i), &m_dumpContext);
lastNode = block->at(i);
lastNodeForDisassembly = block->at(i);
}
}
dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, m_endOfMainPath, lastNode);
append(result, out, previousOrigin);
out.print(prefix, "(End Of Main Path)\n");
append(result, out, previousOrigin);
dumpDisassembly(out, disassemblyPrefix, linkBuffer, previousLabel, m_endOfCode, nullptr);
append(result, out, previousOrigin);
m_dumpContext.dump(out, prefix);
append(result, out, previousOrigin);
return result;
}
void Disassembler::dumpDisassembly(PrintStream& out, const char* prefix, LinkBuffer& linkBuffer, MacroAssembler::Label& previousLabel, MacroAssembler::Label currentLabel, Node* context)
{
size_t prefixLength = strlen(prefix);
int amountOfNodeWhiteSpace;
if (!context)
amountOfNodeWhiteSpace = 0;
else
amountOfNodeWhiteSpace = Graph::amountOfNodeWhiteSpace(context);
Vector<char> prefixBuffer(prefixLength + amountOfNodeWhiteSpace + 1);
memcpy(prefixBuffer.data(), prefix, prefixLength);
for (int i = 0; i < amountOfNodeWhiteSpace; ++i)
prefixBuffer[i + prefixLength] = ' ';
prefixBuffer[prefixLength + amountOfNodeWhiteSpace] = 0;
void* codeStart = linkBuffer.entrypoint<DisassemblyPtrTag>().untaggedPtr();
void* codeEnd = std::bit_cast<uint8_t*>(codeStart) + linkBuffer.size();
CodeLocationLabel<DisassemblyPtrTag> start = linkBuffer.locationOf<DisassemblyPtrTag>(previousLabel);
CodeLocationLabel<DisassemblyPtrTag> end = linkBuffer.locationOf<DisassemblyPtrTag>(currentLabel);
previousLabel = currentLabel;
ASSERT(end.dataLocation<uintptr_t>() >= start.dataLocation<uintptr_t>());
disassemble(start, end.dataLocation<uintptr_t>() - start.dataLocation<uintptr_t>(), codeStart, codeEnd, prefixBuffer.data(), out);
}
} } // namespace JSC::DFG
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
#endif // ENABLE(DFG_JIT)
|