Run-Time Environments

Similar documents
Concepts Introduced in Chapter 7

Run-time Environments

Run-time Environments

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

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Programming Languages

Runtime Environments I. Basilio B. Fraguela

Implementing Subroutines. Outline [1]

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

Chapter 10. Implementing Subprograms ISBN

G Programming Languages - Fall 2012

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

Implementing Subprograms

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

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

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

Lecture08: Scope and Lexical Address

Run-Time Environments

Scope, Functions, and Storage Management

CS61C : Machine Structures

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

Programming Languages: Lecture 12

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

Module 27 Switch-case statements and Run-time storage management

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

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

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

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

CIT Week13 Lecture

Run-time Environments - 2

Run Time Environments

Run Time Environment

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Run-Time Environments/Garbage Collection

Compilers and Code Optimization EDOARDO FUSELLA

Topic 7: Activation Records

Chapter 10. Implementing Subprograms

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

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

Procedures and Run-Time Storage

The Procedure Abstraction

Procedures and Run-Time Storage

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

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Chapter 10 Implementing Subprograms

Procedure and Object- Oriented Abstraction


Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

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

Implementing Subprograms

COP4020 Programming Languages. Names, Scopes, and Bindings Prof. Robert van Engelen

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

Qualifying Exam in Programming Languages and Compilers

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

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

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

THEORY OF COMPILATION

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

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

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

G Programming Languages - Fall 2012

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

COP5621 Exam 4 - Spring 2005

Run-Time Environements

Chapter 5. Names, Bindings, and Scopes

In Java we have the keyword null, which is the value of an uninitialized reference type

The role of semantic analysis in a compiler

Lecture 9: Procedures & Functions. CS 540 George Mason University

Programming Languages Third Edition. Chapter 7 Basic Semantics

CS201 - Introduction to Programming Glossary By

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

LECTURE 14. Names, Scopes, and Bindings: Scopes

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

COMPILER DESIGN - RUN-TIME ENVIRONMENT

6. Names, Scopes, and Bindings

Overview COMP Microprocessors and Embedded Systems. Lectures 18 : Pointers & Arrays in C/ Assembly

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

Lecture 7: Procedures and Program Execution Preview

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

CSCI312 Principles of Programming Languages!

2. Reachability in garbage collection is just an approximation of garbage.

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Names and Abstractions: What s in a Name?

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CSE 504: Compiler Design. Runtime Environments

Short Notes of CS201

LECTURE 18. Control Flow

Pointers II. Class 31

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS 406/534 Compiler Construction Putting It All Together

CSC 533: Organization of Programming Languages. Spring 2005

Weeks 6&7: Procedures and Parameter Passing

CSE 3302 Notes 7: Control Abstraction

Structure of Programming Languages Lecture 10

System Software Assignment 1 Runtime Support for Procedures

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Time : 1 Hour Max Marks : 30

Transcription:

CS308 Run-Time Environments Li Jiang Department of Computer Science and Engineering Shanghai Jiao Tong University

Current Progress Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code Generator Analysis Intermediate Language Symbol Table Code Optimizer Code Generator Synthesis Target Language

Run-Time Environment Before discussing code generation, we need to understand what we are trying to generate. There are a number of standard techniques for structuring executable code that are widely used.

Run-Time Environment Cont d Run-time environment is created by the compiler to support target programs. allocation of storage locations for the objects mechanisms to access variables linkages between procedures mechanisms for passing parameters interfaces to the operating system input/output devices

Run-Time Storage Organization low end Code Static Data Stack Memory locations for code are determined at compile time. Locations of static data can also be determined at compile time. Data objects allocated at run-time. (Activation Records) high end Heap Other dynamically allocated data objects at run-time. (For example, malloc area in C).

Procedure Activations An execution of a procedure starts at the beginning of the procedure body; When the procedure is completed, it returns the control to the point immediately after the place where that procedure is called. Each execution of a procedure is called as its activation. Lifetime of an activation of a procedure is the sequence of the steps between the first and the last steps in the execution of that procedure (including the other procedures called by that procedure). If a and b are procedure activations, then their lifetimes are either non-overlapping or are nested. (single thread) If a procedure is recursive, a new activation can begin before an earlier activation of the same procedure has ended.

Activation Tree We can use a tree (called activation tree) to show the way control enters and leaves activations. In an activation tree: Each node represents an activation of a procedure. The root represents the activation of the main program. The node a is a parent of the node b iff the control flows from a to b. The node a is left to the node b iff the lifetime of a occurs before the lifetime of b.

