10
10
*/
11
11
12
12
public class CodeGenerator extends DepthFirstAdapter {
13
- public String code = "" ;
14
- public HashMap <String , Integer > symbolTable = new HashMap <String , Integer >();
13
+ private String code = "" ;
14
+ private HashMap <String , Integer > symbolTable = new HashMap <String , Integer >();
15
15
private HashMap <String , String > typeTable ;
16
16
private String type = "" ;
17
17
private int countLabels = 0 ;
18
+ private int compareCounter = 0 ;
19
+ private int breakCounter = 0 ;
20
+ private int stackHeight = 1 ;
18
21
19
22
public CodeGenerator (HashMap <String , String > symbolTable ) {
20
23
typeTable = symbolTable ;
@@ -24,45 +27,41 @@ public CodeGenerator(HashMap<String, String> symbolTable) {
24
27
@ Override
25
28
public void outAAndExpr (AAndExpr node ) {
26
29
code += "\t iand\n " ;
30
+ stackHeight --;
27
31
type = "boolean" ;
28
32
}
29
33
@ Override
30
34
public void outAOrExpr (AOrExpr node ) {
31
35
code += "\t ior\n " ;
36
+ stackHeight --;
32
37
type = "boolean" ;
33
38
}
34
39
@ Override
35
40
public void outAXorExpr (AXorExpr node ) {
36
41
code += "\t ixor\n " ;
42
+ stackHeight --;
37
43
type = "boolean" ;
38
44
}
39
45
@ Override
40
46
public void outANotExpr (ANotExpr node ) {
41
47
code += "\t ineg\n " ;
42
48
type = "boolean" ;
43
49
}
44
- @ Override
45
- public void outATrueExpr (ATrueExpr node ) {
46
- code += "\t bipush 1\n " ;
47
- type = "boolean" ;
48
- }
49
- @ Override
50
- public void outAFalseExpr (AFalseExpr node ) {
51
- code += "\t bipush 0\n " ;
52
- type = "boolean" ;
53
- }
54
-
55
50
// Arithmetic operations
56
51
@ Override
57
52
public void outAPlusExpr (APlusExpr node ) {
58
53
code += "\t iadd\n " ;
54
+ stackHeight --;
59
55
type = "integer" ;
60
56
}
61
57
@ Override
62
58
public void outAMinusExpr (AMinusExpr node ) {
63
59
code += "\t isub\n " ;
60
+ stackHeight --;
64
61
type = "integer" ;
65
62
}
63
+
64
+ // Arithmetic expressions
66
65
@ Override // NEED TO TEST THIS!
67
66
public void outAUnaryMinusExpr (AUnaryMinusExpr node ) {
68
67
code += "\t ineg\n " ;
@@ -71,11 +70,13 @@ public void outAUnaryMinusExpr(AUnaryMinusExpr node) {
71
70
@ Override
72
71
public void outAMultExpr (AMultExpr node ) {
73
72
code += "\t imul\n " ;
73
+ stackHeight --;
74
74
type = "integer" ;
75
75
}
76
76
@ Override
77
77
public void outADivExpr (ADivExpr node ) {
78
78
code += "\t idiv\n " ;
79
+ stackHeight --;
79
80
type = "integer" ;
80
81
}
81
82
@ Override
@@ -84,16 +85,14 @@ public void outAModExpr(AModExpr node) {
84
85
type = "integer" ;
85
86
}
86
87
87
- // Comparisons
88
-
89
- // Identifier
88
+ // Check identifier
90
89
@ Override
91
90
public void outAIdentifierExpr (AIdentifierExpr node ) {
92
91
boolean check = true ;
93
92
String identifier = node .getIdentifier ().toString ().toLowerCase ().replaceAll (" " ,"" );
94
93
Node parent = node ;
95
94
String parentName ;
96
- do {
95
+ do { // Needed to check, if we are in a declaration-context
97
96
parent = parent .parent ();
98
97
parentName = parent .getClass ().getSimpleName ().replaceAll (" " ,"" );
99
98
if (parentName .equals ("ADeclarationExpr" )) check = false ;
@@ -102,6 +101,7 @@ public void outAIdentifierExpr(AIdentifierExpr node) {
102
101
103
102
if (check ) {
104
103
code += "\t iload " +symbolTable .get (identifier )+"\n " ;
104
+ stackHeight ++;
105
105
type = typeTable .get (identifier );
106
106
}
107
107
}
@@ -111,47 +111,137 @@ public void outAIdentifierExpr(AIdentifierExpr node) {
111
111
public void caseAAssignmentExpr (AAssignmentExpr node ) {
112
112
node .getExpr ().apply (this );
113
113
code += "\t istore " +symbolTable .get (node .getIdentifier ().toString ().toLowerCase ().replaceAll (" " ,"" ))+"\n " ;
114
+ stackHeight --;
114
115
}
115
116
@ Override
116
117
public void caseANumberExpr (ANumberExpr node ) {
117
- code += "\t ldc " +node .getNumber ().toString ().replaceAll (" " ,"" )+"\n " ;
118
+ code += "\t bipush " +node .getNumber ().toString ().replaceAll (" " ,"" )+"\n " ;
119
+ stackHeight ++;
118
120
type = "integer" ;
119
121
}
120
122
@ Override
121
123
public void caseATrueExpr (ATrueExpr node ) {
122
- code += "\t ldc 1\n " ;
124
+ code += "\t bipush 1\n " ;
125
+ stackHeight ++;
123
126
type = "boolean" ;
124
127
}
125
128
@ Override
126
129
public void caseAFalseExpr (AFalseExpr node ) {
127
- code += "\t ldc 0\n " ;
130
+ code += "\t bipush 0\n " ;
131
+ stackHeight ++;
128
132
type = "boolean" ;
129
133
}
130
134
@ Override
131
135
public void caseAPrintExpr (APrintExpr node ) {
132
136
String intOrBoolean = "I" ;
133
-
134
137
code += "\t getstatic java/lang/System/out Ljava/io/PrintStream;\n " ;
135
138
node .getExpr ().apply (this );
136
139
if (type .equals ("boolean" )) intOrBoolean = "Z" ;
137
140
code += "\t invokevirtual java/io/PrintStream/println(" +intOrBoolean +")V\n " ;
141
+ stackHeight ++;
138
142
}
139
143
140
144
// While loop
141
145
@ Override
142
146
public void caseAWhileExpr (AWhileExpr node ) {
143
- code += "\t Label" +countLabels +":\n " ;
147
+ breakCounter = countLabels ;
148
+ int temp = countLabels ++;
149
+ code += "LabelWhileUp" +temp +":\n " ;
144
150
node .getLeft ().apply (this );
145
- code += "\n done\n " ;
151
+ code += "\t ifeq LabelWhileDown" +temp +"\n " ;
152
+ stackHeight --;
153
+ node .getRight ().apply (this );
154
+ code += "\t goto LabelWhileUp" +temp +"\n " ;
155
+ code += "LabelWhileDown" +temp +":\n " ;
156
+ }
157
+
158
+ // Break
159
+ @ Override
160
+ public void caseABreakExpr (ABreakExpr node ) {
161
+ code += "\t goto LabelWhileDown" +breakCounter +"\n " ;
162
+ }
163
+
164
+ // If-then Part
165
+ @ Override
166
+ public void caseAIfThenExpr (AIfThenExpr node ) {
167
+ int temp = countLabels ++;
168
+ node .getLeft ().apply (this );
169
+ code += "\t ifeq LabelIfDown" +temp +"\n " ;
170
+ stackHeight --;
171
+ node .getRight ().apply (this );
172
+ code += "LabelIfDown" +temp +":\n " ;
173
+ }
174
+
175
+ // If-then-else Part
176
+ @ Override
177
+ public void caseAIfThenElseExpr (AIfThenElseExpr node ) {
178
+ int temp = countLabels ++;
179
+ node .getIf ().apply (this );
180
+ code += "\t ifeq LabelIfElse" +temp +"\n " ;
181
+ stackHeight --;
182
+ node .getThen ().apply (this );
183
+ code += "LabelIfElse" +temp +":\n " ;
184
+ node .getElse ().apply (this );
146
185
}
147
186
148
187
// Comparisons
149
188
@ Override
150
189
public void caseAComparisonExpr (AComparisonExpr node ) {
151
- String compare = node .getComparison ().toString ().toLowerCase ().replaceAll (" " ,"" );
152
- if (compare .equals ("<" )) {
153
- System .out .println ("bombe" );
154
- }
190
+ int temp = 0 ;
191
+ node .getLeft ().apply (this );
192
+ node .getRight ().apply (this );
193
+ code += "\t isub\n " ; // To check with zero
194
+ stackHeight --;
195
+ temp = compareCounter ;
196
+ node .getComparison ().apply (this ); // Add line matching to corresponding symbol
197
+ code += "\t bipush 0\n " ;
198
+ stackHeight ++;
199
+ code += "\t goto LabelCompEnd" +temp +"\n " ;
200
+ code += "LabelTrue" +temp +":\n " ;
201
+ code += "\t bipush 1\n " ;
202
+ stackHeight ++;
203
+ code += "LabelCompEnd" +temp +":\n " ;
204
+ }
205
+ @ Override
206
+ public void outAGtExpr (AGtExpr node ) {
207
+ code += "\t ifgt LabelTrue" +(compareCounter ++)+"\n " ;
208
+ stackHeight --;
209
+ }
210
+ @ Override
211
+ public void outAGeExpr (AGeExpr node ) {
212
+ code += "\t ifge LabelTrue" +(compareCounter ++)+"\n " ;
213
+ stackHeight --;
214
+ }
215
+ @ Override
216
+ public void outALtExpr (ALtExpr node ) {
217
+ code += "\t iflt LabelTrue" +(compareCounter ++)+"\n " ;
218
+ stackHeight --;
219
+ }
220
+ @ Override
221
+ public void outALeExpr (ALeExpr node ) {
222
+ code += "\t ifle LabelTrue" +(compareCounter ++)+"\n " ;
223
+ stackHeight --;
224
+ }
225
+ @ Override
226
+ public void outANeExpr (ANeExpr node ) {
227
+ code += "\t ifne LabelTrue" +(compareCounter ++)+"\n " ;
228
+ stackHeight --;
229
+ }
230
+ @ Override
231
+ public void outAEqExpr (AEqExpr node ) {
232
+ code += "\t ifeq LabelTrue" +(compareCounter ++)+"\n " ;
233
+ stackHeight --;
234
+ }
235
+
236
+ /********************************* Getter and Setter **************************************/
155
237
238
+ public HashMap <String , Integer > getSymbolTable () {
239
+ return symbolTable ;
240
+ }
241
+ public String getCode () {
242
+ return code ;
243
+ }
244
+ public int getStackHeight () {
245
+ return stackHeight ;
156
246
}
157
247
}
0 commit comments