From b5a61d053fc03b4cd997af0aaae868cc6f7c79a1 Mon Sep 17 00:00:00 2001 From: Graham Kelly Date: Mon, 19 Feb 2024 13:02:49 -0500 Subject: [PATCH] suggested fixes 1 --- src/backend/stackify.rs | 19 +++------- src/frontend.rs | 2 - src/ir/module.rs | 2 +- src/passes.rs | 1 - src/passes/reorder_funs.rs | 78 -------------------------------------- 5 files changed, 7 insertions(+), 95 deletions(-) delete mode 100644 src/passes/reorder_funs.rs diff --git a/src/backend/stackify.rs b/src/backend/stackify.rs index 8147e97..c61e985 100644 --- a/src/backend/stackify.rs +++ b/src/backend/stackify.rs @@ -31,13 +31,9 @@ pub enum WasmBlock<'a> { header: Block, }, /// A leaf node: one CFG block. - Leaf { - block: Block, - }, + Leaf { block: Block }, /// A translated unconditional branch. - Br { - target: WasmLabel, - }, + Br { target: WasmLabel }, /// A translated conditional. If { cond: Value, @@ -56,13 +52,10 @@ pub enum WasmBlock<'a> { to: &'a [(Type, Value)], }, /// A function return instruction. - Return { - values: &'a [Value], - }, - ReturnCall { - func: Func, - values: &'a [Value], - }, + Return { values: &'a [Value] }, + /// A function tail call instruction + ReturnCall { func: Func, values: &'a [Value] }, + /// A function indirect tail call instruction ReturnCallIndirect { sig: Signature, table: Table, diff --git a/src/frontend.rs b/src/frontend.rs index ed462e3..231fa80 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -1249,7 +1249,6 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { type_index, table_index, } => { - // let sig = self.module.funcs[Func::new(*function_index as usize)].sig(); let retvals = self.pop_n( self.module.signatures[Signature::new(*type_index as usize)] .params @@ -1687,7 +1686,6 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> { t ); if self.reachable { - // let values = values.to_vec(); self.body.set_terminator(self.cur_block, t); self.reachable = false; } diff --git a/src/ir/module.rs b/src/ir/module.rs index dcb1b9e..a3f2d37 100644 --- a/src/ir/module.rs +++ b/src/ir/module.rs @@ -20,7 +20,7 @@ pub struct Module<'a> { pub start_func: Option, pub debug: Debug, pub debug_map: DebugMap, - pub custom_sections: IndexMap> + pub custom_sections: IndexMap>, } #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] diff --git a/src/passes.rs b/src/passes.rs index 838c428..c6d4a58 100644 --- a/src/passes.rs +++ b/src/passes.rs @@ -5,7 +5,6 @@ pub mod dom_pass; pub mod empty_blocks; pub mod maxssa; pub mod remove_phis; -pub mod reorder_funs; pub mod resolve_aliases; pub mod ssa; pub mod trace; diff --git a/src/passes/reorder_funs.rs b/src/passes/reorder_funs.rs deleted file mode 100644 index a070a02..0000000 --- a/src/passes/reorder_funs.rs +++ /dev/null @@ -1,78 +0,0 @@ -use std::collections::BTreeMap; - -use crate::{ - entity::EntityRef, ExportKind, Func, FuncDecl, FunctionBody, ImportKind, Module, Operator, - Terminator, ValueDef, -}; - -pub fn reorder_funcs_in_body(b: &mut FunctionBody, f: &BTreeMap) { - for v in b.values.values_mut() { - if let ValueDef::Operator(a, _, _) = v { - if let Operator::Call { function_index } = a { - *function_index = *f.get(&*function_index).unwrap(); - } - } - } - for k in b.blocks.values_mut() { - if let Terminator::ReturnCall { func, args } = &mut k.terminator { - *func = *f.get(&*func).unwrap(); - } - } -} -pub fn reorder_funcs(m: &mut Module, fs: &BTreeMap) { - let mut n = m.funcs.clone(); - for (f, b) in m.funcs.entries() { - let mut b = b.clone(); - if let Some(b) = b.body_mut() { - reorder_funcs_in_body(b, fs); - } - n[*fs.get(&f).unwrap()] = b; - } - m.funcs = n; - for t in m.tables.values_mut() { - if let Some(e) = t.func_elements.as_mut() { - for e in e.iter_mut() { - let Some(f) = fs.get(&*e) else{ - let f = *e; - panic!("invalid func: {f}; {}",m.funcs[f].name()) - }; - *e = *f; - } - } - } - for i in m.imports.iter_mut() { - if let ImportKind::Func(f) = &mut i.kind { - *f = *fs.get(&*f).unwrap(); - } - } - for i in m.exports.iter_mut() { - if let ExportKind::Func(f) = &mut i.kind { - *f = *fs.get(&*f).unwrap(); - } - } -} -pub fn fixup_orders(m: &mut Module) { - let mut fs = BTreeMap::new(); - let mut a = vec![]; - let mut b = vec![]; - for (f, d) in m.funcs.entries() { - if let FuncDecl::Import(_, _) = d { - a.push(f) - } else { - b.push(f) - } - } - let mut i = 0; - for v in a { - fs.insert(v, Func::new(i)); - i += 1; - } - for v in b { - fs.insert(v, Func::new(i)); - i += 1; - } - assert_eq!(fs.len(),m.funcs.len()); - fs.insert(Func::invalid(),Func::invalid()); - reorder_funcs(m, &fs); - return; -}