Download presentation
Presentation is loading. Please wait.
Published byGarry Montgomery Modified over 9 years ago
1
April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341) Lecture #16 April 23, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT. icu.ac.kr
2
April 23, 2004 2 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Announcements Check your homework, project and exam scores at the class homepage Check your homework, project and exam scores at the class homepage Submit HW#4 electronically Submit HW#4 electronically to Mr. Seung-Bok Ryu (sbryu.AT. icu.ac.kr) to Mr. Seung-Bok Ryu (sbryu.AT. icu.ac.kr) by April 28 th 11:59:59PM by April 28 th 11:59:59PM send your FORTRAN source program send your FORTRAN source program
3
April 23, 2004 3 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Review of the Previous Lectures Characteristics and Basic Definitions about Subprograms Characteristics and Basic Definitions about Subprograms Parameter Passing Models Parameter Passing Models In Mode, Out Mode, Inout Mode In Mode, Out Mode, Inout Mode Parameter Passing Methods Parameter Passing Methods Call-by-Value, Pass-by-Value, Call-by-Value, Pass-by-Value, Pass-by-Reference, Pass-by-Result, Pass-by-Reference, Pass-by-Result, Pass-by-Value-Result, Pass-by-Name Pass-by-Value-Result, Pass-by-Name Stack Implementation of Parameter Passing Stack Implementation of Parameter Passing Passing Multi-dimensional Arrays as Parameters Passing Multi-dimensional Arrays as Parameters Passing Subprogram Names as Parameters Passing Subprogram Names as Parameters
4
April 23, 2004 4 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Overloaded Subprograms A subprogram that has the same name as another subprogram in the same referencing environment A subprogram that has the same name as another subprogram in the same referencing environment Each has a unique protocol to differentiate from others Each has a unique protocol to differentiate from others Number, order, types of parameters Number, order, types of parameters Return type Return type class MyVector extends Vector { … public boolean add(int num) { Integer obj = new Integer(num); Integer obj = new Integer(num); return add(obj); return add(obj);} public boolean add(float num) { Float obj = new Float(num); Float obj = new Float(num); return add(obj); return add(obj);}…} MyVector vec = new MyVector(); vec.add(13.41); vec.add(1341); Java
5
April 23, 2004 5 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Generic (Polymorphic) Subprograms Takes parameters of different types on different activations Takes parameters of different types on different activations Needs to be instantiated Needs to be instantiated Compiler generates a version for a data type Compiler generates a version for a data type Overloaded subprograms provide ad hoc polymorphism Overloaded subprograms provide ad hoc polymorphism e.g., e.g., C++ Template Functions C++ Template Functions Ada Generic Units Ada Generic Units template template Type max(Type a, Type b) { return a > b ? a : b; } int n1, n2; float f1, f2; … int n3 = max(n1, n2); float f3 = max(f1, f2); int max(int a, int b) { return a > b ? a : b; } C++
6
April 23, 2004 6 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko User-Defined Overloaded Operators function "*"(A, B : in Vector_Type) return Integer is return Integer is Sum : Integer := 0; begin for Index in A'range loop Sum := Sum + A(Index) * B(Index); Sum := Sum + A(Index) * B(Index); end loop; return Sum; end "*"; Which of the following is more readable? c = a * b c = a * b c = DotProduct(a, b); c = DotProduct(a, b); Ada
7
April 23, 2004 7 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Coroutines Symmetric Unit Control Model (cf. Master- slave Control Model) Symmetric Unit Control Model (cf. Master- slave Control Model) Quasi-concurrent execution of program units Quasi-concurrent execution of program units
8
April 23, 2004 8 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Activation Records Noncode Part (Local variables and data) Code Part (Execution code) Activation Record Local Variables (list, sum) Parameters (total, part) Dynamic Links Return Address void sub (float total, int part) { int list[4]; float sum; …} Top of the activation record instance A location in the caller code
9
April 23, 2004 9 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Subprograms with Stack-Dynamic Local Variables (1) void fun1 (int x) { int y;... fun3(y);... } void fun2 (float r) { int s, t;... fun1(s);... } void fun3 (int q) {...} void main () { float p;... fun2(p);... } Activation Record Instances of main & fun2 in the Run-time Stack
10
April 23, 2004 10 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Subprograms with Stack-Dynamic Local Variables (2) void fun1 (int x) { int y;... fun3(y);... } void fun2 (float r) { int s, t;... fun1(s);... } void fun3 (int q) {...} void main () { float p;... fun2(p);... } Local Offset
11
April 23, 2004 11 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Subprograms with Stack-Dynamic Local Variables (3) void fun1 (int x) { int y;... fun3(y);... } void fun2 (float r) { int s, t;... fun1(s);... } void fun3 (int q) {...} void main () { float p;... fun2(p);... } Dynamic Chain (Call Chain)
12
April 23, 2004 12 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Recursive Subprograms (1) int factorial (int n) { if (n <= 1) return 1; return 1; else else return return (n * factorial(n - 1)); } void main () { int value; int value; value = factorial(3); value = factorial(3);} Activation Record for factorial A recursive function
13
April 23, 2004 13 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Recursive Subprograms (2) int factorial (int n) { if (n <= 1) return 1; return 1; else else return return (n * factorial(n - 1)); } void main () { int value; int value; value = factorial(3); value = factorial(3);} A recursive function
14
April 23, 2004 14 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Recursive Subprograms (3) int factorial (int n) { if (n <= 1) return 1; return 1; else else return return (n * factorial(n - 1)); } void main () { int value; int value; value = factorial(3); value = factorial(3);} A recursive function
15
April 23, 2004 15 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Recursive Subprograms (4) int factorial (int n) { if (n <= 1) return 1; return 1; else else return return (n * factorial(n - 1)); } void main () { int value; int value; value = factorial(3); value = factorial(3);} A recursive function
16
April 23, 2004 16 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Recursive Subprograms (5) int factorial (int n) { if (n <= 1) return 1; return 1; else else return return (n * factorial(n - 1)); } void main () { int value; int value; value = factorial(3); value = factorial(3);} A recursive function
17
April 23, 2004 17 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Recursive Subprograms (6) int factorial (int n) { if (n <= 1) return 1; return 1; else else return return (n * factorial(n - 1)); } void main () { int value; int value; value = factorial(3); value = factorial(3);} A recursive function
18
April 23, 2004 18 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Recursive Subprograms (7) int factorial (int n) { if (n <= 1) return 1; return 1; else else return return (n * factorial(n - 1)); } void main () { int value; int value; value = factorial(3); value = factorial(3);} A recursive function
19
April 23, 2004 19 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Recursive Subprograms (8) int factorial (int n) { if (n <= 1) return 1; return 1; else else return return (n * factorial(n - 1)); } void main () { int value; int value; value = factorial(3); value = factorial(3);} A recursive function
20
April 23, 2004 20 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Nested Subprograms (1) program Main_2; var X : integer; var X : integer; procedure Bigsub; procedure Bigsub; var A, B, C : integer; var A, B, C : integer; procedure Sub1; procedure Sub1; var A, D : integer; var A, D : integer; begin begin A := B + C; A := B + C; end; end; procedure Sub2(X : integer); procedure Sub2(X : integer); var B, E : integer; var B, E : integer; procedure Sub3; procedure Sub3; var C, E : integer; var C, E : integer; begin begin Sub1; Sub1; E := B + A: E := B + A: end; end; begin begin Sub3; Sub3; A := D + E; A := D + E; end; end; begin begin Sub2(7); Sub2(7); end; end; begin begin Bigsub; Bigsub; end. end. Ada Pointer to the activation record instance of the static parent Activation Record Local Variables Parameters Dynamic Links Return Address Static Links
21
April 23, 2004 21 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Nested Subprograms (2) program Main_2; var X : integer; var X : integer; procedure Bigsub; procedure Bigsub; var A, B, C : integer; var A, B, C : integer; procedure Sub1; procedure Sub1; var A, D : integer; var A, D : integer; begin begin A := B + C; A := B + C; end; end; procedure Sub2(X : integer); procedure Sub2(X : integer); var B, E : integer; var B, E : integer; procedure Sub3; procedure Sub3; var C, E : integer; var C, E : integer; begin begin Sub1; Sub1; E := B + A: E := B + A: end; end; begin begin Sub3; Sub3; A := D + E; A := D + E; end; end; begin begin Sub2(7); Sub2(7); end; end; begin begin Bigsub; Bigsub; end. end. Ada Static Chain
22
April 23, 2004 22 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Nested Subprograms (3) program Main_2; var X : integer; var X : integer; procedure Bigsub; procedure Bigsub; var A, B, C : integer; var A, B, C : integer; procedure Sub1; procedure Sub1; var A, D : integer; var A, D : integer; begin begin A := B + C; A := B + C; end; end; procedure Sub2(X : integer); procedure Sub2(X : integer); var B, E : integer; var B, E : integer; procedure Sub3; procedure Sub3; var C, E : integer; var C, E : integer; begin begin Sub1; Sub1; E := B + A: E := B + A: end; end; begin begin Sub3; Sub3; A := D + E; A := D + E; end; end; begin begin Sub2(7); Sub2(7); end; end; begin begin Bigsub; Bigsub; end. end. Ada Static Depth: the depth of nesting of a scope Static Depth: the depth of nesting of a scope e.g., Sub1 SD = 2 e.g., Sub1 SD = 2 Chain Offset (Nesting Depth): the difference between the static depth of a reference and that of the scope where it is declared Chain Offset (Nesting Depth): the difference between the static depth of a reference and that of the scope where it is declared e.g., A in Sub3 CO = 2 e.g., A in Sub3 CO = 2 A reference can be represented by: (chain_offset, local_offset), where local_offset is the offset in the activation record of the variable being referenced A reference can be represented by: (chain_offset, local_offset), where local_offset is the offset in the activation record of the variable being referenced
23
April 23, 2004 23 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Implementing Nested Subprograms (4) program Main_2; var X : integer; var X : integer; procedure Bigsub; procedure Bigsub; var A, B, C : integer; var A, B, C : integer; procedure Sub1; procedure Sub1; var A, D : integer; var A, D : integer; begin begin A := B + C; A := B + C; end; end; procedure Sub2(X : integer); procedure Sub2(X : integer); var B, E : integer; var B, E : integer; procedure Sub3; procedure Sub3; var C, E : integer; var C, E : integer; begin begin Sub1; Sub1; E := B + A: E := B + A: end; end; begin begin Sub3; Sub3; A := D + E; A := D + E; end; end; begin begin Sub2(7); Sub2(7); end; end; begin begin Bigsub; Bigsub; end. end. Ada References to the variable A (0, 3) (2, 3) (1, 3)
24
April 23, 2004 24 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Evaluation of the Static Chain Method A nonlocal reference is slow if the number of scopes between the reference and the declaration of the referenced variable is large A nonlocal reference is slow if the number of scopes between the reference and the declaration of the referenced variable is large Time-critical code is difficult, because the costs of nonlocal references are not equal, and can change with code upgrades and fixes Time-critical code is difficult, because the costs of nonlocal references are not equal, and can change with code upgrades and fixes Display: An alternative to static chains. An array of static links (addresses of activation record instances). Display: An alternative to static chains. An array of static links (addresses of activation record instances).
25
April 23, 2004 25 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Program Blocks 2. Allocate locals on top of the activation record instance Must use a different method to access locals Must use a different method to access locals void main () { int x, y, z; while ( … ) { int a, b, c; int a, b, c; … while ( … ) { while ( … ) { int d, e; int d, e; … }} while ( … ) { int f, g; int f, g; …}…} 1. Treat blocks as parameterless subprograms
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.