Presentation is loading. Please wait.

Presentation is loading. Please wait.

PLLab, NTHU,Cs2403 Programming Languages Subprogram and its implementation.

Similar presentations


Presentation on theme: "PLLab, NTHU,Cs2403 Programming Languages Subprogram and its implementation."— Presentation transcript:

1 PLLab, NTHU,Cs2403 Programming Languages Subprogram and its implementation

2 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

3 PLLab, NTHU,Cs2403 Programming Languages Definitions Subprogram definition describes the interface to and the actions of the subprogram abstraction –Header: 1 st part of the definition (name, return type, parameters…) –Parameter profile: describes the number, order and type of its formal parameters –Declaration: providing type information

4 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

5 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)

6 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);

7 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

8 PLLab, NTHU,Cs2403 Programming Languages Example I #include 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 var ptr run-time stack ptr virtual address space

9 PLLab, NTHU,Cs2403 Programming Languages Example I #include 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 countstatic main::istaticstack-dynamic test::istaticstack-dynamic test::countstatic

10 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

11 PLLab, NTHU,Cs2403 Programming Languages Parameter-passing methods

12 PLLab, NTHU,Cs2403 Programming Languages 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; }

13 PLLab, NTHU,Cs2403 Programming Languages b=4 a=6 Pass-by-value example 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; } main stack point Cswap stack point d=4 c=6 temp=6 4 6

14 PLLab, NTHU,Cs2403 Programming Languages 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 ; }

15 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

16 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;

17 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

18 PLLab, NTHU,Cs2403 Programming Languages 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 ; }

19 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 3 swap stack point

20 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 2000 caller stack point =7 swap stack point &c=2000 &d=2004 temp=3 1 3 &c=2004 &d=2020 temp=3 7 3 swap stack point

21 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); }

22 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!!

23 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

24 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 7 a[1, ] = 6 2 5  3, 4, 7, 6, 2, 5, 1, 3, 8 a[2, ] = 1 3 8 &a[0,0] = 1200  &a[1,2] = 1216 1220

25 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 }

26 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)

27 PLLab, NTHU,Cs2403 Programming Languages Function as parameters int 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 }

28 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;

29 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

30 PLLab, NTHU,Cs2403 Programming Languages Without generic data structure class 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--]; } } ;

31 PLLab, NTHU,Cs2403 Programming Languages Generic data structure class Name { char fullname[64]; } ; class Person { char firstName[64]; char lastName[32]; } ; template 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 DepartmentStack; stack StudentsStack; stack SchoolStack; Name dep; DepartmentStack.push(dep); }

32 PLLab, NTHU,Cs2403 Programming Languages Generic function Example in C++ (similar in Ada) template 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 }

33 PLLab, NTHU,Cs2403 Programming Languages Subprogram implementation

34 PLLab, NTHU,Cs2403 Programming Languages General semantic Def: The subprogram call and return operations of a language are together called its subprogram linkage

35 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

36 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

37 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

38 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

39 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

40 PLLab, NTHU,Cs2403 Programming Languages Static & dynamic scope Procedure big; var x : integer; procedure sub1; begin …x…  (1) end; procedure sub2; var x : integer; begin …x…  (2) sub1; end; begin sub1;  (3) …x…  (4) sub2;  (5) end Static scope: (3  1) x = big’s x (4) x = big’s x (2) x = sub2’s x (5  1) x = big’s x Dynamic scope (3  1) x = big’s x (4) x = big’s x (2) x = sub2’s x (5  1) x = sub2’s x

41 PLLab, NTHU,Cs2403 Programming Languages Procedure sub (var total: real; part: integer); var list : array [1..5] of integer; sum : real; Begin … End; Static link Return address Local Dynamic link Parameter Local sum total part 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

42 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 link Parameter R Local S Local T Local P Return (to B) Static link Dynamic link Parameter X Local Y Return (to A) Static link Dynamic link Parameter Q Local reference example

43 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 n3 Function value? Return(to factorial) Static link Dynamic link Parameter n2 Function value? Return(to factorial) Static link Dynamic link Parameter n1 Function value? If (n <=1) return 1; 1 2 66 Recursion example

44 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

45 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

46 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

47 PLLab, NTHU,Cs2403 Programming Languages Static Chains (continued) main ----- static_depth = 0 A ----- static_depth = 1 B ----- static_depth = 2 C ----- static_depth = 1

48 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

49 PLLab, NTHU,Cs2403 Programming Languages Nonlocal reference example MAIN_2 var xproc BIGSUB var A, B, Cproc SUB1 var A, D A := B + C; proc SUB2(X) var B, Eproc 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 }

50 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

51 PLLab, NTHU,Cs2403 Programming Languages Stack Contents at Position 1

52 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)

53 PLLab, NTHU,Cs2403 Programming Languages Return(to SUB2) Static link Dynamic link Local D A Return(to SUB2) Static link Dynamic link Local E C Return(to BIGSUB) Static link Dynamic link Parameter7 Local E B X Return(to MAIN_2) Static link Dynamic link Local A C B X Nonlocal reference example MAIN_2 var xproc BIGSUB var A, B, Cproc SUB1 var A, D A := B + C; proc SUB2(X) var C, Eproc 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

54 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

55 PLLab, NTHU,Cs2403 Programming Languages MAIN_5() { int x, y, z; while ( … ) { int a, b, c; … while ( … ) { int d, e; } while ( … ) { int f, g; } Activation Record instance For MAIN_5 x y z a and f b and g c d e Block Variables Locals Blocks

56 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

57 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

58 PLLab, NTHU,Cs2403 Programming Languages Procedure C; integer x, z; integer x, z; begin begin x := u + v; x := u + v; … end; end; Procedure B; integer w, x; integer w, x; begin begin … end; end; Procedure A; integer v, w; integer v, w; begin begin … end; end; Program MAIN_6 integer v, u; integer v, u; begin begin … end; end; MAIN_6 calls A A calls A A calls B B calls C Local u v ARI for MAIN_6 Return (to MAIN_6) Dynamic link Local w v ARI for A Local Return (to A) Dynamic link Local w v ARI for A Local Return (to A) Dynamic link Local x w ARI for B Local Return (to A) Dynamic link Local z x ARI for C Local Deep Access

59 PLLab, NTHU,Cs2403 Programming Languages Procedure C; integer x, z; integer x, z; begin begin x := u + v; x := u + v; … end; end; Procedure B; integer w, x; integer w, x; begin begin … end; end; Procedure A; integer v, w; integer v, w; begin begin … end; end; Program MAIN_6 integer v, u; integer v, u; begin begin … end; end; MAIN_6 calls A A calls A A calls B B calls C uvxzw MAIN_6 A A A A B B C C Shallow Access


Download ppt "PLLab, NTHU,Cs2403 Programming Languages Subprogram and its implementation."

Similar presentations


Ads by Google