Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="localDefinition"></param>
        /// <param name="isCompilerGenerated"></param>
        /// <returns></returns>
        public string GetSourceNameFor(ILocalDefinition localDefinition, out bool isCompilerGenerated)
        {
            isCompilerGenerated = true;
            PdbFunction /*?*/ pdbFunction = this.GetPdbFunctionFor(localDefinition);

            if (pdbFunction != null)
            {
                uint index = 0;
                foreach (ILocation location in localDefinition.Locations)
                {
                    MethodBodyLocation /*?*/ mbLocation = location as MethodBodyLocation;
                    if (mbLocation != null)
                    {
                        index = mbLocation.Offset;
                        break;
                    }
                }
                PdbSlot /*?*/ slot = this.GetSlotFor(pdbFunction.scopes, index);
                if (slot != null)
                {
                    isCompilerGenerated = (slot.flags & 0x4) != 0;
                    return(slot.name);
                }
            }
            return(localDefinition.Name.Value);
        }
Example #2
0
        private ITypeReference GetTypeForConstant()
        {
            foreach (ILocation location in this.methodDefinition.Locations)
            {
                MethodBodyLocation /*?*/ mbLocation = location as MethodBodyLocation;
                if (mbLocation != null)
                {
                    ITypeReference result = mbLocation.Document.GetTypeFromToken(this.pdbConstant.token);
                    if (result == Dummy.TypeReference)
                    {
                        //TODO: error
                        continue;
                    }
                    return(result);
                }
            }
            IPlatformType platformType = this.methodDefinition.Type.PlatformType;
            IConvertible  ic           = this.pdbConstant.value as IConvertible;

            if (ic == null)
            {
                return(platformType.SystemObject);
            }
            switch (ic.GetTypeCode())
            {
            case TypeCode.Boolean: return(platformType.SystemBoolean);

            case TypeCode.Byte: return(platformType.SystemUInt8);

            case TypeCode.Char: return(platformType.SystemChar);

            case TypeCode.Decimal: return(platformType.SystemDecimal);

            case TypeCode.Double: return(platformType.SystemFloat64);

            case TypeCode.Int16: return(platformType.SystemInt16);

            case TypeCode.Int32: return(platformType.SystemInt32);

            case TypeCode.Int64: return(platformType.SystemInt64);

            case TypeCode.SByte: return(platformType.SystemInt8);

            case TypeCode.Single: return(platformType.SystemFloat64);

            case TypeCode.String: return(platformType.SystemString);

            case TypeCode.UInt16: return(platformType.SystemUInt16);

            case TypeCode.UInt32: return(platformType.SystemUInt32);

            case TypeCode.UInt64: return(platformType.SystemUInt64);

            default: return(platformType.SystemObject);
            }
        }
Example #3
0
 internal CilInstruction(
     OperationCode cilOpCode,
     MethodBodyLocation location,
     object /*?*/ value
     )
 {
     this.CilOpCode = cilOpCode;
     this.Location  = location;
     this.Value     = value;
 }
Example #4
0
 private static uint GetTokenFor(IMethodBody methodBody)
 {
     foreach (ILocation location in methodBody.MethodDefinition.Locations)
     {
         MethodBodyLocation /*?*/ mbLocation = location as MethodBodyLocation;
         if (mbLocation != null)
         {
             return(mbLocation.Document.MethodToken);
         }
     }
     return(0);
 }
Example #5
0
        private IPrimarySourceLocation /*?*/ MapMethodBodyLocationToSourceLocation(MethodBodyLocation mbLocation, bool exact)
        {
            PdbFunction /*?*/ pdbFunction;

            if (!this.pdbFunctionMap.TryGetValue(mbLocation.Document.MethodToken, out pdbFunction))
            {
                return(null);
            }
            if (pdbFunction.lines == null)
            {
                return(null);
            }
            foreach (PdbLines pdbLines in pdbFunction.lines)
            {
                PdbSource pdbSourceFile = pdbLines.file;
                if (pdbSourceFile == null)
                {
                    return(null);
                }

                PdbLine[] array    = pdbLines.lines;
                int       minIndex = 0;
                int       maxIndex = array.Length - 1;

                uint desiredOffset = mbLocation.Offset;

                while (minIndex <= maxIndex)
                {
                    int     midPointIndex = (minIndex + maxIndex) >> 1;
                    PdbLine mid           = array[midPointIndex];
                    if (midPointIndex == maxIndex ||
                        (mid.offset <= desiredOffset && desiredOffset < array[midPointIndex + 1].offset))
                    {
                        if (exact && desiredOffset != mid.offset)
                        {
                            return(null);
                        }
                        PdbLine           line  = mid;
                        PdbSourceDocument psDoc = this.GetPrimarySourceDocumentFor(pdbSourceFile);
                        return(new PdbSourceLineLocation(psDoc, (int)line.lineBegin, line.colBegin, (int)line.lineEnd, line.colEnd));
                    }
                    if (mid.offset < desiredOffset)
                    {
                        minIndex = midPointIndex + 1;
                    }
                    else
                    {
                        maxIndex = midPointIndex - 1;
                    }
                }
            }
            return(null);
        }
