Handle spike-dasm inputs with leading 0x correctly
[riscv-isa-sim.git] / spike_main / spike-dasm.cc
1 // See LICENSE for license details.
2
3 // This little program finds occurrences of strings like
4 // DASM(ffabc013)
5 // in its input, then replaces them with the disassembly
6 // enclosed hexadecimal number, interpreted as a RISC-V
7 // instruction.
8
9 #include "disasm.h"
10 #include "extension.h"
11 #include <iostream>
12 #include <string>
13 #include <cstdint>
14 #include <fesvr/option_parser.h>
15 using namespace std;
16
17 int main(int argc, char** argv)
18 {
19 string s;
20 const char* isa = DEFAULT_ISA;
21
22 std::function<extension_t*()> extension;
23 option_parser_t parser;
24 parser.option(0, "extension", 1, [&](const char* s){extension = find_extension(s);});
25 parser.option(0, "isa", 1, [&](const char* s){isa = s;});
26 parser.parse(argv);
27
28 processor_t p(isa, 0, 0);
29 if (extension)
30 p.register_extension(extension());
31
32 while (getline(cin, s))
33 {
34 for (size_t pos = 0; (pos = s.find("DASM(", pos)) != string::npos; )
35 {
36 size_t start = pos;
37
38 pos += strlen("DASM(");
39
40 if (s[pos] == '0' && (s[pos+1] == 'x' || s[pos+1] == 'X'))
41 pos += 2;
42
43 if (!isxdigit(s[pos]))
44 continue;
45
46 char* endp;
47 int64_t bits = strtoull(&s[pos], &endp, 16);
48 if (*endp != ')')
49 continue;
50
51 size_t nbits = 4 * (endp - &s[pos]);
52 if (nbits < 64)
53 bits = bits << (64 - nbits) >> (64 - nbits);
54
55 string dis = p.get_disassembler()->disassemble(bits);
56 s = s.substr(0, start) + dis + s.substr(endp - &s[0] + 1);
57 pos = start + dis.length();
58 }
59
60 cout << s << '\n';
61 }
62
63 return 0;
64 }