waffle/src/frontend.rs

528 lines
18 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 11:38:47 +00:00
use wasmparser::{
ImportSectionEntryType, Operator, Parser, Payload, Type, TypeDef, TypeOrFuncType,
};
2021-11-13 11:49:19 +00:00
pub fn wasm_to_ir(bytes: &[u8]) -> Result<Module<'_>> {
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()?;
2021-11-13 11:49:19 +00:00
if let TypeDef::Func(fty) = ty {
module.signatures.push(fty);
}
}
}
2021-11-13 08:59:15 +00:00
Payload::ImportSection(mut reader) => {
for _ in 0..reader.get_count() {
2021-11-13 11:49:19 +00: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 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();
2021-11-13 11:49:19 +00:00
let body = parse_body(module, my_sig, body)?;
2021-11-13 10:32:05 +00:00
match &mut module.funcs[func_idx] {
2021-11-13 11:49:19 +00:00
FuncDecl::Body(_, ref mut existing_body) => {
2021-11-13 10:32:05 +00:00
*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 11:49:19 +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 22:13:31 +00:00
cur_block: Option<BlockId>,
2021-11-13 09:41:32 +00:00
ctrl_stack: Vec<Frame>,
op_stack: Vec<ValueId>,
}
2021-11-13 22:13:31 +00:00
#[derive(Clone, Debug)]
2021-11-13 09:41:32 +00:00
enum Frame {
Block {
2021-11-13 21:49:57 +00:00
start_depth: usize,
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 21:49:57 +00:00
start_depth: usize,
header: BlockId,
2021-11-13 10:32:05 +00:00
out: BlockId,
2021-11-13 09:41:32 +00:00
params: Vec<Type>,
results: Vec<Type>,
},
If {
2021-11-13 21:49:57 +00:00
start_depth: usize,
2021-11-13 10:32:05 +00:00
out: BlockId,
el: BlockId,
2021-11-13 21:49:57 +00:00
param_values: Vec<ValueId>,
2021-11-13 09:41:32 +00:00
params: Vec<Type>,
results: Vec<Type>,
},
Else {
2021-11-13 21:49:57 +00:00
start_depth: usize,
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 21:49:57 +00:00
impl Frame {
fn start_depth(&self) -> usize {
match self {
Frame::Block { start_depth, .. }
| Frame::Loop { start_depth, .. }
| Frame::If { start_depth, .. }
| Frame::Else { start_depth, .. } => *start_depth,
}
}
fn br_args(&self) -> &[Type] {
match self {
Frame::Block { results, .. }
| Frame::If { results, .. }
| Frame::Else { results, .. } => &results[..],
Frame::Loop { params, .. } => &params[..],
}
}
fn br_target(&self) -> BlockId {
match self {
Frame::Block { out, .. } => *out,
Frame::Loop { header, .. } => *header,
Frame::If { out, .. } | Frame::Else { out, .. } => *out,
}
}
}
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![],
2021-11-13 22:13:31 +00:00
cur_block: Some(0),
2021-11-13 09:41:32 +00:00
}
2021-11-13 09:17:53 +00:00
}
2021-11-13 21:49:57 +00:00
fn add_block_params(&mut self, block: BlockId, tys: &[Type]) {
self.body.blocks[block].params.extend_from_slice(tys);
}
2021-11-13 10:32:05 +00:00
fn handle_op(&mut self, op: Operator<'a>) -> Result<()> {
2021-11-13 21:49:57 +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())?,
2021-11-13 11:38:47 +00:00
Operator::End => match self.ctrl_stack.pop() {
None => {
2021-11-13 10:32:05 +00:00
self.emit(Operator::Return)?;
}
2021-11-13 21:49:57 +00:00
Some(Frame::Block {
start_depth,
out,
results,
..
})
| Some(Frame::Loop {
start_depth,
out,
results,
..
}) => {
// Generate a branch to the out-block with
// blockparams for the results.
let result_values = self.op_stack.split_off(results.len());
self.emit_branch(out, &result_values[..]);
assert_eq!(self.op_stack.len(), start_depth);
2021-11-13 22:13:31 +00:00
self.cur_block = Some(out);
2021-11-13 21:49:57 +00:00
self.push_block_params(&results[..]);
}
Some(Frame::If {
start_depth,
out,
el,
param_values,
results,
..
}) => {
// Generate a branch to the out-block with
// blockparams for the results.
let result_values = self.op_stack.split_off(results.len());
self.emit_branch(out, &result_values[..]);
// No `else`, so we need to generate a trivial
// branch in the else-block. If the if-block-type
// has results, they must be exactly the params.
let else_result_values = param_values;
assert_eq!(else_result_values.len(), results.len());
self.emit_branch(el, &else_result_values[..]);
assert_eq!(self.op_stack.len(), start_depth);
2021-11-13 22:13:31 +00:00
self.cur_block = Some(out);
2021-11-13 21:49:57 +00:00
self.push_block_params(&results[..]);
}
Some(Frame::Else {
out,
results,
start_depth,
..
}) => {
// Generate a branch to the out-block with
// blockparams for the results.
let result_values = self.op_stack.split_off(results.len());
assert_eq!(self.op_stack.len(), start_depth);
self.emit_branch(out, &result_values[..]);
2021-11-13 22:13:31 +00:00
self.cur_block = Some(out);
2021-11-13 21:49:57 +00:00
self.push_block_params(&results[..]);
2021-11-13 11:38:47 +00:00
}
},
Operator::Block { ty } => {
2021-11-13 21:49:57 +00:00
let (params, results) = self.block_params_and_results(*ty);
2021-11-13 11:38:47 +00:00
let out = self.create_block();
2021-11-13 21:49:57 +00:00
self.add_block_params(out, &results[..]);
let start_depth = self.op_stack.len() - params.len();
2021-11-13 11:38:47 +00:00
self.ctrl_stack.push(Frame::Block {
2021-11-13 21:49:57 +00:00
start_depth,
out,
params,
results,
});
}
Operator::Loop { ty } => {
let (params, results) = self.block_params_and_results(*ty);
let header = self.create_block();
self.add_block_params(header, &params[..]);
let initial_args = self.op_stack.split_off(params.len());
let start_depth = self.op_stack.len();
self.emit_branch(header, &initial_args[..]);
2021-11-13 22:13:31 +00:00
self.cur_block = Some(header);
2021-11-13 21:49:57 +00:00
self.push_block_params(&params[..]);
let out = self.create_block();
self.ctrl_stack.push(Frame::Loop {
start_depth,
header,
2021-11-13 11:38:47 +00:00
out,
params,
results,
});
2021-11-13 10:32:05 +00:00
}
2021-11-13 21:49:57 +00:00
Operator::If { ty } => {
let (params, results) = self.block_params_and_results(*ty);
let if_true = self.create_block();
let if_false = self.create_block();
let join = self.create_block();
self.add_block_params(join, &results[..]);
let cond = self.op_stack.pop().unwrap();
let param_values = self.op_stack[self.op_stack.len() - params.len()..].to_vec();
let start_depth = self.op_stack.len();
self.ctrl_stack.push(Frame::If {
start_depth,
out: join,
el: if_false,
param_values,
params,
results,
});
2021-11-13 22:13:31 +00:00
self.cur_block = Some(if_true);
2021-11-13 21:49:57 +00:00
self.emit_cond_branch(cond, if_true, &[], if_false, &[]);
}
Operator::Else => {
if let Frame::If {
start_depth,
out,
el,
param_values,
params,
results,
} = self.ctrl_stack.pop().unwrap()
{
let if_results = self.op_stack.split_off(results.len());
self.emit_branch(out, &if_results[..]);
self.op_stack.extend(param_values);
self.ctrl_stack.push(Frame::Else {
start_depth,
out,
params,
results,
});
2021-11-13 22:13:31 +00:00
self.cur_block = Some(el);
2021-11-13 21:49:57 +00:00
} else {
bail!("Else without If on top of frame stack");
}
}
Operator::Br { relative_depth } | Operator::BrIf { relative_depth } => {
let cond = match &op {
Operator::Br { .. } => None,
Operator::BrIf { .. } => Some(self.op_stack.pop().unwrap()),
_ => unreachable!(),
};
// Get the frame we're branching to.
2021-11-13 22:13:31 +00:00
let frame = self.relative_frame(*relative_depth).clone();
2021-11-13 21:49:57 +00:00
// Get the args off the stack.
let args = self.op_stack.split_off(frame.br_args().len());
// Finally, generate the branch itself.
match cond {
None => {
self.emit_branch(frame.br_target(), &args[..]);
2021-11-13 22:13:31 +00:00
self.cur_block = None;
2021-11-13 21:49:57 +00:00
}
Some(cond) => {
let cont = self.create_block();
self.emit_cond_branch(cond, frame.br_target(), &args[..], cont, &[]);
2021-11-13 22:13:31 +00:00
self.cur_block = Some(cont);
2021-11-13 21:49:57 +00:00
}
}
}
2021-11-13 22:13:31 +00:00
Operator::BrTable { table } => {
// Get the selector index.
let index = self.op_stack.pop().unwrap();
// Get the signature of the default frame; this tells
// us the signature of all frames (since wasmparser
// validates the input for us). Pop that many args.
let default_frame = self.relative_frame(table.default());
let default_term_target = default_frame.br_target();
let arg_len = default_frame.br_args().len();
let args = self.op_stack.split_off(arg_len);
// Generate a branch terminator with the same args for
// every branch target.
let mut term_targets = vec![];
for target in table.targets() {
let target = target?;
let frame = self.relative_frame(target);
assert_eq!(frame.br_args().len(), args.len());
let block = frame.br_target();
term_targets.push(block);
}
self.emit_br_table(index, default_term_target, &term_targets[..], &args[..]);
}
2021-11-13 21:49:57 +00:00
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 11:38:47 +00: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()),
)
}
}
}
2021-11-13 22:13:31 +00:00
fn relative_frame(&self, relative_depth: u32) -> &Frame {
&self.ctrl_stack[self.ctrl_stack.len() - 1 - relative_depth as usize]
}
2021-11-13 21:49:57 +00:00
fn emit_branch(&mut self, target: BlockId, args: &[ValueId]) {
2021-11-13 22:13:31 +00:00
if let Some(block) = self.cur_block {
let args = args.iter().map(|&val| Operand::Value(val)).collect();
let target = BlockTarget {
block: target,
args,
};
self.body.blocks[block].terminator = Terminator::Br { target };
}
2021-11-13 11:38:47 +00:00
}
2021-11-13 21:49:57 +00:00
fn emit_cond_branch(
&mut self,
cond: ValueId,
if_true: BlockId,
if_true_args: &[ValueId],
if_false: BlockId,
if_false_args: &[ValueId],
) {
2021-11-13 22:13:31 +00:00
if let Some(block) = self.cur_block {
let if_true_args = if_true_args
.iter()
.map(|&val| Operand::Value(val))
.collect();
let if_false_args = if_false_args
.iter()
.map(|&val| Operand::Value(val))
.collect();
self.body.blocks[block].terminator = Terminator::CondBr {
cond: Operand::Value(cond),
if_true: BlockTarget {
block: if_true,
args: if_true_args,
},
if_false: BlockTarget {
block: if_false,
args: if_false_args,
},
};
}
}
fn emit_br_table(
&mut self,
index: ValueId,
default_target: BlockId,
indexed_targets: &[BlockId],
args: &[ValueId],
) {
if let Some(block) = self.cur_block {
let args: Vec<Operand<'a>> = args.iter().map(|&arg| Operand::Value(arg)).collect();
let targets = indexed_targets
.iter()
.map(|&block| BlockTarget {
block,
args: args.clone(),
})
.collect();
let default = BlockTarget {
block: default_target,
args: args.clone(),
};
self.body.blocks[block].terminator = Terminator::Select {
value: Operand::Value(index),
targets,
default,
};
}
2021-11-13 21:49:57 +00:00
}
fn push_block_params(&mut self, tys: &[Type]) {
2021-11-13 22:13:31 +00:00
assert_eq!(tys, self.body.blocks[self.cur_block.unwrap()].params);
2021-11-13 21:49:57 +00:00
for (i, &ty) in tys.iter().enumerate() {
let value_id = self.body.values.len() as ValueId;
self.body.values.push(ValueDef {
2021-11-13 22:13:31 +00:00
kind: ValueKind::BlockParam(self.cur_block.unwrap(), i),
2021-11-13 21:49:57 +00:00
ty,
});
self.op_stack.push(value_id);
}
}
2021-11-13 10:32:05 +00:00
fn emit(&mut self, op: Operator<'a>) -> Result<()> {
2021-11-13 22:13:31 +00:00
if let Some(block) = self.cur_block {
let inst = self.body.blocks[block].insts.len() as InstId;
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));
}
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,
});
self.op_stack.push(val);
}
2021-11-13 10:32:05 +00:00
2021-11-13 22:13:31 +00:00
self.body.blocks[block].insts.push(Inst {
operator: op,
outputs,
inputs,
2021-11-13 09:41:32 +00:00
});
}
Ok(())
}
2021-11-13 09:17:53 +00:00
}