Code generation

This section describes how the generated code interacts with and supports the run time system. We need to look at function entry points and node descriptors.

Note: this section contains examples with generated assembly code for an x86_64 target. The sizes of values are given in .word (16 bits), .long (32 bits) and .quad (64 bits).

Function entry points

Each function has one or more entry points. Which entry point is used depends on the context in which the function is called. Entry points that are not needed are not generated.

As a running example, consider the entry points generated for ++:

.export e_StdList_s++
.export e_StdList_ea++
.descexp e_StdList_d++ e_StdList_n++ e_StdList_l++ 2 0 "++"
.a 2 e_StdList_ea++
.o 2 0
e_StdList_l++
    push_args 0 1 1
    update_a 2 1
    create
    update_a 0 3
    pop_a 1
.d 3 0
    jmp e_StdList_ea++
.n 2 e_StdList_d++ e_StdList_ea++
.o 1 0
e_StdList_n++
    push_node _cycle_in_spine 2
.o 3 0
e_StdList_ea++
    jsr_eval 0
.o 3 0
e_StdList_s++
    | actual implementation ...

The strict entry point

The strict entry point has the prefix s (e.g., e_StdList_s++). All other entry points eventually end up here. It contains the actual implementation of the function, with the assumption that all strict arguments have been evaluated: boxed arguments are in head normal form and passed as pointers on the A stack; unboxed basic values are passed on the B stack. Unboxed records and tuples are passed component-wise and may occupy both stacks. Lazy arguments, including basic values and algebraic data types, are passed as pointers on the A stack and may still refer to unevaluated thunks.

When evaluating a thunk, the run time system must update it with the result so that other references to the thunk share the computed value. Some strict entry points therefore expect a destination node below the arguments on the A stack, which the function fills with its result. When evaluating a thunk, that thunk serves as the destination. For a direct call without an existing thunk, the caller supplies an empty node instead, as in the create example below.

Whether this destination node is passed depends on the compiler’s representation of the result. It is not passed for unboxed basic results, unboxed records and tuples, arrays, or results returned as pointers to existing nodes (as in id :: a -> a). These entry points return their results on the stacks instead; when evaluating a thunk, the surrounding entry-point code handles updating it. Boxed record results can still require a destination node.

When the result of a function call is needed, the caller can call the strict entry point directly if it knows the function being called, supplies all its arguments, and has evaluated and arranged them in the expected representation. For a known function whose arguments still need preparation, the evaluate arguments entry point handles that work. For a higher-order call where the function is only known at run time, the application machinery must first inspect the closure and determine whether enough arguments have been supplied; it cannot simply jump to a statically known strict entry point.

A direct call can be seen in the code generated for Start = [] ++ []:

    buildh _Nil 0
    buildh _Nil 0
.d 3 0
    jmp e_StdList_s++

Here it is the Start thunk that is below the arguments on the stack and is overwritten with the result. In some contexts, an empty _cycle_in_spine node is created to hold the result:

    create 
    buildh _Nil 0
    buildh _Nil 0
.d 3 0 
    jsr e_StdList_s++

The evaluate arguments entry point

In contexts where it is uncertain that the strict arguments are in head normal form or unboxed, the evaluate arguments entry point is used. The prefix for this entry point is ea (e.g., e_StdList_ea++). It uses jsr_eval to evaluate the necessary arguments and may use instructions like pushI_a to unbox values from the A stack to the B stack. It then moves on to the strict entry point.

The node entry point

The node entry point has the prefix n (e.g., e_StdList_n++). It is used when evaluating a thunk. The descriptor of a thunk points directly to the node entry point. To evaluate a thunk, all that is needed is to jump into this entry point with the node itself on the stack.

The thunk arguments are pushed to the stack, and the thunk itself is overwritten with _cycle_in_spine. This allows the run time system to recognize the situation that the evaluation of a thunk depends on its own result, as in Start = let xs = [hd xs] in xs.

This brings the stack in the state required for the evaluate arguments entry point.

Node entry points are preceded by a .n ABC annotation, which generates the following data, in order:

  • A piece of code, used by the jmp_eval_upd instruction. Either:
    1. jsr_eval, fill_a, pop, rtn
    2. eval_upd
    3. jmp
  • A pointer to the descriptor of the function, which has the same contents as an ADT constructor of the same arity. The descriptor contains the name of the function which is needed to print the node.
  • The arity (a long). This is needed for the garbage collector. It is stored as (a_size + b_size) | (b_size << 8).

Example

This example comes from the (++) definition in StdList (++ gets translated to _A_A).

.n 2 e_StdList_d++ e_StdList_ea++
    lea e__StdList__ea_A_A(%rip),%rbp ; eval_upd
    jmp eval_upd_2                    ; eval_upd
    .long   e__StdList__d_A_A         ; pointer to descriptor
    .long   2                         ; arity
    .globl  e__StdList__n_A_A
e__StdList__n_A_A:                    ; start of actual function code

The lazy entry point

The lazy entry point, with prefix l (e.g., e_StdList_l++) is used for the implementation of currying. It covers the case that all arguments but one are in a closure and the final argument is provided. The closure is on top of the stack; the argument is right below. The lazy entry point creates a new node for the result and brings the stack in the state required for the evaluate arguments entry point.

