HLE: Refactor ReplaceFunctionIfPossible
This commit is contained in:
parent
d28155a7bb
commit
ba6fea1c81
|
@ -200,6 +200,23 @@ HookFlag GetHookFlagsByIndex(u32 index)
|
||||||
return os_patches[index].flags;
|
return os_patches[index].flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TryReplaceFunctionResult TryReplaceFunction(u32 address)
|
||||||
|
{
|
||||||
|
const u32 hook_index = GetHookByFunctionAddress(address);
|
||||||
|
if (hook_index == 0)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
const HookType type = GetHookTypeByIndex(hook_index);
|
||||||
|
if (type != HookType::Start && type != HookType::Replace)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
const HookFlag flags = GetHookFlagsByIndex(hook_index);
|
||||||
|
if (!IsEnabled(flags))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return {type, hook_index};
|
||||||
|
}
|
||||||
|
|
||||||
bool IsEnabled(HookFlag flag)
|
bool IsEnabled(HookFlag flag)
|
||||||
{
|
{
|
||||||
return flag != HLE::HookFlag::Debug || Config::Get(Config::MAIN_ENABLE_DEBUGGING) ||
|
return flag != HLE::HookFlag::Debug || Config::Get(Config::MAIN_ENABLE_DEBUGGING) ||
|
||||||
|
|
|
@ -19,9 +19,9 @@ using HookFunction = void (*)(const Core::CPUThreadGuard&);
|
||||||
|
|
||||||
enum class HookType
|
enum class HookType
|
||||||
{
|
{
|
||||||
|
None, // Do not hook the function
|
||||||
Start, // Hook the beginning of the function and execute the function afterwards
|
Start, // Hook the beginning of the function and execute the function afterwards
|
||||||
Replace, // Replace the function with the HLE version
|
Replace, // Replace the function with the HLE version
|
||||||
None, // Do not hook the function
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class HookFlag
|
enum class HookFlag
|
||||||
|
@ -39,6 +39,14 @@ struct Hook
|
||||||
HookFlag flags;
|
HookFlag flags;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct TryReplaceFunctionResult
|
||||||
|
{
|
||||||
|
HookType type = HookType::None;
|
||||||
|
u32 hook_index = 0;
|
||||||
|
|
||||||
|
explicit operator bool() const { return type != HookType::None; }
|
||||||
|
};
|
||||||
|
|
||||||
void PatchFixedFunctions(Core::System& system);
|
void PatchFixedFunctions(Core::System& system);
|
||||||
void PatchFunctions(Core::System& system);
|
void PatchFunctions(Core::System& system);
|
||||||
void Clear();
|
void Clear();
|
||||||
|
@ -59,32 +67,8 @@ HookFlag GetHookFlagsByIndex(u32 index);
|
||||||
|
|
||||||
bool IsEnabled(HookFlag flag);
|
bool IsEnabled(HookFlag flag);
|
||||||
|
|
||||||
// Performs the backend-independent preliminary checking before calling a
|
// Performs the backend-independent preliminary checking for whether a function
|
||||||
// FunctionObject to do the actual replacing. Typically, this callback will
|
// can be HLEd. If it can be, the information needed for HLEing it is returned.
|
||||||
// be in the backend itself, containing the backend-specific portions
|
TryReplaceFunctionResult TryReplaceFunction(u32 address);
|
||||||
// required in replacing a function.
|
|
||||||
//
|
|
||||||
// fn may be any object that satisfies the FunctionObject concept in the C++
|
|
||||||
// standard library. That is, any lambda, object with an overloaded function
|
|
||||||
// call operator, or regular function pointer.
|
|
||||||
//
|
|
||||||
// fn must return a bool indicating whether or not function replacing occurred.
|
|
||||||
// fn must also accept a parameter list of the form: fn(u32 function, HLE::HookType type).
|
|
||||||
template <typename FunctionObject>
|
|
||||||
bool ReplaceFunctionIfPossible(u32 address, FunctionObject fn)
|
|
||||||
{
|
|
||||||
const u32 hook_index = GetHookByFunctionAddress(address);
|
|
||||||
if (hook_index == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
const HookType type = GetHookTypeByIndex(hook_index);
|
|
||||||
if (type != HookType::Start && type != HookType::Replace)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
const HookFlag flags = GetHookFlagsByIndex(hook_index);
|
|
||||||
if (!IsEnabled(flags))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return fn(hook_index, type);
|
|
||||||
}
|
|
||||||
} // namespace HLE
|
} // namespace HLE
|
||||||
|
|
|
@ -269,17 +269,19 @@ bool CachedInterpreter::CheckIdle(CachedInterpreter& cached_interpreter, u32 idl
|
||||||
|
|
||||||
bool CachedInterpreter::HandleFunctionHooking(u32 address)
|
bool CachedInterpreter::HandleFunctionHooking(u32 address)
|
||||||
{
|
{
|
||||||
return HLE::ReplaceFunctionIfPossible(address, [&](u32 hook_index, HLE::HookType type) {
|
const auto result = HLE::TryReplaceFunction(address);
|
||||||
m_code.emplace_back(WritePC, address);
|
if (!result)
|
||||||
m_code.emplace_back(Interpreter::HLEFunction, hook_index);
|
return false;
|
||||||
|
|
||||||
if (type != HLE::HookType::Replace)
|
m_code.emplace_back(WritePC, address);
|
||||||
return false;
|
m_code.emplace_back(Interpreter::HLEFunction, result.hook_index);
|
||||||
|
|
||||||
m_code.emplace_back(EndBlock, js.downcountAmount);
|
if (result.type != HLE::HookType::Replace)
|
||||||
m_code.emplace_back();
|
return false;
|
||||||
return true;
|
|
||||||
});
|
m_code.emplace_back(EndBlock, js.downcountAmount);
|
||||||
|
m_code.emplace_back();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CachedInterpreter::Jit(u32 address)
|
void CachedInterpreter::Jit(u32 address)
|
||||||
|
|
|
@ -106,10 +106,13 @@ void Interpreter::Trace(const UGeckoInstruction& inst)
|
||||||
|
|
||||||
bool Interpreter::HandleFunctionHooking(u32 address)
|
bool Interpreter::HandleFunctionHooking(u32 address)
|
||||||
{
|
{
|
||||||
return HLE::ReplaceFunctionIfPossible(address, [this](u32 hook_index, HLE::HookType type) {
|
const auto result = HLE::TryReplaceFunction(address);
|
||||||
HLEFunction(*this, hook_index);
|
if (!result)
|
||||||
return type != HLE::HookType::Start;
|
return false;
|
||||||
});
|
|
||||||
|
HLEFunction(*this, result.hook_index);
|
||||||
|
|
||||||
|
return result.type != HLE::HookType::Start;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Interpreter::SingleStepInner()
|
int Interpreter::SingleStepInner()
|
||||||
|
|
|
@ -1238,17 +1238,19 @@ void Jit64::IntializeSpeculativeConstants()
|
||||||
|
|
||||||
bool Jit64::HandleFunctionHooking(u32 address)
|
bool Jit64::HandleFunctionHooking(u32 address)
|
||||||
{
|
{
|
||||||
return HLE::ReplaceFunctionIfPossible(address, [&](u32 hook_index, HLE::HookType type) {
|
const auto result = HLE::TryReplaceFunction(address);
|
||||||
HLEFunction(hook_index);
|
if (!result)
|
||||||
|
return false;
|
||||||
|
|
||||||
if (type != HLE::HookType::Replace)
|
HLEFunction(result.hook_index);
|
||||||
return false;
|
|
||||||
|
|
||||||
MOV(32, R(RSCRATCH), PPCSTATE(npc));
|
if (result.type != HLE::HookType::Replace)
|
||||||
js.downcountAmount += js.st.numCycles;
|
return false;
|
||||||
WriteExitDestInRSCRATCH();
|
|
||||||
return true;
|
MOV(32, R(RSCRATCH), PPCSTATE(npc));
|
||||||
});
|
js.downcountAmount += js.st.numCycles;
|
||||||
|
WriteExitDestInRSCRATCH();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogGeneratedX86(size_t size, const PPCAnalyst::CodeBuffer& code_buffer, const u8* normalEntry,
|
void LogGeneratedX86(size_t size, const PPCAnalyst::CodeBuffer& code_buffer, const u8* normalEntry,
|
||||||
|
|
|
@ -696,17 +696,19 @@ void JitArm64::WriteConditionalExceptionExit(int exception, ARM64Reg temp_gpr, A
|
||||||
|
|
||||||
bool JitArm64::HandleFunctionHooking(u32 address)
|
bool JitArm64::HandleFunctionHooking(u32 address)
|
||||||
{
|
{
|
||||||
return HLE::ReplaceFunctionIfPossible(address, [&](u32 hook_index, HLE::HookType type) {
|
const auto result = HLE::TryReplaceFunction(address);
|
||||||
HLEFunction(hook_index);
|
if (!result)
|
||||||
|
return false;
|
||||||
|
|
||||||
if (type != HLE::HookType::Replace)
|
HLEFunction(result.hook_index);
|
||||||
return false;
|
|
||||||
|
|
||||||
LDR(IndexType::Unsigned, DISPATCHER_PC, PPC_REG, PPCSTATE_OFF(npc));
|
if (result.type != HLE::HookType::Replace)
|
||||||
js.downcountAmount += js.st.numCycles;
|
return false;
|
||||||
WriteExit(DISPATCHER_PC);
|
|
||||||
return true;
|
LDR(IndexType::Unsigned, DISPATCHER_PC, PPC_REG, PPCSTATE_OFF(npc));
|
||||||
});
|
js.downcountAmount += js.st.numCycles;
|
||||||
|
WriteExit(DISPATCHER_PC);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JitArm64::DumpCode(const u8* start, const u8* end)
|
void JitArm64::DumpCode(const u8* start, const u8* end)
|
||||||
|
|
Loading…
Reference in New Issue