waffle/src/ir.rs

370 lines
9.7 KiB
Rust
Raw Normal View History

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-21 19:15:26 -06:00
use fxhash::FxHashMap;
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;
pub type LocalId = 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
#[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>,
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
}
#[derive(Clone, Debug, Default)]
2021-11-22 01:12:07 -06:00
pub struct FunctionBody {
2021-11-21 19:15:26 -06:00
pub arg_values: Vec<Value>,
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-11-21 19:15:26 -06:00
pub types: FxHashMap<Value, Type>,
}
#[derive(Clone, Debug, Default)]
2021-11-22 01:12:07 -06:00
pub struct Block {
2021-11-21 19:15:26 -06:00
pub id: BlockId,
pub params: Vec<Type>,
2021-11-22 01:12:07 -06:00
pub insts: Vec<Inst>,
2021-11-14 02:00:34 -06:00
pub terminator: Terminator,
}
2021-11-22 01:12:07 -06:00
impl Block {
2021-11-15 01:56:56 -06:00
pub fn successors(&self) -> Vec<BlockId> {
self.terminator.successors()
}
2021-11-22 01:12:07 -06:00
pub fn defs<'b>(&'b self) -> impl Iterator<Item = Value> + 'b {
2021-11-21 19:15:26 -06:00
let block = self.id;
2021-11-22 01:12:07 -06:00
let param_values = (0..self.params.len()).map(move |i| Value::blockparam(block, i));
let inst_values = self
.insts
2021-11-15 01:56:56 -06:00
.iter()
2021-11-21 19:15:26 -06:00
.enumerate()
.map(move |(inst_id, inst)| {
(0..inst.n_outputs).map(move |i| Value::inst(block, inst_id, i))
})
2021-11-22 01:12:07 -06:00
.flatten();
param_values.chain(inst_values)
2021-11-15 01:56:56 -06:00
}
2021-11-22 01:12:07 -06:00
pub fn visit_uses<F: FnMut(Value)>(&self, mut f: F) {
2021-11-15 01:56:56 -06:00
for inst in &self.insts {
2021-11-22 01:12:07 -06:00
for &input in &inst.inputs {
2021-11-15 01:56:56 -06:00
f(input);
}
}
2021-11-22 01:12:07 -06:00
self.terminator.visit_uses(f);
2021-11-15 01:56:56 -06:00
}
2021-11-22 01:12:07 -06:00
pub fn update_uses<F: FnMut(&mut Value)>(&mut self, mut f: F) {
2021-11-15 01:56:56 -06:00
for inst in &mut self.insts {
for input in &mut inst.inputs {
f(input);
}
}
2021-11-22 01:12:07 -06:00
self.terminator.update_uses(f);
2021-11-15 01:56:56 -06:00
}
}
#[derive(Clone, Debug)]
2021-11-22 01:12:07 -06:00
pub struct Inst {
pub operator: Operator<'static>,
2021-11-21 19:15:26 -06:00
pub n_outputs: usize,
pub inputs: Vec<Value>,
}
2021-11-22 01:12:07 -06:00
impl Inst {
pub fn make<'a>(operator: &Operator<'a>, n_outputs: usize, inputs: Vec<Value>) -> Self {
// The only operator that actually makes use of the lifetime
// parameter is BrTable.
assert!(!matches!(operator, &Operator::BrTable { .. }));
let operator = operator.clone();
let operator = unsafe { std::mem::transmute(operator) };
Inst {
operator,
n_outputs,
inputs,
}
}
}
2021-11-21 19:15:26 -06:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Value(u64);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u64)]
enum ValueTag {
/// Undefined value. Fields: tag(4) unused(60).
Undef = 0,
/// Function argument. Fields: tag(4) unused(52) index(8).
Arg = 1,
/// Block param. Fields: tag(4) unused(2) block(26) param(32).
BlockParam = 2,
/// Instruction output. Fields: tag(4) block(26) inst(26) output(8).
InstOutput = 3,
2021-11-13 16:23:22 -06:00
}
2021-11-21 19:15:26 -06:00
const VALUE_TAG_SHIFT: usize = 60;
impl Value {
pub fn undef() -> Self {
Value((ValueTag::Undef as u64) << VALUE_TAG_SHIFT)
}
pub fn arg(index: usize) -> Self {
assert!(index < 256);
Value(((ValueTag::Arg as u64) << VALUE_TAG_SHIFT) | (index as u64))
}
pub fn blockparam(block: BlockId, index: usize) -> Self {
assert!(index < 256);
assert!(block < (1 << 26));
Value(
((ValueTag::BlockParam as u64) << VALUE_TAG_SHIFT)
| ((block as u64) << 32)
| (index as u64),
)
}
pub fn inst(block: BlockId, inst: InstId, index: usize) -> Self {
assert!(index < 256);
assert!(block < (1 << 26));
assert!(inst < (1 << 26));
Value(
((ValueTag::InstOutput as u64) << VALUE_TAG_SHIFT)
| ((block as u64) << 34)
| ((inst as u64) << 8)
| (index as u64),
)
}
pub fn unpack(self) -> ValueKind {
let tag = self.0 >> VALUE_TAG_SHIFT;
match tag {
0 => ValueKind::Undef,
1 => ValueKind::Arg((self.0 & ((1 << 8) - 1)) as usize),
2 => ValueKind::BlockParam(
((self.0 >> 32) & ((1 << 26) - 1)) as usize,
(self.0 & 0xff) as usize,
),
3 => ValueKind::Inst(
((self.0 >> 34) & ((1 << 26) - 1)) as usize,
((self.0 >> 8) & ((1 << 26) - 1)) as usize,
(self.0 & 0xff) as usize,
),
_ => unreachable!(),
2021-11-13 16:23:22 -06:00
}
}
2021-11-21 19:15:26 -06:00
pub fn as_arg(self) -> Option<usize> {
match self.unpack() {
ValueKind::Arg(arg) => Some(arg),
_ => None,
}
}
pub fn as_blockparam(self) -> Option<(BlockId, usize)> {
match self.unpack() {
ValueKind::BlockParam(block, param) => Some((block, param)),
_ => None,
}
}
pub fn as_inst(self) -> Option<(BlockId, InstId, usize)> {
match self.unpack() {
ValueKind::Inst(block, inst, param) => Some((block, inst, param)),
_ => None,
}
}
2021-11-22 01:12:07 -06:00
pub fn index(self) -> u64 {
self.0
}
pub fn from_index(value: u64) -> Value {
let tag = value >> VALUE_TAG_SHIFT;
assert!(tag < 4);
Self(value)
}
2021-11-21 19:15:26 -06:00
}
impl std::fmt::Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self.unpack() {
ValueKind::Undef => write!(f, "undef"),
ValueKind::Arg(i) => write!(f, "arg{}", i),
ValueKind::BlockParam(block, i) => write!(f, "block{}_{}", block, i),
ValueKind::Inst(block, inst, i) => write!(f, "inst{}_{}_{}", block, inst, i),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ValueKind {
Undef,
Arg(usize),
BlockParam(BlockId, usize),
Inst(BlockId, InstId, usize),
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-21 19:15:26 -06:00
pub args: Vec<Value>,
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-21 19:15:26 -06:00
cond: Value,
2021-11-14 02:00:34 -06:00
if_true: BlockTarget,
if_false: BlockTarget,
2021-11-13 05:38:47 -06:00
},
Select {
2021-11-21 19:15:26 -06:00
value: Value,
2021-11-14 02:00:34 -06:00
targets: Vec<BlockTarget>,
default: BlockTarget,
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-11-21 19:15:26 -06:00
pub fn args(&self) -> Vec<Value> {
2021-11-14 02:00:34 -06:00
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
}
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
}