Mini-vm
A small, register-based virtual machine (bytecode interpreter) in C.
Project maintained by philipaconrad
Hosted on GitHub Pages — Theme by mattgraham
A small vm for small projects
Mini-vm is a small (~300 SLOC) bytecode interpreter meant to be used as a starting point for home-made interpreted languages.
How it works:
- Each instruction has to be of the same 3-address format.
- The VM has a dispatch table of function pointers that all have the same signature.
- When the instruction is processed, its Instruction ID serves as an index into that dispatch table.
- The instruction runs on the provided data, changes state where required, etc.
How the virtual machine is built:
- The interpreter is a register-based machine, with a static number of registers set at compile time. (can't exceed 255, due to bytecode format)
- The compiler for the VM is provided by the programmer.
- Semantics of instructions are also left up to the programmer.
- The VM only covers handling registers and instruction execution. It doesn't even try to address concurrency, stack ops, etc.
Instruction Format:
Each instruction consists of 4 bytes, stored in little-endian byte order:
0 1 2 3
+---+---+---+---+
| A | B | C | D |
+---+---+---+---+
- A - Instruction ID. This is the index of the function in the function table you wish to call.
- B - Register #1. (usually the destination register)
- C - Register #2. (usually an argument source register)
- D - Register #3. (usually an argument source register)
Get:
$ git clone https://github.com/philipaconrad/mini-vm.git
Build:
-
Linux-likes:
If you're using a Linux-like platform (or Cygwin), just use
make
to build the Mini-vm example program. Use make test
to generate sample bytecode and run the example program.
-
Windows:
If you're on a modern Windows platform and have MSVC 2010 installed, just go to the
/msvc
directory and double click on the .vcxproj file. MSVC should generate a solution file for you, all set up and ready to build.
License
Mini-vm is under the liberal MIT License, so hack away!
Authors and Contributors
Currently, Mini-vm is maintained by @philipaconrad. If you'd like to contribute, just make a pull request.
Support or Contact
Having trouble with Mini-vm? Check out the issues page and we’ll help you sort it out.