CodeGenerator.cs 63 KB

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