Skip to content

Commit

Permalink
Removed remainder of implicit global context functions and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDan64 committed Nov 16, 2019
1 parent 59cba91 commit cf61e2b
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 253 deletions.
34 changes: 2 additions & 32 deletions src/basic_block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A `BasicBlock` is a container of instructions.
use llvm_sys::core::{LLVMGetBasicBlockParent, LLVMGetBasicBlockTerminator, LLVMGetNextBasicBlock, LLVMInsertBasicBlock, LLVMIsABasicBlock, LLVMIsConstant, LLVMMoveBasicBlockAfter, LLVMMoveBasicBlockBefore, LLVMPrintTypeToString, LLVMPrintValueToString, LLVMTypeOf, LLVMDeleteBasicBlock, LLVMGetPreviousBasicBlock, LLVMRemoveBasicBlockFromParent, LLVMGetFirstInstruction, LLVMGetLastInstruction, LLVMGetTypeContext, LLVMBasicBlockAsValue};
use llvm_sys::core::{LLVMGetBasicBlockParent, LLVMGetBasicBlockTerminator, LLVMGetNextBasicBlock, LLVMIsABasicBlock, LLVMIsConstant, LLVMMoveBasicBlockAfter, LLVMMoveBasicBlockBefore, LLVMPrintTypeToString, LLVMPrintValueToString, LLVMTypeOf, LLVMDeleteBasicBlock, LLVMGetPreviousBasicBlock, LLVMRemoveBasicBlockFromParent, LLVMGetFirstInstruction, LLVMGetLastInstruction, LLVMGetTypeContext, LLVMBasicBlockAsValue};
#[llvm_versions(3.9..=latest)]
use llvm_sys::core::LLVMGetBasicBlockName;
use llvm_sys::prelude::{LLVMValueRef, LLVMBasicBlockRef};
Expand All @@ -9,7 +9,7 @@ use crate::context::ContextRef;
use crate::values::{FunctionValue, InstructionValue};

use std::fmt;
use std::ffi::{CStr, CString};
use std::ffi::CStr;

/// A `BasicBlock` is a container of instructions.
///
Expand Down Expand Up @@ -218,36 +218,6 @@ impl BasicBlock {
Ok(())
}

/// Prepends a new `BasicBlock` before this one.
///
/// # Example
/// ```no_run
/// use inkwell::context::Context;
/// use inkwell::module::Module;
/// use inkwell::builder::Builder;
///
/// let context = Context::create();
/// let module = context.create_module("my_module");
/// let void_type = context.void_type();
/// let fn_type = void_type.fn_type(&[], false);
/// let function = module.add_function("do_nothing", fn_type, None);
///
/// let basic_block1 = context.append_basic_block(function, "entry");
/// let basic_block2 = basic_block1.prepend_basic_block("previous");
///
/// assert!(basic_block1.get_next_basic_block().is_none());
/// assert_eq!(basic_block2.get_next_basic_block().unwrap(), basic_block1);
/// ```
pub fn prepend_basic_block(&self, name: &str) -> BasicBlock {
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");

let bb = unsafe {
LLVMInsertBasicBlock(self.basic_block, c_string.as_ptr())
};

BasicBlock::new(bb).expect("Prepending basic block should never fail")
}

