Programming Bytecode Interpreters

Programming a bytecode interpreter is very much like programming a real processor. The biggest difference is that bytecode instruction sets are much simpler and slightly higher-level. For example, we don’t have to worry about running out of registers in a register-based interpreter. (We can assume there are an infinite number of registers.)

Like the assembly language for a physical processor, each instruction only does a tiny amount of work. For example, it takes four instructions to say print 1+2 in the assembly language syntax of Pattern 28, Register-Based Bytecode Interpreter:

 
iload r1, 1 ; load int 1 into register one: r1 = 1
 
iload r2, 2 ; r2 = 2
 
iadd r1, r2, r3 ; r3 = r1 + r2
 
print r3 ; print value ...

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.