In binaryen, each pass is constructed by several visitors and walkers.
This page is about how to select suitable visitors and walkers for your passes.
in wasm-traversal.h, we can find all basic visitors and walkers.
There are two layers of abstraction here, visitors and walkers.
visitor
The visitor provides a set of interfaces for accessing expressions.
Visitor: will callvisit<OpCode>for each expression.UnifiedExpressionVisitor: will callvisitExpressionfor each expression.
walker
The walker is decoupled with visitors, is used to control the order of access expressions.
PostWalker: will call visitor in post-order, i.e., children first. Its access order is related to the execution order of the wasm bytecode.ExpressionStackWalker: based onPostWalker, and using a stack to store the parent expression.
hook point
Each walker maintains a stack to store operation (static method pointer and Expression *). Hook point can be found by searching self->pushTask(SubType::... in base class. These static methods need to be overridden in derived classes to hook.
A common used hook point:
scan can be used to change the visit behavior. We can insert specific methods into the scan or adjust the traversal order.