/// Obtains the first `InstructionValue` in this `BasicBlock`, if any.
///
/// # Example
Expand Down
50 changes: 16 additions & 34 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! A `Builder` enables you to build instructions.
use either::{Either, Left, Right};
use llvm_sys::core::{LLVMBuildAdd, LLVMBuildAlloca, LLVMBuildAnd, LLVMBuildArrayAlloca, LLVMBuildArrayMalloc, LLVMBuildAtomicRMW, LLVMBuildBr, LLVMBuildCall, LLVMBuildCast, LLVMBuildCondBr, LLVMBuildExtractValue, LLVMBuildFAdd, LLVMBuildFCmp, LLVMBuildFDiv, LLVMBuildFence, LLVMBuildFMul, LLVMBuildFNeg, LLVMBuildFree, LLVMBuildFSub, LLVMBuildGEP, LLVMBuildICmp, LLVMBuildInsertValue, LLVMBuildIsNotNull, LLVMBuildIsNull, LLVMBuildLoad, LLVMBuildMalloc, LLVMBuildMul, LLVMBuildNeg, LLVMBuildNot, LLVMBuildOr, LLVMBuildPhi, LLVMBuildPointerCast, LLVMBuildRet, LLVMBuildRetVoid, LLVMBuildStore, LLVMBuildSub, LLVMBuildUDiv, LLVMBuildUnreachable, LLVMBuildXor, LLVMDisposeBuilder, LLVMGetElementType, LLVMGetInsertBlock, LLVMGetReturnType, LLVMGetTypeKind, LLVMInsertIntoBuilder, LLVMPositionBuilderAtEnd, LLVMTypeOf, LLVMBuildExtractElement, LLVMBuildInsertElement, LLVMBuildIntToPtr, LLVMBuildPtrToInt, LLVMInsertIntoBuilderWithName, LLVMClearInsertionPosition, LLVMCreateBuilder, LLVMPositionBuilder, LLVMPositionBuilderBefore, LLVMBuildAggregateRet, LLVMBuildStructGEP, LLVMBuildInBoundsGEP, LLVMBuildPtrDiff, LLVMBuildNSWAdd, LLVMBuildNUWAdd, LLVMBuildNSWSub, LLVMBuildNUWSub, LLVMBuildNSWMul, LLVMBuildNUWMul, LLVMBuildSDiv, LLVMBuildSRem, LLVMBuildURem, LLVMBuildFRem, LLVMBuildNSWNeg, LLVMBuildNUWNeg, LLVMBuildFPToUI, LLVMBuildFPToSI, LLVMBuildSIToFP, LLVMBuildUIToFP, LLVMBuildFPTrunc, LLVMBuildFPExt, LLVMBuildIntCast, LLVMBuildFPCast, LLVMBuildSExtOrBitCast, LLVMBuildZExtOrBitCast, LLVMBuildTruncOrBitCast, LLVMBuildSwitch, LLVMAddCase, LLVMBuildShl, LLVMBuildAShr, LLVMBuildLShr, LLVMBuildGlobalString, LLVMBuildGlobalStringPtr, LLVMBuildExactSDiv, LLVMBuildTrunc, LLVMBuildSExt, LLVMBuildZExt, LLVMBuildSelect, LLVMBuildAddrSpaceCast, LLVMBuildBitCast, LLVMBuildShuffleVector, LLVMBuildVAArg, LLVMBuildIndirectBr, LLVMAddDestination};
use llvm_sys::core::{LLVMBuildAdd, LLVMBuildAlloca, LLVMBuildAnd, LLVMBuildArrayAlloca, LLVMBuildArrayMalloc, LLVMBuildAtomicRMW, LLVMBuildBr, LLVMBuildCall, LLVMBuildCast, LLVMBuildCondBr, LLVMBuildExtractValue, LLVMBuildFAdd, LLVMBuildFCmp, LLVMBuildFDiv, LLVMBuildFence, LLVMBuildFMul, LLVMBuildFNeg, LLVMBuildFree, LLVMBuildFSub, LLVMBuildGEP, LLVMBuildICmp, LLVMBuildInsertValue, LLVMBuildIsNotNull, LLVMBuildIsNull, LLVMBuildLoad, LLVMBuildMalloc, LLVMBuildMul, LLVMBuildNeg, LLVMBuildNot, LLVMBuildOr, LLVMBuildPhi, LLVMBuildPointerCast, LLVMBuildRet, LLVMBuildRetVoid, LLVMBuildStore, LLVMBuildSub, LLVMBuildUDiv, LLVMBuildUnreachable, LLVMBuildXor, LLVMDisposeBuilder, LLVMGetElementType, LLVMGetInsertBlock, LLVMGetReturnType, LLVMGetTypeKind, LLVMInsertIntoBuilder, LLVMPositionBuilderAtEnd, LLVMTypeOf, LLVMBuildExtractElement, LLVMBuildInsertElement, LLVMBuildIntToPtr, LLVMBuildPtrToInt, LLVMInsertIntoBuilderWithName, LLVMClearInsertionPosition, LLVMPositionBuilder, LLVMPositionBuilderBefore, LLVMBuildAggregateRet, LLVMBuildStructGEP, LLVMBuildInBoundsGEP, LLVMBuildPtrDiff, LLVMBuildNSWAdd, LLVMBuildNUWAdd, LLVMBuildNSWSub, LLVMBuildNUWSub, LLVMBuildNSWMul, LLVMBuildNUWMul, LLVMBuildSDiv, LLVMBuildSRem, LLVMBuildURem, LLVMBuildFRem, LLVMBuildNSWNeg, LLVMBuildNUWNeg, LLVMBuildFPToUI, LLVMBuildFPToSI, LLVMBuildSIToFP, LLVMBuildUIToFP, LLVMBuildFPTrunc, LLVMBuildFPExt, LLVMBuildIntCast, LLVMBuildFPCast, LLVMBuildSExtOrBitCast, LLVMBuildZExtOrBitCast, LLVMBuildTruncOrBitCast, LLVMBuildSwitch, LLVMAddCase, LLVMBuildShl, LLVMBuildAShr, LLVMBuildLShr, LLVMBuildGlobalString, LLVMBuildGlobalStringPtr, LLVMBuildExactSDiv, LLVMBuildTrunc, LLVMBuildSExt, LLVMBuildZExt, LLVMBuildSelect, LLVMBuildAddrSpaceCast, LLVMBuildBitCast, LLVMBuildShuffleVector, LLVMBuildVAArg, LLVMBuildIndirectBr, LLVMAddDestination};
#[llvm_versions(3.9..=latest)]
use llvm_sys::core::LLVMBuildAtomicCmpXchg;
use llvm_sys::prelude::{LLVMBuilderRef, LLVMValueRef};
use llvm_sys::{LLVMTypeKind};

