fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | 32622c9 | 2015-03-24 05:37:19 +0000 | [diff] [blame] | 2 | * Copyright (C) 2012-2015 Apple Inc. All rights reserved. |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 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 "LinkBuffer.h" |
| 28 | |
| 29 | #if ENABLE(ASSEMBLER) |
| 30 | |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 31 | #include "CodeBlock.h" |
| 32 | #include "JITCode.h" |
fpizlo@apple.com | fb7eff2 | 2014-02-11 01:45:50 +0000 | [diff] [blame] | 33 | #include "JSCInlines.h" |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 34 | #include "Options.h" |
mhahnenberg@apple.com | 50a0ca1 | 2013-12-17 01:33:05 +0000 | [diff] [blame] | 35 | #include "VM.h" |
oliver@apple.com | 90fce82 | 2013-07-25 04:00:13 +0000 | [diff] [blame] | 36 | #include <wtf/CompilationThread.h> |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 37 | |
| 38 | namespace JSC { |
| 39 | |
mark.lam@apple.com | ee3c410 | 2015-10-14 18:57:07 +0000 | [diff] [blame] | 40 | bool shouldDumpDisassemblyFor(CodeBlock* codeBlock) |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 41 | { |
mark.lam@apple.com | ee3c410 | 2015-10-14 18:57:07 +0000 | [diff] [blame] | 42 | if (JITCode::isOptimizingJIT(codeBlock->jitType()) && Options::dumpDFGDisassembly()) |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 43 | return true; |
mark.lam@apple.com | ee3c410 | 2015-10-14 18:57:07 +0000 | [diff] [blame] | 44 | return Options::dumpDisassembly(); |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 45 | } |
| 46 | |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 47 | LinkBuffer::CodeRef LinkBuffer::finalizeCodeWithoutDisassembly() |
| 48 | { |
| 49 | performFinalization(); |
| 50 | |
fpizlo@apple.com | 2e7ada0 | 2013-10-23 18:22:09 +0000 | [diff] [blame] | 51 | ASSERT(m_didAllocate); |
| 52 | if (m_executableMemory) |
| 53 | return CodeRef(m_executableMemory); |
| 54 | |
| 55 | return CodeRef::createSelfManagedCodeRef(MacroAssemblerCodePtr(m_code)); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | LinkBuffer::CodeRef LinkBuffer::finalizeCodeWithDisassembly(const char* format, ...) |
| 59 | { |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 60 | CodeRef result = finalizeCodeWithoutDisassembly(); |
mark.lam@apple.com | 10190c4 | 2013-12-03 01:32:43 +0000 | [diff] [blame] | 61 | |
fpizlo@apple.com | 32622c9 | 2015-03-24 05:37:19 +0000 | [diff] [blame] | 62 | if (m_alreadyDisassembled) |
| 63 | return result; |
| 64 | |
| 65 | StringPrintStream out; |
| 66 | out.printf("Generated JIT code for "); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 67 | va_list argList; |
| 68 | va_start(argList, format); |
fpizlo@apple.com | 32622c9 | 2015-03-24 05:37:19 +0000 | [diff] [blame] | 69 | out.vprintf(format, argList); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 70 | va_end(argList); |
fpizlo@apple.com | 32622c9 | 2015-03-24 05:37:19 +0000 | [diff] [blame] | 71 | out.printf(":\n"); |
| 72 | |
| 73 | out.printf(" Code at [%p, %p):\n", result.code().executableAddress(), static_cast<char*>(result.code().executableAddress()) + result.size()); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 74 | |
fpizlo@apple.com | 32622c9 | 2015-03-24 05:37:19 +0000 | [diff] [blame] | 75 | CString header = out.toCString(); |
| 76 | |
| 77 | if (Options::asyncDisassembly()) { |
| 78 | disassembleAsynchronously(header, result, m_size, " "); |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | dataLog(header); |
fpizlo@apple.com | 2860dbe | 2012-11-22 03:47:23 +0000 | [diff] [blame] | 83 | disassemble(result.code(), m_size, " ", WTF::dataFile()); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 84 | |
| 85 | return result; |
| 86 | } |
| 87 | |
dbates@webkit.org | 98f0de0 | 2013-10-15 22:16:39 +0000 | [diff] [blame] | 88 | #if ENABLE(BRANCH_COMPACTION) |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 89 | static ALWAYS_INLINE void recordLinkOffsets(AssemblerData& assemblerData, int32_t regionStart, int32_t regionEnd, int32_t offset) |
| 90 | { |
| 91 | int32_t ptr = regionStart / sizeof(int32_t); |
| 92 | const int32_t end = regionEnd / sizeof(int32_t); |
| 93 | int32_t* offsets = reinterpret_cast<int32_t*>(assemblerData.buffer()); |
| 94 | while (ptr < end) |
| 95 | offsets[ptr++] = offset; |
| 96 | } |
| 97 | |
dbates@webkit.org | 98f0de0 | 2013-10-15 22:16:39 +0000 | [diff] [blame] | 98 | template <typename InstructionType> |
benjamin@webkit.org | f766fd9 | 2014-07-08 04:23:30 +0000 | [diff] [blame] | 99 | void LinkBuffer::copyCompactAndLinkCode(MacroAssembler& macroAssembler, void* ownerUID, JITCompilationEffort effort) |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 100 | { |
benjamin@webkit.org | f766fd9 | 2014-07-08 04:23:30 +0000 | [diff] [blame] | 101 | m_initialSize = macroAssembler.m_assembler.codeSize(); |
fpizlo@apple.com | a26c904 | 2013-10-20 02:07:39 +0000 | [diff] [blame] | 102 | allocate(m_initialSize, ownerUID, effort); |
fpizlo@apple.com | 41bed90 | 2014-02-25 22:18:21 +0000 | [diff] [blame] | 103 | if (didFailToAllocate()) |
| 104 | return; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 105 | Vector<LinkRecord, 0, UnsafeVectorOverflow>& jumpsToLink = macroAssembler.jumpsToLink(); |
| 106 | m_assemblerStorage = macroAssembler.m_assembler.buffer().releaseAssemblerData(); |
| 107 | uint8_t* inData = reinterpret_cast<uint8_t*>(m_assemblerStorage.buffer()); |
oliver@apple.com | 3243ddf | 2016-04-11 19:00:48 +0000 | [diff] [blame] | 108 | |
oliver@apple.com | 89b03a6 | 2016-03-09 00:08:53 +0000 | [diff] [blame] | 109 | AssemblerData outBuffer(m_size); |
| 110 | uint8_t* outData = reinterpret_cast<uint8_t*>(outBuffer.buffer()); |
oliver@apple.com | 3243ddf | 2016-04-11 19:00:48 +0000 | [diff] [blame] | 111 | uint8_t* codeOutData = reinterpret_cast<uint8_t*>(m_code); |
| 112 | |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 113 | int readPtr = 0; |
| 114 | int writePtr = 0; |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 115 | unsigned jumpCount = jumpsToLink.size(); |
| 116 | for (unsigned i = 0; i < jumpCount; ++i) { |
| 117 | int offset = readPtr - writePtr; |
| 118 | ASSERT(!(offset & 1)); |
| 119 | |
| 120 | // Copy the instructions from the last jump to the current one. |
| 121 | size_t regionSize = jumpsToLink[i].from() - readPtr; |
dbates@webkit.org | 98f0de0 | 2013-10-15 22:16:39 +0000 | [diff] [blame] | 122 | InstructionType* copySource = reinterpret_cast_ptr<InstructionType*>(inData + readPtr); |
| 123 | InstructionType* copyEnd = reinterpret_cast_ptr<InstructionType*>(inData + readPtr + regionSize); |
| 124 | InstructionType* copyDst = reinterpret_cast_ptr<InstructionType*>(outData + writePtr); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 125 | ASSERT(!(regionSize % 2)); |
| 126 | ASSERT(!(readPtr % 2)); |
| 127 | ASSERT(!(writePtr % 2)); |
| 128 | while (copySource != copyEnd) |
| 129 | *copyDst++ = *copySource++; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 130 | recordLinkOffsets(m_assemblerStorage, readPtr, jumpsToLink[i].from(), offset); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 131 | readPtr += regionSize; |
| 132 | writePtr += regionSize; |
| 133 | |
| 134 | // Calculate absolute address of the jump target, in the case of backwards |
| 135 | // branches we need to be precise, forward branches we are pessimistic |
| 136 | const uint8_t* target; |
| 137 | if (jumpsToLink[i].to() >= jumpsToLink[i].from()) |
oliver@apple.com | 3243ddf | 2016-04-11 19:00:48 +0000 | [diff] [blame] | 138 | target = codeOutData + jumpsToLink[i].to() - offset; // Compensate for what we have collapsed so far |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 139 | else |
oliver@apple.com | 3243ddf | 2016-04-11 19:00:48 +0000 | [diff] [blame] | 140 | target = codeOutData + jumpsToLink[i].to() - executableOffsetFor(jumpsToLink[i].to()); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 141 | |
oliver@apple.com | 3243ddf | 2016-04-11 19:00:48 +0000 | [diff] [blame] | 142 | JumpLinkType jumpLinkType = MacroAssembler::computeJumpType(jumpsToLink[i], codeOutData + writePtr, target); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 143 | // Compact branch if we can... |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 144 | if (MacroAssembler::canCompact(jumpsToLink[i].type())) { |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 145 | // Step back in the write stream |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 146 | int32_t delta = MacroAssembler::jumpSizeDelta(jumpsToLink[i].type(), jumpLinkType); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 147 | if (delta) { |
| 148 | writePtr -= delta; |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 149 | recordLinkOffsets(m_assemblerStorage, jumpsToLink[i].from() - delta, readPtr, readPtr - writePtr); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | jumpsToLink[i].setFrom(writePtr); |
| 153 | } |
| 154 | // Copy everything after the last jump |
| 155 | memcpy(outData + writePtr, inData + readPtr, m_initialSize - readPtr); |
benjamin@webkit.org | f6de077 | 2014-07-15 23:59:14 +0000 | [diff] [blame] | 156 | recordLinkOffsets(m_assemblerStorage, readPtr, m_initialSize, readPtr - writePtr); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 157 | |
| 158 | for (unsigned i = 0; i < jumpCount; ++i) { |
oliver@apple.com | 3243ddf | 2016-04-11 19:00:48 +0000 | [diff] [blame] | 159 | uint8_t* location = codeOutData + jumpsToLink[i].from(); |
| 160 | uint8_t* target = codeOutData + jumpsToLink[i].to() - executableOffsetFor(jumpsToLink[i].to()); |
| 161 | MacroAssembler::link(jumpsToLink[i], outData + jumpsToLink[i].from(), location, target); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | jumpsToLink.clear(); |
fpizlo@apple.com | a26c904 | 2013-10-20 02:07:39 +0000 | [diff] [blame] | 165 | shrink(writePtr + m_initialSize - readPtr); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 166 | |
cdumez@apple.com | 35ccc14 | 2016-03-16 19:33:47 +0000 | [diff] [blame] | 167 | performJITMemcpy(m_code, outBuffer.buffer(), m_size); |
oliver@apple.com | 89b03a6 | 2016-03-09 00:08:53 +0000 | [diff] [blame] | 168 | |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 169 | #if DUMP_LINK_STATISTICS |
| 170 | dumpLinkStatistics(m_code, m_initialSize, m_size); |
| 171 | #endif |
| 172 | #if DUMP_CODE |
| 173 | dumpCode(m_code, m_size); |
| 174 | #endif |
dbates@webkit.org | 98f0de0 | 2013-10-15 22:16:39 +0000 | [diff] [blame] | 175 | } |
| 176 | #endif |
| 177 | |
| 178 | |
benjamin@webkit.org | f766fd9 | 2014-07-08 04:23:30 +0000 | [diff] [blame] | 179 | void LinkBuffer::linkCode(MacroAssembler& macroAssembler, void* ownerUID, JITCompilationEffort effort) |
dbates@webkit.org | 98f0de0 | 2013-10-15 22:16:39 +0000 | [diff] [blame] | 180 | { |
dbates@webkit.org | 98f0de0 | 2013-10-15 22:16:39 +0000 | [diff] [blame] | 181 | #if !ENABLE(BRANCH_COMPACTION) |
commit-queue@webkit.org | 30a6a70 | 2013-10-22 15:51:51 +0000 | [diff] [blame] | 182 | #if defined(ASSEMBLER_HAS_CONSTANT_POOL) && ASSEMBLER_HAS_CONSTANT_POOL |
benjamin@webkit.org | f766fd9 | 2014-07-08 04:23:30 +0000 | [diff] [blame] | 183 | macroAssembler.m_assembler.buffer().flushConstantPool(false); |
commit-queue@webkit.org | 30a6a70 | 2013-10-22 15:51:51 +0000 | [diff] [blame] | 184 | #endif |
benjamin@webkit.org | f766fd9 | 2014-07-08 04:23:30 +0000 | [diff] [blame] | 185 | AssemblerBuffer& buffer = macroAssembler.m_assembler.buffer(); |
fpizlo@apple.com | a26c904 | 2013-10-20 02:07:39 +0000 | [diff] [blame] | 186 | allocate(buffer.codeSize(), ownerUID, effort); |
| 187 | if (!m_didAllocate) |
dbates@webkit.org | 98f0de0 | 2013-10-15 22:16:39 +0000 | [diff] [blame] | 188 | return; |
dbates@webkit.org | 98f0de0 | 2013-10-15 22:16:39 +0000 | [diff] [blame] | 189 | ASSERT(m_code); |
commit-queue@webkit.org | 6539105 | 2013-10-29 19:02:46 +0000 | [diff] [blame] | 190 | #if CPU(ARM_TRADITIONAL) |
benjamin@webkit.org | f766fd9 | 2014-07-08 04:23:30 +0000 | [diff] [blame] | 191 | macroAssembler.m_assembler.prepareExecutableCopy(m_code); |
commit-queue@webkit.org | 6539105 | 2013-10-29 19:02:46 +0000 | [diff] [blame] | 192 | #endif |
oliver@apple.com | 3243ddf | 2016-04-11 19:00:48 +0000 | [diff] [blame] | 193 | performJITMemcpy(m_code, buffer.data(), buffer.codeSize()); |
commit-queue@webkit.org | 40a5dbd | 2013-11-05 15:32:27 +0000 | [diff] [blame] | 194 | #if CPU(MIPS) |
benjamin@webkit.org | f766fd9 | 2014-07-08 04:23:30 +0000 | [diff] [blame] | 195 | macroAssembler.m_assembler.relocateJumps(buffer.data(), m_code); |
commit-queue@webkit.org | 40a5dbd | 2013-11-05 15:32:27 +0000 | [diff] [blame] | 196 | #endif |
dbates@webkit.org | 98f0de0 | 2013-10-15 22:16:39 +0000 | [diff] [blame] | 197 | #elif CPU(ARM_THUMB2) |
benjamin@webkit.org | 719b757 | 2014-07-08 04:35:42 +0000 | [diff] [blame] | 198 | copyCompactAndLinkCode<uint16_t>(macroAssembler, ownerUID, effort); |
dbates@webkit.org | 98f0de0 | 2013-10-15 22:16:39 +0000 | [diff] [blame] | 199 | #elif CPU(ARM64) |
benjamin@webkit.org | 719b757 | 2014-07-08 04:35:42 +0000 | [diff] [blame] | 200 | copyCompactAndLinkCode<uint32_t>(macroAssembler, ownerUID, effort); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 201 | #endif |
fpizlo@apple.com | 6e69796 | 2015-10-12 17:56:26 +0000 | [diff] [blame] | 202 | |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 203 | m_linkTasks = WTFMove(macroAssembler.m_linkTasks); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 204 | } |
| 205 | |
fpizlo@apple.com | a26c904 | 2013-10-20 02:07:39 +0000 | [diff] [blame] | 206 | void LinkBuffer::allocate(size_t initialSize, void* ownerUID, JITCompilationEffort effort) |
| 207 | { |
fpizlo@apple.com | 2e7ada0 | 2013-10-23 18:22:09 +0000 | [diff] [blame] | 208 | if (m_code) { |
| 209 | if (initialSize > m_size) |
| 210 | return; |
| 211 | |
| 212 | m_didAllocate = true; |
| 213 | m_size = initialSize; |
| 214 | return; |
| 215 | } |
| 216 | |
oliver@apple.com | 89b03a6 | 2016-03-09 00:08:53 +0000 | [diff] [blame] | 217 | ASSERT(m_vm != nullptr); |
fpizlo@apple.com | a26c904 | 2013-10-20 02:07:39 +0000 | [diff] [blame] | 218 | m_executableMemory = m_vm->executableAllocator.allocate(*m_vm, initialSize, ownerUID, effort); |
| 219 | if (!m_executableMemory) |
| 220 | return; |
fpizlo@apple.com | a26c904 | 2013-10-20 02:07:39 +0000 | [diff] [blame] | 221 | m_code = m_executableMemory->start(); |
| 222 | m_size = initialSize; |
| 223 | m_didAllocate = true; |
| 224 | } |
| 225 | |
| 226 | void LinkBuffer::shrink(size_t newSize) |
| 227 | { |
fpizlo@apple.com | 41bed90 | 2014-02-25 22:18:21 +0000 | [diff] [blame] | 228 | if (!m_executableMemory) |
| 229 | return; |
fpizlo@apple.com | a26c904 | 2013-10-20 02:07:39 +0000 | [diff] [blame] | 230 | m_size = newSize; |
| 231 | m_executableMemory->shrink(m_size); |
| 232 | } |
| 233 | |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 234 | void LinkBuffer::performFinalization() |
| 235 | { |
fpizlo@apple.com | 6e69796 | 2015-10-12 17:56:26 +0000 | [diff] [blame] | 236 | for (auto& task : m_linkTasks) |
| 237 | task->run(*this); |
| 238 | |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 239 | #ifndef NDEBUG |
oliver@apple.com | 90fce82 | 2013-07-25 04:00:13 +0000 | [diff] [blame] | 240 | ASSERT(!isCompilationThread()); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 241 | ASSERT(!m_completed); |
| 242 | ASSERT(isValid()); |
| 243 | m_completed = true; |
| 244 | #endif |
| 245 | |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 246 | MacroAssembler::cacheFlush(code(), m_size); |
| 247 | } |
| 248 | |
| 249 | #if DUMP_LINK_STATISTICS |
| 250 | void LinkBuffer::dumpLinkStatistics(void* code, size_t initializeSize, size_t finalSize) |
| 251 | { |
| 252 | static unsigned linkCount = 0; |
| 253 | static unsigned totalInitialSize = 0; |
| 254 | static unsigned totalFinalSize = 0; |
| 255 | linkCount++; |
| 256 | totalInitialSize += initialSize; |
| 257 | totalFinalSize += finalSize; |
fpizlo@apple.com | 01902c8 | 2012-11-22 04:23:36 +0000 | [diff] [blame] | 258 | dataLogF("link %p: orig %u, compact %u (delta %u, %.2f%%)\n", |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 259 | code, static_cast<unsigned>(initialSize), static_cast<unsigned>(finalSize), |
| 260 | static_cast<unsigned>(initialSize - finalSize), |
| 261 | 100.0 * (initialSize - finalSize) / initialSize); |
fpizlo@apple.com | 01902c8 | 2012-11-22 04:23:36 +0000 | [diff] [blame] | 262 | dataLogF("\ttotal %u: orig %u, compact %u (delta %u, %.2f%%)\n", |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 263 | linkCount, totalInitialSize, totalFinalSize, totalInitialSize - totalFinalSize, |
| 264 | 100.0 * (totalInitialSize - totalFinalSize) / totalInitialSize); |
| 265 | } |
| 266 | #endif |
| 267 | |
| 268 | #if DUMP_CODE |
| 269 | void LinkBuffer::dumpCode(void* code, size_t size) |
| 270 | { |
| 271 | #if CPU(ARM_THUMB2) |
| 272 | // Dump the generated code in an asm file format that can be assembled and then disassembled |
| 273 | // for debugging purposes. For example, save this output as jit.s: |
| 274 | // gcc -arch armv7 -c jit.s |
| 275 | // otool -tv jit.o |
| 276 | static unsigned codeCount = 0; |
| 277 | unsigned short* tcode = static_cast<unsigned short*>(code); |
| 278 | size_t tsize = size / sizeof(short); |
| 279 | char nameBuf[128]; |
| 280 | snprintf(nameBuf, sizeof(nameBuf), "_jsc_jit%u", codeCount++); |
fpizlo@apple.com | 01902c8 | 2012-11-22 04:23:36 +0000 | [diff] [blame] | 281 | dataLogF("\t.syntax unified\n" |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 282 | "\t.section\t__TEXT,__text,regular,pure_instructions\n" |
| 283 | "\t.globl\t%s\n" |
| 284 | "\t.align 2\n" |
| 285 | "\t.code 16\n" |
| 286 | "\t.thumb_func\t%s\n" |
| 287 | "# %p\n" |
| 288 | "%s:\n", nameBuf, nameBuf, code, nameBuf); |
| 289 | |
| 290 | for (unsigned i = 0; i < tsize; i++) |
fpizlo@apple.com | 01902c8 | 2012-11-22 04:23:36 +0000 | [diff] [blame] | 291 | dataLogF("\t.short\t0x%x\n", tcode[i]); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 292 | #elif CPU(ARM_TRADITIONAL) |
| 293 | // gcc -c jit.s |
| 294 | // objdump -D jit.o |
| 295 | static unsigned codeCount = 0; |
| 296 | unsigned int* tcode = static_cast<unsigned int*>(code); |
| 297 | size_t tsize = size / sizeof(unsigned int); |
| 298 | char nameBuf[128]; |
| 299 | snprintf(nameBuf, sizeof(nameBuf), "_jsc_jit%u", codeCount++); |
fpizlo@apple.com | 01902c8 | 2012-11-22 04:23:36 +0000 | [diff] [blame] | 300 | dataLogF("\t.globl\t%s\n" |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 301 | "\t.align 4\n" |
| 302 | "\t.code 32\n" |
| 303 | "\t.text\n" |
| 304 | "# %p\n" |
| 305 | "%s:\n", nameBuf, code, nameBuf); |
| 306 | |
| 307 | for (unsigned i = 0; i < tsize; i++) |
fpizlo@apple.com | 01902c8 | 2012-11-22 04:23:36 +0000 | [diff] [blame] | 308 | dataLogF("\t.long\t0x%x\n", tcode[i]); |
fpizlo@apple.com | 2adf527 | 2012-06-20 01:33:30 +0000 | [diff] [blame] | 309 | #endif |
| 310 | } |
| 311 | #endif |
| 312 | |
| 313 | } // namespace JSC |
| 314 | |
| 315 | #endif // ENABLE(ASSEMBLER) |
| 316 | |
| 317 | |