use anyhow::Result;
pub trait Optimizer {
fn optimize(&mut self, assembly: &str) -> Result<String>;
fn statistics(&self) -> OptimizationStats;
}
#[derive(Debug, Clone, Default)]
pub struct OptimizationStats {
pub passes_run: usize,
pub instructions_removed: usize,
pub instructions_added: usize,
pub registers_saved: usize,
pub functions_inlined: usize,
pub loops_unrolled: usize,
}
pub trait ConstantFolding {
fn fold_constants(&mut self, assembly: &str) -> Result<String>;
fn evaluate_constant(&self, expr: &str) -> Option<i64>;
}
pub trait DeadCodeElimination {
fn remove_dead_code(&mut self, assembly: &str) -> Result<String>;
fn remove_unused_variables(&mut self, assembly: &str) -> Result<String>;
fn remove_empty_blocks(&mut self, assembly: &str) -> Result<String>;
}
pub trait CommonSubexprElimination {
fn eliminate_common_subexprs(&mut self, assembly: &str) -> Result<String>;
fn find_common_subexprs(&self, assembly: &str) -> Vec<String>;
}
pub trait FunctionInlining {
fn inline_functions(&mut self, assembly: &str) -> Result<String>;
fn should_inline(&self, func_name: &str, func_size: usize) -> bool;
fn inlining_threshold(&self) -> usize;
}
pub trait LoopOptimizer {
fn unroll_loops(&mut self, assembly: &str) -> Result<String>;
fn vectorize_loops(&mut self, assembly: &str) -> Result<String>;
fn hoist_loop_invariants(&mut self, assembly: &str) -> Result<String>;
fn optimize_induction_vars(&mut self, assembly: &str) -> Result<String>;
}
pub trait RegisterOptimizer {
fn optimize_registers(&mut self, assembly: &str) -> Result<String>;
fn allocate_registers(&mut self, assembly: &str) -> Result<String>;
fn spill_registers(&mut self, assembly: &str) -> Result<String>;
}
pub trait PeepholeOptimizer {
fn optimize_peephole(&mut self, assembly: &str) -> Result<String>;
fn apply_pattern(&mut self, pattern: &str, replacement: &str) -> Result<String>;
fn peephole_patterns(&self) -> Vec<(String, String)>;
}
pub trait StrengthReduction {
fn reduce_strength(&mut self, assembly: &str) -> Result<String>;
fn replace_mul_with_shift(&mut self, assembly: &str) -> Result<String>;
fn replace_div_with_shift(&mut self, assembly: &str) -> Result<String>;
}
pub trait TailCallOptimization {
fn optimize_tail_calls(&mut self, assembly: &str) -> Result<String>;
fn is_tail_call(&self, instruction: &str) -> bool;
}
pub trait InlineAssembly {
fn parse_inline_asm(&mut self, asm: &str) -> Result<Vec<String>>;
fn validate_inline_asm(&mut self, asm: &str) -> Result<bool>;
fn integrate_inline_asm(&mut self, assembly: &str, inline: &str) -> Result<String>;
}