CodeGenerator.cs 63 KB

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