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

Slides:



Advertisements
Similar presentations
Code Generation.
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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)
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
Run-Time Storage Organization
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Week 8-9b spring 2002CSCI337 Organization of Prog. Lang0 A Brief Recap Subprogram benefits, basic characteristics from names to their values  names can.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Presented by Dr Ioanna Dionysiou
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
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.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
CPSC 388 – Compiler Design and Construction Runtime Environments.
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
ISBN Chapter 10 Implementing Subprograms.
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.
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
CSC 8505 Compiler Construction Runtime Environments.
Lecture 19: Control Abstraction (Section )
7. Runtime Environments Zhang Zhizheng
Parameter Passing Mechanisms CS308 Compiler Theory.
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
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Lecture 9: Procedures & Functions CS 540 George Mason University.
1 Semantic Analysis  Semantic analysis includes  Dynamic Checking (Those checks for which to perform, compiler doesn’t have sufficient information) 
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Run-Time Environments Chapter 7
Implementing Subprograms
Subject Name Compiler Design Subject Code: 10CS63
Run-Time Storage Organization
Run-Time Storage Organization
Run-Time Environments
Run-Time Environments
Semantic Analysis Semantic analysis includes
UNIT V Run Time Environments.
Runtime Environments What is in the memory?.
Presentation transcript:

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

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 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 s q(1,9) q(1,3) p(1,3) q(1,0)q(2,3) q(2,1)p(2,3)q(3,3) p(1,9) r q(5,9) p(5,9) q(5,5)q(7,9) q(7,7)p(7,9)q(9,9)

5 Control Stack s q(1,9) q(1,3) p(1,3) q(1,0)q(2,3) p(1,9) r 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) … Control stack: Activation tree: s q(1,9) q(1,3) q(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; begin x := 1; … end; begin y := x(0.0); … end. Variable x locally declared in p Function x

7 Mapping Names to Values name storagevalue environmentstate var i; … i := 0; … i := i + 1;

8 Static and Dynamic Notions of Bindings Static NotionDynamic Notion Definition of a procedure Activations of the procedure Declaration of a nameBindings of the name Scope of a declarationLifetime of a binding

9 Stack Allocation Activation records (subroutine frames) on the run- time 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, access 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

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

11 Control Links fp sp Control link Stack grows Callee’s activation record Caller’s activation record

12 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.

13 Access Links s a x q(1,9) access k v s a x q(1,9) access k v q(1,3) access k v s a x q(1,9) access k v q(1,3) access k v p(1,3) access i j s a x q(1,9) access k v q(1,3) access k v p(1,3) access i j e(1,3) access

14 Accessing Nonlocal Data To implement access to nonlocal data a in procedure p, the compiler generates code to traverse n p - n a access 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

15 Parameter Passing 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 (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