ir.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. use std::collections::BTreeMap;
  2. #[derive(Debug)]
  3. pub enum Instruction {
  4. Nop,
  5. Add{ offset: i64, value: i64 },
  6. Set{ offset: i64, value: i64 },
  7. LinearLoop(BTreeMap<i64, i64>),
  8. MovePtr(i64),
  9. Loop(Vec<Instruction>),
  10. Read(i64),
  11. Write(i64)
  12. }
  13. pub trait MutVisitor {
  14. type Ret: Default;
  15. fn visit_instructions(&mut self, instr: &mut Vec<Instruction>) {
  16. for inst in instr {
  17. self.walk_instruction(inst);
  18. }
  19. }
  20. fn visit_nop(&mut self, nop: &mut Instruction) -> Self::Ret {
  21. Self::Ret::default()
  22. }
  23. fn visit_add(&mut self, add: &mut Instruction) -> Self::Ret {
  24. Self::Ret::default()
  25. }
  26. fn visit_set(&mut self, add: &mut Instruction) -> Self::Ret {
  27. Self::Ret::default()
  28. }
  29. fn visit_linear_loop(&mut self, lloop: &mut Instruction) -> Self::Ret {
  30. Self::Ret::default()
  31. }
  32. fn visit_move_ptr(&mut self, move_ptr: &mut Instruction) -> Self::Ret {
  33. Self::Ret::default()
  34. }
  35. fn visit_loop(&mut self, l: &mut Instruction) -> Self::Ret {
  36. if let Instruction::Loop(instrs) = l {
  37. self.visit_instructions(instrs);
  38. }
  39. Self::Ret::default()
  40. }
  41. fn visit_read(&mut self, read: &mut Instruction) -> Self::Ret {
  42. Self::Ret::default()
  43. }
  44. fn visit_write(&mut self, write: &mut Instruction) -> Self::Ret {
  45. Self::Ret::default()
  46. }
  47. fn walk_instruction(&mut self, inst: &mut Instruction) -> Self::Ret {
  48. use self::Instruction::*;
  49. match inst {
  50. Nop => self.visit_nop(inst),
  51. Add {offset: _, value: _} => self.visit_add(inst),
  52. Set {offset: _, value: _} => self.visit_set(inst),
  53. LinearLoop (_) => self.visit_linear_loop(inst),
  54. MovePtr(_) => self.visit_move_ptr(inst),
  55. Loop(_) => self.visit_loop(inst),
  56. Read(_) => self.visit_read(inst),
  57. Write(_) => self.visit_write(inst),
  58. }
  59. }
  60. }