WIP.
This commit is contained in:
parent
26428a9f4d
commit
ab260c224f
113
src/frontend.rs
113
src/frontend.rs
|
@ -4,28 +4,26 @@
|
|||
|
||||
use crate::ir::*;
|
||||
use crate::op_traits::{op_inputs, op_outputs};
|
||||
use anyhow::anyhow;
|
||||
use anyhow::{bail, Result};
|
||||
use log::trace;
|
||||
use std::collections::VecDeque;
|
||||
use wasmparser::{FuncType, ImportSectionEntryType, Operator, Parser, Payload, Type, TypeDef};
|
||||
use wasmparser::{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 parser = Parser::new(0);
|
||||
let mut sigs = VecDeque::new();
|
||||
let mut next_func = 0;
|
||||
for payload in parser.parse_all(bytes) {
|
||||
let payload = payload?;
|
||||
handle_payload(&mut module, payload, &mut sigs)?;
|
||||
handle_payload(&mut module, payload, &mut next_func)?;
|
||||
}
|
||||
|
||||
Ok(module)
|
||||
}
|
||||
|
||||
fn handle_payload<'a>(
|
||||
module: &mut Module,
|
||||
module: &mut Module<'a>,
|
||||
payload: Payload<'a>,
|
||||
func_sigs: &mut VecDeque<SignatureId>,
|
||||
next_func: &mut usize,
|
||||
) -> Result<()> {
|
||||
trace!("Wasm parser item: {:?}", payload);
|
||||
match payload {
|
||||
|
@ -45,6 +43,7 @@ fn handle_payload<'a>(
|
|||
match reader.read()?.ty {
|
||||
ImportSectionEntryType::Function(sig_idx) => {
|
||||
module.funcs.push(FuncDecl::Import(sig_idx as SignatureId));
|
||||
*next_func += 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -52,15 +51,25 @@ fn handle_payload<'a>(
|
|||
}
|
||||
Payload::FunctionSection(mut reader) => {
|
||||
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) => {
|
||||
let sig = func_sigs
|
||||
.pop_front()
|
||||
.ok_or_else(|| anyhow!("mismatched func section and code section sizes"))?;
|
||||
let body = parse_body(&module, body)?;
|
||||
module.funcs.push(FuncDecl::Body(sig as SignatureId, body));
|
||||
let func_idx = *next_func;
|
||||
*next_func += 1;
|
||||
|
||||
let my_sig = module.funcs[func_idx].sig();
|
||||
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(())
|
||||
}
|
||||
|
||||
fn parse_body(module: &Module, body: wasmparser::FunctionBody) -> Result<FunctionBody> {
|
||||
let mut ret = FunctionBody::default();
|
||||
fn parse_body<'a, 'b>(
|
||||
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()?;
|
||||
for _ in 0..locals.get_count() {
|
||||
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()?;
|
||||
for op in ops.into_iter() {
|
||||
let op = op?;
|
||||
|
@ -89,9 +102,10 @@ fn parse_body(module: &Module, body: wasmparser::FunctionBody) -> Result<Functio
|
|||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FunctionBodyBuilder<'a> {
|
||||
signatures: &'a [FuncType],
|
||||
body: &'a mut FunctionBody,
|
||||
struct FunctionBodyBuilder<'a, 'b> {
|
||||
module: &'b Module<'a>,
|
||||
my_sig: SignatureId,
|
||||
body: &'b mut FunctionBody<'a>,
|
||||
cur_block: BlockId,
|
||||
ctrl_stack: Vec<Frame>,
|
||||
op_stack: Vec<ValueId>,
|
||||
|
@ -100,34 +114,35 @@ struct FunctionBodyBuilder<'a> {
|
|||
#[derive(Debug)]
|
||||
enum Frame {
|
||||
Block {
|
||||
out: Block,
|
||||
out: BlockId,
|
||||
params: Vec<Type>,
|
||||
results: Vec<Type>,
|
||||
},
|
||||
Loop {
|
||||
top: Block,
|
||||
out: Block,
|
||||
top: BlockId,
|
||||
out: BlockId,
|
||||
params: Vec<Type>,
|
||||
results: Vec<Type>,
|
||||
},
|
||||
If {
|
||||
out: Block,
|
||||
el: Block,
|
||||
out: BlockId,
|
||||
el: BlockId,
|
||||
params: Vec<Type>,
|
||||
results: Vec<Type>,
|
||||
},
|
||||
Else {
|
||||
out: Block,
|
||||
out: BlockId,
|
||||
params: Vec<Type>,
|
||||
results: Vec<Type>,
|
||||
},
|
||||
}
|
||||
|
||||
impl<'a> FunctionBodyBuilder<'a> {
|
||||
fn new(signatures: &'a [FuncType], body: &'a mut FunctionBody) -> Self {
|
||||
impl<'a, 'b> FunctionBodyBuilder<'a, 'b> {
|
||||
fn new(module: &'b Module<'a>, my_sig: SignatureId, body: &'b mut FunctionBody<'a>) -> Self {
|
||||
body.blocks.push(Block::default());
|
||||
Self {
|
||||
signatures,
|
||||
module,
|
||||
my_sig,
|
||||
body,
|
||||
ctrl_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 {
|
||||
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),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn emit(
|
||||
&mut self,
|
||||
op: Operator<'static>,
|
||||
outputs: Vec<ValueId>,
|
||||
inputs: Vec<Operand>,
|
||||
) -> Result<()> {
|
||||
fn emit(&mut self, op: Operator<'a>) -> Result<()> {
|
||||
let block = self.cur_block;
|
||||
let inst = self.body.blocks[block].insts.len() as InstId;
|
||||
|
||||
for input in op_inputs(self.signatures, &op)?.into_iter().rev() {
|
||||
assert_eq!(self.body.values[self.op_stack.pop().unwrap()].ty, input);
|
||||
let mut inputs = vec![];
|
||||
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;
|
||||
outputs.push(val);
|
||||
self.body.values.push(ValueDef {
|
||||
kind: ValueKind::Inst(block, inst),
|
||||
ty: output,
|
||||
|
|
38
src/ir.rs
38
src/ir.rs
|
@ -9,21 +9,30 @@ pub type InstId = usize;
|
|||
pub type ValueId = usize;
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Module {
|
||||
pub funcs: Vec<FuncDecl>,
|
||||
pub struct Module<'a> {
|
||||
pub funcs: Vec<FuncDecl<'a>>,
|
||||
pub signatures: Vec<FuncType>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum FuncDecl {
|
||||
pub enum FuncDecl<'a> {
|
||||
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)]
|
||||
pub struct FunctionBody {
|
||||
pub struct FunctionBody<'a> {
|
||||
pub locals: Vec<Type>,
|
||||
pub blocks: Vec<Block>,
|
||||
pub blocks: Vec<Block<'a>>,
|
||||
pub values: Vec<ValueDef>,
|
||||
}
|
||||
|
||||
|
@ -35,26 +44,25 @@ pub struct ValueDef {
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ValueKind {
|
||||
BlockParam(Block),
|
||||
BlockParam(BlockId, usize),
|
||||
Inst(BlockId, InstId),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Block {
|
||||
pub struct Block<'a> {
|
||||
pub params: Vec<Type>,
|
||||
pub insts: Vec<Inst>,
|
||||
pub insts: Vec<Inst<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Inst {
|
||||
pub operator: Operator<'static>,
|
||||
pub struct Inst<'a> {
|
||||
pub operator: Operator<'a>,
|
||||
pub outputs: Vec<ValueId>,
|
||||
pub inputs: Vec<Operand>,
|
||||
pub inputs: Vec<Operand<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Operand {
|
||||
pub enum Operand<'a> {
|
||||
Value(ValueId),
|
||||
Sub(Box<Inst>),
|
||||
Local(usize), // eliminated during local2ssa pass
|
||||
Sub(Box<Inst<'a>>),
|
||||
}
|
||||
|
|
|
@ -1,18 +1,45 @@
|
|||
//! Metadata on operators.
|
||||
|
||||
use crate::ir::{Module, SignatureId};
|
||||
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 {
|
||||
&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),
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
&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),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue