private static bool CheckReassignmentsOfParameters(int i, int i1, CallMethodStatic firstCallMethodStatic,
                                                           List <LocalOperation> localOperations)
        {
            var parametersFirst = new HashSet <LocalVariable>();

            foreach (var identifierValue in firstCallMethodStatic.Parameters)
            {
                var localVar = identifierValue as LocalVariable;
                if (localVar != null)
                {
                    parametersFirst.Add(localVar);
                }
            }

            for (var pos = i; pos <= i1; pos++)
            {
                var op         = localOperations[pos];
                var definition = op.GetDefinition();
                if (definition == null)
                {
                    continue;
                }
                if (parametersFirst.Contains(definition))
                {
                    return(false);
                }
            }
            return(true);
        }
Exemple #2
0
        public void NewObject(MethodBase constructorInfo)
        {
            if (constructorInfo.DeclaringType == typeof(object))
            {
                return;
            }

            constructorInfo.Register();
            var result = SetNewVReg();

            result.FixedType = new TypeDescription(constructorInfo.DeclaringType);
            var constructedObject = new NewConstructedObject
            {
                AssignedTo = result,
                Info       = constructorInfo
            };

            AddOperation(constructedObject);

            var interpreter = new CilMethodInterpreter(constructedObject.Info);
            var methodData  = new CallMethodStatic(interpreter);

            CallMethodData(constructedObject.Info, methodData);
            var vreg = SetNewVReg();

            vreg.FixedType = new TypeDescription(methodData.Info.DeclaringType);
            var assign = new Assignment
            {
                AssignedTo = vreg,
                Right      = methodData.Parameters.First()
            };

            AddOperation(assign);
        }
        private static bool ValidateParametersAreTheSame(CallMethodStatic firstCallMethodStatic, CallMethodStatic secondCallMethodStatic)
        {
            var parametersFirst = new List <IdentifierValue>();

            foreach (var identifierValue in firstCallMethodStatic.Parameters)
            {
                parametersFirst.Add(identifierValue);
            }
            var parametersSecond = new List <IdentifierValue>();

            foreach (var identifierValue in secondCallMethodStatic.Parameters)
            {
                parametersSecond.Add(identifierValue);
            }
            for (var index = 0; index < parametersFirst.Count; index++)
            {
                var id  = parametersFirst[index];
                var id2 = parametersSecond[index];
                if (!id.Equals(id2))
                {
                    return(false);
                }
            }
            return(true);
        }
        public static void InlineMethod(
            MetaMidRepresentation intermediateCode,
            CallMethodStatic callMethodStatic,
            int pos)
        {
            var methodToInlineInterpreter = (CilMethodInterpreter)callMethodStatic.Interpreter;
            var mappedParameters          = BuildMappedParameters(methodToInlineInterpreter,
                                                                  callMethodStatic);

            var mappedLocals = BuildMappedLocals(methodToInlineInterpreter,
                                                 intermediateCode.Vars.LocalVars.Count);

            var mappedVregs = BuildMappedVregs(intermediateCode, methodToInlineInterpreter);

            var indexCall  = pos;
            var assignment = (CallMethodStatic)intermediateCode.LocalOperations[indexCall];

            var localOperationsToInline = BuildLocalOperationsToInline(methodToInlineInterpreter,
                                                                       mappedParameters,
                                                                       assignment != null? assignment.Result:null);

            MergeVRegs(intermediateCode, methodToInlineInterpreter, mappedVregs);
            MergeLocalVariables(intermediateCode, methodToInlineInterpreter, mappedLocals);

            intermediateCode.LocalOperations.RemoveAt(indexCall);
            intermediateCode.LocalOperations.InsertRange(indexCall, localOperationsToInline);
        }
        private static bool TryMergeCalls(int i, int i1, CallMethodStatic firstCallMethodStatic, CallMethodStatic secondCallMethodStatic,
                                          List <LocalOperation> localOperations)
        {
            var validateParametersAreTheSame = ValidateParametersAreTheSame(firstCallMethodStatic, secondCallMethodStatic);

            if (!validateParametersAreTheSame)
            {
                return(false);
            }
            return(CheckReassignmentsOfParameters(i, i1, firstCallMethodStatic, localOperations));
        }