Activation Tree Cont d program main; procedure s; begin... end; procedure p; procedure q; begin... end; begin q; s; end; begin p; s; end; enter main enter p enter q exit q enter s exit s exit p enter s exit s exit main A Nested Structure

Activation Tree Cont d Sequence of procedure is depth-first traversal of the activation tree main p s q s Activation Tree Now control is in s; what procedures are live? What is their order?

Remarks for Activation Tree The activation tree depends on run-time behavior. The activation tree may be different for every program input. Since activations are properly nested, a stack can track currently active procedures. What data structure? why?

Control Stack The flow of the control in a program corresponds to a depth-first traversal of the activation tree that: starts at the root, visits a node before its children, and recursively visits children at each node in a left-to-right order. A stack (called control stack) can be used to keep track of live procedure activations. An activation record is pushed onto the control stack as the activation starts. That activation record is popped when that activation ends. When node n is at the top of the control stack, the stack contains the nodes along the path from n to the root.

Activation Records Information needed by a single execution of a procedure is managed using a contiguous block of storage called activation record. An activation record is allocated when a procedure is entered, and it is de-allocated when that procedure exits. Size of each field can be determined at compile time (Although actual location of the activation record is determined at run-time). Except that if the procedure has a local variable and its size depends on a parameter, its size is determined at the run time.

Activation Records Cont d return value actual parameters control link access link saved machine status local data temporaries store the returned values to the calling procedure. In practice, we may use a machine register for the return value. used by the calling procedure to supply parameters to the called procedure points to the activation record of the caller used to refer nonlocal data, e.g., in other activation records holds information about the state of the machine before the procedure is called. holds data that local to an execution of a procedure temporary variables

Activation Records Example program main; procedure p; var a:real; procedure q; var b:integer; begin... end; begin q; end; procedure s; var c:integer; begin... end; begin p; s; end; q p main s a: main p q stack b:

Activation Records for Recursive Procedures program main; procedure p; function q(a:integer):integer; begin if (a=1) then q:=1; else q:=a+q(a-1); end; begin q(3); end; begin p; end; main p q(3) a:3 main p q(3) q(2) a:2 q(2) q(1) a:1 q(1)

Test Yourself The following function computes Fibonacci numbers recursively. Assume that the initial call is f(4). function f(n:integer):integer; var s:integer; var t:integer; begin if (n<2) then f:=1; else begin s:=f(n-1); t:=f(n-2); f:=s+t; end; end; a) Draw the complete activation tree. b) Draw the activation records in the stack the first time f(0) is about to return?

Solution a) f(4) b) f(4) f(1) f(2) f(3) f(0) f(1) f(1) f(2) f(0) n: 4 s: t: n: 3 s: t: n: 2 s: 1 t: n: 0 s: t: f(3) f(2) f(0)

Creation of An Activation Record Who allocates an activation record of a procedure? Some part is created by the caller of that procedure before that procedure is entered. Some part of the activation record of a procedure is created by that procedure itself immediately after that procedure is entered. Who de-allocates? Callee de-allocates the part allocated by Callee. Caller de-allocates the part allocated by Caller.

Creation of An Activation Record Cont d return value actual parameters control link access link saved machine status local data temporaries return value actual parameters control link access link saved machine status local data temporaries Caller s Activation Record Caller s Responsibility Callee s Activation Record Callee s Responsibility

Variable-Length Data actual parameters return value control link access link saved machine status local data pointer to a pointer to b temporaries Variable-length data is allocated after temporaries, and there is a link from local data to that array. array a array b

Variable Scopes The same variable name can be used in different parts of the program. The scope rules of the language determine which declaration of a name applies when the name appears in the program. An occurrence of a variable (a name) is: local: If that occurrence is in the same procedure in which that name is declared. non-local: Otherwise (i.e., it is declared outside of that procedure) procedure p; var b:real; procedure q; var a: integer; begin a := 1; b := 2; end; begin... end; a is local b is non-local

Access to Nonlocal Names Scope rules of a language determine the treatment of references to nonlocal names. Scope rules: Lexical scope (static scope) Most-closely nested rule is used. Determines the declaration that applies to a name by examining the program text alone at compiletime. Pascal, C,... Dynamic scope Determines the declaration that applies to a name at run-time. Lisp, APL,...

