suggested fixes 1

This commit is contained in:
Graham Kelly 2024-02-19 13:02:49 -05:00
parent ff4d2aba3c
commit b5a61d053f
5 changed files with 7 additions and 95 deletions

View file

@ -31,13 +31,9 @@ pub enum WasmBlock<'a> {
header: Block, header: Block,
}, },
/// A leaf node: one CFG block. /// A leaf node: one CFG block.
Leaf { Leaf { block: Block },
block: Block,
},
/// A translated unconditional branch. /// A translated unconditional branch.
Br { Br { target: WasmLabel },
target: WasmLabel,
},
/// A translated conditional. /// A translated conditional.
If { If {
cond: Value, cond: Value,
@ -56,13 +52,10 @@ pub enum WasmBlock<'a> {
to: &'a [(Type, Value)], to: &'a [(Type, Value)],
}, },
/// A function return instruction. /// A function return instruction.
Return { Return { values: &'a [Value] },
values: &'a [Value], /// A function tail call instruction
}, ReturnCall { func: Func, values: &'a [Value] },
ReturnCall { /// A function indirect tail call instruction
func: Func,
values: &'a [Value],
},
ReturnCallIndirect { ReturnCallIndirect {
sig: Signature, sig: Signature,
table: Table, table: Table,

View file

@ -1249,7 +1249,6 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> {
type_index, type_index,
table_index, table_index,
} => { } => {
// let sig = self.module.funcs[Func::new(*function_index as usize)].sig();
let retvals = self.pop_n( let retvals = self.pop_n(
self.module.signatures[Signature::new(*type_index as usize)] self.module.signatures[Signature::new(*type_index as usize)]
.params .params
@ -1687,7 +1686,6 @@ impl<'a, 'b> FunctionBodyBuilder<'a, 'b> {
t t
); );
if self.reachable { if self.reachable {
// let values = values.to_vec();
self.body.set_terminator(self.cur_block, t); self.body.set_terminator(self.cur_block, t);
self.reachable = false; self.reachable = false;
} }

View file

@ -20,7 +20,7 @@ pub struct Module<'a> {
pub start_func: Option<Func>, pub start_func: Option<Func>,
pub debug: Debug, pub debug: Debug,
pub debug_map: DebugMap, pub debug_map: DebugMap,
pub custom_sections: IndexMap<String,Vec<u8>> pub custom_sections: IndexMap<String, Vec<u8>>,
} }
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]

View file

@ -5,7 +5,6 @@ pub mod dom_pass;
pub mod empty_blocks; pub mod empty_blocks;
pub mod maxssa; pub mod maxssa;
pub mod remove_phis; pub mod remove_phis;
pub mod reorder_funs;
pub mod resolve_aliases; pub mod resolve_aliases;
pub mod ssa; pub mod ssa;
pub mod trace; pub mod trace;

View file

@ -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<Func, Func>) {
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<Func, Func>) {
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;
}