-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathInterpreter.java
More file actions
311 lines (299 loc) · 17.6 KB
/
Interpreter.java
File metadata and controls
311 lines (299 loc) · 17.6 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.compilerprogramming.ezlang.interpreter;
import com.compilerprogramming.ezlang.compiler.BasicBlock;
import com.compilerprogramming.ezlang.compiler.CompiledFunction;
import com.compilerprogramming.ezlang.compiler.Instruction;
import com.compilerprogramming.ezlang.compiler.Operand;
import com.compilerprogramming.ezlang.exceptions.CompilerException;
import com.compilerprogramming.ezlang.exceptions.InterpreterException;
import com.compilerprogramming.ezlang.types.Symbol;
import com.compilerprogramming.ezlang.types.EZType;
import com.compilerprogramming.ezlang.types.TypeDictionary;
public class Interpreter {
TypeDictionary typeDictionary;
public Interpreter(TypeDictionary typeDictionary) {
this.typeDictionary = typeDictionary;
}
public Value run(String functionName) {
Symbol symbol = typeDictionary.lookup(functionName);
if (symbol instanceof Symbol.FunctionTypeSymbol functionSymbol) {
Frame frame = new Frame(functionSymbol);
ExecutionStack execStack = new ExecutionStack(1024);
return interpret(execStack, frame);
}
else {
throw new InterpreterException("Unknown function: " + functionName);
}
}
public Value interpret(ExecutionStack execStack, Frame frame) {
CompiledFunction currentFunction = frame.bytecodeFunction;
BasicBlock currentBlock = currentFunction.entry;
int ip = -1;
int base = frame.base;
boolean done = false;
Value returnValue = null;
while (!done) {
Instruction instruction;
ip++;
instruction = currentBlock.instructions.get(ip);
switch (instruction) {
case Instruction.Ret retInst -> {
if (retInst.value() instanceof Operand.ConstantOperand constantOperand) {
execStack.stack[base] = new Value.IntegerValue(constantOperand.value);
}
else if (retInst.value() instanceof Operand.NullConstantOperand) {
execStack.stack[base] = new Value.NullValue();
}
else if (retInst.value() instanceof Operand.RegisterOperand registerOperand) {
execStack.stack[base] = execStack.stack[base+registerOperand.frameSlot()];
}
else throw new IllegalStateException();
returnValue = execStack.stack[base];
}
case Instruction.Move moveInst -> {
if (moveInst.to() instanceof Operand.RegisterOperand toReg) {
if (moveInst.from() instanceof Operand.RegisterOperand fromReg) {
execStack.stack[base + toReg.frameSlot()] = execStack.stack[base + fromReg.frameSlot()];
}
else if (moveInst.from() instanceof Operand.ConstantOperand constantOperand) {
execStack.stack[base + toReg.frameSlot()] = new Value.IntegerValue(constantOperand.value);
}
else if (moveInst.from() instanceof Operand.NullConstantOperand) {
execStack.stack[base + toReg.frameSlot()] = new Value.NullValue();
}
else throw new IllegalStateException();
}
else throw new IllegalStateException();
}
case Instruction.Jump jumpInst -> {
currentBlock = jumpInst.jumpTo;
ip = -1;
if (currentBlock == currentFunction.exit)
done = true;
}
case Instruction.ConditionalBranch cbrInst -> {
boolean condition;
if (cbrInst.condition() instanceof Operand.RegisterOperand registerOperand) {
Value value = execStack.stack[base + registerOperand.frameSlot()];
if (value instanceof Value.IntegerValue integerValue) {
condition = integerValue.value != 0;
}
else {
condition = value != null;
}
}
else if (cbrInst.condition() instanceof Operand.ConstantOperand constantOperand) {
condition = constantOperand.value != 0;
}
else throw new IllegalStateException();
if (condition)
currentBlock = cbrInst.trueBlock;
else
currentBlock = cbrInst.falseBlock;
ip = -1;
if (currentBlock == currentFunction.exit)
done = true;
}
case Instruction.Call callInst -> {
// Copy args to new frame
int baseReg = base+currentFunction.frameSize();
int reg = baseReg;
for (Operand arg: callInst.args()) {
if (arg instanceof Operand.RegisterOperand param) {
execStack.stack[reg] = execStack.stack[base + param.frameSlot()];
}
else if (arg instanceof Operand.ConstantOperand constantOperand) {
execStack.stack[reg] = new Value.IntegerValue(constantOperand.value);
}
else if (arg instanceof Operand.NullConstantOperand) {
execStack.stack[reg] = new Value.NullValue();
}
reg += 1;
}
// Call function
Frame newFrame = new Frame(frame, baseReg, callInst.callee);
interpret(execStack, newFrame);
// Copy return value in expected location
if (!(callInst.callee.returnType instanceof EZType.EZTypeVoid)) {
execStack.stack[base + callInst.returnOperand().frameSlot()] = execStack.stack[baseReg];
}
}
case Instruction.Unary unaryInst -> {
// We don't expect constant here because we fold constants in unary expressions
Operand.RegisterOperand unaryOperand = (Operand.RegisterOperand) unaryInst.operand();
Value unaryValue = execStack.stack[base + unaryOperand.frameSlot()];
if (unaryValue instanceof Value.IntegerValue integerValue) {
switch (unaryInst.unop) {
case "-": execStack.stack[base + unaryInst.result().frameSlot()] = new Value.IntegerValue(-integerValue.value); break;
// Maybe below we should explicitly set Int
case "!": execStack.stack[base + unaryInst.result().frameSlot()] = new Value.IntegerValue(integerValue.value==0?1:0); break;
default: throw new CompilerException("Invalid unary op");
}
}
else
throw new IllegalStateException("Unexpected unary operand: " + unaryOperand);
}
case Instruction.Binary binaryInst -> {
long x, y;
long value = 0;
boolean intOp = true;
if (binaryInst.binOp.equals("==") || binaryInst.binOp.equals("!=")) {
Operand.RegisterOperand nonNullLitOperand = null;
if (binaryInst.left() instanceof Operand.NullConstantOperand) {
nonNullLitOperand = (Operand.RegisterOperand)binaryInst.right();
}
else if (binaryInst.right() instanceof Operand.NullConstantOperand) {
nonNullLitOperand = (Operand.RegisterOperand)binaryInst.left();
}
if (nonNullLitOperand != null) {
intOp = false;
Value otherValue = execStack.stack[base + nonNullLitOperand.frameSlot()];
switch (binaryInst.binOp) {
case "==": {
value = otherValue instanceof Value.NullValue ? 1 : 0;
break;
}
case "!=": {
value = otherValue instanceof Value.NullValue ? 0 : 1;
break;
}
default:
throw new IllegalStateException();
}
execStack.stack[base + binaryInst.result().frameSlot()] = new Value.IntegerValue(value);
}
}
if (intOp) {
if (binaryInst.left() instanceof Operand.ConstantOperand constant)
x = constant.value;
else if (binaryInst.left() instanceof Operand.RegisterOperand registerOperand)
x = ((Value.IntegerValue) execStack.stack[base + registerOperand.frameSlot()]).value;
else throw new IllegalStateException();
if (binaryInst.right() instanceof Operand.ConstantOperand constant)
y = constant.value;
else if (binaryInst.right() instanceof Operand.RegisterOperand registerOperand)
y = ((Value.IntegerValue) execStack.stack[base + registerOperand.frameSlot()]).value;
else throw new IllegalStateException();
switch (binaryInst.binOp) {
case "+": value = x + y; break;
case "-": value = x - y; break;
case "*": value = x * y; break;
case "/": value = x / y; break;
case "%": value = x % y; break;
case "==": value = x == y ? 1 : 0; break;
case "!=": value = x != y ? 1 : 0; break;
case "<": value = x < y ? 1: 0; break;
case ">": value = x > y ? 1 : 0; break;
case "<=": value = x <= y ? 1 : 0; break;
case ">=": value = x >= y ? 1 : 0; break;
default: throw new IllegalStateException();
}
execStack.stack[base + binaryInst.result().frameSlot()] = new Value.IntegerValue(value);
}
}
case Instruction.NewArray newArrayInst -> {
long size = 0;
Value initValue = null;
if (newArrayInst.len() instanceof Operand.ConstantOperand constantOperand)
size = constantOperand.value;
else if (newArrayInst.len() instanceof Operand.RegisterOperand registerOperand) {
Value.IntegerValue indexValue = (Value.IntegerValue) execStack.stack[base + registerOperand.frameSlot()];
size = (long) indexValue.value;
}
if (newArrayInst.initValue() instanceof Operand.ConstantOperand constantOperand)
initValue = new Value.IntegerValue(constantOperand.value);
else if (newArrayInst.initValue() instanceof Operand.RegisterOperand registerOperand)
initValue = execStack.stack[base + registerOperand.frameSlot()];
execStack.stack[base + newArrayInst.destOperand().frameSlot()] = new Value.ArrayValue(newArrayInst.type, size, initValue);
}
case Instruction.NewStruct newStructInst -> {
execStack.stack[base + newStructInst.destOperand().frameSlot()] = new Value.StructValue(newStructInst.type);
}
case Instruction.ArrayStore arrayStoreInst -> {
if (arrayStoreInst.arrayOperand() instanceof Operand.RegisterOperand arrayOperand) {
Value.ArrayValue arrayValue = (Value.ArrayValue) execStack.stack[base + arrayOperand.frameSlot()];
int index = 0;
if (arrayStoreInst.indexOperand() instanceof Operand.ConstantOperand constant) {
index = (int) constant.value;
}
else if (arrayStoreInst.indexOperand() instanceof Operand.RegisterOperand registerOperand) {
Value.IntegerValue indexValue = (Value.IntegerValue) execStack.stack[base + registerOperand.frameSlot()];
index = (int) indexValue.value;
}
else throw new IllegalStateException();
Value value;
if (arrayStoreInst.sourceOperand() instanceof Operand.ConstantOperand constantOperand) {
value = new Value.IntegerValue(constantOperand.value);
}
else if (arrayStoreInst.sourceOperand() instanceof Operand.NullConstantOperand) {
value = new Value.NullValue();
}
else if (arrayStoreInst.sourceOperand() instanceof Operand.RegisterOperand registerOperand) {
value = execStack.stack[base + registerOperand.frameSlot()];
}
else throw new IllegalStateException();
if (index == arrayValue.values.size())
arrayValue.values.add(value);
else
arrayValue.values.set(index, value);
} else throw new IllegalStateException();
}
case Instruction.ArrayLoad arrayLoadInst -> {
if (arrayLoadInst.arrayOperand() instanceof Operand.RegisterOperand arrayOperand) {
Value.ArrayValue arrayValue = (Value.ArrayValue) execStack.stack[base + arrayOperand.frameSlot()];
if (arrayLoadInst.indexOperand() instanceof Operand.ConstantOperand constant) {
execStack.stack[base + arrayLoadInst.destOperand().frameSlot()] = arrayValue.values.get((int) constant.value);
}
else if (arrayLoadInst.indexOperand() instanceof Operand.RegisterOperand registerOperand) {
Value.IntegerValue index = (Value.IntegerValue) execStack.stack[base + registerOperand.frameSlot()];
execStack.stack[base + arrayLoadInst.destOperand().frameSlot()] = arrayValue.values.get((int) index.value);
}
else throw new IllegalStateException();
} else throw new IllegalStateException();
}
case Instruction.SetField setFieldInst -> {
if (setFieldInst.structOperand() instanceof Operand.RegisterOperand structOperand) {
Value.StructValue structValue = (Value.StructValue) execStack.stack[base + structOperand.frameSlot()];
int index = setFieldInst.fieldIndex;
Value value;
if (setFieldInst.sourceOperand() instanceof Operand.ConstantOperand constant) {
value = new Value.IntegerValue(constant.value);
}
else if (setFieldInst.sourceOperand() instanceof Operand.NullConstantOperand) {
value = new Value.NullValue();
}
else if (setFieldInst.sourceOperand() instanceof Operand.RegisterOperand registerOperand) {
value = execStack.stack[base + registerOperand.frameSlot()];
}
else throw new IllegalStateException();
structValue.fields[index] = value;
} else throw new IllegalStateException();
}
case Instruction.GetField getFieldInst -> {
if (getFieldInst.structOperand() instanceof Operand.RegisterOperand structOperand) {
Value.StructValue structValue = (Value.StructValue) execStack.stack[base + structOperand.frameSlot()];
int index = getFieldInst.fieldIndex;
execStack.stack[base + getFieldInst.destOperand().frameSlot()] = structValue.fields[index];
} else throw new IllegalStateException();
}
case Instruction.ArgInstruction argInst -> {}
default -> throw new IllegalStateException("Unexpected value: " + instruction);
}
}
return returnValue;
}
static class Frame {
Frame caller;
int base;
CompiledFunction bytecodeFunction;
public Frame(Symbol.FunctionTypeSymbol functionSymbol) {
this.caller = null;
this.base = 0;
this.bytecodeFunction = (CompiledFunction) functionSymbol.code();
}
Frame(Frame caller, int base, EZType.EZTypeFunction functionType) {
this.caller = caller;
this.base = base;
this.bytecodeFunction = (CompiledFunction) functionType.code;
}
}
}