Skip to content

Commit 4739401

Browse files
committed
add pesude test
1 parent 0b5e622 commit 4739401

File tree

3 files changed

+97
-63
lines changed

3 files changed

+97
-63
lines changed

src/machine/instruction.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,6 @@ size_t Instruction::pseudo_from_tokens(
11431143
code += 1;
11441144
inst.base = "srli";
11451145
inst.fields[1] = inst.fields[0];
1146-
inst.fields.append("XLEN-16");
11471146
*code = base_from_tokens(inst, reloc).data();
11481147
return 8;
11491148
}
@@ -1155,7 +1154,6 @@ size_t Instruction::pseudo_from_tokens(
11551154
code += 1;
11561155
inst.base = "srli";
11571156
inst.fields[1] = inst.fields[0];
1158-
inst.fields.append("XLEN-32");
11591157
*code = base_from_tokens(inst, reloc).data();
11601158
return 8;
11611159
}

src/machine/instruction.test.cpp

+65-15
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,83 @@ void TestInstruction::instruction_access() {
2424
QCOMPARE(i.address().get_raw(), (uint64_t)0x3ffffff);
2525
}
2626

27-
static struct { uint32_t code; QString str; QString alias_str = nullptr; } code_to_string[] = {
28-
{0xffffffff, "unknown"},
29-
{0x0, "unknown"},
30-
{0b00000000000000000000000000010011, "nop"},
31-
{0b00000000000000001000000010010011, "addi x1, x1, 0"},
32-
{0b01111111111111111000111110010011, "addi x31, x31, 2047"},
33-
{0b11111111111100001000000010010011, "addi x1, x1, -1"},
34-
{0b10000000000000001000000010010011, "addi x1, x1, -2048"},
35-
#include<./instruction.test.data.h>
27+
static struct {
28+
uint32_t code;
29+
QString str;
30+
QString alias_str = nullptr;
31+
} code_to_string[] = {
32+
{ 0xffffffff, "unknown" },
33+
{ 0x0, "unknown" },
34+
{ 0b00000000000000000000000000010011, "nop" },
35+
{ 0b00000000000000001000000010010011, "addi x1, x1, 0" },
36+
{ 0b01111111111111111000111110010011, "addi x31, x31, 2047" },
37+
{ 0b11111111111100001000000010010011, "addi x1, x1, -1" },
38+
{ 0b10000000000000001000000010010011, "addi x1, x1, -2048" },
39+
#include <./instruction.test.data.h>
40+
};
41+
42+
struct Pair {
43+
uint32_t code;
44+
QString str;
45+
};
46+
static struct {
47+
QString string_data;
48+
std::vector<Pair> pairs;
49+
} pesude_code_to_string[] = {
50+
{ "nop", { { 0b00000000000000000000000000010011, "nop" } } },
51+
{ "la x1, 0xffffefff",
52+
{ { 0xfffff097, "auipc x1, 0xfffff" }, { 0xfff08093, "addi x1, x1, -1" } } },
53+
{ "li x1, 0xffffefff",
54+
{ { 0xfffff0b7, "lui x1, 0xfffff" }, { 0xfff08093, "addi x1, x1, -1" } } },
55+
{ "call 0xfffeffff",
56+
{ { 0xffff0317, "auipc x6, 0xffff0" }, { 0xfff300e7, "jalr x1, -1(x6)" } } },
57+
{ "tail 0xfffeffff",
58+
{ { 0xffff0317, "auipc x6, 0xffff0" }, { 0xfff30067, "jalr x0, -1(x6)" } } },
59+
{ "sext.b x1, x2", { { 0x11093, "slli x1, x2, 0x0" }, { 0x4000d093, "srai x1, x1, 0x0" } } },
60+
{ "sext.h x1, x2", { { 0x11093, "slli x1, x2, 0x0" }, { 0x4000d093, "srai x1, x1, 0x0" } } },
61+
{ "zext.h x1, x2", { { 0x11093, "slli x1, x2, 0x0" }, { 0xd093, "srli x1, x1, 0x0" } } },
62+
{ "zext.w x1, x2", { { 0x11093, "slli x1, x2, 0x0" }, { 0xd093, "srli x1, x1, 0x0" } } },
3663
};
3764

3865
void TestInstruction::instruction_to_str() {
39-
size_t array_length = sizeof(code_to_string) / sizeof(code_to_string[0]);
40-
for (size_t i = 0; i < array_length; ++i) {
66+
for (size_t i = 0; i < sizeof(code_to_string) / sizeof(code_to_string[0]); i++) {
4167
QCOMPARE(Instruction(code_to_string[i].code).to_str(), code_to_string[i].str);
4268
}
69+
70+
for (size_t i = 0; i < sizeof(pesude_code_to_string) / sizeof(pesude_code_to_string[0]); i++) {
71+
for (size_t j = 0; j < pesude_code_to_string[i].pairs.size(); j++) {
72+
Pair pair = pesude_code_to_string[i].pairs[j];
73+
uint32_t code;
74+
Instruction::code_from_string(
75+
&code, pair.str.length(), pair.str, machine::Address(0x0));
76+
QCOMPARE(Instruction(pair.code).to_str(), pair.str);
77+
}
78+
}
4379
}
4480

4581
void TestInstruction::instruction_code_from_str() {
46-
size_t array_length = sizeof(code_to_string) / sizeof(code_to_string[0]);
47-
for (size_t i = 0; i < array_length; ++i) {
82+
for (size_t i = 0; i < sizeof(code_to_string) / sizeof(code_to_string[0]); i++) {
4883
if (code_to_string[i].str == "unknown") { continue; }
49-
QString test_string_data = code_to_string[i].alias_str == nullptr ? code_to_string[i].str : code_to_string[i].alias_str;
84+
QString test_string_data = code_to_string[i].alias_str == nullptr
85+
? code_to_string[i].str
86+
: code_to_string[i].alias_str;
5087
uint32_t code = 0;
51-
Instruction::code_from_string(&code, code_to_string[i].str.length(), test_string_data, machine::Address(0x0));
88+
Instruction::code_from_string(
89+
&code, code_to_string[i].str.length(), test_string_data, machine::Address(0x0));
5290
QCOMPARE(code, code_to_string[i].code);
5391
}
92+
93+
for (size_t i = 0; i < sizeof(pesude_code_to_string) / sizeof(pesude_code_to_string[0]); i++) {
94+
uint32_t code[2];
95+
RelocExpressionList reloc = {};
96+
Instruction::code_from_string(
97+
code, pesude_code_to_string[i].string_data.length(),
98+
pesude_code_to_string[i].string_data, machine::Address(0x0), &reloc, nullptr, 0, true);
99+
for (size_t j = 0; j < pesude_code_to_string[i].pairs.size(); j++) {
100+
Pair pair = pesude_code_to_string[i].pairs[j];
101+
QCOMPARE(code[j], pair.code);
102+
}
103+
}
54104
}
55105

56106
QTEST_APPLESS_MAIN(TestInstruction)

src/machine/instruction.test.gendata.cpp

+32-46
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
1-
#include "common/logging.h"
21
#include "machine/instruction.h"
2+
33
#include "machine/instruction.cpp"
4+
45
#include <QFile>
56
#include <unordered_set>
67

78
using namespace machine;
89

910
static uint32_t MASK_AMO_RS2 = 0b1111100000000000000000000;
1011

11-
#define AMO_MAP_4ITEMS(NAME_BASE, MASK) \
12-
{ NAME_BASE, MASK}, \
13-
{ NAME_BASE ".rl", MASK }, \
14-
{ NAME_BASE ".aq", MASK }, \
15-
{ NAME_BASE ".aqrl", MASK },
12+
#define AMO_MAP_4ITEMS(NAME_BASE, MASK) \
13+
{ NAME_BASE, MASK }, { NAME_BASE ".rl", MASK }, { NAME_BASE ".aq", MASK }, \
14+
{ NAME_BASE ".aqrl", MASK },
1615

17-
static std::unordered_map<QString, uint32_t> mask_map = {
18-
AMO_MAP_4ITEMS("lr.w", MASK_AMO_RS2)
19-
AMO_MAP_4ITEMS("lr.d", MASK_AMO_RS2)
20-
};
16+
static std::unordered_map<QString, uint32_t> mask_map
17+
= { AMO_MAP_4ITEMS("lr.w", MASK_AMO_RS2) AMO_MAP_4ITEMS("lr.d", MASK_AMO_RS2) };
2118

22-
void generate_code_and_string_data(QTextStream& out, const InstructionMap* im_iter, BitField subfield, const InstructionMap* alias_parent) {
23-
for (int i = 0; ; i++) {
19+
void generate_code_and_string_data(
20+
QTextStream &out,
21+
const InstructionMap *im_iter,
22+
BitField subfield,
23+
const InstructionMap *alias_parent) {
24+
for (int i = 0;; i++) {
2425
if (alias_parent == nullptr) {
2526
// end of bits
26-
if (i >= (1 << subfield.count)) {
27-
break;
28-
}
27+
if (i >= (1 << subfield.count)) { break; }
2928
} else {
3029
// end of alias
31-
if (im_iter[i].name == nullptr) {
32-
break;
33-
}
30+
if (im_iter[i].name == nullptr) { break; }
3431
}
35-
const InstructionMap* im = &im_iter[i];
32+
const InstructionMap *im = &im_iter[i];
3633

37-
if (im->name == "unknown") {
38-
continue;
39-
}
34+
if (im->name == "unknown") { continue; }
4035

4136
if (im->subclass != nullptr) {
4237
generate_code_and_string_data(out, im->subclass, im->subfield, nullptr);
@@ -49,18 +44,14 @@ void generate_code_and_string_data(QTextStream& out, const InstructionMap* im_it
4944
if (im->args.size()) {
5045
int val_mask;
5146
Instruction::Type t = im->type;
52-
if (alias_parent != nullptr) {
53-
t = alias_parent->type;
54-
}
47+
if (alias_parent != nullptr) { t = alias_parent->type; }
5548
switch (t) {
5649
case Instruction::Type::R:
57-
case Instruction::Type::AMO: {
50+
case Instruction::Type::AMO: {
5851
val_mask = 0b00000000000100001000000010000000;
5952
break;
6053
}
61-
case Instruction::Type::I:
62-
val_mask = 0b00000000000100001000000010000000;
63-
break;
54+
case Instruction::Type::I: val_mask = 0b00000000000100001000000010000000; break;
6455
case Instruction::Type::ZICSR: {
6556
val_mask = 0b00000000000100001000000010000000;
6657
break;
@@ -86,9 +77,7 @@ void generate_code_and_string_data(QTextStream& out, const InstructionMap* im_it
8677
}
8778
}
8879
code |= val_mask & ~im->mask;
89-
if (mask_map.count(im->name)) {
90-
code &= ~mask_map[im->name];
91-
}
80+
if (mask_map.count(im->name)) { code &= ~mask_map[im->name]; }
9281

9382
QString next_delim = " ";
9483
for (const QString &arg_string : im->args) {
@@ -127,31 +116,28 @@ void generate_code_and_string_data(QTextStream& out, const InstructionMap* im_it
127116
}
128117
}
129118
}
130-
119+
131120
if (im->aliases != nullptr) {
132121
generate_code_and_string_data(out, im->aliases, im->subfield, im);
133122
}
134123

135-
QString enum_str;;
124+
QString enum_str;
136125
bool check_failed = false;
137126
QString parsed_string_data = Instruction(code).to_str();
138127
if (alias_parent == nullptr) {
139128
enum_str = QString::asprintf("{0x%x, \"%s\"},", code, qPrintable(string_data));
140-
if (parsed_string_data != string_data) {
141-
check_failed = true;
142-
}
129+
if (parsed_string_data != string_data) { check_failed = true; }
143130
} else {
144-
enum_str = QString::asprintf("{0x%x, \"%s\", \"%s\"},", code, qPrintable(parsed_string_data), qPrintable(string_data));
131+
enum_str = QString::asprintf(
132+
"{0x%x, \"%s\", \"%s\"},", code, qPrintable(parsed_string_data),
133+
qPrintable(string_data));
145134
}
146135
uint32_t parsed_code;
147-
Instruction::code_from_string(&parsed_code, parsed_string_data.length(), string_data, Address(0x0));
148-
if (parsed_code != code) {
149-
check_failed = true;
150-
}
151-
if (check_failed) {
152-
enum_str += " // failed";
153-
}
154-
136+
Instruction::code_from_string(
137+
&parsed_code, parsed_string_data.length(), string_data, Address(0x0));
138+
if (parsed_code != code) { check_failed = true; }
139+
if (check_failed) { enum_str += " // failed"; }
140+
155141
enum_str += "\n";
156142
out << enum_str;
157143
}

0 commit comments

Comments
 (0)