From d39df5314dba662a3fb587f98830c5a246623d16 Mon Sep 17 00:00:00 2001 From: Mitchell Plamann Date: Sun, 19 Apr 2015 19:11:20 -0400 Subject: [PATCH 1/3] Fixed strings --- src/c0vm.js | 19 +++++++++---------- test/tests.js | 23 +++++++++++++++++------ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/c0vm.js b/src/c0vm.js index 3bce00e..768964c 100755 --- a/src/c0vm.js +++ b/src/c0vm.js @@ -28,7 +28,6 @@ function i32_to_array(i32) { ((i32 >> 8) & 0xFF), ((i32 >> 16) & 0xFF), ((i32 >> 24) & 0xFF)]; - } function array_to_i32(array) { @@ -63,13 +62,13 @@ var ProgramState = function(parsed_file, callback_dict) { try { this.natives[i] = callback_dict[i]; } catch (key_not_found) { - dict[i] = function (arg) { + this.natives[i] = function (arg) { console.log("Native function " + name + " called, ran method stub."); return 0; }; } } - + // Memory is just a big array of bytes, right? // "Allocation" is appending onto this array // A pointer to memory is an index into this array. @@ -138,7 +137,7 @@ ProgramState.prototype.step = function() { case op.BIPUSH: this.frame.pc += 2; var val = this.frame.program[this.frame.pc-1]; - + // Do sign extension if necessary if ((val & 0x80) != 0) val = -0x80 + (val & 0x7F); @@ -340,9 +339,9 @@ ProgramState.prototype.step = function() { }; console.log("Unknown native function index " + f.function_table_index); } - log("Calling native function with index " + index + " with arguments " + + log("Calling native function with index " + index + " with arguments " + arg_array); - this.push(native_function(arg_array)); + this.push(native_function(arg_array, this)); break; // Memory allocation operations: @@ -352,7 +351,7 @@ ProgramState.prototype.step = function() { var address = this.heap.length; for (var i = 0; i < size; i++) this.heap.push(0); - + this.push(address); this.frame.pc += 2; break; @@ -365,7 +364,7 @@ ProgramState.prototype.step = function() { this.heap.push(num_elements); this.heap.push(size); - + for (var i = 0; i < num_elements; i++) { for (var j = 0; j < size; j++) this.heap.push(0); @@ -481,7 +480,7 @@ function execute(file, callbacks, v) { if (verbose) log(file); log("Beginning execution"); - + while (true) { var val = state.step(); if (val !== undefined) return val; @@ -500,7 +499,7 @@ function execute(file, callbacks, v) { // save state (maybe in a global in this file?) // return; // } - } + } } exports.execute = execute; diff --git a/test/tests.js b/test/tests.js index 9d6a198..ad533a2 100644 --- a/test/tests.js +++ b/test/tests.js @@ -24,13 +24,25 @@ callbacks[c0ffi.NATIVE_STRING_LENGTH] = function(args) { return args[0].length; } -callbacks[c0ffi.NATIVE_STRING_TO_CHARARRAY] = function(args) { - return args[0]; +callbacks[c0ffi.NATIVE_STRING_TO_CHARARRAY] = function(args, vm) { + var address = vm.heap.length; + vm.heap.push(args[0].length+1); + vm.heap.push(1); + for (var i = 0; i < args[0].length; i++) { + vm.heap.push(args[0][i]); + } + vm.heap.push(0); + return address; } -callbacks[c0ffi.NATIVE_STRING_FROM_CHARARRAY] = function(args) { - console.log("string_from_chararray: " + args); - return args[0]; +callbacks[c0ffi.NATIVE_STRING_FROM_CHARARRAY] = function(args, vm) { + var i = args[0] + 2; + var result = ""; + while (vm.heap[i] !== 0) { + result += vm.heap[i]; + i++; + } + return result; } callbacks[c0ffi.NATIVE_CHAR_CHR] = function(args) { @@ -38,7 +50,6 @@ callbacks[c0ffi.NATIVE_CHAR_CHR] = function(args) { } callbacks[c0ffi.NATIVE_CHAR_ORD] = function(args) { - console.log("native_car_ord: " + args); if (typeof args[0] == "string") return args[0].charCodeAt(0); return args[0]; From 6138821c3af6071650ee16e894e0b7cf67787c2e Mon Sep 17 00:00:00 2001 From: Mitchell Plamann Date: Sun, 19 Apr 2015 19:18:49 -0400 Subject: [PATCH 2/3] Moved non-IO callbacks to c0ffi.js --- src/c0ffi.js | 43 +++++++++++++++++++++++++++++++++++++++++++ test/tests.js | 38 ++------------------------------------ 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/c0ffi.js b/src/c0ffi.js index c3ce859..d8b1412 100644 --- a/src/c0ffi.js +++ b/src/c0ffi.js @@ -108,3 +108,46 @@ exports.NATIVE_STRING_TERMINATED = 92 exports.NATIVE_STRING_TO_CHARARRAY = 93 exports.NATIVE_STRING_TOLOWER = 94 +callbacks = {}; +callbacks[exports.NATIVE_STRING_LENGTH] = + function(args) { + return args[0].length; + }; + +callbacks[exports.NATIVE_STRING_TO_CHARARRAY] = + function(args, vm) { + var address = vm.heap.length; + vm.heap.push(args[0].length+1); + vm.heap.push(1); + for (var i = 0; i < args[0].length; i++) { + vm.heap.push(args[0][i]); + } + vm.heap.push(0); + return address; + }; + + +callbacks[exports.NATIVE_STRING_FROM_CHARARRAY] = + function(args, vm) { + var i = args[0] + 2; + var result = ""; + while (vm.heap[i] !== 0) { + result += vm.heap[i]; + i++; + } + return result; + }; + +callbacks[exports.NATIVE_CHAR_CHR] = + function(args) { + return String.fromCharCode(args[0]); + }; + +callbacks[exports.NATIVE_CHAR_ORD] = + function(args) { + if (typeof args[0] == "string") + return args[0].charCodeAt(0); + return args[0]; + }; + +exports.default_callbacks = callbacks; diff --git a/test/tests.js b/test/tests.js index ad533a2..96e1d7d 100644 --- a/test/tests.js +++ b/test/tests.js @@ -2,7 +2,8 @@ parser = require("../src/bytecode-parser.js"); c0vm = require("../src/c0vm.js"); c0ffi = require("../src/c0ffi.js"); -var callbacks = {} +var callbacks = c0ffi.default_callbacks; +console.log("Initial callbacks: " + callbacks[c0ffi.NATIVE_STRING_LENGTH](["hi"])); var printout = ""; callbacks[c0ffi.NATIVE_PRINT] = function(args) { @@ -20,41 +21,6 @@ callbacks[c0ffi.NATIVE_PRINTLN] = function(args) { return 0; } -callbacks[c0ffi.NATIVE_STRING_LENGTH] = function(args) { - return args[0].length; -} - -callbacks[c0ffi.NATIVE_STRING_TO_CHARARRAY] = function(args, vm) { - var address = vm.heap.length; - vm.heap.push(args[0].length+1); - vm.heap.push(1); - for (var i = 0; i < args[0].length; i++) { - vm.heap.push(args[0][i]); - } - vm.heap.push(0); - return address; -} - -callbacks[c0ffi.NATIVE_STRING_FROM_CHARARRAY] = function(args, vm) { - var i = args[0] + 2; - var result = ""; - while (vm.heap[i] !== 0) { - result += vm.heap[i]; - i++; - } - return result; -} - -callbacks[c0ffi.NATIVE_CHAR_CHR] = function(args) { - return String.fromCharCode(args[0]); -} - -callbacks[c0ffi.NATIVE_CHAR_ORD] = function(args) { - if (typeof args[0] == "string") - return args[0].charCodeAt(0); - return args[0]; -} - function doTest(filename, expected_result) { return function(test) { var result = c0vm.execute(parser.parse(filename), callbacks, false); From 349fd6ff7886ffb14e68e7cfd043fe4505834ac3 Mon Sep 17 00:00:00 2001 From: Mitchell Plamann Date: Sun, 19 Apr 2015 19:48:45 -0400 Subject: [PATCH 3/3] Start of a breakpoints system --- src/bytecode-parser.js | 1 + src/c0vm.js | 49 ++++++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/bytecode-parser.js b/src/bytecode-parser.js index 891a1af..00f8da9 100644 --- a/src/bytecode-parser.js +++ b/src/bytecode-parser.js @@ -85,6 +85,7 @@ var Bc0File = function (filename) { this.function_pool = []; for (var i = 0; i < this.function_count; i++) { this.function_pool.push(new FunctionInfo(stream)); + this.function_pool[i].function_id = i; } this.native_count = stream.get_u2(); diff --git a/src/c0vm.js b/src/c0vm.js index 768964c..a7e7414 100755 --- a/src/c0vm.js +++ b/src/c0vm.js @@ -42,6 +42,7 @@ var StackFrame = function(file, f) { this.stack = []; this.pc = 0; this.program = f.code; + this.function_id = f.function_id; this.variables = []; for (var i = 0; i < f.num_vars; i++) this.variables.push(0); @@ -69,6 +70,8 @@ var ProgramState = function(parsed_file, callback_dict) { } } + this.breakpoints = []; + // Memory is just a big array of bytes, right? // "Allocation" is appending onto this array // A pointer to memory is an index into this array. @@ -470,8 +473,11 @@ ProgramState.prototype.step = function() { } } -// Takes in a parsed .bc0 file and runs it -function execute(file, callbacks, v) { +ProgramState.prototype.set_breakpoint = function(function_index, opcode_index) { + this.breakpoints.push([function_index, opcode_index]); +} + +function initialize_vm(file, callbacks, v) { verbose = typeof v !== 'undefined' ? v : true; log("Initializing with file " + file); @@ -481,25 +487,40 @@ function execute(file, callbacks, v) { log("Beginning execution"); + return state; +} + +function run_vm(vm) { while (true) { - var val = state.step(); + for (breakpoint in vm.breakpoints) { + if (vm.frame.function_id == breakpoint[0] && + vm.frame.pc == breakpoint[1]) { + console.log("Breakpoint reached!"); + return vm; + } + } + + var val = vm.step(); if (val !== undefined) return val; if (verbose) { - console.log("Machine state:"); + console.log("Machine vm:"); console.log(" Current Stack Frame:"); - 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(" Heap: " + state.heap); + console.log(" Stack: " + vm.frame.stack); + console.log(" PC: " + vm.frame.pc); + console.log(" Vars: " + vm.frame.variables); + // console.log(" Code: " + vm.frame.program); + console.log(" Heap: " + vm.heap); } - - // if (at_breakpoint) { - // save state (maybe in a global in this file?) - // return; - // } } } +// Takes in a parsed .bc0 file and runs it +function execute(file, callbacks, v) { + var state = initialize_vm(file, callbacks, v); + return run_vm(state); +} + exports.execute = execute; +exports.initialize_vm = initialize_vm; +exports.run_vm = run_vm;