@@ -24,33 +24,83 @@ void TestInstruction::instruction_access() {
24
24
QCOMPARE (i.address ().get_raw (), (uint64_t )0x3ffffff );
25
25
}
26
26
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" } } },
36
63
};
37
64
38
65
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++) {
41
67
QCOMPARE (Instruction (code_to_string[i].code ).to_str (), code_to_string[i].str );
42
68
}
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
+ }
43
79
}
44
80
45
81
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++) {
48
83
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 ;
50
87
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 ));
52
90
QCOMPARE (code, code_to_string[i].code );
53
91
}
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
+ }
54
104
}
55
105
56
106
QTEST_APPLESS_MAIN (TestInstruction)
0 commit comments