CodeGenerator.cs 63 KB

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