Activation Records Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc05.html Chapter 6.3.

Slides:



Advertisements
Similar presentations
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Advertisements

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.
Activation Records Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Chapter.
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
1 Subroutines and Control Abstraction. 2 Control Abstraction Abstraction Abstraction associate a name N to a program part P associate a name N to a program.
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:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Register Allocation Mooly Sagiv html://
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.
Activation Records Mooly Sagiv html:// Chapter 6.3.
ISBN Chapter 10 Implementing Subprograms.
Activation Records Mooly Sagiv html:// Chapter 6.3.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Run time vs. Compile time
Activation Records Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Chapter.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
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.
Code Generation for Control Flow Mooly Sagiv html:// Chapter 6.4.
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.
CS 2104 Prog. Lang. Concepts Subprograms
COMPILER CONSTRUCTION Principles and Practice Kenneth C. Louden.
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.
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.
Compilation (Semester A, 2013/14) Lecture 9: Activation Records + Register Allocation Noam Rinetzky Slides credit: Roman Manevich, Mooly Sagiv.
Compilation /15a Lecture 8 IR for Procedure calls + Optimizations Noam Rinetzky 1.
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.
Programming Languages and Paradigms Imperative Programming.
ISBN Chapter 10 Implementing Subprograms.
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.
7. Runtime Environments Zhang Zhizheng
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
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.
Implementing Subprograms Chapter 10
Mooly Sagiv html://
Compilation /15a Lecture 7
COMPILERS Activation Records
Activation Records Mooly Sagiv
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
MIPS Instructions.
Code generation for procedure calls Noam Rinetzky
Runtime Environments What is in the memory?.
Presentation transcript:

Activation Records Mooly Sagiv html:// Chapter 6.3

Outline of this lecture Operations on routines Properties of variables, L-Values, R-Values Stack Frames The Frame Pointer and Frame Size The Lexical Pointers and Nesting Levels Machine Architectures Parameter Passing and Return Address Frame Resident Variables Limitations Summary

Operations on Routines Declarations Definitions Call Return Jumping out of routines Passing routines as parameters Returning routines as parameters

Nested routines in C syntax

Non-Local goto in C syntax

Non-local gotos in C setjmp remembers the current location and the stack frame longjmp jumps to the current location (popping many activation records)

Non-Local Transfer of Control in C

Passing a function as parameter void foo (void (*interrupt_handler)(void)) { … if (…) interrupt_handler(); … }

Currying in C syntax int (*)() f(int x) { int g(int y) { return x + y; } return g ; } int (*h)() = f(3); int (*j)() = f(4); int z = h(5); int w = j(7);

Compile-Time Information on Variables Name Type Scope –when is it recognized Duration –Until when does its value exist Size –How many bytes are required at runtime Address –Fixed –Relative –Dynamic

L-values vs. R-values Assignment x := exp is compiled into: –Compute the address of x –Compute the value of exp –Store the value of exp into the address of x Generalization –R-value –L-value

Stack Frames Allocate a separate space for every procedure incarnation Relative addresses Provide a simple mean to achieve modularity Supports separate code generation of procedures Naturally supports recursion Efficient memory allocation policy –Low overhead –Hardware support may be available LIFO policy Not a pure stack –Non local references –Updated using arithmetic

A Typical Stack Frame higher addresses previous frame current frame lexical pointer argument 1 argument 2 dynamic link return address temporaries argument 2 argument 1 outgoing parameters lower addresses next frame frame size frame pointer stack pointer outgoing parameters registers locals administrative

L-Values of Local Variables The offset in the stack is known at compile time L-val(x) = FP+offset(x) x = 5  Load_Constant 5, R3 Store R3, offset(x)(FP)

Code Blocks Programming language provide code blocks void foo() { int x = 8 ; y=9; { int x = y * y ; } { int x = y * 7 ;} x = y + 1; }

lexical pointer Pascal Frame higher addresses previous frame current frame lexical pointer argument 2 argument 1 locals return address temporaries argument 1 argument 2 outgoing parameters saved registers lower addresses next frame ebp sp previous ebp

Summary thus far The structure of the stack frame may depend on –Machine –Architecture –Programming language –Compiler Conventions The stack is updated by: –Emitted compiler instructions –Designated hardware instructions

The Frame Pointer The caller –the calling routine The callee –the called routine caller responsibilities: –Calculate arguments and save in the stack –Store lexical pointer call instruction: M[--SP] := RA PC := callee callee responsibilities: –FP := SP –SP := SP - frame-size Why use both SP and FP?

Variable Length Frame Size C allows allocating objects of unbounded size in the stack void p() { int i; char *p; scanf(“%d”, &i); p = (char *) alloca(i*sizeof(int)); } Some versions of Pascal allows conformant array value parameters

Pascal Conformant Arrays program foo ; const max = 4 ; var m 1, m 2, m 3 : array [1..max, 1..max] of integer var i, j: integer procedure mult(a, b: array [1..l, 1..l] of integer; var c:array [1..l, 1..l] of integer)); var i, j, k: integer; begin { mult } for i := 1 to l do for j := 1 to l do begin c[i, j] := 0 ; for k := 1 to l do c[i, j] := c[i, j] + a[i, k] * b[k, j]; end end; { mult} begin { foo} … mult(m 1, m 2, m 3 ) end. { foo}

A Typical Stack Frame higher addresses previous frame current frame lexical pointer argument 1 argument 2 dynamic link return address temporaries lower addresses Constant frame size frame pointer stack pointer outgoing parameters registers local1 administrative local2 Space Of Local 1

Supporting Static Scoping References to non-local variables Language rules –No nesting of functions C, C++, Java –Non-local references are bounded to the most recently enclosed declared procedure and “die” when the procedure end Algol, Pascal, Scheme Simplest implementation Pass the lexical pointer as an extra argument to functions –Scope rules guarantee that this can be done Generate code to traverse the frames

