Skip to content
Permalink
Browse files

add MIR code (unused thus far)

  • Loading branch information...
nikomatsakis committed Aug 18, 2015
1 parent 0e764ec commit 9bd35c07c2607b93bd889650f42aab4f9e4d5e58
Showing with 5,856 additions and 12 deletions.
  1. +3 −0 src/librustc/middle/region.rs
  2. +12 −8 src/librustc/middle/ty.rs
  3. +28 −0 src/librustc_mir/build/block.rs
  4. +88 −0 src/librustc_mir/build/cfg.rs
  5. +123 −0 src/librustc_mir/build/expr/as_constant.rs
  6. +131 −0 src/librustc_mir/build/expr/as_lvalue.rs
  7. +68 −0 src/librustc_mir/build/expr/as_operand.rs
  8. +220 −0 src/librustc_mir/build/expr/as_rvalue.rs
  9. +85 −0 src/librustc_mir/build/expr/as_temp.rs
  10. +93 −0 src/librustc_mir/build/expr/category.rs
  11. +282 −0 src/librustc_mir/build/expr/into.rs
  12. +79 −0 src/librustc_mir/build/expr/mod.rs
  13. +70 −0 src/librustc_mir/build/into.rs
  14. +409 −0 src/librustc_mir/build/matches/mod.rs
  15. +127 −0 src/librustc_mir/build/matches/simplify.rs
  16. +301 −0 src/librustc_mir/build/matches/test.rs
  17. +82 −0 src/librustc_mir/build/matches/util.rs
  18. +78 −0 src/librustc_mir/build/misc.rs
  19. +174 −0 src/librustc_mir/build/mod.rs
  20. +304 −0 src/librustc_mir/build/scope.rs
  21. +61 −0 src/librustc_mir/build/stmt.rs
  22. +225 −0 src/librustc_mir/dump.rs
  23. +157 −0 src/librustc_mir/graphviz/mod.rs
  24. +382 −0 src/librustc_mir/hair.rs
  25. +34 −0 src/librustc_mir/lib.rs
  26. +676 −0 src/librustc_mir/repr.rs
  27. +114 −0 src/librustc_mir/tcx/block.rs
  28. +870 −0 src/librustc_mir/tcx/expr.rs
  29. +184 −0 src/librustc_mir/tcx/mod.rs
  30. +291 −0 src/librustc_mir/tcx/pattern.rs
  31. +94 −0 src/librustc_mir/tcx/to_ref.rs
  32. +10 −3 src/libsyntax/feature_gate.rs
  33. +1 −1 src/test/compile-fail/feature-gate-rustc-attrs.rs
