2021-11-13 00:16:54 -06:00
|
|
|
//! Intermediate representation for Wasm.
|
|
|
|
|
2021-11-13 02:52:35 -06:00
|
|
|
use wasmparser::{FuncType, Operator, Type};
|
2021-11-13 00:16:54 -06:00
|
|
|
|
|
|
|
pub type SignatureId = usize;
|
|
|
|
pub type FuncId = usize;
|
2021-11-13 02:20:02 -06:00
|
|
|
pub type BlockId = usize;
|
|
|
|
pub type InstId = usize;
|
2021-11-13 02:52:35 -06:00
|
|
|
pub type ValueId = usize;
|
2021-11-13 00:16:54 -06:00
|
|
|
|
2021-11-13 02:52:35 -06:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2021-11-13 04:32:05 -06:00
|
|
|
pub struct Module<'a> {
|
|
|
|
pub funcs: Vec<FuncDecl<'a>>,
|
2021-11-13 00:16:54 -06:00
|
|
|
pub signatures: Vec<FuncType>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2021-11-13 04:32:05 -06:00
|
|
|
pub enum FuncDecl<'a> {
|
2021-11-13 00:16:54 -06:00
|
|
|
Import(SignatureId),
|
2021-11-13 04:32:05 -06:00
|
|
|
Body(SignatureId, FunctionBody<'a>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FuncDecl<'a> {
|
|
|
|
pub fn sig(&self) -> SignatureId {
|
|
|
|
match self {
|
|
|
|
&FuncDecl::Import(sig) => sig,
|
|
|
|
&FuncDecl::Body(sig, ..) => sig,
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 00:16:54 -06:00
|
|
|
}
|
|
|
|
|
2021-11-13 02:52:35 -06:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2021-11-13 04:32:05 -06:00
|
|
|
pub struct FunctionBody<'a> {
|
2021-11-13 00:16:54 -06:00
|
|
|
pub locals: Vec<Type>,
|
2021-11-13 04:32:05 -06:00
|
|
|
pub blocks: Vec<Block<'a>>,
|
2021-11-13 02:52:35 -06:00
|
|
|
pub values: Vec<ValueDef>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ValueDef {
|
|
|
|
pub kind: ValueKind,
|
|
|
|
pub ty: Type,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum ValueKind {
|
2021-11-13 04:32:05 -06:00
|
|
|
BlockParam(BlockId, usize),
|
2021-11-13 03:41:32 -06:00
|
|
|
Inst(BlockId, InstId),
|
2021-11-13 02:52:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
2021-11-13 04:32:05 -06:00
|
|
|
pub struct Block<'a> {
|
2021-11-13 02:52:35 -06:00
|
|
|
pub params: Vec<Type>,
|
2021-11-13 04:32:05 -06:00
|
|
|
pub insts: Vec<Inst<'a>>,
|
2021-11-13 05:38:47 -06:00
|
|
|
pub terminator: Terminator<'a>,
|
2021-11-13 02:52:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2021-11-13 04:32:05 -06:00
|
|
|
pub struct Inst<'a> {
|
|
|
|
pub operator: Operator<'a>,
|
2021-11-13 02:52:35 -06:00
|
|
|
pub outputs: Vec<ValueId>,
|
2021-11-13 04:32:05 -06:00
|
|
|
pub inputs: Vec<Operand<'a>>,
|
2021-11-13 02:52:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2021-11-13 04:32:05 -06:00
|
|
|
pub enum Operand<'a> {
|
2021-11-13 02:52:35 -06:00
|
|
|
Value(ValueId),
|
2021-11-13 04:32:05 -06:00
|
|
|
Sub(Box<Inst<'a>>),
|
2021-11-13 00:16:54 -06:00
|
|
|
}
|
2021-11-13 05:38:47 -06:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum Terminator<'a> {
|
|
|
|
Br {
|
|
|
|
target: BlockId,
|
|
|
|
},
|
|
|
|
CondBr {
|
|
|
|
cond: Operand<'a>,
|
|
|
|
if_true: BlockId,
|
|
|
|
if_false: BlockId,
|
|
|
|
},
|
|
|
|
Select {
|
|
|
|
value: Operand<'a>,
|
|
|
|
targets: Vec<BlockId>,
|
|
|
|
default: BlockId,
|
|
|
|
},
|
|
|
|
Return {
|
|
|
|
values: Vec<Operand<'a>>,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> std::default::Default for Terminator<'a> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Terminator::None
|
|
|
|
}
|
|
|
|
}
|