[virtmach] VM with garbage collection

Kasper Verdich Lund virtmach@iecc.com
Fri, 11 Jan 2002 11:17:58 +0100

> I am trying to build a VM for a garbage collected language.
> The byte code will typically contain many references to
> garbage collected objects. This means that the GC must be
> somehow able to distinguish bytecode instructions from
> pointers to GC-able objects.

So you want the references embedded in the bytecode stream? In that case you
can just let the GC iterate through the bytecodes. Consider a pseudo-Java
bytecode sequence like:

    new XXX

This might be represented in bits as:

    [ 0xaa ] [ 0xbb ] [ reference to XXX ] [ 0xaa ]
     1 byte   1 byte        4 bytes         1 byte

To do the interpretation of these bytecodes, you would skip over the
reference to XXX by adding 5 to the bytecode index instead of 1 after
executing the new bytecode. The GC can do the same. When looking for
pointers in the bytecode, you simply scan through the bytecodes and only
when you reach opcode 0xbb, you know that the next 4 bytes are in fact a
reference to a garbage collected object (in this case a class).

Take care,