waffle/src/frontend.rs

210 lines
5.6 KiB
Rust
Raw Normal View History

//! Frontend: convert Wasm to IR.
2021-11-13 09:41:32 +00:00
#![allow(dead_code)]
use crate::ir::*;
2021-11-13 09:41:32 +00:00
use crate::op_traits::{op_inputs, op_outputs};
use anyhow::{bail, Result};
2021-11-13 08:56:49 +00:00
use log::trace;
2021-11-13 10:32:05 +00:00
use wasmparser::{ImportSectionEntryType, Operator, Parser, Payload, Type, TypeDef};
2021-11-13 10:32:05 +00:00
pub fn wasm_to_ir<'a>(bytes: &'a [u8]) -> Result<Module<'a>> {
let mut module = Module::default();
2021-11-13 08:56:49 +00:00
let parser = Parser::new(0);
2021-11-13 10:32:05 +00:00
let mut next_func = 0;
2021-11-13 08:56:49 +00:00
for payload in parser.parse_all(bytes) {
let payload = payload?;
2021-11-13 10:32:05 +00:00
handle_payload(&mut module, payload, &mut next_func)?;
}
Ok(module)
}
2021-11-13 09:10:22 +00:00
fn handle_payload<'a>(
2021-11-13 10:32:05 +00:00
module: &mut Module<'a>,
2021-11-13 09:10:22 +00:00
payload: Payload<'a>,
2021-11-13 10:32:05 +00:00
next_func: &mut usize,
2021-11-13 09:10:22 +00:00
) -> Result<()> {
2021-11-13 08:56:49 +00:00
trace!("Wasm parser item: {:?}", payload);
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 08:59:15 +00: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 10:32:05 +00:00
*next_func += 1;
2021-11-13 08:59:15 +00:00
}
_ => {}
}
}
}
2021-11-13 09:10:22 +00:00
Payload::FunctionSection(mut reader) => {
for _ in 0..reader.get_count() {
2021-11-13 10:32:05 +00:00
let sig_idx = reader.read()? as SignatureId;
module
.funcs
.push(FuncDecl::Body(sig_idx, FunctionBody::default()));
2021-11-13 09:10:22 +00:00
}
}
Payload::CodeSectionEntry(body) => {
2021-11-13 10:32:05 +00:00
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!(),
}
2021-11-13 09:10:22 +00:00
}
_ => {}
}
Ok(())
}
2021-11-13 09:10:22 +00:00
2021-11-13 10:32:05 +00:00
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();
2021-11-13 09:10:22 +00:00
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 10:32:05 +00:00
let mut builder = FunctionBodyBuilder::new(&module, my_sig, &mut ret);
2021-11-13 09:17:53 +00:00
let ops = body.get_operators_reader()?;
for op in ops.into_iter() {
let op = op?;
builder.handle_op(op)?;
}
2021-11-13 09:10:22 +00:00
Ok(ret)
}
2021-11-13 09:17:53 +00:00
#[derive(Debug)]
2021-11-13 10:32:05 +00:00
struct FunctionBodyBuilder<'a, 'b> {
module: &'b Module<'a>,
my_sig: SignatureId,
body: &'b mut FunctionBody<'a>,
2021-11-13 09:41:32 +00:00
cur_block: BlockId,
ctrl_stack: Vec<Frame>,
op_stack: Vec<ValueId>,
}
#[derive(Debug)]
enum Frame {
Block {
2021-11-13 10:32:05 +00:00
out: BlockId,
2021-11-13 09:41:32 +00:00
params: Vec<Type>,
results: Vec<Type>,
},
Loop {
2021-11-13 10:32:05 +00:00
top: BlockId,
out: BlockId,
2021-11-13 09:41:32 +00:00
params: Vec<Type>,
results: Vec<Type>,
},
If {
2021-11-13 10:32:05 +00:00
out: BlockId,
el: BlockId,
2021-11-13 09:41:32 +00:00
params: Vec<Type>,
results: Vec<Type>,
},
Else {
2021-11-13 10:32:05 +00:00
out: BlockId,
2021-11-13 09:41:32 +00:00
params: Vec<Type>,
results: Vec<Type>,
},
2021-11-13 09:17:53 +00:00
}
2021-11-13 10:32:05 +00:00
impl<'a, 'b> FunctionBodyBuilder<'a, 'b> {
fn new(module: &'b Module<'a>, my_sig: SignatureId, body: &'b mut FunctionBody<'a>) -> Self {
2021-11-13 09:41:32 +00:00
body.blocks.push(Block::default());
Self {
2021-11-13 10:32:05 +00:00
module,
my_sig,
2021-11-13 09:41:32 +00:00
body,
ctrl_stack: vec![],
op_stack: vec![],
cur_block: 0,
}
2021-11-13 09:17:53 +00:00
}
2021-11-13 10:32:05 +00:00
fn handle_op(&mut self, op: Operator<'a>) -> Result<()> {
2021-11-13 09:17:53 +00:00
match op {
2021-11-13 10:32:05 +00:00
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");
}
}
2021-11-13 09:41:32 +00:00
_ => bail!("Unsupported operator: {:?}", op),
2021-11-13 09:17:53 +00:00
}
Ok(())
}
2021-11-13 09:41:32 +00:00
2021-11-13 10:32:05 +00:00
fn emit(&mut self, op: Operator<'a>) -> Result<()> {
2021-11-13 09:41:32 +00:00
let block = self.cur_block;
let inst = self.body.blocks[block].insts.len() as InstId;
2021-11-13 10:32:05 +00:00
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));
2021-11-13 09:41:32 +00:00
}
2021-11-13 10:32:05 +00:00
inputs.reverse();
let mut outputs = vec![];
for output in op_outputs(self.module, &self.body.locals[..], &op)?.into_iter() {
2021-11-13 09:41:32 +00:00
let val = self.body.values.len() as ValueId;
2021-11-13 10:32:05 +00:00
outputs.push(val);
2021-11-13 09:41:32 +00:00
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 09:17:53 +00:00
}