-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pattern Matching setup and SetToZero already added
- Loading branch information
1 parent
4d21e3f
commit d49c490
Showing
7 changed files
with
128 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod instruction_types; | ||
mod instructions; | ||
mod parser; | ||
mod instruction_types; | ||
mod patterns; | ||
|
||
pub use parser::Parser; | ||
pub use instructions::{InstructionTrait, Instruction}; | ||
pub use instruction_types::InstructionType; | ||
pub use instructions::{Instruction, InstructionTrait}; | ||
pub use parser::Parser; | ||
pub use patterns::{Pattern, PatternType}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
mod set_to_zero; | ||
|
||
use crate::ast::{Instruction, InstructionTrait}; | ||
use crate::optimization::optimized_instructions::OptimizedInstruction; | ||
|
||
pub trait Pattern<T> | ||
where | ||
T: InstructionTrait<T>, | ||
{ | ||
fn match_pattern(&self, instructions: &Vec<T>) -> bool; | ||
fn replace(&self, instructions: Vec<T>) -> Vec<T>; | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq)] | ||
pub enum PatternType { | ||
SetToZero(set_to_zero::SetToZero), | ||
} | ||
|
||
impl PatternType { | ||
pub fn iter() -> std::slice::Iter<'static, PatternType> { | ||
static PATTERNS: [PatternType; 1] = [PatternType::SetToZero(set_to_zero::SetToZero {})]; | ||
PATTERNS.iter() | ||
} | ||
|
||
pub fn get_pattern(&self) -> Box<dyn Pattern<OptimizedInstruction>> { | ||
match self { | ||
PatternType::SetToZero(pattern) => Box::new(pattern.clone()), | ||
} | ||
} | ||
|
||
pub fn replace(&self, instructions: Vec<OptimizedInstruction>) -> Vec<OptimizedInstruction> { | ||
self.get_pattern().replace(instructions) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::ast::{InstructionTrait, InstructionType, Pattern, PatternType}; | ||
|
||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct SetToZero {} | ||
|
||
impl<T> Pattern<T> for SetToZero | ||
// This pattern is Loop<Decrement(2n+1) or Loop<Increment(2n+1) | ||
where | ||
T: InstructionTrait<T> + Clone, | ||
{ | ||
fn match_pattern(&self, instructions: &Vec<T>) -> bool { | ||
if instructions.len() != 1 { | ||
return false; | ||
} | ||
|
||
match instructions[0].get_instruction_type() { | ||
InstructionType::Loop => { | ||
let content = instructions[0].get_content_ref(); | ||
if content.len() != 1 { | ||
return false; | ||
} | ||
match content[0].get_instruction_type() { | ||
InstructionType::Decrement | InstructionType::Increment => { | ||
let amount = content[0].get_amount(); | ||
if amount % 2 != 1 { | ||
return false; | ||
} | ||
} | ||
_ => return false, | ||
} | ||
} | ||
_ => return false, | ||
} | ||
|
||
true | ||
} | ||
|
||
fn replace(&self, mut instructions: Vec<T>) -> Vec<T> { | ||
// Find in instructions somewhere where the pattern matches | ||
// Replace the pattern with a SetToZero instruction | ||
// Remove the instructions that were replaced | ||
for instruction in &mut instructions { | ||
if self.match_pattern(&vec![instruction.clone()]) { | ||
let new_instruction = T::new(InstructionType::Pattern(PatternType::SetToZero(SetToZero {})), None); | ||
*instruction = new_instruction; | ||
} | ||
} | ||
|
||
instructions | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters