Expand description
Stack shuffler for optimal stack layout transitions.
This module implements a greedy shuffler algorithm similar to solc’s approach. The shuffler converts a source stack layout to a target layout using minimal DUP, SWAP, and POP operations.
§Algorithm Overview
The shuffler uses a greedy approach with multiplicity tracking:
- Count how many copies of each value are needed in the target
- DUP values that need multiple copies
- SWAP values to correct positions
- POP excess values
Key insight: Values that can be “freely generated” (literals, function labels) don’t need to be in the source stack - they can be pushed when needed.
Structs§
- Block
Stack Layout - Represents a stack layout for a block - the values that should be on the stack when entering or exiting a block, from top to bottom.
- Layout
Analysis - Computes the ideal entry layout for an instruction given the desired exit layout.
- Shuffle
Result - Result of a shuffle operation.
- Stack
Shuffler - The stack shuffler transforms a source stack layout to a target layout.
Enums§
- Freely
Generable - Represents a “freely generable” value that can be pushed without being on the stack. These values don’t need to be in the source layout when shuffling.
- Target
Slot - Represents a slot in the target layout.
Functions§
- combine_
stack_ layouts - Computes a common stack layout for multiple incoming edges at a merge point.
- estimate_
shuffle_ cost - Computes the shuffle cost from a source layout to a target layout. This estimates the number of DUP/SWAP/POP operations needed.
- ideal_
binary_ op_ entry - Computes the ideal pre-layout for a binary operation.
- ideal_
operand_ layout - Creates an ideal layout for preparing multiple operands.
- ideal_
unary_ op_ entry - Computes the ideal pre-layout for a unary operation.
- is_
freely_ generable - Checks if a value can be freely generated (doesn’t need to be on stack).