Skip to content

Commit ae63caf

Browse files
committed
Implement m_cast matcher
1 parent 40545eb commit ae63caf

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

docs/matchers/cast.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
| Function | Parameters | Return | Description |
44
| :---------------: | :-------------------: | :---------: | :--------------------------------------------------------------: |
5+
| m_cast | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers any cast instruction |
56
| m_trunc | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers trunc instruction |
67
| m_fp_to_ui | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers fp to ui instruction |
78
| m_fp_to_si | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers fp to si instruction |

src/functions/matchers/cast.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::matchers::cast::CastInstMatcher;
1212

1313
#[inline(always)]
1414
pub fn register_cast_matchers_function(map: &mut HashMap<&'static str, StandardFunction>) {
15+
map.insert("m_cast", match_any_cast);
1516
map.insert("m_trunc", match_trunc);
1617
map.insert("m_fp_to_ui", match_fp_to_ui);
1718
map.insert("m_fp_to_si", match_fp_to_si);
@@ -28,6 +29,7 @@ pub fn register_cast_matchers_function(map: &mut HashMap<&'static str, StandardF
2829

2930
#[inline(always)]
3031
pub fn register_cast_matchers_function_signatures(map: &mut HashMap<&'static str, Signature>) {
32+
map.insert("m_cast", cast_function_signature());
3133
map.insert("m_trunc", cast_function_signature());
3234
map.insert("m_fp_to_ui", cast_function_signature());
3335
map.insert("m_fp_to_si", cast_function_signature());
@@ -48,6 +50,12 @@ fn cast_function_signature() -> Signature {
4850
.add_parameter(Box::new(OptionType::new(Some(Box::new(InstMatcherType)))))
4951
}
5052

53+
fn match_any_cast(values: &[Box<dyn Value>]) -> Box<dyn Value> {
54+
let value_matcher = single_optional_matcher_value(values);
55+
let matcher = Box::new(CastInstMatcher::create_any(value_matcher));
56+
Box::new(InstMatcherValue { matcher })
57+
}
58+
5159
fn match_trunc(values: &[Box<dyn Value>]) -> Box<dyn Value> {
5260
let value_matcher = single_optional_matcher_value(values);
5361
let matcher = Box::new(CastInstMatcher::create_trunc(value_matcher));

src/matchers/cast.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::Matcher;
66

77
#[derive(PartialEq, Clone)]
88
enum CastMatcherKind {
9+
Any,
910
Trunc,
1011
IntToPtr,
1112
PtrToInt,
@@ -23,19 +24,42 @@ enum CastMatcherKind {
2324
impl CastMatcherKind {
2425
pub fn match_llvm_opcode(&self, llvm_op: LLVMOpcode) -> bool {
2526
match llvm_op {
26-
LLVMOpcode::LLVMTrunc => matches!(self, CastMatcherKind::Trunc),
27-
LLVMOpcode::LLVMFPToUI => matches!(self, CastMatcherKind::FPToUI),
28-
LLVMOpcode::LLVMFPToSI => matches!(self, CastMatcherKind::FPToSI),
29-
LLVMOpcode::LLVMFPTrunc => matches!(self, CastMatcherKind::FPTrunc),
30-
31-
LLVMOpcode::LLVMFPExt => matches!(self, CastMatcherKind::Ext | CastMatcherKind::FPExt),
32-
LLVMOpcode::LLVMZExt => matches!(self, CastMatcherKind::Ext | CastMatcherKind::ZExt),
33-
LLVMOpcode::LLVMSExt => matches!(self, CastMatcherKind::Ext | CastMatcherKind::SExt),
34-
35-
LLVMOpcode::LLVMIntToPtr => matches!(self, CastMatcherKind::IntToPtr),
36-
LLVMOpcode::LLVMPtrToInt => matches!(self, CastMatcherKind::PtrToInt),
37-
LLVMOpcode::LLVMBitCast => matches!(self, CastMatcherKind::BitCast),
38-
LLVMOpcode::LLVMAddrSpaceCast => matches!(self, CastMatcherKind::AddrSpaceCast),
27+
LLVMOpcode::LLVMTrunc => matches!(self, CastMatcherKind::Any | CastMatcherKind::Trunc),
28+
LLVMOpcode::LLVMFPToUI => {
29+
matches!(self, CastMatcherKind::Any | CastMatcherKind::FPToUI)
30+
}
31+
LLVMOpcode::LLVMFPToSI => {
32+
matches!(self, CastMatcherKind::Any | CastMatcherKind::FPToSI)
33+
}
34+
LLVMOpcode::LLVMFPTrunc => {
35+
matches!(self, CastMatcherKind::Any | CastMatcherKind::FPTrunc)
36+
}
37+
38+
LLVMOpcode::LLVMFPExt => matches!(
39+
self,
40+
CastMatcherKind::Any | CastMatcherKind::Ext | CastMatcherKind::FPExt
41+
),
42+
LLVMOpcode::LLVMZExt => matches!(
43+
self,
44+
CastMatcherKind::Any | CastMatcherKind::Ext | CastMatcherKind::ZExt
45+
),
46+
LLVMOpcode::LLVMSExt => matches!(
47+
self,
48+
CastMatcherKind::Any | CastMatcherKind::Ext | CastMatcherKind::SExt
49+
),
50+
51+
LLVMOpcode::LLVMIntToPtr => {
52+
matches!(self, CastMatcherKind::Any | CastMatcherKind::IntToPtr)
53+
}
54+
LLVMOpcode::LLVMPtrToInt => {
55+
matches!(self, CastMatcherKind::Any | CastMatcherKind::PtrToInt)
56+
}
57+
LLVMOpcode::LLVMBitCast => {
58+
matches!(self, CastMatcherKind::Any | CastMatcherKind::BitCast)
59+
}
60+
LLVMOpcode::LLVMAddrSpaceCast => {
61+
matches!(self, CastMatcherKind::Any | CastMatcherKind::AddrSpaceCast)
62+
}
3963
_ => false,
4064
}
4165
}
@@ -57,6 +81,10 @@ impl CastInstMatcher {
5781
}
5882

5983
impl CastInstMatcher {
84+
pub fn create_any(value_matcher: Box<dyn Matcher<LLVMValueRef>>) -> CastInstMatcher {
85+
CastInstMatcher::new(value_matcher, CastMatcherKind::Any)
86+
}
87+
6088
pub fn create_trunc(value_matcher: Box<dyn Matcher<LLVMValueRef>>) -> CastInstMatcher {
6189
CastInstMatcher::new(value_matcher, CastMatcherKind::Trunc)
6290
}

0 commit comments

Comments
 (0)