Title: Languages and Compilers (SProg og Overs
1Languages and Compilers(SProg og Oversættere)
- Bent Thomsen
- Department of Computer Science
- Aalborg University
With acknowledgement to Angela Guercio whos
slides this lecture is based on.
2Subprograms
- A subprogram has a single entry point
- The caller is suspended during execution of the
called subprogram - Control always returns to the caller when the
called subprograms execution terminates - Functions or Procedures?
- Procedures provide user-defined statements
- Functions provide user-defined operators
3Executing Subprograms
- The subprogram call and return operations of a
language are together called its subprogram
linkage - Call Semantics
- 1. Save the execution status of the caller
- 2. Carry out the parameter-passing process
- 3. Pass the return address
- 4. Transfer control to the callee
- Return Semantics
- 1. If pass-by-value-result parameters are used,
move the current values of those parameters to
their corresponding actual parameters - 2. If it is a function, move the functional
value to a place the caller can get it - 3. Restore the execution status of the caller
- 4. Transfer control back to the caller
- Required Storage
- - Status information of the caller, parameters,
returnaddress, and functional value (if it is a
function)
4Activation Record
Functional value Parameter Dynamic
Link Static Link Return address
5FORTRAN
- FORTRAN subprograms can have no more than one
activation record instance at any given time - No recursion!
- The code of all of the program units of a
FORTRAN program may reside together in memory,
with the data for all units stored together
elsewhere - The alternative is to store all local subprogram
data with the subprogram code
6Implementing Subprograms in ALGOL-like Languages
- This is more complicated than implementing
FORTRAN 77 subprograms because - Local variables are often dynamically allocated
- Recursion must be supported
- Static scoping must be supported
- Parameters are often passed by two methods
7Recursion it needs a stack
- Recursive procedures can call themselves an
infinite number of times - For each of the calls a new activation record is
put on top of a stack - a new set of memory location is needed to handle
the local variables and the computation - The calling pattern is determined at runtime,
therefore infinite memory might be needed at run
time - this is way memory get exhausted quickly
8Activation record
- A typical activation record for an ALGOL-like
language - Local variables
- Parameters
- Dynamic link
- Static link stack top
- Return address
- The activation record format is static, but its
size may be dynamic - An activation record instance is dynamically
created when a subprogram is called
9Pointers in the Stack
- Dynamic Link (also called the Previous Frame
Pointer) - points to the previous calling procedure
- Static Link
- used in languages with nested procedures such as
Pascal - used to access non-local variables
- Return Instruction Pointer
- points to the next instruction of the calling
procedure - Frame Pointer (or current)
- points to the first location in the current frame
- Stack Pointer (or top)
- points to the top of the stack
10Stack Based Language Behavior
- Calling a subprogram
- copy the outgoing parameters from the previous
frame to the incoming parameters - set up the return link to point to the next
instruction - set up the dynamic link to point to the previous
frame - set up the static link to point to the frame of
the procedure under which the current procedure
is nested - save the register which have been altered
- set up the top of the current frame
- jump to the instruction area of the called
subprogram
11Returning from subprograms
- Return Semantics
- restore the registers
- If pass-by-value-result parameters are used, move
the current values of those parameters to their
corresponding actual parameters - If it is a function, move the functional value to
a place the caller can get it - Restore the execution status of the caller by
restoring the rest of the pointers - Set IP using the return link to transfer control
back to the caller
12Nonlocal References - Static Scoping
- Observation All variables that can be
non-locally accessed reside in some activation
record instance in the stack - The process of locating a non-local reference
- 1. Find the correct activation record instance
- 2. Determine the correct offset within that
activation record instance - Finding the offset is easy and can be done at
translation time - Finding the correct activation record instance
- - Static semantic rules guarantee that all
non-local variables that can be referenced have
been allocated in some activation record instance
that is on the stack when the reference is made
13Subprogram Parameters
- Formal parameters names (and types) of arguments
to the subprogram used in defining the subprogram
body - Actual parameters arguments supplied for formal
parameters when subprogram is called - Actual/Formal Parameter Correspondence
- attributes of variables are used to exchange
information - Name Call-by-name
- Memory Location Call-by reference
- Value
- Call-by-value (one way from actual to formal
parameter) - Call-by-value-result (two ways between actual and
formal parameter) - Call-by-result (one way from formal to actual
parameter)
14Call by Name
- Replace the formal parameter with the name of the
actual parameter - Name conflicts in the called procedure must be
removed - rename the conflicting local variables
- replace the modified code in the calling
proceedure - int area_of_square(int x)
- int y x x return(y)
- Invocation of square( ai ) is
- int y ai ai return(y)
15Side effects with Call by Name
- swap(name int a, b)
- int temp
- temp a a b b temp
-
- after replacing the code
- void main() void main()
- int i3 int a5 int i3 int a5
- a34 a34
- swap(i, ai) int temp
- temp i i ai ai temp
- Result i4 and a43a3 is unaffected!!!
16Call by Value
- Create a memory location for each formal
parameter - formal parameters are treated as local variables
- Evaluate the expression of the actual parameter
and store it in the area called Outgoing
Parameters - Copy the value of the actual parameters into
formal parameters - copying strictly one way
- no relationship between actual and formal
parameter
17Value Parameter Rules
- Formal parameter is created on function
invocation and it is initialized with the value
of the actual parameter - Changes to formal parameter do not affect actual
parameter - Reference to a formal parameter produces the
value for it in the current activation record - New activation record for every function
invocation - Formal parameter name is only known within its
function - Formal parameter ceases to exist when the
function completes - Activation record memory is automatically
released at function completion
18Call by Value Evaluation
- Disadvantages
- creates a memory location for each data
- requires excessive memory location
- copying cost is high
- no values are passed back to callee
- Advantages
- memory access cost is low
- no side effect
- efficient when a lot of computation on formal
parameters is performed
19Call by Reference
- Create a memory location for each formal
parameter in the activation record of the called
procedure - Formal parameters are treated as pointers to
actual parameters - NO copy of the value of the actual parameter!
Only copy of the address. - If the parameter is a complex data structure or
an array, - one memory location is allocated to save the
pointer to the base location of the data
structure - use indexing to access other locations in the
structure.
20Call by Reference Effect on store
- Disadvantages
- It alters continuously the store of the calling
procedure. - The effect on the store is dependent upon the
computation order - main() void foo(int i, j)
- int x, y
- x1 y2 foo(x,x) j4 i8
- result x8
- Value of x in the caller depends upon the last
- assignments of the alias pointer
21Call by Reference Loss of Commutativity
- main()
- int a,b,x,y
- x3y4 // x is 3
- // x and y are passed by reference
- a square-sum(x,y) x
- b xy
- int square_sum(int x, y)
- xx x
- yy y
- return(x y)
- After execution of square_sum x9, y16a34
- not what we expected! (a would be 28)
- if axsquare_sum(x, y) then a28!
- loss of commutativity!
22Call by Reference Evaluation
- Compare with Call by value evaluation
- The advantages of one are the disadvantages of
the other - Advantages
- creates only one memory location for each data
- requires minimal memory location
- copying cost is low
- no values are passed but actual parameter is
altered in its original location - Disadvantages
- memory access cost is high (indirect addressing)
- efficient when minimal computation on formal
parameters is performed - it has possible side effect
- can allow aliasing
23Call by Reference Aliasing
- Disadvantages Side effect - Aliasing
- i. Actual parameter collisions
- e.g. procedure sub1(a int, b int)
- ...
- sub1(x, x)
- ii. Array element collisions
- e.g. sub1(ai, aj) / if i j /
- Also, sub2(a, ai)
- iii. Collision between formals and globals
- e.g. int global
- sub1(global)
-
- void sub1(local)
- int global..
24Call by Reference Aliasing disadvantages
- Root cause of all of these is The called
subprogram is provided wider access to non-locals
than is necessary - Aliasing disadvantages readability, reliability
25Call by Value Result
- Create a memory location for each formal
parameter (same as a call-by value) - formal parameters are treated as local variables
- No alteration of the locations in calling
procedure during the execution of the called
procedure - Result (if available) is passed back to calling
procedure - Result alters the actual parameter
- OBSERVE Call by value is strictly one way and
does not allow results to be returned through
parameters.
26Call by Value Result Evaluation
- It alters the store after the termination of the
called procedure. - The result is dependent on order of the parameter
passing - order dependence may be a problem
- main() void foo(value-result int i, j)
- int x, y
- x1 y2 foo(x,x) j4 i8
- result x4
- Value of x in the caller depends on order of
assignments at the return
27Call by Value Result Evaluation
- Compare it with Call by reference evaluation
- Disadvantages
- creates memory location for each data
- requires excessive memory location
- copying cost is high
- values are passed back altering the store after
computation - Advantages
- memory access cost is low (no need for indirect
addressing) - efficient when minimal computation on formal
parameters is performed - suitable for distributed computing
- it has possible side effect
28Removing problems of side effect
- Restricted use of the pointers
- disallow arithmetic on pointers
- associate pointers with specific types to
disallow type violation - Disciplined programming
- disciplined use of global and non local variables
- The non-local variables of a subprogram are those
that are visible but not declared in the
subprogram - Global variables are those that may be visible in
all of the subprograms of a program - disciplined used of call by reference
- Disallow destructive update of variables
- variables can be associates with only one value
- functional programming paradigm
- logic programming paradigm
29Languages Examples
- 1. FORTRAN
- Before 77, pass-by-reference
- 77 - scalar variables are often passed by
value-result - 2. ALGOL 60
- Pass-by-name is default pass-by-value is
optional - 3. ALGOL W
- Pass-by-value-result
- 4. C
- Pass-by-value (but these can be pointers)
- 5. Pascal and Modula-2
- Default is pass-by-value pass-by-reference is
optional -
30Languages Examples
- 6. C
- Like C, but also allows reference type actual
parameters the corresponding formal parameters
can be pointers to constants, which provide the
efficiency of pass-by-reference with in-mode
semantics - 7. Ada
- All three semantic modes are available
- 8. Java
- Primitive datatypes are passed by value, composit
data types (Objects, arrays, ) are passed
references
31Design Considerations for Parameter Passing
- Efficiency
- One-way or two-way
- - These two are in conflict with one another!
- Good programming ? limited access to variables,
which means one-way whenever possible - Efficiency ? pass by reference is fastest way to
pass structures of significant size - Also, functions should not allow reference
parameters