Skip to content

Commit db44468

Browse files
committed
trans::foreign: Simplify some code by using our builder impl
1 parent 36d7d74 commit db44468

File tree

1 file changed

+23
-58
lines changed

1 file changed

+23
-58
lines changed

src/librustc/middle/trans/foreign.rs

+23-58
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use middle::weak_lang_items;
1717
use middle::trans::base::push_ctxt;
1818
use middle::trans::base;
1919
use middle::trans::build::*;
20-
use middle::trans::builder::noname;
2120
use middle::trans::cabi;
2221
use middle::trans::common::*;
2322
use middle::trans::machine;
@@ -625,8 +624,8 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
625624
"the block".with_c_str(
626625
|s| llvm::LLVMAppendBasicBlockInContext(ccx.llcx, llwrapfn, s));
627626

628-
let builder = ccx.builder.b;
629-
llvm::LLVMPositionBuilderAtEnd(builder, the_block);
627+
let builder = ccx.builder();
628+
builder.position_at_end(the_block);
630629

631630
// Array for the arguments we will pass to the rust function.
632631
let mut llrust_args = Vec::new();
@@ -666,23 +665,15 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
666665
debug!("out pointer, foreign={}",
667666
ccx.tn.val_to_str(llforeign_outptr));
668667
let llrust_retptr =
669-
llvm::LLVMBuildBitCast(builder,
670-
llforeign_outptr,
671-
llrust_ret_ty.ptr_to().to_ref(),
672-
noname());
668+
builder.bitcast(llforeign_outptr, llrust_ret_ty.ptr_to());
673669
debug!("out pointer, foreign={} (casted)",
674670
ccx.tn.val_to_str(llrust_retptr));
675671
llrust_args.push(llrust_retptr);
676672
return_alloca = None;
677673
}
678674

