2 * Copyright (C) 2009 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.
26 #ifndef RepatchBuffer_h
27 #define RepatchBuffer_h
31 #include "CodeBlock.h"
32 #include <MacroAssembler.h>
33 #include <wtf/Noncopyable.h>
39 // This class is used to modify code after code generation has been completed,
40 // and after the code has potentially already been executed. This mechanism is
41 // used to apply optimizations to the code.
44 typedef MacroAssemblerCodePtr CodePtr
;
47 RepatchBuffer(CodeBlock
* codeBlock
)
49 JITCode
& code
= codeBlock
->getJITCode();
50 m_start
= code
.start();
53 ExecutableAllocator::makeWritable(m_start
, m_size
);
58 ExecutableAllocator::makeExecutable(m_start
, m_size
);
61 void relink(CodeLocationJump jump
, CodeLocationLabel destination
)
63 MacroAssembler::repatchJump(jump
, destination
);
66 void relink(CodeLocationCall call
, CodeLocationLabel destination
)
68 MacroAssembler::repatchCall(call
, destination
);
71 void relink(CodeLocationCall call
, FunctionPtr destination
)
73 MacroAssembler::repatchCall(call
, destination
);
76 void relink(CodeLocationNearCall nearCall
, CodePtr destination
)
78 MacroAssembler::repatchNearCall(nearCall
, CodeLocationLabel(destination
));
81 void relink(CodeLocationNearCall nearCall
, CodeLocationLabel destination
)
83 MacroAssembler::repatchNearCall(nearCall
, destination
);
86 void repatch(CodeLocationDataLabel32 dataLabel32
, int32_t value
)
88 MacroAssembler::repatchInt32(dataLabel32
, value
);
91 void repatch(CodeLocationDataLabelCompact dataLabelCompact
, int32_t value
)
93 MacroAssembler::repatchCompact(dataLabelCompact
, value
);
96 void repatch(CodeLocationDataLabelPtr dataLabelPtr
, void* value
)
98 MacroAssembler::repatchPointer(dataLabelPtr
, value
);
101 void relinkCallerToTrampoline(ReturnAddressPtr returnAddress
, CodeLocationLabel label
)
103 relink(CodeLocationCall(CodePtr(returnAddress
)), label
);
106 void relinkCallerToTrampoline(ReturnAddressPtr returnAddress
, CodePtr newCalleeFunction
)
108 relinkCallerToTrampoline(returnAddress
, CodeLocationLabel(newCalleeFunction
));
111 void relinkCallerToFunction(ReturnAddressPtr returnAddress
, FunctionPtr function
)
113 relink(CodeLocationCall(CodePtr(returnAddress
)), function
);
116 void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress
, CodeLocationLabel label
)
118 relink(CodeLocationNearCall(CodePtr(returnAddress
)), label
);
121 void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress
, CodePtr newCalleeFunction
)
123 relinkNearCallerToTrampoline(returnAddress
, CodeLocationLabel(newCalleeFunction
));
126 void replaceWithLoad(CodeLocationConvertibleLoad label
)
128 MacroAssembler::replaceWithLoad(label
);
131 void replaceWithAddressComputation(CodeLocationConvertibleLoad label
)
133 MacroAssembler::replaceWithAddressComputation(label
);
136 void setLoadInstructionIsActive(CodeLocationConvertibleLoad label
, bool isActive
)
139 replaceWithLoad(label
);
141 replaceWithAddressComputation(label
);
144 static CodeLocationLabel
startOfBranchPtrWithPatchOnRegister(CodeLocationDataLabelPtr label
)
146 return MacroAssembler::startOfBranchPtrWithPatchOnRegister(label
);
149 static CodeLocationLabel
startOfPatchableBranchPtrWithPatchOnAddress(CodeLocationDataLabelPtr label
)
151 return MacroAssembler::startOfPatchableBranchPtrWithPatchOnAddress(label
);
154 void replaceWithJump(CodeLocationLabel instructionStart
, CodeLocationLabel destination
)
156 MacroAssembler::replaceWithJump(instructionStart
, destination
);
159 // This is a *bit* of a silly API, since we currently always also repatch the
160 // immediate after calling this. But I'm fine with that, since this just feels
162 void revertJumpReplacementToBranchPtrWithPatch(CodeLocationLabel instructionStart
, MacroAssembler::RegisterID reg
, void* value
)
164 MacroAssembler::revertJumpReplacementToBranchPtrWithPatch(instructionStart
, reg
, value
);
167 void revertJumpReplacementToPatchableBranchPtrWithPatch(CodeLocationLabel instructionStart
, MacroAssembler::Address address
, void* value
)
169 MacroAssembler::revertJumpReplacementToPatchableBranchPtrWithPatch(instructionStart
, address
, value
);
179 #endif // ENABLE(ASSEMBLER)
181 #endif // RepatchBuffer_h