working roundtrip for simple module

This commit is contained in:
Chris Fallin 2021-12-24 13:20:30 -08:00
parent 4585ec48be
commit 63aa7e4cb6
6 changed files with 35 additions and 7 deletions

View file

@ -170,5 +170,10 @@ pub fn produce_func_wasm<FT: FuncTypeSink>(
ctx.translate(operator, locations);
}
// Fixup: add unreachable just before last `end`; there must be an explicit return.
// assert!(matches!(wasm.operators.pop(), Some(wasm_encoder::Instruction::End)));
// wasm.operators.push(wasm_encoder::Instruction::Unreachable);
// wasm.operators.push(wasm_encoder::Instruction::End);
wasm
}

View file

@ -288,6 +288,8 @@ impl<'a> SerializedBodyContext<'a> {
self.operators.extend(rev_ops);
self.operators
.push(SerializedOperator::BrTable { targets, default });
self.operators
.push(SerializedOperator::Operator(Operator::Unreachable));
}
&Terminator::Return { ref values, .. } => {
let mut rev_ops = vec![];

View file

@ -345,13 +345,17 @@ impl BlockOrder {
target_stack.push(target);
}
let params = f.blocks[header].params.clone();
let results = match fallthrough {
let results = if header == 0 {
f.rets.clone()
} else {
match fallthrough {
Some(fallthrough) => f.blocks[fallthrough]
.params
.iter()
.map(|(ty, _)| *ty)
.collect(),
None => vec![],
}
};
if is_loop {
entries.push(BlockOrderEntry::StartLoop(header, params, results));

13
src/bin/roundtrip.rs Normal file
View file

@ -0,0 +1,13 @@
//! Roundtrip utility.
use std::io::{Read, Write};
use waffle::Module;
fn main() {
let _ = env_logger::try_init();
let mut bytes = vec![];
std::io::stdin().read_to_end(&mut bytes).unwrap();
let module = Module::from_wasm_bytes(&bytes).unwrap();
let new_bytes = module.to_wasm_bytes();
std::io::stdout().write(&new_bytes[..]).unwrap();
}

View file

@ -109,6 +109,9 @@ fn parse_body<'a>(
for &param in &module.signatures[my_sig].params[..] {
ret.locals.push(param);
}
for &r in &module.signatures[my_sig].returns[..] {
ret.rets.push(r);
}
let mut locals = body.get_locals_reader()?;
for _ in 0..locals.get_count() {

View file

@ -53,6 +53,7 @@ impl FuncDecl {
#[derive(Clone, Debug, Default)]
pub struct FunctionBody {
pub rets: Vec<Type>,
pub locals: Vec<Type>,
pub blocks: Vec<Block>,
/// Sea-of-nodes representation.