Пример #1
0
        protected virtual void ScanMethodBody(QueuedMethod aMethod, EcmaCil.MethodMeta aMethodMeta)
        {
            if (aMethod.Method.HasBody)
            {
                var xBody     = aMethod.Method.Body;
                var xBodyMeta = aMethodMeta.Body = new EcmaCil.MethodBodyMeta();
                xBodyMeta.InitLocals = xBody.InitLocals;
                #region handle exception handling clauses
                if (xBody.HasExceptionHandlers)
                {
                    throw new Exception("ExceptionHandlers are not supported yet");
                }
                #endregion
                #region handle locals
                xBodyMeta.LocalVariables = new EcmaCil.LocalVariableMeta[xBody.Variables.Count];
                for (int i = 0; i < xBody.Variables.Count; i++)
                {
                    var xVar     = xBody.Variables[i];
                    var xVarMeta = xBodyMeta.LocalVariables[i] = new EcmaCil.LocalVariableMeta();
                    xVarMeta.LocalType = EnqueueType(xVar.VariableType, aMethod, "Local variable");
#if DEBUG
                    xVarMeta.Data[EcmaCil.DataIds.DebugMetaId] = xVar.Name + ":" + xVar.VariableType.ToString();
#endif
                }
                #endregion

                xBodyMeta.Instructions = new EcmaCil.IL.BaseInstruction[xBody.Instructions.Count];
                var xILOffsetToInstructionOffset = new Dictionary <int, int>();
                var xInstructionOffsetToILOffset = new Dictionary <int, int>();
                var xSecondStageInits            = new List <Action <EcmaCil.MethodMeta> >();
                for (int i = 0; i < xBody.Instructions.Count; i++)
                {
                    xILOffsetToInstructionOffset.Add(xBody.Instructions[i].Offset, i);
                    xInstructionOffsetToILOffset.Add(i, xBody.Instructions[i].Offset);
                }

                for (int i = 0; i < xBody.Instructions.Count; i++)
                {
                    var xInstr = xBody.Instructions[i];
                    xBodyMeta.Instructions[i] =
                        CreateInstructionMeta(aMethod, aMethodMeta, xInstr, xILOffsetToInstructionOffset, xInstructionOffsetToILOffset, i, xSecondStageInits, i + 1);
                }

                if (xSecondStageInits.Count > 0)
                {
                    foreach (var xInit in xSecondStageInits)
                    {
                        xInit(aMethodMeta);
                    }
                }
            }
        }
Пример #2
0
 protected override void ScanMethodBody(QueuedMethod aMethod, EcmaCil.MethodMeta aMethodMeta)
 {
     base.ScanMethodBody(aMethod, aMethodMeta);
 }
Пример #3
0
        private BaseInstruction CreateInstructionMeta(QueuedMethod aMethod, EcmaCil.MethodMeta aMethodMeta, Instruction curInstruction, IDictionary <int, int> aILToInstructionOffset, IDictionary <int, int> aInstructionToILOffset, int aCurrentIndex, IList <Action <EcmaCil.MethodMeta> > aSecondStageInits, int aNextIndex)
        {
            switch (curInstruction.OpCode.Code)
            {
            case Code.Nop:
                return(new InstructionNone(InstructionKindEnum.Nop, aCurrentIndex));

            case Code.Pop:
                return(new InstructionNone(InstructionKindEnum.Pop, aCurrentIndex));

            case Code.Ldc_I4_S:
                return(new InstructionInt32(GetInstructionKind(curInstruction.OpCode.Code), aCurrentIndex, (int)(sbyte)curInstruction.Operand));

            case Code.Stloc_0:
                return(new InstructionInt32(InstructionKindEnum.Stloc, aCurrentIndex, 0));

            case Code.Stloc_1:
                return(new InstructionInt32(InstructionKindEnum.Stloc, aCurrentIndex, 1));

            case Code.Ldloc_0:
                return(new InstructionLocal(InstructionKindEnum.Ldloc, aCurrentIndex, aMethodMeta.Body.LocalVariables[0]));

            case Code.Ldloc_1:
                return(new InstructionLocal(InstructionKindEnum.Ldloc, aCurrentIndex, aMethodMeta.Body.LocalVariables[1]));

            case Code.Ldloc_2:
                return(new InstructionLocal(InstructionKindEnum.Ldloc, aCurrentIndex, aMethodMeta.Body.LocalVariables[2]));

            case Code.Call:
                return(new InstructionMethod(InstructionKindEnum.Call, aCurrentIndex, EnqueueMethod(curInstruction.Operand as MethodReference, aMethod.Method, "Operand value")));

            case Code.Callvirt:
                return(new InstructionMethod(InstructionKindEnum.Callvirt, aCurrentIndex, EnqueueMethod(curInstruction.Operand as MethodReference, aMethod.Method, "Operand value")));

            case Code.Stloc_2:
                return(new InstructionLocal(InstructionKindEnum.Stloc, aCurrentIndex, aMethodMeta.Body.LocalVariables[2]));

            case Code.Stloc_3:
                return(new InstructionLocal(InstructionKindEnum.Stloc, aCurrentIndex, aMethodMeta.Body.LocalVariables[3]));

            case Code.Ret:
                return(new InstructionNone(InstructionKindEnum.Ret, aCurrentIndex));

            case Code.Ldarg_0:
                return(new InstructionArgument(InstructionKindEnum.Ldarg, aCurrentIndex, aMethodMeta.Parameters[0]));

            case Code.Ldarg_1:
                return(new InstructionArgument(InstructionKindEnum.Ldarg, aCurrentIndex, aMethodMeta.Parameters[1]));

            case Code.Add:
                return(new InstructionNone(InstructionKindEnum.Add, aCurrentIndex));

            case Code.Ldstr:
                return(new InstructionString(InstructionKindEnum.Ldstr, aCurrentIndex, (string)curInstruction.Operand));

            case Code.Newobj:
                return(new InstructionMethod(InstructionKindEnum.Newobj, aCurrentIndex, EnqueueMethod(curInstruction.Operand as MethodReference, aMethod.Method, "Operand value")));

            case Code.Br_S:
            case Code.Brfalse_S:
                var xInstr        = new InstructionBranch(GetInstructionKind(curInstruction.OpCode.Code), aCurrentIndex);
                var xTargetInstr  = (Instruction)curInstruction.Operand;
                var xTargetOffset = xTargetInstr.Offset;
                aSecondStageInits.Add(delegate(EcmaCil.MethodMeta aTheMethod)
                {
                    xInstr.Target = aTheMethod.Body.Instructions[aILToInstructionOffset[xTargetOffset]];
                });
                return(xInstr);

            //    case
            //case OperandType.InlineNone:
            //    {
            //        xInstrMeta = new InstructionNone(GetInstructionKind(xInstr.OpCode.Code), i + xOffset);
            //        break;
            //    }
            //case OperandType.ShortInlineI:
            //    {
            //        xInstrMeta = new InstructionInt32(GetInstructionKind(xInstr.OpCode.Code), i + xOffset, (int)(sbyte)xInstr.Operand);
            //        break;
            //    }
            default: throw new Exception("Op '" + curInstruction.OpCode.Code + "' not implemented!");
            }
        }
