Exemple #1
0
        private static void RewriteConstantString(ScopedProcessor processor, TypeDefinition enumTypeDefinition, Instruction loadTokenInstruction, Instruction getTypeFromHandleInstruction, Instruction loadStrInstruction, Instruction isDefinedInstruction)
        {
            var name      = (string)loadStrInstruction.Operand;
            var constExpr = enumTypeDefinition.Fields.Skip(1).Any(x => x.Name == name);

            processor
            .Remove(loadTokenInstruction)
            .Remove(getTypeFromHandleInstruction)
            .Remove(loadStrInstruction)
            .Replace(isDefinedInstruction, InstructionUtility.LoadConstant(constExpr));
        }
Exemple #2
0
 public static ILProcessor Sub(this ILProcessor processor, long value)
 {
     if (value == 0L)
     {
         return(processor);
     }
     if (value < 0L && value >= -7L)
     {
         return(processor.AddRange(InstructionUtility.LoadConstant(-value)).Add());
     }
     return(processor.AddRange(InstructionUtility.LoadConstant(value)).Sub());
 }
Exemple #3
0
 public static ILProcessor Sub(this ILProcessor processor, int value)
 {
     if (value == 0)
     {
         return(processor);
     }
     if (value < 0 && value >= -7)
     {
         return(processor.Add(InstructionUtility.LoadConstant(-value)).Add());
     }
     return(processor.Add(InstructionUtility.LoadConstant(value)).Sub());
 }
Exemple #4
0
        private static Instruction[] InitializeArrayInstructions(ModuleDefinition moduleDefinition, TypeDefinition enumTypeDefinition, ModuleDefinition systemModuleDefinition, int count)
        {
            var privateImplementationDetails    = moduleDefinition.GetOrCreatePrivateImplementationDetails(systemModuleDefinition);
            var dataFieldDefinition             = moduleDefinition.GetOrCreateEnumValues(privateImplementationDetails, systemModuleDefinition, enumTypeDefinition);
            var initializeArrayMethodDefinition = systemModuleDefinition.GetRuntimeHelpers_InitializeArray();

            return(new[]
            {
                InstructionUtility.LoadConstant(count),
                Instruction.Create(OpCodes.Newarr, moduleDefinition.ImportReference(enumTypeDefinition)),
                Instruction.Create(OpCodes.Dup),
                Instruction.Create(OpCodes.Ldtoken, moduleDefinition.ImportReference(dataFieldDefinition)),
                Instruction.Create(OpCodes.Call, moduleDefinition.ImportReference(initializeArrayMethodDefinition)),
            });
        }
Exemple #5
0
 public static ILProcessor LdC <T>(this ILProcessor processor, T value) where T : unmanaged
 {
     if (typeof(T) == typeof(byte))
     {
         return(processor.Add(InstructionUtility.LoadConstant((sbyte)*(byte *)&value)));
     }
     if (typeof(T) == typeof(sbyte))
     {
         return(processor.Add(InstructionUtility.LoadConstant(*(sbyte *)&value)));
     }
     if (typeof(T) == typeof(short))
     {
         return(processor.Add(InstructionUtility.LoadConstant(*(short *)&value)));
     }
     if (typeof(T) == typeof(ushort))
     {
         return(processor.Add(InstructionUtility.LoadConstant(*(ushort *)&value)));
     }
     if (typeof(T) == typeof(int))
     {
         return(processor.Add(InstructionUtility.LoadConstant(*(int *)&value)));
     }
     if (typeof(T) == typeof(uint))
     {
         return(processor.Add(InstructionUtility.LoadConstant(*(int *)&value)));
     }
     if (typeof(T) == typeof(long))
     {
         return(processor.AddRange(InstructionUtility.LoadConstant(*(long *)&value)));
     }
     if (typeof(T) == typeof(ulong))
     {
         return(processor.AddRange(InstructionUtility.LoadConstant(*(long *)&value)));
     }
     if (typeof(T) == typeof(float))
     {
         return(processor.Add(InstructionUtility.LoadConstant(*(float *)&value)));
     }
     if (typeof(T) == typeof(double))
     {
         return(processor.Add(InstructionUtility.LoadConstant(*(double *)&value)));
     }
     if (typeof(T) == typeof(bool))
     {
         return(processor.Add(InstructionUtility.LoadConstant(*(bool *)&value)));
     }
     throw new ArgumentException("Type mismatch!" + typeof(T).Name);
 }
