EECS 483: Storage Allocation & Management at UMich, Study notes of Electrical and Electronics Engineering

This document from the university of michigan covers the concepts of storage classes, storage class selection, distinct regions of memory, memory organization, variable binding, static allocation, heap allocation, accessing static/heap variables, run-time stack, stack pointers, and stack frame construction. It also includes examples and problem-solving exercises.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-s5o
koofers-user-s5o 🇺🇸

10 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Intermediate Representation II
Storage Allocation and
Management
EECS 483 – Lecture 18
University of Michigan
Wednesday, November 8, 2006
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download EECS 483: Storage Allocation & Management at UMich and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Intermediate Representation II Storage Allocation and Management

EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006

  • 1 -

2 Classes of Storage in Processor

Registers

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

y

Doesn’t support pointer access!

Memory

» Slow access, but large » Supports pointers

Storage class for each variable generallydetermined when map HIR to LIR

  • 3 -

4 Distinct Regions of Memory

Code space – Instructions to be executed

» Best if read-only

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

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

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

  • 4 -

Memory Organization

Code

Static Data

Stack^ Heap

Code and static data sizes determined by the compiler Stack and heap sizes vary at run-time

Stack grows downward Heap grows upward

Some ABI’s have stack/heap switched

  • 6 -

Variable Binding

Definitions:

» Environment – A function that maps a name to

a storage location

» State – A function that maps a storage location

to a value

When an environment associates a storagelocation S with a name N, we say N isbound to S

» If N is a composite type, then N might be

bound to a set of locations (usuallycontiguous, though not required)

  • 7 -

Static Allocation

Static storage has fixed allocation that isunchanged during program execution

Used for:

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

global lifetime!)

int count (int n) {

static int sum = 0; sum += n;

sum has local visibility coupled with a global lifetime

Æ

lots of bugs!

  • 9 -

Accessing Static/Heap Variables

Static

» Addresses are known to compiler

y

Assigned by linker

» Compiler backend uses symbolic names (labels)

y

Same for branch addresses

Heap

» Are unnamed locations » Can be accessed only by dereferencing variables

which hold their addresses

  • 10 -

Run-Time Stack

™

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)

™

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

  • 12 -

Why 2 Stack Pointers?

For fun – not quite!

Keep small offsets

» Instruction encoding limits sizes of literals that

can be encoded

Real reason

» Stack frame size not

always known at compiletime

» Example: alloca (dynamic

allocation on stack)

Prev Frame

Top Frame

FP SP

stackgrows

  • 13 -

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

Outgoing parameters Incoming parameters

SP
FP

Current frame responsibility of the callee

Previous frame responsibility of the caller

  • 15 -

Class Problem

For the following program: int foo(int a) {

int x; if (a = 1) return 1; x = foo(a-1) + foo(a-2); return (x);

} main() {

int y, z = 10; y = foo(z);

  1. Show the first 3 stack frames created when this program is executed (starting with main). 2. Whats the maximum number of frames the stack grows to during the execution of this program?
  • 16 -

Static Links

™

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

™

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

»

Defined away in C as C hasonly a 2 level binding

™

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

Previous FP

Local 1... Local n

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

Return address

SP
FP

Static link

  • 18 -

Calling Sequences

How to generate the code that builds theframes?

Generate code which pushes values onstack:

» Before call instructions (caller responsibilities) » At function entry (callee responsibilities)

Generate code which pops values off stack:

» After call instructions (caller responsibilities) » At return instructions (callee responsibilities)

  • 19 -

Push Values on Stack

™

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

™

Prologue = code at function entry

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