]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/mir/constant.rs
Add comment about the problem, and use provided path if available
[rust.git] / src / librustc_trans / mir / constant.rs
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.
4 //
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.
10
11 use llvm::{self, ValueRef};
12 use rustc::middle::const_val::{ConstEvalErr, ConstVal, ErrKind};
13 use rustc_const_math::ConstInt::*;
14 use rustc_const_math::{ConstInt, ConstMathErr, MAX_F32_PLUS_HALF_ULP};
15 use rustc::hir::def_id::DefId;
16 use rustc::infer::TransNormalize;
17 use rustc::traits;
18 use rustc::mir;
19 use rustc::mir::tcx::PlaceTy;
20 use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
21 use rustc::ty::layout::{self, LayoutOf, Size};
22 use rustc::ty::cast::{CastTy, IntTy};
23 use rustc::ty::subst::{Kind, Substs};
24 use rustc_apfloat::{ieee, Float, Status};
25 use rustc_data_structures::indexed_vec::{Idx, IndexVec};
26 use base;
27 use abi::{self, Abi};
28 use callee;
29 use builder::Builder;
30 use common::{self, CodegenCx, const_get_elt, val_ty};
31 use common::{C_array, C_bool, C_bytes, C_int, C_uint, C_uint_big, C_u32, C_u64};
32 use common::{C_null, C_struct, C_str_slice, C_undef, C_usize, C_vector, C_fat_ptr};
33 use common::const_to_opt_u128;
34 use consts;
35 use type_of::LayoutLlvmExt;
36 use type_::Type;
37 use value::Value;
38
39 use syntax_pos::Span;
40 use syntax::ast;
41
42 use std::fmt;
43 use std::ptr;
44
45 use super::operand::{OperandRef, OperandValue};
46 use super::FunctionCx;
47
48 /// A sized constant rvalue.
49 /// The LLVM type might not be the same for a single Rust type,
50 /// e.g. each enum variant would have its own LLVM struct type.
51 #[derive(Copy, Clone)]
52 pub struct Const<'tcx> {
53     pub llval: ValueRef,
54     pub ty: Ty<'tcx>
55 }
56
57 impl<'a, 'tcx> Const<'tcx> {
58     pub fn new(llval: ValueRef, ty: Ty<'tcx>) -> Const<'tcx> {
59         Const {
60             llval,
61             ty,
62         }
63     }
64
65     pub fn from_constint(cx: &CodegenCx<'a, 'tcx>, ci: &ConstInt) -> Const<'tcx> {
66         let tcx = cx.tcx;
67         let (llval, ty) = match *ci {
68             I8(v) => (C_int(Type::i8(cx), v as i64), tcx.types.i8),
69             I16(v) => (C_int(Type::i16(cx), v as i64), tcx.types.i16),
70             I32(v) => (C_int(Type::i32(cx), v as i64), tcx.types.i32),
71             I64(v) => (C_int(Type::i64(cx), v as i64), tcx.types.i64),
72             I128(v) => (C_uint_big(Type::i128(cx), v as u128), tcx.types.i128),
73             Isize(v) => (C_int(Type::isize(cx), v.as_i64()), tcx.types.isize),
74             U8(v) => (C_uint(Type::i8(cx), v as u64), tcx.types.u8),
75             U16(v) => (C_uint(Type::i16(cx), v as u64), tcx.types.u16),
76             U32(v) => (C_uint(Type::i32(cx), v as u64), tcx.types.u32),
77             U64(v) => (C_uint(Type::i64(cx), v), tcx.types.u64),
78             U128(v) => (C_uint_big(Type::i128(cx), v), tcx.types.u128),
79             Usize(v) => (C_uint(Type::isize(cx), v.as_u64()), tcx.types.usize),
80         };
81         Const { llval: llval, ty: ty }
82     }
83
84     /// Translate ConstVal into a LLVM constant value.
85     pub fn from_constval(cx: &CodegenCx<'a, 'tcx>,
86                          cv: &ConstVal,
87                          ty: Ty<'tcx>)
88                          -> Const<'tcx> {
89         let llty = cx.layout_of(ty).llvm_type(cx);
90         let val = match *cv {
91             ConstVal::Float(v) => {
92                 let bits = match v.ty {
93                     ast::FloatTy::F32 => C_u32(cx, v.bits as u32),
94                     ast::FloatTy::F64 => C_u64(cx, v.bits as u64)
95                 };
96                 consts::bitcast(bits, llty)
97             }
98             ConstVal::Bool(v) => C_bool(cx, v),
99             ConstVal::Integral(ref i) => return Const::from_constint(cx, i),
100             ConstVal::Str(ref v) => C_str_slice(cx, v.clone()),
101             ConstVal::ByteStr(v) => {
102                 consts::addr_of(cx, C_bytes(cx, v.data), cx.align_of(ty), "byte_str")
103             }
104             ConstVal::Char(c) => C_uint(Type::char(cx), c as u64),
105             ConstVal::Function(..) => C_undef(llty),
106             ConstVal::Variant(_) |
107             ConstVal::Aggregate(..) |
108             ConstVal::Unevaluated(..) => {
109                 bug!("MIR must not use `{:?}` (aggregates are expanded to MIR rvalues)", cv)
110             }
111         };
112
113         assert!(!ty.has_erasable_regions());
114
115         Const::new(val, ty)
116     }
117
118     fn get_field(&self, cx: &CodegenCx<'a, 'tcx>, i: usize) -> ValueRef {
119         let layout = cx.layout_of(self.ty);
120         let field = layout.field(cx, i);
121         if field.is_zst() {
122             return C_undef(field.immediate_llvm_type(cx));
123         }
124         let offset = layout.fields.offset(i);
125         match layout.abi {
126             layout::Abi::Scalar(_) |
127             layout::Abi::ScalarPair(..) |
128             layout::Abi::Vector { .. }
129                 if offset.bytes() == 0 && field.size == layout.size => self.llval,
130
131             layout::Abi::ScalarPair(ref a, ref b) => {
132                 if offset.bytes() == 0 {
133                     assert_eq!(field.size, a.value.size(cx));
134                     const_get_elt(self.llval, 0)
135                 } else {
136                     assert_eq!(offset, a.value.size(cx)
137                         .abi_align(b.value.align(cx)));
138                     assert_eq!(field.size, b.value.size(cx));
139                     const_get_elt(self.llval, 1)
140                 }
141             }
142             _ => {
143                 match layout.fields {
144                     layout::FieldPlacement::Union(_) => self.llval,
145                     _ => const_get_elt(self.llval, layout.llvm_field_index(i)),
146                 }
147             }
148         }
149     }
150
151     fn get_pair(&self, cx: &CodegenCx<'a, 'tcx>) -> (ValueRef, ValueRef) {
152         (self.get_field(cx, 0), self.get_field(cx, 1))
153     }
154
155     fn get_fat_ptr(&self, cx: &CodegenCx<'a, 'tcx>) -> (ValueRef, ValueRef) {
156         assert_eq!(abi::FAT_PTR_ADDR, 0);
157         assert_eq!(abi::FAT_PTR_EXTRA, 1);
158         self.get_pair(cx)
159     }
160
161     fn as_place(&self) -> ConstPlace<'tcx> {
162         ConstPlace {
163             base: Base::Value(self.llval),
164             llextra: ptr::null_mut(),
165             ty: self.ty
166         }
167     }
168
169     pub fn to_operand(&self, cx: &CodegenCx<'a, 'tcx>) -> OperandRef<'tcx> {
170         let layout = cx.layout_of(self.ty);
171         let llty = layout.immediate_llvm_type(cx);
172         let llvalty = val_ty(self.llval);
173
174         let val = if llty == llvalty && layout.is_llvm_scalar_pair() {
175             OperandValue::Pair(
176                 const_get_elt(self.llval, 0),
177                 const_get_elt(self.llval, 1))
178         } else if llty == llvalty && layout.is_llvm_immediate() {
179             // If the types match, we can use the value directly.
180             OperandValue::Immediate(self.llval)
181         } else {
182             // Otherwise, or if the value is not immediate, we create
183             // a constant LLVM global and cast its address if necessary.
184             let align = cx.align_of(self.ty);
185             let ptr = consts::addr_of(cx, self.llval, align, "const");
186             OperandValue::Ref(consts::ptrcast(ptr, layout.llvm_type(cx).ptr_to()),
187                               layout.align)
188         };
189
190         OperandRef {
191             val,
192             layout
193         }
194     }
195 }
196
197 impl<'tcx> fmt::Debug for Const<'tcx> {
198     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199         write!(f, "Const({:?}: {:?})", Value(self.llval), self.ty)
200     }
201 }
202
203 #[derive(Copy, Clone)]
204 enum Base {
205     /// A constant value without an unique address.
206     Value(ValueRef),
207
208     /// String literal base pointer (cast from array).
209     Str(ValueRef),
210
211     /// The address of a static.
212     Static(ValueRef)
213 }
214
215 /// An place as seen from a constant.
216 #[derive(Copy, Clone)]
217 struct ConstPlace<'tcx> {
218     base: Base,
219     llextra: ValueRef,
220     ty: Ty<'tcx>
221 }
222
223 impl<'tcx> ConstPlace<'tcx> {
224     fn to_const(&self, span: Span) -> Const<'tcx> {
225         match self.base {
226             Base::Value(val) => Const::new(val, self.ty),
227             Base::Str(ptr) => {
228                 span_bug!(span, "loading from `str` ({:?}) in constant",
229                           Value(ptr))
230             }
231             Base::Static(val) => {
232                 span_bug!(span, "loading from `static` ({:?}) in constant",
233                           Value(val))
234             }
235         }
236     }
237
238     pub fn len<'a>(&self, cx: &CodegenCx<'a, 'tcx>) -> ValueRef {
239         match self.ty.sty {
240             ty::TyArray(_, n) => {
241                 C_usize(cx, n.val.to_const_int().unwrap().to_u64().unwrap())
242             }
243             ty::TySlice(_) | ty::TyStr => {
244                 assert!(self.llextra != ptr::null_mut());
245                 self.llextra
246             }
247             _ => bug!("unexpected type `{}` in ConstPlace::len", self.ty)
248         }
249     }
250 }
251
252 /// Machinery for translating a constant's MIR to LLVM values.
253 /// FIXME(eddyb) use miri and lower its allocations to LLVM.
254 struct MirConstContext<'a, 'tcx: 'a> {
255     cx: &'a CodegenCx<'a, 'tcx>,
256     mir: &'a mir::Mir<'tcx>,
257
258     /// Type parameters for const fn and associated constants.
259     substs: &'tcx Substs<'tcx>,
260
261     /// Values of locals in a constant or const fn.
262     locals: IndexVec<mir::Local, Option<Result<Const<'tcx>, ConstEvalErr<'tcx>>>>
263 }
264
265 fn add_err<'tcx, U, V>(failure: &mut Result<U, ConstEvalErr<'tcx>>,
266                        value: &Result<V, ConstEvalErr<'tcx>>)
267 {
268     if let &Err(ref err) = value {
269         if failure.is_ok() {
270             *failure = Err(err.clone());
271         }
272     }
273 }
274
275 impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
276     fn new(cx: &'a CodegenCx<'a, 'tcx>,
277            mir: &'a mir::Mir<'tcx>,
278            substs: &'tcx Substs<'tcx>,
279            args: IndexVec<mir::Local, Result<Const<'tcx>, ConstEvalErr<'tcx>>>)
280            -> MirConstContext<'a, 'tcx> {
281         let mut context = MirConstContext {
282             cx,
283             mir,
284             substs,
285             locals: (0..mir.local_decls.len()).map(|_| None).collect(),
286         };
287         for (i, arg) in args.into_iter().enumerate() {
288             // Locals after local 0 are the function arguments
289             let index = mir::Local::new(i + 1);
290             context.locals[index] = Some(arg);
291         }
292         context
293     }
294
295     fn trans_def(cx: &'a CodegenCx<'a, 'tcx>,
296                  def_id: DefId,
297                  substs: &'tcx Substs<'tcx>,
298                  args: IndexVec<mir::Local, Result<Const<'tcx>, ConstEvalErr<'tcx>>>)
299                  -> Result<Const<'tcx>, ConstEvalErr<'tcx>> {
300         let instance = ty::Instance::resolve(cx.tcx,
301                                              ty::ParamEnv::empty(traits::Reveal::All),
302                                              def_id,
303                                              substs).unwrap();
304         let mir = cx.tcx.instance_mir(instance.def);
305         MirConstContext::new(cx, &mir, instance.substs, args).trans()
306     }
307
308     fn monomorphize<T>(&self, value: &T) -> T
309         where T: TransNormalize<'tcx>
310     {
311         self.cx.tcx.trans_apply_param_substs(self.substs, value)
312     }
313
314     fn trans(&mut self) -> Result<Const<'tcx>, ConstEvalErr<'tcx>> {
315         let tcx = self.cx.tcx;
316         let mut bb = mir::START_BLOCK;
317
318         // Make sure to evaluate all statemenets to
319         // report as many errors as we possibly can.
320         let mut failure = Ok(());
321
322         loop {
323             let data = &self.mir[bb];
324             for statement in &data.statements {
325                 let span = statement.source_info.span;
326                 match statement.kind {
327                     mir::StatementKind::Assign(ref dest, ref rvalue) => {
328                         let ty = dest.ty(self.mir, tcx);
329                         let ty = self.monomorphize(&ty).to_ty(tcx);
330                         let value = self.const_rvalue(rvalue, ty, span);
331                         add_err(&mut failure, &value);
332                         self.store(dest, value, span);
333                     }
334                     mir::StatementKind::StorageLive(_) |
335                     mir::StatementKind::StorageDead(_) |
336                     mir::StatementKind::Validate(..) |
337                     mir::StatementKind::EndRegion(_) |
338                     mir::StatementKind::Nop => {}
339                     mir::StatementKind::InlineAsm { .. } |
340                     mir::StatementKind::SetDiscriminant{ .. } => {
341                         span_bug!(span, "{:?} should not appear in constants?", statement.kind);
342                     }
343                 }
344             }
345
346             let terminator = data.terminator();
347             let span = terminator.source_info.span;
348             bb = match terminator.kind {
349                 mir::TerminatorKind::Drop { target, .. } | // No dropping.
350                 mir::TerminatorKind::Goto { target } => target,
351                 mir::TerminatorKind::Return => {
352                     failure?;
353                     return self.locals[mir::RETURN_PLACE].clone().unwrap_or_else(|| {
354                         span_bug!(span, "no returned value in constant");
355                     });
356                 }
357
358                 mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, .. } => {
359                     let cond = self.const_operand(cond, span)?;
360                     let cond_bool = common::const_to_uint(cond.llval) != 0;
361                     if cond_bool != expected {
362                         let err = match *msg {
363                             mir::AssertMessage::BoundsCheck { ref len, ref index } => {
364                                 let len = self.const_operand(len, span)?;
365                                 let index = self.const_operand(index, span)?;
366                                 ErrKind::IndexOutOfBounds {
367                                     len: common::const_to_uint(len.llval),
368                                     index: common::const_to_uint(index.llval)
369                                 }
370                             }
371                             mir::AssertMessage::Math(ref err) => {
372                                 ErrKind::Math(err.clone())
373                             }
374                             mir::AssertMessage::GeneratorResumedAfterReturn |
375                             mir::AssertMessage::GeneratorResumedAfterPanic =>
376                                 span_bug!(span, "{:?} should not appear in constants?", msg),
377                         };
378
379                         let err = ConstEvalErr { span: span, kind: err };
380                         err.report(tcx, span, "expression");
381                         failure = Err(err);
382                     }
383                     target
384                 }
385
386                 mir::TerminatorKind::Call { ref func, ref args, ref destination, .. } => {
387                     let fn_ty = func.ty(self.mir, tcx);
388                     let fn_ty = self.monomorphize(&fn_ty);
389                     let (def_id, substs) = match fn_ty.sty {
390                         ty::TyFnDef(def_id, substs) => (def_id, substs),
391                         _ => span_bug!(span, "calling {:?} (of type {}) in constant",
392                                        func, fn_ty)
393                     };
394
395                     let mut arg_vals = IndexVec::with_capacity(args.len());
396                     for arg in args {
397                         let arg_val = self.const_operand(arg, span);
398                         add_err(&mut failure, &arg_val);
399                         arg_vals.push(arg_val);
400                     }
401                     if let Some((ref dest, target)) = *destination {
402                         let result = if fn_ty.fn_sig(tcx).abi() == Abi::RustIntrinsic {
403                             match &tcx.item_name(def_id)[..] {
404                                 "size_of" => {
405                                     let llval = C_usize(self.cx,
406                                         self.cx.size_of(substs.type_at(0)).bytes());
407                                     Ok(Const::new(llval, tcx.types.usize))
408                                 }
409                                 "min_align_of" => {
410                                     let llval = C_usize(self.cx,
411                                         self.cx.align_of(substs.type_at(0)).abi());
412                                     Ok(Const::new(llval, tcx.types.usize))
413                                 }
414                                 _ => span_bug!(span, "{:?} in constant", terminator.kind)
415                             }
416                         } else if let Some((op, is_checked)) = self.is_binop_lang_item(def_id) {
417                             (||{
418                                 assert_eq!(arg_vals.len(), 2);
419                                 let rhs = arg_vals.pop().unwrap()?;
420                                 let lhs = arg_vals.pop().unwrap()?;
421                                 if !is_checked {
422                                     let binop_ty = op.ty(tcx, lhs.ty, rhs.ty);
423                                     let (lhs, rhs) = (lhs.llval, rhs.llval);
424                                     Ok(Const::new(const_scalar_binop(op, lhs, rhs, binop_ty),
425                                                   binop_ty))
426                                 } else {
427                                     let ty = lhs.ty;
428                                     let val_ty = op.ty(tcx, lhs.ty, rhs.ty);
429                                     let binop_ty = tcx.intern_tup(&[val_ty, tcx.types.bool], false);
430                                     let (lhs, rhs) = (lhs.llval, rhs.llval);
431                                     assert!(!ty.is_fp());
432
433                                     match const_scalar_checked_binop(tcx, op, lhs, rhs, ty) {
434                                         Some((llval, of)) => {
435                                             Ok(trans_const_adt(
436                                                 self.cx,
437                                                 binop_ty,
438                                                 &mir::AggregateKind::Tuple,
439                                                 &[
440                                                     Const::new(llval, val_ty),
441                                                     Const::new(C_bool(self.cx, of), tcx.types.bool)
442                                                 ]))
443                                         }
444                                         None => {
445                                             span_bug!(span,
446                                                 "{:?} got non-integer operands: {:?} and {:?}",
447                                                 op, Value(lhs), Value(rhs));
448                                         }
449                                     }
450                                 }
451                             })()
452                         } else {
453                             MirConstContext::trans_def(self.cx, def_id, substs, arg_vals)
454                         };
455                         add_err(&mut failure, &result);
456                         self.store(dest, result, span);
457                         target
458                     } else {
459                         span_bug!(span, "diverging {:?} in constant", terminator.kind);
460                     }
461                 }
462                 _ => span_bug!(span, "{:?} in constant", terminator.kind)
463             };
464         }
465     }
466
467     fn is_binop_lang_item(&mut self, def_id: DefId) -> Option<(mir::BinOp, bool)> {
468         let tcx = self.cx.tcx;
469         let items = tcx.lang_items();
470         let def_id = Some(def_id);
471         if items.i128_add_fn() == def_id { Some((mir::BinOp::Add, false)) }
472         else if items.u128_add_fn() == def_id { Some((mir::BinOp::Add, false)) }
473         else if items.i128_sub_fn() == def_id { Some((mir::BinOp::Sub, false)) }
474         else if items.u128_sub_fn() == def_id { Some((mir::BinOp::Sub, false)) }
475         else if items.i128_mul_fn() == def_id { Some((mir::BinOp::Mul, false)) }
476         else if items.u128_mul_fn() == def_id { Some((mir::BinOp::Mul, false)) }
477         else if items.i128_div_fn() == def_id { Some((mir::BinOp::Div, false)) }
478         else if items.u128_div_fn() == def_id { Some((mir::BinOp::Div, false)) }
479         else if items.i128_rem_fn() == def_id { Some((mir::BinOp::Rem, false)) }
480         else if items.u128_rem_fn() == def_id { Some((mir::BinOp::Rem, false)) }
481         else if items.i128_shl_fn() == def_id { Some((mir::BinOp::Shl, false)) }
482         else if items.u128_shl_fn() == def_id { Some((mir::BinOp::Shl, false)) }
483         else if items.i128_shr_fn() == def_id { Some((mir::BinOp::Shr, false)) }
484         else if items.u128_shr_fn() == def_id { Some((mir::BinOp::Shr, false)) }
485         else if items.i128_addo_fn() == def_id { Some((mir::BinOp::Add, true)) }
486         else if items.u128_addo_fn() == def_id { Some((mir::BinOp::Add, true)) }
487         else if items.i128_subo_fn() == def_id { Some((mir::BinOp::Sub, true)) }
488         else if items.u128_subo_fn() == def_id { Some((mir::BinOp::Sub, true)) }
489         else if items.i128_mulo_fn() == def_id { Some((mir::BinOp::Mul, true)) }
490         else if items.u128_mulo_fn() == def_id { Some((mir::BinOp::Mul, true)) }
491         else if items.i128_shlo_fn() == def_id { Some((mir::BinOp::Shl, true)) }
492         else if items.u128_shlo_fn() == def_id { Some((mir::BinOp::Shl, true)) }
493         else if items.i128_shro_fn() == def_id { Some((mir::BinOp::Shr, true)) }
494         else if items.u128_shro_fn() == def_id { Some((mir::BinOp::Shr, true)) }
495         else { None }
496     }
497
498     fn store(&mut self,
499              dest: &mir::Place<'tcx>,
500              value: Result<Const<'tcx>, ConstEvalErr<'tcx>>,
501              span: Span) {
502         if let mir::Place::Local(index) = *dest {
503             self.locals[index] = Some(value);
504         } else {
505             span_bug!(span, "assignment to {:?} in constant", dest);
506         }
507     }
508
509     fn const_place(&self, place: &mir::Place<'tcx>, span: Span)
510                     -> Result<ConstPlace<'tcx>, ConstEvalErr<'tcx>> {
511         let tcx = self.cx.tcx;
512
513         if let mir::Place::Local(index) = *place {
514             return self.locals[index].clone().unwrap_or_else(|| {
515                 span_bug!(span, "{:?} not initialized", place)
516             }).map(|v| v.as_place());
517         }
518
519         let place = match *place {
520             mir::Place::Local(_)  => bug!(), // handled above
521             mir::Place::Static(box mir::Static { def_id, ty }) => {
522                 ConstPlace {
523                     base: Base::Static(consts::get_static(self.cx, def_id)),
524                     llextra: ptr::null_mut(),
525                     ty: self.monomorphize(&ty),
526                 }
527             }
528             mir::Place::Projection(ref projection) => {
529                 let tr_base = self.const_place(&projection.base, span)?;
530                 let projected_ty = PlaceTy::Ty { ty: tr_base.ty }
531                     .projection_ty(tcx, &projection.elem);
532                 let base = tr_base.to_const(span);
533                 let projected_ty = self.monomorphize(&projected_ty).to_ty(tcx);
534                 let has_metadata = self.cx.type_has_metadata(projected_ty);
535
536                 let (projected, llextra) = match projection.elem {
537                     mir::ProjectionElem::Deref => {
538                         let (base, extra) = if !has_metadata {
539                             (base.llval, ptr::null_mut())
540                         } else {
541                             base.get_fat_ptr(self.cx)
542                         };
543                         if self.cx.statics.borrow().contains_key(&base) {
544                             (Base::Static(base), extra)
545                         } else if let ty::TyStr = projected_ty.sty {
546                             (Base::Str(base), extra)
547                         } else {
548                             let v = base;
549                             let v = self.cx.const_unsized.borrow().get(&v).map_or(v, |&v| v);
550                             let mut val = unsafe { llvm::LLVMGetInitializer(v) };
551                             if val.is_null() {
552                                 span_bug!(span, "dereference of non-constant pointer `{:?}`",
553                                           Value(base));
554                             }
555                             let layout = self.cx.layout_of(projected_ty);
556                             if let layout::Abi::Scalar(ref scalar) = layout.abi {
557                                 let i1_type = Type::i1(self.cx);
558                                 if scalar.is_bool() && val_ty(val) != i1_type {
559                                     unsafe {
560                                         val = llvm::LLVMConstTrunc(val, i1_type.to_ref());
561                                     }
562                                 }
563                             }
564                             (Base::Value(val), extra)
565                         }
566                     }
567                     mir::ProjectionElem::Field(ref field, _) => {
568                         let llprojected = base.get_field(self.cx, field.index());
569                         let llextra = if !has_metadata {
570                             ptr::null_mut()
571                         } else {
572                             tr_base.llextra
573                         };
574                         (Base::Value(llprojected), llextra)
575                     }
576                     mir::ProjectionElem::Index(index) => {
577                         let index = &mir::Operand::Copy(mir::Place::Local(index));
578                         let llindex = self.const_operand(index, span)?.llval;
579
580                         let iv = if let Some(iv) = common::const_to_opt_u128(llindex, false) {
581                             iv
582                         } else {
583                             span_bug!(span, "index is not an integer-constant expression")
584                         };
585
586                         // Produce an undef instead of a LLVM assertion on OOB.
587                         let len = common::const_to_uint(tr_base.len(self.cx));
588                         let llelem = if iv < len as u128 {
589                             const_get_elt(base.llval, iv as u64)
590                         } else {
591                             C_undef(self.cx.layout_of(projected_ty).llvm_type(self.cx))
592                         };
593
594                         (Base::Value(llelem), ptr::null_mut())
595                     }
596                     _ => span_bug!(span, "{:?} in constant", projection.elem)
597                 };
598                 ConstPlace {
599                     base: projected,
600                     llextra,
601                     ty: projected_ty
602                 }
603             }
604         };
605         Ok(place)
606     }
607
608     fn const_operand(&self, operand: &mir::Operand<'tcx>, span: Span)
609                      -> Result<Const<'tcx>, ConstEvalErr<'tcx>> {
610         debug!("const_operand({:?} @ {:?})", operand, span);
611         let result = match *operand {
612             mir::Operand::Copy(ref place) |
613             mir::Operand::Move(ref place) => {
614                 Ok(self.const_place(place, span)?.to_const(span))
615             }
616
617             mir::Operand::Constant(ref constant) => {
618                 let ty = self.monomorphize(&constant.ty);
619                 match constant.literal.clone() {
620                     mir::Literal::Promoted { index } => {
621                         let mir = &self.mir.promoted[index];
622                         MirConstContext::new(self.cx, mir, self.substs, IndexVec::new()).trans()
623                     }
624                     mir::Literal::Value { value } => {
625                         if let ConstVal::Unevaluated(def_id, substs) = value.val {
626                             let substs = self.monomorphize(&substs);
627                             MirConstContext::trans_def(self.cx, def_id, substs, IndexVec::new())
628                         } else {
629                             Ok(Const::from_constval(self.cx, &value.val, ty))
630                         }
631                     }
632                 }
633             }
634         };
635         debug!("const_operand({:?} @ {:?}) = {:?}", operand, span,
636                result.as_ref().ok());
637         result
638     }
639
640     fn const_array(&self, array_ty: Ty<'tcx>, fields: &[ValueRef])
641                    -> Const<'tcx>
642     {
643         let elem_ty = array_ty.builtin_index().unwrap_or_else(|| {
644             bug!("bad array type {:?}", array_ty)
645         });
646         let llunitty = self.cx.layout_of(elem_ty).llvm_type(self.cx);
647         // If the array contains enums, an LLVM array won't work.
648         let val = if fields.iter().all(|&f| val_ty(f) == llunitty) {
649             C_array(llunitty, fields)
650         } else {
651             C_struct(self.cx, fields, false)
652         };
653         Const::new(val, array_ty)
654     }
655
656     fn const_rvalue(&self, rvalue: &mir::Rvalue<'tcx>,
657                     dest_ty: Ty<'tcx>, span: Span)
658                     -> Result<Const<'tcx>, ConstEvalErr<'tcx>> {
659         let tcx = self.cx.tcx;
660         debug!("const_rvalue({:?}: {:?} @ {:?})", rvalue, dest_ty, span);
661         let val = match *rvalue {
662             mir::Rvalue::Use(ref operand) => self.const_operand(operand, span)?,
663
664             mir::Rvalue::Repeat(ref elem, count) => {
665                 let elem = self.const_operand(elem, span)?;
666                 let size = count.as_u64();
667                 assert_eq!(size as usize as u64, size);
668                 let fields = vec![elem.llval; size as usize];
669                 self.const_array(dest_ty, &fields)
670             }
671
672             mir::Rvalue::Aggregate(box mir::AggregateKind::Array(_), ref operands) => {
673                 // Make sure to evaluate all operands to
674                 // report as many errors as we possibly can.
675                 let mut fields = Vec::with_capacity(operands.len());
676                 let mut failure = Ok(());
677                 for operand in operands {
678                     match self.const_operand(operand, span) {
679                         Ok(val) => fields.push(val.llval),
680                         Err(err) => if failure.is_ok() { failure = Err(err); }
681                     }
682                 }
683                 failure?;
684
685                 self.const_array(dest_ty, &fields)
686             }
687
688             mir::Rvalue::Aggregate(ref kind, ref operands) => {
689                 // Make sure to evaluate all operands to
690                 // report as many errors as we possibly can.
691                 let mut fields = Vec::with_capacity(operands.len());
692                 let mut failure = Ok(());
693                 for operand in operands {
694                     match self.const_operand(operand, span) {
695                         Ok(val) => fields.push(val),
696                         Err(err) => if failure.is_ok() { failure = Err(err); }
697                     }
698                 }
699                 failure?;
700
701                 trans_const_adt(self.cx, dest_ty, kind, &fields)
702             }
703
704             mir::Rvalue::Cast(ref kind, ref source, cast_ty) => {
705                 let operand = self.const_operand(source, span)?;
706                 let cast_ty = self.monomorphize(&cast_ty);
707
708                 let val = match *kind {
709                     mir::CastKind::ReifyFnPointer => {
710                         match operand.ty.sty {
711                             ty::TyFnDef(def_id, substs) => {
712                                 callee::resolve_and_get_fn(self.cx, def_id, substs)
713                             }
714                             _ => {
715                                 span_bug!(span, "{} cannot be reified to a fn ptr",
716                                           operand.ty)
717                             }
718                         }
719                     }
720                     mir::CastKind::ClosureFnPointer => {
721                         match operand.ty.sty {
722                             ty::TyClosure(def_id, substs) => {
723                                 // Get the def_id for FnOnce::call_once
724                                 let fn_once = tcx.lang_items().fn_once_trait().unwrap();
725                                 let call_once = tcx
726                                     .global_tcx().associated_items(fn_once)
727                                     .find(|it| it.kind == ty::AssociatedKind::Method)
728                                     .unwrap().def_id;
729                                 // Now create its substs [Closure, Tuple]
730                                 let input = substs.closure_sig(def_id, tcx).input(0);
731                                 let input = tcx.erase_late_bound_regions_and_normalize(&input);
732                                 let substs = tcx.mk_substs([operand.ty, input]
733                                     .iter().cloned().map(Kind::from));
734                                 callee::resolve_and_get_fn(self.cx, call_once, substs)
735                             }
736                             _ => {
737                                 bug!("{} cannot be cast to a fn ptr", operand.ty)
738                             }
739                         }
740                     }
741                     mir::CastKind::UnsafeFnPointer => {
742                         // this is a no-op at the LLVM level
743                         operand.llval
744                     }
745                     mir::CastKind::Unsize => {
746                         let pointee_ty = operand.ty.builtin_deref(true, ty::NoPreference)
747                             .expect("consts: unsizing got non-pointer type").ty;
748                         let (base, old_info) = if !self.cx.type_is_sized(pointee_ty) {
749                             // Normally, the source is a thin pointer and we are
750                             // adding extra info to make a fat pointer. The exception
751                             // is when we are upcasting an existing object fat pointer
752                             // to use a different vtable. In that case, we want to
753                             // load out the original data pointer so we can repackage
754                             // it.
755                             let (base, extra) = operand.get_fat_ptr(self.cx);
756                             (base, Some(extra))
757                         } else {
758                             (operand.llval, None)
759                         };
760
761                         let unsized_ty = cast_ty.builtin_deref(true, ty::NoPreference)
762                             .expect("consts: unsizing got non-pointer target type").ty;
763                         let ptr_ty = self.cx.layout_of(unsized_ty).llvm_type(self.cx).ptr_to();
764                         let base = consts::ptrcast(base, ptr_ty);
765                         let info = base::unsized_info(self.cx, pointee_ty,
766                                                       unsized_ty, old_info);
767
768                         if old_info.is_none() {
769                             let prev_const = self.cx.const_unsized.borrow_mut()
770                                                      .insert(base, operand.llval);
771                             assert!(prev_const.is_none() || prev_const == Some(operand.llval));
772                         }
773                         C_fat_ptr(self.cx, base, info)
774                     }
775                     mir::CastKind::Misc if self.cx.layout_of(operand.ty).is_llvm_immediate() => {
776                         let r_t_in = CastTy::from_ty(operand.ty).expect("bad input type for cast");
777                         let r_t_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
778                         let cast_layout = self.cx.layout_of(cast_ty);
779                         assert!(cast_layout.is_llvm_immediate());
780                         let ll_t_out = cast_layout.immediate_llvm_type(self.cx);
781                         let llval = operand.llval;
782
783                         let mut signed = false;
784                         let l = self.cx.layout_of(operand.ty);
785                         if let layout::Abi::Scalar(ref scalar) = l.abi {
786                             if let layout::Int(_, true) = scalar.value {
787                                 signed = true;
788                             }
789                         }
790
791                         unsafe {
792                             match (r_t_in, r_t_out) {
793                                 (CastTy::Int(_), CastTy::Int(_)) => {
794                                     let s = signed as llvm::Bool;
795                                     llvm::LLVMConstIntCast(llval, ll_t_out.to_ref(), s)
796                                 }
797                                 (CastTy::Int(_), CastTy::Float) => {
798                                     cast_const_int_to_float(self.cx, llval, signed, ll_t_out)
799                                 }
800                                 (CastTy::Float, CastTy::Float) => {
801                                     llvm::LLVMConstFPCast(llval, ll_t_out.to_ref())
802                                 }
803                                 (CastTy::Float, CastTy::Int(IntTy::I)) => {
804                                     cast_const_float_to_int(self.cx, &operand,
805                                                             true, ll_t_out, span)
806                                 }
807                                 (CastTy::Float, CastTy::Int(_)) => {
808                                     cast_const_float_to_int(self.cx, &operand,
809                                                             false, ll_t_out, span)
810                                 }
811                                 (CastTy::Ptr(_), CastTy::Ptr(_)) |
812                                 (CastTy::FnPtr, CastTy::Ptr(_)) |
813                                 (CastTy::RPtr(_), CastTy::Ptr(_)) => {
814                                     consts::ptrcast(llval, ll_t_out)
815                                 }
816                                 (CastTy::Int(_), CastTy::Ptr(_)) => {
817                                     let s = signed as llvm::Bool;
818                                     let usize_llval = llvm::LLVMConstIntCast(llval,
819                                         self.cx.isize_ty.to_ref(), s);
820                                     llvm::LLVMConstIntToPtr(usize_llval, ll_t_out.to_ref())
821                                 }
822                                 (CastTy::Ptr(_), CastTy::Int(_)) |
823                                 (CastTy::FnPtr, CastTy::Int(_)) => {
824                                     llvm::LLVMConstPtrToInt(llval, ll_t_out.to_ref())
825                                 }
826                                 _ => bug!("unsupported cast: {:?} to {:?}", operand.ty, cast_ty)
827                             }
828                         }
829                     }
830                     mir::CastKind::Misc => { // Casts from a fat-ptr.
831                         let l = self.cx.layout_of(operand.ty);
832                         let cast = self.cx.layout_of(cast_ty);
833                         if l.is_llvm_scalar_pair() {
834                             let (data_ptr, meta) = operand.get_fat_ptr(self.cx);
835                             if cast.is_llvm_scalar_pair() {
836                                 let data_cast = consts::ptrcast(data_ptr,
837                                     cast.scalar_pair_element_llvm_type(self.cx, 0));
838                                 C_fat_ptr(self.cx, data_cast, meta)
839                             } else { // cast to thin-ptr
840                                 // Cast of fat-ptr to thin-ptr is an extraction of data-ptr and
841                                 // pointer-cast of that pointer to desired pointer type.
842                                 let llcast_ty = cast.immediate_llvm_type(self.cx);
843                                 consts::ptrcast(data_ptr, llcast_ty)
844                             }
845                         } else {
846                             bug!("Unexpected non-fat-pointer operand")
847                         }
848                     }
849                 };
850                 Const::new(val, cast_ty)
851             }
852
853             mir::Rvalue::Ref(_, bk, ref place) => {
854                 let tr_place = self.const_place(place, span)?;
855
856                 let ty = tr_place.ty;
857                 let ref_ty = tcx.mk_ref(tcx.types.re_erased,
858                     ty::TypeAndMut { ty: ty, mutbl: bk.to_mutbl_lossy() });
859
860                 let base = match tr_place.base {
861                     Base::Value(llval) => {
862                         // FIXME: may be wrong for &*(&simd_vec as &fmt::Debug)
863                         let align = if self.cx.type_is_sized(ty) {
864                             self.cx.align_of(ty)
865                         } else {
866                             self.cx.tcx.data_layout.pointer_align
867                         };
868                         if bk == mir::BorrowKind::Mut {
869                             consts::addr_of_mut(self.cx, llval, align, "ref_mut")
870                         } else {
871                             consts::addr_of(self.cx, llval, align, "ref")
872                         }
873                     }
874                     Base::Str(llval) |
875                     Base::Static(llval) => llval
876                 };
877
878                 let ptr = if self.cx.type_is_sized(ty) {
879                     base
880                 } else {
881                     C_fat_ptr(self.cx, base, tr_place.llextra)
882                 };
883                 Const::new(ptr, ref_ty)
884             }
885
886             mir::Rvalue::Len(ref place) => {
887                 let tr_place = self.const_place(place, span)?;
888                 Const::new(tr_place.len(self.cx), tcx.types.usize)
889             }
890
891             mir::Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
892                 let lhs = self.const_operand(lhs, span)?;
893                 let rhs = self.const_operand(rhs, span)?;
894                 let ty = lhs.ty;
895                 let binop_ty = op.ty(tcx, lhs.ty, rhs.ty);
896                 let (lhs, rhs) = (lhs.llval, rhs.llval);
897                 Const::new(const_scalar_binop(op, lhs, rhs, ty), binop_ty)
898             }
899
900             mir::Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => {
901                 let lhs = self.const_operand(lhs, span)?;
902                 let rhs = self.const_operand(rhs, span)?;
903                 let ty = lhs.ty;
904                 let val_ty = op.ty(tcx, lhs.ty, rhs.ty);
905                 let binop_ty = tcx.intern_tup(&[val_ty, tcx.types.bool], false);
906                 let (lhs, rhs) = (lhs.llval, rhs.llval);
907                 assert!(!ty.is_fp());
908
909                 match const_scalar_checked_binop(tcx, op, lhs, rhs, ty) {
910                     Some((llval, of)) => {
911                         trans_const_adt(self.cx, binop_ty, &mir::AggregateKind::Tuple, &[
912                             Const::new(llval, val_ty),
913                             Const::new(C_bool(self.cx, of), tcx.types.bool)
914                         ])
915                     }
916                     None => {
917                         span_bug!(span, "{:?} got non-integer operands: {:?} and {:?}",
918                                   rvalue, Value(lhs), Value(rhs));
919                     }
920                 }
921             }
922
923             mir::Rvalue::UnaryOp(op, ref operand) => {
924                 let operand = self.const_operand(operand, span)?;
925                 let lloperand = operand.llval;
926                 let llval = match op {
927                     mir::UnOp::Not => {
928                         unsafe {
929                             llvm::LLVMConstNot(lloperand)
930                         }
931                     }
932                     mir::UnOp::Neg => {
933                         let is_float = operand.ty.is_fp();
934                         unsafe {
935                             if is_float {
936                                 llvm::LLVMConstFNeg(lloperand)
937                             } else {
938                                 llvm::LLVMConstNeg(lloperand)
939                             }
940                         }
941                     }
942                 };
943                 Const::new(llval, operand.ty)
944             }
945
946             mir::Rvalue::NullaryOp(mir::NullOp::SizeOf, ty) => {
947                 assert!(self.cx.type_is_sized(ty));
948                 let llval = C_usize(self.cx, self.cx.size_of(ty).bytes());
949                 Const::new(llval, tcx.types.usize)
950             }
951
952             _ => span_bug!(span, "{:?} in constant", rvalue)
953         };
954
955         debug!("const_rvalue({:?}: {:?} @ {:?}) = {:?}", rvalue, dest_ty, span, val);
956
957         Ok(val)
958     }
959
960 }
961
962 fn to_const_int(value: ValueRef, t: Ty, tcx: TyCtxt) -> Option<ConstInt> {
963     match t.sty {
964         ty::TyInt(int_type) => const_to_opt_u128(value, true)
965             .and_then(|input| ConstInt::new_signed(input as i128, int_type,
966                                                    tcx.sess.target.isize_ty)),
967         ty::TyUint(uint_type) => const_to_opt_u128(value, false)
968             .and_then(|input| ConstInt::new_unsigned(input, uint_type,
969                                                      tcx.sess.target.usize_ty)),
970         _ => None
971
972     }
973 }
974
975 pub fn const_scalar_binop(op: mir::BinOp,
976                           lhs: ValueRef,
977                           rhs: ValueRef,
978                           input_ty: Ty) -> ValueRef {
979     assert!(!input_ty.is_simd());
980     let is_float = input_ty.is_fp();
981     let signed = input_ty.is_signed();
982
983     unsafe {
984         match op {
985             mir::BinOp::Add if is_float => llvm::LLVMConstFAdd(lhs, rhs),
986             mir::BinOp::Add             => llvm::LLVMConstAdd(lhs, rhs),
987
988             mir::BinOp::Sub if is_float => llvm::LLVMConstFSub(lhs, rhs),
989             mir::BinOp::Sub             => llvm::LLVMConstSub(lhs, rhs),
990
991             mir::BinOp::Mul if is_float => llvm::LLVMConstFMul(lhs, rhs),
992             mir::BinOp::Mul             => llvm::LLVMConstMul(lhs, rhs),
993
994             mir::BinOp::Div if is_float => llvm::LLVMConstFDiv(lhs, rhs),
995             mir::BinOp::Div if signed   => llvm::LLVMConstSDiv(lhs, rhs),
996             mir::BinOp::Div             => llvm::LLVMConstUDiv(lhs, rhs),
997
998             mir::BinOp::Rem if is_float => llvm::LLVMConstFRem(lhs, rhs),
999             mir::BinOp::Rem if signed   => llvm::LLVMConstSRem(lhs, rhs),
1000             mir::BinOp::Rem             => llvm::LLVMConstURem(lhs, rhs),
1001
1002             mir::BinOp::BitXor => llvm::LLVMConstXor(lhs, rhs),
1003             mir::BinOp::BitAnd => llvm::LLVMConstAnd(lhs, rhs),
1004             mir::BinOp::BitOr  => llvm::LLVMConstOr(lhs, rhs),
1005             mir::BinOp::Shl    => {
1006                 let rhs = base::cast_shift_const_rhs(op.to_hir_binop(), lhs, rhs);
1007                 llvm::LLVMConstShl(lhs, rhs)
1008             }
1009             mir::BinOp::Shr    => {
1010                 let rhs = base::cast_shift_const_rhs(op.to_hir_binop(), lhs, rhs);
1011                 if signed { llvm::LLVMConstAShr(lhs, rhs) }
1012                 else      { llvm::LLVMConstLShr(lhs, rhs) }
1013             }
1014             mir::BinOp::Eq | mir::BinOp::Ne |
1015             mir::BinOp::Lt | mir::BinOp::Le |
1016             mir::BinOp::Gt | mir::BinOp::Ge => {
1017                 if is_float {
1018                     let cmp = base::bin_op_to_fcmp_predicate(op.to_hir_binop());
1019                     llvm::LLVMConstFCmp(cmp, lhs, rhs)
1020                 } else {
1021                     let cmp = base::bin_op_to_icmp_predicate(op.to_hir_binop(),
1022                                                                 signed);
1023                     llvm::LLVMConstICmp(cmp, lhs, rhs)
1024                 }
1025             }
1026             mir::BinOp::Offset => unreachable!("BinOp::Offset in const-eval!")
1027         }
1028     }
1029 }
1030
1031 pub fn const_scalar_checked_binop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1032                                             op: mir::BinOp,
1033                                             lllhs: ValueRef,
1034                                             llrhs: ValueRef,
1035                                             input_ty: Ty<'tcx>)
1036                                             -> Option<(ValueRef, bool)> {
1037     if let (Some(lhs), Some(rhs)) = (to_const_int(lllhs, input_ty, tcx),
1038                                      to_const_int(llrhs, input_ty, tcx)) {
1039         let result = match op {
1040             mir::BinOp::Add => lhs + rhs,
1041             mir::BinOp::Sub => lhs - rhs,
1042             mir::BinOp::Mul => lhs * rhs,
1043             mir::BinOp::Shl => lhs << rhs,
1044             mir::BinOp::Shr => lhs >> rhs,
1045             _ => {
1046                 bug!("Operator `{:?}` is not a checkable operator", op)
1047             }
1048         };
1049
1050         let of = match result {
1051             Ok(_) => false,
1052             Err(ConstMathErr::Overflow(_)) |
1053             Err(ConstMathErr::ShiftNegative) => true,
1054             Err(err) => {
1055                 bug!("Operator `{:?}` on `{:?}` and `{:?}` errored: {}",
1056                      op, lhs, rhs, err.description());
1057             }
1058         };
1059
1060         Some((const_scalar_binop(op, lllhs, llrhs, input_ty), of))
1061     } else {
1062         None
1063     }
1064 }
1065
1066 unsafe fn cast_const_float_to_int(cx: &CodegenCx,
1067                                   operand: &Const,
1068                                   signed: bool,
1069                                   int_ty: Type,
1070                                   span: Span) -> ValueRef {
1071     let llval = operand.llval;
1072     let float_bits = match operand.ty.sty {
1073         ty::TyFloat(fty) => fty.bit_width(),
1074         _ => bug!("cast_const_float_to_int: operand not a float"),
1075     };
1076     // Note: this breaks if llval is a complex constant expression rather than a simple constant.
1077     // One way that might happen would be if addresses could be turned into integers in constant
1078     // expressions, but that doesn't appear to be possible?
1079     // In any case, an ICE is better than producing undef.
1080     let llval_bits = consts::bitcast(llval, Type::ix(cx, float_bits as u64));
1081     let bits = const_to_opt_u128(llval_bits, false).unwrap_or_else(|| {
1082         panic!("could not get bits of constant float {:?}",
1083                Value(llval));
1084     });
1085     let int_width = int_ty.int_width() as usize;
1086     // Try to convert, but report an error for overflow and NaN. This matches HIR const eval.
1087     let cast_result = match float_bits {
1088         32 if signed => ieee::Single::from_bits(bits).to_i128(int_width).map(|v| v as u128),
1089         64 if signed => ieee::Double::from_bits(bits).to_i128(int_width).map(|v| v as u128),
1090         32 => ieee::Single::from_bits(bits).to_u128(int_width),
1091         64 => ieee::Double::from_bits(bits).to_u128(int_width),
1092         n => bug!("unsupported float width {}", n),
1093     };
1094     if cast_result.status.contains(Status::INVALID_OP) {
1095         let err = ConstEvalErr { span: span, kind: ErrKind::CannotCast };
1096         err.report(cx.tcx, span, "expression");
1097     }
1098     C_uint_big(int_ty, cast_result.value)
1099 }
1100
1101 unsafe fn cast_const_int_to_float(cx: &CodegenCx,
1102                                   llval: ValueRef,
1103                                   signed: bool,
1104                                   float_ty: Type) -> ValueRef {
1105     // Note: this breaks if llval is a complex constant expression rather than a simple constant.
1106     // One way that might happen would be if addresses could be turned into integers in constant
1107     // expressions, but that doesn't appear to be possible?
1108     // In any case, an ICE is better than producing undef.
1109     let value = const_to_opt_u128(llval, signed).unwrap_or_else(|| {
1110         panic!("could not get z128 value of constant integer {:?}",
1111                Value(llval));
1112     });
1113     if signed {
1114         llvm::LLVMConstSIToFP(llval, float_ty.to_ref())
1115     } else if float_ty.float_width() == 32 && value >= MAX_F32_PLUS_HALF_ULP {
1116         // We're casting to f32 and the value is > f32::MAX + 0.5 ULP -> round up to infinity.
1117         let infinity_bits = C_u32(cx, ieee::Single::INFINITY.to_bits() as u32);
1118         consts::bitcast(infinity_bits, float_ty)
1119     } else {
1120         llvm::LLVMConstUIToFP(llval, float_ty.to_ref())
1121     }
1122 }
1123
1124 impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
1125     pub fn trans_constant(&mut self,
1126                           bx: &Builder<'a, 'tcx>,
1127                           constant: &mir::Constant<'tcx>)
1128                           -> Const<'tcx>
1129     {
1130         debug!("trans_constant({:?})", constant);
1131         let ty = self.monomorphize(&constant.ty);
1132         let result = match constant.literal.clone() {
1133             mir::Literal::Promoted { index } => {
1134                 let mir = &self.mir.promoted[index];
1135                 MirConstContext::new(bx.cx, mir, self.param_substs, IndexVec::new()).trans()
1136             }
1137             mir::Literal::Value { value } => {
1138                 if let ConstVal::Unevaluated(def_id, substs) = value.val {
1139                     let substs = self.monomorphize(&substs);
1140                     MirConstContext::trans_def(bx.cx, def_id, substs, IndexVec::new())
1141                 } else {
1142                     Ok(Const::from_constval(bx.cx, &value.val, ty))
1143                 }
1144             }
1145         };
1146
1147         let result = result.unwrap_or_else(|_| {
1148             // We've errored, so we don't have to produce working code.
1149             let llty = bx.cx.layout_of(ty).llvm_type(bx.cx);
1150             Const::new(C_undef(llty), ty)
1151         });
1152
1153         debug!("trans_constant({:?}) = {:?}", constant, result);
1154         result
1155     }
1156 }
1157
1158
1159 pub fn trans_static_initializer<'a, 'tcx>(
1160     cx: &CodegenCx<'a, 'tcx>,
1161     def_id: DefId)
1162     -> Result<ValueRef, ConstEvalErr<'tcx>>
1163 {
1164     MirConstContext::trans_def(cx, def_id, Substs::empty(), IndexVec::new())
1165         .map(|c| c.llval)
1166 }
1167
1168 /// Construct a constant value, suitable for initializing a
1169 /// GlobalVariable, given a case and constant values for its fields.
1170 /// Note that this may have a different LLVM type (and different
1171 /// alignment!) from the representation's `type_of`, so it needs a
1172 /// pointer cast before use.
1173 ///
1174 /// The LLVM type system does not directly support unions, and only
1175 /// pointers can be bitcast, so a constant (and, by extension, the
1176 /// GlobalVariable initialized by it) will have a type that can vary
1177 /// depending on which case of an enum it is.
1178 ///
1179 /// To understand the alignment situation, consider `enum E { V64(u64),
1180 /// V32(u32, u32) }` on Windows.  The type has 8-byte alignment to
1181 /// accommodate the u64, but `V32(x, y)` would have LLVM type `{i32,
1182 /// i32, i32}`, which is 4-byte aligned.
1183 ///
1184 /// Currently the returned value has the same size as the type, but
1185 /// this could be changed in the future to avoid allocating unnecessary
1186 /// space after values of shorter-than-maximum cases.
1187 fn trans_const_adt<'a, 'tcx>(
1188     cx: &CodegenCx<'a, 'tcx>,
1189     t: Ty<'tcx>,
1190     kind: &mir::AggregateKind,
1191     vals: &[Const<'tcx>]
1192 ) -> Const<'tcx> {
1193     let l = cx.layout_of(t);
1194     let variant_index = match *kind {
1195         mir::AggregateKind::Adt(_, index, _, _) => index,
1196         _ => 0,
1197     };
1198
1199     if let layout::Abi::Uninhabited = l.abi {
1200         return Const::new(C_undef(l.llvm_type(cx)), t);
1201     }
1202
1203     match l.variants {
1204         layout::Variants::Single { index } => {
1205             assert_eq!(variant_index, index);
1206             if let layout::FieldPlacement::Union(_) = l.fields {
1207                 assert_eq!(variant_index, 0);
1208                 assert_eq!(vals.len(), 1);
1209                 let (field_size, field_align) = cx.size_and_align_of(vals[0].ty);
1210                 let contents = [
1211                     vals[0].llval,
1212                     padding(cx, l.size - field_size)
1213                 ];
1214
1215                 let packed = l.align.abi() < field_align.abi();
1216                 Const::new(C_struct(cx, &contents, packed), t)
1217             } else {
1218                 if let layout::Abi::Vector { .. } = l.abi {
1219                     if let layout::FieldPlacement::Array { .. } = l.fields {
1220                         return Const::new(C_vector(&vals.iter().map(|x| x.llval)
1221                             .collect::<Vec<_>>()), t);
1222                     }
1223                 }
1224                 build_const_struct(cx, l, vals, None)
1225             }
1226         }
1227         layout::Variants::Tagged { .. } => {
1228             let discr = match *kind {
1229                 mir::AggregateKind::Adt(adt_def, _, _, _) => {
1230                     adt_def.discriminant_for_variant(cx.tcx, variant_index)
1231                            .to_u128_unchecked() as u64
1232                 },
1233                 _ => 0,
1234             };
1235             let discr_field = l.field(cx, 0);
1236             let discr = C_int(discr_field.llvm_type(cx), discr as i64);
1237             if let layout::Abi::Scalar(_) = l.abi {
1238                 Const::new(discr, t)
1239             } else {
1240                 let discr = Const::new(discr, discr_field.ty);
1241                 build_const_struct(cx, l.for_variant(cx, variant_index), vals, Some(discr))
1242             }
1243         }
1244         layout::Variants::NicheFilling {
1245             dataful_variant,
1246             ref niche_variants,
1247             niche_start,
1248             ..
1249         } => {
1250             if variant_index == dataful_variant {
1251                 build_const_struct(cx, l.for_variant(cx, dataful_variant), vals, None)
1252             } else {
1253                 let niche = l.field(cx, 0);
1254                 let niche_llty = niche.llvm_type(cx);
1255                 let niche_value = ((variant_index - niche_variants.start) as u128)
1256                     .wrapping_add(niche_start);
1257                 // FIXME(eddyb) Check the actual primitive type here.
1258                 let niche_llval = if niche_value == 0 {
1259                     // HACK(eddyb) Using `C_null` as it works on all types.
1260                     C_null(niche_llty)
1261                 } else {
1262                     C_uint_big(niche_llty, niche_value)
1263                 };
1264                 build_const_struct(cx, l, &[Const::new(niche_llval, niche.ty)], None)
1265             }
1266         }
1267     }
1268 }
1269
1270 /// Building structs is a little complicated, because we might need to
1271 /// insert padding if a field's value is less aligned than its type.
1272 ///
1273 /// Continuing the example from `trans_const_adt`, a value of type `(u32,
1274 /// E)` should have the `E` at offset 8, but if that field's
1275 /// initializer is 4-byte aligned then simply translating the tuple as
1276 /// a two-element struct will locate it at offset 4, and accesses to it
1277 /// will read the wrong memory.
1278 fn build_const_struct<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
1279                                 layout: layout::TyLayout<'tcx>,
1280                                 vals: &[Const<'tcx>],
1281                                 discr: Option<Const<'tcx>>)
1282                                 -> Const<'tcx> {
1283     assert_eq!(vals.len(), layout.fields.count());
1284
1285     match layout.abi {
1286         layout::Abi::Scalar(_) |
1287         layout::Abi::ScalarPair(..) |
1288         layout::Abi::Vector { .. } if discr.is_none() => {
1289             let mut non_zst_fields = vals.iter().enumerate().map(|(i, f)| {
1290                 (f, layout.fields.offset(i))
1291             }).filter(|&(f, _)| !cx.layout_of(f.ty).is_zst());
1292             match (non_zst_fields.next(), non_zst_fields.next()) {
1293                 (Some((x, offset)), None) if offset.bytes() == 0 => {
1294                     return Const::new(x.llval, layout.ty);
1295                 }
1296                 (Some((a, a_offset)), Some((b, _))) if a_offset.bytes() == 0 => {
1297                     return Const::new(C_struct(cx, &[a.llval, b.llval], false), layout.ty);
1298                 }
1299                 (Some((a, _)), Some((b, b_offset))) if b_offset.bytes() == 0 => {
1300                     return Const::new(C_struct(cx, &[b.llval, a.llval], false), layout.ty);
1301                 }
1302                 _ => {}
1303             }
1304         }
1305         _ => {}
1306     }
1307
1308     // offset of current value
1309     let mut packed = false;
1310     let mut offset = Size::from_bytes(0);
1311     let mut cfields = Vec::new();
1312     cfields.reserve(discr.is_some() as usize + 1 + layout.fields.count() * 2);
1313
1314     if let Some(discr) = discr {
1315         let (field_size, field_align) = cx.size_and_align_of(discr.ty);
1316         packed |= layout.align.abi() < field_align.abi();
1317         cfields.push(discr.llval);
1318         offset = field_size;
1319     }
1320
1321     let parts = layout.fields.index_by_increasing_offset().map(|i| {
1322         (vals[i], layout.fields.offset(i))
1323     });
1324     for (val, target_offset) in parts {
1325         let (field_size, field_align) = cx.size_and_align_of(val.ty);
1326         packed |= layout.align.abi() < field_align.abi();
1327         cfields.push(padding(cx, target_offset - offset));
1328         cfields.push(val.llval);
1329         offset = target_offset + field_size;
1330     }
1331
1332     // Pad to the size of the whole type, not e.g. the variant.
1333     cfields.push(padding(cx, cx.size_of(layout.ty) - offset));
1334
1335     Const::new(C_struct(cx, &cfields, packed), layout.ty)
1336 }
1337
1338 fn padding(cx: &CodegenCx, size: Size) -> ValueRef {
1339     C_undef(Type::array(&Type::i8(cx), size.bytes()))
1340 }