Skip to content

Commit dea5a96

Browse files
committed
Simplify argument forwarding in the various shim generators
1 parent f862da5 commit dea5a96

File tree

4 files changed

+28
-62
lines changed

4 files changed

+28
-62
lines changed

src/librustc_llvm/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,18 @@ pub fn get_param(llfn: ValueRef, index: c_uint) -> ValueRef {
22522252
}
22532253
}
22542254

2255+
pub fn get_params(llfn: ValueRef) -> Vec<ValueRef> {
2256+
unsafe {
2257+
let num_params = LLVMCountParams(llfn);
2258+
let mut params = Vec::with_capacity(num_params as usize);
2259+
for idx in 0..num_params {
2260+
params.push(LLVMGetParam(llfn, idx));
2261+
}
2262+
2263+
params
2264+
}
2265+
}
2266+
22552267
#[allow(missing_copy_implementations)]
22562268
pub enum RustString_opaque {}
22572269
pub type RustStringRef = *mut RustString_opaque;

src/librustc_trans/trans/callee.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ pub use self::CallArgs::*;
2121
use arena::TypedArena;
2222
use back::link;
2323
use session;
24-
use llvm::ValueRef;
25-
use llvm::get_param;
26-
use llvm;
24+
use llvm::{self, ValueRef, get_params};
2725
use metadata::csearch;
2826
use middle::def;
2927
use middle::subst;
@@ -343,19 +341,15 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>(
343341
&block_arena);
344342
let mut bcx = init_function(&fcx, false, sig.output);
345343

344+
let llargs = get_params(fcx.llfn);
345+
346346
// the first argument (`self`) will be ptr to the the fn pointer
347347
let llfnpointer = if is_by_ref {
348-
Load(bcx, get_param(fcx.llfn, fcx.arg_pos(0) as u32))
348+
Load(bcx, llargs[fcx.arg_pos(0)])
349349
} else {
350-
get_param(fcx.llfn, fcx.arg_pos(0) as u32)
350+
llargs[fcx.arg_pos(0)]
351351
};
352352

353-
// the remaining arguments will be the untupled values
354-
let llargs: Vec<_> =
355-
sig.inputs.iter()
356-
.enumerate()
357-
.map(|(i, _)| get_param(fcx.llfn, fcx.arg_pos(i+1) as u32))
358-
.collect();
359353
assert!(!fcx.needs_ret_allocas);
360354

361355
let dest = fcx.llretslotptr.get().map(|_|
@@ -366,7 +360,7 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>(
366360
DebugLoc::None,
367361
bare_fn_ty,
368362
|bcx, _| Callee { bcx: bcx, data: Fn(llfnpointer) },
369-
ArgVals(&llargs[..]),
363+
ArgVals(&llargs[fcx.arg_pos(1)..]),
370364
dest).bcx;
371365

372366
finish_fn(&fcx, bcx, sig.output, DebugLoc::None);

src/librustc_trans/trans/closure.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use arena::TypedArena;
1212
use back::link::{self, mangle_internal_name_by_path_and_seq};
13-
use llvm::{ValueRef, get_param};
13+
use llvm::{ValueRef, get_params};
1414
use middle::mem_categorization::Typer;
1515
use trans::adt;
1616
use trans::attributes;
@@ -405,11 +405,13 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
405405
&block_arena);
406406
let mut bcx = init_function(&fcx, false, sig.output);
407407

