Skip to content

Commit 270a8a3

Browse files
committed
Implement m_range_int Matcher
1 parent a4be548 commit 270a8a3

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

docs/matchers/constants.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
### Constants Instructions Matchers functions
22

3-
| Function | Parameters | Return | Description |
4-
| :------------: | :----------: | :---------: | :--------------------------------------------------------------: |
5-
| m_const_int | | InstMatcher | Build Inst Matcher that match constants int value |
6-
| m_zero | | InstMatcher | Build Inst Matcher that match constants int with value 0 |
7-
| m_one | | InstMatcher | Build Inst Matcher that match constants int with value 1 |
8-
| m_power2 | | InstMatcher | Build Inst Matcher that match constants int if it's power of two |
9-
| m_specific_int | (value: Int) | InstMatcher | Build Inst Matcher that match specific constants int value |
10-
| m_const_fp | | InstMatcher | Build Inst Matcher that match constants float value |
11-
| m_const_null | | InstMatcher | Build Inst Matcher that match constants pointer null |
3+
| Function | Parameters | Return | Description |
4+
| :------------: | :---------------------: | :---------: | :--------------------------------------------------------------: |
5+
| m_const_int | | InstMatcher | Build Inst Matcher that match constants int value |
6+
| m_zero | | InstMatcher | Build Inst Matcher that match constants int with value 0 |
7+
| m_one | | InstMatcher | Build Inst Matcher that match constants int with value 1 |
8+
| m_power2 | | InstMatcher | Build Inst Matcher that match constants int if it's power of two |
9+
| m_specific_int | (value: Int) | InstMatcher | Build Inst Matcher that match specific constants int value |
10+
| m_range_int | (start: Int, end : Int) | InstMatcher | Build Inst Matcher that match a range of constants int value |
11+
| m_const_fp | | InstMatcher | Build Inst Matcher that match constants float value |
12+
| m_const_null | | InstMatcher | Build Inst Matcher that match constants pointer null |

src/functions/matchers/constants.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn register_constants_matchers_functions(map: &mut HashMap<&'static str, Sta
1919
map.insert("m_one", match_const_one_inst);
2020
map.insert("m_power2", match_const_power_of_two_inst);
2121
map.insert("m_specific_int", match_const_specific_int_inst);
22+
map.insert("m_range_int", match_range_int_inst);
2223

2324
map.insert("m_const_fp", match_const_fp_inst);
2425
map.insert("m_const_null", match_const_null_inst);
@@ -38,6 +39,14 @@ pub fn register_constants_matchers_function_signatures(map: &mut HashMap<&'stati
3839
},
3940
);
4041

42+
map.insert(
43+
"m_range_int",
44+
Signature {
45+
parameters: vec![Box::new(IntType), Box::new(IntType)],
46+
return_type: Box::new(InstMatcherType),
47+
},
48+
);
49+
4150
map.insert("m_const_fp", matcher_signature_without_parameters());
4251
map.insert("m_const_null", matcher_signature_without_parameters());
4352
}
@@ -68,6 +77,13 @@ fn match_const_specific_int_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
6877
Box::new(InstMatcherValue { matcher })
6978
}
7079

80+
fn match_range_int_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
81+
let range_start = values[0].as_int().unwrap();
82+
let range_end = values[1].as_int().unwrap();
83+
let matcher = Box::new(ConstIntMatcher::create_range_int(range_start, range_end));
84+
Box::new(InstMatcherValue { matcher })
85+
}
86+
7187
fn match_const_fp_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
7288
let matcher = Box::new(ConstFloatMatcher);
7389
Box::new(InstMatcherValue { matcher })

src/matchers/constants.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::Matcher;
77
#[derive(Clone)]
88
enum ConstIntMatcherCondition {
99
Specific(i64),
10+
InRange(i64, i64),
1011
PowerOfTwo,
1112
}
1213

@@ -33,6 +34,12 @@ impl ConstIntMatcher {
3334
}
3435
}
3536

37+
pub fn create_range_int(start: i64, end: i64) -> Self {
38+
ConstIntMatcher {
39+
condition: Some(ConstIntMatcherCondition::InRange(start, end)),
40+
}
41+
}
42+
3643
pub fn create_one() -> Self {
3744
ConstIntMatcher {
3845
condition: Some(ConstIntMatcherCondition::Specific(1)),
@@ -59,6 +66,7 @@ impl Matcher<LLVMValueRef> for ConstIntMatcher {
5966
return match matcher_condition {
6067
ConstIntMatcherCondition::Specific(value) => si64_value == *value,
6168
ConstIntMatcherCondition::PowerOfTwo => (si64_value & (si64_value - 1)) != 0,
69+
ConstIntMatcherCondition::InRange(s, e) => si64_value >= *s || si64_value <= *e,
6270
};
6371
}
6472

0 commit comments

Comments
 (0)