CodeGenerator.cs 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. using ARMeilleure.CodeGen.Optimizations;
  2. using ARMeilleure.CodeGen.RegisterAllocators;
  3. using ARMeilleure.CodeGen.Unwinding;
  4. using ARMeilleure.Common;
  5. using ARMeilleure.Diagnostics;
  6. using ARMeilleure.IntermediateRepresentation;
  7. using ARMeilleure.Translation;
  8. using ARMeilleure.Translation.PTC;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Numerics;
  14. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  15. namespace ARMeilleure.CodeGen.X86
  16. {
  17. static class CodeGenerator
  18. {
  19. private const int PageSize = 0x1000;
  20. private const int StackGuardSize = 0x2000;
  21. private static Action<CodeGenContext, Operation>[] _instTable;
  22. static CodeGenerator()
  23. {
  24. _instTable = new Action<CodeGenContext, Operation>[EnumUtils.GetCount(typeof(Instruction))];
  25. Add(Instruction.Add, GenerateAdd);
  26. Add(Instruction.BitwiseAnd, GenerateBitwiseAnd);
  27. Add(Instruction.BitwiseExclusiveOr, GenerateBitwiseExclusiveOr);
  28. Add(Instruction.BitwiseNot, GenerateBitwiseNot);
  29. Add(Instruction.BitwiseOr, GenerateBitwiseOr);
  30. Add(Instruction.Branch, GenerateBranch);
  31. Add(Instruction.BranchIfFalse, GenerateBranchIfFalse);
  32. Add(Instruction.BranchIfTrue, GenerateBranchIfTrue);
  33. Add(Instruction.ByteSwap, GenerateByteSwap);
  34. Add(Instruction.Call, GenerateCall);
  35. Add(Instruction.Clobber, GenerateClobber);
  36. Add(Instruction.CompareAndSwap, GenerateCompareAndSwap);
  37. Add(Instruction.CompareEqual, GenerateCompareEqual);
  38. Add(Instruction.CompareGreater, GenerateCompareGreater);
  39. Add(Instruction.CompareGreaterOrEqual, GenerateCompareGreaterOrEqual);
  40. Add(Instruction.CompareGreaterOrEqualUI, GenerateCompareGreaterOrEqualUI);
  41. Add(Instruction.CompareGreaterUI, GenerateCompareGreaterUI);
  42. Add(Instruction.CompareLess, GenerateCompareLess);
  43. Add(Instruction.CompareLessOrEqual, GenerateCompareLessOrEqual);
  44. Add(Instruction.CompareLessOrEqualUI, GenerateCompareLessOrEqualUI);
  45. Add(Instruction.CompareLessUI, GenerateCompareLessUI);
  46. Add(Instruction.CompareNotEqual, GenerateCompareNotEqual);
  47. Add(Instruction.ConditionalSelect, GenerateConditionalSelect);
  48. Add(Instruction.ConvertI64ToI32, GenerateConvertI64ToI32);
  49. Add(Instruction.ConvertToFP, GenerateConvertToFP);
  50. Add(Instruction.Copy, GenerateCopy);
  51. Add(Instruction.CountLeadingZeros, GenerateCountLeadingZeros);
  52. Add(Instruction.Divide, GenerateDivide);
  53. Add(Instruction.DivideUI, GenerateDivideUI);
  54. Add(Instruction.Fill, GenerateFill);
  55. Add(Instruction.Load, GenerateLoad);
  56. Add(Instruction.Load16, GenerateLoad16);
  57. Add(Instruction.Load8, GenerateLoad8);
  58. Add(Instruction.Multiply, GenerateMultiply);
  59. Add(Instruction.Multiply64HighSI, GenerateMultiply64HighSI);
  60. Add(Instruction.Multiply64HighUI, GenerateMultiply64HighUI);
  61. Add(Instruction.Negate, GenerateNegate);
  62. Add(Instruction.Return, GenerateReturn);
  63. Add(Instruction.RotateRight, GenerateRotateRight);
  64. Add(Instruction.ShiftLeft, GenerateShiftLeft);
  65. Add(Instruction.ShiftRightSI, GenerateShiftRightSI);
  66. Add(Instruction.ShiftRightUI, GenerateShiftRightUI);
  67. Add(Instruction.SignExtend16, GenerateSignExtend16);
  68. Add(Instruction.SignExtend32, GenerateSignExtend32);
  69. Add(Instruction.SignExtend8, GenerateSignExtend8);
  70. Add(Instruction.Spill, GenerateSpill);
  71. Add(Instruction.SpillArg, GenerateSpillArg);
  72. Add(Instruction.StackAlloc, GenerateStackAlloc);
  73. Add(Instruction.Store, GenerateStore);
  74. Add(Instruction.Store16, GenerateStore16);
  75. Add(Instruction.Store8, GenerateStore8);
  76. Add(Instruction.Subtract, GenerateSubtract);
  77. Add(Instruction.Tailcall, GenerateTailcall);
  78. Add(Instruction.VectorCreateScalar, GenerateVectorCreateScalar);
  79. Add(Instruction.VectorExtract, GenerateVectorExtract);
  80. Add(Instruction.VectorExtract16, GenerateVectorExtract16);
  81. Add(Instruction.VectorExtract8, GenerateVectorExtract8);
  82. Add(Instruction.VectorInsert, GenerateVectorInsert);
  83. Add(Instruction.VectorInsert16, GenerateVectorInsert16);
  84. Add(Instruction.VectorInsert8, GenerateVectorInsert8);
  85. Add(Instruction.VectorOne, GenerateVectorOne);
  86. Add(Instruction.VectorZero, GenerateVectorZero);
  87. Add(Instruction.VectorZeroUpper64, GenerateVectorZeroUpper64);
  88. Add(Instruction.VectorZeroUpper96, GenerateVectorZeroUpper96);
  89. Add(Instruction.ZeroExtend16, GenerateZeroExtend16);
  90. Add(Instruction.ZeroExtend32, GenerateZeroExtend32);
  91. Add(Instruction.ZeroExtend8, GenerateZeroExtend8);
  92. }
  93. private static void Add(Instruction inst, Action<CodeGenContext, Operation> func)
  94. {
  95. _instTable[(int)inst] = func;
  96. }
  97. public static CompiledFunction Generate(CompilerContext cctx, PtcInfo ptcInfo = null)
  98. {
  99. ControlFlowGraph cfg = cctx.Cfg;
  100. Logger.StartPass(PassName.Optimization);
  101. if ((cctx.Options & CompilerOptions.SsaForm) != 0 &&
  102. (cctx.Options & CompilerOptions.Optimize) != 0)
  103. {
  104. Optimizer.RunPass(cfg);
  105. }
  106. X86Optimizer.RunPass(cfg);
  107. Logger.EndPass(PassName.Optimization, cfg);
  108. Logger.StartPass(PassName.PreAllocation);
  109. StackAllocator stackAlloc = new StackAllocator();
  110. PreAllocator.RunPass(cctx, stackAlloc, out int maxCallArgs);
  111. Logger.EndPass(PassName.PreAllocation, cfg);
  112. Logger.StartPass(PassName.RegisterAllocation);
  113. if ((cctx.Options & CompilerOptions.SsaForm) != 0)
  114. {
  115. Ssa.Deconstruct(cfg);
  116. }
  117. IRegisterAllocator regAlloc;
  118. if ((cctx.Options & CompilerOptions.Lsra) != 0)
  119. {
  120. regAlloc = new LinearScanAllocator();
  121. }
  122. else
  123. {
  124. regAlloc = new HybridAllocator();
  125. }
  126. RegisterMasks regMasks = new RegisterMasks(
  127. CallingConvention.GetIntAvailableRegisters(),
  128. CallingConvention.GetVecAvailableRegisters(),
  129. CallingConvention.GetIntCallerSavedRegisters(),
  130. CallingConvention.GetVecCallerSavedRegisters(),
  131. CallingConvention.GetIntCalleeSavedRegisters(),
  132. CallingConvention.GetVecCalleeSavedRegisters());
  133. AllocationResult allocResult = regAlloc.RunPass(cfg, stackAlloc, regMasks);
  134. Logger.EndPass(PassName.RegisterAllocation, cfg);
  135. Logger.StartPass(PassName.CodeGeneration);
  136. using (MemoryStream stream = new MemoryStream())
  137. {
  138. CodeGenContext context = new CodeGenContext(stream, allocResult, maxCallArgs, cfg.Blocks.Count, ptcInfo);
  139. UnwindInfo unwindInfo = WritePrologue(context);
  140. ptcInfo?.WriteUnwindInfo(unwindInfo);
  141. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  142. {
  143. context.EnterBlock(block);
  144. for (Node node = block.Operations.First; node != null; node = node.ListNext)
  145. {
  146. if (node is Operation operation)
  147. {
  148. GenerateOperation(context, operation);
  149. }
  150. }
  151. }
  152. Logger.EndPass(PassName.CodeGeneration);
  153. return new CompiledFunction(context.GetCode(), unwindInfo);
  154. }
  155. }
  156. private static void GenerateOperation(CodeGenContext context, Operation operation)
  157. {
  158. if (operation.Instruction == Instruction.Extended)
  159. {
  160. IntrinsicOperation intrinOp = (IntrinsicOperation)operation;
  161. IntrinsicInfo info = IntrinsicTable.GetInfo(intrinOp.Intrinsic);
  162. switch (info.Type)
  163. {
  164. case IntrinsicType.Comis_:
  165. {
  166. Operand dest = operation.Destination;
  167. Operand src1 = operation.GetSource(0);
  168. Operand src2 = operation.GetSource(1);
  169. switch (intrinOp.Intrinsic)
  170. {
  171. case Intrinsic.X86Comisdeq:
  172. context.Assembler.Comisd(src1, src2);
  173. context.Assembler.Setcc(dest, X86Condition.Equal);
  174. break;
  175. case Intrinsic.X86Comisdge:
  176. context.Assembler.Comisd(src1, src2);
  177. context.Assembler.Setcc(dest, X86Condition.AboveOrEqual);
  178. break;
  179. case Intrinsic.X86Comisdlt:
  180. context.Assembler.Comisd(src1, src2);
  181. context.Assembler.Setcc(dest, X86Condition.Below);
  182. break;
  183. case Intrinsic.X86Comisseq:
  184. context.Assembler.Comiss(src1, src2);
  185. context.Assembler.Setcc(dest, X86Condition.Equal);
  186. break;
  187. case Intrinsic.X86Comissge:
  188. context.Assembler.Comiss(src1, src2);
  189. context.Assembler.Setcc(dest, X86Condition.AboveOrEqual);
  190. break;
  191. case Intrinsic.X86Comisslt:
  192. context.Assembler.Comiss(src1, src2);
  193. context.Assembler.Setcc(dest, X86Condition.Below);
  194. break;
  195. }
  196. context.Assembler.Movzx8(dest, dest, OperandType.I32);
  197. break;
  198. }
  199. case IntrinsicType.PopCount:
  200. {
  201. Operand dest = operation.Destination;
  202. Operand source = operation.GetSource(0);
  203. EnsureSameType(dest, source);
  204. Debug.Assert(dest.Type.IsInteger());
  205. context.Assembler.Popcnt(dest, source, dest.Type);
  206. break;
  207. }
  208. case IntrinsicType.Unary:
  209. {
  210. Operand dest = operation.Destination;
  211. Operand source = operation.GetSource(0);
  212. EnsureSameType(dest, source);
  213. Debug.Assert(!dest.Type.IsInteger());
  214. context.Assembler.WriteInstruction(info.Inst, dest, source);
  215. break;
  216. }
  217. case IntrinsicType.UnaryToGpr:
  218. {
  219. Operand dest = operation.Destination;
  220. Operand source = operation.GetSource(0);
  221. Debug.Assert(dest.Type.IsInteger() && !source.Type.IsInteger());
  222. if (intrinOp.Intrinsic == Intrinsic.X86Cvtsi2si)
  223. {
  224. if (dest.Type == OperandType.I32)
  225. {
  226. context.Assembler.Movd(dest, source); // int _mm_cvtsi128_si32(__m128i a)
  227. }
  228. else /* if (dest.Type == OperandType.I64) */
  229. {
  230. context.Assembler.Movq(dest, source); // __int64 _mm_cvtsi128_si64(__m128i a)
  231. }
  232. }
  233. else
  234. {
  235. context.Assembler.WriteInstruction(info.Inst, dest, source, dest.Type);
  236. }
  237. break;
  238. }
  239. case IntrinsicType.Binary:
  240. {
  241. Operand dest = operation.Destination;
  242. Operand src1 = operation.GetSource(0);
  243. Operand src2 = operation.GetSource(1);
  244. EnsureSameType(dest, src1);
  245. if (!HardwareCapabilities.SupportsVexEncoding)
  246. {
  247. EnsureSameReg(dest, src1);
  248. }
  249. Debug.Assert(!dest.Type.IsInteger());
  250. Debug.Assert(!src2.Type.IsInteger() || src2.Kind == OperandKind.Constant);
  251. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2);
  252. break;
  253. }
  254. case IntrinsicType.BinaryGpr:
  255. {
  256. Operand dest = operation.Destination;
  257. Operand src1 = operation.GetSource(0);
  258. Operand src2 = operation.GetSource(1);
  259. EnsureSameType(dest, src1);
  260. if (!HardwareCapabilities.SupportsVexEncoding)
  261. {
  262. EnsureSameReg(dest, src1);
  263. }
  264. Debug.Assert(!dest.Type.IsInteger() && src2.Type.IsInteger());
  265. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2, src2.Type);
  266. break;
  267. }
  268. case IntrinsicType.Crc32:
  269. {
  270. Operand dest = operation.Destination;
  271. Operand src1 = operation.GetSource(0);
  272. Operand src2 = operation.GetSource(1);
  273. EnsureSameReg(dest, src1);
  274. Debug.Assert(dest.Type.IsInteger() && src1.Type.IsInteger() && src2.Type.IsInteger());
  275. context.Assembler.WriteInstruction(info.Inst, dest, src2, dest.Type);
  276. break;
  277. }
  278. case IntrinsicType.BinaryImm:
  279. {
  280. Operand dest = operation.Destination;
  281. Operand src1 = operation.GetSource(0);
  282. Operand src2 = operation.GetSource(1);
  283. EnsureSameType(dest, src1);
  284. if (!HardwareCapabilities.SupportsVexEncoding)
  285. {
  286. EnsureSameReg(dest, src1);
  287. }
  288. Debug.Assert(!dest.Type.IsInteger() && src2.Kind == OperandKind.Constant);
  289. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2.AsByte());
  290. break;
  291. }
  292. case IntrinsicType.Ternary:
  293. {
  294. Operand dest = operation.Destination;
  295. Operand src1 = operation.GetSource(0);
  296. Operand src2 = operation.GetSource(1);
  297. Operand src3 = operation.GetSource(2);
  298. EnsureSameType(dest, src1, src2, src3);
  299. Debug.Assert(!dest.Type.IsInteger());
  300. if (info.Inst == X86Instruction.Blendvpd && HardwareCapabilities.SupportsVexEncoding)
  301. {
  302. context.Assembler.WriteInstruction(X86Instruction.Vblendvpd, dest, src1, src2, src3);
  303. }
  304. else if (info.Inst == X86Instruction.Blendvps && HardwareCapabilities.SupportsVexEncoding)
  305. {
  306. context.Assembler.WriteInstruction(X86Instruction.Vblendvps, dest, src1, src2, src3);
  307. }
  308. else if (info.Inst == X86Instruction.Pblendvb && HardwareCapabilities.SupportsVexEncoding)
  309. {
  310. context.Assembler.WriteInstruction(X86Instruction.Vpblendvb, dest, src1, src2, src3);
  311. }
  312. else
  313. {
  314. EnsureSameReg(dest, src1);
  315. Debug.Assert(src3.GetRegister().Index == 0);
  316. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2);
  317. }
  318. break;
  319. }
  320. case IntrinsicType.TernaryImm:
  321. {
  322. Operand dest = operation.Destination;
  323. Operand src1 = operation.GetSource(0);
  324. Operand src2 = operation.GetSource(1);
  325. Operand src3 = operation.GetSource(2);
  326. EnsureSameType(dest, src1, src2);
  327. if (!HardwareCapabilities.SupportsVexEncoding)
  328. {
  329. EnsureSameReg(dest, src1);
  330. }
  331. Debug.Assert(!dest.Type.IsInteger() && src3.Kind == OperandKind.Constant);
  332. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2, src3.AsByte());
  333. break;
  334. }
  335. }
  336. }
  337. else
  338. {
  339. Action<CodeGenContext, Operation> func = _instTable[(int)operation.Instruction];
  340. if (func != null)
  341. {
  342. func(context, operation);
  343. }
  344. else
  345. {
  346. throw new ArgumentException($"Invalid instruction \"{operation.Instruction}\".");
  347. }
  348. }
  349. }
  350. private static void GenerateAdd(CodeGenContext context, Operation operation)
  351. {
  352. Operand dest = operation.Destination;
  353. Operand src1 = operation.GetSource(0);
  354. Operand src2 = operation.GetSource(1);
  355. ValidateBinOp(dest, src1, src2);
  356. if (dest.Type.IsInteger())
  357. {
  358. context.Assembler.Add(dest, src2, dest.Type);
  359. }
  360. else if (dest.Type == OperandType.FP32)
  361. {
  362. context.Assembler.Addss(dest, src1, src2);
  363. }
  364. else /* if (dest.Type == OperandType.FP64) */
  365. {
  366. context.Assembler.Addsd(dest, src1, src2);
  367. }
  368. }
  369. private static void GenerateBitwiseAnd(CodeGenContext context, Operation operation)
  370. {
  371. Operand dest = operation.Destination;
  372. Operand src1 = operation.GetSource(0);
  373. Operand src2 = operation.GetSource(1);
  374. ValidateBinOp(dest, src1, src2);
  375. Debug.Assert(dest.Type.IsInteger());
  376. context.Assembler.And(dest, src2, dest.Type);
  377. }
  378. private static void GenerateBitwiseExclusiveOr(CodeGenContext context, Operation operation)
  379. {
  380. Operand dest = operation.Destination;
  381. Operand src1 = operation.GetSource(0);
  382. Operand src2 = operation.GetSource(1);
  383. ValidateBinOp(dest, src1, src2);
  384. if (dest.Type.IsInteger())
  385. {
  386. context.Assembler.Xor(dest, src2, dest.Type);
  387. }
  388. else
  389. {
  390. context.Assembler.Xorps(dest, src1, src2);
  391. }
  392. }
  393. private static void GenerateBitwiseNot(CodeGenContext context, Operation operation)
  394. {
  395. Operand dest = operation.Destination;
  396. Operand source = operation.GetSource(0);
  397. ValidateUnOp(dest, source);
  398. Debug.Assert(dest.Type.IsInteger());
  399. context.Assembler.Not(dest);
  400. }
  401. private static void GenerateBitwiseOr(CodeGenContext context, Operation operation)
  402. {
  403. Operand dest = operation.Destination;
  404. Operand src1 = operation.GetSource(0);
  405. Operand src2 = operation.GetSource(1);
  406. ValidateBinOp(dest, src1, src2);
  407. Debug.Assert(dest.Type.IsInteger());
  408. context.Assembler.Or(dest, src2, dest.Type);
  409. }
  410. private static void GenerateBranch(CodeGenContext context, Operation operation)
  411. {
  412. context.JumpTo(context.CurrBlock.Branch);
  413. }
  414. private static void GenerateBranchIfFalse(CodeGenContext context, Operation operation)
  415. {
  416. Operand source = operation.GetSource(0);
  417. context.Assembler.Test(source, source, source.Type);
  418. context.JumpTo(X86Condition.Equal, context.CurrBlock.Branch);
  419. }
  420. private static void GenerateBranchIfTrue(CodeGenContext context, Operation operation)
  421. {
  422. Operand source = operation.GetSource(0);
  423. context.Assembler.Test(source, source, source.Type);
  424. context.JumpTo(X86Condition.NotEqual, context.CurrBlock.Branch);
  425. }
  426. private static void GenerateByteSwap(CodeGenContext context, Operation operation)
  427. {
  428. Operand dest = operation.Destination;
  429. Operand source = operation.GetSource(0);
  430. ValidateUnOp(dest, source);
  431. Debug.Assert(dest.Type.IsInteger());
  432. context.Assembler.Bswap(dest);
  433. }
  434. private static void GenerateCall(CodeGenContext context, Operation operation)
  435. {
  436. context.Assembler.Call(operation.GetSource(0));
  437. }
  438. private static void GenerateClobber(CodeGenContext context, Operation operation)
  439. {
  440. // This is only used to indicate that a register is clobbered to the
  441. // register allocator, we don't need to produce any code.
  442. }
  443. private static void GenerateCompareAndSwap(CodeGenContext context, Operation operation)
  444. {
  445. Operand src1 = operation.GetSource(0);
  446. if (operation.SourcesCount == 5) // CompareAndSwap128 has 5 sources, compared to CompareAndSwap64/32's 3.
  447. {
  448. MemoryOperand memOp = MemoryOp(OperandType.I64, src1);
  449. context.Assembler.Cmpxchg16b(memOp);
  450. }
  451. else
  452. {
  453. Operand src2 = operation.GetSource(1);
  454. Operand src3 = operation.GetSource(2);
  455. EnsureSameType(src2, src3);
  456. MemoryOperand memOp = MemoryOp(src3.Type, src1);
  457. context.Assembler.Cmpxchg(memOp, src3);
  458. }
  459. }
  460. private static void GenerateCompareEqual(CodeGenContext context, Operation operation)
  461. {
  462. GenerateCompare(context, operation, X86Condition.Equal);
  463. }
  464. private static void GenerateCompareGreater(CodeGenContext context, Operation operation)
  465. {
  466. GenerateCompare(context, operation, X86Condition.Greater);
  467. }
  468. private static void GenerateCompareGreaterOrEqual(CodeGenContext context, Operation operation)
  469. {
  470. GenerateCompare(context, operation, X86Condition.GreaterOrEqual);
  471. }
  472. private static void GenerateCompareGreaterOrEqualUI(CodeGenContext context, Operation operation)
  473. {
  474. GenerateCompare(context, operation, X86Condition.AboveOrEqual);
  475. }
  476. private static void GenerateCompareGreaterUI(CodeGenContext context, Operation operation)
  477. {
  478. GenerateCompare(context, operation, X86Condition.Above);
  479. }
  480. private static void GenerateCompareLess(CodeGenContext context, Operation operation)
  481. {
  482. GenerateCompare(context, operation, X86Condition.Less);
  483. }
  484. private static void GenerateCompareLessOrEqual(CodeGenContext context, Operation operation)
  485. {
  486. GenerateCompare(context, operation, X86Condition.LessOrEqual);
  487. }
  488. private static void GenerateCompareLessOrEqualUI(CodeGenContext context, Operation operation)
  489. {
  490. GenerateCompare(context, operation, X86Condition.BelowOrEqual);
  491. }
  492. private static void GenerateCompareLessUI(CodeGenContext context, Operation operation)
  493. {
  494. GenerateCompare(context, operation, X86Condition.Below);
  495. }
  496. private static void GenerateCompareNotEqual(CodeGenContext context, Operation operation)
  497. {
  498. GenerateCompare(context, operation, X86Condition.NotEqual);
  499. }
  500. private static void GenerateCompare(CodeGenContext context, Operation operation, X86Condition condition)
  501. {
  502. Operand dest = operation.Destination;
  503. Operand src1 = operation.GetSource(0);
  504. Operand src2 = operation.GetSource(1);
  505. EnsureSameType(src1, src2);
  506. Debug.Assert(dest.Type == OperandType.I32);
  507. context.Assembler.Cmp(src1, src2, src1.Type);
  508. context.Assembler.Setcc(dest, condition);
  509. context.Assembler.Movzx8(dest, dest, OperandType.I32);
  510. }
  511. private static void GenerateConditionalSelect(CodeGenContext context, Operation operation)
  512. {
  513. Operand dest = operation.Destination;
  514. Operand src1 = operation.GetSource(0);
  515. Operand src2 = operation.GetSource(1);
  516. Operand src3 = operation.GetSource(2);
  517. EnsureSameReg (dest, src3);
  518. EnsureSameType(dest, src2, src3);
  519. Debug.Assert(dest.Type.IsInteger());
  520. Debug.Assert(src1.Type == OperandType.I32);
  521. context.Assembler.Test (src1, src1, src1.Type);
  522. context.Assembler.Cmovcc(dest, src2, dest.Type, X86Condition.NotEqual);
  523. }
  524. private static void GenerateConvertI64ToI32(CodeGenContext context, Operation operation)
  525. {
  526. Operand dest = operation.Destination;
  527. Operand source = operation.GetSource(0);
  528. Debug.Assert(dest.Type == OperandType.I32 && source.Type == OperandType.I64);
  529. context.Assembler.Mov(dest, source, OperandType.I32);
  530. }
  531. private static void GenerateConvertToFP(CodeGenContext context, Operation operation)
  532. {
  533. Operand dest = operation.Destination;
  534. Operand source = operation.GetSource(0);
  535. Debug.Assert(dest.Type == OperandType.FP32 || dest.Type == OperandType.FP64);
  536. if (dest.Type == OperandType.FP32)
  537. {
  538. Debug.Assert(source.Type.IsInteger() || source.Type == OperandType.FP64);
  539. if (source.Type.IsInteger())
  540. {
  541. context.Assembler.Xorps (dest, dest, dest);
  542. context.Assembler.Cvtsi2ss(dest, dest, source, source.Type);
  543. }
  544. else /* if (source.Type == OperandType.FP64) */
  545. {
  546. context.Assembler.Cvtsd2ss(dest, dest, source);
  547. GenerateZeroUpper96(context, dest, dest);
  548. }
  549. }
  550. else /* if (dest.Type == OperandType.FP64) */
  551. {
  552. Debug.Assert(source.Type.IsInteger() || source.Type == OperandType.FP32);
  553. if (source.Type.IsInteger())
  554. {
  555. context.Assembler.Xorps (dest, dest, dest);
  556. context.Assembler.Cvtsi2sd(dest, dest, source, source.Type);
  557. }
  558. else /* if (source.Type == OperandType.FP32) */
  559. {
  560. context.Assembler.Cvtss2sd(dest, dest, source);
  561. GenerateZeroUpper64(context, dest, dest);
  562. }
  563. }
  564. }
  565. private static void GenerateCopy(CodeGenContext context, Operation operation)
  566. {
  567. Operand dest = operation.Destination;
  568. Operand source = operation.GetSource(0);
  569. EnsureSameType(dest, source);
  570. Debug.Assert(dest.Type.IsInteger() || source.Kind != OperandKind.Constant);
  571. // Moves to the same register are useless.
  572. if (dest.Kind == source.Kind && dest.Value == source.Value)
  573. {
  574. return;
  575. }
  576. if (dest.Kind == OperandKind.Register &&
  577. source.Kind == OperandKind.Constant && source.Value == 0)
  578. {
  579. // Assemble "mov reg, 0" as "xor reg, reg" as the later is more efficient.
  580. context.Assembler.Xor(dest, dest, OperandType.I32);
  581. }
  582. else if (dest.Type.IsInteger())
  583. {
  584. context.Assembler.Mov(dest, source, dest.Type);
  585. }
  586. else
  587. {
  588. context.Assembler.Movdqu(dest, source);
  589. }
  590. }
  591. private static void GenerateCountLeadingZeros(CodeGenContext context, Operation operation)
  592. {
  593. Operand dest = operation.Destination;
  594. Operand source = operation.GetSource(0);
  595. EnsureSameType(dest, source);
  596. Debug.Assert(dest.Type.IsInteger());
  597. context.Assembler.Bsr(dest, source, dest.Type);
  598. int operandSize = dest.Type == OperandType.I32 ? 32 : 64;
  599. int operandMask = operandSize - 1;
  600. // When the input operand is 0, the result is undefined, however the
  601. // ZF flag is set. We are supposed to return the operand size on that
  602. // case. So, add an additional jump to handle that case, by moving the
  603. // operand size constant to the destination register.
  604. context.JumpToNear(X86Condition.NotEqual);
  605. context.Assembler.Mov(dest, Const(operandSize | operandMask), OperandType.I32);
  606. context.JumpHere();
  607. // BSR returns the zero based index of the last bit set on the operand,
  608. // starting from the least significant bit. However we are supposed to
  609. // return the number of 0 bits on the high end. So, we invert the result
  610. // of the BSR using XOR to get the correct value.
  611. context.Assembler.Xor(dest, Const(operandMask), OperandType.I32);
  612. }
  613. private static void GenerateDivide(CodeGenContext context, Operation operation)
  614. {
  615. Operand dest = operation.Destination;
  616. Operand dividend = operation.GetSource(0);
  617. Operand divisor = operation.GetSource(1);
  618. if (!dest.Type.IsInteger())
  619. {
  620. ValidateBinOp(dest, dividend, divisor);
  621. }
  622. if (dest.Type.IsInteger())
  623. {
  624. divisor = operation.GetSource(2);
  625. EnsureSameType(dest, divisor);
  626. if (divisor.Type == OperandType.I32)
  627. {
  628. context.Assembler.Cdq();
  629. }
  630. else
  631. {
  632. context.Assembler.Cqo();
  633. }
  634. context.Assembler.Idiv(divisor);
  635. }
  636. else if (dest.Type == OperandType.FP32)
  637. {
  638. context.Assembler.Divss(dest, dividend, divisor);
  639. }
  640. else /* if (dest.Type == OperandType.FP64) */
  641. {
  642. context.Assembler.Divsd(dest, dividend, divisor);
  643. }
  644. }
  645. private static void GenerateDivideUI(CodeGenContext context, Operation operation)
  646. {
  647. Operand divisor = operation.GetSource(2);
  648. Operand rdx = Register(X86Register.Rdx);
  649. Debug.Assert(divisor.Type.IsInteger());
  650. context.Assembler.Xor(rdx, rdx, OperandType.I32);
  651. context.Assembler.Div(divisor);
  652. }
  653. private static void GenerateFill(CodeGenContext context, Operation operation)
  654. {
  655. Operand dest = operation.Destination;
  656. Operand offset = operation.GetSource(0);
  657. Debug.Assert(offset.Kind == OperandKind.Constant);
  658. int offs = offset.AsInt32() + context.CallArgsRegionSize;
  659. Operand rsp = Register(X86Register.Rsp);
  660. MemoryOperand memOp = MemoryOp(dest.Type, rsp, null, Multiplier.x1, offs);
  661. GenerateLoad(context, memOp, dest);
  662. }
  663. private static void GenerateLoad(CodeGenContext context, Operation operation)
  664. {
  665. Operand value = operation.Destination;
  666. Operand address = Memory(operation.GetSource(0), value.Type);
  667. GenerateLoad(context, address, value);
  668. }
  669. private static void GenerateLoad16(CodeGenContext context, Operation operation)
  670. {
  671. Operand value = operation.Destination;
  672. Operand address = Memory(operation.GetSource(0), value.Type);
  673. Debug.Assert(value.Type.IsInteger());
  674. context.Assembler.Movzx16(value, address, value.Type);
  675. }
  676. private static void GenerateLoad8(CodeGenContext context, Operation operation)
  677. {
  678. Operand value = operation.Destination;
  679. Operand address = Memory(operation.GetSource(0), value.Type);
  680. Debug.Assert(value.Type.IsInteger());
  681. context.Assembler.Movzx8(value, address, value.Type);
  682. }
  683. private static void GenerateMultiply(CodeGenContext context, Operation operation)
  684. {
  685. Operand dest = operation.Destination;
  686. Operand src1 = operation.GetSource(0);
  687. Operand src2 = operation.GetSource(1);
  688. if (src2.Kind != OperandKind.Constant)
  689. {
  690. EnsureSameReg(dest, src1);
  691. }
  692. EnsureSameType(dest, src1, src2);
  693. if (dest.Type.IsInteger())
  694. {
  695. if (src2.Kind == OperandKind.Constant)
  696. {
  697. context.Assembler.Imul(dest, src1, src2, dest.Type);
  698. }
  699. else
  700. {
  701. context.Assembler.Imul(dest, src2, dest.Type);
  702. }
  703. }
  704. else if (dest.Type == OperandType.FP32)
  705. {
  706. context.Assembler.Mulss(dest, src1, src2);
  707. }
  708. else /* if (dest.Type == OperandType.FP64) */
  709. {
  710. context.Assembler.Mulsd(dest, src1, src2);
  711. }
  712. }
  713. private static void GenerateMultiply64HighSI(CodeGenContext context, Operation operation)
  714. {
  715. Operand source = operation.GetSource(1);
  716. Debug.Assert(source.Type == OperandType.I64);
  717. context.Assembler.Imul(source);
  718. }
  719. private static void GenerateMultiply64HighUI(CodeGenContext context, Operation operation)
  720. {
  721. Operand source = operation.GetSource(1);
  722. Debug.Assert(source.Type == OperandType.I64);
  723. context.Assembler.Mul(source);
  724. }
  725. private static void GenerateNegate(CodeGenContext context, Operation operation)
  726. {
  727. Operand dest = operation.Destination;
  728. Operand source = operation.GetSource(0);
  729. ValidateUnOp(dest, source);
  730. Debug.Assert(dest.Type.IsInteger());
  731. context.Assembler.Neg(dest);
  732. }
  733. private static void GenerateReturn(CodeGenContext context, Operation operation)
  734. {
  735. WriteEpilogue(context);
  736. context.Assembler.Return();
  737. }
  738. private static void GenerateRotateRight(CodeGenContext context, Operation operation)
  739. {
  740. Operand dest = operation.Destination;
  741. Operand src1 = operation.GetSource(0);
  742. Operand src2 = operation.GetSource(1);
  743. ValidateShift(dest, src1, src2);
  744. context.Assembler.Ror(dest, src2, dest.Type);
  745. }
  746. private static void GenerateShiftLeft(CodeGenContext context, Operation operation)
  747. {
  748. Operand dest = operation.Destination;
  749. Operand src1 = operation.GetSource(0);
  750. Operand src2 = operation.GetSource(1);
  751. ValidateShift(dest, src1, src2);
  752. context.Assembler.Shl(dest, src2, dest.Type);
  753. }
  754. private static void GenerateShiftRightSI(CodeGenContext context, Operation operation)
  755. {
  756. Operand dest = operation.Destination;
  757. Operand src1 = operation.GetSource(0);
  758. Operand src2 = operation.GetSource(1);
  759. ValidateShift(dest, src1, src2);
  760. context.Assembler.Sar(dest, src2, dest.Type);
  761. }
  762. private static void GenerateShiftRightUI(CodeGenContext context, Operation operation)
  763. {
  764. Operand dest = operation.Destination;
  765. Operand src1 = operation.GetSource(0);
  766. Operand src2 = operation.GetSource(1);
  767. ValidateShift(dest, src1, src2);
  768. context.Assembler.Shr(dest, src2, dest.Type);
  769. }
  770. private static void GenerateSignExtend16(CodeGenContext context, Operation operation)
  771. {
  772. Operand dest = operation.Destination;
  773. Operand source = operation.GetSource(0);
  774. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  775. context.Assembler.Movsx16(dest, source, dest.Type);
  776. }
  777. private static void GenerateSignExtend32(CodeGenContext context, Operation operation)
  778. {
  779. Operand dest = operation.Destination;
  780. Operand source = operation.GetSource(0);
  781. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  782. context.Assembler.Movsx32(dest, source, dest.Type);
  783. }
  784. private static void GenerateSignExtend8(CodeGenContext context, Operation operation)
  785. {
  786. Operand dest = operation.Destination;
  787. Operand source = operation.GetSource(0);
  788. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  789. context.Assembler.Movsx8(dest, source, dest.Type);
  790. }
  791. private static void GenerateSpill(CodeGenContext context, Operation operation)
  792. {
  793. GenerateSpill(context, operation, context.CallArgsRegionSize);
  794. }
  795. private static void GenerateSpillArg(CodeGenContext context, Operation operation)
  796. {
  797. GenerateSpill(context, operation, 0);
  798. }
  799. private static void GenerateSpill(CodeGenContext context, Operation operation, int baseOffset)
  800. {
  801. Operand offset = operation.GetSource(0);
  802. Operand source = operation.GetSource(1);
  803. Debug.Assert(offset.Kind == OperandKind.Constant);
  804. int offs = offset.AsInt32() + baseOffset;
  805. Operand rsp = Register(X86Register.Rsp);
  806. MemoryOperand memOp = MemoryOp(source.Type, rsp, null, Multiplier.x1, offs);
  807. GenerateStore(context, memOp, source);
  808. }
  809. private static void GenerateStackAlloc(CodeGenContext context, Operation operation)
  810. {
  811. Operand dest = operation.Destination;
  812. Operand offset = operation.GetSource(0);
  813. Debug.Assert(offset.Kind == OperandKind.Constant);
  814. int offs = offset.AsInt32() + context.CallArgsRegionSize;
  815. Operand rsp = Register(X86Register.Rsp);
  816. MemoryOperand memOp = MemoryOp(OperandType.I64, rsp, null, Multiplier.x1, offs);
  817. context.Assembler.Lea(dest, memOp, OperandType.I64);
  818. }
  819. private static void GenerateStore(CodeGenContext context, Operation operation)
  820. {
  821. Operand value = operation.GetSource(1);
  822. Operand address = Memory(operation.GetSource(0), value.Type);
  823. GenerateStore(context, address, value);
  824. }
  825. private static void GenerateStore16(CodeGenContext context, Operation operation)
  826. {
  827. Operand value = operation.GetSource(1);
  828. Operand address = Memory(operation.GetSource(0), value.Type);
  829. Debug.Assert(value.Type.IsInteger());
  830. context.Assembler.Mov16(address, value);
  831. }
  832. private static void GenerateStore8(CodeGenContext context, Operation operation)
  833. {
  834. Operand value = operation.GetSource(1);
  835. Operand address = Memory(operation.GetSource(0), value.Type);
  836. Debug.Assert(value.Type.IsInteger());
  837. context.Assembler.Mov8(address, value);
  838. }
  839. private static void GenerateSubtract(CodeGenContext context, Operation operation)
  840. {
  841. Operand dest = operation.Destination;
  842. Operand src1 = operation.GetSource(0);
  843. Operand src2 = operation.GetSource(1);
  844. ValidateBinOp(dest, src1, src2);
  845. if (dest.Type.IsInteger())
  846. {
  847. context.Assembler.Sub(dest, src2, dest.Type);
  848. }
  849. else if (dest.Type == OperandType.FP32)
  850. {
  851. context.Assembler.Subss(dest, src1, src2);
  852. }
  853. else /* if (dest.Type == OperandType.FP64) */
  854. {
  855. context.Assembler.Subsd(dest, src1, src2);
  856. }
  857. }
  858. private static void GenerateTailcall(CodeGenContext context, Operation operation)
  859. {
  860. WriteEpilogue(context);
  861. context.Assembler.Jmp(operation.GetSource(0));
  862. }
  863. private static void GenerateVectorCreateScalar(CodeGenContext context, Operation operation)
  864. {
  865. Operand dest = operation.Destination;
  866. Operand source = operation.GetSource(0);
  867. Debug.Assert(!dest.Type.IsInteger() && source.Type.IsInteger());
  868. if (source.Type == OperandType.I32)
  869. {
  870. context.Assembler.Movd(dest, source); // (__m128i _mm_cvtsi32_si128(int a))
  871. }
  872. else /* if (source.Type == OperandType.I64) */
  873. {
  874. context.Assembler.Movq(dest, source); // (__m128i _mm_cvtsi64_si128(__int64 a))
  875. }
  876. }
  877. private static void GenerateVectorExtract(CodeGenContext context, Operation operation)
  878. {
  879. Operand dest = operation.Destination; //Value
  880. Operand src1 = operation.GetSource(0); //Vector
  881. Operand src2 = operation.GetSource(1); //Index
  882. Debug.Assert(src1.Type == OperandType.V128);
  883. Debug.Assert(src2.Kind == OperandKind.Constant);
  884. byte index = src2.AsByte();
  885. Debug.Assert(index < OperandType.V128.GetSizeInBytes() / dest.Type.GetSizeInBytes());
  886. if (dest.Type == OperandType.I32)
  887. {
  888. if (index == 0)
  889. {
  890. context.Assembler.Movd(dest, src1);
  891. }
  892. else if (HardwareCapabilities.SupportsSse41)
  893. {
  894. context.Assembler.Pextrd(dest, src1, index);
  895. }
  896. else
  897. {
  898. int mask0 = 0b11_10_01_00;
  899. int mask1 = 0b11_10_01_00;
  900. mask0 = BitUtils.RotateRight(mask0, index * 2, 8);
  901. mask1 = BitUtils.RotateRight(mask1, 8 - index * 2, 8);
  902. context.Assembler.Pshufd(src1, src1, (byte)mask0);
  903. context.Assembler.Movd (dest, src1);
  904. context.Assembler.Pshufd(src1, src1, (byte)mask1);
  905. }
  906. }
  907. else if (dest.Type == OperandType.I64)
  908. {
  909. if (index == 0)
  910. {
  911. context.Assembler.Movq(dest, src1);
  912. }
  913. else if (HardwareCapabilities.SupportsSse41)
  914. {
  915. context.Assembler.Pextrq(dest, src1, index);
  916. }
  917. else
  918. {
  919. const byte mask = 0b01_00_11_10;
  920. context.Assembler.Pshufd(src1, src1, mask);
  921. context.Assembler.Movq (dest, src1);
  922. context.Assembler.Pshufd(src1, src1, mask);
  923. }
  924. }
  925. else
  926. {
  927. // Floating-point types.
  928. if ((index >= 2 && dest.Type == OperandType.FP32) ||
  929. (index == 1 && dest.Type == OperandType.FP64))
  930. {
  931. context.Assembler.Movhlps(dest, dest, src1);
  932. context.Assembler.Movq (dest, dest);
  933. }
  934. else
  935. {
  936. context.Assembler.Movq(dest, src1);
  937. }
  938. if (dest.Type == OperandType.FP32)
  939. {
  940. context.Assembler.Pshufd(dest, dest, (byte)(0xfc | (index & 1)));
  941. }
  942. }
  943. }
  944. private static void GenerateVectorExtract16(CodeGenContext context, Operation operation)
  945. {
  946. Operand dest = operation.Destination; //Value
  947. Operand src1 = operation.GetSource(0); //Vector
  948. Operand src2 = operation.GetSource(1); //Index
  949. Debug.Assert(src1.Type == OperandType.V128);
  950. Debug.Assert(src2.Kind == OperandKind.Constant);
  951. byte index = src2.AsByte();
  952. Debug.Assert(index < 8);
  953. context.Assembler.Pextrw(dest, src1, index);
  954. }
  955. private static void GenerateVectorExtract8(CodeGenContext context, Operation operation)
  956. {
  957. Operand dest = operation.Destination; //Value
  958. Operand src1 = operation.GetSource(0); //Vector
  959. Operand src2 = operation.GetSource(1); //Index
  960. Debug.Assert(src1.Type == OperandType.V128);
  961. Debug.Assert(src2.Kind == OperandKind.Constant);
  962. byte index = src2.AsByte();
  963. Debug.Assert(index < 16);
  964. if (HardwareCapabilities.SupportsSse41)
  965. {
  966. context.Assembler.Pextrb(dest, src1, index);
  967. }
  968. else
  969. {
  970. context.Assembler.Pextrw(dest, src1, (byte)(index >> 1));
  971. if ((index & 1) != 0)
  972. {
  973. context.Assembler.Shr(dest, Const(8), OperandType.I32);
  974. }
  975. else
  976. {
  977. context.Assembler.Movzx8(dest, dest, OperandType.I32);
  978. }
  979. }
  980. }
  981. private static void GenerateVectorInsert(CodeGenContext context, Operation operation)
  982. {
  983. Operand dest = operation.Destination;
  984. Operand src1 = operation.GetSource(0); //Vector
  985. Operand src2 = operation.GetSource(1); //Value
  986. Operand src3 = operation.GetSource(2); //Index
  987. if (!HardwareCapabilities.SupportsVexEncoding)
  988. {
  989. EnsureSameReg(dest, src1);
  990. }
  991. Debug.Assert(src1.Type == OperandType.V128);
  992. Debug.Assert(src3.Kind == OperandKind.Constant);
  993. byte index = src3.AsByte();
  994. void InsertIntSse2(int words)
  995. {
  996. if (dest.GetRegister() != src1.GetRegister())
  997. {
  998. context.Assembler.Movdqu(dest, src1);
  999. }
  1000. for (int word = 0; word < words; word++)
  1001. {
  1002. // Insert lower 16-bits.
  1003. context.Assembler.Pinsrw(dest, dest, src2, (byte)(index * words + word));
  1004. // Move next word down.
  1005. context.Assembler.Ror(src2, Const(16), src2.Type);
  1006. }
  1007. }
  1008. if (src2.Type == OperandType.I32)
  1009. {
  1010. Debug.Assert(index < 4);
  1011. if (HardwareCapabilities.SupportsSse41)
  1012. {
  1013. context.Assembler.Pinsrd(dest, src1, src2, index);
  1014. }
  1015. else
  1016. {
  1017. InsertIntSse2(2);
  1018. }
  1019. }
  1020. else if (src2.Type == OperandType.I64)
  1021. {
  1022. Debug.Assert(index < 2);
  1023. if (HardwareCapabilities.SupportsSse41)
  1024. {
  1025. context.Assembler.Pinsrq(dest, src1, src2, index);
  1026. }
  1027. else
  1028. {
  1029. InsertIntSse2(4);
  1030. }
  1031. }
  1032. else if (src2.Type == OperandType.FP32)
  1033. {
  1034. Debug.Assert(index < 4);
  1035. if (index != 0)
  1036. {
  1037. if (HardwareCapabilities.SupportsSse41)
  1038. {
  1039. context.Assembler.Insertps(dest, src1, src2, (byte)(index << 4));
  1040. }
  1041. else
  1042. {
  1043. if (src1.GetRegister() == src2.GetRegister())
  1044. {
  1045. int mask = 0b11_10_01_00;
  1046. mask &= ~(0b11 << index * 2);
  1047. context.Assembler.Pshufd(dest, src1, (byte)mask);
  1048. }
  1049. else
  1050. {
  1051. int mask0 = 0b11_10_01_00;
  1052. int mask1 = 0b11_10_01_00;
  1053. mask0 = BitUtils.RotateRight(mask0, index * 2, 8);
  1054. mask1 = BitUtils.RotateRight(mask1, 8 - index * 2, 8);
  1055. context.Assembler.Pshufd(src1, src1, (byte)mask0); // Lane to be inserted in position 0.
  1056. context.Assembler.Movss (dest, src1, src2); // dest[127:0] = src1[127:32] | src2[31:0]
  1057. context.Assembler.Pshufd(dest, dest, (byte)mask1); // Inserted lane in original position.
  1058. if (dest.GetRegister() != src1.GetRegister())
  1059. {
  1060. context.Assembler.Pshufd(src1, src1, (byte)mask1); // Restore src1.
  1061. }
  1062. }
  1063. }
  1064. }
  1065. else
  1066. {
  1067. context.Assembler.Movss(dest, src1, src2);
  1068. }
  1069. }
  1070. else /* if (src2.Type == OperandType.FP64) */
  1071. {
  1072. Debug.Assert(index < 2);
  1073. if (index != 0)
  1074. {
  1075. context.Assembler.Movlhps(dest, src1, src2);
  1076. }
  1077. else
  1078. {
  1079. context.Assembler.Movsd(dest, src1, src2);
  1080. }
  1081. }
  1082. }
  1083. private static void GenerateVectorInsert16(CodeGenContext context, Operation operation)
  1084. {
  1085. Operand dest = operation.Destination;
  1086. Operand src1 = operation.GetSource(0); //Vector
  1087. Operand src2 = operation.GetSource(1); //Value
  1088. Operand src3 = operation.GetSource(2); //Index
  1089. if (!HardwareCapabilities.SupportsVexEncoding)
  1090. {
  1091. EnsureSameReg(dest, src1);
  1092. }
  1093. Debug.Assert(src1.Type == OperandType.V128);
  1094. Debug.Assert(src3.Kind == OperandKind.Constant);
  1095. byte index = src3.AsByte();
  1096. context.Assembler.Pinsrw(dest, src1, src2, index);
  1097. }
  1098. private static void GenerateVectorInsert8(CodeGenContext context, Operation operation)
  1099. {
  1100. Operand dest = operation.Destination;
  1101. Operand src1 = operation.GetSource(0); //Vector
  1102. Operand src2 = operation.GetSource(1); //Value
  1103. Operand src3 = operation.GetSource(2); //Index
  1104. // It's not possible to emulate this instruction without
  1105. // SSE 4.1 support without the use of a temporary register,
  1106. // so we instead handle that case on the pre-allocator when
  1107. // SSE 4.1 is not supported on the CPU.
  1108. Debug.Assert(HardwareCapabilities.SupportsSse41);
  1109. if (!HardwareCapabilities.SupportsVexEncoding)
  1110. {
  1111. EnsureSameReg(dest, src1);
  1112. }
  1113. Debug.Assert(src1.Type == OperandType.V128);
  1114. Debug.Assert(src3.Kind == OperandKind.Constant);
  1115. byte index = src3.AsByte();
  1116. context.Assembler.Pinsrb(dest, src1, src2, index);
  1117. }
  1118. private static void GenerateVectorOne(CodeGenContext context, Operation operation)
  1119. {
  1120. Operand dest = operation.Destination;
  1121. Debug.Assert(!dest.Type.IsInteger());
  1122. context.Assembler.Pcmpeqw(dest, dest, dest);
  1123. }
  1124. private static void GenerateVectorZero(CodeGenContext context, Operation operation)
  1125. {
  1126. Operand dest = operation.Destination;
  1127. Debug.Assert(!dest.Type.IsInteger());
  1128. context.Assembler.Xorps(dest, dest, dest);
  1129. }
  1130. private static void GenerateVectorZeroUpper64(CodeGenContext context, Operation operation)
  1131. {
  1132. Operand dest = operation.Destination;
  1133. Operand source = operation.GetSource(0);
  1134. Debug.Assert(dest.Type == OperandType.V128 && source.Type == OperandType.V128);
  1135. GenerateZeroUpper64(context, dest, source);
  1136. }
  1137. private static void GenerateVectorZeroUpper96(CodeGenContext context, Operation operation)
  1138. {
  1139. Operand dest = operation.Destination;
  1140. Operand source = operation.GetSource(0);
  1141. Debug.Assert(dest.Type == OperandType.V128 && source.Type == OperandType.V128);
  1142. GenerateZeroUpper96(context, dest, source);
  1143. }
  1144. private static void GenerateZeroExtend16(CodeGenContext context, Operation operation)
  1145. {
  1146. Operand dest = operation.Destination;
  1147. Operand source = operation.GetSource(0);
  1148. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  1149. context.Assembler.Movzx16(dest, source, OperandType.I32);
  1150. }
  1151. private static void GenerateZeroExtend32(CodeGenContext context, Operation operation)
  1152. {
  1153. Operand dest = operation.Destination;
  1154. Operand source = operation.GetSource(0);
  1155. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  1156. context.Assembler.Mov(dest, source, OperandType.I32);
  1157. }
  1158. private static void GenerateZeroExtend8(CodeGenContext context, Operation operation)
  1159. {
  1160. Operand dest = operation.Destination;
  1161. Operand source = operation.GetSource(0);
  1162. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  1163. context.Assembler.Movzx8(dest, source, OperandType.I32);
  1164. }
  1165. private static void GenerateLoad(CodeGenContext context, Operand address, Operand value)
  1166. {
  1167. switch (value.Type)
  1168. {
  1169. case OperandType.I32: context.Assembler.Mov (value, address, OperandType.I32); break;
  1170. case OperandType.I64: context.Assembler.Mov (value, address, OperandType.I64); break;
  1171. case OperandType.FP32: context.Assembler.Movd (value, address); break;
  1172. case OperandType.FP64: context.Assembler.Movq (value, address); break;
  1173. case OperandType.V128: context.Assembler.Movdqu(value, address); break;
  1174. default: Debug.Assert(false); break;
  1175. }
  1176. }
  1177. private static void GenerateStore(CodeGenContext context, Operand address, Operand value)
  1178. {
  1179. switch (value.Type)
  1180. {
  1181. case OperandType.I32: context.Assembler.Mov (address, value, OperandType.I32); break;
  1182. case OperandType.I64: context.Assembler.Mov (address, value, OperandType.I64); break;
  1183. case OperandType.FP32: context.Assembler.Movd (address, value); break;
  1184. case OperandType.FP64: context.Assembler.Movq (address, value); break;
  1185. case OperandType.V128: context.Assembler.Movdqu(address, value); break;
  1186. default: Debug.Assert(false); break;
  1187. }
  1188. }
  1189. private static void GenerateZeroUpper64(CodeGenContext context, Operand dest, Operand source)
  1190. {
  1191. context.Assembler.Movq(dest, source);
  1192. }
  1193. private static void GenerateZeroUpper96(CodeGenContext context, Operand dest, Operand source)
  1194. {
  1195. context.Assembler.Movq(dest, source);
  1196. context.Assembler.Pshufd(dest, dest, 0xfc);
  1197. }
  1198. [Conditional("DEBUG")]
  1199. private static void ValidateUnOp(Operand dest, Operand source)
  1200. {
  1201. EnsureSameReg (dest, source);
  1202. EnsureSameType(dest, source);
  1203. }
  1204. [Conditional("DEBUG")]
  1205. private static void ValidateBinOp(Operand dest, Operand src1, Operand src2)
  1206. {
  1207. EnsureSameReg (dest, src1);
  1208. EnsureSameType(dest, src1, src2);
  1209. }
  1210. [Conditional("DEBUG")]
  1211. private static void ValidateShift(Operand dest, Operand src1, Operand src2)
  1212. {
  1213. EnsureSameReg (dest, src1);
  1214. EnsureSameType(dest, src1);
  1215. Debug.Assert(dest.Type.IsInteger() && src2.Type == OperandType.I32);
  1216. }
  1217. private static void EnsureSameReg(Operand op1, Operand op2)
  1218. {
  1219. if (!op1.Type.IsInteger() && HardwareCapabilities.SupportsVexEncoding)
  1220. {
  1221. return;
  1222. }
  1223. Debug.Assert(op1.Kind == OperandKind.Register || op1.Kind == OperandKind.Memory);
  1224. Debug.Assert(op1.Kind == op2.Kind);
  1225. Debug.Assert(op1.Value == op2.Value);
  1226. }
  1227. private static void EnsureSameType(Operand op1, Operand op2)
  1228. {
  1229. Debug.Assert(op1.Type == op2.Type);
  1230. }
  1231. private static void EnsureSameType(Operand op1, Operand op2, Operand op3)
  1232. {
  1233. Debug.Assert(op1.Type == op2.Type);
  1234. Debug.Assert(op1.Type == op3.Type);
  1235. }
  1236. private static void EnsureSameType(Operand op1, Operand op2, Operand op3, Operand op4)
  1237. {
  1238. Debug.Assert(op1.Type == op2.Type);
  1239. Debug.Assert(op1.Type == op3.Type);
  1240. Debug.Assert(op1.Type == op4.Type);
  1241. }
  1242. private static UnwindInfo WritePrologue(CodeGenContext context)
  1243. {
  1244. List<UnwindPushEntry> pushEntries = new List<UnwindPushEntry>();
  1245. Operand rsp = Register(X86Register.Rsp);
  1246. int mask = CallingConvention.GetIntCalleeSavedRegisters() & context.AllocResult.IntUsedRegisters;
  1247. while (mask != 0)
  1248. {
  1249. int bit = BitOperations.TrailingZeroCount(mask);
  1250. context.Assembler.Push(Register((X86Register)bit));
  1251. pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.PushReg, context.StreamOffset, regIndex: bit));
  1252. mask &= ~(1 << bit);
  1253. }
  1254. int reservedStackSize = context.CallArgsRegionSize + context.AllocResult.SpillRegionSize;
  1255. reservedStackSize += context.XmmSaveRegionSize;
  1256. if (reservedStackSize >= StackGuardSize)
  1257. {
  1258. GenerateInlineStackProbe(context, reservedStackSize);
  1259. }
  1260. if (reservedStackSize != 0)
  1261. {
  1262. context.Assembler.Sub(rsp, Const(reservedStackSize), OperandType.I64);
  1263. pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.AllocStack, context.StreamOffset, stackOffsetOrAllocSize: reservedStackSize));
  1264. }
  1265. int offset = reservedStackSize;
  1266. mask = CallingConvention.GetVecCalleeSavedRegisters() & context.AllocResult.VecUsedRegisters;
  1267. while (mask != 0)
  1268. {
  1269. int bit = BitOperations.TrailingZeroCount(mask);
  1270. offset -= 16;
  1271. MemoryOperand memOp = MemoryOp(OperandType.V128, rsp, null, Multiplier.x1, offset);
  1272. context.Assembler.Movdqu(memOp, Xmm((X86Register)bit));
  1273. pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.SaveXmm128, context.StreamOffset, bit, offset));
  1274. mask &= ~(1 << bit);
  1275. }
  1276. return new UnwindInfo(pushEntries.ToArray(), context.StreamOffset);
  1277. }
  1278. private static void WriteEpilogue(CodeGenContext context)
  1279. {
  1280. Operand rsp = Register(X86Register.Rsp);
  1281. int reservedStackSize = context.CallArgsRegionSize + context.AllocResult.SpillRegionSize;
  1282. reservedStackSize += context.XmmSaveRegionSize;
  1283. int offset = reservedStackSize;
  1284. int mask = CallingConvention.GetVecCalleeSavedRegisters() & context.AllocResult.VecUsedRegisters;
  1285. while (mask != 0)
  1286. {
  1287. int bit = BitOperations.TrailingZeroCount(mask);
  1288. offset -= 16;
  1289. MemoryOperand memOp = MemoryOp(OperandType.V128, rsp, null, Multiplier.x1, offset);
  1290. context.Assembler.Movdqu(Xmm((X86Register)bit), memOp);
  1291. mask &= ~(1 << bit);
  1292. }
  1293. if (reservedStackSize != 0)
  1294. {
  1295. context.Assembler.Add(rsp, Const(reservedStackSize), OperandType.I64);
  1296. }
  1297. mask = CallingConvention.GetIntCalleeSavedRegisters() & context.AllocResult.IntUsedRegisters;
  1298. while (mask != 0)
  1299. {
  1300. int bit = BitUtils.HighestBitSet(mask);
  1301. context.Assembler.Pop(Register((X86Register)bit));
  1302. mask &= ~(1 << bit);
  1303. }
  1304. }
  1305. private static void GenerateInlineStackProbe(CodeGenContext context, int size)
  1306. {
  1307. // Windows does lazy stack allocation, and there are just 2
  1308. // guard pages on the end of the stack. So, if the allocation
  1309. // size we make is greater than this guard size, we must ensure
  1310. // that the OS will map all pages that we'll use. We do that by
  1311. // doing a dummy read on those pages, forcing a page fault and
  1312. // the OS to map them. If they are already mapped, nothing happens.
  1313. const int pageMask = PageSize - 1;
  1314. size = (size + pageMask) & ~pageMask;
  1315. Operand rsp = Register(X86Register.Rsp);
  1316. Operand temp = Register(CallingConvention.GetIntReturnRegister());
  1317. for (int offset = PageSize; offset < size; offset += PageSize)
  1318. {
  1319. Operand memOp = MemoryOp(OperandType.I32, rsp, null, Multiplier.x1, -offset);
  1320. context.Assembler.Mov(temp, memOp, OperandType.I32);
  1321. }
  1322. }
  1323. private static MemoryOperand Memory(Operand operand, OperandType type)
  1324. {
  1325. if (operand.Kind == OperandKind.Memory)
  1326. {
  1327. return operand as MemoryOperand;
  1328. }
  1329. return MemoryOp(type, operand);
  1330. }
  1331. private static Operand Register(X86Register register, OperandType type = OperandType.I64)
  1332. {
  1333. return OperandHelper.Register((int)register, RegisterType.Integer, type);
  1334. }
  1335. private static Operand Xmm(X86Register register)
  1336. {
  1337. return OperandHelper.Register((int)register, RegisterType.Vector, OperandType.V128);
  1338. }
  1339. }
  1340. }