From 1dd8341715a4444021efa9776da7a907cd622242 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Tue, 13 May 2014 16:31:38 -0700 Subject: [PATCH] borked up merge. fixed now. --- src/vm/Bytecode.java | 8 -------- src/vm/Test.java | 14 +++++--------- src/vm/VM.java | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/vm/Bytecode.java b/src/vm/Bytecode.java index ff78a40..e3acfbc 100644 --- a/src/vm/Bytecode.java +++ b/src/vm/Bytecode.java @@ -19,13 +19,8 @@ public Instruction(String name, int nargs) { public static final short IEQ = 5; // int equal public static final short BR = 6; // branch public static final short BRT = 7; // branch if true -<<<<<<< HEAD public static final short BRF = 8; // branch if true public static final short ICONST = 9; // push constant integer -======= - public static final short BRF = 8; // branch if true - public static final short ICONST = 9; // push constant integer ->>>>>>> master public static final short LOAD = 10; // load from local context public static final short GLOAD = 11; // load from global memory public static final short STORE = 12; // store in local context @@ -33,11 +28,8 @@ public Instruction(String name, int nargs) { public static final short PRINT = 14; // print stack top public static final short POP = 15; // throw away top of stack public static final short HALT = 16; -<<<<<<< HEAD public static final short CALL = 17; public static final short RET = 18; // return with/without value -======= ->>>>>>> master public static Instruction[] instructions = new Instruction[] { null, // diff --git a/src/vm/Test.java b/src/vm/Test.java index c473574..fed3886 100644 --- a/src/vm/Test.java +++ b/src/vm/Test.java @@ -2,13 +2,18 @@ import static vm.Bytecode.BR; import static vm.Bytecode.BRF; +import static vm.Bytecode.CALL; import static vm.Bytecode.GLOAD; import static vm.Bytecode.GSTORE; import static vm.Bytecode.HALT; import static vm.Bytecode.IADD; import static vm.Bytecode.ICONST; import static vm.Bytecode.ILT; +import static vm.Bytecode.IMUL; +import static vm.Bytecode.ISUB; +import static vm.Bytecode.LOAD; import static vm.Bytecode.PRINT; +import static vm.Bytecode.RET; public class Test { static int[] hello = { @@ -44,7 +49,6 @@ public class Test { HALT // 24 }; -<<<<<<< HEAD static int[] factorial = { //.def fact: ARGS=1, LOCALS=0 ADDRESS // IF N < 2 RETURN 1 @@ -73,14 +77,6 @@ public class Test { public static void main(String[] args) { VM vm = new VM(factorial, 22, 0); -======= - public static void main(String[] args) { - VM vm = new VM(hello, 0, 0); - vm.trace = true; - vm.exec(); - - vm = new VM(loop, 0, 2); ->>>>>>> master vm.trace = true; vm.exec(); } diff --git a/src/vm/VM.java b/src/vm/VM.java index 4330603..ca9207a 100644 --- a/src/vm/VM.java +++ b/src/vm/VM.java @@ -6,6 +6,7 @@ import static vm.Bytecode.BR; import static vm.Bytecode.BRF; import static vm.Bytecode.BRT; +import static vm.Bytecode.CALL; import static vm.Bytecode.GLOAD; import static vm.Bytecode.GSTORE; import static vm.Bytecode.HALT; @@ -18,6 +19,7 @@ import static vm.Bytecode.LOAD; import static vm.Bytecode.POP; import static vm.Bytecode.PRINT; +import static vm.Bytecode.RET; import static vm.Bytecode.STORE; /** A simple stack-based interpreter */ @@ -121,6 +123,26 @@ protected void cpu() { case POP: --sp; break; + case CALL : + // expects all args on stack + addr = code[ip++]; // target addr of function + int nargs = code[ip++]; // how many args got pushed + stack[++sp] = nargs; // save num args + stack[++sp] = fp; // save fp + stack[++sp] = ip; // push return address + fp = sp; // fp points at ret addr on stack + ip = addr; // jump to function + // code preamble of func must push space for locals + break; + case RET: + int rvalue = stack[sp--]; // pop return value + sp = fp; // jump over locals to fp which points at ret addr + ip = stack[sp--]; // pop return address, jump to it + fp = stack[sp--]; // restore fp + nargs = stack[sp--]; // how many args to throw away? + sp -= nargs; // pop args + stack[++sp] = rvalue; // leave result on stack + break; default : throw new Error("invalid opcode: "+opcode+" at ip="+(ip-1)); }