Executing bytecode07 / 19

How an interpreter works

How Ignition executes bytecode instructions and produces a function's result.

In the previous lesson, we generated bytecode from an AST. Now Ignition reads that bytecode and performs the specified operations one at a time.

Each instruction has a handler: the part of the interpreter that performs its operation. The CPU executes the machine code of Ignition’s handlers.

Consider the function add(a, b), which returns the sum of its arguments. When called as add(2, 3), its instructions first load 2, then add 3, and return 5.

Executing add(2, 3)

function add(a, b) {
  return a + b;
}
add(2, 3);

Teaching pseudobytecode: the instruction names are illustrative. One click executes one instruction.

  1. LOAD a→ Next
    Load a
  2. ADD bWaiting
    Add b
  3. RETURNWaiting
    Return the value
Accumulator
—

The arguments are set: a = 2, b = 3. The accumulator has no value yet.

0 / 3
Key takeaway

Ignition reads an instruction, runs its handler, and moves to the next one. The CPU executes the machine code of the interpreter's handlers.

Check the primary source ↗

V8 · Ignition↗The official description of the register-based interpreter.V8 · Ignition handlers and accumulator↗Instruction handlers perform operations; the accumulator stores intermediate results.