Skip to content

Commit

Permalink
Implement m_addr_space_cast inst matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Feb 4, 2025
1 parent 4303e1e commit 1798aa3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
11 changes: 6 additions & 5 deletions docs/matchers/cast.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## Cast Instructions Matchers

| Function | Parameters | Return | Description |
| :----------: | :--------: | :---------: | :---------------------------------------------------: |
| m_int_to_ptr | | InstMatcher | Build Inst Matcher that matchers IntToPtr instruction |
| m_ptr_to_int | | InstMatcher | Build Inst Matcher that matchers PtrToInt instruction |
| m_bit_cast | | InstMatcher | Build Inst Matcher that matchers Bit cast instruction |
| Function | Parameters | Return | Description |
| :---------------: | :--------: | :---------: | :--------------------------------------------------------: |
| m_int_to_ptr | | InstMatcher | Build Inst Matcher that matchers IntToPtr instruction |
| m_ptr_to_int | | InstMatcher | Build Inst Matcher that matchers PtrToInt instruction |
| m_bit_cast | | InstMatcher | Build Inst Matcher that matchers Bit cast instruction |
| m_addr_space_cast | | InstMatcher | Build Inst Matcher that matchers AddrSpaceCast instruction |
26 changes: 14 additions & 12 deletions src/functions/matchers/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,29 @@ pub fn register_cast_matchers_function(map: &mut HashMap<&'static str, StandardF
map.insert("m_int_to_ptr", match_int_to_ptr_inst);
map.insert("m_ptr_to_int", match_ptr_to_int_inst);
map.insert("m_bit_cast", match_bit_cast_inst);
map.insert("m_addr_space_cast", match_addr_space_cast_inst);
}

#[inline(always)]
pub fn register_cast_matchers_function_signatures(map: &mut HashMap<&'static str, Signature>) {
map.insert(
"m_int_to_ptr",
Signature {
parameters: vec![],
return_type: Box::new(InstMatcherType),
},
Signature::with_return(Box::new(InstMatcherType)),
);

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

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

map.insert(
"m_addr_space_cast",
Signature::with_return(Box::new(InstMatcherType)),
);
}

Expand All @@ -56,3 +53,8 @@ fn match_bit_cast_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
let matcher = Box::new(CastInstMatcher::create_bit_cast());
Box::new(InstMatcherValue { matcher })
}

fn match_addr_space_cast_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
let matcher = Box::new(CastInstMatcher::create_addr_space_cast());
Box::new(InstMatcherValue { matcher })
}
18 changes: 5 additions & 13 deletions src/ir/data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) static mut LLVM_CONTEXT: LazyLock<Context> = LazyLock::new(Context::c
pub(crate) static mut LLVM_MODULES: Vec<Module> = Vec::new();

pub struct LLVMIRDataProvider {
pub paths: Vec<String>,
paths: Vec<String>,
}

impl LLVMIRDataProvider {
Expand Down Expand Up @@ -68,30 +68,22 @@ fn select_llvm_ir_instructions(
let mut values: Vec<Box<dyn Value>> = Vec::with_capacity(row_width);
for field_name in selected_columns {
if field_name == "function_name" {
values.push(Box::new(TextValue {
value: function_name.clone(),
}));
values.push(Box::new(TextValue::new(function_name.clone())));
continue;
}

if field_name == "basic_block_name" {
values.push(Box::new(TextValue {
value: basic_block_name.clone(),
}));
values.push(Box::new(TextValue::new(basic_block_name.clone())));
continue;
}

if field_name == "instruction" {
values.push(Box::new(LLVMInstValue {
llvm_value: inst.as_value_ref(),
}));
values.push(Box::new(LLVMInstValue::new(inst.as_value_ref())));
continue;
}

if field_name == "file_name" {
values.push(Box::new(TextValue {
value: path.to_string(),
}));
values.push(Box::new(TextValue::new(path.to_string())));
continue;
}

Expand Down
6 changes: 6 additions & 0 deletions src/ir/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ pub struct LLVMInstValue {
pub llvm_value: LLVMValueRef,
}

impl LLVMInstValue {
pub fn new(llvm_value: LLVMValueRef) -> Self {
LLVMInstValue { llvm_value }
}
}

impl Value for LLVMInstValue {
fn literal(&self) -> String {
unsafe {
Expand Down
14 changes: 11 additions & 3 deletions src/matchers/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ use super::InstMatcher;
enum CastMatcherKind {
IntToPtr,
PtrToInt,
Bit,
BitCast,
AddrSpaceCast,
}

impl CastMatcherKind {
pub fn llvm_opcode(&self) -> LLVMOpcode {
match self {
CastMatcherKind::IntToPtr => LLVMOpcode::LLVMIntToPtr,
CastMatcherKind::PtrToInt => LLVMOpcode::LLVMPtrToInt,
CastMatcherKind::Bit => LLVMOpcode::LLVMBitCast,
CastMatcherKind::BitCast => LLVMOpcode::LLVMBitCast,
CastMatcherKind::AddrSpaceCast => LLVMOpcode::LLVMAddrSpaceCast,
}
}
}
Expand All @@ -41,7 +43,13 @@ impl CastInstMatcher {

pub fn create_bit_cast() -> CastInstMatcher {
CastInstMatcher {
matcher_kind: CastMatcherKind::Bit,
matcher_kind: CastMatcherKind::BitCast,
}
}

pub fn create_addr_space_cast() -> CastInstMatcher {
CastInstMatcher {
matcher_kind: CastMatcherKind::AddrSpaceCast,
}
}
}
Expand Down

0 comments on commit 1798aa3

Please sign in to comment.