Activation Records (in Tiger) CS 471 October 24, 2007.

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.
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.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
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
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 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.
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
CS 536 Spring Code generation I Lecture 20.
Run-Time Storage Organization
Run time vs. Compile time
Semantics of Calls and Returns
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 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.
ISBN Chapter 10 Implementing Subprograms.
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.
Compiler Construction
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.
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.
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.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
ISBN Chapter 10 Implementing Subprograms.
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.
Lecture 19: Control Abstraction (Section )
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Implementing Subprograms
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
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
Implementing Subprograms Chapter 10
COMPILERS Activation Records
Implementing Subprograms
Run-time organization
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Functions and Procedures
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
The University of Adelaide, School of Computer Science
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Computer Architecture
Program and memory layout
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Activation Records (in Tiger) CS 471 October 24, 2007

CS 471 – Fall Course Announcements PA5 due Friday –we’ve got a head start! –still a good bit of work to do! I will be at NSF on Monday-Tuesday –Guest lecture on intermediate representations No class on Monday before Thanksgiving Anonymous feedback? (Link on the web page) –Pace of the lectures –Pace of the project –Any other feedback

CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic Analysis Intermediate Code Gen lexical errors syntax errors semantic errors tokens AST AST’ IR

CS 471 – Fall Activation Records The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage Storage organization A procedure is a control abstraction it associates a name with a chunk of code that piece of code is regarded in terms of its purpose and not of its implementation A procedure creates its own name space It can declare local variables Local declarations may hide non-local ones Local names cannot be seen from outside Why is this separate from symbol tables?

CS 471 – Fall Handling Control Abstractions Generated code must be able to preserve current state –save variables that cannot be saved in registers –save specific register values establish procedure environment on entry –map actual to formal parameters –create storage for locals restore previous state on exit This can be modeled with a stack Allocate a memory block for each activation Maintain a stack of such blocks This mechanism can handle recursion

CS 471 – Fall Handling Variable Storage Static allocation object is allocated an address at compile time location is retained during execution Stack allocation objects are allocated in LIFO order Heap allocation objects may be allocated and deallocated at any time.

CS 471 – Fall Static Allocation Objects that are allocated statically include: globals explicitly declared static variables instructions string literals compiler-generated tables used during run time

CS 471 – Fall Stack Allocation Follows stack model for procedure activation A procedure's memory block associated with an activation of the procedure is called an activation record or stack frame The stack frame is pushed on the stack when the procedure is called and popped (and destroyed) when the procedure terminates What can we determine at compile time? We cannot determine the address of the stack frame But we can determine the size of the stack frame and the offsets of various objects within a frame

CS 471 – Fall Heap Allocation Used for dynamically allocated/resized objects Managed by special algorithms General model maintain list of free blocks allocate block of appropriate size handle fragmentation handle garbage collection

CS 471 – Fall Stack Last In, First Out (LIFO) data structure main () { a(0); } void a (int m) { b(1); } void b (int n) { c(2); } void c (int o) { d(3); } void d (int p) { } stack Stack Pointer Stack grows down Stack Pointer

CS 471 – Fall Stack Frames Activation record or stack frame stores: local vars parameters return address temporaries (…etc) (Frame size not known until late in the compilation process) Arg n … Arg 2 Arg 1 Static link Local vars Ret address Temporaries Saved regs Arg m … Arg 1 Static link current frame previous frame next frame sp fp

CS 471 – Fall The Frame Pointer Keeps track of the bottom of the current activation record g(…) { f(a1,…,an); } g is the caller f is the callee What if f calls two functions? Arg n … Arg 2 Arg 1 Static link Local vars Ret address Temporaries Saved regs Arg m … Arg 1 Static link f’s frame g’s frame next frame sp fp

CS 471 – Fall Activation Records Handling nested procedures –We must keep a static link (vs. dynamic link) Registers vs. memory –Registers are faster. Why? add %eax,%ebx,%ecx ld %ebx, 0[%fp] ld %ecx, 4[%fp] add %eax,%ebx,%ecx Cycles?

CS 471 – Fall Registers Depending on the architecture, may have more or fewer registers (typically 32) Always faster to use registers (remember the memory hierarchy) Want to keep local variables in registers (when possible … why can’t we decide now?) %ah%al %eax %ax

CS 471 – Fall Caller-save vs. Callee-save Registers What if f wants to use a register? Who must save/restore that register? g(…) { f(a1,…,an); } If g does a save before the call and a restore after the call caller-save If f does a save before it uses a register and restore afterward callee-save What are the tradeoffs? Arg n … Arg 2 Arg 1 Static link Local vars Ret address Temporaries Saved regs Arg m … Arg 1 Static link f’s frame g’s frame next frame sp fp

CS 471 – Fall Caller-save vs. Callee-save Registers Usually – some registers are marked caller save and some are marked callee save e.g. MIPS: r16-23 callee save all others caller save Optimization – if g knows it will not need a value after a call, it may put it in a caller-save register, but not save it Deciding on the register will be the task of the register allocator (still a hot research area).

CS 471 – Fall 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 Typical Convention –Usually 4 parameters placed in registers –Rest on stack –Why?

CS 471 – Fall Parameter Passing We often put 4/6 parameters in registers … What happens when we call another function? Leaf procedures – don’t call other procedures Non-leaf procedures may have dead variables Interprocedural register allocation Register windows r32r33r34r35r32111r33r34r35r32r33r34r35r32 A()B()C() Outgoing args of A become incoming args to B

CS 471 – Fall Return Address Return address - after f calls g, we must know where to return  Old machines – return address always pushed on the stack  Newer machines – return address is placed in a special register (often called the link register %lr ) automatically during the call instruction Non-leaf procedures must save %lr on the stack Sidenote: Itanium has 8 “branch registers”

CS 471 – Fall 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

CS 471 – Fall 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

CS 471 – Fall 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

CS 471 – Fall 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

CS 471 – Fall Tiger Compiler Frames How should our Tiger compiler stack frames be organized? –Depends whether we want to call C functions –Actual layout will be architecture dependent Approach: abstract away the implementation (just as we did with the symbol table) typedef struct F_frame_ *F_frame;

CS 471 – Fall Layers of Abstraction Frame and temp interfaces – machine- independent views of memory-resident and register- resident variables Translate – generates IR – manages local variables and static function nesting. (For clarity – translate is separated from semant) semant.c translate.c frame.c temp.c translate.h frame.htemp.h

CS 471 – Fall Summary Stack frames –Keep track of run-time data –Define the interface between procedures Both language and architectural features will affect the stack frame layout and contents Started to see hints of the back-end optimizer, register allocator, etc. Next week: Intermediate Representations