Assembler.cs 68 KB

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