@@ -22,17 +22,147 @@ Tasks of the compiler
22
22
23
23
4 . Translate function calls into machine-level calls
24
24
25
- Infrastructure of the compiler
26
- ------------------------------
25
+ ### Ad 1. Variable names to addresses
27
26
28
- Signature (global symbol table):
29
- - name and type of functions suitable for Jasmin
30
- - Jasmin types:
27
+ Jasmin types:
31
28
* ` I ` : ` int `
32
29
* ` D ` : ` double `
33
30
* ` V ` : ` void `
34
31
* ` Z ` : ` boolean `
35
32
33
+ ``` c
34
+ void foo // size=0 max=0
35
+ ( double x // x:0 size=2 max=2
36
+ , int y) { // y:2 size=3 max=3
37
+ int i; // i:3 size=4 max=4
38
+ { // newBlock
39
+ double y; // y:4 size=6 max=6
40
+ bool b; // b:6 size=7 max=7
41
+ } // popBlock size=4 max=7
42
+ int j; // j:4 size=5 max=7
43
+ }
44
+ ```
45
+ ``` jasmin
46
+ .method public static foo(DI)V
47
+ .limit locals 7
48
+ .end method
49
+ ```
50
+
51
+ ### Ad 2. Expressions trees to stack instructions
52
+
53
+ Example:
54
+ ``` c
55
+ int main () {
56
+ int i; // i:0
57
+ double x = 3 * (i = 42) + i++; // x:1
58
+ }
59
+ ```
60
+ Translates to JVM:
61
+ ``` scheme
62
+ .method public static main()I
63
+ .limit stack 4
64
+
65
+ ;; instr ;; expr ;; stack (S) ;; store (V)
66
+ iconst_3 ;; 3 ;; 3
67
+ bipush 42 ;; 42 ;; 3 . 42
68
+ istore_0 ;; i = ;; 3 ;; 0:42
69
+ iload_0 ;; i = 42 ;; 3 . 42
70
+ imul ;; 3 * (i = 42) ;; 126
71
+ iload_0 ;; i ;; 126 . 42
72
+ dup ;; ;; 126 . 42 . 42
73
+ iconst_1 ;; 1 ;; 126 . 42 . 42 . 1
74
+ iadd ;; i + 1 ;; 126 . 42 . 43
75
+ istore_0 ;; i++ ;; 126 . 42 ;; 0:43
76
+ iadd ;; 3 * (i = 42) + i++ ;; 168
77
+ i2d ;; (double) ;; 168.0
78
+ dstore_1 ;; x = 3 * (i = 42) + i++ ;; ;; 0:43, 1:168.0
79
+ ```
80
+ Maximum stack size: 4 (32bit words)
81
+
82
+ JVM instruction small-step semantics (without jumps):
83
+ ```
84
+ I : ⟨V,S⟩ → ⟨V',S'⟩
85
+
86
+ I : JVM instruction (or instruction sequence)
87
+ V / V' : variable store before/after
88
+ S / S' : stack before/after
89
+ ```
90
+
91
+ Some jump-free instructions:
92
+
93
+ | I | V | S | V' | S' |
94
+ | --------------| -----| ---------| ----------| -----------------|
95
+ | ` iconst ` _ i_ | _ V_ | _ S_ | _ V_ | _ S.i_ |
96
+ | ` bipush ` _ i_ | _ V_ | _ S_ | _ V_ | _ S.i_ |
97
+ | ` iadd ` | _ V_ | _ S.i.j_ | _ V_ | _ S.(i+j)_ |
98
+ | ` imul ` | _ V_ | _ S.i.j_ | _ V_ | _ S.(i* j)_ |
99
+ | ` i2d ` | _ V_ | _ S.i_ | _ V_ | _ S.(double)i_ |
100
+ | ` dup ` | _ V_ | _ S.i_ | _ V_ | _ S.i.i_ |
101
+ | ` istore ` _ a_ | _ V_ | _ S.i_ | _ V[ a: i ] _ | _ S_ |
102
+ | ` dstore ` _ a_ | _ V_ | _ S.d_ | _ V[ a: d ] _ | _ S_ |
103
+ | ` iload ` _ a_ | _ V_ | _ S_ | _ V_ | _ S.V[ a] _ |
104
+ | ` dload ` _ a_ | _ V_ | _ S_ | _ V_ | _ S.V[ a] _ |
105
+
106
+
107
+ Ad 3. Booleans and jumps
108
+ ------------------------
109
+
110
+ Example:
111
+ ``` c
112
+ bool inside (int i, int j, int k) {
113
+ return i <= j && j < k;
114
+ }
115
+ ```
116
+ Machines typically do not have boolean operations; use jumps instead.
117
+ ```scheme
118
+ .method public static inside(III)Z
119
+ .limit locals 3
120
+ .limit stack 2
121
+
122
+ iload_0 ;; i
123
+ iload_1 ;; j
124
+ if_icmpgt Lfalse ;; i <= j ;; false if i > j
125
+ iload_1 ;; j
126
+ iload_2 ;; k
127
+ if_icmpge Lfalse ;; j < k ;; false if j >= k
128
+
129
+ iconst_1 ;; true
130
+ goto Ldone
131
+
132
+ Lfalse:
133
+ iconst_0 ;; false
134
+
135
+ Ldone:
136
+ ireturn ;; return
137
+
138
+ .end method
139
+ ```
140
+
141
+ JVM instruction small-step semantics (with jumps):
142
+ ```
143
+ I : ⟨P,V,S⟩ → ⟨P',V',S'⟩
144
+
145
+ P / P' : code position (program counter) before/after
146
+ ```
147
+
148
+ | I | P | V | S | P' | V' | S' | Condition |
149
+ | -----------------| -----| -----| ---------| -------| -----| -----| -----------|
150
+ | ` goto ` _ L_ | _ P_ | _ V_ | _ S_ | _ L_ | _ V_ | _ S_ | |
151
+ | ` ifeq ` _ L_ | _ P_ | _ V_ | _ S.i_ | _ L_ | _ V_ | _ S_ | _ i = 0_ |
152
+ | ` ifeq ` _ L_ | _ P_ | _ V_ | _ S.i_ | _ P+1_ | _ V_ | _ S_ | _ i ≠ 0_ |
153
+ | ` ifne ` _ L_ | _ P_ | _ V_ | _ S.i_ | _ L_ | _ V_ | _ S_ | _ i ≠ 0_ |
154
+ | ` ifne ` _ L_ | _ P_ | _ V_ | _ S.i_ | _ P+1_ | _ V_ | _ S_ | _ i = 0_ |
155
+ | ` if_icmpgt ` _ L_ | _ P_ | _ V_ | _ S.i.j_ | _ L_ | _ V_ | _ S_ | _ i > j_ |
156
+ | ` if_icmpgt ` _ L_ | _ P_ | _ V_ | _ S.i.j_ | _ P+1_ | _ V_ | _ S_ | _ i ≤ j_ |
157
+
158
+
159
+
160
+ Infrastructure of the compiler
161
+ ------------------------------
162
+
163
+ Signature (global symbol table):
164
+ - name and type of functions suitable for Jasmin
165
+
36
166
Context (local symbol table):
37
167
1 . allocate local variables (` newLocal ` )
38
168
2 . resolve variable names to addresses (` lookupVar ` )
@@ -90,7 +220,6 @@ rest of the stack unchanged.
90
220
emit(t- store a) -- either istore or dstore
91
221
-- Problem here
92
222
```
93
- (We omit ` emit ` in the following.)
94
223
95
224
## Compiler correctness
96
225
@@ -130,17 +259,30 @@ stack is nil (no change).
130
259
compileStm(SInit t x e):
131
260
a <- newLocal t x
132
261
compileExp(e)
133
- t- store a -- either istore or dstore
262
+ emit( t- store a) -- either istore or dstore
134
263
135
264
compileStm(SExp t e):
136
265
compileExp(e)
137
- t- pop -- either pop or pop2
266
+ emit( t- pop) -- either pop or pop2
138
267
139
268
compileStm(SBlock ss):
140
269
newBlock
141
270
for (s : ss)
142
271
compileStm(s)
143
272
popBlock
273
+
274
+ compileStm(SWhile e s): -- s is a SBlock
275
+ LStart , LEnd <- newLabel
276
+
277
+ emit(LStart : )
278
+
279
+ compileExp(e) -- executing e leaves boolean on stack
280
+ emit(ifeq LEnd ) -- if "false" end loop
281
+
282
+ compileStm(s) -- body of loop
283
+ emit(goto LStart ) -- recheck loop condition
284
+
285
+ emit(LEnd : )
144
286
```
145
287
146
288
Correctness statement (simplified):
@@ -150,6 +292,8 @@ Correctness statement (simplified):
150
292
then compileStm(s) : ⟨V,S⟩ →* ⟨V',S⟩
151
293
such that γ' ~ V'.
152
294
295
+ (We omit ` emit ` in the following.)
296
+
153
297
Compiling booleans
154
298
------------------
155
299
0 commit comments