CodeGenerator.cs 62 KB

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