Chapter 8 - Control II: Procedures and Environments

Slides:



Advertisements
Similar presentations
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
1 Chapter 8 – Subroutines and Control Abstractions.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
11 Programming Languages Principles and Practice, 2nd ed Kenneth C. Louden Chapter 5.
Chapter 5 Basic Semantics
Louden, Chapter 8 - Control II: Procedures and Environments.
ISBN Chapter 10 Implementing Subprograms.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
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)
CS 536 Spring Run-time organization Lecture 19.
Chapter 5K. Louden, Programming Languages1 Chapter 5 Basic Semantics.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
Run-time Environment and Program Organization
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
ISBN Chapter 10 Implementing Subprograms –Nested Subprograms –Blocks –Implementing Dynamic Scoping.
Chapter 5 - Basic Semantics
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
COMPILER CONSTRUCTION Principles and Practice Kenneth C. Louden.
Runtime Environments Compiler Construction Chapter 7.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
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.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Basic Semantics Associating meaning with language entities.
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
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,
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
11 Programming Languages Principles and Practice, 2nd ed Kenneth C. Louden Chapter 8.
CSC 8505 Compiler Construction Runtime Environments.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
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
ISBN Chapter 10 Implementing Subprograms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Names and Attributes Names are a key programming language feature
Run-Time Storage Organization
Run-Time Storage Organization
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Runtime Environments What is in the memory?.
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Chapter 8 - Control II: Procedures and Environments 4/23/2017 Chapter 8 - Control II: Procedures and Environments Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden © Kenneth C. Louden, 2003

Three major parts of a runtime environment: Static area allocated at load/startup time. Examples: global/static variables and load-time constants. Stack area for execution-time data that obeys a last-in first-out lifetime rule. Examples: nested declarations and temporaries. Heap or dynamically allocated area for "fully dynamic" data, i.e. data that does not obey a LIFO rule. Examples: objects in Java, lists in Scheme. Chapter 8 K. Louden, Programming Languages

Procedures/functions and the environment Local data in a procedure/function is typically allocated in a contiguous area of the environment, called an activation record or frame ("activation" means "used during a call"): © 2003 Brooks/Cole - Thomson Learning™ Chapter 8 K. Louden, Programming Languages

Languages and Environments Languages differ on where activation records must go in the environment: Fortran is static: all data, including activation records, are statically allocated. (Each function has only one activation record—no recursion!) Functional languages (Scheme,ML) and some OO languages (Smalltalk) are heap-oriented: all (or almost all) data, including activation records, are allocated dynamically. Most languages are in between: data can go anywhere (depending on its properties); activation records go on the stack. Chapter 8 K. Louden, Programming Languages

Simple stack-based allocation Described in Chapter 5. Nested declarations are added to the stack as their code blocks are entered, and removed as their code blocks are exited. Example: Stack at Point 1: { int x; int y; { int z; } { int w; // Point 1 } } x y w Chapter 8 K. Louden, Programming Languages

Activation records and the stack New piece Frames can be kept on the stack (C, Java) if access to the local environment (frame) ends with the call. To facilitate popping of frames, a control link is added to the activation record that records the position of the calling activation, so a pop can be done in one operation. © 2003 Brooks/Cole - Thomson Learning™ A pointer to the current frame is also kept, called the environment pointer, or ep (could be the stack pointer). Chapter 8 K. Louden, Programming Languages

K. Louden, Programming Languages Example (C): int x; void p( int y) { int i = x; char c; ... } void q ( int a) { int x; p(1); main() { q(2); return 0; Chapter 8 K. Louden, Programming Languages

Environment stack during exec of p: © 2003 Brooks/Cole - Thomson Learning™ Note: the ep in this picture actually points to the "bottom" of the frame, as do the control links (which are stored old ep values), so ep  top of stack. Chapter 8 K. Louden, Programming Languages

Local variable access using the ep In a typical language with a stack-based runtime environment, the local declarations in a procedure are fixed at compile-time, both in size and in sequence. This information can be used to speed up accesses to local variables, by precomputing these locations as offsets from the ep. Then the local frame need not have a name-based lookup operation (unlike the symbol table). In fact, names can be dispensed with altogether. The next slide shows how that would look for the procedure p of slide 7. Chapter 8 K. Louden, Programming Languages

Activation record of p: © 2003 Brooks/Cole - Thomson Learning™ Chapter 8 K. Louden, Programming Languages

Non-local variable access Requires that the environment be able to identify frames representing enclosing scopes. Using the control link results in dynamic scope (and also kills the fixed-offset property). If procedures can't be nested (C, C++, Java), the enclosing scope is always locatable by other means: it is either global/static (accessed directly) or belongs to the current object (accessed through the this pointer). If procedures can be nested (Ada, Pascal), to maintain lexical scope a new link must be added to each frame: the access link, pointing to the activation of the defining environment of each procedure. Chapter 8 K. Louden, Programming Languages

K. Louden, Programming Languages Example (Ada): procedure q is x: integer; procedure p (y: integer) is i: integer := x; begin ... end p; procedure r is x: float; begin p(1);... end r; r; end q; Chapter 8 K. Louden, Programming Languages

Environment during exec of p: © 2003 Brooks/Cole - Thomson Learning™ Chapter 8 K. Louden, Programming Languages

Procedure values as pointer pairs With nested procedures in lexically scoped languages requiring an access link, when a procedure is called, this access link must be available at the point of call. One way it can be is for the procedure itself to record its access link (necessary if procedures can be parameters). Then each procedure becomes a pair of pointers: a code pointer (called the instruction pointer or ip in the text), and an environment pointer (ep in the text) pointing to the definition environment of the procedure (which will become the access link during a call). Such an <ep,ip> pair is sometimes called a closure. Chapter 8 K. Louden, Programming Languages

K. Louden, Programming Languages Example (Ada): procedure lastex is procedure p(n: integer) is procedure show is begin if n > 0 then p(n - 1); end if; put(n); new_line; end show; begin -- p show; end p; begin -- lastex p(1); end lastex; Chapter 8 K. Louden, Programming Languages

Environment during 2nd call to show: Note 2nd show has a different access link Chapter 8 K. Louden, Programming Languages

Fully dynamic environments Languages with lambdas or where functions can be created locally and returned from a call (ML, Scheme). Activation records cannot in general be popped after a call. Thus, activations no longer behave as LIFO structures and must be allocated in the heap. Control links make little sense, since each control link might have to point far back in the stack. Access links and closures still make perfect sense, however. Chapter 8 K. Louden, Programming Languages

Example (Scheme–simplified from Chapter 11, see also page 338): (define (makeNewBalance balance) (define (withdraw amt) (set! balance (- balance amt)) balance) withdraw) (define withdraw1 (makeNewBalance 500)) (define withdraw2 (makeNewBalance 100)) Chapter 8 K. Louden, Programming Languages

Scheme example, continued Environment at end of previous code (see also page 339): Chapter 8 K. Louden, Programming Languages

Scheme example, concluded Environment just before exiting the call (withdraw1 200): Chapter 8 K. Louden, Programming Languages