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:

  1. 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]])
  2. 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]])
  3. 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:

  1. [[file:interpreter-basic-switch.c][A trival switch interpreter]].
  2. Immediate [[file:interpreter-immediate-arg.c][operand instruction example]].
  3. [[file:interpreter-stack-machine.c][A stack vm]].
  4. [[file:interpreter-register-machine.c][A register vm]].
  5. A [[file:interpreter-regexp.c][regular expression matching machine]].
  6. Various main loop implementations for [[file:pigletvm.h][PigletVM]].
  7. [[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:

  1. A trivial [[file:test/sum.pvm][Sum of Numbers]]
  2. Naive implementation of the [[file:test/sieve.pvm][Sieve of Eratosthenes]]

Base techinques implemented:

  1. basic switch
  2. basic switch with the switch value range check eliminated
  3. token threaded code
  4. trace interpreter

Thanks to [[https://github.com/iliazeus][@iliazeus]] we now have a second set of the same interpreters with stack top cached:

  1. basic switch with stack top cache
  2. switch with no range check and stack top cache
  3. token threaded code with a stack cache
  4. 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]].