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:

Slides:



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

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.
The University of Adelaide, School of Computer Science
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
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)
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Run time vs. Compile time
Semantics of Calls and Returns
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.
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.
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.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
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.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
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.
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.
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.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
ISBN Chapter 10 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.
1 Topic 6: Activation Records COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer.
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
COMPILERS Activation Records
Implementing Subprograms
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Chapter 10: Implementing Subprograms Sangho Ha
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Implementing Subprograms
MIPS Instructions.
The University of Adelaide, School of Computer Science
Implementing Subprograms
Program and memory layout
Program and memory layout
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
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

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: Small number of registers E.g. if there are no registers available or their contents need to be spilled, we must have a place in memory where this data can be saved. Some data cannot be stored in registers E.g. variables whose address needs to be accessed, or variables that are too large to fit in a register. Things we'd like to store in registers: Frequently used variables Function parameters Function return values

2 Storage Registers vs. memory from the point of view of a function: Some variables can be stored in registers Exceptions: Variables whose address is being used Variables that will be passed by reference to another called function. Variables such as arrays (due to indexing issues) Parameters and return values can be stored in registers Useful fact: most functions have at most 4 parameters. There must be an area in memory, associated with the function, so that the debugger can provide a backtrace. Items that need to be stored in memory, are all grouped together and placed in a stack frame.

3 Storage organization Stack layout Each called procedure is given a stack frame at the top of the stack Stack typically grows towards lower addresses At any time, the stack pointer points to the current top of the stack (first available location) At any time, the frame pointer points to the beginning of the current frame The stack frame size and the offsets are determined at compile time! Stack frame contents are accessed as offsets relative to the stack pointer or frame pointer. Why would we use the frame pointer? Can we do without it?

4 Storage organization Stack frames. What's typically in them? parameters local variables temporary values return value stack pointer frame pointer return address (saved pc) dynamic link Reference to the frame of the caller, used to restore the stack after the call. static link Reference to the nearest frame of the lexically enclosing procedure. Used for non-local accesses in languages that support statically nested scopes.

5 Storage organization Typical stack layout. Assume function f calls g, and g is about to call h parameter 5 parameter 6... temporary values return value saved fp return address local variables other saved registers parameters saved fp return address return value f's frame g's frame current fp current sp The first 4 parameters for g are typically in registers. The rest are stored here in reverse order, so that they are accessed in the "right" order by g: fp+increment The value stored here is f's frame pointer. It will be restored on return from g. This is essentially the dynamic link Temporary variables are created by the compiler during code generation. g is responsible for saving the contents of certain registers (possibly used by f), and restoring them on exit. These are h's parameters Often stored in a register Program counter. So that g knows what the next instruction is after the return from h.

6 Handling nested procedures Some languages allow nested procedures Example: In order to implement the "closest nested scope" rule we need access to the frame of the lexically enclosing procedure proc A() { proc B () { call C() } proc C() { proc D() { proc E() { call B() } call E() } call D() } call B() } call sequence: A C B B E D B can call C B cannot call D or E E can access C's locals C cannot access B's locals

7 Handling nested procedures Some languages allow nested procedures In order to implement the "closest nested scope" rule we need access to the frame of the lexically enclosing procedure Method 1 : static links (a.k.a. access links) Reference to the frame of the lexically enclosing procedure Static chains of such links are created. How do we use them to access non-locals? The compiler knows the scope s of a variable The compiler knows the current scope t Follow s-t links

8 Handling nested procedures Method 1 : static links Setting the links: if the callee is nested directly within the caller, set its static link to point to the caller's frame pointer (or stack pointer) if the callee has the same nesting level as the caller, set its static link to point to wherever the caller's static link points.

9 Handling nested procedures Method 2 : Displays A Display encodes the static link info in an array. The display may be stored in registers or in memory The ith element of the array points to the frame of the most recent procedure at scope level i How? When a new stack frame is created for a procedure at nesting level i, save the current value of D[i] in the new stack frame (to be restored on exit) set D[i] to the new stack frame

10 Handling nested procedures Displays vs. Static links criteria added overhead space nesting depth frequency of non-local accesses storage in registers vs. memory memory storage requires extra loads register storage ties up registers

11 Parameter passing By value actual parameter is copied By reference address of actual parameter is stored By value-result call by value, AND the values of the formal parameters are copied back into the actual parameters. Example: int a; void test(int x) { x = 2; a = 0; } int main () { a = 1; test(a); a is 2 here

12 Stack maintenance Calling sequence : code executed by the caller before and after a call code executed by the callee at the beginning code executed by the callee at the end

13 Stack maintenance A typical calling sequence : 1. Caller assembles arguments and transfers control evaluate arguments place arguments in stack frame and/or registers save caller-saved registers save return address jump to callee's first instruction

14 Stack maintenance A typical calling sequence : 2. Callee saves info on entry allocate memory for stack frame, update stack pointer save callee-saved registers save old frame pointer update frame pointer 3. Callee executes

15 Stack maintenance A typical calling sequence : 4. Callee restores info on exit and returns control place return value in appropriate location restore callee-saved registers restore frame pointer pop the stack frame jump to return address 5. Caller restores info restore caller-saved registers