IU-P423-P523-E313-E513-Fall-2020

Web page for IU Compiler Course for Fall 2020

View the Project on GitHub IUCompilerCourse/IU-P423-P523-E313-E513-Fall-2020

Garbage Collection

Def. The live data are all of the tuples that might be accessed by the program in the future. We can overapproximate this as all of the tuples that are reachable, transitively, from the registers or procedure call stack. We refer to the registers and stack collectively as the root set.

The goal of a garbage collector is to reclaim the data that is not live.

We shall use a 2-space copying collector, using Cheney’s algorithm (BFS) for the copy.

Alternative garbage collection techniques:

Overview of how GC fits into a running program.:

  1. Ask the OS for 2 big chunks of memory. Call them FromSpace and ToSpace.
  2. Run the program, allocating tuples into the FromSpace.
  3. When the FromSpace is full, copy the live data into the ToSpace.
  4. Swap the roles of the ToSpace and FromSpace and go back to step 1.

Draw Fig. 5.6. (just the FromSpace)

Graph Copy via Cheney’s Algorithm

An implementation of a garbage collector is in runtime.c.

Data Representation

type-check

Add cases for the new expressions. (Fig 5.1)

type ::= ... | (Vector type+) | Void
exp ::= ... | (vector exp+) | (vector-ref exp int)
    | (vector-set! exp int exp) | (void)

To help the GC identify vectors (pointers to the heap), wrap every sub-expression with its type. This information will be propagated in the flatten pass to all variables.

(HasType exp type)

shrink

Add HasType to the generated code.

expose-allocation (new)

Lower vector creation into a call to collect, a call to allocate, and then initialize the memory (see 5.3.1).

Make sure to place the code for sub-expressions prior to the sequence collect-allocate-initialize. Sub-expressions may also call collect, and we can’t have partially constructed vectors during collect!

New forms in the output language:

    exp ::= 
       (Collect int)       call the GC and you're going to need `int` bytes
     | (Allocate int type) allocate `int` bytes
     | (GlobalValue name)  access global variables

remove-complex-opera*

The new forms Collect, Allocate, GlobalValue should be treated as complex operands.

Add case for HasType.

Adapt case for Prim to make sure the enclosing HasType does not get separated from it.