Skip to content

Commit

Permalink
borked up merge. fixed now.
Browse files Browse the repository at this point in the history
  • Loading branch information
parrt committed May 13, 2014
1 parent 98ec2e4 commit 1dd8341
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
8 changes: 0 additions & 8 deletions src/vm/Bytecode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,17 @@ 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
14 changes: 5 additions & 9 deletions src/vm/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
22 changes: 22 additions & 0 deletions src/vm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 1dd8341

Please sign in to comment.