| 1 | /* |
| 2 | * Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com> |
| 3 | * Copyright (C) 2017-2018 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "BytecodeDumper.h" |
| 29 | |
| 30 | #include "ArithProfile.h" |
| 31 | #include "BytecodeStructs.h" |
| 32 | #include "CallLinkStatus.h" |
| 33 | #include "CodeBlock.h" |
| 34 | #include "Error.h" |
| 35 | #include "HeapInlines.h" |
| 36 | #include "InterpreterInlines.h" |
| 37 | #include "PolymorphicAccess.h" |
| 38 | #include "PutByIdFlags.h" |
| 39 | #include "StructureInlines.h" |
| 40 | #include "ToThisStatus.h" |
| 41 | #include "UnlinkedCodeBlock.h" |
| 42 | #include "UnlinkedMetadataTableInlines.h" |
| 43 | |
| 44 | namespace JSC { |
| 45 | |
| 46 | template<class Block> |
| 47 | VM* BytecodeDumper<Block>::vm() const |
| 48 | { |
| 49 | return block()->vm(); |
| 50 | } |
| 51 | |
| 52 | template<class Block> |
| 53 | const Identifier& BytecodeDumper<Block>::identifier(int index) const |
| 54 | { |
| 55 | return block()->identifier(index); |
| 56 | } |
| 57 | |
| 58 | static ALWAYS_INLINE bool isConstantRegisterIndex(int index) |
| 59 | { |
| 60 | return index >= FirstConstantRegisterIndex; |
| 61 | } |
| 62 | |
| 63 | template<class Block> |
| 64 | CString BytecodeDumper<Block>::registerName(int r) const |
| 65 | { |
| 66 | if (isConstantRegisterIndex(r)) |
| 67 | return constantName(r); |
| 68 | |
| 69 | return toCString(VirtualRegister(r)); |
| 70 | } |
| 71 | |
| 72 | template<class Block> |
| 73 | CString BytecodeDumper<Block>::constantName(int index) const |
| 74 | { |
| 75 | JSValue value = block()->getConstant(index); |
| 76 | return toCString(value, "(" , VirtualRegister(index), ")" ); |
| 77 | } |
| 78 | |
| 79 | template<class Block> |
| 80 | void BytecodeDumper<Block>::printLocationAndOp(InstructionStream::Offset location, const char* op) |
| 81 | { |
| 82 | m_currentLocation = location; |
| 83 | m_out.printf("[%4u] %-18s " , location, op); |
| 84 | } |
| 85 | |
| 86 | template<class Block> |
| 87 | void BytecodeDumper<Block>::dumpBytecode(const InstructionStream::Ref& it, const ICStatusMap&) |
| 88 | { |
| 89 | ::JSC::dumpBytecode(this, it.offset(), it.ptr()); |
| 90 | m_out.print("\n" ); |
| 91 | } |
| 92 | |
| 93 | template<class Block> |
| 94 | void BytecodeDumper<Block>::dumpBytecode(Block* block, PrintStream& out, const InstructionStream::Ref& it, const ICStatusMap& statusMap) |
| 95 | { |
| 96 | BytecodeDumper dumper(block, out); |
| 97 | dumper.dumpBytecode(it, statusMap); |
| 98 | } |
| 99 | |
| 100 | template<class Block> |
| 101 | void BytecodeDumper<Block>::dumpIdentifiers() |
| 102 | { |
| 103 | if (size_t count = block()->numberOfIdentifiers()) { |
| 104 | m_out.printf("\nIdentifiers:\n" ); |
| 105 | size_t i = 0; |
| 106 | do { |
| 107 | m_out.print(" id" , static_cast<unsigned>(i), " = " , identifier(i), "\n" ); |
| 108 | ++i; |
| 109 | } while (i != count); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | template<class Block> |
| 114 | void BytecodeDumper<Block>::dumpConstants() |
| 115 | { |
| 116 | if (!block()->constantRegisters().isEmpty()) { |
| 117 | m_out.printf("\nConstants:\n" ); |
| 118 | size_t i = 0; |
| 119 | for (const auto& constant : block()->constantRegisters()) { |
| 120 | const char* sourceCodeRepresentationDescription = nullptr; |
| 121 | switch (block()->constantsSourceCodeRepresentation()[i]) { |
| 122 | case SourceCodeRepresentation::Double: |
| 123 | sourceCodeRepresentationDescription = ": in source as double" ; |
| 124 | break; |
| 125 | case SourceCodeRepresentation::Integer: |
| 126 | sourceCodeRepresentationDescription = ": in source as integer" ; |
| 127 | break; |
| 128 | case SourceCodeRepresentation::Other: |
| 129 | sourceCodeRepresentationDescription = "" ; |
| 130 | break; |
| 131 | } |
| 132 | m_out.printf(" k%u = %s%s\n" , static_cast<unsigned>(i), toCString(constant.get()).data(), sourceCodeRepresentationDescription); |
| 133 | ++i; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | template<class Block> |
| 139 | void BytecodeDumper<Block>::dumpExceptionHandlers() |
| 140 | { |
| 141 | if (unsigned count = block()->numberOfExceptionHandlers()) { |
| 142 | m_out.printf("\nException Handlers:\n" ); |
| 143 | unsigned i = 0; |
| 144 | do { |
| 145 | const auto& handler = block()->exceptionHandler(i); |
| 146 | m_out.printf("\t %d: { start: [%4d] end: [%4d] target: [%4d] } %s\n" , i + 1, handler.start, handler.end, handler.target, handler.typeName()); |
| 147 | ++i; |
| 148 | } while (i < count); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | template<class Block> |
| 153 | void BytecodeDumper<Block>::dumpSwitchJumpTables() |
| 154 | { |
| 155 | if (unsigned count = block()->numberOfSwitchJumpTables()) { |
| 156 | m_out.printf("Switch Jump Tables:\n" ); |
| 157 | unsigned i = 0; |
| 158 | do { |
| 159 | m_out.printf(" %1d = {\n" , i); |
| 160 | const auto& switchJumpTable = block()->switchJumpTable(i); |
| 161 | int entry = 0; |
| 162 | auto end = switchJumpTable.branchOffsets.end(); |
| 163 | for (auto iter = switchJumpTable.branchOffsets.begin(); iter != end; ++iter, ++entry) { |
| 164 | if (!*iter) |
| 165 | continue; |
| 166 | m_out.printf("\t\t%4d => %04d\n" , entry + switchJumpTable.min, *iter); |
| 167 | } |
| 168 | m_out.printf(" }\n" ); |
| 169 | ++i; |
| 170 | } while (i < count); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | template<class Block> |
| 175 | void BytecodeDumper<Block>::dumpStringSwitchJumpTables() |
| 176 | { |
| 177 | if (unsigned count = block()->numberOfStringSwitchJumpTables()) { |
| 178 | m_out.printf("\nString Switch Jump Tables:\n" ); |
| 179 | unsigned i = 0; |
| 180 | do { |
| 181 | m_out.printf(" %1d = {\n" , i); |
| 182 | const auto& stringSwitchJumpTable = block()->stringSwitchJumpTable(i); |
| 183 | auto end = stringSwitchJumpTable.offsetTable.end(); |
| 184 | for (auto iter = stringSwitchJumpTable.offsetTable.begin(); iter != end; ++iter) |
| 185 | m_out.printf("\t\t\"%s\" => %04d\n" , iter->key->utf8().data(), iter->value.branchOffset); |
| 186 | m_out.printf(" }\n" ); |
| 187 | ++i; |
| 188 | } while (i < count); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | template<class Block> |
| 193 | void BytecodeDumper<Block>::dumpBlock(Block* block, const InstructionStream& instructions, PrintStream& out, const ICStatusMap& statusMap) |
| 194 | { |
| 195 | size_t instructionCount = 0; |
| 196 | size_t wideInstructionCount = 0; |
| 197 | size_t instructionWithMetadataCount = 0; |
| 198 | |
| 199 | for (const auto& instruction : instructions) { |
| 200 | if (instruction->isWide()) |
| 201 | ++wideInstructionCount; |
| 202 | if (instruction->opcodeID() < NUMBER_OF_BYTECODE_WITH_METADATA) |
| 203 | ++instructionWithMetadataCount; |
| 204 | ++instructionCount; |
| 205 | } |
| 206 | |
| 207 | out.print(*block); |
| 208 | out.printf( |
| 209 | ": %lu instructions (%lu wide instructions, %lu instructions with metadata); %lu bytes (%lu metadata bytes); %d parameter(s); %d callee register(s); %d variable(s)" , |
| 210 | static_cast<unsigned long>(instructionCount), |
| 211 | static_cast<unsigned long>(wideInstructionCount), |
| 212 | static_cast<unsigned long>(instructionWithMetadataCount), |
| 213 | static_cast<unsigned long>(instructions.sizeInBytes() + block->metadataSizeInBytes()), |
| 214 | static_cast<unsigned long>(block->metadataSizeInBytes()), |
| 215 | block->numParameters(), block->numCalleeLocals(), block->numVars()); |
| 216 | out.print("; scope at " , block->scopeRegister()); |
| 217 | out.printf("\n" ); |
| 218 | |
| 219 | BytecodeDumper<Block> dumper(block, out); |
| 220 | for (const auto& it : instructions) |
| 221 | dumper.dumpBytecode(it, statusMap); |
| 222 | |
| 223 | dumper.dumpIdentifiers(); |
| 224 | dumper.dumpConstants(); |
| 225 | dumper.dumpExceptionHandlers(); |
| 226 | dumper.dumpSwitchJumpTables(); |
| 227 | dumper.dumpStringSwitchJumpTables(); |
| 228 | |
| 229 | out.printf("\n" ); |
| 230 | } |
| 231 | |
| 232 | template class BytecodeDumper<UnlinkedCodeBlock>; |
| 233 | template class BytecodeDumper<CodeBlock>; |
| 234 | |
| 235 | } |
| 236 | |