Special Topics: Programming Languages

Similar documents
Special Topics: Programming Languages

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Implementing Subprograms

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

CS 314 Principles of Programming Languages

Programming Languages: Lecture 12

G Programming Languages - Fall 2012

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Concepts Introduced in Chapter 7

Chapter 10. Implementing Subprograms ISBN

CA Compiler Construction

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Today's Topics. CISC 458 Winter J.R. Cordy

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS 314 Principles of Programming Languages. Lecture 13

CSc 520 Principles of Programming Languages

Topic 3-a. Calling Convention 2/29/2008 1

References and pointers

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Run-Time Environments

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Run-time Environments

Run-time Environments

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

NOTE: Answer ANY FOUR of the following 6 sections:

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

CSE 504: Compiler Design. Runtime Environments

CS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation

Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

Implementing Subprograms

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Lecture08: Scope and Lexical Address

UNIT 3

CSE 307: Principles of Programming Languages

Chapter 10. Implementing Subprograms

Programming Languages

Chapter 3:: Names, Scopes, and Bindings

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

CSE 3302 Notes 5: Memory Management

Run Time Environments

G Programming Languages - Fall 2012

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Run-time Environments - 2

System Software Assignment 1 Runtime Support for Procedures

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Scope, Functions, and Storage Management

Programming Languages Third Edition. Chapter 7 Basic Semantics

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction

LECTURE 14. Names, Scopes, and Bindings: Scopes

Subprograms, Subroutines, and Functions

Procedure and Object- Oriented Abstraction

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

CIT Week13 Lecture

Informatica 3 Marcello Restelli

Runtime Support for Algol-Like Languages Comp 412

CS 230 Programming Languages

The Procedure Abstraction

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

LECTURE 18. Control Flow

CS558 Programming Languages

Informatica 3 Syntax and Semantics

Chapter 10 Implementing Subprograms

COP4020 Fall 2006 Final Exam

Procedures: Advanced Topics Chapter 12

Topic 7: Activation Records

Binding and Variables

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

Compilers and computer architecture: A realistic compiler to MIPS

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Chapter 9 :: Subroutines and Control Abstraction

CSCI Compiler Design

Principles of Programming Languages

The Activation Record (AR)

Run-time Environment

CSC 2400: Computer Systems. Using the Stack for Function Calls

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

CS 415 Midterm Exam Spring 2002

CS558 Programming Languages

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Names, Bindings, Scopes

Implementing Subroutines. Outline [1]

Optimizer. Defining and preserving the meaning of the program

Compiler Construction

Weeks 6&7: Procedures and Parameter Passing

Run-Time Data Structures

CS 345. Functions. Vitaly Shmatikov. slide 1

Run-time Environments -Part 1

MIDTERM EXAM (Solutions)

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Memory Management and Run-Time Systems

Transcription:

Lecture #17 0 V22.0490.001 Special Topics: Programming Languages B. Mishra New York University. Lecture # 17

Lecture #17 1 Slide 1 Runtime Representations Variable Names Environment L-values Scope, Extent & Binding Time Scope: Portion of the program text in which the identifier has a specific meaning. Extent: Duration for which the identifier is allocated the location during execution (runtime). Binding Time: Time at which the association is made between an identifier and its allocated location. Usually, Extent Scope.

Lecture #17 2 Slide 2 Dangling Reference Problem Recall storage insecurity, when Example type r = record... end; t = ^r; Extent > Scope. procedure P; var q: t; procedure A; var s: t; begin new(s); q:= s; dispose(s); end; begin... A...; q :=...; (*L-value whose lifetime has passed*) end.

Lecture #17 3 Slide 3 Static Storage Management FORTRAN (as implemented commonly) The main program and each subroutine may declare local data. But all data are preserved across successive calls on subroutines. A given subroutine cannot be called if there is an as yet unfinished call of that same subroutine. Forbids both direct and indirect (mutual) recursion. All storage for FORTRAN data can be allocated statically before execution begins.

Lecture #17 4 Slide 4 Activation Record Activation Record: Set of informations necessary for the execution of a subprogram. FORTRAN When a subroutine executes, it always finds its activation record in the same place. Runtime structure of FORTRAN : Needs only to store the return address in the activation record of the called subroutine.

Slide 5 Lecture #17 5 Static Storage Management in FORTRAN AR for P AR for R AR for Q return return return Call Q Call R code for P code for Q Activation Records: SUBROUTINE P(...) SUBROUTINE Q(...) SUBROUTINE R(...)......... CALL Q(...) CALL R(...)......... RETURN RETURN RETURN END END END In FORTRAN, each of the following has an Activation Record Each subroutine The mainprogram Each COMMON block

