Docsity

Code Generation - Compilers - Lecture Slides | CS 414, Study notes of Computer Science

Partial preview of the text

Download Code Generation - Compilers - Lecture Slides | CS 414 and more Study notes Computer Science in PDF only on Docsity!

Compilers

CS414-2008S-09Code Generation

David Galles Department of Computer Science University of San Francisco

Code Generation

Next Step: Create actual assembly code. Use a tree tiling strategy:

Create a set of tiles with associated assemblycode. Cover the AST with these tiles. Output the code associated with each tiles.

As long as we are clever about the codeassociated with each tile, and how we tile the tree,we will create correct actual assembly.

MIPS

Instruction Description lw rt,

offset

(base) Add the constant value

offset

to the register base to get an address. Load the contents of this address into the register rt

rt = M[base +

offset

]

sw rt,

offset

(base) Add the constant value

offset

to the register base to get an address. Store the contents of rt into this address. M[base +

offset

] = rt add rd, rs, rt Add contents of registers rs and rt, put result in register rd

MIPS

Instruction Description sub rd, rs, rt Subtract contents of register rt from rs, put result in register rd addi rt, rs,

val

Add the constant value

val

to register rs put result in register rt mult rs, rt Multiply contents of register rs by register rt, put the low order bits in register LO, and the high bits in register HI div rs, rt Divide contents of register rs by register rt, put the quotient in register LO, and the remainder in register HI

MIPS

Instruction Description slt rd, rs, rt if rs

rt, rd

1, else rd

beq rs, rt,

target

if rs

rt, jump to the label

target

bne rs, rt,

target

if rs

rt, jump to the label

target

blez rs,

target

if rs

0, jump to label

target

bgtz rs,

target

if rs

0, jump to label

target

bltz rs,

target

if rs

0, jump to the label

target

bgez rs,

target

if rs

0, jump to the label

target

Registers

MIPS processors use 32 different registers We will only use a subset for this project

(Though you can increase the number ofregisters used for temporary values fairly easily)

Registers

Mnemonic

SPIM

Description Name Name $return $ra Return Register – Holds the return address for the current function $zero $zero Zero Register – This register always has the value 0 $ACC $t Accumulator Register – Used for calculating the value of expressions $t $t General Purpose Register $t $t General Purpose Register $t $t General Purpose Register

Expression Stack

Long expressions – a * b + c * d * foo(x) – willrequire us to store several temporary values Since expressions can be arbitrarily long, we willneed to store an unlimited number of partialsolutions

Can’t always use registers – not enough ofthem Use a stack instead

Tree Tilings

We will explore several different tree tilingstrategies:

Simple tiling, that is easy to understand butproduces inefficient code More complex tiling, that relies less on theexpression stack Modifications to complex tilings, to increaseefficiency

Simple Tiling

Based on a post-order traversal of the tree Cover the tree with tiles

Each tile associated with actual assembly

Emit the code associated with the tiles in aleft-to-right, post-order traversal of the tree

Expression Trees

Constant Expressions

Constant(5)

Push the constant 5 on the top of theexpression stack

Expression Trees

Constant Expressions

How can we push the constant

x

on top of the

expression stack, using MIPS assembly?

Expression Trees

Arithmetic Binary Operations

Constant(3)

Constant(4)

Instead of using a single tile to cover this tree, wewill use three.

Expression Trees

Arithmetic Binary Operations

Constant(3) Constant(4)

What should the code for the + tile be?

Code for entire tree needs to push

just

the final

sum on the stack Code for Constant Expressions push constantvalues on top of the stack