679675
None => {
680-
let slot = {
681-
"return_alloca".with_c_str(
682-
|s| llvm::LLVMBuildAlloca(builder,
683-
llrust_ret_ty.to_ref(),
684-
s))
685-
};
676+
let slot = builder.alloca(llrust_ret_ty, "return_alloca");
686677
debug!("out pointer, \
687678
allocad={}, \
688679
llrust_ret_ty={}, \
@@ -724,11 +715,8 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
724715
// pointer). It makes adapting types easier, since we can
725716
// always just bitcast pointers.
726717
if !foreign_indirect {
727-
let lltemp =
728-
llvm::LLVMBuildAlloca(
729-
builder, val_ty(llforeign_arg).to_ref(), noname());
730-
llvm::LLVMBuildStore(
731-
builder, llforeign_arg, lltemp);
718+
let lltemp = builder.alloca(val_ty(llforeign_arg), "");
719+
builder.store(llforeign_arg, lltemp);
732720
llforeign_arg = lltemp;
733721
}
734722

@@ -737,15 +725,13 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
737725
// Rust expects.
738726
if llforeign_arg_ty.cast.is_some() {
739727
assert!(!foreign_indirect);
740-
llforeign_arg = llvm::LLVMBuildBitCast(
741-
builder, llforeign_arg,
742-
llrust_ty.ptr_to().to_ref(), noname());
728+
llforeign_arg = builder.bitcast(llforeign_arg, llrust_ty.ptr_to());
743729
}
744730

745731
let llrust_arg = if rust_indirect {
746732
llforeign_arg
747733
} else {
748-
llvm::LLVMBuildLoad(builder, llforeign_arg, noname())
734+
builder.load(llforeign_arg)
749735
};
750736

751737
debug!("llrust_arg {}{}: {}", "#",
@@ -755,13 +741,8 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
755741

756742
// Perform the call itself
757743
debug!("calling llrustfn = {}, t = {}", ccx.tn.val_to_str(llrustfn), t.repr(ccx.tcx()));
758-
let llrust_ret_val = llvm::LLVMBuildCall(builder, llrustfn, llrust_args.as_ptr(),
759-
llrust_args.len() as c_uint, noname());
760-
761744
let attributes = base::get_fn_llvm_attributes(ccx, t);
762-
for &(idx, attr) in attributes.iter() {
763-
llvm::LLVMAddCallSiteAttribute(llrust_ret_val, idx as c_uint, attr);
764-
}
745+
let llrust_ret_val = builder.call(llrustfn, llrust_args.as_slice(), attributes.as_slice());
765746

766747
// Get the return value where the foreign fn expects it.
767748
let llforeign_ret_ty = match tys.fn_ty.ret_ty.cast {
@@ -772,20 +753,16 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
772753
None if !tys.ret_def => {
773754
// Function returns `()` or `bot`, which in Rust is the LLVM
774755
// type "{}" but in foreign ABIs is "Void".
775-
llvm::LLVMBuildRetVoid(builder);
756+
builder.ret_void();
776757
}
777758

778759
None if rust_uses_outptr => {
779760
// Rust uses an outpointer, but the foreign ABI does not. Load.
780761
let llrust_outptr = return_alloca.unwrap();
781762
let llforeign_outptr_casted =
782-
llvm::LLVMBuildBitCast(builder,
783-
llrust_outptr,
784-
llforeign_ret_ty.ptr_to().to_ref(),
785-
noname());
786-
let llforeign_retval =
787-
llvm::LLVMBuildLoad(builder, llforeign_outptr_casted, noname());
788-
llvm::LLVMBuildRet(builder, llforeign_retval);
763+
builder.bitcast(llrust_outptr, llforeign_ret_ty.ptr_to());
764+
let llforeign_retval = builder.load(llforeign_outptr_casted);
765+
builder.ret(llforeign_retval);
789766
}
790767

791768
None if llforeign_ret_ty != llrust_ret_ty => {
@@ -795,43 +772,31 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: &CrateContext,
795772
// right now we just use a temp memory location and
796773
// bitcast the pointer, which is the same thing the
797774
// old wrappers used to do.
798-
let lltemp =
799-
llvm::LLVMBuildAlloca(
800-
builder, llforeign_ret_ty.to_ref(), noname());
801-
let lltemp_casted =
802-
llvm::LLVMBuildBitCast(builder,
803-
lltemp,
804-
llrust_ret_ty.ptr_to().to_ref(),
805-
noname());
806-
llvm::LLVMBuildStore(
807-
builder, llrust_ret_val, lltemp_casted);
808-
let llforeign_retval =
809-
llvm::LLVMBuildLoad(builder, lltemp, noname());
810-
llvm::LLVMBuildRet(builder, llforeign_retval);
775+
let lltemp = builder.alloca(llforeign_ret_ty, "");
776+
let lltemp_casted = builder.bitcast(lltemp, llrust_ret_ty.ptr_to());
777+
builder.store(llrust_ret_val, lltemp_casted);
778+
let llforeign_retval = builder.load(lltemp);
779+
builder.ret(llforeign_retval);
811780
}
812781

813782
None => {
814783
// Neither ABI uses an outpointer, and the types
815784
// match. Easy peasy.
816-
llvm::LLVMBuildRet(builder, llrust_ret_val);
785+
builder.ret(llrust_ret_val);
817786
}
818787

819788
Some(llforeign_outptr) if !rust_uses_outptr => {
820789
// Foreign ABI requires an out pointer, but Rust doesn't.
821790
// Store Rust return value.
822791
let llforeign_outptr_casted =
823-
llvm::LLVMBuildBitCast(builder,
824-
llforeign_outptr,
825-
llrust_retptr_ty.to_ref(),
826-
noname());
827-
llvm::LLVMBuildStore(
828-
builder, llrust_ret_val, llforeign_outptr_casted);
829-
llvm::LLVMBuildRetVoid(builder);
792+
builder.bitcast(llforeign_outptr, llrust_retptr_ty);
793+
builder.store(llrust_ret_val, llforeign_outptr_casted);
794+
builder.ret_void();
830795
}
831796

832797
Some(_) => {
833798
// Both ABIs use outpointers. Easy peasy.
834-
llvm::LLVMBuildRetVoid(builder);
799+
builder.ret_void();
835800
}
836801
}
837802
}

0 commit comments

Comments
 (0)