Next: , Previous: , Up: objects continuations   [Index]


13.25.2 Scheme continuation objects

To understand the contents of a continuation object, we have to recall how the Scheme stack is used, Continuations implementation. We also need to remember that stack segments are consumed starting from the highest memory addresses, growing towards the lowest memory addresses.

Whenever a Scheme stack segment is allocated, the fields of the PCB structure are initialised such that: stack_base references the lowest machine word in the segment, frame_base references the highest word right after the end of the segment, frame_pointer references the highest word of the segment which is initialised to the memory address of the assembly routine ik_underflow_handler; objects continuations scheme.

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

Figure 13.1: Empty Scheme stack segment as it appears right after allocation and initialisation.

Whenever Scheme functions are called, and they perform nested calls: stack frames are created, starting from the highest machine words; let’s say there are 2 of them, objects continuations scheme. The CPU’s Frame Pointer Register (FPR) references the return address in the topmost stack frame (while executing Scheme code FPR gets out of sync with pcb->frame_pointer).

      high memory
|                        |
|------------------------|
|                        | <-- pcb->frame_base
|------------------------|                       --
|  ik_underflow_handler  |                       .
|------------------------|        --             .
|   local value frame 1  |        .              .
|------------------------|        .              .
|   local value frame 1  |        . framesize 1  .
|------------------------|        .              .
| return address frame 1 |        .              .
|------------------------|        --             .
|   local value frame 0  |        .              .
|------------------------|        .              . stack
|   local value frame 0  |        . framesize 0  . segment
|------------------------|        .              .
| return address frame 0 | <- FPR .              .
|------------------------|        --             .
|     call argument      |                       .
|------------------------|                       .
|     call argument      |                       .
|------------------------|                       .
           ...                                   .
|------------------------|                       .
|                        | <-- pcb->stack_base   .
|------------------------|                        --
|                        |
      low memory

Figure 13.2: 2 frames on the Scheme stack.

If a continuation object is created now: it “freezes” (or “seals”) all the stack frames in the stack segment, so that they will never be mutated again; in practice the portion of the Scheme stack referenced by the continuation is no more part of the Scheme stack itself, rather the stack segment is resized to reference only the portion of memory still unused, objects continuations scheme.

      high memory
|                        |
|------------------------|
|  ik_underflow_handler  |
|------------------------|                       --
|   local value frame 1  |                       .
|------------------------|                       .
|   local value frame 1  |                       .
|------------------------|                       .
| return address frame 1 |                       . continuation
|------------------------|                       . size
|   local value frame 0  |                       .
|------------------------|                       .
|   local value frame 0  |                       .
|------------------------|                       .
| return address frame 0 | <- pcb->frame_base    .
|------------------------|                       --
|  ik_underflow_handler  | <- FPR                .
|------------------------|                       .
|     call argument      |                       .
|------------------------|                       . stack
|     call argument      |                       . segment
|------------------------|                       .
           ...                                   .
|------------------------|                       .
|                        | <-- pcb->stack_base   .
|------------------------|                       --
|                        |
      low memory

Figure 13.3: freezed stack frames and resized stack segment.

The field top of the continuation object is an untagged memory pointer referencing the return address in the topmost freezed frame (frame 0 in the pictures); such machine word is the address of the code execution return point of this continuation, in other words: the address of the next assembly instruction to execute when returning to this continuation.

The field size of the continuation object is the number of bytes in all the freezed stack frames this continuation references: the sum between the all frame sizes.

Notice that the single continuation object referencing all the freezed frames is equivalent to two continuation objects each referencing a single freezed frame, provided that the continuation object of frame 0 has the continuation object of frame 1 as next, objects continuations scheme.

      high memory
|                        |
|------------------------|
|  ik_underflow_handler  |
|------------------------|           --
|   local value frame 1  |           .
|------------------------|           . continuation
|   local value frame 1  |           . size 1
|------------------------|           .
| return address frame 1 | <- top 1  .
|------------------------|           --
|   local value frame 0  |           .
|------------------------|           . continuation
|   local value frame 0  |           . size 0
|------------------------|           .
| return address frame 0 | <- top 0  .
|------------------------|           --
|                        |
      low memory

     next_k
|...|------|...| PCB structure
       |
       v
      tag  top  size next
     |----|----|----|----| continuation object 0
                      |
               -------
              v
             tag  top  size next
            |----|----|----|----| continuation object 1
                            NULL

Figure 13.4: 2 chained continuation objects equivalent to the single continuation object.


Next: , Previous: , Up: objects continuations   [Index]