CS 536 Spring 20011 Run-time organization Lecture 19.

Slides:



Advertisements
Similar presentations
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Advertisements

CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
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 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
ISBN Chapter 10 Implementing Subprograms.
CS 536 Spring Code generation I Lecture 20.
1 Lecture 9 Runtime Environment. 2 Outline Basic computer execution model Procedure abstraction run-time storage management Procedure linkage We need.
Run-Time Storage Organization
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
CS 536 Spring Code Generation II Lecture 21.
Semantics of Calls and Returns
Run-time Environment and Program Organization
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
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.
ISBN Chapter 10 Implementing Subprograms.
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.
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.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
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.
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.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
Activation Records (in Tiger) CS 471 October 24, 2007.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
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,
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
CSC 8505 Compiler Construction Runtime Environments.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
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.
Implementing Subprograms
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.
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?
Implementing Subprograms Chapter 10
Implementing Subprograms
Run-time organization
Run-Time Storage Organization
Run-Time Storage Organization
Introduction to Compilers Tim Teitelbaum
Chapter 9 :: Subroutines and Control Abstraction
Memory Allocation CS 217.
Topic 3-b Run-Time Environment
UNIT V Run Time Environments.
Operating Systems Run-time Organization
Run Time Environments 薛智文
Presentation transcript:

CS 536 Spring Run-time organization Lecture 19

CS 536 Spring Status We have covered the front-end phases –Lexical analysis –Parsing –Semantic analysis Next are the back-end phases –Optimization –Code generation We’ll do code generation first...

CS 536 Spring Run-time environments Before discussing code generation, we need to understand what we are trying to generate There are a number of standard techniques for structuring executable code that are widely used

CS 536 Spring Outline Management of run-time resources Correspondence between static (compile-time) and dynamic (run-time) structures Storage organization

CS 536 Spring Run-time Resources Execution of a program is initially under the control of the operating system When a program is invoked: –The OS allocates space for the program –The code is loaded into part of the space –The OS jumps to the entry point (i.e., “main”)

CS 536 Spring Memory Layout Low Address High Address Memory Code Other Space

CS 536 Spring Notes By tradition, pictures of machine organization have: –Low address at the top –High address at the bottom –Lines delimiting areas for different kinds of data These pictures are simplifications –E.g., not all memory need be contiguous

CS 536 Spring What is Other Space? Holds all data for the program Other Space = Data Space Compiler is responsible for: –Generating code –Orchestrating use of the data area

CS 536 Spring Code Generation Goals Two goals: –Correctness –Speed Most complications in code generation come from trying to be fast as well as correct

CS 536 Spring Assumptions about Execution 1.Execution is sequential; control moves from one point in a program to another in a well- defined order 2.When a procedure is called, control eventually returns to the point immediately after the call Do these assumptions always hold?

CS 536 Spring Activations An invocation of procedure P is an activation of P The lifetime of an activation of P is –All the steps to execute P –Including all the steps in procedures P calls

CS 536 Spring Lifetimes of Variables The lifetime of a variable x is the portion of execution in which x is defined Note that –Lifetime is a dynamic (run-time) concept –Scope is a static concept

CS 536 Spring Activation Trees Assumption (2) requires that when P calls Q, then Q returns before P does Lifetimes of procedure activations are properly nested Activation lifetimes can be depicted as a tree

CS 536 Spring Example class Main { int g() { return 1; } int f() {return g(); } void main() { g(); f(); } } Main f g g

CS 536 Spring Example 2 class Main { int g() { return 1; } int f(int x) { if (x == 0) { return g(); } else { return f(x - 1); } } void main() { f(3); } } What is the activation tree for this example?

CS 536 Spring Notes The activation tree depends on run-time behavior The activation tree may be different for every program input Since activations are properly nested, a stack can track currently active procedures

CS 536 Spring Example class Main { int g() { return 1; } int f() { return g(); } void main() { g(); f(); } } MainStack Main

CS 536 Spring Example Main g Stack Main g class Main { int g() { return 1; } int f() { return g(); } void main() { g(); f(); } }

