Skip to content

Commit

Permalink
add the function that tests the local variable allocation; update CAL…
Browse files Browse the repository at this point in the history
…L so that it allocates space for locals
  • Loading branch information
parrt committed Apr 15, 2015
1 parent bb6674f commit 6daee2b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
38 changes: 35 additions & 3 deletions src/vm/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static vm.Bytecode.LOAD;
import static vm.Bytecode.PRINT;
import static vm.Bytecode.RET;
import static vm.Bytecode.STORE;

public class Test {
static int[] hello = {
Expand Down Expand Up @@ -85,14 +86,45 @@ public class Test {
PRINT, // 25
HALT // 26
};

static FuncMetaData[] metadata = {
static FuncMetaData[] factorial_metadata = {
//.def factorial: ARGS=1, LOCALS=0 ADDRESS
new FuncMetaData("factorial", 1, 0, FACTORIAL_ADDRESS)
};


static int[] f = {
// ADDRESS
//.def main() { print f(10); }
ICONST, 10, // 0
CALL, 0, // 2
PRINT, // 4
HALT, // 5
//.def f(x): ARGS=1, LOCALS=1
// a = x;
LOAD, 0, // 6 <-- start of f
STORE, 1,
// return 2*a
LOAD, 1,
ICONST, 2,
IMUL,
RET
};
static FuncMetaData[] f_metadata = {
//.def factorial: ARGS=1, LOCALS=0 ADDRESS
new FuncMetaData("f", 1, 1, 6)
};


public static void main(String[] args) {
VM vm = new VM(factorial, MAIN_ADDRESS, 0, metadata);
VM vm = new VM(factorial, MAIN_ADDRESS, 0, factorial_metadata);
vm.trace = true;
vm.exec();

vm = new VM(f, 0, 2, f_metadata);
vm.trace = true;
vm.exec();

vm = new VM(loop, 0, 2, null);
vm.trace = true;
vm.exec();
}
Expand Down
4 changes: 2 additions & 2 deletions src/vm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ protected void cpu() {
// expects all args on stack
int findex = code[ip++]; // index of target function
int nargs = metadata[findex].nargs; // how many args got pushed
callstack[++callsp] = new Context(ip,nargs);
int nlocals = metadata[findex].nlocals;
callstack[++callsp] = new Context(ip,nargs+nlocals);
// copy args into new context
for (int i=0; i<nargs; i++) {
callstack[callsp].locals[i] = stack[sp-i];
}
sp -= nargs;
ip = metadata[findex].address; // jump to function
// TODO: code preamble of func must push space for locals
break;
case RET:
ip = callstack[callsp--].returnip;
Expand Down

0 comments on commit 6daee2b

Please sign in to comment.