initial import
[vuplus_webkit] / Source / JavaScriptCore / assembler / LinkBuffer.h
1 /*
2  * Copyright (C) 2009, 2010 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 #ifndef LinkBuffer_h
27 #define LinkBuffer_h
28
29 #if ENABLE(ASSEMBLER)
30
31 #define DUMP_LINK_STATISTICS 0
32 #define DUMP_CODE 0
33
34 #include <MacroAssembler.h>
35 #include <wtf/Noncopyable.h>
36
37 namespace JSC {
38
39 class JSGlobalData;
40
41 // LinkBuffer:
42 //
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.
47 //
48 // Specifically:
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.
54 //
55 class LinkBuffer {
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;
69 #endif
70
71 public:
72     LinkBuffer(JSGlobalData& globalData, MacroAssembler* masm)
73         : m_size(0)
74         , m_code(0)
75         , m_assembler(masm)
76         , m_globalData(&globalData)
77 #ifndef NDEBUG
78         , m_completed(false)
79 #endif
80     {
81         linkCode();
82     }
83
84     ~LinkBuffer()
85     {
86         ASSERT(m_completed);
87     }
88
89     // These methods are used to link or set values at code generation time.
90
91     void link(Call call, FunctionPtr function)
92     {
93         ASSERT(call.isFlagSet(Call::Linkable));
94         call.m_label = applyOffset(call.m_label);
95         MacroAssembler::linkCall(code(), call, function);
96     }
97     
98     void link(Jump jump, CodeLocationLabel label)
99     {
100         jump.m_label = applyOffset(jump.m_label);
101         MacroAssembler::linkJump(code(), jump, label);
102     }
103
104     void link(JumpList list, CodeLocationLabel label)
105     {
106         for (unsigned i = 0; i < list.m_jumps.size(); ++i)
107             link(list.m_jumps[i], label);
108     }
109
110     void patch(DataLabelPtr label, void* value)
111     {
112         AssemblerLabel target = applyOffset(label.m_label);
113         MacroAssembler::linkPointer(code(), target, value);
114     }
115
116     void patch(DataLabelPtr label, CodeLocationLabel value)
117     {
118         AssemblerLabel target = applyOffset(label.m_label);
119         MacroAssembler::linkPointer(code(), target, value.executableAddress());
120     }
121
122     // These methods are used to obtain handles to allow the code to be relinked / repatched later.
123
124     CodeLocationCall locationOf(Call call)
125     {
126         ASSERT(call.isFlagSet(Call::Linkable));
127         ASSERT(!call.isFlagSet(Call::Near));
128         return CodeLocationCall(MacroAssembler::getLinkerAddress(code(), applyOffset(call.m_label)));
129     }
130
131     CodeLocationNearCall locationOfNearCall(Call call)
132     {
133         ASSERT(call.isFlagSet(Call::Linkable));
134         ASSERT(call.isFlagSet(Call::Near));
135         return CodeLocationNearCall(MacroAssembler::getLinkerAddress(code(), applyOffset(call.m_label)));
136     }
137
138     CodeLocationLabel locationOf(Label label)
139     {
140         return CodeLocationLabel(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
141     }
142
143     CodeLocationDataLabelPtr locationOf(DataLabelPtr label)
144     {
145         return CodeLocationDataLabelPtr(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
146     }
147
148     CodeLocationDataLabel32 locationOf(DataLabel32 label)
149     {
150         return CodeLocationDataLabel32(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
151     }
152     
153     CodeLocationDataLabelCompact locationOf(DataLabelCompact label)
154     {
155         return CodeLocationDataLabelCompact(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
156     }
157
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)
161     {
162         call.m_label = applyOffset(call.m_label);
163         return MacroAssembler::getLinkerCallReturnOffset(call);
164     }
165
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()
171     {
172         performFinalization();
173
174         return CodeRef(m_executableMemory);
175     }
176
177     CodePtr trampolineAt(Label label)
178     {
179         return CodePtr(MacroAssembler::AssemblerType_T::getRelocatedAddress(code(), applyOffset(label.m_label)));
180     }
181
182 #ifndef NDEBUG
183     void* debugAddress()
184     {
185         return m_code;
186     }
187     
188     size_t debugSize()
189     {
190         return m_size;
191     }
192 #endif
193
194 private:
195     template <typename T> T applyOffset(T src)
196     {
197 #if ENABLE(BRANCH_COMPACTION)
198         src.m_offset -= m_assembler->executableOffsetFor(src.m_offset);
199 #endif
200         return src;
201     }
202     
203     // Keep this private! - the underlying code should only be obtained externally via 
204     // finalizeCode() or finalizeCodeAddendum().
205     void* code()
206     {
207         return m_code;
208     }
209
210     void linkCode()
211     {
212         ASSERT(!m_code);
213 #if !ENABLE(BRANCH_COMPACTION)
214         m_executableMemory = m_assembler->m_assembler.executableCopy(*m_globalData);
215         if (!m_executableMemory)
216             return;
217         m_code = m_executableMemory->start();
218         m_size = m_assembler->m_assembler.codeSize();
219         ASSERT(m_code);
220 #else
221         size_t initialSize = m_assembler->m_assembler.codeSize();
222         m_executableMemory = m_globalData->executableAllocator.allocate(*m_globalData, initialSize);
223         if (!m_executableMemory)
224             return;
225         m_code = (uint8_t*)m_executableMemory->start();
226         ASSERT(m_code);
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);
230         int readPtr = 0;
231         int writePtr = 0;
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));
237             
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;
251             
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
257             else
258                 target = outData + jumpsToLink[i].to() - m_assembler->executableOffsetFor(jumpsToLink[i].to());
259             
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);
265                 if (delta) {
266                     writePtr -= delta;
267                     m_assembler->recordLinkOffsets(jumpsToLink[i].from() - delta, readPtr, readPtr - writePtr);
268                 }
269             }
270             jumpsToLink[i].setFrom(writePtr);
271         }
272         // Copy everything after the last jump
273         memcpy(outData + writePtr, inData + readPtr, initialSize - readPtr);
274         m_assembler->recordLinkOffsets(readPtr, initialSize, readPtr - writePtr);
275         
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);
280         }
281
282         jumpsToLink.clear();
283         m_size = writePtr + initialSize - readPtr;
284         m_executableMemory->shrink(m_size);
285
286 #if DUMP_LINK_STATISTICS
287         dumpLinkStatistics(m_code, initialSize, m_size);
288 #endif
289 #if DUMP_CODE
290         dumpCode(m_code, m_size);
291 #endif
292 #endif
293     }
294
295     void performFinalization()
296     {
297 #ifndef NDEBUG
298         ASSERT(!m_completed);
299         m_completed = true;
300 #endif
301
302         ExecutableAllocator::makeExecutable(code(), m_size);
303         ExecutableAllocator::cacheFlush(code(), m_size);
304     }
305
306 #if DUMP_LINK_STATISTICS
307     static void dumpLinkStatistics(void* code, size_t initialSize, size_t finalSize)
308     {
309         static unsigned linkCount = 0;
310         static unsigned totalInitialSize = 0;
311         static unsigned totalFinalSize = 0;
312         linkCount++;
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);
322     }
323 #endif
324     
325 #if DUMP_CODE
326     static void dumpCode(void* code, size_t size)
327     {
328 #if CPU(ARM_THUMB2)
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
332         //   otool -tv jit.o
333         static unsigned codeCount = 0;
334         unsigned short* tcode = static_cast<unsigned short*>(code);
335         size_t tsize = size / sizeof(short);
336         char nameBuf[128];
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"
340                "\t.globl\t%s\n"
341                "\t.align 2\n"
342                "\t.code 16\n"
343                "\t.thumb_func\t%s\n"
344                "# %p\n"
345                "%s:\n", nameBuf, nameBuf, code, nameBuf);
346         
347         for (unsigned i = 0; i < tsize; i++)
348             printf("\t.short\t0x%x\n", tcode[i]);
349 #endif
350     }
351 #endif
352     
353     RefPtr<ExecutableMemoryHandle> m_executableMemory;
354     size_t m_size;
355     void* m_code;
356     MacroAssembler* m_assembler;
357     JSGlobalData* m_globalData;
358 #ifndef NDEBUG
359     bool m_completed;
360 #endif
361 };
362
363 } // namespace JSC
364
365 #endif // ENABLE(ASSEMBLER)
366
367 #endif // LinkBuffer_h