Professional Documents
Culture Documents
1 2 3 4
5
call call
B C
6
9 8 7
return
return return
Flow of control
Chapter 5, Slide 1
Preserving function’s context: the activation frame
returned value
actual arguments
saved machine
status
local data
temporaries
Chapter 5, Slide 2
Allocation of activation frame:
• static (no multiple activations, no recursion!)
• dynamic on the heap (activation tree does not reflect the
true activations)
• dynamic on the stack (control stack!!)
Passing of arguments:
• call-by-value (C,C++, Java) --- expressions evaluated,
their values are passed
• call-by-reference [call-by-location, call-by-address]
(C++,Java) --- a reference to a variable or object is passed
instead
Chapter 5, Slide 3
The calling sequence:
• The activation frame for the callee is created by the caller.
• The arguments are evaluated by the caller. If passing by value is
required, the value of the argument is stored in the field actual
arguments, while if passing by reference is required, the pointer to
the argument is stored there.
• The callee saves the machine's status in its activation frame (in
particular the value of the program counter that indicates which
instruction should be executed next, i.e. the one right after the call.
• The callee initializes its local data and starts executing.
Chapter 5, Slide 5
The return sequence:
• The callee stores the return value in the field returned value in its
activation frame.
• The callee uses the information from the field saved machine
status and restores the registers for the caller. This includes popping
the system stack.
• The callee branches to the return address of the caller.
• The caller copies the returned value from the activation frame of
the callee; though the system stack was “popped”, the data is still
there since on the system stack we do not deallocate the memory but
only manipulate the register that points to the top of the stack.
• The caller uses, if it is the case, the returned value for evaluation
of an expression and continues with its normal execution.
Chapter 5, Slide 6
Consider the following program: chapter5_1
main s ..a.. sp 0
next instruction: call to A( )
Chapter 5, Slide 7
main s ..a.. sp 0
next instruction: test value returned by A( )
A s sp
next instruction: call to B( )
Chapter 5, Slide 8
main s ..a.. sp 0
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
next instruction: call to Next( )
Chapter 5, Slide 9
main s ..a.. sp 1
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
next instruction: test value returned by Next( )
Next s sp
consumes s[0], sets sp=1, returns dot
Chapter 5, Slide 10
main s ..a.. sp 1
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
next instruction: call to B( )
Chapter 5, Slide 11
main s ..a.. sp 1
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
next instruction: test value returned by B( )
B s sp sp1 1
next instruction: call to Next( )
Chapter 5, Slide 12
main s ..a.. sp 2
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
next instruction: test value returned by B( )
B s sp sp1 1
next instruction: test value returned by Next( )
Next s sp
consumes s[1], sets sp=2, returns dot
Chapter 5, Slide 13
main s ..a.. sp 2
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
next instruction: test value returned by B( )
B s sp sp1 1
next instruction: call to B( )
Chapter 5, Slide 14
main s ..a.. sp 2
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
next instruction: test value returned by B( )
B s sp sp1 1
next instruction: test value returned by B( )
B s sp sp1 2
next instruction: call to Next( )
Chapter 5, Slide 15
main s ..a.. sp 3
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
next instruction: test value returned by B( )
B s sp sp1 1
next instruction: test value returned by B( )
B s sp sp1 2
next instruction: test value returned by Next( )
Next s sp
consumes s[2], sets sp=3, returns id
Chapter 5, Slide 16
main s ..a.. sp 2
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
next instruction: test value returned by B( )
B s sp sp1 1
next instruction: test value returned by B( )
B s sp sp1 2
Chapter 5, Slide 17
main s ..a.. sp 2
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
next instruction: test value returned by B( )
B s sp sp1 1
returns 1
Chapter 5, Slide 18
main s ..a.. sp 2
next instruction: test value returned by A( )
A s sp
next instruction: test value resturned by B( )
B s sp sp1 0
returns 1
Chapter 5, Slide 19
main s ..a.. sp 2
next instruction: test value returned by A( )
A s sp
next instruction: call to Next( )
Chapter 5, Slide 20
main s ..a.. sp 3
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by Next( )
Next s sp
consumes s[2], sets sp=3, returns id
Chapter 5, Slide 21
main s ..a.. sp 3
next instruction: test value returned by A( )
A s sp
next instruction: call to B( )
Chapter 5, Slide 22
main s ..a.. sp 3
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
next instruction: call to Next( )
Chapter 5, Slide 23
main s ..a.. sp 4
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
next instruction: test value returned by Next( )
Next s sp
consumes s[3], sets sp=4, returns dot
Chapter 5, Slide 24
main s ..a.. sp 4
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
next instruction:call to B( )
Chapter 5, Slide 25
main s ..a.. sp 4
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
next instruction:test value returned by B( )
B s sp sp1 4
next instruction:call to Next( )
Chapter 5, Slide 26
main s ..a.. sp 5
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
next instruction:test value returned by B( )
B s sp sp1 4
next instruction:test value returned by Next( )
Next s sp
consumes s[4], sets sp=5, returns dot
Chapter 5, Slide 27
main s ..a.. sp 5
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
next instruction:test value returned by B( )
B s sp sp1 4
next instruction:call to B( )
Chapter 5, Slide 28
main s ..a.. sp 5
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
next instruction:test value returned by B( )
B s sp sp1 4
next instruction: test value returned by B( )
B s sp sp1 5
next instruction:call to Next( )
Chapter 5, Slide 29
main s ..a.. sp 6
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
next instruction:test value returned by B( )
B s sp sp1 4
next instruction:test value returned by B( )
B s sp sp1 5
next instruction: test value returned by Next( )
Next s sp
consumes s[5], sets sp=6, returns error
Chapter 5, Slide 30
main s ..a.. sp 5
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
next instruction:test value returned by B( )
B s sp sp1 4
next instruction:test value returned by B( )
B s sp sp1 5
sets spp=sp1, returns 1
Chapter 5, Slide 31
main s ..a.. sp 5
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
next instruction:test value returned by B( )
B s sp sp1 4
returns 1
Chapter 5, Slide 32
main s ..a.. sp 4
next instruction: test value returned by A( )
A s sp
next instruction: test value returned by B( )
B s sp sp1 3
returns 1
Chapter 5, Slide 33
main s ..a.. sp 4
next instruction: test value returned by A( )
A s sp
returns 1
Chapter 5, Slide 34
main s ..a.. sp 4
exits
Chapter 5, Slide 35
End of slides for chapter 5
Chapter 5, Slide 36