april 23, 2004 1 ice 1341 – programming languages (lecture #16) in-young ko programming languages...

25
April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages Programming Languages (ICE 1341) (ICE 1341) Lecture #16 Lecture #16 April 23, 2004 In-Young Ko iko .AT. i cu . ac.kr Information and Communications University (ICU)

Upload: garry-montgomery

Post on 01-Jan-2016

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Programming LanguagesProgramming Languages(ICE 1341)(ICE 1341)

Lecture #16Lecture #16 April 23, 2004

In-Young Koiko .AT. icu.ac.kr

Information and Communications University (ICU)

Page 2: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 2 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

AnnouncementsAnnouncements

Check your homework, project and exam Check your homework, project and exam scores at the class homepagescores at the class homepage

Submit HW#4 electronicallySubmit HW#4 electronically to to Mr. Seung-Bok RyuMr. Seung-Bok Ryu ( (sbryu .AT. icu.ac.krsbryu .AT. icu.ac.kr)) by by April 28April 28th th 11:59:59PM11:59:59PM send your FORTRAN send your FORTRAN source programsource program

Page 3: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 3 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Review of the Previous LecturesReview of the Previous Lectures

Characteristics and Basic Definitions about Characteristics and Basic Definitions about SubprogramsSubprograms

Parameter Passing ModelsParameter Passing Models In Mode, Out Mode, Inout ModeIn Mode, Out Mode, Inout Mode

Parameter Passing MethodsParameter 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-NamePass-by-Value-Result, Pass-by-Name

Stack Implementation of Parameter PassingStack Implementation of Parameter Passing Passing Multi-dimensional Arrays as ParametersPassing Multi-dimensional Arrays as Parameters Passing Subprogram Names as ParametersPassing Subprogram Names as Parameters

Page 4: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 4 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Overloaded SubprogramsOverloaded Subprograms

A subprogramA subprogram that has that has the the same namesame name as as another subprogram another subprogram in in the same referencing the same referencing environmentenvironment

Each has Each has a unique a unique protocol protocol to differentiate to differentiate from othersfrom others

Number, order, types of Number, order, types of parametersparameters

Return typeReturn type

class MyVector extends class MyVector extends Vector Vector {{……public boolean public boolean addadd(int num) {(int num) { Integer obj = new Integer(num);Integer obj = new Integer(num); return return add(obj)add(obj);;}}

public boolean public boolean addadd(float num) {(float num) { Float obj = new Float(num);Float obj = new Float(num); return return add(obj)add(obj);;}}……

}}

MyVector vec = new MyVector();MyVector vec = new MyVector();vec.vec.addadd(13.41);(13.41);vec.vec.addadd(1341);(1341);

Java

Page 5: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 5 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Generic (Polymorphic) SubprogramsGeneric (Polymorphic) Subprograms

TTakes akes parameters of parameters of different typesdifferent types on on different activationsdifferent activations

Needs to be Needs to be instantiatedinstantiated Compiler generates a Compiler generates a

version for a data typeversion for a data type Overloaded Overloaded

subprogramssubprograms provide provide ad ad hoc polymorphismhoc polymorphism

e.g.,e.g., C++ C++ Template FunctionsTemplate Functions Ada Ada Generic UnitsGeneric Units

template <class Type>template <class Type>Type Type maxmax(Type a, Type b) {(Type a, Type b) {

return a > b ? a : b;return a > b ? a : b;}}

int n1, n2;int n1, n2;float f1, f2;float f1, f2;……int n3 = int n3 = maxmax(n1, n2);(n1, n2);float f3 = float f3 = maxmax(f1, f2);(f1, f2);

int max(int a, int b) {int max(int a, int b) {return a > b ? a : b;return a > b ? a : b;

}}C++

Page 6: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 6 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

User-Defined Overloaded OperatorsUser-Defined Overloaded Operators

function "function "**"(A, B : in "(A, B : in Vector_TypeVector_Type)) return return IntegerInteger is is

Sum : Integer := 0;Sum : Integer := 0;beginbeginfor Index in A'range loopfor Index in A'range loop

