Run-Time Environments

Similar documents
Run-Time Environments

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

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

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

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

CA Compiler Construction

Intermediate Representations & Symbol Tables

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

System Software Assignment 1 Runtime Support for Procedures

Concepts Introduced in Chapter 7

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

Chapter 10. Implementing Subprograms ISBN

Implementing Subprograms

G Programming Languages - Fall 2012

Chapter 10. Implementing Subprograms

Procedures and Run-Time Storage

Procedures and Run-Time Storage

Functions and Procedures

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

Runtime Environments I. Basilio B. Fraguela

Implementing Subroutines. Outline [1]

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

Run Time Environments

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

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

THEORY OF COMPILATION

Run-Time Environments

CIT Week13 Lecture

LECTURE 19. Subroutines and Parameter Passing

Memory Management and Run-Time Systems

Run-time Environment

Special Topics: Programming Languages

Programming Languages: Lecture 12

Chapter 10 Implementing Subprograms

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

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

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

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

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Principles of Programming Languages

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

CS 314 Principles of Programming Languages

COP4020 Fall 2006 Final Exam

Topic 7: Activation Records

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

CS 314 Principles of Programming Languages. Lecture 13

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

Lecture 7: Binding Time and Storage

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Run-time Environments - 2

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Weeks 6&7: Procedures and Parameter Passing

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

G Programming Languages - Fall 2012

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

Programming Languages

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Programming Languages

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

Informatica 3 Marcello Restelli

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

MIPS Functions and the Runtime Stack

Chap. 8 :: Subroutines and Control Abstraction

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

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

Implementing Subprograms

ECE260: Fundamentals of Computer Engineering

Run-time Environments -Part 1

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

Run Time Environment

CS 314 Principles of Programming Languages. Lecture 11

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

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

Syntax-Directed Translation Part I

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

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

CSCI312 Principles of Programming Languages!

7.3.3 A Language With Nested Procedure Declarations

MIPS Procedure Calls. Lecture 6 CS301

Chapter 3:: Names, Scopes, and Bindings

Chapter 9 :: Subroutines and Control Abstraction

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

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

COMPILER DESIGN - RUN-TIME ENVIRONMENT

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Course Administration

Lecture08: Scope and Lexical Address

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

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

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

Procedure Call. Procedure Call CS 217. Involves following actions

Run-Time Data Structures

The role of semantic analysis in a compiler

Compilers and Code Optimization EDOARDO FUSELLA

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

PART 6 - RUN-TIME ENVIRONMENT. F. Wotawa TU Graz) Compiler Construction Summer term / 309

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

Transcription:

1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011

2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the sequence of steps between the first and last steps in the execution of the procedure body A procedure is recursive if a new activation can begin before an earlier activation of the same procedure has ended

3 Procedure Activations: Example program sort(input, output) var a : array [0..10] of integer; procedure readarray; var i : integer; begin for i := 1 to 9 do read(a[i]) end; function partition(y, z : integer) : integer var i, j, x, v : integer; begin end procedure quicksort(m, n : integer); var i : integer; begin if (n > m) then begin i := partition(m, n); quicksort(m, i - 1); quicksort(i + 1, n) end end; begin a[0] := -9999; a[10] := 9999; readarray; quicksort(1, 9) end. Activations: begin sort enter readarray leave readarray enter quicksort(1,9) enter partition(1,9) leave partition(1,9) enter quicksort(1,3) leave quicksort(1,3) enter quicksort(5,9) leave quicksort(5,9) leave quicksort(1,9) end sort.

4 Activation Trees: Example s r p(1,9) q(1,9) q(1,3) q(5,9) p(1,3) q(1,0) q(2,3) p(5,9) q(5,5) q(7,9) p(2,3) q(2,1) q(3,3) p(7,9) q(7,7) q(9,9) Activation tree for the sort program Note: also referred to as the dynamic call graph

5 Control Stack Activation tree: s r q(1,9) p(1,9) q(1,3) p(1,3) q(1,0) q(2,3) Control stack: s q(1,9) q(1,3) q(2,3) Activations: begin sort enter readarray leave readarray enter quicksort(1,9) enter partition(1,9) leave partition(1,9) enter quicksort(1,3) enter partition(1,3) leave partition(1,3) enter quicksort(1,0) leave quicksort(1,0) enter quicksort(2,3)

6 Scope Rules Environment determines name-to-object bindings: which objects are in scope? program prg; var y : real; function x(a : real) : real; begin end; procedure p; var x : integer; Variable x locally declared in p A function x begin x := 1; end; begin y := x(0.0); end.

7 Mapping Names to Values environment state name storage value var i; i := 0; i := i + 1;

8 Mapping Names to Values At compile time At run time environment state name storage value var i; i := 0; i := i + 1;

9 Static and Dynamic Notions of Bindings Static Notion Definition of a procedure Declaration of a name Scope of a declaration Dynamic Notion Activations of the procedure Bindings of the name Lifetime of a binding

10 Stack Allocation Activation records (subroutine frames) on the runtime stack hold the state of a subroutine Calling sequences are code statements to create activations records on the stack and enter data in them Caller s calling sequence enters actual arguments, control link, link, and saved machine state Callee s calling sequence initializes local data Callee s return sequence enters return value Caller s return sequence removes activation record

11 Activation Records (Subroutine Frames) fp (frame pointer) Returned value Actual parameters Optional control link Optional link Save machine status Local data Temporaries Caller s responsibility to initialize Callee s responsibility to initialize

12 Control Links The control link is the old value of the fp fp sp Control link Stack growth Caller s activation record Callee s activation record

13 Scope with Nested Procedures program sort(input, output) var a : array [0..10] of integer; x : integer; procedure readarray; var i : integer; begin end; procedure exchange(i, j : integer); begin x := a[i]; a[i] := a[j]; a[j] := x end; procedure quicksort(m, n : integer); var k, v : integer; function partition(y, z : integer) : integer var i, j : integer; begin exchange(i, j) end begin if (n > m) then begin i := partition(m, n); quicksort(m, i - 1); quicksort(i + 1, n) end end; begin quicksort(1, 9) end.

14 Access Links (Static Links) s s s s a x q(1,9) k v a x q(1,9) k v q(1,3) k v The link points to the activation record of the static parent procedure: s is parent of r, e, and q q is parent of p a x q(1,9) k v q(1,3) k v p(1,3) i j a x q(1,9) k v q(1,3) k v p(1,3) i j e(1,3)

15 Accessing Nonlocal Data To implement to nonlocal data a in procedure p, the compiler generates code to traverse n p - n a links to reach the activation record where a resides n p is the nesting depth of procedure p n a is the nesting depth of the procedure containing a

16 Parameter Passing Modes Call-by-value: evaluate actual parameters and enter r-values in activation record Call-by-reference: enter pointer to the storage of the actual parameter Copy-restore (aka value-result): evaluate actual parameters and enter r-values, after the call copy r-values of formal parameters into actuals Call-by-name: use a form of in-line code expansion (thunk) to evaluate parameters