2015-10-21 17:42:25 -04:00
|
|
|
// Copyright 2012-2014 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.
|
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
use llvm::{self, ValueRef};
|
2018-01-16 09:31:48 +01:00
|
|
|
use rustc::middle::const_val::{ConstVal, ConstEvalErr};
|
|
|
|
use rustc_mir::interpret::{read_target_uint, const_val_field};
|
2016-04-21 16:15:56 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir;
|
2018-01-16 09:31:48 +01:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2018-04-26 09:18:19 +02:00
|
|
|
use rustc::mir::interpret::{GlobalId, MemoryPointer, PrimVal, Allocation, ConstValue};
|
2018-01-25 16:44:45 +01:00
|
|
|
use rustc::ty::{self, Ty};
|
2018-01-16 09:31:48 +01:00
|
|
|
use rustc::ty::layout::{self, HasDataLayout, LayoutOf, Scalar};
|
2016-12-31 16:00:24 -07:00
|
|
|
use builder::Builder;
|
2018-01-16 09:31:48 +01:00
|
|
|
use common::{CodegenCx};
|
|
|
|
use common::{C_bytes, C_struct, C_uint_big, C_undef, C_usize};
|
2016-08-16 17:41:38 +03:00
|
|
|
use consts;
|
2017-09-21 20:40:50 +03:00
|
|
|
use type_of::LayoutLlvmExt;
|
2016-03-22 19:23:36 +02:00
|
|
|
use type_::Type;
|
2018-01-26 14:28:58 +01:00
|
|
|
use syntax::ast::Mutability;
|
2015-11-16 19:57:57 +02:00
|
|
|
|
2018-01-16 09:31:48 +01:00
|
|
|
use super::super::callee;
|
2018-01-16 10:16:38 +01:00
|
|
|
use super::FunctionCx;
|
2016-05-27 14:40:05 +03:00
|
|
|
|
2018-01-16 10:16:38 +01:00
|
|
|
pub fn primval_to_llvm(cx: &CodegenCx,
|
2018-01-16 09:31:48 +01:00
|
|
|
cv: PrimVal,
|
|
|
|
scalar: &Scalar,
|
|
|
|
llty: Type) -> ValueRef {
|
2018-01-16 10:16:38 +01:00
|
|
|
let bits = if scalar.is_bool() { 1 } else { scalar.value.size(cx).bits() };
|
2018-01-16 09:31:48 +01:00
|
|
|
match cv {
|
2018-01-16 10:16:38 +01:00
|
|
|
PrimVal::Undef => C_undef(Type::ix(cx, bits)),
|
2018-01-16 09:31:48 +01:00
|
|
|
PrimVal::Bytes(b) => {
|
2018-01-16 10:16:38 +01:00
|
|
|
let llval = C_uint_big(Type::ix(cx, bits), b);
|
2018-01-16 09:31:48 +01:00
|
|
|
if scalar.value == layout::Pointer {
|
|
|
|
unsafe { llvm::LLVMConstIntToPtr(llval, llty.to_ref()) }
|
|
|
|
} else {
|
|
|
|
consts::bitcast(llval, llty)
|
2016-01-05 12:29:50 -05:00
|
|
|
}
|
2018-01-16 09:31:48 +01:00
|
|
|
},
|
|
|
|
PrimVal::Ptr(ptr) => {
|
2018-01-25 12:59:24 +01:00
|
|
|
if let Some(fn_instance) = cx.tcx.interpret_interner.get_fn(ptr.alloc_id) {
|
2018-01-16 10:16:38 +01:00
|
|
|
callee::get_fn(cx, fn_instance)
|
2018-01-16 09:31:48 +01:00
|
|
|
} else {
|
2018-01-25 12:12:07 +01:00
|
|
|
let static_ = cx
|
|
|
|
.tcx
|
|
|
|
.interpret_interner
|
2018-04-10 16:25:10 +02:00
|
|
|
.get_static(ptr.alloc_id);
|
2018-01-16 09:31:48 +01:00
|
|
|
let base_addr = if let Some(def_id) = static_ {
|
2018-01-16 10:16:38 +01:00
|
|
|
assert!(cx.tcx.is_static(def_id).is_some());
|
|
|
|
consts::get_static(cx, def_id)
|
2018-01-25 12:59:24 +01:00
|
|
|
} else if let Some(alloc) = cx.tcx.interpret_interner
|
2018-01-25 12:12:07 +01:00
|
|
|
.get_alloc(ptr.alloc_id) {
|
2018-04-26 09:18:19 +02:00
|
|
|
let init = const_alloc_to_llvm(cx, alloc);
|
2018-01-26 14:28:58 +01:00
|
|
|
if alloc.runtime_mutability == Mutability::Mutable {
|
2018-01-16 10:16:38 +01:00
|
|
|
consts::addr_of_mut(cx, init, alloc.align, "byte_str")
|
2018-01-16 09:31:48 +01:00
|
|
|
} else {
|
2018-01-16 10:16:38 +01:00
|
|
|
consts::addr_of(cx, init, alloc.align, "byte_str")
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
2017-09-03 20:34:48 +03:00
|
|
|
} else {
|
2018-01-16 09:31:48 +01:00
|
|
|
bug!("missing allocation {:?}", ptr.alloc_id);
|
|
|
|
};
|
|
|
|
|
|
|
|
let llval = unsafe { llvm::LLVMConstInBoundsGEP(
|
2018-01-16 10:16:38 +01:00
|
|
|
consts::bitcast(base_addr, Type::i8p(cx)),
|
|
|
|
&C_usize(cx, ptr.offset),
|
2018-01-16 09:31:48 +01:00
|
|
|
1,
|
|
|
|
) };
|
|
|
|
if scalar.value != layout::Pointer {
|
|
|
|
unsafe { llvm::LLVMConstPtrToInt(llval, llty.to_ref()) }
|
|
|
|
} else {
|
|
|
|
consts::bitcast(llval, llty)
|
2017-09-03 20:34:48 +03:00
|
|
|
}
|
2016-05-03 00:26:41 +03:00
|
|
|
}
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
|
2018-04-26 09:18:19 +02:00
|
|
|
pub fn const_alloc_to_llvm(cx: &CodegenCx, alloc: &Allocation) -> ValueRef {
|
2018-01-16 09:31:48 +01:00
|
|
|
let mut llvals = Vec::with_capacity(alloc.relocations.len() + 1);
|
2018-01-16 10:16:38 +01:00
|
|
|
let layout = cx.data_layout();
|
2018-01-16 09:31:48 +01:00
|
|
|
let pointer_size = layout.pointer_size.bytes() as usize;
|
|
|
|
|
|
|
|
let mut next_offset = 0;
|
|
|
|
for (&offset, &alloc_id) in &alloc.relocations {
|
|
|
|
assert_eq!(offset as usize as u64, offset);
|
|
|
|
let offset = offset as usize;
|
|
|
|
if offset > next_offset {
|
2018-01-16 10:16:38 +01:00
|
|
|
llvals.push(C_bytes(cx, &alloc.bytes[next_offset..offset]));
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
|
|
|
let ptr_offset = read_target_uint(
|
|
|
|
layout.endian,
|
|
|
|
&alloc.bytes[offset..(offset + pointer_size)],
|
2018-04-26 09:18:19 +02:00
|
|
|
).expect("const_alloc_to_llvm: could not read relocation pointer") as u64;
|
2018-01-16 09:31:48 +01:00
|
|
|
llvals.push(primval_to_llvm(
|
2018-01-16 10:16:38 +01:00
|
|
|
cx,
|
2018-01-16 09:31:48 +01:00
|
|
|
PrimVal::Ptr(MemoryPointer { alloc_id, offset: ptr_offset }),
|
|
|
|
&Scalar {
|
|
|
|
value: layout::Primitive::Pointer,
|
|
|
|
valid_range: 0..=!0
|
|
|
|
},
|
2018-01-16 10:16:38 +01:00
|
|
|
Type::i8p(cx)
|
2018-01-16 09:31:48 +01:00
|
|
|
));
|
|
|
|
next_offset = offset + pointer_size;
|
|
|
|
}
|
|
|
|
if alloc.bytes.len() >= next_offset {
|
2018-01-16 10:16:38 +01:00
|
|
|
llvals.push(C_bytes(cx, &alloc.bytes[next_offset ..]));
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 10:16:38 +01:00
|
|
|
C_struct(cx, &llvals, true)
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
pub fn codegen_static_initializer<'a, 'tcx>(
|
2018-01-05 07:04:08 +02:00
|
|
|
cx: &CodegenCx<'a, 'tcx>,
|
2017-02-15 15:00:20 +02:00
|
|
|
def_id: DefId)
|
|
|
|
-> Result<ValueRef, ConstEvalErr<'tcx>>
|
|
|
|
{
|
2018-01-16 10:16:38 +01:00
|
|
|
let instance = ty::Instance::mono(cx.tcx, def_id);
|
2018-01-16 09:31:48 +01:00
|
|
|
let cid = GlobalId {
|
|
|
|
instance,
|
|
|
|
promoted: None
|
2017-01-02 11:00:42 -07:00
|
|
|
};
|
2018-02-10 13:18:02 -05:00
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
2018-04-10 16:25:10 +02:00
|
|
|
let static_ = cx.tcx.const_eval(param_env.and(cid))?;
|
2018-01-16 09:31:48 +01:00
|
|
|
|
2018-05-12 18:47:20 +02:00
|
|
|
let alloc = match static_.val {
|
2018-05-14 18:54:24 +02:00
|
|
|
ConstVal::Value(ConstValue::ByRef(alloc, 0)) => alloc,
|
2018-04-10 16:25:10 +02:00
|
|
|
_ => bug!("static const eval returned {:#?}", static_),
|
|
|
|
};
|
2018-05-12 18:47:20 +02:00
|
|
|
Ok(const_alloc_to_llvm(cx, alloc))
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
2017-09-26 21:34:10 +03:00
|
|
|
|
2018-01-16 09:31:48 +01:00
|
|
|
impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
|
2018-04-26 09:18:19 +02:00
|
|
|
fn const_to_const_value(
|
2018-01-16 09:31:48 +01:00
|
|
|
&mut self,
|
2018-01-16 10:16:38 +01:00
|
|
|
bx: &Builder<'a, 'tcx>,
|
2018-01-16 09:31:48 +01:00
|
|
|
constant: &'tcx ty::Const<'tcx>,
|
2018-04-26 09:18:19 +02:00
|
|
|
) -> Result<ConstValue<'tcx>, ConstEvalErr<'tcx>> {
|
2018-01-16 09:31:48 +01:00
|
|
|
match constant.val {
|
|
|
|
ConstVal::Unevaluated(def_id, ref substs) => {
|
2018-01-16 10:16:38 +01:00
|
|
|
let tcx = bx.tcx();
|
2018-02-10 13:18:02 -05:00
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
2018-01-16 09:31:48 +01:00
|
|
|
let instance = ty::Instance::resolve(tcx, param_env, def_id, substs).unwrap();
|
|
|
|
let cid = GlobalId {
|
|
|
|
instance,
|
|
|
|
promoted: None,
|
2017-09-24 12:12:26 +03:00
|
|
|
};
|
2018-01-16 09:31:48 +01:00
|
|
|
let c = tcx.const_eval(param_env.and(cid))?;
|
2018-04-26 09:18:19 +02:00
|
|
|
self.const_to_const_value(bx, c)
|
2018-01-16 09:31:48 +01:00
|
|
|
},
|
2018-04-26 09:18:19 +02:00
|
|
|
ConstVal::Value(val) => Ok(val),
|
2017-01-02 11:00:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 09:18:19 +02:00
|
|
|
pub fn mir_constant_to_const_value(
|
2018-01-16 09:31:48 +01:00
|
|
|
&mut self,
|
2018-01-16 10:16:38 +01:00
|
|
|
bx: &Builder<'a, 'tcx>,
|
2018-01-16 09:31:48 +01:00
|
|
|
constant: &mir::Constant<'tcx>,
|
2018-04-26 09:18:19 +02:00
|
|
|
) -> Result<ConstValue<'tcx>, ConstEvalErr<'tcx>> {
|
2018-01-16 09:31:48 +01:00
|
|
|
match constant.literal {
|
|
|
|
mir::Literal::Promoted { index } => {
|
2018-02-10 13:18:02 -05:00
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
2018-01-16 09:31:48 +01:00
|
|
|
let cid = mir::interpret::GlobalId {
|
|
|
|
instance: self.instance,
|
|
|
|
promoted: Some(index),
|
|
|
|
};
|
2018-01-16 10:16:38 +01:00
|
|
|
bx.tcx().const_eval(param_env.and(cid))
|
2017-10-09 02:31:06 +03:00
|
|
|
}
|
2018-01-16 09:31:48 +01:00
|
|
|
mir::Literal::Value { value } => {
|
|
|
|
Ok(self.monomorphize(&value))
|
|
|
|
}
|
2018-04-26 09:18:19 +02:00
|
|
|
}.and_then(|c| self.const_to_const_value(bx, c))
|
2017-09-10 17:15:29 +03:00
|
|
|
}
|
2017-01-02 11:00:42 -07:00
|
|
|
|
2018-01-26 13:45:41 +01:00
|
|
|
/// process constant containing SIMD shuffle indices
|
|
|
|
pub fn simd_shuffle_indices(
|
|
|
|
&mut self,
|
|
|
|
bx: &Builder<'a, 'tcx>,
|
|
|
|
constant: &mir::Constant<'tcx>,
|
|
|
|
) -> (ValueRef, Ty<'tcx>) {
|
2018-04-26 09:18:19 +02:00
|
|
|
self.mir_constant_to_const_value(bx, constant)
|
2018-01-16 09:31:48 +01:00
|
|
|
.and_then(|c| {
|
2018-01-29 09:58:28 +01:00
|
|
|
let field_ty = constant.ty.builtin_index().unwrap();
|
|
|
|
let fields = match constant.ty.sty {
|
2018-04-26 09:18:19 +02:00
|
|
|
ty::TyArray(_, n) => n.unwrap_usize(bx.tcx()),
|
2018-01-29 09:58:28 +01:00
|
|
|
ref other => bug!("invalid simd shuffle type: {}", other),
|
2018-01-16 09:31:48 +01:00
|
|
|
};
|
2018-01-29 09:58:28 +01:00
|
|
|
let values: Result<Vec<ValueRef>, _> = (0..fields).map(|field| {
|
|
|
|
let field = const_val_field(
|
|
|
|
bx.tcx(),
|
2018-02-10 13:18:02 -05:00
|
|
|
ty::ParamEnv::reveal_all(),
|
2018-01-29 09:58:28 +01:00
|
|
|
self.instance,
|
|
|
|
None,
|
|
|
|
mir::Field::new(field as usize),
|
|
|
|
c,
|
|
|
|
constant.ty,
|
|
|
|
)?;
|
2018-04-26 09:18:19 +02:00
|
|
|
if let Some(prim) = field.to_primval() {
|
|
|
|
let layout = bx.cx.layout_of(field_ty);
|
|
|
|
let scalar = match layout.abi {
|
|
|
|
layout::Abi::Scalar(ref x) => x,
|
|
|
|
_ => bug!("from_const: invalid ByVal layout: {:#?}", layout)
|
|
|
|
};
|
|
|
|
Ok(primval_to_llvm(
|
|
|
|
bx.cx, prim, scalar,
|
|
|
|
layout.immediate_llvm_type(bx.cx),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
bug!("simd shuffle field {:?}", field)
|
2018-01-29 09:58:28 +01:00
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
let llval = C_struct(bx.cx, &values?, false);
|
2018-01-16 09:31:48 +01:00
|
|
|
Ok((llval, constant.ty))
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|e| {
|
2018-01-16 10:16:38 +01:00
|
|
|
e.report(bx.tcx(), constant.span, "shuffle_indices");
|
2018-01-16 09:31:48 +01:00
|
|
|
// We've errored, so we don't have to produce working code.
|
|
|
|
let ty = self.monomorphize(&constant.ty);
|
2018-01-16 10:16:38 +01:00
|
|
|
let llty = bx.cx.layout_of(ty).llvm_type(bx.cx);
|
2018-01-16 09:31:48 +01:00
|
|
|
(C_undef(llty), ty)
|
|
|
|
})
|
2017-01-02 11:00:42 -07:00
|
|
|
}
|
|
|
|
}
|