#ifndef RepatchBuffer_h
#define RepatchBuffer_h
#if ENABLE(JIT)
#include "CodeBlock.h"
#include <MacroAssembler.h>
#include <wtf/Noncopyable.h>
namespace JSC {
class RepatchBuffer {
typedef MacroAssemblerCodePtr CodePtr;
public:
RepatchBuffer(CodeBlock* codeBlock)
: m_codeBlock(codeBlock)
{
#if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
RefPtr<JITCode> code = codeBlock->jitCode();
m_start = code->start();
m_size = code->size();
ExecutableAllocator::makeWritable(m_start, m_size);
#endif
}
~RepatchBuffer()
{
#if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
ExecutableAllocator::makeExecutable(m_start, m_size);
#endif
}
CodeBlock* codeBlock() const { return m_codeBlock; }
void relink(CodeLocationJump jump, CodeLocationLabel destination)
{
MacroAssembler::repatchJump(jump, destination);
}
void relink(CodeLocationCall call, CodeLocationLabel destination)
{
MacroAssembler::repatchCall(call, destination);
}
void relink(CodeLocationCall call, FunctionPtr destination)
{
MacroAssembler::repatchCall(call, destination);
}
void relink(CodeLocationNearCall nearCall, CodePtr destination)
{
MacroAssembler::repatchNearCall(nearCall, CodeLocationLabel(destination));
}
void relink(CodeLocationNearCall nearCall, CodeLocationLabel destination)
{
MacroAssembler::repatchNearCall(nearCall, destination);
}
void repatch(CodeLocationDataLabel32 dataLabel32, int32_t value)
{
MacroAssembler::repatchInt32(dataLabel32, value);
}
void repatch(CodeLocationDataLabelCompact dataLabelCompact, int32_t value)
{
MacroAssembler::repatchCompact(dataLabelCompact, value);
}
void repatch(CodeLocationDataLabelPtr dataLabelPtr, void* value)
{
MacroAssembler::repatchPointer(dataLabelPtr, value);
}
void relinkCallerToTrampoline(ReturnAddressPtr returnAddress, CodeLocationLabel label)
{
relink(CodeLocationCall(CodePtr(returnAddress)), label);
}
void relinkCallerToTrampoline(ReturnAddressPtr returnAddress, CodePtr newCalleeFunction)
{
relinkCallerToTrampoline(returnAddress, CodeLocationLabel(newCalleeFunction));
}
void relinkCallerToFunction(ReturnAddressPtr returnAddress, FunctionPtr function)
{
relink(CodeLocationCall(CodePtr(returnAddress)), function);
}
void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress, CodeLocationLabel label)
{
relink(CodeLocationNearCall(CodePtr(returnAddress)), label);
}
void relinkNearCallerToTrampoline(ReturnAddressPtr returnAddress, CodePtr newCalleeFunction)
{
relinkNearCallerToTrampoline(returnAddress, CodeLocationLabel(newCalleeFunction));
}
void replaceWithLoad(CodeLocationConvertibleLoad label)
{
MacroAssembler::replaceWithLoad(label);
}
void replaceWithAddressComputation(CodeLocationConvertibleLoad label)
{
MacroAssembler::replaceWithAddressComputation(label);
}
void setLoadInstructionIsActive(CodeLocationConvertibleLoad label, bool isActive)
{
if (isActive)
replaceWithLoad(label);
else
replaceWithAddressComputation(label);
}
static CodeLocationLabel startOfBranchPtrWithPatchOnRegister(CodeLocationDataLabelPtr label)
{
return MacroAssembler::startOfBranchPtrWithPatchOnRegister(label);
}
static CodeLocationLabel startOfPatchableBranchPtrWithPatchOnAddress(CodeLocationDataLabelPtr label)
{
return MacroAssembler::startOfPatchableBranchPtrWithPatchOnAddress(label);
}
static CodeLocationLabel startOfPatchableBranch32WithPatchOnAddress(CodeLocationDataLabel32 label)
{
return MacroAssembler::startOfPatchableBranch32WithPatchOnAddress(label);
}
void replaceWithJump(CodeLocationLabel instructionStart, CodeLocationLabel destination)
{
MacroAssembler::replaceWithJump(instructionStart, destination);
}
void revertJumpReplacementToBranchPtrWithPatch(CodeLocationLabel instructionStart, MacroAssembler::RegisterID reg, void* value)
{
MacroAssembler::revertJumpReplacementToBranchPtrWithPatch(instructionStart, reg, value);
}
void revertJumpReplacementToPatchableBranchPtrWithPatch(CodeLocationLabel instructionStart, MacroAssembler::Address address, void* value)
{
MacroAssembler::revertJumpReplacementToPatchableBranchPtrWithPatch(instructionStart, address, value);
}
void revertJumpReplacementToPatchableBranch32WithPatch(CodeLocationLabel instructionStart, MacroAssembler::Address address, int32_t value)
{
MacroAssembler::revertJumpReplacementToPatchableBranch32WithPatch(instructionStart, address, value);
}
private:
CodeBlock* m_codeBlock;
#if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
void* m_start;
size_t m_size;
#endif
};
}
#endif // ENABLE(ASSEMBLER)
#endif // RepatchBuffer_h