-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmain.cpp
More file actions
165 lines (142 loc) · 4.64 KB
/
main.cpp
File metadata and controls
165 lines (142 loc) · 4.64 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <b9/ExecutionContext.hpp>
#include <b9/compiler/Compiler.hpp>
#include <b9/deserialize.hpp>
#include <OMR/GC/StackRoot.hpp>
#include <OMR/Om/Context.hpp>
#include <OMR/Runtime.hpp>
#include <strings.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
/// B9run's usage string. Printed when run with -help.
static const char* usage =
"Usage: b9run [<option>...] [--] <module> [<arg>...]\n"
" Or: b9run -help\n"
"Jit Options:\n"
" -jit: Enable the jit\n"
" -directcall: make direct jit to jit calls\n"
" -passparam: Pass arguments in CPU registers\n"
" -lazyvmstate: Only update the VM state as needed\n"
"Run Options:\n"
" -inline <n>: Set the jit's max inline depth (default: 0)\n"
" -debug: Enable debug code\n"
" -verbose: Run with verbose printing\n"
" -help: Print this help message";
/// The b9run program's global configuration.
struct RunConfig {
b9::Config b9;
const char* moduleName = "";
const char* mainFunction = "<script>";
bool verbose = false;
std::vector<b9::StackElement> usrArgs;
};
std::ostream& operator<<(std::ostream& out, const RunConfig& cfg) {
out << "Module: " << cfg.moduleName << std::endl;
out << "Arguments: [ ";
for (const auto& arg : cfg.usrArgs) {
out << arg << " ";
}
out << "]" << std::endl;
out << cfg.b9;
return out;
}
/// Parse CLI arguments and set up the config.
static bool parseArguments(RunConfig& cfg, const int argc, char* argv[]) {
std::size_t i = 1;
for (; i < argc; i++) {
const char* arg = argv[i];
if (strcasecmp(arg, "-help") == 0) {
std::cout << usage << std::endl;
exit(EXIT_SUCCESS);
} else if (strcasecmp(arg, "-inline") == 0) {
cfg.b9.maxInlineDepth = atoi(argv[++i]);
} else if (strcasecmp(arg, "-verbose") == 0) {
cfg.verbose = true;
cfg.b9.verbose = true;
} else if (strcasecmp(arg, "-debug") == 0) {
cfg.b9.debug = true;
} else if (strcasecmp(arg, "-jit") == 0) {
cfg.b9.jit = true;
} else if (strcasecmp(arg, "-directcall") == 0) {
cfg.b9.directCall = true;
} else if (strcasecmp(arg, "-passparam") == 0) {
cfg.b9.passParam = true;
} else if (strcasecmp(arg, "-lazyvmstate") == 0) {
cfg.b9.lazyVmState = true;
} else if (strcmp(arg, "--") == 0) {
i++;
break;
} else if (strcmp(arg, "-") == 0) {
std::cerr << "Unrecognized option: " << arg << std::endl;
return false;
} else {
break;
}
}
// check for user defined module
if (i < argc) {
cfg.moduleName = argv[i++];
} else {
std::cerr << "No module name given to b9run" << std::endl;
return false;
}
// check for user defined arguments
for (; i < argc; i++) {
cfg.usrArgs.push_back(Om::Value(Om::AS_INT48, std::atoi(argv[i])));
}
// check that dependent options are enabled
if (cfg.b9.directCall && !cfg.b9.jit) {
std::cerr << "-directcall requires -jit" << std::endl;
return false;
}
if (cfg.b9.passParam && !cfg.b9.directCall) {
std::cerr << "-passparam requires -directcall" << std::endl;
return false;
}
if (cfg.b9.lazyVmState && !cfg.b9.passParam) {
std::cerr << "-lazyvmstate requires -passparam" << std::endl;
return false;
}
return true;
}
static void run(OMR::Runtime& runtime, const RunConfig& cfg) {
b9::VirtualMachine vm{runtime, cfg.b9};
std::ifstream file(cfg.moduleName, std::ios_base::in | std::ios_base::binary);
auto module = b9::deserialize(file);
vm.load(module);
if (cfg.b9.jit) {
vm.generateAllCode();
}
size_t functionIndex = module->getFunctionIndex(cfg.mainFunction);
auto result = vm.run(functionIndex, cfg.usrArgs);
std::cout << std::endl << "=> " << result << std::endl;
}
int main(int argc, char* argv[]) {
OMR::Runtime runtime;
RunConfig cfg;
if (!parseArguments(cfg, argc, argv)) {
std::cerr << usage << std::endl;
exit(EXIT_FAILURE);
}
if (cfg.verbose) {
std::cout << cfg << std::endl << std::endl;
}
try {
run(runtime, cfg);
} catch (const b9::DeserializeException& e) {
std::cerr << "Failed to load module: " << e.what() << std::endl;
exit(EXIT_FAILURE);
} catch (const b9::FunctionNotFoundException& e) {
std::cerr << "Failed to find function: " << e.what() << std::endl;
exit(EXIT_FAILURE);
} catch (const b9::BadFunctionCallException& e) {
std::cerr << "Failed to call function " << e.what() << std::endl;
exit(EXIT_FAILURE);
} catch (const b9::CompilationException& e) {
std::cerr << "Failed to compile function: " << e.what() << std::endl;
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}