CodeGenerator.cs 64 KB

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