Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Languages and Compilers (SProg og Oversættere) Code Generation.
1 Compiler Construction Intermediate Code Generation.
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
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)
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
1 Languages and Compilers (SProg og Oversættere) Lecture 10 Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
CS 536 Spring Run-time organization Lecture 19.
ISBN Chapter 10 Implementing Subprograms.
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
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Semantics of Calls and Returns
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Compilation (Chapter 3) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
Chapter 8 - Control II: Procedures and Environments
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
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.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
7. Runtime Environments Zhang Zhizheng
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Code Generation (Chapter 7) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
ISBN Chapter 10 Implementing Subprograms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Run-Time Environments Chapter 7
Implementing Subprograms
Run-Time Storage Organization
UNIT V Run Time Environments.
Course Overview PART I: overview material PART II: inside a compiler
Run-time environments
Course Overview PART I: overview material PART II: inside a compiler
Presentation transcript:

Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion 8Interpretation 9Review

Runtime Organization (Chapter 6) 2 Storage Allocation Data Representation: how to represent values of the source language on the target machine Expression Evaluation: How to organize computing the values of expressions (taking care of intermediate results) Stack Storage Allocation: How to organize storage for variables (considering lifetimes of global and local variables) Routines: How to implement procedures, functions (and how to pass their parameters and return values) Heap Storage Allocation: How to organize storage for variables (considering lifetimes of heap variables) Object Orientation: Runtime organization for OO languages (how to handle classes, objects, methods)

Runtime Organization (Chapter 6) 3 Static Storage Allocation Example: Global variables in Triangle let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date; in... let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date; in... Exist as long as program is running Compiler can: compute exactly how much memory is needed for all globals. allocate memory at a fixed position for each global variable.

Runtime Organization (Chapter 6) 4 Static Storage Allocation address[a] = 0 address[b] = 3 address[c] = 4 address[t] = 5 a[0] a[1] a[2] a b c t.y t.m t.d t let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date; let type Date = record y: Integer, m:Integer, d:Integer end; //Date var a: array 3 of Integer; var b: Boolean; var c: Char; var t: Date; Example: Global variables in Triangle

Runtime Organization (Chapter 6) 5 Stack Storage Allocation let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y( ) ~ let var d: Integer; var e:... in... ; proc Z( ) ~ let var f: Integer; in begin...; Y( );... end in begin...; Y( );...; Z( ); end let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y( ) ~ let var d: Integer; var e:... in... ; proc Z( ) ~ let var f: Integer; in begin...; Y( );... end in begin...; Y( );...; Z( ); end Example: When do the variables in this program “exist” as long as the program is running when procedure Y is active when procedure Z is active Now we will look at allocation of local variables

Runtime Organization (Chapter 6) 6 Stack Storage Allocation Start of programEnd of program time call depth global Y Z 1 2 Y Z 1. Procedure activation behaves like a stack (LIFO). 2. The local variables “live” only as long as the procedure in which they are declared. Therefore => Allocation of locals on the “call stack” is a good model. A “picture” of our program running:

Runtime Organization (Chapter 6) 7 Stack Storage Allocation: Accessing locals/globals First time around, we assume that in a procedure only local variables declared in that procedure and global variables are accessible. (Essentially, a “flat” block structure.) Later we will extend this idea to include nested scopes and parameters. A stack allocation model (under the above assumption): Globals are allocated at the base of the stack. Stack contains “frames”. Each frame corresponds to a currently active procedure. (often called an “activation frame”) When a procedure is called (activated) a frame is pushed onto the stack When a procedure returns, its frame is popped off the stack

Runtime Organization (Chapter 6) 8 Stack Storage Allocation: Accessing locals/globals SB LB ST call frame SB = Stack base LB = Locals base ST = Stack top call frame Dynamic link globals

Runtime Organization (Chapter 6) 9 What’s in a Frame? Each frame contains Dynamic link: points to next frame on the stack (the frame of the caller) Return address Local variables for the current activation return address locals Link data Local data LB ST dynamic link

Runtime Organization (Chapter 6) 10 What Happens when a Procedure is called? LB ST SB = Stack base LB = Locals base ST = Stack top call frame call frame When procedure f( ) is called push new f( ) call frame on top of stack. Make dynamic link in new frame point to old LB Update LB (becomes old ST ) new call frame for f( )

Runtime Organization (Chapter 6) 11 What Happens when the Procedure returns? LB ST When procedure f( ) returns Update LB (from dynamic link) Update ST (to old LB ) current call frame for f( ) current call frame for f( ) call frame Note: updating the ST implicitly “destroys” or “pops” the frame.

