CodeGenerator.cs 66 KB

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