-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement
m_inst_oneof
, m_inst_allof
Matchers
- Loading branch information
1 parent
a3b2491
commit 7df70cd
Showing
13 changed files
with
176 additions
and
22 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::collections::HashMap; | ||
|
||
use gitql_ast::types::varargs::VarargsType; | ||
use gitql_core::signature::Signature; | ||
use gitql_core::signature::StandardFunction; | ||
use gitql_core::values::base::Value; | ||
|
||
use crate::ir::types::InstMatcherType; | ||
use crate::ir::values::InstMatcherValue; | ||
use crate::matchers::compound::CompoundInstMatcher; | ||
use crate::matchers::InstMatcher; | ||
|
||
#[inline(always)] | ||
pub fn register_compound_matchers_function(map: &mut HashMap<&'static str, StandardFunction>) { | ||
map.insert("m_inst_oneof", match_oneof_compound_inst); | ||
map.insert("m_inst_allof", match_allof_compound_inst); | ||
} | ||
|
||
#[inline(always)] | ||
pub fn register_compound_matchers_function_signatures(map: &mut HashMap<&'static str, Signature>) { | ||
map.insert( | ||
"m_inst_oneof", | ||
Signature { | ||
parameters: vec![ | ||
Box::new(InstMatcherType), | ||
Box::new(VarargsType { | ||
base: Box::new(InstMatcherType), | ||
}), | ||
], | ||
return_type: Box::new(InstMatcherType), | ||
}, | ||
); | ||
|
||
map.insert( | ||
"m_inst_allof", | ||
Signature { | ||
parameters: vec![ | ||
Box::new(InstMatcherType), | ||
Box::new(VarargsType { | ||
base: Box::new(InstMatcherType), | ||
}), | ||
], | ||
return_type: Box::new(InstMatcherType), | ||
}, | ||
); | ||
} | ||
|
||
fn match_oneof_compound_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> { | ||
let mut matchers: Vec<Box<dyn InstMatcher>> = vec![]; | ||
for value in values.iter() { | ||
if let Some(inst_matcher) = value.as_any().downcast_ref::<InstMatcherValue>() { | ||
matchers.push(inst_matcher.matcher.to_owned()); | ||
} | ||
} | ||
let matcher = Box::new(CompoundInstMatcher::create_one_of(matchers)); | ||
Box::new(InstMatcherValue { matcher }) | ||
} | ||
|
||
fn match_allof_compound_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> { | ||
let mut matchers: Vec<Box<dyn InstMatcher>> = vec![]; | ||
for value in values.iter() { | ||
if let Some(inst_matcher) = value.as_any().downcast_ref::<InstMatcherValue>() { | ||
matchers.push(inst_matcher.matcher.to_owned()); | ||
} | ||
} | ||
let matcher = Box::new(CompoundInstMatcher::create_all_of(matchers)); | ||
Box::new(InstMatcherValue { matcher }) | ||
} |
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,5 +1,6 @@ | ||
pub mod arithmetic; | ||
pub mod binary; | ||
pub mod compound; | ||
pub mod constants; | ||
pub mod exception; | ||
pub mod fcmp; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use inkwell::llvm_sys::prelude::LLVMValueRef; | ||
|
||
use super::InstMatcher; | ||
|
||
#[derive(PartialEq, Clone)] | ||
enum CompoundMatcherKind { | ||
OneOf, | ||
AllOf, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct CompoundInstMatcher { | ||
matchers: Vec<Box<dyn InstMatcher>>, | ||
matcher_kind: CompoundMatcherKind, | ||
} | ||
|
||
impl CompoundInstMatcher { | ||
pub fn create_one_of(matchers: Vec<Box<dyn InstMatcher>>) -> Self { | ||
CompoundInstMatcher { | ||
matchers, | ||
matcher_kind: CompoundMatcherKind::OneOf, | ||
} | ||
} | ||
|
||
pub fn create_all_of(matchers: Vec<Box<dyn InstMatcher>>) -> Self { | ||
CompoundInstMatcher { | ||
matchers, | ||
matcher_kind: CompoundMatcherKind::AllOf, | ||
} | ||
} | ||
} | ||
|
||
impl InstMatcher for CompoundInstMatcher { | ||
fn is_match(&self, instruction: LLVMValueRef) -> bool { | ||
let mut matches_count = 0; | ||
for matcher in self.matchers.iter() { | ||
let is_matches = matcher.is_match(instruction); | ||
|
||
// If kind is `oneOf` and one if matches, return true | ||
if is_matches && self.matcher_kind == CompoundMatcherKind::OneOf { | ||
return true; | ||
} | ||
|
||
// If kind is `allOf` and one is not matches, return false | ||
if !is_matches && self.matcher_kind == CompoundMatcherKind::AllOf { | ||
return false; | ||
} | ||
|
||
if is_matches { | ||
matches_count += 1; | ||
} | ||
} | ||
|
||
match self.matcher_kind { | ||
CompoundMatcherKind::OneOf => matches_count > 1, | ||
CompoundMatcherKind::AllOf => matches_count == self.matchers.len(), | ||
} | ||
} | ||
} |
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
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