2019-04-11 15:06:42 +02:00
|
|
|
use rustc::mir::interpret::{ConstValue, ErrorHandled, Pointer, Scalar};
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir;
|
2018-01-30 09:40:46 +01:00
|
|
|
use rustc::ty;
|
2019-04-11 15:06:42 +02:00
|
|
|
use rustc::ty::layout::{self, Align, LayoutOf, TyLayout, Size};
|
2016-06-07 17:28:36 +03:00
|
|
|
|
2019-02-09 23:31:47 +09:00
|
|
|
use crate::base;
|
|
|
|
use crate::MemFlags;
|
|
|
|
use crate::glue;
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2019-02-09 23:31:47 +09:00
|
|
|
use crate::traits::*;
|
2018-08-07 17:14:40 +02:00
|
|
|
|
2016-02-18 19:49:45 +02:00
|
|
|
use std::fmt;
|
|
|
|
|
2018-01-05 07:34:28 +02:00
|
|
|
use super::{FunctionCx, LocalRef};
|
2017-12-01 19:16:39 +02:00
|
|
|
use super::place::PlaceRef;
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2015-11-13 00:12:50 +02:00
|
|
|
/// The representation of a Rust value. The enum variant is in fact
|
|
|
|
/// uniquely determined by the value's type, but is kept as a
|
2015-11-10 22:05:11 +02:00
|
|
|
/// safety check.
|
2018-07-10 13:28:39 +03:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-08-02 17:48:44 +03:00
|
|
|
pub enum OperandValue<V> {
|
2015-11-10 22:05:11 +02:00
|
|
|
/// A reference to the actual operand. The data is guaranteed
|
|
|
|
/// to be valid for the operand's lifetime.
|
2018-08-03 23:50:13 +09:00
|
|
|
/// The second value, if any, is the extra data (vtable or length)
|
|
|
|
/// which indicates that it refers to an unsized rvalue.
|
2018-09-09 01:16:45 +03:00
|
|
|
Ref(V, Option<V>, Align),
|
2015-11-10 22:05:11 +02:00
|
|
|
/// A single LLVM value.
|
2018-08-02 17:48:44 +03:00
|
|
|
Immediate(V),
|
2016-05-25 11:55:44 +03:00
|
|
|
/// A pair of immediate LLVM values. Used by fat pointers too.
|
2018-08-02 17:48:44 +03:00
|
|
|
Pair(V, V)
|
2017-09-20 18:17:23 +03:00
|
|
|
}
|
|
|
|
|
2015-11-13 00:12:50 +02:00
|
|
|
/// An `OperandRef` is an "SSA" reference to a Rust value, along with
|
|
|
|
/// its type.
|
|
|
|
///
|
|
|
|
/// NOTE: unless you know a value's type exactly, you should not
|
|
|
|
/// generate LLVM opcodes acting on it and instead act via methods,
|
2017-06-25 12:41:24 +03:00
|
|
|
/// to avoid nasty edge cases. In particular, using `Builder::store`
|
|
|
|
/// directly is sure to cause problems -- use `OperandRef::store`
|
2016-02-01 11:04:46 +01:00
|
|
|
/// instead.
|
2015-11-03 06:35:09 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2018-08-02 17:48:44 +03:00
|
|
|
pub struct OperandRef<'tcx, V> {
|
2015-11-13 00:12:50 +02:00
|
|
|
// The value.
|
2018-08-02 17:48:44 +03:00
|
|
|
pub val: OperandValue<V>,
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2017-09-20 18:17:23 +03:00
|
|
|
// The layout of value, based on its Rust type.
|
2017-09-21 20:40:50 +03:00
|
|
|
pub layout: TyLayout<'tcx>,
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
impl<V: CodegenObject> fmt::Debug for OperandRef<'tcx, V> {
|
2019-02-25 08:52:46 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-09-20 18:17:23 +03:00
|
|
|
write!(f, "OperandRef({:?} @ {:?})", self.val, self.layout)
|
2015-11-10 22:05:11 +02:00
|
|
|
}
|
2016-02-18 19:49:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
|
2019-03-01 15:03:48 +01:00
|
|
|
pub fn new_zst<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
|
|
|
bx: &mut Bx,
|
2018-09-14 17:48:57 +02:00
|
|
|
layout: TyLayout<'tcx>
|
|
|
|
) -> OperandRef<'tcx, V> {
|
2017-09-20 18:17:23 +03:00
|
|
|
assert!(layout.is_zst());
|
2017-10-05 04:22:23 +03:00
|
|
|
OperandRef {
|
2019-03-01 15:03:48 +01:00
|
|
|
val: OperandValue::Immediate(bx.const_undef(bx.immediate_backend_type(layout))),
|
2017-10-05 04:22:23 +03:00
|
|
|
layout
|
|
|
|
}
|
2017-03-08 20:03:04 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2019-04-03 15:29:31 +02:00
|
|
|
val: &'tcx ty::Const<'tcx>
|
2019-04-22 13:53:52 +02:00
|
|
|
) -> Self {
|
2019-03-01 15:03:48 +01:00
|
|
|
let layout = bx.layout_of(val.ty);
|
2018-01-16 09:31:48 +01:00
|
|
|
|
|
|
|
if layout.is_zst() {
|
2019-04-22 13:53:52 +02:00
|
|
|
return OperandRef::new_zst(bx, layout);
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-25 20:53:02 +02:00
|
|
|
let val = match val.val {
|
2019-03-14 10:19:31 +01:00
|
|
|
ConstValue::Unevaluated(..) => bug!("unevaluated constant in `OperandRef::from_const`"),
|
2019-02-20 01:11:10 +00:00
|
|
|
ConstValue::Param(_) => bug!("encountered a ConstValue::Param in codegen"),
|
|
|
|
ConstValue::Infer(_) => bug!("encountered a ConstValue::Infer in codegen"),
|
2019-03-12 20:25:41 +00:00
|
|
|
ConstValue::Placeholder(_) => bug!("encountered a ConstValue::Placeholder in codegen"),
|
2018-05-20 23:46:30 +02:00
|
|
|
ConstValue::Scalar(x) => {
|
2018-01-16 09:31:48 +01:00
|
|
|
let scalar = match layout.abi {
|
|
|
|
layout::Abi::Scalar(ref x) => x,
|
|
|
|
_ => bug!("from_const: invalid ByVal layout: {:#?}", layout)
|
|
|
|
};
|
2019-03-01 15:03:48 +01:00
|
|
|
let llval = bx.scalar_to_backend(
|
2018-01-16 09:31:48 +01:00
|
|
|
x,
|
|
|
|
scalar,
|
2019-03-01 15:03:48 +01:00
|
|
|
bx.immediate_backend_type(layout),
|
2018-01-16 09:31:48 +01:00
|
|
|
);
|
|
|
|
OperandValue::Immediate(llval)
|
|
|
|
},
|
2019-04-11 15:06:42 +02:00
|
|
|
ConstValue::Slice { data, start, end } => {
|
2019-01-08 13:49:37 +01:00
|
|
|
let a_scalar = match layout.abi {
|
|
|
|
layout::Abi::ScalarPair(ref a, _) => a,
|
2018-05-20 23:46:30 +02:00
|
|
|
_ => bug!("from_const: invalid ScalarPair layout: {:#?}", layout)
|
2018-01-16 09:31:48 +01:00
|
|
|
};
|
2019-04-11 15:06:42 +02:00
|
|
|
let a = Scalar::from(Pointer::new(
|
2019-05-30 13:05:05 +02:00
|
|
|
bx.tcx().alloc_map.lock().create_memory_alloc(data),
|
2019-04-11 15:06:42 +02:00
|
|
|
Size::from_bytes(start as u64),
|
|
|
|
)).into();
|
2019-03-01 15:03:48 +01:00
|
|
|
let a_llval = bx.scalar_to_backend(
|
2018-01-16 09:31:48 +01:00
|
|
|
a,
|
|
|
|
a_scalar,
|
2019-03-01 15:03:48 +01:00
|
|
|
bx.scalar_pair_element_backend_type(layout, 0, true),
|
2018-01-16 09:31:48 +01:00
|
|
|
);
|
2019-04-11 15:06:42 +02:00
|
|
|
let b_llval = bx.const_usize((end - start) as u64);
|
2018-01-16 09:31:48 +01:00
|
|
|
OperandValue::Pair(a_llval, b_llval)
|
|
|
|
},
|
2019-06-19 19:43:13 +02:00
|
|
|
ConstValue::ByRef { offset, align, alloc } => {
|
2019-06-19 15:58:51 +02:00
|
|
|
return bx.load_operand(bx.from_const_alloc(layout, align, alloc, offset));
|
2018-01-16 09:31:48 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-04-22 13:53:52 +02:00
|
|
|
OperandRef {
|
2018-01-16 09:31:48 +01:00
|
|
|
val,
|
|
|
|
layout
|
2019-04-22 13:53:52 +02:00
|
|
|
}
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
|
|
|
|
2016-02-18 19:49:45 +02:00
|
|
|
/// Asserts that this operand refers to a scalar and returns
|
|
|
|
/// a reference to its value.
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn immediate(self) -> V {
|
2016-02-18 19:49:45 +02:00
|
|
|
match self.val {
|
|
|
|
OperandValue::Immediate(s) => s,
|
2016-12-31 04:55:29 +02:00
|
|
|
_ => bug!("not immediate: {:?}", self)
|
2016-02-18 19:49:45 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-25 11:55:44 +03:00
|
|
|
|
2019-03-01 15:03:48 +01:00
|
|
|
pub fn deref<Cx: LayoutTypeMethods<'tcx>>(
|
2018-09-20 15:47:22 +02:00
|
|
|
self,
|
|
|
|
cx: &Cx
|
|
|
|
) -> PlaceRef<'tcx, V> {
|
2018-01-28 23:29:40 +02:00
|
|
|
let projected_ty = self.layout.ty.builtin_deref(true)
|
2017-05-18 18:43:52 +03:00
|
|
|
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", self)).ty;
|
2017-03-14 01:08:21 +02:00
|
|
|
let (llptr, llextra) = match self.val {
|
2018-07-10 13:28:39 +03:00
|
|
|
OperandValue::Immediate(llptr) => (llptr, None),
|
|
|
|
OperandValue::Pair(llptr, llextra) => (llptr, Some(llextra)),
|
2018-08-03 23:50:13 +09:00
|
|
|
OperandValue::Ref(..) => bug!("Deref of by-Ref operand {:?}", self)
|
2017-03-14 01:08:21 +02:00
|
|
|
};
|
2018-01-05 07:04:08 +02:00
|
|
|
let layout = cx.layout_of(projected_ty);
|
2017-12-01 14:31:47 +02:00
|
|
|
PlaceRef {
|
2017-03-14 01:08:21 +02:00
|
|
|
llval: llptr,
|
2017-08-06 22:54:09 -07:00
|
|
|
llextra,
|
2017-12-01 19:16:39 +02:00
|
|
|
layout,
|
2018-09-09 01:16:45 +03:00
|
|
|
align: layout.align.abi,
|
2017-03-14 01:08:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 04:22:23 +03:00
|
|
|
/// If this operand is a `Pair`, we return an aggregate with the two values.
|
|
|
|
/// For other cases, see `immediate`.
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn immediate_or_packed_pair<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
|
|
|
self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx
|
2018-09-20 15:47:22 +02:00
|
|
|
) -> V {
|
2016-05-25 11:55:44 +03:00
|
|
|
if let OperandValue::Pair(a, b) = self.val {
|
2018-09-20 15:47:22 +02:00
|
|
|
let llty = bx.cx().backend_type(self.layout);
|
2017-10-05 04:22:23 +03:00
|
|
|
debug!("Operand::immediate_or_packed_pair: packing {:?} into {:?}",
|
|
|
|
self, llty);
|
2017-09-15 22:42:23 +03:00
|
|
|
// Reconstruct the immediate aggregate.
|
2018-09-06 11:57:42 -07:00
|
|
|
let mut llpair = bx.cx().const_undef(llty);
|
2018-10-05 15:08:49 +02:00
|
|
|
let imm_a = base::from_immediate(bx, a);
|
|
|
|
let imm_b = base::from_immediate(bx, b);
|
|
|
|
llpair = bx.insert_value(llpair, imm_a, 0);
|
|
|
|
llpair = bx.insert_value(llpair, imm_b, 1);
|
2017-10-05 04:22:23 +03:00
|
|
|
llpair
|
|
|
|
} else {
|
|
|
|
self.immediate()
|
2016-05-25 11:55:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 04:22:23 +03:00
|
|
|
/// If the type is a pair, we return a `Pair`, otherwise, an `Immediate`.
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn from_immediate_or_packed_pair<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
llval: V,
|
|
|
|
layout: TyLayout<'tcx>
|
|
|
|
) -> Self {
|
Store scalar pair bools as i8 in memory
We represent `bool` as `i1` in a `ScalarPair`, unlike other aggregates,
to optimize IR for checked operators and the like. With this patch, we
still do so when the pair is an immediate value, but we use the `i8`
memory type when the value is loaded or stored as an LLVM aggregate.
So `(bool, bool)` looks like an `{ i1, i1 }` immediate, but `{ i8, i8 }`
in memory. When a pair is a direct function argument, `PassMode::Pair`,
it is still passed using the immediate `i1` type, but as a return value
it will use the `i8` memory type. Also, `bool`-like` enum tags will now
use scalar pairs when possible, where they were previously excluded due
to optimization issues.
2018-06-15 15:47:54 -07:00
|
|
|
let val = if let layout::Abi::ScalarPair(ref a, ref b) = layout.abi {
|
2017-10-05 04:22:23 +03:00
|
|
|
debug!("Operand::from_immediate_or_packed_pair: unpacking {:?} @ {:?}",
|
|
|
|
llval, layout);
|
2016-06-20 23:55:14 +03:00
|
|
|
|
2017-10-05 04:22:23 +03:00
|
|
|
// Deconstruct the immediate aggregate.
|
2018-10-05 15:08:49 +02:00
|
|
|
let a_llval = bx.extract_value(llval, 0);
|
|
|
|
let a_llval = base::to_immediate_scalar(bx, a_llval, a);
|
|
|
|
let b_llval = bx.extract_value(llval, 1);
|
|
|
|
let b_llval = base::to_immediate_scalar(bx, b_llval, b);
|
Store scalar pair bools as i8 in memory
We represent `bool` as `i1` in a `ScalarPair`, unlike other aggregates,
to optimize IR for checked operators and the like. With this patch, we
still do so when the pair is an immediate value, but we use the `i8`
memory type when the value is loaded or stored as an LLVM aggregate.
So `(bool, bool)` looks like an `{ i1, i1 }` immediate, but `{ i8, i8 }`
in memory. When a pair is a direct function argument, `PassMode::Pair`,
it is still passed using the immediate `i1` type, but as a return value
it will use the `i8` memory type. Also, `bool`-like` enum tags will now
use scalar pairs when possible, where they were previously excluded due
to optimization issues.
2018-06-15 15:47:54 -07:00
|
|
|
OperandValue::Pair(a_llval, b_llval)
|
2017-10-05 04:22:23 +03:00
|
|
|
} else {
|
|
|
|
OperandValue::Immediate(llval)
|
|
|
|
};
|
|
|
|
OperandRef { val, layout }
|
2016-05-25 11:55:44 +03:00
|
|
|
}
|
2017-10-09 19:56:41 +03:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn extract_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
2018-08-02 17:48:44 +03:00
|
|
|
&self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
i: usize
|
|
|
|
) -> Self {
|
2018-08-28 17:50:57 +02:00
|
|
|
let field = self.layout.field(bx.cx(), i);
|
2017-10-09 19:56:41 +03:00
|
|
|
let offset = self.layout.fields.offset(i);
|
|
|
|
|
|
|
|
let mut val = match (self.val, &self.layout.abi) {
|
2018-05-10 19:24:06 +03:00
|
|
|
// If the field is ZST, it has no data.
|
|
|
|
_ if field.is_zst() => {
|
2019-03-01 15:03:48 +01:00
|
|
|
return OperandRef::new_zst(bx, field);
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
|
|
|
|
2017-12-13 01:57:56 +02:00
|
|
|
// Newtype of a scalar, scalar pair or vector.
|
2017-10-09 19:56:41 +03:00
|
|
|
(OperandValue::Immediate(_), _) |
|
|
|
|
(OperandValue::Pair(..), _) if field.size == self.layout.size => {
|
|
|
|
assert_eq!(offset.bytes(), 0);
|
|
|
|
self.val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract a scalar component from a pair.
|
|
|
|
(OperandValue::Pair(a_llval, b_llval), &layout::Abi::ScalarPair(ref a, ref b)) => {
|
|
|
|
if offset.bytes() == 0 {
|
2018-08-28 17:50:57 +02:00
|
|
|
assert_eq!(field.size, a.value.size(bx.cx()));
|
2017-10-09 19:56:41 +03:00
|
|
|
OperandValue::Immediate(a_llval)
|
|
|
|
} else {
|
2018-08-28 17:50:57 +02:00
|
|
|
assert_eq!(offset, a.value.size(bx.cx())
|
2018-09-09 01:16:45 +03:00
|
|
|
.align_to(b.value.align(bx.cx()).abi));
|
2018-08-28 17:50:57 +02:00
|
|
|
assert_eq!(field.size, b.value.size(bx.cx()));
|
2017-10-09 19:56:41 +03:00
|
|
|
OperandValue::Immediate(b_llval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `#[repr(simd)]` types are also immediate.
|
2017-12-13 01:57:56 +02:00
|
|
|
(OperandValue::Immediate(llval), &layout::Abi::Vector { .. }) => {
|
2017-10-09 19:56:41 +03:00
|
|
|
OperandValue::Immediate(
|
2018-09-06 11:57:42 -07:00
|
|
|
bx.extract_element(llval, bx.cx().const_usize(i as u64)))
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_ => bug!("OperandRef::extract_field({:?}): not applicable", self)
|
|
|
|
};
|
|
|
|
|
|
|
|
// HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
|
2018-11-20 13:24:41 +01:00
|
|
|
// Bools in union fields needs to be truncated.
|
|
|
|
let to_immediate_or_cast = |bx: &mut Bx, val, ty| {
|
2018-11-20 12:14:53 +01:00
|
|
|
if ty == bx.cx().type_i1() {
|
|
|
|
bx.trunc(val, ty)
|
|
|
|
} else {
|
|
|
|
bx.bitcast(val, ty)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-09 19:56:41 +03:00
|
|
|
match val {
|
|
|
|
OperandValue::Immediate(ref mut llval) => {
|
2018-11-20 13:24:41 +01:00
|
|
|
*llval = to_immediate_or_cast(bx, *llval, bx.cx().immediate_backend_type(field));
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
|
|
|
OperandValue::Pair(ref mut a, ref mut b) => {
|
2018-11-20 13:24:41 +01:00
|
|
|
*a = to_immediate_or_cast(bx, *a, bx.cx()
|
|
|
|
.scalar_pair_element_backend_type(field, 0, true));
|
|
|
|
*b = to_immediate_or_cast(bx, *b, bx.cx()
|
|
|
|
.scalar_pair_element_backend_type(field, 1, true));
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
2018-08-03 23:50:13 +09:00
|
|
|
OperandValue::Ref(..) => bug!()
|
2017-10-09 19:56:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
OperandRef {
|
|
|
|
val,
|
|
|
|
layout: field
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
|
2018-09-14 17:48:57 +02:00
|
|
|
pub fn store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
|
|
|
self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
dest: PlaceRef<'tcx, V>
|
2018-09-14 17:48:57 +02:00
|
|
|
) {
|
2018-05-11 12:26:32 +02:00
|
|
|
self.store_with_flags(bx, dest, MemFlags::empty());
|
2018-05-11 11:26:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn volatile_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
2018-08-07 17:14:40 +02:00
|
|
|
self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
dest: PlaceRef<'tcx, V>
|
2018-08-07 17:14:40 +02:00
|
|
|
) {
|
2018-05-11 12:26:32 +02:00
|
|
|
self.store_with_flags(bx, dest, MemFlags::VOLATILE);
|
2018-05-11 11:26:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn unaligned_volatile_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
2018-08-02 17:48:44 +03:00
|
|
|
self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
dest: PlaceRef<'tcx, V>,
|
2018-08-02 17:48:44 +03:00
|
|
|
) {
|
2018-07-14 23:28:39 +01:00
|
|
|
self.store_with_flags(bx, dest, MemFlags::VOLATILE | MemFlags::UNALIGNED);
|
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn nontemporal_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
2018-08-07 17:14:40 +02:00
|
|
|
self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
dest: PlaceRef<'tcx, V>
|
2018-08-07 17:14:40 +02:00
|
|
|
) {
|
2018-05-11 12:26:32 +02:00
|
|
|
self.store_with_flags(bx, dest, MemFlags::NONTEMPORAL);
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:48:57 +02:00
|
|
|
fn store_with_flags<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
2018-07-17 18:26:58 +03:00
|
|
|
self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
dest: PlaceRef<'tcx, V>,
|
2018-07-17 18:26:58 +03:00
|
|
|
flags: MemFlags,
|
|
|
|
) {
|
2017-06-25 12:41:24 +03:00
|
|
|
debug!("OperandRef::store: operand={:?}, dest={:?}", self, dest);
|
|
|
|
// Avoid generating stores of zero-sized values, because the only way to have a zero-sized
|
|
|
|
// value is through `undef`, and store itself is useless.
|
2017-09-20 18:17:23 +03:00
|
|
|
if dest.layout.is_zst() {
|
2017-06-25 12:41:24 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
match self {
|
2018-08-03 23:50:13 +09:00
|
|
|
OperandValue::Ref(r, None, source_align) => {
|
2018-11-02 23:38:16 +01:00
|
|
|
base::memcpy_ty(bx, dest.llval, dest.align, r, source_align,
|
|
|
|
dest.layout, flags)
|
2018-05-11 12:26:32 +02:00
|
|
|
}
|
2018-08-03 23:50:13 +09:00
|
|
|
OperandValue::Ref(_, Some(_), _) => {
|
2018-05-29 00:12:55 +09:00
|
|
|
bug!("cannot directly store unsized values");
|
|
|
|
}
|
2017-06-25 12:41:24 +03:00
|
|
|
OperandValue::Immediate(s) => {
|
2018-05-11 11:26:51 +02:00
|
|
|
let val = base::from_immediate(bx, s);
|
2018-05-11 12:26:32 +02:00
|
|
|
bx.store_with_flags(val, dest.llval, dest.align, flags);
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
|
|
|
OperandValue::Pair(a, b) => {
|
2018-11-28 00:25:40 +01:00
|
|
|
let (a_scalar, b_scalar) = match dest.layout.abi {
|
|
|
|
layout::Abi::ScalarPair(ref a, ref b) => (a, b),
|
|
|
|
_ => bug!("store_with_flags: invalid ScalarPair layout: {:#?}", dest.layout)
|
|
|
|
};
|
|
|
|
let b_offset = a_scalar.value.size(bx).align_to(b_scalar.value.align(bx).abi);
|
|
|
|
|
|
|
|
let llptr = bx.struct_gep(dest.llval, 0);
|
|
|
|
let val = base::from_immediate(bx, a);
|
|
|
|
let align = dest.align;
|
|
|
|
bx.store_with_flags(val, llptr, align, flags);
|
|
|
|
|
|
|
|
let llptr = bx.struct_gep(dest.llval, 1);
|
|
|
|
let val = base::from_immediate(bx, b);
|
|
|
|
let align = dest.align.restrict_for_offset(b_offset);
|
|
|
|
bx.store_with_flags(val, llptr, align, flags);
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
}
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn store_unsized<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
|
2018-08-02 17:48:44 +03:00
|
|
|
self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
indirect_dest: PlaceRef<'tcx, V>
|
2018-08-02 17:48:44 +03:00
|
|
|
) {
|
2018-05-29 00:12:55 +09:00
|
|
|
debug!("OperandRef::store_unsized: operand={:?}, indirect_dest={:?}", self, indirect_dest);
|
|
|
|
let flags = MemFlags::empty();
|
|
|
|
|
|
|
|
// `indirect_dest` must have `*mut T` type. We extract `T` out of it.
|
|
|
|
let unsized_ty = indirect_dest.layout.ty.builtin_deref(true)
|
|
|
|
.unwrap_or_else(|| bug!("indirect_dest has non-pointer type: {:?}", indirect_dest)).ty;
|
|
|
|
|
|
|
|
let (llptr, llextra) =
|
2018-08-03 23:50:13 +09:00
|
|
|
if let OperandValue::Ref(llptr, Some(llextra), _) = self {
|
2018-05-29 00:12:55 +09:00
|
|
|
(llptr, llextra)
|
|
|
|
} else {
|
|
|
|
bug!("store_unsized called with a sized value")
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: choose an appropriate alignment, or use dynamic align somehow
|
2018-09-09 01:16:45 +03:00
|
|
|
let max_align = Align::from_bits(128).unwrap();
|
|
|
|
let min_align = Align::from_bits(8).unwrap();
|
2018-05-29 00:12:55 +09:00
|
|
|
|
|
|
|
// Allocate an appropriate region on the stack, and copy the value into it
|
2018-08-07 17:14:40 +02:00
|
|
|
let (llsize, _) = glue::size_and_align_of_dst(bx, unsized_ty, Some(llextra));
|
2018-09-06 13:52:15 -07:00
|
|
|
let lldst = bx.array_alloca(bx.cx().type_i8(), llsize, "unsized_tmp", max_align);
|
2018-09-10 17:59:20 +02:00
|
|
|
bx.memcpy(lldst, max_align, llptr, min_align, llsize, flags);
|
2018-05-29 00:12:55 +09:00
|
|
|
|
|
|
|
// Store the allocated region and the extra to the indirect place.
|
|
|
|
let indirect_operand = OperandValue::Pair(lldst, llextra);
|
2018-08-07 17:14:40 +02:00
|
|
|
indirect_operand.store(bx, indirect_dest);
|
2018-05-29 00:12:55 +09:00
|
|
|
}
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2018-09-20 15:47:22 +02:00
|
|
|
fn maybe_codegen_consume_direct(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
place: &mir::Place<'tcx>
|
|
|
|
) -> Option<OperandRef<'tcx, Bx::Value>> {
|
2018-05-08 16:10:16 +03:00
|
|
|
debug!("maybe_codegen_consume_direct(place={:?})", place);
|
2016-06-09 18:13:16 +03:00
|
|
|
|
2019-05-22 05:51:50 +02:00
|
|
|
place.iterate(|place_base, place_projection| {
|
|
|
|
if let mir::PlaceBase::Local(index) = place_base {
|
|
|
|
match self.locals[*index] {
|
|
|
|
LocalRef::Operand(Some(mut o)) => {
|
|
|
|
// Moves out of scalar and scalar pair fields are trivial.
|
|
|
|
for proj in place_projection {
|
|
|
|
match proj.elem {
|
|
|
|
mir::ProjectionElem::Field(ref f, _) => {
|
|
|
|
o = o.extract_field(bx, f.index());
|
|
|
|
}
|
|
|
|
mir::ProjectionElem::Index(_) |
|
|
|
|
mir::ProjectionElem::ConstantIndex { .. } => {
|
|
|
|
// ZSTs don't require any actual memory access.
|
|
|
|
// FIXME(eddyb) deduplicate this with the identical
|
|
|
|
// checks in `codegen_consume` and `extract_field`.
|
|
|
|
let elem = o.layout.field(bx.cx(), 0);
|
|
|
|
if elem.is_zst() {
|
|
|
|
o = OperandRef::new_zst(bx, elem);
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 06:35:09 -05:00
|
|
|
|
2019-05-22 05:51:50 +02:00
|
|
|
Some(o)
|
2017-12-26 03:20:35 +02:00
|
|
|
}
|
2019-05-22 05:51:50 +02:00
|
|
|
LocalRef::Operand(None) => {
|
|
|
|
bug!("use of {:?} before def", place);
|
|
|
|
}
|
|
|
|
LocalRef::Place(..) | LocalRef::UnsizedPlace(..) => {
|
|
|
|
// watch out for locals that do not have an
|
|
|
|
// alloca; they are handled somewhat differently
|
|
|
|
None
|
2017-12-26 03:20:35 +02:00
|
|
|
}
|
2016-05-25 11:58:08 +03:00
|
|
|
}
|
2019-05-22 05:51:50 +02:00
|
|
|
} else {
|
|
|
|
None
|
2016-06-09 18:13:16 +03:00
|
|
|
}
|
2019-05-22 05:51:50 +02:00
|
|
|
})
|
2017-10-09 00:38:10 +03:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn codegen_consume(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
place: &mir::Place<'tcx>
|
|
|
|
) -> OperandRef<'tcx, Bx::Value> {
|
2018-05-08 16:10:16 +03:00
|
|
|
debug!("codegen_consume(place={:?})", place);
|
2017-10-09 00:38:10 +03:00
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
let ty = self.monomorphized_place_ty(place);
|
2018-08-28 17:50:57 +02:00
|
|
|
let layout = bx.cx().layout_of(ty);
|
2017-10-09 00:38:10 +03:00
|
|
|
|
|
|
|
// ZSTs don't require any actual memory access.
|
|
|
|
if layout.is_zst() {
|
2019-03-01 15:03:48 +01:00
|
|
|
return OperandRef::new_zst(bx, layout);
|
2017-10-09 00:38:10 +03:00
|
|
|
}
|
|
|
|
|
2018-05-08 16:10:16 +03:00
|
|
|
if let Some(o) = self.maybe_codegen_consume_direct(bx, place) {
|
2017-10-09 00:38:10 +03:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
// for most places, to consume them we just load them
|
2016-06-09 18:13:16 +03:00
|
|
|
// out from their home
|
2018-10-05 15:08:49 +02:00
|
|
|
let place = self.codegen_place(bx, place);
|
|
|
|
bx.load_operand(place)
|
2016-06-09 18:13:16 +03:00
|
|
|
}
|
2016-05-25 11:58:08 +03:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
pub fn codegen_operand(
|
|
|
|
&mut self,
|
2018-10-05 15:08:49 +02:00
|
|
|
bx: &mut Bx,
|
2018-09-20 15:47:22 +02:00
|
|
|
operand: &mir::Operand<'tcx>
|
|
|
|
) -> OperandRef<'tcx, Bx::Value> {
|
2018-05-08 16:10:16 +03:00
|
|
|
debug!("codegen_operand(operand={:?})", operand);
|
2016-06-09 18:13:16 +03:00
|
|
|
|
|
|
|
match *operand {
|
2017-12-01 14:39:51 +02:00
|
|
|
mir::Operand::Copy(ref place) |
|
|
|
|
mir::Operand::Move(ref place) => {
|
2018-05-08 16:10:16 +03:00
|
|
|
self.codegen_consume(bx, place)
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::Operand::Constant(ref constant) => {
|
2018-01-16 09:31:48 +01:00
|
|
|
let ty = self.monomorphize(&constant.ty);
|
2018-12-02 13:57:41 +01:00
|
|
|
self.eval_mir_constant(constant)
|
2019-04-22 13:53:52 +02:00
|
|
|
.map(|c| OperandRef::from_const(bx, c))
|
2018-01-16 09:31:48 +01:00
|
|
|
.unwrap_or_else(|err| {
|
2018-08-26 15:19:34 +02:00
|
|
|
match err {
|
|
|
|
// errored or at least linted
|
|
|
|
ErrorHandled::Reported => {},
|
|
|
|
ErrorHandled::TooGeneric => {
|
|
|
|
bug!("codgen encountered polymorphic constant")
|
|
|
|
},
|
|
|
|
}
|
2018-07-22 01:01:07 +02:00
|
|
|
// Allow RalfJ to sleep soundly knowing that even refactorings that remove
|
|
|
|
// the above error (or silence it under some conditions) will not cause UB
|
2018-11-24 16:01:47 +01:00
|
|
|
bx.abort();
|
2018-01-16 09:31:48 +01:00
|
|
|
// We've errored, so we don't have to produce working code.
|
2018-08-28 17:50:57 +02:00
|
|
|
let layout = bx.cx().layout_of(ty);
|
2018-09-14 17:48:57 +02:00
|
|
|
bx.load_operand(PlaceRef::new_sized(
|
2018-09-20 15:47:22 +02:00
|
|
|
bx.cx().const_undef(bx.cx().type_ptr_to(bx.cx().backend_type(layout))),
|
2018-01-16 09:31:48 +01:00
|
|
|
layout,
|
2018-09-09 01:16:45 +03:00
|
|
|
layout.align.abi,
|
2018-09-14 17:48:57 +02:00
|
|
|
))
|
2018-01-16 09:31:48 +01:00
|
|
|
})
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|