CS 536 Spring Example Main g f Stack Main f class Main { int g() { return 1; } int f() { return g(); } void main() { g(); f(); } }

CS 536 Spring Example Main f g g Stack Main f g class Main { int g() { return 1; } int f() { return g(); } void main() { g(); f(); } }

CS 536 Spring Revised Memory Layout Low Address High Address Memory Code Stack

CS 536 Spring Activation Records The information needed to manage one procedure activation is called an activation record (AR) or frame If procedure F calls G, then G’s activation record contains a mix of info about F and G.

CS 536 Spring What is in G’s AR when F calls G? F is “suspended” until G completes, at which point F resumes. G’s AR contains information needed to resume execution of F. G’s AR may also contain: –G’s return value (needed by F) –Actual parameters to G (supplied by F) –Space for G’s local variables

CS 536 Spring The Contents of a Typical AR for G Space for G’s return value Actual parameters Pointer to the previous activation record –The control link; points to AR of caller of G Machine status prior to calling G –Contents of registers & program counter –Local variables Other temporary values

CS 536 Spring Example 2, Revisited class Main { int g() { return 1; } int f(int x) { if (x == 0) { return g(); } else { return f(x - 1); (**) } } void main() { f(3); (*) } } AR for f: result argument control link return address

CS 536 Spring Stack After Two Calls to f Main (**) 2 (result)f (*)(*) 3 f

CS 536 Spring Notes Main has no argument or local variables and its result is never used; its AR is uninteresting (*) and (**) are return addresses of the invocations of f –The return address is where execution resumes after a procedure call finishes This is only one of many possible AR designs –Would also work for C, Pascal, FORTRAN, etc.

CS 536 Spring The Main Point The compiler must determine, at compile-time, the layout of activation records and generate code that correctly accesses locations in the activation record Thus, the AR layout and the code generator must be designed together!

CS 536 Spring Example The picture shows the state after the call to 2nd invocation of f returns Main (**) 2 1f (*)(*) 3 (result)f

CS 536 Spring Discussion The advantage of placing the return value 1st in a frame is that the caller can find it at a fixed offset from its own frame There is nothing magic about this organization –Can rearrange order of frame elements –Can divide caller/callee responsibilities differently –An organization is better if it improves execution speed or simplifies code generation

CS 536 Spring Discussion (Cont.) Real compilers hold as much of the frame as possible in registers –Especially the method result and arguments

CS 536 Spring Globals All references to a global variable point to the same object –Can’t store a global in an activation record Globals are assigned a fixed address once –Variables with fixed address are “statically allocated” Depending on the language, there may be other statically allocated values

CS 536 Spring Memory Layout with Static Data Low Address High Address Memory Code Stack Static Data

CS 536 Spring Heap Storage A value that outlives the procedure that creates it cannot be kept in the Arvoid Bar foo() { return new Bar } The Bar value must survive deallocation of foo’s AR Languages with dynamically allocated data use a heap to store dynamic data

CS 536 Spring Notes The code area contains object code –For most languages, fixed size and read only The static area contains data (not code) with fixed addresses (e.g., global data) –Fixed size, may be readable or writable The stack contains an AR for each currently active procedure –Each AR usually fixed size, contains locals Heap contains all other data –In C, heap is managed by malloc and free

CS 536 Spring Notes (Cont.) Both the heap and the stack grow Must take care that they don’t grow into each other Solution: start heap and stack at opposite ends of memory and let the grow towards each other

CS 536 Spring Memory Layout with Heap Low Address High Address Memory Code Stack Static Data Heap

CS 536 Spring Data Layout Low-level details of machine architecture are important in laying out data for correct code and maximum performance Chief among these concerns is alignment

CS 536 Spring Alignment Most modern machines are (still) 32 bit –8 bits in a byte –4 bytes in a word –Machines are either byte or word addressable Data is word aligned if it begins at a word boundary Most machines have some alignment restrictions –Or performance penalties for poor alignment

CS 536 Spring Alignment (Cont.) Example: A string “Hello” Takes 5 characters (without a terminating \0) To word align next datum, add 3 “padding” characters to the string The padding is not part of the string, it’s just unused memory