@@ -329,6 +329,9 @@ impl RegionMaps {
pub fn item_extent(&self, n: ast::NodeId) -> CodeExtent {
self.lookup_code_extent(CodeExtentData::DestructionScope(n))
}
pub fn opt_destruction_extent(&self, n: ast::NodeId) -> Option<CodeExtent> {
self.code_extent_interner.borrow().get(&CodeExtentData::DestructionScope(n)).cloned()
}
pub fn intern_code_extent(&self,
e: CodeExtentData,
parent: CodeExtent) -> CodeExtent {
@@ -3475,6 +3475,13 @@ impl<'tcx, 'container> AdtDefData<'tcx, 'container> {
.expect("variant_with_id: unknown variant")
}

pub fn variant_index_with_id(&self, vid: DefId) -> usize {
self.variants
.iter()
.position(|v| v.did == vid)
.expect("variant_index_with_id: unknown variant")
}

pub fn variant_of_def(&self, def: def::Def) -> &VariantDefData<'tcx, 'container> {
match def {
def::DefVariant(_, vid, _) => self.variant_with_id(vid),
@@ -5223,14 +5230,11 @@ impl<'tcx> TyS<'tcx> {
{
let method_call = MethodCall::autoderef(expr_id, autoderef);
let mut adjusted_ty = self;
match method_type(method_call) {
Some(method_ty) => {
// Method calls always have all late-bound regions
// fully instantiated.
let fn_ret = cx.no_late_bound_regions(&method_ty.fn_ret()).unwrap();
adjusted_ty = fn_ret.unwrap();
}
None => {}
if let Some(method_ty) = method_type(method_call) {
// Method calls always have all late-bound regions
// fully instantiated.
let fn_ret = cx.no_late_bound_regions(&method_ty.fn_ret()).unwrap();
adjusted_ty = fn_ret.unwrap();
}
match adjusted_ty.builtin_deref(true, NoPreference) {
Some(mt) => mt.ty,
@@ -0,0 +1,28 @@
// 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.

use hair::*;
use repr::*;
use build::{BlockAnd, Builder};

impl<H:Hair> Builder<H> {
pub fn ast_block(&mut self,
destination: &Lvalue<H>,
mut block: BasicBlock,
ast_block: H::Block)
-> BlockAnd<()> {
let this = self;
let Block { extent, span: _, stmts, expr } = this.hir.mirror(ast_block);
this.in_scope(extent, block, |this| {
unpack!(block = this.stmts(block, stmts));
this.into(destination, block, expr)
})
}
}
@@ -0,0 +1,88 @@
// 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.




//! Routines for manipulating the control-flow graph.

use build::CFG;
use hair::*;
use repr::*;

impl<H:Hair> CFG<H> {
pub fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<H> {
&self.basic_blocks[blk.index()]
}

pub fn block_data_mut(&mut self, blk: BasicBlock) -> &mut BasicBlockData<H> {
&mut self.basic_blocks[blk.index()]
}

pub fn end_point(&self, block: BasicBlock) -> ExecutionPoint {
ExecutionPoint {
block: block,
statement: self.block_data(block).statements.len() as u32
}
}

pub fn start_new_block(&mut self) -> BasicBlock {
let node_index = self.basic_blocks.len();
self.basic_blocks.push(BasicBlockData::new(Terminator::Diverge));
BasicBlock::new(node_index)
}

pub fn push(&mut self, block: BasicBlock, statement: Statement<H>) {
debug!("push({:?}, {:?})", block, statement);
self.block_data_mut(block).statements.push(statement);
}

pub fn push_assign_constant(&mut self,
block: BasicBlock,
span: H::Span,
temp: &Lvalue<H>,
constant: Constant<H>) {
self.push_assign(block, span, temp, Rvalue::Use(Operand::Constant(constant)));
}

pub fn push_drop(&mut self, block: BasicBlock, span: H::Span,
kind: DropKind, lvalue: &Lvalue<H>) {
self.push(block, Statement {
span: span,
kind: StatementKind::Drop(kind, lvalue.clone())
});
}

pub fn push_assign(&mut self,
block: BasicBlock,
span: H::Span,
lvalue: &Lvalue<H>,
rvalue: Rvalue<H>) {
self.push(block, Statement {
span: span,
kind: StatementKind::Assign(lvalue.clone(), rvalue)
});
}

pub fn terminate(&mut self,
block: BasicBlock,
terminator: Terminator<H>) {
// Check whether this block has already been terminated. For
// this, we rely on the fact that the initial state is to have
// a Diverge terminator and an empty list of targets (which
// is not a valid state).
debug_assert!(match self.block_data(block).terminator { Terminator::Diverge => true,
_ => false },
"terminate: block {:?} already has a terminator set", block);

self.block_data_mut(block).terminator = terminator;
}
}

@@ -0,0 +1,123 @@
// 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

use rustc_data_structures::fnv::FnvHashMap;

use build::{Builder};
use hair::*;
use repr::*;

impl<H:Hair> Builder<H> {
/// Compile `expr`, yielding a compile-time constant. Assumes that
/// `expr` is a valid compile-time constant!
pub fn as_constant<M>(&mut self, expr: M) -> Constant<H>
where M: Mirror<H, Output=Expr<H>>
{
let expr = self.hir.mirror(expr);
self.expr_as_constant(expr)
}

fn expr_as_constant(&mut self, expr: Expr<H>) -> Constant<H> {
let this = self;
let Expr { ty: _, temp_lifetime: _, span, kind } = expr;
let kind = match kind {
ExprKind::Scope { extent: _, value } => {
return this.as_constant(value);
}
ExprKind::Paren { arg } => {
return this.as_constant(arg);
}
ExprKind::Literal { literal } => {
ConstantKind::Literal(literal)
}
ExprKind::Vec { fields } => {
let fields = this.as_constants(fields);
ConstantKind::Aggregate(AggregateKind::Vec, fields)
}
ExprKind::Tuple { fields } => {
let fields = this.as_constants(fields);
ConstantKind::Aggregate(AggregateKind::Tuple, fields)
}
ExprKind::Adt { adt_def, variant_index, substs, fields, base: None } => {
let field_names = this.hir.fields(adt_def, variant_index);
let fields = this.named_field_constants(field_names, fields);
ConstantKind::Aggregate(AggregateKind::Adt(adt_def, variant_index, substs), fields)
}
ExprKind::Repeat { value, count } => {
let value = Box::new(this.as_constant(value));
let count = Box::new(this.as_constant(count));
ConstantKind::Repeat(value, count)
}
ExprKind::Binary { op, lhs, rhs } => {
let lhs = Box::new(this.as_constant(lhs));
let rhs = Box::new(this.as_constant(rhs));
ConstantKind::BinaryOp(op, lhs, rhs)
}
ExprKind::Unary { op, arg } => {
let arg = Box::new(this.as_constant(arg));
ConstantKind::UnaryOp(op, arg)
}
ExprKind::Field { lhs, name } => {
let lhs = this.as_constant(lhs);
ConstantKind::Projection(
Box::new(ConstantProjection {
base: lhs,
elem: ProjectionElem::Field(name),
}))
}
ExprKind::Deref { arg } => {
let arg = this.as_constant(arg);
ConstantKind::Projection(
Box::new(ConstantProjection {
base: arg,
elem: ProjectionElem::Deref,
}))
}
ExprKind::Call { fun, args } => {
let fun = this.as_constant(fun);
let args = this.as_constants(args);
ConstantKind::Call(Box::new(fun), args)
}
_ => {
this.hir.span_bug(
span,
&format!("expression is not a valid constant {:?}", kind));
}
};
Constant { span: span, kind: kind }
}

fn as_constants(&mut self,
exprs: Vec<ExprRef<H>>)
-> Vec<Constant<H>>
{
exprs.into_iter().map(|expr| self.as_constant(expr)).collect()
}

fn named_field_constants(&mut self,
field_names: Vec<Field<H>>,
field_exprs: Vec<FieldExprRef<H>>)
-> Vec<Constant<H>>
{
let fields_map: FnvHashMap<_, _> =
field_exprs.into_iter()
.map(|f| (f.name, self.as_constant(f.expr)))
.collect();

let fields: Vec<_> =
field_names.into_iter()
.map(|n| fields_map[&n].clone())
.collect();

fields
}
}

0 comments on commit 9bd35c0

Please sign in to comment.
You can’t perform that action at this time.