Merge "Save and re-bind previously bounded texture when using cairo_gl_surface_set_bi...
[framework/web/webkit-efl.git] / Source / JavaScriptCore / assembler / LinkBuffer.h
1 /*
2  * Copyright (C) 2009, 2010, 2012 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 #define GLOBAL_THUNK_ID reinterpret_cast<void*>(static_cast<intptr_t>(-1))
35 #define REGEXP_CODE_ID reinterpret_cast<void*>(static_cast<intptr_t>(-2))
36
37 #include "JITCompilationEffort.h"
38 #include "MacroAssembler.h"
39 #include <wtf/DataLog.h>
40 #include <wtf/Noncopyable.h>
41
42 namespace JSC {
43
44 class JSGlobalData;
45
46 // LinkBuffer:
47 //
48 // This class assists in linking code generated by the macro assembler, once code generation
49 // has been completed, and the code has been copied to is final location in memory.  At this
50 // time pointers to labels within the code may be resolved, and relative offsets to external
51 // addresses may be fixed.
52 //
53 // Specifically:
54 //   * Jump objects may be linked to external targets,
55 //   * The address of Jump objects may taken, such that it can later be relinked.
56 //   * The return address of a Call may be acquired.
57 //   * The address of a Label pointing into the code may be resolved.
58 //   * The value referenced by a DataLabel may be set.
59 //
60 class LinkBuffer {
61     WTF_MAKE_NONCOPYABLE(LinkBuffer);
62     typedef MacroAssemblerCodeRef CodeRef;
63     typedef MacroAssemblerCodePtr CodePtr;
64     typedef MacroAssembler::Label Label;
65     typedef MacroAssembler::Jump Jump;
66     typedef MacroAssembler::PatchableJump PatchableJump;
67     typedef MacroAssembler::JumpList JumpList;
68     typedef MacroAssembler::Call Call;
69     typedef MacroAssembler::DataLabelCompact DataLabelCompact;
70     typedef MacroAssembler::DataLabel32 DataLabel32;
71     typedef MacroAssembler::DataLabelPtr DataLabelPtr;
72     typedef MacroAssembler::ConvertibleLoadLabel ConvertibleLoadLabel;
73 #if ENABLE(BRANCH_COMPACTION)
74     typedef MacroAssembler::LinkRecord LinkRecord;
75     typedef MacroAssembler::JumpLinkType JumpLinkType;
76 #endif
77
78 public:
79     LinkBuffer(JSGlobalData& globalData, MacroAssembler* masm, void* ownerUID, JITCompilationEffort effort = JITCompilationMustSucceed)
80         : m_size(0)
81 #if ENABLE(BRANCH_COMPACTION)
82         , m_initialSize(0)
83 #endif
84         , m_code(0)
85         , m_assembler(masm)
86         , m_globalData(&globalData)
87 #ifndef NDEBUG
88         , m_completed(false)
89         , m_effort(effort)
90 #endif
91     {
92         linkCode(ownerUID, effort);
93     }
94
95     ~LinkBuffer()
96     {
97         ASSERT(m_completed || (!m_executableMemory && m_effort == JITCompilationCanFail));
98     }
99     
100     bool didFailToAllocate() const
101     {
102         return !m_executableMemory;
103     }
104
105     bool isValid() const
106     {
107         return !didFailToAllocate();
108     }
109     
110     // These methods are used to link or set values at code generation time.
111
112     void link(Call call, FunctionPtr function)
113     {
114         ASSERT(call.isFlagSet(Call::Linkable));
115         call.m_label = applyOffset(call.m_label);
116         MacroAssembler::linkCall(code(), call, function);
117     }
118     
119     void link(Jump jump, CodeLocationLabel label)
120     {
121         jump.m_label = applyOffset(jump.m_label);
122         MacroAssembler::linkJump(code(), jump, label);
123     }
124
125     void link(JumpList list, CodeLocationLabel label)
126     {
127         for (unsigned i = 0; i < list.m_jumps.size(); ++i)
128             link(list.m_jumps[i], label);
129     }
130
131     void patch(DataLabelPtr label, void* value)
132     {
133         AssemblerLabel target = applyOffset(label.m_label);
134         MacroAssembler::linkPointer(code(), target, value);
135     }
136
137     void patch(DataLabelPtr label, CodeLocationLabel value)
138     {
139         AssemblerLabel target = applyOffset(label.m_label);
140         MacroAssembler::linkPointer(code(), target, value.executableAddress());
141     }
142
143     // These methods are used to obtain handles to allow the code to be relinked / repatched later.
144
145     CodeLocationCall locationOf(Call call)
146     {
147         ASSERT(call.isFlagSet(Call::Linkable));
148         ASSERT(!call.isFlagSet(Call::Near));
149         return CodeLocationCall(MacroAssembler::getLinkerAddress(code(), applyOffset(call.m_label)));
150     }
151
152     CodeLocationNearCall locationOfNearCall(Call call)
153     {
154         ASSERT(call.isFlagSet(Call::Linkable));
155         ASSERT(call.isFlagSet(Call::Near));
156         return CodeLocationNearCall(MacroAssembler::getLinkerAddress(code(), applyOffset(call.m_label)));
157     }
158
159     CodeLocationLabel locationOf(PatchableJump jump)
160     {
161         return CodeLocationLabel(MacroAssembler::getLinkerAddress(code(), applyOffset(jump.m_jump.m_label)));
162     }
163
164     CodeLocationLabel locationOf(Label label)
165     {
166         return CodeLocationLabel(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
167     }
168
169     CodeLocationDataLabelPtr locationOf(DataLabelPtr label)
170     {
171         return CodeLocationDataLabelPtr(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
172     }
173
174     CodeLocationDataLabel32 locationOf(DataLabel32 label)
175     {
176         return CodeLocationDataLabel32(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
177     }
178     
179     CodeLocationDataLabelCompact locationOf(DataLabelCompact label)
180     {
181         return CodeLocationDataLabelCompact(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
182     }
183
184     CodeLocationConvertibleLoad locationOf(ConvertibleLoadLabel label)
185     {
186         return CodeLocationConvertibleLoad(MacroAssembler::getLinkerAddress(code(), applyOffset(label.m_label)));
187     }
188
189     // This method obtains the return address of the call, given as an offset from
190     // the start of the code.
191     unsigned returnAddressOffset(Call call)
192     {
193         call.m_label = applyOffset(call.m_label);
194         return MacroAssembler::getLinkerCallReturnOffset(call);
195     }
196
197     uint32_t offsetOf(Label label)
198     {
199         return applyOffset(label.m_label).m_offset;
200     }
201
202     // Upon completion of all patching 'FINALIZE_CODE()' should be called once to
203     // complete generation of the code. Alternatively, call
204     // finalizeCodeWithoutDisassembly() directly if you have your own way of
205     // displaying disassembly.
206     
207     CodeRef finalizeCodeWithoutDisassembly();
208     CodeRef finalizeCodeWithDisassembly(const char* format, ...) WTF_ATTRIBUTE_PRINTF(2, 3);
209
210     CodePtr trampolineAt(Label label)
211     {
212         return CodePtr(MacroAssembler::AssemblerType_T::getRelocatedAddress(code(), applyOffset(label.m_label)));
213     }
214
215     void* debugAddress()
216     {
217         return m_code;
218     }
219     
220     size_t debugSize()
221     {
222         return m_size;
223     }
224
225 private:
226     template <typename T> T applyOffset(T src)
227     {
228 #if ENABLE(BRANCH_COMPACTION)
229         src.m_offset -= m_assembler->executableOffsetFor(src.m_offset);
230 #endif
231         return src;
232     }
233     
234     // Keep this private! - the underlying code should only be obtained externally via finalizeCode().
235     void* code()
236     {
237         return m_code;
238     }
239
240     void linkCode(void* ownerUID, JITCompilationEffort);
241
242     void performFinalization();
243
244 #if DUMP_LINK_STATISTICS
245     static void dumpLinkStatistics(void* code, size_t initialSize, size_t finalSize);
246 #endif
247     
248 #if DUMP_CODE
249     static void dumpCode(void* code, size_t);
250 #endif
251     
252     RefPtr<ExecutableMemoryHandle> m_executableMemory;
253     size_t m_size;
254 #if ENABLE(BRANCH_COMPACTION)
255     size_t m_initialSize;
256 #endif
257     void* m_code;
258     MacroAssembler* m_assembler;
259     JSGlobalData* m_globalData;
260 #ifndef NDEBUG
261     bool m_completed;
262     JITCompilationEffort m_effort;
263 #endif
264 };
265
266 #define FINALIZE_CODE_IF(condition, linkBufferReference, dataLogArgumentsForHeading)  \
267     (UNLIKELY((condition))                                              \
268      ? ((linkBufferReference).finalizeCodeWithDisassembly dataLogArgumentsForHeading) \
269      : (linkBufferReference).finalizeCodeWithoutDisassembly())
270
271 // Use this to finalize code, like so:
272 //
273 // CodeRef code = FINALIZE_CODE(linkBuffer, ("my super thingy number %d", number));
274 //
275 // Which, in disassembly mode, will print:
276 //
277 // Generated JIT code for my super thingy number 42:
278 //     Code at [0x123456, 0x234567]:
279 //         0x123456: mov $0, 0
280 //         0x12345a: ret
281 //
282 // ... and so on.
283 //
284 // Note that the dataLogArgumentsForHeading are only evaluated when showDisassembly
285 // is true, so you can hide expensive disassembly-only computations inside there.
286
287 #define FINALIZE_CODE(linkBufferReference, dataLogArgumentsForHeading)  \
288     FINALIZE_CODE_IF(Options::showDisassembly(), linkBufferReference, dataLogArgumentsForHeading)
289
290 } // namespace JSC
291
292 #endif // ENABLE(ASSEMBLER)
293
294 #endif // LinkBuffer_h