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;
|
2016-03-22 17:30:57 +02:00
|
|
|
|
use rustc::ty::Ty;
|
2015-11-19 16:37:34 +01:00
|
|
|
|
use rustc::mir::repr as mir;
|
2016-03-22 19:23:36 +02:00
|
|
|
|
use base;
|
|
|
|
|
use common::{self, Block, BlockAndBuilder};
|
|
|
|
|
use datum;
|
|
|
|
|
use value::Value;
|
|
|
|
|
use glue;
|
2015-10-21 17:42:25 -04:00
|
|
|
|
|
2016-02-18 19:49:45 +02:00
|
|
|
|
use std::fmt;
|
|
|
|
|
|
2016-03-09 14:20:22 +02:00
|
|
|
|
use super::lvalue::load_fat_ptr;
|
2016-02-04 19:40:28 +02:00
|
|
|
|
use super::{MirContext, TempRef, drop};
|
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.
|
|
|
|
|
Ref(ValueRef),
|
|
|
|
|
/// A single LLVM value.
|
2015-11-13 00:12:50 +02:00
|
|
|
|
Immediate(ValueRef),
|
2015-11-10 22:05:11 +02:00
|
|
|
|
/// A fat pointer. The first ValueRef is the data and the second
|
|
|
|
|
/// is the extra.
|
|
|
|
|
FatPtr(ValueRef, ValueRef)
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2016-02-01 11:04:46 +01:00
|
|
|
|
/// to avoid nasty edge cases. In particular, using `Builder.store`
|
|
|
|
|
/// directly is sure to cause problems -- use `MirContext.store_operand`
|
|
|
|
|
/// 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 {
|
|
|
|
|
OperandValue::Ref(r) => {
|
2016-02-18 19:49:45 +02:00
|
|
|
|
write!(f, "OperandRef(Ref({:?}) @ {:?})",
|
|
|
|
|
Value(r), 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
|
|
|
|
}
|
|
|
|
|
OperandValue::FatPtr(a, d) => {
|
2016-02-18 19:49:45 +02:00
|
|
|
|
write!(f, "OperandRef(FatPtr({:?}, {:?}) @ {:?})",
|
|
|
|
|
Value(a), Value(d), self.ty)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-18 19:49:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> OperandRef<'tcx> {
|
|
|
|
|
/// 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-03-29 01:46:02 +02:00
|
|
|
|
_ => bug!()
|
2016-02-18 19:49:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-10 22:05:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
|
impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
|
2016-02-11 18:30:34 +02:00
|
|
|
|
pub fn trans_load(&mut self,
|
|
|
|
|
bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
|
|
|
|
llval: ValueRef,
|
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
|
-> OperandRef<'tcx>
|
|
|
|
|
{
|
2016-02-18 19:49:45 +02:00
|
|
|
|
debug!("trans_load: {:?} @ {:?}", Value(llval), ty);
|
2016-02-11 18:30:34 +02:00
|
|
|
|
|
|
|
|
|
let val = match datum::appropriate_rvalue_mode(bcx.ccx(), ty) {
|
|
|
|
|
datum::ByValue => {
|
2016-03-09 14:20:22 +02:00
|
|
|
|
OperandValue::Immediate(base::load_ty_builder(bcx, llval, ty))
|
2016-02-11 18:30:34 +02:00
|
|
|
|
}
|
|
|
|
|
datum::ByRef if common::type_is_fat_ptr(bcx.tcx(), ty) => {
|
2016-03-09 14:20:22 +02:00
|
|
|
|
let (lldata, llextra) = load_fat_ptr(bcx, llval);
|
2016-02-11 18:30:34 +02:00
|
|
|
|
OperandValue::FatPtr(lldata, llextra)
|
|
|
|
|
}
|
|
|
|
|
datum::ByRef => OperandValue::Ref(llval)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
OperandRef { val: val, ty: ty }
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
|
pub fn trans_operand(&mut self,
|
2016-02-01 11:04:46 +01:00
|
|
|
|
bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
2015-10-21 17:42:25 -04:00
|
|
|
|
operand: &mir::Operand<'tcx>)
|
|
|
|
|
-> OperandRef<'tcx>
|
|
|
|
|
{
|
|
|
|
|
debug!("trans_operand(operand={:?})", operand);
|
|
|
|
|
|
|
|
|
|
match *operand {
|
|
|
|
|
mir::Operand::Consume(ref lvalue) => {
|
2015-11-03 06:35:09 -05:00
|
|
|
|
// watch out for temporaries that do not have an
|
|
|
|
|
// alloca; they are handled somewhat differently
|
|
|
|
|
if let &mir::Lvalue::Temp(index) = lvalue {
|
|
|
|
|
match self.temps[index as usize] {
|
|
|
|
|
TempRef::Operand(Some(o)) => {
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
TempRef::Operand(None) => {
|
2016-03-29 01:46:02 +02:00
|
|
|
|
bug!("use of {:?} before def", lvalue);
|
2015-11-03 06:35:09 -05:00
|
|
|
|
}
|
|
|
|
|
TempRef::Lvalue(..) => {
|
|
|
|
|
// use path below
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for most lvalues, to consume them we just load them
|
|
|
|
|
// out from their home
|
2015-10-21 17:42:25 -04:00
|
|
|
|
let tr_lvalue = self.trans_lvalue(bcx, lvalue);
|
|
|
|
|
let ty = tr_lvalue.ty.to_ty(bcx.tcx());
|
2016-02-11 18:30:34 +02:00
|
|
|
|
self.trans_load(bcx, tr_lvalue.llval, ty)
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mir::Operand::Constant(ref constant) => {
|
2015-11-10 22:05:11 +02:00
|
|
|
|
self.trans_constant(bcx, constant)
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-10 22:05:11 +02:00
|
|
|
|
pub fn store_operand(&mut self,
|
2016-02-01 11:04:46 +01:00
|
|
|
|
bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
2015-11-10 22:05:11 +02:00
|
|
|
|
lldest: ValueRef,
|
|
|
|
|
operand: OperandRef<'tcx>)
|
|
|
|
|
{
|
2016-02-18 19:49:45 +02:00
|
|
|
|
debug!("store_operand: operand={:?}", operand);
|
2016-02-04 19:40:28 +02:00
|
|
|
|
bcx.with_block(|bcx| self.store_operand_direct(bcx, lldest, operand))
|
2016-02-01 11:04:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn store_operand_direct(&mut self,
|
|
|
|
|
bcx: Block<'bcx, 'tcx>,
|
|
|
|
|
lldest: ValueRef,
|
|
|
|
|
operand: OperandRef<'tcx>)
|
|
|
|
|
{
|
2016-01-12 16:36:47 +02:00
|
|
|
|
// 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(), operand.ty) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-11-10 22:05:11 +02:00
|
|
|
|
match operand.val {
|
|
|
|
|
OperandValue::Ref(r) => base::memcpy_ty(bcx, lldest, r, operand.ty),
|
2015-11-13 00:12:50 +02:00
|
|
|
|
OperandValue::Immediate(s) => base::store_ty(bcx, s, lldest, operand.ty),
|
2015-11-10 22:05:11 +02:00
|
|
|
|
OperandValue::FatPtr(data, extra) => {
|
|
|
|
|
base::store_fat_ptr(bcx, data, extra, lldest, operand.ty);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|
2016-02-11 18:30:34 +02:00
|
|
|
|
|
2016-02-04 19:40:28 +02:00
|
|
|
|
pub fn set_operand_dropped(&mut self,
|
|
|
|
|
bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
|
|
|
|
operand: &mir::Operand<'tcx>) {
|
|
|
|
|
match *operand {
|
|
|
|
|
mir::Operand::Constant(_) => return,
|
|
|
|
|
mir::Operand::Consume(ref lvalue) => {
|
|
|
|
|
if let mir::Lvalue::Temp(idx) = *lvalue {
|
|
|
|
|
if let TempRef::Operand(..) = self.temps[idx as usize] {
|
|
|
|
|
// All lvalues which have an associated drop are promoted to an alloca
|
|
|
|
|
// beforehand. If this is an operand, it is safe to say this is never
|
|
|
|
|
// dropped and there’s no reason for us to zero this out at all.
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let lvalue = self.trans_lvalue(bcx, lvalue);
|
|
|
|
|
let ty = lvalue.ty.to_ty(bcx.tcx());
|
2016-02-25 16:56:11 -05:00
|
|
|
|
if !glue::type_needs_drop(bcx.tcx(), ty) {
|
2016-02-04 19:40:28 +02:00
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
drop::drop_fill(bcx, lvalue.llval, ty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-21 17:42:25 -04:00
|
|
|
|
}
|