-
Notifications
You must be signed in to change notification settings - Fork 12
/
disassembler_tool.cc
112 lines (106 loc) · 3.63 KB
/
disassembler_tool.cc
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
// (c) Robert Muth - see LICENSE for more info
#include "CpuA32/symbolic.h"
#include "Util/assert.h"
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string_view>
using namespace cwerg::a32;
int main(int argc, char* argv[]) {
if (argc <= 1) {
std::cout << "no command specified\n";
return 1;
}
if (std::string_view("batch") == argv[1]) {
for (std::string line; getline(std::cin, line);) {
if (line.size() < 8 || line[0] == '#') continue;
const uint32_t data = strtoul(line.data(), nullptr, 16);
Ins ins;
if (!Disassemble(&ins, data)) {
std::cout << "could not find opcode for: " << std::hex << data << "\n";
continue;
}
std::vector<std::string> ops;
std::string_view enum_name = InsSymbolize(ins, &ops);
std::cout << std::hex << std::setfill('0') << std::setw(8) << data
<< std::dec << " " << enum_name;
std::string_view sep = " ";
for (const std::string& op : ops) {
std::cout << sep << op;
sep = ", ";
}
std::cout << "\n";
}
} else if (std::string_view("benchmark") == argv[1]) {
// The first 1<<28 (256M) bit patterns exercise all opcodes with predicate
// "eq"
uint32_t num_bad = 0;
std::vector<std::string> ops;
for (uint32_t data = 0; data < (1U << 28U); ++data) {
Ins ins;
if (Disassemble(&ins, data)) {
const uint32_t data2 = Assemble(ins);
if (data != data2) {
if (ins.opcode->fields[2] != OK::IMM_0_7_8_11 &&
ins.opcode->fields[3] != OK::IMM_0_7_8_11) {
std::cout << "Disassembler failure " << std::hex << data << " vs "
<< data2 << ": ";
ops.clear();
std::string_view enum_name = InsSymbolize(ins, &ops);
std::cout << enum_name;
std::string_view sep = " ";
for (const std::string& op : ops) {
std::cout << sep << op;
sep = ", ";
}
std::cout << "\n";
return 1;
}
continue;
}
} else {
++num_bad;
}
if (data % 10000000 == 0) {
std::cout << num_bad << "/" << data << "\n";
}
}
std::cout << "unsupported opcodes: " << std::dec << num_bad << "\n";
return 0;
} else {
for (unsigned i = 1; i < argc; ++i) {
const uint32_t data = strtoul(argv[i], nullptr, 16);
Ins ins;
if (!Disassemble(&ins, data)) {
std::cout << "could not find opcode for: " << std::hex << data << "\n";
continue;
}
std::vector<std::string> ops;
std::string_view enum_name = InsSymbolize(ins, &ops);
std::cout << std::hex << std::setw(8)<< std::setfill('0') << data
<< std::dec << std::setfill(' ') << " " << enum_name;
std::string_view sep = " ";
for (const std::string& op : ops) {
std::cout << sep << op;
sep = ", ";
}
std::cout << "\n";
for (unsigned x = 0; x < ins.opcode->num_fields; ++x) {
std::cout << " " << std::left << std::setw(25)
<< EnumToString(ins.opcode->fields[x]) << " " << std::setw(15)
<< ops[x] << " (" << ins.operands[x] << ")\n" << std::right;
}
std::cout << "\n";
// check that the assembler works - this is not strictly
// necessary but useful for debugging the assembler
const uint32_t data2 = Assemble(ins);
if (data != data2) {
std::cout << "Disassembler failure " << std::hex << data << " vs "
<< data2 << "\n";
return 1;
}
ASSERT(data == data2, "");
}
return 0;
}
}