IU-Fall-2021

Course web page for Fall 2021.

View the Project on GitHub IUCompilerCourse/IU-Fall-2021

Graph Copy via Cheney’s Algorithm

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

Data Representation

Problems:

  1. how to differentiate pointers from other things on the procedure call stack?
  2. how can the GC access the pointers that are in registers?
  3. how to differentiate poitners from other things inside tuples?

Solutions:

  1. Use a root stack (aka. shadow stack), i.e., place all tuples in a separate stack that works in parallel to the normal stack. Draw Fig. 5.7.
  2. Spill vector-typed variables to the root stack if they are live during a call to the collector.
  3. Add a 64-bit header or “tag” to each tuple. (Fig. 5.8) The header includes
    • 1 bit to indicate forwarding (0) or not (1). If 0, then the header is the forwarding pointer.
    • 6 bits to store the length of the tuple (max of 50)
    • 50 bits for the pointer mask to indicate which elements of the tuple are pointers.

type checking and type information

Racket: The type-check-Rvec function wraps a HasType around each vector creation expression.

       (HasType (Prim 'vector es) (Vector ts))

Python: The type_check_Ltup writes the tuple type into a new has_type field in the Tuple AST node.

This type information is used in the expose_allocation pass.

Racket: The type-check-Cvec function stores an alist mapping variables to their types in the info field, under the key locals-types, of the CProgram AST node.

Python: The type_check_Ctup stores a dictionary mapping variables to their types into a new var_types field of the CProgram AST node.

This type information is used in the register allocator and in the generation of the prelude and conclusion.

expose-allocation (new)

Lower tuple 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 call to collect. Sub-expressions may also call collect, and we can’t have partially constructed tuples 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` many bytes, `type` is the type of the tuple
     | (GlobalValue name)  access global variables e.g. free_ptr, fromspace_end

For Python, we need a way to intialize the tuple elements.

  1. We introduce a Begin expression that contains a list of statements and a result expression, and
  2. Allow Subscript on the left-hand side of an assignment

Grammar:

exp ::= ... | (Begin stmt ... exp)

lhs ::= Name(var) | Subscript(exp, exp)
stmt ::= ... | Assign(lhs, exp)

remove-complex-opera*

The new forms Collect, Allocate, GlobalValue, Begin, Subscript should be treated as complex operands. Operands of Subscript need to be atomic.

explicate-control

straightforward additions to handle the new forms

select-instructions

Here is where we implement the new operations needed for tuples.

example: block9056

allocate-registers

prelude-and-conclusion