Example #6
0
 /// <summary>
 /// Return zero or more locations in primary source documents that correspond to one or more of the given derived (non primary) document locations.
 /// </summary>
 /// <param name="locations">Zero or more locations in documents that have been derived from one or more source documents.</param>
 public IEnumerable <IPrimarySourceLocation> GetPrimarySourceLocationsFor(IEnumerable <ILocation> locations)
 {
     foreach (ILocation location in locations)
     {
         MethodBodyLocation /*?*/ mbLocation = location as MethodBodyLocation;
         if (mbLocation != null)
         {
             IPrimarySourceLocation /*?*/ psloc = this.MapMethodBodyLocationToSourceLocation(mbLocation);
             if (psloc != null)
             {
                 yield return(psloc);
             }
         }
     }
 }
Example #7
0
        private PdbFunction GetPdbFunctionFor(ILocalDefinition localDefinition)
        {
            PdbFunction /*?*/ result = null;

            foreach (ILocation location in localDefinition.Locations)
            {
                MethodBodyLocation /*?*/ mbLocation = location as MethodBodyLocation;
                if (mbLocation != null)
                {
                    this.pdbFunctionMap.TryGetValue(mbLocation.Document.MethodToken, out result);
                    break;
                }
            }
            return(result);
        }
Example #8
0
        /// <summary>
        /// Return zero or more locations in primary source documents that correspond to the given derived (non primary) document location.
        /// </summary>
        /// <param name="location">A location in a document that have been derived from one or more source documents.</param>
        public IEnumerable <IPrimarySourceLocation> GetPrimarySourceLocationsFor(ILocation location)
        {
            var psloc = location as IPrimarySourceLocation;

            if (psloc != null)
            {
                yield return(psloc);
            }
            else
            {
                MethodBodyLocation /*?*/ mbLocation = location as MethodBodyLocation;
                if (mbLocation != null)
                {
                    psloc = this.MapMethodBodyLocationToSourceLocation(mbLocation);
                    if (psloc != null)
                    {
                        yield return(psloc);
                    }
                }
            }
        }
Example #9
0
        /// <summary>
        /// Return zero or more locations in primary source documents that correspond to the definition of the given local.
        /// </summary>
        public IEnumerable <IPrimarySourceLocation> GetPrimarySourceLocationsForDefinitionOf(ILocalDefinition localDefinition)
        {
            PdbFunction /*?*/ pdbFunction = this.GetPdbFunctionFor(localDefinition);

            if (pdbFunction != null)
            {
                uint index = 0;
                foreach (ILocation location in localDefinition.Locations)
                {
                    MethodBodyLocation /*?*/ mbLocation = location as MethodBodyLocation;
                    if (mbLocation != null)
                    {
                        index = mbLocation.Offset;
                        break;
                    }
                }
                PdbSlot /*?*/ slot = this.GetSlotFor(pdbFunction.scopes, index);
                if (slot != null && (slot.flags & 0x4) == 0)
                {
                    return(IteratorHelper.GetSingletonEnumerable <IPrimarySourceLocation>(new LocalNameSourceLocation(slot.name)));
                }
            }
            return(IteratorHelper.GetEmptyEnumerable <IPrimarySourceLocation>());
        }
