@@ -7,7 +7,6 @@ using namespace machine;
77// Test that we are correctly encoding instructions in constructor
88void TestInstruction::instruction () {
99 QCOMPARE (Instruction (0x0 ), Instruction ());
10- QCOMPARE (Instruction (0x0 ), Instruction (Instruction (0x0 )));
1110}
1211
1312// Test that we are correctly decoding instruction fields
@@ -25,152 +24,32 @@ void TestInstruction::instruction_access() {
2524 QCOMPARE (i.address ().get_raw (), (uint64_t )0x3ffffff );
2625}
2726
27+ static struct { uint32_t code; QString str; } 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>
36+ };
37+
2838void TestInstruction::instruction_to_str () {
29- QCOMPARE (Instruction (0xffffffff ).to_str (), " unknown" );
30- QCOMPARE (Instruction (0x0 ).to_str (), " unknown" );
31- QCOMPARE (Instruction (0b00000000000000000000000000010011 ).to_str (), " nop" );
32- QCOMPARE (Instruction (0b00000000000000001000000010010011 ).to_str (), " addi x1, x1, 0" );
33- QCOMPARE (Instruction (0b01111111111111111000111110010011 ).to_str (), " addi x31, x31, 2047" );
34- QCOMPARE (Instruction (0b11111111111100001000000010010011 ).to_str (), " addi x1, x1, -1" );
35- QCOMPARE (Instruction (0b10000000000000001000000010010011 ).to_str (), " addi x1, x1, -2048" );
36- QCOMPARE (Instruction (0x108083 ).to_str (), " lb x1, 1(x1)" );
37- QCOMPARE (Instruction (0x109083 ).to_str (), " lh x1, 1(x1)" );
38- QCOMPARE (Instruction (0x10a083 ).to_str (), " lw x1, 1(x1)" );
39- QCOMPARE (Instruction (0x10b083 ).to_str (), " ld x1, 1(x1)" );
40- QCOMPARE (Instruction (0x10c083 ).to_str (), " lbu x1, 1(x1)" );
41- QCOMPARE (Instruction (0x10d083 ).to_str (), " lhu x1, 1(x1)" );
42- QCOMPARE (Instruction (0x10e083 ).to_str (), " lwu x1, 1(x1)" );
43- QCOMPARE (Instruction (0x10808f ).to_str (), " fence" );
44- QCOMPARE (Instruction (0x10908f ).to_str (), " fence.i" );
45- QCOMPARE (Instruction (0x108093 ).to_str (), " addi x1, x1, 1" );
46- QCOMPARE (Instruction (0x109093 ).to_str (), " slli x1, x1, 0x1" );
47- QCOMPARE (Instruction (0x10a093 ).to_str (), " slti x1, x1, 1" );
48- QCOMPARE (Instruction (0x10b093 ).to_str (), " sltiu x1, x1, 1" );
49- QCOMPARE (Instruction (0x10c093 ).to_str (), " xori x1, x1, 1" );
50- QCOMPARE (Instruction (0x10d093 ).to_str (), " srli x1, x1, 0x1" );
51- QCOMPARE (Instruction (0x10e093 ).to_str (), " ori x1, x1, 1" );
52- QCOMPARE (Instruction (0x1097 ).to_str (), " auipc x1, 0x1" );
53- QCOMPARE (Instruction (0x10809b ).to_str (), " addiw x1, x1, 1" );
54- QCOMPARE (Instruction (0x10909b ).to_str (), " slliw x1, x1, 0x1" );
55- QCOMPARE (Instruction (0x10d09b ).to_str (), " srliw x1, x1, 0x1" );
56- QCOMPARE (Instruction (0x1080a3 ).to_str (), " sb x1, 1(x1)" );
57- QCOMPARE (Instruction (0x1090a3 ).to_str (), " sh x1, 1(x1)" );
58- QCOMPARE (Instruction (0x10a0a3 ).to_str (), " sw x1, 1(x1)" );
59- QCOMPARE (Instruction (0x10b0a3 ).to_str (), " sd x1, 1(x1)" );
60- QCOMPARE (Instruction (0x10a0af ).to_str (), " amoadd.w x1, x1, (x1)" );
61- QCOMPARE (Instruction (0x210a0af ).to_str (), " amoadd.w.rl x1, x1, (x1)" );
62- QCOMPARE (Instruction (0x410a0af ).to_str (), " amoadd.w.aq x1, x1, (x1)" );
63- QCOMPARE (Instruction (0x610a0af ).to_str (), " amoadd.w.aqrl x1, x1, (x1)" );
64- QCOMPARE (Instruction (0x810a0af ).to_str (), " amoswap.w x1, x1, (x1)" );
65- QCOMPARE (Instruction (0xa10a0af ).to_str (), " amoswap.w.rl x1, x1, (x1)" );
66- QCOMPARE (Instruction (0xc10a0af ).to_str (), " amoswap.w.aq x1, x1, (x1)" );
67- QCOMPARE (Instruction (0xe10a0af ).to_str (), " amoswap.w.aqrl x1, x1, (x1)" );
68- QCOMPARE (Instruction (0x1010a0af ).to_str (), " lr.w x1, (x1)" );
69- QCOMPARE (Instruction (0x1210a0af ).to_str (), " lr.w.rl x1, (x1)" );
70- QCOMPARE (Instruction (0x1410a0af ).to_str (), " lr.w.aq x1, (x1)" );
71- QCOMPARE (Instruction (0x1610a0af ).to_str (), " lr.w.aqrl x1, (x1)" );
72- QCOMPARE (Instruction (0x1810a0af ).to_str (), " sc.w x1, x1, (x1)" );
73- QCOMPARE (Instruction (0x1a10a0af ).to_str (), " sc.w.rl x1, x1, (x1)" );
74- QCOMPARE (Instruction (0x1c10a0af ).to_str (), " sc.w.aq x1, x1, (x1)" );
75- QCOMPARE (Instruction (0x1e10a0af ).to_str (), " sc.w.aqrl x1, x1, (x1)" );
76- QCOMPARE (Instruction (0x2010a0af ).to_str (), " amoxor.w x1, x1, (x1)" );
77- QCOMPARE (Instruction (0x2210a0af ).to_str (), " amoxor.w.rl x1, x1, (x1)" );
78- QCOMPARE (Instruction (0x2410a0af ).to_str (), " amoxor.w.aq x1, x1, (x1)" );
79- QCOMPARE (Instruction (0x2610a0af ).to_str (), " amoxor.w.aqrl x1, x1, (x1)" );
80- QCOMPARE (Instruction (0x4010a0af ).to_str (), " amoor.w x1, x1, (x1)" );
81- QCOMPARE (Instruction (0x4210a0af ).to_str (), " amoor.w.rl x1, x1, (x1)" );
82- QCOMPARE (Instruction (0x4410a0af ).to_str (), " amoor.w.aq x1, x1, (x1)" );
83- QCOMPARE (Instruction (0x4610a0af ).to_str (), " amoor.w.aqrl x1, x1, (x1)" );
84- QCOMPARE (Instruction (0x6010a0af ).to_str (), " amoand.w x1, x1, (x1)" );
85- QCOMPARE (Instruction (0x6210a0af ).to_str (), " amoand.w.rl x1, x1, (x1)" );
86- QCOMPARE (Instruction (0x6410a0af ).to_str (), " amoand.w.aq x1, x1, (x1)" );
87- QCOMPARE (Instruction (0x6610a0af ).to_str (), " amoand.w.aqrl x1, x1, (x1)" );
88- QCOMPARE (Instruction (0x8010a0af ).to_str (), " amomin.w x1, x1, (x1)" );
89- QCOMPARE (Instruction (0x8210a0af ).to_str (), " amomin.w.rl x1, x1, (x1)" );
90- QCOMPARE (Instruction (0x8410a0af ).to_str (), " amomin.w.aq x1, x1, (x1)" );
91- QCOMPARE (Instruction (0x8610a0af ).to_str (), " amomin.w.aqrl x1, x1, (x1)" );
92- QCOMPARE (Instruction (0xa010a0af ).to_str (), " amomax.w x1, x1, (x1)" );
93- QCOMPARE (Instruction (0xa210a0af ).to_str (), " amomax.w.rl x1, x1, (x1)" );
94- QCOMPARE (Instruction (0xa410a0af ).to_str (), " amomax.w.aq x1, x1, (x1)" );
95- QCOMPARE (Instruction (0xa610a0af ).to_str (), " amomax.w.aqrl x1, x1, (x1)" );
96- QCOMPARE (Instruction (0xc010a0af ).to_str (), " amominu.w x1, x1, (x1)" );
97- QCOMPARE (Instruction (0xc210a0af ).to_str (), " amominu.w.rl x1, x1, (x1)" );
98- QCOMPARE (Instruction (0xc410a0af ).to_str (), " amominu.w.aq x1, x1, (x1)" );
99- QCOMPARE (Instruction (0xc610a0af ).to_str (), " amominu.w.aqrl x1, x1, (x1)" );
100- QCOMPARE (Instruction (0xe010a0af ).to_str (), " amomaxu.w x1, x1, (x1)" );
101- QCOMPARE (Instruction (0xe210a0af ).to_str (), " amomaxu.w.rl x1, x1, (x1)" );
102- QCOMPARE (Instruction (0xe410a0af ).to_str (), " amomaxu.w.aq x1, x1, (x1)" );
103- QCOMPARE (Instruction (0xe610a0af ).to_str (), " amomaxu.w.aqrl x1, x1, (x1)" );
104- QCOMPARE (Instruction (0x10b0af ).to_str (), " amoadd.d x1, x1, (x1)" );
105- QCOMPARE (Instruction (0x210b0af ).to_str (), " amoadd.d.rl x1, x1, (x1)" );
106- QCOMPARE (Instruction (0x410b0af ).to_str (), " amoadd.d.aq x1, x1, (x1)" );
107- QCOMPARE (Instruction (0x610b0af ).to_str (), " amoadd.d.aqrl x1, x1, (x1)" );
108- QCOMPARE (Instruction (0x810b0af ).to_str (), " amoswap.d x1, x1, (x1)" );
109- QCOMPARE (Instruction (0xa10b0af ).to_str (), " amoswap.d.rl x1, x1, (x1)" );
110- QCOMPARE (Instruction (0xc10b0af ).to_str (), " amoswap.d.aq x1, x1, (x1)" );
111- QCOMPARE (Instruction (0xe10b0af ).to_str (), " amoswap.d.aqrl x1, x1, (x1)" );
112- QCOMPARE (Instruction (0x1010b0af ).to_str (), " lr.d x1, (x1)" );
113- QCOMPARE (Instruction (0x1210b0af ).to_str (), " lr.d.rl x1, (x1)" );
114- QCOMPARE (Instruction (0x1410b0af ).to_str (), " lr.d.aq x1, (x1)" );
115- QCOMPARE (Instruction (0x1610b0af ).to_str (), " lr.d.aqrl x1, (x1)" );
116- QCOMPARE (Instruction (0x1810b0af ).to_str (), " sc.d x1, x1, (x1)" );
117- QCOMPARE (Instruction (0x1a10b0af ).to_str (), " sc.d.rl x1, x1, (x1)" );
118- QCOMPARE (Instruction (0x1c10b0af ).to_str (), " sc.d.aq x1, x1, (x1)" );
119- QCOMPARE (Instruction (0x1e10b0af ).to_str (), " sc.d.aqrl x1, x1, (x1)" );
120- QCOMPARE (Instruction (0x2010b0af ).to_str (), " amoxor.d x1, x1, (x1)" );
121- QCOMPARE (Instruction (0x2210b0af ).to_str (), " amoxor.d.rl x1, x1, (x1)" );
122- QCOMPARE (Instruction (0x2410b0af ).to_str (), " amoxor.d.aq x1, x1, (x1)" );
123- QCOMPARE (Instruction (0x2610b0af ).to_str (), " amoxor.d.aqrl x1, x1, (x1)" );
124- QCOMPARE (Instruction (0x4010b0af ).to_str (), " amoor.d x1, x1, (x1)" );
125- QCOMPARE (Instruction (0x4210b0af ).to_str (), " amoor.d.rl x1, x1, (x1)" );
126- QCOMPARE (Instruction (0x4410b0af ).to_str (), " amoor.d.aq x1, x1, (x1)" );
127- QCOMPARE (Instruction (0x4610b0af ).to_str (), " amoor.d.aqrl x1, x1, (x1)" );
128- QCOMPARE (Instruction (0x6010b0af ).to_str (), " amoand.d x1, x1, (x1)" );
129- QCOMPARE (Instruction (0x6210b0af ).to_str (), " amoand.d.rl x1, x1, (x1)" );
130- QCOMPARE (Instruction (0x6410b0af ).to_str (), " amoand.d.aq x1, x1, (x1)" );
131- QCOMPARE (Instruction (0x6610b0af ).to_str (), " amoand.d.aqrl x1, x1, (x1)" );
132- QCOMPARE (Instruction (0x8010b0af ).to_str (), " amomin.d x1, x1, (x1)" );
133- QCOMPARE (Instruction (0x8210b0af ).to_str (), " amomin.d.rl x1, x1, (x1)" );
134- QCOMPARE (Instruction (0x8410b0af ).to_str (), " amomin.d.aq x1, x1, (x1)" );
135- QCOMPARE (Instruction (0x8610b0af ).to_str (), " amomin.d.aqrl x1, x1, (x1)" );
136- QCOMPARE (Instruction (0xa010b0af ).to_str (), " amomax.d x1, x1, (x1)" );
137- QCOMPARE (Instruction (0xa210b0af ).to_str (), " amomax.d.rl x1, x1, (x1)" );
138- QCOMPARE (Instruction (0xa410b0af ).to_str (), " amomax.d.aq x1, x1, (x1)" );
139- QCOMPARE (Instruction (0xa610b0af ).to_str (), " amomax.d.aqrl x1, x1, (x1)" );
140- QCOMPARE (Instruction (0xc010b0af ).to_str (), " amominu.d x1, x1, (x1)" );
141- QCOMPARE (Instruction (0xc210b0af ).to_str (), " amominu.d.rl x1, x1, (x1)" );
142- QCOMPARE (Instruction (0xc410b0af ).to_str (), " amominu.d.aq x1, x1, (x1)" );
143- QCOMPARE (Instruction (0xc610b0af ).to_str (), " amominu.d.aqrl x1, x1, (x1)" );
144- QCOMPARE (Instruction (0xe010b0af ).to_str (), " amomaxu.d x1, x1, (x1)" );
145- QCOMPARE (Instruction (0xe210b0af ).to_str (), " amomaxu.d.rl x1, x1, (x1)" );
146- QCOMPARE (Instruction (0xe410b0af ).to_str (), " amomaxu.d.aq x1, x1, (x1)" );
147- QCOMPARE (Instruction (0xe610b0af ).to_str (), " amomaxu.d.aqrl x1, x1, (x1)" );
148- QCOMPARE (Instruction (0x1080b3 ).to_str (), " add x1, x1, x1" );
149- QCOMPARE (Instruction (0x1090b3 ).to_str (), " sll x1, x1, x1" );
150- QCOMPARE (Instruction (0x10a0b3 ).to_str (), " slt x1, x1, x1" );
151- QCOMPARE (Instruction (0x10b0b3 ).to_str (), " sltu x1, x1, x1" );
152- QCOMPARE (Instruction (0x10c0b3 ).to_str (), " xor x1, x1, x1" );
153- QCOMPARE (Instruction (0x10d0b3 ).to_str (), " srl x1, x1, x1" );
154- QCOMPARE (Instruction (0x10e0b3 ).to_str (), " or x1, x1, x1" );
155- QCOMPARE (Instruction (0x10b7 ).to_str (), " lui x1, 0x1" );
156- QCOMPARE (Instruction (0x1080bb ).to_str (), " addw x1, x1, x1" );
157- QCOMPARE (Instruction (0x1090bb ).to_str (), " sllw x1, x1, x1" );
158- QCOMPARE (Instruction (0x10d0bb ).to_str (), " srlw x1, x1, x1" );
159- QCOMPARE (Instruction (0x108163 ).to_str (), " beq x1, x1, 0x2" );
160- QCOMPARE (Instruction (0x109163 ).to_str (), " bne x1, x1, 0x2" );
161- QCOMPARE (Instruction (0x10c163 ).to_str (), " blt x1, x1, 0x2" );
162- QCOMPARE (Instruction (0x10d163 ).to_str (), " bge x1, x1, 0x2" );
163- QCOMPARE (Instruction (0x10e163 ).to_str (), " bltu x1, x1, 0x2" );
164- QCOMPARE (Instruction (0x1080e7 ).to_str (), " jalr x1, 1(x1)" );
165- QCOMPARE (Instruction (0x2000ef ).to_str (), " jal x1, 0x2" );
166- QCOMPARE (Instruction (0x73 ).to_str (), " ecall" );
167- QCOMPARE (Instruction (0x10200073 ).to_str (), " sret" );
168- QCOMPARE (Instruction (0x30200073 ).to_str (), " mret" );
169- QCOMPARE (Instruction (0x1090f3 ).to_str (), " csrrw x1, 0x1, x1" );
170- QCOMPARE (Instruction (0x10a0f3 ).to_str (), " csrrs x1, 0x1, x1" );
171- QCOMPARE (Instruction (0x10b0f3 ).to_str (), " csrrc x1, 0x1, x1" );
172- QCOMPARE (Instruction (0x10d0f3 ).to_str (), " csrrwi x1, 0x1, 0x1" );
173- QCOMPARE (Instruction (0x10e0f3 ).to_str (), " csrrsi x1, 0x1, 0x1" );
39+ size_t array_length = sizeof (code_to_string) / sizeof (code_to_string[0 ]);
40+ for (size_t i = 0 ; i < array_length; ++i) {
41+ QCOMPARE (Instruction (code_to_string[i].code ).to_str (), code_to_string[i].str );
42+ }
43+ }
44+
45+ 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) {
48+ if (code_to_string[i].str == " unknown" ) { continue ; }
49+ uint32_t code = 0 ;
50+ Instruction::code_from_string (&code, code_to_string[i].str .length (), code_to_string[i].str , machine::Address (0x0 ));
51+ QCOMPARE (code, code_to_string[i].code );
52+ }
17453}
17554
17655QTEST_APPLESS_MAIN (TestInstruction)
0 commit comments