2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
# Translation of expressions.
|
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
## Recommended entry point
|
|
|
|
|
|
|
|
If you wish to translate an expression, the preferred way to do
|
|
|
|
so is to use:
|
|
|
|
|
|
|
|
expr::trans_into(block, expr, Dest) -> block
|
|
|
|
|
|
|
|
This will generate code that evaluates `expr`, storing the result into
|
|
|
|
`Dest`, which must either be the special flag ignore (throw the result
|
|
|
|
away) or be a pointer to memory of the same type/size as the
|
|
|
|
expression. It returns the resulting basic block. This form will
|
|
|
|
handle all automatic adjustments and moves for you.
|
|
|
|
|
|
|
|
## Translation to a datum
|
|
|
|
|
|
|
|
In some cases, `trans_into()` is too narrow of an interface.
|
|
|
|
Generally this occurs either when you know that the result value is
|
|
|
|
going to be a scalar, or when you need to evaluate the expression into
|
|
|
|
some memory location so you can go and inspect it (e.g., assignments,
|
|
|
|
`match` expressions, the `&` operator).
|
|
|
|
|
|
|
|
In such cases, you want the following function:
|
|
|
|
|
|
|
|
trans_to_datum(block, expr) -> DatumBlock
|
|
|
|
|
|
|
|
This function generates code to evaluate the expression and return a
|
|
|
|
`Datum` describing where the result is to be found. This function
|
|
|
|
tries to return its result in the most efficient way possible, without
|
|
|
|
introducing extra copies or sacrificing information. Therefore, for
|
|
|
|
lvalue expressions, you always get a by-ref `Datum` in return that
|
|
|
|
points at the memory for this lvalue (almost, see [1]). For rvalue
|
|
|
|
expressions, we will return a by-value `Datum` whenever possible, but
|
|
|
|
it is often necessary to allocate a stack slot, store the result of
|
|
|
|
the rvalue in there, and then return a pointer to the slot (see the
|
|
|
|
discussion later on about the different kinds of rvalues).
|
|
|
|
|
|
|
|
NB: The `trans_to_datum()` function does perform adjustments, but
|
|
|
|
since it returns a pointer to the value "in place" it does not handle
|
|
|
|
any moves that may be relevant. If you are transing an expression
|
|
|
|
whose result should be moved, you should either use the Datum methods
|
|
|
|
`move_to()` (for unconditional moves) or `store_to()` (for moves
|
|
|
|
conditioned on the type of the expression) at some point.
|
|
|
|
|
|
|
|
## Translating local variables
|
|
|
|
|
|
|
|
`trans_local_var()` can be used to trans a ref to a local variable
|
|
|
|
that is not an expression. This is needed for captures.
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
## Ownership and cleanups
|
|
|
|
|
|
|
|
The current system for cleanups associates required cleanups with
|
|
|
|
block contexts. Block contexts are structured into a tree that
|
|
|
|
resembles the code itself. Not every block context has cleanups
|
|
|
|
associated with it, only those blocks that have a kind of
|
|
|
|
`block_scope`. See `common::block_kind` for more details.
|
|
|
|
|
|
|
|
If you invoke `trans_into()`, no cleanup is scheduled for you. The
|
|
|
|
value is written into the given destination and is assumed to be owned
|
|
|
|
by that destination.
|
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
When you invoke `trans_to_datum()` on an rvalue, the resulting
|
|
|
|
datum/value will have an appropriate cleanup scheduled for the
|
|
|
|
innermost cleanup scope. If you later use `move_to()` or
|
|
|
|
`drop_val()`, this cleanup will be canceled.
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
During the evaluation of an expression, temporary cleanups are created
|
|
|
|
and later canceled. These represent intermediate or partial results
|
|
|
|
which must be cleaned up in the event of task failure.
|
|
|
|
|
|
|
|
## Implementation details
|
|
|
|
|
|
|
|
We divide expressions into three categories, based on how they are most
|
|
|
|
naturally implemented:
|
|
|
|
|
|
|
|
1. Lvalues
|
|
|
|
2. Datum rvalues
|
|
|
|
3. DPS rvalues
|
|
|
|
4. Statement rvalues
|
|
|
|
|
|
|
|
Lvalues always refer to user-assignable memory locations.
|
|
|
|
Translating those always results in a by-ref datum; this introduces
|
|
|
|
no inefficiencies into the generated code, because all lvalues are
|
|
|
|
naturally addressable.
|
|
|
|
|
|
|
|
Datum rvalues are rvalues that always generate datums as a result.
|
|
|
|
These are generally scalar results, such as `a+b` where `a` and `b`
|
|
|
|
are integers.
|
|
|
|
|
|
|
|
DPS rvalues are rvalues that, when translated, must be given a
|
|
|
|
memory location to write into (or the Ignore flag). These are
|
|
|
|
generally expressions that produce structural results that are
|
|
|
|
larger than one word (e.g., a struct literal), but also expressions
|
|
|
|
(like `if`) that involve control flow (otherwise we'd have to
|
|
|
|
generate phi nodes).
|
|
|
|
|
|
|
|
Finally, statement rvalues are rvalues that always produce a nil
|
|
|
|
return type, such as `while` loops or assignments (`a = b`).
|
|
|
|
|
|
|
|
## Caveats
|
|
|
|
|
|
|
|
[1] Actually, some lvalues are only stored by value and not by
|
|
|
|
reference. An example (as of this writing) would be immutable
|
|
|
|
arguments or pattern bindings of immediate type. However, mutable
|
|
|
|
lvalues are *never* stored by value.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
use back::abi;
|
|
|
|
use lib;
|
|
|
|
use lib::llvm::{ValueRef, TypeRef, llvm, True};
|
2013-01-25 18:57:39 -06:00
|
|
|
use middle::borrowck::root_map_key;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::_match;
|
2013-02-23 15:36:01 -06:00
|
|
|
use middle::trans::adt;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::base;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::base::*;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::build::*;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::callee::{AutorefArg, DoAutorefArg, DontAutorefArg};
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::callee;
|
|
|
|
use middle::trans::closure;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::common::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::consts;
|
|
|
|
use middle::trans::controlflow;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::datum::*;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::debuginfo;
|
2013-03-07 00:30:20 -06:00
|
|
|
use middle::trans::inline;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::machine;
|
|
|
|
use middle::trans::meth;
|
|
|
|
use middle::trans::tvec;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::type_of;
|
|
|
|
use middle::ty;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::ty::struct_mutable_fields;
|
2013-02-27 18:28:37 -06:00
|
|
|
use middle::ty::{AutoPtr, AutoBorrowVec, AutoBorrowVecRef, AutoBorrowFn,
|
|
|
|
AutoDerefRef, AutoAddEnv};
|
2012-12-13 15:05:22 -06:00
|
|
|
use util::common::indenter;
|
|
|
|
use util::ppaux::ty_to_str;
|
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
use std::oldmap::HashMap;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::print::pprust::{expr_to_str};
|
2013-01-09 11:49:11 -06:00
|
|
|
use syntax::ast;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::codemap;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// Destinations
|
|
|
|
|
|
|
|
// These are passed around by the code generating functions to track the
|
|
|
|
// destination of a computation's value.
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub enum Dest {
|
2012-08-28 17:54:45 -05:00
|
|
|
SaveIn(ValueRef),
|
|
|
|
Ignore,
|
|
|
|
}
|
|
|
|
|
2013-02-26 19:47:41 -06:00
|
|
|
pub impl Dest {
|
2013-02-22 00:41:37 -06:00
|
|
|
fn to_str(&self, ccx: @CrateContext) -> ~str {
|
|
|
|
match *self {
|
2012-08-28 17:54:45 -05:00
|
|
|
SaveIn(v) => fmt!("SaveIn(%s)", val_str(ccx.tn, v)),
|
|
|
|
Ignore => ~"Ignore"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl cmp::Eq for Dest {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &Dest) -> bool {
|
|
|
|
match ((*self), (*other)) {
|
|
|
|
(SaveIn(e0a), SaveIn(e0b)) => e0a == e0b,
|
|
|
|
(Ignore, Ignore) => true,
|
|
|
|
(SaveIn(*), _) => false,
|
|
|
|
(Ignore, _) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pure fn ne(&self, other: &Dest) -> bool { !(*self).eq(other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-10-31 18:04:10 -05:00
|
|
|
fn drop_and_cancel_clean(bcx: block, dat: Datum) -> block {
|
2012-10-31 17:49:37 -05:00
|
|
|
let bcx = dat.drop_val(bcx);
|
|
|
|
dat.cancel_clean(bcx);
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn trans_to_datum(bcx: block, expr: @ast::expr) -> DatumBlock {
|
2012-09-11 23:25:01 -05:00
|
|
|
debug!("trans_to_datum(expr=%s)", bcx.expr_to_str(expr));
|
2013-02-05 21:41:45 -06:00
|
|
|
return match bcx.tcx().adjustments.find(&expr.id) {
|
2012-09-11 23:25:01 -05:00
|
|
|
None => {
|
|
|
|
trans_to_datum_unadjusted(bcx, expr)
|
|
|
|
}
|
2013-02-27 18:28:37 -06:00
|
|
|
Some(@AutoAddEnv(*)) => {
|
|
|
|
let mut bcx = bcx;
|
|
|
|
let mut datum = unpack_datum!(bcx, {
|
|
|
|
trans_to_datum_unadjusted(bcx, expr)
|
|
|
|
});
|
|
|
|
add_env(bcx, expr, datum)
|
|
|
|
}
|
|
|
|
Some(@AutoDerefRef(ref adj)) => {
|
2012-09-11 23:25:01 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
let mut datum = unpack_datum!(bcx, {
|
|
|
|
trans_to_datum_unadjusted(bcx, expr)
|
|
|
|
});
|
|
|
|
|
|
|
|
if adj.autoderefs > 0 {
|
2013-01-11 23:01:42 -06:00
|
|
|
let DatumBlock { bcx: new_bcx, datum: new_datum } =
|
|
|
|
datum.autoderef(bcx, expr.id, adj.autoderefs);
|
|
|
|
datum = new_datum;
|
|
|
|
bcx = new_bcx;
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
datum = match adj.autoref {
|
|
|
|
None => datum,
|
|
|
|
Some(ref autoref) => {
|
|
|
|
match autoref.kind {
|
|
|
|
AutoPtr => {
|
|
|
|
unpack_datum!(bcx, auto_ref(bcx, datum))
|
|
|
|
}
|
2012-11-04 22:41:00 -06:00
|
|
|
AutoBorrowVec => {
|
2012-09-11 23:25:01 -05:00
|
|
|
unpack_datum!(bcx, auto_slice(bcx, datum))
|
|
|
|
}
|
2012-12-06 18:29:17 -06:00
|
|
|
AutoBorrowVecRef => {
|
|
|
|
unpack_datum!(bcx, auto_slice_and_ref(bcx, datum))
|
|
|
|
}
|
2012-11-04 22:41:00 -06:00
|
|
|
AutoBorrowFn => {
|
|
|
|
// currently, all closure types are
|
|
|
|
// represented precisely the same, so no
|
|
|
|
// runtime adjustment is required:
|
|
|
|
datum
|
|
|
|
}
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("after adjustments, datum=%s", datum.to_str(bcx.ccx()));
|
|
|
|
|
|
|
|
return DatumBlock {bcx: bcx, datum: datum};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fn auto_ref(bcx: block, datum: Datum) -> DatumBlock {
|
|
|
|
DatumBlock {bcx: bcx, datum: datum.to_rptr(bcx)}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn auto_slice(bcx: block, datum: Datum) -> DatumBlock {
|
|
|
|
// This is not the most efficient thing possible; since slices
|
|
|
|
// are two words it'd be better if this were compiled in
|
|
|
|
// 'dest' mode, but I can't find a nice way to structure the
|
|
|
|
// code and keep it DRY that accommodates that use case at the
|
|
|
|
// moment.
|
|
|
|
|
|
|
|
let tcx = bcx.tcx();
|
|
|
|
let unit_ty = ty::sequence_element_type(tcx, datum.ty);
|
|
|
|
let (base, len) = datum.get_base_and_len(bcx);
|
|
|
|
|
|
|
|
// this type may have a different region/mutability than the
|
|
|
|
// real one, but it will have the same runtime representation
|
2013-01-14 23:36:27 -06:00
|
|
|
let slice_ty = ty::mk_evec(tcx,
|
|
|
|
ty::mt { ty: unit_ty, mutbl: ast::m_imm },
|
2012-09-11 23:25:01 -05:00
|
|
|
ty::vstore_slice(ty::re_static));
|
|
|
|
|
|
|
|
let scratch = scratch_datum(bcx, slice_ty, false);
|
|
|
|
Store(bcx, base, GEPi(bcx, scratch.val, [0u, abi::slice_elt_base]));
|
|
|
|
Store(bcx, len, GEPi(bcx, scratch.val, [0u, abi::slice_elt_len]));
|
|
|
|
DatumBlock {bcx: bcx, datum: scratch}
|
|
|
|
}
|
2012-12-06 18:29:17 -06:00
|
|
|
|
2013-02-27 18:28:37 -06:00
|
|
|
fn add_env(bcx: block, expr: @ast::expr, datum: Datum) -> DatumBlock {
|
|
|
|
// This is not the most efficient thing possible; since closures
|
|
|
|
// are two words it'd be better if this were compiled in
|
|
|
|
// 'dest' mode, but I can't find a nice way to structure the
|
|
|
|
// code and keep it DRY that accommodates that use case at the
|
|
|
|
// moment.
|
|
|
|
|
|
|
|
let tcx = bcx.tcx();
|
|
|
|
let closure_ty = expr_ty_adjusted(bcx, expr);
|
|
|
|
debug!("add_env(closure_ty=%s)", ty_to_str(tcx, closure_ty));
|
|
|
|
let scratch = scratch_datum(bcx, closure_ty, false);
|
|
|
|
let llfn = GEPi(bcx, scratch.val, [0u, abi::fn_field_code]);
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(datum.appropriate_mode() == ByValue);
|
2013-02-27 18:28:37 -06:00
|
|
|
Store(bcx, datum.to_appropriate_llval(bcx), llfn);
|
|
|
|
let llenv = GEPi(bcx, scratch.val, [0u, abi::fn_field_box]);
|
|
|
|
Store(bcx, base::null_env_ptr(bcx), llenv);
|
|
|
|
DatumBlock {bcx: bcx, datum: scratch}
|
|
|
|
}
|
|
|
|
|
2012-12-06 18:29:17 -06:00
|
|
|
fn auto_slice_and_ref(bcx: block, datum: Datum) -> DatumBlock {
|
|
|
|
let DatumBlock { bcx, datum } = auto_slice(bcx, datum);
|
|
|
|
auto_ref(bcx, datum)
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn trans_into(bcx: block, expr: @ast::expr, dest: Dest) -> block {
|
2013-02-08 16:08:02 -06:00
|
|
|
if bcx.tcx().adjustments.contains_key(&expr.id) {
|
2013-01-10 12:59:58 -06:00
|
|
|
// use trans_to_datum, which is mildly less efficient but
|
|
|
|
// which will perform the adjustments:
|
|
|
|
let datumblock = trans_to_datum(bcx, expr);
|
|
|
|
return match dest {
|
|
|
|
Ignore => datumblock.bcx,
|
|
|
|
SaveIn(lldest) => datumblock.store_to(expr.id, INIT, lldest)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let ty = expr_ty(bcx, expr);
|
|
|
|
|
|
|
|
debug!("trans_into_unadjusted(expr=%s, dest=%s)",
|
|
|
|
bcx.expr_to_str(expr),
|
|
|
|
dest.to_str(bcx.ccx()));
|
|
|
|
let _indenter = indenter();
|
|
|
|
|
|
|
|
debuginfo::update_source_pos(bcx, expr.span);
|
|
|
|
|
|
|
|
let dest = {
|
|
|
|
if ty::type_is_nil(ty) || ty::type_is_bot(ty) {
|
|
|
|
Ignore
|
|
|
|
} else {
|
|
|
|
dest
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let kind = bcx.expr_kind(expr);
|
|
|
|
debug!("expr kind = %?", kind);
|
|
|
|
return match kind {
|
|
|
|
ty::LvalueExpr => {
|
|
|
|
let datumblock = trans_lvalue_unadjusted(bcx, expr);
|
2012-09-11 23:25:01 -05:00
|
|
|
match dest {
|
|
|
|
Ignore => datumblock.bcx,
|
2013-01-10 12:59:58 -06:00
|
|
|
SaveIn(lldest) => datumblock.store_to(expr.id, INIT, lldest)
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
ty::RvalueDatumExpr => {
|
|
|
|
let datumblock = trans_rvalue_datum_unadjusted(bcx, expr);
|
|
|
|
match dest {
|
|
|
|
Ignore => datumblock.drop_val(),
|
|
|
|
|
|
|
|
// NB: We always do `move_to()` regardless of the
|
|
|
|
// moves_map because we're processing an rvalue
|
|
|
|
SaveIn(lldest) => datumblock.move_to(INIT, lldest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::RvalueDpsExpr => {
|
|
|
|
trans_rvalue_dps_unadjusted(bcx, expr, dest)
|
|
|
|
}
|
|
|
|
ty::RvalueStmtExpr => {
|
|
|
|
trans_rvalue_stmt_unadjusted(bcx, expr)
|
|
|
|
}
|
|
|
|
};
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_lvalue(bcx: block, expr: @ast::expr) -> DatumBlock {
|
2013-01-10 12:59:58 -06:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates an lvalue expression, always yielding a by-ref
|
|
|
|
* datum. Generally speaking you should call trans_to_datum()
|
|
|
|
* instead, but sometimes we call trans_lvalue() directly as a
|
|
|
|
* means of asserting that a particular expression is an lvalue. */
|
|
|
|
|
2013-02-05 21:41:45 -06:00
|
|
|
return match bcx.tcx().adjustments.find(&expr.id) {
|
2012-09-11 23:25:01 -05:00
|
|
|
None => trans_lvalue_unadjusted(bcx, expr),
|
|
|
|
Some(_) => {
|
|
|
|
bcx.sess().span_bug(
|
|
|
|
expr.span,
|
|
|
|
fmt!("trans_lvalue() called on an expression \
|
|
|
|
with adjustments"));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_to_datum_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock {
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates an expression into a datum. If this expression
|
|
|
|
* is an rvalue, this will result in a temporary value being
|
|
|
|
* created. If you already know where the result should be stored,
|
|
|
|
* you should use `trans_into()` instead. */
|
|
|
|
|
|
|
|
let mut bcx = bcx;
|
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
debug!("trans_to_datum_unadjusted(expr=%s)", bcx.expr_to_str(expr));
|
2012-08-28 17:54:45 -05:00
|
|
|
let _indenter = indenter();
|
|
|
|
|
|
|
|
debuginfo::update_source_pos(bcx, expr.span);
|
|
|
|
|
|
|
|
match ty::expr_kind(bcx.tcx(), bcx.ccx().maps.method_map, expr) {
|
|
|
|
ty::LvalueExpr => {
|
2012-09-11 23:25:01 -05:00
|
|
|
return trans_lvalue_unadjusted(bcx, expr);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::RvalueDatumExpr => {
|
2012-09-11 23:25:01 -05:00
|
|
|
let datum = unpack_datum!(bcx, {
|
|
|
|
trans_rvalue_datum_unadjusted(bcx, expr)
|
|
|
|
});
|
2012-08-28 17:54:45 -05:00
|
|
|
datum.add_clean(bcx);
|
|
|
|
return DatumBlock {bcx: bcx, datum: datum};
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::RvalueStmtExpr => {
|
2012-09-11 23:25:01 -05:00
|
|
|
bcx = trans_rvalue_stmt_unadjusted(bcx, expr);
|
2012-08-28 17:54:45 -05:00
|
|
|
return nil(bcx, expr_ty(bcx, expr));
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::RvalueDpsExpr => {
|
|
|
|
let ty = expr_ty(bcx, expr);
|
|
|
|
if ty::type_is_nil(ty) || ty::type_is_bot(ty) {
|
2012-09-11 23:25:01 -05:00
|
|
|
bcx = trans_rvalue_dps_unadjusted(bcx, expr, Ignore);
|
2012-08-28 17:54:45 -05:00
|
|
|
return nil(bcx, ty);
|
|
|
|
} else {
|
|
|
|
let scratch = scratch_datum(bcx, ty, false);
|
2012-09-11 23:25:01 -05:00
|
|
|
bcx = trans_rvalue_dps_unadjusted(
|
|
|
|
bcx, expr, SaveIn(scratch.val));
|
2012-09-06 17:21:42 -05:00
|
|
|
|
|
|
|
// Note: this is not obviously a good idea. It causes
|
|
|
|
// immediate values to be loaded immediately after a
|
|
|
|
// return from a call or other similar expression,
|
|
|
|
// which in turn leads to alloca's having shorter
|
|
|
|
// lifetimes and hence larger stack frames. However,
|
|
|
|
// in turn it can lead to more register pressure.
|
|
|
|
// Still, in practice it seems to increase
|
|
|
|
// performance, since we have fewer problems with
|
|
|
|
// morestack churn.
|
|
|
|
let scratch = scratch.to_appropriate_datum(bcx);
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
scratch.add_clean(bcx);
|
|
|
|
return DatumBlock {bcx: bcx, datum: scratch};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nil(bcx: block, ty: ty::t) -> DatumBlock {
|
|
|
|
let datum = immediate_rvalue(C_nil(), ty);
|
|
|
|
DatumBlock {bcx: bcx, datum: datum}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
fn trans_rvalue_datum_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock {
|
|
|
|
let _icx = bcx.insn_ctxt("trans_rvalue_datum_unadjusted");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-02-20 18:41:21 -06:00
|
|
|
trace_span!(bcx, expr.span, @shorten(bcx.expr_to_str(expr)));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
match expr.node {
|
2013-02-27 18:28:37 -06:00
|
|
|
ast::expr_path(_) => {
|
|
|
|
return trans_def_datum_unadjusted(bcx, expr, bcx.def(expr.id));
|
|
|
|
}
|
2012-11-21 13:39:06 -06:00
|
|
|
ast::expr_vstore(contents, ast::expr_vstore_box) |
|
|
|
|
ast::expr_vstore(contents, ast::expr_vstore_mut_box) => {
|
2013-02-20 17:02:21 -06:00
|
|
|
return tvec::trans_uniq_or_managed_vstore(bcx, heap_managed,
|
2012-08-28 17:54:45 -05:00
|
|
|
expr, contents);
|
|
|
|
}
|
2012-09-11 23:25:01 -05:00
|
|
|
ast::expr_vstore(contents, ast::expr_vstore_uniq) => {
|
2013-02-20 17:02:21 -06:00
|
|
|
let heap = heap_for_unique(bcx, expr_ty(bcx, contents));
|
|
|
|
return tvec::trans_uniq_or_managed_vstore(bcx, heap,
|
2012-08-28 17:54:45 -05:00
|
|
|
expr, contents);
|
|
|
|
}
|
|
|
|
ast::expr_lit(lit) => {
|
|
|
|
return trans_immediate_lit(bcx, expr, *lit);
|
|
|
|
}
|
|
|
|
ast::expr_binary(op, lhs, rhs) => {
|
|
|
|
// if overloaded, would be RvalueDpsExpr
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(!bcx.ccx().maps.method_map.contains_key(&expr.id));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
return trans_binary(bcx, expr, op, lhs, rhs);
|
|
|
|
}
|
|
|
|
ast::expr_unary(op, x) => {
|
|
|
|
return trans_unary_datum(bcx, expr, op, x);
|
|
|
|
}
|
|
|
|
ast::expr_addr_of(_, x) => {
|
|
|
|
return trans_addr_of(bcx, expr, x);
|
|
|
|
}
|
|
|
|
ast::expr_cast(val, _) => {
|
|
|
|
return trans_imm_cast(bcx, val, expr.id);
|
|
|
|
}
|
2012-10-27 19:14:09 -05:00
|
|
|
ast::expr_paren(e) => {
|
|
|
|
return trans_rvalue_datum_unadjusted(bcx, e);
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
2012-09-11 23:25:01 -05:00
|
|
|
fmt!("trans_rvalue_datum_unadjusted reached \
|
|
|
|
fall-through case: %?",
|
2012-08-28 17:54:45 -05:00
|
|
|
expr.node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
fn trans_rvalue_stmt_unadjusted(bcx: block, expr: @ast::expr) -> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
let _icx = bcx.insn_ctxt("trans_rvalue_stmt");
|
|
|
|
|
2013-02-20 18:41:21 -06:00
|
|
|
trace_span!(bcx, expr.span, @shorten(bcx.expr_to_str(expr)));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
match expr.node {
|
|
|
|
ast::expr_break(label_opt) => {
|
2012-10-18 14:20:18 -05:00
|
|
|
return controlflow::trans_break(bcx, label_opt);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_again(label_opt) => {
|
2012-10-18 14:20:18 -05:00
|
|
|
return controlflow::trans_cont(bcx, label_opt);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_ret(ex) => {
|
|
|
|
return controlflow::trans_ret(bcx, ex);
|
|
|
|
}
|
|
|
|
ast::expr_log(_, lvl, a) => {
|
|
|
|
return controlflow::trans_log(expr, lvl, bcx, a);
|
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
ast::expr_while(cond, ref body) => {
|
2013-01-31 19:12:29 -06:00
|
|
|
return controlflow::trans_while(bcx, cond, body);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
ast::expr_loop(ref body, opt_label) => {
|
2013-01-31 19:12:29 -06:00
|
|
|
return controlflow::trans_loop(bcx, body, opt_label);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_assign(dst, src) => {
|
2013-01-10 12:59:58 -06:00
|
|
|
let src_datum = unpack_datum!(
|
|
|
|
bcx, trans_to_datum(bcx, src));
|
|
|
|
let dst_datum = unpack_datum!(
|
|
|
|
bcx, trans_lvalue(bcx, dst));
|
|
|
|
return src_datum.store_to_datum(
|
|
|
|
bcx, src.id, DROP_EXISTING, dst_datum);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_swap(dst, src) => {
|
|
|
|
let dst_datum = unpack_datum!(bcx, trans_lvalue(bcx, dst));
|
|
|
|
let src_datum = unpack_datum!(bcx, trans_lvalue(bcx, src));
|
|
|
|
|
2013-02-19 18:12:12 -06:00
|
|
|
// If the source and destination are the same, then don't swap.
|
|
|
|
// Avoids performing an overlapping memcpy
|
|
|
|
let dst_datum_ref = dst_datum.to_ref_llval(bcx);
|
|
|
|
let src_datum_ref = src_datum.to_ref_llval(bcx);
|
|
|
|
let cmp = ICmp(bcx, lib::llvm::IntEQ,
|
|
|
|
src_datum_ref,
|
|
|
|
dst_datum_ref);
|
|
|
|
|
|
|
|
let swap_cx = base::sub_block(bcx, ~"swap");
|
|
|
|
let next_cx = base::sub_block(bcx, ~"next");
|
|
|
|
|
|
|
|
CondBr(bcx, cmp, next_cx.llbb, swap_cx.llbb);
|
|
|
|
|
|
|
|
let scratch = scratch_datum(swap_cx, dst_datum.ty, false);
|
|
|
|
|
|
|
|
let swap_cx = dst_datum.move_to_datum(swap_cx, INIT, scratch);
|
|
|
|
let swap_cx = src_datum.move_to_datum(swap_cx, INIT, dst_datum);
|
|
|
|
let swap_cx = scratch.move_to_datum(swap_cx, INIT, src_datum);
|
|
|
|
|
|
|
|
Br(swap_cx, next_cx.llbb);
|
|
|
|
|
|
|
|
return next_cx;
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_assign_op(op, dst, src) => {
|
|
|
|
return trans_assign_op(bcx, expr, op, dst, src);
|
|
|
|
}
|
2012-10-27 19:14:09 -05:00
|
|
|
ast::expr_paren(a) => {
|
|
|
|
return trans_rvalue_stmt_unadjusted(bcx, a);
|
|
|
|
}
|
2013-03-12 19:53:25 -05:00
|
|
|
ast::expr_inline_asm(asm, ref ins, ref outs,
|
2013-03-13 02:32:23 -05:00
|
|
|
clobs, volatile, alignstack) => {
|
|
|
|
let mut constraints = ~[];
|
|
|
|
let mut cleanups = ~[];
|
2013-03-13 12:11:36 -05:00
|
|
|
let mut aoutputs = ~[];
|
2013-03-13 02:32:23 -05:00
|
|
|
|
2013-03-13 03:21:52 -05:00
|
|
|
let outputs = do outs.map |&(c, out)| {
|
|
|
|
constraints.push(copy *c);
|
2013-03-13 02:32:23 -05:00
|
|
|
|
2013-03-13 12:11:36 -05:00
|
|
|
let aoutty = ty::arg {
|
2013-03-13 03:21:52 -05:00
|
|
|
mode: ast::expl(ast::by_val),
|
|
|
|
ty: expr_ty(bcx, out)
|
|
|
|
};
|
2013-03-13 12:11:36 -05:00
|
|
|
aoutputs.push(unpack_result!(bcx, {
|
|
|
|
callee::trans_arg_expr(bcx, aoutty, out, &mut cleanups,
|
|
|
|
None, callee::DontAutorefArg)
|
|
|
|
}));
|
|
|
|
|
|
|
|
let e = match out.node {
|
|
|
|
ast::expr_addr_of(_, e) => e,
|
|
|
|
_ => fail!(~"Expression must be addr of")
|
|
|
|
};
|
|
|
|
|
|
|
|
let outty = ty::arg {
|
|
|
|
mode: ast::expl(ast::by_val),
|
|
|
|
ty: expr_ty(bcx, e)
|
|
|
|
};
|
2013-03-13 02:32:23 -05:00
|
|
|
|
2013-03-13 03:21:52 -05:00
|
|
|
unpack_result!(bcx, {
|
2013-03-13 12:11:36 -05:00
|
|
|
callee::trans_arg_expr(bcx, outty, e, &mut cleanups,
|
2013-03-13 03:21:52 -05:00
|
|
|
None, callee::DontAutorefArg)
|
|
|
|
})
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
for cleanups.each |c| {
|
|
|
|
revoke_clean(bcx, *c);
|
|
|
|
}
|
|
|
|
cleanups = ~[];
|
|
|
|
|
|
|
|
let inputs = do ins.map |&(c, in)| {
|
2013-03-13 02:32:23 -05:00
|
|
|
constraints.push(copy *c);
|
|
|
|
|
|
|
|
let inty = ty::arg {
|
|
|
|
mode: ast::expl(ast::by_val),
|
|
|
|
ty: expr_ty(bcx, in)
|
|
|
|
};
|
|
|
|
|
|
|
|
unpack_result!(bcx, {
|
|
|
|
callee::trans_arg_expr(bcx, inty, in, &mut cleanups,
|
|
|
|
None, callee::DontAutorefArg)
|
|
|
|
})
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
for cleanups.each |c| {
|
|
|
|
revoke_clean(bcx, *c);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut constraints = str::connect(constraints, ",");
|
|
|
|
|
|
|
|
// Add the clobbers
|
|
|
|
if *clobs != ~"" {
|
|
|
|
if constraints == ~"" {
|
|
|
|
constraints += *clobs;
|
|
|
|
} else {
|
|
|
|
constraints += ~"," + *clobs;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
constraints += *clobs;
|
|
|
|
}
|
|
|
|
|
2013-03-13 03:21:52 -05:00
|
|
|
io::println(fmt!("Constraints: %?\n", constraints));
|
2013-03-13 02:32:23 -05:00
|
|
|
|
2013-03-13 03:21:52 -05:00
|
|
|
// TODO: Handle >1 outputs
|
|
|
|
let output = outputs[0];
|
|
|
|
|
|
|
|
let r = do str::as_c_str(*asm) |a| {
|
2013-03-13 02:32:23 -05:00
|
|
|
do str::as_c_str(constraints) |c| {
|
2013-03-13 03:21:52 -05:00
|
|
|
InlineAsmCall(bcx, a, c, inputs, output, volatile,
|
|
|
|
alignstack, lib::llvm::AD_ATT)
|
2013-03-12 19:53:25 -05:00
|
|
|
}
|
2013-03-13 03:21:52 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Handle >1 outputs
|
2013-03-13 12:11:36 -05:00
|
|
|
let op = PointerCast(bcx, aoutputs[0], T_ptr(val_ty(output)));
|
2013-03-13 03:21:52 -05:00
|
|
|
Store(bcx, r, op);
|
|
|
|
|
2013-03-12 19:53:25 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
2012-09-11 23:25:01 -05:00
|
|
|
fmt!("trans_rvalue_stmt_unadjusted reached \
|
|
|
|
fall-through case: %?",
|
2012-08-28 17:54:45 -05:00
|
|
|
expr.node));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
fn trans_rvalue_dps_unadjusted(bcx: block, expr: @ast::expr,
|
|
|
|
dest: Dest) -> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
2012-09-11 23:25:01 -05:00
|
|
|
let _icx = bcx.insn_ctxt("trans_rvalue_dps_unadjusted");
|
2012-08-28 17:54:45 -05:00
|
|
|
let tcx = bcx.tcx();
|
|
|
|
|
2013-02-20 18:41:21 -06:00
|
|
|
trace_span!(bcx, expr.span, @shorten(bcx.expr_to_str(expr)));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
match expr.node {
|
2012-10-27 19:14:09 -05:00
|
|
|
ast::expr_paren(e) => {
|
|
|
|
return trans_rvalue_dps_unadjusted(bcx, e, dest);
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::expr_path(_) => {
|
2012-09-11 23:25:01 -05:00
|
|
|
return trans_def_dps_unadjusted(bcx, expr,
|
|
|
|
bcx.def(expr.id), dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
ast::expr_if(cond, ref thn, els) => {
|
2013-01-31 19:12:29 -06:00
|
|
|
return controlflow::trans_if(bcx, cond, thn, els, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
ast::expr_match(discr, ref arms) => {
|
2013-01-04 08:52:07 -06:00
|
|
|
return _match::trans_match(bcx, expr, discr, /*bad*/copy *arms,
|
|
|
|
dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
ast::expr_block(ref blk) => {
|
2013-01-10 12:59:58 -06:00
|
|
|
return do base::with_scope(bcx, blk.info(),
|
2012-08-28 17:54:45 -05:00
|
|
|
~"block-expr body") |bcx| {
|
2013-01-31 19:12:29 -06:00
|
|
|
controlflow::trans_block(bcx, blk, dest)
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
|
|
|
}
|
2012-12-04 23:13:02 -06:00
|
|
|
ast::expr_struct(_, ref fields, base) => {
|
2012-12-04 12:50:00 -06:00
|
|
|
return trans_rec_or_struct(bcx, (*fields), base, expr.id, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
ast::expr_tup(ref args) => {
|
2013-02-23 15:36:01 -06:00
|
|
|
let repr = adt::represent_type(bcx.ccx(), expr_ty(bcx, expr));
|
2013-02-25 03:49:21 -06:00
|
|
|
return trans_adt(bcx, repr, 0, args.mapi(|i, arg| (i, *arg)),
|
2013-02-24 00:53:40 -06:00
|
|
|
None, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-01-30 11:56:33 -06:00
|
|
|
ast::expr_lit(@codemap::spanned {node: ast::lit_str(s), _}) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return tvec::trans_lit_str(bcx, expr, s, dest);
|
|
|
|
}
|
2012-12-07 18:26:52 -06:00
|
|
|
ast::expr_vstore(contents, ast::expr_vstore_slice) |
|
|
|
|
ast::expr_vstore(contents, ast::expr_vstore_mut_slice) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return tvec::trans_slice_vstore(bcx, expr, contents, dest);
|
|
|
|
}
|
2012-09-11 23:25:01 -05:00
|
|
|
ast::expr_vstore(contents, ast::expr_vstore_fixed(_)) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return tvec::trans_fixed_vstore(bcx, expr, contents, dest);
|
|
|
|
}
|
|
|
|
ast::expr_vec(*) | ast::expr_repeat(*) => {
|
|
|
|
return tvec::trans_fixed_vstore(bcx, expr, expr, dest);
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
ast::expr_fn_block(ref decl, ref body) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let expr_ty = expr_ty(bcx, expr);
|
2013-01-31 19:12:29 -06:00
|
|
|
let sigil = ty::ty_closure_sigil(expr_ty);
|
|
|
|
debug!("translating fn_block %s with type %s",
|
|
|
|
expr_to_str(expr, tcx.sess.intr()),
|
|
|
|
ty_to_str(tcx, expr_ty));
|
|
|
|
return closure::trans_expr_fn(bcx, sigil, decl, body,
|
|
|
|
expr.id, expr.id,
|
|
|
|
None, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_loop_body(blk) => {
|
2013-01-31 19:12:29 -06:00
|
|
|
let expr_ty = expr_ty(bcx, expr);
|
|
|
|
let sigil = ty::ty_closure_sigil(expr_ty);
|
|
|
|
match blk.node {
|
|
|
|
ast::expr_fn_block(ref decl, ref body) => {
|
|
|
|
return closure::trans_expr_fn(bcx, sigil,
|
|
|
|
decl, body,
|
|
|
|
expr.id, blk.id,
|
|
|
|
Some(None), dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.sess().impossible_case(
|
2013-01-31 19:12:29 -06:00
|
|
|
expr.span,
|
|
|
|
"loop_body has the wrong kind of contents")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::expr_do_body(blk) => {
|
|
|
|
return trans_into(bcx, blk, dest);
|
|
|
|
}
|
|
|
|
ast::expr_copy(a) => {
|
|
|
|
return trans_into(bcx, a, dest);
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
ast::expr_call(f, ref args, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return callee::trans_call(
|
2013-01-10 12:59:58 -06:00
|
|
|
bcx, expr, f, callee::ArgExprs(*args), expr.id, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
ast::expr_method_call(rcvr, _, _, ref args, _) => {
|
2012-11-30 13:18:25 -06:00
|
|
|
return callee::trans_method_call(bcx,
|
|
|
|
expr,
|
|
|
|
rcvr,
|
2013-01-10 12:59:58 -06:00
|
|
|
callee::ArgExprs(*args),
|
2012-11-30 13:18:25 -06:00
|
|
|
dest);
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::expr_binary(_, lhs, rhs) => {
|
|
|
|
// if not overloaded, would be RvalueDatumExpr
|
2012-09-19 20:00:26 -05:00
|
|
|
return trans_overloaded_op(bcx, expr, lhs, ~[rhs], dest,
|
|
|
|
DoAutorefArg);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_unary(_, subexpr) => {
|
|
|
|
// if not overloaded, would be RvalueDatumExpr
|
2012-09-19 20:00:26 -05:00
|
|
|
return trans_overloaded_op(bcx, expr, subexpr, ~[], dest,
|
|
|
|
DontAutorefArg);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_index(base, idx) => {
|
|
|
|
// if not overloaded, would be RvalueDatumExpr
|
2012-09-19 20:00:26 -05:00
|
|
|
return trans_overloaded_op(bcx, expr, base, ~[idx], dest,
|
|
|
|
DontAutorefArg);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_cast(val, _) => {
|
2012-10-31 17:09:26 -05:00
|
|
|
match ty::get(node_id_type(bcx, expr.id)).sty {
|
2013-03-08 23:16:09 -06:00
|
|
|
ty::ty_trait(_, _, store) => {
|
2012-10-31 17:09:26 -05:00
|
|
|
return meth::trans_trait_cast(bcx, val, expr.id, dest,
|
2013-03-08 23:16:09 -06:00
|
|
|
store);
|
2012-10-31 17:09:26 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(expr.span,
|
|
|
|
~"expr_cast of non-trait");
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_assign_op(op, dst, src) => {
|
|
|
|
return trans_assign_op(bcx, expr, op, dst, src);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
2012-09-11 23:25:01 -05:00
|
|
|
fmt!("trans_rvalue_dps_unadjusted reached \
|
|
|
|
fall-through case: %?",
|
2012-08-28 17:54:45 -05:00
|
|
|
expr.node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
fn trans_def_dps_unadjusted(bcx: block, ref_expr: @ast::expr,
|
|
|
|
def: ast::def, dest: Dest) -> block {
|
|
|
|
let _icx = bcx.insn_ctxt("trans_def_dps_unadjusted");
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
|
|
|
let lldest = match dest {
|
|
|
|
SaveIn(lldest) => lldest,
|
|
|
|
Ignore => { return bcx; }
|
|
|
|
};
|
|
|
|
|
|
|
|
match def {
|
|
|
|
ast::def_variant(tid, vid) => {
|
2013-02-06 17:08:33 -06:00
|
|
|
let variant_info = ty::enum_variant_with_id(ccx.tcx, tid, vid);
|
|
|
|
if variant_info.args.len() > 0u {
|
2012-08-28 17:54:45 -05:00
|
|
|
// N-ary variant.
|
|
|
|
let fn_data = callee::trans_fn_ref(bcx, vid, ref_expr.id);
|
2013-02-27 18:28:37 -06:00
|
|
|
Store(bcx, fn_data.llfn, lldest);
|
|
|
|
return bcx;
|
2013-01-07 05:42:49 -06:00
|
|
|
} else {
|
2013-02-24 13:08:30 -06:00
|
|
|
// Nullary variant.
|
|
|
|
let ty = expr_ty(bcx, ref_expr);
|
|
|
|
let repr = adt::represent_type(ccx, ty);
|
2013-03-02 18:08:49 -06:00
|
|
|
adt::trans_start_init(bcx, repr, lldest,
|
|
|
|
variant_info.disr_val);
|
2013-01-07 05:42:49 -06:00
|
|
|
return bcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2012-12-10 15:47:54 -06:00
|
|
|
ast::def_struct(*) => {
|
2012-10-30 17:53:06 -05:00
|
|
|
// Nothing to do here.
|
|
|
|
// XXX: May not be true in the case of classes with destructors.
|
|
|
|
return bcx;
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(ref_expr.span, fmt!(
|
|
|
|
"Non-DPS def %? referened by %s",
|
|
|
|
def, bcx.node_id_to_str(ref_expr.id)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 18:28:37 -06:00
|
|
|
fn trans_def_datum_unadjusted(bcx: block,
|
|
|
|
ref_expr: @ast::expr,
|
|
|
|
def: ast::def) -> DatumBlock
|
|
|
|
{
|
|
|
|
let _icx = bcx.insn_ctxt("trans_def_datum_unadjusted");
|
|
|
|
|
|
|
|
match def {
|
|
|
|
ast::def_fn(did, _) | ast::def_static_method(did, None, _) => {
|
|
|
|
let fn_data = callee::trans_fn_ref(bcx, did, ref_expr.id);
|
|
|
|
return fn_data_to_datum(bcx, ref_expr, did, fn_data);
|
|
|
|
}
|
|
|
|
ast::def_static_method(impl_did, Some(trait_did), _) => {
|
|
|
|
let fn_data = meth::trans_static_method_callee(bcx, impl_did,
|
|
|
|
trait_did,
|
|
|
|
ref_expr.id);
|
|
|
|
return fn_data_to_datum(bcx, ref_expr, impl_did, fn_data);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(ref_expr.span, fmt!(
|
|
|
|
"Non-DPS def %? referened by %s",
|
|
|
|
def, bcx.node_id_to_str(ref_expr.id)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fn_data_to_datum(bcx: block,
|
|
|
|
ref_expr: @ast::expr,
|
|
|
|
def_id: ast::def_id,
|
|
|
|
fn_data: callee::FnData) -> DatumBlock {
|
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates a reference to a top-level fn item into a rust
|
|
|
|
* value. This is just a fn pointer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let is_extern = {
|
|
|
|
let fn_tpt = ty::lookup_item_type(bcx.tcx(), def_id);
|
|
|
|
ty::ty_fn_purity(fn_tpt.ty) == ast::extern_fn
|
|
|
|
};
|
|
|
|
let (rust_ty, llval) = if is_extern {
|
|
|
|
let rust_ty = ty::mk_ptr(
|
|
|
|
bcx.tcx(),
|
|
|
|
ty::mt {
|
|
|
|
ty: ty::mk_mach_uint(bcx.tcx(), ast::ty_u8),
|
|
|
|
mutbl: ast::m_imm
|
|
|
|
}); // *u8
|
|
|
|
(rust_ty, PointerCast(bcx, fn_data.llfn, T_ptr(T_i8())))
|
|
|
|
} else {
|
|
|
|
let fn_ty = expr_ty(bcx, ref_expr);
|
|
|
|
(fn_ty, fn_data.llfn)
|
|
|
|
};
|
|
|
|
return DatumBlock {
|
|
|
|
bcx: bcx,
|
|
|
|
datum: Datum {val: llval,
|
|
|
|
ty: rust_ty,
|
|
|
|
mode: ByValue,
|
|
|
|
source: RevokeClean}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
fn trans_lvalue_unadjusted(bcx: block, expr: @ast::expr) -> DatumBlock {
|
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates an lvalue expression, always yielding a by-ref
|
2013-01-10 12:59:58 -06:00
|
|
|
* datum. Does not apply any adjustments. */
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let _icx = bcx.insn_ctxt("trans_lval");
|
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
debug!("trans_lvalue(expr=%s)", bcx.expr_to_str(expr));
|
|
|
|
let _indenter = indenter();
|
|
|
|
|
2013-02-20 18:41:21 -06:00
|
|
|
trace_span!(bcx, expr.span, @shorten(bcx.expr_to_str(expr)));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let unrooted_datum = unpack_datum!(bcx, unrooted(bcx, expr));
|
|
|
|
|
|
|
|
// If the lvalue must remain rooted, create a scratch datum, copy
|
|
|
|
// the lvalue in there, and then arrange for it to be cleaned up
|
|
|
|
// at the end of the scope with id `scope_id`:
|
2013-01-25 18:57:39 -06:00
|
|
|
let root_key = root_map_key { id: expr.id, derefs: 0u };
|
2013-02-05 21:41:45 -06:00
|
|
|
for bcx.ccx().maps.root_map.find(&root_key).each |&root_info| {
|
2013-01-11 23:01:42 -06:00
|
|
|
bcx = unrooted_datum.root(bcx, root_info);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return DatumBlock {bcx: bcx, datum: unrooted_datum};
|
|
|
|
|
|
|
|
fn unrooted(bcx: block, expr: @ast::expr) -> DatumBlock {
|
2013-01-10 12:59:58 -06:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates `expr`. Note that this version generally
|
|
|
|
* yields an unrooted, unmoved version. Rooting and possible
|
|
|
|
* moves are dealt with above in trans_lvalue_unadjusted().
|
|
|
|
*
|
|
|
|
* One exception is if `expr` refers to a local variable,
|
|
|
|
* in which case the source may already be FromMovedLvalue
|
|
|
|
* if appropriate.
|
|
|
|
*/
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
match expr.node {
|
2012-10-27 19:14:09 -05:00
|
|
|
ast::expr_paren(e) => {
|
|
|
|
return unrooted(bcx, e);
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
ast::expr_path(_) => {
|
|
|
|
return trans_def_lvalue(bcx, expr, bcx.def(expr.id));
|
|
|
|
}
|
|
|
|
ast::expr_field(base, ident, _) => {
|
2013-01-10 12:59:58 -06:00
|
|
|
return trans_rec_field(bcx, base, ident);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::expr_index(base, idx) => {
|
|
|
|
return trans_index(bcx, expr, base, idx);
|
|
|
|
}
|
|
|
|
ast::expr_unary(ast::deref, base) => {
|
|
|
|
let basedatum = unpack_datum!(bcx, trans_to_datum(bcx, base));
|
2013-01-11 23:01:42 -06:00
|
|
|
return basedatum.deref(bcx, base, 0);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
|
|
|
fmt!("trans_lvalue reached fall-through case: %?",
|
|
|
|
expr.node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
fn trans_rec_field(bcx: block,
|
|
|
|
base: @ast::expr,
|
|
|
|
field: ast::ident) -> DatumBlock {
|
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates `base.field`. Note that this version always
|
|
|
|
* yields an unrooted, unmoved version. Rooting and possible
|
|
|
|
* moves are dealt with above in trans_lvalue_unadjusted().
|
|
|
|
*/
|
|
|
|
|
|
|
|
let mut bcx = bcx;
|
|
|
|
let _icx = bcx.insn_ctxt("trans_rec_field");
|
|
|
|
|
|
|
|
let base_datum = unpack_datum!(bcx, trans_to_datum(bcx, base));
|
2013-02-24 14:42:39 -06:00
|
|
|
let repr = adt::represent_type(bcx.ccx(), base_datum.ty);
|
|
|
|
do with_field_tys(bcx.tcx(), base_datum.ty, None) |discr, field_tys| {
|
2013-01-10 12:59:58 -06:00
|
|
|
let ix = ty::field_idx_strict(bcx.tcx(), field, field_tys);
|
2012-08-28 17:54:45 -05:00
|
|
|
DatumBlock {
|
2013-02-24 14:42:39 -06:00
|
|
|
datum: do base_datum.get_element(bcx,
|
|
|
|
field_tys[ix].mt.ty,
|
|
|
|
ZeroMem) |srcval| {
|
2013-03-02 18:08:49 -06:00
|
|
|
adt::trans_field_ptr(bcx, repr, srcval, discr, ix)
|
2013-02-24 14:42:39 -06:00
|
|
|
},
|
2013-01-10 12:59:58 -06:00
|
|
|
bcx: bcx
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_index(bcx: block,
|
|
|
|
index_expr: @ast::expr,
|
|
|
|
base: @ast::expr,
|
|
|
|
idx: @ast::expr) -> DatumBlock {
|
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates `base[idx]`. Note that this version always
|
|
|
|
* yields an unrooted, unmoved version. Rooting and possible
|
|
|
|
* moves are dealt with above in trans_lvalue_unadjusted().
|
|
|
|
*/
|
|
|
|
|
|
|
|
let _icx = bcx.insn_ctxt("trans_index");
|
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let base_ty = expr_ty(bcx, base);
|
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
let base_datum = unpack_datum!(bcx, trans_to_datum(bcx, base));
|
|
|
|
|
|
|
|
// Translate index expression and cast to a suitable LLVM integer.
|
|
|
|
// Rust is less strict than LLVM in this regard.
|
|
|
|
let Result {bcx, val: ix_val} = trans_to_datum(bcx, idx).to_result();
|
|
|
|
let ix_size = machine::llbitsize_of_real(bcx.ccx(), val_ty(ix_val));
|
|
|
|
let int_size = machine::llbitsize_of_real(bcx.ccx(), ccx.int_type);
|
|
|
|
let ix_val = {
|
|
|
|
if ix_size < int_size {
|
|
|
|
if ty::type_is_signed(expr_ty(bcx, idx)) {
|
|
|
|
SExt(bcx, ix_val, ccx.int_type)
|
|
|
|
} else { ZExt(bcx, ix_val, ccx.int_type) }
|
|
|
|
} else if ix_size > int_size {
|
|
|
|
Trunc(bcx, ix_val, ccx.int_type)
|
|
|
|
} else {
|
|
|
|
ix_val
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let vt = tvec::vec_types(bcx, base_datum.ty);
|
|
|
|
base::maybe_name_value(bcx.ccx(), vt.llunit_size, ~"unit_sz");
|
|
|
|
let scaled_ix = Mul(bcx, ix_val, vt.llunit_size);
|
|
|
|
base::maybe_name_value(bcx.ccx(), scaled_ix, ~"scaled_ix");
|
|
|
|
|
|
|
|
let mut (base, len) = base_datum.get_base_and_len(bcx);
|
|
|
|
|
|
|
|
if ty::type_is_str(base_ty) {
|
|
|
|
// acccount for null terminator in the case of string
|
|
|
|
len = Sub(bcx, len, C_uint(bcx.ccx(), 1u));
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("trans_index: base %s", val_str(bcx.ccx().tn, base));
|
|
|
|
debug!("trans_index: len %s", val_str(bcx.ccx().tn, len));
|
|
|
|
|
|
|
|
let bounds_check = ICmp(bcx, lib::llvm::IntUGE, scaled_ix, len);
|
|
|
|
let bcx = do with_cond(bcx, bounds_check) |bcx| {
|
|
|
|
let unscaled_len = UDiv(bcx, len, vt.llunit_size);
|
|
|
|
controlflow::trans_fail_bounds_check(bcx, index_expr.span,
|
|
|
|
ix_val, unscaled_len)
|
|
|
|
};
|
|
|
|
let elt = InBoundsGEP(bcx, base, ~[ix_val]);
|
|
|
|
let elt = PointerCast(bcx, elt, T_ptr(vt.llunit_ty));
|
|
|
|
return DatumBlock {
|
|
|
|
bcx: bcx,
|
|
|
|
datum: Datum {val: elt,
|
|
|
|
ty: vt.unit_ty,
|
|
|
|
mode: ByRef,
|
|
|
|
source: ZeroMem}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_def_lvalue(bcx: block,
|
|
|
|
ref_expr: @ast::expr,
|
|
|
|
def: ast::def)
|
|
|
|
-> DatumBlock
|
|
|
|
{
|
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates a reference to a path. Note that this version
|
|
|
|
* generally yields an unrooted, unmoved version. Rooting and
|
|
|
|
* possible moves are dealt with above in
|
|
|
|
* trans_lvalue_unadjusted(), with the caveat that local variables
|
|
|
|
* may already be in move mode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let _icx = bcx.insn_ctxt("trans_def_lvalue");
|
|
|
|
let ccx = bcx.ccx();
|
|
|
|
match def {
|
|
|
|
ast::def_const(did) => {
|
|
|
|
let const_ty = expr_ty(bcx, ref_expr);
|
2013-03-07 00:30:20 -06:00
|
|
|
|
|
|
|
#[cfg(stage0)]
|
|
|
|
fn get_did(_ccx: @CrateContext, did: ast::def_id)
|
|
|
|
-> ast::def_id {
|
|
|
|
did
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
#[cfg(stage3)]
|
|
|
|
fn get_did(ccx: @CrateContext, did: ast::def_id)
|
|
|
|
-> ast::def_id {
|
|
|
|
if did.crate != ast::local_crate {
|
|
|
|
inline::maybe_instantiate_inline(ccx, did, true)
|
|
|
|
} else {
|
|
|
|
did
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(stage0)]
|
|
|
|
fn get_val(bcx: block, did: ast::def_id, const_ty: ty::t)
|
|
|
|
-> ValueRef {
|
|
|
|
let ccx = bcx.ccx();
|
|
|
|
if did.crate == ast::local_crate {
|
|
|
|
// The LLVM global has the type of its initializer,
|
|
|
|
// which may not be equal to the enum's type for
|
|
|
|
// non-C-like enums.
|
|
|
|
PointerCast(bcx, base::get_item_val(ccx, did.node),
|
|
|
|
T_ptr(type_of(bcx.ccx(), const_ty)))
|
|
|
|
} else {
|
|
|
|
base::trans_external_path(ccx, did, const_ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
#[cfg(stage3)]
|
|
|
|
fn get_val(bcx: block, did: ast::def_id, const_ty: ty::t)
|
|
|
|
-> ValueRef {
|
2013-01-10 12:59:58 -06:00
|
|
|
// The LLVM global has the type of its initializer,
|
|
|
|
// which may not be equal to the enum's type for
|
|
|
|
// non-C-like enums.
|
2013-03-07 00:30:20 -06:00
|
|
|
PointerCast(bcx, base::get_item_val(bcx.ccx(), did.node),
|
2013-01-10 12:59:58 -06:00
|
|
|
T_ptr(type_of(bcx.ccx(), const_ty)))
|
2013-03-07 00:30:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let did = get_did(ccx, did);
|
|
|
|
let val = get_val(bcx, did, const_ty);
|
2013-01-10 12:59:58 -06:00
|
|
|
DatumBlock {
|
|
|
|
bcx: bcx,
|
|
|
|
datum: Datum {val: val,
|
|
|
|
ty: const_ty,
|
|
|
|
mode: ByRef,
|
|
|
|
source: ZeroMem}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
DatumBlock {
|
|
|
|
bcx: bcx,
|
|
|
|
datum: trans_local_var(bcx, def)
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
pub fn trans_local_var(bcx: block, def: ast::def) -> Datum {
|
2012-08-28 17:54:45 -05:00
|
|
|
let _icx = bcx.insn_ctxt("trans_local_var");
|
|
|
|
|
|
|
|
return match def {
|
|
|
|
ast::def_upvar(nid, _, _, _) => {
|
2013-01-10 12:59:58 -06:00
|
|
|
// Can't move upvars, so this is never a ZeroMemLastUse.
|
2012-08-28 17:54:45 -05:00
|
|
|
let local_ty = node_id_type(bcx, nid);
|
2013-02-05 21:41:45 -06:00
|
|
|
match bcx.fcx.llupvars.find(&nid) {
|
2012-08-28 17:54:45 -05:00
|
|
|
Some(val) => {
|
|
|
|
Datum {
|
|
|
|
val: val,
|
|
|
|
ty: local_ty,
|
|
|
|
mode: ByRef,
|
2013-01-10 12:59:58 -06:00
|
|
|
source: ZeroMem
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bcx.sess().bug(fmt!(
|
|
|
|
"trans_local_var: no llval for upvar %? found", nid));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-03 11:57:08 -06:00
|
|
|
ast::def_arg(nid, _, _) => {
|
2013-02-21 17:30:24 -06:00
|
|
|
take_local(bcx, *bcx.fcx.llargs, nid)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::def_local(nid, _) | ast::def_binding(nid, _) => {
|
2013-02-21 17:30:24 -06:00
|
|
|
take_local(bcx, *bcx.fcx.lllocals, nid)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-12-10 13:58:37 -06:00
|
|
|
ast::def_self(nid, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let self_info: ValSelfData = match bcx.fcx.llself {
|
|
|
|
Some(ref self_info) => *self_info,
|
|
|
|
None => {
|
|
|
|
bcx.sess().bug(fmt!(
|
|
|
|
"trans_local_var: reference to self \
|
|
|
|
out of context with id %?", nid));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This cast should not be necessary. We should cast self *once*,
|
|
|
|
// but right now this conflicts with default methods.
|
2012-10-08 14:39:30 -05:00
|
|
|
let real_self_ty = monomorphize_type(bcx, self_info.t);
|
|
|
|
let llselfty = T_ptr(type_of::type_of(bcx.ccx(), real_self_ty));
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let casted_val = PointerCast(bcx, self_info.v, llselfty);
|
|
|
|
Datum {
|
|
|
|
val: casted_val,
|
|
|
|
ty: self_info.t,
|
|
|
|
mode: ByRef,
|
2013-01-10 12:59:58 -06:00
|
|
|
source: ZeroMem
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.sess().unimpl(fmt!(
|
|
|
|
"unsupported def type in trans_local_var: %?", def));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fn take_local(bcx: block,
|
2012-09-10 17:38:28 -05:00
|
|
|
table: HashMap<ast::node_id, local_val>,
|
2013-01-10 12:59:58 -06:00
|
|
|
nid: ast::node_id) -> Datum {
|
2013-02-05 21:41:45 -06:00
|
|
|
let (v, mode) = match table.find(&nid) {
|
2012-08-28 17:54:45 -05:00
|
|
|
Some(local_mem(v)) => (v, ByRef),
|
|
|
|
Some(local_imm(v)) => (v, ByValue),
|
|
|
|
None => {
|
|
|
|
bcx.sess().bug(fmt!(
|
|
|
|
"trans_local_var: no llval for local/arg %? found", nid));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let ty = node_id_type(bcx, nid);
|
|
|
|
|
2012-09-19 00:54:57 -05:00
|
|
|
debug!("take_local(nid=%?, v=%s, mode=%?, ty=%s)",
|
|
|
|
nid, bcx.val_str(v), mode, bcx.ty_to_str(ty));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-12-04 17:38:04 -06:00
|
|
|
Datum {
|
|
|
|
val: v,
|
|
|
|
ty: ty,
|
|
|
|
mode: mode,
|
2013-01-10 12:59:58 -06:00
|
|
|
source: ZeroMem
|
2012-12-04 17:38:04 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 17:56:40 -05:00
|
|
|
// The optional node ID here is the node ID of the path identifying the enum
|
|
|
|
// variant in use. If none, this cannot possibly an enum variant (so, if it
|
|
|
|
// is and `node_id_opt` is none, this function fails).
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn with_field_tys<R>(tcx: ty::ctxt,
|
|
|
|
ty: ty::t,
|
|
|
|
node_id_opt: Option<ast::node_id>,
|
2013-03-07 16:38:38 -06:00
|
|
|
op: &fn(int, (&[ty::field])) -> R) -> R {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(ty).sty {
|
2012-12-10 15:47:54 -06:00
|
|
|
ty::ty_struct(did, ref substs) => {
|
2013-02-24 00:53:40 -06:00
|
|
|
op(0, struct_mutable_fields(tcx, did, substs))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2012-10-23 17:56:40 -05:00
|
|
|
ty::ty_enum(_, ref substs) => {
|
|
|
|
// We want the *variant* ID here, not the enum ID.
|
|
|
|
match node_id_opt {
|
|
|
|
None => {
|
|
|
|
tcx.sess.bug(fmt!(
|
|
|
|
"cannot get field types from the enum type %s \
|
|
|
|
without a node ID",
|
|
|
|
ty_to_str(tcx, ty)));
|
|
|
|
}
|
|
|
|
Some(node_id) => {
|
2013-02-05 21:41:45 -06:00
|
|
|
match tcx.def_map.get(&node_id) {
|
2013-02-24 00:53:40 -06:00
|
|
|
ast::def_variant(enum_id, variant_id) => {
|
|
|
|
let variant_info = ty::enum_variant_with_id(
|
|
|
|
tcx, enum_id, variant_id);
|
|
|
|
op(variant_info.disr_val, struct_mutable_fields(
|
2012-10-23 17:56:40 -05:00
|
|
|
tcx, variant_id, substs))
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
tcx.sess.bug(~"resolve didn't map this expr to a \
|
|
|
|
variant ID")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
|
|
|
tcx.sess.bug(fmt!(
|
|
|
|
"cannot get field types from the type %s",
|
|
|
|
ty_to_str(tcx, ty)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_rec_or_struct(bcx: block,
|
|
|
|
fields: &[ast::field],
|
|
|
|
base: Option<@ast::expr>,
|
|
|
|
id: ast::node_id,
|
|
|
|
dest: Dest) -> block
|
|
|
|
{
|
|
|
|
let _icx = bcx.insn_ctxt("trans_rec");
|
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
let ty = node_id_type(bcx, id);
|
|
|
|
let tcx = bcx.tcx();
|
2013-02-24 00:53:40 -06:00
|
|
|
do with_field_tys(tcx, ty, Some(id)) |discr, field_tys| {
|
|
|
|
let mut need_base = vec::from_elem(field_tys.len(), true);
|
|
|
|
|
|
|
|
let numbered_fields = do fields.map |field| {
|
2013-03-02 16:03:41 -06:00
|
|
|
let opt_pos = vec::position(field_tys, |field_ty|
|
|
|
|
field_ty.ident == field.node.ident);
|
|
|
|
match opt_pos {
|
2013-02-24 00:53:40 -06:00
|
|
|
Some(i) => {
|
|
|
|
need_base[i] = false;
|
|
|
|
(i, field.node.expr)
|
2012-10-23 17:56:40 -05:00
|
|
|
}
|
2013-02-24 00:53:40 -06:00
|
|
|
None => {
|
|
|
|
tcx.sess.span_bug(field.span,
|
|
|
|
~"Couldn't find field in struct type")
|
2012-10-23 17:56:40 -05:00
|
|
|
}
|
|
|
|
}
|
2013-02-24 00:53:40 -06:00
|
|
|
};
|
|
|
|
let optbase = match base {
|
|
|
|
Some(base_expr) => {
|
|
|
|
let mut leftovers = ~[];
|
|
|
|
for need_base.eachi |i, b| {
|
|
|
|
if *b {
|
|
|
|
leftovers.push((i, field_tys[i].mt.ty))
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-02-24 00:53:40 -06:00
|
|
|
Some(StructBaseInfo {expr: base_expr,
|
|
|
|
fields: leftovers })
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-02-24 00:53:40 -06:00
|
|
|
None => {
|
|
|
|
if need_base.any(|b| *b) {
|
|
|
|
// XXX should be span bug
|
|
|
|
tcx.sess.bug(~"missing fields and no base expr")
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-02-24 00:53:40 -06:00
|
|
|
let repr = adt::represent_type(bcx.ccx(), ty);
|
2013-02-25 03:49:21 -06:00
|
|
|
trans_adt(bcx, repr, discr, numbered_fields, optbase, dest)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 16:03:41 -06:00
|
|
|
/**
|
|
|
|
* Information that `trans_adt` needs in order to fill in the fields
|
|
|
|
* of a struct copied from a base struct (e.g., from an expression
|
|
|
|
* like `Foo { a: b, ..base }`.
|
|
|
|
*
|
|
|
|
* Note that `fields` may be empty; the base expression must always be
|
|
|
|
* evaluated for side-effects.
|
|
|
|
*/
|
2013-02-24 00:53:40 -06:00
|
|
|
struct StructBaseInfo {
|
2013-03-02 16:03:41 -06:00
|
|
|
/// The base expression; will be evaluated after all explicit fields.
|
2013-02-24 00:53:40 -06:00
|
|
|
expr: @ast::expr,
|
2013-03-02 16:03:41 -06:00
|
|
|
/// The indices of fields to copy paired with their types.
|
2013-02-24 00:53:40 -06:00
|
|
|
fields: ~[(uint, ty::t)]
|
|
|
|
}
|
|
|
|
|
2013-03-02 16:03:41 -06:00
|
|
|
/**
|
|
|
|
* Constructs an ADT instance:
|
|
|
|
*
|
|
|
|
* - `fields` should be a list of field indices paired with the
|
|
|
|
* expression to store into that field. The initializers will be
|
|
|
|
* evaluated in the order specified by `fields`.
|
|
|
|
*
|
|
|
|
* - `optbase` contains information on the base struct (if any) from
|
|
|
|
* which remaining fields are copied; see comments on `StructBaseInfo`.
|
|
|
|
*/
|
2013-02-24 00:53:40 -06:00
|
|
|
fn trans_adt(bcx: block, repr: &adt::Repr, discr: int,
|
|
|
|
fields: &[(uint, @ast::expr)],
|
|
|
|
optbase: Option<StructBaseInfo>,
|
2013-02-23 15:36:01 -06:00
|
|
|
dest: Dest) -> block {
|
2013-02-24 00:53:40 -06:00
|
|
|
let _icx = bcx.insn_ctxt("trans_adt");
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
let addr = match dest {
|
|
|
|
Ignore => {
|
2013-02-24 00:53:40 -06:00
|
|
|
for fields.each |&(_i, e)| {
|
|
|
|
bcx = trans_into(bcx, e, Ignore);
|
|
|
|
}
|
|
|
|
for optbase.each |sbi| {
|
|
|
|
bcx = trans_into(bcx, sbi.expr, Ignore);
|
2012-09-18 23:41:37 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
2013-02-24 00:53:40 -06:00
|
|
|
SaveIn(pos) => pos
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
|
|
|
let mut temp_cleanups = ~[];
|
2013-03-02 18:08:49 -06:00
|
|
|
adt::trans_start_init(bcx, repr, addr, discr);
|
2013-02-24 00:53:40 -06:00
|
|
|
for fields.each |&(i, e)| {
|
2013-03-02 18:08:49 -06:00
|
|
|
let dest = adt::trans_field_ptr(bcx, repr, addr, discr, i);
|
2013-02-24 00:53:40 -06:00
|
|
|
let e_ty = expr_ty(bcx, e);
|
|
|
|
bcx = trans_into(bcx, e, SaveIn(dest));
|
2012-08-28 17:54:45 -05:00
|
|
|
add_clean_temp_mem(bcx, dest, e_ty);
|
2012-09-26 19:33:34 -05:00
|
|
|
temp_cleanups.push(dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-02-24 00:53:40 -06:00
|
|
|
for optbase.each |base| {
|
2013-02-24 14:42:39 -06:00
|
|
|
// XXX is it sound to use the destination's repr on the base?
|
|
|
|
// XXX would it ever be reasonable to be here with discr != 0?
|
2013-02-24 00:53:40 -06:00
|
|
|
let base_datum = unpack_datum!(bcx, trans_to_datum(bcx, base.expr));
|
|
|
|
for base.fields.each |&(i, t)| {
|
2013-02-24 14:42:39 -06:00
|
|
|
let datum = do base_datum.get_element(bcx, t, ZeroMem) |srcval| {
|
2013-03-02 18:08:49 -06:00
|
|
|
adt::trans_field_ptr(bcx, repr, srcval, discr, i)
|
2013-02-24 14:42:39 -06:00
|
|
|
};
|
2013-03-02 18:08:49 -06:00
|
|
|
let dest = adt::trans_field_ptr(bcx, repr, addr, discr, i);
|
2013-02-24 00:53:40 -06:00
|
|
|
bcx = datum.store_to(bcx, base.expr.id, INIT, dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 23:41:37 -05:00
|
|
|
for vec::each(temp_cleanups) |cleanup| {
|
|
|
|
revoke_clean(bcx, *cleanup);
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2013-02-24 00:53:40 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
fn trans_immediate_lit(bcx: block, expr: @ast::expr,
|
|
|
|
lit: ast::lit) -> DatumBlock {
|
|
|
|
// must not be a string constant, that is a RvalueDpsExpr
|
|
|
|
let _icx = bcx.insn_ctxt("trans_immediate_lit");
|
|
|
|
let ty = expr_ty(bcx, expr);
|
|
|
|
immediate_rvalue_bcx(bcx, consts::const_lit(bcx.ccx(), expr, lit), ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_unary_datum(bcx: block,
|
|
|
|
un_expr: @ast::expr,
|
|
|
|
op: ast::unop,
|
|
|
|
sub_expr: @ast::expr) -> DatumBlock {
|
|
|
|
let _icx = bcx.insn_ctxt("trans_unary_datum");
|
|
|
|
|
|
|
|
// if deref, would be LvalueExpr
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(op != ast::deref);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// if overloaded, would be RvalueDpsExpr
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(!bcx.ccx().maps.method_map.contains_key(&un_expr.id));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let un_ty = expr_ty(bcx, un_expr);
|
|
|
|
let sub_ty = expr_ty(bcx, sub_expr);
|
|
|
|
|
|
|
|
return match op {
|
|
|
|
ast::not => {
|
2012-09-11 23:25:01 -05:00
|
|
|
let Result {bcx, val} = trans_to_datum(bcx, sub_expr).to_result();
|
2013-02-06 16:28:02 -06:00
|
|
|
|
|
|
|
// If this is a boolean type, we must not use the LLVM Not
|
|
|
|
// instruction, as that is a *bitwise* not and we want *logical*
|
|
|
|
// not on our 8-bit boolean values.
|
|
|
|
let llresult = match ty::get(un_ty).sty {
|
|
|
|
ty::ty_bool => {
|
|
|
|
let llcond = ICmp(bcx,
|
|
|
|
lib::llvm::IntEQ,
|
|
|
|
val,
|
|
|
|
C_bool(false));
|
|
|
|
Select(bcx, llcond, C_bool(true), C_bool(false))
|
|
|
|
}
|
|
|
|
_ => Not(bcx, val)
|
|
|
|
};
|
|
|
|
immediate_rvalue_bcx(bcx, llresult, un_ty)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::neg => {
|
2012-09-11 23:25:01 -05:00
|
|
|
let Result {bcx, val} = trans_to_datum(bcx, sub_expr).to_result();
|
2012-08-28 17:54:45 -05:00
|
|
|
let llneg = {
|
|
|
|
if ty::type_is_fp(un_ty) {
|
|
|
|
FNeg(bcx, val)
|
|
|
|
} else {
|
|
|
|
Neg(bcx, val)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
immediate_rvalue_bcx(bcx, llneg, un_ty)
|
|
|
|
}
|
|
|
|
ast::box(_) => {
|
2013-02-20 17:02:21 -06:00
|
|
|
trans_boxed_expr(bcx, un_ty, sub_expr, sub_ty,
|
|
|
|
heap_managed)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::uniq(_) => {
|
2013-02-20 17:02:21 -06:00
|
|
|
let heap = heap_for_unique(bcx, un_ty);
|
|
|
|
trans_boxed_expr(bcx, un_ty, sub_expr, sub_ty, heap)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ast::deref => {
|
|
|
|
bcx.sess().bug(~"deref expressions should have been \
|
|
|
|
translated using trans_lvalue(), not \
|
|
|
|
trans_unary_datum()")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fn trans_boxed_expr(bcx: block,
|
|
|
|
box_ty: ty::t,
|
|
|
|
contents: @ast::expr,
|
|
|
|
contents_ty: ty::t,
|
|
|
|
heap: heap) -> DatumBlock {
|
|
|
|
let _icx = bcx.insn_ctxt("trans_boxed_expr");
|
2013-02-19 01:40:42 -06:00
|
|
|
let base::MallocResult { bcx, box: bx, body } =
|
2012-08-28 17:54:45 -05:00
|
|
|
base::malloc_general(bcx, contents_ty, heap);
|
2013-01-30 15:44:24 -06:00
|
|
|
add_clean_free(bcx, bx, heap);
|
2012-08-28 17:54:45 -05:00
|
|
|
let bcx = trans_into(bcx, contents, SaveIn(body));
|
2013-01-30 15:44:24 -06:00
|
|
|
revoke_clean(bcx, bx);
|
|
|
|
return immediate_rvalue_bcx(bcx, bx, box_ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_addr_of(bcx: block, expr: @ast::expr,
|
|
|
|
subexpr: @ast::expr) -> DatumBlock {
|
|
|
|
let _icx = bcx.insn_ctxt("trans_addr_of");
|
|
|
|
let mut bcx = bcx;
|
|
|
|
let sub_datum = unpack_datum!(bcx, trans_to_datum(bcx, subexpr));
|
|
|
|
let llval = sub_datum.to_ref_llval(bcx);
|
|
|
|
return immediate_rvalue_bcx(bcx, llval, expr_ty(bcx, expr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Important to get types for both lhs and rhs, because one might be _|_
|
|
|
|
// and the other not.
|
|
|
|
fn trans_eager_binop(bcx: block,
|
|
|
|
binop_expr: @ast::expr,
|
|
|
|
binop_ty: ty::t,
|
|
|
|
op: ast::binop,
|
|
|
|
lhs_datum: &Datum,
|
2013-02-06 16:28:02 -06:00
|
|
|
rhs_datum: &Datum)
|
|
|
|
-> DatumBlock {
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
let _icx = bcx.insn_ctxt("trans_eager_binop");
|
|
|
|
|
|
|
|
let lhs = lhs_datum.to_appropriate_llval(bcx);
|
|
|
|
let lhs_t = lhs_datum.ty;
|
|
|
|
|
|
|
|
let rhs = rhs_datum.to_appropriate_llval(bcx);
|
|
|
|
let rhs_t = rhs_datum.ty;
|
|
|
|
|
|
|
|
let intype = {
|
|
|
|
if ty::type_is_bot(lhs_t) { rhs_t }
|
|
|
|
else { lhs_t }
|
|
|
|
};
|
|
|
|
let is_float = ty::type_is_fp(intype);
|
|
|
|
|
|
|
|
let rhs = base::cast_shift_expr_rhs(bcx, op, lhs, rhs);
|
|
|
|
|
|
|
|
let mut bcx = bcx;
|
|
|
|
let val = match op {
|
|
|
|
ast::add => {
|
|
|
|
if is_float { FAdd(bcx, lhs, rhs) }
|
|
|
|
else { Add(bcx, lhs, rhs) }
|
|
|
|
}
|
|
|
|
ast::subtract => {
|
|
|
|
if is_float { FSub(bcx, lhs, rhs) }
|
|
|
|
else { Sub(bcx, lhs, rhs) }
|
|
|
|
}
|
|
|
|
ast::mul => {
|
|
|
|
if is_float { FMul(bcx, lhs, rhs) }
|
|
|
|
else { Mul(bcx, lhs, rhs) }
|
|
|
|
}
|
|
|
|
ast::div => {
|
|
|
|
if is_float {
|
|
|
|
FDiv(bcx, lhs, rhs)
|
|
|
|
} else {
|
|
|
|
// Only zero-check integers; fp /0 is NaN
|
|
|
|
bcx = base::fail_if_zero(bcx, binop_expr.span,
|
|
|
|
op, rhs, rhs_t);
|
|
|
|
if ty::type_is_signed(intype) {
|
|
|
|
SDiv(bcx, lhs, rhs)
|
|
|
|
} else {
|
|
|
|
UDiv(bcx, lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::rem => {
|
|
|
|
if is_float {
|
|
|
|
FRem(bcx, lhs, rhs)
|
|
|
|
} else {
|
|
|
|
// Only zero-check integers; fp %0 is NaN
|
|
|
|
bcx = base::fail_if_zero(bcx, binop_expr.span,
|
|
|
|
op, rhs, rhs_t);
|
|
|
|
if ty::type_is_signed(intype) {
|
|
|
|
SRem(bcx, lhs, rhs)
|
|
|
|
} else {
|
|
|
|
URem(bcx, lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::bitor => Or(bcx, lhs, rhs),
|
|
|
|
ast::bitand => And(bcx, lhs, rhs),
|
|
|
|
ast::bitxor => Xor(bcx, lhs, rhs),
|
|
|
|
ast::shl => Shl(bcx, lhs, rhs),
|
|
|
|
ast::shr => {
|
|
|
|
if ty::type_is_signed(intype) {
|
|
|
|
AShr(bcx, lhs, rhs)
|
|
|
|
} else { LShr(bcx, lhs, rhs) }
|
|
|
|
}
|
2012-09-07 20:53:14 -05:00
|
|
|
ast::eq | ast::ne | ast::lt | ast::ge | ast::le | ast::gt => {
|
|
|
|
if ty::type_is_bot(rhs_t) {
|
|
|
|
C_bool(false)
|
|
|
|
} else {
|
|
|
|
if !ty::type_is_scalar(rhs_t) {
|
|
|
|
bcx.tcx().sess.span_bug(binop_expr.span,
|
|
|
|
~"non-scalar comparison");
|
|
|
|
}
|
|
|
|
let cmpr = base::compare_scalar_types(bcx, lhs, rhs, rhs_t, op);
|
|
|
|
bcx = cmpr.bcx;
|
2013-02-06 16:28:02 -06:00
|
|
|
ZExt(bcx, cmpr.val, T_i8())
|
2012-09-07 20:53:14 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
2012-09-07 20:53:14 -05:00
|
|
|
bcx.tcx().sess.span_bug(binop_expr.span, ~"unexpected binop");
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return immediate_rvalue_bcx(bcx, val, binop_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// refinement types would obviate the need for this
|
|
|
|
enum lazy_binop_ty { lazy_and, lazy_or }
|
|
|
|
|
|
|
|
fn trans_lazy_binop(bcx: block,
|
|
|
|
binop_expr: @ast::expr,
|
|
|
|
op: lazy_binop_ty,
|
|
|
|
a: @ast::expr,
|
2013-02-06 16:28:02 -06:00
|
|
|
b: @ast::expr) -> DatumBlock {
|
2012-08-28 17:54:45 -05:00
|
|
|
let _icx = bcx.insn_ctxt("trans_lazy_binop");
|
|
|
|
let binop_ty = expr_ty(bcx, binop_expr);
|
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
let Result {bcx: past_lhs, val: lhs} = {
|
|
|
|
do base::with_scope_result(bcx, a.info(), ~"lhs") |bcx| {
|
2012-09-11 23:25:01 -05:00
|
|
|
trans_to_datum(bcx, a).to_result()
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if past_lhs.unreachable {
|
|
|
|
return immediate_rvalue_bcx(past_lhs, lhs, binop_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
let join = base::sub_block(bcx, ~"join");
|
|
|
|
let before_rhs = base::sub_block(bcx, ~"rhs");
|
|
|
|
|
2013-02-06 16:28:02 -06:00
|
|
|
let lhs_i1 = bool_to_i1(past_lhs, lhs);
|
2012-08-28 17:54:45 -05:00
|
|
|
match op {
|
2013-02-06 16:28:02 -06:00
|
|
|
lazy_and => CondBr(past_lhs, lhs_i1, before_rhs.llbb, join.llbb),
|
|
|
|
lazy_or => CondBr(past_lhs, lhs_i1, join.llbb, before_rhs.llbb)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-02-06 16:28:02 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let Result {bcx: past_rhs, val: rhs} = {
|
|
|
|
do base::with_scope_result(before_rhs, b.info(), ~"rhs") |bcx| {
|
2012-09-11 23:25:01 -05:00
|
|
|
trans_to_datum(bcx, b).to_result()
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if past_rhs.unreachable {
|
|
|
|
return immediate_rvalue_bcx(join, lhs, binop_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Br(past_rhs, join.llbb);
|
|
|
|
let phi = Phi(join, T_bool(), ~[lhs, rhs], ~[past_lhs.llbb,
|
|
|
|
past_rhs.llbb]);
|
|
|
|
|
|
|
|
return immediate_rvalue_bcx(join, phi, binop_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_binary(bcx: block,
|
|
|
|
binop_expr: @ast::expr,
|
|
|
|
op: ast::binop,
|
|
|
|
lhs: @ast::expr,
|
|
|
|
rhs: @ast::expr) -> DatumBlock
|
|
|
|
{
|
|
|
|
let _icx = bcx.insn_ctxt("trans_binary");
|
|
|
|
|
|
|
|
match op {
|
|
|
|
ast::and => {
|
|
|
|
trans_lazy_binop(bcx, binop_expr, lazy_and, lhs, rhs)
|
|
|
|
}
|
|
|
|
ast::or => {
|
|
|
|
trans_lazy_binop(bcx, binop_expr, lazy_or, lhs, rhs)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let mut bcx = bcx;
|
|
|
|
let lhs_datum = unpack_datum!(bcx, trans_to_datum(bcx, lhs));
|
|
|
|
let rhs_datum = unpack_datum!(bcx, trans_to_datum(bcx, rhs));
|
|
|
|
let binop_ty = expr_ty(bcx, binop_expr);
|
|
|
|
trans_eager_binop(bcx, binop_expr, binop_ty, op,
|
|
|
|
&lhs_datum, &rhs_datum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_overloaded_op(bcx: block,
|
|
|
|
expr: @ast::expr,
|
|
|
|
rcvr: @ast::expr,
|
|
|
|
+args: ~[@ast::expr],
|
2012-09-19 20:00:26 -05:00
|
|
|
dest: Dest,
|
|
|
|
+autoref_arg: AutorefArg) -> block
|
2012-08-28 17:54:45 -05:00
|
|
|
{
|
2013-02-05 21:41:45 -06:00
|
|
|
let origin = bcx.ccx().maps.method_map.get(&expr.id);
|
2012-08-28 17:54:45 -05:00
|
|
|
let fty = node_id_type(bcx, expr.callee_id);
|
|
|
|
return callee::trans_call_inner(
|
|
|
|
bcx, expr.info(), fty,
|
|
|
|
expr_ty(bcx, expr),
|
2012-09-11 21:28:14 -05:00
|
|
|
|bcx| meth::trans_method_callee(bcx, expr.callee_id, rcvr, origin),
|
2012-09-19 20:00:26 -05:00
|
|
|
callee::ArgExprs(args), dest, autoref_arg);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn int_cast(bcx: block, lldsttype: TypeRef, llsrctype: TypeRef,
|
|
|
|
llsrc: ValueRef, signed: bool) -> ValueRef {
|
|
|
|
let _icx = bcx.insn_ctxt("int_cast");
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
let srcsz = llvm::LLVMGetIntTypeWidth(llsrctype);
|
|
|
|
let dstsz = llvm::LLVMGetIntTypeWidth(lldsttype);
|
|
|
|
return if dstsz == srcsz {
|
|
|
|
BitCast(bcx, llsrc, lldsttype)
|
|
|
|
} else if srcsz > dstsz {
|
|
|
|
TruncOrBitCast(bcx, llsrc, lldsttype)
|
|
|
|
} else if signed {
|
|
|
|
SExtOrBitCast(bcx, llsrc, lldsttype)
|
|
|
|
} else {
|
|
|
|
ZExtOrBitCast(bcx, llsrc, lldsttype)
|
|
|
|
};
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn float_cast(bcx: block, lldsttype: TypeRef, llsrctype: TypeRef,
|
|
|
|
llsrc: ValueRef) -> ValueRef {
|
|
|
|
let _icx = bcx.insn_ctxt("float_cast");
|
|
|
|
let srcsz = lib::llvm::float_width(llsrctype);
|
|
|
|
let dstsz = lib::llvm::float_width(lldsttype);
|
|
|
|
return if dstsz > srcsz {
|
|
|
|
FPExt(bcx, llsrc, lldsttype)
|
|
|
|
} else if srcsz > dstsz {
|
|
|
|
FPTrunc(bcx, llsrc, lldsttype)
|
|
|
|
} else { llsrc };
|
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub enum cast_kind {
|
2012-08-28 17:54:45 -05:00
|
|
|
cast_pointer,
|
|
|
|
cast_integral,
|
|
|
|
cast_float,
|
|
|
|
cast_enum,
|
|
|
|
cast_other,
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl cmp::Eq for cast_kind {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &cast_kind) -> bool {
|
|
|
|
match ((*self), (*other)) {
|
|
|
|
(cast_pointer, cast_pointer) => true,
|
|
|
|
(cast_integral, cast_integral) => true,
|
|
|
|
(cast_float, cast_float) => true,
|
|
|
|
(cast_enum, cast_enum) => true,
|
|
|
|
(cast_other, cast_other) => true,
|
|
|
|
(cast_pointer, _) => false,
|
|
|
|
(cast_integral, _) => false,
|
|
|
|
(cast_float, _) => false,
|
|
|
|
(cast_enum, _) => false,
|
|
|
|
(cast_other, _) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pure fn ne(&self, other: &cast_kind) -> bool { !(*self).eq(other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn cast_type_kind(t: ty::t) -> cast_kind {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(t).sty {
|
2012-08-28 17:54:45 -05:00
|
|
|
ty::ty_float(*) => cast_float,
|
|
|
|
ty::ty_ptr(*) => cast_pointer,
|
|
|
|
ty::ty_rptr(*) => cast_pointer,
|
|
|
|
ty::ty_int(*) => cast_integral,
|
|
|
|
ty::ty_uint(*) => cast_integral,
|
|
|
|
ty::ty_bool => cast_integral,
|
|
|
|
ty::ty_enum(*) => cast_enum,
|
|
|
|
_ => cast_other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_imm_cast(bcx: block, expr: @ast::expr,
|
|
|
|
id: ast::node_id) -> DatumBlock {
|
|
|
|
let _icx = bcx.insn_ctxt("trans_cast");
|
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
|
|
|
let t_out = node_id_type(bcx, id);
|
|
|
|
|
|
|
|
let mut bcx = bcx;
|
2012-09-11 23:25:01 -05:00
|
|
|
let llexpr = unpack_result!(bcx, trans_to_datum(bcx, expr).to_result());
|
2012-08-28 17:54:45 -05:00
|
|
|
let ll_t_in = val_ty(llexpr);
|
|
|
|
let t_in = expr_ty(bcx, expr);
|
|
|
|
let ll_t_out = type_of::type_of(ccx, t_out);
|
|
|
|
|
|
|
|
let k_in = cast_type_kind(t_in);
|
|
|
|
let k_out = cast_type_kind(t_out);
|
|
|
|
let s_in = k_in == cast_integral && ty::type_is_signed(t_in);
|
|
|
|
|
|
|
|
let newval =
|
2013-02-19 01:40:42 -06:00
|
|
|
match (k_in, k_out) {
|
|
|
|
(cast_integral, cast_integral) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
int_cast(bcx, ll_t_out, ll_t_in, llexpr, s_in)
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_float, cast_float) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
float_cast(bcx, ll_t_out, ll_t_in, llexpr)
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_integral, cast_float) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
if s_in {
|
|
|
|
SIToFP(bcx, llexpr, ll_t_out)
|
|
|
|
} else { UIToFP(bcx, llexpr, ll_t_out) }
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_float, cast_integral) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
if ty::type_is_signed(t_out) {
|
|
|
|
FPToSI(bcx, llexpr, ll_t_out)
|
|
|
|
} else { FPToUI(bcx, llexpr, ll_t_out) }
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_integral, cast_pointer) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
IntToPtr(bcx, llexpr, ll_t_out)
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_pointer, cast_integral) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
PtrToInt(bcx, llexpr, ll_t_out)
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_pointer, cast_pointer) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
PointerCast(bcx, llexpr, ll_t_out)
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_enum, cast_integral) |
|
|
|
|
(cast_enum, cast_float) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let bcx = bcx;
|
2013-02-24 18:24:54 -06:00
|
|
|
let repr = adt::represent_type(ccx, t_in);
|
2013-02-28 14:13:00 -06:00
|
|
|
let lldiscrim_a = adt::trans_get_discr(bcx, repr, llexpr);
|
2012-08-28 17:54:45 -05:00
|
|
|
match k_out {
|
|
|
|
cast_integral => int_cast(bcx, ll_t_out,
|
|
|
|
val_ty(lldiscrim_a),
|
|
|
|
lldiscrim_a, true),
|
|
|
|
cast_float => SIToFP(bcx, lldiscrim_a, ll_t_out),
|
|
|
|
_ => ccx.sess.bug(~"translating unsupported cast.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ccx.sess.bug(~"translating unsupported cast.")
|
|
|
|
};
|
|
|
|
return immediate_rvalue_bcx(bcx, newval, t_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trans_assign_op(bcx: block,
|
|
|
|
expr: @ast::expr,
|
|
|
|
op: ast::binop,
|
|
|
|
dst: @ast::expr,
|
|
|
|
src: @ast::expr) -> block
|
|
|
|
{
|
|
|
|
let _icx = bcx.insn_ctxt("trans_assign_op");
|
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
debug!("trans_assign_op(expr=%s)", bcx.expr_to_str(expr));
|
|
|
|
|
|
|
|
// Evaluate LHS (destination), which should be an lvalue
|
2012-11-27 16:12:33 -06:00
|
|
|
let dst_datum = unpack_datum!(bcx, trans_lvalue_unadjusted(bcx, dst));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// A user-defined operator method
|
2013-02-05 21:41:45 -06:00
|
|
|
if bcx.ccx().maps.method_map.find(&expr.id).is_some() {
|
2012-10-11 18:42:40 -05:00
|
|
|
// FIXME(#2528) evaluates the receiver twice!!
|
2012-08-28 17:54:45 -05:00
|
|
|
let scratch = scratch_datum(bcx, dst_datum.ty, false);
|
|
|
|
let bcx = trans_overloaded_op(bcx, expr, dst, ~[src],
|
2012-09-19 20:00:26 -05:00
|
|
|
SaveIn(scratch.val), DoAutorefArg);
|
2012-08-28 17:54:45 -05:00
|
|
|
return scratch.move_to_datum(bcx, DROP_EXISTING, dst_datum);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate RHS (source)
|
|
|
|
let src_datum = unpack_datum!(bcx, trans_to_datum(bcx, src));
|
|
|
|
|
|
|
|
// Perform computation and store the result
|
|
|
|
let result_datum =
|
|
|
|
unpack_datum!(bcx,
|
|
|
|
trans_eager_binop(
|
|
|
|
bcx, expr, dst_datum.ty, op,
|
|
|
|
&dst_datum, &src_datum));
|
2013-01-10 12:59:58 -06:00
|
|
|
return result_datum.copy_to_datum(bcx, DROP_EXISTING, dst_datum);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn shorten(+x: ~str) -> ~str {
|
|
|
|
if x.len() > 60 { x.substr(0, 60) } else { x }
|
2012-09-07 14:06:02 -05:00
|
|
|
}
|