408+
let llargs = get_params(fcx.llfn);
409+
408410
// the first argument (`self`) will be the (by value) closure env.
409411
let self_scope = fcx.push_custom_cleanup_scope();
410412
let self_scope_id = CustomScope(self_scope);
411413
let rvalue_mode = datum::appropriate_rvalue_mode(ccx, closure_ty);
412-
let llself = get_param(lloncefn, fcx.arg_pos(0) as u32);
414+
let llself = llargs[fcx.arg_pos(0)];
413415
let env_datum = Datum::new(llself, closure_ty, Rvalue::new(rvalue_mode));
414416
let env_datum = unpack_datum!(bcx,
415417
env_datum.to_lvalue_datum_in_scope(bcx, "self",
@@ -418,19 +420,6 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
418420
debug!("trans_fn_once_adapter_shim: env_datum={}",
419421
bcx.val_to_string(env_datum.val));
420422

421-
// the remaining arguments will be packed up in a tuple.
422-
let input_tys = match sig.inputs[1].sty {
423-
ty::TyTuple(ref tys) => &**tys,
424-
_ => bcx.sess().bug(&format!("trans_fn_once_adapter_shim: not rust-call! \
425-
closure_def_id={:?}",
426-
closure_def_id))
427-
};
428-
let llargs: Vec<_> =
429-
input_tys.iter()
430-
.enumerate()
431-
.map(|(i, _)| get_param(lloncefn, fcx.arg_pos(i+1) as u32))
432-
.collect();
433-
434423
let dest =
435424
fcx.llretslotptr.get().map(
436425
|_| expr::SaveIn(fcx.get_ret_slot(bcx, sig.output, "ret_slot")));
@@ -442,7 +431,7 @@ fn trans_fn_once_adapter_shim<'a, 'tcx>(
442431
DebugLoc::None,
443432
llref_fn_ty,
444433
|bcx, _| Callee { bcx: bcx, data: callee_data },
445-
ArgVals(&llargs),
434+
ArgVals(&llargs[fcx.arg_pos(1)..]),
446435
dest).bcx;
447436

448437
fcx.pop_custom_cleanup_scope(self_scope);

src/librustc_trans/trans/meth.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use arena::TypedArena;
1212
use back::abi;
1313
use back::link;
14-
use llvm::{ValueRef, get_param};
14+
use llvm::{ValueRef, get_params};
1515
use metadata::csearch;
1616
use middle::subst::{Subst, Substs};
1717
use middle::subst::VecPerParamSpace;
@@ -611,43 +611,14 @@ pub fn trans_object_shim<'a, 'tcx>(
611611
&block_arena);
612612
let mut bcx = init_function(&fcx, false, sig.output);
613613

614+
let llargs = get_params(fcx.llfn);
615+
614616
// the first argument (`self`) will be a trait object
615-
let llobject = get_param(fcx.llfn, fcx.arg_pos(0) as u32);
617+
let llobject = llargs[fcx.arg_pos(0)];
616618

617619
debug!("trans_object_shim: llobject={}",
618620
bcx.val_to_string(llobject));
619621

620-
// the remaining arguments will be, well, whatever they are
621-
let input_tys =
622-
match fty.abi {
623-
RustCall => {
624-
// unpack the tuple to extract the input type arguments:
625-
match sig.inputs[1].sty {
626-
ty::TyTuple(ref tys) => &**tys,
627-
_ => {
628-
bcx.sess().bug(
629-
&format!("rust-call expects a tuple not {:?}",
630-
sig.inputs[1]));
631-
}
632-
}
633-
}
634-
_ => {
635-
// skip the self parameter:
636-
&sig.inputs[1..]
637-
}
638-
};
639-
640-
let llargs: Vec<_> =
641-
input_tys.iter()
642-
.enumerate()
643-
.map(|(i, _)| {
644-
let llarg = get_param(fcx.llfn, fcx.arg_pos(i+1) as u32);
645-
debug!("trans_object_shim: input #{} == {}",
646-
i, bcx.val_to_string(llarg));
647-
llarg
648-
})
649-
.collect();
650-
651622
assert!(!fcx.needs_ret_allocas);
652623

653624
let dest =
@@ -669,7 +640,7 @@ pub fn trans_object_shim<'a, 'tcx>(
669640
method_bare_fn_ty,
670641
method_offset_in_vtable,
671642
llobject),
672-
ArgVals(&llargs),
643+
ArgVals(&llargs[fcx.arg_pos(1)..]),
673644
dest).bcx;
674645

675646
finish_fn(&fcx, bcx, sig.output, DebugLoc::None);

0 commit comments

Comments
 (0)