CodeGenerator.cs 64 KB

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