Debian Codegraph @

Source/Core/Common/x64Emitter.h

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 // WARNING - THIS LIBRARY IS NOT THREAD SAFE!!!
5
6 #pragma once
7
8 #include <cstddef>
9 #include <cstring>
10 #include <functional>
11 #include <tuple>
12 #include <type_traits>
13
14 #include "Common/Assert.h"
15 #include "Common/BitSet.h"
16 #include "Common/CodeBlock.h"
17 #include "Common/CommonTypes.h"
18 #include "Common/x64ABI.h"
19
20 namespace Gen
21 {
22 enum CCFlags
23 {
24 CC_O = 0,
25 CC_NO = 1,
26 CC_B = 2,
27 CC_C = 2,
28 CC_NAE = 2,
29 CC_NB = 3,
30 CC_NC = 3,
31 CC_AE = 3,
32 CC_Z = 4,
33 CC_E = 4,
34 CC_NZ = 5,
35 CC_NE = 5,
36 CC_BE = 6,
37 CC_NA = 6,
38 CC_NBE = 7,
39 CC_A = 7,
40 CC_S = 8,
41 CC_NS = 9,
42 CC_P = 0xA,
43 CC_PE = 0xA,
44 CC_NP = 0xB,
45 CC_PO = 0xB,
46 CC_L = 0xC,
47 CC_NGE = 0xC,
48 CC_NL = 0xD,
49 CC_GE = 0xD,
50 CC_LE = 0xE,
51 CC_NG = 0xE,
52 CC_NLE = 0xF,
53 CC_G = 0xF
54 };
55
56 enum
57 {
58 NUMGPRs = 16,
59 NUMXMMs = 16,
60 };
61
62 enum
63 {
64 SCALE_NONE = 0,
65 SCALE_1 = 1,
66 SCALE_2 = 2,
67 SCALE_4 = 4,
68 SCALE_8 = 8,
69 SCALE_ATREG = 16,
70 // SCALE_NOBASE_1 is not supported and can be replaced with SCALE_ATREG
71 SCALE_NOBASE_2 = 34,
72 SCALE_NOBASE_4 = 36,
73 SCALE_NOBASE_8 = 40,
74 SCALE_RIP = 0xFF,
75 SCALE_IMM8 = 0xF0,
76 SCALE_IMM16 = 0xF1,
77 SCALE_IMM32 = 0xF2,
78 SCALE_IMM64 = 0xF3,
79 };
80
81 enum SSECompare
82 {
83 CMP_EQ = 0,
84 CMP_LT = 1,
85 CMP_LE = 2,
86 CMP_UNORD = 3,
87 CMP_NEQ = 4,
88 CMP_NLT = 5,
89 CMP_NLE = 6,
90 CMP_ORD = 7,
91 };
92
93 class XEmitter;
94 enum class FloatOp;
95 enum class NormalOp;
96
97 // Information about a generated MOV op
98 struct MovInfo final
99 {
100 u8* address;
101 bool nonAtomicSwapStore;
102 // valid iff nonAtomicSwapStore is true
103 X64Reg nonAtomicSwapStoreSrc;
104 };
105
106 // RIP addressing does not benefit from micro op fusion on Core arch
107 struct OpArg
108 {
109 // For accessing offset and operandReg.
110 // This also allows us to keep the op writing functions private.
111 friend class XEmitter;
112
113 // dummy op arg, used for storage
114 constexpr OpArg() = default;
115 constexpr OpArg(u64 offset_, int scale_, X64Reg rm_reg = RAX, X64Reg scaled_reg = RAX)
116 : offset{offset_}, offsetOrBaseReg{static_cast<u16>(rm_reg)},
117 indexReg{static_cast<u16>(scaled_reg)}, scale{static_cast<u8>(scale_)}
118 {
119 }
120 constexpr bool operator==(const OpArg& b) const
121 {
122 return std::tie(scale, offsetOrBaseReg, indexReg, offset, operandReg) ==
123 std::tie(b.scale, b.offsetOrBaseReg, b.indexReg, b.offset, b.operandReg);
124 }
125 u64 Imm64() const
126 {
127 DEBUG_ASSERT(scale == SCALE_IMM64);
128 return (u64)offset;
129 }
130 u32 Imm32() const
131 {
132 DEBUG_ASSERT(scale == SCALE_IMM32);
133 return (u32)offset;
134 }
135 u16 Imm16() const
136 {
137 DEBUG_ASSERT(scale == SCALE_IMM16);
138 return (u16)offset;
139 }
140 u8 Imm8() const
141 {
142 DEBUG_ASSERT(scale == SCALE_IMM8);
143 return (u8)offset;
144 }
145
146 s64 SImm64() const
147 {
148 DEBUG_ASSERT(scale == SCALE_IMM64);
149 return (s64)offset;
150 }
151 s32 SImm32() const
152 {
153 DEBUG_ASSERT(scale == SCALE_IMM32);
154 return (s32)offset;
155 }
156 s16 SImm16() const
157 {
158 DEBUG_ASSERT(scale == SCALE_IMM16);
159 return (s16)offset;
160 }
161 s8 SImm8() const
162 {
163 DEBUG_ASSERT(scale == SCALE_IMM8);
164 return (s8)offset;
165 }
166
167 OpArg AsImm64() const
168 {
169 DEBUG_ASSERT(IsImm());
170 return OpArg((u64)offset, SCALE_IMM64);
171 }
172 OpArg AsImm32() const
173 {
174 DEBUG_ASSERT(IsImm());
175 return OpArg((u32)offset, SCALE_IMM32);
176 }
177 OpArg AsImm16() const
178 {
179 DEBUG_ASSERT(IsImm());
180 return OpArg((u16)offset, SCALE_IMM16);
181 }
182 OpArg AsImm8() const
183 {
184 DEBUG_ASSERT(IsImm());
185 return OpArg((u8)offset, SCALE_IMM8);
186 }
187
188 constexpr bool IsImm() const
189 {
190 return scale == SCALE_IMM8 || scale == SCALE_IMM16 || scale == SCALE_IMM32 ||
191 scale == SCALE_IMM64;
192 }
193 constexpr bool IsSimpleReg() const { return scale == SCALE_NONE; }
194 constexpr bool IsSimpleReg(X64Reg reg) const { return IsSimpleReg() && GetSimpleReg() == reg; }
195 constexpr bool IsZero() const { return IsImm() && offset == 0; }
196 constexpr int GetImmBits() const
197 {
198 switch (scale)
199 {
200 case SCALE_IMM8:
201 return 8;
202 case SCALE_IMM16:
203 return 16;
204 case SCALE_IMM32:
205 return 32;
206 case SCALE_IMM64:
207 return 64;
208 default:
209 return -1;
210 }
211 }
212
213 constexpr X64Reg GetSimpleReg() const
214 {
215 if (scale == SCALE_NONE)
216 return static_cast<X64Reg>(offsetOrBaseReg);
217
218 return INVALID_REG;
219 }
220
221 void AddMemOffset(int val)
222 {
223 DEBUG_ASSERT_MSG(DYNA_REC, scale == SCALE_RIP || (scale <= SCALE_ATREG && scale > SCALE_NONE),
224 "Tried to increment an OpArg which doesn't have an offset");
225 offset += val;
226 }
227
228 private:
229 void WriteREX(XEmitter* emit, int opBits, int bits, int customOp = -1) const;
230 void WriteVEX(XEmitter* emit, X64Reg regOp1, X64Reg regOp2, int L, int pp, int mmmmm,
231 int W = 0) const;
232 void WriteRest(XEmitter* emit, int extraBytes = 0, X64Reg operandReg = INVALID_REG,
233 bool warn_64bit_offset = true) const;
234 void WriteSingleByteOp(XEmitter* emit, u8 op, X64Reg operandReg, int bits);
235 void WriteNormalOp(XEmitter* emit, bool toRM, NormalOp op, const OpArg& operand, int bits) const;
236
237 u64 offset = 0; // Also used to store immediates.
238 u16 offsetOrBaseReg = 0;
239 u16 indexReg = 0;
240 u16 operandReg = 0;
241 u8 scale = 0;
242 };
243
244 template <typename T>
245 inline OpArg M(const T* ptr)
246 {
247 return OpArg((u64)(const void*)ptr, (int)SCALE_RIP);
248 }
249 constexpr OpArg R(X64Reg value)
250 {
251 return OpArg(0, SCALE_NONE, value);
252 }
253 constexpr OpArg MatR(X64Reg value)
254 {
255 return OpArg(0, SCALE_ATREG, value);
256 }
257
258 constexpr OpArg MDisp(X64Reg value, int offset)
259 {
260 return OpArg(static_cast<u32>(offset), SCALE_ATREG, value);
261 }
262
263 constexpr OpArg MComplex(X64Reg base, X64Reg scaled, int scale, int offset)
264 {
265 return OpArg(offset, scale, base, scaled);
266 }
267
268 constexpr OpArg MScaled(X64Reg scaled, int scale, int offset)
269 {
270 if (scale == SCALE_1)
271 return OpArg(offset, SCALE_ATREG, scaled);
272
273 return OpArg(offset, scale | 0x20, RAX, scaled);
274 }
275
276 constexpr OpArg MRegSum(X64Reg base, X64Reg offset)
277 {
278 return MComplex(base, offset, 1, 0);
279 }
280
281 constexpr OpArg Imm8(u8 imm)
282 {
283 return OpArg(imm, SCALE_IMM8);
284 }
285 constexpr OpArg Imm16(u16 imm)
286 {
287 return OpArg(imm, SCALE_IMM16);
288 } // rarely used
289 constexpr OpArg Imm32(u32 imm)
290 {
291 return OpArg(imm, SCALE_IMM32);
292 }
293 constexpr OpArg Imm64(u64 imm)
294 {
295 return OpArg(imm, SCALE_IMM64);
296 }
297 inline OpArg ImmPtr(const void* imm)
298 {
299 return Imm64(reinterpret_cast<u64>(imm));
300 }
301
302 inline u32 PtrOffset(const void* ptr, const void* base = nullptr)
303 {
304 s64 distance = (s64)ptr - (s64)base;
305 if (distance >= 0x80000000LL || distance < -0x80000000LL)
306 {
307 ASSERT_MSG(DYNA_REC, 0, "pointer offset out of range");
308 return 0;
309 }
310
311 return (u32)distance;
312 }
313
314 struct FixupBranch
315 {
316 enum class Type
317 {
318 Branch8Bit,
319 Branch32Bit
320 };
321
322 u8* ptr;
323 Type type;
324 };
325
326 class XEmitter
327 {
328 friend struct OpArg; // for Write8 etc
329 private:
330 // Pointer to memory where code will be emitted to.
331 u8* code = nullptr;
332
333 // Pointer past the end of the memory region we're allowed to emit to.
334 // Writes that would reach this memory are refused and will set the m_write_failed flag instead.
335 u8* m_code_end = nullptr;
336
337 bool flags_locked = false;
338
339 // Set to true when a write request happens that would write past m_code_end.
340 // Must be cleared with SetCodePtr() afterwards.
341 bool m_write_failed = false;
342
343 void CheckFlags();
344
345 void Rex(int w, int r, int x, int b);
346 void WriteModRM(int mod, int reg, int rm);
347 void WriteSIB(int scale, int index, int base);
348 void WriteSimple1Byte(int bits, u8 byte, X64Reg reg);
349 void WriteSimple2Byte(int bits, u8 byte1, u8 byte2, X64Reg reg);
350 void WriteMulDivType(int bits, OpArg src, int ext);
351 void WriteBitSearchType(int bits, X64Reg dest, OpArg src, u8 byte2, bool rep = false);
352 void WriteShift(int bits, OpArg dest, const OpArg& shift, int ext);
353 void WriteBitTest(int bits, const OpArg& dest, const OpArg& index, int ext);
354 void WriteMXCSR(OpArg arg, int ext);
355 void WriteSSEOp(u8 opPrefix, u16 op, X64Reg regOp, OpArg arg, int extrabytes = 0);
356 void WriteSSSE3Op(u8 opPrefix, u16 op, X64Reg regOp, const OpArg& arg, int extrabytes = 0);
357 void WriteSSE41Op(u8 opPrefix, u16 op, X64Reg regOp, const OpArg& arg, int extrabytes = 0);
358 void WriteVEXOp(u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg, int W = 0,
359 int extrabytes = 0);
360 void WriteVEXOp4(u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg,
361 X64Reg regOp3, int W = 0);
362 void WriteAVXOp(u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg, int W = 0,
363 int extrabytes = 0);
364 void WriteAVXOp4(u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg,
365 X64Reg regOp3, int W = 0);
366 void WriteFMA3Op(u8 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg, int W = 0);
367 void WriteFMA4Op(u8 op, X64Reg dest, X64Reg regOp1, X64Reg regOp2, const OpArg& arg, int W = 0);
368 void WriteBMIOp(int size, u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg,
369 int extrabytes = 0);
370 void WriteBMI1Op(int size, u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg,
371 int extrabytes = 0);
372 void WriteBMI2Op(int size, u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg,
373 int extrabytes = 0);
374 void WriteMOVBE(int bits, u8 op, X64Reg regOp, const OpArg& arg);
375 void WriteNormalOp(int bits, NormalOp op, const OpArg& a1, const OpArg& a2);
376
377 void ABI_CalculateFrameSize(BitSet32 mask, size_t rsp_alignment, size_t needed_frame_size,
378 size_t* shadowp, size_t* subtractionp, size_t* xmm_offsetp);
379
380 protected:
381 void Write8(u8 value);
382 void Write16(u16 value);
383 void Write32(u32 value);
384 void Write64(u64 value);
385
386 public:
387 XEmitter() = default;
388 explicit XEmitter(u8* code_ptr, u8* code_end) : code(code_ptr), m_code_end(code_end) {}
389 virtual ~XEmitter() = default;
390 void SetCodePtr(u8* ptr, u8* end, bool write_failed = false);
391 void ReserveCodeSpace(int bytes);
392 u8* AlignCodeTo(size_t alignment);
393 u8* AlignCode4();
394 u8* AlignCode16();
395 u8* AlignCodePage();
396 const u8* GetCodePtr() const { return code; }
397 u8* GetWritableCodePtr() { return code; }
398 const u8* GetCodeEnd() const { return m_code_end; }
399 u8* GetWritableCodeEnd() { return m_code_end; }
400
401 void LockFlags() { flags_locked = true; }
402 void UnlockFlags() { flags_locked = false; }
403
404 // Should be checked after a block of code has been generated to see if the code has been
405 // successfully written to memory. Do not call the generated code when this returns true!
406 bool HasWriteFailed() const { return m_write_failed; }
407
408 // Looking for one of these? It's BANNED!! Some instructions are slow on modern CPU
409 // INC, DEC, LOOP, LOOPNE, LOOPE, ENTER, LEAVE, XCHG, XLAT, REP MOVSB/MOVSD, REP SCASD + other
410 // string instr.,
411 // INC and DEC are slow on Intel Core, but not on AMD. They create a
412 // false flag dependency because they only update a subset of the flags.
413 // XCHG is SLOW and should be avoided.
414
415 // Debug breakpoint
416 void INT3();
417
418 // Do nothing
419 void NOP(size_t count = 1);
420
421 // Save energy in wait-loops on P4 only. Probably not too useful.
422 void PAUSE();
423
424 // Flag control
425 void STC();
426 void CLC();
427 void CMC();
428
429 // These two can not be executed in 64-bit mode on early Intel 64-bit CPU:s, only on Core2 and
430 // AMD!
431 void LAHF(); // 3 cycle vector path
432 void SAHF(); // direct path fast
433
434 // Stack control
435 void PUSH(X64Reg reg);
436 void POP(X64Reg reg);
437 void PUSH(int bits, const OpArg& reg);
438 void POP(int bits, const OpArg& reg);
439 void PUSHF();
440 void POPF();
441
442 enum class Jump
443 {
444 Short,
445 Near,
446 };
447 static const int SHORT_JMP_LEN = 2;
448 static const int NEAR_JMP_LEN = 5;
449
450 // Flow control
451 void RET();
452 void RET_FAST();
453 void UD2();
454 [[nodiscard]] FixupBranch J(Jump jump = Jump::Short);
455
456 void JMP(const u8* addr, bool force_near_padding = false);
457 void JMPptr(const OpArg& arg);
458 void JMPself(); // infinite loop!
459 #ifdef CALL
460 #undef CALL
461 #endif
462 void CALL(const void* fnptr);
463 [[nodiscard]] FixupBranch CALL();
464 void CALLptr(OpArg arg);
465
466 [[nodiscard]] FixupBranch J_CC(CCFlags conditionCode, Jump jump = Jump::Short);
467 void J_CC(CCFlags conditionCode, const u8* addr);
468
469 void SetJumpTarget(const FixupBranch& branch);
470
471 void SETcc(CCFlags flag, OpArg dest);
472 // Note: CMOV brings small if any benefit on current CPUs.
473 void CMOVcc(int bits, X64Reg dest, OpArg src, CCFlags flag);
474
475 // Fences
476 void LFENCE();
477 void MFENCE();
478 void SFENCE();
479
480 // Bit scan
481 void BSF(int bits, X64Reg dest, const OpArg& src); // Bottom bit to top bit
482 void BSR(int bits, X64Reg dest, const OpArg& src); // Top bit to bottom bit
483
484 // Cache control
485 enum class PrefetchLevel : u8
486 {
487 NTA = 0, // Non-temporal (data used once and only once)
488 T0 = 1, // All cache levels
489 T1 = 2, // Levels 2+ (aliased to T0 on AMD)
490 T2 = 3, // Levels 3+ (aliased to T0 on AMD)
491 };
492 void PREFETCH(PrefetchLevel level, OpArg arg);
493 void MOVNTI(int bits, const OpArg& dest, X64Reg src);
494 void MOVNTDQ(const OpArg& arg, X64Reg regOp);
495 void MOVNTPS(const OpArg& arg, X64Reg regOp);
496 void MOVNTPD(const OpArg& arg, X64Reg regOp);
497
498 // Multiplication / division
499 void MUL(int bits, const OpArg& src); // UNSIGNED
500 void IMUL(int bits, const OpArg& src); // SIGNED
501 void IMUL(int bits, X64Reg regOp, const OpArg& src);
502 void IMUL(int bits, X64Reg regOp, const OpArg& src, const OpArg& imm);
503 void DIV(int bits, const OpArg& src);
504 void IDIV(int bits, const OpArg& src);
505
506 // Shift
507 void ROL(int bits, const OpArg& dest, const OpArg& shift);
508 void ROR(int bits, const OpArg& dest, const OpArg& shift);
509 void RCL(int bits, const OpArg& dest, const OpArg& shift);
510 void RCR(int bits, const OpArg& dest, const OpArg& shift);
511 void SHL(int bits, const OpArg& dest, const OpArg& shift);
512 void SHR(int bits, const OpArg& dest, const OpArg& shift);
513 void SAR(int bits, const OpArg& dest, const OpArg& shift);
514
515 // Bit Test
516 void BT(int bits, const OpArg& dest, const OpArg& index);
517 void BTS(int bits, const OpArg& dest, const OpArg& index);
518 void BTR(int bits, const OpArg& dest, const OpArg& index);
519 void BTC(int bits, const OpArg& dest, const OpArg& index);
520
521 // Double-Precision Shift
522 void SHRD(int bits, const OpArg& dest, const OpArg& src, const OpArg& shift);
523 void SHLD(int bits, const OpArg& dest, const OpArg& src, const OpArg& shift);
524
525 // Extend EAX into EDX in various ways
526 void CWD(int bits = 16);
527 inline void CDQ() { CWD(32); }
528 inline void CQO() { CWD(64); }
529 void CBW(int bits = 8);
530 inline void CWDE() { CBW(16); }
531 inline void CDQE() { CBW(32); }
532 // Load effective address
533 void LEA(int bits, X64Reg dest, OpArg src);
534
535 // Integer arithmetic
536 void NEG(int bits, const OpArg& src);
537 void ADD(int bits, const OpArg& a1, const OpArg& a2);
538 void ADC(int bits, const OpArg& a1, const OpArg& a2);
539 void SUB(int bits, const OpArg& a1, const OpArg& a2);
540 void SBB(int bits, const OpArg& a1, const OpArg& a2);
541 void AND(int bits, const OpArg& a1, const OpArg& a2);
542 void CMP(int bits, const OpArg& a1, const OpArg& a2);
543
544 // Bit operations
545 void NOT(int bits, const OpArg& src);
546 void OR(int bits, const OpArg& a1, const OpArg& a2);
547 void XOR(int bits, const OpArg& a1, const OpArg& a2);
548 void MOV(int bits, const OpArg& a1, const OpArg& a2);
549 void TEST(int bits, const OpArg& a1, const OpArg& a2);
550
551 void CMP_or_TEST(int bits, const OpArg& a1, const OpArg& a2);
552 void MOV_sum(int bits, X64Reg dest, const OpArg& a1, const OpArg& a2);
553
554 // Are these useful at all? Consider removing.
555 void XCHG(int bits, const OpArg& a1, const OpArg& a2);
556 void XCHG_AHAL();
557
558 // Byte swapping (32 and 64-bit only).
559 void BSWAP(int bits, X64Reg reg);
560
561 // Sign/zero extension
562 void MOVSX(int dbits, int sbits, X64Reg dest,
563 OpArg src); // automatically uses MOVSXD if necessary
564 void MOVZX(int dbits, int sbits, X64Reg dest, OpArg src);
565
566 // Available only on Atom or >= Haswell so far. Test with cpu_info.bMOVBE.
567 void MOVBE(int bits, X64Reg dest, const OpArg& src);
568 void MOVBE(int bits, const OpArg& dest, X64Reg src);
569 void LoadAndSwap(int size, X64Reg dst, const OpArg& src, bool sign_extend = false,
570 MovInfo* info = nullptr);
571 void SwapAndStore(int size, const OpArg& dst, X64Reg src, MovInfo* info = nullptr);
572
573 // Available only on AMD >= Phenom or Intel >= Haswell
574 void LZCNT(int bits, X64Reg dest, const OpArg& src);
575 // Note: this one is actually part of BMI1
576 void TZCNT(int bits, X64Reg dest, const OpArg& src);
577
578 // WARNING - These two take 11-13 cycles and are VectorPath! (AMD64)
579 void STMXCSR(const OpArg& memloc);
580 void LDMXCSR(const OpArg& memloc);
581
582 // Prefixes
583 void LOCK();
584 void REP();
585 void REPNE();
586 void FSOverride();
587 void GSOverride();
588
589 // SSE/SSE2: Floating point arithmetic
590 void ADDSS(X64Reg regOp, const OpArg& arg);
591 void ADDSD(X64Reg regOp, const OpArg& arg);
592 void SUBSS(X64Reg regOp, const OpArg& arg);
593 void SUBSD(X64Reg regOp, const OpArg& arg);
594 void MULSS(X64Reg regOp, const OpArg& arg);
595 void MULSD(X64Reg regOp, const OpArg& arg);
596 void DIVSS(X64Reg regOp, const OpArg& arg);
597 void DIVSD(X64Reg regOp, const OpArg& arg);
598 void MINSS(X64Reg regOp, const OpArg& arg);
599 void MINSD(X64Reg regOp, const OpArg& arg);
600 void MAXSS(X64Reg regOp, const OpArg& arg);
601 void MAXSD(X64Reg regOp, const OpArg& arg);
602 void SQRTSS(X64Reg regOp, const OpArg& arg);
603 void SQRTSD(X64Reg regOp, const OpArg& arg);
604 void RCPSS(X64Reg regOp, const OpArg& arg);
605 void RSQRTSS(X64Reg regOp, const OpArg& arg);
606
607 // SSE/SSE2: Floating point bitwise (yes)
608 void CMPSS(X64Reg regOp, const OpArg& arg, u8 compare);
609 void CMPSD(X64Reg regOp, const OpArg& arg, u8 compare);
610
611 // SSE/SSE2: Floating point packed arithmetic (x4 for float, x2 for double)
612 void ADDPS(X64Reg regOp, const OpArg& arg);
613 void ADDPD(X64Reg regOp, const OpArg& arg);
614 void SUBPS(X64Reg regOp, const OpArg& arg);
615 void SUBPD(X64Reg regOp, const OpArg& arg);
616 void CMPPS(X64Reg regOp, const OpArg& arg, u8 compare);
617 void CMPPD(X64Reg regOp, const OpArg& arg, u8 compare);
618 void MULPS(X64Reg regOp, const OpArg& arg);
619 void MULPD(X64Reg regOp, const OpArg& arg);
620 void DIVPS(X64Reg regOp, const OpArg& arg);
621 void DIVPD(X64Reg regOp, const OpArg& arg);
622 void MINPS(X64Reg regOp, const OpArg& arg);
623 void MINPD(X64Reg regOp, const OpArg& arg);
624 void MAXPS(X64Reg regOp, const OpArg& arg);
625 void MAXPD(X64Reg regOp, const OpArg& arg);
626 void SQRTPS(X64Reg regOp, const OpArg& arg);
627 void SQRTPD(X64Reg regOp, const OpArg& arg);
628 void RCPPS(X64Reg regOp, const OpArg& arg);
629 void RSQRTPS(X64Reg regOp, const OpArg& arg);
630
631 // SSE/SSE2: Floating point packed bitwise (x4 for float, x2 for double)
632 void ANDPS(X64Reg regOp, const OpArg& arg);
633 void ANDPD(X64Reg regOp, const OpArg& arg);
634 void ANDNPS(X64Reg regOp, const OpArg& arg);
635 void ANDNPD(X64Reg regOp, const OpArg& arg);
636 void ORPS(X64Reg regOp, const OpArg& arg);
637 void ORPD(X64Reg regOp, const OpArg& arg);
638 void XORPS(X64Reg regOp, const OpArg& arg);
639 void XORPD(X64Reg regOp, const OpArg& arg);
640
641 // SSE/SSE2: Shuffle components. These are tricky - see Intel documentation.
642 void SHUFPS(X64Reg regOp, const OpArg& arg, u8 shuffle);
643 void SHUFPD(X64Reg regOp, const OpArg& arg, u8 shuffle);
644
645 // SSE3
646 void MOVSLDUP(X64Reg regOp, const OpArg& arg);
647 void MOVSHDUP(X64Reg regOp, const OpArg& arg);
648 void MOVDDUP(X64Reg regOp, const OpArg& arg);
649
650 // SSE/SSE2: Useful alternative to shuffle in some cases.
651 void UNPCKLPS(X64Reg dest, const OpArg& src);
652 void UNPCKHPS(X64Reg dest, const OpArg& src);
653 void UNPCKLPD(X64Reg dest, const OpArg& src);
654 void UNPCKHPD(X64Reg dest, const OpArg& src);
655
656 // SSE/SSE2: Compares.
657 void COMISS(X64Reg regOp, const OpArg& arg);
658 void COMISD(X64Reg regOp, const OpArg& arg);
659 void UCOMISS(X64Reg regOp, const OpArg& arg);
660 void UCOMISD(X64Reg regOp, const OpArg& arg);
661
662 // SSE/SSE2: Moves. Use the right data type for your data, in most cases.
663 void MOVAPS(X64Reg regOp, const OpArg& arg);
664 void MOVAPD(X64Reg regOp, const OpArg& arg);
665 void MOVAPS(const OpArg& arg, X64Reg regOp);
666 void MOVAPD(const OpArg& arg, X64Reg regOp);
667
668 void MOVUPS(X64Reg regOp, const OpArg& arg);
669 void MOVUPD(X64Reg regOp, const OpArg& arg);
670 void MOVUPS(const OpArg& arg, X64Reg regOp);
671 void MOVUPD(const OpArg& arg, X64Reg regOp);
672
673 void MOVDQA(X64Reg regOp, const OpArg& arg);
674 void MOVDQA(const OpArg& arg, X64Reg regOp);
675 void MOVDQU(X64Reg regOp, const OpArg& arg);
676 void MOVDQU(const OpArg& arg, X64Reg regOp);
677
678 void MOVSS(X64Reg regOp, const OpArg& arg);
679 void MOVSD(X64Reg regOp, const OpArg& arg);
680 void MOVSS(const OpArg& arg, X64Reg regOp);
681 void MOVSD(const OpArg& arg, X64Reg regOp);
682
683 void MOVLPS(X64Reg regOp, const OpArg& arg);
684 void MOVLPD(X64Reg regOp, const OpArg& arg);
685 void MOVLPS(const OpArg& arg, X64Reg regOp);
686 void MOVLPD(const OpArg& arg, X64Reg regOp);
687
688 void MOVHPS(X64Reg regOp, const OpArg& arg);
689 void MOVHPD(X64Reg regOp, const OpArg& arg);
690 void MOVHPS(const OpArg& arg, X64Reg regOp);
691 void MOVHPD(const OpArg& arg, X64Reg regOp);
692
693 void MOVHLPS(X64Reg regOp1, X64Reg regOp2);
694 void MOVLHPS(X64Reg regOp1, X64Reg regOp2);
695
696 // Be careful when using these overloads for reg <--> xmm moves.
697 // The one you cast to OpArg with R(reg) is the x86 reg, the other
698 // one is the xmm reg.
699 // ie: "MOVD_xmm(eax, R(xmm1))" generates incorrect code (movd xmm0, rcx)
700 // use "MOVD_xmm(R(eax), xmm1)" instead.
701 void MOVD_xmm(X64Reg dest, const OpArg& arg);
702 void MOVQ_xmm(X64Reg dest, OpArg arg);
703 void MOVD_xmm(const OpArg& arg, X64Reg src);
704 void MOVQ_xmm(OpArg arg, X64Reg src);
705
706 // SSE/SSE2: Generates a mask from the high bits of the components of the packed register in
707 // question.
708 void MOVMSKPS(X64Reg dest, const OpArg& arg);
709 void MOVMSKPD(X64Reg dest, const OpArg& arg);
710
711 // SSE2: Selective byte store, mask in src register. EDI/RDI specifies store address. This is a
712 // weird one.
713 void MASKMOVDQU(X64Reg dest, X64Reg src);
714 void LDDQU(X64Reg dest, const OpArg& src);
715
716 // SSE/SSE2: Data type conversions.
717 void CVTPS2PD(X64Reg dest, const OpArg& src);
718 void CVTPD2PS(X64Reg dest, const OpArg& src);
719 void CVTSS2SD(X64Reg dest, const OpArg& src);
720 void CVTSI2SS(X64Reg dest, const OpArg& src);
721 void CVTSD2SS(X64Reg dest, const OpArg& src);
722 void CVTSI2SD(X64Reg dest, const OpArg& src);
723 void CVTDQ2PD(X64Reg regOp, const OpArg& arg);
724 void CVTPD2DQ(X64Reg regOp, const OpArg& arg);
725 void CVTDQ2PS(X64Reg regOp, const OpArg& arg);
726 void CVTPS2DQ(X64Reg regOp, const OpArg& arg);
727
728 void CVTTPS2DQ(X64Reg regOp, const OpArg& arg);
729 void CVTTPD2DQ(X64Reg regOp, const OpArg& arg);
730
731 // Destinations are X64 regs (rax, rbx, ...) for these instructions.
732 void CVTSS2SI(X64Reg xregdest, const OpArg& src);
733 void CVTSD2SI(X64Reg xregdest, const OpArg& src);
734 void CVTTSS2SI(X64Reg xregdest, const OpArg& arg);
735 void CVTTSD2SI(X64Reg xregdest, const OpArg& arg);
736
737 // SSE2: Packed integer instructions
738 void PACKSSDW(X64Reg dest, const OpArg& arg);
739 void PACKSSWB(X64Reg dest, const OpArg& arg);
740 void PACKUSDW(X64Reg dest, const OpArg& arg);
741 void PACKUSWB(X64Reg dest, const OpArg& arg);
742
743 void PUNPCKLBW(X64Reg dest, const OpArg& arg);
744 void PUNPCKLWD(X64Reg dest, const OpArg& arg);
745 void PUNPCKLDQ(X64Reg dest, const OpArg& arg);
746 void PUNPCKLQDQ(X64Reg dest, const OpArg& arg);
747
748 void PTEST(X64Reg dest, const OpArg& arg);
749 void PAND(X64Reg dest, const OpArg& arg);
750 void PANDN(X64Reg dest, const OpArg& arg);
751 void PXOR(X64Reg dest, const OpArg& arg);
752 void POR(X64Reg dest, const OpArg& arg);
753
754 void PADDB(X64Reg dest, const OpArg& arg);
755 void PADDW(X64Reg dest, const OpArg& arg);
756 void PADDD(X64Reg dest, const OpArg& arg);
757 void PADDQ(X64Reg dest, const OpArg& arg);
758
759 void PADDSB(X64Reg dest, const OpArg& arg);
760 void PADDSW(X64Reg dest, const OpArg& arg);
761 void PADDUSB(X64Reg dest, const OpArg& arg);
762 void PADDUSW(X64Reg dest, const OpArg& arg);
763
764 void PSUBB(X64Reg dest, const OpArg& arg);
765 void PSUBW(X64Reg dest, const OpArg& arg);
766 void PSUBD(X64Reg dest, const OpArg& arg);
767 void PSUBQ(X64Reg dest, const OpArg& arg);
768
769 void PSUBSB(X64Reg dest, const OpArg& arg);
770 void PSUBSW(X64Reg dest, const OpArg& arg);
771 void PSUBUSB(X64Reg dest, const OpArg& arg);
772 void PSUBUSW(X64Reg dest, const OpArg& arg);
773
774 void PAVGB(X64Reg dest, const OpArg& arg);
775 void PAVGW(X64Reg dest, const OpArg& arg);
776
777 void PCMPEQB(X64Reg dest, const OpArg& arg);
778 void PCMPEQW(X64Reg dest, const OpArg& arg);
779 void PCMPEQD(X64Reg dest, const OpArg& arg);
780
781 void PCMPGTB(X64Reg dest, const OpArg& arg);
782 void PCMPGTW(X64Reg dest, const OpArg& arg);
783 void PCMPGTD(X64Reg dest, const OpArg& arg);
784
785 void PEXTRW(X64Reg dest, const OpArg& arg, u8 subreg);
786 void PINSRW(X64Reg dest, const OpArg& arg, u8 subreg);
787 void PINSRD(X64Reg dest, const OpArg& arg, u8 subreg);
788
789 void PMADDWD(X64Reg dest, const OpArg& arg);
790 void PSADBW(X64Reg dest, const OpArg& arg);
791
792 void PMAXSW(X64Reg dest, const OpArg& arg);
793 void PMAXUB(X64Reg dest, const OpArg& arg);
794 void PMINSW(X64Reg dest, const OpArg& arg);
795 void PMINUB(X64Reg dest, const OpArg& arg);
796
797 void PMOVMSKB(X64Reg dest, const OpArg& arg);
798 void PSHUFD(X64Reg dest, const OpArg& arg, u8 shuffle);
799 void PSHUFB(X64Reg dest, const OpArg& arg);
800
801 void PSHUFLW(X64Reg dest, const OpArg& arg, u8 shuffle);
802 void PSHUFHW(X64Reg dest, const OpArg& arg, u8 shuffle);
803
804 void PSRLW(X64Reg reg, u8 shift);
805 void PSRLD(X64Reg reg, u8 shift);
806 void PSRLQ(X64Reg reg, u8 shift);
807 void PSRLQ(X64Reg reg, const OpArg& arg);
808 void PSRLDQ(X64Reg reg, u8 shift);
809
810 void PSLLW(X64Reg reg, u8 shift);
811 void PSLLD(X64Reg reg, u8 shift);
812 void PSLLQ(X64Reg reg, u8 shift);
813 void PSLLDQ(X64Reg reg, u8 shift);
814
815 void PSRAW(X64Reg reg, u8 shift);
816 void PSRAD(X64Reg reg, u8 shift);
817
818 // SSE4: data type conversions
819 void PMOVSXBW(X64Reg dest, const OpArg& arg);
820 void PMOVSXBD(X64Reg dest, const OpArg& arg);
821 void PMOVSXBQ(X64Reg dest, const OpArg& arg);
822 void PMOVSXWD(X64Reg dest, const OpArg& arg);
823 void PMOVSXWQ(X64Reg dest, const OpArg& arg);
824 void PMOVSXDQ(X64Reg dest, const OpArg& arg);
825 void PMOVZXBW(X64Reg dest, const OpArg& arg);
826 void PMOVZXBD(X64Reg dest, const OpArg& arg);
827 void PMOVZXBQ(X64Reg dest, const OpArg& arg);
828 void PMOVZXWD(X64Reg dest, const OpArg& arg);
829 void PMOVZXWQ(X64Reg dest, const OpArg& arg);
830 void PMOVZXDQ(X64Reg dest, const OpArg& arg);
831
832 // SSE4: blend instructions
833 void PBLENDVB(X64Reg dest, const OpArg& arg);
834 void BLENDVPS(X64Reg dest, const OpArg& arg);
835 void BLENDVPD(X64Reg dest, const OpArg& arg);
836 void BLENDPS(X64Reg dest, const OpArg& arg, u8 blend);
837 void BLENDPD(X64Reg dest, const OpArg& arg, u8 blend);
838
839 // SSE4: compare instructions
840 void PCMPEQQ(X64Reg dest, const OpArg& arg);
841
842 // AVX
843 void VADDSS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
844 void VSUBSS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
845 void VMULSS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
846 void VDIVSS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
847 void VADDPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
848 void VSUBPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
849 void VMULPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
850 void VDIVPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
851 void VADDSD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
852 void VSUBSD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
853 void VMULSD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
854 void VDIVSD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
855 void VADDPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
856 void VSUBPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
857 void VMULPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
858 void VDIVPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
859 void VSQRTSD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
860 void VCMPPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg, u8 compare);
861 void VSHUFPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg, u8 shuffle);
862 void VSHUFPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg, u8 shuffle);
863 void VUNPCKLPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
864 void VUNPCKLPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
865 void VUNPCKHPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
866 void VBLENDVPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg, X64Reg mask);
867 void VBLENDPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg, u8 blend);
868 void VBLENDPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg, u8 blend);
869
870 void VANDPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
871 void VANDPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
872 void VANDNPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
873 void VANDNPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
874 void VORPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
875 void VORPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
876 void VXORPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
877 void VXORPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
878
879 void VPAND(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
880 void VPANDN(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
881 void VPOR(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
882 void VPXOR(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
883
884 void VPSLLQ(X64Reg regOp1, X64Reg regOp2, u8 shift);
885
886 void VMOVAPS(const OpArg& arg, X64Reg regOp);
887
888 void VZEROUPPER();
889
890 // FMA3
891 void VFMADD132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
892 void VFMADD213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
893 void VFMADD231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
894 void VFMADD132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
895 void VFMADD213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
896 void VFMADD231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
897 void VFMADD132SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
898 void VFMADD213SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
899 void VFMADD231SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
900 void VFMADD132SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
901 void VFMADD213SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
902 void VFMADD231SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
903 void VFMSUB132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
904 void VFMSUB213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
905 void VFMSUB231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
906 void VFMSUB132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
907 void VFMSUB213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
908 void VFMSUB231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
909 void VFMSUB132SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
910 void VFMSUB213SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
911 void VFMSUB231SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
912 void VFMSUB132SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
913 void VFMSUB213SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
914 void VFMSUB231SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
915 void VFNMADD132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
916 void VFNMADD213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
917 void VFNMADD231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
918 void VFNMADD132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
919 void VFNMADD213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
920 void VFNMADD231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
921 void VFNMADD132SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
922 void VFNMADD213SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
923 void VFNMADD231SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
924 void VFNMADD132SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
925 void VFNMADD213SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
926 void VFNMADD231SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
927 void VFNMSUB132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
928 void VFNMSUB213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
929 void VFNMSUB231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
930 void VFNMSUB132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
931 void VFNMSUB213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
932 void VFNMSUB231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
933 void VFNMSUB132SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
934 void VFNMSUB213SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
935 void VFNMSUB231SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
936 void VFNMSUB132SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
937 void VFNMSUB213SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
938 void VFNMSUB231SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
939 void VFMADDSUB132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
940 void VFMADDSUB213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
941 void VFMADDSUB231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
942 void VFMADDSUB132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
943 void VFMADDSUB213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
944 void VFMADDSUB231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
945 void VFMSUBADD132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
946 void VFMSUBADD213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
947 void VFMSUBADD231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
948 void VFMSUBADD132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
949 void VFMSUBADD213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
950 void VFMSUBADD231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
951
952 #define FMA4(name) \
953 void name(X64Reg dest, X64Reg regOp1, X64Reg regOp2, const OpArg& arg); \
954 void name(X64Reg dest, X64Reg regOp1, const OpArg& arg, X64Reg regOp2);
955
956 FMA4(VFMADDSUBPS)
957 FMA4(VFMADDSUBPD)
958 FMA4(VFMSUBADDPS)
959 FMA4(VFMSUBADDPD)
960 FMA4(VFMADDPS)
961 FMA4(VFMADDPD)
962 FMA4(VFMADDSS)
963 FMA4(VFMADDSD)
964 FMA4(VFMSUBPS)
965 FMA4(VFMSUBPD)
966 FMA4(VFMSUBSS)
967 FMA4(VFMSUBSD)
968 FMA4(VFNMADDPS)
969 FMA4(VFNMADDPD)
970 FMA4(VFNMADDSS)
971 FMA4(VFNMADDSD)
972 FMA4(VFNMSUBPS)
973 FMA4(VFNMSUBPD)
974 FMA4(VFNMSUBSS)
975 FMA4(VFNMSUBSD)
976 #undef FMA4
977
978 // VEX GPR instructions
979 void SARX(int bits, X64Reg regOp1, const OpArg& arg, X64Reg regOp2);
980 void SHLX(int bits, X64Reg regOp1, const OpArg& arg, X64Reg regOp2);
981 void SHRX(int bits, X64Reg regOp1, const OpArg& arg, X64Reg regOp2);
982 void RORX(int bits, X64Reg regOp, const OpArg& arg, u8 rotate);
983 void PEXT(int bits, X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
984 void PDEP(int bits, X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
985 void MULX(int bits, X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
986 void BZHI(int bits, X64Reg regOp1, const OpArg& arg, X64Reg regOp2);
987 void BLSR(int bits, X64Reg regOp, const OpArg& arg);
988 void BLSMSK(int bits, X64Reg regOp, const OpArg& arg);
989 void BLSI(int bits, X64Reg regOp, const OpArg& arg);
990 void BEXTR(int bits, X64Reg regOp1, const OpArg& arg, X64Reg regOp2);
991 void ANDN(int bits, X64Reg regOp1, X64Reg regOp2, const OpArg& arg);
992
993 void RDTSC();
994
995 // Utility functions
996 // The difference between this and CALL is that this aligns the stack
997 // where appropriate.
998 template <typename FunctionPointer>
999 void ABI_CallFunction(FunctionPointer func)
1000 {
1001 static_assert(std::is_pointer<FunctionPointer>() &&
1002 std::is_function<std::remove_pointer_t<FunctionPointer>>(),
1003 "Supplied type must be a function pointer.");
1004
1005 const void* ptr = reinterpret_cast<const void*>(func);
1006 const u64 address = reinterpret_cast<u64>(ptr);
1007 const u64 distance = address - (reinterpret_cast<u64>(code) + 5);
1008
1009 if (distance >= 0x0000000080000000ULL && distance < 0xFFFFFFFF80000000ULL)
1010 {
1011 // Far call
1012 MOV(64, R(RAX), Imm64(address));
1013 CALLptr(R(RAX));
1014 }
1015 else
1016 {
1017 CALL(ptr);
1018 }
1019 }
1020
1021 template <typename FunctionPointer>
1022 void ABI_CallFunctionC16(FunctionPointer func, u16 param1)
1023 {
1024 MOV(32, R(ABI_PARAM1), Imm32(param1));
1025 ABI_CallFunction(func);
1026 }
1027
1028 template <typename FunctionPointer>
1029 void ABI_CallFunctionCC16(FunctionPointer func, u32 param1, u16 param2)
1030 {
1031 MOV(32, R(ABI_PARAM1), Imm32(param1));
1032 MOV(32, R(ABI_PARAM2), Imm32(param2));
1033 ABI_CallFunction(func);
1034 }
1035
1036 template <typename FunctionPointer>
1037 void ABI_CallFunctionC(FunctionPointer func, u32 param1)
1038 {
1039 MOV(32, R(ABI_PARAM1), Imm32(param1));
1040 ABI_CallFunction(func);
1041 }
1042
1043 template <typename FunctionPointer>
1044 void ABI_CallFunctionCC(FunctionPointer func, u32 param1, u32 param2)
1045 {
1046 MOV(32, R(ABI_PARAM1), Imm32(param1));
1047 MOV(32, R(ABI_PARAM2), Imm32(param2));
1048 ABI_CallFunction(func);
1049 }
1050
1051 template <typename FunctionPointer>
1052 void ABI_CallFunctionCP(FunctionPointer func, u32 param1, const void* param2)
1053 {
1054 MOV(32, R(ABI_PARAM1), Imm32(param1));
1055 MOV(64, R(ABI_PARAM2), Imm64(reinterpret_cast<u64>(param2)));
1056 ABI_CallFunction(func);
1057 }
1058
1059 template <typename FunctionPointer>
1060 void ABI_CallFunctionCCC(FunctionPointer func, u32 param1, u32 param2, u32 param3)
1061 {
1062 MOV(32, R(ABI_PARAM1), Imm32(param1));
1063 MOV(32, R(ABI_PARAM2), Imm32(param2));
1064 MOV(32, R(ABI_PARAM3), Imm32(param3));
1065 ABI_CallFunction(func);
1066 }
1067
1068 template <typename FunctionPointer>
1069 void ABI_CallFunctionCCP(FunctionPointer func, u32 param1, u32 param2, const void* param3)
1070 {
1071 MOV(32, R(ABI_PARAM1), Imm32(param1));
1072 MOV(32, R(ABI_PARAM2), Imm32(param2));
1073 MOV(64, R(ABI_PARAM3), Imm64(reinterpret_cast<u64>(param3)));
1074 ABI_CallFunction(func);
1075 }
1076
1077 template <typename FunctionPointer>
1078 void ABI_CallFunctionCCCP(FunctionPointer func, u32 param1, u32 param2, u32 param3,
1079 const void* param4)
1080 {
1081 MOV(32, R(ABI_PARAM1), Imm32(param1));
1082 MOV(32, R(ABI_PARAM2), Imm32(param2));
1083 MOV(32, R(ABI_PARAM3), Imm32(param3));
1084 MOV(64, R(ABI_PARAM4), Imm64(reinterpret_cast<u64>(param4)));
1085 ABI_CallFunction(func);
1086 }
1087
1088 template <typename FunctionPointer>
1089 void ABI_CallFunctionP(FunctionPointer func, const void* param1)
1090 {
1091 MOV(64, R(ABI_PARAM1), Imm64(reinterpret_cast<u64>(param1)));
1092 ABI_CallFunction(func);
1093 }
1094
1095 template <typename FunctionPointer>
1096 void ABI_CallFunctionPP(FunctionPointer func, const void* param1, const void* param2)
1097 {
1098 MOV(64, R(ABI_PARAM1), Imm64(reinterpret_cast<u64>(param1)));
1099 MOV(64, R(ABI_PARAM2), Imm64(reinterpret_cast<u64>(param2)));
1100 ABI_CallFunction(func);
1101 }
1102
1103 template <typename FunctionPointer>
1104 void ABI_CallFunctionPC(FunctionPointer func, const void* param1, u32 param2)
1105 {
1106 MOV(64, R(ABI_PARAM1), Imm64(reinterpret_cast<u64>(param1)));
1107 MOV(32, R(ABI_PARAM2), Imm32(param2));
1108 ABI_CallFunction(func);
1109 }
1110
1111 template <typename FunctionPointer>
1112 void ABI_CallFunctionPPC(FunctionPointer func, const void* param1, const void* param2, u32 param3)
1113 {
1114 MOV(64, R(ABI_PARAM1), Imm64(reinterpret_cast<u64>(param1)));
1115 MOV(64, R(ABI_PARAM2), Imm64(reinterpret_cast<u64>(param2)));
1116 MOV(32, R(ABI_PARAM3), Imm32(param3));
1117 ABI_CallFunction(func);
1118 }
1119
1120 // Pass a register as a parameter.
1121 template <typename FunctionPointer>
1122 void ABI_CallFunctionR(FunctionPointer func, X64Reg reg1)
1123 {
1124 if (reg1 != ABI_PARAM1)
1125 MOV(32, R(ABI_PARAM1), R(reg1));
1126 ABI_CallFunction(func);
1127 }
1128
1129 // Pass a pointer and register as a parameter.
1130 template <typename FunctionPointer>
1131 void ABI_CallFunctionPR(FunctionPointer func, const void* ptr, X64Reg reg1)
1132 {
1133 if (reg1 != ABI_PARAM2)
1134 MOV(64, R(ABI_PARAM2), R(reg1));
1135 MOV(64, R(ABI_PARAM1), Imm64(reinterpret_cast<u64>(ptr)));
1136 ABI_CallFunction(func);
1137 }
1138
1139 // Pass two registers as parameters.
1140 template <typename FunctionPointer>
1141 void ABI_CallFunctionRR(FunctionPointer func, X64Reg reg1, X64Reg reg2)
1142 {
1143 MOVTwo(64, ABI_PARAM1, reg1, 0, ABI_PARAM2, reg2);
1144 ABI_CallFunction(func);
1145 }
1146
1147 // Pass a pointer and two registers as parameters.
1148 template <typename FunctionPointer>
1149 void ABI_CallFunctionPRR(FunctionPointer func, const void* ptr, X64Reg reg1, X64Reg reg2)
1150 {
1151 MOVTwo(64, ABI_PARAM2, reg1, 0, ABI_PARAM3, reg2);
1152 MOV(64, R(ABI_PARAM1), Imm64(reinterpret_cast<u64>(ptr)));
1153 ABI_CallFunction(func);
1154 }
1155
1156 template <typename FunctionPointer>
1157 void ABI_CallFunctionAC(int bits, FunctionPointer func, const Gen::OpArg& arg1, u32 param2)
1158 {
1159 if (!arg1.IsSimpleReg(ABI_PARAM1))
1160 MOV(bits, R(ABI_PARAM1), arg1);
1161 MOV(32, R(ABI_PARAM2), Imm32(param2));
1162 ABI_CallFunction(func);
1163 }
1164
1165 template <typename FunctionPointer>
1166 void ABI_CallFunctionPAC(int bits, FunctionPointer func, const void* ptr1, const Gen::OpArg& arg2,
1167 u32 param3)
1168 {
1169 if (!arg2.IsSimpleReg(ABI_PARAM2))
1170 MOV(bits, R(ABI_PARAM2), arg2);
1171 MOV(32, R(ABI_PARAM3), Imm32(param3));
1172 MOV(64, R(ABI_PARAM1), Imm64(reinterpret_cast<u64>(ptr1)));
1173 ABI_CallFunction(func);
1174 }
1175
1176 template <typename FunctionPointer>
1177 void ABI_CallFunctionA(int bits, FunctionPointer func, const Gen::OpArg& arg1)
1178 {
1179 if (!arg1.IsSimpleReg(ABI_PARAM1))
1180 MOV(bits, R(ABI_PARAM1), arg1);
1181 ABI_CallFunction(func);
1182 }
1183
1184 // Helper method for ABI functions related to calling functions. May be used by itself as well.
1185 void MOVTwo(int bits, X64Reg dst1, X64Reg src1, s32 offset, X64Reg dst2, X64Reg src2);
1186
1187 // Saves/restores the registers and adjusts the stack to be aligned as
1188 // required by the ABI, where the previous alignment was as specified.
1189 // Push returns the size of the shadow space, i.e. the offset of the frame.
1190 size_t ABI_PushRegistersAndAdjustStack(BitSet32 mask, size_t rsp_alignment,
1191 size_t needed_frame_size = 0);
1192 void ABI_PopRegistersAndAdjustStack(BitSet32 mask, size_t rsp_alignment,
1193 size_t needed_frame_size = 0);
1194
1195 // Utility to generate a call to a std::function object.
1196 //
1197 // Unfortunately, calling operator() directly is undefined behavior in C++
1198 // (this method might be a thunk in the case of multi-inheritance) so we
1199 // have to go through a trampoline function.
1200 template <typename T, typename... Args>
1201 static T CallLambdaTrampoline(const std::function<T(Args...)>* f, Args... args)
1202 {
1203 return (*f)(args...);
1204 }
1205
1206 template <typename T, typename... Args>
1207 void ABI_CallLambdaPC(const std::function<T(Args...)>* f, void* p1, u32 p2)
1208 {
1209 auto trampoline = &XEmitter::CallLambdaTrampoline<T, Args...>;
1210 ABI_CallFunctionPPC(trampoline, reinterpret_cast<const void*>(f), p1, p2);
1211 }
1212 }; // class XEmitter
1213
1214 class X64CodeBlock : public Common::CodeBlock<XEmitter>
1215 {
1216 private:
1217 void PoisonMemory() override
1218 {
1219 // x86/64: 0xCC = breakpoint
1220 memset(region, 0xCC, region_size);
1221 }
1222 };
1223
1224 } // namespace Gen