Skip to content

Commit ec8c246

Browse files
committed
Rename Compound Matchers to Combine Matchers similar to LLVM API
1 parent 9cdaa9b commit ec8c246

File tree

8 files changed

+98
-29
lines changed

8 files changed

+98
-29
lines changed

docs/InstructionMatcher.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ Instructions Matchers are functions that build a instruction matcher to match ag
137137
| m_invoke | () | InstMatcher | Build Inst Matcher that match invoke Instruction |
138138
| m_landingpad | () | InstMatcher | Build Inst Matcher that match landingpad Instruction |
139139

140-
### Compound Instructions Matchers functions
140+
### Combine Instructions Matchers functions
141141

142142
| Function | Parameters | Return | Description |
143143
| :----------: | :--------------------: | :---------: | :------------------------------------------------------------------------------: |
144144
| m_inst_oneof | (inst: ...InstMatcher) | InstMatcher | Build Inst Matcher from list of matchers that return true if one of them matches |
145-
| m_inst_allof | (inst: ...InstMatcher) | InstMatcher | Build Inst Matcher from list of matchers that return true if all of them matches |
145+
| m_inst_allof | (inst: ...InstMatcher) | InstMatcher | Build Inst Matcher from list of matchers that return true if all of them matches |

src/functions/matchers/combine.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use std::collections::HashMap;
2+
3+
use gitql_ast::types::varargs::VarargsType;
4+
use gitql_core::signature::Signature;
5+
use gitql_core::signature::StandardFunction;
6+
use gitql_core::values::base::Value;
7+
8+
use crate::ir::types::InstMatcherType;
9+
use crate::ir::values::InstMatcherValue;
10+
use crate::matchers::combine::CombineInstMatcher;
11+
use crate::matchers::InstMatcher;
12+
13+
#[inline(always)]
14+
pub fn register_combine_matchers_function(map: &mut HashMap<&'static str, StandardFunction>) {
15+
map.insert("m_inst_oneof", match_oneof_combine_inst);
16+
map.insert("m_inst_allof", match_allof_combine_inst);
17+
}
18+
19+
#[inline(always)]
20+
pub fn register_combine_matchers_function_signatures(map: &mut HashMap<&'static str, Signature>) {
21+
map.insert(
22+
"m_inst_oneof",
23+
Signature {
24+
parameters: vec![
25+
Box::new(InstMatcherType),
26+
Box::new(VarargsType {
27+
base: Box::new(InstMatcherType),
28+
}),
29+
],
30+
return_type: Box::new(InstMatcherType),
31+
},
32+
);
33+
34+
map.insert(
35+
"m_inst_allof",
36+
Signature {
37+
parameters: vec![
38+
Box::new(InstMatcherType),
39+
Box::new(VarargsType {
40+
base: Box::new(InstMatcherType),
41+
}),
42+
],
43+
return_type: Box::new(InstMatcherType),
44+
},
45+
);
46+
}
47+
48+
fn match_oneof_combine_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
49+
let mut matchers: Vec<Box<dyn InstMatcher>> = vec![];
50+
for value in values.iter() {
51+
if let Some(inst_matcher) = value.as_any().downcast_ref::<InstMatcherValue>() {
52+
matchers.push(inst_matcher.matcher.to_owned());
53+
}
54+
}
55+
let matcher = Box::new(CombineInstMatcher::create_one_of(matchers));
56+
Box::new(InstMatcherValue { matcher })
57+
}
58+
59+
fn match_allof_combine_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
60+
let mut matchers: Vec<Box<dyn InstMatcher>> = vec![];
61+
for value in values.iter() {
62+
if let Some(inst_matcher) = value.as_any().downcast_ref::<InstMatcherValue>() {
63+
matchers.push(inst_matcher.matcher.to_owned());
64+
}
65+
}
66+
let matcher = Box::new(CombineInstMatcher::create_all_of(matchers));
67+
Box::new(InstMatcherValue { matcher })
68+
}