Descriptors

The first word of every node in the heap points to a descriptor. This descriptor is a section in the data segment containing information about the node type which is used in various places:

  • In the garbage collector, to determine the arity and node layout.
  • In the driver in _system.abc, to print values correctly.

To be able to check efficiently whether a node is a head normal form or a thunk, the descriptor of head normal forms is bitwise OR’ed with 2. This leads to the following Assembly code for a jsr_eval instruction:

ea4:                       # evaluate arguments entry point
        testb   $2,(%rcx)  # check for head normal form
        jne     e_310      # if so, omit evaluation
        call    *(%rcx)    # if not, evaluate with the node entry point
e_310:                     # proceed

Because there are different kinds of nodes, different types of descriptors are generated:

Records

Descriptors for records (generated from .record ABC annotations) contain the following data, in order:

  • The arity (two words). This is needed for the garbage collector. It is stored as a_size + b_size + 256 followed by a_size.
  • The type string. In the ABC interpreter, this is preceded by its length in a long. This contains the type of each unboxed value (or a indicating an A stack argument), so that the run time system knows how to print them. The format is more complex for records containing records or tuples, but these are the basic types that can occur:

    • i: Int
    • c: Char
    • r: Real
    • b: Bool
    • f: File
  • The name of the record, preceded by its length in a long. This is needed to print the node.

Example

:: Record = { i :: !Int, b :: !Bool }
.record r7 ib 0 2 "Record"
r8:
    .word   258      ; a_size + b_size + 256
    .word   0        ; a_size
    .ascii  "ib"     ; type string
    .byte   0,0      ; (padding)
i_10:
l_3:
    .long   6        ; name length
    .ascii  "Record" ; record name

ADT constructors

Descriptors for ADT constructors (generated from .desc ABC annotations) contain the following data, in order:

  • The curry table.
  • The lazy record flag (a word).
  • The arity (a word). This is needed for the garbage collector.
  • The module label (a long, .long m__descriptors in the example below). This is 0 in the ABC interpreter.
  • The name of the constructor, preceded by its length in a long. This is needed to print the node.

Located just before the descriptor is a shared HNF node of arity 0 (.quad d6+2 in the example below, where 2 is the HNF flag). This is simply a pointer to the first element of the curry table. It is used in the garbage collector and code generator, for curried ADT constructors with no arguments supplied or for 0-argument ADT constructors.

Example

:: ADT = Pair Int Int
.desc d6 _hnf _add_arg 2 0 "Pair"
    .quad   d6+2
d6:
    .word   0                 ; curry table (entry 0)
    .word   16
    .long   yet_args_needed_0
    .word   1                 ; curry table (entry 1)
    .word   8
    .long   yet_args_needed_1
    .word   2                 ; curry table (entry 2)
    .word   0
    .word   0                 ; lazy record flag
    .word   2                 ; arity
    .long   m__descriptors    ; module label
i_1:
l_1:
    .long   4                 ; name length
    .ascii  "Pair"            ; ADT constructor name

Curry tables

For nodes that can be curried (ADT constructors and function closures) a curry table is generated. It is best to look at an example, the descriptor for ++:

e__StdList__d_A_A:
        .word   0
        .word   16
        .long   yet_args_needed_0
        .word   1
        .word   8
        .long   e__StdList__l_A_A
        .word   2
        .word   0

e_StdList__d_A_A is the e_StdList_d++ descriptor (see symbol names), which is generated with the following ABC code:

.descexp e_StdList_d++ e_StdList_n++ e_StdList_l++ 2 0 "++"

2 here is the A stack arity and 0 is the B stack arity. ++ is the name, for printing, and the labels refer to the relevant entry points.

When a node (++) is generated, it uses the d++ descriptor. This descriptor thus points to 0, the arity.

When currying one argument into the closure (i.e., (++) xs), the label at offset 4 (for this target platform) is used, i.e., yet_args_needed_0. This label is defined in the run time system and will create a closure or thunk with one argument.

The closure with one argument has the descriptor e__StdList__d_A_A+8: it points to 1, which is the arity. Again, to curry one additional argument in (i.e., ((++) xs) ys), we use the label, here e__StdList__l_A_A. This is the lazy entry point, because we are precisely in that situation: there is a closure that expects one argument, and the argument is being provided.

The numbers 16, 8 and 0 are the number of needed arguments, shifted to the left by 3: 2, 1 and 0, respectively. The actual amount by which these are shifted depends on the word size of the target platform, such that each is the offset to the last curry table entry. These are used when currying more than one argument at the time (e.g., (++) xs ys). The run time system checks whether the number of provided arguments matches the number of needed arguments. If this is the case, it finds the lazy entry and finds the evaluate arguments entry point from right above it (this is what the .a 2 e_StdList_ea++ directive above the lazy entry point is for). It moves all arguments from the closure to the stack (in addition to the curried arguments which are already on the stack), and calls the evaluate arguments entry point. However, if still more arguments are needed, a new closure is created.