Skip to content

Commit 40b660a

Browse files
committed
Validate contents of overlap list in CI
1 parent 0325be5 commit 40b660a

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

riscv/check-opcode-overlap.t.cc

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "decode.h"
22
#include "common.h"
3-
#include <unordered_set>
3+
#include <unordered_map>
44
#include <vector>
55
#include <string>
66
#include <cstdio>
@@ -11,13 +11,9 @@ struct opcode {
1111
std::string name;
1212
};
1313

14-
static void check_overlap(const opcode& a, const opcode& b)
14+
static bool overlaps(const opcode& a, const opcode& b)
1515
{
16-
if ((a.match & b.mask) == b.match) {
17-
fprintf(stderr, "Instruction %s (%" PRIx64 ") overlaps instruction %s (%" PRIx64 ", mask %" PRIx64 ")\n",
18-
a.name.c_str(), a.match, b.name.c_str(), b.match, b.mask);
19-
exit(-1);
20-
}
16+
return (a.mask & b.mask & (a.match ^ b.match)) == 0;
2117
}
2218

2319
int main()
@@ -34,24 +30,47 @@ int main()
3430
#undef DEFINE_INSN
3531
};
3632

37-
std::unordered_set<std::string> overlap_list;
33+
std::unordered_map<std::string, bool> overlap_list;
3834
#define DECLARE_OVERLAP_INSN(name, ext) \
39-
overlap_list.insert(std::string(#name));
35+
overlap_list[std::string(#name)] = false;
4036
#include "overlap_list.h"
4137
#undef DECLARE_OVERLAP_INSN
4238

4339
std::vector<const opcode*> list;
44-
for (size_t i = 0; i < sizeof(static_list) / sizeof(static_list[0]); i++) {
40+
for (size_t i = 0; i < std::size(static_list); i++) {
4541
if (!overlap_list.count(static_list[i].name))
4642
list.push_back(&static_list[i]);
4743
}
4844

45+
bool ok = true;
46+
4947
for (size_t i = 1; i < list.size(); i++) {
5048
for (size_t j = 0; j < i; j++) {
51-
check_overlap(*list[i], *list[j]);
52-
check_overlap(*list[j], *list[i]);
49+
if (overlaps(*list[i], *list[j])) {
50+
fprintf(stderr, "Instruction %s (%" PRIx64 ") overlaps instruction %s (%" PRIx64 ", mask %" PRIx64 ")\n",
51+
list[i]->name.c_str(), list[i]->match, list[j]->name.c_str(), list[j]->match, list[j]->mask);
52+
ok = false;
53+
}
54+
}
55+
}
56+
57+
// make sure nothing in the overlap list is unused
58+
for (size_t i = 1; i < std::size(static_list); i++) {
59+
for (size_t j = 0; j < i; j++) {
60+
if (overlaps(static_list[i], static_list[j])) {
61+
overlap_list[static_list[i].name] = true;
62+
overlap_list[static_list[j].name] = true;
63+
}
64+
}
65+
}
66+
67+
for (auto const& [name, used] : overlap_list) {
68+
if (!used) {
69+
fprintf(stderr, "Instruction %s overlaps nothing, so overlap list entry has no effect\n",
70+
name.c_str());
71+
ok = false;
5372
}
5473
}
5574

56-
return 0;
75+
return ok ? 0 : -1;
5776
}

0 commit comments

Comments
 (0)