/*
* Copyright (C) 2009-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include
namespace JSC {
enum class NearCallMode : uint8_t { Regular, Tail };
template class CodeLocationInstruction;
template class CodeLocationLabel;
template class CodeLocationJump;
template class CodeLocationCall;
template class CodeLocationNearCall;
template class CodeLocationDataLabelCompact;
template class CodeLocationDataLabel32;
template class CodeLocationDataLabelPtr;
template class CodeLocationConvertibleLoad;
// The CodeLocation* types are all pretty much do-nothing wrappers around
// CodePtr. These classes only exist to provide type-safety when linking and
// patching code.
//
// The one new piece of functionallity introduced by these classes is the
// ability to create (or put another way, to re-discover) another CodeLocation
// at an offset from one you already know. When patching code to optimize it
// we often want to patch a number of instructions that are short, fixed
// offsets apart. To reduce memory overhead we will only retain a pointer to
// one of the instructions, and we will use the *AtOffset methods provided by
// CodeLocationCommon to find the other points in the code to modify.
template
class CodeLocationCommon : public CodePtr {
using Base = CodePtr;
public:
template CodeLocationInstruction instructionAtOffset(int offset);
template CodeLocationLabel labelAtOffset(int offset);
template CodeLocationJump jumpAtOffset(int offset);
template CodeLocationCall callAtOffset(int offset);
template CodeLocationNearCall nearCallAtOffset(int offset, NearCallMode);
template CodeLocationDataLabelPtr dataLabelPtrAtOffset(int offset);
template CodeLocationDataLabel32 dataLabel32AtOffset(int offset);
template CodeLocationDataLabelCompact dataLabelCompactAtOffset(int offset);
template CodeLocationConvertibleLoad convertibleLoadAtOffset(int offset);
template
T dataLocation() const { return Base::template dataLocation(); }
template
requires (std::is_base_of_v)
operator T()
{
return T(CodePtr::fromTaggedPtr(this->taggedPtr()));
}
protected:
CodeLocationCommon()
{
}
CodeLocationCommon(CodePtr location)
: CodePtr(location)
{
}
};
template
class CodeLocationInstruction : public CodeLocationCommon {
public:
CodeLocationInstruction() { }
explicit CodeLocationInstruction(CodePtr location)
: CodeLocationCommon(location) { }
explicit CodeLocationInstruction(void* location)
: CodeLocationCommon(CodePtr(location)) { }
};
template
class CodeLocationLabel : public CodeLocationCommon {
public:
CodeLocationLabel() { }
explicit CodeLocationLabel(CodePtr location)
: CodeLocationCommon(location) { }
explicit CodeLocationLabel(void* location)
: CodeLocationCommon(CodePtr(location)) { }
template
CodeLocationLabel retagged() { return CodeLocationLabel(CodeLocationCommon::template retagged()); }
template
T untaggedPtr() const { return CodeLocationCommon::template untaggedPtr(); }
template
T dataLocation() const { return CodeLocationCommon::template dataLocation(); }
};
template
class CodeLocationJump : public CodeLocationCommon {
public:
CodeLocationJump() { }
explicit CodeLocationJump(CodePtr location)
: CodeLocationCommon(location) { }
explicit CodeLocationJump(void* location)
: CodeLocationCommon(CodePtr(location)) { }
template
CodeLocationJump retagged() { return CodeLocationJump(CodePtr::template retagged()); }
};
template
class CodeLocationCall : public CodeLocationCommon {
public:
CodeLocationCall() { }
explicit CodeLocationCall(CodePtr location)
: CodeLocationCommon(location) { }
explicit CodeLocationCall(void* location)
: CodeLocationCommon(CodePtr(location)) { }
template
CodeLocationCall retagged() { return CodeLocationCall(CodeLocationCommon::template retagged()); }
};
template
class CodeLocationNearCall : public CodeLocationCommon {
public:
CodeLocationNearCall() { }
explicit CodeLocationNearCall(CodePtr location, NearCallMode callMode)
: CodeLocationCommon(location), m_callMode(callMode) { }
explicit CodeLocationNearCall(void* location, NearCallMode callMode)
: CodeLocationCommon(CodePtr(location)), m_callMode(callMode) { }
NearCallMode callMode() { return m_callMode; }
private:
NearCallMode m_callMode { NearCallMode::Regular };
};
template
class CodeLocationDataLabel32 : public CodeLocationCommon {
public:
CodeLocationDataLabel32() { }
explicit CodeLocationDataLabel32(CodePtr location)
: CodeLocationCommon(location) { }
explicit CodeLocationDataLabel32(void* location)
: CodeLocationCommon(CodePtr(location)) { }
};
template
class CodeLocationDataLabelCompact : public CodeLocationCommon {
public:
CodeLocationDataLabelCompact() { }
explicit CodeLocationDataLabelCompact(CodePtr location)
: CodeLocationCommon(location) { }
explicit CodeLocationDataLabelCompact(void* location)
: CodeLocationCommon(CodePtr(location)) { }
};
template
class CodeLocationDataLabelPtr : public CodeLocationCommon {
public:
CodeLocationDataLabelPtr() { }
explicit CodeLocationDataLabelPtr(CodePtr location)
: CodeLocationCommon(location) { }
explicit CodeLocationDataLabelPtr(void* location)
: CodeLocationCommon(CodePtr(location)) { }
};
template
class CodeLocationConvertibleLoad : public CodeLocationCommon {
public:
CodeLocationConvertibleLoad() { }
explicit CodeLocationConvertibleLoad(CodePtr location)
: CodeLocationCommon(location) { }
explicit CodeLocationConvertibleLoad(void* location)
: CodeLocationCommon(CodePtr(location)) { }
};
template
template
inline CodeLocationInstruction CodeLocationCommon::instructionAtOffset(int offset)
{
ASSERT_VALID_CODE_OFFSET(offset);
return CodeLocationInstruction(tagCodePtr(dataLocation() + offset));
}
template
template
inline CodeLocationLabel CodeLocationCommon::labelAtOffset(int offset)
{
ASSERT_VALID_CODE_OFFSET(offset);
return CodeLocationLabel(tagCodePtr(dataLocation() + offset));
}
template
template
inline CodeLocationJump CodeLocationCommon::jumpAtOffset(int offset)
{
ASSERT_VALID_CODE_OFFSET(offset);
return CodeLocationJump(tagCodePtr(dataLocation() + offset));
}
template
template
inline CodeLocationCall CodeLocationCommon::callAtOffset(int offset)
{
ASSERT_VALID_CODE_OFFSET(offset);
return CodeLocationCall(tagCodePtr(dataLocation() + offset));
}
template
template
inline CodeLocationNearCall CodeLocationCommon::nearCallAtOffset(int offset, NearCallMode callMode)
{
ASSERT_VALID_CODE_OFFSET(offset);
return CodeLocationNearCall(tagCodePtr(dataLocation() + offset), callMode);
}
template
template
inline CodeLocationDataLabelPtr CodeLocationCommon::dataLabelPtrAtOffset(int offset)
{
ASSERT_VALID_CODE_OFFSET(offset);
return CodeLocationDataLabelPtr(tagCodePtr(dataLocation() + offset));
}
template
template
inline CodeLocationDataLabel32 CodeLocationCommon::dataLabel32AtOffset(int offset)
{
ASSERT_VALID_CODE_OFFSET(offset);
return CodeLocationDataLabel32(tagCodePtr(dataLocation() + offset));
}
template
template
inline CodeLocationDataLabelCompact CodeLocationCommon::dataLabelCompactAtOffset(int offset)
{
ASSERT_VALID_CODE_OFFSET(offset);
return CodeLocationDataLabelCompact(tagCodePtr(dataLocation() + offset));
}
template
template
inline CodeLocationConvertibleLoad CodeLocationCommon::convertibleLoadAtOffset(int offset)
{
ASSERT_VALID_CODE_OFFSET(offset);
return CodeLocationConvertibleLoad(tagCodePtr(dataLocation() + offset));
}
} // namespace JSC