forked from tenstorrent/whisper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Args.cpp
650 lines (593 loc) · 24 KB
/
Args.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
// Copyright 2020 Western Digital Corporation or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <boost/algorithm/string.hpp>
#include "Args.hpp"
using namespace WdRiscv;
static
void
printVersion()
{
unsigned version = 1;
unsigned subversion = 845;
std::cout << "Version " << version << "." << subversion << " compiled on "
<< __DATE__ << " at " << __TIME__ << '\n';
#ifdef GIT_SHA
#define xstr(x) str(x)
#define str(x) #x
std::cout << "Git SHA: " << xstr(GIT_SHA) << '\n';
#undef str
#undef xstr
#endif
}
void
Args::expandTargets()
{
this->expandedTargets.clear();
for (const auto& target : this->targets)
{
StringVec tokens;
boost::split(tokens, target, boost::is_any_of(this->targetSep),
boost::token_compress_on);
this->expandedTargets.push_back(tokens);
}
}
bool
Args::collectCommandLineValues(const boost::program_options::variables_map& varMap)
{
bool ok = true;
if (varMap.count("startpc"))
{
auto numStr = varMap["startpc"].as<std::string>();
if (not parseCmdLineNumber("startpc", numStr, this->startPc))
ok = false;
}
if (varMap.count("endpc"))
{
auto numStr = varMap["endpc"].as<std::string>();
if (not parseCmdLineNumber("endpc", numStr, this->endPc))
ok = false;
}
if (varMap.count("tohost"))
{
auto numStr = varMap["tohost"].as<std::string>();
if (not parseCmdLineNumber("tohost", numStr, this->toHost))
ok = false;
}
if (varMap.count("fromhost"))
{
auto numStr = varMap["fromhost"].as<std::string>();
if (not parseCmdLineNumber("fromhost", numStr, this->fromHost))
ok = false;
}
if (varMap.count("consoleio"))
{
auto numStr = varMap["consoleio"].as<std::string>();
if (not parseCmdLineNumber("consoleio", numStr, this->consoleIo))
ok = false;
}
if (varMap.count("maxinst"))
{
auto numStr = varMap["maxinst"].as<std::string>();
if (not parseCmdLineNumber("maxinst", numStr, this->instCountLim))
ok = false;
this->relativeInstCount = not numStr.empty() and numStr.at(0) == '+';
}
if (varMap.count("memorysize"))
{
auto numStr = varMap["memorysize"].as<std::string>();
if (not parseCmdLineNumber("memorysize", numStr, this->memorySize))
ok = false;
}
if (varMap.count("tlbsize"))
{
auto numStr = varMap["tlbsize"].as<std::string>();
if (not parseCmdLineNumber("tlbsize", numStr, this->tlbSize))
ok = false;
}
if (varMap.count("tohostsym"))
this->toHostSym = varMap["tohostsym"].as<std::string>();
if (varMap.count("consoleiosym"))
this->consoleIoSym = varMap["consoleiosym"].as<std::string>();
if (varMap.count("xlen"))
{
std::cerr << "Command line option --xlen is deprecated.\n";
this->hasRegWidth = true;
}
if (varMap.count("cores"))
this->hasCores = true;
if (varMap.count("harts"))
this->hasHarts = true;
if (varMap.count("alarm"))
{
auto numStr = varMap["alarm"].as<std::string>();
if (not parseCmdLineNumber("alarm", numStr, this->alarmInterval))
ok = false;
else if (this->alarmInterval.has_value() and *this->alarmInterval == 0)
std::cerr << "Warning: Zero alarm period ignored.\n";
}
if (varMap.count("branchwindow"))
{
auto numStr = varMap["branchwindow"].as<std::string>();
if (not parseCmdLineNumber("branchwindow", numStr, this->branchWindow))
ok = false;
}
if (varMap.count("clint"))
{
auto numStr = varMap["clint"].as<std::string>();
if (not parseCmdLineNumber("clint", numStr, this->clint))
ok = false;
else if (this->clint.has_value() and (*this->clint & 7) != 0)
{
std::cerr << "Error: clint address must be a multiple of 8\n";
ok = false;
}
}
if (varMap.count("interruptor"))
{
auto numStr = varMap["interruptor"].as<std::string>();
if (not parseCmdLineNumber("interruptor", numStr, this->interruptor))
ok = false;
else if (this->interruptor.has_value() and (*this->interruptor & 7) != 0)
{
std::cerr << "Error: interruptor address must be a multiple of 8\n";
ok = false;
}
}
if (varMap.count("syscallslam"))
{
auto numStr = varMap["syscallslam"].as<std::string>();
if (not parseCmdLineNumber("syscallslam", numStr, this->syscallSlam))
ok = false;
}
if (varMap.count("mcmls"))
{
auto numStr = varMap["mcmls"].as<std::string>();
if (not parseCmdLineNumber("mcmls", numStr, this->mcmls))
ok = false;
}
if (varMap.count("steesr"))
{
auto rangeStr = varMap["steesr"].as<std::string>();
StringVec tokens;
boost::split(tokens, rangeStr, boost::is_any_of(":"));
if (tokens.size() != 2)
{
std::cerr << "Error: Bad value for --steesr: \"" << rangeStr << "\". "
<< "Expecting a colon separated pair of numbers.\n";
ok = false;
}
else
{
uint64_t s0 = 0, s1 = 0;
if (not parseCmdLineNumber("steesr", tokens.at(0), s0) or
not parseCmdLineNumber("steesr", tokens.at(1), s1))
ok = false;
else
{
this->steesr.clear();
this->steesr.push_back(s0);
this->steesr.push_back(s1);
}
}
}
if (varMap.count("instcounter"))
{
auto numStr = varMap["instcounter"].as<std::string>();
if (not parseCmdLineNumber("instcounter", numStr, this->instCounter))
ok = false;
}
if (varMap.count("logstart"))
{
auto numStr = varMap["logstart"].as<std::string>();
if (not parseCmdLineNumber("logstart", numStr, this->logStart))
ok = false;
}
if (varMap.count("deterministic"))
{
auto rangeStr = varMap["deterministic"].as<std::string>();
StringVec tokens;
boost::split(tokens, rangeStr, boost::is_any_of(":"));
if (tokens.size() == 1)
{
uint64_t s1 = 0;
if (not parseCmdLineNumber("deterministic", tokens.at(0), s1))
ok = false;
else
{
this->deterministic.clear();
this->deterministic.push_back(1);
this->deterministic.push_back(s1);
}
}
else if (tokens.size() == 2)
{
uint64_t s0 = 0, s1 = 0;
if (not parseCmdLineNumber("deterministic", tokens.at(0), s0) or
not parseCmdLineNumber("deterministic", tokens.at(1), s1))
ok = false;
else
{
this->deterministic.clear();
this->deterministic.push_back(s0);
this->deterministic.push_back(s1);
}
}
else
{
std::cerr << "Error: Bad value for --determinstic: \"" << rangeStr << "\". "
<< "Expecting a number or a colon separated pair of numbers.\n";
ok = false;
}
}
if (varMap.count("seed"))
{
auto numStr = varMap["seed"].as<std::string>();
if (not parseCmdLineNumber("seed", numStr, this->seed))
ok = false;
}
if (this->interactive)
this->trace = true; // Enable instruction tracing in interactive mode.
return ok;
}
bool
Args::parseCmdLineArgs(std::span<char*> argv)
{
try
{
// Define command line options.
namespace po = boost::program_options;
po::options_description desc("options");
desc.add_options()
("help,h", po::bool_switch(&this->help),
"Produce this message.")
("log,l", po::bool_switch(&this->trace),
"Enable tracing to standard output of executed instructions.")
("isa", po::value(&this->isa),
"Specify instruction set extensions to enable. Supported extensions "
"are a, c, d, f, i, m, s and u. Default is imc.")
("xlen", po::value(&this->regWidth),
"Specify register width (32 or 64), defaults to 32")
("harts", po::value(&this->harts),
"Specify number of hardware threads per core (default=1).")
("cores", po::value(&this->cores),
"Specify number of core per system (default=1).")
("pagesize", po::value(&this->pageSize),
"Specify memory page size.")
("target,t", po::value(&this->targets)->multitoken(),
"Target program (ELF file) to load into simulator memory. In "
"newlib/Linux emulation mode, program options may follow program name.")
("targetsep", po::value(&this->targetSep),
"Target program argument separator.")
("hex,x", po::value(&this->hexFiles)->multitoken(),
"HEX file to load into simulator memory.")
("binary,b", po::value(&this->binaryFiles)->multitoken(),
"Binary file to load into simulator memory. File path may be suffixed with a colon followed "
"by an address (integer) in which case data will be loaded at address as opposed to zero. "
"An addional suffix of :u may be added to write back the file with the contents of memory "
"at the end of the run. "
"Example: -b file1 -b file2:0x1040 -b file3:0x20000:u")
#ifdef LZ4_COMPRESS
("lz4", po::value(&this->lz4Files)->multitoken(),
"LZ4 file to load into simulator memory.")
#endif
("kernel", po::value(&this->kernelFile),
"Kernel binary file to load into simulator memory. File will be loaded at 0x400000 for "
"rv32 or 0x200000 for rv64 unless an explicit address is specified after a colon suffix "
"to the file path.")
("testsignature", po::value(&this->testSignatureFile),
"Produce a signature file used to score tests provided by the riscv-arch-test project.")
("logfile,f", po::value(&this->traceFile),
"Enable tracing to given file of executed instructions. Output is compressed (with /usr/bin/gzip) if file name ends with \".gz\".")
("csvlog", po::bool_switch(&this->csv),
"Enable CSV format for log file.")
("consoleoutfile", po::value(&this->consoleOutFile),
"Redirect console output to given file.")
("commandlog", po::value(&this->commandLogFile),
"Enable logging of interactive/socket commands to the given file.")
("server", po::value(&this->serverFile),
"Interactive server mode. Put server hostname and port in file. If shared memory "
"is enabled, file is memory mapped filename")
("shm", po::bool_switch(&this->shm),
"Enable shared memory IPC for server mode (default mode uses socket).")
("startpc,s", po::value<std::string>(),
"Set program entry point. If not specified, use entry point of the "
"most recently loaded ELF file.")
("endpc,e", po::value<std::string>(),
"Set stop program counter. Simulator will stop once instruction at "
"the stop program counter is executed.")
("tohost", po::value<std::string>(),
"Memory address for host target interface (HTIF).")
("tohostsym", po::value<std::string>(),
"ELF symbol to use for setting tohost from ELF file (in the case "
"where tohost is not specified on the command line). Default: "
"\"tohost\".")
("fromhost", po::value<std::string>(),
"Memory address for host target interface (HTIF).")
("consoleio", po::value<std::string>(),
"Memory address corresponding to console io. Reading/writing "
"(lw/lh/lb sw/sh/sb) from given address reads/writes a byte from the "
"console.")
("consoleiosym", po::value<std::string>(),
"ELF symbol to use as console-io address (in the case where "
"consoleio is not specified on the command line). Default: "
"\"__whisper_console_io\".")
("maxinst,m", po::value<std::string>(),
"Limit executed instruction count to arg. With a leading plus sign interpret the count as relative to the loaded (from a snapshot) instruction count.")
("memorysize", po::value<std::string>(),
"Memory size (must be a multiple of 4096).")
("tlbsize", po::value<std::string>(),
"Memory size (must be a power of 2).")
("interactive,i", po::bool_switch(&this->interactive),
"Enable interactive mode.")
("traceload", po::bool_switch(&this->traceLdSt),
"Enable tracing of load/store instruction data address (deprecated -- now always on).")
("traceptw", po::bool_switch(&this->tracePtw),
"Enable printing of page table walk information in log.")
("triggers", po::bool_switch(&this->triggers),
"Enable debug triggers (triggers are on in interactive and server modes)")
("counters", po::bool_switch(&this->counters),
"Enable performance counters")
("gdb", po::bool_switch(&this->gdb),
"Run in gdb mode enabling remote debugging from gdb (this requires gdb version"
"8.2 or higher).")
("gdb-tcp-port", po::value(&this->gdbTcpPort)->multitoken(),
"TCP port number for gdb; If port num is negative,"
" gdb will work with stdio (default -1).")
("profileinst", po::value(&this->instFreqFile),
"Report instruction frequency to file.")
("tracebranch", po::value(&this->branchTraceFile),
"Trace branch instructions to the given file.")
("branchwindow", po::value<std::string>(),
"Trace branches in the last n instructions.")
("tracerlib", po::value(&this->tracerLib),
"Path to tracer extension shared library which should provide C symbol tracerExtension32 or tracerExtension64."
"Optionally include arguments after a colon to be exposed to the shared library "
"as C symbol tracerExtensionArgs (ex. tracer.so or tracer.so:hello42).")
("logstart", po::value<std::string>(),
"Start logging at given instruction rank.")
("logperhart", po::bool_switch(&this->logPerHart),
"Use a separate log per hart. This allows a faster trace by reducing lock contention on the logfile.")
("setreg", po::value(&this->regInits)->multitoken(),
"Initialize registers. Apply to all harts unless specific prefix "
"present (hart is 1 in 1:x3=0xabc). Example: --setreg x1=4 x2=0xff "
"1:x3=0xabc")
("configfile", po::value(&this->configFile),
"Configuration file (JSON file defining system features).")
("bblockfile", po::value(&this->bblockFile),
"Basic blocks output stats file.")
("bblockinterval", po::value(&this->bblockInsts),
"Basic block stats are reported even mulitples of given instruction counts and once at end of run.")
("snapshotdir", po::value(&this->snapshotDir),
"Directory prefix for saving snapshots.")
("snapshotperiod", po::value(&this->snapshotPeriods)->multitoken(),
"Snapshot period: Save snapshot using snapshotdir every so many instructions. "
"Specifying multiple periods will only save a snapshot on first instance (not periodic).")
("loadfrom", po::value(&this->loadFrom),
"Snapshot directory from which to restore a previously saved (snapshot) state.")
("stdout", po::value(&this->stdoutFile),
"Redirect standard output of newlib/Linux target program to this.")
("stderr", po::value(&this->stderrFile),
"Redirect standard error of newlib/Linux target program to this.")
("stdin", po::value(&this->stdinFile),
"Redirect standard input of newlib/Linux target program to this.")
("datalines", po::value(&this->dataLines),
"Generate data line address trace to the given file.")
("instrlines", po::value(&this->instrLines),
"Generate instruction line address trace to the given file.")
("initstate", po::value(&this->initStateFile),
"Generate to given file the initial state of accessed memory lines.")
("abinames", po::bool_switch(&this->abiNames),
"Use ABI register names (e.g. sp instead of x2) in instruction dis-assembly.")
("newlib", po::bool_switch(&this->newlib),
"Emulate (some) newlib system calls. Done automatically if newlib "
"symbols are detected in the target ELF file.")
("linux", po::bool_switch(&this->linux),
"Emulate (some) Linux system calls. Done automatically if Linux "
"symbols are detected in the target ELF file.")
("raw", po::bool_switch(&this->raw),
"Bare metal mode: Disable emulation of Linux/newlib system call emulation "
"even if Linux/newlib symbols detected in the target ELF file.")
("envvar", po::value(&this->envVars)->multitoken(),
"Pass environment variable to newlib/Linux target program "
"(e.g. ENV_VAR_NAME=4)")
("elfisa", po::bool_switch(&this->elfisa),
"Configure reset value of MISA according to the RISCV architecture tag(s) "
"encoded into the loaded ELF file(s) if any.")
("unmappedelfok", po::bool_switch(&this->unmappedElfOk),
"Do not flag as error ELF file sections targeting unmapped memory.")
("alarm", po::value<std::string>(),
"External interrupt period in micro-seconds: Convert arg to an "
"instruction count, n, assuming a 1ghz clock, and force an external "
" interrupt every n instructions. No-op if arg is zero.")
("clint", po::value<std::string>(),
"Define address, a, of memory mapped area for clint (core local "
"interruptor). In an n-hart system, words at addresses a, a+4, ... "
"a+(n-1)*4, are associated with the n harts. Store a 0/1 to one of "
"these locations clears/sets the software interrupt bit in the MIP CSR "
"of the corresponding hart. Similarly, addresses b, b+8, ... b+(n-1)*8, "
"where b is a+0x4000, are associated with the n harts. Writing to one "
"of these double words sets the timer-limit of the corresponding hart. "
"A timer interrupt in such a hart becomes pending when the timer value "
"equals or exceeds the timer limit.")
("interruptor", po::value<std::string>(),
"Define address, z, of a memory mapped interrupt agent. Storing a word in z,"
"using a sw instruction, will set/clear a bit in the MIP CSR of a hart"
"in the system. The stored word should have the hart index in bits 0 to 11,"
"the interrupt id (bit number of MIP) in bits 12 to 19, and the interrupt"
"value in bits 20 to 31 (0 to clear, non-zero to set).")
("syscallslam", po::value<std::string>(),
"Define address, a, of a non-cached memory area in which the "
"memory changes of an emulated system call will be slammed. This "
"is used in server mode to relay the effects of a system call "
"to the RTL simulator. The memory area at location a will be filled "
"with a sequence of pairs of double words designating addresses and "
"corresponding values. A zero/zero pair will indicate the end of "
"sequence.")
("mcm", po::bool_switch(&this->mcm),
"Enable memory consistency checks. This is meaningful in server/interactive mode.")
("noppo", po::bool_switch(&this->noPpo),
"Skip preserve program order rule check in MCM when this is used.")
("mcmca", po::bool_switch(&this->mcmca),
"Check all bytes of the memory consistency check merge buffer. If not used "
"we only check the bytes inserted into the merge buffer.")
("mcmls", po::value<std::string>(),
"Memory consistency checker merge buffer line size. If set to zero then "
"write operations are not buffered and will happen as soon a received.")
("steesr", po::value<std::string>(),
"Static trusted execution environment secure range: A colon separated pair of numbers"
" defining the range of memory addresses considered secure. Secure access bit must"
" be zero in each address of the pair.")
("perfapi", po::bool_switch(&this->perfApi),
"Enable performance mode API.")
#ifdef MEM_CALLBACKS
("reportusedblocks", po::bool_switch(&this->reportub),
"Report used blocks with sparse memory. Useful for finding memory footprint of program")
#endif
("pcidev", po::value(&this->pciDevs)->multitoken(),
"Add PCI device to simulation. Format is <device>:<bus>:<slot>:<device-specific>. "
"This should be combined with the pci option to declare a memory region for these devices. "
"Currently only supports virtio-blk, which requires a file")
("deterministic", po::value<std::string>(),
"Used for deterministic multi-hart runs. Define a window range [x:y] for the amount of instructions "
"a hart will execute before switching to the next hart. A range of 0:0 turns this off. The "
"actual amount of instructions is determined by corresponding seed value.")
("seed", po::value<std::string>(),
"Corresponding seed for deterministic runs. If this is not specified, but 'deterministic' is, whisper will "
"generate a seed value based on current time.")
("instcounter", po::value<std::string>(),
"Set instruction counter to given value.")
("quitany", po::bool_switch(&this->quitOnAnyHart),
"Terminate multi-threaded run when any hart finishes (default is to wait "
"for all harts.)")
("noconinput", po::bool_switch(&this->noConInput),
"Do not use console IO address for input. Loads from the console io address "
"simply return last value stored there.")
("verbose,v", po::bool_switch(&this->verbose),
"Be verbose.")
("version", po::bool_switch(&this->version),
"Print version.");
// Define positional options.
po::positional_options_description pdesc;
pdesc.add("target", -1);
// Parse command line options.
po::variables_map varMap;
po::command_line_parser parser(static_cast<int>(argv.size()), argv.data());
auto parsed = parser.options(desc).positional(pdesc).run();
po::store(parsed, varMap);
po::notify(varMap);
bool earlyExit = false;
if (this->version)
{
printVersion();
earlyExit = true;
}
if (this->help)
{
std::cout <<
"Simulate a RISCV system running the program specified by the given ELF\n"
"and/or HEX file. With --newlib/--linux, the ELF file is a newlib/linux linked\n"
"program and may be followed by corresponding command line arguments.\n"
"All numeric arguments are interpreted as hexadecimal numbers when prefixed"
" with 0x."
"Examples:\n"
" whisper --target prog --log\n"
" whisper --target prog --setreg sp=0xffffff00\n"
" whisper --newlib --log --target \"prog -x -y\"\n"
" whisper --linux --log --targetsep ':' --target \"prog:-x:-y\"\n\n";
std::cout << desc;
earlyExit = true;
}
if (earlyExit)
return true;
if (not this->collectCommandLineValues(varMap))
return false;
// Expand each target program string into program name and args.
this->expandTargets();
}
catch (std::exception& exp)
{
std::cerr << "Failed to parse command line args: " << exp.what() << '\n';
return false;
}
return true;
}
template <typename TYPE>
bool
Args::parseCmdLineNumber(const std::string& option, const std::string& numberStr,
TYPE& number)
{
std::string str = numberStr;
bool good = not str.empty();
uint64_t scale = 1;
if (good)
{
char suffix = str.back();
if (suffix == 'k')
scale = 1024;
else if (suffix == 'm')
scale = UINT64_C(1024)*1024;
else if (suffix == 'g')
scale = UINT64_C(1024)*1024*1024;
else if (suffix == 't')
scale = UINT64_C(1024)*1024*1024*1024;
if (scale != 1)
{
str = str.substr(0, str.length() - 1);
if (str.empty())
good = false;
}
}
if (good)
{
using STYPE = typename std::make_signed_t<TYPE>;
char* end = nullptr;
bool bad = false;
if (std::is_same<TYPE, STYPE>::value)
{
int64_t val = strtoll(str.c_str(), &end, 0) * static_cast<int64_t>(scale);
number = static_cast<TYPE>(val);
bad = val != number;
}
else
{
uint64_t val = strtoull(str.c_str(), &end, 0) * scale;
number = static_cast<TYPE>(val);
bad = val != number;
}
if (bad)
{
std::cerr << "parseCmdLineNumber: Number too large: " << numberStr
<< '\n';
return false;
}
if (end and *end)
good = false; // Part of the string are non parseable.
}
if (not good)
std::cerr << "Invalid command line " << option << " value: " << numberStr
<< '\n';
return good;
}
template <typename TYPE>
bool
Args::parseCmdLineNumber(const std::string& option, const std::string& numberStr,
std::optional<TYPE>& number)
{
TYPE n;
if (not parseCmdLineNumber(option, numberStr, n))
return false;
number = n;
return true;
}