|
1 | 1 | use std::{
|
2 | 2 | collections::{btree_map::Entry, BTreeMap},
|
3 |
| - env::var, |
| 3 | + iter::Peekable, |
4 | 4 | ops::RangeFrom,
|
5 | 5 | str::FromStr,
|
6 | 6 | };
|
@@ -906,7 +906,7 @@ fn infinite_registers_allocation<'a>(
|
906 | 906 | );
|
907 | 907 |
|
908 | 908 | // The rest of the directives are taken from the function body definition.
|
909 |
| - let mut op_reader = body.get_operators_reader()?.into_iter(); |
| 909 | + let mut op_reader = body.get_operators_reader()?.into_iter().peekable(); |
910 | 910 | while let Some(operator) = op_reader.next() {
|
911 | 911 | // There shouldn't be any more operators after the outmost
|
912 | 912 | // block (the function itself) has ended.
|
@@ -1174,6 +1174,7 @@ fn infinite_registers_allocation<'a>(
|
1174 | 1174 | }
|
1175 | 1175 | }
|
1176 | 1176 | }
|
| 1177 | + assert!(tracker.control_stack.is_empty()); |
1177 | 1178 |
|
1178 | 1179 | Ok(directives)
|
1179 | 1180 | }
|
@@ -1584,28 +1585,37 @@ impl<'a> StackTracker<'a> {
|
1584 | 1585 | /// and fix the stack.
|
1585 | 1586 | fn discard_unreachable_and_fix_the_stack(
|
1586 | 1587 | &mut self,
|
1587 |
| - op_reader: &mut OperatorsIterator<'_>, |
| 1588 | + op_reader: &mut Peekable<OperatorsIterator<'_>>, |
1588 | 1589 | ) -> wasmparser::Result<()> {
|
1589 | 1590 | // Discard unreachable code
|
1590 | 1591 | let mut stack_count = 0;
|
1591 |
| - for operator in op_reader { |
1592 |
| - match operator? { |
1593 |
| - Operator::Block { .. } | Operator::Loop { .. } | Operator::If { .. } => { |
| 1592 | + |
| 1593 | + // We do a peek loop so we leave Else and End operators in the iterator, |
| 1594 | + // to be handled by the caller. |
| 1595 | + while let Some(operator) = op_reader.peek() { |
| 1596 | + match operator { |
| 1597 | + Ok(Operator::Block { .. }) |
| 1598 | + | Ok(Operator::Loop { .. }) |
| 1599 | + | Ok(Operator::If { .. }) => { |
1594 | 1600 | stack_count += 1;
|
1595 | 1601 | }
|
1596 |
| - Operator::Else => { |
| 1602 | + Ok(Operator::Else) => { |
1597 | 1603 | if stack_count == 0 {
|
1598 | 1604 | break;
|
1599 | 1605 | }
|
1600 | 1606 | }
|
1601 |
| - Operator::End => { |
| 1607 | + Ok(Operator::End) => { |
1602 | 1608 | if stack_count == 0 {
|
1603 | 1609 | break;
|
1604 | 1610 | }
|
1605 | 1611 | stack_count -= 1;
|
1606 | 1612 | }
|
1607 |
| - _ => {} |
| 1613 | + Ok(_) => {} |
| 1614 | + Err(_) => { |
| 1615 | + return Err(op_reader.next().unwrap().unwrap_err()); |
| 1616 | + } |
1608 | 1617 | }
|
| 1618 | + op_reader.next(); |
1609 | 1619 | }
|
1610 | 1620 |
|
1611 | 1621 | // Fix the stack
|
|
0 commit comments