CodeGenerator.cs 63 KB

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