|
@@ -137,10 +137,34 @@ impl<'a> Compiler<'a> {
|
|
|
}
|
|
|
|
|
|
pub fn remove_dead_code(opcodes: Vec<OpCode>) -> Vec<OpCode> {
|
|
|
- let (new_opcodes, _) = optimizations::remove_dead_code(opcodes);
|
|
|
+ let mut new_opcodes = opcodes;
|
|
|
+ loop {
|
|
|
+ let (new_opcodes_, has_changed) = optimizations::remove_dead_code(new_opcodes);
|
|
|
+ new_opcodes = new_opcodes_;
|
|
|
+ if !has_changed {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
new_opcodes
|
|
|
}
|
|
|
|
|
|
+ pub fn optimize(opcodes: Vec<OpCode>) -> (Vec<OpCode>, bool) {
|
|
|
+ vec![
|
|
|
+ optimizations::assign_unique_register_addresses,
|
|
|
+ optimizations::remove_redundant_load_and_mov,
|
|
|
+ optimizations::remove_unused_values,
|
|
|
+ optimizations::calculate_static_values,
|
|
|
+ optimizations::remove_useless_jumps,
|
|
|
+ //optimizations::move_load_top,
|
|
|
+ ]
|
|
|
+ .into_iter()
|
|
|
+ .fold((opcodes, false), |(opcodes, has_optimized), f| {
|
|
|
+ let (opcodes, optimized) = f(opcodes);
|
|
|
+ (opcodes, has_optimized || optimized)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
/// Returns a list of Addresses that are labeled in the opcodes list. This function is required
|
|
|
/// to resolve any jump in the compiler
|
|
|
pub fn labels_to_addr(opcodes: &[OpCode]) -> HashMap<Addr, Addr> {
|
|
@@ -161,22 +185,6 @@ impl<'a> Compiler<'a> {
|
|
|
.collect::<HashMap<_, _>>()
|
|
|
}
|
|
|
|
|
|
- pub fn optimize(opcodes: Vec<OpCode>) -> (Vec<OpCode>, bool) {
|
|
|
- vec![
|
|
|
- optimizations::assign_unique_register_addresses,
|
|
|
- optimizations::remove_redundant_load_and_mov,
|
|
|
- optimizations::remove_unused_values,
|
|
|
- optimizations::calculate_static_values,
|
|
|
- optimizations::remove_useless_jumps,
|
|
|
- optimizations::move_load_top,
|
|
|
- ]
|
|
|
- .into_iter()
|
|
|
- .fold((opcodes, false), |(opcodes, has_optimized), f| {
|
|
|
- let (opcodes, optimized) = f(opcodes);
|
|
|
- (opcodes, has_optimized || optimized)
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
pub fn resolve_label_to_addr(opcodes: Vec<OpCode>) -> Result<Vec<OpCode>, Error> {
|
|
|
let labels_to_addr = Self::labels_to_addr(&opcodes);
|
|
|
|