CodeGenerator.cs 65 KB

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