Skip to content

Commit 3e77f3b

Browse files
committed
(feat) threaded interpretion
1 parent 5382a37 commit 3e77f3b

File tree

13 files changed

+72
-25
lines changed

13 files changed

+72
-25
lines changed

src/main/java/com/googlecode/aviator/InterpretExpression.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.Set;
99
import com.googlecode.aviator.code.interpreter.IR;
1010
import com.googlecode.aviator.code.interpreter.InterpretContext;
11-
import com.googlecode.aviator.code.interpreter.ir.JumpIR;
1211
import com.googlecode.aviator.code.interpreter.ir.LoadIR;
1312
import com.googlecode.aviator.lexer.SymbolTable;
1413
import com.googlecode.aviator.lexer.token.Token;
@@ -51,7 +50,7 @@ private void loadConstants(final Set<Token<?>> constants, final List<IR> instrum
5150
InterpretContext ctx = new InterpretContext(this, instruments, getCompileEnv());
5251
for (Token<?> token : constants) {
5352
final LoadIR loadConstantIR = new LoadIR(this.sourceFile, token, null, false);
54-
loadConstantIR.eval(ctx);
53+
loadConstantIR.evalWithoutDispatch(ctx);
5554
this.constantPool.put(token, ctx.pop());
5655
}
5756
}
@@ -102,22 +101,23 @@ public Object executeDirectly(final Map<String, Object> env) {
102101
}
103102

104103
InterpretContext ctx = new InterpretContext(this, this.instruments, (Env) env);
105-
IR ir = null;
106-
while ((ir = ctx.getPc()) != null) {
107-
if (trace) {
108-
RuntimeUtils.printlnTrace(env, " " + ir + " " + ctx.descOperandsStack());
109-
}
110-
ir.eval(ctx);
111-
if (ir instanceof JumpIR) {
112-
if (ir != ctx.getPc()) {
113-
// jump successfully, we don't move pc to next.
114-
continue;
115-
}
116-
}
117-
if (!ctx.next()) {
118-
break;
119-
}
120-
}
104+
ctx.dispatch(false);
105+
106+
// while ((ir = ctx.getPc()) != null) {
107+
// if (trace) {
108+
// RuntimeUtils.printlnTrace(env, " " + ir + " " + ctx.descOperandsStack());
109+
// }
110+
// ir.eval(ctx);
111+
// if (ir instanceof JumpIR) {
112+
// if (ir != ctx.getPc()) {
113+
// // jump successfully, we don't move pc to next.
114+
// continue;
115+
// }
116+
// }
117+
// if (!ctx.next()) {
118+
// break;
119+
// }
120+
// }
121121
if (trace) {
122122
RuntimeUtils.printlnTrace(env, " return " + ctx.descOperandsStack());
123123
}

src/main/java/com/googlecode/aviator/code/interpreter/InterpretContext.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.googlecode.aviator.code.interpreter;
22

