Activation Records and Code Generation in Compilers - Prof. Sean P. Peisert, Study Guides, Projects, Research of Computer Science

A lecture note from ecs 142 - computer organization course taught by dr. Sean peisert during spring 2009. It covers the concept of activation records and their role in code generation during compiler development. The lecture explains how the activation record is designed together with the code generator, the stack discipline that guarantees the same pointer value on function exit, and the use of a frame pointer to find variables. The document also discusses the advantages and disadvantages of stack machines and proposes a better way to keep temporaries in the activation record.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 09/17/2009

koofers-user-0d8
koofers-user-0d8 🇺🇸

10 documents

1 / 49

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31

Partial preview of the text

Download Activation Records and Code Generation in Compilers - Prof. Sean P. Peisert and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Code Generation

Lecture 21 Dr. Sean Peisert – ECS 142 – Spring 2009 1

Status

1 week to go on project 3

Read Sec. 7.1 through 7.4 by Monday, May 18 2

The Activation Record

The stack discipline guarantees that on function exit $sp is the same as it was on function entry

No need to save $sp

We need the return address

It’s handy to have a pointer to start of the current activation

This pointer lives in register $fp (frame pointer)

Reason for frame pointer will be clear shortly 4

The Activation Record

Summary: for this language, an AR with the caller’s frame pointer, the actual parameters, and the return address suffices.

Picture: consider a call to f(x,y), the AR will be: 5 old FP y x High Addresses

FP
SP

AR of f Low Addresses

Calling Sequence for f(x,y) 7

FP
SP

old FP y x

FP
SP
FP
SP
FP
SP

old FP y x return Before call On entry Before exit After call

Code Generation for Variables 8

Variable references are the last construct

The “variables” of a function are just its parameters

They are all in the AR

Pushed by the caller

Problem: Because the stack grows when intermediate results are saved, the variables are not at a fixed offset from $sp

Code Generation for Variables 10

Example: For a function def f(x 1 ,x 2 ) = e the activation and frame pointer are set up as follows: FP SP old FP x 2 x 1 return x 1 is at fp + 4 x 2 is at fp + 8 Thus: where z = 4 * i cgen(xi) = lw $a0 z($fp)

Summary

The activation record must be designed together with the code generator

Code generation can be done by recursive traversal of the AST

We recommend you use a stack machine for your Cool compiler (it’s simple) 11

Stack Machine Pros & Cons

The stack machine has activation records and intermediate results interleaved on the stack

Advantage: very simple code generation

Disadvantage: Very slow code

Storing/loading temporaries requires a store/load and $sp adjustment 13 AR Intermediates AR Intermediates

A Better Way

Keep Temporaries in the AR

Code generator must assign a location in the AR for each temporary. 14

How Many Temporaries?

  • Let NT(e)= # of temps needed to evaluate e
  • NT(e 1 + e 2 )

Needs at least as many temporaries as NT(e 1 )

Needs at least as many temporaries as NT(e 2 ) + 1

Space used for temporaries in e 1 can be reused for temporaries in e 2 16 

The Equations

NT(e 1 + e 2 ) = max(NT(e 1 ), 1 + NT(e 2 )) NT(e 1 - e 2 ) = max(NT(e 1 ), 1 + NT(e 2 )) NT(if e 1 = e 2 then e 3 else e 4 ) = max(NT(e 1 ), 1 + NT(e 2 ), NT(e 3 ), NT(e 4 )) NT(id(e 1 ,…,en) = max(NT(e 1 ),…,NT(en)) NT(int) = 0 NT(id) = 0 Exercise: Is this bottom-up or top-down? Exercise: What is NT(…code for fib…)? 17

Picture

19 Old FP xn ... x 1 Return Addr. Temp NT(e) ... Temp 1

Revised Code Generation 20

  • Code generation must know how many temporaries to use at each point
  • Add a new argument to code generation: the position of the next available temporary.