Files
rust/src/librustc_codegen_llvm/mir/constant.rs

221 lines
8.3 KiB
Rust
Raw Normal View History

// 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.
use llvm::{self, ValueRef};
2018-06-25 20:53:02 +02:00
use rustc::mir::interpret::ConstEvalErr;
2018-01-16 09:31:48 +01:00
use rustc_mir::interpret::{read_target_uint, const_val_field};
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;
use rustc_data_structures::sync::Lrc;
2018-05-21 00:37:44 +02:00
use rustc::mir::interpret::{GlobalId, Pointer, Scalar, Allocation, ConstValue, AllocType};
2018-01-25 16:44:45 +01:00
use rustc::ty::{self, Ty};
use rustc::ty::layout::{self, HasDataLayout, LayoutOf, Size};
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};
use consts;
use type_of::LayoutLlvmExt;
use type_::Type;
2018-01-26 14:28:58 +01:00
use syntax::ast::Mutability;
use syntax::codemap::Span;
2018-01-16 09:31:48 +01:00
use super::super::callee;
use super::FunctionCx;
2018-05-22 19:30:16 +02:00
pub fn scalar_to_llvm(cx: &CodegenCx,
2018-05-20 23:43:16 +02:00
cv: Scalar,
layout: &layout::Scalar,
2018-01-16 09:31:48 +01:00
llty: Type) -> ValueRef {
2018-05-22 10:28:46 +02:00
let bitsize = if layout.is_bool() { 1 } else { layout.value.size(cx).bits() };
2018-01-16 09:31:48 +01:00
match cv {
2018-05-22 10:28:46 +02:00
Scalar::Bits { defined, .. } if (defined as u64) < bitsize || defined == 0 => {
C_undef(Type::ix(cx, bitsize))
},
Scalar::Bits { bits, .. } => {
let llval = C_uint_big(Type::ix(cx, bitsize), bits);
if layout.value == layout::Pointer {
2018-01-16 09:31:48 +01:00
unsafe { llvm::LLVMConstIntToPtr(llval, llty.to_ref()) }
} else {
consts::bitcast(llval, llty)
}
2018-01-16 09:31:48 +01:00
},
2018-05-20 23:43:16 +02:00
Scalar::Ptr(ptr) => {
let alloc_type = cx.tcx.alloc_map.lock().get(ptr.alloc_id);
let base_addr = match alloc_type {
Some(AllocType::Memory(alloc)) => {
let init = const_alloc_to_llvm(cx, alloc);
2018-01-26 14:28:58 +01:00
if alloc.runtime_mutability == Mutability::Mutable {
consts::addr_of_mut(cx, init, alloc.align, "byte_str")
2018-01-16 09:31:48 +01:00
} else {
consts::addr_of(cx, init, alloc.align, "byte_str")
2018-01-16 09:31:48 +01:00
}
}
Some(AllocType::Function(fn_instance)) => {
callee::get_fn(cx, fn_instance)
}
Some(AllocType::Static(def_id)) => {
assert!(cx.tcx.is_static(def_id).is_some());
consts::get_static(cx, def_id)
}
None => bug!("missing allocation {:?}", ptr.alloc_id),
};
let llval = unsafe { llvm::LLVMConstInBoundsGEP(
consts::bitcast(base_addr, Type::i8p(cx)),
&C_usize(cx, ptr.offset.bytes()),
1,
) };
if layout.value != layout::Pointer {
unsafe { llvm::LLVMConstPtrToInt(llval, llty.to_ref()) }
} else {
consts::bitcast(llval, llty)
}
2018-01-16 09:31:48 +01: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);
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.iter() {
let offset = offset.bytes();
2018-01-16 09:31:48 +01:00
assert_eq!(offset as usize as u64, offset);
let offset = offset as usize;
if offset > next_offset {
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)],
).expect("const_alloc_to_llvm: could not read relocation pointer") as u64;
2018-05-22 19:30:16 +02:00
llvals.push(scalar_to_llvm(
cx,
2018-05-21 00:37:44 +02:00
Pointer { alloc_id, offset: Size::from_bytes(ptr_offset) }.into(),
&layout::Scalar {
2018-01-16 09:31:48 +01:00
value: layout::Primitive::Pointer,
valid_range: 0..=!0
},
Type::i8p(cx)
2018-01-16 09:31:48 +01:00
));
next_offset = offset + pointer_size;
}
if alloc.bytes.len() >= next_offset {
llvals.push(C_bytes(cx, &alloc.bytes[next_offset ..]));
2018-01-16 09:31:48 +01:00
}
C_struct(cx, &llvals, true)
2018-01-16 09:31:48 +01: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>,
def_id: DefId,
) -> Result<(ValueRef, &'tcx Allocation), Lrc<ConstEvalErr<'tcx>>> {
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
};
let param_env = ty::ParamEnv::reveal_all();
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-06-25 20:53:02 +02:00
ConstValue::ByRef(alloc, n) if n.bytes() == 0 => alloc,
_ => bug!("static const eval returned {:#?}", static_),
};
Ok((const_alloc_to_llvm(cx, alloc), alloc))
2018-01-16 09:31:48 +01:00
}
2018-01-16 09:31:48 +01:00
impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
2018-06-25 20:53:02 +02:00
fn fully_evaluate(
2018-01-16 09:31:48 +01:00
&mut self,
bx: &Builder<'a, 'tcx>,
2018-01-16 09:31:48 +01:00
constant: &'tcx ty::Const<'tcx>,
2018-06-25 20:53:02 +02:00
) -> Result<&'tcx ty::Const<'tcx>, Lrc<ConstEvalErr<'tcx>>> {
2018-01-16 09:31:48 +01:00
match constant.val {
2018-06-25 20:53:02 +02:00
ConstValue::Unevaluated(def_id, ref substs) => {
let tcx = bx.tcx();
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,
};
2018-06-25 20:53:02 +02:00
tcx.const_eval(param_env.and(cid))
2018-01-16 09:31:48 +01:00
},
2018-06-25 20:53:02 +02:00
_ => Ok(constant),
2017-01-02 11:00:42 -07:00
}
}
2018-06-25 20:53:02 +02:00
pub fn eval_mir_constant(
2018-01-16 09:31:48 +01:00
&mut self,
bx: &Builder<'a, 'tcx>,
2018-01-16 09:31:48 +01:00
constant: &mir::Constant<'tcx>,
2018-06-25 20:53:02 +02:00
) -> Result<&'tcx ty::Const<'tcx>, Lrc<ConstEvalErr<'tcx>>> {
let c = self.monomorphize(&constant.literal);
self.fully_evaluate(bx, c)
}
2017-01-02 11:00:42 -07:00
/// process constant containing SIMD shuffle indices
pub fn simd_shuffle_indices(
&mut self,
bx: &Builder<'a, 'tcx>,
span: Span,
ty: Ty<'tcx>,
constant: Result<&'tcx ty::Const<'tcx>, Lrc<ConstEvalErr<'tcx>>>,
) -> (ValueRef, Ty<'tcx>) {
constant
2018-01-16 09:31:48 +01:00
.and_then(|c| {
2018-06-25 20:53:02 +02:00
let field_ty = c.ty.builtin_index().unwrap();
let fields = match c.ty.sty {
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
};
let values: Result<Vec<ValueRef>, Lrc<_>> = (0..fields).map(|field| {
2018-01-29 09:58:28 +01:00
let field = const_val_field(
bx.tcx(),
ty::ParamEnv::reveal_all(),
2018-01-29 09:58:28 +01:00
self.instance,
None,
mir::Field::new(field as usize),
c,
)?;
2018-05-22 19:30:16 +02:00
if let Some(prim) = field.to_scalar() {
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)
};
2018-05-22 19:30:16 +02:00
Ok(scalar_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-06-25 20:53:02 +02:00
Ok((llval, c.ty))
2018-01-16 09:31:48 +01:00
})
.unwrap_or_else(|e| {
2018-06-02 23:38:57 +02:00
e.report_as_error(
bx.tcx().at(span),
2018-06-02 23:38:57 +02:00
"could not evaluate shuffle_indices at compile time",
);
2018-01-16 09:31:48 +01:00
// We've errored, so we don't have to produce working code.
let ty = self.monomorphize(&ty);
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
}
}