19d579d48cbbd1328af02c8506480d29c181eba4
[riscv-isa-sim.git] / riscv / riscv-dis.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 disassembler_t d;
21
22 std::function<extension_t*()> extension;
23 option_parser_t parser;
24 parser.option(0, "extension", 1, [&](const char* s){
25 if (!extensions().count(s))
26 fprintf(stderr, "unknown extension %s!\n", s), exit(-1);
27 extension = extensions()[s];
28
29 for (auto disasm_insn : extension()->get_disasms())
30 d.add_insn(disasm_insn);
31 });
32
33 while (getline(cin, s))
34 {
35 for (size_t start = 0; (start = s.find("DASM(", start)) != string::npos; )
36 {
37 size_t end = s.find(')', start);
38 if (end == string::npos)
39 break;
40
41 size_t numstart = start + strlen("DASM(");
42 uint32_t n = strtoul(&s[numstart], NULL, 16);
43
44 string dis = d.disassemble(*(insn_t*)&n);
45
46 s = s.substr(0, start) + dis + s.substr(end+1);
47 start += dis.length();
48 }
49
50 cout << s << '\n';
51 }
52
53 return 0;
54 }