Пример #4
0
        private void ScanMethod(QueuedMethod aMethod, EcmaCil.MethodMeta aMethodMeta)
        {
            // todo: add support for plugs
#if DEBUG
            aMethodMeta.Data[EcmaCil.DataIds.DebugMetaId] = aMethod.Method.ToString();
#endif
            var xParamOffset = 0;
            if (!aMethod.Method.IsStatic)
            {
                xParamOffset = 1;
            }
            aMethodMeta.Parameters = new EcmaCil.MethodParameterMeta[aMethod.Method.Parameters.Count + xParamOffset];
            if (!aMethod.Method.IsStatic)
            {
                aMethodMeta.Parameters[0] = new EcmaCil.MethodParameterMeta
                {
                    IsByRef      = aMethod.Method.DeclaringType.IsValueType,
                    PropertyType = EnqueueType(aMethod.Method.DeclaringType, aMethod, "Declaring type")
                };
#if DEBUG
                aMethodMeta.Parameters[0].Data[EcmaCil.DataIds.DebugMetaId] = "$this";
#endif
            }
            for (int i = 0; i < aMethod.Method.Parameters.Count; i++)
            {
                var xParam     = aMethod.Method.Parameters[i];
                var xParamType = xParam.ParameterType;
                if (xParamType is GenericParameter)
                {
                    // todo: resolve generics.
                    throw new NotImplementedException();
                }

                var xParamMeta = aMethodMeta.Parameters[i + xParamOffset] = new EcmaCil.MethodParameterMeta();
                //if (xParamType is ReferenceType)
                //{
                //    xParamType = ((ReferenceType)xParamType).ElementType;
                //    xParamMeta.IsByRef = true;
                //}

                var xType = EnqueueType(xParamType, aMethod, "parameter");
#if DEBUG
                var xSB = new StringBuilder();
                xSB.Append(xParam.Name);
                xSB.Append(": ");
                if (xParamMeta.IsByRef)
                {
                    xSB.Append("ref ");
                }
                xSB.Append(xParamType.ToString());
                xParamMeta.Data[EcmaCil.DataIds.DebugMetaId] = xSB.ToString();
#endif
                xParamMeta.PropertyType = xType;
            }
            if (aMethod.Method.ReturnType.FullName != "System.Void")
            {
                aMethodMeta.ReturnType = EnqueueType(aMethod.Method.ReturnType, aMethod.Method, "Return Type");
            }
            aMethodMeta.IsStatic = aMethod.Method.IsStatic;
            ScanMethodBody(aMethod, aMethodMeta);
            #region Virtuals scan
            if (aMethod.Method.IsVirtual)
            {
                if (aMethod.Method.HasGenericParameters)
                {
                    throw new Exception("GEnerics not yet fully supported");
                }
                // For virtuals we need to climb up the type tree
                // and find the top base method. We then add that top
                // node to the mVirtuals list. We don't need to add the
                // types becuase adding DeclaringType will already cause
                // all ancestor types to be added.

                var           xVirtMethod = aMethod.Method;
                TypeReference xVirtType   = aMethod.Method.DeclaringType;

                xVirtMethod = xVirtMethod.GetOriginalBaseMethod();
                #region old code
                //                MethodReference xNewVirtMethod = null;
//                while (true)
//                {
//                    var xNewVirtType = xVirtType.Resolve();
//                    if (xNewVirtType.HasGenericParameters)
//                    {
//                        throw new Exception("Generics not fully supported yet!");
//                    }
//                    if (xNewVirtType == null)
//                    {
//                        xVirtType = null;
//                    }
//                    else
//                    {
//#warning // todo: verify if next code works ok with generics
//                        var xTempNewVirtMethod = xNewVirtType.  .m.Methods..GetMethod(aMethod.Method.Name, aMethod.Method.Parameters);
//                        if (xTempNewVirtMethod !=null)
//                        {
//                            if (xTempNewVirtMethod.IsVirtual)
//                            {
//                                xNewVirtMethod = xTempNewVirtMethod;
//                            }
//                        }
//                        else
//                        {
//                            xNewVirtMethod = null;
//                        }
//                    }
//                    if (xNewVirtMethod == null)
//                    {
//                        if (mVirtuals.ContainsKey(aMethod))
//                        {
//                            xVirtMethod = null;
//                        }
//                        break;
//                    }
//                    xVirtMethod = xNewVirtMethod.Resolve();
//                    xVirtType = xNewVirtType.BaseType;
//                    if (xVirtType == null)
//                    {
//                        break;
//                    }
                //                }
                #endregion old code
                if (xVirtMethod != null)
                {
                    EnqueueMethod(xVirtMethod, aMethod, "Virtual Base");

                    foreach (var xType in mTypes)
                    {
                        if (xType.Key.Type.IsSubclassOf(xVirtMethod.DeclaringType))
                        {
                            //xType.Key.Type.res
                            //var xNewMethod = xType.Key.Type.Methods.GetMethod(aMethod.Method.Name, aMethod.Method.Parameters);
                            //if (xNewMethod != null)
                            //{
                            //                        // We need to check IsVirtual, a non virtual could
                            //                        // "replace" a virtual above it?
                            //    // MtW: correct
                            //    if (xNewMethod.IsVirtual)
                            //    {
                            //        EnqueueMethod(xNewMethod, aMethod, "Virtual Downscan");
                            //    }
                            //}
                            throw new NotImplementedException();
                        }
                    }
                }
            }
            #endregion
        }
