From 349fd6ff7886ffb14e68e7cfd043fe4505834ac3 Mon Sep 17 00:00:00 2001 From: Mitchell Plamann Date: Sun, 19 Apr 2015 19:48:45 -0400 Subject: [PATCH] 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;