Skip to content

Commit 8e134ef

Browse files
committed
Implement matchers for the rest of casting functions
1 parent 2ef91ee commit 8e134ef

File tree

3 files changed

+101
-58
lines changed

3 files changed

+101
-58
lines changed

docs/matchers/cast.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
| Function | Parameters | Return | Description |
44
| :---------------: | :--------: | :---------: | :--------------------------------------------------------: |
5-
| m_zext | | InstMatcher | Build Inst Matcher that matchers zext instruction |
6-
| m_sext | | InstMatcher | Build Inst Matcher that matchers sext instruction |
5+
| m_trunc | | InstMatcher | Build Inst Matcher that matchers trunc instruction |
6+
| m_fp_to_ui | | InstMatcher | Build Inst Matcher that matchers fp to ui instruction |
7+
| m_fp_to_si | | InstMatcher | Build Inst Matcher that matchers fp to si instruction |
8+
| m_fp_trunc | | InstMatcher | Build Inst Matcher that matchers trunc instruction |
79
| m_int_to_ptr | | InstMatcher | Build Inst Matcher that matchers IntToPtr instruction |
810
| m_ptr_to_int | | InstMatcher | Build Inst Matcher that matchers PtrToInt instruction |
11+
| m_zext | | InstMatcher | Build Inst Matcher that matchers zext instruction |
12+
| m_sext | | InstMatcher | Build Inst Matcher that matchers sext instruction |
913
| m_bit_cast | | InstMatcher | Build Inst Matcher that matchers Bit cast instruction |
1014
| m_addr_space_cast | | InstMatcher | Build Inst Matcher that matchers AddrSpaceCast instruction |

src/functions/matchers/cast.rs

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,82 @@ use crate::matchers::cast::CastInstMatcher;
1010

1111
#[inline(always)]
1212
pub fn register_cast_matchers_function(map: &mut HashMap<&'static str, StandardFunction>) {
13+
map.insert("m_trunc", match_trunc);
14+
map.insert("m_fp_to_ui", match_fp_to_ui);
15+
map.insert("m_fp_to_si", match_fp_to_si);
16+
map.insert("m_fp_trunc", match_fp_trunc);
17+
map.insert("m_int_to_ptr", match_int_to_ptr);
18+
map.insert("m_ptr_to_int", match_ptr_to_int);
1319
map.insert("m_zext", match_zext);
1420
map.insert("m_sext", match_sext);
15-
map.insert("m_int_to_ptr", match_int_to_ptr_inst);
16-
map.insert("m_ptr_to_int", match_ptr_to_int_inst);
17-
map.insert("m_bit_cast", match_bit_cast_inst);
18-
map.insert("m_addr_space_cast", match_addr_space_cast_inst);
21+
map.insert("m_bit_cast", match_bit_cast);
22+
map.insert("m_addr_space_cast", match_addr_space_cast);
1923
}
2024

2125
#[inline(always)]
2226
pub fn register_cast_matchers_function_signatures(map: &mut HashMap<&'static str, Signature>) {
23-
map.insert("m_zext", Signature::with_return(Box::new(InstMatcherType)));
24-
25-
map.insert("m_sext", Signature::with_return(Box::new(InstMatcherType)));
26-
27-
map.insert(
28-
"m_int_to_ptr",
29-
Signature::with_return(Box::new(InstMatcherType)),
30-
);
27+
map.insert("m_trunc", cast_function_signature());
28+
map.insert("m_fp_to_ui", cast_function_signature());
29+
map.insert("m_fp_to_si", cast_function_signature());
30+
map.insert("m_fp_trunc", cast_function_signature());
31+
map.insert("m_int_to_ptr", cast_function_signature());
32+
map.insert("m_ptr_to_int", cast_function_signature());
33+
map.insert("m_zext", cast_function_signature());
34+
map.insert("m_sext", cast_function_signature());
35+
map.insert("m_bit_cast", cast_function_signature());
36+
map.insert("m_addr_space_cast", cast_function_signature());
37+
}
3138

