






























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!































































Code Generation (I)
Lecture Outline
Example of a Stack Machine Program
push 7 push 5 add
Stack Machine. Example
stack …
5 7 …
push 5
12 …
…
push 7
7
add
Why Use a Stack Machine?
Optimizing the Stack Machine
acc acc + top_of_stack
Stack Machine with Accumulator. Example
…
acc
stack
5
7 …
acc 5
12
…
acc acc + top_of_stack pop
…
7
acc 7 push acc
7
A Bigger Example: 3 + (7 + 5)
Code Acc Stack acc 3 3
From Stack Machines to MIPS
Simulating a Stack Machine…
A Sample of MIPS Instructions
MIPS Assembly. Example.
acc 7 push acc
acc 5 acc acc + top_of_stack
pop
li $a0 7 sw $a0 0($sp) addiu $sp $sp - li $a0 5 lw $t1 4($sp) add $a0 $a0 $t addiu $sp $sp 4
A Small Language
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)
A Small Language (Cont.)
def fib(x) = if x = 1 then 0 else if x = 2 then 1 else fib(x - 1) + fib(x – 2)