CodeGenerator.cs 60 KB

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