Runtime Organization (Chapter 6) 12 Accessing global/local variables Q: Is the stack frame for a particular procedure always at the same position in the stack? A: No, look at picture of procedure activation below. Imagine what the stack looks like at each point in time. time call depth global Y Z 1 2 Y Z G Y G Z Y

Runtime Organization (Chapter 6) 13 Accessing global/local variables The global frame is always at the same place in the stack. => Address global variables relative to SB A typical instruction to access a global variable: LOAD 4[SB] Frames are not always at the same position in the stack. Depends on the number of frames already on the stack. => Address local variables relative to LB A typical instruction to access a local variable: LOAD 3[LB] Recall: We are still working under the assumption of a “flat” block structure How do we access global and local variables on the stack?

Runtime Organization (Chapter 6) 14 Accessing global/local variables Example: Compute the addresses of the variables in this program let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y( ) ~ let var d: Integer; var e:... in... ; proc Z( ) ~ let var f: Integer; in begin...; Y( );... end in begin...; Y( );...; Z( ); end let var a: array 3 of Integer; var b: Boolean; var c: Char; proc Y( ) ~ let var d: Integer; var e:... in... ; proc Z( ) ~ let var f: Integer; in begin...; Y( );... end in begin...; Y( );...; Z( ); end Var Size Address abcdefabcdef [0]SB [3]SB [4]SB 1 ? 1 [2]LB [3]LB [2]LB

Runtime Organization (Chapter 6) 15 Accessing non-local variables Recap: We have discussed Stack allocation of locals in the call frames of procedures Some other things stored in frames: A dynamic link: points to the previous frame on the stack => corresponds to the “caller” of the current procedure. A return address: points to the next instruction of the caller. Addressing global variables relative to SB (stack base) Addressing local variables relative to LB (locals base) Next, we will look at accessing non-local variables. Or in other words, we will attempt to answer the question: How does lexical scoping work?

Runtime Organization (Chapter 6) 16 Accessing non-local variables: what is this about? Example: How to access p1, p2 from within Q or S? let var g1: array 3 of Integer; var g2: Boolean; proc P( ) ~ let var p1, p2 proc Q( ) ~ let var q:... in... ; proc S( ) ~ let var s: Integer; in... let var g1: array 3 of Integer; var g2: Boolean; proc P( ) ~ let var p1, p2 proc Q( ) ~ let var q:... in... ; proc S( ) ~ let var s: Integer; in... Scope Structure var g1,g2 proc P( ) var p1,p2 proc Q( ) proc S( ) var q Q: When inside Q, does the dynamic link always point to frame of P? var s

Runtime Organization (Chapter 6) 17 Accessing non-local variables Q: When inside Q, does the dynamic link always point to frame of P? A: No! Consider following scenarios: var g1,g2 proc P( ) var p1,p2 proc Q( ) proc S( ) var q var s time G 1 P S Q G P Q 2 3 P S Q P Q

Runtime Organization (Chapter 6) 18 Accessing non-local variables So, we can not rely on the dynamic link to get to the lexically scoped frame(s) of a procedure. => Another item is added in the link data: the static link. The static link in a frame points to the next lexically scoped frame, which must be somewhere earlier on the stack. Registers L1, L2, etc. are used to point to the lexically scoped frames. (L1 is more local than L2, so L0 would just be the same as LB.) Typical instructions for accessing a non-local variable: LOAD [4]L1 LOAD [3]L2 These and LB and SB are called the display registers

Runtime Organization (Chapter 6) 19 What’s in a Frame (revised)? Each frame contains Static link: points to the frame of the enclosing procedure Dynamic link: points to next frame on the stack (the frame of the caller) Return address Local variables for the current activation static link locals Link data Local data LB ST dynamic link return address

Runtime Organization (Chapter 6) 20 Accessing non-local variables proc P( ) proc Q( )... proc S( )... time G P S Q LB ST P( ) frame globals SB S( ) frame Q( ) frame Dynamic L. Static Link L1

Runtime Organization (Chapter 6) 21 Accessing variables, addressing schemas overview We now have a complete picture of the different kinds of addresses that are used for accessing variables stored on the stack. Type of variable Global Local Non-local, 1 level up Non-local, 2 levels up... Load instruction LOAD offset[SB] LOAD offset[LB] LOAD offset[L1] LOAD offset[L2]