Subroutine Logo
Subroutine
← Back to Playground Languages Intermediate

Virtual Machine Bytecode Execution Engines

An interactive programming languages exploration of CPython and JVM stack machines, instruction pointers, and bytecode dispatch loops.

Published: 2026-07-26
#Languages#Virtual Machines#Bytecode#Python#Java#Compilers

High-level programming languages like Python (CPython), Java (JVM), and JavaScript (V8) do not compile high-level source code directly into hardware machine instructions. Instead, compilers translate source code into an intermediate format called Bytecode, which is then evaluated by a virtual machine Bytecode Interpreter Loop.


1. Summary & Key Takeaways

  • Stack-Based Virtual Machines: Most language runtimes (JVM, CPython, V8 Ignition) use a LIFO Operand Stack to evaluate operations rather than CPU hardware registers.
  • Instruction Pointer (IP): Tracks the current bytecode instruction byte in memory during execution.
  • Bytecode Dispatch Loop: A high-speed switch-case or computed goto loop that pops operands, executes math/logic operations, and pushes results back onto the stack.
  • JIT Compilation Optimization: Runtimes like JVM HotSpot and V8 translate frequently executed “hot” bytecode sequences into native assembly instructions at runtime!

2. Interactive Bytecode VM Simulator

Use the interactive stack machine below to step through complex multi-frame bytecode formulas!

Stack Machine Execution

Click Step 1 Bytecode Instruction to watch operands get pushed onto the LIFO stack and evaluated step-by-step into the local frame variable table!

Virtual Machine Bytecode Execution EngineStack Height: 0

Step through CPython / JVM bytecode instructions. Watch the LIFO Operand Stack grow and shrink!

Bytecode Instruction Stream
0: PUSH 10
1: PUSH 20
2: PUSH 3
3: MUL
4: ADD
5: PUSH 8
6: PUSH 2
7: DIV
8: SUB
9: STORE result
LIFO Operand StackMax Height: 3
[ Empty Stack ]
Local Frame Variables
[ No Variables Stored ]
Virtual Machine Code
bytecode_vm.py
Python (dis module)
# Disassembling Complex Python Bytecode
import dis

def compute_formula():
    x = 4
    # (10 + 20 * 3) - (8 / 2)
    result = (10 + 20 * 3) - (8 // 2)
    return result

# Disassemble into CPython opcode stack operations
dis.dis(compute_formula)

3. Stack Machine vs. Register Machine Architecture

1. Stack-Based Virtual Machine (CPython, JVM)

Operands are pushed onto and popped from a LIFO evaluation stack:

PUSH 10   ; Stack: [10]
PUSH 20   ; Stack: [10, 20]
ADD       ; Pops 10 & 20, Pushes 30 -> Stack: [30]

2. Register-Based Virtual Machine (Lua JIT, Android Dalvik/ART)

Operands are read and written directly to virtual CPU registers (R0,R1,R2R_0, R_1, R_2):

LOAD  R0, 10      ; R0 = 10
LOAD  R1, 20      ; R1 = 20
ADD   R2, R0, R1  ; R2 = R0 + R1 (R2 = 30)

4. Virtual Machine Architecture Comparison

Runtime EngineVM TypeDispatch MechanismJIT Compiler Included?
CPython (Python)Stack Machineswitch-case loopPython 3.11+ JIT (Copy-and-Patch)
JVM (Java)Stack MachineDirect ThreadingHotSpot C1 / C2 JIT Compiler
V8 Engine (JS)Hybrid Stack/RegisterBytecode (Ignition)TurboFan Optimizing Compiler