1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=79:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Copyright (C) 2009 Apple Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * ***** END LICENSE BLOCK ***** */
33 #include "assembler/wtf/Platform.h"
37 #include <assembler/MacroAssembler.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 typedef MacroAssemblerCodeRef CodeRef;
57 typedef MacroAssembler::Label Label;
58 typedef MacroAssembler::Jump Jump;
59 typedef MacroAssembler::JumpList JumpList;
60 typedef MacroAssembler::Call Call;
61 typedef MacroAssembler::DataLabel32 DataLabel32;
62 typedef MacroAssembler::DataLabelPtr DataLabelPtr;
65 // Note: Initialization sequence is significant, since executablePool is a PassRefPtr.
66 // First, executablePool is copied into m_executablePool, then the initialization of
67 // m_code uses m_executablePool, *not* executablePool, since this is no longer valid.
68 LinkBuffer(MacroAssembler* masm, ExecutablePool* executablePool)
69 : m_executablePool(executablePool)
70 , m_code(executableCopy(*masm, executablePool))
71 , m_size(masm->m_assembler.size())
79 : m_executablePool(NULL)
88 LinkBuffer(uint8* ncode, size_t size)
89 : m_executablePool(NULL)
100 ASSERT(!m_executablePool || m_completed);
103 // These methods are used to link or set values at code generation time.
105 void link(Call call, FunctionPtr function)
107 ASSERT(call.isFlagSet(Call::Linkable));
108 MacroAssembler::linkCall(code(), call, function);
111 void link(Jump jump, CodeLocationLabel label)
113 MacroAssembler::linkJump(code(), jump, label);
116 void link(JumpList list, CodeLocationLabel label)
118 for (unsigned i = 0; i < list.m_jumps.length(); ++i)
119 MacroAssembler::linkJump(code(), list.m_jumps[i], label);
122 void patch(DataLabelPtr label, void* value)
124 MacroAssembler::linkPointer(code(), label.m_label, value);
127 void patch(DataLabelPtr label, CodeLocationLabel value)
129 MacroAssembler::linkPointer(code(), label.m_label, value.executableAddress());
132 // These methods are used to obtain handles to allow the code to be relinked / repatched later.
134 CodeLocationCall locationOf(Call call)
136 ASSERT(call.isFlagSet(Call::Linkable));
137 ASSERT(!call.isFlagSet(Call::Near));
138 return CodeLocationCall(MacroAssembler::getLinkerAddress(code(), call.m_jmp));
141 CodeLocationJump locationOf(Jump j)
143 return CodeLocationJump(MacroAssembler::getLinkerAddress(code(), j.m_jmp));
146 CodeLocationNearCall locationOfNearCall(Call call)
148 ASSERT(call.isFlagSet(Call::Linkable));
149 ASSERT(call.isFlagSet(Call::Near));
150 return CodeLocationNearCall(MacroAssembler::getLinkerAddress(code(), call.m_jmp));
153 CodeLocationLabel locationOf(Label label)
155 return CodeLocationLabel(MacroAssembler::getLinkerAddress(code(), label.m_label));
158 CodeLocationDataLabelPtr locationOf(DataLabelPtr label)
160 return CodeLocationDataLabelPtr(MacroAssembler::getLinkerAddress(code(), label.m_label));
163 CodeLocationDataLabel32 locationOf(DataLabel32 label)
165 return CodeLocationDataLabel32(MacroAssembler::getLinkerAddress(code(), label.m_label));
168 // This method obtains the return address of the call, given as an offset from
169 // the start of the code.
170 unsigned returnAddressOffset(Call call)
172 return MacroAssembler::getLinkerCallReturnOffset(call);
175 // Upon completion of all patching either 'finalizeCode()' or 'finalizeCodeAddendum()' should be called
176 // once to complete generation of the code. 'finalizeCode()' is suited to situations
177 // where the executable pool must also be retained, the lighter-weight 'finalizeCodeAddendum()' is
178 // suited to adding to an existing allocation.
179 CodeRef finalizeCode()
181 performFinalization();
183 return CodeRef(m_code, m_executablePool, m_size);
185 CodeLocationLabel finalizeCodeAddendum()
187 performFinalization();
189 return CodeLocationLabel(code());
193 // Keep this private! - the underlying code should only be obtained externally via
194 // finalizeCode() or finalizeCodeAddendum().
200 void *executableCopy(MacroAssembler &masm, ExecutablePool *pool)
202 return masm.m_assembler.executableCopy(pool);
205 void performFinalization()
208 ASSERT(!m_completed);
212 ExecutableAllocator::makeExecutable(code(), m_size);
213 ExecutableAllocator::cacheFlush(code(), m_size);
216 ExecutablePool* m_executablePool;
226 #endif // ENABLE(ASSEMBLER)
228 #endif // LinkBuffer_h