CodeGenerator.cs 62 KB

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