This commit is contained in:
Chris Fallin 2021-11-13 01:10:22 -08:00
parent 3ca0886fbb
commit de2fb20b94

View file

@ -1,22 +1,29 @@
//! Frontend: convert Wasm to IR. //! Frontend: convert Wasm to IR.
use crate::ir::*; use crate::ir::*;
use anyhow::anyhow;
use anyhow::Result; use anyhow::Result;
use log::trace; use log::trace;
use std::collections::VecDeque;
use wasmparser::{ImportSectionEntryType, Parser, Payload, TypeDef}; use wasmparser::{ImportSectionEntryType, 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 parser = Parser::new(0); let parser = Parser::new(0);
let mut sigs = VecDeque::new();
for payload in parser.parse_all(bytes) { for payload in parser.parse_all(bytes) {
let payload = payload?; let payload = payload?;
handle_payload(&mut module, payload)?; handle_payload(&mut module, payload, &mut sigs)?;
} }
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>,
func_sigs: &mut VecDeque<SignatureId>,
) -> Result<()> {
trace!("Wasm parser item: {:?}", payload); trace!("Wasm parser item: {:?}", payload);
match payload { match payload {
Payload::TypeSection(mut reader) => { Payload::TypeSection(mut reader) => {
@ -40,8 +47,33 @@ fn handle_payload<'a>(module: &mut Module, payload: Payload<'a>) -> Result<()> {
} }
} }
} }
Payload::FunctionSection(mut reader) => {
for _ in 0..reader.get_count() {
func_sigs.push_back(reader.read()? as SignatureId);
}
}
Payload::CodeSectionEntry(body) => {
let sig = func_sigs
.pop_front()
.ok_or_else(|| anyhow!("mismatched func section and code section sizes"))?;
let body = parse_body(body)?;
module.funcs.push(FuncDecl::Body(sig as SignatureId, body));
}
_ => {} _ => {}
} }
Ok(()) Ok(())
} }
fn parse_body(body: wasmparser::FunctionBody) -> Result<FunctionBody> {
let mut ret = FunctionBody::default();
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);
}
}
Ok(ret)
}