Files
psxrecomp/recompiler/include/function_analysis.h
T
Matthew Stanley b2929aa504 overlay: restore overlay DLL cache + capture (faithful port of codex/cache-discovery)
Reinstates the runtime-loaded overlay DLL cache (Layer A) and the offline
overlay compilation pipeline that were built on the pruned codex/cache-discovery
branch (tip 2bbafc2) and never merged. Ported onto current master; rebuilt
rather than history-restored.

Runtime (off by default, opt in via [runtime] overlay_cache = true):
- overlay_loader.c/.h, overlay_api.h: CRC32-keyed DLL cache at
  cache/<game_id>/<region>_<crc>.dll. On a dirty-RAM dispatch miss the loader
  LoadLibrary's the matching DLL, wires runtime callbacks via overlay_init(),
  enumerates func_XXXXXXXX exports, and dispatches them BEFORE the interpreter.
- overlay_capture.c/.h: write-once capture set; records overlay DMA bytes and
  writes overlay_captures.json (execution-verified seeds) at clean exit.
- dma.c: capture + cd_dma_log hook at CD DMA completion (async model).
- cdrom.c: cdrom_get_setloc_lba() for disc-position correlation.
- dirty_ram_interp.c: g_dirty_ram_exec_pc_table (exec-verified PCs), a
  decodability guard on JAL-into-dirty targets, and the overlay dispatch hooks.
- debug_server.c: cd_read_log + overlay_loader_status TCP commands.
- config_loader: [runtime] overlay_cache flag (default false).

Recompiler (compile_overlays.py / extract_overlays.py drive these):
- main_psx.cpp --overlay-exact: analyze only explicit entries + direct JAL
  targets and set split_mid_function_targets=false, so overlay basic-block
  targets are NOT split into bogus standalone functions (the mid-function-seed
  failure mode that produced black-on-load / blue-on-new-game).
- function_analysis.cpp: analyze_exact_entries(); code_generator.cpp: gate the
  mid-function split pre-pass behind split_mid_function_targets (default true,
  so normal main-EXE codegen is unchanged - Tomba still regens to 2854 funcs).

Deltas from the original: save states dropped (never worked); overlay enable is
runtime-config-gated (off by default) instead of a build-time define; loader
stderr logging converted to the overlay_loader_status TCP path (CLAUDE.md Rule 3).

Recovered from _backups/2026-06-06-consolidation/psxrecomp-all-PRE.bundle.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-07 12:32:19 -07:00

74 lines
2.5 KiB
C++

#pragma once
#include <cstdint>
#include <vector>
#include <string>
#include "ps1_exe_parser.h"
namespace PSXRecomp {
struct Function {
uint32_t start_addr;
uint32_t end_addr;
uint32_t size;
bool has_prologue;
bool has_epilogue;
int32_t stack_frame_size; // Size of stack frame (if has prologue)
std::string name; // Optional name (for now, just "func_<addr>")
bool is_data_section = false; // True if this "function" is actually a data section
};
struct FunctionAnalysisResult {
std::vector<Function> functions;
int total_instructions;
int jr_ra_count;
int prologue_count;
int call_discovered_count = 0; // Functions found via JAL call-target following
int strong_prologue_count = 0; // Functions found from prologues with saved $ra
int state_continuation_count = 0; // Split entries after calls to SaveState-style helpers
};
class FunctionAnalyzer {
public:
explicit FunctionAnalyzer(const PS1Executable& exe);
// Scan entire executable for function boundaries
FunctionAnalysisResult analyze();
// Analyze only explicit entry points and direct JAL targets reachable from
// them. This is for runtime-loaded overlays where scanning the whole blob
// treats data and basic-block targets as standalone functions.
FunctionAnalysisResult analyze_exact_entries(const std::vector<uint32_t>& entries);
// Add a forced entry point address that is treated as a function start
// even if it has no standard ADDIU $sp prologue. The function will be
// included in the analysis result with has_prologue = false.
void add_forced_entry(uint32_t addr);
// Check if instruction is jr $ra (return)
static bool is_jr_ra(uint32_t instr);
// Check if instruction is function prologue (addiu $sp, $sp, -N)
static bool is_prologue(uint32_t instr, int32_t& stack_size);
// Check if instruction is epilogue (addiu $sp, $sp, +N)
static bool is_epilogue(uint32_t instr, int32_t& stack_size);
// Check if instruction is a branch or jump (has a delay slot)
static bool is_branch_or_jump(uint32_t instr);
private:
const PS1Executable& exe_;
// Forced entry points (from add_forced_entry())
std::vector<uint32_t> forced_entry_points_;
// Find function start by scanning backward from jr $ra
uint32_t find_function_start(uint32_t return_addr);
// Detect if a region is likely a data section masquerading as code
bool is_likely_data_section(uint32_t start_addr, uint32_t end_addr) const;
};
} // namespace PSXRecomp