Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions ps2xRecomp/src/lib/code_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace ps2recomp

namespace ps2recomp
{
static bool isReservedCxxIdentifier(const std::string& name)
static bool isReservedCxxIdentifier(const std::string &name)
{
if (name.size() >= 2 && name[0] == '_' && name[1] == '_')
return true;
Expand All @@ -37,7 +37,7 @@ namespace ps2recomp
return false;
}

static bool isReservedCxxKeyword(const std::string& name)
static bool isReservedCxxKeyword(const std::string &name)
{
return kKeywords.contains(name);
}
Expand Down Expand Up @@ -75,7 +75,7 @@ namespace ps2recomp
}

return "";
}
}

std::string CodeGenerator::sanitizeFunctionName(const std::string &name) const
{
Expand Down Expand Up @@ -252,13 +252,13 @@ namespace ps2recomp
std::string funcName = getFunctionName(target);
bool isInternalTarget = internalTargets.contains(target);

if (!funcName.empty())
if (isInternalTarget)
{
targetAction = fmt::format("{}(rdram, ctx, runtime); return;", funcName);
targetAction = fmt::format("goto label_{:x};", target);
}
else if (isInternalTarget)
else if (!funcName.empty())
{
targetAction = fmt::format("goto label_{:x};", target);
targetAction = fmt::format("{}(rdram, ctx, runtime); return;", funcName);
}
else
{
Expand Down Expand Up @@ -325,23 +325,15 @@ namespace ps2recomp

if (target >= function.start && target < function.end)
{
std::string funcName = getFunctionName(target);
if (funcName.empty())
{
targets.insert(target);
}
targets.insert(target);
}
}
else if (isStaticJump)
{
uint32_t target = (inst.address & 0xF0000000) | (inst.target << 2);
if (target >= function.start && target < function.end)
{
std::string funcName = getFunctionName(target);
if (funcName.empty())
{
targets.insert(target);
}
targets.insert(target);
}
}
}
Expand Down
35 changes: 22 additions & 13 deletions ps2xRecomp/src/lib/ps2_recompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,24 @@ namespace ps2recomp
{
m_functionRenames.clear();

auto makeName = [&](const Function &function) -> std::string
{
std::string sanitized = sanitizeFunctionName(function.name);
if (sanitized.empty())
{
std::stringstream ss;
ss << "func_" << std::hex << function.start;
sanitized = ss.str();
}
return sanitized;
};

std::unordered_map<std::string, int> nameCounts;
for (const auto &function : m_functions)
{
if (!function.isRecompiled && !function.isStub)
continue;
std::string sanitized = sanitizeFunctionName(function.name);
std::string sanitized = makeName(function);
nameCounts[sanitized]++;
}

Expand All @@ -232,22 +244,19 @@ namespace ps2recomp
if (!function.isRecompiled && !function.isStub)
continue;

std::string sanitized = sanitizeFunctionName(function.name);
std::string sanitized = makeName(function);
bool isDuplicate = nameCounts[sanitized] > 1;

if (isDuplicate || sanitized != function.name)
std::stringstream ss;
if (isDuplicate)
{
std::stringstream ss;
if (isDuplicate)
{
ss << sanitized << "_0x" << std::hex << function.start;
}
else
{
ss << sanitized;
}
m_functionRenames[function.start] = ss.str();
ss << sanitized << "_0x" << std::hex << function.start;
}
else
{
ss << sanitized;
}
m_functionRenames[function.start] = ss.str();
}

if (m_codeGenerator)
Expand Down
53 changes: 53 additions & 0 deletions ps2xRuntime/include/ThreadNaming.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <string_view>
#include <string>

#if defined(_WIN32)
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WINAPI
#define WINAPI __stdcall
#endif

extern "C"
{
typedef void *HANDLE;
typedef void *HMODULE;
typedef const wchar_t *PCWSTR;
typedef long HRESULT;

__declspec(dllimport) HMODULE WINAPI GetModuleHandleW(const wchar_t *lpModuleName);
__declspec(dllimport) void *WINAPI GetProcAddress(HMODULE hModule, const char *lpProcName);
__declspec(dllimport) HANDLE WINAPI GetCurrentThread(void);
}
#elif defined(__APPLE__) || defined(__linux__)
#include <pthread.h>
#endif

namespace ThreadNaming
{
inline void SetCurrentThreadName(std::string_view name)
{
#if defined(_WIN32)
using SetThreadDescriptionFn = HRESULT(WINAPI*)(HANDLE, PCWSTR);

HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll");
auto setThreadDescription =
reinterpret_cast<SetThreadDescriptionFn>(::GetProcAddress(kernel32, "SetThreadDescription"));

if (setThreadDescription)
{
std::wstring wname(name.begin(), name.end());
setThreadDescription(::GetCurrentThread(), wname.c_str());
}

#elif defined(__APPLE__)
pthread_setname_np(name.data());

#elif defined(__linux__)
pthread_setname_np(pthread_self(), name.data());
#endif
}
}
9 changes: 2 additions & 7 deletions ps2xRuntime/src/lib/ps2_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <thread>
#include <unordered_map>
#include "raylib.h"
#include <ThreadNaming.h>

#define ELF_MAGIC 0x464C457F // "\x7FELF" in little endian
#define ET_EXEC 2 // Executable file
Expand Down Expand Up @@ -107,12 +108,7 @@ static void UploadFrame(Texture2D &tex, PS2Runtime *rt)
return;
}

constexpr uint32_t DEFAULT_FB_ADDR = 0x00100000;
uint32_t baseBytes = fbp * 2048;
if (fbp == 0)
{
baseBytes = DEFAULT_FB_ADDR;
}
uint32_t strideBytes = (fbw ? fbw : (FB_WIDTH / 64)) * 64 * 4;
uint8_t *rdram = rt->memory().getRDRAM();
uint8_t *gsvram = rt->memory().getGSVRAM();
Expand Down Expand Up @@ -165,7 +161,6 @@ static void UploadFrame(Texture2D &tex, PS2Runtime *rt)
UpdateTexture(tex, scratch.data());
}


PS2Runtime::PS2Runtime()
{
std::memset(&m_cpuContext, 0, sizeof(m_cpuContext));
Expand Down Expand Up @@ -322,7 +317,6 @@ void PS2Runtime::SignalException(R5900Context *ctx, PS2Exception exception)
}
}


void PS2Runtime::executeVU0Microprogram(uint8_t *rdram, R5900Context *ctx, uint32_t address)
{
static std::unordered_map<uint32_t, int> seen;
Expand Down Expand Up @@ -426,6 +420,7 @@ void PS2Runtime::run()

std::thread gameThread([&, entryPoint]()
{
ThreadNaming::SetCurrentThreadName("GameThread");
try
{
entryPoint(m_memory.getRDRAM(), &m_cpuContext, this);
Expand Down