DOC PREVIEW
UW-Madison CS 536 - Lecture 30

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 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 19 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 19 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 19 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 19 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 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

394CS 536 Spring 2007©Virtual Memory & Run-TimeMemory OrganizationThe compiler decides how dataand instructions are placed inmemory.It uses an address space providedby the hardware and operatingsystem.This address space is usuallyvirtual—the hardware andoperating system mapinstruction-level addresses to“actual” memory addresses.Virtual memory allows:• Multiple processes to run inprivate, protected address spaces.• Paging can be used to extendaddress ranges beyond actualmemory limits.395CS 536 Spring 2007©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.396CS 536 Spring 2007©Reading AssignmentRead Chapter 11 of Crafting aCompiler featuring Java.(Available from DoIt Tech Store)397CS 536 Spring 2007©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.398CS 536 Spring 2007©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;}399CS 536 Spring 2007©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 beginning of theframe is stored in the symboltable.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 8400CS 536 Spring 2007©bytes, and local array c requires80 bytes.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.401CS 536 Spring 2007©Here is p’s frame: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.Control InformationSpace for aSpace for bSpace for cPaddingOffset = 0Offset = 8Offset = 12Offset = 20Total size= 104402CS 536 Spring 2007©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.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.403CS 536 Spring 2007©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 be able to access itdirectly.The active frame is at the top ofthe stack, so the stack top404CS 536 Spring 2007©register could be used to accessit.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.Consider the following recursivefunction that computes factorials.405CS 536 Spring 2007©int fact(int n) {if (n > 1)return n * fact(n-1);elsereturn 1;}The run-time stackcorresponding to the callfact(3) (when the call offact(1) is about to return) is:Control InformationSpace for n = 3Return ValueControl InformationSpace for n = 1Return Value = 1Control InformationSpace for n = 2Return ValueTop of StackFrame Pointer406CS 536 Spring 2007©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 speciallydesignated 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.407CS 536 Spring 2007©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.408CS 536 Spring 2007©The run-time stack correspondingto a call of fact(3), withdynamic links included, is:Dynamic Link =


View Full Document

UW-Madison CS 536 - Lecture 30

Download Lecture 30
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 30 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 30 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?