Remove unneeded Luau branch gadgets

Fixiee
This commit is contained in:
Rerumu 2022-07-07 20:12:40 -04:00
parent 49bc994353
commit cdefb2f06a
4 changed files with 50 additions and 36 deletions

View File

@ -1,29 +0,0 @@
use std::collections::HashMap;
use wasm_ast::{
node::{BrTable, FuncData},
visit::{Driver, Visitor},
};
struct Visit {
id_map: HashMap<usize, usize>,
}
impl Visitor for Visit {
fn visit_br_table(&mut self, table: &BrTable) {
let id = table as *const _ as usize;
let len = self.id_map.len() + 1;
self.id_map.insert(id, len);
}
}
pub fn visit(ast: &FuncData) -> HashMap<usize, usize> {
let mut visit = Visit {
id_map: HashMap::new(),
};
ast.accept(&mut visit);
visit.id_map
}

View File

@ -0,0 +1,40 @@
use std::collections::HashMap;
use wasm_ast::{
node::{Br, BrIf, BrTable, FuncData},
visit::{Driver, Visitor},
};
struct Visit {
br_map: HashMap<usize, usize>,
has_branch: bool,
}
impl Visitor for Visit {
fn visit_br(&mut self, _: &Br) {
self.has_branch = true;
}
fn visit_br_if(&mut self, _: &BrIf) {
self.has_branch = true;
}
fn visit_br_table(&mut self, table: &BrTable) {
let id = table as *const _ as usize;
let len = self.br_map.len() + 1;
self.has_branch = true;
self.br_map.insert(id, len);
}
}
pub fn visit(ast: &FuncData) -> (HashMap<usize, usize>, bool) {
let mut visit = Visit {
br_map: HashMap::new(),
has_branch: false,
};
ast.accept(&mut visit);
(visit.br_map, visit.has_branch)
}

View File

@ -1,3 +1,3 @@
pub mod as_symbol;
pub mod br_table;
pub mod br_target;
pub mod localize;

View File

@ -9,7 +9,7 @@ use wasm_ast::node::{
};
use wasmparser::ValType;
use crate::analyzer::br_table;
use crate::analyzer::br_target;
use super::manager::{
write_ascending, write_condition, write_separated, write_variable, Driver, Manager,
@ -130,7 +130,7 @@ fn write_br_gadget(
label: usize,
w: &mut dyn Write,
) -> Result<()> {
if label_list.len() == 1 {
if label_list.len() == 1 || label_list.iter().all(Option::is_none) {
return Ok(());
}
@ -340,17 +340,20 @@ fn write_variable_list(ast: &FuncData, w: &mut dyn Write) -> Result<()> {
impl Driver for FuncData {
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
let br_map = br_table::visit(self);
let br_data = br_target::visit(self);
write_parameter_list(self, w)?;
write_variable_list(self, w)?;
write!(w, "local desired ")?;
if !br_map.is_empty() {
if br_data.1 {
write!(w, "local desired ")?;
}
if !br_data.0.is_empty() {
write!(w, "local br_map = {{}} ")?;
}
mng.set_table_map(br_map);
mng.set_table_map(br_data.0);
mng.set_num_param(self.num_param());
self.code().write(mng, w)?;