Exemple #6
0
        private static void TryRewriteLoadInt64ConstantWhenBaseInt64(Instruction loadNumberInstruction, TypeDefinition enumTypeDefinition, string enumBaseTypeName, ScopedProcessor processor, Instruction loadTokenInstruction, Instruction getTypeFromHandleInstruction)
        {
            var boxInstruction = loadNumberInstruction.Next;

            if (!IsValidBoxInstruction(boxInstruction, enumTypeDefinition, enumBaseTypeName))
            {
                return;
            }
            var isDefinedInstruction = boxInstruction.Next;

            if (!IsValidIsDefinedInstruction(isDefinedInstruction))
            {
                return;
            }
            var  number = (long)loadNumberInstruction.Operand;
            bool answer;

            switch (enumBaseTypeName)
            {
            case "Int64":
                answer = enumTypeDefinition.Fields.Skip(1).Any(x => (long)x.Constant == number);
                break;

            case "UInt64":
                answer = enumTypeDefinition.Fields.Skip(1).Any(x => (ulong)x.Constant == (ulong)number);
                break;

            default: throw new ArgumentOutOfRangeException(enumBaseTypeName);
            }
            processor
            .Remove(loadTokenInstruction)
            .Remove(getTypeFromHandleInstruction)
            .Remove(loadNumberInstruction)
            .Remove(boxInstruction)
            .Replace(isDefinedInstruction, InstructionUtility.LoadConstant(answer));
        }
Exemple #7
0
 public static ILProcessor BleS <T>(this ILProcessor processor, Instruction instruction) => processor.Add(InstructionUtility.BleS <T>(instruction));
Exemple #8
0
 public static ILProcessor Switch <T>(this ILProcessor processor, Instruction[] instructions) => processor.AddRange(InstructionUtility.Switch <T>(instructions));
Exemple #9
0
 public static ILProcessor Clt <T>(this ILProcessor processor) => processor.Add(InstructionUtility.Clt <T>());
Exemple #10
0
        private static void TryRewriteLoadInt64ConstantWhenBaseInt32(Instruction loadNumberInstruction, TypeDefinition enumTypeDefinition, string enumBaseTypeName, ScopedProcessor processor, Instruction loadTokenInstruction, Instruction getTypeFromHandleInstruction)
        {
            var convInstruction = loadNumberInstruction.Next;

            if (convInstruction is null)
            {
                return;
            }
            switch (convInstruction.OpCode.Code)
            {
            case Code.Conv_I1:
            case Code.Conv_I2:
            case Code.Conv_I4:
            case Code.Conv_U1:
            case Code.Conv_U2:
            case Code.Conv_U4:
                break;

            default:
                return;
            }
            var boxInstruction = convInstruction.Next;

            if (!IsValidBoxInstruction(boxInstruction, enumTypeDefinition, enumBaseTypeName))
            {
                return;
            }
            var isDefinedInstruction = boxInstruction.Next;

            if (!IsValidIsDefinedInstruction(isDefinedInstruction))
            {
                return;
            }
            var  number = (long)loadNumberInstruction.Operand;
            bool answer;

            switch (enumBaseTypeName)
            {
            case "Byte":
                answer = enumTypeDefinition.Fields.Skip(1).Any(x => (byte)x.Constant == number);
                break;

            case "SByte":
                answer = enumTypeDefinition.Fields.Skip(1).Any(x => (sbyte)x.Constant == number);
                break;

            case "Int16":
                answer = enumTypeDefinition.Fields.Skip(1).Any(x => (short)x.Constant == number);
                break;

            case "UInt16":
                answer = enumTypeDefinition.Fields.Skip(1).Any(x => (ushort)x.Constant == number);
                break;

            case "Int32":
                answer = enumTypeDefinition.Fields.Skip(1).Any(x => (int)x.Constant == number);
                break;

            case "UInt32":
                answer = enumTypeDefinition.Fields.Skip(1).Any(x => (uint)x.Constant == number);
                break;

            default: throw new ArgumentException(enumBaseTypeName);
            }
            processor
            .Remove(loadTokenInstruction)
            .Remove(getTypeFromHandleInstruction)
            .Remove(loadNumberInstruction)
            .Remove(convInstruction)
            .Remove(boxInstruction)
            .Replace(isDefinedInstruction, InstructionUtility.LoadConstant(answer));
        }