Skip to content

Commit a17735c

Browse files
committed
Some simple cleanup of trans_bind.
1 parent be0629d commit a17735c

File tree

1 file changed

+162
-172
lines changed

1 file changed

+162
-172
lines changed

src/comp/middle/trans.rs

Lines changed: 162 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -4616,187 +4616,177 @@ fn trans_bind_1(&@block_ctxt cx, &@ast::expr f, &lval_result f_res,
46164616
&(option::t[@ast::expr])[] args, ast::node_id id) -> result {
46174617
if (f_res.is_mem) {
46184618
bcx_ccx(cx).sess.unimpl("re-binding existing function");
4619-
} else {
4620-
let (@ast::expr)[] bound = ~[];
4621-
for (option::t[@ast::expr] argopt in args) {
4622-
alt (argopt) {
4623-
case (none) { }
4624-
case (some(?e)) { bound += ~[e]; }
4625-
}
4626-
}
4619+
}
46274620

4628-
// Figure out which tydescs we need to pass, if any.
4629-
let ty::t outgoing_fty;
4630-
let ValueRef[] lltydescs;
4631-
alt (f_res.generic) {
4632-
case (none) {
4633-
outgoing_fty = ty::expr_ty(bcx_tcx(cx), f);
4634-
lltydescs = ~[];
4635-
}
4636-
case (some(?ginfo)) {
4637-
lazily_emit_all_generic_info_tydesc_glues(cx, ginfo);
4638-
outgoing_fty = ginfo.item_type;
4639-
lltydescs = ginfo.tydescs;
4640-
}
4621+
let (@ast::expr)[] bound = ~[];
4622+
for (option::t[@ast::expr] argopt in args) {
4623+
alt (argopt) {
4624+
case (none) { }
4625+
case (some(?e)) { bound += ~[e]; }
46414626
}
4642-
auto ty_param_count = std::ivec::len[ValueRef](lltydescs);
4643-
if (std::ivec::len[@ast::expr](bound) == 0u && ty_param_count == 0u) {
4644-
4645-
// Trivial 'binding': just return the static pair-ptr.
4646-
ret f_res.res;
4647-
} else {
4648-
auto bcx = f_res.res.bcx;
4649-
auto pair_t = node_type(bcx_ccx(cx), cx.sp, id);
4650-
auto pair_v = alloca(bcx, pair_t);
4651-
4652-
// Translate the bound expressions.
4653-
let ty::t[] bound_tys = ~[];
4654-
let lval_result[] bound_vals = ~[];
4655-
for (@ast::expr e in bound) {
4656-
auto lv = trans_lval(bcx, e);
4657-
bcx = lv.res.bcx;
4658-
bound_vals += ~[lv];
4659-
bound_tys += ~[ty::expr_ty(bcx_tcx(cx), e)];
4660-
}
4661-
4662-
// Synthesize a closure type.
4663-
4664-
// First, synthesize a tuple type containing the types of all the
4665-
// bound expressions.
4666-
// bindings_ty = ~[bound_ty1, bound_ty2, ...]
4667-
4668-
let ty::t bindings_ty = ty::mk_imm_tup(bcx_tcx(cx),
4669-
bound_tys);
4670-
4671-
// NB: keep this in sync with T_closure_ptr; we're making
4672-
// a ty::t structure that has the same "shape" as the LLVM type
4673-
// it constructs.
4674-
4675-
// Make a vector that contains ty_param_count copies of tydesc_ty.
4676-
// (We'll need room for that many tydescs in the closure.)
4677-
let ty::t tydesc_ty = ty::mk_type(bcx_tcx(cx));
4678-
let ty::t[] captured_tys =
4679-
std::ivec::init_elt[ty::t](tydesc_ty, ty_param_count);
4680-
4681-
// Get all the types we've got (some of which we synthesized
4682-
// ourselves) into a vector. The whole things ends up looking
4683-
// like:
4684-
4685-
// closure_tys = [tydesc_ty, outgoing_fty, [bound_ty1, bound_ty2,
4686-
// ...], [tydesc_ty, tydesc_ty, ...]]
4687-
let ty::t[] closure_tys =
4688-
~[tydesc_ty, outgoing_fty, bindings_ty,
4689-
ty::mk_imm_tup(bcx_tcx(cx), captured_tys)];
4627+
}
46904628

4691-
// Finally, synthesize a type for that whole vector.
4692-
let ty::t closure_ty =
4693-
ty::mk_imm_tup(bcx_tcx(cx), closure_tys);
4629+
// Figure out which tydescs we need to pass, if any.
4630+
let ty::t outgoing_fty;
4631+
let ValueRef[] lltydescs;
4632+
alt (f_res.generic) {
4633+
case (none) {
4634+
outgoing_fty = ty::expr_ty(bcx_tcx(cx), f);
4635+
lltydescs = ~[];
4636+
}
4637+
case (some(?ginfo)) {
4638+
lazily_emit_all_generic_info_tydesc_glues(cx, ginfo);
4639+
outgoing_fty = ginfo.item_type;
4640+
lltydescs = ginfo.tydescs;
4641+
}
4642+
}
4643+
auto ty_param_count = std::ivec::len[ValueRef](lltydescs);
4644+
if (std::ivec::len[@ast::expr](bound) == 0u && ty_param_count == 0u) {
46944645

4695-
// Allocate a box that can hold something closure-sized, including
4696-
// space for a refcount.
4697-
auto r = trans_malloc_boxed(bcx, closure_ty);
4698-
auto box = r.val;
4699-
bcx = r.bcx;
4646+
// Trivial 'binding': just return the static pair-ptr.
4647+
ret f_res.res;
4648+
}
4649+
auto bcx = f_res.res.bcx;
4650+
auto pair_t = node_type(bcx_ccx(cx), cx.sp, id);
4651+
auto pair_v = alloca(bcx, pair_t);
47004652

4701-
// Grab onto the refcount and body parts of the box we allocated.
4702-
auto rc =
4703-
bcx.build.GEP(box,
4704-
~[C_int(0), C_int(abi::box_rc_field_refcnt)]);
4705-
auto closure =
4706-
bcx.build.GEP(box, ~[C_int(0),
4707-
C_int(abi::box_rc_field_body)]);
4708-
bcx.build.Store(C_int(1), rc);
4709-
4710-
// Store bindings tydesc.
4711-
auto bound_tydesc =
4712-
bcx.build.GEP(closure,
4713-
~[C_int(0), C_int(abi::closure_elt_tydesc)]);
4714-
auto ti = none[@tydesc_info];
4715-
auto bindings_tydesc = get_tydesc(bcx, bindings_ty, true, ti);
4716-
lazily_emit_tydesc_glue(bcx, abi::tydesc_field_drop_glue, ti);
4717-
lazily_emit_tydesc_glue(bcx, abi::tydesc_field_free_glue, ti);
4718-
bcx = bindings_tydesc.bcx;
4719-
bcx.build.Store(bindings_tydesc.val, bound_tydesc);
4720-
4721-
// Determine the LLVM type for the outgoing function type. This
4722-
// may be different from the type returned by trans_malloc_boxed()
4723-
// since we have more information than that function does;
4724-
// specifically, we know how many type descriptors the outgoing
4725-
// function has, which type_of() doesn't, as only we know which
4726-
// item the function refers to.
4727-
auto llfnty =
4728-
type_of_fn(bcx_ccx(bcx), cx.sp,
4729-
ty::ty_fn_proto(bcx_tcx(bcx), outgoing_fty),
4730-
ty::ty_fn_args(bcx_tcx(bcx), outgoing_fty),
4731-
ty::ty_fn_ret(bcx_tcx(bcx), outgoing_fty),
4732-
ty_param_count);
4733-
auto llclosurety = T_ptr(T_fn_pair(*bcx_ccx(bcx), llfnty));
4734-
4735-
// Store thunk-target.
4736-
auto bound_target =
4737-
bcx.build.GEP(closure,
4738-
~[C_int(0), C_int(abi::closure_elt_target)]);
4739-
auto src = bcx.build.Load(f_res.res.val);
4740-
bound_target = bcx.build.PointerCast(bound_target, llclosurety);
4741-
bcx.build.Store(src, bound_target);
4742-
4743-
// Copy expr values into boxed bindings.
4744-
auto i = 0u;
4745-
auto bindings =
4746-
bcx.build.GEP(closure,
4747-
~[C_int(0), C_int(abi::closure_elt_bindings)]);
4748-
for (lval_result lv in bound_vals) {
4749-
auto bound =
4750-
bcx.build.GEP(bindings, ~[C_int(0), C_int(i as int)]);
4751-
bcx = move_val_if_temp(bcx, INIT, bound, lv,
4752-
bound_tys.(i)).bcx;
4753-
i += 1u;
4754-
}
4653+
// Translate the bound expressions.
4654+
let ty::t[] bound_tys = ~[];
4655+
let lval_result[] bound_vals = ~[];
4656+
for (@ast::expr e in bound) {
4657+
auto lv = trans_lval(bcx, e);
4658+
bcx = lv.res.bcx;
4659+
bound_vals += ~[lv];
4660+
bound_tys += ~[ty::expr_ty(bcx_tcx(cx), e)];
4661+
}
4662+
4663+
// Synthesize a closure type.
4664+
4665+
// First, synthesize a tuple type containing the types of all the
4666+
// bound expressions.
4667+
// bindings_ty = ~[bound_ty1, bound_ty2, ...]
4668+
4669+
let ty::t bindings_ty = ty::mk_imm_tup(bcx_tcx(cx), bound_tys);
4670+
4671+
// NB: keep this in sync with T_closure_ptr; we're making
4672+
// a ty::t structure that has the same "shape" as the LLVM type
4673+
// it constructs.
4674+
4675+
// Make a vector that contains ty_param_count copies of tydesc_ty.
4676+
// (We'll need room for that many tydescs in the closure.)
4677+
let ty::t tydesc_ty = ty::mk_type(bcx_tcx(cx));
4678+
let ty::t[] captured_tys = std::ivec::init_elt(tydesc_ty, ty_param_count);
4679+
4680+
// Get all the types we've got (some of which we synthesized
4681+
// ourselves) into a vector. The whole things ends up looking
4682+
// like:
4683+
4684+
// closure_tys = [tydesc_ty, outgoing_fty, [bound_ty1, bound_ty2,
4685+
// ...], [tydesc_ty, tydesc_ty, ...]]
4686+
let ty::t[] closure_tys =
4687+
~[tydesc_ty, outgoing_fty, bindings_ty,
4688+
ty::mk_imm_tup(bcx_tcx(cx), captured_tys)];
4689+
4690+
// Finally, synthesize a type for that whole vector.
4691+
let ty::t closure_ty =
4692+
ty::mk_imm_tup(bcx_tcx(cx), closure_tys);
4693+
4694+
// Allocate a box that can hold something closure-sized, including
4695+
// space for a refcount.
4696+
auto r = trans_malloc_boxed(bcx, closure_ty);
4697+
auto box = r.val;
4698+
bcx = r.bcx;
4699+
4700+
// Grab onto the refcount and body parts of the box we allocated.
4701+
auto rc =
4702+
bcx.build.GEP(box, ~[C_int(0), C_int(abi::box_rc_field_refcnt)]);
4703+
auto closure =
4704+
bcx.build.GEP(box, ~[C_int(0), C_int(abi::box_rc_field_body)]);
4705+
bcx.build.Store(C_int(1), rc);
4706+
4707+
// Store bindings tydesc.
4708+
auto bound_tydesc =
4709+
bcx.build.GEP(closure, ~[C_int(0), C_int(abi::closure_elt_tydesc)]);
4710+
auto ti = none;
4711+
auto bindings_tydesc = get_tydesc(bcx, bindings_ty, true, ti);
4712+
lazily_emit_tydesc_glue(bcx, abi::tydesc_field_drop_glue, ti);
4713+
lazily_emit_tydesc_glue(bcx, abi::tydesc_field_free_glue, ti);
4714+
bcx = bindings_tydesc.bcx;
4715+
bcx.build.Store(bindings_tydesc.val, bound_tydesc);
4716+
4717+
// Determine the LLVM type for the outgoing function type. This
4718+
// may be different from the type returned by trans_malloc_boxed()
4719+
// since we have more information than that function does;
4720+
// specifically, we know how many type descriptors the outgoing
4721+
// function has, which type_of() doesn't, as only we know which
4722+
// item the function refers to.
4723+
auto llfnty =
4724+
type_of_fn(bcx_ccx(bcx), cx.sp,
4725+
ty::ty_fn_proto(bcx_tcx(bcx), outgoing_fty),
4726+
ty::ty_fn_args(bcx_tcx(bcx), outgoing_fty),
4727+
ty::ty_fn_ret(bcx_tcx(bcx), outgoing_fty),
4728+
ty_param_count);
4729+
auto llclosurety = T_ptr(T_fn_pair(*bcx_ccx(bcx), llfnty));
4730+
4731+
// Store thunk-target.
4732+
auto bound_target =
4733+
bcx.build.GEP(closure, ~[C_int(0), C_int(abi::closure_elt_target)]);
4734+
auto src = bcx.build.Load(f_res.res.val);
4735+
bound_target = bcx.build.PointerCast(bound_target, llclosurety);
4736+
bcx.build.Store(src, bound_target);
4737+
4738+
// Copy expr values into boxed bindings.
4739+
auto i = 0u;
4740+
auto bindings =
4741+
bcx.build.GEP(closure,
4742+
~[C_int(0), C_int(abi::closure_elt_bindings)]);
4743+
for (lval_result lv in bound_vals) {
4744+
auto bound =
4745+
bcx.build.GEP(bindings, ~[C_int(0), C_int(i as int)]);
4746+
bcx = move_val_if_temp(bcx, INIT, bound, lv, bound_tys.(i)).bcx;
4747+
i += 1u;
4748+
}
47554749

4756-
// If necessary, copy tydescs describing type parameters into the
4757-
// appropriate slot in the closure.
4758-
alt (f_res.generic) {
4759-
case (none) {/* nothing to do */ }
4760-
case (some(?ginfo)) {
4761-
lazily_emit_all_generic_info_tydesc_glues(cx, ginfo);
4762-
auto ty_params_slot =
4763-
bcx.build.GEP(closure,
4764-
~[C_int(0),
4765-
C_int(abi::closure_elt_ty_params)]);
4766-
auto i = 0;
4767-
for (ValueRef td in ginfo.tydescs) {
4768-
auto ty_param_slot =
4769-
bcx.build.GEP(ty_params_slot,
4770-
~[C_int(0), C_int(i)]);
4771-
bcx.build.Store(td, ty_param_slot);
4772-
i += 1;
4773-
}
4774-
outgoing_fty = ginfo.item_type;
4775-
}
4776-
}
4750+
// If necessary, copy tydescs describing type parameters into the
4751+
// appropriate slot in the closure.
4752+
alt (f_res.generic) {
4753+
case (none) {/* nothing to do */ }
4754+
case (some(?ginfo)) {
4755+
lazily_emit_all_generic_info_tydesc_glues(cx, ginfo);
4756+
auto ty_params_slot =
4757+
bcx.build.GEP(closure,
4758+
~[C_int(0), C_int(abi::closure_elt_ty_params)]);
4759+
auto i = 0;
4760+
for (ValueRef td in ginfo.tydescs) {
4761+
auto ty_param_slot = bcx.build.GEP(ty_params_slot,
4762+
~[C_int(0), C_int(i)]);
4763+
bcx.build.Store(td, ty_param_slot);
4764+
i += 1;
4765+
}
4766+
outgoing_fty = ginfo.item_type;
4767+
}
4768+
}
47774769

4778-
// Make thunk and store thunk-ptr in outer pair's code slot.
4779-
auto pair_code =
4780-
bcx.build.GEP(pair_v, ~[C_int(0), C_int(abi::fn_field_code)]);
4781-
// The type of the entire bind expression.
4782-
let ty::t pair_ty = node_id_type(bcx_ccx(cx), id);
4770+
// Make thunk and store thunk-ptr in outer pair's code slot.
4771+
auto pair_code =
4772+
bcx.build.GEP(pair_v, ~[C_int(0), C_int(abi::fn_field_code)]);
4773+
// The type of the entire bind expression.
4774+
let ty::t pair_ty = node_id_type(bcx_ccx(cx), id);
47834775

4784-
let ValueRef llthunk =
4785-
trans_bind_thunk(cx.fcx.lcx, cx.sp, pair_ty, outgoing_fty,
4786-
args, closure_ty, bound_tys, ty_param_count);
4787-
bcx.build.Store(llthunk, pair_code);
4776+
let ValueRef llthunk =
4777+
trans_bind_thunk(cx.fcx.lcx, cx.sp, pair_ty, outgoing_fty,
4778+
args, closure_ty, bound_tys, ty_param_count);
4779+
bcx.build.Store(llthunk, pair_code);
47884780

4789-
// Store box ptr in outer pair's box slot.
4790-
auto ccx = *bcx_ccx(bcx);
4791-
auto pair_box =
4792-
bcx.build.GEP(pair_v, ~[C_int(0), C_int(abi::fn_field_box)]);
4793-
bcx.build.Store(bcx.build.PointerCast(box,
4794-
T_opaque_closure_ptr(ccx)),
4795-
pair_box);
4796-
add_clean_temp(cx, pair_v, pair_ty);
4797-
ret rslt(bcx, pair_v);
4798-
}
4799-
}
4781+
// Store box ptr in outer pair's box slot.
4782+
auto ccx = *bcx_ccx(bcx);
4783+
auto pair_box =
4784+
bcx.build.GEP(pair_v, ~[C_int(0), C_int(abi::fn_field_box)]);
4785+
bcx.build.Store(
4786+
bcx.build.PointerCast(box, T_opaque_closure_ptr(ccx)),
4787+
pair_box);
4788+
add_clean_temp(cx, pair_v, pair_ty);
4789+
ret rslt(bcx, pair_v);
48004790
}
48014791

48024792
fn trans_arg_expr(&@block_ctxt cx, &ty::arg arg, TypeRef lldestty0,

0 commit comments

Comments
 (0)