Skip to content

Commit 20937b3

Browse files
committed
Port math to boxed arithmetic to simplify memory model.
1 parent 347b766 commit 20937b3

File tree

20 files changed

+38
-37
lines changed

20 files changed

+38
-37
lines changed

Lang/src/main/java/chipmunk/vm/tree/Context.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public class Context {
3232
int[] frame = new int[40];
3333
int framePtr = 0;
3434
int stackPtr = 0;
35-
long[] vars = new long[200];
35+
long[] locals = new long[200];
36+
Object[] localRefs = new Object[200];
3637

3738
volatile boolean interrupt;
3839
public boolean _return;
@@ -52,12 +53,12 @@ public void postCall() {
5253
}
5354

5455
public long setLocal(int local, long value) {
55-
vars[stackPtr + local] = value;
56+
locals[stackPtr + local] = value;
5657
return value;
5758
}
5859

5960
public long getLocal(int local) {
60-
return vars[stackPtr + local];
61+
return locals[stackPtr + local];
6162
}
6263

6364
public void interrupt(){

Lang/src/main/java/chipmunk/vm/tree/Node.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
package chipmunk.vm.tree;
2222

2323
public interface Node {
24-
long execute(Context ctx);
24+
Object execute(Context ctx);
2525
}

Lang/src/main/java/chipmunk/vm/tree/nodes/Add.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Add implements Node {
2727
public Node l, r;
2828

2929
@Override
30-
public long execute(Context ctx) {
31-
return l.execute(ctx) + r.execute(ctx);
30+
public Object execute(Context ctx) {
31+
return ((Number)l.execute(ctx)).longValue() + ((Number)r.execute(ctx)).longValue();
3232
}
3333
}

Lang/src/main/java/chipmunk/vm/tree/nodes/Block.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public class Block implements Node {
2727
public Node[] body;
2828

2929
@Override
30-
public long execute(Context ctx) {
30+
public Object execute(Context ctx) {
3131
long result = 0;
3232
for (int i = 0; i < body.length; i++) {
3333
try {
34-
result = body[i].execute(ctx);
34+
result = (Long)body[i].execute(ctx);
3535
} catch (Exception e) {
3636
throw e;
3737
}

Lang/src/main/java/chipmunk/vm/tree/nodes/CallNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public CallNode(int locals, Node f, Node... args) {
3535
}
3636

3737
@Override
38-
public long execute(Context ctx) {
38+
public Object execute(Context ctx) {
3939
for (int i = 0; i < args.length; i++) {
40-
ctx.setLocal(i + locals, args[i].execute(ctx));
40+
ctx.setLocal(i + locals, ((Number)args[i].execute(ctx)).longValue());
4141
}
4242
ctx.preCall(locals);
43-
long result = f.execute(ctx);
43+
long result = (Long)f.execute(ctx);
4444
ctx.postCall();
4545
return result;
4646
}

Lang/src/main/java/chipmunk/vm/tree/nodes/Const.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Const(float f) {
3535
}
3636

3737
@Override
38-
public long execute(Context ctx) {
38+
public Object execute(Context ctx) {
3939
return value;
4040
}
4141
}

Lang/src/main/java/chipmunk/vm/tree/nodes/FDiv.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class FDiv implements Node {
2727
public Node l, r;
2828

2929
@Override
30-
public long execute(Context ctx) {
31-
return Float.floatToIntBits(Float.intBitsToFloat((int)l.execute(ctx)) / Float.intBitsToFloat((int)r.execute(ctx)));
30+
public Object execute(Context ctx) {
31+
return Float.floatToIntBits(Float.intBitsToFloat(((Number)l.execute(ctx)).intValue()) / Float.intBitsToFloat(((Number)r.execute(ctx)).intValue()));
3232
}
3333
}

Lang/src/main/java/chipmunk/vm/tree/nodes/FSub.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class FSub implements Node {
2727
public Node l, r;
2828

2929
@Override
30-
public long execute(Context ctx) {
30+
public Object execute(Context ctx) {
3131
return Float.floatToIntBits(Float.intBitsToFloat((int)l.execute(ctx)) - Float.intBitsToFloat((int)r.execute(ctx)));
3232
}
3333
}

Lang/src/main/java/chipmunk/vm/tree/nodes/For.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class For implements Node {
3030
public Node post;
3131

3232
@Override
33-
public long execute(Context ctx) {
33+
public Object execute(Context ctx) {
3434
doPre(ctx, 0);
3535
return doBody(ctx, doTest(ctx, 0));
3636
}
@@ -48,7 +48,7 @@ public int doPre(Context ctx, int prior) {
4848

4949
public long doTest(Context ctx, long prior) {
5050
try {
51-
return test.execute(ctx);
51+
return ((Number)test.execute(ctx)).longValue();
5252
} catch (Exception e) {
5353
ctx.suspendStateless(e, this::doBody);
5454
}

Lang/src/main/java/chipmunk/vm/tree/nodes/FunctionNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class FunctionNode implements Node {
2727
public Node[] nodes;
2828

2929
@Override
30-
public long execute(Context ctx) {
30+
public Object execute(Context ctx) {
3131
for (int i = 0; i < nodes.length - 1; i++) {
3232
var v = nodes[i].execute(ctx);
3333
if (ctx._return) {

0 commit comments

Comments
 (0)