Skip to content

Commit

Permalink
feat: Implement m_call inst matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Jan 10, 2025
1 parent c2976a6 commit 82ee1df
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/InstructionMatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
24 changes: 24 additions & 0 deletions src/functions/matchers/call.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Value>]) -> Box<dyn Value> {
let matcher = Box::new(CallInstMatcher::create_call());
Box::new(InstMatcherValue { matcher })
}
1 change: 1 addition & 0 deletions src/functions/matchers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod arithmetic;
pub mod binary;
pub mod call;
pub mod combine;
pub mod constants;
pub mod exception;
Expand Down
4 changes: 4 additions & 0 deletions src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
})
}
Expand All @@ -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
}

Expand Down
21 changes: 21 additions & 0 deletions src/matchers/call.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
}
1 change: 1 addition & 0 deletions src/matchers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 82ee1df

Please sign in to comment.