diff --git a/src/vm/Bytecode.java b/src/vm/Bytecode.java index 90425a9..f9f760d 100644 --- a/src/vm/Bytecode.java +++ b/src/vm/Bytecode.java @@ -17,19 +17,17 @@ public Instruction(String name, int nargs) { public static final short IMUL = 3; public static final short ILT = 4; // int less than public static final short IEQ = 5; // int equal - public static final short CALL = 6; - public static final short RET = 7; // return with/without value - public static final short BR = 8; // branch - public static final short BRT = 9; // branch if true - public static final short BRF = 10; // branch if true - public static final short ICONST = 11; // push constant integer - public static final short LOAD = 12; // load from local context - public static final short GLOAD = 13; // load from global memory - public static final short STORE = 14; // store in local context - public static final short GSTORE = 15; // store in global memory - public static final short PRINT = 16; // print stack top - public static final short POP = 17; // throw away top of stack - public static final short HALT = 18; + public static final short BR = 6; // branch + public static final short BRT = 7; // branch if true + public static final short BRF = 8; // branch if true + public static final short ICONST = 9; // push constant integer + 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 + public static final short GSTORE = 13; // store in global memory + 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; public static Instruction[] instructions = new Instruction[] { null, // @@ -38,7 +36,6 @@ public Instruction(String name, int nargs) { new Instruction("imul"), new Instruction("ilt"), new Instruction("ieq"), - new Instruction("call", 2), // call addr, nargs new Instruction("ret"), new Instruction("br", 1), new Instruction("brt", 1), diff --git a/src/vm/Test.java b/src/vm/Test.java index 90be2c3..6549430 100644 --- a/src/vm/Test.java +++ b/src/vm/Test.java @@ -2,18 +2,13 @@ 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 = { @@ -47,41 +42,11 @@ public class Test { HALT // 24 }; - static int[] factorial = { -//.def fact: ARGS=1, LOCALS=0 ADDRESS -// IF N < 2 RETURN 1 - LOAD, -3, // 0 - ICONST, 2, // 2 - ILT, // 4 - BRF, 10, // 5 - ICONST, 1, // 7 - RET, // 9 -//CONT: -// RETURN N * FACT(N-1) - LOAD, -3, // 10 - LOAD, -3, // 12 - ICONST, 1, // 14 - ISUB, // 16 - CALL, 0, 1, // 17 - IMUL, // 20 - RET, // 21 -//.DEF MAIN: ARGS=0, LOCALS=0 -// PRINT FACT(10) - ICONST, 5, // 22 - CALL, 0, 1, // 24 - PRINT, // 27 - HALT // 28 - }; - public static void main(String[] args) { -// VM vm = new VM(hello, 0, 0); -// vm.exec(); -// -// vm = new VM(loop, 0, 2); -// vm.trace = true; -// vm.exec(); + VM vm = new VM(hello, 0, 0); + vm.exec(); - VM vm = new VM(factorial, 22, 0); + vm = new VM(loop, 0, 2); vm.trace = true; vm.exec(); } diff --git a/src/vm/VM.java b/src/vm/VM.java index 52500ca..bf75f1b 100644 --- a/src/vm/VM.java +++ b/src/vm/VM.java @@ -6,7 +6,6 @@ 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; @@ -19,7 +18,6 @@ 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 */ @@ -38,7 +36,7 @@ public class VM { // memory int[] code; // word-addressable code memory but still bytecodes. int[] globals; // global variable space - int[] stack; // Operand/call stack, grows upwards + int[] stack; // Operand stack, grows upwards public boolean trace = false; @@ -87,26 +85,6 @@ protected void cpu() { a = stack[sp--]; stack[++sp] = (a == b) ? TRUE : FALSE; 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; case BR : ip = code[ip++]; break;