Assembler.cs 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. using ARMeilleure.IntermediateRepresentation;
  2. using ARMeilleure.Translation.PTC;
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. namespace ARMeilleure.CodeGen.X86
  7. {
  8. class Assembler
  9. {
  10. private const int BadOp = 0;
  11. private const int OpModRMBits = 24;
  12. private const byte RexPrefix = 0x40;
  13. private const byte RexWPrefix = 0x48;
  14. private const byte LockPrefix = 0xf0;
  15. private const int MaxRegNumber = 15;
  16. [Flags]
  17. private enum InstructionFlags
  18. {
  19. None = 0,
  20. RegOnly = 1 << 0,
  21. Reg8Src = 1 << 1,
  22. Reg8Dest = 1 << 2,
  23. RexW = 1 << 3,
  24. Vex = 1 << 4,
  25. PrefixBit = 16,
  26. PrefixMask = 7 << PrefixBit,
  27. Prefix66 = 1 << PrefixBit,
  28. PrefixF3 = 2 << PrefixBit,
  29. PrefixF2 = 4 << PrefixBit
  30. }
  31. private struct InstructionInfo
  32. {
  33. public int OpRMR { get; }
  34. public int OpRMImm8 { get; }
  35. public int OpRMImm32 { get; }
  36. public int OpRImm64 { get; }
  37. public int OpRRM { get; }
  38. public InstructionFlags Flags { get; }
  39. public InstructionInfo(
  40. int opRMR,
  41. int opRMImm8,
  42. int opRMImm32,
  43. int opRImm64,
  44. int opRRM,
  45. InstructionFlags flags)
  46. {
  47. OpRMR = opRMR;
  48. OpRMImm8 = opRMImm8;
  49. OpRMImm32 = opRMImm32;
  50. OpRImm64 = opRImm64;
  51. OpRRM = opRRM;
  52. Flags = flags;
  53. }
  54. }
  55. private static InstructionInfo[] _instTable;
  56. private Stream _stream;
  57. private PtcInfo _ptcInfo;
  58. private bool _ptcDisabled;
  59. static Assembler()
  60. {
  61. _instTable = new InstructionInfo[(int)X86Instruction.Count];
  62. // Name RM/R RM/I8 RM/I32 R/I64 R/RM Flags
  63. Add(X86Instruction.Add, new InstructionInfo(0x00000001, 0x00000083, 0x00000081, BadOp, 0x00000003, InstructionFlags.None));
  64. Add(X86Instruction.Addpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f58, InstructionFlags.Vex | InstructionFlags.Prefix66));
  65. Add(X86Instruction.Addps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f58, InstructionFlags.Vex));
  66. Add(X86Instruction.Addsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f58, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  67. Add(X86Instruction.Addss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f58, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  68. Add(X86Instruction.Aesdec, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f38de, InstructionFlags.Vex | InstructionFlags.Prefix66));
  69. Add(X86Instruction.Aesdeclast, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f38df, InstructionFlags.Vex | InstructionFlags.Prefix66));
  70. Add(X86Instruction.Aesenc, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f38dc, InstructionFlags.Vex | InstructionFlags.Prefix66));
  71. Add(X86Instruction.Aesenclast, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f38dd, InstructionFlags.Vex | InstructionFlags.Prefix66));
  72. Add(X86Instruction.Aesimc, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f38db, InstructionFlags.Vex | InstructionFlags.Prefix66));
  73. Add(X86Instruction.And, new InstructionInfo(0x00000021, 0x04000083, 0x04000081, BadOp, 0x00000023, InstructionFlags.None));
  74. Add(X86Instruction.Andnpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f55, InstructionFlags.Vex | InstructionFlags.Prefix66));
  75. Add(X86Instruction.Andnps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f55, InstructionFlags.Vex));
  76. Add(X86Instruction.Andpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f54, InstructionFlags.Vex | InstructionFlags.Prefix66));
  77. Add(X86Instruction.Andps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f54, InstructionFlags.Vex));
  78. Add(X86Instruction.Blendvpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3815, InstructionFlags.Prefix66));
  79. Add(X86Instruction.Blendvps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3814, InstructionFlags.Prefix66));
  80. Add(X86Instruction.Bsr, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fbd, InstructionFlags.None));
  81. Add(X86Instruction.Bswap, new InstructionInfo(0x00000fc8, BadOp, BadOp, BadOp, BadOp, InstructionFlags.RegOnly));
  82. Add(X86Instruction.Call, new InstructionInfo(0x020000ff, BadOp, BadOp, BadOp, BadOp, InstructionFlags.None));
  83. Add(X86Instruction.Cmovcc, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f40, InstructionFlags.None));
  84. Add(X86Instruction.Cmp, new InstructionInfo(0x00000039, 0x07000083, 0x07000081, BadOp, 0x0000003b, InstructionFlags.None));
  85. Add(X86Instruction.Cmppd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fc2, InstructionFlags.Vex | InstructionFlags.Prefix66));
  86. Add(X86Instruction.Cmpps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fc2, InstructionFlags.Vex));
  87. Add(X86Instruction.Cmpsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fc2, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  88. Add(X86Instruction.Cmpss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fc2, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  89. Add(X86Instruction.Cmpxchg, new InstructionInfo(0x00000fb1, BadOp, BadOp, BadOp, BadOp, InstructionFlags.None));
  90. Add(X86Instruction.Cmpxchg16b, new InstructionInfo(0x01000fc7, BadOp, BadOp, BadOp, BadOp, InstructionFlags.RexW));
  91. Add(X86Instruction.Cmpxchg8, new InstructionInfo(0x00000fb0, BadOp, BadOp, BadOp, BadOp, InstructionFlags.Reg8Src));
  92. Add(X86Instruction.Comisd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f2f, InstructionFlags.Vex | InstructionFlags.Prefix66));
  93. Add(X86Instruction.Comiss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f2f, InstructionFlags.Vex));
  94. Add(X86Instruction.Cpuid, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fa2, InstructionFlags.RegOnly));
  95. Add(X86Instruction.Crc32, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f38f1, InstructionFlags.PrefixF2));
  96. Add(X86Instruction.Crc32_16, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f38f1, InstructionFlags.PrefixF2 | InstructionFlags.Prefix66));
  97. Add(X86Instruction.Crc32_8, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f38f0, InstructionFlags.PrefixF2 | InstructionFlags.Reg8Src));
  98. Add(X86Instruction.Cvtdq2pd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fe6, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  99. Add(X86Instruction.Cvtdq2ps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5b, InstructionFlags.Vex));
  100. Add(X86Instruction.Cvtpd2dq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fe6, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  101. Add(X86Instruction.Cvtpd2ps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5a, InstructionFlags.Vex | InstructionFlags.Prefix66));
  102. Add(X86Instruction.Cvtps2dq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5b, InstructionFlags.Vex | InstructionFlags.Prefix66));
  103. Add(X86Instruction.Cvtps2pd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5a, InstructionFlags.Vex));
  104. Add(X86Instruction.Cvtsd2si, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f2d, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  105. Add(X86Instruction.Cvtsd2ss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5a, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  106. Add(X86Instruction.Cvtsi2sd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f2a, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  107. Add(X86Instruction.Cvtsi2ss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f2a, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  108. Add(X86Instruction.Cvtss2sd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5a, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  109. Add(X86Instruction.Cvtss2si, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f2d, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  110. Add(X86Instruction.Div, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x060000f7, InstructionFlags.None));
  111. Add(X86Instruction.Divpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5e, InstructionFlags.Vex | InstructionFlags.Prefix66));
  112. Add(X86Instruction.Divps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5e, InstructionFlags.Vex));
  113. Add(X86Instruction.Divsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5e, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  114. Add(X86Instruction.Divss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5e, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  115. Add(X86Instruction.Haddpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f7c, InstructionFlags.Vex | InstructionFlags.Prefix66));
  116. Add(X86Instruction.Haddps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f7c, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  117. Add(X86Instruction.Idiv, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x070000f7, InstructionFlags.None));
  118. Add(X86Instruction.Imul, new InstructionInfo(BadOp, 0x0000006b, 0x00000069, BadOp, 0x00000faf, InstructionFlags.None));
  119. Add(X86Instruction.Imul128, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x050000f7, InstructionFlags.None));
  120. Add(X86Instruction.Insertps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a21, InstructionFlags.Vex | InstructionFlags.Prefix66));
  121. Add(X86Instruction.Jmp, new InstructionInfo(0x040000ff, BadOp, BadOp, BadOp, BadOp, InstructionFlags.None));
  122. Add(X86Instruction.Lea, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x0000008d, InstructionFlags.None));
  123. Add(X86Instruction.Maxpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5f, InstructionFlags.Vex | InstructionFlags.Prefix66));
  124. Add(X86Instruction.Maxps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5f, InstructionFlags.Vex));
  125. Add(X86Instruction.Maxsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5f, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  126. Add(X86Instruction.Maxss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5f, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  127. Add(X86Instruction.Minpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5d, InstructionFlags.Vex | InstructionFlags.Prefix66));
  128. Add(X86Instruction.Minps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5d, InstructionFlags.Vex));
  129. Add(X86Instruction.Minsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5d, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  130. Add(X86Instruction.Minss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5d, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  131. Add(X86Instruction.Mov, new InstructionInfo(0x00000089, BadOp, 0x000000c7, 0x000000b8, 0x0000008b, InstructionFlags.None));
  132. Add(X86Instruction.Mov16, new InstructionInfo(0x00000089, BadOp, 0x000000c7, BadOp, 0x0000008b, InstructionFlags.Prefix66));
  133. Add(X86Instruction.Mov8, new InstructionInfo(0x00000088, 0x000000c6, BadOp, BadOp, 0x0000008a, InstructionFlags.Reg8Src | InstructionFlags.Reg8Dest));
  134. Add(X86Instruction.Movd, new InstructionInfo(0x00000f7e, BadOp, BadOp, BadOp, 0x00000f6e, InstructionFlags.Vex | InstructionFlags.Prefix66));
  135. Add(X86Instruction.Movdqu, new InstructionInfo(0x00000f7f, BadOp, BadOp, BadOp, 0x00000f6f, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  136. Add(X86Instruction.Movhlps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f12, InstructionFlags.Vex));
  137. Add(X86Instruction.Movlhps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f16, InstructionFlags.Vex));
  138. Add(X86Instruction.Movq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f7e, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  139. Add(X86Instruction.Movsd, new InstructionInfo(0x00000f11, BadOp, BadOp, BadOp, 0x00000f10, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  140. Add(X86Instruction.Movss, new InstructionInfo(0x00000f11, BadOp, BadOp, BadOp, 0x00000f10, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  141. Add(X86Instruction.Movsx16, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fbf, InstructionFlags.None));
  142. Add(X86Instruction.Movsx32, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000063, InstructionFlags.None));
  143. Add(X86Instruction.Movsx8, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fbe, InstructionFlags.Reg8Src));
  144. Add(X86Instruction.Movzx16, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fb7, InstructionFlags.None));
  145. Add(X86Instruction.Movzx8, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fb6, InstructionFlags.Reg8Src));
  146. Add(X86Instruction.Mul128, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x040000f7, InstructionFlags.None));
  147. Add(X86Instruction.Mulpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f59, InstructionFlags.Vex | InstructionFlags.Prefix66));
  148. Add(X86Instruction.Mulps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f59, InstructionFlags.Vex));
  149. Add(X86Instruction.Mulsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f59, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  150. Add(X86Instruction.Mulss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f59, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  151. Add(X86Instruction.Neg, new InstructionInfo(0x030000f7, BadOp, BadOp, BadOp, BadOp, InstructionFlags.None));
  152. Add(X86Instruction.Not, new InstructionInfo(0x020000f7, BadOp, BadOp, BadOp, BadOp, InstructionFlags.None));
  153. Add(X86Instruction.Or, new InstructionInfo(0x00000009, 0x01000083, 0x01000081, BadOp, 0x0000000b, InstructionFlags.None));
  154. Add(X86Instruction.Paddb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000ffc, InstructionFlags.Vex | InstructionFlags.Prefix66));
  155. Add(X86Instruction.Paddd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000ffe, InstructionFlags.Vex | InstructionFlags.Prefix66));
  156. Add(X86Instruction.Paddq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fd4, InstructionFlags.Vex | InstructionFlags.Prefix66));
  157. Add(X86Instruction.Paddw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000ffd, InstructionFlags.Vex | InstructionFlags.Prefix66));
  158. Add(X86Instruction.Pand, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fdb, InstructionFlags.Vex | InstructionFlags.Prefix66));
  159. Add(X86Instruction.Pandn, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fdf, InstructionFlags.Vex | InstructionFlags.Prefix66));
  160. Add(X86Instruction.Pavgb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fe0, InstructionFlags.Vex | InstructionFlags.Prefix66));
  161. Add(X86Instruction.Pavgw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fe3, InstructionFlags.Vex | InstructionFlags.Prefix66));
  162. Add(X86Instruction.Pblendvb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3810, InstructionFlags.Prefix66));
  163. Add(X86Instruction.Pclmulqdq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a44, InstructionFlags.Vex | InstructionFlags.Prefix66));
  164. Add(X86Instruction.Pcmpeqb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f74, InstructionFlags.Vex | InstructionFlags.Prefix66));
  165. Add(X86Instruction.Pcmpeqd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f76, InstructionFlags.Vex | InstructionFlags.Prefix66));
  166. Add(X86Instruction.Pcmpeqq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3829, InstructionFlags.Vex | InstructionFlags.Prefix66));
  167. Add(X86Instruction.Pcmpeqw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f75, InstructionFlags.Vex | InstructionFlags.Prefix66));
  168. Add(X86Instruction.Pcmpgtb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f64, InstructionFlags.Vex | InstructionFlags.Prefix66));
  169. Add(X86Instruction.Pcmpgtd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f66, InstructionFlags.Vex | InstructionFlags.Prefix66));
  170. Add(X86Instruction.Pcmpgtq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3837, InstructionFlags.Vex | InstructionFlags.Prefix66));
  171. Add(X86Instruction.Pcmpgtw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f65, InstructionFlags.Vex | InstructionFlags.Prefix66));
  172. Add(X86Instruction.Pextrb, new InstructionInfo(0x000f3a14, BadOp, BadOp, BadOp, BadOp, InstructionFlags.Vex | InstructionFlags.Prefix66));
  173. Add(X86Instruction.Pextrd, new InstructionInfo(0x000f3a16, BadOp, BadOp, BadOp, BadOp, InstructionFlags.Vex | InstructionFlags.Prefix66));
  174. Add(X86Instruction.Pextrq, new InstructionInfo(0x000f3a16, BadOp, BadOp, BadOp, BadOp, InstructionFlags.Vex | InstructionFlags.RexW | InstructionFlags.Prefix66));
  175. Add(X86Instruction.Pextrw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fc5, InstructionFlags.Vex | InstructionFlags.Prefix66));
  176. Add(X86Instruction.Pinsrb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a20, InstructionFlags.Vex | InstructionFlags.Prefix66));
  177. Add(X86Instruction.Pinsrd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a22, InstructionFlags.Vex | InstructionFlags.Prefix66));
  178. Add(X86Instruction.Pinsrq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a22, InstructionFlags.Vex | InstructionFlags.RexW | InstructionFlags.Prefix66));
  179. Add(X86Instruction.Pinsrw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fc4, InstructionFlags.Vex | InstructionFlags.Prefix66));
  180. Add(X86Instruction.Pmaxsb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f383c, InstructionFlags.Vex | InstructionFlags.Prefix66));
  181. Add(X86Instruction.Pmaxsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f383d, InstructionFlags.Vex | InstructionFlags.Prefix66));
  182. Add(X86Instruction.Pmaxsw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fee, InstructionFlags.Vex | InstructionFlags.Prefix66));
  183. Add(X86Instruction.Pmaxub, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fde, InstructionFlags.Vex | InstructionFlags.Prefix66));
  184. Add(X86Instruction.Pmaxud, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f383f, InstructionFlags.Vex | InstructionFlags.Prefix66));
  185. Add(X86Instruction.Pmaxuw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f383e, InstructionFlags.Vex | InstructionFlags.Prefix66));
  186. Add(X86Instruction.Pminsb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3838, InstructionFlags.Vex | InstructionFlags.Prefix66));
  187. Add(X86Instruction.Pminsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3839, InstructionFlags.Vex | InstructionFlags.Prefix66));
  188. Add(X86Instruction.Pminsw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fea, InstructionFlags.Vex | InstructionFlags.Prefix66));
  189. Add(X86Instruction.Pminub, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fda, InstructionFlags.Vex | InstructionFlags.Prefix66));
  190. Add(X86Instruction.Pminud, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f383b, InstructionFlags.Vex | InstructionFlags.Prefix66));
  191. Add(X86Instruction.Pminuw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f383a, InstructionFlags.Vex | InstructionFlags.Prefix66));
  192. Add(X86Instruction.Pmovsxbw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3820, InstructionFlags.Vex | InstructionFlags.Prefix66));
  193. Add(X86Instruction.Pmovsxdq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3825, InstructionFlags.Vex | InstructionFlags.Prefix66));
  194. Add(X86Instruction.Pmovsxwd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3823, InstructionFlags.Vex | InstructionFlags.Prefix66));
  195. Add(X86Instruction.Pmovzxbw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3830, InstructionFlags.Vex | InstructionFlags.Prefix66));
  196. Add(X86Instruction.Pmovzxdq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3835, InstructionFlags.Vex | InstructionFlags.Prefix66));
  197. Add(X86Instruction.Pmovzxwd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3833, InstructionFlags.Vex | InstructionFlags.Prefix66));
  198. Add(X86Instruction.Pmulld, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3840, InstructionFlags.Vex | InstructionFlags.Prefix66));
  199. Add(X86Instruction.Pmullw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fd5, InstructionFlags.Vex | InstructionFlags.Prefix66));
  200. Add(X86Instruction.Pop, new InstructionInfo(0x0000008f, BadOp, BadOp, BadOp, BadOp, InstructionFlags.None));
  201. Add(X86Instruction.Popcnt, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fb8, InstructionFlags.PrefixF3));
  202. Add(X86Instruction.Por, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000feb, InstructionFlags.Vex | InstructionFlags.Prefix66));
  203. Add(X86Instruction.Pshufb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3800, InstructionFlags.Vex | InstructionFlags.Prefix66));
  204. Add(X86Instruction.Pshufd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f70, InstructionFlags.Vex | InstructionFlags.Prefix66));
  205. Add(X86Instruction.Pslld, new InstructionInfo(BadOp, 0x06000f72, BadOp, BadOp, 0x00000ff2, InstructionFlags.Vex | InstructionFlags.Prefix66));
  206. Add(X86Instruction.Pslldq, new InstructionInfo(BadOp, 0x07000f73, BadOp, BadOp, BadOp, InstructionFlags.Vex | InstructionFlags.Prefix66));
  207. Add(X86Instruction.Psllq, new InstructionInfo(BadOp, 0x06000f73, BadOp, BadOp, 0x00000ff3, InstructionFlags.Vex | InstructionFlags.Prefix66));
  208. Add(X86Instruction.Psllw, new InstructionInfo(BadOp, 0x06000f71, BadOp, BadOp, 0x00000ff1, InstructionFlags.Vex | InstructionFlags.Prefix66));
  209. Add(X86Instruction.Psrad, new InstructionInfo(BadOp, 0x04000f72, BadOp, BadOp, 0x00000fe2, InstructionFlags.Vex | InstructionFlags.Prefix66));
  210. Add(X86Instruction.Psraw, new InstructionInfo(BadOp, 0x04000f71, BadOp, BadOp, 0x00000fe1, InstructionFlags.Vex | InstructionFlags.Prefix66));
  211. Add(X86Instruction.Psrld, new InstructionInfo(BadOp, 0x02000f72, BadOp, BadOp, 0x00000fd2, InstructionFlags.Vex | InstructionFlags.Prefix66));
  212. Add(X86Instruction.Psrlq, new InstructionInfo(BadOp, 0x02000f73, BadOp, BadOp, 0x00000fd3, InstructionFlags.Vex | InstructionFlags.Prefix66));
  213. Add(X86Instruction.Psrldq, new InstructionInfo(BadOp, 0x03000f73, BadOp, BadOp, BadOp, InstructionFlags.Vex | InstructionFlags.Prefix66));
  214. Add(X86Instruction.Psrlw, new InstructionInfo(BadOp, 0x02000f71, BadOp, BadOp, 0x00000fd1, InstructionFlags.Vex | InstructionFlags.Prefix66));
  215. Add(X86Instruction.Psubb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000ff8, InstructionFlags.Vex | InstructionFlags.Prefix66));
  216. Add(X86Instruction.Psubd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000ffa, InstructionFlags.Vex | InstructionFlags.Prefix66));
  217. Add(X86Instruction.Psubq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000ffb, InstructionFlags.Vex | InstructionFlags.Prefix66));
  218. Add(X86Instruction.Psubw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000ff9, InstructionFlags.Vex | InstructionFlags.Prefix66));
  219. Add(X86Instruction.Punpckhbw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f68, InstructionFlags.Vex | InstructionFlags.Prefix66));
  220. Add(X86Instruction.Punpckhdq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f6a, InstructionFlags.Vex | InstructionFlags.Prefix66));
  221. Add(X86Instruction.Punpckhqdq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f6d, InstructionFlags.Vex | InstructionFlags.Prefix66));
  222. Add(X86Instruction.Punpckhwd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f69, InstructionFlags.Vex | InstructionFlags.Prefix66));
  223. Add(X86Instruction.Punpcklbw, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f60, InstructionFlags.Vex | InstructionFlags.Prefix66));
  224. Add(X86Instruction.Punpckldq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f62, InstructionFlags.Vex | InstructionFlags.Prefix66));
  225. Add(X86Instruction.Punpcklqdq, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f6c, InstructionFlags.Vex | InstructionFlags.Prefix66));
  226. Add(X86Instruction.Punpcklwd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f61, InstructionFlags.Vex | InstructionFlags.Prefix66));
  227. Add(X86Instruction.Push, new InstructionInfo(BadOp, 0x0000006a, 0x00000068, BadOp, 0x060000ff, InstructionFlags.None));
  228. Add(X86Instruction.Pxor, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fef, InstructionFlags.Vex | InstructionFlags.Prefix66));
  229. Add(X86Instruction.Rcpps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f53, InstructionFlags.Vex));
  230. Add(X86Instruction.Rcpss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f53, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  231. Add(X86Instruction.Ror, new InstructionInfo(0x010000d3, 0x010000c1, BadOp, BadOp, BadOp, InstructionFlags.None));
  232. Add(X86Instruction.Roundpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a09, InstructionFlags.Vex | InstructionFlags.Prefix66));
  233. Add(X86Instruction.Roundps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a08, InstructionFlags.Vex | InstructionFlags.Prefix66));
  234. Add(X86Instruction.Roundsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a0b, InstructionFlags.Vex | InstructionFlags.Prefix66));
  235. Add(X86Instruction.Roundss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a0a, InstructionFlags.Vex | InstructionFlags.Prefix66));
  236. Add(X86Instruction.Rsqrtps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f52, InstructionFlags.Vex));
  237. Add(X86Instruction.Rsqrtss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f52, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  238. Add(X86Instruction.Sar, new InstructionInfo(0x070000d3, 0x070000c1, BadOp, BadOp, BadOp, InstructionFlags.None));
  239. Add(X86Instruction.Setcc, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f90, InstructionFlags.Reg8Dest));
  240. Add(X86Instruction.Shl, new InstructionInfo(0x040000d3, 0x040000c1, BadOp, BadOp, BadOp, InstructionFlags.None));
  241. Add(X86Instruction.Shr, new InstructionInfo(0x050000d3, 0x050000c1, BadOp, BadOp, BadOp, InstructionFlags.None));
  242. Add(X86Instruction.Shufpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fc6, InstructionFlags.Vex | InstructionFlags.Prefix66));
  243. Add(X86Instruction.Shufps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000fc6, InstructionFlags.Vex));
  244. Add(X86Instruction.Sqrtpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f51, InstructionFlags.Vex | InstructionFlags.Prefix66));
  245. Add(X86Instruction.Sqrtps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f51, InstructionFlags.Vex));
  246. Add(X86Instruction.Sqrtsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f51, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  247. Add(X86Instruction.Sqrtss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f51, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  248. Add(X86Instruction.Sub, new InstructionInfo(0x00000029, 0x05000083, 0x05000081, BadOp, 0x0000002b, InstructionFlags.None));
  249. Add(X86Instruction.Subpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5c, InstructionFlags.Vex | InstructionFlags.Prefix66));
  250. Add(X86Instruction.Subps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5c, InstructionFlags.Vex));
  251. Add(X86Instruction.Subsd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5c, InstructionFlags.Vex | InstructionFlags.PrefixF2));
  252. Add(X86Instruction.Subss, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f5c, InstructionFlags.Vex | InstructionFlags.PrefixF3));
  253. Add(X86Instruction.Test, new InstructionInfo(0x00000085, BadOp, 0x000000f7, BadOp, BadOp, InstructionFlags.None));
  254. Add(X86Instruction.Unpckhpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f15, InstructionFlags.Vex | InstructionFlags.Prefix66));
  255. Add(X86Instruction.Unpckhps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f15, InstructionFlags.Vex));
  256. Add(X86Instruction.Unpcklpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f14, InstructionFlags.Vex | InstructionFlags.Prefix66));
  257. Add(X86Instruction.Unpcklps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f14, InstructionFlags.Vex));
  258. Add(X86Instruction.Vblendvpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a4b, InstructionFlags.Vex | InstructionFlags.Prefix66));
  259. Add(X86Instruction.Vblendvps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a4a, InstructionFlags.Vex | InstructionFlags.Prefix66));
  260. Add(X86Instruction.Vpblendvb, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x000f3a4c, InstructionFlags.Vex | InstructionFlags.Prefix66));
  261. Add(X86Instruction.Xor, new InstructionInfo(0x00000031, 0x06000083, 0x06000081, BadOp, 0x00000033, InstructionFlags.None));
  262. Add(X86Instruction.Xorpd, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f57, InstructionFlags.Vex | InstructionFlags.Prefix66));
  263. Add(X86Instruction.Xorps, new InstructionInfo(BadOp, BadOp, BadOp, BadOp, 0x00000f57, InstructionFlags.Vex));
  264. }
  265. private static void Add(X86Instruction inst, InstructionInfo info)
  266. {
  267. _instTable[(int)inst] = info;
  268. }
  269. public Assembler(Stream stream, PtcInfo ptcInfo = null)
  270. {
  271. _stream = stream;
  272. _ptcInfo = ptcInfo;
  273. _ptcDisabled = ptcInfo == null;
  274. }
  275. public void Add(Operand dest, Operand source, OperandType type)
  276. {
  277. WriteInstruction(dest, source, type, X86Instruction.Add);
  278. }
  279. public void Addsd(Operand dest, Operand src1, Operand src2)
  280. {
  281. WriteInstruction(dest, src1, src2, X86Instruction.Addsd);
  282. }
  283. public void Addss(Operand dest, Operand src1, Operand src2)
  284. {
  285. WriteInstruction(dest, src1, src2, X86Instruction.Addss);
  286. }
  287. public void And(Operand dest, Operand source, OperandType type)
  288. {
  289. WriteInstruction(dest, source, type, X86Instruction.And);
  290. }
  291. public void Bsr(Operand dest, Operand source, OperandType type)
  292. {
  293. WriteInstruction(dest, source, type, X86Instruction.Bsr);
  294. }
  295. public void Bswap(Operand dest)
  296. {
  297. WriteInstruction(dest, null, dest.Type, X86Instruction.Bswap);
  298. }
  299. public void Call(Operand dest)
  300. {
  301. WriteInstruction(dest, null, OperandType.None, X86Instruction.Call);
  302. }
  303. public void Cdq()
  304. {
  305. WriteByte(0x99);
  306. }
  307. public void Cmovcc(Operand dest, Operand source, OperandType type, X86Condition condition)
  308. {
  309. InstructionInfo info = _instTable[(int)X86Instruction.Cmovcc];
  310. WriteOpCode(dest, null, source, type, info.Flags, info.OpRRM | (int)condition, rrm: true);
  311. }
  312. public void Cmp(Operand src1, Operand src2, OperandType type)
  313. {
  314. WriteInstruction(src1, src2, type, X86Instruction.Cmp);
  315. }
  316. public void Cqo()
  317. {
  318. WriteByte(0x48);
  319. WriteByte(0x99);
  320. }
  321. public void Cmpxchg(MemoryOperand memOp, Operand src)
  322. {
  323. WriteByte(LockPrefix);
  324. WriteInstruction(memOp, src, src.Type, X86Instruction.Cmpxchg);
  325. }
  326. public void Cmpxchg16(MemoryOperand memOp, Operand src)
  327. {
  328. WriteByte(LockPrefix);
  329. WriteByte(0x66);
  330. WriteInstruction(memOp, src, src.Type, X86Instruction.Cmpxchg);
  331. }
  332. public void Cmpxchg16b(MemoryOperand memOp)
  333. {
  334. WriteByte(LockPrefix);
  335. WriteInstruction(memOp, null, OperandType.None, X86Instruction.Cmpxchg16b);
  336. }
  337. public void Cmpxchg8(MemoryOperand memOp, Operand src)
  338. {
  339. WriteByte(LockPrefix);
  340. WriteInstruction(memOp, src, src.Type, X86Instruction.Cmpxchg8);
  341. }
  342. public void Comisd(Operand src1, Operand src2)
  343. {
  344. WriteInstruction(src1, null, src2, X86Instruction.Comisd);
  345. }
  346. public void Comiss(Operand src1, Operand src2)
  347. {
  348. WriteInstruction(src1, null, src2, X86Instruction.Comiss);
  349. }
  350. public void Cpuid()
  351. {
  352. WriteInstruction(null, null, OperandType.None, X86Instruction.Cpuid);
  353. }
  354. public void Cvtsd2ss(Operand dest, Operand src1, Operand src2)
  355. {
  356. WriteInstruction(dest, src1, src2, X86Instruction.Cvtsd2ss);
  357. }
  358. public void Cvtsi2sd(Operand dest, Operand src1, Operand src2, OperandType type)
  359. {
  360. WriteInstruction(dest, src1, src2, X86Instruction.Cvtsi2sd, type);
  361. }
  362. public void Cvtsi2ss(Operand dest, Operand src1, Operand src2, OperandType type)
  363. {
  364. WriteInstruction(dest, src1, src2, X86Instruction.Cvtsi2ss, type);
  365. }
  366. public void Cvtss2sd(Operand dest, Operand src1, Operand src2)
  367. {
  368. WriteInstruction(dest, src1, src2, X86Instruction.Cvtss2sd);
  369. }
  370. public void Div(Operand source)
  371. {
  372. WriteInstruction(null, source, source.Type, X86Instruction.Div);
  373. }
  374. public void Divsd(Operand dest, Operand src1, Operand src2)
  375. {
  376. WriteInstruction(dest, src1, src2, X86Instruction.Divsd);
  377. }
  378. public void Divss(Operand dest, Operand src1, Operand src2)
  379. {
  380. WriteInstruction(dest, src1, src2, X86Instruction.Divss);
  381. }
  382. public void Idiv(Operand source)
  383. {
  384. WriteInstruction(null, source, source.Type, X86Instruction.Idiv);
  385. }
  386. public void Imul(Operand source)
  387. {
  388. WriteInstruction(null, source, source.Type, X86Instruction.Imul128);
  389. }
  390. public void Imul(Operand dest, Operand source, OperandType type)
  391. {
  392. if (source.Kind != OperandKind.Register)
  393. {
  394. throw new ArgumentException($"Invalid source operand kind \"{source.Kind}\".");
  395. }
  396. WriteInstruction(dest, source, type, X86Instruction.Imul);
  397. }
  398. public void Imul(Operand dest, Operand src1, Operand src2, OperandType type)
  399. {
  400. InstructionInfo info = _instTable[(int)X86Instruction.Imul];
  401. if (src2.Kind != OperandKind.Constant)
  402. {
  403. throw new ArgumentException($"Invalid source 2 operand kind \"{src2.Kind}\".");
  404. }
  405. if (IsImm8(src2.Value, src2.Type) && info.OpRMImm8 != BadOp)
  406. {
  407. WriteOpCode(dest, null, src1, type, info.Flags, info.OpRMImm8, rrm: true);
  408. WriteByte(src2.AsByte());
  409. }
  410. else if (IsImm32(src2.Value, src2.Type) && info.OpRMImm32 != BadOp)
  411. {
  412. WriteOpCode(dest, null, src1, type, info.Flags, info.OpRMImm32, rrm: true);
  413. WriteInt32(src2.AsInt32());
  414. }
  415. else
  416. {
  417. throw new ArgumentException($"Failed to encode constant 0x{src2.Value:X}.");
  418. }
  419. }
  420. public void Insertps(Operand dest, Operand src1, Operand src2, byte imm)
  421. {
  422. WriteInstruction(dest, src1, src2, X86Instruction.Insertps);
  423. WriteByte(imm);
  424. }
  425. public void Jcc(X86Condition condition, long offset)
  426. {
  427. if (_ptcDisabled && ConstFitsOnS8(offset))
  428. {
  429. WriteByte((byte)(0x70 | (int)condition));
  430. WriteByte((byte)offset);
  431. }
  432. else if (ConstFitsOnS32(offset))
  433. {
  434. WriteByte(0x0f);
  435. WriteByte((byte)(0x80 | (int)condition));
  436. WriteInt32((int)offset);
  437. }
  438. else
  439. {
  440. throw new ArgumentOutOfRangeException(nameof(offset));
  441. }
  442. }
  443. public void Jmp(long offset)
  444. {
  445. if (_ptcDisabled && ConstFitsOnS8(offset))
  446. {
  447. WriteByte(0xeb);
  448. WriteByte((byte)offset);
  449. }
  450. else if (ConstFitsOnS32(offset))
  451. {
  452. WriteByte(0xe9);
  453. WriteInt32((int)offset);
  454. }
  455. else
  456. {
  457. throw new ArgumentOutOfRangeException(nameof(offset));
  458. }
  459. }
  460. public void Jmp(Operand dest)
  461. {
  462. WriteInstruction(dest, null, OperandType.None, X86Instruction.Jmp);
  463. }
  464. public void Lea(Operand dest, Operand source, OperandType type)
  465. {
  466. WriteInstruction(dest, source, type, X86Instruction.Lea);
  467. }
  468. public void Mov(Operand dest, Operand source, OperandType type)
  469. {
  470. WriteInstruction(dest, source, type, X86Instruction.Mov);
  471. }
  472. public void Mov16(Operand dest, Operand source)
  473. {
  474. WriteInstruction(dest, source, OperandType.None, X86Instruction.Mov16);
  475. }
  476. public void Mov8(Operand dest, Operand source)
  477. {
  478. WriteInstruction(dest, source, OperandType.None, X86Instruction.Mov8);
  479. }
  480. public void Movd(Operand dest, Operand source)
  481. {
  482. InstructionInfo info = _instTable[(int)X86Instruction.Movd];
  483. if (source.Type.IsInteger() || source.Kind == OperandKind.Memory)
  484. {
  485. WriteOpCode(dest, null, source, OperandType.None, info.Flags, info.OpRRM, rrm: true);
  486. }
  487. else
  488. {
  489. WriteOpCode(dest, null, source, OperandType.None, info.Flags, info.OpRMR);
  490. }
  491. }
  492. public void Movdqu(Operand dest, Operand source)
  493. {
  494. WriteInstruction(dest, null, source, X86Instruction.Movdqu);
  495. }
  496. public void Movhlps(Operand dest, Operand src1, Operand src2)
  497. {
  498. WriteInstruction(dest, src1, src2, X86Instruction.Movhlps);
  499. }
  500. public void Movlhps(Operand dest, Operand src1, Operand src2)
  501. {
  502. WriteInstruction(dest, src1, src2, X86Instruction.Movlhps);
  503. }
  504. public void Movq(Operand dest, Operand source)
  505. {
  506. InstructionInfo info = _instTable[(int)X86Instruction.Movd];
  507. InstructionFlags flags = info.Flags | InstructionFlags.RexW;
  508. if (source.Type.IsInteger() || source.Kind == OperandKind.Memory)
  509. {
  510. WriteOpCode(dest, null, source, OperandType.None, flags, info.OpRRM, rrm: true);
  511. }
  512. else if (dest.Type.IsInteger() || dest.Kind == OperandKind.Memory)
  513. {
  514. WriteOpCode(dest, null, source, OperandType.None, flags, info.OpRMR);
  515. }
  516. else
  517. {
  518. WriteInstruction(dest, source, OperandType.None, X86Instruction.Movq);
  519. }
  520. }
  521. public void Movsd(Operand dest, Operand src1, Operand src2)
  522. {
  523. WriteInstruction(dest, src1, src2, X86Instruction.Movsd);
  524. }
  525. public void Movss(Operand dest, Operand src1, Operand src2)
  526. {
  527. WriteInstruction(dest, src1, src2, X86Instruction.Movss);
  528. }
  529. public void Movsx16(Operand dest, Operand source, OperandType type)
  530. {
  531. WriteInstruction(dest, source, type, X86Instruction.Movsx16);
  532. }
  533. public void Movsx32(Operand dest, Operand source, OperandType type)
  534. {
  535. WriteInstruction(dest, source, type, X86Instruction.Movsx32);
  536. }
  537. public void Movsx8(Operand dest, Operand source, OperandType type)
  538. {
  539. WriteInstruction(dest, source, type, X86Instruction.Movsx8);
  540. }
  541. public void Movzx16(Operand dest, Operand source, OperandType type)
  542. {
  543. WriteInstruction(dest, source, type, X86Instruction.Movzx16);
  544. }
  545. public void Movzx8(Operand dest, Operand source, OperandType type)
  546. {
  547. WriteInstruction(dest, source, type, X86Instruction.Movzx8);
  548. }
  549. public void Mul(Operand source)
  550. {
  551. WriteInstruction(null, source, source.Type, X86Instruction.Mul128);
  552. }
  553. public void Mulsd(Operand dest, Operand src1, Operand src2)
  554. {
  555. WriteInstruction(dest, src1, src2, X86Instruction.Mulsd);
  556. }
  557. public void Mulss(Operand dest, Operand src1, Operand src2)
  558. {
  559. WriteInstruction(dest, src1, src2, X86Instruction.Mulss);
  560. }
  561. public void Neg(Operand dest)
  562. {
  563. WriteInstruction(dest, null, dest.Type, X86Instruction.Neg);
  564. }
  565. public void Not(Operand dest)
  566. {
  567. WriteInstruction(dest, null, dest.Type, X86Instruction.Not);
  568. }
  569. public void Or(Operand dest, Operand source, OperandType type)
  570. {
  571. WriteInstruction(dest, source, type, X86Instruction.Or);
  572. }
  573. public void Pclmulqdq(Operand dest, Operand source, byte imm)
  574. {
  575. WriteInstruction(dest, null, source, X86Instruction.Pclmulqdq);
  576. WriteByte(imm);
  577. }
  578. public void Pcmpeqw(Operand dest, Operand src1, Operand src2)
  579. {
  580. WriteInstruction(dest, src1, src2, X86Instruction.Pcmpeqw);
  581. }
  582. public void Pextrb(Operand dest, Operand source, byte imm)
  583. {
  584. WriteInstruction(dest, null, source, X86Instruction.Pextrb);
  585. WriteByte(imm);
  586. }
  587. public void Pextrd(Operand dest, Operand source, byte imm)
  588. {
  589. WriteInstruction(dest, null, source, X86Instruction.Pextrd);
  590. WriteByte(imm);
  591. }
  592. public void Pextrq(Operand dest, Operand source, byte imm)
  593. {
  594. WriteInstruction(dest, null, source, X86Instruction.Pextrq);
  595. WriteByte(imm);
  596. }
  597. public void Pextrw(Operand dest, Operand source, byte imm)
  598. {
  599. WriteInstruction(dest, null, source, X86Instruction.Pextrw);
  600. WriteByte(imm);
  601. }
  602. public void Pinsrb(Operand dest, Operand src1, Operand src2, byte imm)
  603. {
  604. WriteInstruction(dest, src1, src2, X86Instruction.Pinsrb);
  605. WriteByte(imm);
  606. }
  607. public void Pinsrd(Operand dest, Operand src1, Operand src2, byte imm)
  608. {
  609. WriteInstruction(dest, src1, src2, X86Instruction.Pinsrd);
  610. WriteByte(imm);
  611. }
  612. public void Pinsrq(Operand dest, Operand src1, Operand src2, byte imm)
  613. {
  614. WriteInstruction(dest, src1, src2, X86Instruction.Pinsrq);
  615. WriteByte(imm);
  616. }
  617. public void Pinsrw(Operand dest, Operand src1, Operand src2, byte imm)
  618. {
  619. WriteInstruction(dest, src1, src2, X86Instruction.Pinsrw);
  620. WriteByte(imm);
  621. }
  622. public void Pop(Operand dest)
  623. {
  624. if (dest.Kind == OperandKind.Register)
  625. {
  626. WriteCompactInst(dest, 0x58);
  627. }
  628. else
  629. {
  630. WriteInstruction(dest, null, dest.Type, X86Instruction.Pop);
  631. }
  632. }
  633. public void Popcnt(Operand dest, Operand source, OperandType type)
  634. {
  635. WriteInstruction(dest, source, type, X86Instruction.Popcnt);
  636. }
  637. public void Pshufd(Operand dest, Operand source, byte imm)
  638. {
  639. WriteInstruction(dest, null, source, X86Instruction.Pshufd);
  640. WriteByte(imm);
  641. }
  642. public void Push(Operand source)
  643. {
  644. if (source.Kind == OperandKind.Register)
  645. {
  646. WriteCompactInst(source, 0x50);
  647. }
  648. else
  649. {
  650. WriteInstruction(null, source, source.Type, X86Instruction.Push);
  651. }
  652. }
  653. public void Return()
  654. {
  655. WriteByte(0xc3);
  656. }
  657. public void Ror(Operand dest, Operand source, OperandType type)
  658. {
  659. WriteShiftInst(dest, source, type, X86Instruction.Ror);
  660. }
  661. public void Sar(Operand dest, Operand source, OperandType type)
  662. {
  663. WriteShiftInst(dest, source, type, X86Instruction.Sar);
  664. }
  665. public void Shl(Operand dest, Operand source, OperandType type)
  666. {
  667. WriteShiftInst(dest, source, type, X86Instruction.Shl);
  668. }
  669. public void Shr(Operand dest, Operand source, OperandType type)
  670. {
  671. WriteShiftInst(dest, source, type, X86Instruction.Shr);
  672. }
  673. public void Setcc(Operand dest, X86Condition condition)
  674. {
  675. InstructionInfo info = _instTable[(int)X86Instruction.Setcc];
  676. WriteOpCode(dest, null, null, OperandType.None, info.Flags, info.OpRRM | (int)condition);
  677. }
  678. public void Sub(Operand dest, Operand source, OperandType type)
  679. {
  680. WriteInstruction(dest, source, type, X86Instruction.Sub);
  681. }
  682. public void Subsd(Operand dest, Operand src1, Operand src2)
  683. {
  684. WriteInstruction(dest, src1, src2, X86Instruction.Subsd);
  685. }
  686. public void Subss(Operand dest, Operand src1, Operand src2)
  687. {
  688. WriteInstruction(dest, src1, src2, X86Instruction.Subss);
  689. }
  690. public void Test(Operand src1, Operand src2, OperandType type)
  691. {
  692. WriteInstruction(src1, src2, type, X86Instruction.Test);
  693. }
  694. public void Xor(Operand dest, Operand source, OperandType type)
  695. {
  696. WriteInstruction(dest, source, type, X86Instruction.Xor);
  697. }
  698. public void Xorps(Operand dest, Operand src1, Operand src2)
  699. {
  700. WriteInstruction(dest, src1, src2, X86Instruction.Xorps);
  701. }
  702. public void WriteInstruction(
  703. X86Instruction inst,
  704. Operand dest,
  705. Operand source,
  706. OperandType type = OperandType.None)
  707. {
  708. WriteInstruction(dest, null, source, inst, type);
  709. }
  710. public void WriteInstruction(X86Instruction inst, Operand dest, Operand src1, Operand src2)
  711. {
  712. if (src2.Kind == OperandKind.Constant)
  713. {
  714. WriteInstruction(src1, dest, src2, inst);
  715. }
  716. else
  717. {
  718. WriteInstruction(dest, src1, src2, inst);
  719. }
  720. }
  721. public void WriteInstruction(
  722. X86Instruction inst,
  723. Operand dest,
  724. Operand src1,
  725. Operand src2,
  726. OperandType type)
  727. {
  728. WriteInstruction(dest, src1, src2, inst, type);
  729. }
  730. public void WriteInstruction(X86Instruction inst, Operand dest, Operand source, byte imm)
  731. {
  732. WriteInstruction(dest, null, source, inst);
  733. WriteByte(imm);
  734. }
  735. public void WriteInstruction(
  736. X86Instruction inst,
  737. Operand dest,
  738. Operand src1,
  739. Operand src2,
  740. Operand src3)
  741. {
  742. // 3+ operands can only be encoded with the VEX encoding scheme.
  743. Debug.Assert(HardwareCapabilities.SupportsVexEncoding);
  744. WriteInstruction(dest, src1, src2, inst);
  745. WriteByte((byte)(src3.AsByte() << 4));
  746. }
  747. public void WriteInstruction(
  748. X86Instruction inst,
  749. Operand dest,
  750. Operand src1,
  751. Operand src2,
  752. byte imm)
  753. {
  754. WriteInstruction(dest, src1, src2, inst);
  755. WriteByte(imm);
  756. }
  757. private void WriteShiftInst(Operand dest, Operand source, OperandType type, X86Instruction inst)
  758. {
  759. if (source.Kind == OperandKind.Register)
  760. {
  761. X86Register shiftReg = (X86Register)source.GetRegister().Index;
  762. Debug.Assert(shiftReg == X86Register.Rcx, $"Invalid shift register \"{shiftReg}\".");
  763. source = null;
  764. }
  765. else if (source.Kind == OperandKind.Constant)
  766. {
  767. source = source.With((uint)source.Value & (dest.Type == OperandType.I32 ? 0x1f : 0x3f));
  768. }
  769. WriteInstruction(dest, source, type, inst);
  770. }
  771. private void WriteInstruction(Operand dest, Operand source, OperandType type, X86Instruction inst)
  772. {
  773. InstructionInfo info = _instTable[(int)inst];
  774. if (source != null)
  775. {
  776. if (source.Kind == OperandKind.Constant)
  777. {
  778. ulong imm = source.Value;
  779. if (inst == X86Instruction.Mov8)
  780. {
  781. WriteOpCode(dest, null, null, type, info.Flags, info.OpRMImm8);
  782. WriteByte((byte)imm);
  783. }
  784. else if (inst == X86Instruction.Mov16)
  785. {
  786. WriteOpCode(dest, null, null, type, info.Flags, info.OpRMImm32);
  787. WriteInt16((short)imm);
  788. }
  789. else if (IsImm8(imm, type) && info.OpRMImm8 != BadOp)
  790. {
  791. WriteOpCode(dest, null, null, type, info.Flags, info.OpRMImm8);
  792. WriteByte((byte)imm);
  793. }
  794. else if (!source.Relocatable && IsImm32(imm, type) && info.OpRMImm32 != BadOp)
  795. {
  796. WriteOpCode(dest, null, null, type, info.Flags, info.OpRMImm32);
  797. WriteInt32((int)imm);
  798. }
  799. else if (dest != null && dest.Kind == OperandKind.Register && info.OpRImm64 != BadOp)
  800. {
  801. int? index = source.PtcIndex;
  802. int rexPrefix = GetRexPrefix(dest, source, type, rrm: false);
  803. if (rexPrefix != 0)
  804. {
  805. WriteByte((byte)rexPrefix);
  806. }
  807. WriteByte((byte)(info.OpRImm64 + (dest.GetRegister().Index & 0b111)));
  808. if (_ptcInfo != null && index != null)
  809. {
  810. _ptcInfo.WriteRelocEntry(new RelocEntry((int)_stream.Position, (int)index));
  811. }
  812. WriteUInt64(imm);
  813. }
  814. else
  815. {
  816. throw new ArgumentException($"Failed to encode constant 0x{imm:X}.");
  817. }
  818. }
  819. else if (source.Kind == OperandKind.Register && info.OpRMR != BadOp)
  820. {
  821. WriteOpCode(dest, null, source, type, info.Flags, info.OpRMR);
  822. }
  823. else if (info.OpRRM != BadOp)
  824. {
  825. WriteOpCode(dest, null, source, type, info.Flags, info.OpRRM, rrm: true);
  826. }
  827. else
  828. {
  829. throw new ArgumentException($"Invalid source operand kind \"{source.Kind}\".");
  830. }
  831. }
  832. else if (info.OpRRM != BadOp)
  833. {
  834. WriteOpCode(dest, null, source, type, info.Flags, info.OpRRM, rrm: true);
  835. }
  836. else if (info.OpRMR != BadOp)
  837. {
  838. WriteOpCode(dest, null, source, type, info.Flags, info.OpRMR);
  839. }
  840. else
  841. {
  842. throw new ArgumentNullException(nameof(source));
  843. }
  844. }
  845. private void WriteInstruction(
  846. Operand dest,
  847. Operand src1,
  848. Operand src2,
  849. X86Instruction inst,
  850. OperandType type = OperandType.None)
  851. {
  852. InstructionInfo info = _instTable[(int)inst];
  853. if (src2 != null)
  854. {
  855. if (src2.Kind == OperandKind.Constant)
  856. {
  857. ulong imm = src2.Value;
  858. if ((byte)imm == imm && info.OpRMImm8 != BadOp)
  859. {
  860. WriteOpCode(dest, src1, null, type, info.Flags, info.OpRMImm8);
  861. WriteByte((byte)imm);
  862. }
  863. else
  864. {
  865. throw new ArgumentException($"Failed to encode constant 0x{imm:X}.");
  866. }
  867. }
  868. else if (src2.Kind == OperandKind.Register && info.OpRMR != BadOp)
  869. {
  870. WriteOpCode(dest, src1, src2, type, info.Flags, info.OpRMR);
  871. }
  872. else if (info.OpRRM != BadOp)
  873. {
  874. WriteOpCode(dest, src1, src2, type, info.Flags, info.OpRRM, rrm: true);
  875. }
  876. else
  877. {
  878. throw new ArgumentException($"Invalid source operand kind \"{src2.Kind}\".");
  879. }
  880. }
  881. else if (info.OpRRM != BadOp)
  882. {
  883. WriteOpCode(dest, src1, src2, type, info.Flags, info.OpRRM, rrm: true);
  884. }
  885. else if (info.OpRMR != BadOp)
  886. {
  887. WriteOpCode(dest, src1, src2, type, info.Flags, info.OpRMR);
  888. }
  889. else
  890. {
  891. throw new ArgumentNullException(nameof(src2));
  892. }
  893. }
  894. private void WriteOpCode(
  895. Operand dest,
  896. Operand src1,
  897. Operand src2,
  898. OperandType type,
  899. InstructionFlags flags,
  900. int opCode,
  901. bool rrm = false)
  902. {
  903. int rexPrefix = GetRexPrefix(dest, src2, type, rrm);
  904. if ((flags & InstructionFlags.RexW) != 0)
  905. {
  906. rexPrefix |= RexWPrefix;
  907. }
  908. int modRM = (opCode >> OpModRMBits) << 3;
  909. MemoryOperand memOp = null;
  910. if (dest != null)
  911. {
  912. if (dest.Kind == OperandKind.Register)
  913. {
  914. int regIndex = dest.GetRegister().Index;
  915. modRM |= (regIndex & 0b111) << (rrm ? 3 : 0);
  916. if ((flags & InstructionFlags.Reg8Dest) != 0 && regIndex >= 4)
  917. {
  918. rexPrefix |= RexPrefix;
  919. }
  920. }
  921. else if (dest.Kind == OperandKind.Memory)
  922. {
  923. memOp = dest as MemoryOperand;
  924. }
  925. else
  926. {
  927. throw new ArgumentException("Invalid destination operand kind \"" + dest.Kind + "\".");
  928. }
  929. }
  930. if (src2 != null)
  931. {
  932. if (src2.Kind == OperandKind.Register)
  933. {
  934. int regIndex = src2.GetRegister().Index;
  935. modRM |= (regIndex & 0b111) << (rrm ? 0 : 3);
  936. if ((flags & InstructionFlags.Reg8Src) != 0 && regIndex >= 4)
  937. {
  938. rexPrefix |= RexPrefix;
  939. }
  940. }
  941. else if (src2.Kind == OperandKind.Memory && memOp == null)
  942. {
  943. memOp = src2 as MemoryOperand;
  944. }
  945. else
  946. {
  947. throw new ArgumentException("Invalid source operand kind \"" + src2.Kind + "\".");
  948. }
  949. }
  950. bool needsSibByte = false;
  951. bool needsDisplacement = false;
  952. int sib = 0;
  953. if (memOp != null)
  954. {
  955. // Either source or destination is a memory operand.
  956. Register baseReg = memOp.BaseAddress.GetRegister();
  957. X86Register baseRegLow = (X86Register)(baseReg.Index & 0b111);
  958. needsSibByte = memOp.Index != null || baseRegLow == X86Register.Rsp;
  959. needsDisplacement = memOp.Displacement != 0 || baseRegLow == X86Register.Rbp;
  960. if (needsDisplacement)
  961. {
  962. if (ConstFitsOnS8(memOp.Displacement))
  963. {
  964. modRM |= 0x40;
  965. }
  966. else /* if (ConstFitsOnS32(memOp.Displacement)) */
  967. {
  968. modRM |= 0x80;
  969. }
  970. }
  971. if (baseReg.Index >= 8)
  972. {
  973. Debug.Assert((uint)baseReg.Index <= MaxRegNumber);
  974. rexPrefix |= RexPrefix | (baseReg.Index >> 3);
  975. }
  976. if (needsSibByte)
  977. {
  978. sib = (int)baseRegLow;
  979. if (memOp.Index != null)
  980. {
  981. int indexReg = memOp.Index.GetRegister().Index;
  982. Debug.Assert(indexReg != (int)X86Register.Rsp, "Using RSP as index register on the memory operand is not allowed.");
  983. if (indexReg >= 8)
  984. {
  985. Debug.Assert((uint)indexReg <= MaxRegNumber);
  986. rexPrefix |= RexPrefix | (indexReg >> 3) << 1;
  987. }
  988. sib |= (indexReg & 0b111) << 3;
  989. }
  990. else
  991. {
  992. sib |= 0b100 << 3;
  993. }
  994. sib |= (int)memOp.Scale << 6;
  995. modRM |= 0b100;
  996. }
  997. else
  998. {
  999. modRM |= (int)baseRegLow;
  1000. }
  1001. }
  1002. else
  1003. {
  1004. // Source and destination are registers.
  1005. modRM |= 0xc0;
  1006. }
  1007. Debug.Assert(opCode != BadOp, "Invalid opcode value.");
  1008. if ((flags & InstructionFlags.Vex) != 0 && HardwareCapabilities.SupportsVexEncoding)
  1009. {
  1010. // In a vex encoding, only one prefix can be active at a time. The active prefix is encoded in the second byte using two bits.
  1011. int vexByte2 = (flags & InstructionFlags.PrefixMask) switch
  1012. {
  1013. InstructionFlags.Prefix66 => 1,
  1014. InstructionFlags.PrefixF3 => 2,
  1015. InstructionFlags.PrefixF2 => 3,
  1016. _ => 0
  1017. };
  1018. if (src1 != null)
  1019. {
  1020. vexByte2 |= (src1.GetRegister().Index ^ 0xf) << 3;
  1021. }
  1022. else
  1023. {
  1024. vexByte2 |= 0b1111 << 3;
  1025. }
  1026. ushort opCodeHigh = (ushort)(opCode >> 8);
  1027. if ((rexPrefix & 0b1011) == 0 && opCodeHigh == 0xf)
  1028. {
  1029. // Two-byte form.
  1030. WriteByte(0xc5);
  1031. vexByte2 |= (~rexPrefix & 4) << 5;
  1032. WriteByte((byte)vexByte2);
  1033. }
  1034. else
  1035. {
  1036. // Three-byte form.
  1037. WriteByte(0xc4);
  1038. int vexByte1 = (~rexPrefix & 7) << 5;
  1039. switch (opCodeHigh)
  1040. {
  1041. case 0xf: vexByte1 |= 1; break;
  1042. case 0xf38: vexByte1 |= 2; break;
  1043. case 0xf3a: vexByte1 |= 3; break;
  1044. default: Debug.Assert(false, $"Failed to VEX encode opcode 0x{opCode:X}."); break;
  1045. }
  1046. vexByte2 |= (rexPrefix & 8) << 4;
  1047. WriteByte((byte)vexByte1);
  1048. WriteByte((byte)vexByte2);
  1049. }
  1050. opCode &= 0xff;
  1051. }
  1052. else
  1053. {
  1054. if (flags.HasFlag(InstructionFlags.Prefix66))
  1055. {
  1056. WriteByte(0x66);
  1057. }
  1058. if (flags.HasFlag(InstructionFlags.PrefixF2))
  1059. {
  1060. WriteByte(0xf2);
  1061. }
  1062. if (flags.HasFlag(InstructionFlags.PrefixF3))
  1063. {
  1064. WriteByte(0xf3);
  1065. }
  1066. if (rexPrefix != 0)
  1067. {
  1068. WriteByte((byte)rexPrefix);
  1069. }
  1070. }
  1071. if (dest != null && (flags & InstructionFlags.RegOnly) != 0)
  1072. {
  1073. opCode += dest.GetRegister().Index & 7;
  1074. }
  1075. if ((opCode & 0xff0000) != 0)
  1076. {
  1077. WriteByte((byte)(opCode >> 16));
  1078. }
  1079. if ((opCode & 0xff00) != 0)
  1080. {
  1081. WriteByte((byte)(opCode >> 8));
  1082. }
  1083. WriteByte((byte)opCode);
  1084. if ((flags & InstructionFlags.RegOnly) == 0)
  1085. {
  1086. WriteByte((byte)modRM);
  1087. if (needsSibByte)
  1088. {
  1089. WriteByte((byte)sib);
  1090. }
  1091. if (needsDisplacement)
  1092. {
  1093. if (ConstFitsOnS8(memOp.Displacement))
  1094. {
  1095. WriteByte((byte)memOp.Displacement);
  1096. }
  1097. else /* if (ConstFitsOnS32(memOp.Displacement)) */
  1098. {
  1099. WriteInt32(memOp.Displacement);
  1100. }
  1101. }
  1102. }
  1103. }
  1104. private void WriteCompactInst(Operand operand, int opCode)
  1105. {
  1106. int regIndex = operand.GetRegister().Index;
  1107. if (regIndex >= 8)
  1108. {
  1109. WriteByte(0x41);
  1110. }
  1111. WriteByte((byte)(opCode + (regIndex & 0b111)));
  1112. }
  1113. private static int GetRexPrefix(Operand dest, Operand source, OperandType type, bool rrm)
  1114. {
  1115. int rexPrefix = 0;
  1116. if (Is64Bits(type))
  1117. {
  1118. rexPrefix = RexWPrefix;
  1119. }
  1120. void SetRegisterHighBit(Register reg, int bit)
  1121. {
  1122. if (reg.Index >= 8)
  1123. {
  1124. rexPrefix |= RexPrefix | (reg.Index >> 3) << bit;
  1125. }
  1126. }
  1127. if (dest != null && dest.Kind == OperandKind.Register)
  1128. {
  1129. SetRegisterHighBit(dest.GetRegister(), rrm ? 2 : 0);
  1130. }
  1131. if (source != null && source.Kind == OperandKind.Register)
  1132. {
  1133. SetRegisterHighBit(source.GetRegister(), rrm ? 0 : 2);
  1134. }
  1135. return rexPrefix;
  1136. }
  1137. private static bool Is64Bits(OperandType type)
  1138. {
  1139. return type == OperandType.I64 || type == OperandType.FP64;
  1140. }
  1141. private static bool IsImm8(ulong immediate, OperandType type)
  1142. {
  1143. long value = type == OperandType.I32 ? (int)immediate : (long)immediate;
  1144. return ConstFitsOnS8(value);
  1145. }
  1146. private static bool IsImm32(ulong immediate, OperandType type)
  1147. {
  1148. long value = type == OperandType.I32 ? (int)immediate : (long)immediate;
  1149. return ConstFitsOnS32(value);
  1150. }
  1151. public static int GetJccLength(long offset, bool ptcDisabled = true)
  1152. {
  1153. if (ptcDisabled && ConstFitsOnS8(offset < 0 ? offset - 2 : offset))
  1154. {
  1155. return 2;
  1156. }
  1157. else if (ConstFitsOnS32(offset < 0 ? offset - 6 : offset))
  1158. {
  1159. return 6;
  1160. }
  1161. else
  1162. {
  1163. throw new ArgumentOutOfRangeException(nameof(offset));
  1164. }
  1165. }
  1166. public static int GetJmpLength(long offset, bool ptcDisabled = true)
  1167. {
  1168. if (ptcDisabled && ConstFitsOnS8(offset < 0 ? offset - 2 : offset))
  1169. {
  1170. return 2;
  1171. }
  1172. else if (ConstFitsOnS32(offset < 0 ? offset - 5 : offset))
  1173. {
  1174. return 5;
  1175. }
  1176. else
  1177. {
  1178. throw new ArgumentOutOfRangeException(nameof(offset));
  1179. }
  1180. }
  1181. private static bool ConstFitsOnS8(long value)
  1182. {
  1183. return value == (sbyte)value;
  1184. }
  1185. private static bool ConstFitsOnS32(long value)
  1186. {
  1187. return value == (int)value;
  1188. }
  1189. private void WriteInt16(short value)
  1190. {
  1191. WriteUInt16((ushort)value);
  1192. }
  1193. private void WriteInt32(int value)
  1194. {
  1195. WriteUInt32((uint)value);
  1196. }
  1197. private void WriteByte(byte value)
  1198. {
  1199. _stream.WriteByte(value);
  1200. }
  1201. private void WriteUInt16(ushort value)
  1202. {
  1203. _stream.WriteByte((byte)(value >> 0));
  1204. _stream.WriteByte((byte)(value >> 8));
  1205. }
  1206. private void WriteUInt32(uint value)
  1207. {
  1208. _stream.WriteByte((byte)(value >> 0));
  1209. _stream.WriteByte((byte)(value >> 8));
  1210. _stream.WriteByte((byte)(value >> 16));
  1211. _stream.WriteByte((byte)(value >> 24));
  1212. }
  1213. private void WriteUInt64(ulong value)
  1214. {
  1215. _stream.WriteByte((byte)(value >> 0));
  1216. _stream.WriteByte((byte)(value >> 8));
  1217. _stream.WriteByte((byte)(value >> 16));
  1218. _stream.WriteByte((byte)(value >> 24));
  1219. _stream.WriteByte((byte)(value >> 32));
  1220. _stream.WriteByte((byte)(value >> 40));
  1221. _stream.WriteByte((byte)(value >> 48));
  1222. _stream.WriteByte((byte)(value >> 56));
  1223. }
  1224. }
  1225. }