2021-11-13 00:16:54 -06:00
|
|
|
//! Intermediate representation for Wasm.
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
use crate::{backend::Shape, cfg::CFGInfo, frontend, Operator};
|
2021-11-13 19:52:30 -06:00
|
|
|
use anyhow::Result;
|
2021-11-21 19:15:26 -06:00
|
|
|
use fxhash::FxHashMap;
|
2021-12-03 01:33:04 -06:00
|
|
|
use wasmparser::{FuncType, 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-14 01:02:47 -06:00
|
|
|
pub type LocalId = u32;
|
2021-12-03 01:33:04 -06:00
|
|
|
pub type GlobalId = u32;
|
|
|
|
pub type TableId = u32;
|
|
|
|
pub type MemoryId = u32;
|
2021-11-13 00:16:54 -06:00
|
|
|
|
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-22 01:12:07 -06:00
|
|
|
pub funcs: Vec<FuncDecl>,
|
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-22 01:12:07 -06:00
|
|
|
pub enum FuncDecl {
|
2021-11-13 00:16:54 -06:00
|
|
|
Import(SignatureId),
|
2021-11-22 01:12:07 -06:00
|
|
|
Body(SignatureId, FunctionBody),
|
2021-11-13 04:32:05 -06:00
|
|
|
}
|
|
|
|
|
2021-11-22 01:12:07 -06:00
|
|
|
impl FuncDecl {
|
2021-11-13 04:32:05 -06:00
|
|
|
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-22 01:12:07 -06:00
|
|
|
pub struct FunctionBody {
|
2021-11-13 00:16:54 -06:00
|
|
|
pub locals: Vec<Type>,
|
2021-11-22 01:12:07 -06:00
|
|
|
pub blocks: Vec<Block>,
|
2021-12-03 01:33:04 -06:00
|
|
|
/// Sea-of-nodes representation.
|
|
|
|
pub values: Vec<ValueDef>,
|
|
|
|
value_dedup: FxHashMap<ValueDef, Value>,
|
|
|
|
pub types: Vec</* Value, */ Option<Type>>,
|
2021-11-13 02:52:35 -06:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
impl FunctionBody {
|
|
|
|
pub fn add_block(&mut self) -> BlockId {
|
|
|
|
let id = self.blocks.len();
|
|
|
|
self.blocks.push(Block::default());
|
|
|
|
self.blocks[id].id = id;
|
|
|
|
id
|
|
|
|
}
|
2021-11-13 02:52:35 -06:00
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
pub fn add_edge(&mut self, from: BlockId, to: BlockId) {
|
|
|
|
let succ_pos = self.blocks[from].succs.len();
|
|
|
|
let pred_pos = self.blocks[to].preds.len();
|
|
|
|
self.blocks[from].succs.push(to);
|
|
|
|
self.blocks[to].preds.push(from);
|
|
|
|
self.blocks[from].pos_in_succ_pred.push(pred_pos);
|
|
|
|
self.blocks[to].pos_in_pred_succ.push(succ_pos);
|
2021-11-15 01:56:56 -06:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
pub fn add_value(&mut self, value: ValueDef, ty: Option<Type>) -> Value {
|
|
|
|
let id = Value(self.values.len() as u32);
|
|
|
|
self.values.push(value);
|
|
|
|
self.types.push(ty);
|
|
|
|
id
|
2021-11-15 01:56:56 -06:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
pub fn append_to_block(&mut self, block: BlockId, value: Value) {
|
|
|
|
self.blocks[block].insts.push(value);
|
2021-11-15 01:56:56 -06:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
pub fn end_block(&mut self, block: BlockId, terminator: Terminator) {
|
|
|
|
terminator.visit_successors(|succ| {
|
|
|
|
self.add_edge(block, succ);
|
|
|
|
});
|
|
|
|
self.blocks[block].terminator = terminator;
|
2021-11-15 01:56:56 -06:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
pub fn add_local(&mut self, ty: Type) -> LocalId {
|
|
|
|
let id = self.locals.len() as LocalId;
|
|
|
|
self.locals.push(ty);
|
|
|
|
id
|
|
|
|
}
|
2021-11-13 02:52:35 -06:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
impl std::ops::Index<Value> for FunctionBody {
|
|
|
|
type Output = ValueDef;
|
|
|
|
fn index(&self, index: Value) -> &ValueDef {
|
|
|
|
&self.values[index.0 as usize]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::ops::IndexMut<Value> for FunctionBody {
|
|
|
|
fn index_mut(&mut self, index: Value) -> &mut ValueDef {
|
|
|
|
&mut self.values[index.0 as usize]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::ops::Index<BlockId> for FunctionBody {
|
|
|
|
type Output = Block;
|
|
|
|
fn index(&self, index: BlockId) -> &Block {
|
|
|
|
&self.blocks[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::ops::IndexMut<BlockId> for FunctionBody {
|
|
|
|
fn index_mut(&mut self, index: BlockId) -> &mut Block {
|
|
|
|
&mut self.blocks[index]
|
2021-11-22 01:12:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Block {
|
|
|
|
pub id: BlockId,
|
|
|
|
/// Side-effecting values from the sea-of-nodes that are computed, in order.
|
|
|
|
pub insts: Vec<Value>,
|
|
|
|
/// Terminator: branch or return.
|
|
|
|
pub terminator: Terminator,
|
|
|
|
/// Successor blocks.
|
|
|
|
pub succs: Vec<BlockId>,
|
|
|
|
/// For each successor block, our index in its `preds` array.
|
|
|
|
pub pos_in_succ_pred: Vec<usize>,
|
|
|
|
/// Predecessor blocks.
|
|
|
|
pub preds: Vec<BlockId>,
|
|
|
|
/// For each predecessor block, our index in its `succs` array.
|
|
|
|
pub pos_in_pred_succ: Vec<usize>,
|
2021-11-13 16:23:22 -06:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct Value(u32);
|
2021-11-21 19:15:26 -06:00
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn undef() -> Self {
|
2021-12-03 01:33:04 -06:00
|
|
|
Value(u32::MAX)
|
2021-11-21 19:15:26 -06:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
pub fn index(self) -> usize {
|
|
|
|
self.0 as usize
|
2021-11-21 19:15:26 -06:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
pub fn from_index(value: usize) -> Value {
|
|
|
|
Self(value as u32)
|
2021-11-13 16:23:22 -06:00
|
|
|
}
|
2021-12-03 01:33:04 -06:00
|
|
|
}
|
2021-11-21 19:15:26 -06:00
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
impl std::default::Default for Value {
|
|
|
|
fn default() -> Self {
|
|
|
|
Value::undef()
|
2021-11-21 19:15:26 -06:00
|
|
|
}
|
2021-12-03 01:33:04 -06:00
|
|
|
}
|
2021-11-21 19:15:26 -06:00
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum ValueDef {
|
|
|
|
Arg { index: usize },
|
|
|
|
BlockParam { block: BlockId, index: usize },
|
|
|
|
Operator { op: Operator, args: Vec<Value> },
|
|
|
|
PickOutput { from: Value, index: usize },
|
|
|
|
}
|
2021-11-21 19:15:26 -06:00
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
impl ValueDef {
|
|
|
|
pub fn visit_uses<F: FnMut(Value)>(&self, mut f: F) {
|
|
|
|
match self {
|
2021-12-03 01:34:57 -06:00
|
|
|
&ValueDef::Arg { .. } => {}
|
|
|
|
&ValueDef::BlockParam { .. } => {}
|
2021-12-03 01:33:04 -06:00
|
|
|
&ValueDef::Operator { ref args, .. } => {
|
|
|
|
for &arg in args {
|
|
|
|
f(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&ValueDef::PickOutput { from, .. } => f(from),
|
2021-11-21 19:15:26 -06:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 01:12:07 -06:00
|
|
|
|
2021-12-03 01:33:04 -06:00
|
|
|
pub fn update_uses<F: FnMut(&mut Value)>(&mut self, mut f: F) {
|
|
|
|
match self {
|
2021-12-03 01:34:57 -06:00
|
|
|
&mut ValueDef::Arg { .. } => {}
|
|
|
|
&mut ValueDef::BlockParam { .. } => {}
|
2021-12-03 01:33:04 -06:00
|
|
|
&mut ValueDef::Operator { ref mut args, .. } => {
|
2021-12-03 01:34:57 -06:00
|
|
|
for arg in args {
|
2021-12-03 01:33:04 -06:00
|
|
|
f(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&mut ValueDef::PickOutput { ref mut from, .. } => f(from),
|
2021-11-21 19:15:26 -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-12-03 01:33:04 -06:00
|
|
|
target: BlockId,
|
2021-11-13 05:38:47 -06:00
|
|
|
},
|
|
|
|
CondBr {
|
2021-11-21 19:15:26 -06:00
|
|
|
cond: Value,
|
2021-12-03 01:33:04 -06:00
|
|
|
if_true: BlockId,
|
|
|
|
if_false: BlockId,
|
2021-11-13 05:38:47 -06:00
|
|
|
},
|
|
|
|
Select {
|
2021-11-21 19:15:26 -06:00
|
|
|
value: Value,
|
2021-12-03 01:33:04 -06:00
|
|
|
targets: Vec<BlockId>,
|
|
|
|
default: BlockId,
|
2021-11-13 05:38:47 -06:00
|
|
|
},
|
|
|
|
Return {
|
2021-11-21 19:15:26 -06:00
|
|
|
values: Vec<Value>,
|
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 {
|
2021-12-03 01:33:04 -06:00
|
|
|
pub fn visit_successors<F: FnMut(BlockId)>(&self, mut f: F) {
|
2021-11-14 02:00:34 -06:00
|
|
|
match self {
|
2021-12-03 01:33:04 -06:00
|
|
|
Terminator::Return { .. } => {}
|
2021-12-03 01:34:57 -06:00
|
|
|
Terminator::Br { target, .. } => f(*target),
|
2021-11-14 02:00:34 -06:00
|
|
|
Terminator::CondBr {
|
2021-12-03 01:33:04 -06:00
|
|
|
if_true, if_false, ..
|
2021-11-14 02:00:34 -06:00
|
|
|
} => {
|
2021-12-03 01:34:57 -06:00
|
|
|
f(*if_true);
|
|
|
|
f(*if_false);
|
2021-11-14 02:00:34 -06:00
|
|
|
}
|
2021-11-13 22:59:37 -06:00
|
|
|
Terminator::Select {
|
|
|
|
ref targets,
|
|
|
|
default,
|
|
|
|
..
|
|
|
|
} => {
|
2021-12-03 01:33:04 -06:00
|
|
|
for &target in targets {
|
|
|
|
f(target);
|
|
|
|
}
|
2021-12-03 01:34:57 -06:00
|
|
|
f(*default);
|
2021-11-13 22:59:37 -06:00
|
|
|
}
|
2021-12-03 01:33:04 -06:00
|
|
|
Terminator::None => {}
|
2021-11-13 22:59:37 -06:00
|
|
|
}
|
2021-11-13 19:52:30 -06:00
|
|
|
}
|
2021-11-22 01:12:07 -06:00
|
|
|
|
|
|
|
pub fn visit_uses<F: FnMut(Value)>(&self, mut f: F) {
|
|
|
|
match self {
|
|
|
|
&Terminator::CondBr { cond, .. } => f(cond),
|
|
|
|
&Terminator::Select { value, .. } => f(value),
|
|
|
|
&Terminator::Return { ref values, .. } => {
|
|
|
|
for &value in values {
|
|
|
|
f(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_uses<F: FnMut(&mut Value)>(&mut self, mut f: F) {
|
|
|
|
match self {
|
|
|
|
&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 19:52:30 -06:00
|
|
|
}
|
2021-12-03 01:33:04 -06:00
|
|
|
|
|
|
|
impl<'a> Module<'a> {
|
|
|
|
pub fn from_wasm_bytes(bytes: &'a [u8]) -> Result<Self> {
|
|
|
|
frontend::wasm_to_ir(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO
|
|
|
|
self.orig_bytes.to_vec()
|
|
|
|
}
|
|
|
|
}
|