Docsity

Where to Store Variables - Code Generation Part 2 | CS 322, Study notes of Computer Science

Toggle sidebar
bg1
1
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Where to Store Variables?
Static Allocation
Variables created at compile-time
Size and address known at compile-time
Stack Allocation
Variables placed in activation records on a stack
Variables are created / destroyed in LIFO order
Heap Allocation
Size and address determined at run-time
Creation / destruction of data occurs in any order
2
CS-322 Code Generation-Part 2
Ā© Harry H. Porter, 2006
Static Allocation
Early Languages (FORTRAN)
Each variable is placed in memory (ā€œstatic allocationā€)
Fortran had routines, but...
•!No stack
• Recursion was not possible
Values of a routine’s variables are retained across invocations
Initialization vs. re-initialization
Each variable’s size must be known at compile-time
Dynamic arrays?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Where to Store Variables - Code Generation Part 2 | CS 322 and more Study notes Computer Science in PDF only on Docsity!

Ā© Harry H. Porter, 2006

Where to Store Variables?

Static Allocation

Variables created at compile-time

Size and address known at compile-time

Stack Allocation

Variables placed in activation records on a stack

Variables are created / destroyed in LIFO order

Heap Allocation

Size and address determined at run-time

Creation / destruction of data occurs in any order

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Static Allocation

Early Languages (FORTRAN)

Each variable is placed in memory (ā€œstatic allocationā€)

Fortran had routines, but...

  • !No stack
  • Recursion was not possible

Values of a routine’s variables are retained across invocations

Initialization vs. re-initialization

Each variable’s size must be known at compile-time

Dynamic arrays?

Ā© Harry H. Porter, 2006

Stack Allocation

Each variable is ā€œ local ā€ to some routine

Invoke a routine?

Allocate storage for its variables

(and initialize it?)

Return?

Pop frame

(Variables are destroyed)

Consider one routine (e.g., ā€œquicksortā€)

Many activations, many frames

! Many copies of each local variable

Local variables:

Each invocation has its own set of variables

The ā€œcurrently activeā€ invocation

Its variables will be in the frame on top of stack.

Every reference to a local variable...

will access data in the top frame

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

References to a local variable in the currently active routine...

%sp

%fp

StackTop

General Idea

In the SPARC

*(StackTop + offset X

) ld [%fp-48],%l

Ā© Harry H. Porter, 2006

Laying Out the Frame

Each local (and temp) variable has a size

ā€œCā€ int: 4 bytes,

double: 8 bytes, ....

ā€œPCATā€ all variables: 4 bytes

Each local and temp variable needs an ā€œ offset ā€

for each procedure (or block) do

offset = 0;

for each local and temp variable do

assign this variable to current offset

offset = offset + this variable’s length

endFor

endFor

We’ll treat ā€œmainā€ body as just another routine.

It will have a frame

Global variables

Treat identically to local variables for procedures!

May start at some other value ( -4 )

We’ll use

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Example

Variable Size Offset

w 4 0

x 8 4

y 4 12

z 1 16

a 8 17

b 1 25

c 8 26

Ignoring alignment issues

Ā© Harry H. Porter, 2006

Example

Variable Size Offset Offset with Alignment

w 4 0 0

x 8 4 8

y 4 12 16

z 1 16 20

a 8 17 24

b 1 25 32

c 8 26 40

4 bytes of padding

3 bytes of padding

7 bytes of padding

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Example

Re-order!

Place variables with

most restrictive alignment

first.

Variable Size Offset Offset with Alignment

w 4 0 0

x 8 4 8

y 4 12 16

z 1 16 20

a 8 17 24

b 1 25 32

c 8 26 40

4 bytes of padding

3 bytes of padding

7 bytes of padding

Variable Size Offset

x 8 0

a 8 8

c 8 16

w 4 24

y 4 28

z 1 32

b 1 33

Ā© Harry H. Porter, 2006

The Dynamic Link

When we return, we need to be able to go back to previous frame.

During a call:

  • !Save old StackTop
  • !Increment StackTop

(Allocate new frame)

  • !Store old StackTop

in Dynamic Link field

During a return:

  • !Use the Dynamic Link to restore

the old StackTop

To access local variables:

[StackTop + offset X

]

To access variables in our caller’s frame:

[[StackTop + offset DynamicLink

] + offset X

]

What do we need from the caller’s frame?

  • !Arguments?
  • !Place to store a returned result?

•••

StackTop

The Static Link

used to access

non-local variables

(to be discussed later)

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

SPARC

Much of the Activation Record is cached in registers!!!

Dynamic Link

Stored in registers (%sp, %fp)

Return Address

Stored in registers (%i7, %o7)

Arguments

Some in registers %i0 ... %i

Rest in caller’s frame

Returned Value

32-bits: in register (%i0, %o0)

Machine Status

64 bytes

Architecture dependent & often not needed

•••

%sp

%fp

hidden register

hidden register

hidden register

Ā© Harry H. Porter, 2006

The ā€œCalling Sequenceā€

  • !Compute argument values
  • Allocate new frame
  • !Initialize it
    • Move arguments into the new frame (optional)
    • Save machine state (optional)
    • Save return address
  • Transfer control to new routine

The ā€œReturn Sequenceā€

  • !Compute and move return value (optional)
  • Pop stack / delete the top frame
  • Resume execution in the caller’s code

Flexibility as to who...

**_- !caller

  • callee_**

...does what...

**_- calling sequence

  • return sequence_**

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Caller’s Code: Callee’s Code:

