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-12-11 23:46:14 -06:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2021-11-13 02:52:35 -06:00
|
|
|
use crate::ir::*;
|
2021-12-11 23:46:14 -06:00
|
|
|
use crate::op_traits::{op_effects, op_inputs, op_outputs};
|
|
|
|
use crate::ops::Operator;
|
2021-11-13 03:41:32 -06:00
|
|
|
use anyhow::{bail, Result};
|
2021-12-11 23:46:14 -06:00
|
|
|
use fxhash::{FxHashMap, FxHashSet};
|
2021-11-13 02:56:49 -06:00
|
|
|
use log::trace;
|
2021-11-13 05:38:47 -06:00
|
|
|
use wasmparser::{
|
2021-12-11 23:46:14 -06:00
|
|
|
Ieee32, Ieee64, ImportSectionEntryType, Parser, Payload, Type, TypeDef, TypeOrFuncType,
|
2021-11-13 05:38:47 -06:00
|
|
|
};
|
2021-11-13 02:52:35 -06:00
|
|
|
|
2021-11-13 05:49:19 -06:00
|
|
|
pub fn wasm_to_ir(bytes: &[u8]) -> Result<Module<'_>> {
|
2021-11-13 02:52:35 -06:00
|
|
|
let mut module = Module::default();
|
2021-11-14 02:00:34 -06:00
|
|
|
module.orig_bytes = bytes;
|
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)?;
|
2021-11-13 02:52:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-11-13 02:52:35 -06:00
|
|
|
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:52:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 02:59:15 -06:00
|
|
|
Payload::ImportSection(mut reader) => {
|
|
|
|
for _ in 0..reader.get_count() {
|
2021-11-13 18:31:11 -06:00
|
|
|
match reader.read()?.ty {
|
|
|
|
ImportSectionEntryType::Function(sig_idx) => {
|
|
|
|
module.funcs.push(FuncDecl::Import(sig_idx as SignatureId));
|
|
|
|
*next_func += 1;
|
|
|
|
}
|
|
|
|
ImportSectionEntryType::Global(ty) => {
|
|
|
|
module.globals.push(ty.content_type);
|
|
|
|
}
|
|
|
|
ImportSectionEntryType::Table(ty) => {
|
|
|
|
module.tables.push(ty.element_type);
|
|
|
|
}
|
|
|
|
_ => {}
|
2021-11-13 02:59:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 18:31:11 -06:00
|
|
|
Payload::GlobalSection(mut reader) => {
|
|
|
|
for _ in 0..reader.get_count() {
|
|
|
|
let global = reader.read()?;
|
|
|
|
module.globals.push(global.ty.content_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Payload::TableSection(mut reader) => {
|
|
|
|
for _ in 0..reader.get_count() {
|
|
|
|
let table = reader.read()?;
|
|
|
|
module.tables.push(table.element_type);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2021-11-13 02:52:35 -06:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-13 03:10:22 -06:00
|
|
|
|
2021-11-22 01:12:07 -06:00
|
|
|
fn parse_body<'a>(
|
|
|
|
module: &'a Module,
|
2021-11-13 04:32:05 -06:00
|
|
|
my_sig: SignatureId,
|
2021-11-22 01:12:07 -06:00
|
|
|
body: wasmparser::FunctionBody,
|
|
|
|
) -> Result<FunctionBody> {
|
|
|
|
let mut ret: FunctionBody = FunctionBody::default();
|
2021-11-13 19:47:33 -06:00
|
|
|
|
|
|
|
for ¶m in &module.signatures[my_sig].params[..] {
|
|
|
|
ret.locals.push(param);
|
|
|
|
}
|
|
|
|
|
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-12-11 23:46:14 -06:00
|
|
|
let locals = ret.locals.clone();
|
2021-11-13 03:10:22 -06:00
|
|
|
|
2021-11-13 18:44:53 -06:00
|
|
|
trace!(
|
|
|
|
"Parsing function body: locals = {:?} sig = {:?}",
|
|
|
|
ret.locals,
|
|
|
|
module.signatures[my_sig]
|
|
|
|
);
|
|
|
|
|
2021-11-13 05:49:19 -06:00
|
|
|
let mut builder = FunctionBodyBuilder::new(module, my_sig, &mut ret);
|
2021-12-11 23:46:14 -06:00
|
|
|
builder.locals.seal_block_preds(0, &mut builder.body);
|
|
|
|
builder.locals.start_block(0);
|
2021-11-14 00:25:27 -06:00
|
|
|
|
|
|
|
for (arg_idx, &arg_ty) in module.signatures[my_sig].params.iter().enumerate() {
|
|
|
|
let local_idx = arg_idx as LocalId;
|
2021-12-11 23:46:14 -06:00
|
|
|
let value = builder.body.add_value(ValueDef::Arg(arg_idx), Some(arg_ty));
|
2021-11-14 00:25:27 -06:00
|
|
|
trace!("defining local {} to value {}", local_idx, value);
|
2021-12-11 23:46:14 -06:00
|
|
|
builder.locals.declare(local_idx, arg_ty);
|
|
|
|
builder.locals.set(local_idx, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
let n_args = module.signatures[my_sig].params.len();
|
|
|
|
for (offset, local_ty) in locals.into_iter().enumerate() {
|
|
|
|
let local_idx = (n_args + offset) as u32;
|
|
|
|
builder.locals.declare(local_idx, local_ty);
|
2021-11-14 00:25:27 -06:00
|
|
|
}
|
|
|
|
|
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 18:44:53 -06:00
|
|
|
if builder.cur_block.is_some() {
|
2021-12-11 23:46:14 -06:00
|
|
|
builder.handle_op(wasmparser::Operator::Return)?;
|
2021-11-13 18:44:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
trace!("Final function body:{:?}", ret);
|
|
|
|
|
2021-11-13 03:10:22 -06:00
|
|
|
Ok(ret)
|
|
|
|
}
|
2021-11-13 03:17:53 -06:00
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
struct LocalTracker {
|
|
|
|
/// Types of locals, as declared.
|
|
|
|
types: FxHashMap<LocalId, Type>,
|
|
|
|
/// The current block.
|
|
|
|
cur_block: Option<BlockId>,
|
|
|
|
/// Is the given block sealed?
|
|
|
|
block_sealed: FxHashSet<BlockId>,
|
|
|
|
/// Block predecessors.
|
|
|
|
block_preds: FxHashMap<BlockId, Vec<BlockId>>,
|
|
|
|
/// The local-to-value mapping at the start of a block.
|
|
|
|
block_start: FxHashMap<BlockId, FxHashMap<LocalId, Value>>,
|
|
|
|
/// The local-to-value mapping at the end of a block.
|
|
|
|
block_end: FxHashMap<BlockId, FxHashMap<LocalId, Value>>,
|
|
|
|
in_cur_block: FxHashMap<LocalId, Value>,
|
|
|
|
incomplete_phis: FxHashMap<BlockId, Vec<(LocalId, Value)>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalTracker {
|
|
|
|
pub fn declare(&mut self, local: LocalId, ty: Type) {
|
|
|
|
let was_present = self.types.insert(local, ty).is_some();
|
|
|
|
assert!(!was_present);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_pred(&mut self, block: BlockId, pred: BlockId) {
|
|
|
|
assert!(!self.block_sealed.contains(&block));
|
|
|
|
self.block_preds
|
|
|
|
.entry(block)
|
|
|
|
.or_insert_with(|| vec![])
|
|
|
|
.push(pred);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start_block(&mut self, block: BlockId) {
|
|
|
|
self.finish_block();
|
|
|
|
self.cur_block = Some(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finish_block(&mut self) {
|
|
|
|
if let Some(block) = self.cur_block {
|
|
|
|
let mapping = std::mem::take(&mut self.in_cur_block);
|
|
|
|
self.block_end.insert(block, mapping);
|
|
|
|
}
|
|
|
|
self.cur_block = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn seal_block_preds(&mut self, block: BlockId, body: &mut FunctionBody) {
|
|
|
|
let not_sealed = self.block_sealed.insert(block);
|
|
|
|
assert!(not_sealed);
|
|
|
|
for (local, phi_value) in self
|
|
|
|
.incomplete_phis
|
|
|
|
.remove(&block)
|
|
|
|
.unwrap_or_else(|| vec![])
|
|
|
|
{
|
|
|
|
self.compute_blockparam(body, block, local, phi_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_sealed(&self, block: BlockId) -> bool {
|
|
|
|
self.block_sealed.contains(&block)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(&mut self, local: LocalId, value: Value) {
|
|
|
|
assert!(self.cur_block.is_some());
|
|
|
|
debug_assert!(self.is_sealed(self.cur_block.unwrap()));
|
|
|
|
self.in_cur_block.insert(local, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_in_block(
|
|
|
|
&mut self,
|
|
|
|
body: &mut FunctionBody,
|
|
|
|
at_block: BlockId,
|
|
|
|
local: LocalId,
|
|
|
|
) -> Value {
|
|
|
|
let ty = body.locals[local as usize];
|
|
|
|
|
|
|
|
if self.cur_block.unwrap() == at_block {
|
|
|
|
if let Some(&value) = self.in_cur_block.get(&local) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_sealed(at_block) {
|
|
|
|
if let Some(end_mapping) = self.block_end.get(&at_block) {
|
|
|
|
if let Some(&value) = end_mapping.get(&local) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self
|
|
|
|
.block_preds
|
|
|
|
.get(&at_block)
|
|
|
|
.map(|preds| preds.is_empty())
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
|
|
|
return self.create_default_value(body, ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
let placeholder = body.add_placeholder(ty);
|
|
|
|
self.block_end
|
|
|
|
.entry(at_block)
|
|
|
|
.or_insert_with(|| FxHashMap::default())
|
|
|
|
.insert(local, placeholder);
|
|
|
|
self.compute_blockparam(body, at_block, local, placeholder);
|
|
|
|
placeholder
|
|
|
|
} else {
|
|
|
|
let placeholder = body.add_placeholder(ty);
|
|
|
|
self.block_end
|
|
|
|
.entry(at_block)
|
|
|
|
.or_insert_with(|| FxHashMap::default())
|
|
|
|
.insert(local, placeholder);
|
|
|
|
self.incomplete_phis
|
|
|
|
.entry(at_block)
|
|
|
|
.or_insert_with(|| vec![])
|
|
|
|
.push((local, placeholder));
|
|
|
|
|
|
|
|
placeholder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&mut self, body: &mut FunctionBody, local: LocalId) -> Value {
|
|
|
|
assert!(self.cur_block.is_some());
|
|
|
|
debug_assert!(self.is_sealed(self.cur_block.unwrap()));
|
|
|
|
let block = self.cur_block.unwrap();
|
|
|
|
assert!((local as usize) < body.locals.len());
|
|
|
|
self.get_in_block(body, block, local)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_default_value(&mut self, body: &mut FunctionBody, ty: Type) -> Value {
|
|
|
|
match ty {
|
|
|
|
Type::I32 => body.add_value(
|
|
|
|
ValueDef::Operator(Operator::I32Const { value: 0 }, vec![]),
|
|
|
|
Some(ty),
|
|
|
|
),
|
|
|
|
Type::I64 => body.add_value(
|
|
|
|
ValueDef::Operator(Operator::I64Const { value: 0 }, vec![]),
|
|
|
|
Some(ty),
|
|
|
|
),
|
|
|
|
Type::F32 => body.add_value(
|
|
|
|
ValueDef::Operator(
|
|
|
|
Operator::F32Const {
|
|
|
|
value: Ieee32::from_bits(0),
|
|
|
|
},
|
|
|
|
vec![],
|
|
|
|
),
|
|
|
|
Some(ty),
|
|
|
|
),
|
|
|
|
Type::F64 => body.add_value(
|
|
|
|
ValueDef::Operator(
|
|
|
|
Operator::F64Const {
|
|
|
|
value: Ieee64::from_bits(0),
|
|
|
|
},
|
|
|
|
vec![],
|
|
|
|
),
|
|
|
|
Some(ty),
|
|
|
|
),
|
|
|
|
_ => todo!("unsupported type: {:?}", ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_blockparam(
|
|
|
|
&mut self,
|
|
|
|
body: &mut FunctionBody,
|
|
|
|
block: BlockId,
|
|
|
|
local: LocalId,
|
|
|
|
value: Value,
|
|
|
|
) {
|
|
|
|
let mut results: Vec<Value> = vec![];
|
|
|
|
let preds = self
|
|
|
|
.block_preds
|
|
|
|
.get(&block)
|
|
|
|
.cloned()
|
|
|
|
.unwrap_or_else(|| vec![]);
|
|
|
|
for pred in preds {
|
|
|
|
let pred_value = self.get_in_block(body, pred, local);
|
|
|
|
results.push(pred_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut non_self = results.iter().filter(|&&v| v != value);
|
|
|
|
let trivial_alias = match non_self.next() {
|
|
|
|
None => None,
|
|
|
|
Some(&first) if non_self.all(|&v| v == first) => Some(first),
|
|
|
|
Some(_) => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(v) = trivial_alias {
|
|
|
|
body.set_alias(value, v);
|
|
|
|
} else {
|
|
|
|
body.replace_placeholder_with_blockparam(block, value);
|
|
|
|
for (i, (pred, result)) in self
|
|
|
|
.block_preds
|
|
|
|
.get(&block)
|
|
|
|
.cloned()
|
|
|
|
.unwrap_or_else(|| vec![])
|
|
|
|
.into_iter()
|
|
|
|
.zip(results.into_iter())
|
|
|
|
.enumerate()
|
|
|
|
{
|
|
|
|
let index = body.blocks[block].pos_in_pred_succ[i];
|
|
|
|
body.blocks[pred].terminator.update_target(index, |target| {
|
|
|
|
target.args.push(result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2021-11-22 01:12:07 -06:00
|
|
|
body: &'b mut FunctionBody,
|
2021-12-11 23:46:14 -06:00
|
|
|
locals: LocalTracker,
|
2021-11-13 16:13:31 -06:00
|
|
|
cur_block: Option<BlockId>,
|
2021-11-13 03:41:32 -06:00
|
|
|
ctrl_stack: Vec<Frame>,
|
2021-11-21 19:15:26 -06:00
|
|
|
op_stack: Vec<(Type, Value)>,
|
2021-11-13 03:41:32 -06:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:13:31 -06:00
|
|
|
#[derive(Clone, Debug)]
|
2021-11-13 03:41:32 -06:00
|
|
|
enum Frame {
|
|
|
|
Block {
|
2021-11-13 15:49:57 -06:00
|
|
|
start_depth: usize,
|
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 15:49:57 -06:00
|
|
|
start_depth: usize,
|
|
|
|
header: BlockId,
|
2021-11-13 04:32:05 -06:00
|
|
|
out: BlockId,
|
2021-11-13 03:41:32 -06:00
|
|
|
params: Vec<Type>,
|
|
|
|
results: Vec<Type>,
|
|
|
|
},
|
|
|
|
If {
|
2021-11-13 15:49:57 -06:00
|
|
|
start_depth: usize,
|
2021-11-13 04:32:05 -06:00
|
|
|
out: BlockId,
|
|
|
|
el: BlockId,
|
2021-11-21 19:15:26 -06:00
|
|
|
param_values: Vec<(Type, Value)>,
|
2021-11-13 03:41:32 -06:00
|
|
|
params: Vec<Type>,
|
|
|
|
results: Vec<Type>,
|
|
|
|
},
|
|
|
|
Else {
|
2021-11-13 15:49:57 -06:00
|
|
|
start_depth: usize,
|
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 15:49:57 -06: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, .. } => ¶ms[..],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 19:47:33 -06:00
|
|
|
|
|
|
|
fn out(&self) -> BlockId {
|
|
|
|
match self {
|
|
|
|
Frame::Block { out, .. }
|
|
|
|
| Frame::Loop { out, .. }
|
|
|
|
| Frame::If { out, .. }
|
|
|
|
| Frame::Else { out, .. } => *out,
|
|
|
|
}
|
|
|
|
}
|
2021-12-11 23:46:14 -06:00
|
|
|
|
|
|
|
fn params(&self) -> &[Type] {
|
|
|
|
match self {
|
|
|
|
Frame::Block { params, .. }
|
|
|
|
| Frame::Loop { params, .. }
|
|
|
|
| Frame::If { params, .. }
|
|
|
|
| Frame::Else { params, .. } => ¶ms[..],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn results(&self) -> &[Type] {
|
|
|
|
match self {
|
|
|
|
Frame::Block { results, .. }
|
|
|
|
| Frame::Loop { results, .. }
|
|
|
|
| Frame::If { results, .. }
|
|
|
|
| Frame::Else { results, .. } => &results[..],
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 15:49:57 -06:00
|
|
|
}
|
|
|
|
|
2021-11-13 04:32:05 -06:00
|
|
|
impl<'a, 'b> FunctionBodyBuilder<'a, 'b> {
|
2021-11-22 01:12:07 -06:00
|
|
|
fn new(module: &'b Module<'a>, my_sig: SignatureId, body: &'b mut FunctionBody) -> Self {
|
2021-11-13 03:41:32 -06:00
|
|
|
body.blocks.push(Block::default());
|
2021-11-13 18:44:53 -06:00
|
|
|
let mut ret = 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![],
|
2021-11-13 16:13:31 -06:00
|
|
|
cur_block: Some(0),
|
2021-12-11 23:46:14 -06:00
|
|
|
locals: LocalTracker::default(),
|
2021-11-13 18:44:53 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Push initial implicit Block.
|
2021-11-13 19:47:33 -06:00
|
|
|
let results = module.signatures[my_sig].returns.to_vec();
|
2021-12-11 23:46:14 -06:00
|
|
|
let out = ret.body.add_block();
|
2021-11-13 18:44:53 -06:00
|
|
|
ret.ctrl_stack.push(Frame::Block {
|
|
|
|
start_depth: 0,
|
|
|
|
out,
|
2021-11-13 19:47:33 -06:00
|
|
|
params: vec![],
|
2021-11-13 18:44:53 -06:00
|
|
|
results,
|
|
|
|
});
|
|
|
|
ret
|
2021-11-13 03:17:53 -06:00
|
|
|
}
|
|
|
|
|
2021-11-21 19:15:26 -06:00
|
|
|
fn pop_n(&mut self, n: usize) -> Vec<Value> {
|
2021-11-13 19:47:33 -06:00
|
|
|
let new_top = self.op_stack.len() - n;
|
|
|
|
let ret = self.op_stack[new_top..]
|
|
|
|
.iter()
|
|
|
|
.map(|(_ty, value)| *value)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
self.op_stack.truncate(new_top);
|
|
|
|
ret
|
2021-11-13 18:31:11 -06:00
|
|
|
}
|
|
|
|
|
2021-11-21 19:15:26 -06:00
|
|
|
fn pop_1(&mut self) -> Value {
|
2021-11-13 18:31:11 -06:00
|
|
|
self.op_stack.pop().unwrap().1
|
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
fn handle_op(&mut self, op: wasmparser::Operator<'a>) -> Result<()> {
|
2021-11-13 18:44:53 -06:00
|
|
|
trace!("handle_op: {:?}", op);
|
2021-11-13 19:47:33 -06:00
|
|
|
trace!("op_stack = {:?}", self.op_stack);
|
|
|
|
trace!("ctrl_stack = {:?}", self.ctrl_stack);
|
2021-11-14 00:25:27 -06:00
|
|
|
trace!("locals = {:?}", self.locals);
|
2021-11-13 15:49:57 -06:00
|
|
|
match &op {
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::Unreachable => {
|
2021-11-13 19:47:33 -06:00
|
|
|
if let Some(block) = self.cur_block {
|
|
|
|
self.body.blocks[block].terminator = Terminator::None;
|
2021-12-11 23:46:14 -06:00
|
|
|
self.locals.finish_block();
|
2021-11-13 19:47:33 -06:00
|
|
|
}
|
|
|
|
self.cur_block = None;
|
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::LocalGet { local_index } => {
|
2021-11-14 00:25:27 -06:00
|
|
|
let ty = self.body.locals[*local_index as usize];
|
2021-12-11 23:46:14 -06:00
|
|
|
let value = self.locals.get(&mut self.body, *local_index);
|
2021-11-14 00:25:27 -06:00
|
|
|
self.op_stack.push((ty, value));
|
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::LocalSet { local_index } => {
|
|
|
|
let (_, value) = self.op_stack.pop().unwrap();
|
|
|
|
self.locals.set(*local_index, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
wasmparser::Operator::LocalTee { local_index } => {
|
|
|
|
let (_ty, value) = *self.op_stack.last().unwrap();
|
|
|
|
self.locals.set(*local_index, value);
|
2021-11-14 00:25:27 -06:00
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::Call { .. }
|
|
|
|
| wasmparser::Operator::CallIndirect { .. }
|
|
|
|
| wasmparser::Operator::Select
|
|
|
|
| wasmparser::Operator::TypedSelect { .. }
|
|
|
|
| wasmparser::Operator::GlobalGet { .. }
|
|
|
|
| wasmparser::Operator::GlobalSet { .. }
|
|
|
|
| wasmparser::Operator::I32Load { .. }
|
|
|
|
| wasmparser::Operator::I64Load { .. }
|
|
|
|
| wasmparser::Operator::F32Load { .. }
|
|
|
|
| wasmparser::Operator::F64Load { .. }
|
|
|
|
| wasmparser::Operator::I32Load8S { .. }
|
|
|
|
| wasmparser::Operator::I32Load8U { .. }
|
|
|
|
| wasmparser::Operator::I32Load16S { .. }
|
|
|
|
| wasmparser::Operator::I32Load16U { .. }
|
|
|
|
| wasmparser::Operator::I64Load8S { .. }
|
|
|
|
| wasmparser::Operator::I64Load8U { .. }
|
|
|
|
| wasmparser::Operator::I64Load16S { .. }
|
|
|
|
| wasmparser::Operator::I64Load16U { .. }
|
|
|
|
| wasmparser::Operator::I64Load32S { .. }
|
|
|
|
| wasmparser::Operator::I64Load32U { .. }
|
|
|
|
| wasmparser::Operator::I32Store { .. }
|
|
|
|
| wasmparser::Operator::I64Store { .. }
|
|
|
|
| wasmparser::Operator::F32Store { .. }
|
|
|
|
| wasmparser::Operator::F64Store { .. }
|
|
|
|
| wasmparser::Operator::I32Store8 { .. }
|
|
|
|
| wasmparser::Operator::I32Store16 { .. }
|
|
|
|
| wasmparser::Operator::I64Store8 { .. }
|
|
|
|
| wasmparser::Operator::I64Store16 { .. }
|
|
|
|
| wasmparser::Operator::I64Store32 { .. }
|
|
|
|
| wasmparser::Operator::MemorySize { .. }
|
|
|
|
| wasmparser::Operator::MemoryGrow { .. }
|
|
|
|
| wasmparser::Operator::I32Const { .. }
|
|
|
|
| wasmparser::Operator::I64Const { .. }
|
|
|
|
| wasmparser::Operator::F32Const { .. }
|
|
|
|
| wasmparser::Operator::F64Const { .. }
|
|
|
|
| wasmparser::Operator::I32Eqz
|
|
|
|
| wasmparser::Operator::I32Eq
|
|
|
|
| wasmparser::Operator::I32Ne
|
|
|
|
| wasmparser::Operator::I32LtS
|
|
|
|
| wasmparser::Operator::I32LtU
|
|
|
|
| wasmparser::Operator::I32GtS
|
|
|
|
| wasmparser::Operator::I32GtU
|
|
|
|
| wasmparser::Operator::I32LeS
|
|
|
|
| wasmparser::Operator::I32LeU
|
|
|
|
| wasmparser::Operator::I32GeS
|
|
|
|
| wasmparser::Operator::I32GeU
|
|
|
|
| wasmparser::Operator::I64Eqz
|
|
|
|
| wasmparser::Operator::I64Eq
|
|
|
|
| wasmparser::Operator::I64Ne
|
|
|
|
| wasmparser::Operator::I64LtS
|
|
|
|
| wasmparser::Operator::I64LtU
|
|
|
|
| wasmparser::Operator::I64GtU
|
|
|
|
| wasmparser::Operator::I64GtS
|
|
|
|
| wasmparser::Operator::I64LeS
|
|
|
|
| wasmparser::Operator::I64LeU
|
|
|
|
| wasmparser::Operator::I64GeS
|
|
|
|
| wasmparser::Operator::I64GeU
|
|
|
|
| wasmparser::Operator::F32Eq
|
|
|
|
| wasmparser::Operator::F32Ne
|
|
|
|
| wasmparser::Operator::F32Lt
|
|
|
|
| wasmparser::Operator::F32Gt
|
|
|
|
| wasmparser::Operator::F32Le
|
|
|
|
| wasmparser::Operator::F32Ge
|
|
|
|
| wasmparser::Operator::F64Eq
|
|
|
|
| wasmparser::Operator::F64Ne
|
|
|
|
| wasmparser::Operator::F64Lt
|
|
|
|
| wasmparser::Operator::F64Gt
|
|
|
|
| wasmparser::Operator::F64Le
|
|
|
|
| wasmparser::Operator::F64Ge
|
|
|
|
| wasmparser::Operator::I32Clz
|
|
|
|
| wasmparser::Operator::I32Ctz
|
|
|
|
| wasmparser::Operator::I32Popcnt
|
|
|
|
| wasmparser::Operator::I32Add
|
|
|
|
| wasmparser::Operator::I32Sub
|
|
|
|
| wasmparser::Operator::I32Mul
|
|
|
|
| wasmparser::Operator::I32DivS
|
|
|
|
| wasmparser::Operator::I32DivU
|
|
|
|
| wasmparser::Operator::I32RemS
|
|
|
|
| wasmparser::Operator::I32RemU
|
|
|
|
| wasmparser::Operator::I32And
|
|
|
|
| wasmparser::Operator::I32Or
|
|
|
|
| wasmparser::Operator::I32Xor
|
|
|
|
| wasmparser::Operator::I32Shl
|
|
|
|
| wasmparser::Operator::I32ShrS
|
|
|
|
| wasmparser::Operator::I32ShrU
|
|
|
|
| wasmparser::Operator::I32Rotl
|
|
|
|
| wasmparser::Operator::I32Rotr
|
|
|
|
| wasmparser::Operator::I64Clz
|
|
|
|
| wasmparser::Operator::I64Ctz
|
|
|
|
| wasmparser::Operator::I64Popcnt
|
|
|
|
| wasmparser::Operator::I64Add
|
|
|
|
| wasmparser::Operator::I64Sub
|
|
|
|
| wasmparser::Operator::I64Mul
|
|
|
|
| wasmparser::Operator::I64DivS
|
|
|
|
| wasmparser::Operator::I64DivU
|
|
|
|
| wasmparser::Operator::I64RemS
|
|
|
|
| wasmparser::Operator::I64RemU
|
|
|
|
| wasmparser::Operator::I64And
|
|
|
|
| wasmparser::Operator::I64Or
|
|
|
|
| wasmparser::Operator::I64Xor
|
|
|
|
| wasmparser::Operator::I64Shl
|
|
|
|
| wasmparser::Operator::I64ShrS
|
|
|
|
| wasmparser::Operator::I64ShrU
|
|
|
|
| wasmparser::Operator::I64Rotl
|
|
|
|
| wasmparser::Operator::I64Rotr
|
|
|
|
| wasmparser::Operator::F32Abs
|
|
|
|
| wasmparser::Operator::F32Neg
|
|
|
|
| wasmparser::Operator::F32Ceil
|
|
|
|
| wasmparser::Operator::F32Floor
|
|
|
|
| wasmparser::Operator::F32Trunc
|
|
|
|
| wasmparser::Operator::F32Nearest
|
|
|
|
| wasmparser::Operator::F32Sqrt
|
|
|
|
| wasmparser::Operator::F32Add
|
|
|
|
| wasmparser::Operator::F32Sub
|
|
|
|
| wasmparser::Operator::F32Mul
|
|
|
|
| wasmparser::Operator::F32Div
|
|
|
|
| wasmparser::Operator::F32Min
|
|
|
|
| wasmparser::Operator::F32Max
|
|
|
|
| wasmparser::Operator::F32Copysign
|
|
|
|
| wasmparser::Operator::F64Abs
|
|
|
|
| wasmparser::Operator::F64Neg
|
|
|
|
| wasmparser::Operator::F64Ceil
|
|
|
|
| wasmparser::Operator::F64Floor
|
|
|
|
| wasmparser::Operator::F64Trunc
|
|
|
|
| wasmparser::Operator::F64Nearest
|
|
|
|
| wasmparser::Operator::F64Sqrt
|
|
|
|
| wasmparser::Operator::F64Add
|
|
|
|
| wasmparser::Operator::F64Sub
|
|
|
|
| wasmparser::Operator::F64Mul
|
|
|
|
| wasmparser::Operator::F64Div
|
|
|
|
| wasmparser::Operator::F64Min
|
|
|
|
| wasmparser::Operator::F64Max
|
|
|
|
| wasmparser::Operator::F64Copysign
|
|
|
|
| wasmparser::Operator::I32WrapI64
|
|
|
|
| wasmparser::Operator::I32TruncF32S
|
|
|
|
| wasmparser::Operator::I32TruncF32U
|
|
|
|
| wasmparser::Operator::I32TruncF64S
|
|
|
|
| wasmparser::Operator::I32TruncF64U
|
|
|
|
| wasmparser::Operator::I64ExtendI32S
|
|
|
|
| wasmparser::Operator::I64ExtendI32U
|
|
|
|
| wasmparser::Operator::I64TruncF32S
|
|
|
|
| wasmparser::Operator::I64TruncF32U
|
|
|
|
| wasmparser::Operator::I64TruncF64S
|
|
|
|
| wasmparser::Operator::I64TruncF64U
|
|
|
|
| wasmparser::Operator::F32ConvertI32S
|
|
|
|
| wasmparser::Operator::F32ConvertI32U
|
|
|
|
| wasmparser::Operator::F32ConvertI64S
|
|
|
|
| wasmparser::Operator::F32ConvertI64U
|
|
|
|
| wasmparser::Operator::F32DemoteF64
|
|
|
|
| wasmparser::Operator::F64ConvertI32S
|
|
|
|
| wasmparser::Operator::F64ConvertI32U
|
|
|
|
| wasmparser::Operator::F64ConvertI64S
|
|
|
|
| wasmparser::Operator::F64ConvertI64U
|
|
|
|
| wasmparser::Operator::F64PromoteF32
|
|
|
|
| wasmparser::Operator::I32Extend8S
|
|
|
|
| wasmparser::Operator::I32Extend16S
|
|
|
|
| wasmparser::Operator::I64Extend8S
|
|
|
|
| wasmparser::Operator::I64Extend16S
|
|
|
|
| wasmparser::Operator::I64Extend32S
|
|
|
|
| wasmparser::Operator::I32TruncSatF32S
|
|
|
|
| wasmparser::Operator::I32TruncSatF32U
|
|
|
|
| wasmparser::Operator::I32TruncSatF64S
|
|
|
|
| wasmparser::Operator::I32TruncSatF64U
|
|
|
|
| wasmparser::Operator::I64TruncSatF32S
|
|
|
|
| wasmparser::Operator::I64TruncSatF32U
|
|
|
|
| wasmparser::Operator::I64TruncSatF64S
|
|
|
|
| wasmparser::Operator::I64TruncSatF64U
|
|
|
|
| wasmparser::Operator::F32ReinterpretI32
|
|
|
|
| wasmparser::Operator::F64ReinterpretI64
|
|
|
|
| wasmparser::Operator::I32ReinterpretF32
|
|
|
|
| wasmparser::Operator::I64ReinterpretF64
|
|
|
|
| wasmparser::Operator::TableGet { .. }
|
|
|
|
| wasmparser::Operator::TableSet { .. }
|
|
|
|
| wasmparser::Operator::TableGrow { .. }
|
|
|
|
| wasmparser::Operator::TableSize { .. } => {
|
|
|
|
self.emit(Operator::try_from(&op).unwrap())?
|
2021-11-14 00:25:27 -06:00
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::Nop => {}
|
|
|
|
|
|
|
|
wasmparser::Operator::Drop => {
|
2021-11-13 18:31:11 -06:00
|
|
|
let _ = self.pop_1();
|
2021-11-13 17:09:47 -06:00
|
|
|
}
|
2021-11-13 04:32:05 -06:00
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::End if self.cur_block.is_none() => {
|
2021-11-13 19:47:33 -06:00
|
|
|
let frame = self.ctrl_stack.pop().unwrap();
|
|
|
|
self.op_stack.truncate(frame.start_depth());
|
|
|
|
self.cur_block = Some(frame.out());
|
2021-12-11 23:46:14 -06:00
|
|
|
self.push_block_params(frame.results().len());
|
|
|
|
self.locals.start_block(frame.out());
|
2021-11-13 19:47:33 -06:00
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::End => {
|
2021-11-13 19:47:33 -06:00
|
|
|
let frame = self.ctrl_stack.pop();
|
2021-12-11 23:46:14 -06:00
|
|
|
match &frame {
|
2021-11-13 19:47:33 -06:00
|
|
|
None => {
|
|
|
|
self.emit(Operator::Return)?;
|
|
|
|
}
|
|
|
|
Some(Frame::Block {
|
|
|
|
start_depth,
|
|
|
|
out,
|
2021-12-11 23:46:14 -06:00
|
|
|
ref results,
|
2021-11-13 19:47:33 -06:00
|
|
|
..
|
|
|
|
})
|
|
|
|
| Some(Frame::Loop {
|
|
|
|
start_depth,
|
|
|
|
out,
|
2021-12-11 23:46:14 -06:00
|
|
|
ref results,
|
2021-11-13 19:47:33 -06:00
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
// Generate a branch to the out-block with
|
|
|
|
// blockparams for the results.
|
|
|
|
if self.cur_block.is_some() {
|
|
|
|
let result_values = self.pop_n(results.len());
|
2021-12-11 23:46:14 -06:00
|
|
|
self.emit_branch(*out, &result_values[..]);
|
2021-11-13 19:47:33 -06:00
|
|
|
}
|
2021-12-11 23:46:14 -06:00
|
|
|
self.op_stack.truncate(*start_depth);
|
|
|
|
self.locals.finish_block();
|
|
|
|
// Seal the out-block: no more edges will be
|
|
|
|
// added to it. Also, if we're ending a loop,
|
|
|
|
// seal thea header: no more back-edges will
|
|
|
|
// be added to it.
|
|
|
|
self.locals.seal_block_preds(*out, &mut self.body);
|
|
|
|
if let Some(Frame::Loop { header, .. }) = &frame {
|
|
|
|
self.locals.seal_block_preds(*header, &mut self.body);
|
|
|
|
}
|
|
|
|
self.cur_block = Some(*out);
|
|
|
|
self.locals.start_block(*out);
|
|
|
|
self.push_block_params(results.len());
|
2021-11-13 19:47:33 -06:00
|
|
|
}
|
|
|
|
Some(Frame::If {
|
|
|
|
start_depth,
|
|
|
|
out,
|
|
|
|
el,
|
2021-12-11 23:46:14 -06:00
|
|
|
ref param_values,
|
|
|
|
ref results,
|
2021-11-13 19:47:33 -06:00
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
// Generate a branch to the out-block with
|
|
|
|
// blockparams for the results.
|
2021-12-11 23:46:14 -06:00
|
|
|
let result_values = self.pop_n(results.len());
|
|
|
|
self.emit_branch(*out, &result_values[..]);
|
|
|
|
self.locals.finish_block();
|
|
|
|
self.op_stack.truncate(*start_depth);
|
2021-11-13 19:47:33 -06:00
|
|
|
// 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());
|
|
|
|
let else_result_values = else_result_values
|
|
|
|
.iter()
|
|
|
|
.map(|(_ty, value)| *value)
|
|
|
|
.collect::<Vec<_>>();
|
2021-12-11 23:46:14 -06:00
|
|
|
self.locals.start_block(*el);
|
|
|
|
self.locals.finish_block();
|
|
|
|
self.emit_branch(*el, &else_result_values[..]);
|
|
|
|
assert_eq!(self.op_stack.len(), *start_depth);
|
|
|
|
self.cur_block = Some(*out);
|
|
|
|
self.locals.seal_block_preds(*out, &mut self.body);
|
|
|
|
self.locals.start_block(*out);
|
|
|
|
self.push_block_params(results.len());
|
2021-11-13 19:47:33 -06:00
|
|
|
}
|
|
|
|
Some(Frame::Else {
|
|
|
|
out,
|
2021-12-11 23:46:14 -06:00
|
|
|
ref results,
|
2021-11-13 19:47:33 -06:00
|
|
|
start_depth,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
// Generate a branch to the out-block with
|
|
|
|
// blockparams for the results.
|
2021-12-11 23:46:14 -06:00
|
|
|
let result_values = self.pop_n(results.len());
|
|
|
|
self.emit_branch(*out, &result_values[..]);
|
|
|
|
self.locals.finish_block();
|
|
|
|
self.op_stack.truncate(*start_depth);
|
|
|
|
self.cur_block = Some(*out);
|
|
|
|
self.locals.seal_block_preds(*out, &mut self.body);
|
|
|
|
self.locals.start_block(*out);
|
|
|
|
self.push_block_params(results.len());
|
2021-11-13 19:47:33 -06:00
|
|
|
}
|
2021-11-13 05:38:47 -06:00
|
|
|
}
|
2021-11-13 19:47:33 -06:00
|
|
|
}
|
2021-11-13 05:38:47 -06:00
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::Block { ty } => {
|
2021-11-13 15:49:57 -06:00
|
|
|
let (params, results) = self.block_params_and_results(*ty);
|
2021-12-11 23:46:14 -06:00
|
|
|
let out = self.body.add_block();
|
2021-11-13 15:49:57 -06:00
|
|
|
self.add_block_params(out, &results[..]);
|
|
|
|
let start_depth = self.op_stack.len() - params.len();
|
2021-11-13 05:38:47 -06:00
|
|
|
self.ctrl_stack.push(Frame::Block {
|
2021-11-13 15:49:57 -06:00
|
|
|
start_depth,
|
|
|
|
out,
|
|
|
|
params,
|
|
|
|
results,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::Loop { ty } => {
|
2021-11-13 15:49:57 -06:00
|
|
|
let (params, results) = self.block_params_and_results(*ty);
|
2021-12-11 23:46:14 -06:00
|
|
|
let header = self.body.add_block();
|
2021-11-13 15:49:57 -06:00
|
|
|
self.add_block_params(header, ¶ms[..]);
|
2021-11-13 18:31:11 -06:00
|
|
|
let initial_args = self.pop_n(params.len());
|
2021-11-13 15:49:57 -06:00
|
|
|
let start_depth = self.op_stack.len();
|
|
|
|
self.emit_branch(header, &initial_args[..]);
|
2021-11-13 16:13:31 -06:00
|
|
|
self.cur_block = Some(header);
|
2021-12-11 23:46:14 -06:00
|
|
|
self.locals.start_block(header);
|
|
|
|
self.push_block_params(params.len());
|
|
|
|
let out = self.body.add_block();
|
2021-11-13 19:47:33 -06:00
|
|
|
self.add_block_params(out, &results[..]);
|
2021-11-13 15:49:57 -06:00
|
|
|
self.ctrl_stack.push(Frame::Loop {
|
|
|
|
start_depth,
|
|
|
|
header,
|
2021-11-13 05:38:47 -06:00
|
|
|
out,
|
|
|
|
params,
|
|
|
|
results,
|
|
|
|
});
|
2021-11-13 04:32:05 -06:00
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::If { ty } => {
|
2021-11-13 15:49:57 -06:00
|
|
|
let (params, results) = self.block_params_and_results(*ty);
|
2021-12-11 23:46:14 -06:00
|
|
|
let if_true = self.body.add_block();
|
|
|
|
let if_false = self.body.add_block();
|
|
|
|
let join = self.body.add_block();
|
2021-11-13 15:49:57 -06:00
|
|
|
self.add_block_params(join, &results[..]);
|
2021-11-13 18:31:11 -06:00
|
|
|
let cond = self.pop_1();
|
2021-11-13 15:49:57 -06:00
|
|
|
let param_values = self.op_stack[self.op_stack.len() - params.len()..].to_vec();
|
2021-11-13 19:47:33 -06:00
|
|
|
let start_depth = self.op_stack.len() - params.len();
|
2021-11-13 15:49:57 -06:00
|
|
|
self.ctrl_stack.push(Frame::If {
|
|
|
|
start_depth,
|
|
|
|
out: join,
|
|
|
|
el: if_false,
|
|
|
|
param_values,
|
|
|
|
params,
|
|
|
|
results,
|
|
|
|
});
|
|
|
|
self.emit_cond_branch(cond, if_true, &[], if_false, &[]);
|
2021-12-11 23:46:14 -06:00
|
|
|
self.locals.seal_block_preds(if_true, &mut self.body);
|
|
|
|
self.locals.seal_block_preds(if_false, &mut self.body);
|
|
|
|
self.cur_block = Some(if_true);
|
|
|
|
self.locals.start_block(if_true);
|
2021-11-13 15:49:57 -06:00
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::Else => {
|
2021-11-13 15:49:57 -06:00
|
|
|
if let Frame::If {
|
|
|
|
start_depth,
|
|
|
|
out,
|
|
|
|
el,
|
|
|
|
param_values,
|
|
|
|
params,
|
|
|
|
results,
|
|
|
|
} = self.ctrl_stack.pop().unwrap()
|
|
|
|
{
|
2021-11-13 19:47:33 -06:00
|
|
|
if self.cur_block.is_some() {
|
|
|
|
let if_results = self.pop_n(results.len());
|
|
|
|
self.emit_branch(out, &if_results[..]);
|
|
|
|
}
|
|
|
|
self.op_stack.truncate(start_depth);
|
2021-11-13 15:49:57 -06:00
|
|
|
self.op_stack.extend(param_values);
|
|
|
|
self.ctrl_stack.push(Frame::Else {
|
|
|
|
start_depth,
|
|
|
|
out,
|
|
|
|
params,
|
|
|
|
results,
|
|
|
|
});
|
2021-11-13 16:13:31 -06:00
|
|
|
self.cur_block = Some(el);
|
2021-12-11 23:46:14 -06:00
|
|
|
self.locals.start_block(el);
|
2021-11-13 15:49:57 -06:00
|
|
|
} else {
|
|
|
|
bail!("Else without If on top of frame stack");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::Br { relative_depth }
|
|
|
|
| wasmparser::Operator::BrIf { relative_depth } => {
|
2021-11-13 15:49:57 -06:00
|
|
|
let cond = match &op {
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::Br { .. } => None,
|
|
|
|
wasmparser::Operator::BrIf { .. } => Some(self.pop_1()),
|
2021-11-13 15:49:57 -06:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
// Get the frame we're branching to.
|
2021-11-13 16:13:31 -06:00
|
|
|
let frame = self.relative_frame(*relative_depth).clone();
|
2021-11-13 15:49:57 -06:00
|
|
|
// Finally, generate the branch itself.
|
|
|
|
match cond {
|
|
|
|
None => {
|
2021-11-13 19:47:33 -06:00
|
|
|
// Get the args off the stack unconditionally.
|
|
|
|
let args = self.pop_n(frame.br_args().len());
|
2021-11-13 15:49:57 -06:00
|
|
|
self.emit_branch(frame.br_target(), &args[..]);
|
2021-12-11 23:46:14 -06:00
|
|
|
self.locals.finish_block();
|
2021-11-13 16:13:31 -06:00
|
|
|
self.cur_block = None;
|
2021-11-13 15:49:57 -06:00
|
|
|
}
|
|
|
|
Some(cond) => {
|
2021-12-11 23:46:14 -06:00
|
|
|
let cont = self.body.add_block();
|
2021-11-13 19:47:33 -06:00
|
|
|
// Get the args off the stack but leave for the fallthrough.
|
|
|
|
let args = self.op_stack[self.op_stack.len() - frame.br_args().len()..]
|
|
|
|
.iter()
|
|
|
|
.map(|(_ty, value)| *value)
|
|
|
|
.collect::<Vec<_>>();
|
2021-11-13 15:49:57 -06:00
|
|
|
self.emit_cond_branch(cond, frame.br_target(), &args[..], cont, &[]);
|
2021-11-13 16:13:31 -06:00
|
|
|
self.cur_block = Some(cont);
|
2021-12-11 23:46:14 -06:00
|
|
|
self.locals.start_block(cont);
|
2021-11-13 15:49:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::BrTable { table } => {
|
2021-11-13 16:13:31 -06:00
|
|
|
// Get the selector index.
|
2021-11-13 18:31:11 -06:00
|
|
|
let index = self.pop_1();
|
2021-11-13 16:13:31 -06:00
|
|
|
// 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();
|
2021-11-13 18:31:11 -06:00
|
|
|
let args = self.pop_n(arg_len);
|
2021-11-13 16:13:31 -06:00
|
|
|
// 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-12-11 23:46:14 -06:00
|
|
|
self.locals.finish_block();
|
2021-11-13 17:09:47 -06:00
|
|
|
self.cur_block = None;
|
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
wasmparser::Operator::Return => {
|
2021-11-13 18:31:11 -06:00
|
|
|
let retvals = self.pop_n(self.module.signatures[self.my_sig].returns.len());
|
2021-11-13 17:09:47 -06:00
|
|
|
self.emit_ret(&retvals[..]);
|
2021-12-11 23:46:14 -06:00
|
|
|
self.locals.finish_block();
|
2021-11-13 17:09:47 -06:00
|
|
|
self.cur_block = None;
|
2021-11-13 16:13:31 -06:00
|
|
|
}
|
2021-11-13 15:49:57 -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-12-11 23:46:14 -06:00
|
|
|
fn add_block_params(&mut self, block: BlockId, tys: &[Type]) {
|
|
|
|
for &ty in tys {
|
|
|
|
self.body.add_blockparam(block, ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 05:38:47 -06:00
|
|
|
fn block_params_and_results(&self, ty: TypeOrFuncType) -> (Vec<Type>, Vec<Type>) {
|
|
|
|
match ty {
|
2021-11-13 19:47:33 -06:00
|
|
|
TypeOrFuncType::Type(Type::EmptyBlockType) => (vec![], vec![]),
|
2021-11-13 05:38:47 -06:00
|
|
|
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 16:13:31 -06:00
|
|
|
fn relative_frame(&self, relative_depth: u32) -> &Frame {
|
|
|
|
&self.ctrl_stack[self.ctrl_stack.len() - 1 - relative_depth as usize]
|
|
|
|
}
|
|
|
|
|
2021-11-21 19:15:26 -06:00
|
|
|
fn emit_branch(&mut self, target: BlockId, args: &[Value]) {
|
2021-11-13 16:13:31 -06:00
|
|
|
if let Some(block) = self.cur_block {
|
2021-12-11 23:46:14 -06:00
|
|
|
let args = args.to_vec();
|
|
|
|
self.locals.add_pred(target, block);
|
2021-11-13 16:13:31 -06:00
|
|
|
let target = BlockTarget {
|
|
|
|
block: target,
|
|
|
|
args,
|
|
|
|
};
|
|
|
|
self.body.blocks[block].terminator = Terminator::Br { target };
|
|
|
|
}
|
2021-11-13 05:38:47 -06:00
|
|
|
}
|
|
|
|
|
2021-11-13 15:49:57 -06:00
|
|
|
fn emit_cond_branch(
|
|
|
|
&mut self,
|
2021-11-21 19:15:26 -06:00
|
|
|
cond: Value,
|
2021-11-13 15:49:57 -06:00
|
|
|
if_true: BlockId,
|
2021-11-21 19:15:26 -06:00
|
|
|
if_true_args: &[Value],
|
2021-11-13 15:49:57 -06:00
|
|
|
if_false: BlockId,
|
2021-11-21 19:15:26 -06:00
|
|
|
if_false_args: &[Value],
|
2021-11-13 15:49:57 -06:00
|
|
|
) {
|
2021-11-13 16:13:31 -06:00
|
|
|
if let Some(block) = self.cur_block {
|
2021-12-11 23:46:14 -06:00
|
|
|
let if_true_args = if_true_args.to_vec();
|
|
|
|
let if_false_args = if_false_args.to_vec();
|
2021-11-13 16:13:31 -06:00
|
|
|
self.body.blocks[block].terminator = Terminator::CondBr {
|
2021-11-21 19:15:26 -06:00
|
|
|
cond,
|
2021-11-13 16:13:31 -06:00
|
|
|
if_true: BlockTarget {
|
|
|
|
block: if_true,
|
|
|
|
args: if_true_args,
|
|
|
|
},
|
|
|
|
if_false: BlockTarget {
|
|
|
|
block: if_false,
|
|
|
|
args: if_false_args,
|
|
|
|
},
|
|
|
|
};
|
2021-12-11 23:46:14 -06:00
|
|
|
self.locals.add_pred(if_true, block);
|
|
|
|
self.locals.add_pred(if_false, block);
|
2021-11-13 16:13:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_br_table(
|
|
|
|
&mut self,
|
2021-11-21 19:15:26 -06:00
|
|
|
index: Value,
|
2021-11-13 16:13:31 -06:00
|
|
|
default_target: BlockId,
|
|
|
|
indexed_targets: &[BlockId],
|
2021-11-21 19:15:26 -06:00
|
|
|
args: &[Value],
|
2021-11-13 16:13:31 -06:00
|
|
|
) {
|
|
|
|
if let Some(block) = self.cur_block {
|
2021-11-21 19:15:26 -06:00
|
|
|
let args = args.to_vec();
|
2021-11-13 16:13:31 -06:00
|
|
|
let targets = indexed_targets
|
|
|
|
.iter()
|
2021-11-14 00:25:27 -06:00
|
|
|
.map(|&block| {
|
2021-12-11 23:46:14 -06:00
|
|
|
let args = args.clone();
|
2021-11-14 00:25:27 -06:00
|
|
|
BlockTarget { block, args }
|
2021-11-13 16:13:31 -06:00
|
|
|
})
|
|
|
|
.collect();
|
2021-11-14 00:25:27 -06:00
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
let default_args = args;
|
2021-11-13 16:13:31 -06:00
|
|
|
let default = BlockTarget {
|
|
|
|
block: default_target,
|
2021-11-14 00:25:27 -06:00
|
|
|
args: default_args,
|
2021-11-13 16:13:31 -06:00
|
|
|
};
|
2021-12-11 23:46:14 -06:00
|
|
|
|
|
|
|
for &target in indexed_targets {
|
|
|
|
self.locals.add_pred(target, block);
|
|
|
|
}
|
|
|
|
self.locals.add_pred(default_target, block);
|
|
|
|
|
2021-11-13 16:13:31 -06:00
|
|
|
self.body.blocks[block].terminator = Terminator::Select {
|
2021-11-21 19:15:26 -06:00
|
|
|
value: index,
|
2021-11-13 16:13:31 -06:00
|
|
|
targets,
|
|
|
|
default,
|
|
|
|
};
|
|
|
|
}
|
2021-11-13 15:49:57 -06:00
|
|
|
}
|
|
|
|
|
2021-11-21 19:15:26 -06:00
|
|
|
fn emit_ret(&mut self, values: &[Value]) {
|
2021-11-13 17:09:47 -06:00
|
|
|
if let Some(block) = self.cur_block {
|
2021-11-21 19:15:26 -06:00
|
|
|
let values = values.to_vec();
|
2021-11-13 17:09:47 -06:00
|
|
|
self.body.blocks[block].terminator = Terminator::Return { values };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
fn push_block_params(&mut self, num_params: usize) {
|
2021-11-14 00:25:27 -06:00
|
|
|
let block = self.cur_block.unwrap();
|
2021-12-11 23:46:14 -06:00
|
|
|
for i in 0..num_params {
|
|
|
|
let ty = self.body.blocks[block].params[i];
|
|
|
|
let value = self
|
|
|
|
.body
|
|
|
|
.add_value(ValueDef::BlockParam(block, i), Some(ty));
|
2021-11-21 19:15:26 -06:00
|
|
|
self.op_stack.push((ty, value));
|
2021-11-13 15:49:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
fn emit(&mut self, op: Operator) -> Result<()> {
|
2021-11-13 19:47:33 -06:00
|
|
|
let inputs = op_inputs(
|
|
|
|
self.module,
|
|
|
|
self.my_sig,
|
|
|
|
&self.body.locals[..],
|
|
|
|
&self.op_stack[..],
|
|
|
|
&op,
|
|
|
|
)?;
|
|
|
|
let outputs = op_outputs(self.module, &self.body.locals[..], &self.op_stack[..], &op)?;
|
|
|
|
|
2021-11-13 16:13:31 -06:00
|
|
|
if let Some(block) = self.cur_block {
|
2021-12-11 23:46:14 -06:00
|
|
|
let n_outputs = outputs.len();
|
2021-11-13 16:13:31 -06:00
|
|
|
|
2021-11-13 19:47:33 -06:00
|
|
|
let mut input_operands = vec![];
|
|
|
|
for input in inputs.into_iter().rev() {
|
2021-11-13 18:31:11 -06:00
|
|
|
let (stack_top_ty, stack_top) = self.op_stack.pop().unwrap();
|
|
|
|
assert_eq!(stack_top_ty, input);
|
2021-11-21 19:15:26 -06:00
|
|
|
input_operands.push(stack_top);
|
2021-11-13 16:13:31 -06:00
|
|
|
}
|
2021-11-13 19:47:33 -06:00
|
|
|
input_operands.reverse();
|
2021-11-13 16:13:31 -06:00
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
let ty = if n_outputs == 1 {
|
|
|
|
Some(outputs[0])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let value = self
|
|
|
|
.body
|
|
|
|
.add_value(ValueDef::Operator(op, input_operands), ty);
|
|
|
|
if !op_effects(&op).unwrap().is_empty() {
|
|
|
|
self.body.blocks[block].insts.push(value);
|
2021-11-13 16:13:31 -06:00
|
|
|
}
|
2021-11-13 04:32:05 -06:00
|
|
|
|
2021-12-11 23:46:14 -06:00
|
|
|
if n_outputs == 1 {
|
|
|
|
let output_ty = outputs[0];
|
|
|
|
self.op_stack.push((output_ty, value));
|
|
|
|
} else {
|
|
|
|
for (i, output_ty) in outputs.into_iter().enumerate() {
|
|
|
|
let pick = self
|
|
|
|
.body
|
|
|
|
.add_value(ValueDef::PickOutput(value, i), Some(output_ty));
|
|
|
|
self.op_stack.push((output_ty, pick));
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 16:23:22 -06:00
|
|
|
} else {
|
2021-11-13 19:47:33 -06:00
|
|
|
let _ = self.pop_n(inputs.len());
|
|
|
|
for ty in outputs {
|
2021-11-21 19:15:26 -06:00
|
|
|
self.op_stack.push((ty, Value::undef()));
|
2021-11-13 16:23:22 -06:00
|
|
|
}
|
2021-11-13 03:41:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-13 03:17:53 -06:00
|
|
|
}
|