-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsim_main.cpp
More file actions
262 lines (235 loc) · 7.63 KB
/
Copy pathsim_main.cpp
File metadata and controls
262 lines (235 loc) · 7.63 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "linx/model/sim_main.hpp"
#include <stdexcept>
#include <string>
namespace linx::model::detail {
std::uint64_t ParseUnsignedArg(std::string_view text) {
std::size_t consumed = 0;
const std::string owned(text);
const auto value = std::stoull(owned, &consumed, 0);
LINX_MODEL_ASSERT(consumed == owned.size());
return value;
}
void PrintUsage(std::ostream &os, const char *program) {
os << "Usage: " << program << " [options]\n"
<< " --engine <kind> ref|ca|compare\n"
<< " --bin <path> Load ELF or raw binary image\n"
<< " --raw-base <addr> Base address for raw non-ELF binaries\n"
<< " --emit-minst-trace <path>\n"
<< " Emit Minst-native JSONL trace records\n"
<< " --result-dump <path>\n"
<< " --result-address <addr>\n"
<< " --result-size <bytes>\n"
<< " Export architecture-visible result memory\n"
<< " --disasm Print program disassembly before simulation\n"
<< " --disasm-only Print program disassembly and exit\n"
<< " --compare-window <n> Buffered compare window size (default 128)\n"
<< " --stop-pc <pc> Stop/reference PC in hex or decimal\n"
<< " --max-cycles <n> Terminate after n cycles\n"
<< " --no-reference Skip RunReference() in the main loop\n"
<< " --no-pipeview Skip PrintPipeView() in the main loop\n"
<< " --no-report Skip Report() at shutdown\n"
<< " --log-level <lvl> trace|debug|info|warn|error|fatal\n"
<< " -h, --help Show this help text\n";
}
std::optional<SimMainArgs> ParseSimMainArgs(int argc, char **argv, std::ostream &out,
std::ostream &err, int &exit_code) {
SimMainArgs args;
for (int i = 1; i < argc; ++i) {
const std::string_view arg(argv[i]);
if (Matches(arg, "-h") || Matches(arg, "--help")) {
PrintUsage(out, argv[0]);
exit_code = 0;
return std::nullopt;
}
if (Matches(arg, "--stop-pc")) {
if (i + 1 >= argc) {
err << "missing value for --stop-pc\n";
exit_code = 1;
return std::nullopt;
}
try {
args.stop_pc = ParseUnsignedArg(argv[++i]);
} catch (const std::exception &) {
err << "invalid integer for --stop-pc: " << argv[i] << '\n';
exit_code = 1;
return std::nullopt;
}
continue;
}
if (Matches(arg, "--engine")) {
if (i + 1 >= argc) {
err << "missing value for --engine\n";
exit_code = 1;
return std::nullopt;
}
args.engine = argv[++i];
if (args.engine != "ref" && args.engine != "ca" && args.engine != "compare") {
err << "invalid engine: " << args.engine << '\n';
exit_code = 1;
return std::nullopt;
}
continue;
}
if (Matches(arg, "--bin")) {
if (i + 1 >= argc) {
err << "missing value for --bin\n";
exit_code = 1;
return std::nullopt;
}
args.program_path = std::string(argv[++i]);
continue;
}
if (Matches(arg, "--raw-base")) {
if (i + 1 >= argc) {
err << "missing value for --raw-base\n";
exit_code = 1;
return std::nullopt;
}
try {
args.raw_base_address = ParseUnsignedArg(argv[++i]);
} catch (const std::exception &) {
err << "invalid integer for --raw-base: " << argv[i] << '\n';
exit_code = 1;
return std::nullopt;
}
continue;
}
if (Matches(arg, "--disasm")) {
args.enable_disassembly = true;
continue;
}
if (Matches(arg, "--disasm-only")) {
args.enable_disassembly = true;
args.disassembly_only = true;
continue;
}
if (Matches(arg, "--emit-minst-trace")) {
if (i + 1 >= argc) {
err << "missing value for --emit-minst-trace\n";
exit_code = 1;
return std::nullopt;
}
args.emit_trace_path = std::string(argv[++i]);
continue;
}
if (Matches(arg, "--result-dump")) {
if (i + 1 >= argc) {
err << "missing value for --result-dump\n";
exit_code = 1;
return std::nullopt;
}
args.result_dump_path = std::string(argv[++i]);
continue;
}
if (Matches(arg, "--result-address") || Matches(arg, "--result-size")) {
if (i + 1 >= argc) {
err << "missing value for " << arg << '\n';
exit_code = 1;
return std::nullopt;
}
try {
const auto value = ParseUnsignedArg(argv[++i]);
if (Matches(arg, "--result-address")) {
args.result_address = value;
} else {
args.result_size = value;
}
} catch (const std::exception &) {
err << "invalid integer for " << arg << ": " << argv[i] << '\n';
exit_code = 1;
return std::nullopt;
}
continue;
}
if (Matches(arg, "--max-cycles")) {
if (i + 1 >= argc) {
err << "missing value for --max-cycles\n";
exit_code = 1;
return std::nullopt;
}
try {
args.max_cycles = ParseUnsignedArg(argv[++i]);
} catch (const std::exception &) {
err << "invalid integer for --max-cycles: " << argv[i] << '\n';
exit_code = 1;
return std::nullopt;
}
continue;
}
if (Matches(arg, "--compare-window")) {
if (i + 1 >= argc) {
err << "missing value for --compare-window\n";
exit_code = 1;
return std::nullopt;
}
try {
args.compare_window = static_cast<std::size_t>(ParseUnsignedArg(argv[++i]));
} catch (const std::exception &) {
err << "invalid integer for --compare-window: " << argv[i] << '\n';
exit_code = 1;
return std::nullopt;
}
if (args.compare_window == 0U) {
err << "--compare-window must be > 0\n";
exit_code = 1;
return std::nullopt;
}
continue;
}
if (Matches(arg, "--no-reference")) {
args.enable_reference = false;
continue;
}
if (Matches(arg, "--no-pipeview")) {
args.enable_pipeview = false;
continue;
}
if (Matches(arg, "--no-report")) {
args.enable_report = false;
continue;
}
if (Matches(arg, "--log-level")) {
if (i + 1 >= argc) {
err << "missing value for --log-level\n";
exit_code = 1;
return std::nullopt;
}
const auto level = ParseLogLevel(argv[++i]);
if (!level.has_value()) {
err << "invalid log level: " << argv[i] << '\n';
exit_code = 1;
return std::nullopt;
}
args.log_level = *level;
continue;
}
if (!arg.empty() && arg.front() != '-') {
if (args.program_path.has_value()) {
err << "multiple input binaries provided\n";
exit_code = 1;
return std::nullopt;
}
args.program_path = std::string(arg);
continue;
}
err << "unknown option: " << arg << '\n';
exit_code = 1;
return std::nullopt;
}
const unsigned result_option_count = static_cast<unsigned>(args.result_dump_path.has_value()) +
static_cast<unsigned>(args.result_address.has_value()) +
static_cast<unsigned>(args.result_size.has_value());
if (result_option_count != 0U && result_option_count != 3U) {
err << "--result-dump, --result-address, and --result-size must be specified together\n";
exit_code = 1;
return std::nullopt;
}
if (args.result_size == 0) {
err << "--result-size must be greater than zero\n";
exit_code = 1;
return std::nullopt;
}
exit_code = 0;
return args;
}
} // namespace linx::model::detail