• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    C
  • License
    GNU General Publi...
  • Created about 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A few basic bytecode interpreters used as example code in a series of articles

What..?

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

Where?

Articles:

  1. Home-grown bytecode interpreters (source in Russian, Russian, English)
  2. When pigs fly: optimising bytecode interpreters (source in Russian, Russian, English)
  3. Regex bytecode interpreter: looking for needles in session haystacks (source in Russian, Russian, English)

Interpreter examples:

  1. A trival switch interpreter.
  2. Immediate operand instruction example.
  3. A stack vm.
  4. A register vm.
  5. A regular expression matching machine.
  6. Various main loop implementations for PigletVM.
  7. 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 Sum of Numbers
  2. Naive implementation of the 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 @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:

> # 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

Want a proper language for PigletVM? PigletC to the rescue!

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, event sequence matcher.

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.