README.org
November 27, 2025 ยท View on GitHub
- What..?
A few basic bytecode interpreters used as example code in a series of articles.
- Building
** Using Make (Unix/Linux)
Given a recent GCC, all the interpreters can be compiled using the supplied Makefile:
#+BEGIN_SRC shell make all make test #+END_SRC
** Using CMake (Cross-platform)
For cross-platform builds (including Windows), use CMake:
#+BEGIN_SRC shell cmake -B build cmake --build build cmake --build build --target test #+END_SRC
- Where?
Articles:
- Home-grown bytecode interpreters ([[file:interpreter-p1.org][source in Russian]], [[https://habr.com/company/badoo/blog/425325/][Russian]], [[https://badootech.badoo.com/home-grown-bytecode-interpreters-51e12d59b25c][English]])
- When pigs fly: optimising bytecode interpreters ([[file:interpreter-p2-pigletvm.org][source in Russian]], [[https://habr.com/company/badoo/blog/428878/][Russian]], [[https://badootech.badoo.com/when-pigs-fly-optimising-bytecode-interpreters-f64fb6bfa20f][English]])
- Regex bytecode interpreter: looking for needles in session haystacks ([[file:interpreter-p3-matcher.org][source in Russian]], [[https://habr.com/company/badoo/blog/433054/][Russian]], [[https://badootech.badoo.com/regex-bytecode-interpreter-looking-for-needles-in-session-haystacks-9bbff9db09bc][English]])
Interpreter examples:
- [[file:interpreter-basic-switch.c][A trival switch interpreter]].
- Immediate [[file:interpreter-immediate-arg.c][operand instruction example]].
- [[file:interpreter-stack-machine.c][A stack vm]].
- [[file:interpreter-register-machine.c][A register vm]].
- A [[file:interpreter-regexp.c][regular expression matching machine]].
- Various main loop implementations for [[file:pigletvm.h][PigletVM]].
- [[file:piglet-matcher.h][Regular expression matcher]] defined on event sequences.
- PigletVM, a trivial stack machine
PigletVM is a simple stack machine created for testing various bytecode interpreter main loop implementations.
PigletVM examples in PVM assembly:
- A trivial [[file:test/sum.pvm][Sum of Numbers]]
- Naive implementation of the [[file:test/sieve.pvm][Sieve of Eratosthenes]]
Base techinques implemented:
- basic switch
- basic switch with the switch value range check eliminated
- token threaded code
- trace interpreter
Thanks to [[https://github.com/iliazeus][@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:
#+BEGIN_EXAMPLE
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
#+END_EXAMPLE
- Want a proper language for PigletVM? PigletC to the rescue!
Apart from assembler there's a better way to write PigletVM programs. [[https://github.com/true-grue][@true-grue]] somehow managed to implement a proper language compiled into PigletmVM assembler: [[https://github.com/true-grue/PigletC][PigletC]]!
- PigletMatcher, event sequence matcher.
PigletMatcher is a regular expression engine defined for event sequences. It comes in two pieces: the [[file:piglet-matcher.h][virtual machine itself]] and [[file:regexp/regexp.py][a parser]] based on the same tool that was used for [[https://github.com/true-grue/PigletC][PigletC]].