1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
//! Implementations of various IR traversals.
use crate::ir::*;
/// Perform an intra-procedural, depth-first, in-order traversal of the IR.
///
/// * *Intra-procedural*: Only traverses IR within a function. Does not cross
/// function boundaries (although it will report edges to other functions via
/// `visit_function_id` calls on the visitor, so you can use this as a
/// building block for making global, inter-procedural analyses).
///
/// * *Depth-first, in-order*: Visits instructions and instruction sequences in
/// the order they are defined and nested. See [Wikipedia][in-order] for
/// details.
///
/// Calls `visitor` methods for every instruction, instruction sequence, and
/// resource that the traversal visits.
///
/// The traversals begins at the `start` instruction sequence and goes from
/// there. To traverse everything in a function, pass `func.entry_block()` as
/// `start`.
///
/// This implementation is iterative — not recursive — and so it
/// will not blow the call stack on deeply nested Wasm (although it may still
/// OOM).
///
/// [in-order]: https://en.wikipedia.org/wiki/Tree_traversal#In-order_(LNR)
///
/// # Example
///
/// This example counts the number of instruction sequences in a function.
///
/// ```no_run
/// use walrus::LocalFunction;
/// use walrus::ir::*;
///
/// #[derive(Default)]
/// struct CountInstructionSequences {
/// count: usize,
/// }
///
/// impl<'instr> Visitor<'instr> for CountInstructionSequences {
/// fn start_instr_seq(&mut self, _: &'instr InstrSeq) {
/// self.count += 1;
/// }
/// }
///
/// // Get a function from somewhere.
/// # let get_my_function = || unimplemented!();
/// let my_func: &LocalFunction = get_my_function();
///
/// // Create our visitor.
/// let mut visitor = CountInstructionSequences::default();
///
/// // Traverse everything in the function with our visitor.
/// dfs_in_order(&mut visitor, my_func, my_func.entry_block());
///
/// // Use the aggregate results that `visitor` built up.
/// println!("The number of instruction sequences in `my_func` is {}", visitor.count);
/// ```
pub fn dfs_in_order<'instr>(
visitor: &mut impl Visitor<'instr>,
func: &'instr LocalFunction,
start: InstrSeqId,
) {
// The stack of instruction sequences we still need to visit, and how far
// along in the instruction sequence we are.
let mut stack: Vec<(InstrSeqId, usize)> = vec![(start, 0)];
'traversing_blocks: while let Some((seq_id, index)) = stack.pop() {
let seq = func.block(seq_id);
if index == 0 {
// If the `index` is zero, then we haven't processed any
// instructions in this sequence yet, and it is the first time we
// are entering it, so let the visitor know.
visitor.start_instr_seq(seq);
seq.visit(visitor);
}
'traversing_instrs: for (index, (instr, loc)) in seq.instrs.iter().enumerate().skip(index) {
// Visit this instruction.
log::trace!("dfs_in_order: visit_instr({:?})", instr);
visitor.visit_instr(instr, loc);
// Visit every other resource that this instruction references,
// e.g. `MemoryId`s, `FunctionId`s and all that.
log::trace!("dfs_in_order: ({:?}).visit(..)", instr);
instr.visit(visitor);
match instr {
// Pause iteration through this sequence's instructions and
// enqueue `seq` to be traversed next before continuing with
// this one where we left off.
Instr::Block(Block { seq }) | Instr::Loop(Loop { seq }) => {
stack.push((seq_id, index + 1));
stack.push((*seq, 0));
continue 'traversing_blocks;
}
// Pause iteration through this sequence's instructions.
// Traverse the consequent and then the alternative.
Instr::IfElse(IfElse {
consequent,
alternative,
}) => {
stack.push((seq_id, index + 1));
stack.push((*alternative, 0));
stack.push((*consequent, 0));
continue 'traversing_blocks;
}
// No other instructions define new instruction sequences, so
// continue to the next instruction.
_ => continue 'traversing_instrs,
}
}
// If we made it through the whole loop above, then we processed every
// instruction in the sequence, and its nested sequences, so we are
// finished with it!
visitor.end_instr_seq(seq);
}
}
/// Perform an intra-procedural, depth-first, pre-order, mutable traversal of
/// the IR.
///
/// * *Intra-procedural*: Only traverses IR within a function. Does not cross
/// function boundaries (although it will report edges to other functions via
/// `visit_function_id` calls on the visitor, so you can use this as a
/// building block for making global, inter-procedural analyses).
///
/// * *Depth-first, pre-order*: Visits instructions and instruction sequences in
/// a top-down manner, where all instructions in a parent sequences are
/// visited before child sequences. See [Wikipedia][pre-order] for details.
///
/// Calls `visitor` methods for every instruction, instruction sequence, and
/// resource that the traversal visits.
///
/// The traversals begins at the `start` instruction sequence and goes from
/// there. To traverse everything in a function, pass `func.entry_block()` as
/// `start`.
///
/// This implementation is iterative — not recursive — and so it
/// will not blow the call stack on deeply nested Wasm (although it may still
/// OOM).
///
/// [pre-order]: https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR)
///
/// # Example
///
/// This example walks the IR and adds one to all `i32.const`'s values.
///
/// ```no_run
/// use walrus::LocalFunction;
/// use walrus::ir::*;
///
/// #[derive(Default)]
/// struct AddOneToI32Consts;
///
/// impl VisitorMut for AddOneToI32Consts {
/// fn visit_const_mut(&mut self, c: &mut Const) {
/// match &mut c.value {
/// Value::I32(x) => {
/// *x += 1;
/// }
/// _ => {},
/// }
/// }
/// }
///
/// // Get a function from somewhere.
/// # let get_my_function = || unimplemented!();
/// let my_func: &mut LocalFunction = get_my_function();
///
/// // Create our visitor.
/// let mut visitor = AddOneToI32Consts::default();
///
/// // Traverse and mutate everything in the function with our visitor.
/// dfs_pre_order_mut(&mut visitor, my_func, my_func.entry_block());
/// ```
pub fn dfs_pre_order_mut(
visitor: &mut impl VisitorMut,
func: &mut LocalFunction,
start: InstrSeqId,
) {
let mut stack = vec![start];
while let Some(seq_id) = stack.pop() {
let seq = func.block_mut(seq_id);
visitor.start_instr_seq_mut(seq);
seq.visit_mut(visitor);
for (instr, loc) in &mut seq.instrs {
visitor.visit_instr_mut(instr, loc);
instr.visit_mut(visitor);
match instr {
Instr::Block(Block { seq }) | Instr::Loop(Loop { seq }) => {
stack.push(*seq);
}
Instr::IfElse(IfElse {
consequent,
alternative,
}) => {
stack.push(*alternative);
stack.push(*consequent);
}
_ => {}
}
}
visitor.end_instr_seq_mut(seq);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Default)]
struct TestVisitor {
visits: Vec<String>,
}
impl TestVisitor {
fn push(&mut self, s: impl ToString) {
self.visits.push(s.to_string());
}
}
impl<'a> Visitor<'a> for TestVisitor {
fn start_instr_seq(&mut self, _: &'a InstrSeq) {
self.push("start");
}
fn end_instr_seq(&mut self, _: &'a InstrSeq) {
self.push("end");
}
fn visit_const(&mut self, c: &Const) {
match c.value {
Value::I32(x) => self.push(x),
_ => unreachable!(),
}
}
fn visit_drop(&mut self, _: &Drop) {
self.push("drop");
}
fn visit_block(&mut self, _: &Block) {
self.push("block");
}
fn visit_if_else(&mut self, _: &IfElse) {
self.push("if-else");
}
}
impl VisitorMut for TestVisitor {
fn start_instr_seq_mut(&mut self, _: &mut InstrSeq) {
self.push("start");
}
fn end_instr_seq_mut(&mut self, _: &mut InstrSeq) {
self.push("end");
}
fn visit_const_mut(&mut self, c: &mut Const) {
match &mut c.value {
Value::I32(x) => {
self.push(*x);
*x += 1;
}
_ => unreachable!(),
}
}
fn visit_drop_mut(&mut self, _: &mut Drop) {
self.push("drop");
}
fn visit_block_mut(&mut self, _: &mut Block) {
self.push("block");
}
fn visit_if_else_mut(&mut self, _: &mut IfElse) {
self.push("if-else");
}
}
fn make_test_func(module: &mut crate::Module) -> &mut LocalFunction {
let block_ty = module.types.add(&[], &[]);
let mut builder = crate::FunctionBuilder::new(&mut module.types, &[], &[]);
builder
.func_body()
.i32_const(1)
.drop()
.block(block_ty, |block| {
block
.i32_const(2)
.drop()
.if_else(
block_ty,
|then| {
then.i32_const(3).drop();
},
|else_| {
else_.i32_const(4).drop();
},
)
.i32_const(5)
.drop();
})
.i32_const(6)
.drop();
let func_id = builder.finish(vec![], &mut module.funcs);
module.funcs.get_mut(func_id).kind.unwrap_local_mut()
}
#[test]
fn dfs_in_order() {
let mut module = crate::Module::default();
let func = make_test_func(&mut module);
let mut visitor = TestVisitor::default();
crate::ir::dfs_in_order(&mut visitor, func, func.entry_block());
let expected = [
"start", "1", "drop", "block", "start", "2", "drop", "if-else", "start", "3", "drop",
"end", "start", "4", "drop", "end", "5", "drop", "end", "6", "drop", "end",
];
assert_eq!(
visitor.visits,
expected.iter().map(|s| s.to_string()).collect::<Vec<_>>()
);
}
#[test]
fn dfs_pre_order_mut() {
let mut module = crate::Module::default();
let func = make_test_func(&mut module);
let mut visitor = TestVisitor::default();
crate::ir::dfs_pre_order_mut(&mut visitor, func, func.entry_block());
let mut expected = vec![];
// function entry
expected.extend(vec!["start", "1", "drop", "block", "6", "drop", "end"]);
// block
expected.extend(vec!["start", "2", "drop", "if-else", "5", "drop", "end"]);
// consequent
expected.extend(vec!["start", "3", "drop", "end"]);
// alternative
expected.extend(vec!["start", "4", "drop", "end"]);
assert_eq!(
visitor.visits,
expected.iter().map(|s| s.to_string()).collect::<Vec<_>>()
);
// And then check that the increments of the constant values did indeed
// take effect.
visitor.visits.clear();
crate::ir::dfs_in_order(&mut visitor, func, func.entry_block());
let expected = [
"start", "2", "drop", "block", "start", "3", "drop", "if-else", "start", "4", "drop",
"end", "start", "5", "drop", "end", "6", "drop", "end", "7", "drop", "end",
];
assert_eq!(
visitor.visits,
expected.iter().map(|s| s.to_string()).collect::<Vec<_>>()
);
}
}