-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgvm.cpp
More file actions
77 lines (62 loc) · 1.83 KB
/
Copy pathgvm.cpp
File metadata and controls
77 lines (62 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Example host program for the GVM.
To instead use the method of a class as the host callback:
MyClass myObject;
gvm.setCallback(std::bind(&MyClass::myCallbackMethod, &myObject));
*/
#define DEBUG
#include "gvm.hpp"
#include <iostream>
#include <fstream>
GVM* vm = nullptr;
GVM::memory_t io;
void example_host_function() {
std::cout << "example_host_function() called by the bytecode, pc = " << vm->PC << std::endl;
}
int main(int argc, char* argv[]) {
std::memset(&(io[0]), 0, sizeof(uint64_t) * IO_SIZE);
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <filename> [--debug]" << std::endl;
return 1;
}
bool debug = (argc > 2);
// Load the bytecode
const char* filename = argv[1];
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Error opening file: " << filename << std::endl;
return 1;
}
std::vector<uint8_t> code(std::istreambuf_iterator<char>(file), {});
file.close();
// Run the bytecode
vm = new GVM(io, code, example_host_function);
vm->setDebug(debug);
vm->run();
std::cout << "vm.run() ended, term = " << vm->term << " opcode = " << int(vm->opcode) << std::endl;
// Dump all io (includes registers)
bool skipped = false;
for (size_t i = 0; i < IO_SIZE; ++i) {
uint64_t v = io[i];
if (v > 0 || i < REG_SIZE) {
if (skipped) {
skipped = false;
std::cout << "..." << std::endl;
}
if (i < REG_SIZE)
std::cout << "*";
std::cout << "io[" << i << "] = ";
if (v == UINT64_MAX)
std::cout << "(UINT64_MAX)";
else
std::cout << v;
std::cout << std::endl;
} else {
skipped = true;
}
}
bool success = vm->term != 0;
delete vm;
vm = nullptr;
return success;
}