2015-08-18 17:59:21 -04:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
|
|
|
//! See docs in build/expr/mod.rs
|
|
|
|
|
2016-05-25 08:39:32 +03:00
|
|
|
use rustc_const_math::{ConstMathErr, Op};
|
2016-11-08 14:02:55 +11:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2016-06-07 17:28:36 +03:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2015-11-19 16:37:34 +01:00
|
|
|
use build::{BlockAnd, BlockAndExtension, Builder};
|
2015-08-18 17:59:21 -04:00
|
|
|
use build::expr::category::{Category, RvalueFunc};
|
|
|
|
use hair::*;
|
2016-03-31 18:50:07 +13:00
|
|
|
use rustc::middle::const_val::ConstVal;
|
2017-08-31 21:37:38 +03:00
|
|
|
use rustc::middle::region;
|
2017-09-14 21:44:23 -04:00
|
|
|
use rustc::ty::{self, Ty};
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::*;
|
2018-01-16 09:24:38 +01:00
|
|
|
use rustc::mir::interpret::{Value, PrimVal};
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
|
2017-02-26 16:21:08 +02:00
|
|
|
/// See comment on `as_local_operand`
|
|
|
|
pub fn as_local_rvalue<M>(&mut self, block: BasicBlock, expr: M)
|
|
|
|
-> BlockAnd<Rvalue<'tcx>>
|
|
|
|
where M: Mirror<'tcx, Output = Expr<'tcx>>
|
|
|
|
{
|
2017-05-15 21:09:01 -04:00
|
|
|
let local_scope = self.local_scope();
|
|
|
|
self.as_rvalue(block, local_scope, expr)
|
2017-02-26 16:21:08 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
/// Compile `expr`, yielding an rvalue.
|
2017-08-31 21:37:38 +03:00
|
|
|
pub fn as_rvalue<M>(&mut self, block: BasicBlock, scope: Option<region::Scope>, expr: M)
|
2017-02-26 16:21:08 +02:00
|
|
|
-> BlockAnd<Rvalue<'tcx>>
|
2015-10-07 14:37:42 +02:00
|
|
|
where M: Mirror<'tcx, Output = Expr<'tcx>>
|
2015-08-18 17:59:21 -04:00
|
|
|
{
|
|
|
|
let expr = self.hir.mirror(expr);
|
2017-02-26 16:21:08 +02:00
|
|
|
self.expr_as_rvalue(block, scope, expr)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_as_rvalue(&mut self,
|
|
|
|
mut block: BasicBlock,
|
2017-08-31 21:37:38 +03:00
|
|
|
scope: Option<region::Scope>,
|
2015-10-05 12:31:48 -04:00
|
|
|
expr: Expr<'tcx>)
|
2015-10-07 14:37:42 +02:00
|
|
|
-> BlockAnd<Rvalue<'tcx>> {
|
2017-05-15 21:09:01 -04:00
|
|
|
debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
let this = self;
|
|
|
|
let expr_span = expr.span;
|
2016-06-07 19:21:56 +03:00
|
|
|
let source_info = this.source_info(expr_span);
|
2015-08-18 17:59:21 -04:00
|
|
|
|
|
|
|
match expr.kind {
|
2017-09-13 22:33:07 +03:00
|
|
|
ExprKind::Scope { region_scope, lint_level, value } => {
|
2017-08-31 21:37:38 +03:00
|
|
|
let region_scope = (region_scope, source_info);
|
2017-09-13 22:33:07 +03:00
|
|
|
this.in_scope(region_scope, lint_level, block,
|
|
|
|
|this| this.as_rvalue(block, scope, value))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Repeat { value, count } => {
|
2017-02-26 16:21:08 +02:00
|
|
|
let value_operand = unpack!(block = this.as_operand(block, scope, value));
|
2015-11-10 23:52:23 +02:00
|
|
|
block.and(Rvalue::Repeat(value_operand, count))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Borrow { region, borrow_kind, arg } => {
|
2017-12-01 14:39:51 +02:00
|
|
|
let arg_place = unpack!(block = this.as_place(block, arg));
|
|
|
|
block.and(Rvalue::Ref(region, borrow_kind, arg_place))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Binary { op, lhs, rhs } => {
|
2017-02-26 16:21:08 +02:00
|
|
|
let lhs = unpack!(block = this.as_operand(block, scope, lhs));
|
|
|
|
let rhs = unpack!(block = this.as_operand(block, scope, rhs));
|
2016-03-31 18:50:07 +13:00
|
|
|
this.build_binary_op(block, op, expr_span, expr.ty,
|
|
|
|
lhs, rhs)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Unary { op, arg } => {
|
2017-02-26 16:21:08 +02:00
|
|
|
let arg = unpack!(block = this.as_operand(block, scope, arg));
|
2016-03-31 18:50:07 +13:00
|
|
|
// Check for -MIN on signed integers
|
2016-05-26 20:02:56 +03:00
|
|
|
if this.hir.check_overflow() && op == UnOp::Neg && expr.ty.is_signed() {
|
2016-03-31 18:50:07 +13:00
|
|
|
let bool_ty = this.hir.bool_ty();
|
|
|
|
|
|
|
|
let minval = this.minval_literal(expr_span, expr.ty);
|
2017-04-11 23:52:51 +03:00
|
|
|
let is_min = this.temp(bool_ty, expr_span);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
this.cfg.push_assign(block, source_info, &is_min,
|
2017-12-04 17:46:23 +02:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, arg.to_copy(), minval));
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2016-05-25 08:39:32 +03:00
|
|
|
let err = ConstMathErr::Overflow(Op::Neg);
|
2017-11-17 17:19:57 +02:00
|
|
|
block = this.assert(block, Operand::Move(is_min), false,
|
2016-05-25 08:39:32 +03:00
|
|
|
AssertMessage::Math(err), expr_span);
|
2016-03-31 18:50:07 +13:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
block.and(Rvalue::UnaryOp(op, arg))
|
|
|
|
}
|
2017-08-09 22:23:27 +03:00
|
|
|
ExprKind::Box { value } => {
|
2015-08-18 17:59:21 -04:00
|
|
|
let value = this.hir.mirror(value);
|
2017-09-20 16:36:20 +03:00
|
|
|
// The `Box<T>` temporary created here is not a part of the HIR,
|
|
|
|
// and therefore is not considered during generator OIBIT
|
|
|
|
// determination. See the comment about `box` at `yield_in_scope`.
|
|
|
|
let result = this.local_decls.push(
|
|
|
|
LocalDecl::new_internal(expr.ty, expr_span));
|
2017-09-01 22:53:54 +03:00
|
|
|
this.cfg.push(block, Statement {
|
|
|
|
source_info,
|
2017-09-04 08:01:46 +03:00
|
|
|
kind: StatementKind::StorageLive(result)
|
2017-09-01 22:53:54 +03:00
|
|
|
});
|
2017-08-09 22:23:27 +03:00
|
|
|
if let Some(scope) = scope {
|
2016-01-28 23:59:00 +02:00
|
|
|
// schedule a shallow free of that memory, lest we unwind:
|
2017-12-01 14:31:47 +02:00
|
|
|
this.schedule_drop(expr_span, scope, &Place::Local(result), value.ty);
|
2017-08-09 22:23:27 +03:00
|
|
|
}
|
2017-08-14 14:10:05 +03:00
|
|
|
|
|
|
|
// malloc some memory of suitable type (thus far, uninitialized):
|
|
|
|
let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
|
2017-12-01 14:31:47 +02:00
|
|
|
this.cfg.push_assign(block, source_info, &Place::Local(result), box_);
|
2017-08-14 14:10:05 +03:00
|
|
|
|
2017-08-09 22:23:27 +03:00
|
|
|
// initialize the box contents:
|
2017-12-01 14:31:47 +02:00
|
|
|
unpack!(block = this.into(&Place::Local(result).deref(), block, value));
|
|
|
|
block.and(Rvalue::Use(Operand::Move(Place::Local(result))))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Cast { source } => {
|
2016-03-08 14:16:26 +02:00
|
|
|
let source = this.hir.mirror(source);
|
2016-05-01 17:56:07 +12:00
|
|
|
|
2017-02-26 16:21:08 +02:00
|
|
|
let source = unpack!(block = this.as_operand(block, scope, source));
|
2016-05-01 17:56:07 +12:00
|
|
|
block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2016-10-04 01:13:36 +03:00
|
|
|
ExprKind::Use { source } => {
|
2017-02-26 16:21:08 +02:00
|
|
|
let source = unpack!(block = this.as_operand(block, scope, source));
|
2016-10-04 01:13:36 +03:00
|
|
|
block.and(Rvalue::Use(source))
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
ExprKind::ReifyFnPointer { source } => {
|
2017-02-26 16:21:08 +02:00
|
|
|
let source = unpack!(block = this.as_operand(block, scope, source));
|
2015-08-18 17:59:21 -04:00
|
|
|
block.and(Rvalue::Cast(CastKind::ReifyFnPointer, source, expr.ty))
|
|
|
|
}
|
|
|
|
ExprKind::UnsafeFnPointer { source } => {
|
2017-02-26 16:21:08 +02:00
|
|
|
let source = unpack!(block = this.as_operand(block, scope, source));
|
2015-08-18 17:59:21 -04:00
|
|
|
block.and(Rvalue::Cast(CastKind::UnsafeFnPointer, source, expr.ty))
|
2017-02-22 01:24:16 +01:00
|
|
|
}
|
|
|
|
ExprKind::ClosureFnPointer { source } => {
|
2017-02-26 16:21:08 +02:00
|
|
|
let source = unpack!(block = this.as_operand(block, scope, source));
|
2017-02-22 01:24:16 +01:00
|
|
|
block.and(Rvalue::Cast(CastKind::ClosureFnPointer, source, expr.ty))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Unsize { source } => {
|
2017-02-26 16:21:08 +02:00
|
|
|
let source = unpack!(block = this.as_operand(block, scope, source));
|
2015-08-18 17:59:21 -04:00
|
|
|
block.and(Rvalue::Cast(CastKind::Unsize, source, expr.ty))
|
|
|
|
}
|
2017-01-15 23:36:10 -08:00
|
|
|
ExprKind::Array { fields } => {
|
2015-08-18 17:59:21 -04:00
|
|
|
// (*) We would (maybe) be closer to trans if we
|
|
|
|
// handled this and other aggregate cases via
|
|
|
|
// `into()`, not `as_rvalue` -- in that case, instead
|
|
|
|
// of generating
|
|
|
|
//
|
|
|
|
// let tmp1 = ...1;
|
|
|
|
// let tmp2 = ...2;
|
|
|
|
// dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
|
|
|
|
//
|
|
|
|
// we could just generate
|
|
|
|
//
|
|
|
|
// dest.f = ...1;
|
|
|
|
// dest.g = ...2;
|
|
|
|
//
|
|
|
|
// The problem is that then we would need to:
|
|
|
|
//
|
|
|
|
// (a) have a more complex mechanism for handling
|
|
|
|
// partial cleanup;
|
|
|
|
// (b) distinguish the case where the type `Foo` has a
|
|
|
|
// destructor, in which case creating an instance
|
|
|
|
// as a whole "arms" the destructor, and you can't
|
|
|
|
// write individual fields; and,
|
|
|
|
// (c) handle the case where the type Foo has no
|
|
|
|
// fields. We don't want `let x: ();` to compile
|
|
|
|
// to the same MIR as `let x = ();`.
|
|
|
|
|
|
|
|
// first process the set of fields
|
2017-02-26 00:32:14 +02:00
|
|
|
let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
|
2015-08-18 17:59:21 -04:00
|
|
|
let fields: Vec<_> =
|
|
|
|
fields.into_iter()
|
2017-02-26 16:21:08 +02:00
|
|
|
.map(|f| unpack!(block = this.as_operand(block, scope, f)))
|
2015-08-18 17:59:21 -04:00
|
|
|
.collect();
|
|
|
|
|
2017-05-12 01:38:26 +03:00
|
|
|
block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Tuple { fields } => { // see (*) above
|
|
|
|
// first process the set of fields
|
|
|
|
let fields: Vec<_> =
|
|
|
|
fields.into_iter()
|
2017-02-26 16:21:08 +02:00
|
|
|
.map(|f| unpack!(block = this.as_operand(block, scope, f)))
|
2015-08-18 17:59:21 -04:00
|
|
|
.collect();
|
|
|
|
|
2017-05-12 01:38:26 +03:00
|
|
|
block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2017-08-11 07:15:33 +02:00
|
|
|
ExprKind::Closure { closure_id, substs, upvars, interior } => { // see (*) above
|
2016-12-26 14:34:03 +01:00
|
|
|
let mut operands: Vec<_> =
|
2015-08-18 17:59:21 -04:00
|
|
|
upvars.into_iter()
|
2017-02-26 16:21:08 +02:00
|
|
|
.map(|upvar| unpack!(block = this.as_operand(block, scope, upvar)))
|
2015-08-18 17:59:21 -04:00
|
|
|
.collect();
|
2017-08-11 07:15:33 +02:00
|
|
|
let result = if let Some(interior) = interior {
|
2017-08-25 00:57:08 +02:00
|
|
|
// Add the state operand since it follows the upvars in the generator
|
|
|
|
// struct. See librustc_mir/transform/generator.rs for more details.
|
2016-12-26 14:34:03 +01:00
|
|
|
operands.push(Operand::Constant(box Constant {
|
|
|
|
span: expr_span,
|
|
|
|
ty: this.hir.tcx().types.u32,
|
|
|
|
literal: Literal::Value {
|
2017-08-04 11:25:13 +03:00
|
|
|
value: this.hir.tcx().mk_const(ty::Const {
|
2018-01-16 09:28:27 +01:00
|
|
|
val: ConstVal::Value(Value::ByVal(PrimVal::Bytes(0))),
|
2017-08-04 11:25:13 +03:00
|
|
|
ty: this.hir.tcx().types.u32
|
|
|
|
}),
|
2016-12-26 14:34:03 +01:00
|
|
|
},
|
|
|
|
}));
|
2017-08-11 07:15:33 +02:00
|
|
|
box AggregateKind::Generator(closure_id, substs, interior)
|
2016-12-26 14:34:03 +01:00
|
|
|
} else {
|
|
|
|
box AggregateKind::Closure(closure_id, substs)
|
|
|
|
};
|
|
|
|
block.and(Rvalue::Aggregate(result, operands))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2016-02-11 18:31:42 +02:00
|
|
|
ExprKind::Adt {
|
|
|
|
adt_def, variant_index, substs, fields, base
|
|
|
|
} => { // see (*) above
|
2016-09-06 01:26:02 +03:00
|
|
|
let is_union = adt_def.is_union();
|
2016-08-19 19:20:30 +03:00
|
|
|
let active_field_index = if is_union { Some(fields[0].name.index()) } else { None };
|
|
|
|
|
2015-10-21 17:19:02 -04:00
|
|
|
// first process the set of fields that were provided
|
|
|
|
// (evaluating them in order given by user)
|
2017-02-26 16:21:08 +02:00
|
|
|
let fields_map: FxHashMap<_, _> = fields.into_iter()
|
|
|
|
.map(|f| (f.name, unpack!(block = this.as_operand(block, scope, f.expr))))
|
|
|
|
.collect();
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2015-10-21 17:19:02 -04:00
|
|
|
let field_names = this.hir.all_fields(adt_def, variant_index);
|
|
|
|
|
2016-02-11 18:31:42 +02:00
|
|
|
let fields = if let Some(FruInfo { base, field_types }) = base {
|
2017-12-01 14:39:51 +02:00
|
|
|
let base = unpack!(block = this.as_place(block, base));
|
2016-02-13 01:01:08 +02:00
|
|
|
|
|
|
|
// MIR does not natively support FRU, so for each
|
|
|
|
// base-supplied field, generate an operand that
|
|
|
|
// reads it from the base.
|
2015-08-18 17:59:21 -04:00
|
|
|
field_names.into_iter()
|
2016-02-11 18:31:42 +02:00
|
|
|
.zip(field_types.into_iter())
|
|
|
|
.map(|(n, ty)| match fields_map.get(&n) {
|
|
|
|
Some(v) => v.clone(),
|
2017-11-17 17:19:57 +02:00
|
|
|
None => this.consume_by_copy_or_move(base.clone().field(n, ty))
|
2016-02-11 18:31:42 +02:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
2016-08-19 19:20:30 +03:00
|
|
|
field_names.iter().filter_map(|n| fields_map.get(n).cloned()).collect()
|
2016-02-11 18:31:42 +02:00
|
|
|
};
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2017-05-12 01:38:26 +03:00
|
|
|
let adt =
|
|
|
|
box AggregateKind::Adt(adt_def, variant_index, substs, active_field_index);
|
2016-08-19 19:20:30 +03:00
|
|
|
block.and(Rvalue::Aggregate(adt, fields))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2016-04-16 19:45:28 +12:00
|
|
|
ExprKind::Assign { .. } |
|
|
|
|
ExprKind::AssignOp { .. } => {
|
|
|
|
block = unpack!(this.stmt_expr(block, expr));
|
|
|
|
block.and(this.unit_rvalue())
|
|
|
|
}
|
2017-07-10 21:11:31 +02:00
|
|
|
ExprKind::Yield { value } => {
|
2016-12-26 14:34:03 +01:00
|
|
|
let value = unpack!(block = this.as_operand(block, scope, value));
|
|
|
|
let resume = this.cfg.start_new_block();
|
2017-08-07 22:30:39 -07:00
|
|
|
let cleanup = this.generator_drop_cleanup();
|
2017-07-10 21:11:31 +02:00
|
|
|
this.cfg.terminate(block, source_info, TerminatorKind::Yield {
|
2016-12-26 14:34:03 +01:00
|
|
|
value: value,
|
|
|
|
resume: resume,
|
|
|
|
drop: cleanup,
|
|
|
|
});
|
|
|
|
resume.and(this.unit_rvalue())
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
ExprKind::Literal { .. } |
|
|
|
|
ExprKind::Block { .. } |
|
|
|
|
ExprKind::Match { .. } |
|
|
|
|
ExprKind::If { .. } |
|
2016-08-02 15:56:20 +08:00
|
|
|
ExprKind::NeverToAny { .. } |
|
2015-08-18 17:59:21 -04:00
|
|
|
ExprKind::Loop { .. } |
|
|
|
|
ExprKind::LogicalOp { .. } |
|
|
|
|
ExprKind::Call { .. } |
|
|
|
|
ExprKind::Field { .. } |
|
|
|
|
ExprKind::Deref { .. } |
|
|
|
|
ExprKind::Index { .. } |
|
|
|
|
ExprKind::VarRef { .. } |
|
|
|
|
ExprKind::SelfRef |
|
|
|
|
ExprKind::Break { .. } |
|
|
|
|
ExprKind::Continue { .. } |
|
|
|
|
ExprKind::Return { .. } |
|
2017-02-15 21:21:36 +02:00
|
|
|
ExprKind::InlineAsm { .. } |
|
2015-08-18 17:59:21 -04:00
|
|
|
ExprKind::StaticRef { .. } => {
|
|
|
|
// these do not have corresponding `Rvalue` variants,
|
|
|
|
// so make an operand and then return that
|
|
|
|
debug_assert!(match Category::of(&expr.kind) {
|
|
|
|
Some(Category::Rvalue(RvalueFunc::AsRvalue)) => false,
|
|
|
|
_ => true,
|
|
|
|
});
|
2017-02-26 16:21:08 +02:00
|
|
|
let operand = unpack!(block = this.as_operand(block, scope, expr));
|
2015-08-18 17:59:21 -04:00
|
|
|
block.and(Rvalue::Use(operand))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2016-04-29 14:10:56 +12:00
|
|
|
pub fn build_binary_op(&mut self, mut block: BasicBlock,
|
2017-09-14 21:44:23 -04:00
|
|
|
op: BinOp, span: Span, ty: Ty<'tcx>,
|
2016-03-31 18:50:07 +13:00
|
|
|
lhs: Operand<'tcx>, rhs: Operand<'tcx>) -> BlockAnd<Rvalue<'tcx>> {
|
2016-06-07 19:21:56 +03:00
|
|
|
let source_info = self.source_info(span);
|
2016-03-31 18:50:07 +13:00
|
|
|
let bool_ty = self.hir.bool_ty();
|
2016-05-26 20:02:56 +03:00
|
|
|
if self.hir.check_overflow() && op.is_checkable() && ty.is_integral() {
|
2017-01-11 15:58:37 +08:00
|
|
|
let result_tup = self.hir.tcx().intern_tup(&[ty, bool_ty], false);
|
2017-04-11 23:52:51 +03:00
|
|
|
let result_value = self.temp(result_tup, span);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
self.cfg.push_assign(block, source_info,
|
2016-03-31 18:50:07 +13:00
|
|
|
&result_value, Rvalue::CheckedBinaryOp(op,
|
|
|
|
lhs,
|
|
|
|
rhs));
|
|
|
|
let val_fld = Field::new(0);
|
|
|
|
let of_fld = Field::new(1);
|
|
|
|
|
|
|
|
let val = result_value.clone().field(val_fld, ty);
|
|
|
|
let of = result_value.field(of_fld, bool_ty);
|
|
|
|
|
2016-05-25 08:39:32 +03:00
|
|
|
let err = ConstMathErr::Overflow(match op {
|
|
|
|
BinOp::Add => Op::Add,
|
|
|
|
BinOp::Sub => Op::Sub,
|
|
|
|
BinOp::Mul => Op::Mul,
|
|
|
|
BinOp::Shl => Op::Shl,
|
|
|
|
BinOp::Shr => Op::Shr,
|
|
|
|
_ => {
|
|
|
|
bug!("MIR build_binary_op: {:?} is not checkable", op)
|
|
|
|
}
|
|
|
|
});
|
2016-04-29 00:04:45 +12:00
|
|
|
|
2017-11-17 17:19:57 +02:00
|
|
|
block = self.assert(block, Operand::Move(of), false,
|
2016-05-25 08:39:32 +03:00
|
|
|
AssertMessage::Math(err), span);
|
2016-04-29 00:04:45 +12:00
|
|
|
|
2017-11-17 17:19:57 +02:00
|
|
|
block.and(Rvalue::Use(Operand::Move(val)))
|
2016-03-31 18:50:07 +13:00
|
|
|
} else {
|
|
|
|
if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
|
|
|
|
// Checking division and remainder is more complex, since we 1. always check
|
|
|
|
// and 2. there are two possible failure cases, divide-by-zero and overflow.
|
|
|
|
|
2016-05-25 08:39:32 +03:00
|
|
|
let (zero_err, overflow_err) = if op == BinOp::Div {
|
|
|
|
(ConstMathErr::DivisionByZero,
|
|
|
|
ConstMathErr::Overflow(Op::Div))
|
2016-03-31 18:50:07 +13:00
|
|
|
} else {
|
2016-05-25 08:39:32 +03:00
|
|
|
(ConstMathErr::RemainderByZero,
|
|
|
|
ConstMathErr::Overflow(Op::Rem))
|
2016-03-31 18:50:07 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
// Check for / 0
|
2017-04-11 23:52:51 +03:00
|
|
|
let is_zero = self.temp(bool_ty, span);
|
2016-03-31 18:50:07 +13:00
|
|
|
let zero = self.zero_literal(span, ty);
|
2016-06-07 19:21:56 +03:00
|
|
|
self.cfg.push_assign(block, source_info, &is_zero,
|
2017-12-04 17:46:23 +02:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), zero));
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2017-11-17 17:19:57 +02:00
|
|
|
block = self.assert(block, Operand::Move(is_zero), false,
|
2016-05-25 08:39:32 +03:00
|
|
|
AssertMessage::Math(zero_err), span);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
|
|
|
// We only need to check for the overflow in one case:
|
|
|
|
// MIN / -1, and only for signed values.
|
|
|
|
if ty.is_signed() {
|
|
|
|
let neg_1 = self.neg_1_literal(span, ty);
|
|
|
|
let min = self.minval_literal(span, ty);
|
|
|
|
|
2017-04-11 23:52:51 +03:00
|
|
|
let is_neg_1 = self.temp(bool_ty, span);
|
|
|
|
let is_min = self.temp(bool_ty, span);
|
|
|
|
let of = self.temp(bool_ty, span);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
|
|
|
// this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
|
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
self.cfg.push_assign(block, source_info, &is_neg_1,
|
2017-12-04 17:46:23 +02:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), neg_1));
|
2016-06-07 19:21:56 +03:00
|
|
|
self.cfg.push_assign(block, source_info, &is_min,
|
2017-12-04 17:46:23 +02:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, lhs.to_copy(), min));
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2017-11-17 17:19:57 +02:00
|
|
|
let is_neg_1 = Operand::Move(is_neg_1);
|
|
|
|
let is_min = Operand::Move(is_min);
|
2016-06-07 19:21:56 +03:00
|
|
|
self.cfg.push_assign(block, source_info, &of,
|
2016-03-31 18:50:07 +13:00
|
|
|
Rvalue::BinaryOp(BinOp::BitAnd, is_neg_1, is_min));
|
|
|
|
|
2017-11-17 17:19:57 +02:00
|
|
|
block = self.assert(block, Operand::Move(of), false,
|
2016-05-25 08:39:32 +03:00
|
|
|
AssertMessage::Math(overflow_err), span);
|
2016-03-31 18:50:07 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
block.and(Rvalue::BinaryOp(op, lhs, rhs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to get a `-1` value of the appropriate type
|
2017-09-14 21:44:23 -04:00
|
|
|
fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
|
2018-02-23 13:52:33 +01:00
|
|
|
let bits = self.hir.integer_bit_width(ty);
|
2018-02-21 22:02:52 +01:00
|
|
|
let n = (!0u128) >> (128 - bits);
|
2018-01-25 16:44:45 +01:00
|
|
|
let literal = Literal::Value {
|
|
|
|
value: self.hir.tcx().mk_const(ty::Const {
|
2018-02-21 22:02:52 +01:00
|
|
|
val: ConstVal::Value(Value::ByVal(PrimVal::Bytes(n))),
|
2018-01-25 16:44:45 +01:00
|
|
|
ty
|
|
|
|
})
|
2016-03-31 18:50:07 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
self.literal_operand(span, ty, literal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to get the minimum value of the appropriate type
|
2017-09-14 21:44:23 -04:00
|
|
|
fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
|
2018-02-21 22:02:52 +01:00
|
|
|
assert!(ty.is_signed());
|
2018-02-23 13:52:33 +01:00
|
|
|
let bits = self.hir.integer_bit_width(ty);
|
2018-02-21 22:02:52 +01:00
|
|
|
let n = 1 << (bits - 1);
|
|
|
|
let literal = Literal::Value {
|
|
|
|
value: self.hir.tcx().mk_const(ty::Const {
|
|
|
|
val: ConstVal::Value(Value::ByVal(PrimVal::Bytes(n))),
|
|
|
|
ty
|
|
|
|
})
|
2016-03-31 18:50:07 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
self.literal_operand(span, ty, literal)
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|