A few basic bytecode interpreters used as example code in a series of articles. Given a recent GCC all the interpreters can be compiled in one go using a supplied Makefile:
make all
make test
Articles:
- Home-grown bytecode interpreters (source in Russian, Russian, English)
- When pigs fly: optimising bytecode interpreters (source in Russian, Russian, English)
- Regex bytecode interpreter: looking for needles in session haystacks (source in Russian, Russian, English)
Interpreter examples:
- A trival switch interpreter.
- Immediate operand instruction example.
- A stack vm.
- A register vm.
- A regular expression matching machine.
- Various main loop implementations for PigletVM.
- Regular expression matcher defined on event sequences.
PigletVM is a simple stack machine created for testing various bytecode interpreter main loop implementations.
PigletVM examples in PVM assembly:
- A trivial Sum of Numbers
- Naive implementation of the Sieve of Eratosthenes
Base techinques implemented:
- basic switch
- basic switch with the switch value range check eliminated
- token threaded code
- trace interpreter
Thanks to @iliazeus we now have a second set of the same interpreters with stack top cached:
- basic switch with stack top cache
- switch with no range check and stack top cache
- token threaded code with a stack cache
- trace interpreter with a stack cache
Compiling and running PigletVM assembler examples:
> # build all vms > make all > # Assemble the program and run it > ./pigletvm asm test/sieve.pvm test/sieve.bin > ./pigletvm run test/sieve.bin > /dev/null 07:54:24 PROFILE: switch code finished took 20ms PROFILE: switch code (no range check) finished took 16ms PROFILE: threaded code finished took 7ms PROFILE: trace code finished took 8ms PROFILE: switch code (reg cache) finished took 4ms PROFILE: switch code (reg cache) (no range check) finished took 3ms PROFILE: threaded code (reg cache) finished took 2ms PROFILE: trace code (reg cache) finished took 5ms > # Run the assembled program a number of times: > ./pigletvm runtimes test/sieve.bin 100 > /dev/null 07:54:25 PROFILE: switch code finished took 430ms PROFILE: switch code (no range check) finished took 384ms PROFILE: threaded code finished took 472ms PROFILE: trace code finished took 363ms PROFILE: switch code (reg cache) finished took 350ms PROFILE: switch code (reg cache) (no range check) finished took 304ms PROFILE: threaded code (reg cache) finished took 255ms PROFILE: trace code (reg cache) finished took 301ms
Apart from assembler there’s a better way to write PigletVM programs. @true-grue somehow managed to implement a proper language compiled into PigletmVM assembler: PigletC!
PigletMatcher is a regular expression engine defined for event sequences. It comes in two pieces: the virtual machine itself and a parser based on the same tool that was used for PigletC.