Skip to content

Commit b7f71e1

Browse files
committed
Implementing suggestions from @nikomatsakis
1 parent a85993f commit b7f71e1

File tree

6 files changed

+102
-101
lines changed

6 files changed

+102
-101
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ pub fn new_fn_ctxt_w_id(ccx: @CrateContext,
15931593
impl_id: Option<ast::def_id>,
15941594
param_substs: Option<@param_substs>,
15951595
sp: Option<span>)
1596-
-> (fn_ctxt, bool) {
1596+
-> fn_ctxt {
15971597
for param_substs.each |p| { p.validate(); }
15981598

15991599
debug!("new_fn_ctxt_w_id(path=%s, id=%?, impl_id=%?, \
@@ -1611,12 +1611,11 @@ pub fn new_fn_ctxt_w_id(ccx: @CrateContext,
16111611
ty::subst_tps(ccx.tcx, substs.tys, substs.self_ty, output_type)
16121612
}
16131613
};
1614-
let imm = ty::type_is_immediate(substd_output_type);
1615-
1614+
let is_immediate = ty::type_is_immediate(substd_output_type);
16161615
let fcx = @mut fn_ctxt_ {
16171616
llfn: llfndecl,
16181617
llenv: unsafe {
1619-
llvm::LLVMGetParam(llfndecl, arg_env(imm) as c_uint)
1618+
llvm::LLVMGetUndef(T_ptr(T_i8()))
16201619
},
16211620
llretptr: None,
16221621
llstaticallocas: llbbs.sa,
@@ -1625,7 +1624,7 @@ pub fn new_fn_ctxt_w_id(ccx: @CrateContext,
16251624
llself: None,
16261625
personality: None,
16271626
loop_ret: None,
1628-
has_immediate_return_value: imm,
1627+
has_immediate_return_value: is_immediate,
16291628
llargs: @mut HashMap::new(),
16301629
lllocals: @mut HashMap::new(),
16311630
llupvars: @mut HashMap::new(),
@@ -1636,17 +1635,19 @@ pub fn new_fn_ctxt_w_id(ccx: @CrateContext,
16361635
path: path,
16371636
ccx: @ccx
16381637
};
1639-
1638+
fcx.llenv = unsafe {
1639+
llvm::LLVMGetParam(llfndecl, fcx.env_arg_pos() as c_uint)
1640+
};
16401641
fcx.llretptr = Some(make_return_pointer(fcx, substd_output_type));
1641-
(fcx, imm)
1642+
fcx
16421643
}
16431644

16441645
pub fn new_fn_ctxt(ccx: @CrateContext,
16451646
path: path,
16461647
llfndecl: ValueRef,
16471648
output_type: ty::t,
16481649
sp: Option<span>)
1649-
-> (fn_ctxt, bool) {
1650+
-> fn_ctxt {
16501651
new_fn_ctxt_w_id(ccx, path, llfndecl, -1, output_type, None, None, sp)
16511652
}
16521653

@@ -1666,8 +1667,7 @@ pub fn new_fn_ctxt(ccx: @CrateContext,
16661667
// field of the fn_ctxt with
16671668
pub fn create_llargs_for_fn_args(cx: fn_ctxt,
16681669
self_arg: self_arg,
1669-
args: &[ast::arg],
1670-
ret_imm: bool)
1670+
args: &[ast::arg])
16711671
-> ~[ValueRef] {
16721672
let _icx = cx.insn_ctxt("create_llargs_for_fn_args");
16731673

@@ -1693,7 +1693,7 @@ pub fn create_llargs_for_fn_args(cx: fn_ctxt,
16931693
// llvm::LLVMGetParam for each argument.
16941694
vec::from_fn(args.len(), |i| {
16951695
unsafe {
1696-
let arg_n = arg_pos(ret_imm, i);
1696+
let arg_n = cx.arg_pos(i);
16971697
let arg = &args[i];
16981698
let llarg = llvm::LLVMGetParam(cx.llfn, arg_n as c_uint);
16991699

@@ -1832,15 +1832,15 @@ pub fn trans_closure(ccx: @CrateContext,
18321832
param_substs.repr(ccx.tcx));
18331833

18341834
// Set up arguments to the function.
1835-
let (fcx, imm) = new_fn_ctxt_w_id(ccx,
1836-
path,
1837-
llfndecl,
1838-
id,
1839-
output_type,
1840-
impl_id,
1841-
param_substs,
1842-
Some(body.span));
1843-
let raw_llargs = create_llargs_for_fn_args(fcx, self_arg, decl.inputs, imm);
1835+
let fcx = new_fn_ctxt_w_id(ccx,
1836+
path,
1837+
llfndecl,
1838+
id,
1839+
output_type,
1840+
impl_id,
1841+
param_substs,
1842+
Some(body.span));
1843+
let raw_llargs = create_llargs_for_fn_args(fcx, self_arg, decl.inputs);
18441844

18451845
// Set the fixed stack segment flag if necessary.
18461846
if attr::attrs_contains_name(attributes, "fixed_stack_segment") {
@@ -1965,16 +1965,16 @@ pub fn trans_enum_variant(ccx: @CrateContext,
19651965
ty_param_substs,
19661966
None,
19671967
ty::node_id_to_type(ccx.tcx, enum_id));
1968-
let (fcx, imm) = new_fn_ctxt_w_id(ccx,
1969-
~[],
1970-
llfndecl,
1971-
variant.node.id,
1972-
enum_ty,
1973-
None,
1974-
param_substs,
1975-
None);
1976-
1977-
let raw_llargs = create_llargs_for_fn_args(fcx, no_self, fn_args, imm);
1968+
let fcx = new_fn_ctxt_w_id(ccx,
1969+
~[],
1970+
llfndecl,
1971+
variant.node.id,
1972+
enum_ty,
1973+
None,
1974+
param_substs,
1975+
None);
1976+
1977+
let raw_llargs = create_llargs_for_fn_args(fcx, no_self, fn_args);
19781978
let bcx = top_scope_block(fcx, None), lltop = bcx.llbb;
19791979
let arg_tys = ty::ty_fn_args(node_id_type(bcx, variant.node.id));
19801980
let bcx = copy_args_to_allocas(fcx, bcx, fn_args, raw_llargs, arg_tys);
@@ -2044,16 +2044,16 @@ pub fn trans_tuple_struct(ccx: @CrateContext,
20442044
ty_to_str(ccx.tcx, ctor_ty)))
20452045
};
20462046
2047-
let (fcx, imm) = new_fn_ctxt_w_id(ccx,
2048-
~[],
2049-
llfndecl,
2050-
ctor_id,
2051-
tup_ty,
2052-
None,
2053-
param_substs,
2054-
None);
2047+
let fcx = new_fn_ctxt_w_id(ccx,
2048+
~[],
2049+
llfndecl,
2050+
ctor_id,
2051+
tup_ty,
2052+
None,
2053+
param_substs,
2054+
None);
20552055
2056-
let raw_llargs = create_llargs_for_fn_args(fcx, no_self, fn_args, imm);
2056+
let raw_llargs = create_llargs_for_fn_args(fcx, no_self, fn_args);
20572057
20582058
let bcx = top_scope_block(fcx, None);
20592059
let lltop = bcx.llbb;
@@ -2301,14 +2301,19 @@ pub fn create_entry_wrapper(ccx: @CrateContext,
23012301
let llfdecl = decl_fn(ccx.llmod, "_rust_main",
23022302
lib::llvm::CCallConv, llfty);
23032303
2304-
let (fcx, _) = new_fn_ctxt(ccx, ~[], llfdecl, nt, None);
2304+
let fcx = new_fn_ctxt(ccx, ~[], llfdecl, nt, None);
2305+
2306+
// the args vector built in create_entry_fn will need
2307+
// be updated if this assertion starts to fail.
2308+
assert!(fcx.has_immediate_return_value);
23052309
23062310
let bcx = top_scope_block(fcx, None);
23072311
let lltop = bcx.llbb;
23082312
23092313
// Call main.
23102314
let llenvarg = unsafe {
2311-
llvm::LLVMGetParam(llfdecl, arg_env(true) as c_uint)
2315+
let env_arg = fcx.env_arg_pos();
2316+
llvm::LLVMGetParam(llfdecl, env_arg as c_uint)
23122317
};
23132318
let args = ~[llenvarg];
23142319
let llresult = Call(bcx, main_llfn, args);

src/librustc/middle/trans/cabi.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ pub impl FnType {
132132
bcx: block,
133133
ret_ty: TypeRef,
134134
llwrapfn: ValueRef,
135-
llargbundle: ValueRef,
136-
ret_imm: bool) {
135+
llargbundle: ValueRef) {
137136
let mut atys = /*bad*/copy self.arg_tys;
138137
let mut attrs = /*bad*/copy self.attrs;
139138
let mut j = 0u;

src/librustc/middle/trans/common.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,30 @@ pub struct fn_ctxt_ {
351351
ccx: @@CrateContext
352352
}
353353

354+
pub impl fn_ctxt_ {
355+
pub fn arg_pos(&self, arg: uint) -> uint {
356+
if self.has_immediate_return_value {
357+
arg + 1u
358+
} else {
359+
arg + 2u
360+
}
361+
}
362+
363+
pub fn out_arg_pos(&self) -> uint {
364+
assert!(self.has_immediate_return_value);
365+
0u
366+
}
367+
368+
pub fn env_arg_pos(&self) -> uint {
369+
if !self.has_immediate_return_value {
370+
1u
371+
} else {
372+
0u
373+
}
374+
}
375+
376+
}
377+
354378
pub type fn_ctxt = @mut fn_ctxt_;
355379

356380
pub fn warn_not_to_commit(ccx: @CrateContext, msg: &str) {
@@ -660,27 +684,6 @@ pub fn mk_block(llbb: BasicBlockRef, parent: Option<block>, kind: block_kind,
660684
@mut block_(llbb, parent, kind, is_lpad, node_info, fcx)
661685
}
662686

663-
pub fn arg_pos(ret_imm: bool, arg: uint) -> uint {
664-
if ret_imm {
665-
arg + 1u
666-
} else {
667-
arg + 2u
668-
}
669-
}
670-
671-
pub fn arg_out(ret_imm: bool) -> uint {
672-
assert!(ret_imm);
673-
0u
674-
}
675-
676-
pub fn arg_env(ret_imm: bool) -> uint {
677-
if !ret_imm {
678-
1u
679-
} else {
680-
0u
681-
}
682-
}
683-
684687
pub struct Result {
685688
bcx: block,
686689
val: ValueRef

src/librustc/middle/trans/foreign.rs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,10 @@ fn build_shim_fn_(ccx: @CrateContext,
150150
ccx.llmod, shim_name, tys.shim_fn_ty);
151151

152152
// Declare the body of the shim function:
153-
let (fcx, imm) = new_fn_ctxt(ccx, ~[], llshimfn, tys.fn_sig.output, None);
153+
let fcx = new_fn_ctxt(ccx, ~[], llshimfn, tys.fn_sig.output, None);
154154
let bcx = top_scope_block(fcx, None);
155155
let lltop = bcx.llbb;
156156

157-
//
158-
// FIXME [#6575] this seems to be making the assumption that the first
159-
// implicit argument is always available?
160-
//
161157
let llargbundle = get_param(llshimfn, 0u);
162158
let llargvals = arg_builder(bcx, tys, llargbundle);
163159

@@ -179,8 +175,7 @@ fn build_shim_fn_(ccx: @CrateContext,
179175
type wrap_arg_builder<'self> = &'self fn(bcx: block,
180176
tys: &ShimTypes,
181177
llwrapfn: ValueRef,
182-
llargbundle: ValueRef,
183-
ret_imm: bool);
178+
llargbundle: ValueRef);
184179

185180
type wrap_ret_builder<'self> = &'self fn(bcx: block,
186181
tys: &ShimTypes,
@@ -195,7 +190,7 @@ fn build_wrap_fn_(ccx: @CrateContext,
195190
arg_builder: wrap_arg_builder,
196191
ret_builder: wrap_ret_builder) {
197192
let _icx = ccx.insn_ctxt("foreign::build_wrap_fn_");
198-
let (fcx, imm) = new_fn_ctxt(ccx, ~[], llwrapfn, tys.fn_sig.output, None);
193+
let fcx = new_fn_ctxt(ccx, ~[], llwrapfn, tys.fn_sig.output, None);
199194

200195
// Patch up the return type if it's not immediate and we're returning via
201196
// the C ABI.
@@ -210,7 +205,7 @@ fn build_wrap_fn_(ccx: @CrateContext,
210205

211206
// Allocate the struct and write the arguments into it.
212207
let llargbundle = alloca(bcx, tys.bundle_ty);
213-
arg_builder(bcx, tys, llwrapfn, llargbundle, imm);
208+
arg_builder(bcx, tys, llwrapfn, llargbundle);
214209

215210
// Create call itself.
216211
let llshimfnptr = PointerCast(bcx, llshimfn, T_ptr(T_i8()));
@@ -438,14 +433,14 @@ pub fn trans_foreign_mod(ccx: @CrateContext,
438433
cc: lib::llvm::CallConv) {
439434
debug!("build_direct_fn(%s)", *link_name(ccx, item));
440435

441-
let (fcx, imm) = new_fn_ctxt(ccx, ~[], decl, tys.fn_sig.output, None);
436+
let fcx = new_fn_ctxt(ccx, ~[], decl, tys.fn_sig.output, None);
442437
let bcx = top_scope_block(fcx, None), lltop = bcx.llbb;
443438
let llbasefn = base_fn(ccx, *link_name(ccx, item), tys, cc);
444439
let ty = ty::lookup_item_type(ccx.tcx,
445440
ast_util::local_def(item.id)).ty;
446441
let ret_ty = ty::ty_fn_ret(ty);
447442
let args = vec::from_fn(ty::ty_fn_args(ty).len(), |i| {
448-
get_param(decl, arg_pos(imm, i))
443+
get_param(decl, fcx.arg_pos(i))
449444
});
450445
let retval = Call(bcx, llbasefn, args);
451446
if !ty::type_is_nil(ret_ty) && !ty::type_is_bot(ret_ty) {
@@ -464,7 +459,7 @@ pub fn trans_foreign_mod(ccx: @CrateContext,
464459
cc: lib::llvm::CallConv) {
465460
debug!("build_fast_ffi_fn(%s)", *link_name(ccx, item));
466461

467-
let (fcx, imm) = new_fn_ctxt(ccx, ~[], decl, tys.fn_sig.output, None);
462+
let fcx = new_fn_ctxt(ccx, ~[], decl, tys.fn_sig.output, None);
468463
let bcx = top_scope_block(fcx, None), lltop = bcx.llbb;
469464
let llbasefn = base_fn(ccx, *link_name(ccx, item), tys, cc);
470465
set_no_inline(fcx.llfn);
@@ -473,7 +468,7 @@ pub fn trans_foreign_mod(ccx: @CrateContext,
473468
ast_util::local_def(item.id)).ty;
474469
let ret_ty = ty::ty_fn_ret(ty);
475470
let args = vec::from_fn(ty::ty_fn_args(ty).len(), |i| {
476-
get_param(decl, arg_pos(imm, i))
471+
get_param(decl, fcx.arg_pos(i))
477472
});
478473
let retval = Call(bcx, llbasefn, args);
479474
if !ty::type_is_nil(ret_ty) && !ty::type_is_bot(ret_ty) {
@@ -514,13 +509,13 @@ pub fn trans_foreign_mod(ccx: @CrateContext,
514509
fn build_args(bcx: block,
515510
tys: &ShimTypes,
516511
llwrapfn: ValueRef,
517-
llargbundle: ValueRef,
518-
ret_imm: bool) {
512+
llargbundle: ValueRef) {
519513
let _icx = bcx.insn_ctxt("foreign::wrap::build_args");
520514
let ccx = bcx.ccx();
521515
let n = tys.llsig.llarg_tys.len();
522516
for uint::range(0, n) |i| {
523-
let mut llargval = get_param(llwrapfn, arg_pos(ret_imm, i));
517+
let arg_i = bcx.fcx.arg_pos(i);
518+
let mut llargval = get_param(llwrapfn, arg_i);
524519

525520
// In some cases, Rust will pass a pointer which the
526521
// native C type doesn't have. In that case, just
@@ -558,14 +553,14 @@ pub fn trans_intrinsic(ccx: @CrateContext,
558553

559554
let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx, item.id));
560555

561-
let (fcx, imm) = new_fn_ctxt_w_id(ccx,
562-
path,
563-
decl,
564-
item.id,
565-
output_type,
566-
None,
567-
Some(substs),
568-
Some(item.span));
556+
let fcx = new_fn_ctxt_w_id(ccx,
557+
path,
558+
decl,
559+
item.id,
560+
output_type,
561+
None,
562+
Some(substs),
563+
Some(item.span));
569564

570565
// Set the fixed stack segment flag if necessary.
571566
if attr::attrs_contains_name(attributes, "fixed_stack_segment") {
@@ -574,7 +569,7 @@ pub fn trans_intrinsic(ccx: @CrateContext,
574569

575570
let mut bcx = top_scope_block(fcx, None);
576571
let lltop = bcx.llbb;
577-
let first_real_arg = arg_pos(imm, 0u);
572+
let first_real_arg = fcx.arg_pos(0u);
578573
match *ccx.sess.str_of(item.ident) {
579574
~"atomic_cxchg" => {
580575
let old = AtomicCmpXchg(bcx,
@@ -1356,14 +1351,12 @@ pub fn trans_foreign_fn(ccx: @CrateContext,
13561351
fn build_args(bcx: block,
13571352
tys: &ShimTypes,
13581353
llwrapfn: ValueRef,
1359-
llargbundle: ValueRef,
1360-
ret_imm: bool) {
1354+
llargbundle: ValueRef) {
13611355
let _icx = bcx.insn_ctxt("foreign::foreign::wrap::build_args");
13621356
tys.fn_ty.build_wrap_args(bcx,
13631357
tys.llsig.llret_ty,
13641358
llwrapfn,
1365-
llargbundle,
1366-
ret_imm);
1359+
llargbundle);
13671360
}
13681361

13691362
fn build_ret(bcx: block, tys: &ShimTypes, llargbundle: ValueRef) {

0 commit comments

Comments
 (0)