A bytecode interpreter is software that reads and executes bytecode instructions, usually one instruction at a time. Bytecode is an intermediate representation produced from source code and designed to run on a virtual machine, rather than directly on a physical processor.
How a bytecode interpreter works
The execution process normally has two stages:
- A compiler translates human-readable source code into bytecode.
- The bytecode interpreter repeatedly fetches, decodes, and executes each bytecode instruction.
For example, a source statement such as total = price + tax may be translated into instructions that load price, load tax, add the values, and store the result in total. The interpreter performs these operations through the virtual machine.
Because the bytecode targets a virtual machine, the same bytecode can run on different operating systems and processor architectures, provided each system has a compatible virtual machine. This improves portability. However, interpreting instructions during execution can be slower than running native machine code directly.
| Translation approach | How execution occurs | Main implication |
|---|---|---|
| Source-code interpreter | Reads and executes source statements directly | Simple execution but repeated translation work may be required |
| Bytecode interpreter | Executes previously generated intermediate instructions | Portable and usually more efficient than direct source interpretation |
| Native compiler | Produces machine code for a specific processor | Fast execution but reduced portability |
Some virtual machines also use just-in-time compilation (JIT), which translates frequently executed bytecode into native machine code while the program runs. Therefore, a virtual machine is not necessarily limited to interpretation.