Rename some AST parts to be less odd

This commit is contained in:
Rerumu
2022-04-26 05:03:54 -04:00
parent 12e2799914
commit 1fda67d6d3
9 changed files with 185 additions and 187 deletions
+46 -48
View File
@@ -4,10 +4,10 @@ use parity_wasm::elements::{
};
use crate::node::{
AnyBinOp, AnyCmpOp, AnyLoad, AnyStore, AnyUnOp, Backward, BinOp, Br, BrIf, BrTable, Call,
CallIndirect, CmpOp, Else, Expression, Forward, Function, GetGlobal, GetLocal, If, Load,
Memorize, MemoryGrow, MemorySize, Recall, Return, Select, SetGlobal, SetLocal, Statement,
Store, UnOp, Value,
Backward, BinOp, BinOpType, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, CmpOpType, Else,
Expression, Forward, GetGlobal, GetLocal, If, Intermediate, LoadAt, LoadType, Memorize,
MemoryGrow, MemorySize, Recall, Return, Select, SetGlobal, SetLocal, Statement, StoreAt,
StoreType, UnOp, UnOpType, Value,
};
struct Arity {
@@ -175,50 +175,48 @@ impl Stacked {
}
}
fn push_load(&mut self, op: Load, offset: u32) {
fn push_load(&mut self, what: LoadType, offset: u32) {
let pointer = Box::new(self.pop());
self.stack.push(Expression::AnyLoad(AnyLoad {
op,
self.stack.push(Expression::LoadAt(LoadAt {
what,
offset,
pointer,
}));
}
fn gen_store(&mut self, op: Store, offset: u32, stat: &mut Vec<Statement>) {
fn gen_store(&mut self, what: StoreType, offset: u32, stat: &mut Vec<Statement>) {
let value = self.pop();
let pointer = self.pop();
self.gen_leak_pending(stat);
stat.push(Statement::AnyStore(AnyStore {
op,
stat.push(Statement::StoreAt(StoreAt {
what,
offset,
pointer,
value,
}));
}
fn push_un_op(&mut self, op: UnOp) {
fn push_un_op(&mut self, op: UnOpType) {
let rhs = Box::new(self.pop());
self.stack.push(Expression::AnyUnOp(AnyUnOp { op, rhs }));
self.stack.push(Expression::UnOp(UnOp { op, rhs }));
}
fn push_bin_op(&mut self, op: BinOp) {
fn push_bin_op(&mut self, op: BinOpType) {
let rhs = Box::new(self.pop());
let lhs = Box::new(self.pop());
self.stack
.push(Expression::AnyBinOp(AnyBinOp { op, lhs, rhs }));
self.stack.push(Expression::BinOp(BinOp { op, lhs, rhs }));
}
fn push_cmp_op(&mut self, op: CmpOp) {
fn push_cmp_op(&mut self, op: CmpOpType) {
let rhs = Box::new(self.pop());
let lhs = Box::new(self.pop());
self.stack
.push(Expression::AnyCmpOp(AnyCmpOp { op, lhs, rhs }));
self.stack.push(Expression::CmpOp(CmpOp { op, lhs, rhs }));
}
// Since Eqz is the only unary comparison it's cleaner to
@@ -227,13 +225,13 @@ impl Stacked {
match inst {
Instruction::I32Eqz => {
self.push_constant(0_i32);
self.push_cmp_op(CmpOp::Eq_I32);
self.push_cmp_op(CmpOpType::Eq_I32);
true
}
Instruction::I64Eqz => {
self.push_constant(0_i64);
self.push_cmp_op(CmpOp::Eq_I64);
self.push_cmp_op(CmpOpType::Eq_I64);
true
}
@@ -242,15 +240,15 @@ impl Stacked {
}
fn try_operation(&mut self, inst: &Instruction) -> bool {
if let Ok(op) = UnOp::try_from(inst) {
if let Ok(op) = UnOpType::try_from(inst) {
self.push_un_op(op);
true
} else if let Ok(op) = BinOp::try_from(inst) {
} else if let Ok(op) = BinOpType::try_from(inst) {
self.push_bin_op(op);
true
} else if let Ok(op) = CmpOp::try_from(inst) {
} else if let Ok(op) = CmpOpType::try_from(inst) {
self.push_cmp_op(op);
true
@@ -291,7 +289,7 @@ impl<'a> Builder<'a> {
}
#[must_use]
pub fn consume(mut self, index: usize, func: &'a FuncBody) -> Function {
pub fn consume(mut self, index: usize, func: &'a FuncBody) -> Intermediate {
let arity = &self.type_info.arity_of(self.type_info.len_ex() + index);
self.num_result = arity.num_result;
@@ -299,7 +297,7 @@ impl<'a> Builder<'a> {
let code = self.new_forward(&mut func.code().elements());
let num_stack = self.data.last_stack;
Function {
Intermediate {
local_data: func.locals().to_vec(),
num_param: arity.num_param,
num_stack,
@@ -533,29 +531,29 @@ impl<'a> Builder<'a> {
stat.push(Statement::SetGlobal(SetGlobal { var, value }));
}
Inst::I32Load(_, o) => self.data.push_load(Load::I32, o),
Inst::I64Load(_, o) => self.data.push_load(Load::I64, o),
Inst::F32Load(_, o) => self.data.push_load(Load::F32, o),
Inst::F64Load(_, o) => self.data.push_load(Load::F64, o),
Inst::I32Load8S(_, o) => self.data.push_load(Load::I32_I8, o),
Inst::I32Load8U(_, o) => self.data.push_load(Load::I32_U8, o),
Inst::I32Load16S(_, o) => self.data.push_load(Load::I32_I16, o),
Inst::I32Load16U(_, o) => self.data.push_load(Load::I32_U16, o),
Inst::I64Load8S(_, o) => self.data.push_load(Load::I64_I8, o),
Inst::I64Load8U(_, o) => self.data.push_load(Load::I64_U8, o),
Inst::I64Load16S(_, o) => self.data.push_load(Load::I64_I16, o),
Inst::I64Load16U(_, o) => self.data.push_load(Load::I64_U16, o),
Inst::I64Load32S(_, o) => self.data.push_load(Load::I64_I32, o),
Inst::I64Load32U(_, o) => self.data.push_load(Load::I64_U32, o),
Inst::I32Store(_, o) => self.data.gen_store(Store::I32, o, &mut stat),
Inst::I64Store(_, o) => self.data.gen_store(Store::I64, o, &mut stat),
Inst::F32Store(_, o) => self.data.gen_store(Store::F32, o, &mut stat),
Inst::F64Store(_, o) => self.data.gen_store(Store::F64, o, &mut stat),
Inst::I32Store8(_, o) => self.data.gen_store(Store::I32_N8, o, &mut stat),
Inst::I32Store16(_, o) => self.data.gen_store(Store::I32_N16, o, &mut stat),
Inst::I64Store8(_, o) => self.data.gen_store(Store::I64_N8, o, &mut stat),
Inst::I64Store16(_, o) => self.data.gen_store(Store::I64_N16, o, &mut stat),
Inst::I64Store32(_, o) => self.data.gen_store(Store::I64_N32, o, &mut stat),
Inst::I32Load(_, o) => self.data.push_load(LoadType::I32, o),
Inst::I64Load(_, o) => self.data.push_load(LoadType::I64, o),
Inst::F32Load(_, o) => self.data.push_load(LoadType::F32, o),
Inst::F64Load(_, o) => self.data.push_load(LoadType::F64, o),
Inst::I32Load8S(_, o) => self.data.push_load(LoadType::I32_I8, o),
Inst::I32Load8U(_, o) => self.data.push_load(LoadType::I32_U8, o),
Inst::I32Load16S(_, o) => self.data.push_load(LoadType::I32_I16, o),
Inst::I32Load16U(_, o) => self.data.push_load(LoadType::I32_U16, o),
Inst::I64Load8S(_, o) => self.data.push_load(LoadType::I64_I8, o),
Inst::I64Load8U(_, o) => self.data.push_load(LoadType::I64_U8, o),
Inst::I64Load16S(_, o) => self.data.push_load(LoadType::I64_I16, o),
Inst::I64Load16U(_, o) => self.data.push_load(LoadType::I64_U16, o),
Inst::I64Load32S(_, o) => self.data.push_load(LoadType::I64_I32, o),
Inst::I64Load32U(_, o) => self.data.push_load(LoadType::I64_U32, o),
Inst::I32Store(_, o) => self.data.gen_store(StoreType::I32, o, &mut stat),
Inst::I64Store(_, o) => self.data.gen_store(StoreType::I64, o, &mut stat),
Inst::F32Store(_, o) => self.data.gen_store(StoreType::F32, o, &mut stat),
Inst::F64Store(_, o) => self.data.gen_store(StoreType::F64, o, &mut stat),
Inst::I32Store8(_, o) => self.data.gen_store(StoreType::I32_N8, o, &mut stat),
Inst::I32Store16(_, o) => self.data.gen_store(StoreType::I32_N16, o, &mut stat),
Inst::I64Store8(_, o) => self.data.gen_store(StoreType::I64_N8, o, &mut stat),
Inst::I64Store16(_, o) => self.data.gen_store(StoreType::I64_N16, o, &mut stat),
Inst::I64Store32(_, o) => self.data.gen_store(StoreType::I64_N32, o, &mut stat),
Inst::CurrentMemory(memory) => {
let memory = memory.try_into().unwrap();
+31 -31
View File
@@ -4,7 +4,7 @@ use parity_wasm::elements::{BrTableData, Instruction, Local, SignExtInstruction}
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
pub enum Load {
pub enum LoadType {
I32,
I64,
F32,
@@ -21,7 +21,7 @@ pub enum Load {
I64_U32,
}
impl Load {
impl LoadType {
#[must_use]
pub fn as_name(self) -> &'static str {
match self {
@@ -43,7 +43,7 @@ impl Load {
}
}
impl TryFrom<&Instruction> for Load {
impl TryFrom<&Instruction> for LoadType {
type Error = ();
fn try_from(inst: &Instruction) -> Result<Self, Self::Error> {
@@ -73,7 +73,7 @@ impl TryFrom<&Instruction> for Load {
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
pub enum Store {
pub enum StoreType {
I32,
I64,
F32,
@@ -85,7 +85,7 @@ pub enum Store {
I64_N32,
}
impl Store {
impl StoreType {
#[must_use]
pub fn as_name(self) -> &'static str {
match self {
@@ -102,7 +102,7 @@ impl Store {
}
}
impl TryFrom<&Instruction> for Store {
impl TryFrom<&Instruction> for StoreType {
type Error = ();
fn try_from(inst: &Instruction) -> Result<Self, Self::Error> {
@@ -129,7 +129,7 @@ impl TryFrom<&Instruction> for Store {
// operation_result_parameter
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
pub enum UnOp {
pub enum UnOpType {
Clz_I32,
Ctz_I32,
Popcnt_I32,
@@ -174,7 +174,7 @@ pub enum UnOp {
Reinterpret_F64_I64,
}
impl UnOp {
impl UnOpType {
#[must_use]
pub fn as_name(self) -> (&'static str, &'static str) {
match self {
@@ -224,7 +224,7 @@ impl UnOp {
}
}
impl TryFrom<&Instruction> for UnOp {
impl TryFrom<&Instruction> for UnOpType {
type Error = ();
fn try_from(inst: &Instruction) -> Result<Self, Self::Error> {
@@ -285,7 +285,7 @@ impl TryFrom<&Instruction> for UnOp {
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
pub enum BinOp {
pub enum BinOpType {
Add_I32,
Sub_I32,
Mul_I32,
@@ -325,7 +325,7 @@ pub enum BinOp {
Copysign_FN,
}
impl BinOp {
impl BinOpType {
#[must_use]
pub fn as_operator(self) -> Option<&'static str> {
let op = match self {
@@ -384,7 +384,7 @@ impl BinOp {
}
}
impl TryFrom<&Instruction> for BinOp {
impl TryFrom<&Instruction> for BinOpType {
type Error = ();
fn try_from(inst: &Instruction) -> Result<Self, Self::Error> {
@@ -439,7 +439,7 @@ impl TryFrom<&Instruction> for BinOp {
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
pub enum CmpOp {
pub enum CmpOpType {
Eq_I32,
Ne_I32,
LtS_I32,
@@ -468,7 +468,7 @@ pub enum CmpOp {
Ge_FN,
}
impl CmpOp {
impl CmpOpType {
#[must_use]
pub fn as_operator(self) -> Option<&'static str> {
let op = match self {
@@ -517,7 +517,7 @@ impl CmpOp {
}
}
impl TryFrom<&Instruction> for CmpOp {
impl TryFrom<&Instruction> for CmpOpType {
type Error = ();
fn try_from(inst: &Instruction) -> Result<Self, Self::Error> {
@@ -578,8 +578,8 @@ pub struct GetGlobal {
pub var: usize,
}
pub struct AnyLoad {
pub op: Load,
pub struct LoadAt {
pub what: LoadType,
pub offset: u32,
pub pointer: Box<Expression>,
}
@@ -625,19 +625,19 @@ impl From<u64> for Value {
}
}
pub struct AnyUnOp {
pub op: UnOp,
pub struct UnOp {
pub op: UnOpType,
pub rhs: Box<Expression>,
}
pub struct AnyBinOp {
pub op: BinOp,
pub struct BinOp {
pub op: BinOpType,
pub lhs: Box<Expression>,
pub rhs: Box<Expression>,
}
pub struct AnyCmpOp {
pub op: CmpOp,
pub struct CmpOp {
pub op: CmpOpType,
pub lhs: Box<Expression>,
pub rhs: Box<Expression>,
}
@@ -647,13 +647,13 @@ pub enum Expression {
Select(Select),
GetLocal(GetLocal),
GetGlobal(GetGlobal),
AnyLoad(AnyLoad),
LoadAt(LoadAt),
MemorySize(MemorySize),
MemoryGrow(MemoryGrow),
Value(Value),
AnyUnOp(AnyUnOp),
AnyBinOp(AnyBinOp),
AnyCmpOp(AnyCmpOp),
UnOp(UnOp),
BinOp(BinOp),
CmpOp(CmpOp),
}
impl Expression {
@@ -738,8 +738,8 @@ pub struct SetGlobal {
pub value: Expression,
}
pub struct AnyStore {
pub op: Store,
pub struct StoreAt {
pub what: StoreType,
pub offset: u32,
pub pointer: Expression,
pub value: Expression,
@@ -759,10 +759,10 @@ pub enum Statement {
CallIndirect(CallIndirect),
SetLocal(SetLocal),
SetGlobal(SetGlobal),
AnyStore(AnyStore),
StoreAt(StoreAt),
}
pub struct Function {
pub struct Intermediate {
pub local_data: Vec<Local>,
pub num_param: usize,
pub num_stack: usize,
+24 -24
View File
@@ -1,7 +1,7 @@
use crate::node::{
AnyBinOp, AnyCmpOp, AnyLoad, AnyStore, AnyUnOp, Backward, Br, BrIf, BrTable, Call,
CallIndirect, Else, Expression, Forward, Function, GetGlobal, GetLocal, If, Memorize,
MemoryGrow, MemorySize, Recall, Return, Select, SetGlobal, SetLocal, Statement, Value,
Backward, BinOp, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, Else, Expression, Forward,
GetGlobal, GetLocal, If, Intermediate, LoadAt, Memorize, MemoryGrow, MemorySize, Recall,
Return, Select, SetGlobal, SetLocal, Statement, StoreAt, UnOp, Value,
};
pub trait Visitor {
@@ -13,7 +13,7 @@ pub trait Visitor {
fn visit_get_global(&mut self, _: &GetGlobal) {}
fn visit_any_load(&mut self, _: &AnyLoad) {}
fn visit_load_at(&mut self, _: &LoadAt) {}
fn visit_memory_size(&mut self, _: &MemorySize) {}
@@ -21,11 +21,11 @@ pub trait Visitor {
fn visit_value(&mut self, _: &Value) {}
fn visit_any_unop(&mut self, _: &AnyUnOp) {}
fn visit_un_op(&mut self, _: &UnOp) {}
fn visit_any_binop(&mut self, _: &AnyBinOp) {}
fn visit_bin_op(&mut self, _: &BinOp) {}
fn visit_any_cmpop(&mut self, _: &AnyCmpOp) {}
fn visit_cmp_op(&mut self, _: &CmpOp) {}
fn visit_expression(&mut self, _: &Expression) {}
@@ -57,7 +57,7 @@ pub trait Visitor {
fn visit_set_global(&mut self, _: &SetGlobal) {}
fn visit_any_store(&mut self, _: &AnyStore) {}
fn visit_store_at(&mut self, _: &StoreAt) {}
fn visit_statement(&mut self, _: &Statement) {}
}
@@ -94,11 +94,11 @@ impl<T: Visitor> Driver<T> for GetGlobal {
}
}
impl<T: Visitor> Driver<T> for AnyLoad {
impl<T: Visitor> Driver<T> for LoadAt {
fn accept(&self, visitor: &mut T) {
self.pointer.accept(visitor);
visitor.visit_any_load(self);
visitor.visit_load_at(self);
}
}
@@ -122,29 +122,29 @@ impl<T: Visitor> Driver<T> for Value {
}
}
impl<T: Visitor> Driver<T> for AnyUnOp {
impl<T: Visitor> Driver<T> for UnOp {
fn accept(&self, visitor: &mut T) {
self.rhs.accept(visitor);
visitor.visit_any_unop(self);
visitor.visit_un_op(self);
}
}
impl<T: Visitor> Driver<T> for AnyBinOp {
impl<T: Visitor> Driver<T> for BinOp {
fn accept(&self, visitor: &mut T) {
self.lhs.accept(visitor);
self.rhs.accept(visitor);
visitor.visit_any_binop(self);
visitor.visit_bin_op(self);
}
}
impl<T: Visitor> Driver<T> for AnyCmpOp {
impl<T: Visitor> Driver<T> for CmpOp {
fn accept(&self, visitor: &mut T) {
self.lhs.accept(visitor);
self.rhs.accept(visitor);
visitor.visit_any_cmpop(self);
visitor.visit_cmp_op(self);
}
}
@@ -155,13 +155,13 @@ impl<T: Visitor> Driver<T> for Expression {
Self::Select(v) => v.accept(visitor),
Self::GetLocal(v) => v.accept(visitor),
Self::GetGlobal(v) => v.accept(visitor),
Self::AnyLoad(v) => v.accept(visitor),
Self::LoadAt(v) => v.accept(visitor),
Self::MemorySize(v) => v.accept(visitor),
Self::MemoryGrow(v) => v.accept(visitor),
Self::Value(v) => v.accept(visitor),
Self::AnyUnOp(v) => v.accept(visitor),
Self::AnyBinOp(v) => v.accept(visitor),
Self::AnyCmpOp(v) => v.accept(visitor),
Self::UnOp(v) => v.accept(visitor),
Self::BinOp(v) => v.accept(visitor),
Self::CmpOp(v) => v.accept(visitor),
}
visitor.visit_expression(self);
@@ -292,12 +292,12 @@ impl<T: Visitor> Driver<T> for SetGlobal {
}
}
impl<T: Visitor> Driver<T> for AnyStore {
impl<T: Visitor> Driver<T> for StoreAt {
fn accept(&self, visitor: &mut T) {
self.pointer.accept(visitor);
self.value.accept(visitor);
visitor.visit_any_store(self);
visitor.visit_store_at(self);
}
}
@@ -317,12 +317,12 @@ impl<T: Visitor> Driver<T> for Statement {
Self::CallIndirect(v) => v.accept(visitor),
Self::SetLocal(v) => v.accept(visitor),
Self::SetGlobal(v) => v.accept(visitor),
Self::AnyStore(v) => v.accept(visitor),
Self::StoreAt(v) => v.accept(visitor),
}
}
}
impl<T: Visitor> Driver<T> for Function {
impl<T: Visitor> Driver<T> for Intermediate {
fn accept(&self, visitor: &mut T) {
self.code.accept(visitor);
}