Skip to content

Commit

Permalink
merge From master
Browse files Browse the repository at this point in the history
  • Loading branch information
parrt committed May 13, 2014
2 parents 469b0ec + 4eea301 commit 98ec2e4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
8 changes: 8 additions & 0 deletions src/vm/Bytecode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,25 @@ 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
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;
<<<<<<< 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, // <INVALID>
Expand Down
18 changes: 12 additions & 6 deletions src/vm/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
38 changes: 8 additions & 30 deletions src/vm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand All @@ -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;

Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}
}

0 comments on commit 98ec2e4

Please sign in to comment.