CodeGenerator.cs 61 KB

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