CSc 520
Principles of Programming
Languages
:
Christian Collberg
Department of Computer Science
University of Arizona
1mm
- Value parameters are (usually) copied by the
caller into the callee's activation record.
Changes to a formal won't affect the actual.
1mm
- Reference parameters are passed by passing
the address (location, l-value) fo the parameter.
Changes to a formal affects the actual also.
- The caller computes the arguments' r-value.
- The caller places the r-values in the
callee's activation record.
- The caller's actuals are never affected by the call.
- Copies may have to be made of large structures.
5#5
- The caller computes the arguments' l-value.
- Expression actuals (like 6#6) are stored
in a new location.
- The caller places the l-values in the
callee's activation record.
- The caller's actuals may be affected by the call.
7#7
- (Un-)popularized by Algol 60.
- A name parameter is (re-)evaluated
- every time it is referenced,
- in the callers environment.
Algorithm:
- The caller passes a thunk, a function which
computes the argument's l-value and/or r-value,
to the callee.
- The caller also passes a static link to its environment.
- Every time the callee references the name parameter, the
thunk is called to evaluate it. The static link is
passed to the thunk.
Algorithm:
- If the parameter is used as an l-value, the thunk
should return an l-value, otherwise an r-value.
- If the parameter is used as an l-value, but the
actual parameter has no l-value (it's a constant),
the thunk should produce an error.
Consequences:
- Every time a callee references a name parameter, it
may produce a different result.
8#8
- x := x + 1 becomes a[i] := a[i] + 1.
- Since i is incremented before x, we
get a[2] := a[2] + 1. 9#9 Print 1,3.
10#10
11#11
- Large value parameters have to be treated specially,
so that a change to the formal won't affect the actual.
Example:
12#12
| | Algorithm 1: Callee Copy |
Algorithm 2: Caller Copy |
| | |
|
- In Pascal, parameters are passed either by value or
by reference (if the formal is preceeded by the
keyword var).
- In C, all parameters are passed by value. Pass by
reference can be simulated by explicitly passing
the address of a variable: swap(&x,&y).
- In FORTRAN, all parameters are passed by reference.
A programmer can simulate pass-by-value by
explicitly making a local copy of an argument.
- Unlike most languages, FORTRAN allows r-values
to be passed by reference: swap(3+4,7*x).
The compiler creates a temporary variable to
hold the value.
- In Java, object references are transfered using
pass-by-sharing. This means that the
actual and formal will refer to the same object.
The compiler simply passes the address of the
object.
- In Java, primitive types are passed by value.
In Pascal and Modula-2 a programmer would use
call-by-value to
- ensure that the callee cannot modify the
actual argument.
In Pascal and Modula-2 a programmer would use
call-by-reference to
- ensure that the callee can modify the
actual argument, or to
- make sure that a large parameter (which semantically
should be passed by value) is not copied. (This is done
for efficiency reasons).
Modula-3 provides a READONLY parameter mode.
A READONLY formal parameter cannot be changed by the
callee. The formal
- cannot be on the left-hand-side of
an assignment statement, and
- cannot be passed by reference to
another routine.
- Small READONLY parameters are passed by value.
- Large READONLY parameters are passed by reference.
Ada has three modes:
- in-parameters pass information from
the caller to the callee. The callee cannot
write to them.
- out-parameters pass information to
the callee from the caller. The callee can
read and write them. They start out being
uninitialized.
- in out-parameters pass information
from the caller to the callee and back.
For scalars and pointers, all modes should be
implemented by copying values. Thus
- in-parameters are passed-by-value.
- out-parameters are passed-by-result
(the formal is copied into the actual when the
procedure returns).
- in out-parameters are passed-by-value/result
(On entry, the actual is copied into the formal. On
return, the formal is copied back into the actual).
For constructed types (records, arrays) an implementation is
allowed to pass either values or addresses.
- If an in out parameter is passed by address
an assignment to the formal changes the actual
immediately.
- If an in out parameter is passed by value
an assignment to the formal will not affect the
actual until the procedure returns (and the
formal is copied back into the actual).
- Ada disallows programs that can tell which
implementation a compiler uses.
15#15
- Show the status of the run-time stack when
execution has reached point
16#16 for the second time in the
program on the next slide.
- Fill in the name of each
procedure invocation in the correct activation
record. Also fill in the values of
local variables and actual parameters, and
show where the static links and control links are pointing.
- Assume that all actual parameters are passed
on the stack rather than in registers.
17#17
- Draw the stack when control reaches point 18#18
for the third time.
Include all actual parameters, local variables, return
addresses, and static and dynamic links.
19#19
- Read Scott, pp. 441-448, 450-464
- Read the Dragon Book:
- Parameter Passing
- 424-427
- A parameter is often passed by the caller
copying it (or its address, in case of VAR
parameters) into the callees activation record.
On the MIPS, the caller has an area in its own
activation record in which it puts actual
parameters before it jumps to the callee.
For each procedure P the compiler figures out
the maximum number of arguments P passes to
any procedure it calls. The corresponding amount
of memory has to be allocated in P's activation record.
Christian S. Collberg
2005-04-22