Simpler Compiler: Stack Machines & Code Generation with Dr. Peisert's ECS 142 Lecture 20 -, Study Guides, Projects, Research of Computer Science

A lecture transcript from dr. Sean peisert's ecs 142 computer science course in spring 2009. The lecture focuses on stack machines, their optimization, and the use of stack machines in code generation. The lecture covers the concept of stack machines, their advantages, an example of a stack machine program, and the optimization of stack machines using an accumulator.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/30/2009

koofers-user-v68
koofers-user-v68 ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 31

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

Partial preview of the text

Download Simpler Compiler: Stack Machines & Code Generation with Dr. Peisert's ECS 142 Lecture 20 - and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Code Generation

Lecture 20 Dr. Sean Peisert โ€“ ECS 142 โ€“ Spring 2009 1

Status

1.5 weeks to go on project 3

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

2

Example of a Stack

Machine Program

Consider two instructions

push i โ€” place the int i on top of the stack

add โ€” pop two elements, add them, and put the result back on the stack

A program to compute 7 + 5:

push 7

push 5

add 4

Stack Machine. Example.

Each instruction

takes operands from the top of the stack

removes those operands from the stack

computes the required operation on them

pushes the result on the stack 5 ... ... ... ... 7 7 12 5

Why Use a Stack Machine?

  • Location of the operands is implicit (always on the top of the stack)
  • No need to specify operands explicitly
  • No need to specify the location of the result
  • Instruction โ€œaddโ€ as opposed to โ€œadd r1, r2โ€

Smaller encoding of instructions

More compact programs

This is one reason why Java Bytecodes use a stack evaluation model 7 

Optimizing the Stack Machine

  • The add instruction does 3 memory operations - Two reads and one write to the stack - The top of the stack is frequently accessed
  • Idea: keep the top of the stack in a register (called accumulator) - Register accesses are faster

The โ€œaddโ€ instruction is now: acc โ† acc + top_of_stack - Only one memory operation! 8

Stack Machine w/Accumulator. Example.

Compute 7 + 5 using an accumulator 10 ... ... ... ... 12 7 7

acc (^75) stack acc โ† 7 push acc acc โ† (^5) acc โ† acc + top of stack pop

A Bigger Example: 3 + (7 + 5)

Code acc โ† 3 push acc acc โ† 7 push acc acc โ† 5 acc โ† acc + top _ of_stack pop acc โ† acc + top _ of_stack pop Acc 3 3 7 7 5 12 12 15 15 Stack 3, 3, 7, 3, 7, 3, 7, 3, 3, 3, 11

From Stack Machines to MIPS

  • The compiler generates code for a stack machine with accumulator.
  • We want to run the resulting code on the MIPS processor (or simulator).
  • We implement stack machine instructions using MIPS instructions and registers. 13

Simulating a Stack Machine

  • The accumulator is kept in MIPS register $a
  • The stack is kept in memory
  • The stack grows toward lower addresses

Standard convention on the MIPS architecture

The address of the next location on the stack is kept in MIPS register $sp 

The top of the stack is at address $sp + 4. 14

MIPS Architecture

Prototypical Reduced Instruction Set Computer (RISC) architecture

Arithmetic operations use registers for operands and results

Must use load and store instructions to use operands and results in memory

32 general purpose registers (32 bits each)

We will use $sp, $a0, and $t1 (a temporary register)

Weโ€™ll give you a SPIM document for more details. 16

A Sample of MIPS Instructions

  • lw reg1 offset(reg2)

Load 32-bit word from address reg2 + offset into reg

add reg1 reg2 reg 

reg1 โ† reg2 + reg

sw reg1 offset(reg2) 

Store 32-bit word in reg1 at address reg2 + offset

addiu reg1 reg2 imm 

reg1 โ† reg2 + imm

โ€œuโ€ means overflow is not checked

li reg imm 

reg โ† imm 17

A Small Language

A language with integers and integer operations P โ†’D; P | D D โ†’def id(ARGS) = E; ARGS โ†’ id, ARGS | id E โ†’int | id | if E 1 = E 2 then E 3 else E 4 | E 1 + E 2 | E 1 โ€“E 2 | id(E 1 ,โ€ฆ,En) 19

A Small Language

The first function definition f is the โ€œmainโ€ routine

Running the program on input i means computing f(i)

Program for computing the Fibonacci numbers:

def fib(x) = if x = 1 then 0 else

if x = 2 then 1 else

fib(x -1) + fib(x - 2)

20