]> git.saurik.com Git - apple/javascriptcore.git/blob - assembler/LinkBuffer.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / assembler / LinkBuffer.cpp
1 /*
2 * Copyright (C) 2012-2015 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
26 #include "config.h"
27 #include "LinkBuffer.h"
28
29 #if ENABLE(ASSEMBLER)
30
31 #include "CodeBlock.h"
32 #include "JITCode.h"
33 #include "JSCInlines.h"
34 #include "Options.h"
35 #include "VM.h"
36 #include <wtf/CompilationThread.h>
37
38 namespace JSC {
39
40 bool shouldShowDisassemblyFor(CodeBlock* codeBlock)
41 {
42 if (JITCode::isOptimizingJIT(codeBlock->jitType()) && Options::showDFGDisassembly())
43 return true;
44 return Options::showDisassembly();
45 }
46
47 LinkBuffer::CodeRef LinkBuffer::finalizeCodeWithoutDisassembly()
48 {
49 performFinalization();
50
51 ASSERT(m_didAllocate);
52 if (m_executableMemory)
53 return CodeRef(m_executableMemory);
54
55 return CodeRef::createSelfManagedCodeRef(MacroAssemblerCodePtr(m_code));
56 }
57
58 LinkBuffer::CodeRef LinkBuffer::finalizeCodeWithDisassembly(const char* format, ...)
59 {
60 CodeRef result = finalizeCodeWithoutDisassembly();
61
62 if (m_alreadyDisassembled)
63 return result;
64
65 StringPrintStream out;
66 out.printf("Generated JIT code for ");
67 va_list argList;
68 va_start(argList, format);
69 out.vprintf(format, argList);
70 va_end(argList);
71 out.printf(":\n");
72
73 out.printf(" Code at [%p, %p):\n", result.code().executableAddress(), static_cast<char*>(result.code().executableAddress()) + result.size());
74
75 CString header = out.toCString();
76
77 if (Options::asyncDisassembly()) {
78 disassembleAsynchronously(header, result, m_size, " ");
79 return result;
80 }
81
82 dataLog(header);
83 disassemble(result.code(), m_size, " ", WTF::dataFile());
84
85 return result;
86 }
87
88 #if ENABLE(BRANCH_COMPACTION)
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
98 template <typename InstructionType>
99 void LinkBuffer::copyCompactAndLinkCode(MacroAssembler& macroAssembler, void* ownerUID, JITCompilationEffort effort)
100 {
101 m_initialSize = macroAssembler.m_assembler.codeSize();
102 allocate(m_initialSize, ownerUID, effort);
103 if (didFailToAllocate())
104 return;
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());
108 uint8_t* outData = reinterpret_cast<uint8_t*>(m_code);
109 int readPtr = 0;
110 int writePtr = 0;
111 unsigned jumpCount = jumpsToLink.size();
112 for (unsigned i = 0; i < jumpCount; ++i) {
113 int offset = readPtr - writePtr;
114 ASSERT(!(offset & 1));
115
116 // Copy the instructions from the last jump to the current one.
117 size_t regionSize = jumpsToLink[i].from() - readPtr;
118 InstructionType* copySource = reinterpret_cast_ptr<InstructionType*>(inData + readPtr);
119 InstructionType* copyEnd = reinterpret_cast_ptr<InstructionType*>(inData + readPtr + regionSize);
120 InstructionType* copyDst = reinterpret_cast_ptr<InstructionType*>(outData + writePtr);
121 ASSERT(!(regionSize % 2));
122 ASSERT(!(readPtr % 2));
123 ASSERT(!(writePtr % 2));
124 while (copySource != copyEnd)
125 *copyDst++ = *copySource++;
126 recordLinkOffsets(m_assemblerStorage, readPtr, jumpsToLink[i].from(), offset);
127 readPtr += regionSize;
128 writePtr += regionSize;
129
130 // Calculate absolute address of the jump target, in the case of backwards
131 // branches we need to be precise, forward branches we are pessimistic
132 const uint8_t* target;
133 if (jumpsToLink[i].to() >= jumpsToLink[i].from())
134 target = outData + jumpsToLink[i].to() - offset; // Compensate for what we have collapsed so far
135 else
136 target = outData + jumpsToLink[i].to() - executableOffsetFor(jumpsToLink[i].to());
137
138 JumpLinkType jumpLinkType = MacroAssembler::computeJumpType(jumpsToLink[i], outData + writePtr, target);
139 // Compact branch if we can...
140 if (MacroAssembler::canCompact(jumpsToLink[i].type())) {
141 // Step back in the write stream
142 int32_t delta = MacroAssembler::jumpSizeDelta(jumpsToLink[i].type(), jumpLinkType);
143 if (delta) {
144 writePtr -= delta;
145 recordLinkOffsets(m_assemblerStorage, jumpsToLink[i].from() - delta, readPtr, readPtr - writePtr);
146 }
147 }
148 jumpsToLink[i].setFrom(writePtr);
149 }
150 // Copy everything after the last jump
151 memcpy(outData + writePtr, inData + readPtr, m_initialSize - readPtr);
152 recordLinkOffsets(m_assemblerStorage, readPtr, m_initialSize, readPtr - writePtr);
153
154 for (unsigned i = 0; i < jumpCount; ++i) {
155 uint8_t* location = outData + jumpsToLink[i].from();
156 uint8_t* target = outData + jumpsToLink[i].to() - executableOffsetFor(jumpsToLink[i].to());
157 MacroAssembler::link(jumpsToLink[i], location, target);
158 }
159
160 jumpsToLink.clear();
161 shrink(writePtr + m_initialSize - readPtr);
162
163 #if DUMP_LINK_STATISTICS
164 dumpLinkStatistics(m_code, m_initialSize, m_size);
165 #endif
166 #if DUMP_CODE
167 dumpCode(m_code, m_size);
168 #endif
169 }
170 #endif
171
172
173 void LinkBuffer::linkCode(MacroAssembler& macroAssembler, void* ownerUID, JITCompilationEffort effort)
174 {
175 #if !ENABLE(BRANCH_COMPACTION)
176 #if defined(ASSEMBLER_HAS_CONSTANT_POOL) && ASSEMBLER_HAS_CONSTANT_POOL
177 macroAssembler.m_assembler.buffer().flushConstantPool(false);
178 #endif
179 AssemblerBuffer& buffer = macroAssembler.m_assembler.buffer();
180 allocate(buffer.codeSize(), ownerUID, effort);
181 if (!m_didAllocate)
182 return;
183 ASSERT(m_code);
184 #if CPU(ARM_TRADITIONAL)
185 macroAssembler.m_assembler.prepareExecutableCopy(m_code);
186 #endif
187 memcpy(m_code, buffer.data(), buffer.codeSize());
188 #if CPU(MIPS)
189 macroAssembler.m_assembler.relocateJumps(buffer.data(), m_code);
190 #endif
191 #elif CPU(ARM_THUMB2)
192 copyCompactAndLinkCode<uint16_t>(macroAssembler, ownerUID, effort);
193 #elif CPU(ARM64)
194 copyCompactAndLinkCode<uint32_t>(macroAssembler, ownerUID, effort);
195 #endif
196 }
197
198 void LinkBuffer::allocate(size_t initialSize, void* ownerUID, JITCompilationEffort effort)
199 {
200 if (m_code) {
201 if (initialSize > m_size)
202 return;
203
204 m_didAllocate = true;
205 m_size = initialSize;
206 return;
207 }
208
209 m_executableMemory = m_vm->executableAllocator.allocate(*m_vm, initialSize, ownerUID, effort);
210 if (!m_executableMemory)
211 return;
212 ExecutableAllocator::makeWritable(m_executableMemory->start(), m_executableMemory->sizeInBytes());
213 m_code = m_executableMemory->start();
214 m_size = initialSize;
215 m_didAllocate = true;
216 }
217
218 void LinkBuffer::shrink(size_t newSize)
219 {
220 if (!m_executableMemory)
221 return;
222 m_size = newSize;
223 m_executableMemory->shrink(m_size);
224 }
225
226 void LinkBuffer::performFinalization()
227 {
228 #ifndef NDEBUG
229 ASSERT(!isCompilationThread());
230 ASSERT(!m_completed);
231 ASSERT(isValid());
232 m_completed = true;
233 #endif
234
235 #if ENABLE(BRANCH_COMPACTION)
236 ExecutableAllocator::makeExecutable(code(), m_initialSize);
237 #else
238 ExecutableAllocator::makeExecutable(code(), m_size);
239 #endif
240 MacroAssembler::cacheFlush(code(), m_size);
241 }
242
243 #if DUMP_LINK_STATISTICS
244 void LinkBuffer::dumpLinkStatistics(void* code, size_t initializeSize, size_t finalSize)
245 {
246 static unsigned linkCount = 0;
247 static unsigned totalInitialSize = 0;
248 static unsigned totalFinalSize = 0;
249 linkCount++;
250 totalInitialSize += initialSize;
251 totalFinalSize += finalSize;
252 dataLogF("link %p: orig %u, compact %u (delta %u, %.2f%%)\n",
253 code, static_cast<unsigned>(initialSize), static_cast<unsigned>(finalSize),
254 static_cast<unsigned>(initialSize - finalSize),
255 100.0 * (initialSize - finalSize) / initialSize);
256 dataLogF("\ttotal %u: orig %u, compact %u (delta %u, %.2f%%)\n",
257 linkCount, totalInitialSize, totalFinalSize, totalInitialSize - totalFinalSize,
258 100.0 * (totalInitialSize - totalFinalSize) / totalInitialSize);
259 }
260 #endif
261
262 #if DUMP_CODE
263 void LinkBuffer::dumpCode(void* code, size_t size)
264 {
265 #if CPU(ARM_THUMB2)
266 // Dump the generated code in an asm file format that can be assembled and then disassembled
267 // for debugging purposes. For example, save this output as jit.s:
268 // gcc -arch armv7 -c jit.s
269 // otool -tv jit.o
270 static unsigned codeCount = 0;
271 unsigned short* tcode = static_cast<unsigned short*>(code);
272 size_t tsize = size / sizeof(short);
273 char nameBuf[128];
274 snprintf(nameBuf, sizeof(nameBuf), "_jsc_jit%u", codeCount++);
275 dataLogF("\t.syntax unified\n"
276 "\t.section\t__TEXT,__text,regular,pure_instructions\n"
277 "\t.globl\t%s\n"
278 "\t.align 2\n"
279 "\t.code 16\n"
280 "\t.thumb_func\t%s\n"
281 "# %p\n"
282 "%s:\n", nameBuf, nameBuf, code, nameBuf);
283
284 for (unsigned i = 0; i < tsize; i++)
285 dataLogF("\t.short\t0x%x\n", tcode[i]);
286 #elif CPU(ARM_TRADITIONAL)
287 // gcc -c jit.s
288 // objdump -D jit.o
289 static unsigned codeCount = 0;
290 unsigned int* tcode = static_cast<unsigned int*>(code);
291 size_t tsize = size / sizeof(unsigned int);
292 char nameBuf[128];
293 snprintf(nameBuf, sizeof(nameBuf), "_jsc_jit%u", codeCount++);
294 dataLogF("\t.globl\t%s\n"
295 "\t.align 4\n"
296 "\t.code 32\n"
297 "\t.text\n"
298 "# %p\n"
299 "%s:\n", nameBuf, code, nameBuf);
300
301 for (unsigned i = 0; i < tsize; i++)
302 dataLogF("\t.long\t0x%x\n", tcode[i]);
303 #endif
304 }
305 #endif
306
307 } // namespace JSC
308
309 #endif // ENABLE(ASSEMBLER)
310
311