CodeGenerator.cs 64 KB

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