Exemple #6
0
        public bool UpdateTable(CallMethodStatic callMethodStatic)
        {
            var interpreter = callMethodStatic.Interpreter;
            var arguments   = interpreter.AnalyzeProperties.Arguments.ToArray();
            var result      = false;
            var parameters  = callMethodStatic.Parameters;

            for (var index = 0; index < parameters.Count; index++)
            {
                var argumentVar = arguments[index];
                var param       = parameters[index];
                var constValue  = param as ConstValue;
                if (constValue == null)
                {
                    ConstValueKind constKind;
                    result = !ConstKinds.TryGetValue(argumentVar, out constKind) ||
                             constKind == ConstValueKind.NonConstant;
                    if (!result)
                    {
                        continue;
                    }
                    ConstKinds[argumentVar] = ConstValueKind.NonConstant;
                    continue;
                }
                ConstValue storedConst;
                if (!ConstValues.TryGetValue(argumentVar, out storedConst))
                {
                    ConstValues[argumentVar] = constValue;
                    ConstValueKind constKind;
                    result = !ConstKinds.TryGetValue(argumentVar, out constKind) ||
                             constKind == ConstValueKind.AssignedConstant;
                    if (!result)
                    {
                        continue;
                    }
                    ConstKinds[argumentVar] = ConstValueKind.AssignedConstant;
                    continue;
                }
                if (!storedConst.ValueEquals(constValue))
                {
                    ConstValueKind constKind;
                    result = !ConstKinds.TryGetValue(argumentVar, out constKind) ||
                             constKind == ConstValueKind.NonConstant;
                    if (!result)
                    {
                        continue;
                    }
                    ConstKinds[argumentVar] = ConstValueKind.NonConstant;
                }
            }
            return(result);
        }
Exemple #7
0
        static Dictionary <LocalVariable, IdentifierValue> BuildMappedParameters(
            CilMethodInterpreter interpreter,
            CallMethodStatic callMethodStatic)
        {
            var mappedNames = new Dictionary <LocalVariable, IdentifierValue>();

            for (var i = 0; i < callMethodStatic.Parameters.Count; i++)
            {
                var identifierValue  = callMethodStatic.Parameters[i];
                var argumentVariable = interpreter.AnalyzeProperties.Arguments[i];
                argumentVariable.Id           = i;
                mappedNames[argumentVariable] = identifierValue;
            }
            return(mappedNames);
        }
