-
Notifications
You must be signed in to change notification settings - Fork 1
/
Compiler.java
247 lines (208 loc) · 6.15 KB
/
Compiler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package org.isk.jvmhardcore.lexp;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
import org.isk.jvmhardcore.lexp.core.Expression;
import org.isk.jvmhardcore.lexp.operators.ArithmeticOperator;
import org.isk.jvmhardcore.lexp.operators.BitwiseOperator;
import org.isk.jvmhardcore.lexp.operators.LogicalOperator;
import org.isk.jvmhardcore.lexp.operators.RelationalOperator;
import org.isk.jvmhardcore.lexp.visitor.Visitor;
public class Compiler implements Visitor {
final private StringBuilder bytecode;
final private Expression expression;
final private Set<String> usedLabels;
final private Stack<String> andLabels;
final private Stack<String> orLabels;
final private Stack<LogicalOperator> logicalOperators;
public Compiler(Expression expression) {
this.bytecode = new StringBuilder();
this.expression = expression;
this.usedLabels = new HashSet<>();
this.andLabels = new Stack<>();
this.orLabels = new Stack<>();
this.logicalOperators = new Stack<>();
}
public String compile() {
this.expression.accept(this);
return this.bytecode.toString();
}
@Override
public void visitBeforeLeft(String name, LogicalOperator operator) {
this.logicalOperators.push(operator);
if (operator == LogicalOperator.AND) {
this.andLabels.push(name);
} else {
this.orLabels.push(name);
}
// this.traceLogicalExpressionTraversal("beforeLeft", name);
}
@Override
public void visitAfterLeft(String name, LogicalOperator operator) {
this.logicalOperators.pop();
if (operator == LogicalOperator.AND) {
this.andLabels.pop();
} else if (operator == LogicalOperator.OR) {
this.orLabels.pop();
}
if (this.usedLabels.remove(name)) {
this.writeLabel(name);
}
// this.traceLogicalExpressionTraversal("afterLeft", name);
}
@Override
public void visit(ArithmeticOperator operator) {
String instruction = null;
switch (operator) {
case PLUS:
instruction = "iadd";
break;
case MINUS:
instruction = "isub";
break;
case TIMES:
instruction = "imul";
break;
case DIVIDE:
instruction = "idiv";
break;
}
this.bytecode.append(instruction).append("\n");
}
@Override
public void visit(BitwiseOperator operator) {
String instruction = null;
switch (operator) {
case SHL:
instruction = "ishl";
break;
case SHR:
instruction = "ishr";
break;
case USHR:
instruction = "iushr";
break;
case AND:
instruction = "iand";
break;
case OR:
instruction = "ior";
break;
case XOR:
instruction = "ixor";
break;
}
this.bytecode.append(instruction).append("\n");
}
@Override
public void visitRelationExpression(RelationalOperator operator) {
// this.traceLogicalExpressionTraversal("re", operator);
final RelationalParams params = this.getRelationalParams(operator);
String instruction = null;
switch (params.operator) {
case EQ:
instruction = "if_icmpeq";
break;
case GE:
instruction = "if_icmpge";
break;
case GT:
instruction = "if_icmpgt";
break;
case LE:
instruction = "if_icmple";
break;
case LT:
instruction = "if_icmplt";
break;
case NE:
instruction = "if_icmpne";
break;
}
this.bytecode.append(instruction).append(" ").append(params.label).append("\n");
}
@Override
public void visitZeroRelationExpression(RelationalOperator operator) {
// this.traceLogicalExpressionTraversal("zre", operator);
final RelationalParams params = this.getRelationalParams(operator);
String instruction = null;
switch (params.operator) {
case EQ:
instruction = "ifeq";
break;
case GE:
instruction = "ifge";
break;
case GT:
instruction = "ifgt";
break;
case LE:
instruction = "ifle";
break;
case LT:
instruction = "iflt";
break;
case NE:
instruction = "ifne";
break;
}
this.bytecode.append(instruction).append(" ").append(params.label).append("\n");
}
@Override
public void visitIntValue(Integer value) {
this.bytecode.append("ldc ").append(value).append("\n");
}
@Override
public void visitIntVariable(Integer indexInLV) {
this.bytecode.append("iload ").append(indexInLV).append("\n");
}
private void writeLabel(String label) {
this.bytecode.append(label).append(":\n");
}
private RelationalParams getRelationalParams(RelationalOperator operator) {
RelationalOperator relationalOperator = null;
String label = null;
if (this.logicalOperators.isEmpty()) {
relationalOperator = operator.inverse();
label = "ko";
} else if (this.logicalOperators.peek() == LogicalOperator.AND) {
label = !this.orLabels.isEmpty() ? this.orLabels.peek() : "ko";
relationalOperator = operator.inverse();
} else if (this.logicalOperators.peek() == LogicalOperator.OR) {
label = !this.andLabels.isEmpty() ? this.andLabels.peek() : "ok";
relationalOperator = operator;
} else {
throw new RuntimeException("Don't know what to do!");
}
this.usedLabels.add(label);
return new RelationalParams(relationalOperator, label);
}
private class RelationalParams {
final RelationalOperator operator;
final String label;
public RelationalParams(RelationalOperator operator, String label) {
super();
this.operator = operator;
this.label = label;
}
}
// private void traceLogicalExpressionTraversal(String from, String nodeName) {
// System.out.print(from + " " + nodeName + ": ");
//
// this.trace();
// }
//
// private void traceLogicalExpressionTraversal(String from, RelationalOperator operator) {
// System.out.print(from + " " + operator + ": ");
//
// this.trace();
// }
//
// private void trace() {
// for (LogicalOperator lo : this.logicalOperators) {
// System.out.print(lo + ", ");
// }
//
// System.out.print("\n");
// }
}