Debian Codegraph @

Source/UnitTests/Common/x64EmitterTest.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <cstring>
5 #include <disasm.h> // From Bochs, fallback included in Externals.
6 #include <gtest/gtest.h>
7 #include <memory>
8 #include <vector>
9
10 // gtest defines the TEST macro to generate test case functions. It conflicts
11 // with the TEST method in the x64Emitter.
12 //
13 // Since we use TEST_F in this file to attach the test cases to a fixture, we
14 // can get away with simply undef'ing TEST. Phew.
15 #undef TEST
16
17 #include "Common/CPUDetect.h"
18 #include "Common/StringUtil.h"
19 #include "Common/x64Emitter.h"
20
21 #ifdef _MSC_VER
22 // This file is extremely slow to optimize (40s on amd 3990x), so just disable optimizations
23 #pragma optimize("", off)
24 #endif
25
26 namespace Gen
27 {
28 struct NamedReg
29 {
30 X64Reg reg;
31 std::string name;
32 };
33
34 const std::vector<NamedReg> reg8names{
35 {RAX, "al"}, {RBX, "bl"}, {RCX, "cl"}, {RDX, "dl"}, {RSI, "sil"}, {RDI, "dil"},
36 {RBP, "bpl"}, {RSP, "spl"}, {R8, "r8b"}, {R9, "r9b"}, {R10, "r10b"}, {R11, "r11b"},
37 {R12, "r12b"}, {R13, "r13b"}, {R14, "r14b"}, {R15, "r15b"},
38 };
39
40 const std::vector<NamedReg> reg8hnames{
41 {AH, "ah"},
42 {BH, "bh"},
43 {CH, "ch"},
44 {DH, "dh"},
45 };
46
47 const std::vector<NamedReg> reg16names{
48 {RAX, "ax"}, {RBX, "bx"}, {RCX, "cx"}, {RDX, "dx"}, {RSI, "si"}, {RDI, "di"},
49 {RBP, "bp"}, {RSP, "sp"}, {R8, "r8w"}, {R9, "r9w"}, {R10, "r10w"}, {R11, "r11w"},
50 {R12, "r12w"}, {R13, "r13w"}, {R14, "r14w"}, {R15, "r15w"},
51 };
52
53 const std::vector<NamedReg> reg32names{
54 {RAX, "eax"}, {RBX, "ebx"}, {RCX, "ecx"}, {RDX, "edx"}, {RSI, "esi"}, {RDI, "edi"},
55 {RBP, "ebp"}, {RSP, "esp"}, {R8, "r8d"}, {R9, "r9d"}, {R10, "r10d"}, {R11, "r11d"},
56 {R12, "r12d"}, {R13, "r13d"}, {R14, "r14d"}, {R15, "r15d"},
57 };
58
59 const std::vector<NamedReg> reg64names{
60 {RAX, "rax"}, {RBX, "rbx"}, {RCX, "rcx"}, {RDX, "rdx"}, {RSI, "rsi"}, {RDI, "rdi"},
61 {RBP, "rbp"}, {RSP, "rsp"}, {R8, "r8"}, {R9, "r9"}, {R10, "r10"}, {R11, "r11"},
62 {R12, "r12"}, {R13, "r13"}, {R14, "r14"}, {R15, "r15"},
63 };
64
65 const std::vector<NamedReg> xmmnames{
66 {XMM0, "xmm0"}, {XMM1, "xmm1"}, {XMM2, "xmm2"}, {XMM3, "xmm3"},
67 {XMM4, "xmm4"}, {XMM5, "xmm5"}, {XMM6, "xmm6"}, {XMM7, "xmm7"},
68 {XMM8, "xmm8"}, {XMM9, "xmm9"}, {XMM10, "xmm10"}, {XMM11, "xmm11"},
69 {XMM12, "xmm12"}, {XMM13, "xmm13"}, {XMM14, "xmm14"}, {XMM15, "xmm15"},
70 };
71
72 const std::vector<NamedReg> ymmnames{
73 {YMM0, "ymm0"}, {YMM1, "ymm1"}, {YMM2, "ymm2"}, {YMM3, "ymm3"},
74 {YMM4, "ymm4"}, {YMM5, "ymm5"}, {YMM6, "ymm6"}, {YMM7, "ymm7"},
75 {YMM8, "ymm8"}, {YMM9, "ymm9"}, {YMM10, "ymm10"}, {YMM11, "ymm11"},
76 {YMM12, "ymm12"}, {YMM13, "ymm13"}, {YMM14, "ymm14"}, {YMM15, "ymm15"},
77 };
78
79 struct
80 {
81 CCFlags cc;
82 std::string name;
83 } ccnames[] = {
84 {CC_O, "o"}, {CC_NO, "no"}, {CC_B, "b"}, {CC_NB, "nb"}, {CC_Z, "z"}, {CC_NZ, "nz"},
85 {CC_BE, "be"}, {CC_NBE, "nbe"}, {CC_S, "s"}, {CC_NS, "ns"}, {CC_P, "p"}, {CC_NP, "np"},
86 {CC_L, "l"}, {CC_NL, "nl"}, {CC_LE, "le"}, {CC_NLE, "nle"},
87 };
88
89 class x64EmitterTest : public testing::Test
90 {
91 protected:
92 void SetUp() override
93 {
94 // Ensure settings are constant no matter on which actual hardware the test runs.
95 // Attempt to maximize complex code coverage. Note that this will miss some paths.
96 cpu_info.vendor = CPUVendor::Intel;
97 cpu_info.cpu_id = "GenuineIntel";
98 cpu_info.model_name = "Unknown";
99 cpu_info.HTT = true;
100 cpu_info.num_cores = 8;
101 cpu_info.bSSE3 = true;
102 cpu_info.bSSSE3 = true;
103 cpu_info.bSSE4_1 = true;
104 cpu_info.bSSE4_2 = true;
105 cpu_info.bLZCNT = true;
106 cpu_info.bAVX = true;
107 cpu_info.bBMI1 = true;
108 cpu_info.bBMI2 = true;
109 cpu_info.bBMI2FastParallelBitOps = true;
110 cpu_info.bFMA = true;
111 cpu_info.bFMA4 = true;
112 cpu_info.bAES = true;
113 cpu_info.bMOVBE = true;
114 cpu_info.bFlushToZero = true;
115 cpu_info.bAtom = false;
116 cpu_info.bCRC32 = true;
117 cpu_info.bSHA1 = true;
118 cpu_info.bSHA2 = true;
119
120 emitter.reset(new X64CodeBlock());
121 emitter->AllocCodeSpace(4096);
122 code_buffer = emitter->GetWritableCodePtr();
123 code_buffer_end = emitter->GetWritableCodeEnd();
124
125 disasm.reset(new disassembler);
126 disasm->set_syntax_intel();
127 }
128
129 void TearDown() override { cpu_info = CPUInfo(); }
130
131 void ResetCodeBuffer() { emitter->SetCodePtr(code_buffer, code_buffer_end); }
132
133 void ExpectDisassembly(const std::string& expected)
134 {
135 std::string disasmed;
136 const u8* generated_code_iterator = code_buffer;
137 while (generated_code_iterator < emitter->GetCodePtr())
138 {
139 char instr_buffer[1024] = "";
140 generated_code_iterator +=
141 disasm->disasm64((u64)generated_code_iterator, (u64)generated_code_iterator,
142 generated_code_iterator, instr_buffer);
143 disasmed += instr_buffer;
144 disasmed += "\n";
145 }
146
147 auto NormalizeAssembly = [](const std::string& str) -> std::string {
148 // Normalize assembly code to make it suitable for equality checks.
149 // In particular:
150 // * Replace all whitespace characters by a single space.
151 // * Remove leading and trailing whitespaces.
152 // * Lowercase everything.
153 // * Remove all (0x...) addresses.
154 std::string out;
155 bool previous_was_space = false;
156 bool inside_parens = false;
157 for (auto c : str)
158 {
159 c = Common::ToLower(c);
160 if (c == '(')
161 {
162 inside_parens = true;
163 continue;
164 }
165 else if (inside_parens)
166 {
167 if (c == ')')
168 inside_parens = false;
169 continue;
170 }
171 else if (isspace(c))
172 {
173 previous_was_space = true;
174 continue;
175 }
176 else if (previous_was_space)
177 {
178 previous_was_space = false;
179 if (!out.empty())
180 out += ' ';
181 }
182 out += c;
183 }
184 return out;
185 };
186 std::string expected_norm = NormalizeAssembly(expected);
187 std::string disasmed_norm = NormalizeAssembly(disasmed);
188
189 EXPECT_EQ(expected_norm, disasmed_norm);
190
191 ResetCodeBuffer();
192 }
193
194 void ExpectBytes(const std::vector<u8>& expected_bytes)
195 {
196 const std::vector code_bytes(code_buffer, emitter->GetWritableCodePtr());
197
198 EXPECT_EQ(expected_bytes, code_bytes);
199
200 ResetCodeBuffer();
201 }
202
203 std::unique_ptr<X64CodeBlock> emitter;
204 std::unique_ptr<disassembler> disasm;
205 u8* code_buffer = nullptr;
206 u8* code_buffer_end = nullptr;
207 };
208
209 #define TEST_INSTR_NO_OPERANDS(Name, ExpectedDisasm) \
210 TEST_F(x64EmitterTest, Name) \
211 { \
212 emitter->Name(); \
213 ExpectDisassembly(ExpectedDisasm); \
214 }
215
216 TEST_INSTR_NO_OPERANDS(INT3, "int3")
217 TEST_INSTR_NO_OPERANDS(NOP, "nop")
218 TEST_INSTR_NO_OPERANDS(PAUSE, "pause")
219 TEST_INSTR_NO_OPERANDS(STC, "stc")
220 TEST_INSTR_NO_OPERANDS(CLC, "clc")
221 TEST_INSTR_NO_OPERANDS(CMC, "cmc")
222 TEST_INSTR_NO_OPERANDS(LAHF, "lahf")
223 TEST_INSTR_NO_OPERANDS(SAHF, "sahf")
224 TEST_INSTR_NO_OPERANDS(PUSHF, "pushf")
225 TEST_INSTR_NO_OPERANDS(POPF, "popf")
226 TEST_INSTR_NO_OPERANDS(RET, "ret")
227 TEST_INSTR_NO_OPERANDS(RET_FAST, "rep ret")
228 TEST_INSTR_NO_OPERANDS(UD2, "ud2a")
229 TEST_INSTR_NO_OPERANDS(JMPself, "jmp .-2")
230 TEST_INSTR_NO_OPERANDS(LFENCE, "lfence")
231 TEST_INSTR_NO_OPERANDS(MFENCE, "mfence")
232 TEST_INSTR_NO_OPERANDS(SFENCE, "sfence")
233 TEST_INSTR_NO_OPERANDS(CWD, "cwd")
234 TEST_INSTR_NO_OPERANDS(CDQ, "cdq")
235 TEST_INSTR_NO_OPERANDS(CQO, "cqo")
236 TEST_INSTR_NO_OPERANDS(CBW, "cbw")
237 TEST_INSTR_NO_OPERANDS(CWDE, "cwde")
238 TEST_INSTR_NO_OPERANDS(CDQE, "cdqe")
239 TEST_INSTR_NO_OPERANDS(XCHG_AHAL, "xchg al, ah")
240 TEST_INSTR_NO_OPERANDS(RDTSC, "rdtsc")
241
242 TEST_F(x64EmitterTest, NOP_MultiByte)
243 {
244 // 2 bytes is "rep nop", still a simple nop.
245 emitter->NOP(2);
246 ExpectDisassembly("nop");
247
248 for (int i = 3; i <= 11; ++i)
249 {
250 emitter->NOP(i);
251 ExpectDisassembly("multibyte nop");
252 }
253
254 // Larger NOPs are split into several NOPs.
255 emitter->NOP(20);
256 ExpectDisassembly("multibyte nop "
257 "multibyte nop");
258 }
259
260 TEST_F(x64EmitterTest, PUSH_Register)
261 {
262 for (const auto& r : reg64names)
263 {
264 emitter->PUSH(r.reg);
265 ExpectDisassembly("push " + r.name);
266 }
267 }
268
269 TEST_F(x64EmitterTest, PUSH_Immediate)
270 {
271 emitter->PUSH(64, Imm8(0xf0));
272 ExpectDisassembly("push 0xfffffffffffffff0");
273
274 // X64 is weird like that... this pushes 2 bytes, not 8 bytes with sext.
275 emitter->PUSH(64, Imm16(0xe0f0));
276 ExpectDisassembly("push 0xe0f0");
277
278 emitter->PUSH(64, Imm32(0xc0d0e0f0));
279 ExpectDisassembly("push 0xffffffffc0d0e0f0");
280 }
281
282 TEST_F(x64EmitterTest, PUSH_MComplex)
283 {
284 emitter->PUSH(64, MComplex(RAX, RBX, SCALE_2, 4));
285 ExpectDisassembly("push qword ptr ds:[rax+rbx*2+4]");
286 }
287
288 TEST_F(x64EmitterTest, POP_Register)
289 {
290 for (const auto& r : reg64names)
291 {
292 emitter->POP(r.reg);
293 ExpectDisassembly("pop " + r.name);
294 }
295 }
296
297 TEST_F(x64EmitterTest, JMP)
298 {
299 emitter->NOP(1);
300 emitter->JMP(code_buffer);
301 ExpectBytes({/* nop */ 0x90, /* short jmp */ 0xeb, /* offset -3 */ 0xfd});
302
303 emitter->NOP(0x90);
304 const u8* after_nops = emitter->GetCodePtr();
305 ResetCodeBuffer();
306 emitter->JMP(after_nops);
307 ExpectBytes({/* near jmp */ 0xe9, /* offset */ 0x8B, 0, 0, 0});
308 }
309
310 TEST_F(x64EmitterTest, JMPptr_Register)
311 {
312 for (const auto& r : reg64names)
313 {
314 emitter->JMPptr(R(r.reg));
315 ExpectDisassembly("jmp " + r.name);
316 }
317 }
318
319 TEST_F(x64EmitterTest, J)
320 {
321 FixupBranch jump = emitter->J(XEmitter::Jump::Short);
322 emitter->NOP(1);
323 emitter->SetJumpTarget(jump);
324 ExpectBytes({/* short jmp */ 0xeb, /* offset 1 */ 0x1, /* nop */ 0x90});
325
326 jump = emitter->J(XEmitter::Jump::Near);
327 emitter->NOP(1);
328 emitter->SetJumpTarget(jump);
329 ExpectBytes({/* near jmp */ 0xe9, /* offset 1 */ 0x1, 0x0, 0x0, 0x0, /* nop */ 0x90});
330 }
331
332 TEST_F(x64EmitterTest, CALL)
333 {
334 FixupBranch call = emitter->CALL();
335 emitter->NOP(6);
336 emitter->SetJumpTarget(call);
337 ExpectDisassembly("call .+6 "
338 "multibyte nop");
339
340 const u8* const code_start = emitter->GetCodePtr();
341 emitter->CALL(code_start + 5);
342 ExpectDisassembly("call .+0");
343
344 emitter->NOP(6);
345 emitter->CALL(code_start);
346 ExpectDisassembly("multibyte nop "
347 "call .-11");
348 }
349
350 TEST_F(x64EmitterTest, J_CC)
351 {
352 for (const auto& [condition_code, condition_name] : ccnames)
353 {
354 FixupBranch fixup = emitter->J_CC(condition_code, XEmitter::Jump::Short);
355 emitter->NOP(1);
356 emitter->SetJumpTarget(fixup);
357 const u8 short_jump_condition_opcode = 0x70 + condition_code;
358 ExpectBytes({short_jump_condition_opcode, /* offset 1 */ 0x1, /* nop */ 0x90});
359
360 fixup = emitter->J_CC(condition_code, XEmitter::Jump::Near);
361 emitter->NOP(1);
362 emitter->SetJumpTarget(fixup);
363 const u8 near_jump_condition_opcode = 0x80 + condition_code;
364 ExpectBytes({/* two byte opcode */ 0x0f, near_jump_condition_opcode, /* offset 1 */ 0x1, 0x0,
365 0x0, 0x0, /* nop */ 0x90});
366 }
367
368 // Verify a short jump is used when possible and a near jump when needed.
369 //
370 // A short jump to a particular address and a near jump to that same address will have different
371 // offsets. This is because short jumps are 2 bytes and near jumps are 6 bytes, and the offset to
372 // the target is calculated from the address of the next instruction.
373
374 const u8* const code_start = emitter->GetCodePtr();
375 constexpr int short_jump_bytes = 2;
376 const u8* const next_byte_after_short_jump_instruction = code_start + short_jump_bytes;
377
378 constexpr int longest_backward_short_jump = 0x80;
379 const u8* const furthest_byte_reachable_with_backward_short_jump =
380 next_byte_after_short_jump_instruction - longest_backward_short_jump;
381 emitter->J_CC(CC_O, furthest_byte_reachable_with_backward_short_jump);
382 ExpectBytes({/* JO opcode */ 0x70, /* offset -128 */ 0x80});
383
384 const u8* const closest_byte_requiring_backward_near_jump =
385 furthest_byte_reachable_with_backward_short_jump - 1;
386 emitter->J_CC(CC_O, closest_byte_requiring_backward_near_jump);
387 // This offset is 5 less than the offset for the furthest backward short jump. -1 because this
388 // target is 1 byte before the short target, and -4 because the address of the next instruction is
389 // 4 bytes further away from the jump target than it would be with a short jump.
390 ExpectBytes({/* two byte JO opcode */ 0x0f, 0x80, /* offset -133 */ 0x7b, 0xff, 0xff, 0xff});
391
392 constexpr int longest_forward_short_jump = 0x7f;
393 const u8* const furthest_byte_reachable_with_forward_short_jump =
394 next_byte_after_short_jump_instruction + longest_forward_short_jump;
395 emitter->J_CC(CC_O, furthest_byte_reachable_with_forward_short_jump);
396 ExpectBytes({/* JO opcode */ 0x70, /* offset 127 */ 0x7f});
397
398 const u8* const closest_byte_requiring_forward_near_jump =
399 furthest_byte_reachable_with_forward_short_jump + 1;
400 emitter->J_CC(CC_O, closest_byte_requiring_forward_near_jump);
401 // This offset is 3 less than the offset for the furthest forward short jump. +1 because this
402 // target is 1 byte after the short target, and -4 because the address of the next instruction is
403 // 4 bytes closer to the jump target than it would be with a short jump.
404 ExpectBytes({/* two byte JO opcode */ 0x0f, 0x80, /* offset 124 */ 0x7c, 0x0, 0x0, 0x0});
405 }
406
407 TEST_F(x64EmitterTest, SETcc)
408 {
409 for (const auto& cc : ccnames)
410 {
411 for (const auto& r : reg8names)
412 {
413 emitter->SETcc(cc.cc, R(r.reg));
414 ExpectDisassembly("set" + cc.name + " " + r.name);
415 }
416 for (const auto& r : reg8hnames)
417 {
418 emitter->SETcc(cc.cc, R(r.reg));
419 ExpectDisassembly("set" + cc.name + " " + r.name);
420 }
421 }
422 }
423
424 TEST_F(x64EmitterTest, CMOVcc_Register)
425 {
426 for (const auto& cc : ccnames)
427 {
428 emitter->CMOVcc(64, RAX, R(R12), cc.cc);
429 emitter->CMOVcc(32, RAX, R(R12), cc.cc);
430 emitter->CMOVcc(16, RAX, R(R12), cc.cc);
431
432 ExpectDisassembly("cmov" + cc.name +
433 " rax, r12 "
434 "cmov" +
435 cc.name +
436 " eax, r12d "
437 "cmov" +
438 cc.name + " ax, r12w");
439 }
440 }
441
442 #define BITSEARCH_TEST(Name) \
443 TEST_F(x64EmitterTest, Name) \
444 { \
445 struct \
446 { \
447 int bits; \
448 std::vector<NamedReg> regs; \
449 std::string size; \
450 std::string rax_name; \
451 } regsets[] = { \
452 {16, reg16names, "word", "ax"}, \
453 {32, reg32names, "dword", "eax"}, \
454 {64, reg64names, "qword", "rax"}, \
455 }; \
456 for (const auto& regset : regsets) \
457 for (const auto& r : regset.regs) \
458 { \
459 emitter->Name(regset.bits, r.reg, R(RAX)); \
460 emitter->Name(regset.bits, RAX, R(r.reg)); \
461 emitter->Name(regset.bits, r.reg, MatR(RAX)); \
462 ExpectDisassembly(#Name " " + r.name + ", " + regset.rax_name + " " #Name " " + \
463 regset.rax_name + ", " + r.name + " " #Name " " + r.name + ", " + \
464 regset.size + " ptr ds:[rax] "); \
465 } \
466 }
467
468 BITSEARCH_TEST(BSR);
469 BITSEARCH_TEST(BSF);
470 BITSEARCH_TEST(LZCNT);
471 BITSEARCH_TEST(TZCNT);
472
473 TEST_F(x64EmitterTest, PREFETCH)
474 {
475 emitter->PREFETCH(XEmitter::PrefetchLevel::NTA, MatR(R12));
476 emitter->PREFETCH(XEmitter::PrefetchLevel::T0, MatR(R12));
477 emitter->PREFETCH(XEmitter::PrefetchLevel::T1, MatR(R12));
478 emitter->PREFETCH(XEmitter::PrefetchLevel::T2, MatR(R12));
479
480 ExpectDisassembly("prefetchnta byte ptr ds:[r12] "
481 "prefetcht0 byte ptr ds:[r12] "
482 "prefetcht1 byte ptr ds:[r12] "
483 "prefetcht2 byte ptr ds:[r12]");
484 }
485
486 TEST_F(x64EmitterTest, MOVNTI)
487 {
488 emitter->MOVNTI(32, MatR(RAX), R12);
489 emitter->MOVNTI(32, M(code_buffer), R12);
490 emitter->MOVNTI(64, MatR(RAX), R12);
491 emitter->MOVNTI(64, M(code_buffer), R12);
492
493 ExpectDisassembly("movnti dword ptr ds:[rax], r12d "
494 "movnti dword ptr ds:[rip-12], r12d "
495 "movnti qword ptr ds:[rax], r12 "
496 "movnti qword ptr ds:[rip-24], r12");
497 }
498
499 // Grouped together since these 3 instructions do exactly the same thing.
500 TEST_F(x64EmitterTest, MOVNT_DQ_PS_PD)
501 {
502 for (const auto& r : xmmnames)
503 {
504 emitter->MOVNTDQ(MatR(RAX), r.reg);
505 emitter->MOVNTPS(MatR(RAX), r.reg);
506 emitter->MOVNTPD(MatR(RAX), r.reg);
507 ExpectDisassembly("movntdq dqword ptr ds:[rax], " + r.name +
508 " "
509 "movntps dqword ptr ds:[rax], " +
510 r.name +
511 " "
512 "movntpd dqword ptr ds:[rax], " +
513 r.name);
514 }
515 }
516
517 #define MUL_DIV_TEST(Name) \
518 TEST_F(x64EmitterTest, Name) \
519 { \
520 struct \
521 { \
522 int bits; \
523 std::vector<NamedReg> regs; \
524 std::string out_name; \
525 } regsets[] = { \
526 {8, reg8names, "al"}, {8, reg8hnames, "al"}, {16, reg16names, "ax"}, \
527 {32, reg32names, "eax"}, {64, reg64names, "rax"}, \
528 }; \
529 for (const auto& regset : regsets) \
530 for (const auto& r : regset.regs) \
531 { \
532 emitter->Name(regset.bits, R(r.reg)); \
533 ExpectDisassembly(#Name " " + regset.out_name + ", " + r.name); \
534 } \
535 }
536
537 MUL_DIV_TEST(MUL)
538 MUL_DIV_TEST(IMUL)
539 MUL_DIV_TEST(DIV)
540 MUL_DIV_TEST(IDIV)
541
542 // TODO: More complex IMUL variants.
543
544 #define SHIFT_TEST(Name) \
545 TEST_F(x64EmitterTest, Name) \
546 { \
547 struct \
548 { \
549 int bits; \
550 std::vector<NamedReg> regs; \
551 } regsets[] = { \
552 {8, reg8names}, {8, reg8hnames}, {16, reg16names}, {32, reg32names}, {64, reg64names}, \
553 }; \
554 for (const auto& regset : regsets) \
555 for (const auto& r : regset.regs) \
556 { \
557 emitter->Name(regset.bits, R(r.reg), Imm8(1)); \
558 emitter->Name(regset.bits, R(r.reg), Imm8(4)); \
559 emitter->Name(regset.bits, R(r.reg), R(CL)); \
560 ExpectDisassembly(#Name " " + r.name + ", 1 " #Name " " + r.name + ", 0x04 " #Name " " + \
561 r.name + ", cl"); \
562 } \
563 }
564
565 SHIFT_TEST(ROL)
566 SHIFT_TEST(ROR)
567 SHIFT_TEST(RCL)
568 SHIFT_TEST(RCR)
569 SHIFT_TEST(SHL)
570 SHIFT_TEST(SHR)
571 SHIFT_TEST(SAR)
572
573 #define BT_TEST(Name) \
574 TEST_F(x64EmitterTest, Name) \
575 { \
576 struct \
577 { \
578 int bits; \
579 std::vector<NamedReg> regs; \
580 std::string out_name; \
581 std::string size; \
582 } regsets[] = { \
583 {16, reg16names, "ax", "word"}, \
584 {32, reg32names, "eax", "dword"}, \
585 {64, reg64names, "rax", "qword"}, \
586 }; \
587 for (const auto& regset : regsets) \
588 for (const auto& r : regset.regs) \
589 { \
590 emitter->Name(regset.bits, R(r.reg), R(RAX)); \
591 emitter->Name(regset.bits, R(RAX), R(r.reg)); \
592 emitter->Name(regset.bits, R(r.reg), Imm8(0x42)); \
593 emitter->Name(regset.bits, MatR(R12), R(r.reg)); \
594 ExpectDisassembly(#Name " " + r.name + ", " + regset.out_name + " " #Name " " + \
595 regset.out_name + ", " + r.name + " " #Name " " + r.name + \
596 ", 0x42 " #Name " " + regset.size + " ptr ds:[r12], " + r.name); \
597 } \
598 }
599
600 BT_TEST(BT)
601 BT_TEST(BTS)
602 BT_TEST(BTR)
603 BT_TEST(BTC)
604
605 // TODO: LEA tests
606
607 #define ONE_OP_ARITH_TEST(Name) \
608 TEST_F(x64EmitterTest, Name) \
609 { \
610 struct \
611 { \
612 int bits; \
613 std::vector<NamedReg> regs; \
614 std::string size; \
615 } regsets[] = { \
616 {8, reg8names, "byte"}, {8, reg8hnames, "byte"}, {16, reg16names, "word"}, \
617 {32, reg32names, "dword"}, {64, reg64names, "qword"}, \
618 }; \
619 for (const auto& regset : regsets) \
620 for (const auto& r : regset.regs) \
621 { \
622 emitter->Name(regset.bits, R(r.reg)); \
623 emitter->Name(regset.bits, MatR(RAX)); \
624 emitter->Name(regset.bits, MatR(R12)); \
625 ExpectDisassembly(#Name " " + r.name + " " #Name " " + regset.size + \
626 " ptr ds:[rax] " #Name " " + regset.size + " ptr ds:[r12]"); \
627 } \
628 }
629
630 ONE_OP_ARITH_TEST(NOT)
631 ONE_OP_ARITH_TEST(NEG)
632
633 #define TWO_OP_ARITH_TEST(Name) \
634 TEST_F(x64EmitterTest, Name) \
635 { \
636 struct \
637 { \
638 int bits; \
639 std::vector<NamedReg> regs; \
640 std::string size; \
641 std::string rax_name; \
642 Gen::OpArg imm; \
643 std::string immname; \
644 } regsets[] = { \
645 {8, reg8names, "byte", "al", Imm8(0xEF), "0xef"}, \
646 {8, reg8hnames, "byte", "al", Imm8(0xEF), "0xef"}, \
647 {16, reg16names, "word", "ax", Imm16(0xBEEF), "0xbeef"}, \
648 {32, reg32names, "dword", "eax", Imm32(0xDEADBEEF), "0xdeadbeef"}, \
649 {64, reg64names, "qword", "rax", Imm32(0xDEADBEEF), "0xffffffffdeadbeef"}, \
650 }; \
651 for (const auto& regset : regsets) \
652 for (const auto& r : regset.regs) \
653 { \
654 emitter->Name(regset.bits, R(r.reg), R(RAX)); \
655 emitter->Name(regset.bits, R(RAX), R(r.reg)); \
656 emitter->Name(regset.bits, R(r.reg), MatR(RAX)); \
657 emitter->Name(regset.bits, MatR(RAX), R(r.reg)); \
658 emitter->Name(regset.bits, R(r.reg), regset.imm); \
659 ExpectDisassembly(#Name " " + r.name + ", " + regset.rax_name + " " #Name " " + \
660 regset.rax_name + ", " + r.name + " " #Name " " + r.name + ", " + \
661 regset.size + " ptr ds:[rax] " #Name " " + regset.size + \
662 " ptr ds:[rax], " + r.name + " " #Name " " + r.name + ", " + \
663 regset.immname); \
664 } \
665 }
666
667 TWO_OP_ARITH_TEST(ADD)
668 TWO_OP_ARITH_TEST(ADC)
669 TWO_OP_ARITH_TEST(SUB)
670 TWO_OP_ARITH_TEST(SBB)
671 TWO_OP_ARITH_TEST(AND)
672 TWO_OP_ARITH_TEST(CMP)
673 TWO_OP_ARITH_TEST(OR)
674 TWO_OP_ARITH_TEST(XOR)
675 TWO_OP_ARITH_TEST(MOV)
676
677 TEST_F(x64EmitterTest, MOV64)
678 {
679 for (size_t i = 0; i < reg64names.size(); i++)
680 {
681 emitter->MOV(64, R(reg64names[i].reg), Imm64(0xDEADBEEFDEADBEEF));
682 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 10);
683 ExpectDisassembly("mov " + reg64names[i].name + ", 0xdeadbeefdeadbeef");
684
685 emitter->MOV(64, R(reg64names[i].reg), Imm64(0xFFFFFFFFDEADBEEF));
686 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 7);
687 ExpectDisassembly("mov " + reg64names[i].name + ", 0xffffffffdeadbeef");
688
689 emitter->MOV(64, R(reg64names[i].reg), Imm64(0xDEADBEEF));
690 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 5 + (i > 7));
691 ExpectDisassembly("mov " + reg32names[i].name + ", 0xdeadbeef");
692
693 emitter->MOV(64, R(reg64names[i].reg), Imm32(0x7FFFFFFF));
694 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 5 + (i > 7));
695 ExpectDisassembly("mov " + reg32names[i].name + ", 0x7fffffff");
696 }
697 }
698
699 TEST_F(x64EmitterTest, MOV_AtReg)
700 {
701 for (const auto& src : reg64names)
702 {
703 std::string segment = src.reg == RSP || src.reg == RBP ? "ss" : "ds";
704
705 emitter->MOV(64, R(RAX), MatR(src.reg));
706 EXPECT_EQ(emitter->GetCodePtr(),
707 code_buffer + 3 + ((src.reg & 7) == RBP || (src.reg & 7) == RSP));
708 ExpectDisassembly("mov rax, qword ptr " + segment + ":[" + src.name + "]");
709 }
710 }
711
712 TEST_F(x64EmitterTest, MOV_RegSum)
713 {
714 for (const auto& src2 : reg64names)
715 {
716 for (const auto& src1 : reg64names)
717 {
718 if (src2.reg == RSP)
719 continue;
720 std::string segment = src1.reg == RSP || src1.reg == RBP ? "ss" : "ds";
721
722 emitter->MOV(64, R(RAX), MRegSum(src1.reg, src2.reg));
723 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 4 + ((src1.reg & 7) == RBP));
724 ExpectDisassembly("mov rax, qword ptr " + segment + ":[" + src1.name + "+" + src2.name + "]");
725 }
726 }
727 }
728
729 TEST_F(x64EmitterTest, MOV_Disp)
730 {
731 for (const auto& dest : reg64names)
732 {
733 for (const auto& src : reg64names)
734 {
735 std::string segment = src.reg == RSP || src.reg == RBP ? "ss" : "ds";
736
737 emitter->MOV(64, R(dest.reg), MDisp(src.reg, 42));
738 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 4 + ((src.reg & 7) == RSP));
739 ExpectDisassembly("mov " + dest.name + ", qword ptr " + segment + ":[" + src.name + "+42]");
740
741 emitter->MOV(64, R(dest.reg), MDisp(src.reg, 1000));
742 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 7 + ((src.reg & 7) == RSP));
743 ExpectDisassembly("mov " + dest.name + ", qword ptr " + segment + ":[" + src.name + "+1000]");
744 }
745 }
746 }
747
748 TEST_F(x64EmitterTest, MOV_Scaled)
749 {
750 for (const auto& src : reg64names)
751 {
752 if (src.reg == RSP)
753 continue;
754
755 emitter->MOV(64, R(RAX), MScaled(src.reg, 2, 42));
756 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 8);
757 ExpectDisassembly("mov rax, qword ptr ds:[" + src.name + "*2+42]");
758 }
759 }
760
761 TEST_F(x64EmitterTest, MOV_Complex)
762 {
763 for (const auto& src1 : reg64names)
764 {
765 std::string segment = src1.reg == RSP || src1.reg == RBP ? "ss" : "ds";
766
767 for (const auto& src2 : reg64names)
768 {
769 if (src2.reg == RSP)
770 continue;
771
772 emitter->MOV(64, R(RAX), MComplex(src1.reg, src2.reg, 4, 0));
773 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 4 + ((src1.reg & 7) == RBP));
774 ExpectDisassembly("mov rax, qword ptr " + segment + ":[" + src1.name + "+" + src2.name +
775 "*4]");
776
777 emitter->MOV(64, R(RAX), MComplex(src1.reg, src2.reg, 4, 42));
778 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 5);
779 ExpectDisassembly("mov rax, qword ptr " + segment + ":[" + src1.name + "+" + src2.name +
780 "*4+42]");
781
782 emitter->MOV(64, R(RAX), MComplex(src1.reg, src2.reg, 4, 1000));
783 EXPECT_EQ(emitter->GetCodePtr(), code_buffer + 8);
784 ExpectDisassembly("mov rax, qword ptr " + segment + ":[" + src1.name + "+" + src2.name +
785 "*4+1000]");
786 }
787 }
788 }
789
790 // TODO: Disassembler inverts operands here.
791 // TWO_OP_ARITH_TEST(XCHG)
792 // TWO_OP_ARITH_TEST(TEST)
793
794 TEST_F(x64EmitterTest, BSWAP)
795 {
796 struct
797 {
798 int bits;
799 std::vector<NamedReg> regs;
800 } regsets[] = {
801 {32, reg32names},
802 {64, reg64names},
803 };
804 for (const auto& regset : regsets)
805 for (const auto& r : regset.regs)
806 {
807 emitter->BSWAP(regset.bits, r.reg);
808 ExpectDisassembly("bswap " + r.name);
809 }
810 }
811
812 TEST_F(x64EmitterTest, MOVSX)
813 {
814 emitter->MOVSX(16, 8, RAX, R(AH));
815 emitter->MOVSX(32, 8, RAX, R(R12));
816 emitter->MOVSX(32, 16, R12, R(RBX));
817 emitter->MOVSX(64, 8, R12, R(RBX));
818 emitter->MOVSX(64, 16, RAX, R(R12));
819 emitter->MOVSX(64, 32, R12, R(RSP));
820 ExpectDisassembly("movsx ax, ah "
821 "movsx eax, r12b "
822 "movsx r12d, bx "
823 "movsx r12, bl "
824 "movsx rax, r12w "
825 "movsxd r12, esp");
826 }
827
828 TEST_F(x64EmitterTest, MOVZX)
829 {
830 emitter->MOVZX(16, 8, RAX, R(AH));
831 emitter->MOVZX(32, 8, R12, R(RBP));
832 emitter->MOVZX(64, 8, R12, R(RDI));
833 emitter->MOVZX(32, 16, RAX, R(R12));
834 emitter->MOVZX(64, 16, RCX, R(RSI));
835 ExpectDisassembly("movzx ax, ah "
836 "movzx r12d, bpl "
837 "movzx r12d, dil " // Generates 32 bit movzx
838 "movzx eax, r12w "
839 "movzx ecx, si");
840 }
841
842 TEST_F(x64EmitterTest, MOVBE)
843 {
844 emitter->MOVBE(16, RAX, MatR(R12));
845 emitter->MOVBE(16, MatR(RAX), R12);
846 emitter->MOVBE(32, RAX, MatR(R12));
847 emitter->MOVBE(32, MatR(RAX), R12);
848 emitter->MOVBE(64, RAX, MatR(R12));
849 emitter->MOVBE(64, MatR(RAX), R12);
850 ExpectDisassembly("movbe ax, word ptr ds:[r12] "
851 "movbe word ptr ds:[rax], r12w "
852 "movbe eax, dword ptr ds:[r12] "
853 "movbe dword ptr ds:[rax], r12d "
854 "movbe rax, qword ptr ds:[r12] "
855 "movbe qword ptr ds:[rax], r12");
856 }
857
858 TEST_F(x64EmitterTest, STMXCSR)
859 {
860 emitter->STMXCSR(MatR(R12));
861 ExpectDisassembly("stmxcsr dword ptr ds:[r12]");
862 }
863
864 TEST_F(x64EmitterTest, LDMXCSR)
865 {
866 emitter->LDMXCSR(MatR(R12));
867 ExpectDisassembly("ldmxcsr dword ptr ds:[r12]");
868 }
869
870 #define TWO_OP_SSE_TEST(Name, MemBits) \
871 TEST_F(x64EmitterTest, Name) \
872 { \
873 for (const auto& r1 : xmmnames) \
874 { \
875 for (const auto& r2 : xmmnames) \
876 { \
877 emitter->Name(r1.reg, R(r2.reg)); \
878 ExpectDisassembly(#Name " " + r1.name + ", " + r2.name); \
879 } \
880 emitter->Name(r1.reg, MatR(R12)); \
881 ExpectDisassembly(#Name " " + r1.name + ", " MemBits " ptr ds:[r12]"); \
882 } \
883 }
884
885 TWO_OP_SSE_TEST(ADDSS, "dword")
886 TWO_OP_SSE_TEST(SUBSS, "dword")
887 TWO_OP_SSE_TEST(MULSS, "dword")
888 TWO_OP_SSE_TEST(DIVSS, "dword")
889 TWO_OP_SSE_TEST(MINSS, "dword")
890 TWO_OP_SSE_TEST(MAXSS, "dword")
891 TWO_OP_SSE_TEST(SQRTSS, "dword")
892 TWO_OP_SSE_TEST(RSQRTSS, "dword")
893
894 TWO_OP_SSE_TEST(ADDSD, "qword")
895 TWO_OP_SSE_TEST(SUBSD, "qword")
896 TWO_OP_SSE_TEST(MULSD, "qword")
897 TWO_OP_SSE_TEST(DIVSD, "qword")
898 TWO_OP_SSE_TEST(MINSD, "qword")
899 TWO_OP_SSE_TEST(MAXSD, "qword")
900 TWO_OP_SSE_TEST(SQRTSD, "qword")
901
902 TWO_OP_SSE_TEST(ADDPS, "dqword")
903 TWO_OP_SSE_TEST(SUBPS, "dqword")
904 TWO_OP_SSE_TEST(MULPS, "dqword")
905 TWO_OP_SSE_TEST(DIVPS, "dqword")
906 TWO_OP_SSE_TEST(MINPS, "dqword")
907 TWO_OP_SSE_TEST(MAXPS, "dqword")
908 TWO_OP_SSE_TEST(SQRTPS, "dqword")
909 TWO_OP_SSE_TEST(RSQRTPS, "dqword")
910 TWO_OP_SSE_TEST(ANDPS, "dqword")
911 TWO_OP_SSE_TEST(ANDNPS, "dqword")
912 TWO_OP_SSE_TEST(ORPS, "dqword")
913 TWO_OP_SSE_TEST(XORPS, "dqword")
914
915 TWO_OP_SSE_TEST(ADDPD, "dqword")
916 TWO_OP_SSE_TEST(SUBPD, "dqword")
917 TWO_OP_SSE_TEST(MULPD, "dqword")
918 TWO_OP_SSE_TEST(DIVPD, "dqword")
919 TWO_OP_SSE_TEST(MINPD, "dqword")
920 TWO_OP_SSE_TEST(MAXPD, "dqword")
921 TWO_OP_SSE_TEST(SQRTPD, "dqword")
922 TWO_OP_SSE_TEST(ANDPD, "dqword")
923 TWO_OP_SSE_TEST(ANDNPD, "dqword")
924 TWO_OP_SSE_TEST(ORPD, "dqword")
925 TWO_OP_SSE_TEST(XORPD, "dqword")
926
927 TWO_OP_SSE_TEST(MOVSLDUP, "dqword")
928 TWO_OP_SSE_TEST(MOVSHDUP, "dqword")
929 TWO_OP_SSE_TEST(MOVDDUP, "qword")
930
931 TWO_OP_SSE_TEST(UNPCKLPS, "dqword")
932 TWO_OP_SSE_TEST(UNPCKHPS, "dqword")
933 TWO_OP_SSE_TEST(UNPCKLPD, "dqword")
934 TWO_OP_SSE_TEST(UNPCKHPD, "dqword")
935
936 TWO_OP_SSE_TEST(COMISS, "dword")
937 TWO_OP_SSE_TEST(UCOMISS, "dword")
938 TWO_OP_SSE_TEST(COMISD, "qword")
939 TWO_OP_SSE_TEST(UCOMISD, "qword")
940
941 // register-only instructions
942 #define TWO_OP_SSE_REG_TEST(Name, MemBits) \
943 TEST_F(x64EmitterTest, Name) \
944 { \
945 for (const auto& r1 : xmmnames) \
946 { \
947 for (const auto& r2 : xmmnames) \
948 { \
949 emitter->Name(r1.reg, r2.reg); \
950 ExpectDisassembly(#Name " " + r1.name + ", " + r2.name); \
951 } \
952 } \
953 }
954
955 TWO_OP_SSE_REG_TEST(MOVHLPS, "qword")
956 TWO_OP_SSE_REG_TEST(MOVLHPS, "qword")
957
958 // "register + memory"-only instructions
959 #define TWO_OP_SSE_MEM_TEST(Name, MemBits) \
960 TEST_F(x64EmitterTest, Name) \
961 { \
962 for (const auto& r1 : xmmnames) \
963 { \
964 emitter->Name(r1.reg, MatR(R12)); \
965 ExpectDisassembly(#Name " " + r1.name + ", " MemBits " ptr ds:[r12]"); \
966 emitter->Name(MatR(R12), r1.reg); \
967 ExpectDisassembly(#Name " " MemBits " ptr ds:[r12], " + r1.name); \
968 } \
969 }
970
971 TWO_OP_SSE_MEM_TEST(MOVLPS, "qword")
972 TWO_OP_SSE_MEM_TEST(MOVHPS, "qword")
973 TWO_OP_SSE_MEM_TEST(MOVLPD, "qword")
974 TWO_OP_SSE_MEM_TEST(MOVHPD, "qword")
975
976 // TODO: CMPSS/SD
977 // TODO: SHUFPS/PD
978 // TODO: more SSE MOVs
979 // TODO: MOVMSK
980
981 TEST_F(x64EmitterTest, MASKMOVDQU)
982 {
983 for (const auto& r1 : xmmnames)
984 {
985 for (const auto& r2 : xmmnames)
986 {
987 emitter->MASKMOVDQU(r1.reg, r2.reg);
988 ExpectDisassembly("maskmovdqu " + r1.name + ", " + r2.name + ", dqword ptr ds:[rdi]");
989 }
990 }
991 }
992
993 TEST_F(x64EmitterTest, LDDQU)
994 {
995 for (const auto& r : xmmnames)
996 {
997 emitter->LDDQU(r.reg, MatR(R12));
998 ExpectDisassembly("lddqu " + r.name + ", dqword ptr ds:[r12]");
999 }
1000 }
1001
1002 TWO_OP_SSE_TEST(CVTPS2PD, "dqword")
1003 TWO_OP_SSE_TEST(CVTPD2PS, "dqword")
1004 TWO_OP_SSE_TEST(CVTSS2SD, "dword")
1005 TWO_OP_SSE_TEST(CVTSD2SS, "qword")
1006 TWO_OP_SSE_TEST(CVTDQ2PD, "qword")
1007 TWO_OP_SSE_TEST(CVTPD2DQ, "dqword")
1008 TWO_OP_SSE_TEST(CVTDQ2PS, "dqword")
1009 TWO_OP_SSE_TEST(CVTPS2DQ, "dqword")
1010 TWO_OP_SSE_TEST(CVTTPS2DQ, "dqword")
1011 TWO_OP_SSE_TEST(CVTTPD2DQ, "dqword")
1012
1013 // TODO: CVT2SI
1014
1015 TWO_OP_SSE_TEST(PACKSSDW, "dqword")
1016 TWO_OP_SSE_TEST(PACKSSWB, "dqword")
1017 TWO_OP_SSE_TEST(PACKUSDW, "dqword")
1018 TWO_OP_SSE_TEST(PACKUSWB, "dqword")
1019
1020 TWO_OP_SSE_TEST(PUNPCKLBW, "dqword")
1021 TWO_OP_SSE_TEST(PUNPCKLWD, "dqword")
1022 TWO_OP_SSE_TEST(PUNPCKLDQ, "dqword")
1023 TWO_OP_SSE_TEST(PUNPCKLQDQ, "dqword")
1024
1025 TWO_OP_SSE_TEST(PTEST, "dqword")
1026 TWO_OP_SSE_TEST(PAND, "dqword")
1027 TWO_OP_SSE_TEST(PANDN, "dqword")
1028 TWO_OP_SSE_TEST(POR, "dqword")
1029 TWO_OP_SSE_TEST(PXOR, "dqword")
1030 TWO_OP_SSE_TEST(PADDB, "dqword")
1031 TWO_OP_SSE_TEST(PADDW, "dqword")
1032 TWO_OP_SSE_TEST(PADDD, "dqword")
1033 TWO_OP_SSE_TEST(PADDQ, "dqword")
1034 TWO_OP_SSE_TEST(PADDSB, "dqword")
1035 TWO_OP_SSE_TEST(PADDSW, "dqword")
1036 TWO_OP_SSE_TEST(PADDUSB, "dqword")
1037 TWO_OP_SSE_TEST(PADDUSW, "dqword")
1038 TWO_OP_SSE_TEST(PSUBB, "dqword")
1039 TWO_OP_SSE_TEST(PSUBW, "dqword")
1040 TWO_OP_SSE_TEST(PSUBD, "dqword")
1041 TWO_OP_SSE_TEST(PSUBQ, "dqword")
1042 TWO_OP_SSE_TEST(PSUBUSB, "dqword")
1043 TWO_OP_SSE_TEST(PSUBUSW, "dqword")
1044 TWO_OP_SSE_TEST(PAVGB, "dqword")
1045 TWO_OP_SSE_TEST(PAVGW, "dqword")
1046 TWO_OP_SSE_TEST(PCMPEQB, "dqword")
1047 TWO_OP_SSE_TEST(PCMPEQW, "dqword")
1048 TWO_OP_SSE_TEST(PCMPEQD, "dqword")
1049 TWO_OP_SSE_TEST(PCMPGTB, "dqword")
1050 TWO_OP_SSE_TEST(PCMPGTW, "dqword")
1051 TWO_OP_SSE_TEST(PCMPGTD, "dqword")
1052 TWO_OP_SSE_TEST(PMADDWD, "dqword")
1053 TWO_OP_SSE_TEST(PSADBW, "dqword")
1054 TWO_OP_SSE_TEST(PMAXSW, "dqword")
1055 TWO_OP_SSE_TEST(PMAXUB, "dqword")
1056 TWO_OP_SSE_TEST(PMINSW, "dqword")
1057 TWO_OP_SSE_TEST(PMINUB, "dqword")
1058 TWO_OP_SSE_TEST(PSHUFB, "dqword")
1059
1060 // TODO: PEXT/INS/SHUF/MOVMSK
1061
1062 TWO_OP_SSE_TEST(PMOVSXBW, "qword")
1063 TWO_OP_SSE_TEST(PMOVSXBD, "dword")
1064 TWO_OP_SSE_TEST(PMOVSXBQ, "word")
1065 TWO_OP_SSE_TEST(PMOVSXWD, "qword")
1066 TWO_OP_SSE_TEST(PMOVSXWQ, "dword")
1067 TWO_OP_SSE_TEST(PMOVSXDQ, "qword")
1068
1069 TWO_OP_SSE_TEST(PMOVZXBW, "qword")
1070 TWO_OP_SSE_TEST(PMOVZXBD, "dword")
1071 TWO_OP_SSE_TEST(PMOVZXBQ, "word")
1072 TWO_OP_SSE_TEST(PMOVZXWD, "qword")
1073 TWO_OP_SSE_TEST(PMOVZXWQ, "dword")
1074 TWO_OP_SSE_TEST(PMOVZXDQ, "qword")
1075
1076 TWO_OP_SSE_TEST(PBLENDVB, "dqword")
1077 TWO_OP_SSE_TEST(BLENDVPS, "dqword")
1078 TWO_OP_SSE_TEST(BLENDVPD, "dqword")
1079
1080 #define TWO_OP_PLUS_IMM_SSE_TEST(Name, MemBits) \
1081 TEST_F(x64EmitterTest, Name) \
1082 { \
1083 for (const auto& r1 : xmmnames) \
1084 { \
1085 for (const auto& r2 : xmmnames) \
1086 { \
1087 emitter->Name(r1.reg, R(r2.reg), 0x0b); \
1088 ExpectDisassembly(#Name " " + r1.name + ", " + r2.name + ", 0x0b"); \
1089 } \
1090 emitter->Name(r1.reg, MatR(R12), 0x0b); \
1091 ExpectDisassembly(#Name " " + r1.name + ", " MemBits " ptr ds:[r12], 0x0b"); \
1092 } \
1093 }
1094
1095 TWO_OP_PLUS_IMM_SSE_TEST(BLENDPS, "dqword")
1096 TWO_OP_PLUS_IMM_SSE_TEST(BLENDPD, "dqword")
1097
1098 // for VEX GPR instructions that take the form op reg, r/m, reg
1099 #define VEX_RMR_TEST(Name) \
1100 TEST_F(x64EmitterTest, Name) \
1101 { \
1102 struct \
1103 { \
1104 int bits; \
1105 std::vector<NamedReg> regs; \
1106 std::string out_name; \
1107 std::string size; \
1108 } regsets[] = { \
1109 {32, reg32names, "eax", "dword"}, \
1110 {64, reg64names, "rax", "qword"}, \
1111 }; \
1112 for (const auto& regset : regsets) \
1113 for (const auto& r : regset.regs) \
1114 { \
1115 emitter->Name(regset.bits, r.reg, R(RAX), RAX); \
1116 emitter->Name(regset.bits, RAX, R(r.reg), RAX); \
1117 emitter->Name(regset.bits, RAX, MatR(R12), r.reg); \
1118 ExpectDisassembly(#Name " " + r.name + ", " + regset.out_name + ", " + regset.out_name + \
1119 " " #Name " " + regset.out_name + ", " + r.name + ", " + \
1120 regset.out_name + " " #Name " " + regset.out_name + ", " + regset.size + \
1121 " ptr ds:[r12], " + r.name + " "); \
1122 } \
1123 }
1124
1125 VEX_RMR_TEST(SHRX)
1126 VEX_RMR_TEST(SARX)
1127 VEX_RMR_TEST(SHLX)
1128 VEX_RMR_TEST(BEXTR)
1129 VEX_RMR_TEST(BZHI)
1130
1131 // for VEX GPR instructions that take the form op reg, reg, r/m
1132 #define VEX_RRM_TEST(Name) \
1133 TEST_F(x64EmitterTest, Name) \
1134 { \
1135 struct \
1136 { \
1137 int bits; \
1138 std::vector<NamedReg> regs; \
1139 std::string out_name; \
1140 std::string size; \
1141 } regsets[] = { \
1142 {32, reg32names, "eax", "dword"}, \
1143 {64, reg64names, "rax", "qword"}, \
1144 }; \
1145 for (const auto& regset : regsets) \
1146 for (const auto& r : regset.regs) \
1147 { \
1148 emitter->Name(regset.bits, r.reg, RAX, R(RAX)); \
1149 emitter->Name(regset.bits, RAX, RAX, R(r.reg)); \
1150 emitter->Name(regset.bits, RAX, r.reg, MatR(R12)); \
1151 ExpectDisassembly(#Name " " + r.name + ", " + regset.out_name + ", " + regset.out_name + \
1152 " " #Name " " + regset.out_name + ", " + regset.out_name + ", " + \
1153 r.name + " " #Name " " + regset.out_name + ", " + r.name + ", " + \
1154 regset.size + " ptr ds:[r12] "); \
1155 } \
1156 }
1157
1158 VEX_RRM_TEST(PEXT)
1159 VEX_RRM_TEST(PDEP)
1160 VEX_RRM_TEST(MULX)
1161 VEX_RRM_TEST(ANDN)
1162
1163 // for VEX GPR instructions that take the form op reg, r/m
1164 #define VEX_RM_TEST(Name) \
1165 TEST_F(x64EmitterTest, Name) \
1166 { \
1167 struct \
1168 { \
1169 int bits; \
1170 std::vector<NamedReg> regs; \
1171 std::string out_name; \
1172 std::string size; \
1173 } regsets[] = { \
1174 {32, reg32names, "eax", "dword"}, \
1175 {64, reg64names, "rax", "qword"}, \
1176 }; \
1177 for (const auto& regset : regsets) \
1178 for (const auto& r : regset.regs) \
1179 { \
1180 emitter->Name(regset.bits, r.reg, R(RAX)); \
1181 emitter->Name(regset.bits, RAX, R(r.reg)); \
1182 emitter->Name(regset.bits, r.reg, MatR(R12)); \
1183 ExpectDisassembly(#Name " " + r.name + ", " + regset.out_name + " " #Name " " + \
1184 regset.out_name + ", " + r.name + " " #Name " " + r.name + ", " + \
1185 regset.size + " ptr ds:[r12] "); \
1186 } \
1187 }
1188
1189 VEX_RM_TEST(BLSR)
1190 VEX_RM_TEST(BLSMSK)
1191 VEX_RM_TEST(BLSI)
1192
1193 // for VEX GPR instructions that take the form op reg, r/m, imm
1194 #define VEX_RMI_TEST(Name) \
1195 TEST_F(x64EmitterTest, Name) \
1196 { \
1197 struct \
1198 { \
1199 int bits; \
1200 std::vector<NamedReg> regs; \
1201 std::string out_name; \
1202 std::string size; \
1203 } regsets[] = { \
1204 {32, reg32names, "eax", "dword"}, \
1205 {64, reg64names, "rax", "qword"}, \
1206 }; \
1207 for (const auto& regset : regsets) \
1208 for (const auto& r : regset.regs) \
1209 { \
1210 emitter->Name(regset.bits, r.reg, R(RAX), 4); \
1211 emitter->Name(regset.bits, RAX, R(r.reg), 4); \
1212 emitter->Name(regset.bits, r.reg, MatR(R12), 4); \
1213 ExpectDisassembly(#Name " " + r.name + ", " + regset.out_name + ", 0x04 " #Name " " + \
1214 regset.out_name + ", " + r.name + ", 0x04 " #Name " " + r.name + ", " + \
1215 regset.size + " ptr ds:[r12], 0x04 "); \
1216 } \
1217 }
1218
1219 VEX_RMI_TEST(RORX)
1220
1221 // for AVX instructions that take the form op reg, reg, r/m
1222 #define AVX_RRM_TEST(Name, sizename) \
1223 TEST_F(x64EmitterTest, Name) \
1224 { \
1225 struct \
1226 { \
1227 int bits; \
1228 std::vector<NamedReg> regs; \
1229 std::string out_name; \
1230 std::string size; \
1231 } regsets[] = { \
1232 {64, xmmnames, "xmm0", sizename}, \
1233 }; \
1234 for (const auto& regset : regsets) \
1235 for (const auto& r : regset.regs) \
1236 { \
1237 emitter->Name(r.reg, XMM0, R(XMM0)); \
1238 emitter->Name(XMM0, XMM0, R(r.reg)); \
1239 emitter->Name(XMM0, r.reg, MatR(R12)); \
1240 ExpectDisassembly(#Name " " + r.name + ", " + regset.out_name + ", " + regset.out_name + \
1241 " " #Name " " + regset.out_name + ", " + regset.out_name + ", " + \
1242 r.name + " " #Name " " + regset.out_name + ", " + r.name + ", " + \
1243 regset.size + " ptr ds:[r12] "); \
1244 } \
1245 }
1246
1247 AVX_RRM_TEST(VADDSS, "dword")
1248 AVX_RRM_TEST(VSUBSS, "dword")
1249 AVX_RRM_TEST(VMULSS, "dword")
1250 AVX_RRM_TEST(VDIVSS, "dword")
1251 AVX_RRM_TEST(VADDPS, "dqword")
1252 AVX_RRM_TEST(VSUBPS, "dqword")
1253 AVX_RRM_TEST(VMULPS, "dqword")
1254 AVX_RRM_TEST(VDIVPS, "dqword")
1255 AVX_RRM_TEST(VADDSD, "qword")
1256 AVX_RRM_TEST(VSUBSD, "qword")
1257 AVX_RRM_TEST(VMULSD, "qword")
1258 AVX_RRM_TEST(VDIVSD, "qword")
1259 AVX_RRM_TEST(VADDPD, "dqword")
1260 AVX_RRM_TEST(VSUBPD, "dqword")
1261 AVX_RRM_TEST(VMULPD, "dqword")
1262 AVX_RRM_TEST(VDIVPD, "dqword")
1263 AVX_RRM_TEST(VSQRTSD, "qword")
1264 AVX_RRM_TEST(VUNPCKLPS, "dqword")
1265 AVX_RRM_TEST(VUNPCKLPD, "dqword")
1266 AVX_RRM_TEST(VUNPCKHPD, "dqword")
1267 AVX_RRM_TEST(VANDPS, "dqword")
1268 AVX_RRM_TEST(VANDPD, "dqword")
1269 AVX_RRM_TEST(VANDNPS, "dqword")
1270 AVX_RRM_TEST(VANDNPD, "dqword")
1271 AVX_RRM_TEST(VORPS, "dqword")
1272 AVX_RRM_TEST(VORPD, "dqword")
1273 AVX_RRM_TEST(VXORPS, "dqword")
1274 AVX_RRM_TEST(VXORPD, "dqword")
1275 AVX_RRM_TEST(VPAND, "dqword")
1276 AVX_RRM_TEST(VPANDN, "dqword")
1277 AVX_RRM_TEST(VPOR, "dqword")
1278 AVX_RRM_TEST(VPXOR, "dqword")
1279
1280 #define FMA3_TEST(Name, P, packed) \
1281 AVX_RRM_TEST(Name##132##P##S, packed ? "dqword" : "dword") \
1282 AVX_RRM_TEST(Name##213##P##S, packed ? "dqword" : "dword") \
1283 AVX_RRM_TEST(Name##231##P##S, packed ? "dqword" : "dword") \
1284 AVX_RRM_TEST(Name##132##P##D, packed ? "dqword" : "qword") \
1285 AVX_RRM_TEST(Name##213##P##D, packed ? "dqword" : "qword") \
1286 AVX_RRM_TEST(Name##231##P##D, packed ? "dqword" : "qword")
1287
1288 FMA3_TEST(VFMADD, P, true)
1289 FMA3_TEST(VFMADD, S, false)
1290 FMA3_TEST(VFMSUB, P, true)
1291 FMA3_TEST(VFMSUB, S, false)
1292 FMA3_TEST(VFNMADD, P, true)
1293 FMA3_TEST(VFNMADD, S, false)
1294 FMA3_TEST(VFNMSUB, P, true)
1295 FMA3_TEST(VFNMSUB, S, false)
1296 FMA3_TEST(VFMADDSUB, P, true)
1297 FMA3_TEST(VFMSUBADD, P, true)
1298
1299 #define AVX_RRMI_TEST(Name, MemBits) \
1300 TEST_F(x64EmitterTest, Name) \
1301 { \
1302 for (const auto& r1 : xmmnames) \
1303 { \
1304 for (const auto& r2 : xmmnames) \
1305 { \
1306 for (const auto& r3 : xmmnames) \
1307 { \
1308 emitter->Name(r1.reg, r2.reg, R(r3.reg), 0x0b); \
1309 ExpectDisassembly(#Name " " + r1.name + ", " + r2.name + ", " + r3.name + ", 0x0b"); \
1310 } \
1311 emitter->Name(r1.reg, r2.reg, MatR(R12), 0x0b); \
1312 ExpectDisassembly(#Name " " + r1.name + ", " + r2.name + \
1313 ", " MemBits " ptr ds:[r12], 0x0b"); \
1314 } \
1315 } \
1316 }
1317
1318 AVX_RRMI_TEST(VCMPPD, "dqword")
1319 AVX_RRMI_TEST(VSHUFPS, "dqword")
1320 AVX_RRMI_TEST(VSHUFPD, "dqword")
1321 AVX_RRMI_TEST(VBLENDPS, "dqword")
1322 AVX_RRMI_TEST(VBLENDPD, "dqword")
1323
1324 // for VEX instructions that take the form op reg, reg, r/m, reg OR reg, reg, reg, r/m
1325 #define VEX_RRMR_RRRM_TEST(Name, sizename) \
1326 TEST_F(x64EmitterTest, Name) \
1327 { \
1328 struct \
1329 { \
1330 int bits; \
1331 std::vector<NamedReg> regs; \
1332 std::string out_name; \
1333 std::string size; \
1334 } regsets[] = { \
1335 {64, xmmnames, "xmm0", sizename}, \
1336 }; \
1337 for (const auto& regset : regsets) \
1338 for (const auto& r : regset.regs) \
1339 { \
1340 emitter->Name(r.reg, XMM0, R(XMM0), r.reg); \
1341 emitter->Name(XMM0, XMM0, r.reg, MatR(R12)); \
1342 emitter->Name(XMM0, r.reg, MatR(R12), XMM0); \
1343 ExpectDisassembly(#Name " " + r.name + ", " + regset.out_name + ", " + regset.out_name + \
1344 ", " + r.name + " " #Name " " + regset.out_name + ", " + \
1345 regset.out_name + ", " + r.name + ", " + regset.size + \
1346 " ptr ds:[r12] " #Name " " + regset.out_name + ", " + r.name + ", " + \
1347 regset.size + " ptr ds:[r12], " + regset.out_name); \
1348 } \
1349 }
1350
1351 #define FMA4_TEST(Name, P, packed) \
1352 VEX_RRMR_RRRM_TEST(Name##P##S, packed ? "dqword" : "dword") \
1353 VEX_RRMR_RRRM_TEST(Name##P##D, packed ? "dqword" : "qword")
1354
1355 FMA4_TEST(VFMADD, P, true)
1356 FMA4_TEST(VFMADD, S, false)
1357 FMA4_TEST(VFMSUB, P, true)
1358 FMA4_TEST(VFMSUB, S, false)
1359 FMA4_TEST(VFNMADD, P, true)
1360 FMA4_TEST(VFNMADD, S, false)
1361 FMA4_TEST(VFNMSUB, P, true)
1362 FMA4_TEST(VFNMSUB, S, false)
1363 FMA4_TEST(VFMADDSUB, P, true)
1364 FMA4_TEST(VFMSUBADD, P, true)
1365
1366 } // namespace Gen
1367
1368 #ifdef _MSC_VER
1369 #pragma optimize("", on)
1370 #endif