2019-03-20 16:19:59 +13:00
|
|
|
use crate::context::Context;
|
|
|
|
use crate::env::Env;
|
2019-03-17 18:52:21 +13:00
|
|
|
use crate::ir::IR;
|
2019-03-18 20:20:39 +13:00
|
|
|
use crate::stable::StringIdx;
|
|
|
|
use crate::ty::TyIdx;
|
2019-03-18 18:54:21 +13:00
|
|
|
use std::slice::IterMut;
|
2019-03-17 18:52:21 +13:00
|
|
|
|
2019-03-20 16:19:59 +13:00
|
|
|
/// A basic block.
|
|
|
|
///
|
|
|
|
/// A basic block is a collection of
|
2019-03-18 18:54:21 +13:00
|
|
|
#[derive(Debug, Default, PartialEq, Clone)]
|
2019-03-17 18:52:21 +13:00
|
|
|
pub struct Block {
|
2019-03-20 16:19:59 +13:00
|
|
|
env: Env,
|
2019-03-17 18:52:21 +13:00
|
|
|
ir: Vec<IR>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Block {
|
2019-03-20 16:19:59 +13:00
|
|
|
/// Perform IR-wise improvement.
|
|
|
|
///
|
|
|
|
/// See `IR.improve` for more information.
|
|
|
|
pub fn improve_ir<F: Fn(&mut IR, &mut Context)>(
|
|
|
|
&mut self,
|
|
|
|
mut context: &mut Context,
|
|
|
|
improver: F,
|
|
|
|
) {
|
|
|
|
for ir in self.ir.iter_mut() {
|
|
|
|
improver(ir, &mut context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 18:54:21 +13:00
|
|
|
/// Consumes the block and returns the IR data.
|
|
|
|
pub fn ir(self) -> Vec<IR> {
|
|
|
|
self.ir
|
2019-03-17 18:52:21 +13:00
|
|
|
}
|
|
|
|
|
2019-03-23 09:36:45 +13:00
|
|
|
pub fn ir_ref(&self) -> Vec<&IR> {
|
|
|
|
self.ir.iter().collect()
|
|
|
|
}
|
|
|
|
|
2019-03-17 18:52:21 +13:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.ir.is_empty()
|
|
|
|
}
|
|
|
|
|
2019-03-18 18:54:21 +13:00
|
|
|
pub fn iter_mut(&mut self) -> IterMut<IR> {
|
|
|
|
self.ir.iter_mut()
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:19:59 +13:00
|
|
|
pub fn new(env: Env) -> Block {
|
2019-03-18 20:20:39 +13:00
|
|
|
Block {
|
2019-03-20 16:19:59 +13:00
|
|
|
env,
|
2019-03-18 20:20:39 +13:00
|
|
|
ir: Vec::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 18:54:21 +13:00
|
|
|
pub fn pop(&mut self) -> Option<IR> {
|
|
|
|
self.ir.pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push(&mut self, ir: IR) {
|
|
|
|
self.ir.push(ir);
|
2019-03-17 18:52:21 +13:00
|
|
|
}
|
2019-03-18 20:20:39 +13:00
|
|
|
|
2019-03-20 16:19:59 +13:00
|
|
|
pub fn env(&self) -> &Env {
|
|
|
|
&self.env
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn env_get(&self, name: StringIdx) -> Option<&TyIdx> {
|
|
|
|
self.env.get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn env_set(&mut self, name: StringIdx, ty: TyIdx) {
|
|
|
|
self.env.set(name, ty)
|
2019-03-18 20:20:39 +13:00
|
|
|
}
|
2019-03-17 18:52:21 +13:00
|
|
|
}
|
2019-03-18 11:56:44 +13:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct BlockIdx(usize);
|
|
|
|
|
|
|
|
impl From<usize> for BlockIdx {
|
|
|
|
fn from(i: usize) -> BlockIdx {
|
|
|
|
BlockIdx(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BlockIdx> for usize {
|
|
|
|
fn from(i: BlockIdx) -> usize {
|
|
|
|
i.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&BlockIdx> for usize {
|
|
|
|
fn from(i: &BlockIdx) -> usize {
|
|
|
|
i.0
|
|
|
|
}
|
|
|
|
}
|