The TinyVM is a small, fast and lightweight virtual machine written in pure ANSI C. The source code of this toy virtual machine can easily be read and understood in just a few minutes.
The create_vm (tvm.c) function creates a virtual machine instance by allocating its memory and stack. It also parses a TinyVM program that contains TinyVM assembly language opcodes from a text file. The interpret_program (tvm_program.c) function reads the TinyVM program into a list of instructions and into a further list containing the arguments for all instructions.
The TinyVM has some registers which are mapped to memory (see field registers in struct tvm_memory_t of file tvm_memory.h).
After parsing a program the TinyVM runs it using run_vm (tvm.c). This function contains the interpreter loop of the virtual machine which executes the program by interpreting the list of instructions.
Some more interesting resources about virtual machines:
- Virtual Machine Showdown: Stack Versus Registers: Virtual machine architectures and performance considerations.
- The evolution of the Erlang VM: The BEAM Erlang VM replaces a lot of traditional operating system concepts. E.g. Erlang provides no threads. Instead it allows for up to millions of concurrent Erlang processes. The Erlang virtual machine schedules this processes using a reduction count scheduling strategy (see erlang:bump_reductions). To make the best use of the underlying CPU capabilities the Erlang VM creates one OS thread on each available CPU core. The Erlang VM scheduler then runs all its Erlang processes on those OS threads. For more details about the Erlang programming language take a look into Joe Armstrong's Erlang book.
- Google Android's Dalvik VM: Internals about Android's virtual machine.
- The History and Future of the CLR by Ian Carmichael: Microsoft's .NET CLR (Common Language Runtime) VM.