Run-time Environments - 2

Similar documents
Run-time Environments -Part 1

Run-time Environments - 3

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

G Programming Languages - Fall 2012

Topic 7: Activation Records

Implementing Subroutines. Outline [1]

Concepts Introduced in Chapter 7

System Software Assignment 1 Runtime Support for Procedures

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

Scope, Functions, and Storage Management

CA Compiler Construction

Lecture08: Scope and Lexical Address

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

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

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

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

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CIT Week13 Lecture

Implementing Subprograms

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

Programming Languages: Lecture 12

Run-Time Environements

Chapter 10. Implementing Subprograms ISBN

Procedure and Object- Oriented Abstraction

Run-time Environment

Run-time Environments

Run-time Environments

Run Time Environments

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

Global Register Allocation - Part 3

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

Course Administration

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

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

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

Chapter 10 Implementing Subprograms

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

Compilers and Code Optimization EDOARDO FUSELLA

Chapter 10. Implementing Subprograms

Implementing Subprograms

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

Compiler Construction

Programming Languages

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

CSE 3302 Notes 5: Memory Management

Weeks 6&7: Procedures and Parameter Passing

Runtime Environments I. Basilio B. Fraguela

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

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

Run-time Environments -Part 3

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

Chapter 5. Names, Bindings, and Scopes

THEORY OF COMPILATION

Implementing Subprograms

CSE 504: Compiler Design. Runtime Environments

Memory Management and Run-Time Systems

Run Time Environment

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

INF3110 Programming Languages Runtime Organization part II

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Systems I. Machine-Level Programming V: Procedures

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

CS 314 Principles of Programming Languages. Lecture 13

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

Principles of Programming Languages

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

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

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

Syntax Errors; Static Semantics

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Compiler Construction

Run-Time Environments

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

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

The Procedure Abstraction

Special Topics: Programming Languages

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

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

ECE232: Hardware Organization and Design

The role of semantic analysis in a compiler

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

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

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

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Compilers and computer architecture: A realistic compiler to MIPS

CS 314 Principles of Programming Languages

CSCI565 Compiler Design

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Chapter 10 Memory Model for Program Execution. Problem

Assembly Language: Function Calls

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

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

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Project Compiler. CS031 TA Help Session November 28, 2011

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Transcription:

Run-time Environments - 2 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design

Outline of the Lecture n What is run-time support? (in part 1) n Parameter passing methods (in part 1) n Storage allocation n Activation records n Static scope and dynamic scope n Passing functions as parameters n Heap memory management n Garbage Collection Y.N. Srikant 2

Code and Data Area in Memory n Most programming languages distinguish between code and data n Code consists of only machine instructions and normally does not have embedded data Code area normally does not grow or shrink in size as execution proceeds n Unless code is loaded dynamically or code is produced dynamically As in Java dynamic loading of classes or producing classes and instantiating them dynamically through reflection Memory area can be allocated to code statically n We will not consider Java further in this lecture n Data area of a program may grow or shrink in size during execution Y.N. Srikant 3

Static Versus Dynamic Storage Allocation n Static allocation Compiler makes the decision regarding storage allocation by looking only at the program text n Dynamic allocation Storage allocation decisions are made only while the program is running Stack allocation n Names local to a procedure are allocated space on a stack Heap allocation n Used for data that may live even after a procedure call returns n Ex: dynamic data structures such as symbol tables n Requires memory manager with garbage collection Y.N. Srikant 4

Static Data Storage Allocation n Compiler allocates space for all variables (local and global) of all procedures at compile time No stack/heap allocation; no overheads Ex: Fortran IV and Fortran 77 Variable access is fast since addresses are known at compile time No recursion Main program variables Procedure P1 variables Procedure P2 variables Procedure P4 variables Main memory Y.N. Srikant 5

Dynamic Data Storage Allocation n Compiler allocates space only for global variables at compile time n Space for variables of procedures will be allocated at run-time Stack/heap allocation Ex: C, C++, Java, Fortran 8/9 Variable access is slow (compared to static allocation) since addresses are accessed through the stack/heap pointer Recursion can be implemened Y.N. Srikant 6

Dynamic Stack Storage Allocation Stack of activation records Main R Calling sequence: Main à R à Q à R Q Base Next R Currently active procedure Y.N. Srikant 7

Activation Record Structure Return address Static and Dynamic links (also called Access and Control link resp.) (Address of) function result Actual parameters Local variables Temporaries Note: The position of the fields of the act. record as shown are only notional. Implementations can choose different orders; e.g., function result could be after local var. Saved machine status Space for local arrays Y.N. Srikant 8

Variable Storage Offset Computation n The compiler should compute the offsets at which variables and constants will be stored in the activation record (AR) n These offsets will be with respect to the pointer pointing to the beginning of the AR n Variables are usually stored in the AR in the declaration order n Offsets can be easily computed while performing semantic analysis of declarations Y.N. Srikant 9

