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.
|
|
|
|
|
|
|
|
use llvm::ValueRef;
|
2017-09-20 18:17:23 +03:00
|
|
|
use rustc::ty;
|
2017-09-21 20:40:50 +03:00
|
|
|
use rustc::ty::layout::{LayoutOf, TyLayout};
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir;
|
2016-06-07 17:28:36 +03:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
|
|
|
|
2016-03-22 19:23:36 +02:00
|
|
|
use base;
|
2017-10-05 04:22:23 +03:00
|
|
|
use common::{CrateContext, C_undef};
|
2016-12-31 16:00:24 -07:00
|
|
|
use builder::Builder;
|
2016-03-22 19:23:36 +02:00
|
|
|
use value::Value;
|
2017-06-26 14:57:50 +03:00
|
|
|
use type_of::LayoutLlvmExt;
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-02-18 19:49:45 +02:00
|
|
|
use std::fmt;
|
2017-03-14 01:08:21 +02:00
|
|
|
use std::ptr;
|
2016-02-18 19:49:45 +02:00
|
|
|
|
2016-06-20 23:55:14 +03:00
|
|
|
use super::{MirContext, LocalRef};
|
2017-03-14 01:08:21 +02:00
|
|
|
use super::lvalue::{Alignment, LvalueRef};
|
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.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum OperandValue {
|
|
|
|
/// A reference to the actual operand. The data is guaranteed
|
|
|
|
/// to be valid for the operand's lifetime.
|
2017-02-06 17:27:09 +01:00
|
|
|
Ref(ValueRef, Alignment),
|
2015-11-10 22:05:11 +02:00
|
|
|
/// A single LLVM value.
|
2015-11-13 00:12:50 +02:00
|
|
|
Immediate(ValueRef),
|
2016-05-25 11:55:44 +03:00
|
|
|
/// A pair of immediate LLVM values. Used by fat pointers too.
|
|
|
|
Pair(ValueRef, ValueRef)
|
2015-11-10 22:05:11 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 18:17:23 +03:00
|
|
|
impl fmt::Debug for OperandValue {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
OperandValue::Ref(r, align) => {
|
|
|
|
write!(f, "Ref({:?}, {:?})", Value(r), align)
|
|
|
|
}
|
|
|
|
OperandValue::Immediate(i) => {
|
|
|
|
write!(f, "Immediate({:?})", Value(i))
|
|
|
|
}
|
|
|
|
OperandValue::Pair(a, b) => {
|
|
|
|
write!(f, "Pair({:?}, {:?})", Value(a), Value(b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)]
|
2015-10-21 17:42:25 -04:00
|
|
|
pub struct OperandRef<'tcx> {
|
2015-11-13 00:12:50 +02:00
|
|
|
// The value.
|
2015-11-10 22:05:11 +02:00
|
|
|
pub val: OperandValue,
|
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
|
|
|
}
|
|
|
|
|
2016-02-18 19:49:45 +02:00
|
|
|
impl<'tcx> fmt::Debug for OperandRef<'tcx> {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-17 19:54:32 -07:00
|
|
|
impl<'a, 'tcx> OperandRef<'tcx> {
|
2017-03-08 20:03:04 +02:00
|
|
|
pub fn new_zst(ccx: &CrateContext<'a, 'tcx>,
|
2017-09-21 20:40:50 +03:00
|
|
|
layout: TyLayout<'tcx>) -> OperandRef<'tcx> {
|
2017-09-20 18:17:23 +03:00
|
|
|
assert!(layout.is_zst());
|
2017-10-05 04:22:23 +03:00
|
|
|
OperandRef {
|
|
|
|
val: OperandValue::Immediate(C_undef(layout.llvm_type(ccx))),
|
|
|
|
layout
|
|
|
|
}
|
2017-03-08 20:03:04 +02:00
|
|
|
}
|
|
|
|
|
2016-02-18 19:49:45 +02:00
|
|
|
/// Asserts that this operand refers to a scalar and returns
|
|
|
|
/// a reference to its value.
|
|
|
|
pub fn immediate(self) -> ValueRef {
|
|
|
|
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
|
|
|
|
2017-09-20 18:17:23 +03:00
|
|
|
pub fn deref(self, ccx: &CrateContext<'a, 'tcx>) -> LvalueRef<'tcx> {
|
|
|
|
let projected_ty = self.layout.ty.builtin_deref(true, ty::NoPreference)
|
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 {
|
|
|
|
OperandValue::Immediate(llptr) => (llptr, ptr::null_mut()),
|
|
|
|
OperandValue::Pair(llptr, llextra) => (llptr, llextra),
|
|
|
|
OperandValue::Ref(..) => bug!("Deref of by-Ref operand {:?}", self)
|
|
|
|
};
|
|
|
|
LvalueRef {
|
|
|
|
llval: llptr,
|
2017-08-06 22:54:09 -07:00
|
|
|
llextra,
|
2017-09-20 18:17:23 +03:00
|
|
|
layout: ccx.layout_of(projected_ty),
|
2017-03-14 01:08:21 +02:00
|
|
|
alignment: Alignment::AbiAligned,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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`.
|
|
|
|
pub fn immediate_or_packed_pair(self, bcx: &Builder<'a, 'tcx>) -> ValueRef {
|
2016-05-25 11:55:44 +03:00
|
|
|
if let OperandValue::Pair(a, b) = self.val {
|
2017-09-20 23:07:47 +03:00
|
|
|
let llty = self.layout.llvm_type(bcx.ccx);
|
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.
|
2017-06-18 16:59:51 +03:00
|
|
|
let mut llpair = C_undef(llty);
|
2017-10-06 10:25:35 +03:00
|
|
|
llpair = bcx.insert_value(llpair, a, 0);
|
|
|
|
llpair = bcx.insert_value(llpair, 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`.
|
|
|
|
pub fn from_immediate_or_packed_pair(bcx: &Builder<'a, 'tcx>,
|
|
|
|
llval: ValueRef,
|
|
|
|
layout: TyLayout<'tcx>)
|
|
|
|
-> OperandRef<'tcx> {
|
2017-10-06 10:25:35 +03:00
|
|
|
let val = if layout.is_llvm_scalar_pair() {
|
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.
|
2017-10-06 10:25:35 +03:00
|
|
|
OperandValue::Pair(bcx.extract_value(llval, 0),
|
|
|
|
bcx.extract_value(llval, 1))
|
2017-10-05 04:22:23 +03:00
|
|
|
} else {
|
|
|
|
OperandValue::Immediate(llval)
|
|
|
|
};
|
|
|
|
OperandRef { val, layout }
|
2016-05-25 11:55:44 +03:00
|
|
|
}
|
2017-09-20 18:17:23 +03:00
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
|
2017-09-20 18:17:23 +03:00
|
|
|
impl<'a, 'tcx> OperandValue {
|
2017-06-25 12:41:24 +03:00
|
|
|
pub fn store(self, bcx: &Builder<'a, 'tcx>, dest: LvalueRef<'tcx>) {
|
|
|
|
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 {
|
2017-06-25 12:41:24 +03:00
|
|
|
OperandValue::Ref(r, source_align) =>
|
2017-09-20 18:17:23 +03:00
|
|
|
base::memcpy_ty(bcx, dest.llval, r, dest.layout,
|
2017-06-25 12:41:24 +03:00
|
|
|
(source_align | dest.alignment).non_abi()),
|
|
|
|
OperandValue::Immediate(s) => {
|
|
|
|
bcx.store(base::from_immediate(bcx, s), dest.llval, dest.alignment.non_abi());
|
|
|
|
}
|
|
|
|
OperandValue::Pair(a, b) => {
|
2017-10-05 04:22:23 +03:00
|
|
|
for (i, &x) in [a, b].iter().enumerate() {
|
2017-10-06 10:25:35 +03:00
|
|
|
let field = dest.project_field(bcx, i);
|
|
|
|
// HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
|
|
|
|
let x = bcx.bitcast(x, field.layout.immediate_llvm_type(bcx.ccx));
|
|
|
|
bcx.store(base::from_immediate(bcx, x),
|
|
|
|
field.llval, field.alignment.non_abi());
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
}
|
2017-06-25 12:41:24 +03:00
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
|
2017-06-25 12:41:24 +03:00
|
|
|
impl<'a, 'tcx> MirContext<'a, 'tcx> {
|
2016-06-09 18:13:16 +03:00
|
|
|
pub fn trans_consume(&mut self,
|
2016-12-31 16:00:24 -07:00
|
|
|
bcx: &Builder<'a, 'tcx>,
|
2016-06-09 18:13:16 +03:00
|
|
|
lvalue: &mir::Lvalue<'tcx>)
|
2015-10-21 17:42:25 -04:00
|
|
|
-> OperandRef<'tcx>
|
|
|
|
{
|
2016-06-09 18:13:16 +03:00
|
|
|
debug!("trans_consume(lvalue={:?})", lvalue);
|
|
|
|
|
2016-06-20 23:55:14 +03:00
|
|
|
// watch out for locals that do not have an
|
2016-06-09 18:13:16 +03:00
|
|
|
// alloca; they are handled somewhat differently
|
2016-09-25 01:38:27 +02:00
|
|
|
if let mir::Lvalue::Local(index) = *lvalue {
|
2016-06-20 23:55:14 +03:00
|
|
|
match self.locals[index] {
|
|
|
|
LocalRef::Operand(Some(o)) => {
|
2016-06-09 18:13:16 +03:00
|
|
|
return o;
|
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Operand(None) => {
|
2016-06-09 18:13:16 +03:00
|
|
|
bug!("use of {:?} before def", lvalue);
|
2015-11-03 06:35:09 -05:00
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
LocalRef::Lvalue(..) => {
|
2016-06-09 18:13:16 +03:00
|
|
|
// use path below
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 06:35:09 -05:00
|
|
|
|
2016-06-09 18:13:16 +03:00
|
|
|
// Moves out of pair fields are trivial.
|
|
|
|
if let &mir::Lvalue::Projection(ref proj) = lvalue {
|
2016-09-25 01:38:27 +02:00
|
|
|
if let mir::Lvalue::Local(index) = proj.base {
|
2016-06-20 23:55:14 +03:00
|
|
|
if let LocalRef::Operand(Some(o)) = self.locals[index] {
|
2016-06-09 18:13:16 +03:00
|
|
|
match (o.val, &proj.elem) {
|
|
|
|
(OperandValue::Pair(a, b),
|
|
|
|
&mir::ProjectionElem::Field(ref f, ty)) => {
|
2017-10-06 10:25:35 +03:00
|
|
|
let layout = bcx.ccx.layout_of(self.monomorphize(&ty));
|
2016-06-09 18:13:16 +03:00
|
|
|
let llval = [a, b][f.index()];
|
2017-10-06 10:25:35 +03:00
|
|
|
// HACK(eddyb) have to bitcast pointers
|
|
|
|
// until LLVM removes pointee types.
|
|
|
|
let llval = bcx.bitcast(llval,
|
|
|
|
layout.immediate_llvm_type(bcx.ccx));
|
2017-10-05 04:22:23 +03:00
|
|
|
return OperandRef {
|
2016-06-09 18:13:16 +03:00
|
|
|
val: OperandValue::Immediate(llval),
|
2017-10-06 10:25:35 +03:00
|
|
|
layout
|
2016-06-09 18:13:16 +03:00
|
|
|
};
|
2016-05-25 11:58:08 +03:00
|
|
|
}
|
2016-06-09 18:13:16 +03:00
|
|
|
_ => {}
|
2016-05-25 11:58:08 +03:00
|
|
|
}
|
|
|
|
}
|
2016-06-09 18:13:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for most lvalues, to consume them we just load them
|
|
|
|
// out from their home
|
2017-06-25 12:41:24 +03:00
|
|
|
self.trans_lvalue(bcx, lvalue).load(bcx)
|
2016-06-09 18:13:16 +03:00
|
|
|
}
|
2016-05-25 11:58:08 +03:00
|
|
|
|
2016-06-09 18:13:16 +03:00
|
|
|
pub fn trans_operand(&mut self,
|
2016-12-31 16:00:24 -07:00
|
|
|
bcx: &Builder<'a, 'tcx>,
|
2016-06-09 18:13:16 +03:00
|
|
|
operand: &mir::Operand<'tcx>)
|
|
|
|
-> OperandRef<'tcx>
|
|
|
|
{
|
|
|
|
debug!("trans_operand(operand={:?})", operand);
|
|
|
|
|
|
|
|
match *operand {
|
|
|
|
mir::Operand::Consume(ref lvalue) => {
|
|
|
|
self.trans_consume(bcx, lvalue)
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::Operand::Constant(ref constant) => {
|
2017-03-14 01:08:21 +02:00
|
|
|
let val = self.trans_constant(&bcx, constant);
|
2016-12-19 16:25:00 -07:00
|
|
|
let operand = val.to_operand(bcx.ccx);
|
2017-02-06 17:27:09 +01:00
|
|
|
if let OperandValue::Ref(ptr, align) = operand.val {
|
2016-04-26 23:54:38 +03:00
|
|
|
// If this is a OperandValue::Ref to an immediate constant, load it.
|
2017-09-20 18:17:23 +03:00
|
|
|
LvalueRef::new_sized(ptr, operand.layout, align).load(bcx)
|
2016-04-26 23:54:38 +03:00
|
|
|
} else {
|
|
|
|
operand
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|