| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851 |
- using ARMeilleure.CodeGen.Linking;
- 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 System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Numerics;
- using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
- 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)
- {
- ControlFlowGraph cfg = cctx.Cfg;
- Logger.StartPass(PassName.Optimization);
- if (cctx.Options.HasFlag(CompilerOptions.Optimize))
- {
- if (cctx.Options.HasFlag(CompilerOptions.SsaForm))
- {
- Optimizer.RunPass(cfg);
- }
- BlockPlacement.RunPass(cfg);
- }
- X86Optimizer.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.HasFlag(CompilerOptions.SsaForm))
- {
- Ssa.Deconstruct(cfg);
- }
- IRegisterAllocator regAlloc;
- if (cctx.Options.HasFlag(CompilerOptions.Lsra))
- {
- 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);
- bool relocatable = (cctx.Options & CompilerOptions.Relocatable) != 0;
- using MemoryStream stream = new();
- CodeGenContext context = new(stream, allocResult, maxCallArgs, cfg.Blocks.Count, relocatable);
- UnwindInfo unwindInfo = WritePrologue(context);
- for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
- {
- context.EnterBlock(block);
- for (Operation node = block.Operations.First; node != default; node = node.ListNext)
- {
- GenerateOperation(context, node);
- }
- if (block.SuccessorsCount == 0)
- {
- // The only blocks which can have 0 successors are exit blocks.
- Operation last = block.Operations.Last;
- Debug.Assert(last.Instruction == Instruction.Tailcall ||
- last.Instruction == Instruction.Return);
- }
- else
- {
- BasicBlock succ = block.GetSuccessor(0);
- if (succ != block.ListNext)
- {
- context.JumpTo(succ);
- }
- }
- }
- (byte[] code, RelocInfo relocInfo) = context.GetCode();
- Logger.EndPass(PassName.CodeGeneration);
- return new CompiledFunction(code, unwindInfo, relocInfo);
- }
- private static void GenerateOperation(CodeGenContext context, Operation operation)
- {
- if (operation.Instruction == Instruction.Extended)
- {
- IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
- switch (info.Type)
- {
- case IntrinsicType.Comis_:
- {
- Operand dest = operation.Destination;
- Operand src1 = operation.GetSource(0);
- Operand src2 = operation.GetSource(1);
- switch (operation.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);
- Operand memOp = MemoryOp(OperandType.I32, rsp, default, Multiplier.x1, offs);
- Debug.Assert(HardwareCapabilities.SupportsSse || HardwareCapabilities.SupportsVexEncoding);
- context.Assembler.Stmxcsr(memOp);
- if (operation.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 (operation.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 = default;
- }
- else
- {
- offset = 0;
- index = src2;
- }
- Operand 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.
- {
- Operand memOp = MemoryOp(OperandType.I64, src1);
- context.Assembler.Cmpxchg16b(memOp);
- }
- else
- {
- Operand src2 = operation.GetSource(1);
- Operand src3 = operation.GetSource(2);
- EnsureSameType(src2, src3);
- Operand 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);
- Operand 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);
- Operand 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);
- Operand memOp = MemoryOp(dest.Type, rsp, default, 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);
- Operand memOp = MemoryOp(source.Type, rsp, default, 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);
- Operand memOp = MemoryOp(OperandType.I64, rsp, default, 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(Operation node, Instruction inst, OperandType destType, Register destReg)
- {
- if (node == default || node.DestinationsCount == 0)
- {
- return false;
- }
- if (node.Instruction != inst)
- {
- return false;
- }
- Operand dest = node.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;
- Operand memOp = MemoryOp(OperandType.V128, rsp, default, 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;
- Operand memOp = MemoryOp(OperandType.V128, rsp, default, 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, default, Multiplier.x1, -offset);
- context.Assembler.Mov(temp, memOp, OperandType.I32);
- }
- }
- private static Operand Memory(Operand operand, OperandType type)
- {
- if (operand.Kind == OperandKind.Memory)
- {
- return operand;
- }
- return MemoryOp(type, operand);
- }
- private static Operand Register(X86Register register, OperandType type = OperandType.I64)
- {
- return Operand.Factory.Register((int)register, RegisterType.Integer, type);
- }
- private static Operand Xmm(X86Register register)
- {
- return Operand.Factory.Register((int)register, RegisterType.Vector, OperandType.V128);
- }
- }
- }
|