CSC 8505 Compiler Construction Runtime Environments.

Slides:



Advertisements
Similar presentations
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Advertisements

The University of Adelaide, School of Computer Science
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
(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)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
ISBN Chapter 10 Implementing Subprograms.
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
Semantics of Calls and Returns
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
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.
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.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
COMPILER CONSTRUCTION Principles and Practice Kenneth C. Louden.
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.
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.
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.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
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 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 )
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Lecture 19: Control Abstraction (Section )
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.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
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.
Storage Allocation Mechanisms
Lecture 7 Macro Review Stack Frames
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
Implementing Subprograms Chapter 10
Introduction to Compilers Tim Teitelbaum
Run-Time Environments
Chapter 9 :: Subroutines and Control Abstraction
Stack Frame Linkage.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
Run-Time Environments
UNIT V Run Time Environments.
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Presentation transcript:

CSC 8505 Compiler Construction Runtime Environments

2 Outline Memory organization during program execution Static runtime environments Stack-based runtime environments –Without local procedure –With local procedure Parameter passing

Runtime Environments3 Memory organization during program execution Main memory –Store large amount of data –Slower access Registers –Very small amount of data –Faster access Code area Global/static area stack Free space Heap Main memory registers Data area

Runtime Environments4 Code Area Addresses in code area are static (i.e. no change during execution) for most programming language. Addresses are known at compile time. proc 1 proc 2 proc n entry point

Runtime Environments5 Data Area Addresses in data area are static for some data and dynamic for others. –Static data are located in static area. –Dynamic data are located in stack or heap. Stack (LIFO allocation) for procedure activation record, etc. Heap for user allocated memory, etc.

Runtime Environments6 Registers General-purpose registers –Used for calculation Special purpose registers –Program counter (pc) –Stack pointer (sp) –Frame pointer (fp) –Argument pointer (ap)

Runtime Environments7 Calling Sequence Sequence of operations that must be done for procedure calls –Call sequence Sequence of operations performed during procedure calls –Find the arguments and pass them to the callee. –Save the caller environment, i.e. local variables in activation records, return address. –Create the callee environment, i.e. local variables in activation records, callee’s entry point. –Return sequence Sequence of operations performed when return from procedure calls –Find the arguments and pass them back to the caller. –Free the callee environment. –Restore the caller environment, including PC.

Runtime Environments8 Issues in Call Sequence Which part of the call sequence is included in the caller code? Which is in the callee code? –Save space in the code segment if the call sequence is included in the callee code. –Normally, the caller finds the arguments and provides them to the callee. Which operation is supported in hardware? –The more operations supported in hardware, the lower cost (i.e. execution time and space for code) is.

Runtime Environments9 Static runtime environments Static data –Both local and global variables are allocated once at the beginning and deallocated at program termination –Fixed address No dynamic allocation No recursive call –Procedure calls are allowed, but no recursion. –One activation record for each procedure, allocated statically Example:FORTRAN 77

Runtime Environments10 Memory Organization for Static Runtime Environment PROGRAM TEST COMMON MAX INTEGER MAX REAL TAB(10), TEMP … QMEAN(TAB, 3, TEMP) … END SUBROUTINE QMEAN(A, SIZE, MEAN) COMMON MAX INTEGER MAX, SIZE REAL A(SIZE), MEAN, TEMP INTEGER K … END MAX TAB(1) TAB(2) … TAB(10) TEMP 3 A SIZE MEAN Return addr TEMP K Global area Activation record for main Activation record for QMEAN

Runtime Environments11 Stack-based runtime environments Handle recursive calls Activation records are allocated in stack, called rumtime stack or call stack One procedure can have more than one activation records in the stack at one time Call sequence is more complex than the sequence in static environment

Runtime Environments12 Stack-based Environments Without Local Procedures Maintain pointer to the current activation record –Store frame pointer in an fp register Record the link from an activation record to the previous record –In each activation record, a link from an activation record to the activation record of the caller, called a dynamic link or control link is stored. Sometimes, the area for parameters and local variables need to be identified. A stack pointer is maintained in an sp register. local var.s return addr control link parameters SP FP

