Skip to content

Commit 847842c

Browse files
Downgrade projects to C++17.
1 parent 02d23b3 commit 847842c

20 files changed

Lines changed: 250 additions & 189 deletions

CMakeLists.txt

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
cmake_minimum_required (VERSION 3.20)
2+
3+
include($ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake)
24
set(THIRDPARTY_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty)
3-
set(CMAKE_CXX_STANDARD 23)
5+
6+
set(CMAKE_CXX_STANDARD 17)
47
set(BUILD_SHARED_LIBS OFF)
58

69
# Enable Hot Reload for MSVC compilers if supported.
@@ -12,20 +15,6 @@ endif()
1215
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
1316

1417
include("cmake/bin2h.cmake")
15-
include(FetchContent)
16-
FetchContent_Declare(
17-
tomlplusplus
18-
GIT_REPOSITORY https://github.com/marzer/tomlplusplus.git
19-
GIT_TAG v3.4.0
20-
)
21-
FetchContent_Declare(
22-
xxHash
23-
GIT_REPOSITORY https://github.com/Cyan4973/xxHash.git
24-
GIT_TAG v0.8.2
25-
SOURCE_SUBDIR "cmake_unofficial"
26-
)
27-
FetchContent_MakeAvailable(tomlplusplus)
28-
FetchContent_MakeAvailable(xxHash)
2918

3019
add_subdirectory(${THIRDPARTY_ROOT}/disasm)
3120
set(POWERANALYSE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/PowerAnalyse)

PowerAnalyse/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ project("PowerAnalyse")
55
add_executable(PowerAnalyse "main.cpp" "function.h" "function.cpp")
66
add_library(LibPowerAnalyse "function.h" "function.cpp")
77

8+
find_package(fmt CONFIG REQUIRED)
9+
810
target_include_directories(LibPowerAnalyse PUBLIC .)
911
target_link_libraries(LibPowerAnalyse PUBLIC PowerUtils)
1012

11-
target_link_libraries(PowerAnalyse PRIVATE PowerUtils)
13+
target_link_libraries(PowerAnalyse PRIVATE PowerUtils fmt::fmt)

PowerAnalyse/function.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <bit>
55
#include <algorithm>
66
#include <cassert>
7+
#include <byteswap.h>
78

