RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
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)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
CS 536 Spring Run-time organization Lecture 19.
Runtime Environments Source language issues Storage organization
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Run-Time Storage Organization
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
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 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.
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Chapter 8 :: Subroutines and Control Abstraction
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.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
CS 2104 Prog. Lang. Concepts Subprograms
Runtime Environments Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Basic Semantics Associating meaning with language entities.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Programming Languages and Paradigms Imperative Programming.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
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.
COMP3190: Principle of Programming Languages
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
CSC 8505 Compiler Construction Runtime Environments.
7. Runtime Environments Zhang Zhizheng
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
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?
Storage Allocation Mechanisms
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Run-Time Environments Chapter 7
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Run-Time Storage Organization
Run-Time Storage Organization
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
UNIT V Run Time Environments.
Course Overview PART I: overview material PART II: inside a compiler
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Run-time environments
Presentation transcript:

RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions, storage, and system software) in order to implement the source language. This is called run-time organization

The key issues in run-time organization l Data representation: How should we represent the values of each source-language type in the target machine? Expression evaluation: How should we organize the evaluation of expressions, taking care of intermediate results? l Storage allocation: How should we organize storage for variables, taking into account the different lifetimes of global, local and heap variables? l Routines: How should we implement procedures, functions, and parameters, in terms of low-level routines? Run-time organization for object-oriented languages: How should we represent objects and methods?

Storage classes global: exist throughout lifetime of entire program and can be referenced anywhere. static: exist throughout lifetime of entire program but can only be referenced in the function (or module) in which they are declared. local: (also called automatic) exist only for the duration of a call to the routine in which they are declared; a new variable is created each time the routine is entered (and destroyed on exit). dynamic: variables that are created during program execution; usually represented by an address held in a variable of one of the above classes.

Both global and static variables have a single instance that persists throughout life of program and the usual implementation for these is a collection of memory locations in the global/static data segment of the executable. These locations are fixed at the end of the compilation process. Local variables only come into existence on entry to a routine and persist until its exit. To handle these we use a runtime stack that holds the values of locals. The area of memory used to hold all the locals of a routine is called the stack frame(active record). The stack frame for the routine currently executing will be on top of the stack. Dynamic allocation of further storage during the run of a program is done by calling library functions (e.g., malloc()). This storage is obtained from memory in a different segment than the program code, global/static, or stack. Such memory is called the heap.

Here’s a map depicting the address space of an executing program:

In a stack frame (activation record) we hold the following information : 1) frame pointer: pointer value of the previous stack frame so we can reset the top of stack when we exit this function. This is also sometimes called the dynamic link. 2) static link: in languages (like Pascal but not C or Decaf) that allow nested function declarations, a function may be able to access the variables of the function(s) within which it is declared. In the static link, we hold the pointer value of the stack frame in which the current function was declared. 3) return address: point in the code to which we return at the end of execution of the current function. 4) values of arguments passed to the function and locals and temporaries used in the function.

Here is what typically happens when we call a function Before a function call, the calling routine: 1) saves any necessary registers 2) pushes the arguments onto the stack for the target call 3) set up the static link (if appropriate) 4) pushes the return address onto the stack 5) jumps to the target After a function call, the calling routine: 1) removes return address and parameters from the stack 2) restores any saved registers 3) continues executing During a function call, the target routine: 1) saves any necessary registers 2) sets up the new frame pointer 3) makes space for any local variables 4) does its work 5) tears down frame pointer and static link 7) restores any saved registers 8) jumps to saved return address

Parameter passing Pass by value: This is the mechanism supported by C. Value of parameters are copied into called routine. Pass by reference: No copying is done, but a reference (usually implemented as a pointer) is given to the value. In addition to allowing the called routine to change the values, it is also efficient means for passing large variables (such as structs). Pass by value-result: This interesting variant supported by languages such as Ada copies the value of the parameter into the routine, and then copies the (potentially changed) value back out. This has an effect similar to pass-by-reference, but not exactly. Pass by name: This rather unusual mechanism acts somewhat like C preprocessor macros and was introduced in Algol. Rather than evaluating the parameter value, the name or expression is actually substituted into the calling sequence and each access to the parameter in the calling body re-evaulates it.

Dynamic arrays A dynamic array is an array whose index bounds are not known until run-time. Dynamic arrays are found in Algol and Ada. In such languages, different dynamic arrays of the same type may have different index bounds, and therefore different numbers of elements. How then can we make dynamic arrays satisfy the constant-size requirement? We are forced to adopt an indirect representation, in which the dynamic array’s handle (also called an array descriptor or array information vector) contains not only a pointer to the array’s elements but also the array’s index bounds. The handle has a constant size.

Run-time organization for object-oriented languages Object-oriented (OO) languages give rise to interesting and special problems in run-time organization. An object is a special kind of record. Attached to each object are some methods, each method being a kind of procedure or function that is able to operate on that object. Objects are grouped into classes, such that all objects of the same class have identical structure and identical methods.

Although an object is somewhat similar to a record, the representation of an object must reflect the close association between the object and its instance methods. From an object we must be able to locate the attached instance methods. In turn, each instance method must somehow ‘know’ which object it is attached to.

Java object representation (single class) Class Point { // A Point object represents a geometric point located at (x,y). protected int x, y; (1) public Point (int x, int y) { this.x = x; this.y=y; } (2) public void move (int dx, int dy) {. This.x += dx; this.y += dy; } (3) public float area ( ) { return 0.0; } (4) public float dist (Point that ) {. int dx = this.x – that.x; int dy = this.y – that.y; return Mat.sqrt (dx*dy + dy*dy); }

Associated with class Point is a unique class-object that looks like this: Point Move Area dist Constructor(1) Method(2) Method(3) Method(4) An object of class Point looks like this: Class point class-object x y

Point p =new Point (2, 3); Point q = new Point (0, 0); Point class-object p q class x y x y