Sum := Sum + A(Index) * B(Index);Sum := Sum + A(Index) * B(Index);end loop;end loop;return Sum;return Sum;

end "*";end "*";Which of the following is Which of the following is more readable?more readable? c = a * bc = a * b c = DotProduct(a, b);c = DotProduct(a, b);Ada

Page 7: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 7 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

CoroutinesCoroutines

Symmetric Unit Control ModelSymmetric Unit Control Model (cf. Master- (cf. Master-slave Control Model)slave Control Model)

QQuasi-concurrent executionuasi-concurrent execution of program units of program units

Page 8: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 8 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Activation RecordsActivation Records

Noncode PartNoncode Part(Local variables and data)(Local variables and data)

Code PartCode Part(Execution code)(Execution code)

Activation RecordActivation Record

Local VariablesLocal Variables(list, sum)(list, sum)

ParametersParameters(total, part)(total, part)

Dynamic LinksDynamic Links

Return AddressReturn Address

void sub (float total, int part) {void sub (float total, int part) {int list[4];int list[4];float sum;float sum;……

}}

Top of the activation record instanceTop of the activation record instance

A location in the caller codeA location in the caller code

Page 9: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 9 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Subprograms with Implementing Subprograms with Stack-Dynamic Local VariablesStack-Dynamic Local Variables (1) (1)

void void fun1 fun1 (int x) {(int x) {int y; ...int y; ...fun3fun3(y); ...(y); ...

}}void void fun2 fun2 (float r) {(float r) {

int s, t; ...int s, t; ...fun1fun1(s); ...(s); ...

}}void void fun3 fun3 (int q) {(int q) {

......}}void void main main () {() {

float p; ...float p; ...fun2fun2(p); ...(p); ...

}}

Activation Record Activation Record Instances of main & fun2Instances of main & fun2in the in the Run-time StackRun-time Stack

Page 10: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 10 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Subprograms with Implementing Subprograms with Stack-Dynamic Local VariablesStack-Dynamic Local Variables (2) (2)

void void fun1 fun1 (int x) {(int x) {int y; ...int y; ...fun3fun3(y); ...(y); ...

}}void void fun2 fun2 (float r) {(float r) {

int s, t; ...int s, t; ...fun1fun1(s); ...(s); ...

}}void void fun3 fun3 (int q) {(int q) {

......}}void void main main () {() {

float p; ...float p; ...fun2fun2(p); ...(p); ...

}}

Lo

cal Offset

Lo

cal Offset

Page 11: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 11 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Subprograms with Implementing Subprograms with Stack-Dynamic Local VariablesStack-Dynamic Local Variables (3) (3)

void void fun1 fun1 (int x) {(int x) {int y; ...int y; ...fun3fun3(y); ...(y); ...

}}void void fun2 fun2 (float r) {(float r) {

int s, t; ...int s, t; ...fun1fun1(s); ...(s); ...

}}void void fun3 fun3 (int q) {(int q) {

......}}void void main main () {() {

float p; ...float p; ...fun2fun2(p); ...(p); ...

}}

Dyn

amic C

hain

D

ynam

ic Ch

ain

(Call C

hain

)(C

all Ch

ain)

Page 12: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 12 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Recursive Recursive SubprogramsSubprograms (1) (1)

int factorial (int n) {int factorial (int n) {if (n <= 1)if (n <= 1)

return 1;return 1; elseelse

returnreturn(n * factorial(n - (n * factorial(n -

1));1));}}

void main () {void main () { int value;int value; value = factorial(3);value = factorial(3);}}

Activation RecordActivation Recordfor factorialfor factorial

A recursive functionA recursive function

Page 13: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 13 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Recursive Recursive SubprogramsSubprograms (2) (2)

int factorial (int n) {int factorial (int n) {if (n <= 1)if (n <= 1)

return 1;return 1; elseelse

returnreturn(n * factorial(n - (n * factorial(n -

1));1));}}

void main () {void main () { int value;int value; value = factorial(3);value = factorial(3);}}

A recursive functionA recursive function

Page 14: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 14 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Recursive Recursive SubprogramsSubprograms (3) (3)

