subprogram and its implementation
DESCRIPTION
Subprogram and its implementation. Fundamentals. Two fundamental abstractions in PL Process abstraction (subprogram) Data abstraction (chap 11) Subprogram characteristics Single entry point Caller is suspended during callee execution - PowerPoint PPT PresentationTRANSCRIPT
PLLab, NTHU,Cs2403 Programming Languages
Subprogram and its implementation
PLLab, NTHU,Cs2403 Programming Languages
Fundamentals
• Two fundamental abstractions in PL– Process abstraction (subprogram)– Data abstraction (chap 11)
• Subprogram characteristics– Single entry point– Caller is suspended during callee execution– Control returns to caller when callee execution
terminates
PLLab, NTHU,Cs2403 Programming Languages
Definitions
• Subprogram definition describes the interface to and the actions of the subprogram abstraction– Header: 1st part of the definition (name, return
type, parameters…)– Parameter profile: describes the number, order
and type of its formal parameters– Declaration: providing type information
PLLab, NTHU,Cs2403 Programming Languages
Parameters
• Two ways for subprogram to gain access to the data it is to process:– Direct access to nonlocal variables– Parameter passing
PLLab, NTHU,Cs2403 Programming Languages
Parameters (cont.)
• Formal parameters: defined in the subprogram header– Bound to storage when subprogram is called through
some other variables
– e.g. void sumer(int num1, real num2);
• Actual parameters: parameters used in the subprogram call– Bound to formal parameters
– e.g. Sumer(10. 1.4)
PLLab, NTHU,Cs2403 Programming Languages
Parameters (cont.)
• Actual and formal parameter correspondences– Positional parameters: binding is based on position
– Keyword parameters: binding is based on keyword mapping• e.g. (Ada) sumer(num1=> 10, num2=>1.4)
• Defalut values: C++ FORTRAN 90, Ada allow default values for formal parameters– e.g. (C++) void sumer(int Num1, real num2, int flag = 1);
Caller: sumer(10.1.4);
PLLab, NTHU,Cs2403 Programming Languages
Local referencing environment
• Local variables: variables that are defined inside the subprogram
• Stack dynamic local variables– Storage is allocated from stack– Allows recursive programming– Cost of time for allocation, initialization and
deallocation• Static local variables:
– More efficient for direct addressing– History sensitive
PLLab, NTHU,Cs2403 Programming Languages
Example I#include <stdio.h>
int count;
main( ) {
int i ;
for (i=0; i<=10; i++)
{ test( ) ; }
}
test( ) {
int i ;
static int count = 0;
count = count + 1 ;
}
count
test::count
main::i
test::i
static varptr
run-timestack ptr
virtual address space
PLLab, NTHU,Cs2403 Programming Languages
Example I#include <stdio.h>
int count;
main( ) {
int i ;
for (i=0; i<=10; i++)
{ test( ) ; }
}
test( ) {
int i ;
static int count = 0;
count = count + 1 ;
}
Type-binding
Storage-binding
count static static
main::i static stack-dynamic
test::i static stack-dynamic
test::count static static
PLLab, NTHU,Cs2403 Programming Languages
Parameter Passing Methods
• We discuss these at several different levels:– Semantic models:
• in mode: receive data from actual params.
• out mode: transmit data to actual params
• Inout mode: do both above
– Conceptual models of transfer:• Actual value is copied
• Move an access path
PLLab, NTHU,Cs2403 Programming Languages
Parameter-passing methods
PLLab, NTHU,Cs2403 Programming Languages
Pass-by-value• Pass-by-value
– Use the value of the actual params to initialize the corresponding formal params
– Pascal, c, c++
• EX.main( ) { int a = 6; int b = 4; Cswap(a, b); // a = 6 // b = 4}
Cswap(int c, int d) { int temp = c; c = d; d = temp;}
PLLab, NTHU,Cs2403 Programming Languages
b=4b=4
a=6a=6
Pass-by-value examplemain( ) { int a = 6; int b = 4; Cswap(a, b); // a = 6 // b = 4} Cswap(int c, int d) { int temp = c; c = d; d = temp;} main stack point
Cswap stack point
d=4
c=6c=6
temp=6
4
6
PLLab, NTHU,Cs2403 Programming Languages
Pass-by-result• Pass-by-result
– no value is transmitted to the subprogram– the value of the formal para is passed back to the actual para when
the subprogram returns• Example (in a pseudo language)
caller( ) { int a ; int b ; foo(a, b); // a = 6 // b = 4}
foo(int c, int d ) { c = 6 ; d = 4 ;}
PLLab, NTHU,Cs2403 Programming Languages
Pass-by-result example
b
d
a
c
caller stack point
caller( ) { int a ; int b ; foo(a, b); // a = 6 // b = 4} foo(int c, int d ) { c = 6 ; d = 4 ;}
foo stack point
=6
=4
=6
=4
PLLab, NTHU,Cs2403 Programming Languages
Pass-by-value-result• Pass-by-value-result (a.k.a. pass-by-copy)
– the combination of pass-by-value and pass-by-result
– Ada
• Example (in Ada)
integer a = 3 ; integer b = 1 ; integer k[10] ; k[3] = 7; swap(a, b); swap(b, k[b]);
procedure swap(a : in out integer, b : in out integer) is
temp : integer; begin temp := a ; a := b ; b := temp ; end swap;
PLLab, NTHU,Cs2403 Programming Languages
Pass-by-value-result example
integer a = 3 ; integer b = 1 ; integer k[10] ; k[3] = 7; swap(a, b); swap(b, k[b]);
procedure swap(a : in out integer, b : in out integer) is
temp : integer; begin temp := a ; a := b ; b := temp ; end swap;
a=3
b=1
main stack point
k[2]
k[1]
k[0]
k[3]
…..
=7
swap stack point a=3
b=1
temp
1
3
=3
3
1
a=3
b=7
temp =3
7
3
7
3
swap stack point
PLLab, NTHU,Cs2403 Programming Languages
Pass-by-reference• Pass-by-reference
– access path is transmitted to the called subprogram: efficient in terms of time & space
– access to formal paras are slower
• Example (in C)
caller( ) { int a = 3 ; int b = 1 ; int k[10] ; k[3] = 7; swap(&a, &b) ; swap(&b, &k[b]) ;}
swap(int *c, int *d ) { temp = *c; *c = *d ; *d = temp ;}
PLLab, NTHU,Cs2403 Programming Languages
Pass-by-reference example
caller( ) { int a = 3 ; int b = 1 ; int k[10] ; k[3] = 7; swap(&a, &b) ; swap(&b, &k[b]) ;}
swap(int *c, int *d ) { temp = *c; *c = *d ; *d = temp ;}
a=3
b=1
k[2]
k[1]
k[0]
k[3]
…..2024
2020
2016
2012
2008
2004
2000
=7
caller stack point
swap stack point
c=2000
d=2004
temp=3
1
3
c=2004
d=2020
temp=3
7
3swap stack point
PLLab, NTHU,Cs2403 Programming Languages
Pass-by-reference (con’t)• Example (in C++)
caller( ) { int a = 3 ; int b = 1 ; int k[10] ; k[3] = 7; swap(a, b) ; swap(b, k[b]) ;} swap(int &c, int &d ) { temp = c; c = d ; d = temp ;}
a=3
b=1
k[2]
k[1]
k[0]
k[3]
…..2024
2020
2016
2012
2008
2004
2000caller stack point
=7swap stack point
&c=2000
&d=2004
temp=3
1
3
&c=2004
&d=2020
temp=3
7
3swap stack point
PLLab, NTHU,Cs2403 Programming Languages
Singlemensional array para
• C does not check the array subscript range run-time error if our-of-bound access
void fun(int *a) { a[3] = 77;}
main( ) { int a[10]; a[3] = 55; fun(a); // a[3] = 77}
void fun(int a[ ]) { a[3] = 77;}
main( ) { int a[10]; a[3] = 55; fun(a); // a[3] = 77}
void fun(int a[ ]) { a[13] = 77; // run-time error}
main( ) { int a[10]; a[3] = 55; fun(a);}
PLLab, NTHU,Cs2403 Programming Languages
Interesting C pointer usage
fun(int *a) {
a = (int *) malloc(40);
a[5] = 1234;
}
main( ) {
int *array;
fun(array);
printf(“%d”, array[5]);
} array=0
main stack
d=4
temp=6
4
6
fun stack
a=0
2060
a[5]=1234
2060
memory leakage!!
Segmentation fault!!
PLLab, NTHU,Cs2403 Programming Languages
Correct C pointer usage
fun(int *a) {a[5] = 1234;
}main( ) { int *array; array = (int *) malloc(40); fun(array); // array[5] == 1234
} a=6array=0
main stack
d=4
temp=6
4
6
fun stack
a=2060
2060
a[5]=1234
2060
PLLab, NTHU,Cs2403 Programming Languages
Array implementation• One-dim array address calculation
– addr(a[j]) = addr(a[0]) + j * elementSize;
int bounds[10]; // one int = 4 bytes&bounds[0] = 1200 &bounds[4] =
• Row-major allocation: a[m,n] = a[3, 3]– addr(a[i,j]) = addr(a[0,0]) + ((i * n) + j) * elementSize;
a[0, ] = 3 4 7a[1, ] = 6 2 5 3, 4, 7, 6, 2, 5, 1, 3, 8a[2, ] = 1 3 8
&a[0,0] = 1200 &a[1,2] =
1216
1220
PLLab, NTHU,Cs2403 Programming Languages
Multidimensional array para
• Compiler needs to know the declared size of the multi-dim array to build the mapping function in subprograms– C uses raw-major allocation
fun(int a[ ][ ]) { a[3][5] = 4321;}main( ) { int a[10][20]; a[3][5] = 1234; fun(a);}// compiler error
fun(int a[ ][25]) { a[3][5] = 4321;}main( ) { int a[10][20]; a[3][5] = 1234; fun(a);}// compiler error
fun(int a[ ][20]) { a[3][5] = 4321;}main( ) { int a[10][20]; a[3][5] = 1234; fun(a); // a[3][5] = 4321}
PLLab, NTHU,Cs2403 Programming Languages
Multidimensional array (con’t)
fun(int a[ ][ ][ ]) { a[3][4][5] = 4321;}main( ) { int a[10][20][25] ; a[3][4][5] = 1234 ; fun(a);}// compiler error
fun(int a[ ][ ][25]) { a[3][4][5] = 4321;}main( ) { int a[10][20][25]; a[3][4][5] = 1234; fun(a);}// compiler error
fun(int a[ ][20][25]) { a[3][4][5] = 4321;}main( ) { int a[10][20][25]; a[3][4][5] = 1234; fun(a); // a[3][4][5] = 4321}
Work around: 1. Use heap-dynamic variables for array
• int *a; a = (int *) malloc(sizeof(int) * m * n);2. Pass the arrry variable alone with m and n
• fun(int *a, int num_rows, int num_cols)• a[i, j] == *(a + i * num_cols + j)
PLLab, NTHU,Cs2403 Programming Languages
Function as parametersint Plus (int num) { return num + num ;}
int Square (int num) { return num * num;}
void Execute(int seed, int (*pF)(int)) {
return pF(seed) ;}
int main( ) { int Result ; int (*pF)(int) ;
pF = Plus; Result = Execute(3, pF); // Result = 6
pF = Square; Result = Execute(3, pF); // Result = 9}
PLLab, NTHU,Cs2403 Programming Languages
Reference environment• Referencing env
– shallow binding: env of the call statement
• x = 4
– deep binding: env of the defined subprogram
• x = 1
• C, C++, Java.
– ad hoc binding: env of the actual passing statement
• x = 3
procedure SUB1;var x : integer;procedure SUB2;
begin write(‘x = ‘, x); end;
procedure SUB3; var x : integer; begin x := 3; SUB4(SUB2); end;
procedure SUB4(SUBX); var x : integer;begin x := 4; SUBX end;
begin x := 1; SUB3; end;
PLLab, NTHU,Cs2403 Programming Languages
Generic Subprograms
• A generic or polymorphic subprogram is one that takes parameters of different types on different activations
• A subprogram that takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram provides parametric polymorphism
PLLab, NTHU,Cs2403 Programming Languages
Without generic data structureclass Name { char fullname[64];} ;class Person { char firstName[64]; char lastName[32];} ;
class NameStack { Name nodes[50]; int stackptr;public: stack() { stackptr = 0; } void push( Name data) { nodes[stackptr++] = data; } Name pop() { return nodes[stackptr--]; }} ;
int main(int argc, char* argv[ ]) {
NameStack DepartmentStack; PersonStack StudentsStack; Name dep; DepartmentStack.push(dep);}
class PersonStack { Person nodes[50]; int stackptr;public: stack() { stackptr = 0; } void push( Person data) { nodes[stackptr++] = data; } Person pop() { return nodes[stackptr--]; }} ;
PLLab, NTHU,Cs2403 Programming Languages
Generic data structureclass Name { char fullname[64];} ;class Person { char firstName[64]; char lastName[32];} ;
template<class item, int max_items>class stack { item nodes[max_items]; int stackptr;public: stack() { stackptr = 0; } void push( item data) { nodes[stackptr++] = data; } item pop() { return nodes[stackptr--]; }} ;
int main(int argc, char* argv[ ]) {
stack<Name,10> DepartmentStack; stack<Person,20> StudentsStack; stack<Name,50> SchoolStack;
Name dep; DepartmentStack.push(dep);}
PLLab, NTHU,Cs2403 Programming Languages
Generic function• Example in C++ (similar in Ada)
template <class Type>Type max(Type first, Type second) {
return first > second ? first : second; }main( ) { int a, b, c;
char d, e, f; c = max(a, b); // code generation for int max(…) f = max(d, e); // code generation for char max(…) b = max(a, c); // no code generation}
PLLab, NTHU,Cs2403 Programming Languages
Subprogram implementation
PLLab, NTHU,Cs2403 Programming Languages
General semantic
• Def: The subprogram call and return operations of a language are together called its subprogram linkage
PLLab, NTHU,Cs2403 Programming Languages
Implementing “Simple” Subprograms
• Call Semantics:1. Save the execution status of the caller
2. Carry out the parameter-passing process
3. Pass the return address to the callee
4. Transfer control to the callee
PLLab, NTHU,Cs2403 Programming Languages
Implementing “Simple” Subprograms
• 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
PLLab, NTHU,Cs2403 Programming Languages
Implementing “Simple” Subprograms
• Required Storage: – Caller status, parameters, return address, and
functional value (if it is a function)
• The format, or layout, of the noncode part of an executing subprogram is called an activation record
PLLab, NTHU,Cs2403 Programming Languages
Subprogram implementation
• Activation record: the layout of the call stack for call-related data
• Static-scoping PL implementation– dynamic-scoping discussed later
• Two approaches to implementing nonlocal variables accesses– static chains– displays
Local variables
Parameters
Dynamic link
Static link
Return address stack top
An activation record
PLLab, NTHU,Cs2403 Programming Languages
Scope example proc main var X1, X2; proc A
var X1, X2; end proc B var X1, X2; call A; end call B;end
Proc A reference environment
Static scope: A main
Dynamic scope: A B main
PLLab, NTHU,Cs2403 Programming Languages
Static & dynamic scopeProcedure big;
var x : integer;procedure sub1;
begin…x… (1)end;
procedure sub2;var x : integer;begin…x… (2)sub1;end;
beginsub1; (3)…x… (4)sub2; (5)end
Static scope:(31) x = big’s x(4) x = big’s x(2) x = sub2’s x(51) x = big’s x
Dynamic scope(31) x = big’s x(4) x = big’s x(2) x = sub2’s x(51) x = sub2’s x
PLLab, NTHU,Cs2403 Programming Languages
Procedure sub (var total: real; part: integer);
var list : array [1..5] of integer; sum : real; Begin … End;
Static linkReturn address
Local
Dynamic linkParameterParameter
LocalLocalLocalLocalLocal
sum
totalpart
List [4]List [3]List [2]List [1]
List [5]
The activation record for procedure sub
Activation record
• static link: used for nonlocal variables reference
• dynamic link: points to the top of the AR of the caller
PLLab, NTHU,Cs2403 Programming Languages
Program MAIN_1; var P : real; procedure A (X : integer); var Y : boolean; procedure C (Q : boolean); begin {C} … end; {C} begin {A} … C(Y); … end; {A} procedure B (R : real); var S, T :integer; begin {B} … A(S); … end; {B} begin {MAIN_1} … B(P); … end. {MAIN_1}
Return (to MAIN)Static link
Dynamic linkParameter R
Local SLocal T
Local P
Return (to B)Static link
Dynamic linkParameter X
Local YReturn (to A)
Static linkDynamic linkParameter Q
Local reference example
PLLab, NTHU,Cs2403 Programming Languages
int factorial (int n){ If (n <=1) return 1; else return (n* factorial (n-1));}void main(){ int value; value = factorial (3);}
Local value ?
Return (to main)
Static link
Dynamic link
Parameter n 3
Function value ?
Return(to factorial)
Static link
Dynamic link
Parameter n 2
Function value ?
Return(to factorial)
Static link
Dynamic link
Parameter n 1
Function value ?
If (n <=1)return 1;
1
2
66
Recursion example
PLLab, NTHU,Cs2403 Programming Languages
Static chain & Display
• Static chain: following the static chain for nonlocal variable reference– costly
– nondeterministic
• Display: the only widely used alternative to static chain– static links are collected in a single array called a
display
– exactly 2 steps to access nonlocal variables
PLLab, NTHU,Cs2403 Programming Languages
Nested Subprograms
• Technique 1 - Static Chains• A static chain is a chain of static links that
connects certain activation record instances• The static link in an activation record instance for
subprogram A points to one of the activation record instances of A's static parent
• The static chain from an activation record instance connects it to all of its static ancestors
PLLab, NTHU,Cs2403 Programming Languages
Static Chains (continued)
• To find the declaration for a reference to a nonlocal variable:– You could chase the static chain until the
activation record instance (ari) that has the variable is found, searching each ari as it is found, if variable names were stored in the ari
• Def: static_depth is an integer associated with a static scope whose value is the depth of nesting of that scope
PLLab, NTHU,Cs2403 Programming Languages
Static Chains (continued)
main ----- static_depth = 0A ----- static_depth = 1
B ----- static_depth = 2
C ----- static_depth = 1
PLLab, NTHU,Cs2403 Programming Languages
Static Chains (continued)
• Def: The chain_offset or nesting_depth of a nonlocal reference is the difference between the static_depth of the reference and that of the scope where it is declared
• A reference can be represented by the pair:
(chain_offset, local_offset)
where local_offset is the offset in the activation record of the variable being referenced
PLLab, NTHU,Cs2403 Programming Languages
Nonlocal reference example
MAIN_2
var x proc BIGSUB
var A, B, C proc SUB1
var A, D A := B + C; proc SUB2(X)
var B, E proc SUB3
var C, E SUB1;E := B + A;
SUB3;A := X + E;
SUB2(7);
BIGSUB;
program MAIN_2; var X : integer; procedure BIGSUB; var A, B, C : integer; procedure SUB1; var A, D : integer; begin { SUB1 } A := B + C; <-----------------------1 end; { SUB1 } procedure SUB2(X : integer); var B, E : integer; procedure SUB3; var C, E : integer; begin { SUB3 } SUB1; E := B + A: <--------------------2 end; { SUB3 } begin { SUB2 } SUB3; A := D + E; <-----------------------3 end; { SUB2 } begin { BIGSUB } SUB2(7); end; { BIGSUB } begin BIGSUB; end. { MAIN_2 }
PLLab, NTHU,Cs2403 Programming Languages
Example Pascal Program
Call sequence for MAIN_2 MAIN_2 calls BIGSUB BIGSUB calls SUB2 SUB2 calls SUB3 SUB3 calls SUB1
PLLab, NTHU,Cs2403 Programming Languages
Stack Contents at Position 1
PLLab, NTHU,Cs2403 Programming Languages
Example Pascal Program
At position 1 in SUB1: A - (0, 3) B - (1, 4) C - (1, 5)
At position 2 in SUB3: E - (0, 4) B - (1, 4) A - (2, 3)
At position 3 in SUB2: A - (1, 3) D - an error E - (0, 5)
PLLab, NTHU,Cs2403 Programming Languages
Return(to SUB2)
Static linkDynamic link
LocalLocal D
A
Return(to SUB2)
Static linkDynamic link
LocalLocal E
C
Return(to BIGSUB)Static link
Dynamic linkParameter 7
LocalLocal E
BX
Return(to MAIN_2)
Static linkDynamic link
LocalLocalLocal
A
CB
Local X
Nonlocal reference exampleMAIN_2
var x proc BIGSUB
var A, B, C proc SUB1
var A, D A := B + C;
proc SUB2(X)
var C, E proc SUB3
var C, E SUB1;E := B + A;
SUB3;A := X + E;
SUB2(7);
BIGSUB;
Main_2 calls Bigsub
Bigsub calls sub2
Sub2 calls sub3
Sub3 calls sub1
PLLab, NTHU,Cs2403 Programming Languages
Blocks
• Two Methods:1. Treat blocks as parameterless subprograms– Use activation records
2. Allocate locals on top of the ari of the subprogram
– Must use a different method to access locals– A little more work for the compiler writer
PLLab, NTHU,Cs2403 Programming Languages
MAIN_5() { int x, y, z; while ( … ) { int a, b, c; … while ( … ) { int d, e; } } while ( … ) { int f, g; }}
ActivationRecord instance
ForMAIN_5
x
y
z
a and f
b and g
c
d
e
BlockVariables
Locals
Blocks
PLLab, NTHU,Cs2403 Programming Languages
Implementing dynamic scoping
• Deep access: the dynamic link chain is exactly what needed to reference nonlocal variables in a dynamic-scoped language
• Shallow access: each local variable is stored in a separate stack
PLLab, NTHU,Cs2403 Programming Languages
Scope example proc main var X1, X2; proc A
var X1, X2; end proc B var X1, X2; call A; end call B;end
Proc A reference environment
Static scope: A main
Dynamic scope: A B main
PLLab, NTHU,Cs2403 Programming Languages
Procedure C;Procedure C; integer x, z;integer x, z; beginbegin x := u + v;x := u + v; … … end;end;Procedure B;Procedure B; integer w, x;integer w, x; beginbegin … … end;end;Procedure A;Procedure A; integer v, w;integer v, w; beginbegin … … end;end;Program MAIN_6Program MAIN_6 integer v, u;integer v, u; beginbegin … … end;end;
MAIN_6 calls AA calls AA calls BB calls C
Local
Local u
v
ARI for MAIN_6
Return (to MAIN_6)
Dynamic linkLocal
w
vARI for A
Local
Return (to A)
Dynamic linkLocal
w
vARI for A
Local
Return (to A)
Dynamic linkLocal
x
wARI for B
LocalReturn (to A)
Dynamic linkLocal
z
xARI for C
LocalDeep Access
PLLab, NTHU,Cs2403 Programming Languages
Procedure C;Procedure C; integer x, z;integer x, z; beginbegin x := u + v;x := u + v; … … end;end;Procedure B;Procedure B; integer w, x;integer w, x; beginbegin … … end;end;Procedure A;Procedure A; integer v, w;integer v, w; beginbegin … … end;end;Program MAIN_6Program MAIN_6 integer v, u;integer v, u; beginbegin … … end;end;
MAIN_6 calls AA calls AA calls BB calls C
u v x z w
MAIN_6 MAIN_6
A
A
A
A
B
B
C
C
Shallow Access