waffle/src/frontend.rs

247 lines
7 KiB
Rust
Raw Normal View History

//! Frontend: convert Wasm to IR.
2021-11-13 03:41:32 -06:00
#![allow(dead_code)]
use crate::ir::*;
2021-11-13 03:41:32 -06:00
use crate::op_traits::{op_inputs, op_outputs};
use anyhow::{bail, Result};
2021-11-13 02:56:49 -06:00
use log::trace;
2021-11-13 05:38:47 -06:00
use wasmparser::{
ImportSectionEntryType, Operator, Parser, Payload, Type, TypeDef, TypeOrFuncType,
};
2021-11-13 05:49:19 -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 04:32:05 -06:00
let mut next_func = 0;
2021-11-13 02:56:49 -06:00
for payload in parser.parse_all(bytes) {
let payload = payload?;
2021-11-13 04:32:05 -06:00
handle_payload(&mut module, payload, &mut next_func)?;
}
Ok(module)
}
2021-11-13 03:10:22 -06:00
fn handle_payload<'a>(
2021-11-13 04:32:05 -06:00
module: &mut Module<'a>,
2021-11-13 03:10:22 -06:00
payload: Payload<'a>,
2021-11-13 04:32:05 -06:00
next_func: &mut usize,
2021-11-13 03:10:22 -06:00
) -> Result<()> {
2021-11-13 02:56:49 -06:00
trace!("Wasm parser item: {:?}", payload);
match payload {
Payload::TypeSection(mut reader) => {
for _ in 0..reader.get_count() {
let ty = reader.read()?;
2021-11-13 05:49:19 -06:00
if let TypeDef::Func(fty) = ty {
module.signatures.push(fty);
}
}
}
2021-11-13 02:59:15 -06:00
Payload::ImportSection(mut reader) => {
for _ in 0..reader.get_count() {
2021-11-13 05:49:19 -06:00
if let ImportSectionEntryType::Function(sig_idx) = reader.read()?.ty {
module.funcs.push(FuncDecl::Import(sig_idx as SignatureId));
*next_func += 1;
2021-11-13 02:59:15 -06:00
}
}
}
2021-11-13 03:10:22 -06:00
Payload::FunctionSection(mut reader) => {
for _ in 0..reader.get_count() {
2021-11-13 04:32:05 -06:00
let sig_idx = reader.read()? as SignatureId;
module
.funcs
.push(FuncDecl::Body(sig_idx, FunctionBody::default()));
2021-11-13 03:10:22 -06:00
}
}
Payload::CodeSectionEntry(body) => {
2021-11-13 04:32:05 -06:00
let func_idx = *next_func;
*next_func += 1;
let my_sig = module.funcs[func_idx].sig();
2021-11-13 05:49:19 -06:00
let body = parse_body(module, my_sig, body)?;
2021-11-13 04:32:05 -06:00
match &mut module.funcs[func_idx] {
2021-11-13 05:49:19 -06:00
FuncDecl::Body(_, ref mut existing_body) => {
2021-11-13 04:32:05 -06:00
*existing_body = body;
}
_ => unreachable!(),
}
2021-11-13 03:10:22 -06:00
}
_ => {}
}
Ok(())
}
2021-11-13 03:10:22 -06:00
2021-11-13 04:32:05 -06: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 03:10:22 -06: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 05:49:19 -06:00
let mut builder = FunctionBodyBuilder::new(module, my_sig, &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)]
2021-11-13 04:32:05 -06:00
struct FunctionBodyBuilder<'a, 'b> {
module: &'b Module<'a>,
my_sig: SignatureId,
body: &'b mut FunctionBody<'a>,
2021-11-13 03:41:32 -06:00
cur_block: BlockId,
ctrl_stack: Vec<Frame>,
op_stack: Vec<ValueId>,
}
#[derive(Debug)]
enum Frame {
Block {
2021-11-13 04:32:05 -06:00
out: BlockId,
2021-11-13 03:41:32 -06:00
params: Vec<Type>,
results: Vec<Type>,
},
Loop {
2021-11-13 04:32:05 -06:00
top: BlockId,
out: BlockId,
2021-11-13 03:41:32 -06:00
params: Vec<Type>,
results: Vec<Type>,
},
If {
2021-11-13 04:32:05 -06:00
out: BlockId,
el: BlockId,
2021-11-13 03:41:32 -06:00
params: Vec<Type>,
results: Vec<Type>,
},
Else {
2021-11-13 04:32:05 -06:00
out: BlockId,
2021-11-13 03:41:32 -06:00
params: Vec<Type>,
results: Vec<Type>,
},
2021-11-13 03:17:53 -06:00
}
2021-11-13 04:32:05 -06: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 03:41:32 -06:00
body.blocks.push(Block::default());
Self {
2021-11-13 04:32:05 -06:00
module,
my_sig,
2021-11-13 03:41:32 -06:00
body,
ctrl_stack: vec![],
op_stack: vec![],
cur_block: 0,
}
2021-11-13 03:17:53 -06:00
}
2021-11-13 04:32:05 -06:00
fn handle_op(&mut self, op: Operator<'a>) -> Result<()> {
2021-11-13 03:17:53 -06:00
match op {
2021-11-13 04:32:05 -06:00
Operator::Unreachable
| Operator::Call { .. }
| Operator::LocalGet { .. }
| Operator::LocalSet { .. }
| Operator::LocalTee { .. } => self.emit(op.clone())?,
2021-11-13 05:38:47 -06:00
Operator::End => match self.ctrl_stack.pop() {
None => {
2021-11-13 04:32:05 -06:00
self.emit(Operator::Return)?;
}
2021-11-13 05:38:47 -06:00
Some(Frame::Block { out, .. }) | Some(Frame::Loop { out, .. }) => {
// No need to manipulate stack: assuming the input
// Wasm was validated properly, the `results`
// values must be on the top of the stack now, and
// they will remain so once we exit the block.
self.emit_branch(out);
self.cur_block = out;
}
_ => bail!("unsupported block type"),
},
Operator::Block { ty } => {
let (params, results) = self.block_params_and_results(ty);
let out = self.create_block();
self.ctrl_stack.push(Frame::Block {
out,
params,
results,
});
2021-11-13 04:32:05 -06:00
}
2021-11-13 03:41:32 -06:00
_ => bail!("Unsupported operator: {:?}", op),
2021-11-13 03:17:53 -06:00
}
Ok(())
}
2021-11-13 03:41:32 -06:00
2021-11-13 05:38:47 -06:00
fn create_block(&mut self) -> BlockId {
let id = self.body.blocks.len() as BlockId;
self.body.blocks.push(Block::default());
id
}
fn block_params_and_results(&self, ty: TypeOrFuncType) -> (Vec<Type>, Vec<Type>) {
match ty {
TypeOrFuncType::Type(ret_ty) => (vec![], vec![ret_ty]),
TypeOrFuncType::FuncType(sig_idx) => {
let sig = &self.module.signatures[sig_idx as SignatureId];
(
Vec::from(sig.params.clone()),
Vec::from(sig.returns.clone()),
)
}
}
}
fn emit_branch(&mut self, target: BlockId) {
let block = self.cur_block;
self.body.blocks[block].terminator = Terminator::Br { target };
}
2021-11-13 04:32:05 -06:00
fn emit(&mut self, op: Operator<'a>) -> Result<()> {
2021-11-13 03:41:32 -06:00
let block = self.cur_block;
let inst = self.body.blocks[block].insts.len() as InstId;
2021-11-13 04:32:05 -06: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 03:41:32 -06:00
}
2021-11-13 04:32:05 -06:00
inputs.reverse();
let mut outputs = vec![];
for output in op_outputs(self.module, &self.body.locals[..], &op)?.into_iter() {
2021-11-13 03:41:32 -06:00
let val = self.body.values.len() as ValueId;
2021-11-13 04:32:05 -06:00
outputs.push(val);
2021-11-13 03:41:32 -06: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 03:17:53 -06:00
}