CS 2104 Prog. Lang. Concepts Subprograms

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
PZ05A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05A - Abstract data types Programming Language Design.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Semantics of Calls and Returns
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Chapter 9: Subprogram Control
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.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Chapter 8 :: Subroutines and Control Abstraction
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Runtime Environments Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Basic Semantics Associating meaning with language entities.
Programming Languages and Paradigms Imperative Programming.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
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.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
1 Subroutines Structure of Programming Language. 2 Procedures and Functions There are two categories of subprograms Function: subroutine that returns.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
CSC 8505 Compiler Construction Runtime Environments.
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
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms Chapter 10
PZ05A - Abstract data types
CSC 533: Programming Languages Spring 2015
Implementing Subprograms
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Parameter transmission
PZ09B - Parameter transmission
Abstract data types Programming Language Design and Implementation
Parameter transmission
Parameter transmission
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSC 533: Programming Languages Spring 2018
Parameter transmission
CSC 533: Programming Languages Spring 2019
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Parameter transmission
Presentation transcript:

CS 2104 Prog. Lang. Concepts Subprograms Lecturer : Dr. Abhik Roychoudhury School of Computing Reading: Chapters 4.6, 5.1, 5.2

Topics covered Activation Records Scope : Static, Dynamic Lifetime of variables Parameter passing Value Reference Value-Result, Name etc. Acknowledgements: Diagrams etc. adapted from Pratt/Zelkowitz lecture notes.

Implementation of subprogram storage Each subprogram has a block of storage containing such information, called an activation record. Consider the following C subprogram: float FN( float X, int Y) const initval=2; #define finalval 10 float M(10); int N; N = initval; if(N<finalval){ ... } return (20 * X + M(N)); } Information about procedure FN is contained in its activation record.

Activation records

Dynamic nature of activation records Each invocation of FN causes a new activation record to be created. Thus the static code generated by the compiler for FN will be associated with a new activation record, each time FN is called. As we will see later, a stack structure is used for activation record storage.

Dynamic nature of activation records

Subprogram control Remember that data storage for subprograms is in an activation record. var X: integer; X is of type integer. L-value of X is some specific offset in an activation record.

Subprogram control Goal is to look at locating activation record for P. Given an expression: X = Y + Z 1. Locate activation record containing Y. 2. Get L-value of Y from fixed location in activation record. 3. Repeat process for Z and then X.

Scope rules Scope rules: The scope of a variable are the set of statements where the variable may be accessed (i.e., named) in a program. Static scope: Scope is dependent on the syntax of the program. Dynamic scope: Scope is determined by the execution of the program.

Scope rules Static nested scope: A variable is accessible in the procedure it is declared in, and all procedures internal to that procedure, except a new declaration of that variable name in an internal procedure will eliminate the new variable's scope from the scope of the outer variable. A variable declared in a procedure is local in that procedure; otherwise it is global.

Review of scope rules Q and T are declarations of procedures within P, so scope of names Q and T is same as scope of declaration a. R and S are declarations of procedures in Q. U is a declaration of a procedure in T. Storage managed by adding activation records, when procedure invoked, on a stack.

Activation record stack

Activation record stack Problem is: How to manage this execution stack? Two pointers perform this function: 1. Dynamic link pointer points to activation record that called (invoked) the new activation record. It is used for returning from the procedure to the calling procedure. 2. Static link pointer points to the activation record that is global to the current activation record (i.e., points to the activation record of the procedure containing the declaration of this procedure).

Activation record structure

Example of act. record stack Declarations Var A in P B in Q C in R

Activation record example 1 Ex 1. In R: C := B+A;  C local, A,B global For each variable, get pointer to proper activation record. Assume AR is current act.record pointer (R). 1. B is one level back: Follow AR.SL to get AR containing B. Get R-value of B from fixed offset L-value 2. A is two levels back: Follow (AR.SL).SL to get activation record containing A. Add R-value of A from fixed offset L-value 3. C is local. AR points to correct act record. Store sum of B+A into L-value of C

Activation record example 2 Example 2. Execution in procedure Q: A := B  B is local, A global Assume AR is current activation record pointer (Q) 1. B is now local. AR points to activation record Get R-value from local activation record

Activation record example 2 2. A is now one level back AR.SL is activation record of P Store R-value of B into L-value of A Compiler knows static structure, so it can generate the number of static link chains it has to access in order to access the correct activation record containing the data object. This is a compile time, not a runtime calculation.

Use of displays Problem with static links: Many links to follow if deeply nested. (But how common is that?) Use of displays reduces access always to 2 instructions: 1. Access Display[I] = Act. Rec. pointer at level I 2. Get L-value of variable (fixed offset in act. rec.)

Use of displays Activation record structure: Static chain copied into display vector on subprogram entry.

Display for previous example

Blocks C, C++, Java allow any compound statement to have declarations and thus define a new scope. If (list[I] < list[j]) { int temp; temp = list[I]; list[I] = list[j]; list[j] = temp; } Scopes created by blocks treated like those created by subprograms.

