CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.

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.
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
(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.
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
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:
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)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
ISBN Chapter 10 Implementing Subprograms.
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:
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
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.
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.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
CPSC 388 – Compiler Design and Construction Runtime Environments.
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CSC 8505 Compiler Construction Runtime Environments.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
7. Runtime Environments Zhang Zhizheng
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
© 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.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
1 Topic 6: Activation Records COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer.
Computer Architecture & Operations I
Storage Allocation Mechanisms
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 Chapter 10
COMPILERS Activation Records
Run-Time Storage Organization
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Functions and Procedures
Run-Time Environments
Chapter 9 :: Subroutines and Control Abstraction
Activation Records and Function Calls
Stack Frame Linkage.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
Run-Time Environments
UNIT V Run Time Environments.
Runtime Environments What is in the memory?.
Program and memory layout
Run-time environments
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras

CSE 5317/4305 L7: Run-Time Storage Organization2 Memory Layout Memory layout of an executable program:

CSE 5317/4305 L7: Run-Time Storage Organization3 Run-Time Stack At run-time, function calls behave in a stack-like manner –when you call, you push the return address onto the run-time stack –when you return, you pop the return address from the stack –reason: a function may be recursive When you call a function, inside the function body, you want to be able to access –formal parameters –variables local to the function –variables belonging to an enclosing function (for nested functions) procedure P ( c: integer ) x: integer; procedure Q ( a, b: integer ) i, j: integer; begin x := x+a+j; end; begin Q(x,c); end;

CSE 5317/4305 L7: Run-Time Storage Organization4 Activation Records (Frames) When we call a function, we push an entire frame onto the stack The frame contains –the return address from the function –the values of the local variables –temporary workspace –... The size of a frame is not fixed –need to chain together frames into a list (via dynamic link) –need to be able to access the variables of the enclosing functions efficiently A B C top

CSE 5317/4305 L7: Run-Time Storage Organization5 A Typical Frame Organization

CSE 5317/4305 L7: Run-Time Storage Organization6 Static Links The static link of a function f points to the latest frame in the stack of the function that statically contains f –If f is not lexically contained in any other function, its static link is null procedure P ( c: integer ) x: integer; procedure Q ( a, b: integer ) i, j: integer; begin x := x+a+j; end; begin Q(x,c); end; If P called Q then the static link of Q will point to the latest frame of P in the stack Note that –we may have multiple frames of P in the stack; Q will point to the latest –there is no way to call Q if there is no P frame in the stack, since Q is hidden outside P in the program

CSE 5317/4305 L7: Run-Time Storage Organization7 The Code for Function Calls When a function (the caller) calls another function (the callee), it executes the following code: –pre-call: do before the function call allocate the callee frame on top of the stack evaluate and store function parameters in registers or in the stack store the return address to the caller in a register or in the stack –post-call: do after the function call copy the return value deallocate (pop-out) the callee frame restore parameters if they passed by reference

CSE 5317/4305 L7: Run-Time Storage Organization8 The Code for Function Calls (cont.) In addition, each function has the following code: –prologue: to do at the beginning of the function body store frame pointer in the stack or in a display set the frame pointer to be the top of the stack store static link in the stack or in the display initialize local variables –epilogue: to do at the end of the function body store the return value in the stack restore frame pointer return to the caller

CSE 5317/4305 L7: Run-Time Storage Organization9 Storage Allocation We can classify the variables in a program into four categories: 1)statically allocated data that reside in the static data part of the program –these are the global variables. 2)dynamically allocated data that reside in the heap –these are the data created by malloc in C 3)register allocated variables that reside in the CPU registers –these can be function arguments, function return values, or local variables 4)frame-resident variables that reside in the run-time stack –these can be function arguments, function return values, or local variables

CSE 5317/4305 L7: Run-Time Storage Organization10 Frame-Resident Variables Every frame-resident variable (ie. a local variable) can be viewed as a pair of (level,offset) –the variable level indicates the lexical level in which this variable is defined –the offset is the location of the variable value in the run-time stack relative to the frame pointer procedure P ( c: integer ) x: integer; procedure Q ( a, b: integer ) i, j: integer; begin x := x+a+j; end; begin Q(x,c); end; level offset a 2 8 b 2 4 i j c 1 4 x level 2 level 1

CSE 5317/4305 L7: Run-Time Storage Organization11 Variable Offsets procedure P ( c: integer ) x: integer; procedure Q ( a, b: integer ) i, j: integer; begin x := x+a+j; end; begin Q(x,c); end;

CSE 5317/4305 L7: Run-Time Storage Organization12 Accessing a Variable Let $fp be the frame pointer You are generating code for the body of a function at the level L1 For a variable with (level,offset)=(L2,O) you generate code: 1)traverse the static link (at offset -8) L1-L2 times to get the containing frame 2)accesss the location at the offset O in the containing frame eg, for L1=5, L2=2, and O=-16, we have –Mem[Mem[Mem[Mem[$fp-8]-8]-8]-16] eg: a: Mem[$fp+8] b: Mem[$fp+4] i: Mem[$fp-12] j: Mem[$fp-16] c: Mem[Mem[$fp-8]+4] x: Mem[Mem[$fp-8]-12] level offset a 2 8 b 2 4 i j c 1 4 x 1 -12

CSE 5317/4305 L7: Run-Time Storage Organization13 The Code for the Call Q(x,c) Mem[$sp] = Mem[$fp-12]; push x $sp = $sp-4 Mem[$sp] = Mem[$fp+4]; push c $sp = $sp-4 static_link = $fp call Q $sp = $sp+8; pop arguments

CSE 5317/4305 L7: Run-Time Storage Organization14 The Code for a Function Body Prologue: Mem[$sp] = $fp; store $fp $fp = $sp; new beginning of frame $sp = $sp+frame_size; create frame save return_address save static_link Epilogue: restore return_address $sp = $fp; pop frame $fp = Mem[$fp]; follow dynamic link return using the return_address

CSE 5317/4305 L7: Run-Time Storage Organization15 Finding Static Link The caller set the static_link of the callee before the call –this is because the caller knows both the caller and callee –the callee doesn't know the caller Suppose that L1 and L2 are the nesting levels of the caller and the callee procedures –When the callee is lexically inside the caller's body, that is, when L2=L1+1, we have: static_link = $fp –Otherwise, we follow the static link of the caller L1-L2+1 times For L1=L2, that is, when both caller and callee are at the same level, we have static_link = Mem[$fp-8] For L1=L2+2 we have static_link = Mem[Mem[Mem[$fp-8]-8]-8]

CSE 5317/4305 L7: Run-Time Storage Organization16 Finding Static Link (cont.)