Пример #5
0
        private EcmaCil.MethodMeta EnqueueMethod(MethodReference aMethod, object aSource, string aSourceType)
        {
            if (mLogEnabled)
            {
                LogMapPoint(aSource, aSourceType, aMethod);
            }
            List <TypeReference> xTypeArgs = null;
            var              xGenSpec      = aMethod as GenericInstanceMethod;
            TypeDefinition   xTypeDef;
            MethodDefinition xMethodDef;
            TypeReference    xRefType;

            if (xGenSpec != null)
            {
                xMethodDef = ResolveMethod(xGenSpec.ElementMethod);
                xRefType   = xGenSpec.DeclaringType;
                xTypeArgs  = new List <TypeReference>();
                foreach (TypeReference xArg in xGenSpec.GenericArguments)
                {
                    xTypeArgs.Add(xArg);
                }
            }
            else
            {
                xMethodDef = ResolveMethod(aMethod);
                xRefType   = aMethod.DeclaringType;
            }
            #region resolve type
            xTypeDef = xRefType as TypeDefinition;
            if (xTypeDef == null)
            {
                var xGenType = xRefType as GenericInstanceType;
                if (xGenType != null)
                {
                    xTypeDef = ResolveType(xGenType.DeclaringType);
                    if (xTypeArgs == null)
                    {
                        xTypeArgs = new List <TypeReference>();
                    }
                    for (int i = 0; i < xGenType.GenericArguments.Count; i++)
                    {
                        xTypeArgs.Insert(i, xGenType.GenericArguments[i]);
                    }
                }
                else
                {
                    xTypeDef = ResolveType(xRefType);
                }
            }
            #endregion
            var xQueuedMethod = new QueuedMethod(xTypeDef, xMethodDef, (xTypeArgs == null ? null : xTypeArgs.ToArray()));
            EcmaCil.MethodMeta xMethodMeta;
            if (mMethods.TryGetValue(xQueuedMethod, out xMethodMeta))
            {
                return(xMethodMeta);
            }
            var xDeclaringType = EnqueueType(xRefType, aMethod, "Declaring type");
            xMethodMeta = new EcmaCil.MethodMeta();
            xMethodMeta.DeclaringType = xDeclaringType;
            xDeclaringType.Methods.Add(xMethodMeta);
            mMethods.Add(xQueuedMethod, xMethodMeta);
            mQueue.Enqueue(xQueuedMethod);
            return(xMethodMeta);
        }