1 Topic 6: Activation Records COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer.

Slides:



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

CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedure call frame: Hold values passed to a procedure as arguments
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
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)
Runtime Environments Source language issues Storage organization
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
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:
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Recursion and Implementation of Functions
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.
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
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.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
CSC 8505 Compiler Construction Runtime Environments.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 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.
Implementing Subprograms
Computer Architecture & Operations I
Lecture 7 Macro Review Stack Frames
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Run-Time Environments Chapter 7
Implementing Subprograms
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
© Craig Zilles (adapted from slides by Howard Huang)
COMPILERS Activation Records
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Functions and Procedures
Implementing Subprograms
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
Activation Records and Function Calls
Stack Frame Linkage.
Implementing Subprograms
Understanding Program Address Space
Program and memory layout
Program and memory layout
Runtime Environments What is in the memory?.
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Procedure Support From previous study of high-level languages, we know the basic issues: - declaration: header, body, local variables - call and return.
Implementing Subprograms
Topic 2b ISA Support for High-Level Languages
Runtime Stack Activation record for hanoi;
Presentation transcript:

1 Topic 6: Activation Records COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer

2 Activation Records

3 The Stack holds local variables (and other data, see later) implemented as large array that typically grows downwards towards lower addresses, and shrinks upwards u sp Push(r1): stack_pointer --; M[stack_pointer] = r1; r1 = M[stack_pointer]; stack_pointer++; r1 = Pop(): v sp v u r1 u u v occasionally, we also need to access the previous activation record (ie frame of caller). Hence, simple push/pop insufficient. But: Solution: treat stack as array with index off of stack_pointer push/pop entire activation records

4 The Stack holds local variables (and other data, see later) implemented as large array that typically grows downwards towards lower addresses, and shrinks upwards u sp Push(r1): stack_pointer --; M[stack_pointer] = r1; r1 = M[stack_pointer]; stack_pointer++; r1 = Pop(): v sp v u r1 u u v occasionally, we also need to access the previous activation record (ie frame of caller). Hence, simple push/pop insufficient. But: Solution: treat stack as array with index off of stack_pointer push/pop entire activation records

5 The Stack holds local variables (and other data, see later) implemented as large array that typically grows downwards towards lower addresses, and shrinks upwards u sp Push(r1): stack_pointer --; M[stack_pointer] = r1; r1 = M[stack_pointer]; stack_pointer++; r1 = Pop(): v sp v u r1 u u v occasionally, we also need to access the previous activation record (ie frame of caller). Hence, simple push/pop insufficient. But: Solution: treat stack as array with index off of stack_pointer push/pop entire activation records

6 Example

7 Example

8 Example

9 Example

10 Example

11 Recursive Example

12 Recursive Example

13 Recursive Example

14 Recursive Example

15 Functional Languages

16 Functional Languages Step2

17 Functional Languages Step2 i.e. g(5)

18 Functional Languages Combination of nested functions and functions that are returned as results (i.e. higher-order) requires that local variable remain in existence even after enclosing function has returned activation records are allocated on heap (“closures”), not on the stack For now, focus on languages that use stack.

19 Stack Frame Organizations

20 Typical Stack Frame

21 Stack Frame Example

22 Stack Frame Example b1 b2 b3 push outgoing arguments; decrease SP

23 Stack Frame Example b1 b2 b3 push frame pointer to f’s frame make old SP the new FP for g update SP by subtracting size(g)

24 Stack Frame Example b1 b2 b3 b1/Garbage b2/Garbage restore f’s SP by setting it to g’s FP restore f’s FP by following g’s dynamic link, now located at SP-1 pop the arguments by incrementing SP

25 Parameter Passing in memory a register argument has its address taken, Solution: space is reserved by caller, but only written to by callee, and only if necessary

26 Parameter Passing

27 Registers

28 Registers

29 Registers

30 Return Address and Return Value

31 Frame Resident Variables and must hence be held in memory if

32 Static Links

33 Static Links

34 Static Links: nonrecursive call Dynamic links point to FP of caller Static links point to FP of surrounding function’s most recent instance a=5 Dynamic Link &b = M[FP]-2 &a = M[M[FP]]-2

35 Static Links: recursive call Dynamic links point to FP of caller Static links point to FP of surrounding function’s most recent instance a=5 Dynamic Link &b = M[FP]-2 &a = M[M[FP]]-2

36 Static Links dynamic link still needed to restore caller’s FP during function return offsets on slides need to be modified by +/- 1 to account for the extra slot used by the static link.