use crate::{AtomicOrdering, AtomicRMWBinOp, IntPredicate, FloatPredicate};
use crate::basic_block::BasicBlock;
use crate::context::Context;
use crate::values::{AggregateValue, AggregateValueEnum, AsValueRef, BasicValue, BasicValueEnum, PhiValue, FunctionValue, IntValue, PointerValue, VectorValue, InstructionValue, GlobalValue, IntMathValue, FloatMathValue, PointerMathValue, InstructionOpcode, CallSiteValue};
#[llvm_versions(3.9..=latest)]
use crate::values::StructValue;
Expand All @@ -34,23 +33,6 @@ impl<'ctx> Builder<'ctx> {
}
}

/// Creates a `Builder` belonging to the global `Context`.
///
/// # Example
///
/// ```no_run
/// use inkwell::builder::Builder;
///
/// let builder = Builder::create();
/// ```
pub fn create() -> Self {
let builder = unsafe {
Context::get_global(|_ctx| LLVMCreateBuilder())
};

Builder::new(builder)
}

// REVIEW: Would probably make this API a bit simpler by taking Into<Option<&BasicValue>>
// So that you could just do build_return(&value) or build_return(None). Is that frowned upon?
/// Builds a function return instruction. It should be provided with `None` if the return type
Expand All @@ -69,7 +51,7 @@ impl<'ctx> Builder<'ctx> {
/// let arg_types = [i32_type.into()];
/// let fn_type = i32_type.fn_type(&arg_types, false);
/// let fn_value = module.add_function("ret", fn_type, None);
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
/// let i32_arg = fn_value.get_first_param().unwrap();
///
/// builder.position_at_end(&entry);
Expand Down Expand Up @@ -101,7 +83,7 @@ impl<'ctx> Builder<'ctx> {
/// let struct_type = context.struct_type(&[i32_type.into(), i32_type.into()], false);
/// let fn_type = struct_type.fn_type(&[], false);
/// let fn_value = module.add_function("ret", fn_type, None);
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
///
/// builder.position_at_end(&entry);
/// builder.build_aggregate_return(&[i32_three.into(), i32_seven.into()]);
Expand Down Expand Up @@ -133,7 +115,7 @@ impl<'ctx> Builder<'ctx> {
/// let i32_type = context.i32_type();
/// let fn_type = i32_type.fn_type(&[i32_type.into()], false);
/// let fn_value = module.add_function("ret", fn_type, None);
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
/// let i32_arg = fn_value.get_first_param().unwrap();
///
/// builder.position_at_end(&entry);
Expand Down Expand Up @@ -242,7 +224,7 @@ impl<'ctx> Builder<'ctx> {
/// let i32_ptr_type = i32_type.ptr_type(AddressSpace::Generic);
/// let fn_type = void_type.fn_type(&[i32_ptr_type.into(), i32_ptr_type.into()], false);
/// let fn_value = module.add_function("ret", fn_type, None);
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
/// let i32_ptr_param1 = fn_value.get_first_param().unwrap().into_pointer_value();
/// let i32_ptr_param2 = fn_value.get_nth_param(1).unwrap().into_pointer_value();
///
Expand Down Expand Up @@ -293,7 +275,7 @@ impl<'ctx> Builder<'ctx> {
/// let i32_seven = i32_type.const_int(7, false);
/// let fn_type = void_type.fn_type(&[i32_ptr_type.into()], false);
/// let fn_value = module.add_function("ret", fn_type, None);
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
/// let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value();
///
/// builder.position_at_end(&entry);
Expand Down Expand Up @@ -324,7 +306,7 @@ impl<'ctx> Builder<'ctx> {
/// let i32_ptr_type = i32_type.ptr_type(AddressSpace::Generic);
/// let fn_type = i32_type.fn_type(&[i32_ptr_type.into()], false);
/// let fn_value = module.add_function("ret", fn_type, None);
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
/// let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value();
///
/// builder.position_at_end(&entry);
Expand Down Expand Up @@ -524,7 +506,7 @@ impl<'ctx> Builder<'ctx> {
/// let fn_type = void_type.fn_type(&arg_types, false);
/// let fn_value = module.add_function("bc", fn_type, None);
/// let builder = context.create_builder();
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
/// let i32_arg = fn_value.get_first_param().unwrap();
///
/// builder.position_at_end(&entry);
Expand Down Expand Up @@ -835,7 +817,7 @@ impl<'ctx> Builder<'ctx> {
/// let function = module.add_function("left_shift", fn_type, None);
/// let value = function.get_first_param().unwrap().into_int_value();
/// let n = function.get_nth_param(1).unwrap().into_int_value();
/// let entry_block = function.append_basic_block("entry");
/// let entry_block = context.append_basic_block(function, "entry");
///
/// builder.position_at_end(&entry_block);
///
Expand Down Expand Up @@ -907,7 +889,7 @@ impl<'ctx> Builder<'ctx> {
/// let function = module.add_function("right_shift", fn_type, None);
/// let value = function.get_first_param().unwrap().into_int_value();
/// let n = function.get_nth_param(1).unwrap().into_int_value();
/// let entry_block = function.append_basic_block("entry");
/// let entry_block = context.append_basic_block(function, "entry");
///
/// builder.position_at_end(&entry_block);
///
Expand Down Expand Up @@ -1214,7 +1196,7 @@ impl<'ctx> Builder<'ctx> {
/// let fn_type = void_type.fn_type(&[], false);
/// let fn_value = module.add_function("av_fn", fn_type, None);
/// let builder = context.create_builder();
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
///
/// builder.position_at_end(&entry);
///
Expand Down Expand Up @@ -1276,7 +1258,7 @@ impl<'ctx> Builder<'ctx> {
/// let fn_type = void_type.fn_type(&[], false);
/// let fn_value = module.add_function("av_fn", fn_type, None);
/// let builder = context.create_builder();
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
///
/// builder.position_at_end(&entry);
///
Expand Down Expand Up @@ -1329,7 +1311,7 @@ impl<'ctx> Builder<'ctx> {
/// let fn_type = i32_type.fn_type(&[vec_type.into()], false);
/// let fn_value = module.add_function("vec_fn", fn_type, None);
/// let builder = context.create_builder();
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
/// let vector_param = fn_value.get_first_param().unwrap().into_vector_value();
///
/// builder.position_at_end(&entry);
Expand Down Expand Up @@ -1366,7 +1348,7 @@ impl<'ctx> Builder<'ctx> {
/// let fn_type = void_type.fn_type(&[vec_type.into()], false);
/// let fn_value = module.add_function("vec_fn", fn_type, None);
/// let builder = context.create_builder();
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
/// let vector_param = fn_value.get_first_param().unwrap().into_vector_value();
///
/// builder.position_at_end(&entry);
Expand Down Expand Up @@ -1550,7 +1532,7 @@ impl<'ctx> Builder<'ctx> {
/// let i32_ptr_type = i32_type.ptr_type(AddressSpace::Generic);
/// let fn_type = void_type.fn_type(&[i32_ptr_type.into()], false);
/// let fn_value = module.add_function("rmw", fn_type, None);
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
/// let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value();
/// let builder = context.create_builder();
/// builder.position_at_end(&entry);
Expand Down Expand Up @@ -1594,7 +1576,7 @@ impl<'ctx> Builder<'ctx> {
/// let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value();
/// let i32_seven = i32_type.const_int(7, false);
/// let i32_eight = i32_type.const_int(8, false);
/// let entry = fn_value.append_basic_block("entry");
/// let entry = context.append_basic_block(fn_value, "entry");
/// let builder = context.create_builder();
/// builder.position_at_end(&entry);
/// builder.build_cmpxchg(i32_ptr_param, i32_seven, i32_eight, AtomicOrdering::AcquireRelease, AtomicOrdering::Monotonic);
Expand Down
2 changes: 1 addition & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl Context {
/// let void_type = context.void_type();
/// let fn_type = void_type.fn_type(&[], false);
/// let fn_val = module.add_function("my_fn", fn_type, None);
/// let basic_block = fn_val.append_basic_block("entry");
/// let basic_block = context.append_basic_block(fn_val, "entry");
///
/// builder.position_at_end(&basic_block);
/// builder.build_return(None);
Expand Down
2 changes: 1 addition & 1 deletion src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ impl<'ctx> Module<'ctx> {
/// let void_type = context.void_type();
/// let fn_type = void_type.fn_type(&[], false);
/// let f = module.add_function("f", fn_type, None);
/// let basic_block = f.append_basic_block("entry");
/// let basic_block = context.append_basic_block(f, "entry");
/// let builder = context.create_builder();
///
/// builder.position_at_end(&basic_block);
Expand Down
26 changes: 1 addition & 25 deletions src/types/struct_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use llvm_sys::core::{LLVMConstNamedStruct, LLVMConstStruct, LLVMCountStructElementTypes, LLVMGetStructElementTypes, LLVMGetStructName, LLVMIsPackedStruct, LLVMIsOpaqueStruct, LLVMStructSetBody, LLVMConstArray};
use llvm_sys::core::{LLVMConstNamedStruct, LLVMCountStructElementTypes, LLVMGetStructElementTypes, LLVMGetStructName, LLVMIsPackedStruct, LLVMIsOpaqueStruct, LLVMStructSetBody, LLVMConstArray};
#[llvm_versions(3.7..=latest)]
use llvm_sys::core::LLVMStructGetTypeAtIndex;
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
Expand Down Expand Up @@ -87,30 +87,6 @@ impl<'ctx> StructType<'ctx> {
StructValue::new(value)
}

/// Creates a `StructValue` based on the input values rather than an existing `StructType`.
/// It will be assigned the global `Context`.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// let context = Context::create();
/// let f32_type = context.f32_type();
/// let f32_zero = f32_type.const_float(0.);
/// let struct_val = context.const_struct(&[f32_zero.into()], false);
/// ```
pub fn const_struct(values: &[BasicValueEnum], packed: bool) -> StructValue<'ctx> {
let mut args: Vec<LLVMValueRef> = values.iter()
.map(|val| val.as_value_ref())
.collect();
let value = unsafe {
LLVMConstStruct(args.as_mut_ptr(), args.len() as u32, packed as i32)
};

StructValue::new(value)
}

/// Creates a constant zero value of this `StructType`.
///
/// # Example
Expand Down
Loading

0 comments on commit cf61e2b

Please sign in to comment.