Skip to content

Commit 35dd717

Browse files
committed
Simplify a bunch of trans functions to not need the rust type. Remove some PointerCasts.
1 parent ccf4e8c commit 35dd717

File tree

3 files changed

+33
-66
lines changed

3 files changed

+33
-66
lines changed

src/rustc/middle/trans/alt.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,7 @@ fn compile_submatch(bcx: block, m: match, vals: [ValueRef],
462462
// Unbox in case of a box field
463463
if any_box_pat(m, col) {
464464
let box = Load(bcx, val);
465-
let box_ty = node_id_type(bcx, pat_id);
466-
let box_no_addrspace = non_gc_box_cast(bcx, box, box_ty);
465+
let box_no_addrspace = non_gc_box_cast(bcx, box);
467466
let unboxed = GEPi(bcx, box_no_addrspace, [0u, abi::box_field_body]);
468467
compile_submatch(bcx, enter_box(dm, m, col, val), [unboxed]
469468
+ vals_left, chk, exits);
@@ -472,8 +471,7 @@ fn compile_submatch(bcx: block, m: match, vals: [ValueRef],
472471

473472
if any_uniq_pat(m, col) {
474473
let box = Load(bcx, val);
475-
let box_ty = node_id_type(bcx, pat_id);
476-
let box_no_addrspace = non_gc_box_cast(bcx, box, box_ty);
474+
let box_no_addrspace = non_gc_box_cast(bcx, box);
477475
let unboxed = GEPi(bcx, box_no_addrspace, [0u, abi::box_field_body]);
478476
compile_submatch(bcx, enter_uniq(dm, m, col, val),
479477
[unboxed] + vals_left, chk, exits);

src/rustc/middle/trans/base.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,8 @@ fn malloc_raw(bcx: block, t: ty::t, heap: heap) -> ValueRef {
381381
fn malloc_general(bcx: block, t: ty::t, heap: heap) ->
382382
{box: ValueRef, body: ValueRef} {
383383
let _icx = bcx.insn_ctxt("malloc_general");
384-
let mk_ty = alt heap { heap_shared { ty::mk_imm_box }
385-
heap_exchange { ty::mk_imm_uniq } };
386384
let box = malloc_raw(bcx, t, heap);
387-
let non_gc_box = non_gc_box_cast(bcx, box, mk_ty(bcx.tcx(), t));
385+
let non_gc_box = non_gc_box_cast(bcx, box);
388386
let body = GEPi(bcx, non_gc_box, [0u, abi::box_field_body]);
389387
ret {box: box, body: body};
390388
}
@@ -2682,11 +2680,11 @@ fn trans_lval(cx: block, e: @ast::expr) -> lval_result {
26822680
let t = expr_ty(cx, base);
26832681
let val = alt check ty::get(t).struct {
26842682
ty::ty_box(_) {
2685-
let non_gc_val = non_gc_box_cast(sub.bcx, sub.val, t);
2683+
let non_gc_val = non_gc_box_cast(sub.bcx, sub.val);
26862684
GEPi(sub.bcx, non_gc_val, [0u, abi::box_field_body])
26872685
}
26882686
ty::ty_uniq(_) {
2689-
let non_gc_val = non_gc_box_cast(sub.bcx, sub.val, t);
2687+
let non_gc_val = non_gc_box_cast(sub.bcx, sub.val);
26902688
GEPi(sub.bcx, non_gc_val, [0u, abi::box_field_body])
26912689
}
26922690
ty::ty_res(_, _, _) {
@@ -2714,10 +2712,11 @@ Before taking a pointer to the inside of a box it should be cast into address
27142712
space 0. Otherwise the resulting (non-box) pointer will be in the wrong
27152713
address space and thus be the wrong type.
27162714
"]
2717-
fn non_gc_box_cast(cx: block, val: ValueRef, t: ty::t) -> ValueRef {
2715+
fn non_gc_box_cast(cx: block, val: ValueRef) -> ValueRef {
27182716
#debug("non_gc_box_cast");
27192717
add_comment(cx, "non_gc_box_cast");
2720-
let non_gc_t = type_of_non_gc_box(cx.ccx(), t);
2718+
assert(llvm::LLVMGetPointerAddressSpace(val_ty(val)) as uint == 1u);
2719+
let non_gc_t = T_ptr(llvm::LLVMGetElementType(val_ty(val)));
27212720
PointerCast(cx, val, non_gc_t)
27222721
}
27232722

@@ -3885,11 +3884,8 @@ fn trans_fail_expr(bcx: block, sp_opt: option<span>,
38853884
bcx = expr_res.bcx;
38863885

38873886
if ty::type_is_str(e_ty) {
3888-
let unit_ty = ty::mk_mach_uint(tcx, ast::ty_u8);
3889-
let vec_ty = ty::mk_vec(tcx, {ty: unit_ty, mutbl: ast::m_imm});
3890-
let unit_llty = type_of(ccx, unit_ty);
3891-
let body = tvec::get_bodyptr(bcx, expr_res.val, vec_ty);
3892-
let data = tvec::get_dataptr(bcx, body, unit_llty);
3887+
let body = tvec::get_bodyptr(bcx, expr_res.val);
3888+
let data = tvec::get_dataptr(bcx, body);
38933889
ret trans_fail_value(bcx, sp_opt, data);
38943890
} else if bcx.unreachable || ty::type_is_bot(e_ty) {
38953891
ret bcx;

src/rustc/middle/trans/tvec.rs

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import back::abi;
55
import base::{call_memmove,
66
INIT, copy_val, load_if_immediate, get_tydesc,
77
sub_block, do_spill_noroot,
8-
dest, bcx_icx};
8+
dest, bcx_icx, non_gc_box_cast};
99
import syntax::codemap::span;
1010
import shape::llsize_of;
1111
import build::*;
1212
import common::*;
13+
import util::ppaux::ty_to_str;
1314

1415
fn get_fill(bcx: block, vptr: ValueRef) -> ValueRef {
1516
let _icx = bcx.insn_ctxt("tvec::get_fill");
@@ -22,28 +23,14 @@ fn get_alloc(bcx: block, vptr: ValueRef) -> ValueRef {
2223
Load(bcx, GEPi(bcx, vptr, [0u, abi::vec_elt_alloc]))
2324
}
2425

25-
fn get_bodyptr(bcx: block, vptr: ValueRef, vec_ty: ty::t) -> ValueRef {
26-
let ccx = bcx.ccx();
27-
alt ty::get(vec_ty).struct {
28-
ty::ty_evec(_, ty::vstore_uniq) | ty::ty_estr(ty::vstore_uniq)
29-
| ty::ty_vec(_) | ty::ty_str {
30-
let boxptr = PointerCast(bcx, vptr, T_ptr(T_box_header(ccx)));
31-
let bodyptr = GEPi(bcx, boxptr, [1u]);
32-
let unit_ty = ty::sequence_element_type(bcx.tcx(), vec_ty);
33-
let llunit_ty = type_of::type_of(ccx, unit_ty);
34-
PointerCast(bcx, bodyptr, T_ptr(T_vec(ccx, llunit_ty)))
35-
}
36-
_ {
37-
vptr
38-
}
39-
}
26+
fn get_bodyptr(bcx: block, vptr: ValueRef) -> ValueRef {
27+
non_gc_box_cast(bcx, GEPi(bcx, vptr, [0u, abi::box_field_body]))
4028
}
4129

42-
fn get_dataptr(bcx: block, vptr: ValueRef, unit_ty: TypeRef)
30+
fn get_dataptr(bcx: block, vptr: ValueRef)
4331
-> ValueRef {
4432
let _icx = bcx.insn_ctxt("tvec::get_dataptr");
45-
let ptr = GEPi(bcx, vptr, [0u, abi::vec_elt_elems]);
46-
PointerCast(bcx, ptr, T_ptr(unit_ty))
33+
GEPi(bcx, vptr, [0u, abi::vec_elt_elems, 0u])
4734
}
4835

4936
fn pointer_add(bcx: block, ptr: ValueRef, bytes: ValueRef) -> ValueRef {
@@ -83,7 +70,7 @@ fn alloc_uniq(bcx: block, unit_ty: ty::t, elts: uint) -> result {
8370
fn duplicate_uniq(bcx: block, vptr: ValueRef, vec_ty: ty::t) -> result {
8471
let _icx = bcx.insn_ctxt("tvec::duplicate_uniq");
8572
let ccx = bcx.ccx();
86-
let body_ptr = get_bodyptr(bcx, vptr, vec_ty);
73+
let body_ptr = get_bodyptr(bcx, vptr);
8774
let fill = get_fill(bcx, body_ptr);
8875
let size = Add(bcx, fill, llsize_of(ccx, ccx.opaque_vec_type));
8976

@@ -159,8 +146,7 @@ fn trans_evec(bcx: block, args: [@ast::expr],
159146
ast::vstore_uniq {
160147
let {bcx, val} = alloc_uniq(bcx, unit_ty, args.len());
161148
add_clean_free(bcx, val, true);
162-
let body = get_bodyptr(bcx, val, vec_ty);
163-
let dataptr = get_dataptr(bcx, body, llunitty);
149+
let dataptr = get_dataptr(bcx, get_bodyptr(bcx, val));
164150
{bcx: bcx, val: val, dataptr: dataptr}
165151
}
166152
ast::vstore_box {
@@ -241,10 +227,8 @@ fn get_base_and_len(cx: block, v: ValueRef, e_ty: ty::t)
241227
(base, len)
242228
}
243229
ty::vstore_uniq {
244-
let body = tvec::get_bodyptr(cx, v, vec_ty);
245-
let base = tvec::get_dataptr(cx, body, llunitty);
246-
let len = tvec::get_fill(cx, body);
247-
(base, len)
230+
let body = tvec::get_bodyptr(cx, v);
231+
(tvec::get_dataptr(cx, body), tvec::get_fill(cx, body))
248232
}
249233
ty::vstore_box {
250234
cx.ccx().sess.unimpl("unhandled tvec::get_base_and_len");
@@ -297,14 +281,11 @@ fn trans_append(bcx: block, vec_ty: ty::t, lhsptr: ValueRef,
297281
let ccx = bcx.ccx();
298282
let unit_ty = ty::sequence_element_type(ccx.tcx, vec_ty);
299283
let strings = ty::type_is_str(vec_ty);
300-
let llunitty = type_of::type_of(ccx, unit_ty);
301284

302285
let lhs = Load(bcx, lhsptr);
303286
let self_append = ICmp(bcx, lib::llvm::IntEQ, lhs, rhs);
304-
let lbody = get_bodyptr(bcx, lhs, vec_ty);
305-
let rbody = get_bodyptr(bcx, rhs, vec_ty);
306-
let lfill = get_fill(bcx, lbody);
307-
let rfill = get_fill(bcx, rbody);
287+
let lfill = get_fill(bcx, get_bodyptr(bcx, lhs));
288+
let rfill = get_fill(bcx, get_bodyptr(bcx, rhs));
308289
let mut new_fill = Add(bcx, lfill, rfill);
309290
if strings { new_fill = Sub(bcx, new_fill, C_int(ccx, 1)); }
310291
let opaque_lhs = PointerCast(bcx, lhsptr,
@@ -315,9 +296,9 @@ fn trans_append(bcx: block, vec_ty: ty::t, lhsptr: ValueRef,
315296
let lhs = Load(bcx, lhsptr);
316297
let rhs = Select(bcx, self_append, lhs, rhs);
317298

318-
let lbody = get_bodyptr(bcx, lhs, vec_ty);
299+
let lbody = get_bodyptr(bcx, lhs);
319300

320-
let lhs_data = get_dataptr(bcx, lbody, llunitty);
301+
let lhs_data = get_dataptr(bcx, lbody);
321302
let mut lhs_off = lfill;
322303
if strings { lhs_off = Sub(bcx, lhs_off, C_int(ccx, 1)); }
323304
let write_ptr = pointer_add(bcx, lhs_data, lhs_off);
@@ -342,7 +323,7 @@ fn trans_append_literal(bcx: block, vptrptr: ValueRef, vec_ty: ty::t,
342323
let scratch = base::alloca(bcx, elt_llty);
343324
for vec::each(vals) {|val|
344325
bcx = base::trans_expr_save_in(bcx, val, scratch);
345-
let vptr = get_bodyptr(bcx, Load(bcx, vptrptr), vec_ty);
326+
let vptr = get_bodyptr(bcx, Load(bcx, vptrptr));
346327
let old_fill = get_fill(bcx, vptr);
347328
let new_fill = Add(bcx, old_fill, elt_sz);
348329
let do_grow = ICmp(bcx, lib::llvm::IntUGT, new_fill,
@@ -353,9 +334,9 @@ fn trans_append_literal(bcx: block, vptrptr: ValueRef, vec_ty: ty::t,
353334
Call(bcx, ccx.upcalls.vec_grow, [pt, new_fill]);
354335
bcx
355336
};
356-
let vptr = get_bodyptr(bcx, Load(bcx, vptrptr), vec_ty);
337+
let vptr = get_bodyptr(bcx, Load(bcx, vptrptr));
357338
set_fill(bcx, vptr, new_fill);
358-
let targetptr = pointer_add(bcx, get_dataptr(bcx, vptr, elt_llty),
339+
let targetptr = pointer_add(bcx, get_dataptr(bcx, vptr),
359340
old_fill);
360341
call_memmove(bcx, targetptr, scratch, elt_sz);
361342
}
@@ -379,18 +360,15 @@ fn trans_add(bcx: block, vec_ty: ty::t, lhs: ValueRef,
379360
ret base::store_in_dest(bcx, n, dest);
380361
}
381362

382-
let lhs_body = get_bodyptr(bcx, lhs, vec_ty);
383-
let rhs_body = get_bodyptr(bcx, rhs, vec_ty);
384-
385-
let lhs_fill = get_fill(bcx, lhs_body);
386-
let rhs_fill = get_fill(bcx, rhs_body);
363+
let lhs_fill = get_fill(bcx, get_bodyptr(bcx, lhs));
364+
let rhs_fill = get_fill(bcx, get_bodyptr(bcx, rhs));
387365
let new_fill = Add(bcx, lhs_fill, rhs_fill);
388366
let mut {bcx: bcx, val: new_vec_ptr} =
389367
alloc_uniq_raw(bcx, unit_ty, new_fill, new_fill);
390368

391-
let new_vec_body_ptr = get_bodyptr(bcx, new_vec_ptr, vec_ty);
369+
let new_vec_body_ptr = get_bodyptr(bcx, new_vec_ptr);
392370
let write_ptr_ptr = do_spill_noroot
393-
(bcx, get_dataptr(bcx, new_vec_body_ptr, llunitty));
371+
(bcx, get_dataptr(bcx, new_vec_body_ptr));
394372
let copy_fn = fn@(bcx: block, addr: ValueRef,
395373
_ty: ty::t) -> block {
396374
let ccx = bcx.ccx();
@@ -443,19 +421,14 @@ fn iter_vec_raw(bcx: block, data_ptr: ValueRef, vec_ty: ty::t,
443421
fn iter_vec_uniq(bcx: block, vptr: ValueRef, vec_ty: ty::t,
444422
fill: ValueRef, f: iter_vec_block) -> block {
445423
let _icx = bcx.insn_ctxt("tvec::iter_vec_uniq");
446-
let ccx = bcx.ccx();
447-
let unit_ty = ty::sequence_element_type(bcx.tcx(), vec_ty);
448-
let llunitty = type_of::type_of(ccx, unit_ty);
449-
let body_ptr = get_bodyptr(bcx, vptr, vec_ty);
450-
let data_ptr = get_dataptr(bcx, body_ptr, llunitty);
424+
let data_ptr = get_dataptr(bcx, get_bodyptr(bcx, vptr));
451425
iter_vec_raw(bcx, data_ptr, vec_ty, fill, f)
452426
}
453427

454428
fn iter_vec(bcx: block, vptr: ValueRef, vec_ty: ty::t,
455429
f: iter_vec_block) -> block {
456430
let _icx = bcx.insn_ctxt("tvec::iter_vec");
457-
let body_ptr = get_bodyptr(bcx, vptr, vec_ty);
458-
let fill = get_fill(bcx, body_ptr);
431+
let fill = get_fill(bcx, get_bodyptr(bcx, vptr));
459432
ret iter_vec_uniq(bcx, vptr, vec_ty, fill, f);
460433
}
461434

0 commit comments

Comments
 (0)