Stack Frame Linkage.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

The University of Adelaide, School of Computer Science
Procedures Procedures are very important for writing reusable and maintainable code in assembly and high-level languages. How are they implemented? Application.
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.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
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.
(1) ICS 313: Programming Language Theory 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:
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)
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Run-Time Storage Organization
Run time vs. Compile time
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
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.
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.
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.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
CSC 8505 Compiler Construction Runtime Environments.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
The Stack. ARMSim memory space: 0x Unused 0x x11400 Stack 0x x09400 Heap 0x?????-0x01400 Data 0x x????? Text 0x x01000.
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.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Procedures Procedures are very important for writing reusable and maintainable code in assembly and high-level languages. How are they implemented? Application.
Computer Architecture & Operations I
Storage Allocation Mechanisms
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
Computer Science 210 Computer Organization
Lecture 5: Procedure Calls
CSCI206 - Computer Organization & Programming
The Stack.
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
The Procedure Abstraction Part IV: Allocating Storage & Establishing Addressability Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights.
Chapter 9 :: Subroutines and Control Abstraction
CSCI206 - Computer Organization & Programming
Activation Records and Function Calls
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
MIPS Instructions.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
Topic 3-a Calling Convention 1/10/2019.
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
Topic 2b ISA Support for High-Level Languages
Runtime Stack Activation record for hanoi;
Presentation transcript:

Stack Frame Linkage

Memory Memory: Text Segment : Code Data Segment : Global variables / constants Stack Segment : Temporary storage

Memory Up Close Variables with global duration in .data:

Static Storage Static : Allocated at start of program Assembly static storage for "function":

Static Storage Static Pros: Simple

Static Storage Static Pros: Static Cons: Simple Allocated if we need or not Allocated for entire run of the program Only one copy of memory for function Non-reenterant

Local Variables Alternative: Stack based temporary storage as needed for functions

Local Variables Stack Frame / Activation Record: memory for one function call

Detail View Stack Frame Context for function Book keeping Parameters Local variables Book keeping Stored registers & Return address Start of previous frame -0x0 foo's frame -0x4 return address -0x8 num -0xC a

Local Variables Components located relative to bottom of frame -0x0 foo's frame -0x4 return address -0x8 num -0xC a +0xC num +0x8 a +0x4 return address +0x0 foo's frame

Local Variables Stack Pointer can move during function… not a fixed reference -0x0 foo's frame -0x4 return address -0x8 num -0xC a +0xC num +0x8 a +0x4 return address +0x0 foo's frame

Local Variables Frame Pointer Fixed reference point to bottom of current stack frame -0x0 foo's frame -0x4 return address -0x8 num -0xC a +0xC num +0x8 a +0x4 return address +0x0 foo's frame

What Goes In Registers we may mess up Parameters Local variables r4+ FP LR (if needed) Parameters Local variables

Updated Call Convention Caller Save r0-r3 if you care about them Put arguments in r0-r3, BL Prolog Save r4+ if you will use Push callers fp Push your lr Set frame pointer Move sp to make space on stack for locals/params Store params from registers to frame

Updated Call Convention Epilog Put return values in r0-r3 Reset stack pointer to remove locals Pop all saved registers Including fp and possibly lr Return (MOV PC, LR) Regain Control Get return values from r0 Pop any r0-r3 registers you saved

Sample Sample Function: Has two local variables Each is 4 bytes

Prolog Frame Setup:

Prolog Frame Setup: Push callers fp, your lr

Prolog Frame Setup: Push callers fp, your lr Push r4+ if you will use This function doesn't

Prolog Frame Setup: Push callers fp, your lr Push r4+ if you will use Set fp to start of this frame

Prolog Stack Frame Frame Setup: Push callers fp, your lr Push r4+ if you will use Set fp to start of this frame Allocate params/locals This needs 8 bytes

Prolog Frame Setup: Push callers fp, your lr Push r4+ if you will use Set fp to start of this frame Allocate params/locals Copy params to stack frame

Prolog Params / Locals accessed as fp + offset Ordering set by compiler

Using the Frame Every variable read/write must take place on stack Registers only temporary

Using the Frame Get value Do math Store result

Using the Frame Get value Do math Store result

Using the Frame Get value Do math Store result

Using the Frame Get value Do math Store result

Using the Frame Get value Do math Store result

Using the Frame Get value Do math Store result

Epilog Cleanup Put return in r0+

Epilog Cleanup Put return in r0+ Remove locals/params By moving stack pointer

Epilog Cleanup Put return in r0+ Remove locals/params Restore old fp and our lr Wipes out our fp

Epilog Cleanup Put return in r0+ Remove locals/params Restore old fp and our lr Return

Done to here

Function as Caller & Callee Functions call each other Lifecycle: Prolog Set up stack frame Do work Call others? Set up call Regain control Do Work Epilog Clean up stack frame Return

Call Stack closeEnough calls abs:

closeEnough closeEnough is called

closeEnough Builds frame, stores params

closeEnough Does work…

closeEnough Calls abs

abs closeEnough abs stores old fp with current lr

abs abs closeEnough closeEnough Now safe to update fp

abs abs makes space for x and loads param into it abs closeEnough

abs abs closeEnough closeEnough … after work is done, put x in r0

abs abs closeEnough closeEnough … pop locals/params

abs closeEnough closeEnough … popping fp retores pointer in old frame

Notes All variables allocated up front: Even if all can't be used!!!

Notes Arrays / struct / objects on stack allocated as block:

Real World Compilers may each build frames differently Compilers may omit steps if optimizing Skip saving link register if do not call other functions Skip saving frame pointer Skip loading/storing from stack Extra/large parameters placed on stack by caller

Recursion Recursive procedure Calls itself

Recursion Each call has own stack frame: fact(2) fact(3) fact(4)