use crate::recompiler::decoder::DecodedInstruction;
use std::collections::HashMap;
pub struct Optimizer {
constant_folding: bool,
dead_code_elimination: bool,
register_allocation: bool,
}
impl Optimizer {
#[inline] pub fn new() -> Self {
Self {
constant_folding: true,
dead_code_elimination: true,
register_allocation: true,
}
}
#[inline] pub fn optimize(&self, instructions: &[DecodedInstruction]) -> Vec<DecodedInstruction> {
let mut optimized: Vec<DecodedInstruction> = instructions.to_vec();
if self.constant_folding {
optimized = self.fold_constants(&optimized);
}
if self.dead_code_elimination {
optimized = self.eliminate_dead_code(&optimized);
}
optimized
}
#[inline] fn fold_constants(&self, instructions: &[DecodedInstruction]) -> Vec<DecodedInstruction> {
let mut result: Vec<DecodedInstruction> = Vec::with_capacity(instructions.len());
let mut constants: HashMap<u8, Option<u32>> = HashMap::new();
for inst in instructions.iter() {
let optimized: DecodedInstruction = inst.clone(); result.push(optimized);
}
result
}
#[inline] fn eliminate_dead_code(&self, instructions: &[DecodedInstruction]) -> Vec<DecodedInstruction> {
let mut result: Vec<DecodedInstruction> = Vec::with_capacity(instructions.len());
let mut used_registers: std::collections::HashSet<u8> = std::collections::HashSet::new();
for inst in instructions.iter().rev() {
}
for inst in instructions.iter() {
result.push(inst.clone());
}
result
}
}
impl Default for Optimizer {
#[inline] fn default() -> Self {
Self::new()
}
}