Virtual Machines

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Overview

A virtual machine simulates what a microprocessor would do. It is actually just another program that takes instructions and does things with them.

Advantages & Disadvantages

One of the biggest advantages of having a virtual machine is that you can have the same platform for all OS's and architectures out there. That is, the same bytecodes compiled on one machine will work on another. This makes the job of writing programs easier. It's also pretty easy to get a decent virtual machine put together.

Disadvantages are that it would be a lot slower than real instructions.

Notable Examples

SquirrelFish is very interesting. It's a high-level register-based virtual machine for Javascript that is part of WebKit. See http://webkit.org/blog/189/announcing-squirrelfish/

Python and Java are other notable examples. Both of these compile their source code into bytecodes that run on a virtual machine.

Stack-based or Register-based?

There are two options, really:

  • Stack-based
  • Register-based

What's the difference?

A stack-based virtual machine doesn't use registers, but a single stack. In order to do an add, for instance, the first number is pushed onto the stack, then the second number, then the add operation takes the top two numbers on the stack, pulls them off, and pushed back the sum.

A register-based virtual machine uses registers rather than a stack. Note that to return from function calls and subroutines, there has to be a stack of some sort somewhere. In order to add two numbers in a register-based machine, you put the one number in one register, put the other number in a different register, and then use the add instruction that points to those two registers, as well as a third where the result will be stored.

It turns out that register-based virtual machines are much faster than stack-based ones. This is because there are far fewer memory copies involved. If you need to do something like calculate the Fibonacci Series, you can simply keep reusing the same two registers.

Register-based virtual machines are also very well understood, since every modern real microprocessor operates on such a model.

LLVM

See http://llvm.org/

This looks really, really interesting.