| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856 |
- using ARMeilleure.CodeGen.Optimizations;
- using ARMeilleure.CodeGen.RegisterAllocators;
- using ARMeilleure.CodeGen.Unwinding;
- using ARMeilleure.Common;
- using ARMeilleure.Diagnostics;
- using ARMeilleure.IntermediateRepresentation;
- using ARMeilleure.Translation;
- using ARMeilleure.Translation.PTC;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Numerics;
- using static ARMeilleure.IntermediateRepresentation.OperandHelper;
- namespace ARMeilleure.CodeGen.X86
- {
- static class CodeGenerator
- {
- private const int PageSize = 0x1000;
- private const int StackGuardSize = 0x2000;
- private static Action<CodeGenContext, Operation>[] _instTable;
- static CodeGenerator()
- {
- _instTable = new Action<CodeGenContext, Operation>[EnumUtils.GetCount(typeof(Instruction))];
- Add(Instruction.Add, GenerateAdd);
- Add(Instruction.BitwiseAnd, GenerateBitwiseAnd);
- Add(Instruction.BitwiseExclusiveOr, GenerateBitwiseExclusiveOr);
- Add(Instruction.BitwiseNot, GenerateBitwiseNot);
- Add(Instruction.BitwiseOr, GenerateBitwiseOr);
- Add(Instruction.BranchIf, GenerateBranchIf);
- Add(Instruction.ByteSwap, GenerateByteSwap);
- Add(Instruction.Call, GenerateCall);
- Add(Instruction.Clobber, GenerateClobber);
- Add(Instruction.Compare, GenerateCompare);
- Add(Instruction.CompareAndSwap, GenerateCompareAndSwap);
- Add(Instruction.CompareAndSwap16, GenerateCompareAndSwap16);
- Add(Instruction.CompareAndSwap8, GenerateCompareAndSwap8);
- Add(Instruction.ConditionalSelect, GenerateConditionalSelect);
- Add(Instruction.ConvertI64ToI32, GenerateConvertI64ToI32);
- Add(Instruction.ConvertToFP, GenerateConvertToFP);
- Add(Instruction.Copy, GenerateCopy);
- Add(Instruction.CountLeadingZeros, GenerateCountLeadingZeros);
- Add(Instruction.Divide, GenerateDivide);
- Add(Instruction.DivideUI, GenerateDivideUI);
- Add(Instruction.Fill, GenerateFill);
- Add(Instruction.Load, GenerateLoad);
- Add(Instruction.Load16, GenerateLoad16);
- Add(Instruction.Load8, GenerateLoad8);
- Add(Instruction.Multiply, GenerateMultiply);
- Add(Instruction.Multiply64HighSI, GenerateMultiply64HighSI);
- Add(Instruction.Multiply64HighUI, GenerateMultiply64HighUI);
- Add(Instruction.Negate, GenerateNegate);
- Add(Instruction.Return, GenerateReturn);
- Add(Instruction.RotateRight, GenerateRotateRight);
- Add(Instruction.ShiftLeft, GenerateShiftLeft);
- Add(Instruction.ShiftRightSI, GenerateShiftRightSI);
- Add(Instruction.ShiftRightUI, GenerateShiftRightUI);
- Add(Instruction.SignExtend16, GenerateSignExtend16);
- Add(Instruction.SignExtend32, GenerateSignExtend32);
- Add(Instruction.SignExtend8, GenerateSignExtend8);
- Add(Instruction.Spill, GenerateSpill);
- Add(Instruction.SpillArg, GenerateSpillArg);
- Add(Instruction.StackAlloc, GenerateStackAlloc);
- Add(Instruction.Store, GenerateStore);
- Add(Instruction.Store16, GenerateStore16);
- Add(Instruction.Store8, GenerateStore8);
- Add(Instruction.Subtract, GenerateSubtract);
- Add(Instruction.Tailcall, GenerateTailcall);
- Add(Instruction.VectorCreateScalar, GenerateVectorCreateScalar);
- Add(Instruction.VectorExtract, GenerateVectorExtract);
- Add(Instruction.VectorExtract16, GenerateVectorExtract16);
- Add(Instruction.VectorExtract8, GenerateVectorExtract8);
- Add(Instruction.VectorInsert, GenerateVectorInsert);
- Add(Instruction.VectorInsert16, GenerateVectorInsert16);
- Add(Instruction.VectorInsert8, GenerateVectorInsert8);
- Add(Instruction.VectorOne, GenerateVectorOne);
- Add(Instruction.VectorZero, GenerateVectorZero);
- Add(Instruction.VectorZeroUpper64, GenerateVectorZeroUpper64);
- Add(Instruction.VectorZeroUpper96, GenerateVectorZeroUpper96);
- Add(Instruction.ZeroExtend16, GenerateZeroExtend16);
- Add(Instruction.ZeroExtend32, GenerateZeroExtend32);
- Add(Instruction.ZeroExtend8, GenerateZeroExtend8);
- }
- private static void Add(Instruction inst, Action<CodeGenContext, Operation> func)
- {
- _instTable[(int)inst] = func;
- }
- public static CompiledFunction Generate(CompilerContext cctx, PtcInfo ptcInfo = null)
- {
- ControlFlowGraph cfg = cctx.Cfg;
- Logger.StartPass(PassName.Optimization);
- if ((cctx.Options & CompilerOptions.SsaForm) != 0 &&
- (cctx.Options & CompilerOptions.Optimize) != 0)
- {
- Optimizer.RunPass(cfg);
- }
- X86Optimizer.RunPass(cfg);
- BlockPlacement.RunPass(cfg);
- Logger.EndPass(PassName.Optimization, cfg);
- Logger.StartPass(PassName.PreAllocation);
- StackAllocator stackAlloc = new StackAllocator();
- PreAllocator.RunPass(cctx, stackAlloc, out int maxCallArgs);
- Logger.EndPass(PassName.PreAllocation, cfg);
- Logger.StartPass(PassName.RegisterAllocation);
- if ((cctx.Options & CompilerOptions.SsaForm) != 0)
- {
- Ssa.Deconstruct(cfg);
- }
- IRegisterAllocator regAlloc;
- if ((cctx.Options & CompilerOptions.Lsra) != 0)
- {
- regAlloc = new LinearScanAllocator();
- }
- else
- {
- regAlloc = new HybridAllocator();
- }
- RegisterMasks regMasks = new RegisterMasks(
- CallingConvention.GetIntAvailableRegisters(),
- CallingConvention.GetVecAvailableRegisters(),
- CallingConvention.GetIntCallerSavedRegisters(),
- CallingConvention.GetVecCallerSavedRegisters(),
- CallingConvention.GetIntCalleeSavedRegisters(),
- CallingConvention.GetVecCalleeSavedRegisters());
- AllocationResult allocResult = regAlloc.RunPass(cfg, stackAlloc, regMasks);
- Logger.EndPass(PassName.RegisterAllocation, cfg);
- Logger.StartPass(PassName.CodeGeneration);
- using (MemoryStream stream = new MemoryStream())
- {
- CodeGenContext context = new CodeGenContext(stream, allocResult, maxCallArgs, cfg.Blocks.Count, ptcInfo);
- UnwindInfo unwindInfo = WritePrologue(context);
- ptcInfo?.WriteUnwindInfo(unwindInfo);
- for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
- {
- context.EnterBlock(block);
- for (Node node = block.Operations.First; node != null; node = node.ListNext)
- {
- if (node is Operation operation)
- {
- GenerateOperation(context, operation);
- }
- }
- if (block.SuccessorCount == 0)
- {
- // The only blocks which can have 0 successors are exit blocks.
- Debug.Assert(block.Operations.Last is Operation operation &&
- (operation.Instruction == Instruction.Tailcall ||
- operation.Instruction == Instruction.Return));
- }
- else
- {
- BasicBlock succ = block.GetSuccessor(0);
- if (succ != block.ListNext)
- {
- context.JumpTo(succ);
- }
- }
- }
- byte[] code = context.GetCode();
- Logger.EndPass(PassName.CodeGeneration);
- return new CompiledFunction(code, unwindInfo);
- }
- }
- private static void GenerateOperation(CodeGenContext context, Operation operation)
- {
- if (operation.Instruction == Instruction.Extended)
- {
- IntrinsicOperation intrinOp = (IntrinsicOperation)operation;
- IntrinsicInfo info = IntrinsicTable.GetInfo(intrinOp.Intrinsic);
- switch (info.Type)
- {
- case IntrinsicType.Comis_:
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- switch (intrinOp.Intrinsic)
- {
- case Intrinsic.X86Comisdeq:
- context.Assembler.Comisd(src1, src2);
- context.Assembler.Setcc(dest, X86Condition.Equal);
- break;
- case Intrinsic.X86Comisdge:
- context.Assembler.Comisd(src1, src2);
- context.Assembler.Setcc(dest, X86Condition.AboveOrEqual);
- break;
- case Intrinsic.X86Comisdlt:
- context.Assembler.Comisd(src1, src2);
- context.Assembler.Setcc(dest, X86Condition.Below);
- break;
- case Intrinsic.X86Comisseq:
- context.Assembler.Comiss(src1, src2);
- context.Assembler.Setcc(dest, X86Condition.Equal);
- break;
- case Intrinsic.X86Comissge:
- context.Assembler.Comiss(src1, src2);
- context.Assembler.Setcc(dest, X86Condition.AboveOrEqual);
- break;
- case Intrinsic.X86Comisslt:
- context.Assembler.Comiss(src1, src2);
- context.Assembler.Setcc(dest, X86Condition.Below);
- break;
- }
- context.Assembler.Movzx8(dest, dest, OperandType.I32);
- break;
- }
- case IntrinsicType.Mxcsr:
- {
- Operand offset = operation.GetSource(0);
- Operand bits = operation.GetSource(1);
- Debug.Assert(offset.Kind == OperandKind.Constant && bits.Kind == OperandKind.Constant);
- Debug.Assert(offset.Type == OperandType.I32 && bits.Type == OperandType.I32);
- int offs = offset.AsInt32() + context.CallArgsRegionSize;
- Operand rsp = Register(X86Register.Rsp);
- MemoryOperand memOp = MemoryOp(OperandType.I32, rsp, null, Multiplier.x1, offs);
- Debug.Assert(HardwareCapabilities.SupportsSse || HardwareCapabilities.SupportsVexEncoding);
- context.Assembler.Stmxcsr(memOp);
- if (intrinOp.Intrinsic == Intrinsic.X86Mxcsrmb)
- {
- context.Assembler.Or(memOp, bits, OperandType.I32);
- }
- else /* if (intrinOp.Intrinsic == Intrinsic.X86Mxcsrub) */
- {
- Operand notBits = Const(~bits.AsInt32());
- context.Assembler.And(memOp, notBits, OperandType.I32);
- }
- context.Assembler.Ldmxcsr(memOp);
- break;
- }
- case IntrinsicType.PopCount:
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- EnsureSameType(dest, source);
- Debug.Assert(dest.Type.IsInteger());
- context.Assembler.Popcnt(dest, source, dest.Type);
- break;
- }
- case IntrinsicType.Unary:
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- EnsureSameType(dest, source);
- Debug.Assert(!dest.Type.IsInteger());
- context.Assembler.WriteInstruction(info.Inst, dest, source);
- break;
- }
- case IntrinsicType.UnaryToGpr:
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type.IsInteger() && !source.Type.IsInteger());
- if (intrinOp.Intrinsic == Intrinsic.X86Cvtsi2si)
- {
- if (dest.Type == OperandType.I32)
- {
- context.Assembler.Movd(dest, source); // int _mm_cvtsi128_si32(__m128i a)
- }
- else /* if (dest.Type == OperandType.I64) */
- {
- context.Assembler.Movq(dest, source); // __int64 _mm_cvtsi128_si64(__m128i a)
- }
- }
- else
- {
- context.Assembler.WriteInstruction(info.Inst, dest, source, dest.Type);
- }
- break;
- }
- case IntrinsicType.Binary:
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- EnsureSameType(dest, src1);
- if (!HardwareCapabilities.SupportsVexEncoding)
- {
- EnsureSameReg(dest, src1);
- }
- Debug.Assert(!dest.Type.IsInteger());
- Debug.Assert(!src2.Type.IsInteger() || src2.Kind == OperandKind.Constant);
- context.Assembler.WriteInstruction(info.Inst, dest, src1, src2);
- break;
- }
- case IntrinsicType.BinaryGpr:
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- EnsureSameType(dest, src1);
- if (!HardwareCapabilities.SupportsVexEncoding)
- {
- EnsureSameReg(dest, src1);
- }
- Debug.Assert(!dest.Type.IsInteger() && src2.Type.IsInteger());
- context.Assembler.WriteInstruction(info.Inst, dest, src1, src2, src2.Type);
- break;
- }
- case IntrinsicType.Crc32:
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- EnsureSameReg(dest, src1);
- Debug.Assert(dest.Type.IsInteger() && src1.Type.IsInteger() && src2.Type.IsInteger());
- context.Assembler.WriteInstruction(info.Inst, dest, src2, dest.Type);
- break;
- }
- case IntrinsicType.BinaryImm:
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- EnsureSameType(dest, src1);
- if (!HardwareCapabilities.SupportsVexEncoding)
- {
- EnsureSameReg(dest, src1);
- }
- Debug.Assert(!dest.Type.IsInteger() && src2.Kind == OperandKind.Constant);
- context.Assembler.WriteInstruction(info.Inst, dest, src1, src2.AsByte());
- break;
- }
- case IntrinsicType.Ternary:
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- Operand src3 = operation.GetSource(2);
- EnsureSameType(dest, src1, src2, src3);
- Debug.Assert(!dest.Type.IsInteger());
- if (info.Inst == X86Instruction.Blendvpd && HardwareCapabilities.SupportsVexEncoding)
- {
- context.Assembler.WriteInstruction(X86Instruction.Vblendvpd, dest, src1, src2, src3);
- }
- else if (info.Inst == X86Instruction.Blendvps && HardwareCapabilities.SupportsVexEncoding)
- {
- context.Assembler.WriteInstruction(X86Instruction.Vblendvps, dest, src1, src2, src3);
- }
- else if (info.Inst == X86Instruction.Pblendvb && HardwareCapabilities.SupportsVexEncoding)
- {
- context.Assembler.WriteInstruction(X86Instruction.Vpblendvb, dest, src1, src2, src3);
- }
- else
- {
- EnsureSameReg(dest, src1);
- Debug.Assert(src3.GetRegister().Index == 0);
- context.Assembler.WriteInstruction(info.Inst, dest, src1, src2);
- }
- break;
- }
- case IntrinsicType.TernaryImm:
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- Operand src3 = operation.GetSource(2);
- EnsureSameType(dest, src1, src2);
- if (!HardwareCapabilities.SupportsVexEncoding)
- {
- EnsureSameReg(dest, src1);
- }
- Debug.Assert(!dest.Type.IsInteger() && src3.Kind == OperandKind.Constant);
- context.Assembler.WriteInstruction(info.Inst, dest, src1, src2, src3.AsByte());
- break;
- }
- case IntrinsicType.Fma:
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- Operand src3 = operation.GetSource(2);
- Debug.Assert(HardwareCapabilities.SupportsVexEncoding);
- Debug.Assert(dest.Kind == OperandKind.Register && src1.Kind == OperandKind.Register && src2.Kind == OperandKind.Register);
- Debug.Assert(src3.Kind == OperandKind.Register || src3.Kind == OperandKind.Memory);
- EnsureSameType(dest, src1, src2, src3);
- Debug.Assert(dest.Type == OperandType.V128);
- Debug.Assert(dest.Value == src1.Value);
- context.Assembler.WriteInstruction(info.Inst, dest, src2, src3);
- break;
- }
- }
- }
- else
- {
- Action<CodeGenContext, Operation> func = _instTable[(int)operation.Instruction];
- if (func != null)
- {
- func(context, operation);
- }
- else
- {
- throw new ArgumentException($"Invalid instruction \"{operation.Instruction}\".");
- }
- }
- }
- private static void GenerateAdd(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- if (dest.Type.IsInteger())
- {
- // If Destination and Source 1 Operands are the same, perform a standard add as there are no benefits to using LEA.
- if (dest.Kind == src1.Kind && dest.Value == src1.Value)
- {
- ValidateBinOp(dest, src1, src2);
- context.Assembler.Add(dest, src2, dest.Type);
- }
- else
- {
- EnsureSameType(dest, src1, src2);
- int offset;
- Operand index;
- if (src2.Kind == OperandKind.Constant)
- {
- offset = src2.AsInt32();
- index = null;
- }
- else
- {
- offset = 0;
- index = src2;
- }
- MemoryOperand memOp = MemoryOp(dest.Type, src1, index, Multiplier.x1, offset);
- context.Assembler.Lea(dest, memOp, dest.Type);
- }
- }
- else
- {
- ValidateBinOp(dest, src1, src2);
- if (dest.Type == OperandType.FP32)
- {
- context.Assembler.Addss(dest, src1, src2);
- }
- else /* if (dest.Type == OperandType.FP64) */
- {
- context.Assembler.Addsd(dest, src1, src2);
- }
- }
- }
- private static void GenerateBitwiseAnd(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- ValidateBinOp(dest, src1, src2);
- Debug.Assert(dest.Type.IsInteger());
- // Note: GenerateCompareCommon makes the assumption that BitwiseAnd will emit only a single `and`
- // instruction.
- context.Assembler.And(dest, src2, dest.Type);
- }
- private static void GenerateBitwiseExclusiveOr(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- ValidateBinOp(dest, src1, src2);
- if (dest.Type.IsInteger())
- {
- context.Assembler.Xor(dest, src2, dest.Type);
- }
- else
- {
- context.Assembler.Xorps(dest, src1, src2);
- }
- }
- private static void GenerateBitwiseNot(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- ValidateUnOp(dest, source);
- Debug.Assert(dest.Type.IsInteger());
- context.Assembler.Not(dest);
- }
- private static void GenerateBitwiseOr(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- ValidateBinOp(dest, src1, src2);
- Debug.Assert(dest.Type.IsInteger());
- context.Assembler.Or(dest, src2, dest.Type);
- }
- private static void GenerateBranchIf(CodeGenContext context, Operation operation)
- {
- Operand comp = operation.GetSource(2);
- Debug.Assert(comp.Kind == OperandKind.Constant);
- var cond = ((Comparison)comp.AsInt32()).ToX86Condition();
- GenerateCompareCommon(context, operation);
- context.JumpTo(cond, context.CurrBlock.GetSuccessor(1));
- }
- private static void GenerateByteSwap(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- ValidateUnOp(dest, source);
- Debug.Assert(dest.Type.IsInteger());
- context.Assembler.Bswap(dest);
- }
- private static void GenerateCall(CodeGenContext context, Operation operation)
- {
- context.Assembler.Call(operation.GetSource(0));
- }
- private static void GenerateClobber(CodeGenContext context, Operation operation)
- {
- // This is only used to indicate that a register is clobbered to the
- // register allocator, we don't need to produce any code.
- }
- private static void GenerateCompare(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand comp = operation.GetSource(2);
- Debug.Assert(dest.Type == OperandType.I32);
- Debug.Assert(comp.Kind == OperandKind.Constant);
- var cond = ((Comparison)comp.AsInt32()).ToX86Condition();
- GenerateCompareCommon(context, operation);
- context.Assembler.Setcc(dest, cond);
- context.Assembler.Movzx8(dest, dest, OperandType.I32);
- }
- private static void GenerateCompareCommon(CodeGenContext context, Operation operation)
- {
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- EnsureSameType(src1, src2);
- Debug.Assert(src1.Type.IsInteger());
- if (src2.Kind == OperandKind.Constant && src2.Value == 0)
- {
- if (MatchOperation(operation.ListPrevious, Instruction.BitwiseAnd, src1.Type, src1.GetRegister()))
- {
- // Since the `test` and `and` instruction set the status flags in the same way, we can omit the
- // `test r,r` instruction when it is immediately preceded by an `and r,*` instruction.
- //
- // For example:
- //
- // and eax, 0x3
- // test eax, eax
- // jz .L0
- //
- // =>
- //
- // and eax, 0x3
- // jz .L0
- }
- else
- {
- context.Assembler.Test(src1, src1, src1.Type);
- }
- }
- else
- {
- context.Assembler.Cmp(src1, src2, src1.Type);
- }
- }
- private static void GenerateCompareAndSwap(CodeGenContext context, Operation operation)
- {
- Operand src1 = operation.GetSource(0);
- if (operation.SourcesCount == 5) // CompareAndSwap128 has 5 sources, compared to CompareAndSwap64/32's 3.
- {
- MemoryOperand memOp = MemoryOp(OperandType.I64, src1);
- context.Assembler.Cmpxchg16b(memOp);
- }
- else
- {
- Operand src2 = operation.GetSource(1);
- Operand src3 = operation.GetSource(2);
- EnsureSameType(src2, src3);
- MemoryOperand memOp = MemoryOp(src3.Type, src1);
- context.Assembler.Cmpxchg(memOp, src3);
- }
- }
- private static void GenerateCompareAndSwap16(CodeGenContext context, Operation operation)
- {
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- Operand src3 = operation.GetSource(2);
- EnsureSameType(src2, src3);
- MemoryOperand memOp = MemoryOp(src3.Type, src1);
- context.Assembler.Cmpxchg16(memOp, src3);
- }
- private static void GenerateCompareAndSwap8(CodeGenContext context, Operation operation)
- {
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- Operand src3 = operation.GetSource(2);
- EnsureSameType(src2, src3);
- MemoryOperand memOp = MemoryOp(src3.Type, src1);
- context.Assembler.Cmpxchg8(memOp, src3);
- }
- private static void GenerateConditionalSelect(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- Operand src3 = operation.GetSource(2);
- EnsureSameReg (dest, src3);
- EnsureSameType(dest, src2, src3);
- Debug.Assert(dest.Type.IsInteger());
- Debug.Assert(src1.Type == OperandType.I32);
- context.Assembler.Test (src1, src1, src1.Type);
- context.Assembler.Cmovcc(dest, src2, dest.Type, X86Condition.NotEqual);
- }
- private static void GenerateConvertI64ToI32(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type == OperandType.I32 && source.Type == OperandType.I64);
- context.Assembler.Mov(dest, source, OperandType.I32);
- }
- private static void GenerateConvertToFP(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type == OperandType.FP32 || dest.Type == OperandType.FP64);
- if (dest.Type == OperandType.FP32)
- {
- Debug.Assert(source.Type.IsInteger() || source.Type == OperandType.FP64);
- if (source.Type.IsInteger())
- {
- context.Assembler.Xorps (dest, dest, dest);
- context.Assembler.Cvtsi2ss(dest, dest, source, source.Type);
- }
- else /* if (source.Type == OperandType.FP64) */
- {
- context.Assembler.Cvtsd2ss(dest, dest, source);
- GenerateZeroUpper96(context, dest, dest);
- }
- }
- else /* if (dest.Type == OperandType.FP64) */
- {
- Debug.Assert(source.Type.IsInteger() || source.Type == OperandType.FP32);
- if (source.Type.IsInteger())
- {
- context.Assembler.Xorps (dest, dest, dest);
- context.Assembler.Cvtsi2sd(dest, dest, source, source.Type);
- }
- else /* if (source.Type == OperandType.FP32) */
- {
- context.Assembler.Cvtss2sd(dest, dest, source);
- GenerateZeroUpper64(context, dest, dest);
- }
- }
- }
- private static void GenerateCopy(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- EnsureSameType(dest, source);
- Debug.Assert(dest.Type.IsInteger() || source.Kind != OperandKind.Constant);
- // Moves to the same register are useless.
- if (dest.Kind == source.Kind && dest.Value == source.Value)
- {
- return;
- }
- if (dest.Kind == OperandKind.Register &&
- source.Kind == OperandKind.Constant && source.Value == 0)
- {
- // Assemble "mov reg, 0" as "xor reg, reg" as the later is more efficient.
- context.Assembler.Xor(dest, dest, OperandType.I32);
- }
- else if (dest.Type.IsInteger())
- {
- context.Assembler.Mov(dest, source, dest.Type);
- }
- else
- {
- context.Assembler.Movdqu(dest, source);
- }
- }
- private static void GenerateCountLeadingZeros(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- EnsureSameType(dest, source);
- Debug.Assert(dest.Type.IsInteger());
- context.Assembler.Bsr(dest, source, dest.Type);
- int operandSize = dest.Type == OperandType.I32 ? 32 : 64;
- int operandMask = operandSize - 1;
- // When the input operand is 0, the result is undefined, however the
- // ZF flag is set. We are supposed to return the operand size on that
- // case. So, add an additional jump to handle that case, by moving the
- // operand size constant to the destination register.
- context.JumpToNear(X86Condition.NotEqual);
- context.Assembler.Mov(dest, Const(operandSize | operandMask), OperandType.I32);
- context.JumpHere();
- // BSR returns the zero based index of the last bit set on the operand,
- // starting from the least significant bit. However we are supposed to
- // return the number of 0 bits on the high end. So, we invert the result
- // of the BSR using XOR to get the correct value.
- context.Assembler.Xor(dest, Const(operandMask), OperandType.I32);
- }
- private static void GenerateDivide(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand dividend = operation.GetSource(0);
- Operand divisor = operation.GetSource(1);
- if (!dest.Type.IsInteger())
- {
- ValidateBinOp(dest, dividend, divisor);
- }
- if (dest.Type.IsInteger())
- {
- divisor = operation.GetSource(2);
- EnsureSameType(dest, divisor);
- if (divisor.Type == OperandType.I32)
- {
- context.Assembler.Cdq();
- }
- else
- {
- context.Assembler.Cqo();
- }
- context.Assembler.Idiv(divisor);
- }
- else if (dest.Type == OperandType.FP32)
- {
- context.Assembler.Divss(dest, dividend, divisor);
- }
- else /* if (dest.Type == OperandType.FP64) */
- {
- context.Assembler.Divsd(dest, dividend, divisor);
- }
- }
- private static void GenerateDivideUI(CodeGenContext context, Operation operation)
- {
- Operand divisor = operation.GetSource(2);
- Operand rdx = Register(X86Register.Rdx);
- Debug.Assert(divisor.Type.IsInteger());
- context.Assembler.Xor(rdx, rdx, OperandType.I32);
- context.Assembler.Div(divisor);
- }
- private static void GenerateFill(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand offset = operation.GetSource(0);
- Debug.Assert(offset.Kind == OperandKind.Constant);
- int offs = offset.AsInt32() + context.CallArgsRegionSize;
- Operand rsp = Register(X86Register.Rsp);
- MemoryOperand memOp = MemoryOp(dest.Type, rsp, null, Multiplier.x1, offs);
- GenerateLoad(context, memOp, dest);
- }
- private static void GenerateLoad(CodeGenContext context, Operation operation)
- {
- Operand value = operation.Destination;
- Operand address = Memory(operation.GetSource(0), value.Type);
- GenerateLoad(context, address, value);
- }
- private static void GenerateLoad16(CodeGenContext context, Operation operation)
- {
- Operand value = operation.Destination;
- Operand address = Memory(operation.GetSource(0), value.Type);
- Debug.Assert(value.Type.IsInteger());
- context.Assembler.Movzx16(value, address, value.Type);
- }
- private static void GenerateLoad8(CodeGenContext context, Operation operation)
- {
- Operand value = operation.Destination;
- Operand address = Memory(operation.GetSource(0), value.Type);
- Debug.Assert(value.Type.IsInteger());
- context.Assembler.Movzx8(value, address, value.Type);
- }
- private static void GenerateMultiply(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- if (src2.Kind != OperandKind.Constant)
- {
- EnsureSameReg(dest, src1);
- }
- EnsureSameType(dest, src1, src2);
- if (dest.Type.IsInteger())
- {
- if (src2.Kind == OperandKind.Constant)
- {
- context.Assembler.Imul(dest, src1, src2, dest.Type);
- }
- else
- {
- context.Assembler.Imul(dest, src2, dest.Type);
- }
- }
- else if (dest.Type == OperandType.FP32)
- {
- context.Assembler.Mulss(dest, src1, src2);
- }
- else /* if (dest.Type == OperandType.FP64) */
- {
- context.Assembler.Mulsd(dest, src1, src2);
- }
- }
- private static void GenerateMultiply64HighSI(CodeGenContext context, Operation operation)
- {
- Operand source = operation.GetSource(1);
- Debug.Assert(source.Type == OperandType.I64);
- context.Assembler.Imul(source);
- }
- private static void GenerateMultiply64HighUI(CodeGenContext context, Operation operation)
- {
- Operand source = operation.GetSource(1);
- Debug.Assert(source.Type == OperandType.I64);
- context.Assembler.Mul(source);
- }
- private static void GenerateNegate(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- ValidateUnOp(dest, source);
- Debug.Assert(dest.Type.IsInteger());
- context.Assembler.Neg(dest);
- }
- private static void GenerateReturn(CodeGenContext context, Operation operation)
- {
- WriteEpilogue(context);
- context.Assembler.Return();
- }
- private static void GenerateRotateRight(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- ValidateShift(dest, src1, src2);
- context.Assembler.Ror(dest, src2, dest.Type);
- }
- private static void GenerateShiftLeft(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- ValidateShift(dest, src1, src2);
- context.Assembler.Shl(dest, src2, dest.Type);
- }
- private static void GenerateShiftRightSI(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- ValidateShift(dest, src1, src2);
- context.Assembler.Sar(dest, src2, dest.Type);
- }
- private static void GenerateShiftRightUI(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- ValidateShift(dest, src1, src2);
- context.Assembler.Shr(dest, src2, dest.Type);
- }
- private static void GenerateSignExtend16(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
- context.Assembler.Movsx16(dest, source, dest.Type);
- }
- private static void GenerateSignExtend32(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
- context.Assembler.Movsx32(dest, source, dest.Type);
- }
- private static void GenerateSignExtend8(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
- context.Assembler.Movsx8(dest, source, dest.Type);
- }
- private static void GenerateSpill(CodeGenContext context, Operation operation)
- {
- GenerateSpill(context, operation, context.CallArgsRegionSize);
- }
- private static void GenerateSpillArg(CodeGenContext context, Operation operation)
- {
- GenerateSpill(context, operation, 0);
- }
- private static void GenerateSpill(CodeGenContext context, Operation operation, int baseOffset)
- {
- Operand offset = operation.GetSource(0);
- Operand source = operation.GetSource(1);
- Debug.Assert(offset.Kind == OperandKind.Constant);
- int offs = offset.AsInt32() + baseOffset;
- Operand rsp = Register(X86Register.Rsp);
- MemoryOperand memOp = MemoryOp(source.Type, rsp, null, Multiplier.x1, offs);
- GenerateStore(context, memOp, source);
- }
- private static void GenerateStackAlloc(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand offset = operation.GetSource(0);
- Debug.Assert(offset.Kind == OperandKind.Constant);
- int offs = offset.AsInt32() + context.CallArgsRegionSize;
- Operand rsp = Register(X86Register.Rsp);
- MemoryOperand memOp = MemoryOp(OperandType.I64, rsp, null, Multiplier.x1, offs);
- context.Assembler.Lea(dest, memOp, OperandType.I64);
- }
- private static void GenerateStore(CodeGenContext context, Operation operation)
- {
- Operand value = operation.GetSource(1);
- Operand address = Memory(operation.GetSource(0), value.Type);
- GenerateStore(context, address, value);
- }
- private static void GenerateStore16(CodeGenContext context, Operation operation)
- {
- Operand value = operation.GetSource(1);
- Operand address = Memory(operation.GetSource(0), value.Type);
- Debug.Assert(value.Type.IsInteger());
- context.Assembler.Mov16(address, value);
- }
- private static void GenerateStore8(CodeGenContext context, Operation operation)
- {
- Operand value = operation.GetSource(1);
- Operand address = Memory(operation.GetSource(0), value.Type);
- Debug.Assert(value.Type.IsInteger());
- context.Assembler.Mov8(address, value);
- }
- private static void GenerateSubtract(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- ValidateBinOp(dest, src1, src2);
- if (dest.Type.IsInteger())
- {
- context.Assembler.Sub(dest, src2, dest.Type);
- }
- else if (dest.Type == OperandType.FP32)
- {
- context.Assembler.Subss(dest, src1, src2);
- }
- else /* if (dest.Type == OperandType.FP64) */
- {
- context.Assembler.Subsd(dest, src1, src2);
- }
- }
- private static void GenerateTailcall(CodeGenContext context, Operation operation)
- {
- WriteEpilogue(context);
- context.Assembler.Jmp(operation.GetSource(0));
- }
- private static void GenerateVectorCreateScalar(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(!dest.Type.IsInteger() && source.Type.IsInteger());
- if (source.Type == OperandType.I32)
- {
- context.Assembler.Movd(dest, source); // (__m128i _mm_cvtsi32_si128(int a))
- }
- else /* if (source.Type == OperandType.I64) */
- {
- context.Assembler.Movq(dest, source); // (__m128i _mm_cvtsi64_si128(__int64 a))
- }
- }
- private static void GenerateVectorExtract(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination; //Value
- Operand src1 = operation.GetSource(0); //Vector
- Operand src2 = operation.GetSource(1); //Index
- Debug.Assert(src1.Type == OperandType.V128);
- Debug.Assert(src2.Kind == OperandKind.Constant);
- byte index = src2.AsByte();
- Debug.Assert(index < OperandType.V128.GetSizeInBytes() / dest.Type.GetSizeInBytes());
- if (dest.Type == OperandType.I32)
- {
- if (index == 0)
- {
- context.Assembler.Movd(dest, src1);
- }
- else if (HardwareCapabilities.SupportsSse41)
- {
- context.Assembler.Pextrd(dest, src1, index);
- }
- else
- {
- int mask0 = 0b11_10_01_00;
- int mask1 = 0b11_10_01_00;
- mask0 = BitUtils.RotateRight(mask0, index * 2, 8);
- mask1 = BitUtils.RotateRight(mask1, 8 - index * 2, 8);
- context.Assembler.Pshufd(src1, src1, (byte)mask0);
- context.Assembler.Movd (dest, src1);
- context.Assembler.Pshufd(src1, src1, (byte)mask1);
- }
- }
- else if (dest.Type == OperandType.I64)
- {
- if (index == 0)
- {
- context.Assembler.Movq(dest, src1);
- }
- else if (HardwareCapabilities.SupportsSse41)
- {
- context.Assembler.Pextrq(dest, src1, index);
- }
- else
- {
- const byte mask = 0b01_00_11_10;
- context.Assembler.Pshufd(src1, src1, mask);
- context.Assembler.Movq (dest, src1);
- context.Assembler.Pshufd(src1, src1, mask);
- }
- }
- else
- {
- // Floating-point types.
- if ((index >= 2 && dest.Type == OperandType.FP32) ||
- (index == 1 && dest.Type == OperandType.FP64))
- {
- context.Assembler.Movhlps(dest, dest, src1);
- context.Assembler.Movq (dest, dest);
- }
- else
- {
- context.Assembler.Movq(dest, src1);
- }
- if (dest.Type == OperandType.FP32)
- {
- context.Assembler.Pshufd(dest, dest, (byte)(0xfc | (index & 1)));
- }
- }
- }
- private static void GenerateVectorExtract16(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination; //Value
- Operand src1 = operation.GetSource(0); //Vector
- Operand src2 = operation.GetSource(1); //Index
- Debug.Assert(src1.Type == OperandType.V128);
- Debug.Assert(src2.Kind == OperandKind.Constant);
- byte index = src2.AsByte();
- Debug.Assert(index < 8);
- context.Assembler.Pextrw(dest, src1, index);
- }
- private static void GenerateVectorExtract8(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination; //Value
- Operand src1 = operation.GetSource(0); //Vector
- Operand src2 = operation.GetSource(1); //Index
- Debug.Assert(src1.Type == OperandType.V128);
- Debug.Assert(src2.Kind == OperandKind.Constant);
- byte index = src2.AsByte();
- Debug.Assert(index < 16);
- if (HardwareCapabilities.SupportsSse41)
- {
- context.Assembler.Pextrb(dest, src1, index);
- }
- else
- {
- context.Assembler.Pextrw(dest, src1, (byte)(index >> 1));
- if ((index & 1) != 0)
- {
- context.Assembler.Shr(dest, Const(8), OperandType.I32);
- }
- else
- {
- context.Assembler.Movzx8(dest, dest, OperandType.I32);
- }
- }
- }
- private static void GenerateVectorInsert(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0); //Vector
- Operand src2 = operation.GetSource(1); //Value
- Operand src3 = operation.GetSource(2); //Index
- if (!HardwareCapabilities.SupportsVexEncoding)
- {
- EnsureSameReg(dest, src1);
- }
- Debug.Assert(src1.Type == OperandType.V128);
- Debug.Assert(src3.Kind == OperandKind.Constant);
- byte index = src3.AsByte();
- void InsertIntSse2(int words)
- {
- if (dest.GetRegister() != src1.GetRegister())
- {
- context.Assembler.Movdqu(dest, src1);
- }
- for (int word = 0; word < words; word++)
- {
- // Insert lower 16-bits.
- context.Assembler.Pinsrw(dest, dest, src2, (byte)(index * words + word));
- // Move next word down.
- context.Assembler.Ror(src2, Const(16), src2.Type);
- }
- }
- if (src2.Type == OperandType.I32)
- {
- Debug.Assert(index < 4);
- if (HardwareCapabilities.SupportsSse41)
- {
- context.Assembler.Pinsrd(dest, src1, src2, index);
- }
- else
- {
- InsertIntSse2(2);
- }
- }
- else if (src2.Type == OperandType.I64)
- {
- Debug.Assert(index < 2);
- if (HardwareCapabilities.SupportsSse41)
- {
- context.Assembler.Pinsrq(dest, src1, src2, index);
- }
- else
- {
- InsertIntSse2(4);
- }
- }
- else if (src2.Type == OperandType.FP32)
- {
- Debug.Assert(index < 4);
- if (index != 0)
- {
- if (HardwareCapabilities.SupportsSse41)
- {
- context.Assembler.Insertps(dest, src1, src2, (byte)(index << 4));
- }
- else
- {
- if (src1.GetRegister() == src2.GetRegister())
- {
- int mask = 0b11_10_01_00;
- mask &= ~(0b11 << index * 2);
- context.Assembler.Pshufd(dest, src1, (byte)mask);
- }
- else
- {
- int mask0 = 0b11_10_01_00;
- int mask1 = 0b11_10_01_00;
- mask0 = BitUtils.RotateRight(mask0, index * 2, 8);
- mask1 = BitUtils.RotateRight(mask1, 8 - index * 2, 8);
- context.Assembler.Pshufd(src1, src1, (byte)mask0); // Lane to be inserted in position 0.
- context.Assembler.Movss (dest, src1, src2); // dest[127:0] = src1[127:32] | src2[31:0]
- context.Assembler.Pshufd(dest, dest, (byte)mask1); // Inserted lane in original position.
- if (dest.GetRegister() != src1.GetRegister())
- {
- context.Assembler.Pshufd(src1, src1, (byte)mask1); // Restore src1.
- }
- }
- }
- }
- else
- {
- context.Assembler.Movss(dest, src1, src2);
- }
- }
- else /* if (src2.Type == OperandType.FP64) */
- {
- Debug.Assert(index < 2);
- if (index != 0)
- {
- context.Assembler.Movlhps(dest, src1, src2);
- }
- else
- {
- context.Assembler.Movsd(dest, src1, src2);
- }
- }
- }
- private static void GenerateVectorInsert16(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0); //Vector
- Operand src2 = operation.GetSource(1); //Value
- Operand src3 = operation.GetSource(2); //Index
- if (!HardwareCapabilities.SupportsVexEncoding)
- {
- EnsureSameReg(dest, src1);
- }
- Debug.Assert(src1.Type == OperandType.V128);
- Debug.Assert(src3.Kind == OperandKind.Constant);
- byte index = src3.AsByte();
- context.Assembler.Pinsrw(dest, src1, src2, index);
- }
- private static void GenerateVectorInsert8(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0); //Vector
- Operand src2 = operation.GetSource(1); //Value
- Operand src3 = operation.GetSource(2); //Index
- // It's not possible to emulate this instruction without
- // SSE 4.1 support without the use of a temporary register,
- // so we instead handle that case on the pre-allocator when
- // SSE 4.1 is not supported on the CPU.
- Debug.Assert(HardwareCapabilities.SupportsSse41);
- if (!HardwareCapabilities.SupportsVexEncoding)
- {
- EnsureSameReg(dest, src1);
- }
- Debug.Assert(src1.Type == OperandType.V128);
- Debug.Assert(src3.Kind == OperandKind.Constant);
- byte index = src3.AsByte();
- context.Assembler.Pinsrb(dest, src1, src2, index);
- }
- private static void GenerateVectorOne(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Debug.Assert(!dest.Type.IsInteger());
- context.Assembler.Pcmpeqw(dest, dest, dest);
- }
- private static void GenerateVectorZero(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Debug.Assert(!dest.Type.IsInteger());
- context.Assembler.Xorps(dest, dest, dest);
- }
- private static void GenerateVectorZeroUpper64(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type == OperandType.V128 && source.Type == OperandType.V128);
- GenerateZeroUpper64(context, dest, source);
- }
- private static void GenerateVectorZeroUpper96(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type == OperandType.V128 && source.Type == OperandType.V128);
- GenerateZeroUpper96(context, dest, source);
- }
- private static void GenerateZeroExtend16(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
- context.Assembler.Movzx16(dest, source, OperandType.I32);
- }
- private static void GenerateZeroExtend32(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
- context.Assembler.Mov(dest, source, OperandType.I32);
- }
- private static void GenerateZeroExtend8(CodeGenContext context, Operation operation)
- {
- Operand dest = operation.Destination;
- Operand source = operation.GetSource(0);
- Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
- context.Assembler.Movzx8(dest, source, OperandType.I32);
- }
- private static void GenerateLoad(CodeGenContext context, Operand address, Operand value)
- {
- switch (value.Type)
- {
- case OperandType.I32: context.Assembler.Mov (value, address, OperandType.I32); break;
- case OperandType.I64: context.Assembler.Mov (value, address, OperandType.I64); break;
- case OperandType.FP32: context.Assembler.Movd (value, address); break;
- case OperandType.FP64: context.Assembler.Movq (value, address); break;
- case OperandType.V128: context.Assembler.Movdqu(value, address); break;
- default: Debug.Assert(false); break;
- }
- }
- private static void GenerateStore(CodeGenContext context, Operand address, Operand value)
- {
- switch (value.Type)
- {
- case OperandType.I32: context.Assembler.Mov (address, value, OperandType.I32); break;
- case OperandType.I64: context.Assembler.Mov (address, value, OperandType.I64); break;
- case OperandType.FP32: context.Assembler.Movd (address, value); break;
- case OperandType.FP64: context.Assembler.Movq (address, value); break;
- case OperandType.V128: context.Assembler.Movdqu(address, value); break;
- default: Debug.Assert(false); break;
- }
- }
- private static void GenerateZeroUpper64(CodeGenContext context, Operand dest, Operand source)
- {
- context.Assembler.Movq(dest, source);
- }
- private static void GenerateZeroUpper96(CodeGenContext context, Operand dest, Operand source)
- {
- context.Assembler.Movq(dest, source);
- context.Assembler.Pshufd(dest, dest, 0xfc);
- }
- private static bool MatchOperation(Node node, Instruction inst, OperandType destType, Register destReg)
- {
- if (!(node is Operation operation) || node.DestinationsCount == 0)
- {
- return false;
- }
- if (operation.Instruction != inst)
- {
- return false;
- }
- Operand dest = operation.Destination;
- return dest.Kind == OperandKind.Register &&
- dest.Type == destType &&
- dest.GetRegister() == destReg;
- }
- [Conditional("DEBUG")]
- private static void ValidateUnOp(Operand dest, Operand source)
- {
- EnsureSameReg (dest, source);
- EnsureSameType(dest, source);
- }
- [Conditional("DEBUG")]
- private static void ValidateBinOp(Operand dest, Operand src1, Operand src2)
- {
- EnsureSameReg (dest, src1);
- EnsureSameType(dest, src1, src2);
- }
- [Conditional("DEBUG")]
- private static void ValidateShift(Operand dest, Operand src1, Operand src2)
- {
- EnsureSameReg (dest, src1);
- EnsureSameType(dest, src1);
- Debug.Assert(dest.Type.IsInteger() && src2.Type == OperandType.I32);
- }
- private static void EnsureSameReg(Operand op1, Operand op2)
- {
- if (!op1.Type.IsInteger() && HardwareCapabilities.SupportsVexEncoding)
- {
- return;
- }
- Debug.Assert(op1.Kind == OperandKind.Register || op1.Kind == OperandKind.Memory);
- Debug.Assert(op1.Kind == op2.Kind);
- Debug.Assert(op1.Value == op2.Value);
- }
- private static void EnsureSameType(Operand op1, Operand op2)
- {
- Debug.Assert(op1.Type == op2.Type);
- }
- private static void EnsureSameType(Operand op1, Operand op2, Operand op3)
- {
- Debug.Assert(op1.Type == op2.Type);
- Debug.Assert(op1.Type == op3.Type);
- }
- private static void EnsureSameType(Operand op1, Operand op2, Operand op3, Operand op4)
- {
- Debug.Assert(op1.Type == op2.Type);
- Debug.Assert(op1.Type == op3.Type);
- Debug.Assert(op1.Type == op4.Type);
- }
- private static UnwindInfo WritePrologue(CodeGenContext context)
- {
- List<UnwindPushEntry> pushEntries = new List<UnwindPushEntry>();
- Operand rsp = Register(X86Register.Rsp);
- int mask = CallingConvention.GetIntCalleeSavedRegisters() & context.AllocResult.IntUsedRegisters;
- while (mask != 0)
- {
- int bit = BitOperations.TrailingZeroCount(mask);
- context.Assembler.Push(Register((X86Register)bit));
- pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.PushReg, context.StreamOffset, regIndex: bit));
- mask &= ~(1 << bit);
- }
- int reservedStackSize = context.CallArgsRegionSize + context.AllocResult.SpillRegionSize;
- reservedStackSize += context.XmmSaveRegionSize;
- if (reservedStackSize >= StackGuardSize)
- {
- GenerateInlineStackProbe(context, reservedStackSize);
- }
- if (reservedStackSize != 0)
- {
- context.Assembler.Sub(rsp, Const(reservedStackSize), OperandType.I64);
- pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.AllocStack, context.StreamOffset, stackOffsetOrAllocSize: reservedStackSize));
- }
- int offset = reservedStackSize;
- mask = CallingConvention.GetVecCalleeSavedRegisters() & context.AllocResult.VecUsedRegisters;
- while (mask != 0)
- {
- int bit = BitOperations.TrailingZeroCount(mask);
- offset -= 16;
- MemoryOperand memOp = MemoryOp(OperandType.V128, rsp, null, Multiplier.x1, offset);
- context.Assembler.Movdqu(memOp, Xmm((X86Register)bit));
- pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.SaveXmm128, context.StreamOffset, bit, offset));
- mask &= ~(1 << bit);
- }
- return new UnwindInfo(pushEntries.ToArray(), context.StreamOffset);
- }
- private static void WriteEpilogue(CodeGenContext context)
- {
- Operand rsp = Register(X86Register.Rsp);
- int reservedStackSize = context.CallArgsRegionSize + context.AllocResult.SpillRegionSize;
- reservedStackSize += context.XmmSaveRegionSize;
- int offset = reservedStackSize;
- int mask = CallingConvention.GetVecCalleeSavedRegisters() & context.AllocResult.VecUsedRegisters;
- while (mask != 0)
- {
- int bit = BitOperations.TrailingZeroCount(mask);
- offset -= 16;
- MemoryOperand memOp = MemoryOp(OperandType.V128, rsp, null, Multiplier.x1, offset);
- context.Assembler.Movdqu(Xmm((X86Register)bit), memOp);
- mask &= ~(1 << bit);
- }
- if (reservedStackSize != 0)
- {
- context.Assembler.Add(rsp, Const(reservedStackSize), OperandType.I64);
- }
- mask = CallingConvention.GetIntCalleeSavedRegisters() & context.AllocResult.IntUsedRegisters;
- while (mask != 0)
- {
- int bit = BitUtils.HighestBitSet(mask);
- context.Assembler.Pop(Register((X86Register)bit));
- mask &= ~(1 << bit);
- }
- }
- private static void GenerateInlineStackProbe(CodeGenContext context, int size)
- {
- // Windows does lazy stack allocation, and there are just 2
- // guard pages on the end of the stack. So, if the allocation
- // size we make is greater than this guard size, we must ensure
- // that the OS will map all pages that we'll use. We do that by
- // doing a dummy read on those pages, forcing a page fault and
- // the OS to map them. If they are already mapped, nothing happens.
- const int pageMask = PageSize - 1;
- size = (size + pageMask) & ~pageMask;
- Operand rsp = Register(X86Register.Rsp);
- Operand temp = Register(CallingConvention.GetIntReturnRegister());
- for (int offset = PageSize; offset < size; offset += PageSize)
- {
- Operand memOp = MemoryOp(OperandType.I32, rsp, null, Multiplier.x1, -offset);
- context.Assembler.Mov(temp, memOp, OperandType.I32);
- }
- }
- private static MemoryOperand Memory(Operand operand, OperandType type)
- {
- if (operand.Kind == OperandKind.Memory)
- {
- return operand as MemoryOperand;
- }
- return MemoryOp(type, operand);
- }
- private static Operand Register(X86Register register, OperandType type = OperandType.I64)
- {
- return OperandHelper.Register((int)register, RegisterType.Integer, type);
- }
- private static Operand Xmm(X86Register register)
- {
- return OperandHelper.Register((int)register, RegisterType.Vector, OperandType.V128);
- }
- }
- }
|