diff options
author | Andrew Waterman <andrew@sifive.com> | 2020-09-22 04:28:22 -0700 |
---|---|---|
committer | Andrew Waterman <andrew@sifive.com> | 2020-09-22 04:28:22 -0700 |
commit | 59d450e58646bdc0af5e9250df7d6267c3c791fc (patch) | |
tree | 02a111a8dde57f54d95edda9cb9b3ee532ecef7b /spike_dasm | |
parent | b1dc3826d0254b5d4853037cd399560b39745983 (diff) | |
download | spike-59d450e58646bdc0af5e9250df7d6267c3c791fc.zip spike-59d450e58646bdc0af5e9250df7d6267c3c791fc.tar.gz spike-59d450e58646bdc0af5e9250df7d6267c3c791fc.tar.bz2 |
Separate build of spike and spike-dasm
Diffstat (limited to 'spike_dasm')
-rw-r--r-- | spike_dasm/spike-dasm.cc | 83 | ||||
-rw-r--r-- | spike_dasm/spike_dasm.ac | 0 | ||||
-rw-r--r-- | spike_dasm/spike_dasm.mk.in | 9 | ||||
l--------- | spike_dasm/spike_dasm_option_parser.cc | 1 |
4 files changed, 93 insertions, 0 deletions
diff --git a/spike_dasm/spike-dasm.cc b/spike_dasm/spike-dasm.cc new file mode 100644 index 0000000..fa6a25a --- /dev/null +++ b/spike_dasm/spike-dasm.cc @@ -0,0 +1,83 @@ +// 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" +#include "extension.h" +#include <iostream> +#include <string> +#include <cstdint> +#include <fesvr/option_parser.h> +using namespace std; + +int main(int argc, char** argv) +{ + string s; + const char* isa = DEFAULT_ISA; + + std::function<extension_t*()> extension; + option_parser_t parser; +#ifdef HAVE_DLOPEN + parser.option(0, "extension", 1, [&](const char* s){extension = find_extension(s);}); +#endif + parser.option(0, "isa", 1, [&](const char* s){isa = s;}); + parser.parse(argv); + + 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); + } + } + + while (getline(cin, s)) + { + for (size_t pos = 0; (pos = s.find("DASM(", pos)) != string::npos; ) + { + 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; + + char* endp; + int64_t bits = strtoull(&s[pos], &endp, 16); + if (*endp != ')') + continue; + + size_t nbits = 4 * (endp - &s[pos]); + if (nbits < 64) + bits = bits << (64 - nbits) >> (64 - nbits); + + string dis = disassembler->disassemble(bits); + s = s.substr(0, start) + dis + s.substr(endp - &s[0] + 1); + pos = start + dis.length(); + } + + cout << s << '\n'; + } + + return 0; +} diff --git a/spike_dasm/spike_dasm.ac b/spike_dasm/spike_dasm.ac new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/spike_dasm/spike_dasm.ac diff --git a/spike_dasm/spike_dasm.mk.in b/spike_dasm/spike_dasm.mk.in new file mode 100644 index 0000000..b6118fd --- /dev/null +++ b/spike_dasm/spike_dasm.mk.in @@ -0,0 +1,9 @@ +spike_dasm_subproject_deps = \ + disasm \ + $(if $(HAVE_DLOPEN),riscv,) \ + +spike_dasm_srcs = \ + spike_dasm_option_parser.cc \ + +spike_dasm_install_prog_srcs = \ + spike-dasm.cc \ diff --git a/spike_dasm/spike_dasm_option_parser.cc b/spike_dasm/spike_dasm_option_parser.cc new file mode 120000 index 0000000..4244c15 --- /dev/null +++ b/spike_dasm/spike_dasm_option_parser.cc @@ -0,0 +1 @@ +../fesvr/option_parser.cc
\ No newline at end of file |