Skip to content
Open
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
17 changes: 17 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
BasedOnStyle: LLVM
UseTab: Never
IndentWidth: 4
Language: Cpp
DerivePointerAlignment: false
PointerAlignment: Left
BreakBeforeBraces: Allman
LineEnding: LF
ColumnLimit: 0
IndentCaseLabels: false
InsertNewlineAtEOF: true
Cpp11BracedListStyle: false
SpaceBeforeCpp11BracedList: true
AlignAfterOpenBracket: DontAlign
ContinuationIndentWidth: 4
AllowAllArgumentsOnNextLine: false
SpaceAfterTemplateKeyword: false
25 changes: 25 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Clang Format
on:
push:
paths-ignore:
- "**/*.md"
- '**/*.txt'
- 'thirdparty/*'
pull_request:
paths-ignore:
- "**/*.md"
- '**/*.txt'
- 'thirdparty/*'
workflow_dispatch:
jobs:
clang-format:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Run clang-format
uses: jidicula/clang-format-action@v4.14.0
with:
exclude-regex: (thirdparty)
clang-format-version: 19
26 changes: 13 additions & 13 deletions XenonAnalyse/function.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "function.h"
#include <disasm.h>
#include <vector>
#include <bit>
#include <algorithm>
#include <cassert>
#include <bit>
#include <byteswap.h>
#include <cassert>
#include <disasm.h>
#include <vector>

