2021-11-13 00:16:54 -06:00
|
|
|
//! Intermediate representation for Wasm.
|
|
|
|
|
2021-11-20 03:29:46 -06:00
|
|
|
use crate::{backend::Shape, cfg::CFGInfo, frontend};
|
2021-11-13 19:52:30 -06:00
|
|
|
use anyhow::Result;
|
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-14 01:02:47 -06:00
|
|
|
pub type LocalId = u32;
|
2021-11-13 00:16:54 -06:00
|
|
|
|
2021-11-13 16:23:22 -06:00
|
|
|
pub const NO_VALUE: ValueId = usize::MAX;
|
2021-11-15 01:56:56 -06:00
|
|
|
pub const INVALID_BLOCK: BlockId = usize::MAX;
|
2021-11-13 16:23:22 -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> {
|
2021-11-14 02:00:34 -06:00
|
|
|
pub orig_bytes: &'a [u8],
|
2021-11-13 04:32:05 -06:00
|
|
|
pub funcs: Vec<FuncDecl<'a>>,
|
2021-11-13 00:16:54 -06:00
|
|
|
pub signatures: Vec<FuncType>,
|
2021-11-13 18:31:11 -06:00
|
|
|
pub globals: Vec<Type>,
|
|
|
|
pub tables: Vec<Type>,
|
2021-11-13 00:16:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
2021-11-13 05:49:19 -06:00
|
|
|
FuncDecl::Import(sig) => *sig,
|
|
|
|
FuncDecl::Body(sig, ..) => *sig,
|
2021-11-13 04:32:05 -06:00
|
|
|
}
|
|
|
|
}
|
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-14 00:25:27 -06:00
|
|
|
Arg(usize),
|
2021-11-13 04:32:05 -06:00
|
|
|
BlockParam(BlockId, usize),
|
2021-11-14 00:25:27 -06:00
|
|
|
Inst(BlockId, InstId, usize),
|
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-14 02:00:34 -06:00
|
|
|
pub terminator: Terminator,
|
2021-11-13 02:52:35 -06:00
|
|
|
}
|
|
|
|
|
2021-11-15 01:56:56 -06:00
|
|
|
impl<'a> Block<'a> {
|
|
|
|
pub fn successors(&self) -> Vec<BlockId> {
|
|
|
|
self.terminator.successors()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn values<'b>(&'b self) -> impl Iterator<Item = ValueId> + 'b {
|
|
|
|
self.insts
|
|
|
|
.iter()
|
|
|
|
.map(|inst| inst.outputs.iter().cloned())
|
|
|
|
.flatten()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn visit_operands<F: Fn(&Operand)>(&self, f: F) {
|
|
|
|
for inst in &self.insts {
|
|
|
|
for input in &inst.inputs {
|
|
|
|
f(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match &self.terminator {
|
|
|
|
&Terminator::CondBr { ref cond, .. } => f(cond),
|
|
|
|
&Terminator::Select { ref value, .. } => f(value),
|
|
|
|
&Terminator::Return { ref values, .. } => {
|
|
|
|
for value in values {
|
|
|
|
f(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_operands<F: Fn(&mut Operand)>(&mut self, f: F) {
|
|
|
|
for inst in &mut self.insts {
|
|
|
|
for input in &mut inst.inputs {
|
|
|
|
f(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match &mut self.terminator {
|
|
|
|
&mut Terminator::CondBr { ref mut cond, .. } => f(cond),
|
|
|
|
&mut Terminator::Select { ref mut value, .. } => f(value),
|
|
|
|
&mut Terminator::Return { ref mut values, .. } => {
|
|
|
|
for value in values {
|
|
|
|
f(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-14 02:00:34 -06:00
|
|
|
pub inputs: Vec<Operand>,
|
2021-11-13 02:52:35 -06:00
|
|
|
}
|
|
|
|
|
2021-11-14 02:00:34 -06:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum Operand {
|
2021-11-13 16:23:22 -06:00
|
|
|
/// An SSA value.
|
2021-11-13 02:52:35 -06:00
|
|
|
Value(ValueId),
|
2021-11-13 16:23:22 -06:00
|
|
|
/// Undef values are produced when code is unreachable and thus
|
|
|
|
/// removed/never executed.
|
|
|
|
Undef,
|
|
|
|
}
|
|
|
|
|
2021-11-14 02:00:34 -06:00
|
|
|
impl Operand {
|
2021-11-13 16:23:22 -06:00
|
|
|
pub fn value(value: ValueId) -> Self {
|
|
|
|
if value == NO_VALUE {
|
|
|
|
Operand::Undef
|
|
|
|
} else {
|
|
|
|
Operand::Value(value)
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 00:16:54 -06:00
|
|
|
}
|
2021-11-13 05:38:47 -06:00
|
|
|
|
2021-11-13 15:49:57 -06:00
|
|
|
#[derive(Clone, Debug)]
|
2021-11-14 02:00:34 -06:00
|
|
|
pub struct BlockTarget {
|
2021-11-13 15:49:57 -06:00
|
|
|
pub block: BlockId,
|
2021-11-14 02:00:34 -06:00
|
|
|
pub args: Vec<Operand>,
|
2021-11-13 15:49:57 -06:00
|
|
|
}
|
|
|
|
|
2021-11-13 05:38:47 -06:00
|
|
|
#[derive(Clone, Debug)]
|
2021-11-14 02:00:34 -06:00
|
|
|
pub enum Terminator {
|
2021-11-13 05:38:47 -06:00
|
|
|
Br {
|
2021-11-14 02:00:34 -06:00
|
|
|
target: BlockTarget,
|
2021-11-13 05:38:47 -06:00
|
|
|
},
|
|
|
|
CondBr {
|
2021-11-14 02:00:34 -06:00
|
|
|
cond: Operand,
|
|
|
|
if_true: BlockTarget,
|
|
|
|
if_false: BlockTarget,
|
2021-11-13 05:38:47 -06:00
|
|
|
},
|
|
|
|
Select {
|
2021-11-14 02:00:34 -06:00
|
|
|
value: Operand,
|
|
|
|
targets: Vec<BlockTarget>,
|
|
|
|
default: BlockTarget,
|
2021-11-13 05:38:47 -06:00
|
|
|
},
|
|
|
|
Return {
|
2021-11-14 02:00:34 -06:00
|
|
|
values: Vec<Operand>,
|
2021-11-13 05:38:47 -06:00
|
|
|
},
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2021-11-14 02:00:34 -06:00
|
|
|
impl std::default::Default for Terminator {
|
2021-11-13 05:38:47 -06:00
|
|
|
fn default() -> Self {
|
|
|
|
Terminator::None
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 19:52:30 -06:00
|
|
|
|
2021-11-14 02:00:34 -06:00
|
|
|
impl Terminator {
|
|
|
|
pub fn args(&self) -> Vec<Operand> {
|
|
|
|
match self {
|
|
|
|
Terminator::Br { target } => target.args.clone(),
|
|
|
|
Terminator::CondBr {
|
|
|
|
cond,
|
|
|
|
if_true,
|
|
|
|
if_false,
|
|
|
|
} => {
|
|
|
|
let mut ret = vec![*cond];
|
|
|
|
ret.extend(if_true.args.iter().cloned());
|
|
|
|
ret.extend(if_false.args.iter().cloned());
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
Terminator::Select {
|
|
|
|
value,
|
|
|
|
targets,
|
|
|
|
default,
|
|
|
|
} => {
|
|
|
|
let mut ret = vec![*value];
|
|
|
|
for target in targets {
|
|
|
|
ret.extend(target.args.iter().cloned());
|
|
|
|
}
|
|
|
|
ret.extend(default.args.clone());
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
Terminator::Return { values } => values.clone(),
|
|
|
|
Terminator::None => vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:52:30 -06:00
|
|
|
impl<'a> Module<'a> {
|
|
|
|
pub fn from_wasm_bytes(bytes: &'a [u8]) -> Result<Self> {
|
2021-11-14 00:25:27 -06:00
|
|
|
frontend::wasm_to_ir(bytes)
|
2021-11-13 22:59:37 -06:00
|
|
|
}
|
2021-11-14 02:00:34 -06:00
|
|
|
|
2021-11-20 03:29:46 -06:00
|
|
|
pub fn to_wasm_bytes(self) -> Vec<u8> {
|
|
|
|
for func in &self.funcs {
|
|
|
|
match func {
|
|
|
|
&FuncDecl::Body(_, ref body) => {
|
|
|
|
let cfg = CFGInfo::new(body);
|
|
|
|
let _shape = Shape::compute(body, &cfg);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2021-11-14 02:00:34 -06:00
|
|
|
// TODO
|
|
|
|
self.orig_bytes.to_vec()
|
|
|
|
}
|
2021-11-13 22:59:37 -06:00
|
|
|
}
|
|
|
|
|
2021-11-14 02:00:34 -06:00
|
|
|
impl Terminator {
|
2021-11-13 22:59:37 -06:00
|
|
|
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-13 19:52:30 -06:00
|
|
|
}
|
|
|
|
}
|