DOC PREVIEW
UW-Madison CS 536 - Lecture 29

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

397CS 536 Spring 2008©Run-Time Data StructuresStatic StructuresFor static structures, a fixedaddress is used throughoutexecution.This is the oldest and simplestmemory organization.In current compilers, it is usedfor:• Program code (often read-only &sharable).• Data literals (often read-only &sharable).• Global variables.• Static variables.398CS 536 Spring 2008©Stack AllocationModern programming languagesallow recursion, which requiresdynamic allocation.Each recursive call allocates a newcopy of a routine’s local variables.The number of local dataallocations required duringprogram execution is not knownat compile-time.To implement recursion, all thedata space required for a methodis treated as a distinct data areathat is called a frame or activationrecord.Local data, within a frame, isaccessible only while asubprogram is active.399CS 536 Spring 2008©In mainstream languages like C,C++ and Java, subprograms mustreturn in a stack-like manner—themost recently called subprogramwill be the first to return.A frame is pushed onto a run-timestack when a method is called(activated).When it returns, the frame ispopped from the stack, freeingthe routine’s local data.As an example, consider thefollowing C subprogram:p(int a) {double b;double c[10];b = c[a] * 2.51;}400CS 536 Spring 2008©Procedure p requires space for theparameter a as well as the localvariables b and c.It also needs space for controlinformation, such as the returnaddress.The compiler records the spacerequirements of a method.The offset of each data itemrelative to the start of the frame isstored in the symbol table.The total amount of spaceneeded, and thus the size of theframe, is also recorded.Assume p’s control informationrequires 8 bytes (this size isusually the same for all methods).Assume parameter a requires 4bytes, local variable b requires 8bytes, and local array c requires80 bytes.401CS 536 Spring 2008©Many machines require that wordand doubleword data be aligned,so it is common to pad a frame sothat its size is a multiple of 4 or 8bytes.This guarantees that at all timesthe top of the stack is properlyaligned.Here is p’s frame:Control InformationSpace for aSpace for bSpace for cPaddingOffset = 0Offset = 8Offset = 12Offset = 20Total size= 104402CS 536 Spring 2008©Within p, each local data object isaddressed by its offset relative tothe start of the frame.This offset is a fixed constant,determined at compile-time.We normally store the start of theframe in a register, so each pieceof data can be addressed as a(Register, Offset) pair, which is astandard addressing mode inalmost all computer architectures.For example, if register R pointsto the beginning of p’s frame,variable b can be addressed as(R,12), with 12 actually beingadded to the contents of R at run-time, as memory addresses areevaluated.403CS 536 Spring 2008©Normally, the literal 2.51 ofprocedure p is not stored in p’sframe because the values of localdata that are stored in a framedisappear with it at the end of acall.It is easier and more efficient toallocate literals in a static area,often called a literal pool orconstant pool. Java uses aconstant pool to store literals,type, method and interfaceinformation as well as class andfield names.404CS 536 Spring 2008©Accessing Frames at Run-TimeDuring execution there can bemany frames on the stack. When aprocedure A calls a procedure B,aframe for B’s local variables ispushed on the stack, covering A’sframe. A’s frame can’t be poppedoff because A will resumeexecution after B returns.For recursive routines there canbe hundreds or even thousands offrames on the stack. All framesbut the topmost representsuspended subroutines, waitingfor a call to return.The topmost frame is active; it isimportant to access it directly.The active frame is at the top ofthe stack, so the stack topregister could be used to accessit.405CS 536 Spring 2008©The run-time stack may also beused to hold data other thanframes.It is unwise to require that thecurrently active frame always beat exactly the top of the stack.Instead a distinct register, oftencalled the frame pointer, is usedto access the current frame.This allows local variables to beaccessed directly as offset +frame pointer, using the indexedaddressing mode found on allmodern machines.406CS 536 Spring 2008©Consider the following recursivefunction that computes factorials.int fact(int n) {if (n > 1)return n * fact(n-1);elsereturn 1;}407CS 536 Spring 2008©The run-time stackcorresponding to the callfact(3) (when the call offact(1) is about to return) is:We place a slot for the function’sreturn value at the very beginningof the frame.Upon return, the return value isconveniently placed on the stack,just beyond the end of the caller’sframe. Often compilers returnscalar values in speciallyControl InformationSpace for n = 3Return ValueControl InformationSpace for n = 1Return Value = 1Control InformationSpace for n = 2Return ValueTop of StackFrame Pointer408CS 536 Spring 2008©designated registers, eliminatingunnecessary loads and stores. Forvalues too large to fit in a register(arrays or objects), the stack isused.When a method returns, its frameis popped from the stack and theframe pointer is reset to point tothe caller’s frame.In simple cases this is done byadjusting the frame pointer by thesize of the current frame.409CS 536 Spring 2008©Dynamic LinksBecause the stack may containmore than just frames (e.g.,function return values or registerssaved across calls), it is commonto save the caller’s frame pointeras part of the callee’s controlinformation.Each frame points to its caller’sframe on the stack. This pointer iscalled a dynamic link because itlinks a frame to its dynamic (run-time) predecessor.410CS 536 Spring 2008©The run-time stack correspondingto a call of fact(3), withdynamic links included, is:Dynamic Link = NullSpace for n = 3Return ValueDynamic LinkSpace forn = 1Return Value = 1Dynamic LinkSpace forn = 2Return ValueTop of StackFrame Pointer411CS 536 Spring 2008©Classes and ObjectsC, C++ and Java do not allowprocedures or methods to nest.A procedure may not be declaredwithin another procedure.This simplifies run-time dataaccess—all variables are eitherglobal or local.Global variables are staticallyallocated. Local variables are partof a single frame, accessedthrough the frame pointer.Java and C++ allow classes tohave member functions that havedirect access to instancevariables.412CS 536 Spring


View Full Document

UW-Madison CS 536 - Lecture 29

Download Lecture 29
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 29 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 29 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?