mirror of
https://github.com/open-goal/jak-project.git
synced 2024-10-20 21:27:52 -04:00
75f7609baa
* some work on `ocean-transition` * decomp: finished non-blocked `ocean-transition` functions * decompiler: Support <0 unsigned comparison * decompiler/goalc: Support half-word and byte parallel extend ops * attempt to add support for byte stack spills, assertion errors `Assertion failed: idx < n_src, file ..\..\..\decompiler\Disasm\Instruction.cpp, line 353` * decomp: fix byte stack spills, only 1 issue remains in `draw-ocean-transition` * almost there... * closer, but unknowns about `dma-buffer` blocking * dealing with dma-buffer issues * some more fixes, only an obscure xmm issue remains * add two problematic functions to blacklists, avoid goal_src additions for now * fix building/compilation but TypeConsistency hits an assertion error the `ocean-mid-masks`'s `data` field is none of the types * decomp: finalize `ocean-transition` * address feedback - 1 issue remains * decomp: finalize `ocean-transition`
118 lines
3.8 KiB
C++
118 lines
3.8 KiB
C++
#include <stdexcept>
|
|
#include "third-party/fmt/core.h"
|
|
#include "stack_spill.h"
|
|
#include "decompiler/Disasm/DecompilerLabel.h"
|
|
|
|
namespace decompiler {
|
|
|
|
std::string StackSpillSlot::print() const {
|
|
return fmt::format("[{:3d}] {}{}", offset, is_signed ? 's' : 'u', size * 8);
|
|
}
|
|
|
|
void StackSpillMap::add_access(const StackSpillSlot& access) {
|
|
auto existing = m_slot_map.find(access.offset);
|
|
if (existing != m_slot_map.end()) {
|
|
if (access != existing->second) {
|
|
// the GOAL float -> GPR loads are just totally wrong.
|
|
if (existing->second.size == 16 && access.size == 4) {
|
|
existing->second.size = 4;
|
|
return;
|
|
}
|
|
|
|
if (existing->second.size == 4 && access.size == 16) {
|
|
return;
|
|
}
|
|
|
|
throw std::runtime_error(fmt::format("Inconsistent stack access:\n{}\n{}\n",
|
|
existing->second.print(), access.print()));
|
|
}
|
|
} else {
|
|
m_slot_map.insert({access.offset, access});
|
|
}
|
|
}
|
|
|
|
const StackSpillSlot& StackSpillMap::lookup(int offset) const {
|
|
auto result = m_slot_map.find(offset);
|
|
if (result == m_slot_map.end()) {
|
|
throw std::runtime_error(fmt::format("unknown stack spill slot at offset {}", offset));
|
|
}
|
|
return result->second;
|
|
}
|
|
|
|
void StackSpillMap::finalize() {
|
|
// how many variables exist at each byte. should be 1 or 0.
|
|
int max_offset = 0;
|
|
for (auto& slot : m_slot_map) {
|
|
max_offset = std::max(max_offset, slot.second.offset + slot.second.size);
|
|
}
|
|
|
|
assert(max_offset < 4096); // just a sanity check here
|
|
std::vector<int> var_count(max_offset, 0);
|
|
|
|
for (auto& slot : m_slot_map) {
|
|
for (int i = 0; i < slot.second.size; i++) {
|
|
var_count.at(slot.second.offset + i)++;
|
|
}
|
|
}
|
|
|
|
for (size_t i = 0; i < var_count.size(); i++) {
|
|
if (var_count[i] > 1) {
|
|
throw std::runtime_error(
|
|
fmt::format("There are {} variables at stack offset {}", var_count[i], i));
|
|
}
|
|
}
|
|
}
|
|
|
|
int StackSpillMap::size() const {
|
|
return m_slot_map.size();
|
|
}
|
|
|
|
namespace {
|
|
struct StackInstrInfo {
|
|
InstructionKind kind;
|
|
bool is_load;
|
|
int size;
|
|
bool is_signed;
|
|
};
|
|
|
|
constexpr StackInstrInfo stack_instrs[] = {{InstructionKind::SQ, false, 16, false},
|
|
{InstructionKind::LQ, true, 16, false},
|
|
{InstructionKind::SW, false, 4, false},
|
|
{InstructionKind::SH, false, 2, false},
|
|
{InstructionKind::SB, false, 1, false},
|
|
{InstructionKind::LBU, true, 1, false},
|
|
//{InstructionKind::LWU, true, 4, false}
|
|
{InstructionKind::SD, false, 8, false},
|
|
{InstructionKind::SWC1, false, 4, false},
|
|
{InstructionKind::LWC1, true, 4, false},
|
|
{InstructionKind::SB, false, 1, false},
|
|
{InstructionKind::LBU, true, 1, false}};
|
|
} // namespace
|
|
|
|
StackSpillMap build_spill_map(const std::vector<Instruction>& instructions, Range<int> range) {
|
|
StackSpillMap map;
|
|
|
|
for (auto idx : range) {
|
|
auto& instr = instructions.at(idx);
|
|
|
|
for (auto& instr_template : stack_instrs) {
|
|
if (instr.kind == instr_template.kind) {
|
|
// we are the right kind.
|
|
auto src_reg = instr.get_src(instr_template.is_load ? 1 : 2).get_reg();
|
|
if (src_reg == Register(Reg::GPR, Reg::SP)) {
|
|
StackSpillSlot slot;
|
|
slot.offset = instr.get_src(instr_template.is_load ? 0 : 1).get_imm();
|
|
slot.size = instr_template.size;
|
|
slot.is_signed = instr_template.is_signed;
|
|
map.add_access(slot);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
map.finalize();
|
|
return map;
|
|
}
|
|
} // namespace decompiler
|