diff --git a/src/vm/Bytecode.java b/src/vm/Bytecode.java index e3acfbc..ff78a40 100644 --- a/src/vm/Bytecode.java +++ b/src/vm/Bytecode.java @@ -19,8 +19,13 @@ 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 @@ -28,8 +33,11 @@ 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 2557548..c473574 100644 --- a/src/vm/Test.java +++ b/src/vm/Test.java @@ -2,22 +2,19 @@ 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 = { - ICONST, 1234, + ICONST, 1, + ICONST, 2, + IADD, PRINT, HALT }; @@ -47,6 +44,7 @@ public class Test { HALT // 24 }; +<<<<<<< HEAD static int[] factorial = { //.def fact: ARGS=1, LOCALS=0 ADDRESS // IF N < 2 RETURN 1 @@ -75,6 +73,14 @@ 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 52500ca..4330603 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; @@ -59,7 +57,7 @@ protected void cpu() { int opcode = code[ip]; int a,b,addr,offset; while (opcode!= HALT && ip < code.length) { - if ( trace ) System.out.printf("%-35s", disInstr()); + if ( trace ) System.err.printf("%-35s", disInstr()); ip++; //jump to next instruction or to operand switch (opcode) { case IADD: @@ -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; @@ -146,11 +124,11 @@ protected void cpu() { default : throw new Error("invalid opcode: "+opcode+" at ip="+(ip-1)); } - if ( trace ) System.out.println(stackString()); + if ( trace ) System.err.println(stackString()); opcode = code[ip]; } - if ( trace ) System.out.printf("%-35s", disInstr()); - if ( trace ) System.out.println(stackString()); + if ( trace ) System.err.printf("%-35s", disInstr()); + if ( trace ) System.err.println(stackString()); if ( trace ) dumpDataMemory(); } @@ -187,12 +165,12 @@ protected String disInstr() { } protected void dumpDataMemory() { - System.out.println("Data memory:"); + System.err.println("Data memory:"); int addr = 0; for (int o : globals) { - System.out.printf("%04d: %s\n", addr, o); + System.err.printf("%04d: %s\n", addr, o); addr++; } - System.out.println(); + System.err.println(); } }