...

...

...

AAA

AAA

AAA

DDD

DDD

DDD

...

...

...

BBB

BBB

BBB

...

...

...

CCC

CCC

CCC

...

...

...

call foo

...

...

...

foo: ...

...

...

...

ret

Calling Sequence

Return Sequence

Ā© Harry H. Porter, 2006

Parameter Passing in SPARC

Caller

ā€œfooā€

Callee

ā€œbarā€

fp+

fp+

...

Arguments to ā€œbarā€

at known offset in

the caller’s frame

fp

sp

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Using a Stack for Expression Evaluation

Source Code:

x := y + (2 * z);

Target Code:

push y

push 2

push z

mult

add

pop x

Ā© Harry H. Porter, 2006

Using a Stack for Expression Evaluation

Source Code:

x := y + (2 * z);

Target Code:

push y

push 2

push z

mult

add

pop x

Frame, with

local variables

TOP

FP

Used for expression

evaluation

x:

y:

z:

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Using a Stack for

Argument Evaluation and Parameter Passing

Foo’s Frame

TOP

FP

Calling Sequence:

Push args onto the stack

Save FP

FP = TOP

TOP = TOP + FrameSize

Return Sequence:

Move return value to where arg 1 was

Restore TOP, FP

Pop stack top into...

Ā© Harry H. Porter, 2006

Using a Stack for

Argument Evaluation and Parameter Passing

Foo’s Frame

TOP

FP

Result

Calling Sequence:

Push args onto the stack

Save FP

FP = TOP

TOP = TOP + FrameSize

Return Sequence:

Move return value to where arg 1 was

Restore TOP, FP

Pop stack top into...

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Using a Stack for

Argument Evaluation and Parameter Passing

Source Code:

x := y + 2 * (bar(7,i+1));

Target Code:

push y

push 2

push 7

push i

push 1

add

call bar

mult

add

pop x

Foo’s Frame

TOP

FP

x:

y: 30

z: 40

i: 50

Ā© Harry H. Porter, 2006

Using a Stack for

Argument Evaluation and Parameter Passing

Source Code:

x := y + 2 * (bar(7,i+1));

Target Code:

push y

push 2

push 7

push i

push 1

add

call bar

mult

add

pop x

Foo’s Frame

TOP 30

FP

x:

y: 30

z: 40

i: 50

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Using a Stack for

Argument Evaluation and Parameter Passing

Source Code:

x := y + 2 * (bar(7,i+1));

Target Code:

push y

push 2

push 7

push i

push 1

add

call bar

mult

add

pop x

Foo’s Frame

TOP

FP

x:

y: 30

z: 40

i: 50

Ā© Harry H. Porter, 2006

Using a Stack for

Argument Evaluation and Parameter Passing

Source Code:

x := y + 2 * (bar(7,i+1));

Target Code:

push y

push 2

push 7

push i

push 1

add

call bar

mult

add

pop x

Foo’s Frame

TOP

FP

x:

y: 30

z: 40

i: 50

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Using a Stack for

Argument Evaluation and Parameter Passing

Source Code:

x := y + 2 * (bar(7,i+1));

Target Code:

push y

push 2

push 7

push i

push 1

add

call bar

mult

add

pop x

Foo’s Frame

FP

x:

y: 30

z: 40

i: 50

TOP 51

Ā© Harry H. Porter, 2006

Using a Stack for

Argument Evaluation and Parameter Passing

Source Code:

x := y + 2 * (bar(7,i+1));

Target Code:

push y

push 2

push 7

push i

push 1

add

call bar

mult

add

pop x

Foo’s Frame

FP

x:

y: 30

z: 40

i: 50

TOP

Bar’s Frame

Waiting

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Using a Stack for

Argument Evaluation and Parameter Passing

Source Code:

x := y + 2 * (bar(7,i+1));

Target Code:

push y

push 2

push 7

push i

push 1

add

call bar

mult

add

pop x

Foo’s Frame

FP

x:

y: 30

z: 40

i: 50

TOP 3

Result

from Bar

Ā© Harry H. Porter, 2006

Using a Stack for

Argument Evaluation and Parameter Passing

Source Code:

x := y + 2 * (bar(7,i+1));

Target Code:

push y

push 2

push 7

push i

push 1

add

call bar

mult

add

pop x

Foo’s Frame

FP

x: 36

y: 30

z: 40

i: 50

TOP

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Variable-Length Local Variables

Goal:

Allow a routine to have variable-length data

(i.e., dynamically-sized arrays) as local data in frame

Ā© Harry H. Porter, 2006

Variable-Length Local Variables

Goal:

Allow a routine to have variable-length data

(i.e., dynamically-sized arrays) as local data in frame

Option 1:

Allocate the variable on the heap

Work with pointers to the data

PCAT: Hide the pointers from the programmer

Programmer codes:

a[i]

Compiler produces code like this:

(a + 4i)

Auto free the data when the routine returns?

CS-322 Code Generation-Part 2

Ā© Harry H. Porter, 2006

Variable-Length Local Variables

Goal:

Allow a routine to have variable-length data

(i.e., dynamically-sized arrays) as local data in frame

Option 1:

Allocate the variable on the heap

Work with pointers to the data

PCAT: Hide the pointers from the programmer

Programmer codes:

a[i]

Compiler produces code like this:

(a + 4i)

Auto free the data when the routine returns?

Option 2:

Create the variable on the stack, dynamically

Effectively: Enlarge the frame as necessary

Still need to work with pointers