Runtime Environments13 Call Sequence in Stack-based Environments Without Local Procedures Calling sequence –Push arguments –Push fp as control link –Copy sp to fp –Store return address –Jump to callee –Reserve space for local variables Return sequence –Copy fp to sp –Load control link into fp –Jump to return address –Change sp to pop arguments arguments activation record of main control link return addr sp fp sp local var.s sp arguments control link return addr sp local var.s sp fp Compute arguments and push into stackStore fp as control linkMove fpPush return addressReserve area for local variables Calling sequenceReturn sequence Load fp into sp (to pop local var.s and return addr) Load control link into fp fp Jump to return addrPop arguments Global area Direction of stack growth

Runtime Environments14 Activation Tree main() {…; g(x); return(0);} void g(int m) {…{… f(y);… g(y); … } void f(int n) {g(n); … } main g(2) f(1) g(1) Global area activation record for main activation record for g(2) activation record for f(1) activation record for g(1) activation record for g(1)

Runtime Environments15 Calculating Address in Stack-Based Environments Address of local variables and parameters are calculated dynamically during execution time Offset of variables and parameters from fp is: –static –known during compile time –Stored in symbol table Address of x =fp+xOffset Address of a =fp+aOffset parameterscontrol linkreturn addresslocal variables fp sp x a aOffset xOffset

Runtime Environments16 Considerations in Address Calculation Sizes of variables depend on data types Addressing elements in array –Number of dimensions –Size of array –Ordering in storage Row-major order Column-major order

Runtime Environments17 Local Temporaries X = (a+b)/((c-d)*f(i)) Values of (a+b) and (c-d) must be stored before f can be called. Thus, area for temporary variables must be reserved in the stack. Usually, temporaries are pushed into stacks after local variables.

Runtime Environments18 Nested Declarations Local variables of blocks can be defined. void f {intx;int i; … {charx;intj; … } … } A block could be treated as a procedure, but it is inefficient. Another simpler method is to: –push local variables in stack when the block is entered –pop the local variables when the block is exited control link of f xjxj return addr of f xixi rest of stack activation record of f local var. of block

Runtime Environments19 Call Sequence Calling sequence –Push arguments –Push access link –Push fp as control link –Copy sp to fp –Store return address –Jump to callee –Reserve space for local variables How to find access link Return sequence –Copy fp to sp –Load control link into fp –Pop access link –Jump to return address –Change sp to pop arguments

Runtime Environments20 Parameter Passing Pass by value Pass by reference Pass by value-result Pass by name

Runtime Environments21 Pass by Value Value parameters are not changed during the execution Only the value is sent into the procedure, and are used locally When the control is returned from the callee, the value of the parameter is not passed back to the caller. voidchange(int x) {x++; return; } voidmain() {int y=0; change(y); printf(“%d\n”,y); return; } Output: 0

Runtime Environments22 Pass by Reference The reference to a variable is passed to the callee. The callee used the reference (address) to refer to the variable. –Indirect addressing is needed to refer to parameters The variable in the caller and the referenced memory in the callee share the same memory location. The value of the variable in the caller is also changed when the referenced in the callee is changed. voidchange (int &x) {x++; return; } void main() {int y=0; change(y); printf(“%d\n”,y); return; } Output: 1

Runtime Environments23 Pass by Value-Result The value of the parameter is copied into the callee when the callee is entered. New memory location is provided for the parameter in the callee’s activation record. –No indirect address is needed When the control is returned to the caller, the value is copied back. voidchange(int x) {x++; return; } voidmain() {int y=0; change(y); printf(“%d\n”,y); return; } Output: 1

Runtime Environments24 Pass by Name The argument is replaced by the textual representation of the parameter passed from the caller. The evaluation of the actual parameters must be delayed until the execution. A procedure (thunk) is called to find the value of the parameter in the callee. voidchange(int x) {i=4; x++; return; } voidmain() {int i=0; int a[5]={0,0,0,0,0} ; change(a[i]); return; } After the call: a={0,0,0,0,1}