Skip to content

Commit 342d3e0

Browse files
committed
Add basic block iterator methods and use moderner version of not(ndebug)
1 parent c9a49f6 commit 342d3e0

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/block.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,20 @@ pub struct BlockIter<'a> {
6969
pub min: &'a BasicBlock,
7070
pub max: &'a BasicBlock
7171
}
72+
impl<'a> BlockIter<'a> {
73+
pub fn new(function: &'a Function) -> BlockIter<'a> {
74+
BlockIter {
75+
min: unsafe { core::LLVMGetFirstBasicBlock(function.into()).into() },
76+
max: unsafe { core::LLVMGetLastBasicBlock(function.into()).into() }
77+
}
78+
}
79+
}
7280

7381
impl<'a> IntoIterator for &'a Function {
7482
type IntoIter = BlockIter<'a>;
7583
type Item = &'a BasicBlock;
7684
fn into_iter(self) -> BlockIter<'a> {
77-
BlockIter {
78-
min: unsafe { core::LLVMGetFirstBasicBlock(self.into()).into() },
79-
max: unsafe { core::LLVMGetLastBasicBlock(self.into()).into() }
80-
}
85+
BlockIter::new(self)
8186
}
8287
}
8388
impl<'a> Iterator for BlockIter<'a> {

src/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a> JitEngine {
102102
/// This will check that the types match at runtime when in debug mode, but not release mode.
103103
/// You should make sure to use debug mode if you want it to error when the types don't match.
104104
pub fn with_function<C, A, R>(&self, function: &'a Function, cb: C) where A:Compile<'a>, R:Compile<'a>, C:FnOnce(extern "C" fn (A) -> R) {
105-
if cfg!(not(ndebug)) {
105+
if cfg!(debug_assertions) {
106106
let ctx = function.get_context();
107107
let sig = function.get_signature();
108108
assert_eq!(Type::get::<R>(ctx), sig.get_return());

src/value.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::ffi::CString;
66
use std::{fmt, mem};
77
use std::ops::{Deref, Index};
88
use std::marker::PhantomData;
9-
use block::BasicBlock;
9+
use block::{BasicBlock, BlockIter};
1010
use context::{Context, GetContext};
1111
use types::{FunctionType, Type};
1212
use util::{self, Sub};
@@ -230,6 +230,10 @@ impl Function {
230230
core::LLVMAppendBasicBlockInContext(self.get_context().into(), self.into(), ptr).into()
231231
})
232232
}
233+
/// Iterate through this function's basic blocks.
234+
pub fn blocks(&self) -> BlockIter {
235+
BlockIter::new(self)
236+
}
233237
/// Returns the entry block of this function or `None` if there is none.
234238
pub fn get_entry(&self) -> Option<&BasicBlock> {
235239
unsafe { mem::transmute(core::LLVMGetEntryBasicBlock(self.into())) }

0 commit comments

Comments
 (0)