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
31 changes: 29 additions & 2 deletions ps2xRecomp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
cmake_minimum_required(VERSION 3.20)

project(PS2Recomp VERSION 0.1.0 LANGUAGES CXX)
project(PS2Recomp VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

include(FetchContent)

Expand Down Expand Up @@ -54,6 +56,30 @@ FetchContent_MakeAvailable(libdwarf)

set(LIBDWARF_INCLUDE_DIR "${libdwarf_SOURCE_DIR}/src/lib/libdwarf")

FetchContent_Declare(
rabbitizer
GIT_REPOSITORY https://github.com/Decompollaborate/rabbitizer.git
GIT_TAG 1.14.3
)
FetchContent_MakeAvailable(rabbitizer)

if (NOT TARGET rabbitizer)
file(GLOB_RECURSE RABBITIZER_SOURCES CONFIGURE_DEPENDS
"${rabbitizer_SOURCE_DIR}/src/analysis/*.c"
"${rabbitizer_SOURCE_DIR}/src/common/*.c"
"${rabbitizer_SOURCE_DIR}/src/instructions/*.c"
"${rabbitizer_SOURCE_DIR}/src/instructions/*/*.c"
)

add_library(rabbitizer STATIC ${RABBITIZER_SOURCES})

target_include_directories(rabbitizer PUBLIC
"${rabbitizer_SOURCE_DIR}/include"
"${rabbitizer_SOURCE_DIR}/tables"
"${rabbitizer_SOURCE_DIR}/tables/tables"
)
endif()

file(GLOB_RECURSE PS2RECOMP_LIB_SOURCES CONFIGURE_DEPENDS
src/lib/*.cpp
)
Expand Down Expand Up @@ -87,6 +113,7 @@ PUBLIC
fmt::fmt
toml11::toml11
dwarf
rabbitizer
)

file(GLOB_RECURSE PS2RECOMP_EXE_SOURCES CONFIGURE_DEPENDS
Expand All @@ -108,4 +135,4 @@ install(TARGETS ps2_recomp ps2_recomp_lib

install(DIRECTORY include/
DESTINATION include
)
)
2 changes: 1 addition & 1 deletion ps2xRecomp/include/ps2recomp/code_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace ps2recomp

const Symbol *findSymbolByAddress(uint32_t address) const;
std::string getFunctionName(uint32_t address) const;
std::string getGeneratedFunctionName(const Function &function);
std::string sanitizeFunctionName(const std::string& name) const;
};

}
Expand Down
6 changes: 3 additions & 3 deletions ps2xRecomp/include/ps2recomp/instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ namespace ps2recomp
OPCODE_SDC2 = 0x3E, // PS2 specific Store Quadword from Coprocessor 2 (VU0) - Overrides standard MIPS SDC2
OPCODE_SD = 0x3F, // Store Doubleword

// OPCODE_LQC2 = 0x36,
// OPCODE_SQC2 = 0x3E
OPCODE_LQC2 = OPCODE_LDC2,
OPCODE_SQC2 = OPCODE_SDC2
};

// SPECIAL Function (bits 5-0) for OPCODE_SPECIAL
Expand Down Expand Up @@ -775,4 +775,4 @@ namespace ps2recomp

} // namespace ps2recomp

#endif // PS2RECOMP_INSTRUCTIONS_H
#endif // PS2RECOMP_INSTRUCTIONS_H
71 changes: 35 additions & 36 deletions ps2xRecomp/src/lib/code_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ namespace ps2recomp

namespace ps2recomp
{
static bool isReservedCxxIdentifier(const std::string& name)
{
if (name.size() >= 2 && name[0] == '_' && name[1] == '_')
return true;
if (!name.empty() && name[0] == '_' && std::isupper(static_cast<unsigned char>(name[1])))
return true;
return false;
}

static bool isReservedCxxKeyword(const std::string& name)
{
return kKeywords.contains(name);
}

CodeGenerator::CodeGenerator(const std::vector<Symbol> &symbols)
{
for (auto &symbol : symbols)
Expand Down Expand Up @@ -57,27 +71,13 @@ namespace ps2recomp
const Symbol *sym = findSymbolByAddress(address);
if (sym && sym->isFunction)
{
return sym->name;
return CodeGenerator::sanitizeFunctionName(sym->name);
}

return "";
}
}

static bool isReservedCxxIdentifier(const std::string &name)
{
if (name.size() >= 2 && name[0] == '_' && name[1] == '_')
return true;
if (!name.empty() && name[0] == '_' && std::isupper(static_cast<unsigned char>(name[1])))
return true;
return false;
}

static bool isReservedCxxKeyword(const std::string &name)
{
return kKeywords.contains(name);
}

static std::string sanitizeFunctionName(const std::string &name)
std::string CodeGenerator::sanitizeFunctionName(const std::string &name) const
{
std::string sanitized = name;

Expand All @@ -96,16 +96,6 @@ namespace ps2recomp
return "ps2_" + sanitized;
}

std::string CodeGenerator::getGeneratedFunctionName(const Function &function)
{
std::string name = getFunctionName(function.start);
if (name.empty())
{
name = sanitizeFunctionName(function.name);
}
return name;
}

std::string CodeGenerator::handleBranchDelaySlots(const Instruction &branchInst, const Instruction &delaySlot,
const Function &function, const std::unordered_set<uint32_t> &internalTargets)
{
Expand All @@ -131,10 +121,13 @@ namespace ps2recomp
std::string funcName = getFunctionName(target);
if (!funcName.empty())
{
ss << " " << funcName << "(rdram, ctx, runtime);\n";
if (branchInst.opcode == OPCODE_J)
{
ss << " return;\n";
ss << " " << funcName << "(rdram, ctx, runtime); return;\n";
}
else
{
ss << " " << funcName << "(rdram, ctx, runtime);\n";
}
}
else
Expand Down Expand Up @@ -402,7 +395,13 @@ namespace ps2recomp

ss << "// Function: " << function.name << "\n";
ss << "// Address: 0x" << std::hex << function.start << " - 0x" << function.end << std::dec << "\n";
std::string sanitizedName = getGeneratedFunctionName(function);
std::string sanitizedName = getFunctionName(function.start);
if (sanitizedName.empty())
{
std::stringstream nameBuilder;
nameBuilder << "Errorfunc_" << std::hex << function.start; // this should never happen but lets put here just to track
sanitizedName = nameBuilder.str();
}
ss << "void " << sanitizedName << "(uint8_t* rdram, R5900Context* ctx, PS2Runtime *runtime) {\n\n";

for (size_t i = 0; i < instructions.size(); ++i)
Expand Down Expand Up @@ -2226,7 +2225,7 @@ namespace ps2recomp
if (!function.isRecompiled && !function.isStub)
continue;

std::string generatedName = getGeneratedFunctionName(function);
std::string generatedName = getFunctionName(function.start);

if (function.isStub)
{
Expand All @@ -2246,28 +2245,28 @@ namespace ps2recomp
}

ss << " // Register recompiled functions\n";
for (const auto & [first, second] : normalFunctions)
for (const auto &[first, second] : normalFunctions)
{
ss << " runtime.registerFunction(0x" << std::hex << first << std::dec
<< ", " << second << ");\n";
}

ss << "\n // Register stub functions\n";
for (const auto & [first, second] : stubFunctions)
for (const auto &[first, second] : stubFunctions)
{
ss << " runtime.registerFunction(0x" << std::hex << first << std::dec
<< ", " << second << ");\n";
}

ss << "\n // Register system call stubs\n";
for (const auto & [first, second] : systemCallFunctions)
for (const auto &[first, second] : systemCallFunctions)
{
ss << " runtime.registerFunction(0x" << std::hex << first << std::dec
<< ", " << second << ");\n";
}

ss << "\n // Register library stubs\n";
for (const auto & [first, second] : libraryFunctions)
for (const auto &[first, second] : libraryFunctions)
{
ss << " runtime.registerFunction(0x" << std::hex << first << std::dec
<< ", " << second << ");\n";
Expand All @@ -2287,7 +2286,7 @@ namespace ps2recomp

ss << "switch (ctx->r[" << indexReg << "]) {\n";

for (const auto & [index, target] : entries)
for (const auto &[index, target] : entries)
{
ss << " case " << index << ": {\n";

Expand Down
5 changes: 2 additions & 3 deletions ps2xRecomp/src/lib/ps2_recompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ namespace ps2recomp
{
if (function.isStub)
{
std::string generatedName = m_codeGenerator->getGeneratedFunctionName(function);
std::string generatedName = m_codeGenerator->getFunctionName(function.start);
std::stringstream stub;
stub << "void " << generatedName
<< "(uint8_t* rdram, R5900Context* ctx, PS2Runtime *runtime) { ";
Expand Down Expand Up @@ -483,8 +483,7 @@ namespace ps2recomp
continue;
}

std::string finalName = sanitizeFunctionName(function.name);
finalName = m_codeGenerator->getGeneratedFunctionName(function);
std::string finalName = m_codeGenerator->getFunctionName(function.start);

ss << "void " << finalName << "(uint8_t* rdram, R5900Context* ctx, PS2Runtime *runtime);\n";
}
Expand Down
Loading