Slide 6 Stack Based Modern Languages (ALGOL-like) Lecture #17 6 Subprograms are allowed to be recursive. More than one activation of the same subprogram can exist simultaneously. Each invocation of the subprogram may have 1) A different return point 2) Different values for local variables. Number of activations of a subprogram (that can exist simultaneously) is unpredictable. The activation record for a subprogram can only be created, when the subprogram is actually called. Support for scope-entry declaration of local variables.

Slide 7 Implications of Call-Time Allocation of AR s Lecture #17 7 Allocation lasts for precisely the duration of a particular subprograms execution. Allocate AR, when the execution begins. Release that space, when execution finishes. If a subprogrampcalls a subprogramqthen P cannot complete before Q. Extent(Q s AR) Extent(P s AR) Q s extent is wholly contained in P s. Storage requirements of AR s are Last- In-First-Out. Stack-like data structure for AR s suffices.

Lecture #17 8 P Procedure Call to P Slide 8 Procedure Call Push a new AR for P on stack (containing return address as its return field) Execute in the new environment Pop the current AR for P (saving the return address in T) Go to T.

Slide 9 Activation Records (AR) Stack Based Storage Management Algol and its relatives: Lecture #17 9 STACK AR for P AR for Q AR for P... return return return Call Q Call P code for P code for Q Mutually Recursive Procedures: procedure P(...)<-- procedure Q(...)... \... begin \ begin... \... Q(...) ----- P(...)... ->... end ------------------/ end Each Activation of a procedure has an AR (allocated dynamically).

Slide 10 Lecture #17 10 Up-Level Addressing and the Display In the absence of reference to global variables (procedures reference only formal and locally declared variables) L-value of a variable Address of the current AR + offset address within the AR Procedure Call: Allocate AR on top of the stack Save caller s AR address in its own AR Return from Call: Restore the old AR address Branch to the return point

Slide 11 Reference to the Global Variable Lecture #17 11 Reference to the Global Variables: Simple Case Global variables are all declared in the main program Outer-most Level. Each variable reference is either: Relative to the current AR (for locals), or Relative to the stack base (for globals). What about the intermediate non-local variables?: Up-Level Addressing Problem

Slide 12 Up-level Addressing Problem Intermediate Non-Local Variables Algol, Pascal, Ada, Scope Rules procedure P; begin var x, y: T1; procedure Q; begin var z: T2; Lecture #17 12 procedure R; begin var: a, b: T3;... <----------z is accessible in Q & R end;... <----------x, y are accessible in P, Q & R end;... end; Can R tell where x, y and z are located? Not easily (specially, if R calls itself recursively.)

Lecture #17 13 Slide 13 Displays Lexical Level of a Procedure: An integer value one greater than the lexical level of the procedure in which it is declared. Lexical level of the main Program = 0. LexLev(P) = n LexLev(Q) = n + 1 & LexLev(R) = n + 2. Up-level Addressing Accessing an intermediate non-local variable at level L, where 0 < L < Current-level. Note: The number of AR s accessible to procedure R = Lexlev(R) + 1. (Dictated by the static nature of the lexical scope rule.)

Slide 14 Displays and Setting Them Up Lecture #17 14 Solving the Up-level Addressing Problem: Add LexLev(R) + 1 locations to the AR of R. These locations are called a DISPLAY Vector of pointers to accessible AR s Setting the displays: Assumption: No procedure parameters (False in Ada) Procedure P calls procedure Q: Assumption LexLev(Q) Lexlev(P) 1. LexLev(P) = n then Display(P) = D P [0..n] 2. Two cases to consider: 1) Lexlev(Q) = n (P and Q are at the same level) and 2) Lexlev(Q) = m < n (Q is up-level from P)

D Q [n] := AR Q The nth display of D Q [m] := AR Q The mth display of Lecture #17 15 Slide 15 Setting the Displays Case I) Lexlev(Q) = n (P and Q are at the same level) Display(Q) = D Q [0..n] D Q [0..n 1] := D p [0..n 1] First n displays are the same Q is the base of the current AR Case II) Lexlev(Q) = m < n (Q is up-level from P) Display(Q) = D Q [0..m] D Q [0..m 1] := D p [0..m 1] First m displays are the same Q is the base of the current AR More Efficient Implementation 1) Maintain one vector common to all AR s + 2) One additional word for each AR.

Slide 16 Sample Display Configuration STACK Lecture #17 16 DISPLAY AR(R ) AR(M) AR(P) AR(Q) AR(R ) DISPLAY AR(M) AR(P) AR(Q) AR(R ) AR(R ) DISPLAY AR(R) AR(M) AR(P) AR(Q) AR(R) DISPLAY AR(Q) AR(M) AR(P) AR(Q) DISPLAY AR(P) AR(M) AR(P)... DISPLAY Main Program AR AR(M) [End of Lecture #17]