1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(non_camel_case_types)]
19 use trans::cleanup::CleanupMethods;
23 use trans::debuginfo::DebugLoc;
24 use trans::expr::{Dest, Ignore, SaveIn};
26 use trans::machine::llsize_of_alloc;
27 use trans::type_::Type;
29 use middle::ty::{self, Ty};
33 use syntax::parse::token::InternedString;
35 #[derive(Copy, Clone)]
36 struct VecTypes<'tcx> {
41 impl<'tcx> VecTypes<'tcx> {
42 pub fn to_string<'a>(&self, ccx: &CrateContext<'a, 'tcx>) -> String {
43 format!("VecTypes {{unit_ty={}, llunit_ty={}}}",
45 ccx.tn().type_to_string(self.llunit_ty))
49 pub fn trans_fixed_vstore<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
52 -> Block<'blk, 'tcx> {
55 // [...] allocates a fixed-size array and moves it around "by value".
56 // In this case, it means that the caller has already given us a location
57 // to store the array of the suitable size, so all we have to do is
58 // generate the content.
60 debug!("trans_fixed_vstore(expr={:?}, dest={})",
61 expr, dest.to_string(bcx.ccx()));
63 let vt = vec_types_from_expr(bcx, expr);
66 Ignore => write_content(bcx, &vt, expr, expr, dest),
68 // lldest will have type *[T x N], but we want the type *T,
69 // so use GEP to convert:
70 let lldest = StructGEP(bcx, lldest, 0);
71 write_content(bcx, &vt, expr, expr, SaveIn(lldest))
76 /// &[...] allocates memory on the stack and writes the values into it, returning the vector (the
77 /// caller must make the reference). "..." is similar except that the memory can be statically
78 /// allocated and we return a reference (strings are always by-ref).
79 pub fn trans_slice_vec<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
80 slice_expr: &hir::Expr,
81 content_expr: &hir::Expr)
82 -> DatumBlock<'blk, 'tcx, Expr> {
87 debug!("trans_slice_vec(slice_expr={:?})",
90 let vec_ty = node_id_type(bcx, slice_expr.id);
92 // Handle the "..." case (returns a slice since strings are always unsized):
93 if let hir::ExprLit(ref lit) = content_expr.node {
94 if let hir::LitStr(ref s, _) = lit.node {
95 let scratch = rvalue_scratch_datum(bcx, vec_ty, "");
96 bcx = trans_lit_str(bcx,
100 return DatumBlock::new(bcx, scratch.to_expr_datum());
104 // Handle the &[...] case:
105 let vt = vec_types_from_expr(bcx, content_expr);
106 let count = elements_required(bcx, content_expr);
107 debug!(" vt={}, count={}", vt.to_string(ccx), count);
109 let fixed_ty = bcx.tcx().mk_array(vt.unit_ty, count);
111 // Always create an alloca even if zero-sized, to preserve
112 // the non-null invariant of the inner slice ptr
113 let llfixed = base::alloc_ty(bcx, fixed_ty, "");
114 call_lifetime_start(bcx, llfixed);
117 // Arrange for the backing array to be cleaned up.
118 let cleanup_scope = cleanup::temporary_scope(bcx.tcx(), content_expr.id);
119 fcx.schedule_lifetime_end(cleanup_scope, llfixed);
120 fcx.schedule_drop_mem(cleanup_scope, llfixed, fixed_ty, None);
122 // Generate the content into the backing array.
123 // llfixed has type *[T x N], but we want the type *T,
124 // so use GEP to convert
125 bcx = write_content(bcx, &vt, slice_expr, content_expr,
126 SaveIn(StructGEP(bcx, llfixed, 0)));
129 immediate_rvalue_bcx(bcx, llfixed, vec_ty).to_expr_datumblock()
132 /// Literal strings translate to slices into static memory. This is different from
133 /// trans_slice_vstore() above because it doesn't need to copy the content anywhere.
134 pub fn trans_lit_str<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
135 lit_expr: &hir::Expr,
136 str_lit: InternedString,
138 -> Block<'blk, 'tcx> {
139 debug!("trans_lit_str(lit_expr={:?}, dest={})",
141 dest.to_string(bcx.ccx()));
146 let bytes = str_lit.len();
147 let llbytes = C_uint(bcx.ccx(), bytes);
148 let llcstr = C_cstr(bcx.ccx(), str_lit, false);
149 let llcstr = consts::ptrcast(llcstr, Type::i8p(bcx.ccx()));
150 Store(bcx, llcstr, expr::get_dataptr(bcx, lldest));
151 Store(bcx, llbytes, expr::get_meta(bcx, lldest));
157 fn write_content<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
159 vstore_expr: &hir::Expr,
160 content_expr: &hir::Expr,
162 -> Block<'blk, 'tcx> {
163 let _icx = push_ctxt("tvec::write_content");
167 debug!("write_content(vt={}, dest={}, vstore_expr={:?})",
168 vt.to_string(bcx.ccx()),
169 dest.to_string(bcx.ccx()),
172 match content_expr.node {
173 hir::ExprLit(ref lit) => {
175 hir::LitStr(ref s, _) => {
177 Ignore => return bcx,
180 let llbytes = C_uint(bcx.ccx(), bytes);
181 let llcstr = C_cstr(bcx.ccx(), (*s).clone(), false);
182 base::call_memcpy(bcx,
192 bcx.tcx().sess.span_bug(content_expr.span,
193 "unexpected evec content");
197 hir::ExprVec(ref elements) => {
200 for element in elements {
201 bcx = expr::trans_into(bcx, &**element, Ignore);
206 let temp_scope = fcx.push_custom_cleanup_scope();
207 for (i, element) in elements.iter().enumerate() {
208 let lleltptr = GEPi(bcx, lldest, &[i]);
209 debug!("writing index {} with lleltptr={}",
210 i, bcx.val_to_string(lleltptr));
211 bcx = expr::trans_into(bcx, &**element,
213 let scope = cleanup::CustomScope(temp_scope);
214 fcx.schedule_lifetime_end(scope, lleltptr);
215 fcx.schedule_drop_mem(scope, lleltptr, vt.unit_ty, None);
217 fcx.pop_custom_cleanup_scope(temp_scope);
222 hir::ExprRepeat(ref element, ref count_expr) => {
225 return expr::trans_into(bcx, &**element, Ignore);
228 match bcx.tcx().eval_repeat_count(&**count_expr) {
229 0 => expr::trans_into(bcx, &**element, Ignore),
230 1 => expr::trans_into(bcx, &**element, SaveIn(lldest)),
232 let elem = unpack_datum!(bcx, expr::trans(bcx, &**element));
233 let bcx = iter_vec_loop(bcx, lldest, vt,
234 C_uint(bcx.ccx(), count),
235 |set_bcx, lleltptr, _| {
236 elem.shallow_copy(set_bcx, lleltptr)
245 bcx.tcx().sess.span_bug(content_expr.span,
246 "unexpected vec content");
251 fn vec_types_from_expr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, vec_expr: &hir::Expr)
253 let vec_ty = node_id_type(bcx, vec_expr.id);
254 vec_types(bcx, vec_ty.sequence_element_type(bcx.tcx()))
257 fn vec_types<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, unit_ty: Ty<'tcx>)
261 llunit_ty: type_of::type_of(bcx.ccx(), unit_ty)
265 fn elements_required(bcx: Block, content_expr: &hir::Expr) -> usize {
266 //! Figure out the number of elements we need to store this content
268 match content_expr.node {
269 hir::ExprLit(ref lit) => {
271 hir::LitStr(ref s, _) => s.len(),
273 bcx.tcx().sess.span_bug(content_expr.span,
274 "unexpected evec content")
278 hir::ExprVec(ref es) => es.len(),
279 hir::ExprRepeat(_, ref count_expr) => {
280 bcx.tcx().eval_repeat_count(&**count_expr)
282 _ => bcx.tcx().sess.span_bug(content_expr.span,
283 "unexpected vec content")
287 /// Converts a fixed-length vector into the slice pair. The vector should be stored in `llval`
288 /// which should be by ref.
289 pub fn get_fixed_base_and_len(bcx: Block,
292 -> (ValueRef, ValueRef) {
295 let base = expr::get_dataptr(bcx, llval);
296 let len = C_uint(ccx, vec_length);
300 /// Converts a vector into the slice pair. The vector should be stored in `llval` which should be
301 /// by-reference. If you have a datum, you would probably prefer to call
302 /// `Datum::get_base_and_len()` which will handle any conversions for you.
303 pub fn get_base_and_len<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
306 -> (ValueRef, ValueRef) {
310 ty::TyArray(_, n) => get_fixed_base_and_len(bcx, llval, n),
311 ty::TySlice(_) | ty::TyStr => {
312 let base = Load(bcx, expr::get_dataptr(bcx, llval));
313 let len = Load(bcx, expr::get_meta(bcx, llval));
317 // Only used for pattern matching.
318 ty::TyBox(ty) | ty::TyRef(_, ty::TypeAndMut{ty, ..}) => {
319 let inner = if type_is_sized(bcx.tcx(), ty) {
324 get_base_and_len(bcx, inner, ty)
326 _ => ccx.sess().bug("unexpected type in get_base_and_len"),
330 fn iter_vec_loop<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
335 -> Block<'blk, 'tcx> where
336 F: FnOnce(Block<'blk, 'tcx>, ValueRef, Ty<'tcx>) -> Block<'blk, 'tcx>,
338 let _icx = push_ctxt("tvec::iter_vec_loop");
340 if bcx.unreachable.get() {
345 let loop_bcx = fcx.new_temp_block("expr_repeat");
346 let next_bcx = fcx.new_temp_block("expr_repeat: next");
348 Br(bcx, loop_bcx.llbb, DebugLoc::None);
350 let loop_counter = Phi(loop_bcx, bcx.ccx().int_type(),
351 &[C_uint(bcx.ccx(), 0 as usize)], &[bcx.llbb]);
355 let lleltptr = if llsize_of_alloc(bcx.ccx(), vt.llunit_ty) == 0 {
358 InBoundsGEP(bcx, data_ptr, &[loop_counter])
360 let bcx = f(bcx, lleltptr, vt.unit_ty);
361 let plusone = Add(bcx, loop_counter, C_uint(bcx.ccx(), 1usize), DebugLoc::None);
362 AddIncomingToPhi(loop_counter, plusone, bcx.llbb);
364 let cond_val = ICmp(bcx, llvm::IntULT, plusone, count, DebugLoc::None);
365 CondBr(bcx, cond_val, loop_bcx.llbb, next_bcx.llbb, DebugLoc::None);
370 pub fn iter_vec_raw<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
375 -> Block<'blk, 'tcx> where
376 F: FnOnce(Block<'blk, 'tcx>, ValueRef, Ty<'tcx>) -> Block<'blk, 'tcx>,
378 let _icx = push_ctxt("tvec::iter_vec_raw");
381 let vt = vec_types(bcx, unit_ty);
383 if llsize_of_alloc(bcx.ccx(), vt.llunit_ty) == 0 {
384 // Special-case vectors with elements of size 0 so they don't go out of bounds (#9890)
385 iter_vec_loop(bcx, data_ptr, &vt, len, f)
387 // Calculate the last pointer address we want to handle.
388 let data_end_ptr = InBoundsGEP(bcx, data_ptr, &[len]);
390 // Now perform the iteration.
391 let header_bcx = fcx.new_temp_block("iter_vec_loop_header");
392 Br(bcx, header_bcx.llbb, DebugLoc::None);
394 Phi(header_bcx, val_ty(data_ptr), &[data_ptr], &[bcx.llbb]);
396 ICmp(header_bcx, llvm::IntULT, data_ptr, data_end_ptr, DebugLoc::None);
397 let body_bcx = fcx.new_temp_block("iter_vec_loop_body");
398 let next_bcx = fcx.new_temp_block("iter_vec_next");
399 CondBr(header_bcx, not_yet_at_end, body_bcx.llbb, next_bcx.llbb, DebugLoc::None);
400 let body_bcx = f(body_bcx, data_ptr, unit_ty);
401 AddIncomingToPhi(data_ptr, InBoundsGEP(body_bcx, data_ptr,
402 &[C_int(bcx.ccx(), 1)]),
404 Br(body_bcx, header_bcx.llbb, DebugLoc::None);