Lexical Scope The scope of a declaration in a blockstructured language is given by the mostly closed rule. Each procedure (block) will have its own activation record. procedure begin-end blocks (treated same as procedure without creating most part of its activation record) A procedure may access to a nonlocal name using: access links in activation records, or displays (an efficient way to access to nonlocal names)

Access Links Access links are implemented by adding pointers to each activation record. If procedure p is nested immediately within procedure q in the source code, then the access link in any activation of p points to the most recent activation of q. Access links form a chain from the activation record at the top of the stack to a sequence of activations at lower nesting depths. Along this chain are all the activations whose data and procedures are accessible to the currently executing procedure.

Access Links Example program main; var a:int; procedure p; var d:int; begin a:=1; end; procedure q(i:int); var b:int; procedure s; var c:int; begin p; end; begin if (i<>0) then q(i-1) else s; end; begin q(1); end; Access Links main access link a: q(1) access link i,b: q(0) access link i,b: s access link c: p access link d: Bottom Top

Procedure Parameters Access links must be passed with procedure parameters. main access link Bottom program main; procedure p(procedure a); begin a; end; procedure q; procedure s; begin... end; begin p(s) end; begin q; end; q access link p access link a: s s access link Top

Accessing Nonlocal Variables program main; var a:int; procedure p; var b:int; procedure q(); var c:int; begin c:=a+b; end; begin q; end; begin p; end; main access link a: p access link b: q access link c: addrc := offsetc(curr_pos) pos := traceback(curr_pos) addrb := offsetb(pos) pos := traceback(pos) addra := offseta(pos) ADD addra, addrb, addrc

Displays An array of pointers to activation records can be used to access activation records. This array is called as displays. For each level, there will be an array entry. 1: 2: 3: Current activation record at level 1 Current activation record at level 2 Current activation record at level 3

Accessing Nonlocal Variables using Display program main; var a:int; procedure p; var b:int; procedure q(); var c:int; begin c:=a+b; end; begin q; end; begin p; end; main access link a: p access link b: q access link D[1] D[2] D[3] addrc := offsetc(d[3]) addrb := offsetb(d[2]) addra := offseta(d[1]) ADD addra, addrb, addrc c:

Dynamic Scope A nonlocal name s binding is not determined lexically, but inherits that of the caller. Example: int x = 0; int f() { return x; } int main() { int x = 1; return f(); } Lexical scope: return 0 Dynamic scope: return 1

Implementing Dynamic Scope Stack-based method: traverses the runtime stack, checking each activation record for the first value of the identifier Table-based method: uses a table to associate each name with its current meaning when a procedure is activated, locate each local name to the entry in the table and store the name s previous meaning in the activation record

Problems with Dynamic Scope Bad program readability, understanding of the program relies on a simulation of the it Not suitable for static type checking Therefore, dynamic scope is not commonly used

Heap A value that outlives the procedure that creates it cannot be kept in the activation record Example: int* func() { return new int[10]; } Languages with dynamically allocated data use a heap to store dynamic data

Where to Put the Heap? The heap is an area of memory which is dynamically allocated. Like a stack, it may grow and shrink during runtime. Unlike a stack it is not a LIFO more complicated to manage In a typical programming language implementation we will have both heap and stack allocated memory coexisting.

Where to put the heap? Let both stack and heap share the same memory area, but grow towards each other from opposite ends! SB ST Stack memory area Stack grows downward HT Heap can expand upward Heap memory area HB

Heap Management Allocation: When a program requests memory for a variable or object, the memory manager produces a chunk of contiguous heap memory of the requested size Deallocation: The memory manager returns de-allocated space to the pool of free space Problem: Heap is fragmented into too many small and noncontiguous chunks

Allocation Best-fit algorithm: allocates the requested memory in the smallest available chunk that is large enough. First-fit algorithm: places an object in the first (lowest-address) chunk in which it fits takes less time than best-fit to place objects overall performance is worse than best-fit

Managing Free Space Stack is LIFO allocation => ST moves up/down everything above ST is in use/allocated. Below is free memory. This is easy! But Heap is not LIFO, how to manage free space in the middle of the heap? SB Free HT Allocated ST Free reuse? Mixed: Allocated and Free HB

Managing Free Space How to manage free space in the middle of the heap? => keep track of free blocks in a data structure: the free list. For example we could use a linked list pointing to free blocks. freelist But where do we HT Free Next store this data structure? Free Next HB Free Next

How to keep track of free memory? Where do we find the memory to store a freelist data structure? => Since the free blocks are not used by the program, memory manager can use them for storing the freelist itself. HT HF HF free block size next free HB