Assembler.cs 74 KB

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