From 9e2406e086bd3c8f6b88f4131f5305880d419a7f Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Fri, 9 May 2014 21:40:26 -0700 Subject: [PATCH] got intial interp going. --- src/vm/Bytecode.java | 55 ++++++++++++++ src/vm/Test.java | 57 ++++++++++++++ src/vm/VM.java | 172 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 src/vm/Bytecode.java create mode 100644 src/vm/Test.java create mode 100644 src/vm/VM.java diff --git a/src/vm/Bytecode.java b/src/vm/Bytecode.java new file mode 100644 index 0000000..90425a9 --- /dev/null +++ b/src/vm/Bytecode.java @@ -0,0 +1,55 @@ +package vm; + +public class Bytecode { + public static class Instruction { + String name; // E.g., "iadd", "call" + int n = 0; + public Instruction(String name) { this(name,0); } + public Instruction(String name, int nargs) { + this.name = name; + this.n = nargs; + } + } + + // INSTRUCTION BYTECODES (byte is signed; use a short to keep 0..255) + public static final short IADD = 1; // int add + public static final short ISUB = 2; + 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 Instruction[] instructions = new Instruction[] { + null, // + new Instruction("iadd"), // index is the opcode + new Instruction("isub"), + 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), + new Instruction("brf", 1), + new Instruction("iconst", 1), + new Instruction("load", 1), + new Instruction("gload", 1), + new Instruction("store", 1), + new Instruction("gstore", 1), + new Instruction("print"), + new Instruction("pop"), + new Instruction("halt") + }; +} diff --git a/src/vm/Test.java b/src/vm/Test.java new file mode 100644 index 0000000..239dea4 --- /dev/null +++ b/src/vm/Test.java @@ -0,0 +1,57 @@ +package vm; + +import static vm.Bytecode.BR; +import static vm.Bytecode.BRF; +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.PRINT; + +public class Test { + static int[] hello = { + ICONST, 1234, + PRINT, + HALT + }; + + static int[] loop = { + // .GLOBALS 2; N, I + // N = 100000000 ADDRESS + ICONST, 100000000, // 0 + GSTORE, 0, // 2 + // I = 0 + ICONST, 0, // 4 + GSTORE, 1, // 6 + // WHILE I0 ) { + List operands = new ArrayList(); + for (int i=ip+1; i<=ip+nargs; i++) { + operands.add(String.valueOf(code[i])); + } + for (int i = 0; i < operands.size(); i++) { + String s = operands.get(i); + if ( i>0 ) System.out.print(", "); + System.out.print(s); + } + } + System.out.println(); + } + +}