CodeGenerator.cs 63 KB

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