2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2012-09-04 13:54:36 -05:00
|
|
|
use back::abi;
|
2013-02-25 13:11:21 -06:00
|
|
|
use lib;
|
|
|
|
use lib::llvm::{llvm, ValueRef, TypeRef};
|
|
|
|
use middle::trans::base;
|
|
|
|
use middle::trans::base::*;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::build::*;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::callee;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::common::*;
|
|
|
|
use middle::trans::datum::*;
|
|
|
|
use middle::trans::expr::{Dest, Ignore, SaveIn};
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::expr;
|
|
|
|
use middle::trans::glue;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::machine::{llsize_of, nonzero_llsize_of};
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::type_of;
|
|
|
|
use middle::ty;
|
2012-12-13 15:05:22 -06:00
|
|
|
use util::common::indenter;
|
2012-09-04 13:54:36 -05:00
|
|
|
use util::ppaux::ty_to_str;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2013-03-24 02:59:44 -05:00
|
|
|
use core::option::None;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::ast;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::codemap;
|
2011-08-22 13:48:00 -05:00
|
|
|
|
2012-06-14 19:34:06 -05:00
|
|
|
// Boxed vector types are in some sense currently a "shorthand" for a box
|
|
|
|
// containing an unboxed vector. This expands a boxed vector type into such an
|
|
|
|
// expanded type. It doesn't respect mutability, but that doesn't matter at
|
|
|
|
// this point.
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn expand_boxed_vec_ty(tcx: ty::ctxt, t: ty::t) -> ty::t {
|
2012-06-14 19:34:06 -05:00
|
|
|
let unit_ty = ty::sequence_element_type(tcx, t);
|
|
|
|
let unboxed_vec_ty = ty::mk_mut_unboxed_vec(tcx, unit_ty);
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(t).sty {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_estr(ty::vstore_uniq) | ty::ty_evec(_, ty::vstore_uniq) => {
|
2012-06-14 19:34:06 -05:00
|
|
|
ty::mk_imm_uniq(tcx, unboxed_vec_ty)
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_estr(ty::vstore_box) | ty::ty_evec(_, ty::vstore_box) => {
|
2012-06-14 19:34:06 -05:00
|
|
|
ty::mk_imm_box(tcx, unboxed_vec_ty)
|
|
|
|
}
|
2013-05-19 00:07:44 -05:00
|
|
|
_ => tcx.sess.bug("non boxed-vec type \
|
|
|
|
in tvec::expand_boxed_vec_ty")
|
2012-06-14 19:34:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn get_fill(bcx: block, vptr: ValueRef) -> ValueRef {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::get_fill");
|
2012-08-27 14:16:37 -05:00
|
|
|
Load(bcx, GEPi(bcx, vptr, [0u, abi::vec_elt_fill]))
|
2011-08-25 03:18:02 -05:00
|
|
|
}
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn set_fill(bcx: block, vptr: ValueRef, fill: ValueRef) {
|
2012-08-27 14:16:37 -05:00
|
|
|
Store(bcx, fill, GEPi(bcx, vptr, [0u, abi::vec_elt_fill]));
|
2012-03-22 10:02:34 -05:00
|
|
|
}
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn get_alloc(bcx: block, vptr: ValueRef) -> ValueRef {
|
2012-08-27 14:16:37 -05:00
|
|
|
Load(bcx, GEPi(bcx, vptr, [0u, abi::vec_elt_alloc]))
|
2012-03-22 10:02:34 -05:00
|
|
|
}
|
2012-05-21 20:36:52 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn get_bodyptr(bcx: block, vptr: ValueRef) -> ValueRef {
|
2012-08-28 17:54:45 -05:00
|
|
|
base::non_gc_box_cast(bcx, GEPi(bcx, vptr, [0u, abi::box_field_body]))
|
2012-05-21 20:36:52 -05:00
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn get_dataptr(bcx: block, vptr: ValueRef) -> ValueRef {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::get_dataptr");
|
2012-08-27 14:16:37 -05:00
|
|
|
GEPi(bcx, vptr, [0u, abi::vec_elt_elems, 0u])
|
2011-08-25 03:18:02 -05:00
|
|
|
}
|
2011-08-22 13:48:00 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn pointer_add(bcx: block, ptr: ValueRef, bytes: ValueRef) -> ValueRef {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::pointer_add");
|
2011-08-25 03:18:02 -05:00
|
|
|
let old_ty = val_ty(ptr);
|
|
|
|
let bptr = PointerCast(bcx, ptr, T_ptr(T_i8()));
|
2013-05-19 00:07:44 -05:00
|
|
|
return PointerCast(bcx, InBoundsGEP(bcx, bptr, [bytes]), old_ty);
|
2011-08-25 03:18:02 -05:00
|
|
|
}
|
2011-08-22 13:48:00 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn alloc_raw(bcx: block, unit_ty: ty::t,
|
|
|
|
fill: ValueRef, alloc: ValueRef, heap: heap) -> Result {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::alloc_uniq");
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx();
|
2012-06-12 16:54:02 -05:00
|
|
|
|
|
|
|
let vecbodyty = ty::mk_mut_unboxed_vec(bcx.tcx(), unit_ty);
|
|
|
|
let vecsize = Add(bcx, alloc, llsize_of(ccx, ccx.opaque_vec_type));
|
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
let base::MallocResult {bcx, box: bx, body} =
|
2012-07-17 12:48:19 -05:00
|
|
|
base::malloc_general_dyn(bcx, vecbodyty, heap, vecsize);
|
2012-08-27 14:16:37 -05:00
|
|
|
Store(bcx, fill, GEPi(bcx, body, [0u, abi::vec_elt_fill]));
|
|
|
|
Store(bcx, alloc, GEPi(bcx, body, [0u, abi::vec_elt_alloc]));
|
2013-02-20 17:02:21 -06:00
|
|
|
base::maybe_set_managed_unique_rc(bcx, bx, heap);
|
2013-01-30 15:44:24 -06:00
|
|
|
return rslt(bcx, bx);
|
2011-08-29 15:30:18 -05:00
|
|
|
}
|
2013-02-20 17:02:21 -06:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn alloc_uniq_raw(bcx: block, unit_ty: ty::t,
|
|
|
|
fill: ValueRef, alloc: ValueRef) -> Result {
|
2013-02-25 13:11:21 -06:00
|
|
|
alloc_raw(bcx, unit_ty, fill, alloc, base::heap_for_unique(bcx, unit_ty))
|
2012-06-14 19:34:06 -05:00
|
|
|
}
|
2011-08-29 15:30:18 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn alloc_vec(bcx: block,
|
|
|
|
unit_ty: ty::t,
|
|
|
|
elts: uint,
|
|
|
|
heap: heap)
|
|
|
|
-> Result {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::alloc_uniq");
|
2012-02-21 07:20:18 -06:00
|
|
|
let ccx = bcx.ccx();
|
2012-05-21 20:36:52 -05:00
|
|
|
let llunitty = type_of::type_of(ccx, unit_ty);
|
2013-01-08 02:24:43 -06:00
|
|
|
let unit_sz = nonzero_llsize_of(ccx, llunitty);
|
2011-08-25 03:18:02 -05:00
|
|
|
|
2011-10-14 22:38:24 -05:00
|
|
|
let fill = Mul(bcx, C_uint(ccx, elts), unit_sz);
|
2012-03-12 04:05:15 -05:00
|
|
|
let alloc = if elts < 4u { Mul(bcx, C_int(ccx, 4), unit_sz) }
|
|
|
|
else { fill };
|
2012-08-28 17:54:45 -05:00
|
|
|
let Result {bcx: bcx, val: vptr} =
|
|
|
|
alloc_raw(bcx, unit_ty, fill, alloc, heap);
|
|
|
|
return rslt(bcx, vptr);
|
2011-08-25 03:18:02 -05:00
|
|
|
}
|
2011-08-29 15:30:18 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn duplicate_uniq(bcx: block, vptr: ValueRef, vec_ty: ty::t) -> Result {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::duplicate_uniq");
|
2012-05-21 20:36:52 -05:00
|
|
|
|
2012-06-13 12:46:33 -05:00
|
|
|
let fill = get_fill(bcx, get_bodyptr(bcx, vptr));
|
2012-02-21 07:20:18 -06:00
|
|
|
let unit_ty = ty::sequence_element_type(bcx.tcx(), vec_ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
let Result {bcx, val: newptr} = alloc_uniq_raw(bcx, unit_ty, fill, fill);
|
2012-06-13 12:46:33 -05:00
|
|
|
|
|
|
|
let data_ptr = get_dataptr(bcx, get_bodyptr(bcx, vptr));
|
|
|
|
let new_data_ptr = get_dataptr(bcx, get_bodyptr(bcx, newptr));
|
2012-11-16 13:26:26 -06:00
|
|
|
base::call_memcpy(bcx, new_data_ptr, data_ptr, fill);
|
2012-05-21 20:36:52 -05:00
|
|
|
|
2012-03-23 08:45:47 -05:00
|
|
|
let bcx = if ty::type_needs_drop(bcx.tcx(), unit_ty) {
|
2012-08-28 17:54:45 -05:00
|
|
|
iter_vec_raw(bcx, new_data_ptr, vec_ty, fill, glue::take_ty)
|
2012-03-23 08:45:47 -05:00
|
|
|
} else { bcx };
|
2012-08-01 19:30:05 -05:00
|
|
|
return rslt(bcx, newptr);
|
2011-08-25 03:18:02 -05:00
|
|
|
}
|
2012-06-14 19:34:06 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn make_drop_glue_unboxed(bcx: block, vptr: ValueRef, vec_ty: ty::t) ->
|
2012-02-17 06:17:40 -06:00
|
|
|
block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::make_drop_glue_unboxed");
|
2012-02-21 07:20:18 -06:00
|
|
|
let tcx = bcx.tcx(), unit_ty = ty::sequence_element_type(tcx, vec_ty);
|
2012-06-14 19:34:06 -05:00
|
|
|
if ty::type_needs_drop(tcx, unit_ty) {
|
2012-08-28 17:54:45 -05:00
|
|
|
iter_vec_unboxed(bcx, vptr, vec_ty, glue::drop_ty)
|
2012-06-14 19:34:06 -05:00
|
|
|
} else { bcx }
|
2011-08-22 15:30:53 -05:00
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub struct VecTypes {
|
2012-09-07 16:50:47 -05:00
|
|
|
vec_ty: ty::t,
|
|
|
|
unit_ty: ty::t,
|
|
|
|
llunit_ty: TypeRef,
|
|
|
|
llunit_size: ValueRef
|
2012-08-03 20:48:17 -05:00
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub impl VecTypes {
|
2013-02-22 00:41:37 -06:00
|
|
|
fn to_str(&self, ccx: @CrateContext) -> ~str {
|
2012-08-28 17:54:45 -05:00
|
|
|
fmt!("VecTypes {vec_ty=%s, unit_ty=%s, llunit_ty=%s, llunit_size=%s}",
|
|
|
|
ty_to_str(ccx.tcx, self.vec_ty),
|
|
|
|
ty_to_str(ccx.tcx, self.unit_ty),
|
|
|
|
ty_str(ccx.tn, self.llunit_ty),
|
|
|
|
val_str(ccx.tn, self.llunit_size))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn trans_fixed_vstore(bcx: block,
|
|
|
|
vstore_expr: @ast::expr,
|
|
|
|
content_expr: @ast::expr,
|
|
|
|
dest: expr::Dest)
|
|
|
|
-> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
//!
|
|
|
|
//
|
2012-10-09 23:28:04 -05:00
|
|
|
// [...] allocates a fixed-size array and moves it around "by value".
|
2012-08-28 17:54:45 -05:00
|
|
|
// In this case, it means that the caller has already given us a location
|
|
|
|
// to store the array of the suitable size, so all we have to do is
|
|
|
|
// generate the content.
|
|
|
|
|
|
|
|
debug!("trans_fixed_vstore(vstore_expr=%s, dest=%?)",
|
|
|
|
bcx.expr_to_str(vstore_expr), dest.to_str(bcx.ccx()));
|
|
|
|
let _indenter = indenter();
|
|
|
|
|
|
|
|
let vt = vec_types_from_expr(bcx, vstore_expr);
|
|
|
|
|
|
|
|
return match dest {
|
|
|
|
Ignore => write_content(bcx, &vt, vstore_expr, content_expr, dest),
|
|
|
|
SaveIn(lldest) => {
|
|
|
|
// lldest will have type *[T x N], but we want the type *T,
|
|
|
|
// so use GEP to convert:
|
|
|
|
let lldest = GEPi(bcx, lldest, [0, 0]);
|
|
|
|
write_content(bcx, &vt, vstore_expr, content_expr, SaveIn(lldest))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn trans_slice_vstore(bcx: block,
|
|
|
|
vstore_expr: @ast::expr,
|
|
|
|
content_expr: @ast::expr,
|
|
|
|
dest: expr::Dest)
|
|
|
|
-> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
//!
|
|
|
|
//
|
|
|
|
// &[...] allocates memory on the stack and writes the values into it,
|
|
|
|
// returning a slice (pair of ptr, len). &"..." is similar except that
|
|
|
|
// the memory can be statically allocated.
|
|
|
|
|
2012-03-15 08:47:03 -05:00
|
|
|
let ccx = bcx.ccx();
|
2012-08-03 20:48:17 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
debug!("trans_slice_vstore(vstore_expr=%s, dest=%s)",
|
|
|
|
bcx.expr_to_str(vstore_expr), dest.to_str(ccx));
|
|
|
|
let _indenter = indenter();
|
|
|
|
|
|
|
|
// Handle the &"..." case:
|
|
|
|
match content_expr.node {
|
2013-01-30 11:56:33 -06:00
|
|
|
ast::expr_lit(@codemap::spanned {node: ast::lit_str(s), span: _}) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return trans_lit_str(bcx, content_expr, s, dest);
|
2011-09-27 03:50:18 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {}
|
2011-09-27 03:50:18 -05:00
|
|
|
}
|
2012-04-19 17:42:02 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// Handle the &[...] case:
|
|
|
|
let vt = vec_types_from_expr(bcx, vstore_expr);
|
|
|
|
let count = elements_required(bcx, content_expr);
|
|
|
|
debug!("vt=%s, count=%?", vt.to_str(ccx), count);
|
|
|
|
|
|
|
|
// Make a fixed-length backing array and allocate it on the stack.
|
|
|
|
let llcount = C_uint(ccx, count);
|
|
|
|
let llfixed = base::arrayalloca(bcx, vt.llunit_ty, llcount);
|
|
|
|
|
|
|
|
// Arrange for the backing array to be cleaned up.
|
|
|
|
let fixed_ty = ty::mk_evec(bcx.tcx(),
|
2013-01-14 23:36:27 -06:00
|
|
|
ty::mt {ty: vt.unit_ty, mutbl: ast::m_mutbl},
|
2012-08-28 17:54:45 -05:00
|
|
|
ty::vstore_fixed(count));
|
|
|
|
let llfixed_ty = T_ptr(type_of::type_of(bcx.ccx(), fixed_ty));
|
|
|
|
let llfixed_casted = BitCast(bcx, llfixed, llfixed_ty);
|
|
|
|
add_clean(bcx, llfixed_casted, fixed_ty);
|
|
|
|
|
|
|
|
// Generate the content into the backing array.
|
|
|
|
let bcx = write_content(bcx, &vt, vstore_expr,
|
|
|
|
content_expr, SaveIn(llfixed));
|
|
|
|
|
|
|
|
// Finally, create the slice pair itself.
|
|
|
|
match dest {
|
|
|
|
Ignore => {}
|
|
|
|
SaveIn(lldest) => {
|
|
|
|
Store(bcx, llfixed, GEPi(bcx, lldest, [0u, abi::slice_elt_base]));
|
|
|
|
let lllen = Mul(bcx, llcount, vt.llunit_size);
|
|
|
|
Store(bcx, lllen, GEPi(bcx, lldest, [0u, abi::slice_elt_len]));
|
|
|
|
}
|
2012-08-03 20:48:17 -05:00
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn trans_lit_str(bcx: block,
|
|
|
|
lit_expr: @ast::expr,
|
2013-01-30 15:44:24 -06:00
|
|
|
str_lit: @~str,
|
2013-01-30 13:46:19 -06:00
|
|
|
dest: Dest)
|
|
|
|
-> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
//!
|
|
|
|
//
|
|
|
|
// Literal strings translate to slices into static memory. This is
|
|
|
|
// different from trans_slice_vstore() above because it does need to copy
|
|
|
|
// the content anywhere.
|
|
|
|
|
|
|
|
debug!("trans_lit_str(lit_expr=%s, dest=%s)",
|
|
|
|
bcx.expr_to_str(lit_expr),
|
|
|
|
dest.to_str(bcx.ccx()));
|
|
|
|
let _indenter = indenter();
|
|
|
|
|
|
|
|
match dest {
|
|
|
|
Ignore => bcx,
|
|
|
|
SaveIn(lldest) => {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-01-30 15:44:24 -06:00
|
|
|
let bytes = str_lit.len() + 1; // count null-terminator too
|
2013-01-10 23:23:07 -06:00
|
|
|
let llbytes = C_uint(bcx.ccx(), bytes);
|
2013-02-10 18:33:16 -06:00
|
|
|
let llcstr = C_cstr(bcx.ccx(), str_lit);
|
2013-01-10 23:23:07 -06:00
|
|
|
let llcstr = llvm::LLVMConstPointerCast(llcstr,
|
|
|
|
T_ptr(T_i8()));
|
|
|
|
Store(bcx,
|
|
|
|
llcstr,
|
|
|
|
GEPi(bcx, lldest, [0u, abi::slice_elt_base]));
|
|
|
|
Store(bcx,
|
|
|
|
llbytes,
|
|
|
|
GEPi(bcx, lldest, [0u, abi::slice_elt_len]));
|
|
|
|
bcx
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn trans_uniq_or_managed_vstore(bcx: block,
|
|
|
|
heap: heap,
|
|
|
|
vstore_expr: @ast::expr,
|
|
|
|
content_expr: @ast::expr) -> DatumBlock {
|
2012-08-28 17:54:45 -05:00
|
|
|
//!
|
|
|
|
//
|
|
|
|
// @[...] or ~[...] (also @"..." or ~"...") allocate boxes in the
|
|
|
|
// appropriate heap and write the array elements into them.
|
|
|
|
|
|
|
|
debug!("trans_uniq_or_managed_vstore(vstore_expr=%s, heap=%?)",
|
|
|
|
bcx.expr_to_str(vstore_expr), heap);
|
|
|
|
let _indenter = indenter();
|
|
|
|
|
2013-01-29 21:59:52 -06:00
|
|
|
// Handle ~"".
|
|
|
|
match heap {
|
|
|
|
heap_exchange => {
|
|
|
|
match content_expr.node {
|
2013-01-30 11:56:33 -06:00
|
|
|
ast::expr_lit(@codemap::spanned {
|
2013-01-29 21:59:52 -06:00
|
|
|
node: ast::lit_str(s), _
|
|
|
|
}) => {
|
2013-02-10 18:33:16 -06:00
|
|
|
let llptrval = C_cstr(bcx.ccx(), s);
|
2013-01-29 21:59:52 -06:00
|
|
|
let llptrval = PointerCast(bcx, llptrval, T_ptr(T_i8()));
|
|
|
|
let llsizeval = C_uint(bcx.ccx(), s.len());
|
|
|
|
let typ = ty::mk_estr(bcx.tcx(), ty::vstore_uniq);
|
2013-02-25 13:11:21 -06:00
|
|
|
let lldestval = scratch_datum(bcx, typ, false);
|
2013-02-27 20:34:04 -06:00
|
|
|
let bcx = callee::trans_lang_call(
|
2013-01-29 21:59:52 -06:00
|
|
|
bcx,
|
|
|
|
bcx.tcx().lang_items.strdup_uniq_fn(),
|
2013-05-19 00:07:44 -05:00
|
|
|
[ llptrval, llsizeval ],
|
2013-01-29 21:59:52 -06:00
|
|
|
expr::SaveIn(lldestval.to_ref_llval(bcx)));
|
2013-02-25 13:11:21 -06:00
|
|
|
return DatumBlock {
|
2013-01-29 21:59:52 -06:00
|
|
|
bcx: bcx,
|
|
|
|
datum: lldestval
|
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2013-02-20 17:02:21 -06:00
|
|
|
heap_managed | heap_managed_unique => {}
|
2013-01-29 21:59:52 -06:00
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let vt = vec_types_from_expr(bcx, vstore_expr);
|
|
|
|
let count = elements_required(bcx, content_expr);
|
|
|
|
|
|
|
|
let Result {bcx, val} = alloc_vec(bcx, vt.unit_ty, count, heap);
|
2013-02-20 17:02:21 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
add_clean_free(bcx, val, heap);
|
|
|
|
let dataptr = get_dataptr(bcx, get_bodyptr(bcx, val));
|
|
|
|
|
|
|
|
debug!("alloc_vec() returned val=%s, dataptr=%s",
|
|
|
|
bcx.val_str(val), bcx.val_str(dataptr));
|
|
|
|
|
|
|
|
let bcx = write_content(bcx, &vt, vstore_expr,
|
|
|
|
content_expr, SaveIn(dataptr));
|
|
|
|
|
|
|
|
revoke_clean(bcx, val);
|
2012-04-19 17:42:02 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
return immediate_rvalue_bcx(bcx, val, vt.vec_ty);
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn write_content(bcx: block,
|
|
|
|
vt: &VecTypes,
|
|
|
|
vstore_expr: @ast::expr,
|
|
|
|
content_expr: @ast::expr,
|
|
|
|
dest: Dest)
|
|
|
|
-> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::write_content");
|
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
debug!("write_content(vt=%s, dest=%s, vstore_expr=%?)",
|
|
|
|
vt.to_str(bcx.ccx()),
|
|
|
|
dest.to_str(bcx.ccx()),
|
|
|
|
bcx.expr_to_str(vstore_expr));
|
|
|
|
let _indenter = indenter();
|
|
|
|
|
2013-03-20 00:17:42 -05:00
|
|
|
match content_expr.node {
|
2013-01-30 11:56:33 -06:00
|
|
|
ast::expr_lit(@codemap::spanned { node: ast::lit_str(s), _ }) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
match dest {
|
|
|
|
Ignore => {
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
SaveIn(lldest) => {
|
|
|
|
let bytes = s.len() + 1; // copy null-terminator too
|
|
|
|
let llbytes = C_uint(bcx.ccx(), bytes);
|
2013-02-10 18:33:16 -06:00
|
|
|
let llcstr = C_cstr(bcx.ccx(), s);
|
2012-11-16 13:26:26 -06:00
|
|
|
base::call_memcpy(bcx, lldest, llcstr, llbytes);
|
2012-08-28 17:54:45 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
2012-08-03 20:48:17 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-20 00:17:42 -05:00
|
|
|
ast::expr_vec(ref elements, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
match dest {
|
|
|
|
Ignore => {
|
|
|
|
for elements.each |element| {
|
2012-09-19 18:55:01 -05:00
|
|
|
bcx = expr::trans_into(bcx, *element, Ignore);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveIn(lldest) => {
|
|
|
|
let mut temp_cleanups = ~[];
|
|
|
|
for elements.eachi |i, element| {
|
|
|
|
let lleltptr = GEPi(bcx, lldest, [i]);
|
|
|
|
debug!("writing index %? with lleltptr=%?",
|
|
|
|
i, bcx.val_str(lleltptr));
|
2012-09-21 20:43:30 -05:00
|
|
|
bcx = expr::trans_into(bcx, *element,
|
2012-08-28 17:54:45 -05:00
|
|
|
SaveIn(lleltptr));
|
|
|
|
add_clean_temp_mem(bcx, lleltptr, vt.unit_ty);
|
2012-09-26 19:33:34 -05:00
|
|
|
temp_cleanups.push(lleltptr);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-05-08 05:48:26 -05:00
|
|
|
for temp_cleanups.each |cleanup| {
|
2012-09-18 23:41:37 -05:00
|
|
|
revoke_clean(bcx, *cleanup);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-08-03 20:48:17 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
ast::expr_repeat(element, count_expr, _) => {
|
|
|
|
match dest {
|
|
|
|
Ignore => {
|
|
|
|
return expr::trans_into(bcx, element, Ignore);
|
|
|
|
}
|
|
|
|
SaveIn(lldest) => {
|
2013-03-02 02:43:24 -06:00
|
|
|
let count = ty::eval_repeat_count(bcx.tcx(), count_expr);
|
2012-08-28 17:54:45 -05:00
|
|
|
if count == 0 {
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2013-03-25 17:46:10 -05:00
|
|
|
// Some cleanup would be required in the case in which failure happens
|
|
|
|
// during a copy. But given that copy constructors are not overridable,
|
|
|
|
// this can only happen as a result of OOM. So we just skip out on the
|
|
|
|
// cleanup since things would *probably* be broken at that point anyways.
|
|
|
|
|
2013-03-24 02:59:44 -05:00
|
|
|
let elem = unpack_datum!(bcx, {
|
2012-08-28 17:54:45 -05:00
|
|
|
expr::trans_to_datum(bcx, element)
|
|
|
|
});
|
|
|
|
|
2013-05-02 03:16:07 -05:00
|
|
|
let next_bcx = sub_block(bcx, "expr_repeat: while next");
|
|
|
|
let loop_bcx = loop_scope_block(bcx, next_bcx, None, "expr_repeat", None);
|
|
|
|
let cond_bcx = scope_block(loop_bcx, None, "expr_repeat: loop cond");
|
|
|
|
let set_bcx = scope_block(loop_bcx, None, "expr_repeat: body: set");
|
|
|
|
let inc_bcx = scope_block(loop_bcx, None, "expr_repeat: body: inc");
|
2013-03-24 02:59:44 -05:00
|
|
|
Br(bcx, loop_bcx.llbb);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-03-24 02:59:44 -05:00
|
|
|
let loop_counter = {
|
|
|
|
// i = 0
|
2013-03-24 05:44:47 -05:00
|
|
|
let i = alloca(loop_bcx, bcx.ccx().int_type);
|
|
|
|
Store(loop_bcx, C_uint(bcx.ccx(), 0), i);
|
2013-03-24 02:59:44 -05:00
|
|
|
|
|
|
|
Br(loop_bcx, cond_bcx.llbb);
|
|
|
|
i
|
|
|
|
};
|
|
|
|
|
|
|
|
{ // i < count
|
|
|
|
let lhs = Load(cond_bcx, loop_counter);
|
2013-03-24 05:44:47 -05:00
|
|
|
let rhs = C_uint(bcx.ccx(), count);
|
|
|
|
let cond_val = ICmp(cond_bcx, lib::llvm::IntULT, lhs, rhs);
|
2013-03-24 02:59:44 -05:00
|
|
|
|
|
|
|
CondBr(cond_bcx, cond_val, set_bcx.llbb, next_bcx.llbb);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-03-24 02:59:44 -05:00
|
|
|
{ // v[i] = elem
|
|
|
|
let i = Load(set_bcx, loop_counter);
|
|
|
|
let lleltptr = InBoundsGEP(set_bcx, lldest, [i]);
|
|
|
|
let set_bcx = elem.copy_to(set_bcx, INIT, lleltptr);
|
|
|
|
|
|
|
|
Br(set_bcx, inc_bcx.llbb);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-03-24 02:59:44 -05:00
|
|
|
{ // i += 1
|
|
|
|
let i = Load(inc_bcx, loop_counter);
|
2013-03-24 05:44:47 -05:00
|
|
|
let plusone = Add(inc_bcx, i, C_uint(bcx.ccx(), 1));
|
2013-03-24 02:59:44 -05:00
|
|
|
Store(inc_bcx, plusone, loop_counter);
|
|
|
|
|
|
|
|
Br(inc_bcx, cond_bcx.llbb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return next_bcx;
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(content_expr.span,
|
2013-05-02 11:28:53 -05:00
|
|
|
"Unexpected evec content");
|
2012-08-03 20:48:17 -05:00
|
|
|
}
|
2011-08-22 13:48:00 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-04-19 17:42:02 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn vec_types_from_expr(bcx: block, vec_expr: @ast::expr) -> VecTypes {
|
2012-08-28 17:54:45 -05:00
|
|
|
let vec_ty = node_id_type(bcx, vec_expr.id);
|
|
|
|
vec_types(bcx, vec_ty)
|
|
|
|
}
|
2012-04-19 17:42:02 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn vec_types(bcx: block, vec_ty: ty::t) -> VecTypes {
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let unit_ty = ty::sequence_element_type(bcx.tcx(), vec_ty);
|
|
|
|
let llunit_ty = type_of::type_of(ccx, unit_ty);
|
2013-01-08 02:24:43 -06:00
|
|
|
let llunit_size = nonzero_llsize_of(ccx, llunit_ty);
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
VecTypes {vec_ty: vec_ty,
|
|
|
|
unit_ty: unit_ty,
|
|
|
|
llunit_ty: llunit_ty,
|
|
|
|
llunit_size: llunit_size}
|
2011-08-22 13:48:00 -05:00
|
|
|
}
|
2011-09-27 13:21:44 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn elements_required(bcx: block, content_expr: @ast::expr) -> uint {
|
2012-08-28 17:54:45 -05:00
|
|
|
//! Figure out the number of elements we need to store this content
|
|
|
|
|
2013-03-20 00:17:42 -05:00
|
|
|
match content_expr.node {
|
2013-01-30 11:56:33 -06:00
|
|
|
ast::expr_lit(@codemap::spanned { node: ast::lit_str(s), _ }) => {
|
2012-12-27 13:36:00 -06:00
|
|
|
s.len() + 1
|
|
|
|
},
|
2013-03-20 00:17:42 -05:00
|
|
|
ast::expr_vec(ref es, _) => es.len(),
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::expr_repeat(_, count_expr, _) => {
|
2013-03-02 02:43:24 -06:00
|
|
|
ty::eval_repeat_count(bcx.tcx(), count_expr)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
_ => bcx.tcx().sess.span_bug(content_expr.span,
|
2013-05-02 11:28:53 -05:00
|
|
|
"Unexpected evec content")
|
2012-04-09 19:32:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn get_base_and_len(bcx: block,
|
|
|
|
llval: ValueRef,
|
|
|
|
vec_ty: ty::t) -> (ValueRef, ValueRef) {
|
2012-08-28 17:54:45 -05:00
|
|
|
//!
|
|
|
|
//
|
|
|
|
// Converts a vector into the slice pair. The vector should be stored in
|
|
|
|
// `llval` which should be either immediate or by-ref as appropriate for
|
|
|
|
// the vector type. If you have a datum, you would probably prefer to
|
|
|
|
// call `Datum::get_base_and_len()` which will handle any conversions for
|
|
|
|
// you.
|
2012-04-17 17:07:38 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let vt = vec_types(bcx, vec_ty);
|
2012-04-19 17:42:02 -05:00
|
|
|
|
2012-09-11 18:20:31 -05:00
|
|
|
let vstore = match ty::get(vt.vec_ty).sty {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_estr(vst) | ty::ty_evec(_, vst) => vst,
|
|
|
|
_ => ty::vstore_uniq
|
2012-04-17 17:07:38 -05:00
|
|
|
};
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match vstore {
|
2012-08-28 17:54:45 -05:00
|
|
|
ty::vstore_fixed(n) => {
|
|
|
|
let base = GEPi(bcx, llval, [0u, 0u]);
|
|
|
|
let n = if ty::type_is_str(vec_ty) { n + 1u } else { n };
|
|
|
|
let len = Mul(bcx, C_uint(ccx, n), vt.llunit_size);
|
|
|
|
(base, len)
|
|
|
|
}
|
|
|
|
ty::vstore_slice(_) => {
|
|
|
|
let base = Load(bcx, GEPi(bcx, llval, [0u, abi::slice_elt_base]));
|
|
|
|
let len = Load(bcx, GEPi(bcx, llval, [0u, abi::slice_elt_len]));
|
|
|
|
(base, len)
|
|
|
|
}
|
|
|
|
ty::vstore_uniq | ty::vstore_box => {
|
2013-02-25 13:11:21 -06:00
|
|
|
let body = get_bodyptr(bcx, llval);
|
|
|
|
(get_dataptr(bcx, body), get_fill(bcx, body))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-04-17 17:07:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 14:11:07 -06:00
|
|
|
pub type val_and_ty_fn = @fn(block, ValueRef, ty::t) -> Result;
|
2011-08-22 13:48:00 -05:00
|
|
|
|
2013-03-25 15:21:04 -05:00
|
|
|
pub type iter_vec_block<'self> = &'self fn(block, ValueRef, ty::t) -> block;
|
2011-08-22 13:48:00 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn iter_vec_raw(bcx: block, data_ptr: ValueRef, vec_ty: ty::t,
|
|
|
|
fill: ValueRef, f: iter_vec_block) -> block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::iter_vec_raw");
|
2012-04-19 17:42:02 -05:00
|
|
|
|
2012-02-21 07:20:18 -06:00
|
|
|
let unit_ty = ty::sequence_element_type(bcx.tcx(), vec_ty);
|
2011-08-25 03:18:02 -05:00
|
|
|
|
|
|
|
// Calculate the last pointer address we want to handle.
|
2012-10-11 18:42:40 -05:00
|
|
|
// FIXME (#3729): Optimize this when the size of the unit type is
|
2012-06-21 18:44:10 -05:00
|
|
|
// statically known to not use pointer casts, which tend to confuse
|
|
|
|
// LLVM.
|
2011-08-25 03:18:02 -05:00
|
|
|
let data_end_ptr = pointer_add(bcx, data_ptr, fill);
|
|
|
|
|
|
|
|
// Now perform the iteration.
|
2013-05-02 03:16:07 -05:00
|
|
|
let header_bcx = base::sub_block(bcx, "iter_vec_loop_header");
|
2012-08-28 17:54:45 -05:00
|
|
|
Br(bcx, header_bcx.llbb);
|
2012-06-25 22:00:46 -05:00
|
|
|
let data_ptr =
|
2013-05-19 00:07:44 -05:00
|
|
|
Phi(header_bcx, val_ty(data_ptr), [data_ptr], [bcx.llbb]);
|
2011-09-02 17:34:58 -05:00
|
|
|
let not_yet_at_end =
|
2012-08-28 17:54:45 -05:00
|
|
|
ICmp(header_bcx, lib::llvm::IntULT, data_ptr, data_end_ptr);
|
2013-05-02 03:16:07 -05:00
|
|
|
let body_bcx = base::sub_block(header_bcx, "iter_vec_loop_body");
|
|
|
|
let next_bcx = base::sub_block(header_bcx, "iter_vec_next");
|
2012-08-28 17:54:45 -05:00
|
|
|
CondBr(header_bcx, not_yet_at_end, body_bcx.llbb, next_bcx.llbb);
|
|
|
|
let body_bcx = f(body_bcx, data_ptr, unit_ty);
|
|
|
|
AddIncomingToPhi(data_ptr, InBoundsGEP(body_bcx, data_ptr,
|
2013-05-19 00:07:44 -05:00
|
|
|
[C_int(bcx.ccx(), 1)]),
|
2012-08-28 17:54:45 -05:00
|
|
|
body_bcx.llbb);
|
|
|
|
Br(body_bcx, header_bcx.llbb);
|
|
|
|
return next_bcx;
|
2012-04-19 17:42:02 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn iter_vec_uniq(bcx: block, vptr: ValueRef, vec_ty: ty::t,
|
|
|
|
fill: ValueRef, f: iter_vec_block) -> block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::iter_vec_uniq");
|
2012-06-12 18:41:20 -05:00
|
|
|
let data_ptr = get_dataptr(bcx, get_bodyptr(bcx, vptr));
|
2012-04-19 17:42:02 -05:00
|
|
|
iter_vec_raw(bcx, data_ptr, vec_ty, fill, f)
|
2011-08-22 13:48:00 -05:00
|
|
|
}
|
2011-08-25 03:18:02 -05:00
|
|
|
|
2013-01-30 13:46:19 -06:00
|
|
|
pub fn iter_vec_unboxed(bcx: block, body_ptr: ValueRef, vec_ty: ty::t,
|
|
|
|
f: iter_vec_block) -> block {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("tvec::iter_vec_unboxed");
|
2012-06-14 19:34:06 -05:00
|
|
|
let fill = get_fill(bcx, body_ptr);
|
|
|
|
let dataptr = get_dataptr(bcx, body_ptr);
|
2012-08-01 19:30:05 -05:00
|
|
|
return iter_vec_raw(bcx, dataptr, vec_ty, fill, f);
|
2011-08-29 09:14:24 -05:00
|
|
|
}
|