3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.

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

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
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
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:
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
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.
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.
Intro to Computer Architecture
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 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.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
7/13/20151 Topic 3: Run-Time Environment Memory Model Activation Record Call Convention Storage Allocation Runtime Stack and Heap Garbage Collection.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
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.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
JIT in webkit. What’s JIT See time_compilation for more info. time_compilation.
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.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
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.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
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.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
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.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
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,
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.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
Implementing Subprograms
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
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.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Lecture 3 Translation.
Run-time organization
An Overview to Compiler Design
Chapter 9 :: Subroutines and Control Abstraction
Memory Allocation CS 217.
Lecture 13: The Procedure Abstraction; Run-Time Storage Organisation
UNIT V Run Time Environments.
Operating Systems Run-time Organization
Run Time Environments 薛智文
Run-time environments
Presentation transcript:

3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23

3/17/2008Prof. Hilfinger CS 164 Lecture 232 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...

3/17/2008Prof. Hilfinger CS 164 Lecture 233 Run-time environments Before discussing code generation, we need to understand what we are trying to generate –The term virtual machine refers to the compiler’s target –Can be just a bare hardware architecture (small embedded systems) –Can be an interpreter, as for Java, or an interpreter that does additional compilation, as in modern Java JITs –For now, we’ll stick to hardware + conventions for using it (“API”) + some runtime-support library There are a number of standard techniques/conventions for structuring executable code that are widely used

3/17/2008Prof. Hilfinger CS 164 Lecture 234 Outline Management of run-time resources Correspondence between static (compile-time) and dynamic (run-time) structures Storage organization

3/17/2008Prof. Hilfinger CS 164 Lecture 235 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”)

3/17/2008Prof. Hilfinger CS 164 Lecture 236 Memory Layout (Example) Low Address High Address Memory Code Other Space

3/17/2008Prof. Hilfinger CS 164 Lecture 237 Notes These pictures are simplifications –E.g., not all memory need be contiguous Other Space = Data Space Compiler is responsible for: –Generating code –Orchestrating use of the data area

3/17/2008Prof. Hilfinger CS 164 Lecture 238 Code Generation Goals Two goals: –Correctness –Speed Minimize instruction counts Keep variables easy to access (static offsets, e.g.) Maximize use of registers (“top of the memory hierarchy”) Most complications in code generation come from trying to be fast as well as correct, because this requires attention to special cases.

3/17/2008Prof. Hilfinger CS 164 Lecture 239 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?

3/17/2008Prof. Hilfinger CS 164 Lecture 2310 Activations and Lifetimes (Extents) 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 The lifetime of a variable x is the portion of execution in which x is defined Lifetime is a dynamic (run-time) concept … As opposed to scope, which is a static concept

3/17/2008Prof. Hilfinger CS 164 Lecture 2311 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

3/17/2008Prof. Hilfinger CS 164 Lecture 2312 Example (from Java) class Main { int g() { return 1; } int f() {return g(); } void main() { g(); f(); } } Main f g g

3/17/2008Prof. Hilfinger CS 164 Lecture 2313 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(2); } } What is the activation tree for this example?

3/17/2008Prof. Hilfinger CS 164 Lecture 2314 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(2); } } Main f f f g

3/17/2008Prof. Hilfinger CS 164 Lecture 2315 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

3/17/2008Prof. Hilfinger CS 164 Lecture 2316 Example class Main { int g() { return 1; } int f() { return g(); } void main() { g(); f(); } } MainStack Main

3/17/2008Prof. Hilfinger CS 164 Lecture 2317 Example Main g Stack Main g class Main { int g() { return 1; } int f() { return g(); } void main() { g(); f(); } }

3/17/2008Prof. Hilfinger CS 164 Lecture 2318 Example Main g f Stack Main f class Main { int g() { return 1; } int f() { return g(); } void main() { g(); f(); } }

3/17/2008Prof. Hilfinger CS 164 Lecture 2319 Example Main f g g Stack Main f g class Main { int g() { return 1; } int f() { return g(); } void main() { g(); f(); } }

3/17/2008Prof. Hilfinger CS 164 Lecture 2320 Revised Memory Layout Low Address High Address Memory Code Stack

3/17/2008Prof. Hilfinger CS 164 Lecture 2321 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.

3/17/2008Prof. Hilfinger CS 164 Lecture 2322 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: –Space to save registers used by F or G –Space for G’s local variables –Temporary space for intermediate results, arguments and return values for functions G calls. Depending on architecture and compiler, registers may hold part of AR (at times).

3/17/2008Prof. Hilfinger CS 164 Lecture 2323 What Information is Needed to Return from G? Return address Contents of (some) registers prior to call Information to establish address of AR for G’s caller: –This address is called the dynamic link –Often kept in a register, but this is sometimes not necessary. Various other machine status prior to calling G

3/17/2008Prof. Hilfinger CS 164 Lecture 2324 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); (*) } } space for f’s result argument x return address dynamic link AR for f: AR for Main:

3/17/2008Prof. Hilfinger CS 164 Lecture 2325 Stack After Two Calls to f... Main (*)(*) 3 f 1 ’s result f2f2 f1f1 f 2 ’s result 2 (**) ( *) and (**) denote return addresses Main has no argument or local variables and its result is never used; its AR is uninteresting Only one of many possible AR designs Would also work for C, Pascal, FORTRAN, etc.

3/17/2008Prof. Hilfinger CS 164 Lecture 2326 The Main Point 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 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!

3/17/2008Prof. Hilfinger CS 164 Lecture 2327 Registers Real compilers hold as much of the frame as possible in registers –Especially the method result and arguments Registers also typically hold start of frame (frame pointer) and top of stack.

3/17/2008Prof. Hilfinger CS 164 Lecture 2328 Globals All references to a global variable point to the same object –Don’t generally 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

3/17/2008Prof. Hilfinger CS 164 Lecture 2329 Memory Layout with Static Data Low Address High Address Memory Code Stack Static Data

3/17/2008Prof. Hilfinger CS 164 Lecture 2330 Heap Storage A value that outlives the procedure that creates it cannot be kept in the AR: Bar foo() { return new Bar } The Bar value must survive deallocation of foo’s AR Language implementations with dynamically allocated data use a heap to store dynamic data –(confusingly, not the same as the heap used for priority queues!)

3/17/2008Prof. Hilfinger CS 164 Lecture 2331 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

3/17/2008Prof. Hilfinger CS 164 Lecture 2332 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

3/17/2008Prof. Hilfinger CS 164 Lecture 2333 Memory Layout with Heap Low Address High Address Memory Code Heap Static Data Stack

3/17/2008Prof. Hilfinger CS 164 Lecture 2334 Memory Layout with Heap (Alternative) Low Address High Address Memory Code Stack Static Data Heap

3/17/2008Prof. Hilfinger CS 164 Lecture 2335 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

3/17/2008Prof. Hilfinger CS 164 Lecture 2336 Alignment Many installed 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 New machines use 64-bit or 32/64-bit hardware and APIs.

3/17/2008Prof. Hilfinger CS 164 Lecture 2337 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