src/functions/matchers/compound.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ use crate::matchers::compound::CompoundInstMatcher;
1111
use crate::matchers::InstMatcher;
1212

1313
#[inline(always)]
14-
pub fn register_compound_matchers_function(map: &mut HashMap<&'static str, StandardFunction>) {
15-
map.insert("m_inst_oneof", match_oneof_compound_inst);
16-
map.insert("m_inst_allof", match_allof_compound_inst);
14+
pub fn register_combine_matchers_function(map: &mut HashMap<&'static str, StandardFunction>) {
15+
map.insert("m_inst_oneof", match_oneof_combine_inst);
16+
map.insert("m_inst_allof", match_allof_combine_inst);
1717
}
1818

1919
#[inline(always)]
20-
pub fn register_compound_matchers_function_signatures(map: &mut HashMap<&'static str, Signature>) {
20+
pub fn register_combine_matchers_function_signatures(map: &mut HashMap<&'static str, Signature>) {
2121
map.insert(
2222
"m_inst_oneof",
2323
Signature {
@@ -45,24 +45,24 @@ pub fn register_compound_matchers_function_signatures(map: &mut HashMap<&'static
4545
);
4646
}
4747

48-
fn match_oneof_compound_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
48+
fn match_oneof_combine_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
4949
let mut matchers: Vec<Box<dyn InstMatcher>> = vec![];
5050
for value in values.iter() {
5151
if let Some(inst_matcher) = value.as_any().downcast_ref::<InstMatcherValue>() {
5252
matchers.push(inst_matcher.matcher.to_owned());
5353
}
5454
}
55-
let matcher = Box::new(CompoundInstMatcher::create_one_of(matchers));
55+
let matcher = Box::new(CombineInstMatcher::create_one_of(matchers));
5656
Box::new(InstMatcherValue { matcher })
5757
}
5858

59-
fn match_allof_compound_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
59+
fn match_allof_combine_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
6060
let mut matchers: Vec<Box<dyn InstMatcher>> = vec![];
6161
for value in values.iter() {
6262
if let Some(inst_matcher) = value.as_any().downcast_ref::<InstMatcherValue>() {
6363
matchers.push(inst_matcher.matcher.to_owned());
6464
}
6565
}
66-
let matcher = Box::new(CompoundInstMatcher::create_all_of(matchers));
66+
let matcher = Box::new(CombineInstMatcher::create_all_of(matchers));
6767
Box::new(InstMatcherValue { matcher })
6868
}

src/functions/matchers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub mod arithmetic;
22
pub mod binary;
3-
pub mod compound;
3+
pub mod combine;
44
pub mod constants;
55
pub mod exception;
66
pub mod fcmp;

src/functions/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use matchers::arithmetic::register_arithmetic_matchers_function_signatures;
1212
use matchers::arithmetic::register_arithmetic_matchers_functions;
1313
use matchers::binary::register_binary_inst_matchers_function_signatures;
1414
use matchers::binary::register_binary_inst_matchers_functions;
15-
use matchers::compound::register_compound_matchers_function;
16-
use matchers::compound::register_compound_matchers_function_signatures;
15+
use matchers::combine::register_combine_matchers_function;
16+
use matchers::combine::register_combine_matchers_function_signatures;
1717
use matchers::constants::register_constants_matchers_function_signatures;
1818
use matchers::constants::register_constants_matchers_functions;
1919
use matchers::exception::register_exception_inst_matchers_function_signatures;
@@ -52,7 +52,7 @@ pub fn llvm_ir_functions() -> &'static HashMap<&'static str, StandardFunction> {
5252
register_shift_matchers_functions(&mut map);
5353
register_binary_inst_matchers_functions(&mut map);
5454
register_exception_inst_matchers_functions(&mut map);
55-
register_compound_matchers_function(&mut map);
55+
register_combine_matchers_function(&mut map);
5656
map
5757
})
5858
}
@@ -69,7 +69,7 @@ pub fn llvm_ir_function_signatures() -> HashMap<&'static str, Signature> {
6969
register_shift_matchers_function_signatures(&mut map);
7070
register_binary_inst_matchers_function_signatures(&mut map);
7171
register_exception_inst_matchers_function_signatures(&mut map);
72-
register_compound_matchers_function_signatures(&mut map);
72+
register_combine_matchers_function_signatures(&mut map);
7373
map
7474
}
7575

