Docsity

Classes of Storage in Processor - Compiler Construction - Lecture Notes | EECS 483, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Classes of Storage in Processor - Compiler Construction - Lecture Notes | EECS 483 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Intermediate Representation II Storage Allocation andManagement

EECS 483 – Lecture 15 University of Michigan Monday, November 3, 2003

  • 1 -

2 Classes of Storage in Processor

Y

Registers

» Fast access, but only a few of them » Address space not visible to programmer

y

Doesn’t support pointer access!

Y

Memory

» Slow access, but large » Supports pointers

Y

Storage class for each variable generallydetermined when map HIR to LIR

  • 3 -

4 Distinct Regions of Memory

Y

Code space – Instructions to be executed

» Best if read-only

Y

Static (or Global) – Variables that retaintheir value over the lifetime of the program

Y

Stack – Variables that is only as long as theblock within which they are defined (local)

Y

Heap – Variables that are defined by callsto the system storage allocator (malloc,new)

  • 4 -

Memory Organization

Code

Code and static data sizes determined by the compiler

Static Data

Stack and heap sizes vary at run-time

Stack grows downward Heap grows upward

Some ABI’s have these 2 areas switched

Stack

Heap

  • 6 -

Static Allocation

Y

Static storage has fixed allocation that isunchanged during program execution

Y

Used for:

» Global variables » Constants » All static variables in C have this (hence a

global lifetime!)

sum has local visibility coupled with a global lifetime

Æ

lots of bugs!

int count (int n) {

static int sum = 0; sum += n;

  • 7 -

Heap Allocation

Y

Parcels out pieces of continuous storage

Y

Pieces may be deallocated in any order

» Over time, heap will consist of alternate areas

of free and in-use sections of memory

Y

Heap is global

» Items exist until explicitly freed » Or if support garbage collection, until no-one

points to the piece of memory

» Heap management is for the OS people to

worry about! (e.g., first-fit, best-fit, ...)

  • 9 -

Run-Time Stack

Y

A frame (or activation record) for each functionexecution

» Represents execution environment of the function

y

Per invocation! Recursive function – each dynamic call hasits own frame

» Includes: local variables, parameters, return value,

temporary storage (register spill)

Y

Run-time stack of frames

» Push frame of f on stack when program calls f » Pop stack frame when f returns » Top frame = fame of currently executing function

  • 10 -

Stack Pointers

Y

Assuming stack grows downwards

» Address of top of stack increases

Y

Values of current frame accessed using 2pointers

» Stack pointer (SP):

points to frame top

» Frame pointer (FP):

points to frame base

» Variable access: use

offset from fp (sp)

Prev Frame

Top Frame

FP SP

stackgrows

  • 12 -

Anatomy of a Stack Frame

Param 1... Param n

Return address^ Previous FP

Local 1... Local n

Temp 1... Temp nParam 1... Param n

Return address

Previous frame responsibility of the caller

Incoming parameters

FP

Current frame responsibility of the callee

Outgoing parameters

SP
  • 13 -

Stack Frame Construction Example

int f(int a) {

int b, c;

} void g(int a) {

int b, c; ... b = f(a+c); ...

} main() {

int a, b; ... g(a+b); ...

}

FP for g

ret addr to g

b c

FP for main

ret addr to main

b c a + c

a b

... a + b

local var

main

local var parameter

g

local var local var parameter

f

local var

Note: I have left out the temp part of the stack frame

local var

  • 15 -

Static Links

Y

Problem for languages withnested functions (Pascal,ML): How do we accesslocal variables from otherframes?

Y

Need a static link: a pointerto the frame of the enclosingfunction

»

Defined away in C as C hasonly a 2 level binding

Y

Previous FP = dynamic link,ie, pointer to the previousframe in the currentexecution

FP

Previous FP

Local 1... Local n

Temp 1... Temp nParam 1... Param n

Return address

Static link

SP
  • 16 -

Saving Registers

Y

Problem: Execution of invoked functionmay overwrite useful values in registers

Y

Generated code must:

» Save registers when function is invoked » Restore registers when function returns

Y

Possibilities

» Callee saves/restores registers » Caller saves/restores registers » Split up the job, ie both do part of it

  • 18 -

Push Values on Stack

Y

Code before call instruction

» Push each actual parameter » Push caller-saved registers » Push static link (if necessary) » Push return address (current PC) and jump to caller

code

Y

Prologue = code at function entry

» Push dynamic link (ie FP) » Old stack pointer becomes new frame pointer » Push callee-saved registers » Push local variables

  • 19 -

Pop Values from Stack

Y

Epilogue = code at return instruction

» Pop (restore) callee-saved registers » Store return value at appropriate place » Restore old stack pointer (pop callee frame) » Pop old frame pointer » Pop return address and jump to that address

Y

Code after call

» Pop (restore) caller-saved registers » Use return value