Routine Descriptor for Languages with nested scopes Lexical pointer routine address

Calling Routine R from Q

Nesting Depth The semantic analysis identifies the static nesting hierarchy A possible implementation –Assign integers to functions and variables –Defined inductively The main is at level 0 Updated when new function begins/ends

Calculating L-Values int i; void level_0(void){ int j; void level_1(void) { int k; void level_2(void) { int l; k=l; j =l; }}} offset(lexical_pointer) + FP *( offset(k)

Code for the k=l int i; void level_0(void){ int j; void level_1(void) { int k; void level_2(void) { int l; k=l; j =l; }}}

Code for the j=l int i; void level_0(void){ int j; void level_1(void) { int k; void level_2(void) { int l; k=l; j =l; }}}

Other Implementations of Static Scoping Display –An array of lexical pointers –d[i] is lexical pointer nesting level i –Can be stored in the stack lambda-lifting – Pass non-local variables as extra parameters

Machine Registers Every year –CPUs are improving by 50%-60% –Main memory speed is improving by 10% Machine registers allow efficient accesses –Utilized by the compiler Other memory units exist –Cache

RISC vs. CISC Machines FeatureRISCCISC Registers  32 6, 8, 16 Register ClassesOneSome Arithmetic OperandsRegistersMemory+Registers Instructions3-addr2-addr Addressing Modes r M[r+c] (l,s) several Instruction Length32 bitsVariable Side-effectsNoneSome Instruction-Cost“Uniform”Varied

Callee-Save Registers Saved by the callee before modification Usually at procedure prolog Restored at procedure epilog Hardware support may be available Values are automatically preserved across calls.global _foo Add_Constant -K, SP //allocate space for foo Store_Local R5, -14(FP) // save R5 JSR f1 Load_Reg R5, R0 ; JSR g1; Add_Constant R5, 2; Load_Reg R5, R0 Load_Local -14(FP), R5 // restore R5 Add_Constant K, SP; RTS // deallocate int foo(int a){ int b=a+1; f1(); g1(b); return(b+2); }

Caller-Save Registers Saved by the calller before calls when needed Values are not automatically preserved across calls.global _bar Add_Constant -K, SP //allocate space for bar Add_Constant R0, 1 JSR f2 Load_Constant 2, R0 ; JSR g2; Load_Constant 8, R0 ; JSR g2 Add_Constant K, SP // deallocate space for bar RTS void bar (int y) { int x=y+1; f2(x); g2(2); g2(8); }

Caller-Save and Callee-Save Registers Usually the architecture defines caller-save and callee-save registers –Separate compilation –Interoperability between code produced by different compilers/languages But compiler writers decide when to use calller/callee registers

Parameter Passing 1960s – In memory No recursion is allowed 1970s – In stack 1980s – In registers – First k parameters are passed in registers (k=4 or k=6) –Where is time saved? Most procedures are leaf procedures Interprocedural register allocation Many of the registers may be dead before another invocation Register windows are allocated in some architectures per call (e.g., sun Sparc)

Modern Architectures return-address –also normally saved in a register on a call –a non leaf procedure saves this value on the stack –No stack support in the hardware function-result –Normally saved in a register on a call –A non leaf procedure saves this value on the stack

Limitations The compiler may be forced to store a value on a stack instead of registers The stack may not suffice to handle some language features

Frame-Resident Variables A variable x cannot be stored in register when: –x is passed by reference – Address of x is taken (&x) – is addressed via pointer arithmetic on the stack-frame (C varags) –x is accessed from a nested procedure – The value is too big to fit into a single register – The variable is an array – The register of x is needed for other purposes – Too many local variables An escape variable: –Passed by reference –Address is taken –Addressed via pointer arithmetic on the stack-frame –Accessed from a nested procedure

The Frames in Different Architectures PentiumMIPSSparc InFrame(8)InFrame(0)InFrame(68) InFrame(12)InReg(X 157 ) InFrame(16)InReg(X 158 ) M[sp+0]  fp fp  sp sp  sp-K M[sp+K+0]  r 2 X 157  r4 X 158  r5 save %sp, -K, %sp M[fp+68]  i 0 X 157  i 1 X 158  i 2 g(x, y, z) where x escapes x y z View Change

The Need for Register Copies void m(int x, int y) { h(y, y); h(x, x); }

Limitations of Stack Frames A local variable of P cannot be stored in the activation record of P if its duration exceeds the duration of P Example 1: Static variables in C (own variables in Algol) void p(int x) { static int y = 6 ; y += x; } Example 2: Features of the C language int * f() { int x ; return &x ; } Example 3: Dynamic allocation int * f() { return (int *) malloc(sizeof(int)); }

Currying Functions int (*)() f(int x) { int g(int y) { return x + y; } return g ; } int (*h)() = f(3); int (*j)() = f(4); int z = h(5); int w = j(7);

Compiler Implementation Hide machine dependent parts Hide language dependent part Use special modules

Basic Compiler Phases Source program (string).EXE lexical analysis syntax analysis semantic analysis Code generation Assembler/Linker Tokens Abstract syntax tree Assembly Frame manager

Hidden in the frame ADT Word size The location of the formals Frame resident variables Machine instructions to implement “shift-of- view” (prologue/epilogue) The number of locals “allocated” so far The label in which the machine code starts

Invocations to Frame “Allocate” a new frame “Allocate” new local variable Return the L-value of local variable Generate code for procedure invocation Generate prologue/epilogue Generate code for procedure return

Summary Stack frames provide a simple compile-time memory management scheme –Locality of references is supported Can be complex to implement Limits the duration of allocated objects Memory allocation is one of most interesting areas