CPSC 388 – Compiler Design and Construction Runtime Environments.

Slides:



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

10/6: Lecture Topics Procedure call Calling conventions The stack
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
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)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Runtime Environments Source language issues 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
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.
Presented by Dr Ioanna Dionysiou
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
Chapter 8 :: Subroutines and Control Abstraction
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.
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Compiler Run-time Organization Lecture 7. 2 What we have covered so far… We have covered the front-end phases –Lexical analysis –Parsing –Semantic analysis.
Compiler Construction
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.
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.
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.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
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 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.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
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.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Computer Architecture & Operations I
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
Computer structure: Procedure Calls
COMPILERS Activation Records
Run-time organization
Procedures (Functions)
Functions and Procedures
The University of Adelaide, School of Computer Science
Understanding Program Address Space
10/4: Lecture Topics Overflow and underflow Logical operations
UNIT V Run Time Environments.
Program and memory layout
Procedures and Calling Conventions
Program and memory layout
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Presentation transcript:

CPSC 388 – Compiler Design and Construction Runtime Environments

Announcements  Test Problem 1c, the third example on strings in language is incorrect: Should be:  Test due Wednesday October 21 st

Runtime Environments  How storage is laid out at runtime.  What information is stored for each method.  What happens during method call and return for two different approaches to storage layout: static and stack allocation.

Memory Organization Code Static Data Heap Stack Lowest Address Highest Address

Stack and Heap  Stack stores One activiation record (AR) for each currently active method  Heap stores Dynamically allocated memory (anytime “new” is used in java)

int a[11]; void readArray() { /*reads 9 ints into a[1]-a[9] */ } int partition(int m, int n) { /*picks a separator value v, and partitions a[m..n] so that a[m..p-1] are less than v, a[p]=v and a[p+1,n] are equal to or greater than v. Returns p. */ } void quicksort(int m, int n) { int I; if (n > m){ i=partition(m,n); quicksort(m,i-1); quicksort(i+1,n); } main() { readArray(); a[0] =-9999; a[10]= 9999; quicksort(1,9); } enter main() enter readArray() leave readArray() enter quicksort() 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) leave main()

Activation Tree m r q(1,9) p(1,9) q(1,3) q(5,9) p(1,3)q(1,0)q(2,3) p(2,3)q(2,1)q(3,3) p(5,9)q(5,5)q(7,9) p(7,9) q(7,7)q(9,9) Suppose that control lies within a particular activiation Of some procedure, N, of the activation tree. The stack Corresponds to the ancestors of node N.

You try it Suppose partition always picks a[m] as the seperator and suppose that order is preserved as much as possible. Draw the activation tree for array 9, 8, 7, 6, 5, 4, 3, 2, 1 Draw the activation tree for array 1, 3, 5, 7, 9, 2, 4, 6, 8 What is the largest number of AR that appear on the stack?

Activation Record Low address High address Usually AR stored in the stack. Some languages (such as concurrent languages) store AR in heap or static area Temporaries Local Data Saved machine status Access link Control link Actual Parameters Returned value

Data in Activation Record  Temporary values, those that arise from evaluation of expressions that do not fit into registers  Local data belonging to the procedure  Machine Status, includes the return address and the contents of registers that will be used and must be restored  Access link used to locate data needed by called procedure but found in another AR  Control link pointing to AR of caller  Return value  Actual parameters, may be placed in registers

Static Allocation of AR  Every time a method is called, its parameters and local variables are stored in the same locations  ADVANTAGES + fast access to all names (e.g., no need to compute the address of a variable at runtime) + no overhead of stack/heap manipulation  DISADVANTAGES - no recursion - no dynamic allocation

Static Allocation – Calling Method  Copies each argument into the corresponding parameter's space in the called method's activation record (AR).  May save some registers (in its own AR).  Performs a "Jump & Link": Jump to the first instruction of the called method, and put the address of the next instruction after the call (the return address) into the special RA register (the "return address" register).

Static Allocation – Called Method  Copies the return address from RA into its AR's return-address field.  May save some registers (in its AR).  May initialize local data.

Static Allocation – Called method return  Restores the values of any registers that it saved.  Jumps to the address that it saved in its AR's return-address field.

Static Allocation – Calling method after return  Restores any registers that it saved.  If the called method was non-void (returned a value), put the return value (which may be in a special register or in the AR of the called method) in the appropriate place. For example, if the code was x = f(); then the return value should be copied into variable x.

Let’s Try It – Static Allocation Assume each AR contains local variables, parameters, the return address, and (for non-void methods) the return value. Trace the execution of the following code by filling in the appropriate fields of the activation records of the three methods. void error(String name, String msg) { System.out.println("ERROR in"+name+":"+msg); } int summation(int max) { int sum = 1; for (int k=1; k<=max; k++) { sum += k; } return sum; } void main() { int x = summation(3); if (x != 6) error("main", "bad value returned by summation"); }

Stack Allocation  Each time a method is called, a new AR (also called a stack frame) is pushed onto the stack.  The AR is popped when the method returns.  A register (SP for "stack pointer") points to the top of the stack.  Another register (FP for "frame pointer") points to the start of the current method's AR.

Steps of a method call  Calling Method start call steps  Called Method startup steps  Called Method end steps  Calling Method ends call steps

Calling Method Start Call  May save some registers (in its own AR).  If the language allows nested methods, may set up the access link; this means pushing the appropriate value -- more on this later -- onto the stack.  Pushes the parameters onto the stack (into space that will be part of the called method's AR).  Does a "Jump & Link" -- jumps to the 1st instruction of the called method, and puts the address of the next instruction (the one after the call) into register RA.

Called Method Startup  Pushes the return address (from RA) onto the stack (into its AR's "return address" field).  Pushes the old FP into its AR's "control link" field.  Sets the FP to point to the bottom of its AR (to the "access link" field if there is one; otherwise, to the first parameter). The address of that field is computed as follows: SP + (sizes of params) + (size of "access link" field) + (size of "return address" field) + (size of "control link" field). All of these sizes are computed at compile time. (Note that values are added to the SP because we are assuming that "lower" on the stack means a higher address.)  May save some registers (by pushing them onto the stack).  Sets up the "local data" fields. This may involve pushing actual values if the locals are initialized as part of their declarations, or it may just involve subtracting their total size from the SP.

Called Method Ending  Restores the values of any saved registers.  Loads the return address into register RA (from the AR).  Restores the old stack pointer (SP = FP).  Restores the old frame pointer (FP = saved FP, i.e., the value in the control-link field).  Return (jump to the address in register RA).

Calling Method Ends call  Copy values out of Return Value field (if there are any)

Example void f2(int y) { f1(y); } void f1(int x) { if (x > 0) f2(x-1); } main() { int a = 1; f(1); } See work on board: Keep track of FP, SP, CL field, and parameters and Local variables

You Try It Assume that stack allocation is used, and that each activation record contains local variables, parameters, and a control link. Trace the execution of the following code by filling in the appropriate fields of the activation records (recall that dynamically allocated storage is stored in the heap, not on the stack). void init(int[] A, int len) { for (int k=1; k<len; k++) { A[k] = k; } void main() { int[] x = new int[3]; init(x, 3); }