size_t Function::SearchBlock(size_t address) const
{
Expand Down Expand Up @@ -40,7 +40,7 @@ size_t Function::SearchBlock(size_t address) const

Function Function::Analyze(const void* code, size_t size, size_t base)
{
Function fn{ base, 0 };
Function fn { base, 0 };

if (*((uint32_t*)code + 1) == 0x04000048) // shifted ptr tail call
{
Expand All @@ -55,14 +55,16 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
const auto* data = (uint32_t*)code;
const auto* dataStart = data;
const auto* dataEnd = (uint32_t*)((uint8_t*)code + size);
std::vector<size_t> blockStack{};
std::vector<size_t> blockStack {};
blockStack.reserve(32);
blockStack.emplace_back();

#define RESTORE_DATA() if (!blockStack.empty()) data = (dataStart + ((blocks[blockStack.back()].base + blocks[blockStack.back()].size) / sizeof(*data))) - 1; // continue adds one
#define RESTORE_DATA() \
if (!blockStack.empty()) \
data = (dataStart + ((blocks[blockStack.back()].base + blocks[blockStack.back()].size) / sizeof(*data))) - 1; // continue adds one

// TODO: Branch fallthrough
for (; data <= dataEnd ; ++data)
for (; data <= dataEnd; ++data)
{
const size_t addr = base + ((data - dataStart) * sizeof(*data));
if (blockStack.empty())
Expand All @@ -82,7 +84,7 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
ppc::Disassemble(data, addr, insn);

// Sanity check
assert(addr == base + curBlock.base + curBlock.size);
assert(addr == base + curBlock.base + curBlock.size);
if (curBlock.projectedSize != -1 && curBlock.size >= curBlock.projectedSize) // fallthrough
{
blockStack.pop_back();
Expand Down Expand Up @@ -172,7 +174,7 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
blocks.emplace_back(branchBase, 0, sizeProjection);

blockStack.emplace_back(blocks.size() - 1);

DEBUG(blocks.back().parent = blockBase);
RESTORE_DATA();
continue;
Expand Down Expand Up @@ -214,9 +216,7 @@ Function Function::Analyze(const void* code, size_t size, size_t base)
if (blocks.size() > 1)
{
std::sort(blocks.begin(), blocks.end(), [](const Block& a, const Block& b)
{
return a.base < b.base;
});
{ return a.base < b.base; });

size_t discontinuity = -1;
for (size_t i = 0; i < blocks.size() - 1; i++)
Expand Down
22 changes: 11 additions & 11 deletions XenonAnalyse/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ struct Function
{
struct Block
{
size_t base{};
size_t size{};
size_t projectedSize{ static_cast<size_t>(-1) }; // scratch
DEBUG(size_t parent{});
size_t base {};
size_t size {};
size_t projectedSize { static_cast<size_t>(-1) }; // scratch
DEBUG(size_t parent {});

Block()
Block()
{
}

Block(size_t base, size_t size)
: base(base), size(size)
: base(base), size(size)
{
}

Block(size_t base, size_t size, size_t projectedSize)
Block(size_t base, size_t size, size_t projectedSize)
: base(base), size(size), projectedSize(projectedSize)
{
}
};

size_t base{};
size_t size{};
std::vector<Block> blocks{};
size_t base {};
size_t size {};
std::vector<Block> blocks {};

Function()
{
Expand All @@ -45,7 +45,7 @@ struct Function
: base(base), size(size)
{
}

size_t SearchBlock(size_t address) const;
static Function Analyze(const void* code, size_t size, size_t base);
};
112 changes: 54 additions & 58 deletions XenonAnalyse/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "function.h"
#include <cassert>
#include <iterator>
#include <file.h>
#include <disasm.h>
#include <file.h>
#include <fmt/core.h>
#include <image.h>
#include <iterator>
#include <xbox.h>
#include <fmt/core.h>
#include "function.h"

#define SWITCH_ABSOLUTE 0
#define SWITCH_COMPUTED 1
Expand All @@ -14,11 +14,11 @@

struct SwitchTable
{
std::vector<size_t> labels{};
size_t base{};
size_t defaultLabel{};
uint32_t r{};
uint32_t type{};
std::vector<size_t> labels {};
size_t base {};
size_t defaultLabel {};
uint32_t r {};
uint32_t type {};
};

void ReadTable(Image& image, SwitchTable& table)
Expand Down Expand Up @@ -104,7 +104,7 @@ void ReadTable(Image& image, SwitchTable& table)
void ScanTable(const uint32_t* code, size_t base, SwitchTable& table)
{
ppc_insn insn;
uint32_t cr{ (uint32_t)-1 };
uint32_t cr { (uint32_t)-1 };
for (int i = 0; i < 32; i++)
{
ppc::Disassemble(&code[-i], base - (4 * i), insn);
Expand Down Expand Up @@ -193,65 +193,64 @@ int main(int argc, char** argv)
auto image = Image::ParseImage(file.data(), file.size());

auto printTable = [&](const SwitchTable& table)
{
println("[[switch]]");
println("base = 0x{:X}", table.base);
println("r = {}", table.r);
println("default = 0x{:X}", table.defaultLabel);
println("labels = [");
for (const auto& label : table.labels)
{
println("[[switch]]");
println("base = 0x{:X}", table.base);
println("r = {}", table.r);
println("default = 0x{:X}", table.defaultLabel);
println("labels = [");
for (const auto& label : table.labels)
{
println(" 0x{:X},", label);
}
println(" 0x{:X},", label);
}

println("]");
println("");
};
println("]");
println("");
};

std::vector<SwitchTable> switches{};
std::vector<SwitchTable> switches {};

println("# Generated by XenonAnalyse");

auto scanPattern = [&](uint32_t* pattern, size_t count, size_t type)
{
for (const auto& section : image.sections)
{
for (const auto& section : image.sections)
if (!(section.flags & SectionFlags_Code))
{
if (!(section.flags & SectionFlags_Code))
{
continue;
}
continue;
}

size_t base = section.base;
uint8_t* data = section.data;
uint8_t* dataStart = section.data;
uint8_t* dataEnd = section.data + section.size;
while (data < dataEnd && data != nullptr)
size_t base = section.base;
uint8_t* data = section.data;
uint8_t* dataStart = section.data;
uint8_t* dataEnd = section.data + section.size;
while (data < dataEnd && data != nullptr)
{
data = (uint8_t*)SearchMask(data, pattern, count, dataEnd - data);

if (data != nullptr)
{
data = (uint8_t*)SearchMask(data, pattern, count, dataEnd - data);
SwitchTable table {};
table.type = type;
ScanTable((uint32_t*)data, base + (data - dataStart), table);

if (data != nullptr)
// fmt::println("{:X} ; jmptable - {}", base + (data - dataStart), table.labels.size());
if (table.base != 0)
{
SwitchTable table{};
table.type = type;
ScanTable((uint32_t*)data, base + (data - dataStart), table);

// fmt::println("{:X} ; jmptable - {}", base + (data - dataStart), table.labels.size());
if (table.base != 0)
{
ReadTable(image, table);
printTable(table);
switches.emplace_back(std::move(table));
}

data += 4;
ReadTable(image, table);
printTable(table);
switches.emplace_back(std::move(table));
}
continue;

data += 4;
}
continue;
}
};
}
};

uint32_t absoluteSwitch[] =
{
uint32_t absoluteSwitch[] = {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oversight in clang-format config llvm/llvm-project#90837

Copy link
Copy Markdown

@halotroop2288 halotroop2288 Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set BreakBeforeBraces to Allman, to match the original styling. It keeps all braces on the next line, even array initializers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already set, it doesn't affect array initialisers properly.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is an open issue with clang-format. There's no way to customize the line breaking behavior with array initializers. llvm/llvm-project#90837

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see you already linked the Issue.

PPC_INST_LIS,
PPC_INST_ADDI,
PPC_INST_RLWINM,
Expand All @@ -260,8 +259,7 @@ int main(int argc, char** argv)
PPC_INST_BCTR,
};

uint32_t computedSwitch[] =
{
uint32_t computedSwitch[] = {
PPC_INST_LIS,
PPC_INST_ADDI,
PPC_INST_LBZX,
Expand All @@ -272,8 +270,7 @@ int main(int argc, char** argv)
PPC_INST_MTCTR,
};

uint32_t offsetSwitch[] =
{
uint32_t offsetSwitch[] = {
PPC_INST_LIS,
PPC_INST_ADDI,
PPC_INST_LBZX,
Expand All @@ -283,8 +280,7 @@ int main(int argc, char** argv)
PPC_INST_MTCTR,
};

uint32_t wordOffsetSwitch[] =
{
uint32_t wordOffsetSwitch[] = {
PPC_INST_LIS,
PPC_INST_ADDI,
PPC_INST_RLWINM,
Expand Down
8 changes: 4 additions & 4 deletions XenonRecomp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ int main(int argc, char* argv[])
}
#endif

const char* path =
#ifdef XENON_RECOMP_CONFIG_FILE_PATH
const char* path =
#ifdef XENON_RECOMP_CONFIG_FILE_PATH
XENON_RECOMP_CONFIG_FILE_PATH
#else
#else
argv[1]
#endif
#endif
;

if (std::filesystem::is_regular_file(path))
Expand Down
6 changes: 3 additions & 3 deletions XenonRecomp/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <charconv>
#include <cstddef>
#include <disasm.h>
#include <file.h>
#include <filesystem>
#include <fmt/core.h>
#include <fstream>
#include <function.h>
#include <image.h>
#include <toml++/toml.hpp>
#include <unordered_map>
#include <unordered_set>
#include <xbox.h>
#include <xxhash.h>
#include <fmt/core.h>
#include <xmmintrin.h>
#include <xxhash.h>
Loading