macro_rules! for_each_operator { ($m:ident) => { ... }; }
Expand description
Used to implement routines for the Operator
enum.
A helper macro to conveniently iterate over all opcodes recognized by this
crate. This can be used to work with either the Operator
enumeration or
the VisitOperator
trait if your use case uniformly handles all operators
the same way.
It is also possible to specialize handling of operators depending on the Wasm proposal from which they are originating.
This is an “iterator macro” where this macro is invoked with the name of another macro, and then that macro is invoked with the list of all operators.
The list of specializable Wasm proposals is as follows:
@mvp
: Denoting a Wasm operator from the initial Wasm MVP version.@exceptions
: Wasmexception-handling
proposal@tail_call
: Wasmtail-calls
proposal@reference_types
: Wasmreference-types
proposal@sign_extension
: Wasmsign-extension-ops
proposal@saturating_float_to_int
: Wasmnon_trapping_float-to-int-conversions
proposal@bulk_memory
:Wasmbulk-memory
proposal@simd
: Wasmsimd
proposal@relaxed_simd
: Wasmrelaxed-simd
proposal@threads
: Wasmthreads
proposal@gc
: Wasmgc
proposal@stack_switching
: Wasmstack-switching
proposal@wide_arithmetic
: Wasmwide-arithmetic
proposal
fn do_nothing(op: &wasmparser::Operator) {
macro_rules! define_match_operator {
// The outer layer of repetition represents how all operators are
// provided to the macro at the same time.
//
// The `$proposal` identifier indicates the Wasm proposals from which
// the Wasm operator is originating.
// For example to specialize the macro match arm for Wasm SIMD proposal
// operators you could write `@simd` instead of `@$proposal:ident` to
// only catch those operators.
//
// The `$op` name is bound to the `Operator` variant name. The
// payload of the operator is optionally specified (the `$(...)?`
// clause) since not all instructions have payloads. Within the payload
// each argument is named and has its type specified.
//
// The `$visit` name is bound to the corresponding name in the
// `VisitOperator` trait that this corresponds to.
//
// The `$ann` annotations give information about the operator's type (e.g. binary i32 or arity 2 -> 1).
($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => {
match op {
$(
wasmparser::Operator::$op $( { $($arg),* } )? => {
// do nothing for this example
}
)*
_ => unreachable!(), // required because `Operator` enum is non-exhaustive
}
}
}
wasmparser::for_each_operator!(define_match_operator);
}
If you only wanted to visit the initial base set of wasm instructions, for example, you could do:
fn is_mvp_operator(op: &wasmparser::Operator) -> bool {
macro_rules! define_match_operator {
// delegate the macro invocation to sub-invocations of this macro to
// deal with each instruction on a case-by-case basis.
($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => {
match op {
$(
wasmparser::Operator::$op $( { $($arg),* } )? => {
define_match_operator!(impl_one @$proposal)
}
)*
_ => unreachable!(), // required because `Operator` enum is non-exhaustive
}
};
(impl_one @mvp) => { true };
(impl_one @$proposal:ident) => { false };
}
wasmparser::for_each_operator!(define_match_operator)
}