CodeGenerator.cs 63 KB

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