Run Time Stack - Ch 7 - Part 2 of 2
This lecture for Wednesday 18 Oct 06
s0 for globals
s1 for display level 1 (d[1])
s2 for display level 2 (d[2])
s3 for display level 3 (d[3])
top_sp Top of Stack (TOS)
-----------------------------------------------------------
This is a continuation from lecture1.rts providing another example.
---------------
| space |
| for |
| vars |
---------------
| old level ? | control
Activation record (AR) for a procedure | return addr | info
---------------
*** AR(1) refers to AR for P1; AR(2) refers to AR for P2; AR(3) for P3
and AR(4) for P4 (since each proc with have similar control information,
and different space for local variables
***
The size of the activation record is known at compile time,
so we can set put the appropriate pushes/pops
In lecture 1, we used $s0 for globals, $s1 for level 1, $s2 for level 2
$s3 for level 3 and $top_sp for top of stack (TOS)
The code outline looks like:
main
proc P1
proc P2
proc P3
end 3;
proc P4
invokes proc P3 or proc P4 (recursively)
end 4;
invokes proc P3
invokes proc P4
end 2;
invokes proc P2
end 1;
invokes proc P1
end main
Assume:
Main calls P1 calls P2 calls P3 and P3 returns
calls P4 calls P4' calls P4'' calls P4''' calls P3
Set up for the first part of this calling sequence:
Main calls P1 calls P2 calls P3
The Display:
---------------------------
| calls P1 P2 P3 |
|--------------------------
| d[3] ? ? P3 |
---------------------------
| d[2] ? P2 P2 |
---------------------------
| d[1] P1 P1 P1 |
---------------------------
| saved: ? ? ? |
---------------------------
The Display Picture of the RTS
-------------------------------
$top_sp -> ------------------
|local vars (P3) |
| : old d[3] |----> (old $s3 never set)
|AR(3): return |
$s3 -> ------------------
|local vars (P2) |
| : old d[2] |----> (old $s2 never set)
|AR(2): return |
$s2 -> ------------------
|local vars (P1) |
| : old d[1] |----> (old $s1 never set)
|AR(1): return |
$s1 -> ------------------
| |
| global vars |
$s0 -> ------------------
|Code P1, P2, P3 |
| |
| code main |
------------------
and now we continue, therefore P3 has returned.
Main calls P1 calls P2 calls P4 calls P4(') calls P4('') calls P4(''')
calls P3
---------------------- ------------------------------
| calls P1 P2 P4 P4' P4'' P4''' P3 |
|--------------------- ------------------------------
| d[3] ? ? P4 P4' P4'' P4''' P3 |
---------------------- ------------------------------
| d[2] ? P2 P2 P2 P2 P2 P2 |
---------------------- ------------------------------
| d[1] P1 P1 P1 P1 P1 P1 P1 |
---------------------- ------------------------------
| saved: ? ? ? P4 P4' P4'' P4''' |
---------------------- -----------------------------
$top_sp -> ------------------
|local vars (P3) |
| : old d[3] |----------------------------------*
|AR(3): return | |
$s3 -> ------------------ |
|local vars (P4) | |
| : old d[3] |--------------------------------* |
|AR(4): return | | |
------------------ <<-----------------------------|-*
|local vars (P4) | |
| : old d[3] |-----------------------------* |
|AR(4): return | | |
------------------ <<--------------------------|--*
|local vars (P4) | |
| : old d[3] |--------------------------* |
|AR(4): return | | |
------------------ <<-----------------------|--*
|local vars (P4) | |
| : old d[3] |----> (old $s3 never set) |
|AR(4): return | |
------------------ <<-----------------------*
|local vars (P2) |
| : old d[2] |----> (old $s2 never set)
|AR(2): return |
$s2 -> ------------------
|local vars (P1) |
| : old d[1] |----> (old $s1 never set)
|AR(1): return |
$s1 -> ------------------
| |
| global vars |
$s0 -> ------------------
|Code P1, P2, P3 |
| |
| code main |
------------------
return to Ch7 Run Time Environment lecture