You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.9 KiB
84 lines
1.9 KiB
|
13 years ago
|
// See LICENSE for license details.
|
||
|
|
|
||
|
|
// This little program finds occurrences of strings like
|
||
|
|
// DASM(ffabc013)
|
||
|
|
// in its input, then replaces them with the disassembly
|
||
|
|
// enclosed hexadecimal number, interpreted as a RISC-V
|
||
|
|
// instruction.
|
||
|
|
|
||
|
|
#include "disasm.h"
|
||
|
12 years ago
|
#include "extension.h"
|
||
|
13 years ago
|
#include <iostream>
|
||
|
|
#include <string>
|
||
|
|
#include <cstdint>
|
||
|
12 years ago
|
#include <fesvr/option_parser.h>
|
||
|
13 years ago
|
using namespace std;
|
||
|
|
|
||
|
12 years ago
|
int main(int argc, char** argv)
|
||
|
13 years ago
|
{
|
||
|
|
string s;
|
||
|
10 years ago
|
const char* isa = DEFAULT_ISA;
|
||
|
13 years ago
|
|
||
|
12 years ago
|
std::function<extension_t*()> extension;
|
||
|
|
option_parser_t parser;
|
||
|
6 years ago
|
#ifdef HAVE_DLOPEN
|
||
|
12 years ago
|
parser.option(0, "extension", 1, [&](const char* s){extension = find_extension(s);});
|
||
|
6 years ago
|
#endif
|
||
|
10 years ago
|
parser.option(0, "isa", 1, [&](const char* s){isa = s;});
|
||
|
11 years ago
|
parser.parse(argv);
|
||
|
|
|
||
|
6 years ago
|
std::string lowercase;
|
||
|
|
for (const char *p = isa; *p; p++)
|
||
|
|
lowercase += std::tolower(*p);
|
||
|
|
|
||
|
|
int xlen;
|
||
|
|
if (lowercase.compare(0, 4, "rv32") == 0) {
|
||
|
|
xlen = 32;
|
||
|
|
} else if (lowercase.compare(0, 4, "rv64") == 0) {
|
||
|
|
xlen = 64;
|
||
|
|
} else {
|
||
|
|
fprintf(stderr, "bad ISA string: %s\n", isa);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
disassembler_t* disassembler = new disassembler_t(xlen);
|
||
|
|
if (extension) {
|
||
|
|
for (auto disasm_insn : extension()->get_disasms()) {
|
||
|
|
disassembler->add_insn(disasm_insn);
|
||
|
|
}
|
||
|
|
}
|
||
|
12 years ago
|
|
||
|
13 years ago
|
while (getline(cin, s))
|
||
|
|
{
|
||
|
8 years ago
|
for (size_t pos = 0; (pos = s.find("DASM(", pos)) != string::npos; )
|
||
|
13 years ago
|
{
|
||
|
8 years ago
|
size_t start = pos;
|
||
|
|
|
||
|
|
pos += strlen("DASM(");
|
||
|
|
|
||
|
|
if (s[pos] == '0' && (s[pos+1] == 'x' || s[pos+1] == 'X'))
|
||
|
|
pos += 2;
|
||
|
|
|
||
|
|
if (!isxdigit(s[pos]))
|
||
|
|
continue;
|
||
|
13 years ago
|
|
||
|
11 years ago
|
char* endp;
|
||
|
8 years ago
|
int64_t bits = strtoull(&s[pos], &endp, 16);
|
||
|
|
if (*endp != ')')
|
||
|
|
continue;
|
||
|
|
|
||
|
|
size_t nbits = 4 * (endp - &s[pos]);
|
||
|
11 years ago
|
if (nbits < 64)
|
||
|
|
bits = bits << (64 - nbits) >> (64 - nbits);
|
||
|
|
|
||
|
6 years ago
|
string dis = disassembler->disassemble(bits);
|
||
|
8 years ago
|
s = s.substr(0, start) + dis + s.substr(endp - &s[0] + 1);
|
||
|
|
pos = start + dis.length();
|
||
|
13 years ago
|
}
|
||
|
|
|
||
|
|
cout << s << '\n';
|
||
|
|
}
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|