fn valtype_to_string(ty: &ValType) -> String {
match ty {
ValType::I32 => "i32".to_string(),
ValType::I64 => "i64".to_string(),
ValType::F32 => "f32".to_string(),
ValType::F64 => "f64".to_string(),
ValType::V128 => "v128".to_string(),
ValType::Ref(ref_type) => format!("ref({:?})", ref_type),
}
}
fn categorize_instruction(op: &Operator, breakdown: &mut InstructionCategoryBreakdown) {
match op {
Operator::Unreachable
| Operator::Nop
| Operator::Block { .. }
| Operator::Loop { .. }
| Operator::If { .. }
| Operator::Else
| Operator::End
| Operator::Br { .. }
| Operator::BrIf { .. }
| Operator::BrTable { .. }
| Operator::Return
| Operator::Call { .. }
| Operator::CallIndirect { .. } => {
breakdown.control_flow += 1;
}
Operator::I32Load { .. }
| Operator::I64Load { .. }
| Operator::F32Load { .. }
| Operator::F64Load { .. }
| Operator::I32Store { .. }
| Operator::I64Store { .. }
| Operator::F32Store { .. }
| Operator::F64Store { .. }
| Operator::MemorySize { .. }
| Operator::MemoryGrow { .. } => {
breakdown.memory_ops += 1;
}
Operator::LocalGet { .. }
| Operator::LocalSet { .. }
| Operator::LocalTee { .. }
| Operator::GlobalGet { .. }
| Operator::GlobalSet { .. } => {
breakdown.variable_ops += 1;
}
Operator::TableGet { .. }
| Operator::TableSet { .. }
| Operator::TableGrow { .. }
| Operator::TableSize { .. }
| Operator::TableFill { .. } => {
breakdown.table_ops += 1;
}
Operator::RefNull { .. } | Operator::RefIsNull | Operator::RefFunc { .. } => {
breakdown.reference_ops += 1;
}
Operator::Drop | Operator::Select => {
breakdown.parametric_ops += 1;
}
_ => {
breakdown.numeric_ops += 1;
}
}
}
fn update_stack_depth(op: &Operator, depth: &mut u32) {
match op {
Operator::I32Const { .. }
| Operator::I64Const { .. }
| Operator::F32Const { .. }
| Operator::F64Const { .. }
| Operator::LocalGet { .. }
| Operator::GlobalGet { .. } => {
*depth = depth.saturating_add(1);
}
Operator::I32Eqz | Operator::I64Eqz => {}
Operator::Drop => {
*depth = depth.saturating_sub(1);
}
Operator::I32Store { .. }
| Operator::I64Store { .. }
| Operator::F32Store { .. }
| Operator::F64Store { .. } => {
*depth = depth.saturating_sub(2);
}
Operator::I32Add
| Operator::I32Sub
| Operator::I32Mul
| Operator::I32DivS
| Operator::I32DivU
| Operator::I32RemS
| Operator::I32RemU
| Operator::I32And
| Operator::I32Or
| Operator::I32Xor
| Operator::I32Shl
| Operator::I32ShrS
| Operator::I32ShrU
| Operator::I32Rotl
| Operator::I32Rotr
| Operator::I32Eq
| Operator::I32Ne
| Operator::I32LtS
| Operator::I32LtU
| Operator::I32GtS
| Operator::I32GtU
| Operator::I32LeS
| Operator::I32LeU
| Operator::I32GeS
| Operator::I32GeU => {
*depth = depth.saturating_sub(1);
}
_ => {
}
}
}