Next: objects continuations system, Previous: objects continuations intro, Up: objects continuations [Index]
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
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
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
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
Next: objects continuations system, Previous: objects continuations intro, Up: objects continuations [Index]