Blocks in C Blocks in C are handled as union record types: P() { int I; {int J; . . . } { int K; int L; J and K cannot exist at the same time, so use the same space for them.

Lifetime The lifetime of a variable is that period during program execution when the storage for the declared variable exists in some activation record (i.e., from the time the activation record for the declaring procedure is created until that activation record is destroyed). The scope of a variable is the portion of the source program where the data item can be accessed (i.e., the name of the item is a legal reference at that point in the program).

Lifetimes of variables Lifetime of variable X is the period when the activation record for P exists. Note that we can change the scope, but the not lifetime of a variable by nesting procedures:

Parameter passing Parameter: A variable in a procedure that represents some other data from the procedure that invoked the given procedure. Parameter transmission: How that information is passed to the procedure. The parameter is also called the formal argument.The data from the invoking procedure is called the actual argument or sometimes just the argument.

Parameter passing Usual syntax: Actual arguments: call P(A, B+2, 27+3) Parameters: Procedure P(X, Y, Z) What is connection between the parameters and the arguments? Call by name Call by reference Call by value Call by value-result

Language dependent Difference languages have different mechanisms: ALGOL - name, value Pascal - value, reference C - value (BUT pointers give us reference Constant tension between desire for efficiency and semantic correctness in defining parameter transmission.

Call by name Substitute argument for parameter at each occurrence of parameter: Invocation: P(A, B+2, 27+3) Definition: procedure P(X,Y,Z) {int I; I=7; X = I + (7/Y)*Z;} Meaning: P(X,Y,Z) {int I; I=7; A=I+(7/(B+2))*(27+3);}

Call by name This is a true macro expansion. Simple semantics, BUT: 1. Implementation. How to do it? 2. Aliases. What if statement of P were: I = A? 3. Expressions versus statements: If we had D=P(1,2,3) and a return(42) in P, what does semantics mean? 4. Error conditions: P(A+B, B+2, 27+3)

Impl. of call by name A thunk is the code which computes the L-value and R-value of an argument. For each argument, pass code address that computes both L-values and R-values of arguments. P(A, B+2, 27+3) generates: jump to subroutine P address of thunk to return L-value(A) address of thunk to return R-value(A) address of thunk to return L-value(B+2) address of thunk to return R-value(B+2) address of thunk to return L-value(27+3) address of thunk to return R-value(27+3)

Impl. of call by name To assign to X, call thunk 1, To access X, call thunk 2 To assign to Y, call thunk 3, To access Y, call thunk 4 To assign to Z, call thunk 5, To access Z, call thunk 6 Issue: Assignment to (B+2): How? Call by name is conceptually convenient, but inefficient.

Examples of Call by Name 1. P(x) {x = x + x;} Seems simple enough … Y = 2; P(Y); write(Y)  means Y = Y+Y write(Y)  prints 4 2. int A[10]; for(I=0; I<10; I++) {A[I]=I;}; I=1; P(A[I])  A[1] = A[1] + A[1]  A[1] set to 2

Examples of Call by Name 3. But: F {I = I + 1; return I;} What is: P(A[F])? P(A[F])  A[F] = A[F]+A[F]  A[I++] = A[I++]+A[I++]  A[2] = A[3]+A[4] 4. Write a program to exchange values of X and Y: (swap(X,Y)) Usual way: swap(x,y) {t=x; x=y; y=t;} Cannot do it with call by name. Cannot handle both of following: swap(I, A[I]) swap(A[I],I) One of these must fail.

Call by reference Pass the L-value of the argument for the parameter. Invocation: P(A, B+2, 27+3) whose R-value is the L-value of the argument.

Call by reference Implementation: Temp1 = B+2 Temp2 = 27+3 jump to subroutine P L-value of A L-value of Temp1 L-value of Temp2 This is the most common parameter transmission mechanism. In the procedure activation record, parameter X is a local variable whose R-value is the L-value of the argument.

Call by value Pass the R-value of the argument for the parameter. Invocation: P(A, B+2, 27+3)

Call by value Implementation: Temp1 = B+2 Temp2 = 27+3 jump to subroutine P R-value of A R-value of Temp1 R-value of Temp2 In procedure activation record, parameter X is a local variable whose R-value is the R-value of the argument.

Call by reference in C C only has call by value, BUT pointer variables allow for simulating call by reference: P(i, j)  passes i and j by value. P(&i, &j)  passes L-values of i and j. P(*x, *y) {*x = *y + 1;}  arguments are addresses (pointers)

Call by value result Call by value-result: Call by value, AND pass back the final value to argument upon return. Similar to in-out parameters, discussed next. Similar to call by reference, except for aliasing. (two variable names refering to the Same memory location) E.g. if a global variable is parameter passed by reference.

In-out semantics Parameters in Ada are based upon use (semantics), not implementation: in - argument value will be used in procedure. out - parameter value will be used in calling program. in out - both uses of arguments and parameters P(X in integer; Y out integer; Z in out integer); begin ... end;

In-out semantics In Ada 83, language definition allowed some latitude in implementation  as long as implementation consistent, ok. But this meant that the same program could give different answers from different standards conforming compilers In Ada 95, more restricted: in integer is value, out integer is value-result, composite (e.g., arrays) is reference.

Example of parameter passing Main {A = 2; B = 5; C = 8; D = 9; P(A, B, C, D); write(A, B, C, D); P(U, V, W, X) {V = U+A; W = A+B; A = A+1; X = A+2; write(U, V, W, X)} Fill in table assume parameters are of the given type:

Example of parameter passing A B C D U V W X print P print main Call by name Call by reference Call by value value-result When do call by name and call by reference differ? When L-value can change between parameter references. E.g., P(I, A[I])