Example #10
0
 bool PopulateCilInstructions()
 {
     MethodBodyDocument document = new MethodBodyDocument(this.MethodDefinition);
       MemoryReader memReader = new MemoryReader(this.MethodIL.EncodedILMemoryBlock);
       List<CilInstruction> instrList = new List<CilInstruction>();
       while (memReader.NotEndOfBytes) {
     object/*?*/ value = null;
     uint offset = (uint)memReader.Offset;
     OperationCode cilOpCode = memReader.ReadOpcode();
     switch (cilOpCode) {
       case OperationCode.Nop:
       case OperationCode.Break:
     break;
       case OperationCode.Ldarg_0:
       case OperationCode.Ldarg_1:
       case OperationCode.Ldarg_2:
       case OperationCode.Ldarg_3:
     value = this.GetParameter((uint)(cilOpCode - OperationCode.Ldarg_0));
     break;
       case OperationCode.Ldloc_0:
       case OperationCode.Ldloc_1:
       case OperationCode.Ldloc_2:
       case OperationCode.Ldloc_3:
     value = this.GetLocal((uint)(cilOpCode - OperationCode.Ldloc_0));
     break;
       case OperationCode.Stloc_0:
       case OperationCode.Stloc_1:
       case OperationCode.Stloc_2:
       case OperationCode.Stloc_3:
     value = this.GetLocal((uint)(cilOpCode - OperationCode.Stloc_0));
     break;
       case OperationCode.Ldarg_S:
       case OperationCode.Ldarga_S:
       case OperationCode.Starg_S:
     value = this.GetParameter(memReader.ReadByte());
     break;
       case OperationCode.Ldloc_S:
       case OperationCode.Ldloca_S:
       case OperationCode.Stloc_S:
     value = this.GetLocal(memReader.ReadByte());
     break;
       case OperationCode.Ldnull:
       case OperationCode.Ldc_I4_M1:
       case OperationCode.Ldc_I4_0:
       case OperationCode.Ldc_I4_1:
       case OperationCode.Ldc_I4_2:
       case OperationCode.Ldc_I4_3:
       case OperationCode.Ldc_I4_4:
       case OperationCode.Ldc_I4_5:
       case OperationCode.Ldc_I4_6:
       case OperationCode.Ldc_I4_7:
       case OperationCode.Ldc_I4_8:
     break;
       case OperationCode.Ldc_I4_S:
     value = (int)memReader.ReadSByte();
     break;
       case OperationCode.Ldc_I4:
     value = memReader.ReadInt32();
     break;
       case OperationCode.Ldc_I8:
     value = memReader.ReadInt64();
     break;
       case OperationCode.Ldc_R4:
     value = memReader.ReadSingle();
     break;
       case OperationCode.Ldc_R8:
     value = memReader.ReadDouble();
     break;
       case OperationCode.Dup:
       case OperationCode.Pop:
     break;
       case OperationCode.Jmp:
     value = this.GetMethod(memReader.ReadUInt32());
     break;
       case OperationCode.Call: {
       IMethodReference methodReference = this.GetMethod(memReader.ReadUInt32());
       IArrayTypeReference/*?*/ arrayType = methodReference.ContainingType as IArrayTypeReference;
       if (arrayType != null) {
         // For Get(), Set() and Address() on arrays, the runtime provides method implementations.
         // Hence, CCI2 replaces these with pseudo instrcutions Array_Set, Array_Get and Array_Addr.
         // All other methods on arrays will not use pseudo instruction and will have methodReference as their operand.
         if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.Set.UniqueKey) {
           cilOpCode = OperationCode.Array_Set;
           value = arrayType;
         } else if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.Get.UniqueKey) {
           cilOpCode = OperationCode.Array_Get;
           value = arrayType;
         } else if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.Address.UniqueKey) {
           cilOpCode = OperationCode.Array_Addr;
           value = arrayType;
         } else {
           value = methodReference;
         }
       } else {
         value = methodReference;
       }
     }
     break;
       case OperationCode.Calli:
     value = this.GetFunctionPointerType(memReader.ReadUInt32());
     break;
       case OperationCode.Ret:
     break;
       case OperationCode.Br_S:
       case OperationCode.Brfalse_S:
       case OperationCode.Brtrue_S:
       case OperationCode.Beq_S:
       case OperationCode.Bge_S:
       case OperationCode.Bgt_S:
       case OperationCode.Ble_S:
       case OperationCode.Blt_S:
       case OperationCode.Bne_Un_S:
       case OperationCode.Bge_Un_S:
       case OperationCode.Bgt_Un_S:
       case OperationCode.Ble_Un_S:
       case OperationCode.Blt_Un_S: {
       uint jumpOffset = (uint)(memReader.Offset + 1 + memReader.ReadSByte());
       if (jumpOffset >= this.EndOfMethodOffset) {
         //  Error...
       }
       value = jumpOffset;
     }
     break;
       case OperationCode.Br:
       case OperationCode.Brfalse:
       case OperationCode.Brtrue:
       case OperationCode.Beq:
       case OperationCode.Bge:
       case OperationCode.Bgt:
       case OperationCode.Ble:
       case OperationCode.Blt:
       case OperationCode.Bne_Un:
       case OperationCode.Bge_Un:
       case OperationCode.Bgt_Un:
       case OperationCode.Ble_Un:
       case OperationCode.Blt_Un: {
       uint jumpOffset = (uint)(memReader.Offset + 4 + memReader.ReadInt32());
       if (jumpOffset >= this.EndOfMethodOffset) {
         //  Error...
       }
       value = jumpOffset;
     }
     break;
       case OperationCode.Switch: {
       uint numTargets = memReader.ReadUInt32();
       uint[] result = new uint[numTargets];
       uint asOffset = memReader.Offset + numTargets * 4;
       for (int i = 0; i < numTargets; i++) {
         uint targetAddress = memReader.ReadUInt32() + asOffset;
         if (targetAddress >= this.EndOfMethodOffset) {
           //  Error...
         }
         result[i] = targetAddress;
       }
       value = result;
     }
     break;
       case OperationCode.Ldind_I1:
       case OperationCode.Ldind_U1:
       case OperationCode.Ldind_I2:
       case OperationCode.Ldind_U2:
       case OperationCode.Ldind_I4:
       case OperationCode.Ldind_U4:
       case OperationCode.Ldind_I8:
       case OperationCode.Ldind_I:
       case OperationCode.Ldind_R4:
       case OperationCode.Ldind_R8:
       case OperationCode.Ldind_Ref:
       case OperationCode.Stind_Ref:
       case OperationCode.Stind_I1:
       case OperationCode.Stind_I2:
       case OperationCode.Stind_I4:
       case OperationCode.Stind_I8:
       case OperationCode.Stind_R4:
       case OperationCode.Stind_R8:
       case OperationCode.Add:
       case OperationCode.Sub:
       case OperationCode.Mul:
       case OperationCode.Div:
       case OperationCode.Div_Un:
       case OperationCode.Rem:
       case OperationCode.Rem_Un:
       case OperationCode.And:
       case OperationCode.Or:
       case OperationCode.Xor:
       case OperationCode.Shl:
       case OperationCode.Shr:
       case OperationCode.Shr_Un:
       case OperationCode.Neg:
       case OperationCode.Not:
       case OperationCode.Conv_I1:
       case OperationCode.Conv_I2:
       case OperationCode.Conv_I4:
       case OperationCode.Conv_I8:
       case OperationCode.Conv_R4:
       case OperationCode.Conv_R8:
       case OperationCode.Conv_U4:
       case OperationCode.Conv_U8:
     break;
       case OperationCode.Callvirt: {
       IMethodReference methodReference = this.GetMethod(memReader.ReadUInt32());
       IArrayTypeReference/*?*/ arrayType = methodReference.ContainingType as IArrayTypeReference;
       if (arrayType != null) {
         // For Get(), Set() and Address() on arrays, the runtime provides method implementations.
         // Hence, CCI2 replaces these with pseudo instrcutions Array_Set, Array_Get and Array_Addr.
         // All other methods on arrays will not use pseudo instruction and will have methodReference as their operand.
         if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.Set.UniqueKey) {
           cilOpCode = OperationCode.Array_Set;
           value = arrayType;
         } else if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.Get.UniqueKey) {
           cilOpCode = OperationCode.Array_Get;
           value = arrayType;
         } else if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.Address.UniqueKey) {
           cilOpCode = OperationCode.Array_Addr;
           value = arrayType;
         } else {
           value = methodReference;
         }
       } else {
         value = methodReference;
       }
     }
     break;
       case OperationCode.Cpobj:
       case OperationCode.Ldobj:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Ldstr:
     value = this.GetUserStringForToken(memReader.ReadUInt32());
     break;
       case OperationCode.Newobj: {
       IMethodReference methodReference = this.GetMethod(memReader.ReadUInt32());
       IArrayTypeReference/*?*/ arrayType = methodReference.ContainingType as IArrayTypeReference;
       if (arrayType != null && !arrayType.IsVector) {
         uint numParam = IteratorHelper.EnumerableCount(methodReference.Parameters);
         if (numParam != arrayType.Rank)
           cilOpCode = OperationCode.Array_Create_WithLowerBound;
         else
           cilOpCode = OperationCode.Array_Create;
         value = arrayType;
       } else {
         value = methodReference;
       }
     }
     break;
       case OperationCode.Castclass:
       case OperationCode.Isinst:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Conv_R_Un:
     break;
       case OperationCode.Unbox:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Throw:
     break;
       case OperationCode.Ldfld:
       case OperationCode.Ldflda:
       case OperationCode.Stfld:
     value = this.GetField(memReader.ReadUInt32());
     break;
       case OperationCode.Ldsfld:
       case OperationCode.Ldsflda:
       case OperationCode.Stsfld:
     value = this.GetField(memReader.ReadUInt32());
     var fieldRef = value as FieldReference;
     if (fieldRef != null) fieldRef.isStatic = true;
     break;
       case OperationCode.Stobj:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Conv_Ovf_I1_Un:
       case OperationCode.Conv_Ovf_I2_Un:
       case OperationCode.Conv_Ovf_I4_Un:
       case OperationCode.Conv_Ovf_I8_Un:
       case OperationCode.Conv_Ovf_U1_Un:
       case OperationCode.Conv_Ovf_U2_Un:
       case OperationCode.Conv_Ovf_U4_Un:
       case OperationCode.Conv_Ovf_U8_Un:
       case OperationCode.Conv_Ovf_I_Un:
       case OperationCode.Conv_Ovf_U_Un:
     break;
       case OperationCode.Box:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Newarr: {
       ITypeReference elementType = this.GetType(memReader.ReadUInt32());
       IModuleTypeReference/*?*/ moduleTypeReference = elementType as IModuleTypeReference;
       if (moduleTypeReference != null)
         value = new VectorType(this.PEFileToObjectModel, 0xFFFFFFFF, moduleTypeReference);
       else
         value = Dummy.ArrayType;
     }
     break;
       case OperationCode.Ldlen:
     break;
       case OperationCode.Ldelema:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Ldelem_I1:
       case OperationCode.Ldelem_U1:
       case OperationCode.Ldelem_I2:
       case OperationCode.Ldelem_U2:
       case OperationCode.Ldelem_I4:
       case OperationCode.Ldelem_U4:
       case OperationCode.Ldelem_I8:
       case OperationCode.Ldelem_I:
       case OperationCode.Ldelem_R4:
       case OperationCode.Ldelem_R8:
       case OperationCode.Ldelem_Ref:
       case OperationCode.Stelem_I:
       case OperationCode.Stelem_I1:
       case OperationCode.Stelem_I2:
       case OperationCode.Stelem_I4:
       case OperationCode.Stelem_I8:
       case OperationCode.Stelem_R4:
       case OperationCode.Stelem_R8:
       case OperationCode.Stelem_Ref:
     break;
       case OperationCode.Ldelem:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Stelem:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Unbox_Any:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Conv_Ovf_I1:
       case OperationCode.Conv_Ovf_U1:
       case OperationCode.Conv_Ovf_I2:
       case OperationCode.Conv_Ovf_U2:
       case OperationCode.Conv_Ovf_I4:
       case OperationCode.Conv_Ovf_U4:
       case OperationCode.Conv_Ovf_I8:
       case OperationCode.Conv_Ovf_U8:
     break;
       case OperationCode.Refanyval:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Ckfinite:
     break;
       case OperationCode.Mkrefany:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Ldtoken:
     value = this.GetRuntimeHandleFromToken(memReader.ReadUInt32());
     break;
       case OperationCode.Conv_U2:
       case OperationCode.Conv_U1:
       case OperationCode.Conv_I:
       case OperationCode.Conv_Ovf_I:
       case OperationCode.Conv_Ovf_U:
       case OperationCode.Add_Ovf:
       case OperationCode.Add_Ovf_Un:
       case OperationCode.Mul_Ovf:
       case OperationCode.Mul_Ovf_Un:
       case OperationCode.Sub_Ovf:
       case OperationCode.Sub_Ovf_Un:
       case OperationCode.Endfinally:
     break;
       case OperationCode.Leave: {
       uint leaveOffset = (uint)(memReader.Offset + 4 + memReader.ReadInt32());
       if (leaveOffset >= this.EndOfMethodOffset) {
         //  Error...
       }
       value = leaveOffset;
     }
     break;
       case OperationCode.Leave_S: {
       uint leaveOffset = (uint)(memReader.Offset + 1 + memReader.ReadSByte());
       if (leaveOffset >= this.EndOfMethodOffset) {
         //  Error...
       }
       value = leaveOffset;
     }
     break;
       case OperationCode.Stind_I:
       case OperationCode.Conv_U:
       case OperationCode.Arglist:
       case OperationCode.Ceq:
       case OperationCode.Cgt:
       case OperationCode.Cgt_Un:
       case OperationCode.Clt:
       case OperationCode.Clt_Un:
     break;
       case OperationCode.Ldftn:
       case OperationCode.Ldvirtftn:
     value = this.GetMethod(memReader.ReadUInt32());
     break;
       case OperationCode.Ldarg:
       case OperationCode.Ldarga:
       case OperationCode.Starg:
     value = this.GetParameter(memReader.ReadUInt16());
     break;
       case OperationCode.Ldloc:
       case OperationCode.Ldloca:
       case OperationCode.Stloc:
     value = this.GetLocal(memReader.ReadUInt16());
     break;
       case OperationCode.Localloc:
     value = new PointerType(this.PEFileToObjectModel, 0xFFFFFFFF, this.PEFileToObjectModel.SystemVoid);
     break;
       case OperationCode.Endfilter:
     break;
       case OperationCode.Unaligned_:
     value = memReader.ReadByte();
     break;
       case OperationCode.Volatile_:
       case OperationCode.Tail_:
     break;
       case OperationCode.Initobj:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Constrained_:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Cpblk:
       case OperationCode.Initblk:
     break;
       case OperationCode.No_:
     value = (OperationCheckFlags)memReader.ReadByte();
     break;
       case OperationCode.Rethrow:
     break;
       case OperationCode.Sizeof:
     value = this.GetType(memReader.ReadUInt32());
     break;
       case OperationCode.Refanytype:
       case OperationCode.Readonly_:
     break;
       default:
     this.PEFileToObjectModel.PEFileReader.ErrorContainer.AddILError(this.MethodDefinition, offset, MetadataReaderErrorKind.UnknownILInstruction);
     break;
     }
     MethodBodyLocation location = new MethodBodyLocation(document, offset);
     instrList.Add(new CilInstruction(cilOpCode, location, value));
       }
       this.MethodBody.SetCilInstructions(new EnumerableArrayWrapper<CilInstruction, IOperation>(instrList.ToArray(), Dummy.Operation));
       return true;
 }
