From 1c0b2964afe9afb209819f42ad7b5c87c58f5e60 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Tue, 28 Feb 2023 16:31:53 -0800 Subject: [PATCH] Tweaks to API around function bodie. --- src/backend/mod.rs | 7 +------ src/interp.rs | 2 +- src/ir/display.rs | 2 +- src/ir/func.rs | 24 ++---------------------- src/ir/module.rs | 15 +++++++++++++++ 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index d516f36..3900d41 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -587,8 +587,7 @@ pub fn compile(module: &Module<'_>) -> anyhow::Result> { match func_decl { FuncDecl::Import(_, _) => anyhow::bail!("Import comes after func with body: {}", func), FuncDecl::Lazy(sig, _, _) - | FuncDecl::Body(sig, _, _) - | FuncDecl::Expanded(sig, _, _, _) => { + | FuncDecl::Body(sig, _, _) => { funcs.function(sig.index() as u32); } FuncDecl::None => panic!("FuncDecl::None at compilation time"), @@ -710,10 +709,6 @@ pub fn compile(module: &Module<'_>) -> anyhow::Result> { let data = &module.orig_bytes[reader.range()]; Ok(FuncOrRawBytes::Raw(data)) } - FuncDecl::Expanded(_, _name, range, _) => { - let data = &module.orig_bytes[range.clone()]; - Ok(FuncOrRawBytes::Raw(data)) - } FuncDecl::Body(_, name, body) => { log::debug!("Compiling {} \"{}\"", func, name); WasmFuncBackend::new(body)? diff --git a/src/interp.rs b/src/interp.rs index 0f93f04..9417597 100644 --- a/src/interp.rs +++ b/src/interp.rs @@ -95,7 +95,7 @@ impl InterpContext { assert_eq!(import.kind, ImportKind::Func(func)); return self.call_import(&import.name[..], args); } - FuncDecl::Body(_, _, body) | FuncDecl::Expanded(_, _, _, body) => body, + FuncDecl::Body(_, _, body) => body, FuncDecl::None => panic!("FuncDecl::None in call()"), }; diff --git a/src/ir/display.rs b/src/ir/display.rs index 08ce720..841829c 100644 --- a/src/ir/display.rs +++ b/src/ir/display.rs @@ -230,7 +230,7 @@ impl<'a> Display for ModuleDisplay<'a> { } for (func, func_decl) in self.0.funcs.entries() { match func_decl { - FuncDecl::Body(sig, name, body) | FuncDecl::Expanded(sig, name, _, body) => { + FuncDecl::Body(sig, name, body) => { writeln!( f, " {} \"{}\": {} = # {}", diff --git a/src/ir/func.rs b/src/ir/func.rs index e00df9c..7c937ed 100644 --- a/src/ir/func.rs +++ b/src/ir/func.rs @@ -4,7 +4,6 @@ use crate::entity::{EntityRef, EntityVec, PerEntity}; use crate::frontend::parse_body; use crate::ir::SourceLoc; use anyhow::Result; -use std::ops::Range; /// A declaration of a function: there is one `FuncDecl` per `Func` /// index. @@ -14,10 +13,6 @@ pub enum FuncDecl<'a> { Import(Signature, String), /// An un-expanded body that can be lazily expanded if needed. Lazy(Signature, String, wasmparser::FunctionBody<'a>), - /// Expanded body, but still "clean" (unchanged); range in - /// original Wasm lets us pass it straight through when converting - /// back to a Wasm module. - Expanded(Signature, String, Range, FunctionBody), /// A modified or new function body that requires compilation. Body(Signature, String, FunctionBody), /// A placeholder. @@ -30,7 +25,6 @@ impl<'a> FuncDecl<'a> { match self { FuncDecl::Import(sig, ..) => *sig, FuncDecl::Lazy(sig, ..) => *sig, - FuncDecl::Expanded(sig, ..) => *sig, FuncDecl::Body(sig, ..) => *sig, FuncDecl::None => panic!("No signature for FuncDecl::None"), } @@ -39,9 +33,8 @@ impl<'a> FuncDecl<'a> { pub fn parse(&mut self, module: &Module) -> Result<()> { match self { FuncDecl::Lazy(sig, name, body) => { - let range = body.range(); let body = parse_body(module, *sig, body)?; - *self = FuncDecl::Expanded(*sig, name.clone(), range, body); + *self = FuncDecl::Body(*sig, name.clone(), body); Ok(()) } _ => Ok(()), @@ -49,7 +42,6 @@ impl<'a> FuncDecl<'a> { } pub fn optimize(&mut self) { - self.mark_dirty(); match self { FuncDecl::Body(_, _, body) => { body.optimize(); @@ -59,7 +51,6 @@ impl<'a> FuncDecl<'a> { } pub fn convert_to_max_ssa(&mut self) { - self.mark_dirty(); match self { FuncDecl::Body(_, _, body) => { body.convert_to_max_ssa(); @@ -68,23 +59,14 @@ impl<'a> FuncDecl<'a> { } } - pub fn mark_dirty(&mut self) { - let new = match std::mem::take(self) { - FuncDecl::Expanded(sig, name, _, body) => FuncDecl::Body(sig, name, body), - x => x, - }; - *self = new; - } - pub fn body(&self) -> Option<&FunctionBody> { match self { - FuncDecl::Expanded(_, _, _, body) | FuncDecl::Body(_, _, body) => Some(body), + FuncDecl::Body(_, _, body) => Some(body), _ => None, } } pub fn body_mut(&mut self) -> Option<&mut FunctionBody> { - self.mark_dirty(); match self { FuncDecl::Body(_, _, body) => Some(body), _ => None, @@ -95,7 +77,6 @@ impl<'a> FuncDecl<'a> { match self { FuncDecl::Body(_, name, _) | FuncDecl::Lazy(_, name, _) - | FuncDecl::Expanded(_, name, _, _) | FuncDecl::Import(_, name) => &name[..], FuncDecl::None => panic!("No name for FuncDecl::None"), } @@ -105,7 +86,6 @@ impl<'a> FuncDecl<'a> { match self { FuncDecl::Body(_, name, _) | FuncDecl::Lazy(_, name, _) - | FuncDecl::Expanded(_, name, _, _) | FuncDecl::Import(_, name) => *name = new_name.to_owned(), FuncDecl::None => panic!("No name for FuncDecl::None"), } diff --git a/src/ir/module.rs b/src/ir/module.rs index 886c736..61286dc 100644 --- a/src/ir/module.rs +++ b/src/ir/module.rs @@ -187,6 +187,21 @@ impl<'a> Module<'a> { Ok(&mut self.funcs[id]) } + pub fn clone_and_expand_body(&self, id: Func) -> Result { + let mut body = self.funcs[id].clone(); + body.parse(self)?; + Ok(match body { + FuncDecl::Body(_, _, body) => body, + _ => unreachable!(), + }) + } + + pub fn replace_body(&mut self, id: Func, body: FunctionBody) { + let sig = self.funcs[id].sig(); + let name = self.funcs[id].name().to_owned(); + self.funcs[id] = FuncDecl::Body(sig, name, body); + } + pub fn expand_all_funcs(&mut self) -> Result<()> { for id in 0..self.funcs.len() { let id = Func::new(id);