Overlapped Variable Storage for Blocks in C Overlapped storage int example(int p1, int p2) B1 { a,b,c; /* sizes - 10,10,10; offsets 0,10,20 */... B2 { d,e,f; /* sizes - 100, 180, 40; offsets 30, 130, 310 */...} B3 { g,h,i; /* sizes - 20,20,10; offsets 30, 50, 70 */... B4 { j,k,l; /* sizes - 70, 150, 20; offsets 80, 150, 300 */... } B5 { m,n,p; /* sizes - 20, 50, 30; offsets 80, 100, 150 */... } } } Storage required = B1+max(B2,(B3+max(B4,B5))) = 30+max(320,(50+max(240,100))) = 30+max(320, (50+240)) = 30+max(320,290) = 350 Overlapped storage Y.N. Srikant 10

Overlapped Variable Storage for Blocks in C (Ex.) 0 B1=30 B1=30 B3=50 B1=30 B3=50 Storage required = B1+max(B2,(B3+max(B4,B5))) = 30+max(320,(50+max(240,100))) = 30+max(320, (50+240)) = 30+max(320,290) = 350 B2=320 B4=240 B5=100 350 Y.N. Srikant 11

Allocation of Activation Records (nested procedures) program ; procedure P; procedure Q; procedure R; begin Q; end begin P; end SL chain Base Next Activation records are created at procedure entry time and destroyed at procedure exit time DL chain -> P -> R -> Q -> R Y.N. Srikant 12

Allocation of Activation Records (contd.) program ; procedure P; procedure Q; procedure R; begin Q; end begin P; end SL chain Base Next P DL chain -> P -> R -> Q -> R Y.N. Srikant 13

Allocation of Activation Records (contd.) program ; procedure P; procedure Q; procedure R; begin Q; end begin P; end Base Next SL chain P R DL chain -> P -> R -> Q -> R Y.N. Srikant 14

Allocation of Activation Records (contd.) program ; procedure P; procedure Q; procedure R; begin Q; end begin P; end Base SL chain Next P R Q DL chain -> P -> R -> Q -> R Y.N. Srikant 15

Allocation of Activation Records (contd.) 1 program ; 2 procedure P; 3 procedure Q; 3 procedure R; begin Q; end begin P; end 1 -> P 2 -> R 3 -> Q 3 -> R 3 Base SL chain Next P R Q R DL chain Y.N. Srikant 16

Allocation of Activation Records (contd.) Skip L 1 -L 2 +1 records starting from the caller s AR and establish the static link to the AR reached SL chain P DL chain L 1 caller, L 2 Callee 1 -> P 2 -> R 3 -> Q 3 -> R 3 Ex: Consider P 2 -> R 3 R 2-3+1=0; hence the SL of R points to P Consider R 3 -> Q 3 Q 3-3+1=1; hence skipping one link starting from R, we get P; SL of Q points to P Base Next R Y.N. Srikant 17

Allocation of Activation Records (contd.) program ; procedure P; procedure Q; procedure R; begin Q; end begin P; end Base SL chain Next P R Q DL chain -> P -> R -> Q <- R Return from R Y.N. Srikant 18

Allocation of Activation Records (contd.) program ; procedure P; procedure Q; procedure R; begin Q; end begin P; end Base Next SL chain P R DL chain -> P -> R <- Q Return from Q Y.N. Srikant 19

Allocation of Activation Records (contd.) program ; procedure P; procedure Q; procedure R; begin Q; end begin P; end SL chain Base Next P DL chain -> P <- R Return from R Y.N. Srikant 20

Allocation of Activation Records (contd.) program ; procedure P; procedure Q; procedure R; begin Q; end begin P; end SL chain Base Next DL chain <- P Return from P Y.N. Srikant 21

Display Stack of Activation Records 1 program ; 2 procedure P; 3 procedure Q; 3 procedure R; begin Q; end begin P; end Pop L 1 -L 2 +1 records off the display of the caller and push the pointer to AR of callee (L 1 caller, L 2 Callee) Q P call return P R P The popped pointers are stored in the AR of the caller and restored to the DISPLAY after the callee returns R P Y.N. Srikant 22

Static Scope and Dynamic Scope n Static Scope A global identifier refers to the identifier with that name that is declared in the closest enclosing scope of the program text Uses the static (unchanging) relationship between blocks in the program text n Dynamic Scope A global identifier refers to the identifier associated with the most recent activation record Uses the actual sequence of calls that are executed in the dynamic (changing) execution of the program n Both are identical as far as local variables are concerned Y.N. Srikant 23

Static Scope and Dynamic Scope : An Example int x = 1, y = 0; int g(int z) { return x+z;} int f(int y) { int x; x = y+1; return g(y*x); } y = f(3); -------------------------------- After the call to g, Static scope: x = 1 Dynamic scope: x = 4 x 1 y 0 y 3 x 4 z 12 Stack of activation records after the call to g outer block f(3) g(12) Y.N. Srikant 24

Static Scope and Dynamic Scope: Another Example float r = 0.25; void show() { printf( %f,r); } void small() { float r = 0.125; show(); } int main (){ show(); small(); printf( \n ); show(); small(); printf( \n ); } n Under static scoping, the output is 0.25 0.25 0.25 0.25 n Under dynamic scoping, the output is 0.25 0.125 0.25 0.125 Y.N. Srikant 25