An execution model whose instructions obtain operands from a stack and put results back on that stack.
Code whose operand-producing instructions precede the instruction that consumes those operands. A stack machine can translate a binary program expression by producing its left operand, producing its right operand, and then applying the operation to the two top values.
Suppose each subtree of a binary expression tree is evaluated completely before its sibling, leaves one result on an operand stack, and is not spilled or interleaved. If the child depths are , evaluating the first child needs slots and keeping its result while evaluating the second needs . Reversing the order gives . If either order preserves the expression's meaning, the minimum is when and when . For example, if , integrality gives , so evaluating the deeper child first attains the lower bound . If , the second child needs its own slots plus one saved result regardless of order. Induction over the binary expression tree proves optimality in this evaluation model.
For a noncommutative operator, an instruction swapping the top two stack entries or a reverse-operand opcode permits the same ordering while preserving operand positions. Reordering still requires absence of order-sensitive side effects. Reassociation and interleaving are separate transformations and are not part of this optimality claim.
Articles by others on the same topic
A stack machine is a type of computer architecture that primarily uses a stack for managing data and executing instructions. Instead of using registers for operations, a stack machine relies on a last-in, first-out (LIFO) data structure—a stack—to handle its operations. ### Key Characteristics of Stack Machines: 1. **Data Management**: - Operands for operations are pushed onto the stack.