Exemple #8
0
        private void CallMethodData(MethodBase methodInfo, CallMethodStatic callMethodStatic)
        {
            if (HandleRuntimeHelpersMethod(methodInfo))
            {
                callMethodStatic.ExtractNeededValuesFromStack(_evaluator);
                AddOperation(callMethodStatic);
                return;
            }
            if (methodInfo.IsConstructor && methodInfo.DeclaringType == typeof(object))
            {
                return;
            }
            callMethodStatic.ExtractNeededValuesFromStack(_evaluator);


            if (!callMethodStatic.Info.IsStatic && callMethodStatic.Parameters.Count > 0)
            {
                //GetBestVirtualMatch not required as the appropriate method is called in CppHandleCalls and VirtualMethod
                //TODO: GetBestVirtualMatch does not work  with base class constructors
                // if(!methodInfo.IsConstructor && CallMethodStatic.Parameters[0].ComputedType().ClrType.DeclaringType!=methodInfo.DeclaringType)
                //  CallMethodStatic.Info = ClassHierarchyAnalysis.GetBestVirtualMatch(CallMethodStatic.Info,
                //      CallMethodStatic.Parameters[0].ComputedType().ClrType);
            }
            var declaringType = callMethodStatic.Info.DeclaringType;

            if (declaringType.IsSubclassOf(typeof(Delegate)))
            {
                var signature = declaringType.GetMethod("Invoke");
                DelegateManager.RegisterType(declaringType, signature);
            }

            if (!callMethodStatic.IsVoid)
            {
                var vreg = SetNewVReg();
                vreg.FixedType          = new TypeDescription(methodInfo.GetReturnType());
                callMethodStatic.Result = vreg;
            }
            if (callMethodStatic.Result != null)
            {
                callMethodStatic.Result.FixedType = new TypeDescription(methodInfo.GetReturnType());
            }
            AddOperation(callMethodStatic);
        }
        static bool CheckIfParamAreConst(CallMethodStatic operationStatic, List <object> constParams)
        {
            var paramsAreConst = true;

            foreach (var parameter in operationStatic.Parameters)
            {
                var constParam = parameter as ConstValue;
                if (constParam != null)
                {
                    constParams.Add(constParam.Value);
                }
                else
                {
                    paramsAreConst = false;
                    break;
                }
            }
            return(paramsAreConst);
        }
        public override void OptimizeOperations(CilMethodInterpreter methodInterpreter)
        {
            if (methodInterpreter.MidRepresentation.LocalOperations.Count > MaxLengthInliner)
            {
                return;
            }
            MethodInterpreter interpreter      = null;
            CallMethodStatic  callMethodStatic = null;
            var pos = 0;

            foreach (var localOperation in methodInterpreter.MidRepresentation.LocalOperations)
            {
                pos++;
                if (localOperation.Kind != OperationKind.Call)
                {
                    continue;
                }

                callMethodStatic = (CallMethodStatic)localOperation;
                var methodBase = callMethodStatic.Info;
                interpreter = methodBase.Register();
                if (interpreter == null)
                {
                    continue;
                }
                if (methodInterpreter.MidRepresentation.GetMethodBody == null)
                {
                    continue;
                }
                if (methodInterpreter.MidRepresentation.LocalOperations.Count > MaxLengthChildFunction)
                {
                    continue;
                }
                break;
            }
            if (interpreter == null)
            {
                return;
            }
            InlineMethod(methodInterpreter.MidRepresentation, callMethodStatic, pos);
            Result = true;
        }
Exemple #11
0
        public void Call(MethodBase operand)
        {
            var methodInfo = operand;

            //TODO System.Void System.Array::Resize<System.Char>(!!0[]&,System.Int32) Weird Signature

            if (methodInfo == null)
            {
                return;
            }
            MethodInterpreter interpreter;

            if (PlatformInvokeMethod.IsPlatformInvoke(methodInfo))
            {
                interpreter = new PlatformInvokeMethod(methodInfo);
                var pMethodData = new CallMethodStatic(interpreter);
                CallMethodData(methodInfo, pMethodData);
                return;
            }
            interpreter = new CilMethodInterpreter(methodInfo);
            var methodData = new CallMethodStatic(interpreter);

            CallMethodData(methodInfo, methodData);
        }
        public static void WriteParametersToSb(CallMethodStatic operationStatic, StringBuilder sb,
                                               MethodInterpreter interpreter)
        {
            var fullEscapeData = operationStatic.Interpreter.BuildEscapeModes();
            var parameters     = operationStatic.Parameters;
            var parametersData = new List <EscapingMode>();

            for (int index = 0; index < parameters.Count; index++)
            {
                var identifierValue = parameters[index];
                if (identifierValue is LocalVariable)
                {
                    var callingParameterData =
                        interpreter.AnalyzeProperties.GetVariableData((LocalVariable)identifierValue);
                    parametersData.Add(callingParameterData);
                }
                else
                {
                    parametersData.Add(EscapingMode.Smart);
                }
            }

            BuildCallString(sb, parameters, fullEscapeData, parametersData.ToArray());
        }
Exemple #13
0
 public static MethodInterpreter GetInterpreter(this CallMethodStatic callMethodStatic, ClosureEntities crRuntime)
 {
     return(crRuntime.ResolveMethod(callMethodStatic.Info));
 }