waffle/src/op_traits.rs

46 lines
1.5 KiB
Rust
Raw Normal View History

2021-11-13 03:41:32 -06:00
//! Metadata on operators.
2021-11-13 04:32:05 -06:00
use crate::ir::{Module, SignatureId};
2021-11-13 03:41:32 -06:00
use anyhow::{bail, Result};
2021-11-13 04:32:05 -06:00
use wasmparser::{Operator, Type};
2021-11-13 03:41:32 -06:00
2021-11-13 04:32:05 -06:00
pub fn op_inputs(
module: &Module,
my_sig: SignatureId,
my_locals: &[Type],
op: &Operator<'_>,
) -> Result<Vec<Type>> {
2021-11-13 03:41:32 -06:00
match op {
&Operator::Unreachable | &Operator::Nop => Ok(vec![]),
2021-11-13 04:32:05 -06:00
&Operator::Call { function_index } => {
let sig = module.funcs[function_index as usize].sig();
Ok(Vec::from(module.signatures[sig].params.clone()))
}
&Operator::Return => Ok(Vec::from(module.signatures[my_sig].returns.clone())),
&Operator::LocalSet { local_index } | &Operator::LocalTee { local_index } => {
Ok(vec![my_locals[local_index as usize]])
}
&Operator::LocalGet { .. } => Ok(vec![]),
2021-11-13 03:41:32 -06:00
_ => bail!("Unknown operator in op_inputs(): {:?}", op),
}
}
2021-11-13 04:32:05 -06:00
pub fn op_outputs(module: &Module, my_locals: &[Type], op: &Operator<'_>) -> Result<Vec<Type>> {
2021-11-13 03:41:32 -06:00
match op {
&Operator::Unreachable | &Operator::Nop => Ok(vec![]),
2021-11-13 04:32:05 -06:00
&Operator::Call { function_index } => {
let sig = module.funcs[function_index as usize].sig();
Ok(Vec::from(module.signatures[sig].returns.clone()))
}
&Operator::Return => Ok(vec![]),
&Operator::LocalSet { .. } | &Operator::LocalTee { .. } => Ok(vec![]),
&Operator::LocalGet { local_index } => Ok(vec![my_locals[local_index as usize]]),
2021-11-13 03:41:32 -06:00
_ => bail!("Unknown operator in op_outputs(): {:?}", op),
}
}