emitter.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013
  1. // Copyright (C) 2003 Dolphin Project.
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, version 2.0 or later versions.
  5. // This program is distributed in the hope that it will be useful,
  6. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. // GNU General Public License 2.0 for more details.
  9. // A copy of the GPL 2.0 should have been included with the program.
  10. // If not, see http://www.gnu.org/licenses/
  11. // Official SVN repository and contact information can be found at
  12. // http://code.google.com/p/dolphin-emu/
  13. #include <cinttypes>
  14. #include <cstring>
  15. #include "common/assert.h"
  16. #include "common/logging/log.h"
  17. #include "common/memory_util.h"
  18. #include "abi.h"
  19. #include "cpu_detect.h"
  20. #include "emitter.h"
  21. namespace Gen
  22. {
  23. struct NormalOpDef
  24. {
  25. u8 toRm8, toRm32, fromRm8, fromRm32, imm8, imm32, simm8, eaximm8, eaximm32, ext;
  26. };
  27. // 0xCC is code for invalid combination of immediates
  28. static const NormalOpDef normalops[11] =
  29. {
  30. {0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x83, 0x04, 0x05, 0}, //ADD
  31. {0x10, 0x11, 0x12, 0x13, 0x80, 0x81, 0x83, 0x14, 0x15, 2}, //ADC
  32. {0x28, 0x29, 0x2A, 0x2B, 0x80, 0x81, 0x83, 0x2C, 0x2D, 5}, //SUB
  33. {0x18, 0x19, 0x1A, 0x1B, 0x80, 0x81, 0x83, 0x1C, 0x1D, 3}, //SBB
  34. {0x20, 0x21, 0x22, 0x23, 0x80, 0x81, 0x83, 0x24, 0x25, 4}, //AND
  35. {0x08, 0x09, 0x0A, 0x0B, 0x80, 0x81, 0x83, 0x0C, 0x0D, 1}, //OR
  36. {0x30, 0x31, 0x32, 0x33, 0x80, 0x81, 0x83, 0x34, 0x35, 6}, //XOR
  37. {0x88, 0x89, 0x8A, 0x8B, 0xC6, 0xC7, 0xCC, 0xCC, 0xCC, 0}, //MOV
  38. {0x84, 0x85, 0x84, 0x85, 0xF6, 0xF7, 0xCC, 0xA8, 0xA9, 0}, //TEST (to == from)
  39. {0x38, 0x39, 0x3A, 0x3B, 0x80, 0x81, 0x83, 0x3C, 0x3D, 7}, //CMP
  40. {0x86, 0x87, 0x86, 0x87, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 7}, //XCHG
  41. };
  42. enum NormalSSEOps
  43. {
  44. sseCMP = 0xC2,
  45. sseADD = 0x58, //ADD
  46. sseSUB = 0x5C, //SUB
  47. sseAND = 0x54, //AND
  48. sseANDN = 0x55, //ANDN
  49. sseOR = 0x56,
  50. sseXOR = 0x57,
  51. sseMUL = 0x59, //MUL
  52. sseDIV = 0x5E, //DIV
  53. sseMIN = 0x5D, //MIN
  54. sseMAX = 0x5F, //MAX
  55. sseCOMIS = 0x2F, //COMIS
  56. sseUCOMIS = 0x2E, //UCOMIS
  57. sseSQRT = 0x51, //SQRT
  58. sseRSQRT = 0x52, //RSQRT (NO DOUBLE PRECISION!!!)
  59. sseRCP = 0x53, //RCP
  60. sseMOVAPfromRM = 0x28, //MOVAP from RM
  61. sseMOVAPtoRM = 0x29, //MOVAP to RM
  62. sseMOVUPfromRM = 0x10, //MOVUP from RM
  63. sseMOVUPtoRM = 0x11, //MOVUP to RM
  64. sseMOVLPfromRM= 0x12,
  65. sseMOVLPtoRM = 0x13,
  66. sseMOVHPfromRM= 0x16,
  67. sseMOVHPtoRM = 0x17,
  68. sseMOVHLPS = 0x12,
  69. sseMOVLHPS = 0x16,
  70. sseMOVDQfromRM = 0x6F,
  71. sseMOVDQtoRM = 0x7F,
  72. sseMASKMOVDQU = 0xF7,
  73. sseLDDQU = 0xF0,
  74. sseSHUF = 0xC6,
  75. sseMOVNTDQ = 0xE7,
  76. sseMOVNTP = 0x2B,
  77. sseHADD = 0x7C,
  78. };
  79. void XEmitter::SetCodePtr(u8 *ptr)
  80. {
  81. code = ptr;
  82. }
  83. const u8 *XEmitter::GetCodePtr() const
  84. {
  85. return code;
  86. }
  87. u8 *XEmitter::GetWritableCodePtr()
  88. {
  89. return code;
  90. }
  91. void XEmitter::Write8(u8 value)
  92. {
  93. *code++ = value;
  94. }
  95. void XEmitter::Write16(u16 value)
  96. {
  97. std::memcpy(code, &value, sizeof(u16));
  98. code += sizeof(u16);
  99. }
  100. void XEmitter::Write32(u32 value)
  101. {
  102. std::memcpy(code, &value, sizeof(u32));
  103. code += sizeof(u32);
  104. }
  105. void XEmitter::Write64(u64 value)
  106. {
  107. std::memcpy(code, &value, sizeof(u64));
  108. code += sizeof(u64);
  109. }
  110. void XEmitter::ReserveCodeSpace(int bytes)
  111. {
  112. for (int i = 0; i < bytes; i++)
  113. *code++ = 0xCC;
  114. }
  115. const u8 *XEmitter::AlignCode4()
  116. {
  117. int c = int((u64)code & 3);
  118. if (c)
  119. ReserveCodeSpace(4-c);
  120. return code;
  121. }
  122. const u8 *XEmitter::AlignCode16()
  123. {
  124. int c = int((u64)code & 15);
  125. if (c)
  126. ReserveCodeSpace(16-c);
  127. return code;
  128. }
  129. const u8 *XEmitter::AlignCodePage()
  130. {
  131. int c = int((u64)code & 4095);
  132. if (c)
  133. ReserveCodeSpace(4096-c);
  134. return code;
  135. }
  136. // This operation modifies flags; check to see the flags are locked.
  137. // If the flags are locked, we should immediately and loudly fail before
  138. // causing a subtle JIT bug.
  139. void XEmitter::CheckFlags()
  140. {
  141. ASSERT_MSG(!flags_locked, "Attempt to modify flags while flags locked!");
  142. }
  143. void XEmitter::WriteModRM(int mod, int reg, int rm)
  144. {
  145. Write8((u8)((mod << 6) | ((reg & 7) << 3) | (rm & 7)));
  146. }
  147. void XEmitter::WriteSIB(int scale, int index, int base)
  148. {
  149. Write8((u8)((scale << 6) | ((index & 7) << 3) | (base & 7)));
  150. }
  151. void OpArg::WriteRex(XEmitter *emit, int opBits, int bits, int customOp) const
  152. {
  153. if (customOp == -1) customOp = operandReg;
  154. #ifdef ARCHITECTURE_x86_64
  155. u8 op = 0x40;
  156. // REX.W (whether operation is a 64-bit operation)
  157. if (opBits == 64) op |= 8;
  158. // REX.R (whether ModR/M reg field refers to R8-R15.
  159. if (customOp & 8) op |= 4;
  160. // REX.X (whether ModR/M SIB index field refers to R8-R15)
  161. if (indexReg & 8) op |= 2;
  162. // REX.B (whether ModR/M rm or SIB base or opcode reg field refers to R8-R15)
  163. if (offsetOrBaseReg & 8) op |= 1;
  164. // Write REX if wr have REX bits to write, or if the operation accesses
  165. // SIL, DIL, BPL, or SPL.
  166. if (op != 0x40 ||
  167. (scale == SCALE_NONE && bits == 8 && (offsetOrBaseReg & 0x10c) == 4) ||
  168. (opBits == 8 && (customOp & 0x10c) == 4))
  169. {
  170. emit->Write8(op);
  171. // Check the operation doesn't access AH, BH, CH, or DH.
  172. DEBUG_ASSERT((offsetOrBaseReg & 0x100) == 0);
  173. DEBUG_ASSERT((customOp & 0x100) == 0);
  174. }
  175. #else
  176. DEBUG_ASSERT(opBits != 64);
  177. DEBUG_ASSERT((customOp & 8) == 0 || customOp == -1);
  178. DEBUG_ASSERT((indexReg & 8) == 0);
  179. DEBUG_ASSERT((offsetOrBaseReg & 8) == 0);
  180. DEBUG_ASSERT(opBits != 8 || (customOp & 0x10c) != 4 || customOp == -1);
  181. DEBUG_ASSERT(scale == SCALE_ATREG || bits != 8 || (offsetOrBaseReg & 0x10c) != 4);
  182. #endif
  183. }
  184. void OpArg::WriteVex(XEmitter* emit, X64Reg regOp1, X64Reg regOp2, int L, int pp, int mmmmm, int W) const
  185. {
  186. int R = !(regOp1 & 8);
  187. int X = !(indexReg & 8);
  188. int B = !(offsetOrBaseReg & 8);
  189. int vvvv = (regOp2 == X64Reg::INVALID_REG) ? 0xf : (regOp2 ^ 0xf);
  190. // do we need any VEX fields that only appear in the three-byte form?
  191. if (X == 1 && B == 1 && W == 0 && mmmmm == 1)
  192. {
  193. u8 RvvvvLpp = (R << 7) | (vvvv << 3) | (L << 2) | pp;
  194. emit->Write8(0xC5);
  195. emit->Write8(RvvvvLpp);
  196. }
  197. else
  198. {
  199. u8 RXBmmmmm = (R << 7) | (X << 6) | (B << 5) | mmmmm;
  200. u8 WvvvvLpp = (W << 7) | (vvvv << 3) | (L << 2) | pp;
  201. emit->Write8(0xC4);
  202. emit->Write8(RXBmmmmm);
  203. emit->Write8(WvvvvLpp);
  204. }
  205. }
  206. void OpArg::WriteRest(XEmitter *emit, int extraBytes, X64Reg _operandReg,
  207. bool warn_64bit_offset) const
  208. {
  209. if (_operandReg == INVALID_REG)
  210. _operandReg = (X64Reg)this->operandReg;
  211. int mod = 0;
  212. int ireg = indexReg;
  213. bool SIB = false;
  214. int _offsetOrBaseReg = this->offsetOrBaseReg;
  215. if (scale == SCALE_RIP) //Also, on 32-bit, just an immediate address
  216. {
  217. // Oh, RIP addressing.
  218. _offsetOrBaseReg = 5;
  219. emit->WriteModRM(0, _operandReg, _offsetOrBaseReg);
  220. //TODO : add some checks
  221. #ifdef ARCHITECTURE_x86_64
  222. u64 ripAddr = (u64)emit->GetCodePtr() + 4 + extraBytes;
  223. s64 distance = (s64)offset - (s64)ripAddr;
  224. ASSERT_MSG(
  225. (distance < 0x80000000LL &&
  226. distance >= -0x80000000LL) ||
  227. !warn_64bit_offset,
  228. "WriteRest: op out of range (0x%" PRIx64 " uses 0x%" PRIx64 ")",
  229. ripAddr, offset);
  230. s32 offs = (s32)distance;
  231. emit->Write32((u32)offs);
  232. #else
  233. emit->Write32((u32)offset);
  234. #endif
  235. return;
  236. }
  237. if (scale == 0)
  238. {
  239. // Oh, no memory, Just a reg.
  240. mod = 3; //11
  241. }
  242. else if (scale >= 1)
  243. {
  244. //Ah good, no scaling.
  245. if (scale == SCALE_ATREG && !((_offsetOrBaseReg & 7) == 4 || (_offsetOrBaseReg & 7) == 5))
  246. {
  247. //Okay, we're good. No SIB necessary.
  248. int ioff = (int)offset;
  249. if (ioff == 0)
  250. {
  251. mod = 0;
  252. }
  253. else if (ioff<-128 || ioff>127)
  254. {
  255. mod = 2; //32-bit displacement
  256. }
  257. else
  258. {
  259. mod = 1; //8-bit displacement
  260. }
  261. }
  262. else if (scale >= SCALE_NOBASE_2 && scale <= SCALE_NOBASE_8)
  263. {
  264. SIB = true;
  265. mod = 0;
  266. _offsetOrBaseReg = 5;
  267. }
  268. else //if (scale != SCALE_ATREG)
  269. {
  270. if ((_offsetOrBaseReg & 7) == 4) //this would occupy the SIB encoding :(
  271. {
  272. //So we have to fake it with SIB encoding :(
  273. SIB = true;
  274. }
  275. if (scale >= SCALE_1 && scale < SCALE_ATREG)
  276. {
  277. SIB = true;
  278. }
  279. if (scale == SCALE_ATREG && ((_offsetOrBaseReg & 7) == 4))
  280. {
  281. SIB = true;
  282. ireg = _offsetOrBaseReg;
  283. }
  284. //Okay, we're fine. Just disp encoding.
  285. //We need displacement. Which size?
  286. int ioff = (int)(s64)offset;
  287. if (ioff < -128 || ioff > 127)
  288. {
  289. mod = 2; //32-bit displacement
  290. }
  291. else
  292. {
  293. mod = 1; //8-bit displacement
  294. }
  295. }
  296. }
  297. // Okay. Time to do the actual writing
  298. // ModRM byte:
  299. int oreg = _offsetOrBaseReg;
  300. if (SIB)
  301. oreg = 4;
  302. // TODO(ector): WTF is this if about? I don't remember writing it :-)
  303. //if (RIP)
  304. // oreg = 5;
  305. emit->WriteModRM(mod, _operandReg&7, oreg&7);
  306. if (SIB)
  307. {
  308. //SIB byte
  309. int ss;
  310. switch (scale)
  311. {
  312. case SCALE_NONE: _offsetOrBaseReg = 4; ss = 0; break; //RSP
  313. case SCALE_1: ss = 0; break;
  314. case SCALE_2: ss = 1; break;
  315. case SCALE_4: ss = 2; break;
  316. case SCALE_8: ss = 3; break;
  317. case SCALE_NOBASE_2: ss = 1; break;
  318. case SCALE_NOBASE_4: ss = 2; break;
  319. case SCALE_NOBASE_8: ss = 3; break;
  320. case SCALE_ATREG: ss = 0; break;
  321. default: ASSERT_MSG(0, "Invalid scale for SIB byte"); ss = 0; break;
  322. }
  323. emit->Write8((u8)((ss << 6) | ((ireg&7)<<3) | (_offsetOrBaseReg&7)));
  324. }
  325. if (mod == 1) //8-bit disp
  326. {
  327. emit->Write8((u8)(s8)(s32)offset);
  328. }
  329. else if (mod == 2 || (scale >= SCALE_NOBASE_2 && scale <= SCALE_NOBASE_8)) //32-bit disp
  330. {
  331. emit->Write32((u32)offset);
  332. }
  333. }
  334. // W = operand extended width (1 if 64-bit)
  335. // R = register# upper bit
  336. // X = scale amnt upper bit
  337. // B = base register# upper bit
  338. void XEmitter::Rex(int w, int r, int x, int b)
  339. {
  340. w = w ? 1 : 0;
  341. r = r ? 1 : 0;
  342. x = x ? 1 : 0;
  343. b = b ? 1 : 0;
  344. u8 rx = (u8)(0x40 | (w << 3) | (r << 2) | (x << 1) | (b));
  345. if (rx != 0x40)
  346. Write8(rx);
  347. }
  348. void XEmitter::JMP(const u8* addr, bool force5Bytes)
  349. {
  350. u64 fn = (u64)addr;
  351. if (!force5Bytes)
  352. {
  353. s64 distance = (s64)(fn - ((u64)code + 2));
  354. ASSERT_MSG(distance >= -0x80 && distance < 0x80,
  355. "Jump target too far away, needs force5Bytes = true");
  356. //8 bits will do
  357. Write8(0xEB);
  358. Write8((u8)(s8)distance);
  359. }
  360. else
  361. {
  362. s64 distance = (s64)(fn - ((u64)code + 5));
  363. ASSERT_MSG(
  364. distance >= -0x80000000LL && distance < 0x80000000LL,
  365. "Jump target too far away, needs indirect register");
  366. Write8(0xE9);
  367. Write32((u32)(s32)distance);
  368. }
  369. }
  370. void XEmitter::JMPptr(const OpArg& arg2)
  371. {
  372. OpArg arg = arg2;
  373. if (arg.IsImm()) ASSERT_MSG(0, "JMPptr - Imm argument");
  374. arg.operandReg = 4;
  375. arg.WriteRex(this, 0, 0);
  376. Write8(0xFF);
  377. arg.WriteRest(this);
  378. }
  379. //Can be used to trap other processors, before overwriting their code
  380. // not used in dolphin
  381. void XEmitter::JMPself()
  382. {
  383. Write8(0xEB);
  384. Write8(0xFE);
  385. }
  386. void XEmitter::CALLptr(OpArg arg)
  387. {
  388. if (arg.IsImm()) ASSERT_MSG(0, "CALLptr - Imm argument");
  389. arg.operandReg = 2;
  390. arg.WriteRex(this, 0, 0);
  391. Write8(0xFF);
  392. arg.WriteRest(this);
  393. }
  394. void XEmitter::CALL(const void* fnptr)
  395. {
  396. u64 distance = u64(fnptr) - (u64(code) + 5);
  397. ASSERT_MSG(
  398. distance < 0x0000000080000000ULL ||
  399. distance >= 0xFFFFFFFF80000000ULL,
  400. "CALL out of range (%p calls %p)", code, fnptr);
  401. Write8(0xE8);
  402. Write32(u32(distance));
  403. }
  404. FixupBranch XEmitter::CALL()
  405. {
  406. FixupBranch branch;
  407. branch.type = 1;
  408. branch.ptr = code + 5;
  409. Write8(0xE8);
  410. Write32(0);
  411. return branch;
  412. }
  413. FixupBranch XEmitter::J(bool force5bytes)
  414. {
  415. FixupBranch branch;
  416. branch.type = force5bytes ? 1 : 0;
  417. branch.ptr = code + (force5bytes ? 5 : 2);
  418. if (!force5bytes)
  419. {
  420. //8 bits will do
  421. Write8(0xEB);
  422. Write8(0);
  423. }
  424. else
  425. {
  426. Write8(0xE9);
  427. Write32(0);
  428. }
  429. return branch;
  430. }
  431. FixupBranch XEmitter::J_CC(CCFlags conditionCode, bool force5bytes)
  432. {
  433. FixupBranch branch;
  434. branch.type = force5bytes ? 1 : 0;
  435. branch.ptr = code + (force5bytes ? 6 : 2);
  436. if (!force5bytes)
  437. {
  438. //8 bits will do
  439. Write8(0x70 + conditionCode);
  440. Write8(0);
  441. }
  442. else
  443. {
  444. Write8(0x0F);
  445. Write8(0x80 + conditionCode);
  446. Write32(0);
  447. }
  448. return branch;
  449. }
  450. void XEmitter::J_CC(CCFlags conditionCode, const u8* addr, bool force5bytes)
  451. {
  452. u64 fn = (u64)addr;
  453. s64 distance = (s64)(fn - ((u64)code + 2));
  454. if (distance < -0x80 || distance >= 0x80 || force5bytes)
  455. {
  456. distance = (s64)(fn - ((u64)code + 6));
  457. ASSERT_MSG(
  458. distance >= -0x80000000LL && distance < 0x80000000LL,
  459. "Jump target too far away, needs indirect register");
  460. Write8(0x0F);
  461. Write8(0x80 + conditionCode);
  462. Write32((u32)(s32)distance);
  463. }
  464. else
  465. {
  466. Write8(0x70 + conditionCode);
  467. Write8((u8)(s8)distance);
  468. }
  469. }
  470. void XEmitter::SetJumpTarget(const FixupBranch& branch)
  471. {
  472. if (branch.type == 0)
  473. {
  474. s64 distance = (s64)(code - branch.ptr);
  475. ASSERT_MSG(distance >= -0x80 && distance < 0x80, "Jump target too far away, needs force5Bytes = true");
  476. branch.ptr[-1] = (u8)(s8)distance;
  477. }
  478. else if (branch.type == 1)
  479. {
  480. s64 distance = (s64)(code - branch.ptr);
  481. ASSERT_MSG(distance >= -0x80000000LL && distance < 0x80000000LL, "Jump target too far away, needs indirect register");
  482. ((s32*)branch.ptr)[-1] = (s32)distance;
  483. }
  484. }
  485. void XEmitter::SetJumpTarget(const FixupBranch& branch, const u8* target)
  486. {
  487. if (branch.type == 0)
  488. {
  489. s64 distance = (s64)(target - branch.ptr);
  490. ASSERT_MSG(distance >= -0x80 && distance < 0x80, "Jump target too far away, needs force5Bytes = true");
  491. branch.ptr[-1] = (u8)(s8)distance;
  492. }
  493. else if (branch.type == 1)
  494. {
  495. s64 distance = (s64)(target - branch.ptr);
  496. ASSERT_MSG(distance >= -0x80000000LL && distance < 0x80000000LL, "Jump target too far away, needs indirect register");
  497. ((s32*)branch.ptr)[-1] = (s32)distance;
  498. }
  499. }
  500. //Single byte opcodes
  501. //There is no PUSHAD/POPAD in 64-bit mode.
  502. void XEmitter::INT3() {Write8(0xCC);}
  503. void XEmitter::RET() {Write8(0xC3);}
  504. void XEmitter::RET_FAST() {Write8(0xF3); Write8(0xC3);} //two-byte return (rep ret) - recommended by AMD optimization manual for the case of jumping to a ret
  505. // The first sign of decadence: optimized NOPs.
  506. void XEmitter::NOP(size_t size)
  507. {
  508. DEBUG_ASSERT((int)size > 0);
  509. while (true)
  510. {
  511. switch (size)
  512. {
  513. case 0:
  514. return;
  515. case 1:
  516. Write8(0x90);
  517. return;
  518. case 2:
  519. Write8(0x66); Write8(0x90);
  520. return;
  521. case 3:
  522. Write8(0x0F); Write8(0x1F); Write8(0x00);
  523. return;
  524. case 4:
  525. Write8(0x0F); Write8(0x1F); Write8(0x40); Write8(0x00);
  526. return;
  527. case 5:
  528. Write8(0x0F); Write8(0x1F); Write8(0x44); Write8(0x00);
  529. Write8(0x00);
  530. return;
  531. case 6:
  532. Write8(0x66); Write8(0x0F); Write8(0x1F); Write8(0x44);
  533. Write8(0x00); Write8(0x00);
  534. return;
  535. case 7:
  536. Write8(0x0F); Write8(0x1F); Write8(0x80); Write8(0x00);
  537. Write8(0x00); Write8(0x00); Write8(0x00);
  538. return;
  539. case 8:
  540. Write8(0x0F); Write8(0x1F); Write8(0x84); Write8(0x00);
  541. Write8(0x00); Write8(0x00); Write8(0x00); Write8(0x00);
  542. return;
  543. case 9:
  544. Write8(0x66); Write8(0x0F); Write8(0x1F); Write8(0x84);
  545. Write8(0x00); Write8(0x00); Write8(0x00); Write8(0x00);
  546. Write8(0x00);
  547. return;
  548. case 10:
  549. Write8(0x66); Write8(0x66); Write8(0x0F); Write8(0x1F);
  550. Write8(0x84); Write8(0x00); Write8(0x00); Write8(0x00);
  551. Write8(0x00); Write8(0x00);
  552. return;
  553. default:
  554. // Even though x86 instructions are allowed to be up to 15 bytes long,
  555. // AMD advises against using NOPs longer than 11 bytes because they
  556. // carry a performance penalty on CPUs older than AMD family 16h.
  557. Write8(0x66); Write8(0x66); Write8(0x66); Write8(0x0F);
  558. Write8(0x1F); Write8(0x84); Write8(0x00); Write8(0x00);
  559. Write8(0x00); Write8(0x00); Write8(0x00);
  560. size -= 11;
  561. continue;
  562. }
  563. }
  564. }
  565. void XEmitter::PAUSE() {Write8(0xF3); NOP();} //use in tight spinloops for energy saving on some cpu
  566. void XEmitter::CLC() {CheckFlags(); Write8(0xF8);} //clear carry
  567. void XEmitter::CMC() {CheckFlags(); Write8(0xF5);} //flip carry
  568. void XEmitter::STC() {CheckFlags(); Write8(0xF9);} //set carry
  569. //TODO: xchg ah, al ???
  570. void XEmitter::XCHG_AHAL()
  571. {
  572. Write8(0x86);
  573. Write8(0xe0);
  574. // alt. 86 c4
  575. }
  576. //These two can not be executed on early Intel 64-bit CPU:s, only on AMD!
  577. void XEmitter::LAHF() {Write8(0x9F);}
  578. void XEmitter::SAHF() {CheckFlags(); Write8(0x9E);}
  579. void XEmitter::PUSHF() {Write8(0x9C);}
  580. void XEmitter::POPF() {CheckFlags(); Write8(0x9D);}
  581. void XEmitter::LFENCE() {Write8(0x0F); Write8(0xAE); Write8(0xE8);}
  582. void XEmitter::MFENCE() {Write8(0x0F); Write8(0xAE); Write8(0xF0);}
  583. void XEmitter::SFENCE() {Write8(0x0F); Write8(0xAE); Write8(0xF8);}
  584. void XEmitter::WriteSimple1Byte(int bits, u8 byte, X64Reg reg)
  585. {
  586. if (bits == 16)
  587. Write8(0x66);
  588. Rex(bits == 64, 0, 0, (int)reg >> 3);
  589. Write8(byte + ((int)reg & 7));
  590. }
  591. void XEmitter::WriteSimple2Byte(int bits, u8 byte1, u8 byte2, X64Reg reg)
  592. {
  593. if (bits == 16)
  594. Write8(0x66);
  595. Rex(bits==64, 0, 0, (int)reg >> 3);
  596. Write8(byte1);
  597. Write8(byte2 + ((int)reg & 7));
  598. }
  599. void XEmitter::CWD(int bits)
  600. {
  601. if (bits == 16)
  602. Write8(0x66);
  603. Rex(bits == 64, 0, 0, 0);
  604. Write8(0x99);
  605. }
  606. void XEmitter::CBW(int bits)
  607. {
  608. if (bits == 8)
  609. Write8(0x66);
  610. Rex(bits == 32, 0, 0, 0);
  611. Write8(0x98);
  612. }
  613. //Simple opcodes
  614. //push/pop do not need wide to be 64-bit
  615. void XEmitter::PUSH(X64Reg reg) {WriteSimple1Byte(32, 0x50, reg);}
  616. void XEmitter::POP(X64Reg reg) {WriteSimple1Byte(32, 0x58, reg);}
  617. void XEmitter::PUSH(int bits, const OpArg& reg)
  618. {
  619. if (reg.IsSimpleReg())
  620. PUSH(reg.GetSimpleReg());
  621. else if (reg.IsImm())
  622. {
  623. switch (reg.GetImmBits())
  624. {
  625. case 8:
  626. Write8(0x6A);
  627. Write8((u8)(s8)reg.offset);
  628. break;
  629. case 16:
  630. Write8(0x66);
  631. Write8(0x68);
  632. Write16((u16)(s16)(s32)reg.offset);
  633. break;
  634. case 32:
  635. Write8(0x68);
  636. Write32((u32)reg.offset);
  637. break;
  638. default:
  639. ASSERT_MSG(0, "PUSH - Bad imm bits");
  640. break;
  641. }
  642. }
  643. else
  644. {
  645. if (bits == 16)
  646. Write8(0x66);
  647. reg.WriteRex(this, bits, bits);
  648. Write8(0xFF);
  649. reg.WriteRest(this, 0, (X64Reg)6);
  650. }
  651. }
  652. void XEmitter::POP(int /*bits*/, const OpArg& reg)
  653. {
  654. if (reg.IsSimpleReg())
  655. POP(reg.GetSimpleReg());
  656. else
  657. ASSERT_MSG(0, "POP - Unsupported encoding");
  658. }
  659. void XEmitter::BSWAP(int bits, X64Reg reg)
  660. {
  661. if (bits >= 32)
  662. {
  663. WriteSimple2Byte(bits, 0x0F, 0xC8, reg);
  664. }
  665. else if (bits == 16)
  666. {
  667. ROL(16, R(reg), Imm8(8));
  668. }
  669. else if (bits == 8)
  670. {
  671. // Do nothing - can't bswap a single byte...
  672. }
  673. else
  674. {
  675. ASSERT_MSG(0, "BSWAP - Wrong number of bits");
  676. }
  677. }
  678. // Undefined opcode - reserved
  679. // If we ever need a way to always cause a non-breakpoint hard exception...
  680. void XEmitter::UD2()
  681. {
  682. Write8(0x0F);
  683. Write8(0x0B);
  684. }
  685. void XEmitter::PREFETCH(PrefetchLevel level, OpArg arg)
  686. {
  687. ASSERT_MSG(!arg.IsImm(), "PREFETCH - Imm argument");
  688. arg.operandReg = (u8)level;
  689. arg.WriteRex(this, 0, 0);
  690. Write8(0x0F);
  691. Write8(0x18);
  692. arg.WriteRest(this);
  693. }
  694. void XEmitter::SETcc(CCFlags flag, OpArg dest)
  695. {
  696. ASSERT_MSG(!dest.IsImm(), "SETcc - Imm argument");
  697. dest.operandReg = 0;
  698. dest.WriteRex(this, 0, 8);
  699. Write8(0x0F);
  700. Write8(0x90 + (u8)flag);
  701. dest.WriteRest(this);
  702. }
  703. void XEmitter::CMOVcc(int bits, X64Reg dest, OpArg src, CCFlags flag)
  704. {
  705. ASSERT_MSG(!src.IsImm(), "CMOVcc - Imm argument");
  706. ASSERT_MSG(bits != 8, "CMOVcc - 8 bits unsupported");
  707. if (bits == 16)
  708. Write8(0x66);
  709. src.operandReg = dest;
  710. src.WriteRex(this, bits, bits);
  711. Write8(0x0F);
  712. Write8(0x40 + (u8)flag);
  713. src.WriteRest(this);
  714. }
  715. void XEmitter::WriteMulDivType(int bits, OpArg src, int ext)
  716. {
  717. ASSERT_MSG(!src.IsImm(), "WriteMulDivType - Imm argument");
  718. CheckFlags();
  719. src.operandReg = ext;
  720. if (bits == 16)
  721. Write8(0x66);
  722. src.WriteRex(this, bits, bits, 0);
  723. if (bits == 8)
  724. {
  725. Write8(0xF6);
  726. }
  727. else
  728. {
  729. Write8(0xF7);
  730. }
  731. src.WriteRest(this);
  732. }
  733. void XEmitter::MUL(int bits, const OpArg& src) {WriteMulDivType(bits, src, 4);}
  734. void XEmitter::DIV(int bits, const OpArg& src) {WriteMulDivType(bits, src, 6);}
  735. void XEmitter::IMUL(int bits, const OpArg& src) {WriteMulDivType(bits, src, 5);}
  736. void XEmitter::IDIV(int bits, const OpArg& src) {WriteMulDivType(bits, src, 7);}
  737. void XEmitter::NEG(int bits, const OpArg& src) {WriteMulDivType(bits, src, 3);}
  738. void XEmitter::NOT(int bits, const OpArg& src) {WriteMulDivType(bits, src, 2);}
  739. void XEmitter::WriteBitSearchType(int bits, X64Reg dest, OpArg src, u8 byte2, bool rep)
  740. {
  741. ASSERT_MSG(!src.IsImm(), "WriteBitSearchType - Imm argument");
  742. CheckFlags();
  743. src.operandReg = (u8)dest;
  744. if (bits == 16)
  745. Write8(0x66);
  746. if (rep)
  747. Write8(0xF3);
  748. src.WriteRex(this, bits, bits);
  749. Write8(0x0F);
  750. Write8(byte2);
  751. src.WriteRest(this);
  752. }
  753. void XEmitter::MOVNTI(int bits, const OpArg& dest, X64Reg src)
  754. {
  755. if (bits <= 16)
  756. ASSERT_MSG(0, "MOVNTI - bits<=16");
  757. WriteBitSearchType(bits, src, dest, 0xC3);
  758. }
  759. void XEmitter::BSF(int bits, X64Reg dest, const OpArg& src) {WriteBitSearchType(bits,dest,src,0xBC);} // Bottom bit to top bit
  760. void XEmitter::BSR(int bits, X64Reg dest, const OpArg& src) {WriteBitSearchType(bits,dest,src,0xBD);} // Top bit to bottom bit
  761. void XEmitter::TZCNT(int bits, X64Reg dest, const OpArg& src)
  762. {
  763. CheckFlags();
  764. if (!Common::GetCPUCaps().bmi1)
  765. ASSERT_MSG(0, "Trying to use BMI1 on a system that doesn't support it. Bad programmer.");
  766. WriteBitSearchType(bits, dest, src, 0xBC, true);
  767. }
  768. void XEmitter::LZCNT(int bits, X64Reg dest, const OpArg& src)
  769. {
  770. CheckFlags();
  771. if (!Common::GetCPUCaps().lzcnt)
  772. ASSERT_MSG(0, "Trying to use LZCNT on a system that doesn't support it. Bad programmer.");
  773. WriteBitSearchType(bits, dest, src, 0xBD, true);
  774. }
  775. void XEmitter::MOVSX(int dbits, int sbits, X64Reg dest, OpArg src)
  776. {
  777. ASSERT_MSG(!src.IsImm(), "MOVSX - Imm argument");
  778. if (dbits == sbits)
  779. {
  780. MOV(dbits, R(dest), src);
  781. return;
  782. }
  783. src.operandReg = (u8)dest;
  784. if (dbits == 16)
  785. Write8(0x66);
  786. src.WriteRex(this, dbits, sbits);
  787. if (sbits == 8)
  788. {
  789. Write8(0x0F);
  790. Write8(0xBE);
  791. }
  792. else if (sbits == 16)
  793. {
  794. Write8(0x0F);
  795. Write8(0xBF);
  796. }
  797. else if (sbits == 32 && dbits == 64)
  798. {
  799. Write8(0x63);
  800. }
  801. else
  802. {
  803. Crash();
  804. }
  805. src.WriteRest(this);
  806. }
  807. void XEmitter::MOVZX(int dbits, int sbits, X64Reg dest, OpArg src)
  808. {
  809. ASSERT_MSG(!src.IsImm(), "MOVZX - Imm argument");
  810. if (dbits == sbits)
  811. {
  812. MOV(dbits, R(dest), src);
  813. return;
  814. }
  815. src.operandReg = (u8)dest;
  816. if (dbits == 16)
  817. Write8(0x66);
  818. //the 32bit result is automatically zero extended to 64bit
  819. src.WriteRex(this, dbits == 64 ? 32 : dbits, sbits);
  820. if (sbits == 8)
  821. {
  822. Write8(0x0F);
  823. Write8(0xB6);
  824. }
  825. else if (sbits == 16)
  826. {
  827. Write8(0x0F);
  828. Write8(0xB7);
  829. }
  830. else if (sbits == 32 && dbits == 64)
  831. {
  832. Write8(0x8B);
  833. }
  834. else
  835. {
  836. ASSERT_MSG(0, "MOVZX - Invalid size");
  837. }
  838. src.WriteRest(this);
  839. }
  840. void XEmitter::MOVBE(int bits, const OpArg& dest, const OpArg& src)
  841. {
  842. ASSERT_MSG(Common::GetCPUCaps().movbe, "Generating MOVBE on a system that does not support it.");
  843. if (bits == 8)
  844. {
  845. MOV(bits, dest, src);
  846. return;
  847. }
  848. if (bits == 16)
  849. Write8(0x66);
  850. if (dest.IsSimpleReg())
  851. {
  852. ASSERT_MSG(!src.IsSimpleReg() && !src.IsImm(), "MOVBE: Loading from !mem");
  853. src.WriteRex(this, bits, bits, dest.GetSimpleReg());
  854. Write8(0x0F); Write8(0x38); Write8(0xF0);
  855. src.WriteRest(this, 0, dest.GetSimpleReg());
  856. }
  857. else if (src.IsSimpleReg())
  858. {
  859. ASSERT_MSG(!dest.IsSimpleReg() && !dest.IsImm(), "MOVBE: Storing to !mem");
  860. dest.WriteRex(this, bits, bits, src.GetSimpleReg());
  861. Write8(0x0F); Write8(0x38); Write8(0xF1);
  862. dest.WriteRest(this, 0, src.GetSimpleReg());
  863. }
  864. else
  865. {
  866. ASSERT_MSG(0, "MOVBE: Not loading or storing to mem");
  867. }
  868. }
  869. void XEmitter::LEA(int bits, X64Reg dest, OpArg src)
  870. {
  871. ASSERT_MSG(!src.IsImm(), "LEA - Imm argument");
  872. src.operandReg = (u8)dest;
  873. if (bits == 16)
  874. Write8(0x66); //TODO: performance warning
  875. src.WriteRex(this, bits, bits);
  876. Write8(0x8D);
  877. src.WriteRest(this, 0, INVALID_REG, bits == 64);
  878. }
  879. //shift can be either imm8 or cl
  880. void XEmitter::WriteShift(int bits, OpArg dest, const OpArg& shift, int ext)
  881. {
  882. CheckFlags();
  883. bool writeImm = false;
  884. if (dest.IsImm())
  885. {
  886. ASSERT_MSG(0, "WriteShift - can't shift imms");
  887. }
  888. if ((shift.IsSimpleReg() && shift.GetSimpleReg() != ECX) || (shift.IsImm() && shift.GetImmBits() != 8))
  889. {
  890. ASSERT_MSG(0, "WriteShift - illegal argument");
  891. }
  892. dest.operandReg = ext;
  893. if (bits == 16)
  894. Write8(0x66);
  895. dest.WriteRex(this, bits, bits, 0);
  896. if (shift.GetImmBits() == 8)
  897. {
  898. //ok an imm
  899. u8 imm = (u8)shift.offset;
  900. if (imm == 1)
  901. {
  902. Write8(bits == 8 ? 0xD0 : 0xD1);
  903. }
  904. else
  905. {
  906. writeImm = true;
  907. Write8(bits == 8 ? 0xC0 : 0xC1);
  908. }
  909. }
  910. else
  911. {
  912. Write8(bits == 8 ? 0xD2 : 0xD3);
  913. }
  914. dest.WriteRest(this, writeImm ? 1 : 0);
  915. if (writeImm)
  916. Write8((u8)shift.offset);
  917. }
  918. // large rotates and shift are slower on intel than amd
  919. // intel likes to rotate by 1, and the op is smaller too
  920. void XEmitter::ROL(int bits, const OpArg& dest, const OpArg& shift) {WriteShift(bits, dest, shift, 0);}
  921. void XEmitter::ROR(int bits, const OpArg& dest, const OpArg& shift) {WriteShift(bits, dest, shift, 1);}
  922. void XEmitter::RCL(int bits, const OpArg& dest, const OpArg& shift) {WriteShift(bits, dest, shift, 2);}
  923. void XEmitter::RCR(int bits, const OpArg& dest, const OpArg& shift) {WriteShift(bits, dest, shift, 3);}
  924. void XEmitter::SHL(int bits, const OpArg& dest, const OpArg& shift) {WriteShift(bits, dest, shift, 4);}
  925. void XEmitter::SHR(int bits, const OpArg& dest, const OpArg& shift) {WriteShift(bits, dest, shift, 5);}
  926. void XEmitter::SAR(int bits, const OpArg& dest, const OpArg& shift) {WriteShift(bits, dest, shift, 7);}
  927. // index can be either imm8 or register, don't use memory destination because it's slow
  928. void XEmitter::WriteBitTest(int bits, const OpArg& dest, const OpArg& index, int ext)
  929. {
  930. CheckFlags();
  931. if (dest.IsImm())
  932. {
  933. ASSERT_MSG(0, "WriteBitTest - can't test imms");
  934. }
  935. if ((index.IsImm() && index.GetImmBits() != 8))
  936. {
  937. ASSERT_MSG(0, "WriteBitTest - illegal argument");
  938. }
  939. if (bits == 16)
  940. Write8(0x66);
  941. if (index.IsImm())
  942. {
  943. dest.WriteRex(this, bits, bits);
  944. Write8(0x0F); Write8(0xBA);
  945. dest.WriteRest(this, 1, (X64Reg)ext);
  946. Write8((u8)index.offset);
  947. }
  948. else
  949. {
  950. X64Reg operand = index.GetSimpleReg();
  951. dest.WriteRex(this, bits, bits, operand);
  952. Write8(0x0F); Write8(0x83 + 8*ext);
  953. dest.WriteRest(this, 1, operand);
  954. }
  955. }
  956. void XEmitter::BT(int bits, const OpArg& dest, const OpArg& index) {WriteBitTest(bits, dest, index, 4);}
  957. void XEmitter::BTS(int bits, const OpArg& dest, const OpArg& index) {WriteBitTest(bits, dest, index, 5);}
  958. void XEmitter::BTR(int bits, const OpArg& dest, const OpArg& index) {WriteBitTest(bits, dest, index, 6);}
  959. void XEmitter::BTC(int bits, const OpArg& dest, const OpArg& index) {WriteBitTest(bits, dest, index, 7);}
  960. //shift can be either imm8 or cl
  961. void XEmitter::SHRD(int bits, const OpArg& dest, const OpArg& src, const OpArg& shift)
  962. {
  963. CheckFlags();
  964. if (dest.IsImm())
  965. {
  966. ASSERT_MSG(0, "SHRD - can't use imms as destination");
  967. }
  968. if (!src.IsSimpleReg())
  969. {
  970. ASSERT_MSG(0, "SHRD - must use simple register as source");
  971. }
  972. if ((shift.IsSimpleReg() && shift.GetSimpleReg() != ECX) || (shift.IsImm() && shift.GetImmBits() != 8))
  973. {
  974. ASSERT_MSG(0, "SHRD - illegal shift");
  975. }
  976. if (bits == 16)
  977. Write8(0x66);
  978. X64Reg operand = src.GetSimpleReg();
  979. dest.WriteRex(this, bits, bits, operand);
  980. if (shift.GetImmBits() == 8)
  981. {
  982. Write8(0x0F); Write8(0xAC);
  983. dest.WriteRest(this, 1, operand);
  984. Write8((u8)shift.offset);
  985. }
  986. else
  987. {
  988. Write8(0x0F); Write8(0xAD);
  989. dest.WriteRest(this, 0, operand);
  990. }
  991. }
  992. void XEmitter::SHLD(int bits, const OpArg& dest, const OpArg& src, const OpArg& shift)
  993. {
  994. CheckFlags();
  995. if (dest.IsImm())
  996. {
  997. ASSERT_MSG(0, "SHLD - can't use imms as destination");
  998. }
  999. if (!src.IsSimpleReg())
  1000. {
  1001. ASSERT_MSG(0, "SHLD - must use simple register as source");
  1002. }
  1003. if ((shift.IsSimpleReg() && shift.GetSimpleReg() != ECX) || (shift.IsImm() && shift.GetImmBits() != 8))
  1004. {
  1005. ASSERT_MSG(0, "SHLD - illegal shift");
  1006. }
  1007. if (bits == 16)
  1008. Write8(0x66);
  1009. X64Reg operand = src.GetSimpleReg();
  1010. dest.WriteRex(this, bits, bits, operand);
  1011. if (shift.GetImmBits() == 8)
  1012. {
  1013. Write8(0x0F); Write8(0xA4);
  1014. dest.WriteRest(this, 1, operand);
  1015. Write8((u8)shift.offset);
  1016. }
  1017. else
  1018. {
  1019. Write8(0x0F); Write8(0xA5);
  1020. dest.WriteRest(this, 0, operand);
  1021. }
  1022. }
  1023. void OpArg::WriteSingleByteOp(XEmitter *emit, u8 op, X64Reg _operandReg, int bits)
  1024. {
  1025. if (bits == 16)
  1026. emit->Write8(0x66);
  1027. this->operandReg = (u8)_operandReg;
  1028. WriteRex(emit, bits, bits);
  1029. emit->Write8(op);
  1030. WriteRest(emit);
  1031. }
  1032. //operand can either be immediate or register
  1033. void OpArg::WriteNormalOp(XEmitter *emit, bool toRM, NormalOp op, const OpArg& operand, int bits) const
  1034. {
  1035. X64Reg _operandReg;
  1036. if (IsImm())
  1037. {
  1038. ASSERT_MSG(0, "WriteNormalOp - Imm argument, wrong order");
  1039. }
  1040. if (bits == 16)
  1041. emit->Write8(0x66);
  1042. int immToWrite = 0;
  1043. if (operand.IsImm())
  1044. {
  1045. WriteRex(emit, bits, bits);
  1046. if (!toRM)
  1047. {
  1048. ASSERT_MSG(0, "WriteNormalOp - Writing to Imm (!toRM)");
  1049. }
  1050. if (operand.scale == SCALE_IMM8 && bits == 8)
  1051. {
  1052. // op al, imm8
  1053. if (!scale && offsetOrBaseReg == AL && normalops[op].eaximm8 != 0xCC)
  1054. {
  1055. emit->Write8(normalops[op].eaximm8);
  1056. emit->Write8((u8)operand.offset);
  1057. return;
  1058. }
  1059. // mov reg, imm8
  1060. if (!scale && op == nrmMOV)
  1061. {
  1062. emit->Write8(0xB0 + (offsetOrBaseReg & 7));
  1063. emit->Write8((u8)operand.offset);
  1064. return;
  1065. }
  1066. // op r/m8, imm8
  1067. emit->Write8(normalops[op].imm8);
  1068. immToWrite = 8;
  1069. }
  1070. else if ((operand.scale == SCALE_IMM16 && bits == 16) ||
  1071. (operand.scale == SCALE_IMM32 && bits == 32) ||
  1072. (operand.scale == SCALE_IMM32 && bits == 64))
  1073. {
  1074. // Try to save immediate size if we can, but first check to see
  1075. // if the instruction supports simm8.
  1076. // op r/m, imm8
  1077. if (normalops[op].simm8 != 0xCC &&
  1078. ((operand.scale == SCALE_IMM16 && (s16)operand.offset == (s8)operand.offset) ||
  1079. (operand.scale == SCALE_IMM32 && (s32)operand.offset == (s8)operand.offset)))
  1080. {
  1081. emit->Write8(normalops[op].simm8);
  1082. immToWrite = 8;
  1083. }
  1084. else
  1085. {
  1086. // mov reg, imm
  1087. if (!scale && op == nrmMOV && bits != 64)
  1088. {
  1089. emit->Write8(0xB8 + (offsetOrBaseReg & 7));
  1090. if (bits == 16)
  1091. emit->Write16((u16)operand.offset);
  1092. else
  1093. emit->Write32((u32)operand.offset);
  1094. return;
  1095. }
  1096. // op eax, imm
  1097. if (!scale && offsetOrBaseReg == EAX && normalops[op].eaximm32 != 0xCC)
  1098. {
  1099. emit->Write8(normalops[op].eaximm32);
  1100. if (bits == 16)
  1101. emit->Write16((u16)operand.offset);
  1102. else
  1103. emit->Write32((u32)operand.offset);
  1104. return;
  1105. }
  1106. // op r/m, imm
  1107. emit->Write8(normalops[op].imm32);
  1108. immToWrite = bits == 16 ? 16 : 32;
  1109. }
  1110. }
  1111. else if ((operand.scale == SCALE_IMM8 && bits == 16) ||
  1112. (operand.scale == SCALE_IMM8 && bits == 32) ||
  1113. (operand.scale == SCALE_IMM8 && bits == 64))
  1114. {
  1115. // op r/m, imm8
  1116. emit->Write8(normalops[op].simm8);
  1117. immToWrite = 8;
  1118. }
  1119. else if (operand.scale == SCALE_IMM64 && bits == 64)
  1120. {
  1121. if (scale)
  1122. {
  1123. ASSERT_MSG(0, "WriteNormalOp - MOV with 64-bit imm requres register destination");
  1124. }
  1125. // mov reg64, imm64
  1126. else if (op == nrmMOV)
  1127. {
  1128. emit->Write8(0xB8 + (offsetOrBaseReg & 7));
  1129. emit->Write64((u64)operand.offset);
  1130. return;
  1131. }
  1132. ASSERT_MSG(0, "WriteNormalOp - Only MOV can take 64-bit imm");
  1133. }
  1134. else
  1135. {
  1136. ASSERT_MSG(0, "WriteNormalOp - Unhandled case");
  1137. }
  1138. _operandReg = (X64Reg)normalops[op].ext; //pass extension in REG of ModRM
  1139. }
  1140. else
  1141. {
  1142. _operandReg = (X64Reg)operand.offsetOrBaseReg;
  1143. WriteRex(emit, bits, bits, _operandReg);
  1144. // op r/m, reg
  1145. if (toRM)
  1146. {
  1147. emit->Write8(bits == 8 ? normalops[op].toRm8 : normalops[op].toRm32);
  1148. }
  1149. // op reg, r/m
  1150. else
  1151. {
  1152. emit->Write8(bits == 8 ? normalops[op].fromRm8 : normalops[op].fromRm32);
  1153. }
  1154. }
  1155. WriteRest(emit, immToWrite >> 3, _operandReg);
  1156. switch (immToWrite)
  1157. {
  1158. case 0:
  1159. break;
  1160. case 8:
  1161. emit->Write8((u8)operand.offset);
  1162. break;
  1163. case 16:
  1164. emit->Write16((u16)operand.offset);
  1165. break;
  1166. case 32:
  1167. emit->Write32((u32)operand.offset);
  1168. break;
  1169. default:
  1170. ASSERT_MSG(0, "WriteNormalOp - Unhandled case");
  1171. }
  1172. }
  1173. void XEmitter::WriteNormalOp(XEmitter *emit, int bits, NormalOp op, const OpArg& a1, const OpArg& a2)
  1174. {
  1175. if (a1.IsImm())
  1176. {
  1177. //Booh! Can't write to an imm
  1178. ASSERT_MSG(0, "WriteNormalOp - a1 cannot be imm");
  1179. return;
  1180. }
  1181. if (a2.IsImm())
  1182. {
  1183. a1.WriteNormalOp(emit, true, op, a2, bits);
  1184. }
  1185. else
  1186. {
  1187. if (a1.IsSimpleReg())
  1188. {
  1189. a2.WriteNormalOp(emit, false, op, a1, bits);
  1190. }
  1191. else
  1192. {
  1193. ASSERT_MSG(a2.IsSimpleReg() || a2.IsImm(), "WriteNormalOp - a1 and a2 cannot both be memory");
  1194. a1.WriteNormalOp(emit, true, op, a2, bits);
  1195. }
  1196. }
  1197. }
  1198. void XEmitter::ADD (int bits, const OpArg& a1, const OpArg& a2) {CheckFlags(); WriteNormalOp(this, bits, nrmADD, a1, a2);}
  1199. void XEmitter::ADC (int bits, const OpArg& a1, const OpArg& a2) {CheckFlags(); WriteNormalOp(this, bits, nrmADC, a1, a2);}
  1200. void XEmitter::SUB (int bits, const OpArg& a1, const OpArg& a2) {CheckFlags(); WriteNormalOp(this, bits, nrmSUB, a1, a2);}
  1201. void XEmitter::SBB (int bits, const OpArg& a1, const OpArg& a2) {CheckFlags(); WriteNormalOp(this, bits, nrmSBB, a1, a2);}
  1202. void XEmitter::AND (int bits, const OpArg& a1, const OpArg& a2) {CheckFlags(); WriteNormalOp(this, bits, nrmAND, a1, a2);}
  1203. void XEmitter::OR (int bits, const OpArg& a1, const OpArg& a2) {CheckFlags(); WriteNormalOp(this, bits, nrmOR , a1, a2);}
  1204. void XEmitter::XOR (int bits, const OpArg& a1, const OpArg& a2) {CheckFlags(); WriteNormalOp(this, bits, nrmXOR, a1, a2);}
  1205. void XEmitter::MOV (int bits, const OpArg& a1, const OpArg& a2)
  1206. {
  1207. if (a1.IsSimpleReg() && a2.IsSimpleReg() && a1.GetSimpleReg() == a2.GetSimpleReg())
  1208. LOG_ERROR(Common, "Redundant MOV @ %p - bug in JIT?", code);
  1209. WriteNormalOp(this, bits, nrmMOV, a1, a2);
  1210. }
  1211. void XEmitter::TEST(int bits, const OpArg& a1, const OpArg& a2) {CheckFlags(); WriteNormalOp(this, bits, nrmTEST, a1, a2);}
  1212. void XEmitter::CMP (int bits, const OpArg& a1, const OpArg& a2) {CheckFlags(); WriteNormalOp(this, bits, nrmCMP, a1, a2);}
  1213. void XEmitter::XCHG(int bits, const OpArg& a1, const OpArg& a2) {WriteNormalOp(this, bits, nrmXCHG, a1, a2);}
  1214. void XEmitter::IMUL(int bits, X64Reg regOp, const OpArg& a1, const OpArg& a2)
  1215. {
  1216. CheckFlags();
  1217. if (bits == 8)
  1218. {
  1219. ASSERT_MSG(0, "IMUL - illegal bit size!");
  1220. return;
  1221. }
  1222. if (a1.IsImm())
  1223. {
  1224. ASSERT_MSG(0, "IMUL - second arg cannot be imm!");
  1225. return;
  1226. }
  1227. if (!a2.IsImm())
  1228. {
  1229. ASSERT_MSG(0, "IMUL - third arg must be imm!");
  1230. return;
  1231. }
  1232. if (bits == 16)
  1233. Write8(0x66);
  1234. a1.WriteRex(this, bits, bits, regOp);
  1235. if (a2.GetImmBits() == 8 ||
  1236. (a2.GetImmBits() == 16 && (s8)a2.offset == (s16)a2.offset) ||
  1237. (a2.GetImmBits() == 32 && (s8)a2.offset == (s32)a2.offset))
  1238. {
  1239. Write8(0x6B);
  1240. a1.WriteRest(this, 1, regOp);
  1241. Write8((u8)a2.offset);
  1242. }
  1243. else
  1244. {
  1245. Write8(0x69);
  1246. if (a2.GetImmBits() == 16 && bits == 16)
  1247. {
  1248. a1.WriteRest(this, 2, regOp);
  1249. Write16((u16)a2.offset);
  1250. }
  1251. else if (a2.GetImmBits() == 32 && (bits == 32 || bits == 64))
  1252. {
  1253. a1.WriteRest(this, 4, regOp);
  1254. Write32((u32)a2.offset);
  1255. }
  1256. else
  1257. {
  1258. ASSERT_MSG(0, "IMUL - unhandled case!");
  1259. }
  1260. }
  1261. }
  1262. void XEmitter::IMUL(int bits, X64Reg regOp, const OpArg& a)
  1263. {
  1264. CheckFlags();
  1265. if (bits == 8)
  1266. {
  1267. ASSERT_MSG(0, "IMUL - illegal bit size!");
  1268. return;
  1269. }
  1270. if (a.IsImm())
  1271. {
  1272. IMUL(bits, regOp, R(regOp), a) ;
  1273. return;
  1274. }
  1275. if (bits == 16)
  1276. Write8(0x66);
  1277. a.WriteRex(this, bits, bits, regOp);
  1278. Write8(0x0F);
  1279. Write8(0xAF);
  1280. a.WriteRest(this, 0, regOp);
  1281. }
  1282. void XEmitter::WriteSSEOp(u8 opPrefix, u16 op, X64Reg regOp, OpArg arg, int extrabytes)
  1283. {
  1284. if (opPrefix)
  1285. Write8(opPrefix);
  1286. arg.operandReg = regOp;
  1287. arg.WriteRex(this, 0, 0);
  1288. Write8(0x0F);
  1289. if (op > 0xFF)
  1290. Write8((op >> 8) & 0xFF);
  1291. Write8(op & 0xFF);
  1292. arg.WriteRest(this, extrabytes);
  1293. }
  1294. void XEmitter::WriteAVXOp(u8 opPrefix, u16 op, X64Reg regOp, const OpArg& arg, int extrabytes)
  1295. {
  1296. WriteAVXOp(opPrefix, op, regOp, INVALID_REG, arg, extrabytes);
  1297. }
  1298. static int GetVEXmmmmm(u16 op)
  1299. {
  1300. // Currently, only 0x38 and 0x3A are used as secondary escape byte.
  1301. if ((op >> 8) == 0x3A)
  1302. return 3;
  1303. if ((op >> 8) == 0x38)
  1304. return 2;
  1305. return 1;
  1306. }
  1307. static int GetVEXpp(u8 opPrefix)
  1308. {
  1309. if (opPrefix == 0x66)
  1310. return 1;
  1311. if (opPrefix == 0xF3)
  1312. return 2;
  1313. if (opPrefix == 0xF2)
  1314. return 3;
  1315. return 0;
  1316. }
  1317. void XEmitter::WriteAVXOp(u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg, int extrabytes)
  1318. {
  1319. if (!Common::GetCPUCaps().avx)
  1320. ASSERT_MSG(0, "Trying to use AVX on a system that doesn't support it. Bad programmer.");
  1321. int mmmmm = GetVEXmmmmm(op);
  1322. int pp = GetVEXpp(opPrefix);
  1323. // FIXME: we currently don't support 256-bit instructions, and "size" is not the vector size here
  1324. arg.WriteVex(this, regOp1, regOp2, 0, pp, mmmmm);
  1325. Write8(op & 0xFF);
  1326. arg.WriteRest(this, extrabytes, regOp1);
  1327. }
  1328. // Like the above, but more general; covers GPR-based VEX operations, like BMI1/2
  1329. void XEmitter::WriteVEXOp(int size, u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg, int extrabytes)
  1330. {
  1331. if (size != 32 && size != 64)
  1332. ASSERT_MSG(0, "VEX GPR instructions only support 32-bit and 64-bit modes!");
  1333. int mmmmm = GetVEXmmmmm(op);
  1334. int pp = GetVEXpp(opPrefix);
  1335. arg.WriteVex(this, regOp1, regOp2, 0, pp, mmmmm, size == 64);
  1336. Write8(op & 0xFF);
  1337. arg.WriteRest(this, extrabytes, regOp1);
  1338. }
  1339. void XEmitter::WriteBMI1Op(int size, u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg, int extrabytes)
  1340. {
  1341. CheckFlags();
  1342. if (!Common::GetCPUCaps().bmi1)
  1343. ASSERT_MSG(0, "Trying to use BMI1 on a system that doesn't support it. Bad programmer.");
  1344. WriteVEXOp(size, opPrefix, op, regOp1, regOp2, arg, extrabytes);
  1345. }
  1346. void XEmitter::WriteBMI2Op(int size, u8 opPrefix, u16 op, X64Reg regOp1, X64Reg regOp2, const OpArg& arg, int extrabytes)
  1347. {
  1348. CheckFlags();
  1349. if (!Common::GetCPUCaps().bmi2)
  1350. ASSERT_MSG(0, "Trying to use BMI2 on a system that doesn't support it. Bad programmer.");
  1351. WriteVEXOp(size, opPrefix, op, regOp1, regOp2, arg, extrabytes);
  1352. }
  1353. void XEmitter::MOVD_xmm(X64Reg dest, const OpArg &arg) {WriteSSEOp(0x66, 0x6E, dest, arg, 0);}
  1354. void XEmitter::MOVD_xmm(const OpArg &arg, X64Reg src) {WriteSSEOp(0x66, 0x7E, src, arg, 0);}
  1355. void XEmitter::MOVQ_xmm(X64Reg dest, OpArg arg)
  1356. {
  1357. #ifdef ARCHITECTURE_x86_64
  1358. // Alternate encoding
  1359. // This does not display correctly in MSVC's debugger, it thinks it's a MOVD
  1360. arg.operandReg = dest;
  1361. Write8(0x66);
  1362. arg.WriteRex(this, 64, 0);
  1363. Write8(0x0f);
  1364. Write8(0x6E);
  1365. arg.WriteRest(this, 0);
  1366. #else
  1367. arg.operandReg = dest;
  1368. Write8(0xF3);
  1369. Write8(0x0f);
  1370. Write8(0x7E);
  1371. arg.WriteRest(this, 0);
  1372. #endif
  1373. }
  1374. void XEmitter::MOVQ_xmm(OpArg arg, X64Reg src)
  1375. {
  1376. if (src > 7 || arg.IsSimpleReg())
  1377. {
  1378. // Alternate encoding
  1379. // This does not display correctly in MSVC's debugger, it thinks it's a MOVD
  1380. arg.operandReg = src;
  1381. Write8(0x66);
  1382. arg.WriteRex(this, 64, 0);
  1383. Write8(0x0f);
  1384. Write8(0x7E);
  1385. arg.WriteRest(this, 0);
  1386. }
  1387. else
  1388. {
  1389. arg.operandReg = src;
  1390. arg.WriteRex(this, 0, 0);
  1391. Write8(0x66);
  1392. Write8(0x0f);
  1393. Write8(0xD6);
  1394. arg.WriteRest(this, 0);
  1395. }
  1396. }
  1397. void XEmitter::WriteMXCSR(OpArg arg, int ext)
  1398. {
  1399. if (arg.IsImm() || arg.IsSimpleReg())
  1400. ASSERT_MSG(0, "MXCSR - invalid operand");
  1401. arg.operandReg = ext;
  1402. arg.WriteRex(this, 0, 0);
  1403. Write8(0x0F);
  1404. Write8(0xAE);
  1405. arg.WriteRest(this);
  1406. }
  1407. void XEmitter::STMXCSR(const OpArg& memloc) {WriteMXCSR(memloc, 3);}
  1408. void XEmitter::LDMXCSR(const OpArg& memloc) {WriteMXCSR(memloc, 2);}
  1409. void XEmitter::MOVNTDQ(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0x66, sseMOVNTDQ, regOp, arg);}
  1410. void XEmitter::MOVNTPS(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0x00, sseMOVNTP, regOp, arg);}
  1411. void XEmitter::MOVNTPD(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0x66, sseMOVNTP, regOp, arg);}
  1412. void XEmitter::ADDSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseADD, regOp, arg);}
  1413. void XEmitter::ADDSD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, sseADD, regOp, arg);}
  1414. void XEmitter::SUBSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseSUB, regOp, arg);}
  1415. void XEmitter::SUBSD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, sseSUB, regOp, arg);}
  1416. void XEmitter::CMPSS(X64Reg regOp, const OpArg& arg, u8 compare) {WriteSSEOp(0xF3, sseCMP, regOp, arg, 1); Write8(compare);}
  1417. void XEmitter::CMPSD(X64Reg regOp, const OpArg& arg, u8 compare) {WriteSSEOp(0xF2, sseCMP, regOp, arg, 1); Write8(compare);}
  1418. void XEmitter::MULSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseMUL, regOp, arg);}
  1419. void XEmitter::MULSD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, sseMUL, regOp, arg);}
  1420. void XEmitter::DIVSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseDIV, regOp, arg);}
  1421. void XEmitter::DIVSD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, sseDIV, regOp, arg);}
  1422. void XEmitter::MINSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseMIN, regOp, arg);}
  1423. void XEmitter::MINSD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, sseMIN, regOp, arg);}
  1424. void XEmitter::MAXSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseMAX, regOp, arg);}
  1425. void XEmitter::MAXSD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, sseMAX, regOp, arg);}
  1426. void XEmitter::SQRTSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseSQRT, regOp, arg);}
  1427. void XEmitter::SQRTSD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, sseSQRT, regOp, arg);}
  1428. void XEmitter::RCPSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseRCP, regOp, arg);}
  1429. void XEmitter::RSQRTSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseRSQRT, regOp, arg);}
  1430. void XEmitter::ADDPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseADD, regOp, arg);}
  1431. void XEmitter::ADDPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseADD, regOp, arg);}
  1432. void XEmitter::SUBPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseSUB, regOp, arg);}
  1433. void XEmitter::SUBPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseSUB, regOp, arg);}
  1434. void XEmitter::CMPPS(X64Reg regOp, const OpArg& arg, u8 compare) {WriteSSEOp(0x00, sseCMP, regOp, arg, 1); Write8(compare);}
  1435. void XEmitter::CMPPD(X64Reg regOp, const OpArg& arg, u8 compare) {WriteSSEOp(0x66, sseCMP, regOp, arg, 1); Write8(compare);}
  1436. void XEmitter::ANDPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseAND, regOp, arg);}
  1437. void XEmitter::ANDPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseAND, regOp, arg);}
  1438. void XEmitter::ANDNPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseANDN, regOp, arg);}
  1439. void XEmitter::ANDNPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseANDN, regOp, arg);}
  1440. void XEmitter::ORPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseOR, regOp, arg);}
  1441. void XEmitter::ORPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseOR, regOp, arg);}
  1442. void XEmitter::XORPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseXOR, regOp, arg);}
  1443. void XEmitter::XORPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseXOR, regOp, arg);}
  1444. void XEmitter::MULPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseMUL, regOp, arg);}
  1445. void XEmitter::MULPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseMUL, regOp, arg);}
  1446. void XEmitter::DIVPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseDIV, regOp, arg);}
  1447. void XEmitter::DIVPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseDIV, regOp, arg);}
  1448. void XEmitter::MINPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseMIN, regOp, arg);}
  1449. void XEmitter::MINPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseMIN, regOp, arg);}
  1450. void XEmitter::MAXPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseMAX, regOp, arg);}
  1451. void XEmitter::MAXPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseMAX, regOp, arg);}
  1452. void XEmitter::SQRTPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseSQRT, regOp, arg);}
  1453. void XEmitter::SQRTPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseSQRT, regOp, arg);}
  1454. void XEmitter::RCPPS(X64Reg regOp, const OpArg& arg) { WriteSSEOp(0x00, sseRCP, regOp, arg); }
  1455. void XEmitter::RSQRTPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseRSQRT, regOp, arg);}
  1456. void XEmitter::SHUFPS(X64Reg regOp, const OpArg& arg, u8 shuffle) {WriteSSEOp(0x00, sseSHUF, regOp, arg,1); Write8(shuffle);}
  1457. void XEmitter::SHUFPD(X64Reg regOp, const OpArg& arg, u8 shuffle) {WriteSSEOp(0x66, sseSHUF, regOp, arg,1); Write8(shuffle);}
  1458. void XEmitter::HADDPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, sseHADD, regOp, arg);}
  1459. void XEmitter::COMISS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseCOMIS, regOp, arg);} //weird that these should be packed
  1460. void XEmitter::COMISD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseCOMIS, regOp, arg);} //ordered
  1461. void XEmitter::UCOMISS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseUCOMIS, regOp, arg);} //unordered
  1462. void XEmitter::UCOMISD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseUCOMIS, regOp, arg);}
  1463. void XEmitter::MOVAPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseMOVAPfromRM, regOp, arg);}
  1464. void XEmitter::MOVAPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseMOVAPfromRM, regOp, arg);}
  1465. void XEmitter::MOVAPS(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0x00, sseMOVAPtoRM, regOp, arg);}
  1466. void XEmitter::MOVAPD(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0x66, sseMOVAPtoRM, regOp, arg);}
  1467. void XEmitter::MOVUPS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, sseMOVUPfromRM, regOp, arg);}
  1468. void XEmitter::MOVUPD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseMOVUPfromRM, regOp, arg);}
  1469. void XEmitter::MOVUPS(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0x00, sseMOVUPtoRM, regOp, arg);}
  1470. void XEmitter::MOVUPD(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0x66, sseMOVUPtoRM, regOp, arg);}
  1471. void XEmitter::MOVDQA(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, sseMOVDQfromRM, regOp, arg);}
  1472. void XEmitter::MOVDQA(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0x66, sseMOVDQtoRM, regOp, arg);}
  1473. void XEmitter::MOVDQU(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseMOVDQfromRM, regOp, arg);}
  1474. void XEmitter::MOVDQU(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0xF3, sseMOVDQtoRM, regOp, arg);}
  1475. void XEmitter::MOVSS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, sseMOVUPfromRM, regOp, arg);}
  1476. void XEmitter::MOVSD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, sseMOVUPfromRM, regOp, arg);}
  1477. void XEmitter::MOVSS(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0xF3, sseMOVUPtoRM, regOp, arg);}
  1478. void XEmitter::MOVSD(const OpArg& arg, X64Reg regOp) {WriteSSEOp(0xF2, sseMOVUPtoRM, regOp, arg);}
  1479. void XEmitter::MOVLPS(X64Reg regOp, const OpArg& arg) { WriteSSEOp(0x00, sseMOVLPfromRM, regOp, arg); }
  1480. void XEmitter::MOVLPD(X64Reg regOp, const OpArg& arg) { WriteSSEOp(0x66, sseMOVLPfromRM, regOp, arg); }
  1481. void XEmitter::MOVLPS(const OpArg& arg, X64Reg regOp) { WriteSSEOp(0x00, sseMOVLPtoRM, regOp, arg); }
  1482. void XEmitter::MOVLPD(const OpArg& arg, X64Reg regOp) { WriteSSEOp(0x66, sseMOVLPtoRM, regOp, arg); }
  1483. void XEmitter::MOVHPS(X64Reg regOp, const OpArg& arg) { WriteSSEOp(0x00, sseMOVHPfromRM, regOp, arg); }
  1484. void XEmitter::MOVHPD(X64Reg regOp, const OpArg& arg) { WriteSSEOp(0x66, sseMOVHPfromRM, regOp, arg); }
  1485. void XEmitter::MOVHPS(const OpArg& arg, X64Reg regOp) { WriteSSEOp(0x00, sseMOVHPtoRM, regOp, arg); }
  1486. void XEmitter::MOVHPD(const OpArg& arg, X64Reg regOp) { WriteSSEOp(0x66, sseMOVHPtoRM, regOp, arg); }
  1487. void XEmitter::MOVHLPS(X64Reg regOp1, X64Reg regOp2) {WriteSSEOp(0x00, sseMOVHLPS, regOp1, R(regOp2));}
  1488. void XEmitter::MOVLHPS(X64Reg regOp1, X64Reg regOp2) {WriteSSEOp(0x00, sseMOVLHPS, regOp1, R(regOp2));}
  1489. void XEmitter::CVTPS2PD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, 0x5A, regOp, arg);}
  1490. void XEmitter::CVTPD2PS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, 0x5A, regOp, arg);}
  1491. void XEmitter::CVTSD2SS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, 0x5A, regOp, arg);}
  1492. void XEmitter::CVTSS2SD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, 0x5A, regOp, arg);}
  1493. void XEmitter::CVTSD2SI(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, 0x2D, regOp, arg);}
  1494. void XEmitter::CVTSS2SI(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, 0x2D, regOp, arg);}
  1495. void XEmitter::CVTSI2SD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, 0x2A, regOp, arg);}
  1496. void XEmitter::CVTSI2SS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, 0x2A, regOp, arg);}
  1497. void XEmitter::CVTDQ2PD(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, 0xE6, regOp, arg);}
  1498. void XEmitter::CVTDQ2PS(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x00, 0x5B, regOp, arg);}
  1499. void XEmitter::CVTPD2DQ(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, 0xE6, regOp, arg);}
  1500. void XEmitter::CVTPS2DQ(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, 0x5B, regOp, arg);}
  1501. void XEmitter::CVTTSD2SI(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF2, 0x2C, regOp, arg);}
  1502. void XEmitter::CVTTSS2SI(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, 0x2C, regOp, arg);}
  1503. void XEmitter::CVTTPS2DQ(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0xF3, 0x5B, regOp, arg);}
  1504. void XEmitter::CVTTPD2DQ(X64Reg regOp, const OpArg& arg) {WriteSSEOp(0x66, 0xE6, regOp, arg);}
  1505. void XEmitter::MASKMOVDQU(X64Reg dest, X64Reg src) {WriteSSEOp(0x66, sseMASKMOVDQU, dest, R(src));}
  1506. void XEmitter::MOVMSKPS(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x00, 0x50, dest, arg);}
  1507. void XEmitter::MOVMSKPD(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x50, dest, arg);}
  1508. void XEmitter::LDDQU(X64Reg dest, const OpArg& arg) {WriteSSEOp(0xF2, sseLDDQU, dest, arg);} // For integer data only
  1509. // THESE TWO ARE UNTESTED.
  1510. void XEmitter::UNPCKLPS(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x00, 0x14, dest, arg);}
  1511. void XEmitter::UNPCKHPS(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x00, 0x15, dest, arg);}
  1512. void XEmitter::UNPCKLPD(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x14, dest, arg);}
  1513. void XEmitter::UNPCKHPD(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x15, dest, arg);}
  1514. void XEmitter::MOVDDUP(X64Reg regOp, const OpArg& arg)
  1515. {
  1516. if (Common::GetCPUCaps().sse3)
  1517. {
  1518. WriteSSEOp(0xF2, 0x12, regOp, arg); //SSE3 movddup
  1519. }
  1520. else
  1521. {
  1522. // Simulate this instruction with SSE2 instructions
  1523. if (!arg.IsSimpleReg(regOp))
  1524. MOVSD(regOp, arg);
  1525. UNPCKLPD(regOp, R(regOp));
  1526. }
  1527. }
  1528. //There are a few more left
  1529. // Also some integer instructions are missing
  1530. void XEmitter::PACKSSDW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x6B, dest, arg);}
  1531. void XEmitter::PACKSSWB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x63, dest, arg);}
  1532. void XEmitter::PACKUSWB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x67, dest, arg);}
  1533. void XEmitter::PUNPCKLBW(X64Reg dest, const OpArg &arg) {WriteSSEOp(0x66, 0x60, dest, arg);}
  1534. void XEmitter::PUNPCKLWD(X64Reg dest, const OpArg &arg) {WriteSSEOp(0x66, 0x61, dest, arg);}
  1535. void XEmitter::PUNPCKLDQ(X64Reg dest, const OpArg &arg) {WriteSSEOp(0x66, 0x62, dest, arg);}
  1536. void XEmitter::PUNPCKLQDQ(X64Reg dest, const OpArg &arg) {WriteSSEOp(0x66, 0x6C, dest, arg);}
  1537. void XEmitter::PSRLW(X64Reg reg, int shift)
  1538. {
  1539. WriteSSEOp(0x66, 0x71, (X64Reg)2, R(reg));
  1540. Write8(shift);
  1541. }
  1542. void XEmitter::PSRLD(X64Reg reg, int shift)
  1543. {
  1544. WriteSSEOp(0x66, 0x72, (X64Reg)2, R(reg));
  1545. Write8(shift);
  1546. }
  1547. void XEmitter::PSRLQ(X64Reg reg, int shift)
  1548. {
  1549. WriteSSEOp(0x66, 0x73, (X64Reg)2, R(reg));
  1550. Write8(shift);
  1551. }
  1552. void XEmitter::PSRLQ(X64Reg reg, const OpArg& arg)
  1553. {
  1554. WriteSSEOp(0x66, 0xd3, reg, arg);
  1555. }
  1556. void XEmitter::PSRLDQ(X64Reg reg, int shift) {
  1557. WriteSSEOp(0x66, 0x73, (X64Reg)3, R(reg));
  1558. Write8(shift);
  1559. }
  1560. void XEmitter::PSLLW(X64Reg reg, int shift)
  1561. {
  1562. WriteSSEOp(0x66, 0x71, (X64Reg)6, R(reg));
  1563. Write8(shift);
  1564. }
  1565. void XEmitter::PSLLD(X64Reg reg, int shift)
  1566. {
  1567. WriteSSEOp(0x66, 0x72, (X64Reg)6, R(reg));
  1568. Write8(shift);
  1569. }
  1570. void XEmitter::PSLLQ(X64Reg reg, int shift)
  1571. {
  1572. WriteSSEOp(0x66, 0x73, (X64Reg)6, R(reg));
  1573. Write8(shift);
  1574. }
  1575. void XEmitter::PSLLDQ(X64Reg reg, int shift) {
  1576. WriteSSEOp(0x66, 0x73, (X64Reg)7, R(reg));
  1577. Write8(shift);
  1578. }
  1579. void XEmitter::PSRAW(X64Reg reg, int shift)
  1580. {
  1581. WriteSSEOp(0x66, 0x71, (X64Reg)4, R(reg));
  1582. Write8(shift);
  1583. }
  1584. void XEmitter::PSRAD(X64Reg reg, int shift)
  1585. {
  1586. WriteSSEOp(0x66, 0x72, (X64Reg)4, R(reg));
  1587. Write8(shift);
  1588. }
  1589. void XEmitter::WriteSSSE3Op(u8 opPrefix, u16 op, X64Reg regOp, const OpArg& arg, int extrabytes)
  1590. {
  1591. if (!Common::GetCPUCaps().ssse3)
  1592. ASSERT_MSG(0, "Trying to use SSSE3 on a system that doesn't support it. Bad programmer.");
  1593. WriteSSEOp(opPrefix, op, regOp, arg, extrabytes);
  1594. }
  1595. void XEmitter::WriteSSE41Op(u8 opPrefix, u16 op, X64Reg regOp, const OpArg& arg, int extrabytes)
  1596. {
  1597. if (!Common::GetCPUCaps().sse4_1)
  1598. ASSERT_MSG(0, "Trying to use SSE4.1 on a system that doesn't support it. Bad programmer.");
  1599. WriteSSEOp(opPrefix, op, regOp, arg, extrabytes);
  1600. }
  1601. void XEmitter::PSHUFB(X64Reg dest, const OpArg& arg) {WriteSSSE3Op(0x66, 0x3800, dest, arg);}
  1602. void XEmitter::PTEST(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3817, dest, arg);}
  1603. void XEmitter::PACKUSDW(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x382b, dest, arg);}
  1604. void XEmitter::DPPS(X64Reg dest, const OpArg& arg, u8 mask) {WriteSSE41Op(0x66, 0x3A40, dest, arg, 1); Write8(mask);}
  1605. void XEmitter::PMINSB(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3838, dest, arg);}
  1606. void XEmitter::PMINSD(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3839, dest, arg);}
  1607. void XEmitter::PMINUW(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x383a, dest, arg);}
  1608. void XEmitter::PMINUD(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x383b, dest, arg);}
  1609. void XEmitter::PMAXSB(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x383c, dest, arg);}
  1610. void XEmitter::PMAXSD(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x383d, dest, arg);}
  1611. void XEmitter::PMAXUW(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x383e, dest, arg);}
  1612. void XEmitter::PMAXUD(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x383f, dest, arg);}
  1613. void XEmitter::PMOVSXBW(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3820, dest, arg);}
  1614. void XEmitter::PMOVSXBD(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3821, dest, arg);}
  1615. void XEmitter::PMOVSXBQ(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3822, dest, arg);}
  1616. void XEmitter::PMOVSXWD(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3823, dest, arg);}
  1617. void XEmitter::PMOVSXWQ(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3824, dest, arg);}
  1618. void XEmitter::PMOVSXDQ(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3825, dest, arg);}
  1619. void XEmitter::PMOVZXBW(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3830, dest, arg);}
  1620. void XEmitter::PMOVZXBD(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3831, dest, arg);}
  1621. void XEmitter::PMOVZXBQ(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3832, dest, arg);}
  1622. void XEmitter::PMOVZXWD(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3833, dest, arg);}
  1623. void XEmitter::PMOVZXWQ(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3834, dest, arg);}
  1624. void XEmitter::PMOVZXDQ(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3835, dest, arg);}
  1625. void XEmitter::PBLENDVB(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3810, dest, arg);}
  1626. void XEmitter::BLENDVPS(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3814, dest, arg);}
  1627. void XEmitter::BLENDVPD(X64Reg dest, const OpArg& arg) {WriteSSE41Op(0x66, 0x3815, dest, arg);}
  1628. void XEmitter::BLENDPS(X64Reg dest, const OpArg& arg, u8 blend) { WriteSSE41Op(0x66, 0x3A0C, dest, arg, 1); Write8(blend); }
  1629. void XEmitter::BLENDPD(X64Reg dest, const OpArg& arg, u8 blend) { WriteSSE41Op(0x66, 0x3A0D, dest, arg, 1); Write8(blend); }
  1630. void XEmitter::ROUNDSS(X64Reg dest, const OpArg& arg, u8 mode) {WriteSSE41Op(0x66, 0x3A0A, dest, arg, 1); Write8(mode);}
  1631. void XEmitter::ROUNDSD(X64Reg dest, const OpArg& arg, u8 mode) {WriteSSE41Op(0x66, 0x3A0B, dest, arg, 1); Write8(mode);}
  1632. void XEmitter::ROUNDPS(X64Reg dest, const OpArg& arg, u8 mode) {WriteSSE41Op(0x66, 0x3A08, dest, arg, 1); Write8(mode);}
  1633. void XEmitter::ROUNDPD(X64Reg dest, const OpArg& arg, u8 mode) {WriteSSE41Op(0x66, 0x3A09, dest, arg, 1); Write8(mode);}
  1634. void XEmitter::PAND(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xDB, dest, arg);}
  1635. void XEmitter::PANDN(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xDF, dest, arg);}
  1636. void XEmitter::PXOR(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xEF, dest, arg);}
  1637. void XEmitter::POR(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xEB, dest, arg);}
  1638. void XEmitter::PADDB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xFC, dest, arg);}
  1639. void XEmitter::PADDW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xFD, dest, arg);}
  1640. void XEmitter::PADDD(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xFE, dest, arg);}
  1641. void XEmitter::PADDQ(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xD4, dest, arg);}
  1642. void XEmitter::PADDSB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xEC, dest, arg);}
  1643. void XEmitter::PADDSW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xED, dest, arg);}
  1644. void XEmitter::PADDUSB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xDC, dest, arg);}
  1645. void XEmitter::PADDUSW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xDD, dest, arg);}
  1646. void XEmitter::PSUBB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xF8, dest, arg);}
  1647. void XEmitter::PSUBW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xF9, dest, arg);}
  1648. void XEmitter::PSUBD(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xFA, dest, arg);}
  1649. void XEmitter::PSUBQ(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xFB, dest, arg);}
  1650. void XEmitter::PSUBSB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xE8, dest, arg);}
  1651. void XEmitter::PSUBSW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xE9, dest, arg);}
  1652. void XEmitter::PSUBUSB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xD8, dest, arg);}
  1653. void XEmitter::PSUBUSW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xD9, dest, arg);}
  1654. void XEmitter::PAVGB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xE0, dest, arg);}
  1655. void XEmitter::PAVGW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xE3, dest, arg);}
  1656. void XEmitter::PCMPEQB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x74, dest, arg);}
  1657. void XEmitter::PCMPEQW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x75, dest, arg);}
  1658. void XEmitter::PCMPEQD(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x76, dest, arg);}
  1659. void XEmitter::PCMPGTB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x64, dest, arg);}
  1660. void XEmitter::PCMPGTW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x65, dest, arg);}
  1661. void XEmitter::PCMPGTD(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0x66, dest, arg);}
  1662. void XEmitter::PEXTRW(X64Reg dest, const OpArg& arg, u8 subreg) {WriteSSEOp(0x66, 0xC5, dest, arg, 1); Write8(subreg);}
  1663. void XEmitter::PINSRW(X64Reg dest, const OpArg& arg, u8 subreg) {WriteSSEOp(0x66, 0xC4, dest, arg, 1); Write8(subreg);}
  1664. void XEmitter::PMADDWD(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xF5, dest, arg); }
  1665. void XEmitter::PSADBW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xF6, dest, arg);}
  1666. void XEmitter::PMAXSW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xEE, dest, arg); }
  1667. void XEmitter::PMAXUB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xDE, dest, arg); }
  1668. void XEmitter::PMINSW(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xEA, dest, arg); }
  1669. void XEmitter::PMINUB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xDA, dest, arg); }
  1670. void XEmitter::PMOVMSKB(X64Reg dest, const OpArg& arg) {WriteSSEOp(0x66, 0xD7, dest, arg); }
  1671. void XEmitter::PSHUFD(X64Reg regOp, const OpArg& arg, u8 shuffle) {WriteSSEOp(0x66, 0x70, regOp, arg, 1); Write8(shuffle);}
  1672. void XEmitter::PSHUFLW(X64Reg regOp, const OpArg& arg, u8 shuffle) {WriteSSEOp(0xF2, 0x70, regOp, arg, 1); Write8(shuffle);}
  1673. void XEmitter::PSHUFHW(X64Reg regOp, const OpArg& arg, u8 shuffle) {WriteSSEOp(0xF3, 0x70, regOp, arg, 1); Write8(shuffle);}
  1674. // VEX
  1675. void XEmitter::VADDSD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteAVXOp(0xF2, sseADD, regOp1, regOp2, arg);}
  1676. void XEmitter::VSUBSD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteAVXOp(0xF2, sseSUB, regOp1, regOp2, arg);}
  1677. void XEmitter::VMULSD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteAVXOp(0xF2, sseMUL, regOp1, regOp2, arg);}
  1678. void XEmitter::VDIVSD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteAVXOp(0xF2, sseDIV, regOp1, regOp2, arg);}
  1679. void XEmitter::VADDPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteAVXOp(0x66, sseADD, regOp1, regOp2, arg);}
  1680. void XEmitter::VSUBPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteAVXOp(0x66, sseSUB, regOp1, regOp2, arg);}
  1681. void XEmitter::VMULPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteAVXOp(0x66, sseMUL, regOp1, regOp2, arg);}
  1682. void XEmitter::VDIVPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteAVXOp(0x66, sseDIV, regOp1, regOp2, arg);}
  1683. void XEmitter::VSQRTSD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteAVXOp(0xF2, sseSQRT, regOp1, regOp2, arg);}
  1684. void XEmitter::VSHUFPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg, u8 shuffle) {WriteAVXOp(0x66, sseSHUF, regOp1, regOp2, arg, 1); Write8(shuffle);}
  1685. void XEmitter::VUNPCKLPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg){WriteAVXOp(0x66, 0x14, regOp1, regOp2, arg);}
  1686. void XEmitter::VUNPCKHPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg){WriteAVXOp(0x66, 0x15, regOp1, regOp2, arg);}
  1687. void XEmitter::VANDPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x00, sseAND, regOp1, regOp2, arg); }
  1688. void XEmitter::VANDPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, sseAND, regOp1, regOp2, arg); }
  1689. void XEmitter::VANDNPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x00, sseANDN, regOp1, regOp2, arg); }
  1690. void XEmitter::VANDNPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, sseANDN, regOp1, regOp2, arg); }
  1691. void XEmitter::VORPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x00, sseOR, regOp1, regOp2, arg); }
  1692. void XEmitter::VORPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, sseOR, regOp1, regOp2, arg); }
  1693. void XEmitter::VXORPS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x00, sseXOR, regOp1, regOp2, arg); }
  1694. void XEmitter::VXORPD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, sseXOR, regOp1, regOp2, arg); }
  1695. void XEmitter::VPAND(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0xDB, regOp1, regOp2, arg); }
  1696. void XEmitter::VPANDN(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0xDF, regOp1, regOp2, arg); }
  1697. void XEmitter::VPOR(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0xEB, regOp1, regOp2, arg); }
  1698. void XEmitter::VPXOR(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0xEF, regOp1, regOp2, arg); }
  1699. void XEmitter::VFMADD132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x3898, regOp1, regOp2, arg); }
  1700. void XEmitter::VFMADD213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38A8, regOp1, regOp2, arg); }
  1701. void XEmitter::VFMADD231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38B8, regOp1, regOp2, arg); }
  1702. void XEmitter::VFMADD132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x3898, regOp1, regOp2, arg, 1); }
  1703. void XEmitter::VFMADD213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38A8, regOp1, regOp2, arg, 1); }
  1704. void XEmitter::VFMADD231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38B8, regOp1, regOp2, arg, 1); }
  1705. void XEmitter::VFMADD132SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x3899, regOp1, regOp2, arg); }
  1706. void XEmitter::VFMADD213SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38A9, regOp1, regOp2, arg); }
  1707. void XEmitter::VFMADD231SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38B9, regOp1, regOp2, arg); }
  1708. void XEmitter::VFMADD132SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x3899, regOp1, regOp2, arg, 1); }
  1709. void XEmitter::VFMADD213SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38A9, regOp1, regOp2, arg, 1); }
  1710. void XEmitter::VFMADD231SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38B9, regOp1, regOp2, arg, 1); }
  1711. void XEmitter::VFMSUB132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389A, regOp1, regOp2, arg); }
  1712. void XEmitter::VFMSUB213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AA, regOp1, regOp2, arg); }
  1713. void XEmitter::VFMSUB231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BA, regOp1, regOp2, arg); }
  1714. void XEmitter::VFMSUB132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389A, regOp1, regOp2, arg, 1); }
  1715. void XEmitter::VFMSUB213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AA, regOp1, regOp2, arg, 1); }
  1716. void XEmitter::VFMSUB231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BA, regOp1, regOp2, arg, 1); }
  1717. void XEmitter::VFMSUB132SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389B, regOp1, regOp2, arg); }
  1718. void XEmitter::VFMSUB213SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AB, regOp1, regOp2, arg); }
  1719. void XEmitter::VFMSUB231SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BB, regOp1, regOp2, arg); }
  1720. void XEmitter::VFMSUB132SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389B, regOp1, regOp2, arg, 1); }
  1721. void XEmitter::VFMSUB213SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AB, regOp1, regOp2, arg, 1); }
  1722. void XEmitter::VFMSUB231SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BB, regOp1, regOp2, arg, 1); }
  1723. void XEmitter::VFNMADD132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389C, regOp1, regOp2, arg); }
  1724. void XEmitter::VFNMADD213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AC, regOp1, regOp2, arg); }
  1725. void XEmitter::VFNMADD231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BC, regOp1, regOp2, arg); }
  1726. void XEmitter::VFNMADD132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389C, regOp1, regOp2, arg, 1); }
  1727. void XEmitter::VFNMADD213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AC, regOp1, regOp2, arg, 1); }
  1728. void XEmitter::VFNMADD231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BC, regOp1, regOp2, arg, 1); }
  1729. void XEmitter::VFNMADD132SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389D, regOp1, regOp2, arg); }
  1730. void XEmitter::VFNMADD213SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AD, regOp1, regOp2, arg); }
  1731. void XEmitter::VFNMADD231SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BD, regOp1, regOp2, arg); }
  1732. void XEmitter::VFNMADD132SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389D, regOp1, regOp2, arg, 1); }
  1733. void XEmitter::VFNMADD213SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AD, regOp1, regOp2, arg, 1); }
  1734. void XEmitter::VFNMADD231SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BD, regOp1, regOp2, arg, 1); }
  1735. void XEmitter::VFNMSUB132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389E, regOp1, regOp2, arg); }
  1736. void XEmitter::VFNMSUB213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AE, regOp1, regOp2, arg); }
  1737. void XEmitter::VFNMSUB231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BE, regOp1, regOp2, arg); }
  1738. void XEmitter::VFNMSUB132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389E, regOp1, regOp2, arg, 1); }
  1739. void XEmitter::VFNMSUB213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AE, regOp1, regOp2, arg, 1); }
  1740. void XEmitter::VFNMSUB231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BE, regOp1, regOp2, arg, 1); }
  1741. void XEmitter::VFNMSUB132SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389F, regOp1, regOp2, arg); }
  1742. void XEmitter::VFNMSUB213SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AF, regOp1, regOp2, arg); }
  1743. void XEmitter::VFNMSUB231SS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BF, regOp1, regOp2, arg); }
  1744. void XEmitter::VFNMSUB132SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x389F, regOp1, regOp2, arg, 1); }
  1745. void XEmitter::VFNMSUB213SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38AF, regOp1, regOp2, arg, 1); }
  1746. void XEmitter::VFNMSUB231SD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38BF, regOp1, regOp2, arg, 1); }
  1747. void XEmitter::VFMADDSUB132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x3896, regOp1, regOp2, arg); }
  1748. void XEmitter::VFMADDSUB213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38A6, regOp1, regOp2, arg); }
  1749. void XEmitter::VFMADDSUB231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38B6, regOp1, regOp2, arg); }
  1750. void XEmitter::VFMADDSUB132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x3896, regOp1, regOp2, arg, 1); }
  1751. void XEmitter::VFMADDSUB213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38A6, regOp1, regOp2, arg, 1); }
  1752. void XEmitter::VFMADDSUB231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38B6, regOp1, regOp2, arg, 1); }
  1753. void XEmitter::VFMSUBADD132PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x3897, regOp1, regOp2, arg); }
  1754. void XEmitter::VFMSUBADD213PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38A7, regOp1, regOp2, arg); }
  1755. void XEmitter::VFMSUBADD231PS(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38B7, regOp1, regOp2, arg); }
  1756. void XEmitter::VFMSUBADD132PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x3897, regOp1, regOp2, arg, 1); }
  1757. void XEmitter::VFMSUBADD213PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38A7, regOp1, regOp2, arg, 1); }
  1758. void XEmitter::VFMSUBADD231PD(X64Reg regOp1, X64Reg regOp2, const OpArg& arg) { WriteAVXOp(0x66, 0x38B7, regOp1, regOp2, arg, 1); }
  1759. void XEmitter::SARX(int bits, X64Reg regOp1, const OpArg& arg, X64Reg regOp2) {WriteBMI2Op(bits, 0xF3, 0x38F7, regOp1, regOp2, arg);}
  1760. void XEmitter::SHLX(int bits, X64Reg regOp1, const OpArg& arg, X64Reg regOp2) {WriteBMI2Op(bits, 0x66, 0x38F7, regOp1, regOp2, arg);}
  1761. void XEmitter::SHRX(int bits, X64Reg regOp1, const OpArg& arg, X64Reg regOp2) {WriteBMI2Op(bits, 0xF2, 0x38F7, regOp1, regOp2, arg);}
  1762. void XEmitter::RORX(int bits, X64Reg regOp, const OpArg& arg, u8 rotate) {WriteBMI2Op(bits, 0xF2, 0x3AF0, regOp, INVALID_REG, arg, 1); Write8(rotate);}
  1763. void XEmitter::PEXT(int bits, X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteBMI2Op(bits, 0xF3, 0x38F5, regOp1, regOp2, arg);}
  1764. void XEmitter::PDEP(int bits, X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteBMI2Op(bits, 0xF2, 0x38F5, regOp1, regOp2, arg);}
  1765. void XEmitter::MULX(int bits, X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteBMI2Op(bits, 0xF2, 0x38F6, regOp2, regOp1, arg);}
  1766. void XEmitter::BZHI(int bits, X64Reg regOp1, const OpArg& arg, X64Reg regOp2) {WriteBMI2Op(bits, 0x00, 0x38F5, regOp1, regOp2, arg);}
  1767. void XEmitter::BLSR(int bits, X64Reg regOp, const OpArg& arg) {WriteBMI1Op(bits, 0x00, 0x38F3, (X64Reg)0x1, regOp, arg);}
  1768. void XEmitter::BLSMSK(int bits, X64Reg regOp, const OpArg& arg) {WriteBMI1Op(bits, 0x00, 0x38F3, (X64Reg)0x2, regOp, arg);}
  1769. void XEmitter::BLSI(int bits, X64Reg regOp, const OpArg& arg) {WriteBMI1Op(bits, 0x00, 0x38F3, (X64Reg)0x3, regOp, arg);}
  1770. void XEmitter::BEXTR(int bits, X64Reg regOp1, const OpArg& arg, X64Reg regOp2){WriteBMI1Op(bits, 0x00, 0x38F7, regOp1, regOp2, arg);}
  1771. void XEmitter::ANDN(int bits, X64Reg regOp1, X64Reg regOp2, const OpArg& arg) {WriteBMI1Op(bits, 0x00, 0x38F2, regOp1, regOp2, arg);}
  1772. // Prefixes
  1773. void XEmitter::LOCK() { Write8(0xF0); }
  1774. void XEmitter::REP() { Write8(0xF3); }
  1775. void XEmitter::REPNE() { Write8(0xF2); }
  1776. void XEmitter::FSOverride() { Write8(0x64); }
  1777. void XEmitter::GSOverride() { Write8(0x65); }
  1778. void XEmitter::FWAIT()
  1779. {
  1780. Write8(0x9B);
  1781. }
  1782. // TODO: make this more generic
  1783. void XEmitter::WriteFloatLoadStore(int bits, FloatOp op, FloatOp op_80b, const OpArg& arg)
  1784. {
  1785. int mf = 0;
  1786. ASSERT_MSG(!(bits == 80 && op_80b == floatINVALID), "WriteFloatLoadStore: 80 bits not supported for this instruction");
  1787. switch (bits)
  1788. {
  1789. case 32: mf = 0; break;
  1790. case 64: mf = 4; break;
  1791. case 80: mf = 2; break;
  1792. default: ASSERT_MSG(0, "WriteFloatLoadStore: invalid bits (should be 32/64/80)");
  1793. }
  1794. Write8(0xd9 | mf);
  1795. // x87 instructions use the reg field of the ModR/M byte as opcode:
  1796. if (bits == 80)
  1797. op = op_80b;
  1798. arg.WriteRest(this, 0, (X64Reg) op);
  1799. }
  1800. void XEmitter::FLD(int bits, const OpArg& src) {WriteFloatLoadStore(bits, floatLD, floatLD80, src);}
  1801. void XEmitter::FST(int bits, const OpArg& dest) {WriteFloatLoadStore(bits, floatST, floatINVALID, dest);}
  1802. void XEmitter::FSTP(int bits, const OpArg& dest) {WriteFloatLoadStore(bits, floatSTP, floatSTP80, dest);}
  1803. void XEmitter::FNSTSW_AX() { Write8(0xDF); Write8(0xE0); }
  1804. void XEmitter::RDTSC() { Write8(0x0F); Write8(0x31); }
  1805. void XCodeBlock::PoisonMemory() {
  1806. // x86/64: 0xCC = breakpoint
  1807. memset(region, 0xCC, region_size);
  1808. }
  1809. }