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 ***** */
30 #ifndef RepatchBuffer_h
31 #define RepatchBuffer_h
33 #include "assembler/wtf/Platform.h"
37 #include <assembler/MacroAssembler.h>
38 #include <moco/MocoStubs.h> //MOCO
44 // This class is used to modify code after code generation has been completed,
45 // and after the code has potentially already been executed. This mechanism is
46 // used to apply optimizations to the code.
49 typedef MacroAssemblerCodePtr CodePtr;
52 RepatchBuffer(const MacroAssemblerCodeRef &ref)
54 m_start = ref.m_code.executableAddress();
59 ExecutableAllocator::makeWritable(m_start, m_size);
62 RepatchBuffer(const JITCode &code)
64 m_start = code.start();
69 ExecutableAllocator::makeWritable(m_start, m_size);
75 ExecutableAllocator::makeExecutable(m_start, m_size);
78 void relink(CodeLocationJump jump, CodeLocationLabel destination)
80 MacroAssembler::repatchJump(jump, destination);
83 bool canRelink(CodeLocationJump jump, CodeLocationLabel destination)
85 return MacroAssembler::canRepatchJump(jump, destination);
88 void relink(CodeLocationCall call, CodeLocationLabel destination)
90 MacroAssembler::repatchCall(call, destination);
93 void relink(CodeLocationCall call, FunctionPtr destination)
95 MacroAssembler::repatchCall(call, destination);
98 void relink(CodeLocationNearCall nearCall, CodePtr destination)
100 MacroAssembler::repatchNearCall(nearCall, CodeLocationLabel(destination));
103 void relink(CodeLocationNearCall nearCall, CodeLocationLabel destination)
105 MacroAssembler::repatchNearCall(nearCall, destination);
108 void repatch(CodeLocationDataLabel32 dataLabel32, int32_t value)
110 MacroAssembler::repatchInt32(dataLabel32, value);
113 void repatch(CodeLocationDataLabelPtr dataLabelPtr, void* value)
115 MacroAssembler::repatchPointer(dataLabelPtr, value);
118 void repatchLoadPtrToLEA(CodeLocationInstruction instruction)
120 MacroAssembler::repatchLoadPtrToLEA(instruction);
123 void repatchLEAToLoadPtr(CodeLocationInstruction instruction)
125 MacroAssembler::repatchLEAToLoadPtr(instruction);
128 void relinkCallerToTrampoline(ReturnAddressPtr returnAddress, CodeLocationLabel label)
130 relink(CodeLocationCall(CodePtr(returnAddress)), label);
133 void relinkCallerToTrampoline(ReturnAddressPtr returnAddress, CodePtr newCalleeFunction)
135 relinkCallerToTrampoline(returnAddress, CodeLocationLabel(newCalleeFunction));
138 void relinkCallerToFunction(ReturnAddressPtr returnAddress, FunctionPtr function)
140 relink(CodeLocationCall(CodePtr(returnAddress)), function);
143 void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress, CodeLocationLabel label)
145 relink(CodeLocationNearCall(CodePtr(returnAddress)), label);
148 void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress, CodePtr newCalleeFunction)
150 relinkNearCallerToTrampoline(returnAddress, CodeLocationLabel(newCalleeFunction));
161 #endif // ENABLE(ASSEMBLER)
163 #endif // RepatchBuffer_h