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

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #18 Lecture #18 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
1 Chapter 8 – Subroutines and Control Abstractions.
Chapter 9 Subprograms.
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
ISBN Chapter 10 Implementing Subprograms.
PLLab, NTHU,Cs2403 Programming Languages Subprogram and its implementation.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
ISBN Chapter 9 Subprograms. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Chapter 9 Topics Introduction Fundamentals.
Semantics of Calls and Returns
CSE 452: Programming Languages
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
ISBN Chapter 10 Implementing Subprograms –Nested Subprograms –Blocks –Implementing Dynamic Scoping.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
ISBN Chapter 10 Implementing Subprograms.
1 Subprograms Fundamentals of subprograms Design issues for subprograms –Parameter-passing methods –Type checking of parameters –Static or dynamic storage.
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
Chapter 9 Subprograms Fundamentals of Subprograms
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
1 Chapter 9 © 2002 by Addison Wesley Longman, Inc. 9.2 Fundamentals of Subprograms - General characteristics of subprograms: 1. A subprogram has a single.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 8 Fundamental Characteristics of Subprograms 1. A subprogram has a single entry point 2. The.
1 CS Programming Languages Class 15 October 17, 2000.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Week 7 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for.
Subprograms subroutines, procedures, functions, methods.
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
Chapter 9 Subprograms. 2 Fundamentals of Subprograms Each subprogram has a single entry point The calling program is suspended during execution of the.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Subprograms - implementation
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues.
ISBN Chapter 9 Subprograms. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Chapter Ten: Implementing Subprograms Lesson 10. Implementing?  Previous lesson: parameter passing  In, out, inout  By value  By reference  Passing.
ISBN Chapter 10 Implementing Subprograms.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Chapter 9 Subprograms. Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing.
Implementing Subprograms
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Subprograms In Text: Chapter 9.
Implementing Subprograms
Implementing Subprograms
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

April 23, 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

April 23, 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

April 23, 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

April 23, 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

April 23, 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++

April 23, 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

April 23, 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

April 23, 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

April 23, 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

April 23, 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

April 23, 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)

April 23, 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

April 23, 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

April 23, 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

April 23, 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

April 23, 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

April 23, 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

April 23, 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

April 23, 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

April 23, 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

April 23, 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

April 23, 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

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

April 23, 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).

April 23, 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