This commit is contained in:
Chris Fallin 2021-11-13 00:56:49 -08:00
parent b92643ad71
commit 44c2c8dc17

View file

@ -1,25 +1,23 @@
//! Frontend: convert Wasm to IR. //! Frontend: convert Wasm to IR.
use crate::ir::*; use crate::ir::*;
use anyhow::{bail, Result}; use anyhow::Result;
use log::debug; use log::trace;
use wasmparser::{Chunk, Parser, Payload, TypeDef}; use wasmparser::{Parser, Payload, TypeDef};
pub fn wasm_to_ir(bytes: &[u8]) -> Result<Module> { pub fn wasm_to_ir(bytes: &[u8]) -> Result<Module> {
let mut module = Module::default(); let mut module = Module::default();
let mut parser = Parser::new(0); let parser = Parser::new(0);
for chunk in parser.parse(bytes, /* eof = */ true) { for payload in parser.parse_all(bytes) {
match chunk { let payload = payload?;
Chunk::NeedMoreData(_) => bail!("Unexpected EOF in Wasm"), handle_payload(&mut module, payload)?;
Chunk::Parsed { payload, .. } => handle_payload(&mut module, payload)?,
}
} }
Ok(module) Ok(module)
} }
fn handle_payload<'a>(module: &mut Module, payload: Payload<'a>) -> Result<()> { fn handle_payload<'a>(module: &mut Module, payload: Payload<'a>) -> Result<()> {
debug!("Wasm parser item: {:?}", payload); trace!("Wasm parser item: {:?}", payload);
match payload { match payload {
Payload::TypeSection(mut reader) => { Payload::TypeSection(mut reader) => {
for _ in 0..reader.get_count() { for _ in 0..reader.get_count() {