src/matchers/compound.rs renamed to src/matchers/combine.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,46 @@ use inkwell::llvm_sys::prelude::LLVMValueRef;
33
use super::InstMatcher;
44

55
#[derive(PartialEq, Clone)]
6-
enum CompoundMatcherKind {
6+
enum CombineMatcherKind {
77
OneOf,
88
AllOf,
99
}
1010

1111
#[derive(Clone)]
12-
pub struct CompoundInstMatcher {
12+
pub struct CombineInstMatcher {
1313
matchers: Vec<Box<dyn InstMatcher>>,
14-
matcher_kind: CompoundMatcherKind,
14+
matcher_kind: CombineMatcherKind,
1515
}
1616

17-
impl CompoundInstMatcher {
17+
impl CombineInstMatcher {
1818
pub fn create_one_of(matchers: Vec<Box<dyn InstMatcher>>) -> Self {
19-
CompoundInstMatcher {
19+
CombineInstMatcher {
2020
matchers,
21-
matcher_kind: CompoundMatcherKind::OneOf,
21+
matcher_kind: CombineMatcherKind::OneOf,
2222
}
2323
}
2424

2525
pub fn create_all_of(matchers: Vec<Box<dyn InstMatcher>>) -> Self {
26-
CompoundInstMatcher {
26+
CombineInstMatcher {
2727
matchers,
28-
matcher_kind: CompoundMatcherKind::AllOf,
28+
matcher_kind: CombineMatcherKind::AllOf,
2929
}
3030
}
3131
}
3232

33-
impl InstMatcher for CompoundInstMatcher {
33+
impl InstMatcher for CombineInstMatcher {
3434
fn is_match(&self, instruction: LLVMValueRef) -> bool {
3535
let mut matches_count = 0;
3636
for matcher in self.matchers.iter() {
3737
let is_matches = matcher.is_match(instruction);
3838

3939
// If kind is `oneOf` and one if matches, return true
40-
if is_matches && self.matcher_kind == CompoundMatcherKind::OneOf {
40+
if is_matches && self.matcher_kind == CombineMatcherKind::OneOf {
4141
return true;
4242
}
4343

4444
// If kind is `allOf` and one is not matches, return false
45-
if !is_matches && self.matcher_kind == CompoundMatcherKind::AllOf {
45+
if !is_matches && self.matcher_kind == CombineMatcherKind::AllOf {
4646
return false;
4747
}
4848

@@ -52,8 +52,8 @@ impl InstMatcher for CompoundInstMatcher {
5252
}
5353

5454
match self.matcher_kind {
55-
CompoundMatcherKind::OneOf => matches_count > 1,
56-
CompoundMatcherKind::AllOf => matches_count == self.matchers.len(),
55+
CombineMatcherKind::OneOf => matches_count > 1,
56+
CombineMatcherKind::AllOf => matches_count == self.matchers.len(),
5757
}
5858
}
5959
}

src/matchers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl InstMatcher for AnyInstMatcher {
3939

4040
pub mod arithmetic;
4141
pub mod binary;
42-
pub mod compound;
42+
pub mod combine;
4343
pub mod constants;
4444
pub mod exception;
4545
pub mod fcmp;

src/matchers/other.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct ExtractValueInstMatcher {
3333
}
3434

3535
impl InstMatcher for ExtractValueInstMatcher {
36+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
3637
fn is_match(&self, instruction: LLVMValueRef) -> bool {
3738
unsafe {
3839
if LLVMGetInstructionOpcode(instruction) == LLVMOpcode::LLVMExtractValue {

0 commit comments

Comments
 (0)