Run-time Environments -Part 1

Similar documents
Run-time Environments - 2

Memory Management and Run-Time Systems

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Run-time Environments - 3

UNIT V Sub u P b ro r g o r g a r m a s

G Programming Languages - Fall 2012

Run-time Environments -Part 3

COMPILER DESIGN - RUN-TIME ENVIRONMENT

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

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

Lecture08: Scope and Lexical Address

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

NOTE: Answer ANY FOUR of the following 6 sections:

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

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

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

procedure definition (or declaration - some make a distinction, we generally won t), e.g., void foo(int x) {... }

Run-Time Environments

Informatica 3 Marcello Restelli

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages. ameter Passing Reference Parameter. Call-by-Value Parameters. 28 : Control Structures Parameters

References and pointers

Programming Languages

Chapter 5 Names, Binding, Type Checking and Scopes

Run Time Environments

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

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

Concepts Introduced in Chapter 7

CSc 520 Principles of Programming Languages

Special Topics: Programming Languages

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Compiler Design Spring 2017

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

Chapter 5. Names, Bindings, and Scopes

9. Subprograms. 9.2 Fundamentals of Subprograms

Control Abstraction. Hwansoo Han

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

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

Short Notes of CS201

Topic 7: Activation Records

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

Chap. 8 :: Subroutines and Control Abstraction

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CS201 - Introduction to Programming Glossary By

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

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

Programming Languages: Lecture 11

Binding and Variables

Run-time Environments

Run-time Environments

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

CSCI312 Principles of Programming Languages!

Chapter 9 :: Subroutines and Control Abstraction

Weeks 6&7: Procedures and Parameter Passing

CS558 Programming Languages

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

Compiler Construction

CS 415 Midterm Exam Spring SOLUTION

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

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

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

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

Programmiersprachen (Programming Languages)

Equivalent Notations. Higher-Order Functions. (define (f x y) ( body )) = (define f (lambda (x y) ) ) Anonymous Functions.

G Programming Languages - Fall 2012

Compiler Construction

Global Register Allocation - Part 3

CSE 504: Compiler Design. Runtime Environments

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

Chapter 9 Subprograms

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

Runtime Environments I. Basilio B. Fraguela

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Implementing Object-Oriented Languages - 2

Compiler Construction

One Ring to rule them all, One Ring to bring them all,

Principles of Programming Languages

Chapter 3:: Names, Scopes, and Bindings

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Compiler Construction

Programming Languages

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

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

Run-Time Environments

Topic IV. Block-structured procedural languages Algol and Pascal. References:

(Refer Slide Time: 1:36)

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Types and Type Inference

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

CS 415 Midterm Exam Spring 2002

Parameter passing. Parameter: A variable in a procedure that represents some other data from the procedure that invoked the given procedure.

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

Programming Languages: Lecture 12

Programming Languages Third Edition. Chapter 7 Basic Semantics

Informatica 3 Syntax and Semantics

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

FORTRAN. FORTRAN Concepts/Contributions

CS 345. Functions. Vitaly Shmatikov. slide 1

Transcription:

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

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

What is Run-time Support? It is not enough if we generate machine code from intermediate code Interfaces between the program and computer system resources are needed There is a need to manage memory when a program is running This memory management must connect to the data objects of programs Programs request for memory blocks and release memory blocks Passing parameters to fucntions needs attention Other resources such as printers, file systems, etc., also need to be accessed These are the main tasks of run-time support In this lecture, we focus on memory management Y.N. Srikant 3

Parameter Passing Methods - Call-by-value At runtime, prior to the call, the parameter is evaluated, and its actual value is put in a location private to the called procedure Thus, there is no way to change the actual parameters. Found in C and C++ C has only call-by-value method available Passing pointers does not constitute call-by-reference Pointers are also copied to another location Hence in C, there is no way to write a function to insert a node at the front of a linked list (just after the header) without using pointers to pointers Y.N. Srikant 4

Problem with Call-by-Value p null copy of p, a parameter passed to function f q node inserted by the function f node insertion as desired p null Y.N. Srikant 5

Parameter Passing Methods - Call-by-Reference At runtime, prior to the call, the parameter is evaluated and put in a temporary location, if it is not a variable The address of the variable (or the temporary) is passed to the called procedure Thus, the actual parameter may get changed due to changes to the parameter in the called procedure Found in C++ and Java Y.N. Srikant 6

Call-by-Value-Result Call-by-value-result is a hybrid of Call-by-value and Call-byreference Actual parameter is calculated by the calling procedure and is copied to a local location of the called procedure Actual parameter s value is not affected during execution of the called procedure At return, the value of the formal parameter is copied to the actual parameter, if the actual parameter is a variable Becomes different from call-by-reference method when global variables are passed as parameters to the called procedure and the same global variables are also updated in another procedure invoked by the called procedure Found in Ada Y.N. Srikant 7

Difference between Call-by-Value, Call-by- Reference, and Call-by-Value-Result program RTST; var a: integer; procedure Q; begin a:= a+1; end procedure R(x:integer); begin x:= x+10; Q; end begin a:= 1; R(a); print(a); end 2 12 11 Value of a printed call-byvalue call-byreference call-byvalue-result Note: In Call-by-V-R, value of x is copied into a, when proc R returns. Hence a=11. Y.N. Srikant 8

Parameter Passing Methods -Call-by-Name Use of a call-by-name parameter implies a textual substitution of the formal parameter name by the actual parameter For example, if the procedure procedure R (X,I : integer); begin I := 2; X := 5; I := 3; X := 1; end; is called by R(B[J*2], J) this would result in (effectively) changing the body to begin J :=2; B[J*2] := 5; J :=5; B[J*2] := 1; end; just before executing it Y.N. Srikant 9

Parameter Passing Methods - Call by Name Note that the actual parameter corresponding to X changes whenever J changes Hence, we cannot evaluate the address of the actual parameter just once and use it It must be recomputed every time we reference the formal parameter within the procedure A separate routine ( called thunk) is used to evaluate the parameters whenever they are used Found in Algol and functional languages Y.N. Srikant 10

Example of Using the Four Parameter Passing Methods 1. procedure swap (x, y : integer); 2. var temp : integer; 3. begin 4. temp := x; 5. x := y; 6. y := temp; 7. end (*swap*); 8.... 9. i := 1; 10. a[i]:=10; (* a: array[1..5] of integer *) 11. print(i,a[i]); 12. swap(i,a[i]); 13. print(i,a[1]); Results from the 4 parameter passing methods (print statements) 1 10 1 10 1 10 10 1 1 10 10 1 call-byvalue call-byreference call-byval-result call-byname 1 10 error! Reason for the error in the Call-by-name Example The problem is in the swap routine temp := i; (* => temp:=1 *) i := a[i]; (* => i:=10 since a[i]=10 *) a[i] := temp; (* => a[10]:=1 => index out of bounds *) Y.N. Srikant 11

Code and Data Area in Memory Most programming languages distinguish between code and data 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 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 We will not consider Java further in this lecture Data area of a program may grow or shrink in size during execution Y.N. Srikant 12

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

Static Data Storage Allocation 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 14

Dynamic Data Storage Allocation Compiler allocates space only for golbal variables at compile time 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 15

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 at the top of the act. record. Saved machine status Space for local arrays Y.N. Srikant 16