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.
use crate::ir::*;
use anyhow::{bail, Result};
use log::debug;
use wasmparser::{Chunk, Parser, Payload, TypeDef};
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 mut parser = Parser::new(0);
for chunk in parser.parse(bytes, /* eof = */ true) {
match chunk {
Chunk::NeedMoreData(_) => bail!("Unexpected EOF in Wasm"),
Chunk::Parsed { payload, .. } => handle_payload(&mut module, payload)?,
}
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<()> {
debug!("Wasm parser item: {:?}", payload);
trace!("Wasm parser item: {:?}", payload);
match payload {
Payload::TypeSection(mut reader) => {
for _ in 0..reader.get_count() {