int factorial (int n) {int factorial (int n) {if (n <= 1)if (n <= 1)

return 1;return 1; elseelse

returnreturn(n * factorial(n - (n * factorial(n -

1));1));}}

void main () {void main () { int value;int value; value = factorial(3);value = factorial(3);}}

A recursive functionA recursive function

Page 15: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 15 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Recursive Recursive SubprogramsSubprograms (4) (4)

int factorial (int n) {int factorial (int n) {if (n <= 1)if (n <= 1)

return 1;return 1; elseelse

returnreturn(n * factorial(n - (n * factorial(n -

1));1));}}

void main () {void main () { int value;int value; value = factorial(3);value = factorial(3);}}

A recursive functionA recursive function

Page 16: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 16 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Recursive Recursive SubprogramsSubprograms (5) (5)

int factorial (int n) {int factorial (int n) {if (n <= 1)if (n <= 1)

return 1;return 1; elseelse

returnreturn(n * factorial(n - (n * factorial(n -

1));1));}}

void main () {void main () { int value;int value; value = factorial(3);value = factorial(3);}}

A recursive functionA recursive function

Page 17: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 17 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Recursive Recursive SubprogramsSubprograms (6) (6)

int factorial (int n) {int factorial (int n) {if (n <= 1)if (n <= 1)

return 1;return 1; elseelse

returnreturn(n * factorial(n - (n * factorial(n -

1));1));}}

void main () {void main () { int value;int value; value = factorial(3);value = factorial(3);}}

A recursive functionA recursive function

Page 18: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 18 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Recursive Recursive SubprogramsSubprograms (7) (7)

int factorial (int n) {int factorial (int n) {if (n <= 1)if (n <= 1)

return 1;return 1; elseelse

returnreturn(n * factorial(n - (n * factorial(n -

1));1));}}

void main () {void main () { int value;int value; value = factorial(3);value = factorial(3);}}

A recursive functionA recursive function

Page 19: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 19 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Recursive Recursive SubprogramsSubprograms (8) (8)

int factorial (int n) {int factorial (int n) {if (n <= 1)if (n <= 1)

return 1;return 1; elseelse

returnreturn(n * factorial(n - (n * factorial(n -

1));1));}}

void main () {void main () { int value;int value; value = factorial(3);value = factorial(3);}}

A recursive functionA recursive function

Page 20: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 20 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Nested Nested SubprogramsSubprograms (1) (1)

program program Main_2Main_2;; var X : integer;var X : integer; procedure procedure BigsubBigsub;; var A, B, C : integer;var A, B, C : integer; procedure procedure Sub1Sub1;; var A, D : integer;var A, D : integer; beginbegin A := B + C;A := B + C; end;end; procedure procedure Sub2Sub2(X : integer);(X : integer); var B, E : integer;var B, E : integer; procedure procedure Sub3Sub3;; var C, E : integer;var C, E : integer; beginbegin Sub1;Sub1; E := B + A:E := B + A: end;end; beginbegin Sub3;Sub3; A := D + E;A := D + E; end;end; beginbegin Sub2(7);Sub2(7); end;end; beginbegin Bigsub;Bigsub; end.end. Ada

Pointer to the activation record Pointer to the activation record instance of the instance of the static parentstatic parent

Activation RecordActivation Record

Local VariablesLocal Variables

ParametersParameters

Dynamic LinksDynamic Links

Return AddressReturn Address

Static LinksStatic Links

Page 21: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 21 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Nested Nested SubprogramsSubprograms (2) (2)

program program Main_2Main_2;; var X : integer;var X : integer; procedure procedure BigsubBigsub;; var A, B, C : integer;var A, B, C : integer; procedure procedure Sub1Sub1;; var A, D : integer;var A, D : integer; beginbegin A := B + C;A := B + C; end;end; procedure procedure Sub2Sub2(X : integer);(X : integer); var B, E : integer;var B, E : integer; procedure procedure Sub3Sub3;; var C, E : integer;var C, E : integer; beginbegin Sub1;Sub1; E := B + A:E := B + A: end;end; beginbegin Sub3;Sub3; A := D + E;A := D + E; end;end; beginbegin Sub2(7);Sub2(7); end;end; beginbegin Bigsub;Bigsub; end.end. Ada