32-
map.insert(
33-
"m_ptr_to_int",
34-
Signature::with_return(Box::new(InstMatcherType)),
35-
);
39+
fn cast_function_signature() -> Signature {
40+
Signature::with_return(Box::new(InstMatcherType))
41+
}
3642

37-
map.insert(
38-
"m_bit_cast",
39-
Signature::with_return(Box::new(InstMatcherType)),
40-
);
43+
fn match_trunc(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
44+
let matcher = Box::new(CastInstMatcher::create_trunc());
45+
Box::new(InstMatcherValue { matcher })
46+
}
4147

42-
map.insert(
43-
"m_addr_space_cast",
44-
Signature::with_return(Box::new(InstMatcherType)),
45-
);
48+
fn match_fp_to_ui(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
49+
let matcher = Box::new(CastInstMatcher::create_fp_to_ui());
50+
Box::new(InstMatcherValue { matcher })
4651
}
4752

48-
fn match_zext(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
49-
let matcher = Box::new(CastInstMatcher::create_zext());
53+
fn match_fp_to_si(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
54+
let matcher = Box::new(CastInstMatcher::create_fp_to_si());
5055
Box::new(InstMatcherValue { matcher })
5156
}
5257

53-
fn match_sext(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
54-
let matcher = Box::new(CastInstMatcher::create_sext());
58+
fn match_fp_trunc(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
59+
let matcher = Box::new(CastInstMatcher::create_fp_trunc());
5560
Box::new(InstMatcherValue { matcher })
5661
}
5762

58-
fn match_int_to_ptr_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
63+
fn match_int_to_ptr(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
5964
let matcher = Box::new(CastInstMatcher::create_int_to_ptr());
6065
Box::new(InstMatcherValue { matcher })
6166
}
6267

63-
fn match_ptr_to_int_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
68+
fn match_ptr_to_int(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
6469
let matcher = Box::new(CastInstMatcher::create_ptr_to_int());
6570
Box::new(InstMatcherValue { matcher })
6671
}
6772

68-
fn match_bit_cast_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
73+
fn match_zext(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
74+
let matcher = Box::new(CastInstMatcher::create_zext());
75+
Box::new(InstMatcherValue { matcher })
76+
}
77+
78+
fn match_sext(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
79+
let matcher = Box::new(CastInstMatcher::create_sext());
80+
Box::new(InstMatcherValue { matcher })
81+
}
82+
83+
fn match_bit_cast(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
6984
let matcher = Box::new(CastInstMatcher::create_bit_cast());
7085
Box::new(InstMatcherValue { matcher })
7186
}
7287

73-
fn match_addr_space_cast_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
88+
fn match_addr_space_cast(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
7489
let matcher = Box::new(CastInstMatcher::create_addr_space_cast());
7590
Box::new(InstMatcherValue { matcher })
7691
}

src/matchers/cast.rs

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ use super::InstMatcher;
66

77
#[derive(PartialEq, Clone)]
88
enum CastMatcherKind {
9+
Trunc,
910
IntToPtr,
1011
PtrToInt,
12+
FPToUI,
13+
FPToSI,
14+
FPTrunc,
15+
FPExt,
1116
ZExt,
1217
SExt,
1318
BitCast,
@@ -17,10 +22,15 @@ enum CastMatcherKind {
1722
impl CastMatcherKind {
1823
pub fn llvm_opcode(&self) -> LLVMOpcode {
1924
match self {
20-
CastMatcherKind::ZExt => LLVMOpcode::LLVMZExt,
21-
CastMatcherKind::SExt => LLVMOpcode::LLVMSExt,
25+
CastMatcherKind::Trunc => LLVMOpcode::LLVMTrunc,
26+
CastMatcherKind::FPToUI => LLVMOpcode::LLVMFPToUI,
27+
CastMatcherKind::FPToSI => LLVMOpcode::LLVMFPToSI,
28+
CastMatcherKind::FPTrunc => LLVMOpcode::LLVMFPTrunc,
29+
CastMatcherKind::FPExt => LLVMOpcode::LLVMFPExt,
2230
CastMatcherKind::IntToPtr => LLVMOpcode::LLVMIntToPtr,
2331
CastMatcherKind::PtrToInt => LLVMOpcode::LLVMPtrToInt,
32+
CastMatcherKind::ZExt => LLVMOpcode::LLVMZExt,
33+
CastMatcherKind::SExt => LLVMOpcode::LLVMSExt,
2434
CastMatcherKind::BitCast => LLVMOpcode::LLVMBitCast,
2535
CastMatcherKind::AddrSpaceCast => LLVMOpcode::LLVMAddrSpaceCast,
2636
}
@@ -29,50 +39,64 @@ impl CastMatcherKind {
2939

3040
#[derive(Clone)]
3141
pub struct CastInstMatcher {
32-
matcher_kind: CastMatcherKind,
42+
kind: CastMatcherKind,
3343
}
3444

3545
impl CastInstMatcher {
36-
pub fn create_zext() -> CastInstMatcher {
37-
CastInstMatcher {
38-
matcher_kind: CastMatcherKind::ZExt,
39-
}
46+
fn new(kind: CastMatcherKind) -> Self {
47+
CastInstMatcher { kind }
48+
}
49+
}
50+
51+
impl CastInstMatcher {
52+
pub fn create_trunc() -> CastInstMatcher {
53+
CastInstMatcher::new(CastMatcherKind::Trunc)
4054
}
4155

42-
pub fn create_sext() -> CastInstMatcher {
43-
CastInstMatcher {
44-
matcher_kind: CastMatcherKind::SExt,
45-
}
56+
pub fn create_fp_to_ui() -> CastInstMatcher {
57+
CastInstMatcher::new(CastMatcherKind::FPToUI)
58+
}
59+
60+
pub fn create_fp_to_si() -> CastInstMatcher {
61+
CastInstMatcher::new(CastMatcherKind::FPToSI)
62+
}
63+
64+
pub fn create_fp_trunc() -> CastInstMatcher {
65+
CastInstMatcher::new(CastMatcherKind::FPTrunc)
66+
}
67+
68+
pub fn create_fpext() -> CastInstMatcher {
69+
CastInstMatcher::new(CastMatcherKind::FPExt)
4670
}
4771

4872
pub fn create_int_to_ptr() -> CastInstMatcher {
49-
CastInstMatcher {
50-
matcher_kind: CastMatcherKind::IntToPtr,
51-
}
73+
CastInstMatcher::new(CastMatcherKind::IntToPtr)
5274
}
5375

5476
pub fn create_ptr_to_int() -> CastInstMatcher {
55-
CastInstMatcher {
56-
matcher_kind: CastMatcherKind::PtrToInt,
57-
}
77+
CastInstMatcher::new(CastMatcherKind::PtrToInt)
78+
}
79+
80+
pub fn create_zext() -> CastInstMatcher {
81+
CastInstMatcher::new(CastMatcherKind::ZExt)
82+
}
83+
84+
pub fn create_sext() -> CastInstMatcher {
85+
CastInstMatcher::new(CastMatcherKind::SExt)
5886
}
5987

6088
pub fn create_bit_cast() -> CastInstMatcher {
61-
CastInstMatcher {
62-
matcher_kind: CastMatcherKind::BitCast,
63-
}
89+
CastInstMatcher::new(CastMatcherKind::BitCast)
6490
}
6591

6692
pub fn create_addr_space_cast() -> CastInstMatcher {
67-
CastInstMatcher {
68-
matcher_kind: CastMatcherKind::AddrSpaceCast,
69-
}
93+
CastInstMatcher::new(CastMatcherKind::AddrSpaceCast)
7094
}
7195
}
7296

7397
impl InstMatcher for CastInstMatcher {
7498
#[allow(clippy::not_unsafe_ptr_arg_deref)]
7599
fn is_match(&self, instruction: LLVMValueRef) -> bool {
76-
unsafe { self.matcher_kind.llvm_opcode() == LLVMGetInstructionOpcode(instruction) }
100+
unsafe { self.kind.llvm_opcode() == LLVMGetInstructionOpcode(instruction) }
77101
}
78102
}

0 commit comments

Comments
 (0)