waffle/src/ir.rs

158 lines
3.4 KiB
Rust
Raw Normal View History

2021-11-13 06:16:54 +00:00
//! Intermediate representation for Wasm.
2021-11-14 06:25:27 +00:00
use crate::frontend;
2021-11-14 01:52:30 +00:00
use anyhow::Result;
use wasmparser::{FuncType, Operator, Type};
2021-11-13 06:16:54 +00:00
pub type SignatureId = usize;
pub type FuncId = usize;
2021-11-13 08:20:02 +00:00
pub type BlockId = usize;
pub type InstId = usize;
pub type ValueId = usize;
pub type LocalId = u32;
2021-11-13 06:16:54 +00:00
2021-11-13 22:23:22 +00:00
pub const NO_VALUE: ValueId = usize::MAX;
#[derive(Clone, Debug, Default)]
2021-11-13 10:32:05 +00:00
pub struct Module<'a> {
pub funcs: Vec<FuncDecl<'a>>,
2021-11-13 06:16:54 +00:00
pub signatures: Vec<FuncType>,
pub globals: Vec<Type>,
pub tables: Vec<Type>,
2021-11-13 06:16:54 +00:00
}
#[derive(Clone, Debug)]
2021-11-13 10:32:05 +00:00
pub enum FuncDecl<'a> {
2021-11-13 06:16:54 +00:00
Import(SignatureId),
2021-11-13 10:32:05 +00:00
Body(SignatureId, FunctionBody<'a>),
}
impl<'a> FuncDecl<'a> {
pub fn sig(&self) -> SignatureId {
match self {
2021-11-13 11:49:19 +00:00
FuncDecl::Import(sig) => *sig,
FuncDecl::Body(sig, ..) => *sig,
2021-11-13 10:32:05 +00:00
}
}
2021-11-13 06:16:54 +00:00
}
#[derive(Clone, Debug, Default)]
2021-11-13 10:32:05 +00:00
pub struct FunctionBody<'a> {
2021-11-13 06:16:54 +00:00
pub locals: Vec<Type>,
2021-11-13 10:32:05 +00:00
pub blocks: Vec<Block<'a>>,
pub values: Vec<ValueDef>,
}
#[derive(Clone, Debug)]
pub struct ValueDef {
pub kind: ValueKind,
pub ty: Type,
pub local: Option<LocalId>,
}
#[derive(Clone, Debug)]
pub enum ValueKind {
2021-11-14 06:25:27 +00:00
Arg(usize),
2021-11-13 10:32:05 +00:00
BlockParam(BlockId, usize),
2021-11-14 06:25:27 +00:00
Inst(BlockId, InstId, usize),
}
#[derive(Clone, Debug, Default)]
2021-11-13 10:32:05 +00:00
pub struct Block<'a> {
pub params: Vec<Type>,
2021-11-13 10:32:05 +00:00
pub insts: Vec<Inst<'a>>,
2021-11-13 11:38:47 +00:00
pub terminator: Terminator<'a>,
}
#[derive(Clone, Debug)]
2021-11-13 10:32:05 +00:00
pub struct Inst<'a> {
pub operator: Operator<'a>,
pub outputs: Vec<ValueId>,
2021-11-13 10:32:05 +00:00
pub inputs: Vec<Operand<'a>>,
}
#[derive(Clone, Debug)]
2021-11-13 10:32:05 +00:00
pub enum Operand<'a> {
2021-11-13 22:23:22 +00:00
/// An SSA value.
Value(ValueId),
2021-11-13 22:23:22 +00:00
/// Tree-ified instructions for Wasm emission.
2021-11-13 10:32:05 +00:00
Sub(Box<Inst<'a>>),
2021-11-13 22:23:22 +00:00
/// Undef values are produced when code is unreachable and thus
/// removed/never executed.
Undef,
}
impl<'a> Operand<'a> {
pub fn value(value: ValueId) -> Self {
if value == NO_VALUE {
Operand::Undef
} else {
Operand::Value(value)
}
}
2021-11-13 06:16:54 +00:00
}
2021-11-13 11:38:47 +00:00
2021-11-13 21:49:57 +00:00
#[derive(Clone, Debug)]
pub struct BlockTarget<'a> {
pub block: BlockId,
pub args: Vec<Operand<'a>>,
}
2021-11-13 11:38:47 +00:00
#[derive(Clone, Debug)]
pub enum Terminator<'a> {
Br {
2021-11-13 21:49:57 +00:00
target: BlockTarget<'a>,
2021-11-13 11:38:47 +00:00
},
CondBr {
cond: Operand<'a>,
2021-11-13 21:49:57 +00:00
if_true: BlockTarget<'a>,
if_false: BlockTarget<'a>,
2021-11-13 11:38:47 +00:00
},
Select {
value: Operand<'a>,
2021-11-13 21:49:57 +00:00
targets: Vec<BlockTarget<'a>>,
default: BlockTarget<'a>,
2021-11-13 11:38:47 +00:00
},
Return {
values: Vec<Operand<'a>>,
},
None,
}
impl<'a> std::default::Default for Terminator<'a> {
fn default() -> Self {
Terminator::None
}
}
2021-11-14 01:52:30 +00:00
impl<'a> Module<'a> {
pub fn from_wasm_bytes(bytes: &'a [u8]) -> Result<Self> {
2021-11-14 06:25:27 +00:00
frontend::wasm_to_ir(bytes)
2021-11-14 04:59:37 +00:00
}
}
impl<'a> Terminator<'a> {
pub fn successors(&self) -> Vec<BlockId> {
match self {
Terminator::Return { .. } => vec![],
Terminator::Br { target, .. } => vec![target.block],
Terminator::CondBr {
if_true, if_false, ..
} => vec![if_true.block, if_false.block],
Terminator::Select {
ref targets,
default,
..
} => {
let mut ret = targets
.iter()
.map(|target| target.block)
.collect::<Vec<_>>();
ret.push(default.block);
ret
}
Terminator::None => vec![],
}
2021-11-14 01:52:30 +00:00
}
}