Nicolas Winkler 5 năm trước cách đây
mục cha
commit
13dc5ce865
4 tập tin đã thay đổi với 54 bổ sung28 xóa
  1. 3 1
      src/ir.rs
  2. 3 7
      src/main.rs
  3. 47 19
      src/optimize.rs
  4. 1 1
      src/trans/c.rs

+ 3 - 1
src/ir.rs

@@ -43,7 +43,7 @@ impl Instruction {
                 }
             },
             Set{ offset, value } => format!("Set(@{}, {})", offset, value),
-            LinearLoop{ offset, factors } => {
+            LinearLoop{ offset: _offset, factors: _factors } => {
                 "LinearLoop".to_string()
             },
             MovePtr(val) => format!("MovePtr({})", val),
@@ -62,6 +62,7 @@ impl Instruction {
     }
 }
 
+#[allow(unused_variables)]
 pub trait MutVisitor {
     type Ret: Default;
 
@@ -121,6 +122,7 @@ pub trait MutVisitor {
     }
 }
 
+#[allow(unused_variables)]
 pub trait ConstVisitor {
     type Ret: Default;
 

+ 3 - 7
src/main.rs

@@ -11,9 +11,6 @@ use std::io::{self, Read};
 use std::fs::File;
 use std::env;
 
-use dynasm::dynasm;
-use dynasmrt::{DynasmApi, DynasmLabelApi};
-
 pub mod ir;
 pub mod parser;
 pub mod interpret;
@@ -22,7 +19,6 @@ pub mod compile;
 pub mod formatter;
 pub mod trans;
 
-use trans::{c};
 
 use crate::ir::MutVisitor;
 
@@ -43,12 +39,12 @@ fn main() -> io::Result<()> {
         lin_loop_optimizer.visit_instructions(&mut insts);
         std::mem::replace(&mut insts, lin_loop_optimizer.instructions);
         
-        for ref inst in &insts {
+        //for ref inst in &insts {
             //println!("{}\n", inst.to_string());
-        }
+        //}
         //println!("{}", trans::java::transpile(&insts));
 
-        let code = compile::compile(&insts);
+        let _code = compile::compile(&insts);
     }
     else if let Err(msg) = insts {
         println!("error parsing: {}", msg);

+ 47 - 19
src/optimize.rs

@@ -1,6 +1,6 @@
-use std::cell::{Cell, RefCell};
+use std::cell::{RefCell};
 use std::collections::BTreeMap;
-use super::{ir, compile};
+use super::{ir};
 use super::ir::Instruction;
 use typed_arena::Arena;
 
@@ -26,55 +26,69 @@ pub enum DfInstr<'a> {
 
 
 impl<'a> DfgOptimizer<'a> {
-    fn get_cell(&'a self, offset: i64) -> &'a DfgNode<'a> {
+
+    fn new() -> Self {
+        DfgOptimizer {
+            arena: Arena::new(),
+            cell_states: BTreeMap::new(),
+            cfg: Vec::new()
+        }
+    }
+
+    fn get_cell<'b: 'a>(&'b self, offset: i64) -> &'a DfgNode<'a> {
         if let Some(cell) = self.cell_states.get(&offset) {
             cell
         }
         else {
-            let off: &mut DfgNode<'a> = self.arena.alloc(DfgNode::Cell(offset));
+            let off: &'b mut DfgNode<'a> = self.arena.alloc(DfgNode::Cell(offset));
             off
         }
     }
-}
+/*}
 
-impl<'a> ir::MutVisitor for DfgOptimizer<'a> {
+impl<'a> ir::MutVisitor<'a> for DfgOptimizer<'a> {
     type Ret = ();
-
-    fn visit_instructions(&mut self, instr: &mut Vec<Instruction>) {
+*/
+    fn visit_instructions(&'a mut self, instr: &mut Vec<Instruction>) {
         for inst in instr {
             self.walk_instruction(inst);
         }
     }
 
+    #[allow(unused_variables)]
     fn visit_add(&mut self, add: &mut Instruction) {
         if let Instruction::Add{ offset, value } = add {
-            /*let cell = self.get_cell(*offset);
+            let s: &'a _ = self;
+            let cell = s.get_cell(*offset);
             let adder = self.arena.alloc(DfgNode::Const(*value));
             let addition = self.arena.alloc(DfgNode::Add(cell, adder));
-            */
         }
     }
 
-    fn visit_set<'b>(&'b mut self, set: &mut Instruction) {
+    #[allow(unused_variables)]
+    fn visit_set(&'a self, set: &mut Instruction) {
         if let Instruction::Set{ offset, value } = set {
-            let arena: &'b _ = &self.arena;
-            let setter: &'b DfgNode<'a> = arena.alloc(DfgNode::Const(*value));
+            let arena: &'a _ = &self.arena;
+            let setter: &'a DfgNode<'a> = arena.alloc(DfgNode::Const(*value));
             self.cell_states.insert(13, setter);
         }
     }
 
-    fn visit_linear_loop(&mut self, lloop: &'_ mut Instruction) {
+    fn visit_linear_loop(&self, _lloop: &'_ mut Instruction) {
     }
 
-    fn visit_move_ptr(&mut self, move_ptr: &'_ mut Instruction) {
+    fn visit_move_ptr(&self, _move_ptr: &'_ mut Instruction) {
     }
 
-    fn visit_loop(&mut self, l: &mut Instruction) {
+    fn visit_loop(&self, l: &mut Instruction) {
         if let Instruction::Loop(instrs) = l {
             let mut increments: BTreeMap<i64, i64> = BTreeMap::new();
             let mut dirty = false;
+
+            let mut optimizer = DfgOptimizer::new();
+
             for inst in instrs {
-                self.walk_instruction(inst);
+                optimizer.walk_instruction(inst);
                 if !dirty {
                     use super::ir::Instruction::*;
                     match inst {
@@ -98,10 +112,24 @@ impl<'a> ir::MutVisitor for DfgOptimizer<'a> {
         }
     }
 
-    fn visit_read(&mut self, read: &'_ mut Instruction) {
+    fn visit_read(&self, _read: &'_ mut Instruction) {
     }
 
-    fn visit_write(&mut self, write: &'_ mut Instruction) {
+    fn visit_write(&self, _write: &'_ mut Instruction) {
+    }
+
+    fn walk_instruction(&mut self, inst: &mut Instruction) {
+        use self::Instruction::*;
+        match inst {
+            Nop => {},
+            Add { offset: _, value: _ } => self.visit_add(inst),
+            Set { offset: _, value: _ } => self.visit_set(inst),
+            LinearLoop { offset: _, factors: _ } => self.visit_linear_loop(inst),
+            MovePtr(_) => self.visit_move_ptr(inst),
+            Loop(_) => self.visit_loop(inst),
+            Read(_) => self.visit_read(inst),
+            Write(_) => self.visit_write(inst),
+        }
     }
 }
 

+ 1 - 1
src/trans/c.rs

@@ -1,5 +1,5 @@
 
-use super::super::{ir, optimize, formatter};
+use super::super::{ir, formatter};
 
 use ir::Instruction;
 use ir::ConstVisitor;