NAME

src/gc/register.c - Register handling routines

DESCRIPTION

Parrot has 4 register sets, one for each of its basic types. The number of registers in each set varies depending on the use counts of the subroutine and is determined by the PASM/PIR compiler in the register allocation pass (imcc/reg_alloc.c).

Context and register frame layout

    +----------++----+------+------------+----+
    | context  || N  |  I   |   P        |  S +
    +----------++----+------+------------+----+
    ^          ^     ^                   ^
    |          |     ctx.bp              ctx.bp_ps
    ctx.state  opt
               padding

Registers are addressed as usual via the register base pointer ctx.bp.

The macro CONTEXT() hides these details

Context and register frame allocation

There are two allocation strategies: chunked memory and malloced with a free list.

 CHUNKED_CTX_MEM = 1

ctx_mem.data is a pointer to an allocated chunk of memory. The pointer ctx_mem.free holds the next usable location. With (full) continuations the ctx_mem.free pointer can't be moved below the ctx_mem.threshold, which is the highest context pointer of all active continuations.

[the code for this is incomplete; it had suffered some bit-rot and was getting in the way of maintaining the other case. -- rgr, 4-Feb-06.]

RT #46177 GC has to lower this threshold when collecting continuations.

 CHUNKED_CTX_MEM = 0

Context/register memory is malloced. ctx_mem.free is used as a free list of reusable items.

Round register allocation size up to the nearest multiple of 8. A granularity of 8 is arbitrary, it could have been some bigger power of 2. A "slot" is an index into the free_list array. Each slot in free_list has a linked list of pointers to already allocated contexts available for (re)use. The slot where an available context is stored corresponds to the size of the context.

Context and Register Allocation Functions

void destroy_context
Frees allocated context memory.
void create_initial_context
Creates the interpreter's initial context.
void parrot_gc_context
Cleans up dead context memory; called by the garbage collector. This only applies in the chunked context memory scheme.
static void clear_regs
Clears all registers in a context. PMC and STRING registers contain PMCNULL and NULL, respectively. Integer and float registers contain negative flag values, for debugging purposes.
static void init_context
Initializes a freshly allocated or recycled context.
Parrot_Context *Parrot_push_context
Creates and sets the current context to a new context, remembering the old context in caller_ctx. Suitable to use with Parrot_pop_context.
void Parrot_pop_context
Frees the context created with Parrot_push_context and restores the previous context (the caller context).
Parrot_Context *Parrot_alloc_context
Allocates and returns a new context. Does not set this new context as the current context. Note that the register usage n_regs_used is copied. Use the init flag to indicate whether you want to initialize the new context (setting its default values and clearing its registers).
Parrot_Context *Parrot_set_new_context
Allocates and returns a new context as the current context. Note that the register usage n_regs_used is copied.
void Parrot_free_context
Frees the context if its reference count is zero. If deref is true, then reduce the reference count prior to determining if the context should be freed.
Parrot_Context *Parrot_context_ref_trace
Helper function to trace references when CTX_LEAK_DEBUG is set.
void Parrot_set_context_threshold
Marks the context as possible threshold.

Register Stack Functions

void Parrot_clear_i
Sets all integer registers in the current context to 0.
void Parrot_clear_s
Sets all STRING registers in the current context to NULL.
void Parrot_clear_p
Sets all PMC registers in the current context to NULL.
void Parrot_clear_n
Sets all number registers in the current context to 0.0.

SEE ALSO

include/parrot/register.h and src/stacks.c.