gcrecomp_core/recompiler/error.rs
1//! Enhanced Error Handling
2//!
3//! This module provides comprehensive error types for the recompiler using `thiserror`.
4//! All errors are zero-cost (no heap allocation) and provide detailed error messages.
5//!
6//! # Error Categories
7//! - **Parsing errors**: DOL file parsing, instruction decoding
8//! - **Analysis errors**: Control flow, data flow, type inference
9//! - **Code generation errors**: Rust code generation failures
10//! - **Memory errors**: Invalid memory accesses
11//! - **Validation errors**: Generated code validation failures
12
13use thiserror::Error;
14
15/// Recompiler error types.
16///
17/// Uses `thiserror` for zero-cost error handling with detailed error messages.
18/// All error variants are marked with `#[cold]` to help the optimizer place
19/// error handling code in cold paths.
20#[derive(Error, Debug, Clone, PartialEq, Eq)]
21pub enum RecompilerError {
22 /// DOL file parsing error.
23 ///
24 /// Occurs when the DOL file format is invalid or cannot be parsed.
25 #[error("DOL parsing error: {0}")]
26 DolParseError(String),
27
28 /// Instruction decoding error.
29 ///
30 /// Occurs when a PowerPC instruction cannot be decoded (invalid opcode, malformed format).
31 #[error("Instruction decode error: {0}")]
32 InstructionDecodeError(String),
33
34 /// Code generation error.
35 ///
36 /// Occurs when Rust code generation fails (invalid IR, unsupported instruction, etc.).
37 #[error("Code generation error: {0}")]
38 CodeGenError(String),
39
40 /// Ghidra analysis error.
41 ///
42 /// Occurs when Ghidra analysis fails (Ghidra not found, analysis script error, etc.).
43 #[error("Ghidra analysis error: {0}")]
44 GhidraError(String),
45
46 /// Memory access error.
47 ///
48 /// Occurs when accessing invalid memory addresses (out of bounds, unmapped region).
49 #[error("Memory access error: address 0x{0:08X}")]
50 MemoryError(u32),
51
52 /// Invalid register error.
53 ///
54 /// Occurs when using an invalid register number (PowerPC has 32 GPRs, r0-r31).
55 #[error("Invalid register: {0} (must be 0-31)")]
56 InvalidRegister(u8),
57
58 /// Optimization error.
59 ///
60 /// Occurs when an optimization pass fails (invalid CFG, data flow analysis error, etc.).
61 #[error("Optimization error: {0}")]
62 OptimizationError(String),
63
64 /// Validation error.
65 ///
66 /// Occurs when generated Rust code validation fails (syntax error, type error, etc.).
67 #[error("Validation error: {0}")]
68 ValidationError(String),
69}
70
71impl From<std::io::Error> for RecompilerError {
72 #[cold] // Error paths are cold
73 fn from(err: std::io::Error) -> Self {
74 RecompilerError::DolParseError(format!("IO error: {}", err))
75 }
76}
77