Static C

hain

Static C

hain

Page 22: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 22 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Nested Nested SubprogramsSubprograms (3) (3)

program program Main_2Main_2;; var X : integer;var X : integer; procedure procedure BigsubBigsub;; var A, B, C : integer;var A, B, C : integer; procedure procedure Sub1Sub1;; var A, D : integer;var A, D : integer; beginbegin A := B + C;A := B + C; end;end; procedure procedure Sub2Sub2(X : integer);(X : integer); var B, E : integer;var B, E : integer; procedure procedure Sub3Sub3;; var C, E : integer;var C, E : integer; beginbegin Sub1;Sub1; E := B + A:E := B + A: end;end; beginbegin Sub3;Sub3; A := D + E;A := D + E; end;end; beginbegin Sub2(7);Sub2(7); end;end; beginbegin Bigsub;Bigsub; end.end. Ada

SStatictatic D Depthepth:: the depth of the depth of nesting of nesting of aa scope scope e.g., e.g., Sub1 Sub1 SD = 2 SD = 2

CChainhain O Offset ffset (N(Nestingesting D Depthepth)):: the difference between the staticthe difference between the static depth of depth of aa reference and that of reference and that of the scope where it is declaredthe scope where it is declared e.g., e.g., A in Sub3A in Sub3 CO = 2 CO = 2

A reference can be represented A reference can be represented by: by: ((chain_offsetchain_offset, , local_offsetlocal_offset)), , where where local_offsetlocal_offset is the is the offset in offset in the activation recordthe activation record of the of the variable being referencedvariable being referenced

Page 23: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 23 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Implementing Implementing Nested Nested SubprogramsSubprograms (4) (4)

program program Main_2Main_2;; var X : integer;var X : integer; procedure procedure BigsubBigsub;; var A, B, C : integer;var A, B, C : integer; procedure procedure Sub1Sub1;; var A, D : integer;var A, D : integer; beginbegin A := B + C;A := B + C; end;end; procedure procedure Sub2Sub2(X : integer);(X : integer); var B, E : integer;var B, E : integer; procedure procedure Sub3Sub3;; var C, E : integer;var C, E : integer; beginbegin Sub1;Sub1; E := B + A:E := B + A: end;end; beginbegin Sub3;Sub3; A := D + E;A := D + E; end;end; beginbegin Sub2(7);Sub2(7); end;end; beginbegin Bigsub;Bigsub; end.end. Ada

References to References to the variable Athe variable A

(0, 3)(0, 3)

(2, 3)(2, 3)

(1, 3)(1, 3)

Page 24: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 24 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Evaluation of the Static Chain MethodEvaluation of the Static Chain Method

A nonlocal reference is A nonlocal reference is slowslow if the number of if the number of scopes between the reference and the scopes between the reference and the declaration of the referenced variable is largedeclaration of the referenced variable is large

Time-critical code is difficult, because the costs Time-critical code is difficult, because the costs of of nonlocal references are not equalnonlocal references are not equal, and can , and can change with code upgrades and fixeschange with code upgrades and fixes

DisplayDisplay: An alternative to static chains. An : An alternative to static chains. An array of static linksarray of static links ( (addresses of activation addresses of activation record instancesrecord instances). ).

Page 25: April 23, 2004 1 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)

April 23, 2004 25 ICE 1341 – Programming Languages (Lecture #16) In-Young Ko

Program BlocksProgram Blocks

2. Allocate locals 2. Allocate locals on top of the on top of the activation activation record instancerecord instance Must use a Must use a

different different method to method to access localsaccess locals

void main () {void main () {int x, y, z;int x, y, z;while ( while ( … ) {… ) { int a, b, c;int a, b, c; … … while ( … ) {while ( … ) {

int d, e;int d, e; … …

}}}}while ( … ) {while ( … ) { int f, g;int f, g; … …}}……

}}

1. Treat blocks as 1. Treat blocks as parameterless subprogramsparameterless subprograms