Skip to content

Commit

Permalink
feat: Implement m_const_null Matcher for constants pointer null
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Nov 4, 2024
1 parent 151a2ff commit c445816
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/InstructionMatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Instructions Matchers are functions that build a instruction matcher to match ag
| m_inst | (i: Instruction, m: InstMatcher) | Bool | Check if instruction is matched with Matcher |
| m_any_inst | () | InstMatcher | Build Inst Matcher that match any Instruction |
| m_const_int | () | InstMatcher | Build Inst Matcher that match constants int value |
| m_const_fp | () | InstMatcher | Build Inst Matcher that match constants float value |
| m_const_fp | () | InstMatcher | Build Inst Matcher that match constants float value |
| m_const_null | () | InstMatcher | Build Inst Matcher that match constants pointer null |
| m_poison | () | InstMatcher | Build Inst Matcher that match poison value |
| m_label | (n : Text?) | InstMatcher | Build Inst Matcher that match Label with optional name |
| m_return | (m : InstMatcher?) | InstMatcher | Build Inst Matcher that match Return Instruction |
Expand Down
22 changes: 22 additions & 0 deletions src/functions/inst_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::ir::values::LLVMInstValue;
use crate::matchers::instruction_matcher::AnyInstMatcher;
use crate::matchers::instruction_matcher::ConstFloatMatcher;
use crate::matchers::instruction_matcher::ConstIntMatcher;
use crate::matchers::instruction_matcher::ConstPointerNullMatcher;
use crate::matchers::instruction_matcher::LabelInstMatcher;
use crate::matchers::instruction_matcher::PoisonInstMatcher;
use crate::matchers::instruction_matcher::ReturnInstMatcher;
Expand All @@ -33,6 +34,7 @@ pub fn register_inst_matchers_functions(map: &mut HashMap<&'static str, Function
map.insert("m_any_inst", match_any_inst);
map.insert("m_const_int", match_const_int_inst);
map.insert("m_const_fp", match_const_fp_inst);
map.insert("m_const_null", match_const_null_inst);
map.insert("m_poison", match_poison_inst);
map.insert("m_label", match_label_inst);
map.insert("m_return", match_return_inst);
Expand All @@ -52,13 +54,15 @@ pub fn register_inst_matchers_function_signatures(map: &mut HashMap<&'static str
return_type: Box::new(BoolType),
},
);

map.insert(
"m_any_inst",
Signature {
parameters: vec![],
return_type: Box::new(InstMatcherType),
},
);

map.insert(
"m_const_int",
Signature {
Expand All @@ -74,13 +78,23 @@ pub fn register_inst_matchers_function_signatures(map: &mut HashMap<&'static str
return_type: Box::new(InstMatcherType),
},
);

map.insert(
"m_const_null",
Signature {
parameters: vec![],
return_type: Box::new(InstMatcherType),
},
);

map.insert(
"m_poison",
Signature {
parameters: vec![],
return_type: Box::new(InstMatcherType),
},
);

map.insert(
"m_label",
Signature {
Expand All @@ -90,6 +104,7 @@ pub fn register_inst_matchers_function_signatures(map: &mut HashMap<&'static str
return_type: Box::new(InstMatcherType),
},
);

map.insert(
"m_return",
Signature {
Expand All @@ -99,6 +114,7 @@ pub fn register_inst_matchers_function_signatures(map: &mut HashMap<&'static str
return_type: Box::new(InstMatcherType),
},
);

map.insert(
"m_unreachable",
Signature {
Expand Down Expand Up @@ -176,6 +192,12 @@ fn match_const_fp_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
})
}

fn match_const_null_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
Box::new(InstMatcherValue {
matcher: Box::new(ConstPointerNullMatcher),
})
}

fn match_unreachable_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
Box::new(InstMatcherValue {
matcher: Box::new(UnreachableInstMatcher),
Expand Down
15 changes: 15 additions & 0 deletions src/matchers/instruction_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,21 @@ impl InstMatcher for ConstFloatMatcher {
}
}

// Return instruction matcher to check if current value is a constants pointer null
#[derive(Clone)]
pub struct ConstPointerNullMatcher;

impl InstMatcher for ConstPointerNullMatcher {
#[allow(deprecated)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn is_match(&self, instruction: LLVMValueRef) -> bool {
unsafe {
let value_kind = LLVMGetValueKind(instruction);
value_kind == LLVMValueKind::LLVMConstantPointerNullValueKind
}
}
}

// Return instruction matcher to check if current value is poison
#[derive(Clone)]
pub struct PoisonInstMatcher;
Expand Down

0 comments on commit c445816

Please sign in to comment.