Operand-Stack Bytecode and Stack Effects

Track the stack height of arithmetic bytecode and check that different incoming paths leave compatible operand states.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Each operation has a stack signature
  2. A control-flow merge expects one shape

Each operation has a stack signature

In a stack machine, pushing constants \(2\) and \(3\) yields two operands. An add instruction consumes both and pushes \(5\), changing height by minus one. Pushing \(4\) and multiplying yields \(20\). Operand order matters for subtraction and division, so the instruction contract must identify which popped item supplies each input. A bytecode format can verify underflow statically when enough structure is known.

A control-flow merge expects one shape

If one path to an addition leaves two integers and another leaves just one, the instruction does not have a well-defined input under that verifier. Propagate stack states through the control-flow graph and check merges against the format’s height and type rules. Registers in a lower-level representation can name these stack values explicitly. SSA joins make the correspondence between incoming stack slots and merged values visible.[1][2]

References

  1. Shriram Krishnamurthi, Programming Languages: Application and Interpretation ↩
  2. Robert Nystrom, Crafting Interpreters ↩