Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

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) ICS 313: Programming Language Theory 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)
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
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Run-time Environment and Program Organization
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.
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.
Presented by Dr Ioanna Dionysiou
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
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,
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
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.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
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.
Activation Records (in Tiger) CS 471 October 24, 2007.
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.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
CSC 8505 Compiler Construction Runtime Environments.
7. Runtime Environments Zhang Zhizheng
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
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
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Lecture 9: Procedures & Functions CS 540 George Mason University.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
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.
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.
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Run-Time Storage Organization
Run-Time Storage Organization
Functions and Procedures
Run-Time Environments
Implementing Subprograms
Run-Time Environments
UNIT V Run Time Environments.
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Run-time environments
Presentation transcript:

Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects »data that are used when executing a certain procedure. »Dynamically allocated objects (malloc, free). char abc[1000]; char *foo() { char buf[50], *c; buf[0] = ‘\0’; c=malloc(50); return( c );} main() {char *c; c = foo();}

–Typical organization of run-time memory Code Static Data (global variables) stack (memory for procedures) heap (memory for dynamically allocated data)

Activation Records: also called frames Information(memory) needed by a single execution of a procedure A general activation record: Return value actual parameters optional control link optional access link machine status local variables temporaries

–Storage Allocation Strategies static allocation lays out storage for all data objects at compile time. –Restrictions: »size of object must be known and alignment requirements must be known at compile time. »No recursion. »No dynamic data structure Stack allocation manages the run time storage as a stack –The activation record is pushed on as a function is entered. –The activation record is popped off as a function exits. –Restrictions: »values of locals cannot be retained when an activation ends. »A called activation cannot outlive a caller.

Heap allocation -- allocates and deallocates stroage as needed at runtime from a data area known as heap. –Most flexible: no longer requires the activation of procedures to be LIFO. –Most inefficient: need true dynamic memory management. Note: static allocation too restricted, heap allocation too inefficient. Most current compiler/language/processor uses the stack allocation scheme.

–Example of stack allocation: Program sort var procedure readarray; …. function partition(…) …. procedure quicksort(…) …… partition quicksort …. Begin …… readarray quicksort end Main readarray quicksort(1, 9) partition(1, 9) quicksort(1, 3) partition(1, 3) quicksort(1, 0)

How would this happen (push and pop the activation record)? –Everything must be done by the compiler. –What makes this happen is known as calling sequence (how to implement a procedure call). A calling sequence allocates an activation record and enters information into its fields (push the activation record). –On the opposite of the calling sequence is the return sequence. Return sequence restores the state of the machine so that the calling procedure can continue execution.

A possible calling sequence: –The caller evaluates actuals and push the actuals on the stack –The caller saves return address(pc) the old value of sp into the stack –The caller increments the sp –The callee saves registers and other status information –The callee initializes its local variables and begin execution. A possible return sequence: –The callee places a return value next to the activation record of the caller. –The callee restores other registers and sp and return (jump to pc). –The caller copies the return value to its activation record. In today’s processors, there is usually special support for efficiently realizing calling/return sequence: executing procedures is too important!!

Access to nonlocal variables. –Nonlocal variables in C (without nested procedures): Still have nested scopes (blocks). Solution: –All data declared outside procedures are static. –Other names must be at the activation record at the top of the stack, can be accessed from sp. »Treat a block as a parameter-less procedure »Allocates space for all blocks in a procedure. Example: Fig in page 413.

Access to nonlocal variables. –Nonlocal variables in PASCAL (with nested procedures): the scheme for C will break.

Access to nonlocal variables. –Nonlocal variables in PASCAL (with nested procedures): The scheme for C will break (static for all non- locals). Access links –If p is nested immediately within q in the source text, then the access link in an activation record for p points to the access link in the record for the most recent activation of q. –A procedure p at nesting depth n_p accesses a nonlocal a at nesting depth n_a: (1) following n_p – n_a links and (2) using the relative offset in the activation record.

Display: An alternative to access link ( a faster method to access nonlocals ). Using an array d of pointers to activation records, the array is called a display. –Referencing nonlocal variables always requires only two memory references. Suppose control is in a procedure p at nesting depth j, then the first j-1 elements of the display point to the most recent activation of the procedures that lexically enclose procedure p, and d[j] points to the activation of p.

Setting up the display: When a new activation record for a procedure at nesting depth k: –save the value of d[k] in the new activation record –set d[k] to point to the new activation record.

–Parameter passing The method to associate actual parameters with formal parameters. The parameter passing method will effect the code generated. Call-by-value: –The actual parameters are evaluated and their r-values are passed to the called procedure. –Implementation: »a formal parameter is treated like a local name, so the storage for the formals is in the activation record of the called procedure. »The caller evaluates the actual parameters and places their r-values in the storage for the formals.

–Call-by-reference: also called call-by address or call-by-location. The caller passes to the called procedure a pointer to the storage address of each actual parameter. –Actuall parameter must have an address -- only variables make sense, an expression will not (location of the temporary that holds the result of the expression will be passed). –Copy-restore: A hybrid between call-by-value and call-by- reference. –The actual parameters are evaluated and its r-values are passed to the called procedure as in call-by-value. –When the control returns, the r-value of the formal parameters are copied back into the l-value of the actuals.