33
import java.util.ArrayDeque;
4-
import java.util.Collections;
54
import java.util.List;
65
import com.googlecode.aviator.InterpretExpression;
76
import com.googlecode.aviator.lexer.token.Token;
87
import com.googlecode.aviator.parser.VariableMeta;
8+
import com.googlecode.aviator.runtime.RuntimeUtils;
99
import com.googlecode.aviator.runtime.type.AviatorJavaType;
1010
import com.googlecode.aviator.runtime.type.AviatorObject;
1111
import com.googlecode.aviator.utils.Env;
@@ -20,16 +20,19 @@ public class InterpretContext {
2020
private final ArrayDeque<AviatorObject> operands = new ArrayDeque<>();
2121
private IR pc;
2222
private int pcIndex = -1;
23-
private List<IR> instruments = Collections.emptyList();
23+
private IR[] instruments = new IR[0];
2424
private final Env env;
2525
private final InterpretExpression expression;
2626
private boolean reachEnd;
27+
private final boolean trace;
28+
2729

2830
public InterpretContext(final InterpretExpression exp, final List<IR> instruments,
2931
final Env env) {
3032
this.expression = exp;
31-
this.instruments = instruments;
33+
this.instruments = instruments.toArray(this.instruments);
3234
this.env = env;
35+
this.trace = RuntimeUtils.isTracedEval(env);
3336
next();
3437
}
3538

@@ -54,14 +57,14 @@ public void clearStack() {
5457
}
5558

5659
public void jumpTo(final int tpc) {
57-
if (tpc == this.instruments.size()) {
60+
if (tpc == this.instruments.length) {
5861
this.pc = null;
5962
this.pcIndex = -1;
6063
this.reachEnd = true;
6164
return;
6265
}
6366
this.pcIndex = tpc;
64-
this.pc = this.instruments.get(this.pcIndex);
67+
this.pc = this.instruments[this.pcIndex];
6568
}
6669

6770
public InterpretExpression getExpression() {
@@ -77,8 +80,8 @@ public boolean next() {
7780
return false;
7881
}
7982
this.pcIndex++;
80-
if (this.pcIndex < this.instruments.size()) {
81-
this.pc = this.instruments.get(this.pcIndex);
83+
if (this.pcIndex < this.instruments.length) {
84+
this.pc = this.instruments[this.pcIndex];
8285
return true;
8386
}
8487
return false;
@@ -114,4 +117,29 @@ public String descOperandsStack() {
114117
sb.append("]>");
115118
return sb.toString();
116119
}
120+
121+
/**
122+
* Move pc to next and execute it.
123+
*/
124+
public void dispatch() {
125+
this.dispatch(true);
126+
}
127+
128+
/**
129+
* dispatch next IR execution.
130+
*
131+
* @param whether to move pc next.
132+
*/
133+
public void dispatch(final boolean next) {
134+
if (next && !next()) {
135+
return;
136+
}
137+
138+
if (this.pc != null) {
139+
if (this.trace) {
140+
RuntimeUtils.printlnTrace(this.env, " " + this.pc + " " + descOperandsStack());
141+
}
142+
this.pc.eval(this);
143+
}
144+
}
117145
}

src/main/java/com/googlecode/aviator/code/interpreter/ir/AssertTypeIR.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void eval(final InterpretContext context) {
2929
context.peek().stringValue(context.getEnv());
3030
break;
3131
}
32+
context.dispatch();
3233
}
3334

3435
@Override

src/main/java/com/googlecode/aviator/code/interpreter/ir/BranchIfIR.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public void eval(final InterpretContext context) {
3434
AviatorObject top = context.peek();
3535
if (top.booleanValue(context.getEnv())) {
3636
context.jumpTo(this.pc);
37+
context.dispatch(false);
38+
} else {
39+
context.dispatch();
3740
}
3841
}
3942

src/main/java/com/googlecode/aviator/code/interpreter/ir/BranchUnlessIR.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public void eval(final InterpretContext context) {
3535
AviatorObject top = context.peek();
3636
if (!top.booleanValue(context.getEnv())) {
3737
context.jumpTo(this.pc);
38+
context.dispatch(false);
39+
} else {
40+
context.dispatch();
3841
}
3942
}
4043

src/main/java/com/googlecode/aviator/code/interpreter/ir/ClearIR.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class ClearIR implements IR {
1212
@Override
1313
public void eval(final InterpretContext context) {
1414
context.clearStack();
15+
context.dispatch();
1516
}
1617

1718
@Override

src/main/java/com/googlecode/aviator/code/interpreter/ir/GotoIR.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public Label getLabel() {
3232
@Override
3333
public void eval(final InterpretContext context) {
3434
context.jumpTo(this.pc);
35+
context.dispatch(false);
3536
}
3637

3738
@Override

src/main/java/com/googlecode/aviator/code/interpreter/ir/LoadIR.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public LoadIR(final String sourceFile, final Token<?> token, final VariableMeta
4444

4545
@Override
4646
public void eval(final InterpretContext context) {
47+
evalWithoutDispatch(context);
48+
context.dispatch();
49+
}
50+
51+
public void evalWithoutDispatch(final InterpretContext context) {
4752
if (this.token == null) {
4853
return;
4954
}

src/main/java/com/googlecode/aviator/code/interpreter/ir/NewLambdaIR.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public NewLambdaIR(final String lambdaName) {
1919
@Override
2020
public void eval(final InterpretContext context) {
2121
context.push(context.getExpression().newLambda(context.getEnv(), this.lambdaName));
22+
context.dispatch();
2223
}
2324

2425
@Override

src/main/java/com/googlecode/aviator/code/interpreter/ir/OperatorIR.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public void eval(final InterpretContext context) {
9090
}
9191

9292
context.push(this.op.eval(args, context.getEnv()));
93+
context.dispatch();
9394
}
9495

9596

0 commit comments

Comments
 (0)