Lecture 12
Lecture 12
CS143
Lecture 12
2
From Stack Machines to MIPS
3
Simulating a Stack Machine…
4
MIPS Assembly
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)
5
A Sample of MIPS Instructions
– lw reg1 offset(reg2)
• Load 32-bit word from address reg2 + offset into reg1
– add reg1 reg2 reg3
• reg1 ← reg2 + reg3
– 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
6
MIPS Assembly. Example.
P → D; P | D
D → def id(ARGS) = E;
ARGS → id, ARGS | id
E → int | id | if E1 = E2 then E3 else E4
| E1 + E2 | E1 – E2 | id(E1,…,En)
8
A Small Language (Cont.)
9
Code Generation Strategy
10
Code Generation for Constants
• Color key:
– RED: compile time
– BLUE: run time
11
Code Generation for Add
12
Code Generation for Add. Wrong!
cgen(e1 + e2) =
cgen(e1)
move $t1 $a0
cgen(e2)
add $a0 $t1 $a0
13
Code Generation Notes
14
Code Generation for Sub and Constants
15
Code Generation for Conditional
16
Code Generation for If (Cont.)
17
The Activation Record
18
The Activation Record (Cont.)
19
The Activation Record
old fp
y
AR of f
x
FP return
SP 20
Code Generation for Function Call
21
Code Generation for Function Call (Cont.)
22
Code Generation for Function Definition
FP FP FP
SP old fp old fp SP
y y
x x
SP FP return
SP
24
Code Generation for Variables
26
Code Generation for Variables (Cont.)
old fp
y • X is at fp + 4
x • Y is at fp + 8
FP return
SP
27
Summary
28
Summary
29
An Improvement
30
Example
31
How Many Temporaries?
• NT(e1 + e2)
– Needs at least as many temporaries as NT(e1)
– Needs at least as many temporaries as NT(e2) + 1
32
The Equations
33
The Revised AR
34
Picture
Old FP
xn
...
x1
FP Return Addr.
Temp NT(e)
...
Temp 1
35
Revised Code Generation
36
Code Generation for + (original)
cgen(e1 + e2) =
cgen(e1)
sw $a0 0($sp)
addiu $sp $sp -4
cgen(e2)
lw $t1 4($sp)
add $a0 $t1 $a0
addiu $sp $sp 4
37
Code Generation for + (revised)
cgen(e2, nt + 4)
lw $t1 nt($fp)
add $a0 $t1 $a0
38
Notes
39
Code Generation for OO Languages
Topic II
40
Object Layout
41
Two Issues
42
Object Layout Example
Class A {
a: Int;
d: Int;
f(): Int { a ← a + d };
};
43
Object Layout (Cont.)
44
Object Layout (Cont.)
45
Cool Object Layout
Class Tag 0
Object Size 4
Dispatch Ptr 8
Attribute 1 12
Attribute 2 16
...
46
Cool Object Layout (Cont.)
47
Subclasses
48
Layout Picture
Offset 0 4 8 12 16 20
Class
A Atag 5 * a d
B Btag 6 * a d b
C Ctag 6 * a d c
49
Subclasses (Cont.)
Header A1 object
A1 attrs. A2 object
A2 attrs A3 object
A3 attrs
...
50
Object Layout Example (Repeat)
Class A {
a: Int;
d: Int;
f(): Int { a ← a + d };
};
51
Dynamic Dispatch Example
• e.g()
– g refers to method in B if e is a B
• e.f()
– f refers to method in A if e is an A or C
(inherited in the case of C)
– f refers to method in B if e is a B
52
Dispatch Tables
53
Dispatch Table Example
54
Using Dispatch Tables
55
Using Dispatch Tables (Cont.)
56