/// <summary> /// Makes a copy of the instruction with a new opcode. /// </summary> /// <param name="newOpcode">The new opcode</param> /// <returns>The copy</returns> public MsilInstruction CopyWith(OpCode newOpcode) { var result = new MsilInstruction(newOpcode); Operand?.CopyTo(result.Operand); foreach (MsilLabel x in Labels) { result.Labels.Add(x); } result.TryCatchOperation = TryCatchOperation; return(result); }
/// <summary> /// Gets the constant int this instruction pushes onto the stack. /// </summary> /// <param name="m">Instruction</param> /// <returns>The constant int</returns> public static int GetConstInt(this MsilInstruction m) { if (m.OpCode == OpCodes.Ldc_I4_0) { return(0); } if (m.OpCode == OpCodes.Ldc_I4_1) { return(1); } if (m.OpCode == OpCodes.Ldc_I4_2) { return(2); } if (m.OpCode == OpCodes.Ldc_I4_3) { return(3); } if (m.OpCode == OpCodes.Ldc_I4_4) { return(4); } if (m.OpCode == OpCodes.Ldc_I4_5) { return(5); } if (m.OpCode == OpCodes.Ldc_I4_6) { return(6); } if (m.OpCode == OpCodes.Ldc_I4_7) { return(7); } if (m.OpCode == OpCodes.Ldc_I4_8) { return(8); } if (m.OpCode == OpCodes.Ldc_I4_M1) { return(-1); } if (m.OpCode == OpCodes.Ldc_I4) { return(((MsilOperandInline <int>)m.Operand).Value); } if (m.OpCode == OpCodes.Ldc_I4_S) { return(((MsilOperandInline <byte>)m.Operand).Value); } throw new ArgumentException($"Can't get constant int from instruction {m}"); }
/// <summary> /// Determines if this instruction is a constant int load instruction. /// </summary> /// <param name="m">Instruction</param> /// <returns>True if this instruction pushes a constant int onto the stack</returns> public static bool IsConstIntLoad(this MsilInstruction m) { if (m.OpCode == OpCodes.Ldc_I4_0) { return(true); } if (m.OpCode == OpCodes.Ldc_I4_1) { return(true); } if (m.OpCode == OpCodes.Ldc_I4_2) { return(true); } if (m.OpCode == OpCodes.Ldc_I4_3) { return(true); } if (m.OpCode == OpCodes.Ldc_I4_4) { return(true); } if (m.OpCode == OpCodes.Ldc_I4_5) { return(true); } if (m.OpCode == OpCodes.Ldc_I4_6) { return(true); } if (m.OpCode == OpCodes.Ldc_I4_7) { return(true); } if (m.OpCode == OpCodes.Ldc_I4_8) { return(true); } if (m.OpCode == OpCodes.Ldc_I4_M1) { return(true); } if (m.OpCode == OpCodes.Ldc_I4) { return(true); } return(m.OpCode == OpCodes.Ldc_I4_S); }
/// <summary> /// For a local referencing opcode, get the local it is referencing. /// </summary> public static MsilLocal GetReferencedLocal(this MsilInstruction me) { if (me.Operand is MsilOperandInline.MsilOperandLocal mol) { return(mol.Value); } if (me.OpCode == OpCodes.Stloc_0 || me.OpCode == OpCodes.Ldloc_0) { return(new MsilLocal(0)); } if (me.OpCode == OpCodes.Stloc_1 || me.OpCode == OpCodes.Ldloc_1) { return(new MsilLocal(1)); } if (me.OpCode == OpCodes.Stloc_2 || me.OpCode == OpCodes.Ldloc_2) { return(new MsilLocal(2)); } if (me.OpCode == OpCodes.Stloc_3 || me.OpCode == OpCodes.Ldloc_3) { return(new MsilLocal(3)); } throw new ArgumentException($"Can't get referenced local in instruction {me}"); }
/// <summary> /// For an argument referencing opcode, get the index of the local it is referencing. /// </summary> public static MsilArgument GetReferencedArgument(this MsilInstruction me) { if (me.Operand is MsilOperandInline.MsilOperandArgument mol) { return(mol.Value); } if (me.OpCode == OpCodes.Ldarg_0) { return(new MsilArgument(0)); } if (me.OpCode == OpCodes.Ldarg_1) { return(new MsilArgument(1)); } if (me.OpCode == OpCodes.Ldarg_2) { return(new MsilArgument(2)); } if (me.OpCode == OpCodes.Ldarg_3) { return(new MsilArgument(3)); } throw new ArgumentException($"Can't get referenced argument in instruction {me}"); }
internal MsilOperandLocal(MsilInstruction instruction) : base(instruction) { }
internal MsilOperandArgument(MsilInstruction instruction) : base(instruction) { }
internal MsilOperandSignature(MsilInstruction instruction) : base(instruction) { }
internal MsilOperandString(MsilInstruction instruction) : base(instruction) { }
internal MsilOperandDouble(MsilInstruction instruction) : base(instruction) { }
internal MsilOperandSingle(MsilInstruction instruction) : base(instruction) { }
internal MsilOperandSwitch(MsilInstruction instruction) : base(instruction) { }
/// <summary> /// Is this instruction a local store instruction. /// </summary> public static bool IsLocalStore(this MsilInstruction me) { return(me.OpCode.IsLocalStore()); }
internal MsilOperandBrTarget(MsilInstruction instruction) : base(instruction) { }
/// <summary> /// Is this instruction an argument load-by-value instruction. /// </summary> public static bool IsArgumentLoad(this MsilInstruction me) { return(me.OpCode == OpCodes.Ldarg || me.OpCode == OpCodes.Ldarg_S || me.OpCode == OpCodes.Ldarg_0 || me.OpCode == OpCodes.Ldarg_1 || me.OpCode == OpCodes.Ldarg_2 || me.OpCode == OpCodes.Ldarg_3); }
/// <summary> /// Is this instruction a local load-by-reference instruction. /// </summary> public static bool IsLocalLoadByRef(this MsilInstruction me) { return(me.OpCode.IsLocalLoadByRef()); }
/// <summary> /// Is this instruction an argument load-by-reference instruction. /// </summary> public static bool IsArgumentLoadByRef(this MsilInstruction me) { return(me.OpCode == OpCodes.Ldarga || me.OpCode == OpCodes.Ldarga_S); }
internal MsilOperandReflected(MsilInstruction instruction) : base(instruction) { }
protected MsilOperand(MsilInstruction instruction) { Instruction = instruction; }
internal MsilOperandInt32(MsilInstruction instruction) : base(instruction) { }
/// <summary> /// Is this instruction an argument store instruction. /// </summary> public static bool IsArgumentStore(this MsilInstruction me) { return(me.OpCode == OpCodes.Starg || me.OpCode == OpCodes.Starg_S); }