Merge branch 'master' of https://github.com/aarongut/cdb into mplamann
This commit is contained in:
28
src/c0vm.js
28
src/c0vm.js
@@ -16,6 +16,13 @@ function c0_memory_error(val) {
|
||||
throw ("c0 memory error: " + val);
|
||||
}
|
||||
|
||||
function num_to_i32(num) {
|
||||
log("num is 0x" + num.toString(16));
|
||||
log("num & 0x7FFFFFFF is " + (num & 0x7FFFFFFF));
|
||||
log("neg factor is " + ( (num & 0x80000000)));
|
||||
return (num & 0x7FFFFFFF) + ((num & 0x80000000));
|
||||
}
|
||||
|
||||
function i32_to_array(i32) {
|
||||
return [(i32 & 0xFF),
|
||||
((i32 >> 8) & 0xFF),
|
||||
@@ -154,19 +161,20 @@ ProgramState.prototype.step = function() {
|
||||
var y = this.pop();
|
||||
var x = this.pop();
|
||||
log("Adding " + x + " and " + y);
|
||||
this.push((x+y) % 0x100000000);
|
||||
this.push(num_to_i32(x+y));
|
||||
break;
|
||||
case op.ISUB:
|
||||
this.frame.pc++;
|
||||
var y = this.pop();
|
||||
var x = this.pop();
|
||||
this.push((x-y) % 0x100000000);
|
||||
log("Subtracting " + x + " and " + y);
|
||||
this.push(num_to_i32(x-y));
|
||||
break;
|
||||
case op.IMUL:
|
||||
this.frame.pc++;
|
||||
var y = this.pop();
|
||||
var x = this.pop();
|
||||
this.push((x*y) % 0x100000000);
|
||||
this.push(num_to_i32(x*y));
|
||||
break;
|
||||
case op.IDIV:
|
||||
this.frame.pc++;
|
||||
@@ -231,7 +239,9 @@ ProgramState.prototype.step = function() {
|
||||
case op.VSTORE:
|
||||
this.frame.pc += 2;
|
||||
var index = this.frame.program[this.frame.pc-1];
|
||||
this.frame.variables[index] = this.pop();
|
||||
var val = this.pop();
|
||||
this.frame.variables[index] = val;
|
||||
log("Set variable " + index + " to value " + val);
|
||||
break;
|
||||
case op.ACONST_NULL:
|
||||
this.frame.pc++;
|
||||
@@ -251,7 +261,7 @@ ProgramState.prototype.step = function() {
|
||||
var c2 = this.frame.program[this.frame.pc-1];
|
||||
var index = (c1 * 0x1000) + c2;
|
||||
|
||||
this.push(this.file.string_pool[index]);
|
||||
this.push(this.file.string_from_index(index));
|
||||
break;
|
||||
|
||||
// Control flow
|
||||
@@ -330,6 +340,8 @@ ProgramState.prototype.step = function() {
|
||||
};
|
||||
console.log("Unknown native function index " + f.function_table_index);
|
||||
}
|
||||
log("Calling native function with index " + index + " with arguments " +
|
||||
arg_array);
|
||||
this.push(native_function(arg_array));
|
||||
break;
|
||||
|
||||
@@ -385,7 +397,7 @@ ProgramState.prototype.step = function() {
|
||||
var array_length = this.heap[index];
|
||||
var elt_size = this.heap[index+1];
|
||||
if (elt_index >= array_length) c0_memory_error("Array index out of bounds.");
|
||||
this.push(this.heap[index + 2 + elt_size*elt_index]);
|
||||
this.push(index + 2 + (elt_size*elt_index));
|
||||
this.frame.pc++;
|
||||
break;
|
||||
|
||||
@@ -457,6 +469,8 @@ function execute(file, callbacks, v) {
|
||||
|
||||
var state = new ProgramState(file, callbacks);
|
||||
|
||||
if (verbose) log(file);
|
||||
|
||||
log("Beginning execution");
|
||||
|
||||
while (true) {
|
||||
@@ -469,7 +483,7 @@ function execute(file, callbacks, v) {
|
||||
console.log(" Stack: " + state.frame.stack);
|
||||
console.log(" PC: " + state.frame.pc);
|
||||
console.log(" Vars: " + state.frame.variables);
|
||||
console.log(" Code: " + state.frame.program);
|
||||
// console.log(" Code: " + state.frame.program);
|
||||
console.log(" Heap: " + state.heap);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user