Skip to main content

Overview

The rex::codegen::Recompiler class is the main interface for converting PowerPC binary code to C++ source code. It manages the recompilation process, output buffering, and file generation.

Class Definition

namespace rex::codegen {
  struct Recompiler {
    std::unique_ptr<rex::Runtime> runtime;
    CodegenContext* ctx_;
    std::string out;
    size_t cppFileIndex;
    std::vector<std::pair<std::string, std::string>> pendingWrites;
    bool validationFailed_;

    Recompiler();
    ~Recompiler();

    // Output methods
    template <class... Args>
    void print(fmt::format_string<Args...> fmt, Args&&... args);

    template <class... Args>
    void println(fmt::format_string<Args...> fmt, Args&&... args);

    // Recompilation methods
    bool recompile(const FunctionNode& fn, uint32_t base, const ppc_insn& insn,
                   const uint32_t* data,
                   std::unordered_map<uint32_t, JumpTable>::iterator& switchTable,
                   RecompilerLocalVariables& localVariables, CSRState& csrState);

    bool recompile(const FunctionNode& fn);
    bool recompile(bool force);

    // File management
    void SaveCurrentOutData(const std::string_view& name = std::string_view());
    void FlushPendingWrites();
  };
}

Public Members

runtime
std::unique_ptr<rex::Runtime>
Runtime environment for executing recompiled code
ctx_
CodegenContext*
Non-owning pointer to the code generation context
out
std::string
Output buffer accumulating generated C++ code
cppFileIndex
size_t
Counter for generating unique C++ file names
pendingWrites
std::vector<std::pair<std::string, std::string>>
Deferred file writes buffered until validation passes. Each pair contains (filename, content).
validationFailed_
bool
Tracks if validation failed during analysis

Methods

print()

Append formatted text to the output buffer.
template <class... Args>
void print(fmt::format_string<Args...> fmt, Args&&... args)
fmt
fmt::format_string<Args...>
Format string using syntax
args
Args&&...
Variadic arguments to format
Example:
recompiler.print("uint32_t r{} = ", regNum);

println()

Append formatted text with a newline to the output buffer.
template <class... Args>
void println(fmt::format_string<Args...> fmt, Args&&... args)
fmt
fmt::format_string<Args...>
Format string using syntax
args
Args&&...
Variadic arguments to format
Example:
recompiler.println("  ctx.r[{}] = 0x{:08X};", reg, value);

recompile() - Single Instruction

Recompile a single PowerPC instruction (internal use).
bool recompile(const FunctionNode& fn, uint32_t base, const ppc_insn& insn,
               const uint32_t* data,
               std::unordered_map<uint32_t, JumpTable>::iterator& switchTable,
               RecompilerLocalVariables& localVariables, CSRState& csrState)
fn
const FunctionNode&
Function node containing the instruction
base
uint32_t
Base address for the instruction
insn
const ppc_insn&
PowerPC instruction to recompile
data
const uint32_t*
Pointer to instruction data
switchTable
std::unordered_map<uint32_t, JumpTable>::iterator&
Iterator to current jump table (if any)
localVariables
RecompilerLocalVariables&
Tracks which registers are used as local variables
csrState
CSRState&
Current control/status register state (FPU or VMX)
Returns: true on success, false on error

recompile() - Single Function

Recompile an entire function (internal use).
bool recompile(const FunctionNode& fn)
fn
const FunctionNode&
Function node to recompile
Returns: true on success, false on error

recompile() - All Functions

Recompile all functions and write output files.
bool recompile(bool force)
force
bool
If true, generate output even if validation errors occur. If false, validation errors block code generation.
Returns: true if output was generated, false if blocked due to errors
Generated code includes SDK headers (rexglue/runtime/ppc_context.h) and follows the configured code generation options from RecompilerConfig.

SaveCurrentOutData()

Save the current output buffer to pending writes.
void SaveCurrentOutData(const std::string_view& name = std::string_view())
name
const std::string_view&
default:"std::string_view()"
Optional custom filename. If empty, uses auto-generated name based on cppFileIndex.

FlushPendingWrites()

Write all pending files to disk. Called after validation passes.
void FlushPendingWrites()
This method writes all buffered files to the output directory specified in the configuration. Ensure validation has passed before calling.

RecompilerLocalVariables

Tracks which PowerPC registers are allocated as C++ local variables during recompilation.
struct RecompilerLocalVariables {
  bool ctr;        // Count register
  bool xer;        // Fixed-point exception register
  bool reserved;   // Reserved register (r13)
  bool cr[8];      // Condition register fields
  bool r[32];      // General-purpose registers
  bool f[32];      // Floating-point registers
  bool v[128];     // Vector registers
  bool env;        // Environment pointer
  bool temp;       // Temporary variable
  bool v_temp;     // Vector temporary
  bool ea;         // Effective address

  uint32_t mmio_base_regs;  // Bit mask tracking MMIO base addresses

  void set_mmio_base(size_t reg);
  void clear_mmio_base(size_t reg);
  bool is_mmio_base(size_t reg) const;
};
mmio_base_regs
uint32_t
Bit mask where bit N indicates rN contains an MMIO base address. Set when lis loads a value with upper 16 bits >= 0x7F00 (address >= 0x7F000000) or when oris sets upper bits >= 0xC800 (address >= 0xC8000000).

CSRState

Tracks the current control/status register mode.
enum class CSRState { Unknown, FPU, VMX };
Unknown
enum value
Status register mode is unknown
FPU
enum value
Floating-point unit mode
VMX
enum value
Vector/SIMD (AltiVec) mode

Constants

c_eieio
static constexpr uint32_t
default:"0xAC06007C"
Encoded instruction value for PowerPC EIEIO (Enforce In-order Execution of I/O) instruction.

See Also

Build docs developers (and LLMs) love