GHC Runtime - Stack and Heap

June 9, 2017
haskell

Stack and heap are important data structures for program execution. First we will review the general design of stack and heap [5], [6].

General Review of Heap and Stack

Static object reside in the compiled code and are not moved. Dynamic objects reside in the heap and may be moved by a garbage collector (if that is part of the programming language).

Stack

Heap

Features of the GHC Runtime system

GHC Runtime Heap and Stack

Objects on the stack and heap have almost the same structure. Each object starts with a header, which points to an info table with a closure, and then each type of object has its own payload.

Stack frame info tables have one extra field than heap info tables, SRT field, which points to the static reference that has information about what static objects are referenced by the closure.

There is one difference between heap object payloads and stack object payloads. If a heap object payload has pointers and non-pointers, it will list all the pointers before all of the non-pointers. Stack frame objects may have mixed order pointers and non-pointers.

Objects

Info Table

Contains all the information that runtime needs to know about closures.

Type of Objects

List of object names and their payload structures. All objects start with a header and are then followed by a payload (structure differs between objects).

Thread State Object

Every Thread State Object (TSO) has a stack. TSOs are ordinary objects that live in the heap and can take advantage of existing allocation and garbage collection to manage them. Garbage collection can detect when a blocked thread is unreachable and make it never runnable again.

Boxed and Lifted Types

Int is boxed. Int# is unboxed. Unboxed types are suffixed with a #. The representation of bottom _|_ is a pointer and when evaluated it will throw an exception or enter an infinite loop.

References