Pattern 27Stack-Based Bytecode Interpreter

Purpose

This pattern executes bytecode instructions that store temporary values on an operand stack.

Discussion

A stack-based bytecode interpreter simulates a hardware processor with no general-purpose registers. That means that bytecode instructions must use an operand stack to hold temporary values. Temporary values include arithmetic operands, parameters, and return values. Because the introductory material for this chapter explained general bytecode interpreter architecture, we can focus on the details specific to stack machines.

Let’s look at the operand stack first, since it’s the key identifying feature. Any time we want to load a value from memory or compute a result, we push the result onto ...

Get Language Implementation Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.