89
size_t Function::SearchBlock(size_t address) const
910
{
@@ -63,19 +64,19 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
6364
// TODO: Branch fallthrough
6465
for (; data <= dataEnd ; ++data)
6566
{
66-
const auto addr = base + ((data - dataStart) * sizeof(*data));
67+
const size_t addr = base + ((data - dataStart) * sizeof(*data));
6768
if (blockStack.empty())
6869
{
6970
break; // it's hideover
7071
}
7172

7273
auto& curBlock = blocks[blockStack.back()];
7374
DEBUG(const auto blockBase = curBlock.base);
74-
const auto instruction = std::byteswap(*data);
75+
const uint32_t instruction = ByteSwap(*data);
7576

76-
const auto op = PPC_OP(instruction);
77-
const auto xop = PPC_XOP(instruction);
78-
const auto isLink = PPC_BL(instruction); // call
77+
const uint32_t op = PPC_OP(instruction);
78+
const uint32_t xop = PPC_XOP(instruction);
79+
const uint32_t isLink = PPC_BL(instruction); // call
7980

8081
ppc_insn insn;
8182
ppc::Disassemble(data, addr, insn);
@@ -103,13 +104,13 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
103104

104105
// TODO: Handle absolute branches?
105106
assert(!PPC_BA(instruction));
106-
const auto branchDest = addr + PPC_BD(instruction);
107+
const size_t branchDest = addr + PPC_BD(instruction);
107108

108109
// true/false paths
109110
// left block: false case
110111
// right block: true case
111-
const auto lBase = (addr - base) + 4;
112-
const auto rBase = (addr + PPC_BD(instruction)) - base;
112+
const size_t lBase = (addr - base) + 4;
113+
const size_t rBase = (addr + PPC_BD(instruction)) - base;
113114

114115
// these will be -1 if it's our first time seeing these blocks
115116
auto lBlock = fn.SearchBlock(base + lBase);
@@ -124,7 +125,7 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
124125
blockStack.emplace_back(lBlock);
125126
}
126127

127-
auto rBlock = fn.SearchBlock(base + rBase);
128+
size_t rBlock = fn.SearchBlock(base + rBase);
128129
if (rBlock == -1)
129130
{
130131
blocks.emplace_back(branchDest - base, 0);
@@ -145,10 +146,10 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
145146
if (op == PPC_OP_B)
146147
{
147148
assert(!PPC_BA(instruction));
148-
const auto branchDest = addr + PPC_BI(instruction);
149+
const size_t branchDest = addr + PPC_BI(instruction);
149150

150-
const auto branchBase = branchDest - base;
151-
const auto branchBlock = fn.SearchBlock(branchDest);
151+
const size_t branchBase = branchDest - base;
152+
const size_t branchBlock = fn.SearchBlock(branchDest);
152153

153154
if (branchDest < base)
154155
{
@@ -158,8 +159,8 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
158159
}
159160

160161
// carry over our projection if blocks are next to each other
161-
const auto isContinuous = branchBase == curBlock.base + curBlock.size;
162-
auto sizeProjection = (size_t)-1;
162+
const bool isContinuous = branchBase == curBlock.base + curBlock.size;
163+
size_t sizeProjection = (size_t)-1;
163164

164165
if (curBlock.projectedSize != -1 && isContinuous)
165166
{
@@ -180,12 +181,12 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
180181
else if (op == PPC_OP_CTR)
181182
{
182183
// 5th bit of BO tells cpu to ignore the counter, which is a blr/bctr otherwise it's conditional
183-
const auto conditional = !(PPC_BO(instruction) & 0x10);
184+
const bool conditional = !(PPC_BO(instruction) & 0x10);
184185
if (conditional)
185186
{
186187
// right block's just going to return
187-
const auto lBase = (addr - base) + 4;
188-
auto lBlock = fn.SearchBlock(lBase);
188+
const size_t lBase = (addr - base) + 4;
189+
size_t lBlock = fn.SearchBlock(lBase);
189190
if (lBlock == -1)
190191
{
191192
blocks.emplace_back(lBase, 0);
@@ -212,7 +213,7 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
212213
// Sort and invalidate discontinuous blocks
213214
if (blocks.size() > 1)
214215
{
215-
std::ranges::sort(blocks, [](const Block& a, const Block& b)
216+
std::sort(blocks.begin(), blocks.end(), [](const Block& a, const Block& b)
216217
{
217218
return a.base < b.base;
218219
});

PowerAnalyse/function.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
#include <vector>
33

4-
#ifdef _DEBUG(X)
4+
#ifdef _DEBUG
55
#define DEBUG(X) X
66
#else
77
#define DEBUG(X)
@@ -13,15 +13,36 @@ struct Function
1313
{
1414
size_t base{};
1515
size_t size{};
16+
size_t projectedSize{ static_cast<size_t>(-1) }; // scratch
1617
DEBUG(size_t parent{});
1718

18-
// scratch
19-
size_t projectedSize{ static_cast<size_t>(-1) };
19+
Block()
20+
{
21+
}
22+
23+
Block(size_t base, size_t size)
24+
: base(base), size(size)
25+
{
26+
}
27+
28+
Block(size_t base, size_t size, size_t projectedSize)
29+
: base(base), size(size), projectedSize(projectedSize)
30+
{
31+
}
2032
};
2133

2234
size_t base{};
2335
size_t size{};
2436
std::vector<Block> blocks{};
37+
38+
Function()
39+
{
40+
}
41+
42+
Function(size_t base, size_t size)
43+
: base(base), size(size)
44+
{
45+
}
2546

2647
size_t SearchBlock(size_t address) const;
2748
static Function Analyze(const void* code, size_t size, size_t base);

PowerAnalyse/main.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include <cassert>
2+
#include <iterator>
23
#include <file.h>
34
#include <disasm.h>
45
#include <image.h>
5-
#include "function.h"
6-
#include <print>
76
#include <xbox.h>
7+
#include <fmt/core.h>
8+
#include "function.h"
89

910
#define SWITCH_ABSOLUTE 0
1011
#define SWITCH_COMPUTED 1
@@ -139,7 +140,7 @@ void MakeMask(const uint32_t* instructions, size_t count)
139140
for (size_t i = 0; i < count; i++)
140141
{
141142
ppc::Disassemble(&instructions[i], 0, insn);
142-
std::println("0x{:X}, // {}", std::byteswap(insn.opcode->opcode | (insn.instruction & insn.opcode->mask)), insn.opcode->name);
143+
fmt::println("0x{:X}, // {}", ByteSwap(insn.opcode->opcode | (insn.instruction & insn.opcode->mask)), insn.opcode->name);
143144
}
144145
}
145146

@@ -173,13 +174,13 @@ void* SearchMask(const void* source, const uint32_t* compare, size_t compareCoun
173174

174175
int main()
175176
{
176-
const auto file = LoadFile("private/default.xex").value();
177-
auto image = Image::ParseImage(file.data(), file.size()).value();
177+
const auto file = LoadFile("private/default.xex");
178+
auto image = Image::ParseImage(file.data(), file.size());
178179

179180
std::string out;
180-
auto println = [&]<class... Args>(std::format_string<Args...> fmt, Args&&... args)
181+
auto println = [&]<class... Args>(fmt::format_string<Args...> fmt, Args&&... args)
181182
{
182-
std::vformat_to(std::back_inserter(out), fmt.get(), std::make_format_args(args...));
183+
fmt::vformat_to(std::back_inserter(out), fmt.get(), fmt::make_format_args(args...));
183184
out += '\n';
184185
};
185186
//for (const auto& section : image.sections)
@@ -190,7 +191,7 @@ int main()
190191
// MakeMask((uint32_t*)image.Find(0x82C40D84), 6);
191192

192193
//auto data = "\x4D\x99\x00\x20";
193-
//auto data2 = std::byteswap((2129));
194+
//auto data2 = ByteSwap((2129));
194195
//ppc_insn insn;
195196
//ppc_insn insn2;
196197
//ppc::Disassemble(data, 0, insn);
@@ -261,7 +262,7 @@ int main()
261262
table.type = type;
262263
ScanTable((uint32_t*)data, base + (data - dataStart), table);
263264

264-
// std::println("{:X} ; jmptable - {}", base + (data - dataStart), table.labels.size());
265+
// fmt::println("{:X} ; jmptable - {}", base + (data - dataStart), table.labels.size());
265266
if (table.base != 0)
266267
{
267268
ReadTable(image, table);
@@ -335,15 +336,15 @@ int main()
335336
fwrite(out.data(), 1, out.size(), f);
336337
fclose(f);
337338

338-
uint32_t cxxFrameHandler = std::byteswap(0x831B1C90);
339-
uint32_t cSpecificFrameHandler = std::byteswap(0x8324B3BC);
339+
uint32_t cxxFrameHandler = ByteSwap(0x831B1C90);
340+
uint32_t cSpecificFrameHandler = ByteSwap(0x8324B3BC);
340341
image.symbols.emplace("__CxxFrameHandler", 0x831B1C90, 0x38, Symbol_Function);
341342
image.symbols.emplace("__C_specific_handler", 0x8324B3BC, 0x38, Symbol_Function);
342343
image.symbols.emplace("memcpy", 0x831B0ED0, 0x488, Symbol_Function);
343344
image.symbols.emplace("memset", 0x831B0BA0, 0xA0, Symbol_Function);
344345
image.symbols.emplace("blkmov", 0x831B1358, 0xA8, Symbol_Function);
345346

346-
image.symbols.emplace(std::format("sub_{:X}", 0x82EF5D78), 0x82EF5D78, 0x3F8, Symbol_Function);
347+
image.symbols.emplace(fmt::format("sub_{:X}", 0x82EF5D78), 0x82EF5D78, 0x3F8, Symbol_Function);
347348

348349
// auto fnd = Function::Analyze(image.Find(0x82C40D58), image.size, 0x82C40D58);
349350

@@ -354,8 +355,8 @@ int main()
354355
for (size_t i = 0; i < count; i++)
355356
{
356357
auto fn = pf[i];
357-
fn.BeginAddress = std::byteswap(fn.BeginAddress);
358-
fn.Data = std::byteswap(fn.Data);
358+
fn.BeginAddress = ByteSwap(fn.BeginAddress);
359+
fn.Data = ByteSwap(fn.Data);
359360

360361
auto& f = functions.emplace_back();
361362
f.base = fn.BeginAddress;
@@ -366,7 +367,7 @@ int main()
366367
__debugbreak();
367368
}
368369

369-
image.symbols.emplace(std::format("sub_{:X}", f.base), f.base, f.size, Symbol_Function);
370+
image.symbols.emplace(fmt::format("sub_{:X}", f.base), f.base, f.size, Symbol_Function);
370371
}
371372

372373
auto sym = image.symbols.find(0x82BD7420);
@@ -413,15 +414,15 @@ int main()
413414
base += missingFn.size;
414415
data += missingFn.size;
415416

416-
std::println("sub_{:X}", missingFn.base);
417+
fmt::println("sub_{:X}", missingFn.base);
417418
}
418419
}
419420
}
420421

421422
//ppc_insn insn;
422423
//uint8_t c[4] = { 0x10, 0x00, 0x59, 0xC3 };
423424
//ppc::Disassemble(c, 0x831D6C64, insn);
424-
//std::println("{:20}{}", insn.opcode->name, insn.op_str);
425+
//fmt::println("{:20}{}", insn.opcode->name, insn.op_str);
425426

426427

427428
const auto entrySymbol = image.symbols.find(image.entry_point);
@@ -432,21 +433,21 @@ int main()
432433

433434
image.symbols.emplace("_start", image.entry_point, entrySize, Symbol_Function);
434435

435-
std::println("FUNCTIONS");
436+
fmt::println("FUNCTIONS");
436437
for (const auto& fn : functions)
437438
{
438-
std::println("\tsub_{:X}", fn.base);
439+
fmt::println("\tsub_{:X}", fn.base);
439440
}
440-
std::println("");
441+
fmt::println("");
441442

442443

443-
std::println("SECTIONS");
444+
fmt::println("SECTIONS");
444445
for (const auto& section : image.sections)
445446
{
446-
std::printf("Section %.8s\n", section.name.c_str());
447-
std::printf("\t%X-%X\n", section.base, section.base + section.size);
447+
printf("Section %.8s\n", section.name.c_str());
448+
printf("\t%X-%X\n", section.base, section.base + section.size);
448449
}
449450

450-
std::println("");
451+
fmt::println("");
451452
return 0;
452453
}

PowerRecomp/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ BIN2H(SOURCE_FILE ${POWERUTILS_ROOT}/ppc_context.h HEADER_FILE "generated/ppc_co
66

77
add_executable(PowerRecomp "main.cpp" "pch.h" "recompiler.cpp" "recompiler.h" "test_recompiler.cpp" "test_recompiler.h" "recompiler_config.h" "recompiler_config.cpp")
88
target_precompile_headers(PowerRecomp PUBLIC "pch.h")
9-
target_link_libraries(PowerRecomp PRIVATE LibPowerAnalyse tomlplusplus::tomlplusplus xxHash::xxhash)
9+
10+
find_package(fmt CONFIG REQUIRED)
11+
find_package(PkgConfig REQUIRED)
12+
pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus)
13+
find_package(xxHash CONFIG REQUIRED)
14+
15+
target_link_libraries(PowerRecomp PRIVATE LibPowerAnalyse fmt::fmt PkgConfig::tomlplusplus xxHash::xxhash)
1016

1117
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
1218
target_compile_options(PowerRecomp PRIVATE -Wno-switch -Wno-unused-variable)

PowerRecomp/pch.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
#pragma once
22

3-
// Workaround for the intellisense for some reason not seeing C++23 features
4-
#ifdef __INTELLISENSE__
5-
#undef __cplusplus
6-
#define __cplusplus 202302L
7-
#endif
8-
93
#include <cassert>
4+
#include <charconv>
105
#include <disasm.h>
116
#include <file.h>
127
#include <filesystem>
13-
#include <format>
8+
#include <fstream>
149
#include <function.h>
1510
#include <image.h>
16-
#include <print>
1711
#include <toml++/toml.hpp>
1812
#include <unordered_map>
1913
#include <unordered_set>
2014
#include <xbox.h>
2115
#include <xxhash.h>
16+
#include <fmt/core.h>
2217
#include "generated/ppc_context.gen.h"

0 commit comments

Comments
 (0)