Implementing a bytecode interpreter
A bytecode interpreter runs the following algorithm, which implements a fetch-decode-execute loop in software. Most bytecode interpreters use at least two registers almost continuously: an instruction pointer and a stack pointer. The Jzero machine also includes a base pointer register to track function call frames and a heap pointer register that holds a reference to a current object.
While the instruction pointer is referenced explicitly in the following fetch-decode-execute loop pseudocode, the stack pointer is used almost as frequently, but it’s more often used implicitly as a byproduct of the instruction semantics of most opcodes:
load the bytecode into memory
initialize interpreter state
repeat {
fetch the next instruction, advance the instruction pointer
decode the instruction
execute the instruction
}
Bytecode interpreters are usually implemented in a low-level systems programming language such as C, rather than...