Skip to content

Commit f828918

Browse files
Add BasicBlock::get_address
1 parent 5c61cc7 commit f828918

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/basic_block.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! A `BasicBlock` is a container of instructions.
22
3-
use llvm_sys::core::{LLVMGetBasicBlockParent, LLVMGetBasicBlockTerminator, LLVMGetNextBasicBlock, LLVMIsABasicBlock, LLVMIsConstant, LLVMMoveBasicBlockAfter, LLVMMoveBasicBlockBefore, LLVMPrintTypeToString, LLVMPrintValueToString, LLVMTypeOf, LLVMDeleteBasicBlock, LLVMGetPreviousBasicBlock, LLVMRemoveBasicBlockFromParent, LLVMGetFirstInstruction, LLVMGetLastInstruction, LLVMGetTypeContext, LLVMBasicBlockAsValue, LLVMReplaceAllUsesWith, LLVMGetFirstUse};
3+
use llvm_sys::core::{LLVMGetBasicBlockParent, LLVMGetBasicBlockTerminator, LLVMGetNextBasicBlock, LLVMIsABasicBlock, LLVMIsConstant, LLVMMoveBasicBlockAfter, LLVMMoveBasicBlockBefore, LLVMPrintTypeToString, LLVMPrintValueToString, LLVMTypeOf, LLVMDeleteBasicBlock, LLVMGetPreviousBasicBlock, LLVMRemoveBasicBlockFromParent, LLVMGetFirstInstruction, LLVMGetLastInstruction, LLVMGetTypeContext, LLVMBasicBlockAsValue, LLVMReplaceAllUsesWith, LLVMGetFirstUse, LLVMBlockAddress};
44
#[llvm_versions(3.9..=latest)]
55
use llvm_sys::core::LLVMGetBasicBlockName;
66
use llvm_sys::prelude::{LLVMValueRef, LLVMBasicBlockRef};
77

88
use crate::context::ContextRef;
9-
use crate::values::{BasicValueUse, FunctionValue, InstructionValue};
9+
use crate::values::{AsValueRef, BasicValueUse, FunctionValue, InstructionValue, PointerValue};
1010

1111
use std::fmt;
1212
use std::ffi::CStr;
@@ -514,6 +514,35 @@ impl<'ctx> BasicBlock<'ctx> {
514514
Some(BasicValueUse::new(use_))
515515
}
516516
}
517+
518+
/// Gets the address of this `BasicBlock` if possible.
519+
///
520+
/// # Example
521+
///
522+
/// ```no_run
523+
/// use inkwell::context::Context;
524+
/// let context = Context::create();
525+
/// let module = context.create_module("my_mod");
526+
/// let void_type = context.void_type();
527+
/// let fn_type = void_type.fn_type(&[], false);
528+
/// let fn_val = module.add_function("my_fn", fn_type, None);
529+
/// let bb = context.append_basic_block(fn_val, "entry");
530+
///
531+
/// assert!(bb.get_address().is_some());
532+
/// ```
533+
pub fn get_address(self) -> Option<PointerValue<'ctx>> {
534+
let parent = self.get_parent()?;
535+
536+
let value = unsafe {
537+
PointerValue::new(LLVMBlockAddress(parent.as_value_ref(), self.basic_block))
538+
};
539+
540+
if value.is_null() {
541+
return None;
542+
}
543+
544+
Some(value)
545+
}
517546
}
518547

519548
impl fmt::Debug for BasicBlock<'_> {

0 commit comments

Comments
 (0)