waffle/src/frontend.rs
Chris Fallin 44c2c8dc17 WIP.
2021-11-13 00:56:49 -08:00

38 lines
940 B
Rust

//! Frontend: convert Wasm to IR.
use crate::ir::*;
use anyhow::Result;
use log::trace;
use wasmparser::{Parser, Payload, TypeDef};
pub fn wasm_to_ir(bytes: &[u8]) -> Result<Module> {
let mut module = Module::default();
let parser = Parser::new(0);
for payload in parser.parse_all(bytes) {
let payload = payload?;
handle_payload(&mut module, payload)?;
}
Ok(module)
}
fn handle_payload<'a>(module: &mut Module, payload: Payload<'a>) -> Result<()> {
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);
}
_ => {}
}
}
}
_ => {}
}
Ok(())
}