Skip to content

Commit a4d75a4

Browse files
committed
Remove GEP_tup_like
1 parent c3a93ec commit a4d75a4

File tree

3 files changed

+62
-181
lines changed

3 files changed

+62
-181
lines changed

src/rustc/middle/trans/alt.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,11 @@ fn compile_submatch(bcx: block, m: match, vals: [ValueRef],
404404
let rec_fields = collect_record_fields(m, col);
405405
// Separate path for extracting and binding record fields
406406
if rec_fields.len() > 0u {
407-
let rec_ty = node_id_type(bcx, pat_id);
408-
let fields = ty::get_fields(rec_ty);
407+
let fields = ty::get_fields(node_id_type(bcx, pat_id));
409408
let rec_vals = [];
410409
for field_name: ast::ident in rec_fields {
411410
let ix = option::get(ty::field_idx(field_name, fields));
412-
let r = GEP_tup_like(bcx, rec_ty, val, [0, ix as int]);
413-
rec_vals += [r.val];
414-
bcx = r.bcx;
411+
rec_vals += [GEPi(bcx, val, [0, ix as int])];
415412
}
416413
compile_submatch(bcx, enter_rec(dm, m, col, rec_fields, val),
417414
rec_vals + vals_left, chk, exits);
@@ -426,9 +423,7 @@ fn compile_submatch(bcx: block, m: match, vals: [ValueRef],
426423
};
427424
let tup_vals = [], i = 0u;
428425
while i < n_tup_elts {
429-
let r = GEP_tup_like(bcx, tup_ty, val, [0, i as int]);
430-
tup_vals += [r.val];
431-
bcx = r.bcx;
426+
tup_vals += [GEPi(bcx, val, [0, i as int])];
432427
i += 1u;
433428
}
434429
compile_submatch(bcx, enter_tup(dm, m, col, val, n_tup_elts),
@@ -706,21 +701,19 @@ fn bind_irrefutable_pat(bcx: block, pat: @ast::pat, val: ValueRef,
706701
}
707702
}
708703
ast::pat_rec(fields, _) {
709-
let rec_ty = node_id_type(bcx, pat.id);
710-
let rec_fields = ty::get_fields(rec_ty);
704+
let rec_fields = ty::get_fields(node_id_type(bcx, pat.id));
711705
for f: ast::field_pat in fields {
712706
let ix = option::get(ty::field_idx(f.ident, rec_fields));
713707
// how to get rid of this check?
714-
let r = GEP_tup_like(bcx, rec_ty, val, [0, ix as int]);
715-
bcx = bind_irrefutable_pat(r.bcx, f.pat, r.val, make_copy);
708+
let fldptr = GEPi(bcx, val, [0, ix as int]);
709+
bcx = bind_irrefutable_pat(bcx, f.pat, fldptr, make_copy);
716710
}
717711
}
718712
ast::pat_tup(elems) {
719-
let tup_ty = node_id_type(bcx, pat.id);
720713
let i = 0u;
721714
for elem in elems {
722-
let r = GEP_tup_like(bcx, tup_ty, val, [0, i as int]);
723-
bcx = bind_irrefutable_pat(r.bcx, elem, r.val, make_copy);
715+
let fldptr = GEPi(bcx, val, [0, i as int]);
716+
bcx = bind_irrefutable_pat(bcx, elem, fldptr, make_copy);
724717
i += 1u;
725718
}
726719
}

src/rustc/middle/trans/base.rs

Lines changed: 41 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -273,113 +273,22 @@ fn bump_ptr(bcx: block, t: ty::t, base: ValueRef, sz: ValueRef) ->
273273
} else { bumped }
274274
}
275275

