Module sage::vm

source ·
Expand description

Virtual Machine Module

This module contains all things related to the virtual machine.

What is this machine?

This virtual machine is a simple turing tape machine. There is one register, a tape of cells, and a tape pointer. Cells are restricted to integers in the core variant of the machine code, but floats are supported in the standard variant.

What should I know first?

You should NOT use pointers (values used with the Deref, Refer, Where, Alloc, and Free operations) as if they are integers. Think about pointers and integers as two completely separate types.

Why?

This is because virtual machine implementations are bound to vary. For example: my C implementation uses real pointers (which are retrieved through virtual machine instructions Where and Alloc, and allows the implementation to be used with valgrind, gprof, a custom allocater, or potentially garbage collection!), but an implementation in a language like Python might use integer indices in a list instead.

If the backend implementation uses pointers, using Inc to move a pointer to the next cell will not work. This is because pointers need to be incremented by the size of the data type they point to. Because the virtual machine’s cell size is undefined (purposely, to make it as portable as possible), you cannot know this size. Therefore you cannot use Inc to move a pointer to the next cell unless you want your code to be unportable.

DO NOT USE Inc AND Dec TO MOVE POINTERS! NAVIGATE THE POINTER TO THE DESIRED POSITION AND USE Where INSTEAD! OR YOUR CODE WILL NOT PORT TO ALL IMPLEMENTATIONS!!

What data can it use?

The virtual machine uses cells of any arbitrary bit width >= 16. The tape must contain at least 4096 cells. The bit width is undefined, but it must remain constant for every cells. Additionally, the floating point (and pointer) representation must be identical in size to the integer representation.

In this particular assembler, we assume that the bit width is 64. Supporting smaller or larger bit widths is supported just by using integers of the appropriate size, or using addition / multiplication to reach larger numbers.

An implementation of the virtual machine should be of any reasonable bit width like: 16, 32, 64 bits (the standard), or unbounded. For each implementation, the bits of the integer and floats supported should be identical. The default implementation should be with 64 bit ints and floats, but 16 bit ints + no floats for a hardware implementation would suffice. Infinitely large ints and floats are also supported, but the implementation must be able to handle them.

Structs

  • The interpreter which runs the virtual machine program.
  • A program of only core virtual machine instructions.
  • A device used for standard input and output. This simply retrieves a character from standard-in with get, and writes a character to standard-out with put.
  • The interpreter which runs the standard variant of virtual machine programs.
  • A program of core and standard virtual machine instructions.
  • A device used for testing the compiler. This simply keeps a buffer of sample input to supply to the virtual machine, and keeps an output buffer to keep track of the output of the virtual machine.

Enums

  • An individual core virtual machine instruction.
  • An error generated by the virtual machine.
  • An individual standard virtual machine instruction.

Traits

  • Create an input / output device for the virtual machine interpreter to operate on. The method get retrieves the device’s input, and the function put writes to the devices output.
  • An interface to conveniently create virtual machine programs, of either the core or standard variant.

Functions

  • A function to reinterpret the bits of an integer as a float.
  • A function to reinterpret the bits of a float as an integer.