Example #11
0
     internal CilInstruction(
   OperationCode cilOpCode,
   MethodBodyLocation location,
   object/*?*/ value
 )
     {
         this.CilOpCode = cilOpCode;
           this.Location = location;
           this.Value = value;
     }
Example #12
0
        private bool PopulateCilInstructions()
        {
            MethodBodyDocument    document  = new MethodBodyDocument(this.MethodDefinition);
            MemoryReader          memReader = new MemoryReader(_methodIL.EncodedILMemoryBlock);
            List <CilInstruction> instrList = new List <CilInstruction>();

            while (memReader.NotEndOfBytes)
            {
                object /*?*/  value     = null;
                uint          offset    = (uint)memReader.Offset;
                OperationCode cilOpCode = memReader.ReadOpcode();
                switch (cilOpCode)
                {
                case OperationCode.Nop:
                case OperationCode.Break:
                    break;

                case OperationCode.Ldarg_0:
                case OperationCode.Ldarg_1:
                case OperationCode.Ldarg_2:
                case OperationCode.Ldarg_3:
                    value = this.GetParameter((uint)(cilOpCode - OperationCode.Ldarg_0));
                    break;

                case OperationCode.Ldloc_0:
                case OperationCode.Ldloc_1:
                case OperationCode.Ldloc_2:
                case OperationCode.Ldloc_3:
                    value = this.GetLocal((uint)(cilOpCode - OperationCode.Ldloc_0));
                    break;

                case OperationCode.Stloc_0:
                case OperationCode.Stloc_1:
                case OperationCode.Stloc_2:
                case OperationCode.Stloc_3:
                    value = this.GetLocal((uint)(cilOpCode - OperationCode.Stloc_0));
                    break;

                case OperationCode.Ldarg_S:
                case OperationCode.Ldarga_S:
                case OperationCode.Starg_S:
                    value = this.GetParameter(memReader.ReadByte());
                    break;

                case OperationCode.Ldloc_S:
                case OperationCode.Ldloca_S:
                case OperationCode.Stloc_S:
                    value = this.GetLocal(memReader.ReadByte());
                    break;

                case OperationCode.Ldnull:
                case OperationCode.Ldc_I4_M1:
                case OperationCode.Ldc_I4_0:
                case OperationCode.Ldc_I4_1:
                case OperationCode.Ldc_I4_2:
                case OperationCode.Ldc_I4_3:
                case OperationCode.Ldc_I4_4:
                case OperationCode.Ldc_I4_5:
                case OperationCode.Ldc_I4_6:
                case OperationCode.Ldc_I4_7:
                case OperationCode.Ldc_I4_8:
                    break;

                case OperationCode.Ldc_I4_S:
                    value = (int)memReader.ReadSByte();
                    break;

                case OperationCode.Ldc_I4:
                    value = memReader.ReadInt32();
                    break;

                case OperationCode.Ldc_I8:
                    value = memReader.ReadInt64();
                    break;

                case OperationCode.Ldc_R4:
                    value = memReader.ReadSingle();
                    break;

                case OperationCode.Ldc_R8:
                    value = memReader.ReadDouble();
                    break;

                case OperationCode.Dup:
                case OperationCode.Pop:
                    break;

                case OperationCode.Jmp:
                    value = this.GetMethod(memReader.ReadUInt32());
                    break;

                case OperationCode.Call:
                {
                    IMethodReference          methodReference = this.GetMethod(memReader.ReadUInt32());
                    IArrayTypeReference /*?*/ arrayType       = methodReference.ContainingType as IArrayTypeReference;
                    if (arrayType != null)
                    {
                        if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.GetNameFor("Set").UniqueKey)
                        {
                            cilOpCode = OperationCode.Array_Set;
                        }
                        else if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.Get.UniqueKey)
                        {
                            cilOpCode = OperationCode.Array_Get;
                        }
                        else if (methodReference.Name.UniqueKey == this.PEFileToObjectModel.NameTable.GetNameFor("Address").UniqueKey)
                        {
                            cilOpCode = OperationCode.Array_Addr;
                        }
                        value = arrayType;
                    }
                    else
                    {
                        value = methodReference;
                    }
                }
                break;

                case OperationCode.Calli:
                    value = this.GetFunctionPointerType(memReader.ReadUInt32());
                    break;

                case OperationCode.Ret:
                    break;

                case OperationCode.Br_S:
                case OperationCode.Brfalse_S:
                case OperationCode.Brtrue_S:
                case OperationCode.Beq_S:
                case OperationCode.Bge_S:
                case OperationCode.Bgt_S:
                case OperationCode.Ble_S:
                case OperationCode.Blt_S:
                case OperationCode.Bne_Un_S:
                case OperationCode.Bge_Un_S:
                case OperationCode.Bgt_Un_S:
                case OperationCode.Ble_Un_S:
                case OperationCode.Blt_Un_S:
                {
                    uint jumpOffset = (uint)(memReader.Offset + 1 + memReader.ReadSByte());
                    if (jumpOffset >= _endOfMethodOffset)
                    {
                        //  Error...
                    }
                    value = jumpOffset;
                }
                break;

                case OperationCode.Br:
                case OperationCode.Brfalse:
                case OperationCode.Brtrue:
                case OperationCode.Beq:
                case OperationCode.Bge:
                case OperationCode.Bgt:
                case OperationCode.Ble:
                case OperationCode.Blt:
                case OperationCode.Bne_Un:
                case OperationCode.Bge_Un:
                case OperationCode.Bgt_Un:
                case OperationCode.Ble_Un:
                case OperationCode.Blt_Un:
                {
                    uint jumpOffset = (uint)(memReader.Offset + 4 + memReader.ReadInt32());
                    if (jumpOffset >= _endOfMethodOffset)
                    {
                        //  Error...
                    }
                    value = jumpOffset;
                }
                break;

                case OperationCode.Switch:
                {
                    uint   numTargets = memReader.ReadUInt32();
                    uint[] result     = new uint[numTargets];
                    uint   asOffset   = memReader.Offset + numTargets * 4;
                    for (int i = 0; i < numTargets; i++)
                    {
                        uint targetAddress = memReader.ReadUInt32() + asOffset;
                        if (targetAddress >= _endOfMethodOffset)
                        {
                            //  Error...
                        }
                        result[i] = targetAddress;
                    }
                    value = result;
                }
                break;

                case OperationCode.Ldind_I1:
                case OperationCode.Ldind_U1:
                case OperationCode.Ldind_I2:
                case OperationCode.Ldind_U2:
                case OperationCode.Ldind_I4:
                case OperationCode.Ldind_U4:
                case OperationCode.Ldind_I8:
                case OperationCode.Ldind_I:
                case OperationCode.Ldind_R4:
                case OperationCode.Ldind_R8:
                case OperationCode.Ldind_Ref:
                case OperationCode.Stind_Ref:
                case OperationCode.Stind_I1:
                case OperationCode.Stind_I2:
                case OperationCode.Stind_I4:
                case OperationCode.Stind_I8:
                case OperationCode.Stind_R4:
                case OperationCode.Stind_R8:
                case OperationCode.Add:
                case OperationCode.Sub:
                case OperationCode.Mul:
                case OperationCode.Div:
                case OperationCode.Div_Un:
                case OperationCode.Rem:
                case OperationCode.Rem_Un:
                case OperationCode.And:
                case OperationCode.Or:
                case OperationCode.Xor:
                case OperationCode.Shl:
                case OperationCode.Shr:
                case OperationCode.Shr_Un:
                case OperationCode.Neg:
                case OperationCode.Not:
                case OperationCode.Conv_I1:
                case OperationCode.Conv_I2:
                case OperationCode.Conv_I4:
                case OperationCode.Conv_I8:
                case OperationCode.Conv_R4:
                case OperationCode.Conv_R8:
                case OperationCode.Conv_U4:
                case OperationCode.Conv_U8:
                    break;

                case OperationCode.Callvirt:
                    value = this.GetMethod(memReader.ReadUInt32());
                    break;

                case OperationCode.Cpobj:
                case OperationCode.Ldobj:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Ldstr:
                    value = this.GetUserStringForToken(memReader.ReadUInt32());
                    break;

                case OperationCode.Newobj:
                {
                    IMethodReference          methodReference = this.GetMethod(memReader.ReadUInt32());
                    IArrayTypeReference /*?*/ arrayType       = methodReference.ContainingType as IArrayTypeReference;
                    if (arrayType != null && !arrayType.IsVector)
                    {
                        uint numParam = IteratorHelper.EnumerableCount(methodReference.Parameters);
                        if (numParam != arrayType.Rank)
                        {
                            cilOpCode = OperationCode.Array_Create_WithLowerBound;
                        }
                        else
                        {
                            cilOpCode = OperationCode.Array_Create;
                        }
                        value = arrayType;
                    }
                    else
                    {
                        value = methodReference;
                    }
                }
                break;

                case OperationCode.Castclass:
                case OperationCode.Isinst:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Conv_R_Un:
                    break;

                case OperationCode.Unbox:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Throw:
                    break;

                case OperationCode.Ldfld:
                case OperationCode.Ldflda:
                case OperationCode.Stfld:
                case OperationCode.Ldsfld:
                case OperationCode.Ldsflda:
                case OperationCode.Stsfld:
                    value = this.GetField(memReader.ReadUInt32());
                    break;

                case OperationCode.Stobj:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Conv_Ovf_I1_Un:
                case OperationCode.Conv_Ovf_I2_Un:
                case OperationCode.Conv_Ovf_I4_Un:
                case OperationCode.Conv_Ovf_I8_Un:
                case OperationCode.Conv_Ovf_U1_Un:
                case OperationCode.Conv_Ovf_U2_Un:
                case OperationCode.Conv_Ovf_U4_Un:
                case OperationCode.Conv_Ovf_U8_Un:
                case OperationCode.Conv_Ovf_I_Un:
                case OperationCode.Conv_Ovf_U_Un:
                    break;

                case OperationCode.Box:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Newarr:
                {
                    ITypeReference             elementType         = this.GetType(memReader.ReadUInt32());
                    IModuleTypeReference /*?*/ moduleTypeReference = elementType as IModuleTypeReference;
                    if (moduleTypeReference != null)
                    {
                        value = new VectorType(this.PEFileToObjectModel, 0xFFFFFFFF, moduleTypeReference);
                    }
                    else
                    {
                        value = Dummy.ArrayType;
                    }
                }
                break;

                case OperationCode.Ldlen:
                    break;

                case OperationCode.Ldelema:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Ldelem_I1:
                case OperationCode.Ldelem_U1:
                case OperationCode.Ldelem_I2:
                case OperationCode.Ldelem_U2:
                case OperationCode.Ldelem_I4:
                case OperationCode.Ldelem_U4:
                case OperationCode.Ldelem_I8:
                case OperationCode.Ldelem_I:
                case OperationCode.Ldelem_R4:
                case OperationCode.Ldelem_R8:
                case OperationCode.Ldelem_Ref:
                case OperationCode.Stelem_I:
                case OperationCode.Stelem_I1:
                case OperationCode.Stelem_I2:
                case OperationCode.Stelem_I4:
                case OperationCode.Stelem_I8:
                case OperationCode.Stelem_R4:
                case OperationCode.Stelem_R8:
                case OperationCode.Stelem_Ref:
                    break;

                case OperationCode.Ldelem:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Stelem:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Unbox_Any:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Conv_Ovf_I1:
                case OperationCode.Conv_Ovf_U1:
                case OperationCode.Conv_Ovf_I2:
                case OperationCode.Conv_Ovf_U2:
                case OperationCode.Conv_Ovf_I4:
                case OperationCode.Conv_Ovf_U4:
                case OperationCode.Conv_Ovf_I8:
                case OperationCode.Conv_Ovf_U8:
                    break;

                case OperationCode.Refanyval:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Ckfinite:
                    break;

                case OperationCode.Mkrefany:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Ldtoken:
                    value = this.GetRuntimeHandleFromToken(memReader.ReadUInt32());
                    break;

                case OperationCode.Conv_U2:
                case OperationCode.Conv_U1:
                case OperationCode.Conv_I:
                case OperationCode.Conv_Ovf_I:
                case OperationCode.Conv_Ovf_U:
                case OperationCode.Add_Ovf:
                case OperationCode.Add_Ovf_Un:
                case OperationCode.Mul_Ovf:
                case OperationCode.Mul_Ovf_Un:
                case OperationCode.Sub_Ovf:
                case OperationCode.Sub_Ovf_Un:
                case OperationCode.Endfinally:
                    break;

                case OperationCode.Leave:
                {
                    uint leaveOffset = (uint)(memReader.Offset + 4 + memReader.ReadInt32());
                    if (leaveOffset >= _endOfMethodOffset)
                    {
                        //  Error...
                    }
                    value = leaveOffset;
                }
                break;

                case OperationCode.Leave_S:
                {
                    uint leaveOffset = (uint)(memReader.Offset + 1 + memReader.ReadSByte());
                    if (leaveOffset >= _endOfMethodOffset)
                    {
                        //  Error...
                    }
                    value = leaveOffset;
                }
                break;

                case OperationCode.Stind_I:
                case OperationCode.Conv_U:
                case OperationCode.Arglist:
                case OperationCode.Ceq:
                case OperationCode.Cgt:
                case OperationCode.Cgt_Un:
                case OperationCode.Clt:
                case OperationCode.Clt_Un:
                    break;

                case OperationCode.Ldftn:
                case OperationCode.Ldvirtftn:
                    value = this.GetMethod(memReader.ReadUInt32());
                    break;

                case OperationCode.Ldarg:
                case OperationCode.Ldarga:
                case OperationCode.Starg:
                    value = this.GetParameter(memReader.ReadUInt16());
                    break;

                case OperationCode.Ldloc:
                case OperationCode.Ldloca:
                case OperationCode.Stloc:
                    value = this.GetLocal(memReader.ReadUInt16());
                    break;

                case OperationCode.Localloc:
                    value = new PointerType(this.PEFileToObjectModel, 0xFFFFFFFF, this.PEFileToObjectModel.SystemVoid);
                    break;

                case OperationCode.Endfilter:
                    break;

                case OperationCode.Unaligned_:
                    value = memReader.ReadByte();
                    break;

                case OperationCode.Volatile_:
                case OperationCode.Tail_:
                    break;

                case OperationCode.Initobj:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Constrained_:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Cpblk:
                case OperationCode.Initblk:
                    break;

                case OperationCode.No_:
                    value = (OperationCheckFlags)memReader.ReadByte();
                    break;

                case OperationCode.Rethrow:
                    break;

                case OperationCode.Sizeof:
                    value = this.GetType(memReader.ReadUInt32());
                    break;

                case OperationCode.Refanytype:
                case OperationCode.Readonly_:
                    break;

                default:
                    this.PEFileToObjectModel.PEFileReader.ErrorContainer.AddILError(this.MethodDefinition, offset, MetadataReaderErrorKind.UnknownILInstruction);
                    break;
                }
                MethodBodyLocation location = new MethodBodyLocation(document, offset);
                instrList.Add(new CilInstruction(cilOpCode, location, value));
            }
            this.MethodBody.SetCilInstructions(new EnumerableArrayWrapper <CilInstruction, IOperation>(instrList.ToArray(), Dummy.Operation));
            return(true);
        }