276-
// Replacement for the LLVM 'GEP' instruction when field-indexing into a
277-
// tuple-like structure (tup, rec) with a static index. This one is driven off
278-
// ty::struct and knows what to do when it runs into a ty_param stuck in the
279-
// middle of the thing it's GEP'ing into. Much like size_of and align_of,
280-
// above.
281-
fn GEP_tup_like(bcx: block, t: ty::t, base: ValueRef, ixs: [int])
282-
-> result {
283-
fn compute_off(bcx: block,
284-
off: ValueRef,
285-
t: ty::t,
286-
ixs: [int],
287-
n: uint) -> (block, ValueRef, ty::t) {
288-
if n == ixs.len() {
289-
ret (bcx, off, t);
290-
}
291-
292-
let ix = ixs[n];
293-
let bcx = bcx, off = off;
294-
int::range(0, ix) {|i|
295-
let comp_t = ty::get_element_type(t, i as uint);
296-
let align = align_of(bcx, comp_t);
297-
bcx = align.bcx;
298-
off = align_to(bcx, off, align.val);
299-
let sz = size_of(bcx, comp_t);
300-
bcx = sz.bcx;
301-
off = Add(bcx, off, sz.val);
302-
}
303-
304-
let comp_t = ty::get_element_type(t, ix as uint);
305-
let align = align_of(bcx, comp_t);
306-
bcx = align.bcx;
307-
off = align_to(bcx, off, align.val);
308-
309-
be compute_off(bcx, off, comp_t, ixs, n+1u);
310-
}
311-
312-
if !ty::type_has_dynamic_size(bcx.tcx(), t) {
313-
ret rslt(bcx, GEPi(bcx, base, ixs));
314-
}
315-
316-
#debug["GEP_tup_like(t=%s,base=%s,ixs=%?)",
317-
ty_to_str(bcx.tcx(), t),
318-
val_str(bcx.ccx().tn, base),
319-
ixs];
320-
321-
// We require that ixs start with 0 and we expect the input to be a
322-
// pointer to an instance of type t, so we can safely ignore ixs[0],
323-
// basically.
324-
assert ixs[0] == 0;
325-
326-
let (bcx, off, tar_t) = {
327-
compute_off(bcx, C_int(bcx.ccx(), 0), t, ixs, 1u)
328-
};
329-
ret rslt(bcx, bump_ptr(bcx, tar_t, base, off));
330-
}
331-
332-
333276
// Replacement for the LLVM 'GEP' instruction when field indexing into a enum.
334-
// This function uses GEP_tup_like() above and automatically performs casts as
335-
// appropriate. @llblobptr is the data part of a enum value; its actual type
277+
// @llblobptr is the data part of a enum value; its actual type
336278
// is meaningless, as it will be cast away.
337-
fn GEP_enum(cx: block, llblobptr: ValueRef, enum_id: ast::def_id,
338-
variant_id: ast::def_id, ty_substs: [ty::t],
339-
ix: uint) -> result {
340-
let variant = ty::enum_variant_with_id(cx.tcx(), enum_id, variant_id);
279+
fn GEP_enum(bcx: block, llblobptr: ValueRef, enum_id: ast::def_id,
280+
variant_id: ast::def_id, ty_substs: [ty::t],
281+
ix: uint) -> result {
282+
let ccx = bcx.ccx();
283+
let variant = ty::enum_variant_with_id(ccx.tcx, enum_id, variant_id);
341284
assert ix < variant.args.len();
342285

343-
// Synthesize a tuple type so that GEP_tup_like() can work its magic.
344-
// Separately, store the type of the element we're interested in.
345-
346-
let arg_tys = variant.args;
347-
348-
let true_arg_tys: [ty::t] = [];
349-
for aty: ty::t in arg_tys {
350-
// Would be nice to have a way of stating the invariant
351-
// that ty_substs is valid for aty
352-
let arg_ty = ty::substitute_type_params(cx.tcx(), ty_substs, aty);
353-
true_arg_tys += [arg_ty];
354-
}
355-
356-
// We know that ix < len(variant.args) -- so
357-
// it's safe to do this. (Would be nice to have
358-
// typestate guarantee that a dynamic bounds check
359-
// error can't happen here, but that's in the future.)
360-
let elem_ty = true_arg_tys[ix];
361-
362-
let tup_ty = ty::mk_tup(cx.tcx(), true_arg_tys);
363-
// Cast the blob pointer to the appropriate type, if we need to (i.e. if
364-
// the blob pointer isn't dynamically sized).
365-
366-
let llunionptr: ValueRef;
367-
let ccx = cx.ccx();
368-
if check type_has_static_size(ccx, tup_ty) {
369-
let llty = type_of(ccx, tup_ty);
370-
llunionptr = TruncOrBitCast(cx, llblobptr, T_ptr(llty));
371-
} else { llunionptr = llblobptr; }
372-
373-
// Do the GEP_tup_like().
374-
let rs = GEP_tup_like(cx, tup_ty, llunionptr, [0, ix as int]);
375-
// Cast the result to the appropriate type, if necessary.
376-
377-
let val = if check type_has_static_size(ccx, elem_ty) {
378-
let llelemty = type_of(ccx, elem_ty);
379-
PointerCast(rs.bcx, rs.val, T_ptr(llelemty))
380-
} else { rs.val };
381-
382-
ret rslt(rs.bcx, val);
286+
let arg_lltys = vec::map(variant.args, {|aty|
287+
type_of(ccx, ty::substitute_type_params(ccx.tcx, ty_substs, aty))
288+
});
289+
let typed_blobptr = PointerCast(bcx, llblobptr,
290+
T_ptr(T_struct(arg_lltys)));
291+
rslt(bcx, GEPi(bcx, typed_blobptr, [0, ix as int]))
383292
}
384293

385294
// trans_shared_malloc: expects a type indicating which pointer type we want
@@ -823,11 +732,10 @@ fn trans_res_drop(bcx: block, rs: ValueRef, did: ast::def_id,
823732
inner_t: ty::t, tps: [ty::t]) -> block {
824733
let ccx = bcx.ccx();
825734
let inner_t_s = ty::substitute_type_params(ccx.tcx, tps, inner_t);
826-
let tup_ty = ty::mk_tup(ccx.tcx, [ty::mk_int(ccx.tcx), inner_t_s]);
827735

828-
let {bcx, val: drop_flag} = GEP_tup_like(bcx, tup_ty, rs, [0, 0]);
736+
let drop_flag = GEPi(bcx, rs, [0, 0]);
829737
with_cond(bcx, IsNotNull(bcx, Load(bcx, drop_flag))) {|bcx|
830-
let {bcx, val: valptr} = GEP_tup_like(bcx, tup_ty, rs, [0, 1]);
738+
let valptr = GEPi(bcx, rs, [0, 1]);
831739
// Find and call the actual destructor.
832740
let dtor_addr = get_res_dtor(ccx, did, tps);
833741
let args = [bcx.fcx.llretptr, null_env_ptr(bcx)];
@@ -840,7 +748,7 @@ fn trans_res_drop(bcx: block, rs: ValueRef, did: ast::def_id,
840748
let val_cast = BitCast(bcx, valptr, val_llty);
841749
Call(bcx, dtor_addr, args + [val_cast]);
842750

843-
bcx = drop_ty(bcx, valptr, inner_t_s);
751+
let bcx = drop_ty(bcx, valptr, inner_t_s);
844752
Store(bcx, C_u8(0u), drop_flag);
845753
bcx
846754
}
@@ -1014,26 +922,24 @@ fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
1014922
ty::ty_rec(fields) {
1015923
let i: int = 0;
1016924
for fld: ty::field in fields {
1017-
let {bcx: bcx, val: llfld_a} = GEP_tup_like(cx, t, av, [0, i]);
1018-
cx = f(bcx, llfld_a, fld.mt.ty);
925+
let llfld_a = GEPi(cx, av, [0, i]);
926+
cx = f(cx, llfld_a, fld.mt.ty);
1019927
i += 1;
1020928
}
1021929
}
1022930
ty::ty_tup(args) {
1023931
let i = 0;
1024932
for arg in args {
1025-
let {bcx: bcx, val: llfld_a} = GEP_tup_like(cx, t, av, [0, i]);
1026-
cx = f(bcx, llfld_a, arg);
933+
let llfld_a = GEPi(cx, av, [0, i]);
934+
cx = f(cx, llfld_a, arg);
1027935
i += 1;
1028936
}
1029937
}
1030938
ty::ty_res(_, inner, tps) {
1031939
let tcx = cx.tcx();
1032940
let inner1 = ty::substitute_type_params(tcx, tps, inner);
1033-
let inner_t_s = ty::substitute_type_params(tcx, tps, inner);
1034-
let tup_t = ty::mk_tup(tcx, [ty::mk_int(tcx), inner_t_s]);
1035-
let {bcx: bcx, val: llfld_a} = GEP_tup_like(cx, tup_t, av, [0, 1]);
1036-
ret f(bcx, llfld_a, inner1);
941+
let llfld_a = GEPi(cx, av, [0, 1]);
942+
ret f(cx, llfld_a, inner1);
1037943
}
1038944
ty::ty_enum(tid, tps) {
1039945
let variants = ty::enum_variants(cx.tcx(), tid);
@@ -1074,8 +980,8 @@ fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
1074980
// a class is like a record type
1075981
let i: int = 0;
1076982
for fld: ty::field in ty::class_items_as_fields(cx.tcx(), did) {
1077-
let {bcx: bcx, val: llfld_a} = GEP_tup_like(cx, t, av, [0, i]);
1078-
cx = f(bcx, llfld_a, fld.mt.ty);
983+
let llfld_a = GEPi(cx, av, [0, i]);
984+
cx = f(cx, llfld_a, fld.mt.ty);
1079985
i += 1;
1080986
}
1081987
}
@@ -2305,7 +2211,7 @@ fn trans_rec_field(bcx: block, base: @ast::expr,
23052211
base expr has non-record type"); }
23062212
};
23072213
let ix = option::get(ty::field_idx(field, fields));
2308-
let {bcx, val} = GEP_tup_like(bcx, ty, val, [0, ix as int]);
2214+
let val = GEPi(bcx, val, [0, ix as int]);
23092215
ret {bcx: bcx, val: val, kind: owned};
23102216
}
23112217

@@ -2837,9 +2743,7 @@ fn get_landing_pad(bcx: block) -> BasicBlockRef {
28372743
ret pad_bcx.llbb;
28382744
}
28392745

2840-
fn trans_tup(bcx: block, elts: [@ast::expr], id: ast::node_id,
2841-
dest: dest) -> block {
2842-
let t = node_id_type(bcx, id);
2746+
fn trans_tup(bcx: block, elts: [@ast::expr], dest: dest) -> block {
28432747
let bcx = bcx;
28442748
let addr = alt dest {
28452749
ignore {
@@ -2851,11 +2755,11 @@ fn trans_tup(bcx: block, elts: [@ast::expr], id: ast::node_id,
28512755
};
28522756
let temp_cleanups = [], i = 0;
28532757
for e in elts {
2854-
let dst = GEP_tup_like(bcx, t, addr, [0, i]);
2758+
let dst = GEPi(bcx, addr, [0, i]);
28552759
let e_ty = expr_ty(bcx, e);
2856-
bcx = trans_expr_save_in(dst.bcx, e, dst.val);
2857-
add_clean_temp_mem(bcx, dst.val, e_ty);
2858-
temp_cleanups += [dst.val];
2760+
bcx = trans_expr_save_in(bcx, e, dst);
2761+
add_clean_temp_mem(bcx, dst, e_ty);
2762+
temp_cleanups += [dst];
28592763
i += 1;
28602764
}
28612765
for cleanup in temp_cleanups { revoke_clean(bcx, cleanup); }
@@ -2887,10 +2791,10 @@ fn trans_rec(bcx: block, fields: [ast::field],
28872791
let ix = option::get(vec::position(ty_fields, {|ft|
28882792
str::eq(fld.node.ident, ft.ident)
28892793
}));
2890-
let dst = GEP_tup_like(bcx, t, addr, [0, ix as int]);
2891-
bcx = trans_expr_save_in(dst.bcx, fld.node.expr, dst.val);
2892-
add_clean_temp_mem(bcx, dst.val, ty_fields[ix].mt.ty);
2893-
temp_cleanups += [dst.val];
2794+
let dst = GEPi(bcx, addr, [0, ix as int]);
2795+
bcx = trans_expr_save_in(bcx, fld.node.expr, dst);
2796+
add_clean_temp_mem(bcx, dst, ty_fields[ix].mt.ty);
2797+
temp_cleanups += [dst];
28942798
}
28952799
alt base {
28962800
some(bexp) {
@@ -2899,10 +2803,10 @@ fn trans_rec(bcx: block, fields: [ast::field],
28992803
// Copy over inherited fields
29002804
for tf in ty_fields {
29012805
if !vec::any(fields, {|f| str::eq(f.node.ident, tf.ident)}) {
2902-
let dst = GEP_tup_like(bcx, t, addr, [0, i]);
2903-
let base = GEP_tup_like(bcx, t, base_val, [0, i]);
2904-
let val = load_if_immediate(base.bcx, base.val, tf.mt.ty);
2905-
bcx = copy_val(base.bcx, INIT, dst.val, val, tf.mt.ty);
2806+
let dst = GEPi(bcx, addr, [0, i]);
2807+
let base = GEPi(bcx, base_val, [0, i]);
2808+
let val = load_if_immediate(bcx, base, tf.mt.ty);
2809+
bcx = copy_val(bcx, INIT, dst, val, tf.mt.ty);
29062810
}
29072811
i += 1;
29082812
}
@@ -2997,7 +2901,7 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
29972901
ast::expr_rec(args, base) {
29982902
ret trans_rec(bcx, args, base, e.id, dest);
29992903
}
3000-
ast::expr_tup(args) { ret trans_tup(bcx, args, e.id, dest); }
2904+
ast::expr_tup(args) { ret trans_tup(bcx, args, dest); }
30012905
ast::expr_lit(lit) { ret trans_lit(bcx, *lit, dest); }
30022906
ast::expr_vec(args, _) { ret tvec::trans_vec(bcx, args, e.id, dest); }
30032907
ast::expr_binary(op, lhs, rhs) {
@@ -3974,8 +3878,6 @@ fn trans_res_ctor(ccx: @crate_ctxt, path: path, dtor: ast::fn_decl,
39743878
let bcx = top_scope_block(fcx, none), lltop = bcx.llbb;
39753879
let fty = node_id_type(bcx, ctor_id);
39763880
let arg_t = ty::ty_fn_args(fty)[0].ty;
3977-
let tup_t = ty::mk_tup(ccx.tcx, [ty::mk_mach_uint(ccx.tcx, ast::ty_u8),
3978-
arg_t]);
39793881
let arg = alt fcx.llargs.find(dtor.inputs[0].id) {
39803882
some(local_mem(x)) { x }
39813883
_ { ccx.sess.bug("Someone forgot to document an invariant \
@@ -3987,12 +3889,11 @@ fn trans_res_ctor(ccx: @crate_ctxt, path: path, dtor: ast::fn_decl,
39873889
llretptr = BitCast(bcx, llretptr, llret_t);
39883890
}
39893891

3990-
let {bcx, val: dst} = GEP_tup_like(bcx, tup_t, llretptr, [0, 1]);
3892+
let dst = GEPi(bcx, llretptr, [0, 1]);
39913893
bcx = memmove_ty(bcx, dst, arg, arg_t);
3992-
let flag = GEP_tup_like(bcx, tup_t, llretptr, [0, 0]);
3993-
bcx = flag.bcx;
3894+
let flag = GEPi(bcx, llretptr, [0, 0]);
39943895
let one = C_u8(1u);
3995-
Store(bcx, one, flag.val);
3896+
Store(bcx, one, flag);
39963897
build_return(bcx);
39973898
finish_fn(fcx, lltop);
39983899
}

0 commit comments

Comments
 (0)