2021-11-13 02:52:35 -06:00
|
|
|
//! Frontend: convert Wasm to IR.
|
|
|
|
|
2021-11-13 03:41:32 -06:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2021-11-13 02:52:35 -06:00
|
|
|
use crate::ir::*;
|
2021-11-13 03:41:32 -06:00
|
|
|
use crate::op_traits::{op_inputs, op_outputs};
|
2021-11-13 03:10:22 -06:00
|
|
|
use anyhow::anyhow;
|
2021-11-13 03:41:32 -06:00
|
|
|
use anyhow::{bail, Result};
|
2021-11-13 02:56:49 -06:00
|
|
|
use log::trace;
|
2021-11-13 03:10:22 -06:00
|
|
|
use std::collections::VecDeque;
|
2021-11-13 03:41:32 -06:00
|
|
|
use wasmparser::{FuncType, ImportSectionEntryType, Operator, Parser, Payload, Type, TypeDef};
|
2021-11-13 02:52:35 -06:00
|
|
|
|
|
|
|
pub fn wasm_to_ir(bytes: &[u8]) -> Result<Module> {
|
|
|
|
let mut module = Module::default();
|
2021-11-13 02:56:49 -06:00
|
|
|
let parser = Parser::new(0);
|
2021-11-13 03:10:22 -06:00
|
|
|
let mut sigs = VecDeque::new();
|
2021-11-13 02:56:49 -06:00
|
|
|
for payload in parser.parse_all(bytes) {
|
|
|
|
let payload = payload?;
|
2021-11-13 03:10:22 -06:00
|
|
|
handle_payload(&mut module, payload, &mut sigs)?;
|
2021-11-13 02:52:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(module)
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:10:22 -06:00
|
|
|
fn handle_payload<'a>(
|
|
|
|
module: &mut Module,
|
|
|
|
payload: Payload<'a>,
|
|
|
|
func_sigs: &mut VecDeque<SignatureId>,
|
|
|
|
) -> Result<()> {
|
2021-11-13 02:56:49 -06:00
|
|
|
trace!("Wasm parser item: {:?}", payload);
|
2021-11-13 02:52:35 -06:00
|
|
|
match payload {
|
|
|
|
Payload::TypeSection(mut reader) => {
|
|
|
|
for _ in 0..reader.get_count() {
|
|
|
|
let ty = reader.read()?;
|
|
|
|
match ty {
|
|
|
|
TypeDef::Func(fty) => {
|
|
|
|
module.signatures.push(fty);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 02:59:15 -06:00
|
|
|
Payload::ImportSection(mut reader) => {
|
|
|
|
for _ in 0..reader.get_count() {
|
|
|
|
match reader.read()?.ty {
|
|
|
|
ImportSectionEntryType::Function(sig_idx) => {
|
|
|
|
module.funcs.push(FuncDecl::Import(sig_idx as SignatureId));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 03:10:22 -06:00
|
|
|
Payload::FunctionSection(mut reader) => {
|
|
|
|
for _ in 0..reader.get_count() {
|
|
|
|
func_sigs.push_back(reader.read()? as SignatureId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Payload::CodeSectionEntry(body) => {
|
|
|
|
let sig = func_sigs
|
|
|
|
.pop_front()
|
|
|
|
.ok_or_else(|| anyhow!("mismatched func section and code section sizes"))?;
|
2021-11-13 03:41:32 -06:00
|
|
|
let body = parse_body(&module, body)?;
|
2021-11-13 03:10:22 -06:00
|
|
|
module.funcs.push(FuncDecl::Body(sig as SignatureId, body));
|
|
|
|
}
|
2021-11-13 02:52:35 -06:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-13 03:10:22 -06:00
|
|
|
|
2021-11-13 03:41:32 -06:00
|
|
|
fn parse_body(module: &Module, body: wasmparser::FunctionBody) -> Result<FunctionBody> {
|
2021-11-13 03:10:22 -06:00
|
|
|
let mut ret = FunctionBody::default();
|
|
|
|
let mut locals = body.get_locals_reader()?;
|
|
|
|
for _ in 0..locals.get_count() {
|
|
|
|
let (count, ty) = locals.read()?;
|
|
|
|
for _ in 0..count {
|
|
|
|
ret.locals.push(ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:41:32 -06:00
|
|
|
let mut builder = FunctionBodyBuilder::new(&module.signatures[..], &mut ret);
|
2021-11-13 03:17:53 -06:00
|
|
|
let ops = body.get_operators_reader()?;
|
|
|
|
for op in ops.into_iter() {
|
|
|
|
let op = op?;
|
|
|
|
builder.handle_op(op)?;
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:10:22 -06:00
|
|
|
Ok(ret)
|
|
|
|
}
|
2021-11-13 03:17:53 -06:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct FunctionBodyBuilder<'a> {
|
2021-11-13 03:41:32 -06:00
|
|
|
signatures: &'a [FuncType],
|
2021-11-13 03:17:53 -06:00
|
|
|
body: &'a mut FunctionBody,
|
2021-11-13 03:41:32 -06:00
|
|
|
cur_block: BlockId,
|
|
|
|
ctrl_stack: Vec<Frame>,
|
|
|
|
op_stack: Vec<ValueId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Frame {
|
|
|
|
Block {
|
|
|
|
out: Block,
|
|
|
|
params: Vec<Type>,
|
|
|
|
results: Vec<Type>,
|
|
|
|
},
|
|
|
|
Loop {
|
|
|
|
top: Block,
|
|
|
|
out: Block,
|
|
|
|
params: Vec<Type>,
|
|
|
|
results: Vec<Type>,
|
|
|
|
},
|
|
|
|
If {
|
|
|
|
out: Block,
|
|
|
|
el: Block,
|
|
|
|
params: Vec<Type>,
|
|
|
|
results: Vec<Type>,
|
|
|
|
},
|
|
|
|
Else {
|
|
|
|
out: Block,
|
|
|
|
params: Vec<Type>,
|
|
|
|
results: Vec<Type>,
|
|
|
|
},
|
2021-11-13 03:17:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FunctionBodyBuilder<'a> {
|
2021-11-13 03:41:32 -06:00
|
|
|
fn new(signatures: &'a [FuncType], body: &'a mut FunctionBody) -> Self {
|
|
|
|
body.blocks.push(Block::default());
|
|
|
|
Self {
|
|
|
|
signatures,
|
|
|
|
body,
|
|
|
|
ctrl_stack: vec![],
|
|
|
|
op_stack: vec![],
|
|
|
|
cur_block: 0,
|
|
|
|
}
|
2021-11-13 03:17:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_op(&mut self, op: Operator<'_>) -> Result<()> {
|
|
|
|
match op {
|
2021-11-13 03:41:32 -06:00
|
|
|
Operator::Unreachable => self.emit(Operator::Unreachable, vec![], vec![])?,
|
|
|
|
_ => bail!("Unsupported operator: {:?}", op),
|
2021-11-13 03:17:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-13 03:41:32 -06:00
|
|
|
|
|
|
|
fn emit(
|
|
|
|
&mut self,
|
|
|
|
op: Operator<'static>,
|
|
|
|
outputs: Vec<ValueId>,
|
|
|
|
inputs: Vec<Operand>,
|
|
|
|
) -> 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);
|
|
|
|
}
|
|
|
|
for output in op_outputs(self.signatures, &op)?.into_iter() {
|
|
|
|
let val = self.body.values.len() as ValueId;
|
|
|
|
self.body.values.push(ValueDef {
|
|
|
|
kind: ValueKind::Inst(block, inst),
|
|
|
|
ty: output,
|
|
|
|
});
|
|
|
|
self.op_stack.push(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.body.blocks[self.cur_block].insts.push(Inst {
|
|
|
|
operator: op,
|
|
|
|
outputs,
|
|
|
|
inputs,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-13 03:17:53 -06:00
|
|
|
}
|