2 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
31 #define DUMP_LINK_STATISTICS 0
34 #include <MacroAssembler.h>
35 #include <wtf/Noncopyable.h>
43 // This class assists in linking code generated by the macro assembler, once code generation
44 // has been completed, and the code has been copied to is final location in memory. At this
45 // time pointers to labels within the code may be resolved, and relative offsets to external
46 // addresses may be fixed.
49 // * Jump objects may be linked to external targets,
50 // * The address of Jump objects may taken, such that it can later be relinked.
51 // * The return address of a Call may be acquired.
52 // * The address of a Label pointing into the code may be resolved.
53 // * The value referenced by a DataLabel may be set.
56 WTF_MAKE_NONCOPYABLE(LinkBuffer);
57 typedef MacroAssemblerCodeRef CodeRef;
58 typedef MacroAssemblerCodePtr CodePtr;
59 typedef MacroAssembler::Label Label;
60 typedef MacroAssembler::Jump Jump;
61 typedef MacroAssembler::JumpList JumpList;
62 typedef MacroAssembler::Call Call;
63 typedef MacroAssembler::DataLabelCompact DataLabelCompact;
64 typedef MacroAssembler::DataLabel32 DataLabel32;
65 typedef MacroAssembler::DataLabelPtr DataLabelPtr;
66 #if ENABLE(BRANCH_COMPACTION)
67 typedef MacroAssembler::LinkRecord LinkRecord;
68 typedef MacroAssembler::JumpLinkType JumpLinkType;
72 LinkBuffer(JSGlobalData& globalData, MacroAssembler* masm)
76 , m_globalData(&globalData)
89 // These methods are used to link or set values at code generation time.
91 void link(Call call, FunctionPtr function)
93 ASSERT(call.isFlagSet(Call::Linkable));
94 call.m_label = applyOffset(call.m_label);
95 MacroAssembler::linkCall(code(), call, function);
98 void link(Jump jump, CodeLocationLabel label)
100 jump.m_label = applyOffset(jump.m_label);
101 MacroAssembler::linkJump(code(), jump, label);
104 void link(JumpList list, CodeLocationLabel label)
106 for (unsigned i = 0; i < list.m_jumps.size(); ++i)
107 link(list.m_jumps[i], label);
110 void patch(DataLabelPtr label, void* value)
112 AssemblerLabel target = applyOffset(label.m_label);
113 MacroAssembler::linkPointer(code(), target, value);
116 void patch(DataLabelPtr label, CodeLocationLabel value)
118 AssemblerLabel target = applyOffset(label.m_label);
119 MacroAssembler::linkPointer(code(), target, value.executableAddress());
122 // These methods are used to obtain handles to allow the code to be relinked / repatched later.
124 CodeLocationCall locationOf(Call call)
126 ASSERT(call.isFlagSet(Call::Linkable));
127 ASSERT(!call.isFlagSet(Call::Near));
128 return CodeLocationCall(MacroAssembler::getLinkerAddress(code(), applyOffset(call.m_label)));
131 CodeLocationNearCall locationOfNearCall(Call call)
133 ASSERT(call.isFlagSet(Call::Linkable));
134 ASSERT(call.isFlagSet(Call::Near));
135 return CodeLocationNearCall(MacroAssembler::getLinkerAddress(code(), applyOffset(call.m_label)));
138 CodeLocationLabel locationOf(Label label)
140 return CodeLocationLabel(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
143 CodeLocationDataLabelPtr locationOf(DataLabelPtr label)
145 return CodeLocationDataLabelPtr(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
148 CodeLocationDataLabel32 locationOf(DataLabel32 label)
150 return CodeLocationDataLabel32(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
153 CodeLocationDataLabelCompact locationOf(DataLabelCompact label)
155 return CodeLocationDataLabelCompact(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
158 // This method obtains the return address of the call, given as an offset from
159 // the start of the code.
160 unsigned returnAddressOffset(Call call)
162 call.m_label = applyOffset(call.m_label);
163 return MacroAssembler::getLinkerCallReturnOffset(call);
166 // Upon completion of all patching either 'finalizeCode()' or 'finalizeCodeAddendum()' should be called
167 // once to complete generation of the code. 'finalizeCode()' is suited to situations
168 // where the executable pool must also be retained, the lighter-weight 'finalizeCodeAddendum()' is
169 // suited to adding to an existing allocation.
170 CodeRef finalizeCode()
172 performFinalization();
174 return CodeRef(m_executableMemory);
177 CodePtr trampolineAt(Label label)
179 return CodePtr(MacroAssembler::AssemblerType_T::getRelocatedAddress(code(), applyOffset(label.m_label)));
195 template <typename T> T applyOffset(T src)
197 #if ENABLE(BRANCH_COMPACTION)
198 src.m_offset -= m_assembler->executableOffsetFor(src.m_offset);
203 // Keep this private! - the underlying code should only be obtained externally via
204 // finalizeCode() or finalizeCodeAddendum().
213 #if !ENABLE(BRANCH_COMPACTION)
214 m_executableMemory = m_assembler->m_assembler.executableCopy(*m_globalData);
215 if (!m_executableMemory)
217 m_code = m_executableMemory->start();
218 m_size = m_assembler->m_assembler.codeSize();
221 size_t initialSize = m_assembler->m_assembler.codeSize();
222 m_executableMemory = m_globalData->executableAllocator.allocate(*m_globalData, initialSize);
223 if (!m_executableMemory)
225 m_code = (uint8_t*)m_executableMemory->start();
227 ExecutableAllocator::makeWritable(m_code, initialSize);
228 uint8_t* inData = (uint8_t*)m_assembler->unlinkedCode();
229 uint8_t* outData = reinterpret_cast<uint8_t*>(m_code);
232 Vector<LinkRecord>& jumpsToLink = m_assembler->jumpsToLink();
233 unsigned jumpCount = jumpsToLink.size();
234 for (unsigned i = 0; i < jumpCount; ++i) {
235 int offset = readPtr - writePtr;
236 ASSERT(!(offset & 1));
238 // Copy the instructions from the last jump to the current one.
239 size_t regionSize = jumpsToLink[i].from() - readPtr;
240 uint16_t* copySource = reinterpret_cast<uint16_t*>(inData + readPtr);
241 uint16_t* copyEnd = reinterpret_cast<uint16_t*>(inData + readPtr + regionSize);
242 uint16_t* copyDst = reinterpret_cast<uint16_t*>(outData + writePtr);
243 ASSERT(!(regionSize % 2));
244 ASSERT(!(readPtr % 2));
245 ASSERT(!(writePtr % 2));
246 while (copySource != copyEnd)
247 *copyDst++ = *copySource++;
248 m_assembler->recordLinkOffsets(readPtr, jumpsToLink[i].from(), offset);
249 readPtr += regionSize;
250 writePtr += regionSize;
252 // Calculate absolute address of the jump target, in the case of backwards
253 // branches we need to be precise, forward branches we are pessimistic
254 const uint8_t* target;
255 if (jumpsToLink[i].to() >= jumpsToLink[i].from())
256 target = outData + jumpsToLink[i].to() - offset; // Compensate for what we have collapsed so far
258 target = outData + jumpsToLink[i].to() - m_assembler->executableOffsetFor(jumpsToLink[i].to());
260 JumpLinkType jumpLinkType = m_assembler->computeJumpType(jumpsToLink[i], outData + writePtr, target);
261 // Compact branch if we can...
262 if (m_assembler->canCompact(jumpsToLink[i].type())) {
263 // Step back in the write stream
264 int32_t delta = m_assembler->jumpSizeDelta(jumpsToLink[i].type(), jumpLinkType);
267 m_assembler->recordLinkOffsets(jumpsToLink[i].from() - delta, readPtr, readPtr - writePtr);
270 jumpsToLink[i].setFrom(writePtr);
272 // Copy everything after the last jump
273 memcpy(outData + writePtr, inData + readPtr, initialSize - readPtr);
274 m_assembler->recordLinkOffsets(readPtr, initialSize, readPtr - writePtr);
276 for (unsigned i = 0; i < jumpCount; ++i) {
277 uint8_t* location = outData + jumpsToLink[i].from();
278 uint8_t* target = outData + jumpsToLink[i].to() - m_assembler->executableOffsetFor(jumpsToLink[i].to());
279 m_assembler->link(jumpsToLink[i], location, target);
283 m_size = writePtr + initialSize - readPtr;
284 m_executableMemory->shrink(m_size);
286 #if DUMP_LINK_STATISTICS
287 dumpLinkStatistics(m_code, initialSize, m_size);
290 dumpCode(m_code, m_size);
295 void performFinalization()
298 ASSERT(!m_completed);
302 ExecutableAllocator::makeExecutable(code(), m_size);
303 ExecutableAllocator::cacheFlush(code(), m_size);
306 #if DUMP_LINK_STATISTICS
307 static void dumpLinkStatistics(void* code, size_t initialSize, size_t finalSize)
309 static unsigned linkCount = 0;
310 static unsigned totalInitialSize = 0;
311 static unsigned totalFinalSize = 0;
313 totalInitialSize += initialSize;
314 totalFinalSize += finalSize;
315 printf("link %p: orig %u, compact %u (delta %u, %.2f%%)\n",
316 code, static_cast<unsigned>(initialSize), static_cast<unsigned>(finalSize),
317 static_cast<unsigned>(initialSize - finalSize),
318 100.0 * (initialSize - finalSize) / initialSize);
319 printf("\ttotal %u: orig %u, compact %u (delta %u, %.2f%%)\n",
320 linkCount, totalInitialSize, totalFinalSize, totalInitialSize - totalFinalSize,
321 100.0 * (totalInitialSize - totalFinalSize) / totalInitialSize);
326 static void dumpCode(void* code, size_t size)
329 // Dump the generated code in an asm file format that can be assembled and then disassembled
330 // for debugging purposes. For example, save this output as jit.s:
331 // gcc -arch armv7 -c jit.s
333 static unsigned codeCount = 0;
334 unsigned short* tcode = static_cast<unsigned short*>(code);
335 size_t tsize = size / sizeof(short);
337 snprintf(nameBuf, sizeof(nameBuf), "_jsc_jit%u", codeCount++);
338 printf("\t.syntax unified\n"
339 "\t.section\t__TEXT,__text,regular,pure_instructions\n"
343 "\t.thumb_func\t%s\n"
345 "%s:\n", nameBuf, nameBuf, code, nameBuf);
347 for (unsigned i = 0; i < tsize; i++)
348 printf("\t.short\t0x%x\n", tcode[i]);
353 RefPtr<ExecutableMemoryHandle> m_executableMemory;
356 MacroAssembler* m_assembler;
357 JSGlobalData* m_globalData;
365 #endif // ENABLE(ASSEMBLER)
367 #endif // LinkBuffer_h