Restructure and compartmentalize the project

This commit is contained in:
Rerumu
2022-06-23 20:14:04 -04:00
parent 59a5a3219f
commit 223895e617
34 changed files with 105 additions and 93 deletions
+36
View File
@@ -0,0 +1,36 @@
use wasm_ast::node::{BinOpType, CmpOpType};
pub trait AsSymbol {
fn as_symbol(&self) -> Option<&'static str>;
}
impl AsSymbol for BinOpType {
fn as_symbol(&self) -> Option<&'static str> {
let result = match self {
Self::Add_I64 | Self::Add_FN => "+",
Self::Sub_I64 | Self::Sub_FN => "-",
Self::Mul_I64 | Self::Mul_FN => "*",
Self::DivS_I64 | Self::Div_FN => "/",
Self::RemS_I64 => "%",
_ => return None,
};
Some(result)
}
}
impl AsSymbol for CmpOpType {
fn as_symbol(&self) -> Option<&'static str> {
let result = match self {
Self::Eq_I32 | Self::Eq_I64 | Self::Eq_FN => "==",
Self::Ne_I32 | Self::Ne_I64 | Self::Ne_FN => "~=",
Self::LtS_I32 | Self::LtS_I64 | Self::Lt_FN => "<",
Self::GtS_I32 | Self::GtS_I64 | Self::Gt_FN => ">",
Self::LeS_I32 | Self::LeS_I64 | Self::Le_FN => "<=",
Self::GeS_I32 | Self::GeS_I64 | Self::Ge_FN => ">=",
_ => return None,
};
Some(result)
}
}
+29
View File
@@ -0,0 +1,29 @@
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
}
+74
View File
@@ -0,0 +1,74 @@
use std::collections::BTreeSet;
use wasm_ast::{
node::{BinOp, CmpOp, FuncData, LoadAt, MemoryGrow, MemorySize, StoreAt, UnOp},
visit::{Driver, Visitor},
};
use super::as_symbol::AsSymbol;
struct Visit {
local_set: BTreeSet<(&'static str, &'static str)>,
memory_set: BTreeSet<usize>,
}
impl Visitor for Visit {
fn visit_load_at(&mut self, v: &LoadAt) {
let name = v.load_type().as_name();
self.memory_set.insert(0);
self.local_set.insert(("load", name));
}
fn visit_store_at(&mut self, v: &StoreAt) {
let name = v.store_type().as_name();
self.memory_set.insert(0);
self.local_set.insert(("store", name));
}
fn visit_un_op(&mut self, v: &UnOp) {
let name = v.op_type().as_name();
self.local_set.insert(name);
}
fn visit_bin_op(&mut self, v: &BinOp) {
if v.op_type().as_symbol().is_some() {
return;
}
let name = v.op_type().as_name();
self.local_set.insert(name);
}
fn visit_cmp_op(&mut self, v: &CmpOp) {
if v.op_type().as_symbol().is_some() {
return;
}
let name = v.op_type().as_name();
self.local_set.insert(name);
}
fn visit_memory_size(&mut self, m: &MemorySize) {
self.memory_set.insert(m.memory());
}
fn visit_memory_grow(&mut self, m: &MemoryGrow) {
self.memory_set.insert(m.memory());
}
}
pub fn visit(ast: &FuncData) -> (BTreeSet<(&'static str, &'static str)>, BTreeSet<usize>) {
let mut visit = Visit {
local_set: BTreeSet::new(),
memory_set: BTreeSet::new(),
};
ast.accept(&mut visit);
(visit.local_set, visit.memory_set)
}
+3
View File
@@ -0,0 +1,3 @@
pub mod as_symbol;
pub mod br_table;
pub mod localize;