COMPILERS Activation Records

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
The University of Adelaide, School of Computer Science
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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.
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:
ISBN Chapter 10 Implementing Subprograms.
Run time vs. Compile time
Semantics of Calls and Returns
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 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
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.
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.
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.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
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.
Subprograms - implementation
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.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
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.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Implementing Subprograms
Lecture 7 Macro Review Stack Frames
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Implementing Subprograms
Computer Science 210 Computer Organization
Implementing Subprograms
Implementing Subprograms
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
The Procedure Abstraction Part IV: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights.
Functions and Procedures
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Chapter 9 :: Subroutines and Control Abstraction
Implementing Subprograms
Activation Records and Function Calls
Implementing Subprograms
Implementing Subprograms
10/4: Lecture Topics Overflow and underflow Logical operations
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Runtime Stack Activation record for hanoi;
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

COMPILERS Activation Records hussein suleman uct csc3005h 2006

Subprogram Invocation Mechanics Save status of caller. Process parameters. Save return address. Jump to called subprogram. … do stuff ... Process value-result/result parameters and function return value(s). Restore status of caller. Jump back to caller’s saved position.

Frames / Activation Records An activation record is the layout of data needed to support a call to a subprogram. For languages that do not allow recursion, each subprogram has a single fixed activation record instance stored in memory (and no links). Return address Static link Dynamic link Parameters Local variables Function return value

Stack-based Recursion When recursion is implemented using a stack, activation records are pushed onto the stack at invocation and popped upon return. Example: int sum ( int x ) { if (x==0) return 0; else return (x + sum (x-1)); } void main () { sum (2); }

Recursion Activation Records retvalue (?) parm (x=0) dynamiclink staticlink return (sum) sum(0) retvalue (?) parm (x=1) dynamiclink staticlink return (sum) retvalue (?) parm (x=1) dynamiclink staticlink return (sum) sum(1) sum(1) retvalue (?) parm (x=2) dynamiclink staticlink return (main) retvalue (?) parm (x=2) dynamiclink staticlink return (main) retvalue (?) parm (x=2) dynamiclink staticlink return (main) sum(2) sum(2) sum(2) mainARI mainARI mainARI main main main

Non-local References To access non-local names in statically-scoped languages, a program must keep track of the current referencing environment. Static chains Link a subprogram’s activation record to its static parent. Displays Keep a list of active activation records.

Non-local Reference Example main { int x; sub SUBA { sub SUBB { x = 1; } SUBB; sub SUBC { int y; SUBA; SUBC; breakpoint3 breakpoint2 breakpoint1 breakpoint0

Static Chains SUBB SUBA SUBA SUBC SUBC SUBC main main main breakpoint1 dynamiclink staticlink return (A) SUBB dynamiclink staticlink return (C) dynamiclink staticlink return (C) SUBA SUBA local (x) local (y) dynamiclink staticlink return (main) local (x) local (y) dynamiclink staticlink return (main) local (x) local (y) dynamiclink staticlink return (main) SUBC SUBC SUBC local (x) local (x) local (x) main main main breakpoint1 breakpoint2 breakpoint3

Displays stack display stack display stack display breakpoint1 SUBB ARI SUBA ARI 2 SUBA ARI SUBC ARI 1 SUBC ARI 1 SUBC ARI 1 MAIN ARI MAIN ARI MAIN ARI stack display stack display stack display breakpoint1 breakpoint2 breakpoint3

Static Chains vs. Displays Static chains require more indirect addressing – displays require a fixed amount of work. Displays require pointer maintenance on return – static chains do not. Displays require “backing up” of display pointer – static chains require static links in each activation record.

Dynamic Scoping Dynamically scoped languages can be implemented using: Deep Access Follow the dynamic chains to find most recent non-local name definition. Shallow Access Maintain a separate stack for each name.

Deep Access At breakpoint3, by following dynamic links from SUBB, the closest definition of x is in SUBC. (Remember that for static scoping, by following static links, the closest definition is in main.) dynamiclink staticlink return (A) SUBB dynamiclink staticlink return (C) SUBA local (x) local (y) dynamiclink staticlink return (main) SUBC local (x) main breakpoint3

Frame Pointers Stack frames are usually supported by: stack pointer - points to top of stack frame pointer - points to top of previous frame frame pointer AR f AR f AR g frame pointer AR g stack pointer AR h After call to h() stack pointer

View Shifts On a Pentium machine, M[SP + 0] <-- FP save old frame pointer FP <-- SP move frame pointer to top of stack SP <-- SP - K move stack pointer to end of new frame On machines which use registers for frame optimisation, remember to save registers in temporary variables.

Register Handling One set of registers are typically used by many subprograms, so a value expected by one may be overwritten by another. Solution: Make it the responsibility of the caller to save registers first (caller-save) Make it the responsibility of the callee to save registers first (callee-save) Optimise which registers need to be saved as some values can be thrown away.

Parameter Passing Registers are more efficient than copying every parameter to the stack frame. Registers are limited so pass first k parameters in registers and rest in frame. Nested subprogram calls require saving and restoring so there is dubious cost savings! leaf procedures, different registers, done with variables, register windows How does C support varargs ?

Return Addresses Traditionally a stack frame entry. More efficient to simply use a register. Same saving procedure necessary as before for non-leaf subprograms.

Temporaries and Labels Each time a local variable is encountered, a unique temporary name is generated – this temporary will eventually map to either a register or a memory location (usually on the stack). Each time a subprogram is encountered, a unique label is generated. These must be unique to prevent naming conflicts - the optimiser will deal with efficiency.

Frame Implementation 1/2 A Frame class corresponds to the frame for each subprogram. During translation, frames are created to track variables and generate prologue/epilogue code. Frame can be an abstract class with instantiations for different machine architectures. Each instantiation must know how to implement a “view shift” from one frame to another.

Frame Implementation 2/2 Each time a local variable is defined, a method of Frame can be called to allocate space appropriately (on stack frame or in registers). f.allocLocal (false) Parameter indicates if variable requires memory (escapes) or not - should we allocate stack space or temporary? Allocating a temporary for each variable can be slow - future stages will optimise by reusing both registers and space.

Stack vs. Registers Why use registers? Faster and smaller code If registers are so great, why use stack? variables used/passed by reference nested subprograms variable is not simple or just too big arrays registers are needed for other purposes too many variables