XenonRecomp/PowerAnalyse/function.cpp

246 lines
7.4 KiB
C++
Raw Permalink Normal View History

2024-09-09 21:09:37 +06:00
#include "function.h"
#include <disasm.h>
#include <vector>
#include <bit>
2024-09-11 08:58:50 +06:00
#include <algorithm>
#include <cassert>
2024-09-09 21:09:37 +06:00
2024-09-09 22:52:34 +06:00
size_t Function::SearchBlock(size_t address) const
2024-09-09 21:09:37 +06:00
{
if (address < base)
{
return -1;
}
for (size_t i = 0; i < blocks.size(); i++)
{
const auto& block = blocks[i];
const auto begin = base + block.base;
2024-09-12 14:33:49 +03:00
const auto end = begin + block.size;
2024-09-09 21:09:37 +06:00
2024-09-13 20:27:05 +06:00
if (begin != end)
2024-09-09 21:09:37 +06:00
{
2024-09-13 20:27:05 +06:00
if (address >= begin && address < end)
{
return i;
}
}
else // fresh block
{
if (address == begin)
{
return i;
}
2024-09-09 21:09:37 +06:00
}
}
return -1;
}
2024-09-09 22:52:34 +06:00
Function Function::Analyze(const void* code, size_t size, size_t base)
2024-09-09 21:09:37 +06:00
{
2024-09-09 22:52:34 +06:00
Function fn{ base, 0 };
if (*((uint32_t*)code + 1) == 0x04000048) // shifted ptr tail call
{
fn.size = 0x8;
return fn;
}
2024-09-09 21:09:37 +06:00
auto& blocks = fn.blocks;
2024-09-09 22:52:34 +06:00
blocks.reserve(8);
2024-09-09 21:09:37 +06:00
blocks.emplace_back();
const auto* data = (uint32_t*)code;
const auto* dataStart = data;
const auto* dataEnd = (uint32_t*)((uint8_t*)code + size);
std::vector<size_t> blockStack{};
2024-09-09 22:52:34 +06:00
blockStack.reserve(32);
2024-09-09 21:09:37 +06:00
blockStack.emplace_back();
2024-09-11 08:58:50 +06:00
#define RESTORE_DATA() if (!blockStack.empty()) data = (dataStart + ((blocks[blockStack.back()].base + blocks[blockStack.back()].size) / sizeof(*data))) - 1; // continue adds one
2024-09-09 22:52:34 +06:00
2024-09-09 21:09:37 +06:00
// TODO: Branch fallthrough
for (; data <= dataEnd ; ++data)
{
const auto addr = base + ((data - dataStart) * sizeof(*data));
if (blockStack.empty())
{
break; // it's hideover
}
2024-09-09 22:52:34 +06:00
auto& curBlock = blocks[blockStack.back()];
2024-09-13 20:27:05 +06:00
DEBUG(const auto blockBase = curBlock.base);
2024-09-09 21:09:37 +06:00
const auto instruction = std::byteswap(*data);
const auto op = PPC_OP(instruction);
const auto xop = PPC_XOP(instruction);
2024-09-11 08:58:50 +06:00
const auto isLink = PPC_BL(instruction); // call
2024-09-09 21:09:37 +06:00
ppc_insn insn;
ppc::Disassemble(data, addr, insn);
2024-09-11 08:58:50 +06:00
// Sanity check
assert(addr == base + curBlock.base + curBlock.size);
2024-09-09 22:52:34 +06:00
if (curBlock.projectedSize != -1 && curBlock.size >= curBlock.projectedSize) // fallthrough
2024-09-09 21:09:37 +06:00
{
blockStack.pop_back();
2024-09-09 22:52:34 +06:00
RESTORE_DATA();
continue;
}
curBlock.size += 4;
if (op == PPC_OP_BC) // conditional branches all originate from one opcode, thanks RISC
{
if (isLink) // just a conditional call, nothing to see here
{
continue;
}
2024-09-09 21:09:37 +06:00
2024-09-11 08:58:50 +06:00
// TODO: carry projections over to false
2024-09-09 22:52:34 +06:00
curBlock.projectedSize = -1;
blockStack.pop_back();
2024-09-11 08:58:50 +06:00
// TODO: Handle absolute branches?
assert(!PPC_BA(instruction));
const auto branchDest = addr + PPC_BD(instruction);
2024-09-09 21:09:37 +06:00
// true/false paths
// left block: false case
// right block: true case
const auto lBase = (addr - base) + 4;
2024-09-11 08:58:50 +06:00
const auto rBase = (addr + PPC_BD(instruction)) - base;
2024-09-09 21:09:37 +06:00
// these will be -1 if it's our first time seeing these blocks
auto lBlock = fn.SearchBlock(base + lBase);
if (lBlock == -1)
{
2024-09-09 22:52:34 +06:00
blocks.emplace_back(lBase, 0).projectedSize = rBase - lBase;
2024-09-09 21:09:37 +06:00
lBlock = blocks.size() - 1;
2024-09-11 08:58:50 +06:00
// push this first, this gets overriden by the true case as it'd be further away
DEBUG(blocks[lBlock].parent = blockBase);
2024-09-09 21:09:37 +06:00
blockStack.emplace_back(lBlock);
}
2024-09-09 22:52:34 +06:00
auto rBlock = fn.SearchBlock(base + rBase);
if (rBlock == -1)
2024-09-09 21:09:37 +06:00
{
2024-09-11 08:58:50 +06:00
blocks.emplace_back(branchDest - base, 0);
2024-09-09 22:52:34 +06:00
rBlock = blocks.size() - 1;
2024-09-09 21:09:37 +06:00
2024-09-11 08:58:50 +06:00
DEBUG(blocks[rBlock].parent = blockBase);
2024-09-09 22:52:34 +06:00
blockStack.emplace_back(rBlock);
2024-09-09 21:09:37 +06:00
}
2024-09-11 08:58:50 +06:00
RESTORE_DATA();
2024-09-09 21:09:37 +06:00
}
2024-09-13 20:27:05 +06:00
else if (op == PPC_OP_B || instruction == 0 || (op == PPC_OP_CTR && (xop == 16 || xop == 528))) // b, blr, end padding
2024-09-09 21:09:37 +06:00
{
if (!isLink)
{
blockStack.pop_back();
2024-09-10 02:42:20 +06:00
if (op == PPC_OP_B)
2024-09-09 21:09:37 +06:00
{
2024-09-11 08:58:50 +06:00
assert(!PPC_BA(instruction));
const auto branchDest = addr + PPC_BI(instruction);
const auto branchBase = branchDest - base;
const auto branchBlock = fn.SearchBlock(branchDest);
2024-09-09 21:09:37 +06:00
2024-09-11 10:03:41 +06:00
if (branchDest < base)
{
// Branches before base are just tail calls, no need to chase after those
RESTORE_DATA();
continue;
}
2024-09-09 23:23:04 +06:00
// carry over our projection if blocks are next to each other
2024-09-11 10:03:41 +06:00
const auto isContinuous = branchBase == curBlock.base + curBlock.size;
2024-09-09 22:52:34 +06:00
auto sizeProjection = (size_t)-1;
2024-09-11 10:03:41 +06:00
if (curBlock.projectedSize != -1 && isContinuous)
2024-09-09 22:52:34 +06:00
{
sizeProjection = curBlock.projectedSize - curBlock.size;
2024-09-11 10:03:41 +06:00
}
2024-09-09 22:52:34 +06:00
2024-09-11 10:03:41 +06:00
if (branchBlock == -1)
{
blocks.emplace_back(branchBase, 0, sizeProjection);
2024-09-11 08:58:50 +06:00
2024-09-11 10:03:41 +06:00
blockStack.emplace_back(blocks.size() - 1);
2024-09-13 20:27:05 +06:00
2024-09-11 10:03:41 +06:00
DEBUG(blocks.back().parent = blockBase);
RESTORE_DATA();
continue;
2024-09-09 21:09:37 +06:00
}
2024-09-13 20:27:05 +06:00
}
else if (op == PPC_OP_CTR)
{
// 5th bit of BO tells cpu to ignore the counter, which is a blr/bctr otherwise it's conditional
const auto conditional = !(PPC_BO(instruction) & 0x10);
if (conditional)
{
// right block's just going to return
const auto lBase = (addr - base) + 4;
auto lBlock = fn.SearchBlock(lBase);
if (lBlock == -1)
{
blocks.emplace_back(lBase, 0);
lBlock = blocks.size() - 1;
DEBUG(blocks[lBlock].parent = blockBase);
blockStack.emplace_back(lBlock);
RESTORE_DATA();
continue;
}
}
2024-09-09 21:09:37 +06:00
}
2024-09-11 08:58:50 +06:00
RESTORE_DATA();
2024-09-09 21:09:37 +06:00
}
}
else if (insn.opcode == nullptr)
{
blockStack.pop_back();
RESTORE_DATA();
}
2024-09-09 21:09:37 +06:00
}
2024-09-11 08:58:50 +06:00
2024-09-11 10:03:41 +06:00
// Sort and invalidate discontinuous blocks
if (blocks.size() > 1)
{
std::ranges::sort(blocks, [](const Block& a, const Block& b)
{
return a.base < b.base;
});
size_t discontinuity = -1;
for (size_t i = 0; i < blocks.size() - 1; i++)
{
if (blocks[i].base + blocks[i].size >= blocks[i + 1].base)
{
continue;
}
discontinuity = i + 1;
break;
}
if (discontinuity != -1)
{
blocks.erase(blocks.begin() + discontinuity, blocks.end());
}
}
2024-09-11 08:58:50 +06:00
fn.size = 0;
2024-09-09 21:09:37 +06:00
for (const auto& block : blocks)
{
// pick the block furthest away
2024-09-11 09:10:23 +06:00
fn.size = std::max(fn.size, block.base + block.size);
2024-09-09 21:09:37 +06:00
}
return fn;
}