sage::asm

Module location

Source
Expand description

§Assembly Memory Location

This module contains the Location type, which represents a memory location on the virtual machine’s tape in the assembly language.

§What should I know first?

You should NOT use pointers 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 this backend as flexible 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! USE Next AND Prev INSTEAD! OR YOUR CODE WILL NOT PORT TO ALL VIRTUAL MACHINE IMPLEMENTATIONS!!

§What locations are available?

There are several constant locations to use:

  • BOTTOM_OF_STACK: The bottom of the stack.
  • TMP: A volatile register. Essentially a trashcan.
  • SP: The stack pointer. SP.deref() is the location of the top item on the stack.
  • FP: The frame pointer. Automatically updated by Call and Return. The FP register points to the top of the stack when the function was called.
  • FP_STACK: The stack of frame pointers. Whenever the program starts, a frame pointer stack is initialized. Whenever a function is called, the old frame pointer is pushed to the FP_STACK. Whenever a function returns, it pops the frame pointer from the FP_STACK.
  • A, B, C, D, E, F: General purpose registers.

§What kinds of locations are there?

There are three kinds of locations:

  • Constant addresses: these are addresses that are known at compile time. They’re simply just offsets from the base of the tape.
  • Offset addresses: these are addresses that are relative to other addresses. You might want to dereference a pointer at an address, and then move the pointer by an offset. This would be represented like:
    Offset(Indirect(Address(6)), -2) // go the address stored at the 6th cell
                                     // from the start of the tape, and move 2 cells back
  • Indirect addresses: these are dereferenced addresses. To dereference a value stored at another location, use the Indirect constructor.
    Indirect(Address(6)) // go the address pointed to by the value in the 6th cell of the tape

Enums§

  • A location in memory (on the tape of the virtual machine).

Constants§

  • The “A” general purpose register.
  • The “B” general purpose register.
  • The “C” general purpose register.
  • The “D” general purpose register.
  • The “E” general purpose register.
  • The “F” general purpose register.
  • The frame pointer register.
  • The Global Pointer register. This is used to access global variables.
  • The stack pointer register.