Skip to content

Commit 32e99c5

Browse files
committed
Implement m_global_var & m_global_val matchers
1 parent b114c7d commit 32e99c5

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

src/functions/matchers/globals.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::collections::HashMap;
2+
3+
use gitql_core::signature::Signature;
4+
use gitql_core::signature::StandardFunction;
5+
use gitql_core::values::Value;
6+
7+
use crate::functions::matcher_signature_without_parameters;
8+
use crate::ir::values::InstMatcherValue;
9+
use crate::matchers::globals::GlobalValueExprMatcher;
10+
use crate::matchers::globals::GlobalVariableExprMatcher;
11+
12+
#[inline(always)]
13+
pub fn register_globals_matchers_functions(map: &mut HashMap<&'static str, StandardFunction>) {
14+
map.insert("m_global_var", match_global_variable_expr_inst);
15+
map.insert("m_global_val", match_global_value_expr_inst);
16+
}
17+
18+
#[inline(always)]
19+
pub fn register_globals_matchers_function_signatures(map: &mut HashMap<&'static str, Signature>) {
20+
map.insert("m_global_var", matcher_signature_without_parameters());
21+
map.insert("m_global_val", matcher_signature_without_parameters());
22+
}
23+
24+
fn match_global_variable_expr_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
25+
let matcher = Box::new(GlobalVariableExprMatcher);
26+
Box::new(InstMatcherValue { matcher })
27+
}
28+
29+
fn match_global_value_expr_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
30+
let matcher = Box::new(GlobalValueExprMatcher);
31+
Box::new(InstMatcherValue { matcher })
32+
}

src/functions/matchers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod debug;
88
pub mod exception;
99
pub mod fcmp;
1010
pub mod get_element_ptr;
11+
pub mod globals;
1112
pub mod icmp;
1213
pub mod operand_bundle;
1314
pub mod other;

src/functions/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ use matchers::usage::register_usage_matchers_functions;
4242

4343
use crate::functions::matchers::get_element_ptr::register_get_element_ptr_inst_matchers_function_signatures;
4444
use crate::functions::matchers::get_element_ptr::register_get_element_ptr_inst_matchers_functions;
45+
use crate::functions::matchers::globals::register_globals_matchers_function_signatures;
46+
use crate::functions::matchers::globals::register_globals_matchers_functions;
4547
use crate::ir::types::InstMatcherType;
4648
use crate::ir::values::InstMatcherValue;
4749
use crate::matchers::other::AnyInstMatcher;
@@ -70,6 +72,7 @@ pub fn llvm_ir_functions() -> &'static HashMap<&'static str, StandardFunction> {
7072
register_debug_inst_matchers_functions(&mut map);
7173
register_operand_bundle_inst_matchers_functions(&mut map);
7274
register_get_element_ptr_inst_matchers_functions(&mut map);
75+
register_globals_matchers_functions(&mut map);
7376
map
7477
})
7578
}
@@ -93,7 +96,7 @@ pub fn llvm_ir_function_signatures() -> HashMap<&'static str, Signature> {
9396
register_debug_inst_matchers_function_signatures(&mut map);
9497
register_operand_bundle_inst_matchers_function_signatures(&mut map);
9598
register_get_element_ptr_inst_matchers_function_signatures(&mut map);
96-
99+
register_globals_matchers_function_signatures(&mut map);
97100
map
98101
}
99102

src/matchers/globals.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use inkwell::llvm_sys::core::LLVMIsAGlobalValue;
2+
use inkwell::llvm_sys::core::LLVMIsAGlobalVariable;
3+
use inkwell::llvm_sys::prelude::LLVMValueRef;
4+
5+
use super::Matcher;
6+
7+
#[derive(Clone)]
8+
pub struct GlobalValueExprMatcher;
9+
10+
impl Matcher<LLVMValueRef> for GlobalValueExprMatcher {
11+
fn is_match(&self, instruction: &LLVMValueRef) -> bool {
12+
unsafe { !LLVMIsAGlobalValue(*instruction).is_null() }
13+
}
14+
}
15+
16+
#[derive(Clone)]
17+
pub struct GlobalVariableExprMatcher;
18+
19+
impl Matcher<LLVMValueRef> for GlobalVariableExprMatcher {
20+
fn is_match(&self, instruction: &LLVMValueRef) -> bool {
21+
unsafe { !LLVMIsAGlobalVariable(*instruction).is_null() }
22+
}
23+
}

src/matchers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod debug;
99
pub mod exception;
1010
pub mod fcmp;
1111
pub mod get_element_ptr;
12+
pub mod globals;
1213
pub mod icmp;
1314
pub mod operand_bundle;
1415
pub mod other;

0 commit comments

Comments
 (0)