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-03-14 01:08:21 +02:00
|
|
|
use rustc::ty::{self, Ty};
|
2017-06-25 12:41:24 +03:00
|
|
|
use rustc::ty::layout::LayoutTyper;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir;
|
2017-03-14 01:08:21 +02:00
|
|
|
use rustc::mir::tcx::LvalueTy;
|
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-06-18 16:59:51 +03:00
|
|
|
use common::{self, 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-25 12:41:24 +03:00
|
|
|
use type_of::{self, 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-06-18 17:42:03 +03:00
|
|
|
use super::constant::Const;
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// The type of value being returned.
|
|
|
|
pub ty: Ty<'tcx>
|
|
|
|
}
|
|
|
|
|
2016-02-18 19:49:45 +02:00
|
|
|
impl<'tcx> fmt::Debug for OperandRef<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-11-10 22:05:11 +02:00
|
|
|
match self.val {
|
2017-02-06 17:27:09 +01:00
|
|
|
OperandValue::Ref(r, align) => {
|
|
|
|
write!(f, "OperandRef(Ref({:?}, {:?}) @ {:?})",
|
|
|
|
Value(r), align, self.ty)
|
2015-11-10 22:05:11 +02:00
|
|
|
}
|
2015-11-13 00:12:50 +02:00
|
|
|
OperandValue::Immediate(i) => {
|
2016-02-18 19:49:45 +02:00
|
|
|
write!(f, "OperandRef(Immediate({:?}) @ {:?})",
|
|
|
|
Value(i), self.ty)
|
2015-11-10 22:05:11 +02:00
|
|
|
}
|
2016-05-25 11:55:44 +03:00
|
|
|
OperandValue::Pair(a, b) => {
|
|
|
|
write!(f, "OperandRef(Pair({:?}, {:?}) @ {:?})",
|
|
|
|
Value(a), Value(b), self.ty)
|
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>,
|
|
|
|
ty: Ty<'tcx>) -> OperandRef<'tcx> {
|
|
|
|
assert!(common::type_is_zero_size(ccx, ty));
|
|
|
|
let llty = type_of::type_of(ccx, ty);
|
2017-06-18 17:42:03 +03:00
|
|
|
Const::new(C_undef(llty), ty).to_operand(ccx)
|
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-03-14 01:08:21 +02:00
|
|
|
pub fn deref(self) -> LvalueRef<'tcx> {
|
|
|
|
let projected_ty = self.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-03-14 01:08:21 +02:00
|
|
|
ty: LvalueTy::from_ty(projected_ty),
|
|
|
|
alignment: Alignment::AbiAligned,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 11:55:44 +03:00
|
|
|
/// If this operand is a Pair, we return an
|
|
|
|
/// Immediate aggregate with the two values.
|
2016-12-31 16:00:24 -07:00
|
|
|
pub fn pack_if_pair(mut self, bcx: &Builder<'a, 'tcx>) -> OperandRef<'tcx> {
|
2016-05-25 11:55:44 +03:00
|
|
|
if let OperandValue::Pair(a, b) = self.val {
|
|
|
|
// Reconstruct the immediate aggregate.
|
2016-12-19 16:25:00 -07:00
|
|
|
let llty = type_of::type_of(bcx.ccx, self.ty);
|
2017-06-18 16:59:51 +03:00
|
|
|
let mut llpair = C_undef(llty);
|
2016-05-25 11:55:44 +03:00
|
|
|
let elems = [a, b];
|
2017-06-25 12:41:24 +03:00
|
|
|
let layout = bcx.ccx.layout_of(self.ty);
|
2016-05-25 11:55:44 +03:00
|
|
|
for i in 0..2 {
|
2017-06-18 16:59:51 +03:00
|
|
|
let elem = base::from_immediate(bcx, elems[i]);
|
2017-06-25 12:41:24 +03:00
|
|
|
llpair = bcx.insert_value(llpair, elem, layout.llvm_field_index(i));
|
2016-05-25 11:55:44 +03:00
|
|
|
}
|
|
|
|
self.val = OperandValue::Immediate(llpair);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If this operand is a pair in an Immediate,
|
|
|
|
/// we return a Pair with the two halves.
|
2016-12-31 16:00:24 -07:00
|
|
|
pub fn unpack_if_pair(mut self, bcx: &Builder<'a, 'tcx>) -> OperandRef<'tcx> {
|
2016-05-25 11:55:44 +03:00
|
|
|
if let OperandValue::Immediate(llval) = self.val {
|
|
|
|
// Deconstruct the immediate aggregate.
|
2016-12-19 16:25:00 -07:00
|
|
|
if common::type_is_imm_pair(bcx.ccx, self.ty) {
|
2016-06-20 23:55:14 +03:00
|
|
|
debug!("Operand::unpack_if_pair: unpacking {:?}", self);
|
|
|
|
|
2017-04-25 20:24:33 +10:00
|
|
|
let layout = bcx.ccx.layout_of(self.ty);
|
|
|
|
|
2017-06-25 12:41:24 +03:00
|
|
|
let a = bcx.extract_value(llval, layout.llvm_field_index(0));
|
|
|
|
let a = base::to_immediate(bcx, a, layout.field_type(bcx.ccx, 0));
|
2016-05-25 11:55:44 +03:00
|
|
|
|
2017-06-25 12:41:24 +03:00
|
|
|
let b = bcx.extract_value(llval, layout.llvm_field_index(1));
|
|
|
|
let b = base::to_immediate(bcx, b, layout.field_type(bcx.ccx, 1));
|
2016-05-25 11:55:44 +03:00
|
|
|
|
|
|
|
self.val = OperandValue::Pair(a, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
|
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.
|
|
|
|
if common::type_is_zero_size(bcx.ccx, self.ty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match self.val {
|
|
|
|
OperandValue::Ref(r, source_align) =>
|
|
|
|
base::memcpy_ty(bcx, dest.llval, r, self.ty,
|
|
|
|
(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) => {
|
|
|
|
// See comment above about zero-sized values.
|
|
|
|
let dest_a = dest.project_field(bcx, 0);
|
|
|
|
if !common::type_is_zero_size(bcx.ccx, dest_a.ty.to_ty(bcx.tcx())) {
|
|
|
|
let a = base::from_immediate(bcx, a);
|
|
|
|
bcx.store(a, dest_a.llval, dest_a.alignment.non_abi());
|
|
|
|
}
|
|
|
|
let dest_b = dest.project_field(bcx, 1);
|
|
|
|
if !common::type_is_zero_size(bcx.ccx, dest_b.ty.to_ty(bcx.tcx())) {
|
|
|
|
let b = base::from_immediate(bcx, b);
|
|
|
|
bcx.store(b, dest_b.llval, dest_b.alignment.non_abi());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)) => {
|
|
|
|
let llval = [a, b][f.index()];
|
2016-07-06 14:41:35 +03:00
|
|
|
let op = OperandRef {
|
2016-06-09 18:13:16 +03:00
|
|
|
val: OperandValue::Immediate(llval),
|
2016-12-18 16:05:40 -07:00
|
|
|
ty: self.monomorphize(&ty)
|
2016-06-09 18:13:16 +03:00
|
|
|
};
|
2016-07-06 14:41:35 +03:00
|
|
|
|
|
|
|
// Handle nested pairs.
|
|
|
|
return op.unpack_if_pair(bcx);
|
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-06-25 12:41:24 +03:00
|
|
|
LvalueRef::new_sized(ptr, operand.ty, align).load(bcx)
|
2016-04-26 23:54:38 +03:00
|
|
|
} else {
|
|
|
|
operand
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|