Next: , Previous: , Up: machinery   [Index]


16.3 Basics of the Scheme stack

We assume the validity of machinery simplifications to focus on some aspect of the runtime behaviour, Simplification assumptions.

When running C code: the state of the Scheme stack is represented by the fields of the PCB data structure. When running Scheme code: the state of the Scheme stack is represented by the Frame Pointer Register (FPR) and some fields of the PCB data structure.

When the execution flow moves from C language code to Scheme language code or from Scheme code to C code: Assembly language routines are executed to perform some low–level operations. Such Assembly code takes care of synchronising the PCB fields with the CPU registers.

Let’s start by taking a look at an empty Scheme stack segment, machinery call frames. The stack memory segment begins at the machine word referenced by the PCB field stack_base (included) and ends at the machine word referenced by the PCB field frame_base (excluded). stack_base changes only when the stack overflows, frame_base changes when the stack overflows and every time a continuation object is created.

      high memory
|                      |
|----------------------|
|                      | <- pcb->frame_base
|----------------------|                     --
           .                                 .
           .                                 . stack
           .                                 . segment
|----------------------|                     . size
|                      | <- pcb->stack_base  .
|----------------------|                     --
|                      |
       low memory

Figure 16.1: Size of the Scheme stack segment.

The stack space is consumed starting from the high memory addresses towards the low memory addresses; the first used word on the Scheme stack is right below the machine word referenced by frame_base.

Upon creation: the first machine word on the stack is initialised to the address of the Assembly routine ik_underflow_handler; when the execution flow of Scheme code returns to such address: the routine takes care of switching from Scheme code to C code and itself returns to a C function.

When running C code: the machine word holding the address of the underflow handler is referenced by the PCB field frame_pointer, machinery call frames. When running Scheme code: the machine word holding the address of the underflow handler is referenced by the FPR register, machinery call frames.

      high memory
|                      |
|----------------------|
|                      | <- pcb->frame_base
|----------------------|
| ik_underflow_handler | <- pcb->frame_pointer
|----------------------|
            .
            .
            .
|----------------------|
|                      | <- pcb->stack_base
|----------------------|
|                      |
       low memory

Figure 16.2: Empty Scheme stack as represented in the PCB structure while running C code.

      high memory
|                      |
|----------------------|
|                      | <- pcb->frame_base
|----------------------|
| ik_underflow_handler | <- Frame Pointer Register (FPR)
|----------------------|
            .
            .
            .
|----------------------|
|                      | <- pcb->stack_base
|----------------------|
|                      |
       low memory

Figure 16.3: Empty Scheme stack as represented in the CPU registers and the PCB structure while running Scheme code.


Next: , Previous: , Up: machinery   [Index]