959 words
5 minutes
What a Virtual Machine Actually Does: Think of It as an Emulator
阅读中文版

I have been following a tutorial that rebuilds a simple SQLite clone in C. The architecture diagram in chapter one has a box right in the middle labelled virtual machine.

That stopped me. The virtual machines I knew were EC2 and VirtualBox. Why would a database need one?

After some digging I found that Python, Java and Ethereum all have a VM too, and the easiest way to understand one is through something many of us have used: a game emulator.

1. Different CPUs read different instructions#

A CPU only reads one thing: machine code. The catch is that different CPUs read different machine code. My Mac has an M-series chip (ARM). Many PCs have Intel or AMD chips (x86).

I wrote the simplest C function, a + b * 2, and compiled it for each:

ARM (M-series): add w0, w0, w1, lsl #1
x86 (Intel/AMD): lea eax, [rdi + rsi*2]

Same calculation, completely different instructions and bytes. So a compiled C program runs on one kind of CPU only. For another kind, you compile again.

2. A VM is an emulator#

Plenty of people have played old Game Boy games on a laptop with an emulator.

  • The game file holds instructions written for the Game Boy’s CPU. The laptop’s CPU can’t read them.
  • The emulator is an ordinary program on the laptop that pretends to be a Game Boy.
  • It reads one game instruction, does that step with its own code, then reads the next.

Python does exactly the same thing:

EmulatorPython
game filebytecode (.pyc files)
emulator programthe VM (the python3 you installed)
the laptop’s CPUthe Mac’s M-series chip

The one difference: a Game Boy is a real machine, while Python’s VM pretends to be a machine that was never built. Python invented its instructions. Code written in those instructions is called bytecode.

Python’s built-in dis module shows the bytecode for a + b * 2:

LOAD_FAST_BORROW_LOAD_FAST_BORROW (a, b)
LOAD_SMALL_INT 2
BINARY_OP (*)
BINARY_OP (+)
RETURN_VALUE

Each line is simple: get a and b, get 2, multiply, add, return. These are instructions for the pretend CPU.

Two paths: C/Go/Rust compile straight to machine code the CPU reads; Python/Java/SQLite/EVM compile to bytecode, which a VM runs, the way a computer runs an emulator

3. An emulator doesn’t translate; it does the work itself#

This is the part I got wrong at first. I thought the VM translated bytecode into ARM instructions and sent them to the CPU.

It doesn’t. Think about the emulator: it is an ordinary program installed on the laptop. When it reads the game instruction “jump”, it runs its own, already written code for “jump”. It doesn’t produce a new program on the spot.

python3 is the same. Here is what file says on my Mac:

python3.14: Mach-O 64-bit executable arm64

It is an ordinary arm64 program. The CPU is always running python3’s own code; the bytecode only decides which part of it runs next.

(The thing that does translate bytecode into machine code on the spot is a JIT. Java and the JavaScript engine in your browser do this to run faster.)

So when I type python3 file.py, this happens:

file.py
│ python3 compiles it to bytecode (automatic)
▼
bytecode ← the same on every machine
│ python3's VM runs it one instruction at a time
▼
the M-series chip is running the python3 program

4. What a VM is good for#

1. The same code runs everywhere. This is the main point. The emulator has a Windows build and a Mac build, but there is only one game file, and it plays on both. Python is the same: one copy of the bytecode, and each kind of computer installs its own build of python3.

The same bytecode goes to three different python3 builds: arm64 on an M-series Mac, x86-64 on an Intel/AMD PC, and Linux arm64 on an ARM server

Java’s “write once, run anywhere” is exactly this. It is also why Python programmers almost never think about which CPU their code will run on.

2. Compile once, run many times. Understanding source code takes work. Once it is bytecode, that work isn’t repeated. Python keeps the bytecode in __pycache__/ and reuses it if the source hasn’t changed.

This also answers my opening question. SQLite compiles every SQL statement into bytecode and runs it on its own VM. Put EXPLAIN in front of a query and you can see the instructions, for example:

OpenRead open the index
SeekGE find the target position in the index
ResultRow hand back one row

Compile a statement once, then run it many times with different values: that is a prepared statement.

3. The VM makes its own rules. An emulator can do things the real console can’t, like save anywhere or run at double speed. A VM is the same, because it is just a program and can add any rule it likes. Ethereum’s EVM adds three:

  • A contract can’t touch your computer. The EVM simply has no instruction for “read a file” or “use the network”.
  • Every machine must get the same result. So there is no instruction for “read the current time” or “make a random number” either.
  • Every instruction costs money. This is gas. An addition costs 3 gas, and when the gas runs out, the program stops. Even an infinite loop only runs until the money is gone.

A real CPU can’t do any of this.

In one paragraph#

Different CPUs read different machine code. A VM is an emulator: an ordinary program that pretends to be a machine, except the machine was designed by Python, Java or Ethereum. Code is compiled once into bytecode, each kind of computer installs its own build of the VM, and the code runs everywhere. The VM doesn’t translate; it reads an instruction and does that step with code it already has. As for why EC2 is also called a VM: it pretends to be a whole computer, while Python’s VM only pretends to be a CPU. Both are pretending to be a machine.