This commit is contained in:
Chris Fallin 2021-11-13 02:32:05 -08:00
parent 26428a9f4d
commit ab260c224f
3 changed files with 126 additions and 58 deletions

View file

@ -4,28 +4,26 @@
use crate::ir::*; use crate::ir::*;
use crate::op_traits::{op_inputs, op_outputs}; use crate::op_traits::{op_inputs, op_outputs};
use anyhow::anyhow;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use log::trace; use log::trace;
use std::collections::VecDeque; use wasmparser::{ImportSectionEntryType, Operator, Parser, Payload, Type, TypeDef};
use wasmparser::{FuncType, ImportSectionEntryType, Operator, Parser, Payload, Type, TypeDef};
pub fn wasm_to_ir(bytes: &[u8]) -> Result<Module> { pub fn wasm_to_ir<'a>(bytes: &'a [u8]) -> Result<Module<'a>> {
let mut module = Module::default(); let mut module = Module::default();
let parser = Parser::new(0); let parser = Parser::new(0);
let mut sigs = VecDeque::new(); let mut next_func = 0;
for payload in parser.parse_all(bytes) { for payload in parser.parse_all(bytes) {
let payload = payload?; let payload = payload?;
handle_payload(&mut module, payload, &mut sigs)?; handle_payload(&mut module, payload, &mut next_func)?;
} }
Ok(module) Ok(module)
} }
fn handle_payload<'a>( fn handle_payload<'a>(
module: &mut Module, module: &mut Module<'a>,
payload: Payload<'a>, payload: Payload<'a>,
func_sigs: &mut VecDeque<SignatureId>, next_func: &mut usize,
) -> Result<()> { ) -> Result<()> {
trace!("Wasm parser item: {:?}", payload); trace!("Wasm parser item: {:?}", payload);
match payload { match payload {
@ -45,6 +43,7 @@ fn handle_payload<'a>(
match reader.read()?.ty { match reader.read()?.ty {
ImportSectionEntryType::Function(sig_idx) => { ImportSectionEntryType::Function(sig_idx) => {
module.funcs.push(FuncDecl::Import(sig_idx as SignatureId)); module.funcs.push(FuncDecl::Import(sig_idx as SignatureId));
*next_func += 1;
} }
_ => {} _ => {}
} }
@ -52,15 +51,25 @@ fn handle_payload<'a>(
} }
Payload::FunctionSection(mut reader) => { Payload::FunctionSection(mut reader) => {
for _ in 0..reader.get_count() { for _ in 0..reader.get_count() {
func_sigs.push_back(reader.read()? as SignatureId); let sig_idx = reader.read()? as SignatureId;
module
.funcs
.push(FuncDecl::Body(sig_idx, FunctionBody::default()));
} }
} }
Payload::CodeSectionEntry(body) => { Payload::CodeSectionEntry(body) => {
let sig = func_sigs let func_idx = *next_func;
.pop_front() *next_func += 1;
.ok_or_else(|| anyhow!("mismatched func section and code section sizes"))?;
let body = parse_body(&module, body)?; let my_sig = module.funcs[func_idx].sig();
module.funcs.push(FuncDecl::Body(sig as SignatureId, body)); let body = parse_body(&module, my_sig, body)?;
match &mut module.funcs[func_idx] {
&mut FuncDecl::Body(_, ref mut existing_body) => {
*existing_body = body;
}
_ => unreachable!(),
}
} }
_ => {} _ => {}
} }
@ -68,8 +77,12 @@ fn handle_payload<'a>(
Ok(()) Ok(())
} }
fn parse_body(module: &Module, body: wasmparser::FunctionBody) -> Result<FunctionBody> { fn parse_body<'a, 'b>(
let mut ret = FunctionBody::default(); module: &'b Module<'a>,
my_sig: SignatureId,
body: wasmparser::FunctionBody<'a>,
) -> Result<FunctionBody<'a>> {
let mut ret: FunctionBody<'a> = FunctionBody::default();
let mut locals = body.get_locals_reader()?; let mut locals = body.get_locals_reader()?;
for _ in 0..locals.get_count() { for _ in 0..locals.get_count() {
let (count, ty) = locals.read()?; let (count, ty) = locals.read()?;
@ -78,7 +91,7 @@ fn parse_body(module: &Module, body: wasmparser::FunctionBody) -> Result<Functio
} }
} }
let mut builder = FunctionBodyBuilder::new(&module.signatures[..], &mut ret); let mut builder = FunctionBodyBuilder::new(&module, my_sig, &mut ret);
let ops = body.get_operators_reader()?; let ops = body.get_operators_reader()?;
for op in ops.into_iter() { for op in ops.into_iter() {
let op = op?; let op = op?;
@ -89,9 +102,10 @@ fn parse_body(module: &Module, body: wasmparser::FunctionBody) -> Result<Functio
} }
#[derive(Debug)] #[derive(Debug)]
struct FunctionBodyBuilder<'a> { struct FunctionBodyBuilder<'a, 'b> {
signatures: &'a [FuncType], module: &'b Module<'a>,
body: &'a mut FunctionBody, my_sig: SignatureId,
body: &'b mut FunctionBody<'a>,
cur_block: BlockId, cur_block: BlockId,
ctrl_stack: Vec<Frame>, ctrl_stack: Vec<Frame>,
op_stack: Vec<ValueId>, op_stack: Vec<ValueId>,
@ -100,34 +114,35 @@ struct FunctionBodyBuilder<'a> {
#[derive(Debug)] #[derive(Debug)]
enum Frame { enum Frame {
Block { Block {
out: Block, out: BlockId,
params: Vec<Type>, params: Vec<Type>,
results: Vec<Type>, results: Vec<Type>,
}, },
Loop { Loop {
top: Block, top: BlockId,
out: Block, out: BlockId,
params: Vec<Type>, params: Vec<Type>,
results: Vec<Type>, results: Vec<Type>,
}, },
If { If {
out: Block, out: BlockId,
el: Block, el: BlockId,
params: Vec<Type>, params: Vec<Type>,
results: Vec<Type>, results: Vec<Type>,
}, },
Else { Else {
out: Block, out: BlockId,
params: Vec<Type>, params: Vec<Type>,
results: Vec<Type>, results: Vec<Type>,
}, },
} }
impl<'a> FunctionBodyBuilder<'a> { impl<'a, 'b> FunctionBodyBuilder<'a, 'b> {
fn new(signatures: &'a [FuncType], body: &'a mut FunctionBody) -> Self { fn new(module: &'b Module<'a>, my_sig: SignatureId, body: &'b mut FunctionBody<'a>) -> Self {
body.blocks.push(Block::default()); body.blocks.push(Block::default());
Self { Self {
signatures, module,
my_sig,
body, body,
ctrl_stack: vec![], ctrl_stack: vec![],
op_stack: vec![], op_stack: vec![],
@ -135,29 +150,47 @@ impl<'a> FunctionBodyBuilder<'a> {
} }
} }
fn handle_op(&mut self, op: Operator<'_>) -> Result<()> { fn handle_op(&mut self, op: Operator<'a>) -> Result<()> {
match op { match op {
Operator::Unreachable => self.emit(Operator::Unreachable, vec![], vec![])?, Operator::Unreachable
| Operator::Call { .. }
| Operator::LocalGet { .. }
| Operator::LocalSet { .. }
| Operator::LocalTee { .. } => self.emit(op.clone())?,
Operator::End => {
if self.ctrl_stack.is_empty() {
self.emit(Operator::Return)?;
} else {
bail!("Unsupported End");
}
}
_ => bail!("Unsupported operator: {:?}", op), _ => bail!("Unsupported operator: {:?}", op),
} }
Ok(()) Ok(())
} }
fn emit( fn emit(&mut self, op: Operator<'a>) -> Result<()> {
&mut self,
op: Operator<'static>,
outputs: Vec<ValueId>,
inputs: Vec<Operand>,
) -> Result<()> {
let block = self.cur_block; let block = self.cur_block;
let inst = self.body.blocks[block].insts.len() as InstId; let inst = self.body.blocks[block].insts.len() as InstId;
for input in op_inputs(self.signatures, &op)?.into_iter().rev() { let mut inputs = vec![];
assert_eq!(self.body.values[self.op_stack.pop().unwrap()].ty, input); for input in op_inputs(self.module, self.my_sig, &self.body.locals[..], &op)?
.into_iter()
.rev()
{
let stack_top = self.op_stack.pop().unwrap();
assert_eq!(self.body.values[stack_top].ty, input);
inputs.push(Operand::Value(stack_top));
} }
for output in op_outputs(self.signatures, &op)?.into_iter() { inputs.reverse();
let mut outputs = vec![];
for output in op_outputs(self.module, &self.body.locals[..], &op)?.into_iter() {
let val = self.body.values.len() as ValueId; let val = self.body.values.len() as ValueId;
outputs.push(val);
self.body.values.push(ValueDef { self.body.values.push(ValueDef {
kind: ValueKind::Inst(block, inst), kind: ValueKind::Inst(block, inst),
ty: output, ty: output,

View file

@ -9,21 +9,30 @@ pub type InstId = usize;
pub type ValueId = usize; pub type ValueId = usize;
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct Module { pub struct Module<'a> {
pub funcs: Vec<FuncDecl>, pub funcs: Vec<FuncDecl<'a>>,
pub signatures: Vec<FuncType>, pub signatures: Vec<FuncType>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum FuncDecl { pub enum FuncDecl<'a> {
Import(SignatureId), Import(SignatureId),
Body(SignatureId, FunctionBody), Body(SignatureId, FunctionBody<'a>),
}
impl<'a> FuncDecl<'a> {
pub fn sig(&self) -> SignatureId {
match self {
&FuncDecl::Import(sig) => sig,
&FuncDecl::Body(sig, ..) => sig,
}
}
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct FunctionBody { pub struct FunctionBody<'a> {
pub locals: Vec<Type>, pub locals: Vec<Type>,
pub blocks: Vec<Block>, pub blocks: Vec<Block<'a>>,
pub values: Vec<ValueDef>, pub values: Vec<ValueDef>,
} }
@ -35,26 +44,25 @@ pub struct ValueDef {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum ValueKind { pub enum ValueKind {
BlockParam(Block), BlockParam(BlockId, usize),
Inst(BlockId, InstId), Inst(BlockId, InstId),
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct Block { pub struct Block<'a> {
pub params: Vec<Type>, pub params: Vec<Type>,
pub insts: Vec<Inst>, pub insts: Vec<Inst<'a>>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Inst { pub struct Inst<'a> {
pub operator: Operator<'static>, pub operator: Operator<'a>,
pub outputs: Vec<ValueId>, pub outputs: Vec<ValueId>,
pub inputs: Vec<Operand>, pub inputs: Vec<Operand<'a>>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Operand { pub enum Operand<'a> {
Value(ValueId), Value(ValueId),
Sub(Box<Inst>), Sub(Box<Inst<'a>>),
Local(usize), // eliminated during local2ssa pass
} }

View file

@ -1,18 +1,45 @@
//! Metadata on operators. //! Metadata on operators.
use crate::ir::{Module, SignatureId};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use wasmparser::{FuncType, Operator, Type}; use wasmparser::{Operator, Type};
pub fn op_inputs(_sigs: &[FuncType], op: &Operator<'_>) -> Result<Vec<Type>> { pub fn op_inputs(
module: &Module,
my_sig: SignatureId,
my_locals: &[Type],
op: &Operator<'_>,
) -> Result<Vec<Type>> {
match op { match op {
&Operator::Unreachable | &Operator::Nop => Ok(vec![]), &Operator::Unreachable | &Operator::Nop => Ok(vec![]),
&Operator::Call { function_index } => {
let sig = module.funcs[function_index as usize].sig();
Ok(Vec::from(module.signatures[sig].params.clone()))
}
&Operator::Return => Ok(Vec::from(module.signatures[my_sig].returns.clone())),
&Operator::LocalSet { local_index } | &Operator::LocalTee { local_index } => {
Ok(vec![my_locals[local_index as usize]])
}
&Operator::LocalGet { .. } => Ok(vec![]),
_ => bail!("Unknown operator in op_inputs(): {:?}", op), _ => bail!("Unknown operator in op_inputs(): {:?}", op),
} }
} }
pub fn op_outputs(_sigs: &[FuncType], op: &Operator<'_>) -> Result<Vec<Type>> { pub fn op_outputs(module: &Module, my_locals: &[Type], op: &Operator<'_>) -> Result<Vec<Type>> {
match op { match op {
&Operator::Unreachable | &Operator::Nop => Ok(vec![]), &Operator::Unreachable | &Operator::Nop => Ok(vec![]),
&Operator::Call { function_index } => {
let sig = module.funcs[function_index as usize].sig();
Ok(Vec::from(module.signatures[sig].returns.clone()))
}
&Operator::Return => Ok(vec![]),
&Operator::LocalSet { .. } | &Operator::LocalTee { .. } => Ok(vec![]),
&Operator::LocalGet { local_index } => Ok(vec![my_locals[local_index as usize]]),
_ => bail!("Unknown operator in op_outputs(): {:?}", op), _ => bail!("Unknown operator in op_outputs(): {:?}", op),
} }
} }