Soil

A low-level VM

by Marcel Garus · 2024-9-19
available at www.marcelgarus.dev/soil

When implementing my programming language Martinaise, I was faced with a difficult challenge. I wanted to achieve both of these goals simultaneously:

The solution is pretty obvious:

Any problem in computer science can be solved with another layer of indirection. [...]

So, I invented Soil.

Similar to fantasy consoles such as PICO-8, Soil is a specification for a VM. It is on a similar abstraction level as existing CPU instruction sets: There are registers, memory, and instructions.

Because Soil is low-level, it's easy to write interpreters. There already exist a few implementations written in C, Zig, Dart, Rust, and x86_64 assembly.

Now, I can write Martinaise functions in platform-independent Soil assembly:

fun memcopy(fromAddresstoAddressamountIntasm {
  moveib 8  add a sp load a a | from
  moveib 16 add b sp load b b | to
  moveib 24 add c sp load c c | amount
  moveib 1
  cmp a b isless cjump .right_to_left
  .left_to_right: ..loop:
  move st c isequal cjump .done
  loadb d a storeb b d
  add a e add b e sub c e
  jump ..loop
  .right_to_left:
  add a c add b c sub a e sub b e | make a and b point to the last byte
  ..loop:
  move st c isequal cjump .done
  loadb d a storeb b d
  sub a e sub b e sub c e
  jump ..loop
  .doneret
}