Arrays and structs kinda work...
This commit is contained in:
57
src/c0vm.js
57
src/c0vm.js
@@ -16,6 +16,21 @@ function c0_memory_error(val) {
|
||||
throw ("c0 memory error: " + val);
|
||||
}
|
||||
|
||||
function i32_to_array(i32) {
|
||||
return [(i32 & 0xFF),
|
||||
((i32 >> 8) & 0xFF),
|
||||
((i32 >> 16) & 0xFF),
|
||||
((i32 >> 24) & 0xFF)];
|
||||
|
||||
}
|
||||
|
||||
function array_to_i32(array) {
|
||||
return array[0] +
|
||||
(array[1] << 8) +
|
||||
(array[2] << 16) +
|
||||
(array[3] << 24);
|
||||
}
|
||||
|
||||
var StackFrame = function(file, f) {
|
||||
log("Creating stack frame");
|
||||
this.stack = [];
|
||||
@@ -375,13 +390,49 @@ ProgramState.prototype.step = function() {
|
||||
case op.IMLOAD:
|
||||
var addr = this.pop();
|
||||
// Get int32 from bytes
|
||||
var val = this.heapdsfjsldkfjsd
|
||||
this.push(this.heap[addr]);
|
||||
var c1 = this.heap[addr];
|
||||
var c2 = this.heap[addr+1];
|
||||
var c3 = this.heap[addr+2];
|
||||
var c4 = this.heap[addr+3];
|
||||
var combined = array_to_i32([c1, c2, c3, c4]);
|
||||
this.push(combined);
|
||||
this.frame.pc++;
|
||||
break;
|
||||
|
||||
case op.IMSTORE:
|
||||
|
||||
var value = this.pop();
|
||||
var addr = this.pop();
|
||||
var array = i32_to_array(value);
|
||||
|
||||
for (var i = 0; i < 4; i++)
|
||||
this.heap[addr + i] = array[i];
|
||||
this.frame.pc++;
|
||||
|
||||
case op.AMLOAD:
|
||||
var addr = this.pop();
|
||||
this.push(this.heap[addr]);
|
||||
this.frame.pc++;
|
||||
break;
|
||||
|
||||
case op.AMSTORE:
|
||||
var value = this.pop();
|
||||
var addr = this.pop();
|
||||
this.heap[addr] = value;
|
||||
this.frame.pc++;
|
||||
break;
|
||||
|
||||
case op.CMLOAD:
|
||||
var addr = this.pop();
|
||||
this.push(this.heap[addr]);
|
||||
this.frame.pc++;
|
||||
break;
|
||||
|
||||
case op.CMSTORE:
|
||||
var value = this.pop();
|
||||
var addr = this.pop();
|
||||
this.heap[addr] = value;
|
||||
this.frame.pc++;
|
||||
break;
|
||||
|
||||
default:
|
||||
var opcode_name;
|
||||
|
||||
@@ -14,10 +14,12 @@ callbacks = {};
|
||||
callbacks[c0ffi.NATIVE_PRINT] = function(args) {
|
||||
console.log("Print function says: " + args[0]);
|
||||
}
|
||||
callbacks["printint"] = function(args) {
|
||||
callbacks[c0ffi.NATIVE_PRINTINT] = function(args) {
|
||||
console.log("Printint function says: " + args[0]);
|
||||
}
|
||||
|
||||
var file = parser.parse("../test/sample2.5.c0.bc0");
|
||||
console.log(callbacks);
|
||||
|
||||
var file = parser.parse("../test/structs.c0.bc0");
|
||||
console.log("Result is " + c0vm.execute(file, callbacks));
|
||||
|
||||
|
||||
@@ -49,6 +49,6 @@ B0 # return #
|
||||
|
||||
00 02 # native count
|
||||
# native pool
|
||||
00 01 00 06 # print
|
||||
00 01 00 09 # printint
|
||||
00 01 00 10 # print
|
||||
00 01 00 13 # printint
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ exports.testArrays = function(test) {
|
||||
exports.testStructs = function(test) {
|
||||
printout = "";
|
||||
var result = c0vm.execute(parser.parse("structs.c0.bc0"), callbacks, false);
|
||||
test.ok(printout == "expected result here",
|
||||
test.ok(printout == "potato chip123",
|
||||
"structs.c0.bc0 - Did not print to screen correctly, result was " +
|
||||
printout);
|
||||
test.done();
|
||||
|
||||
Reference in New Issue
Block a user