diff --git a/docs/InstructionMatcher.md b/docs/InstructionMatcher.md index de96243..c17bb15 100644 --- a/docs/InstructionMatcher.md +++ b/docs/InstructionMatcher.md @@ -174,6 +174,16 @@ Instructions Matchers are functions that build a instruction matcher to match ag | m_inst_combine_xor | (lhs: InstMatcher, rhs: InstMatcher) | InstMatcher | Build Inst Matcher two matchers that return true if (lhs xor rhs )= true | | m_inst_combine_not | (rhs: InstMatcher) | InstMatcher | Build Inst Matcher two matchers that return true if (!rhs) = true | +--- + +### Call Instructions Matchers functions + +| Function | Parameters | Return | Description | +| :------: | :--------: | :---------: | :--------------------------------------------: | +| m_call | () | InstMatcher | Build Inst Matcher that match call instruction | + +--- + ### Combine Instructions Matchers Operators | Operator | Description | diff --git a/src/functions/matchers/call.rs b/src/functions/matchers/call.rs new file mode 100644 index 0000000..fa9c4f3 --- /dev/null +++ b/src/functions/matchers/call.rs @@ -0,0 +1,24 @@ +use std::collections::HashMap; + +use gitql_core::signature::Signature; +use gitql_core::signature::StandardFunction; +use gitql_core::values::base::Value; + +use crate::ir::types::InstMatcherType; +use crate::ir::values::InstMatcherValue; +use crate::matchers::call::CallInstMatcher; + +#[inline(always)] +pub fn register_call_inst_matchers_functions(map: &mut HashMap<&'static str, StandardFunction>) { + map.insert("m_call", match_call_inst); +} + +#[inline(always)] +pub fn register_call_inst_matchers_function_signatures(map: &mut HashMap<&'static str, Signature>) { + map.insert("m_call", Signature::with_return(Box::new(InstMatcherType))); +} + +fn match_call_inst(_values: &[Box]) -> Box { + let matcher = Box::new(CallInstMatcher::create_call()); + Box::new(InstMatcherValue { matcher }) +} diff --git a/src/functions/matchers/mod.rs b/src/functions/matchers/mod.rs index a78030e..936b171 100644 --- a/src/functions/matchers/mod.rs +++ b/src/functions/matchers/mod.rs @@ -1,5 +1,6 @@ pub mod arithmetic; pub mod binary; +pub mod call; pub mod combine; pub mod constants; pub mod exception; diff --git a/src/functions/mod.rs b/src/functions/mod.rs index 589d30c..8353bb6 100644 --- a/src/functions/mod.rs +++ b/src/functions/mod.rs @@ -12,6 +12,8 @@ use matchers::arithmetic::register_arithmetic_matchers_function_signatures; use matchers::arithmetic::register_arithmetic_matchers_functions; use matchers::binary::register_binary_inst_matchers_function_signatures; use matchers::binary::register_binary_inst_matchers_functions; +use matchers::call::register_call_inst_matchers_function_signatures; +use matchers::call::register_call_inst_matchers_functions; use matchers::combine::register_combine_matchers_function; use matchers::combine::register_combine_matchers_function_signatures; use matchers::constants::register_constants_matchers_function_signatures; @@ -53,6 +55,7 @@ pub fn llvm_ir_functions() -> &'static HashMap<&'static str, StandardFunction> { register_binary_inst_matchers_functions(&mut map); register_exception_inst_matchers_functions(&mut map); register_combine_matchers_function(&mut map); + register_call_inst_matchers_functions(&mut map); map }) } @@ -70,6 +73,7 @@ pub fn llvm_ir_function_signatures() -> HashMap<&'static str, Signature> { register_binary_inst_matchers_function_signatures(&mut map); register_exception_inst_matchers_function_signatures(&mut map); register_combine_matchers_function_signatures(&mut map); + register_call_inst_matchers_function_signatures(&mut map); map } diff --git a/src/matchers/call.rs b/src/matchers/call.rs new file mode 100644 index 0000000..6cbf239 --- /dev/null +++ b/src/matchers/call.rs @@ -0,0 +1,21 @@ +use inkwell::llvm_sys::core::LLVMGetInstructionOpcode; +use inkwell::llvm_sys::prelude::LLVMValueRef; +use inkwell::llvm_sys::LLVMOpcode; + +use super::InstMatcher; + +#[derive(Clone)] +pub struct CallInstMatcher; + +impl CallInstMatcher { + pub fn create_call() -> Self { + CallInstMatcher + } +} + +impl InstMatcher for CallInstMatcher { + #[allow(clippy::not_unsafe_ptr_arg_deref)] + fn is_match(&self, instruction: LLVMValueRef) -> bool { + unsafe { LLVMGetInstructionOpcode(instruction) == LLVMOpcode::LLVMCall } + } +} diff --git a/src/matchers/mod.rs b/src/matchers/mod.rs index 91fd06b..525ad39 100644 --- a/src/matchers/mod.rs +++ b/src/matchers/mod.rs @@ -39,6 +39,7 @@ impl InstMatcher for AnyInstMatcher { pub mod arithmetic; pub mod binary; +pub mod call; pub mod combine; pub mod constants; pub mod exception;