Beispiel #1
0
        /// <summary>
        /// Parses the generic instance.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="index">The index.</param>
        /// <returns></returns>
        private static SigType ParseGenericInstance(byte[] buffer, ref int index)
        {
            SigType        originalType;
            CilElementType type = (CilElementType)buffer[index++];

            switch (type)
            {
            case CilElementType.Class:
                originalType = ParseClassSignature(buffer, ref index);
                break;

            case CilElementType.ValueType:
                originalType = ParseValueType(buffer, ref index);
                break;

            default:

                throw new InvalidOperationException("Invalid signature type.");
            }

            int genArgCount = Utilities.ReadCompressedInt32(buffer, ref index);

            SigType[] genArgs = new SigType[genArgCount];
            for (int i = 0; i < genArgCount; i++)
            {
                genArgs[i] = ParseTypeSignature(buffer, ref index);
            }

            return(new GenericInstSigType(originalType, genArgs));
        }
Beispiel #2
0
        /// <summary>
        /// Parses the generic instance.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        private static SigType ParseGenericInstance(SignatureReader reader)
        {
            TypeSigType    originalType;
            CilElementType type = (CilElementType)reader.ReadByte();

            switch (type)
            {
            case CilElementType.Class:
                originalType = ParseClassSignature(reader);
                break;

            case CilElementType.ValueType:
                originalType = ParseValueType(reader);
                break;

            default:
                throw new InvalidOperationException(@"Invalid signature type.");
            }

            int genArgCount = reader.ReadCompressedInt32();

            SigType[] genArgs = new SigType[genArgCount];
            for (int i = 0; i < genArgCount; i++)
            {
                genArgs[i] = ParseTypeSignature(reader);
            }

            return(new GenericInstSigType(originalType, genArgs));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="VariableSignature"/> class.
 /// </summary>
 /// <param name="provider">The provider.</param>
 /// <param name="token">The token.</param>
 public VariableSignature(VariableSignature signature)
     : base(signature)
 {
     this.customMods = signature.customMods;
     this.modifier = signature.modifier;
     this.type = signature.type;
 }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VariableSignature"/> class.
 /// </summary>
 /// <param name="provider">The provider.</param>
 /// <param name="token">The token.</param>
 public VariableSignature(VariableSignature signature)
     : base(signature)
 {
     this.customMods = signature.customMods;
     this.modifier   = signature.modifier;
     this.type       = signature.type;
 }
Beispiel #5
0
        /// <summary>
        /// Parses the modifier.
        /// </summary>
        /// <param name="reader">The reader.</param>
        private void ParseModifier(SignatureReader reader)
        {
            CilElementType value = (CilElementType)reader.PeekByte();

            if (value == CilElementType.Pinned)
            {
                Modifier = value;
                reader.SkipByte();
            }
        }
Beispiel #6
0
        /// <summary>
        /// Reads the specified token.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <param name="result">The result.</param>
        public void Read(TokenTypes token, out ConstantRow result)
        {
            if ((token & TokenTypes.TableMask) != TokenTypes.Constant)
            {
                throw new ArgumentException("Invalid token type for ConstantRow.", "token");
            }

            using (BinaryReader reader = CreateReaderForToken(token))
            {
                CilElementType cet = (CilElementType)reader.ReadByte();
                reader.ReadByte();

                result = new ConstantRow(cet, ReadIndexValue(reader, IndexType.HasConstant), ReadIndexValue(reader, IndexType.BlobHeap));
            }
        }
Beispiel #7
0
        /// <summary>
        /// Parses the custom mods.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="index">The index.</param>
        /// <returns></returns>
        public static CustomMod[] ParseCustomMods(byte[] buffer, ref int index)
        {
            List <CustomMod> mods = new List <CustomMod>();

            for (int i = index; i < buffer.Length; i++)
            {
                CilElementType type = (CilElementType)buffer[i++];
                if (type != CilElementType.Optional && type != CilElementType.Required)
                {
                    break;
                }

                index++;
                TokenTypes modType = SigType.ReadTypeDefOrRefEncoded(buffer, ref index);
                mods.Add(new CustomMod((CustomModType)(type - CilElementType.Required + 1), modType));
            }

            return(mods.ToArray());
        }
Beispiel #8
0
        /// <summary>
        /// Reads the specified token.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public ConstantRow ReadConstantRow(Token token)
        {
            if (token.Table != TableType.Constant)
            {
                throw new ArgumentException("Invalid token type for ConstantRow.", @"token");
            }

            using (BinaryReader reader = CreateReaderForToken(token))
            {
                CilElementType cet = (CilElementType)reader.ReadByte();
                reader.ReadByte();

                return(new ConstantRow(
                           cet,
                           ReadMetadataToken(reader, IndexType.HasConstant),
                           ReadHeapToken(reader, IndexType.BlobHeap)
                           ));
            }
        }
Beispiel #9
0
        /// <summary>
        /// Parses the custom mods.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public static CustomMod[] ParseCustomMods(SignatureReader reader)
        {
            List <CustomMod> mods = new List <CustomMod>();

            while (reader.Index != reader.Length)
            {
                CilElementType type = (CilElementType)reader.PeekByte();
                if (type != CilElementType.Optional && type != CilElementType.Required)
                {
                    break;
                }

                reader.SkipByte();

                Token modType = reader.ReadEncodedTypeDefOrRef();
                mods.Add(new CustomMod((CustomModType)(type - CilElementType.Required + 1), modType));
            }

            return(mods.ToArray());
        }
Beispiel #10
0
		/// <summary>
		/// Initializes a new instance of the <see cref="ConstantRow" /> struct.
		/// </summary>
		/// <param name="type">The type.</param>
		/// <param name="parent">The parent.</param>
		/// <param name="value">The value.</param>
		public ConstantRow(CilElementType type, Token parent, HeapIndexToken value)
		{
			Type = type;
			Parent = parent;
			Value = value;
		}
Beispiel #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SigType"/> class.
 /// </summary>
 /// <param name="type">The type.</param>
 public SigType(CilElementType type)
 {
     _type = type;
 }
Beispiel #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstantRow" /> struct.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="parent">The parent.</param>
 /// <param name="value">The value.</param>
 public ConstantRow(CilElementType type, Token parent, HeapIndexToken value)
 {
     Type   = type;
     Parent = parent;
     Value  = value;
 }
Beispiel #13
0
        /// <summary>
        /// Parses the type signature.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="index">The index.</param>
        /// <returns></returns>
        public static SigType ParseTypeSignature(byte[] buffer, ref int index)
        {
            SigType        result;
            CilElementType type = (CilElementType)buffer[index++];

            switch (type)
            {
            case CilElementType.Void:
                result = new SigType(type);
                break;

            case CilElementType.Boolean:
                goto case CilElementType.Void;

            case CilElementType.Char:
                goto case CilElementType.Void;

            case CilElementType.I1:
                goto case CilElementType.Void;

            case CilElementType.U1:
                goto case CilElementType.Void;

            case CilElementType.I2:
                goto case CilElementType.Void;

            case CilElementType.U2:
                goto case CilElementType.Void;

            case CilElementType.I4:
                goto case CilElementType.Void;

            case CilElementType.U4:
                goto case CilElementType.Void;

            case CilElementType.I8:
                goto case CilElementType.Void;

            case CilElementType.U8:
                goto case CilElementType.Void;

            case CilElementType.R4:
                goto case CilElementType.Void;

            case CilElementType.R8:
                goto case CilElementType.Void;

            case CilElementType.String:
                goto case CilElementType.Void;

            case CilElementType.Object:
                goto case CilElementType.Void;

            case CilElementType.I:
                goto case CilElementType.Void;

            case CilElementType.U:
                goto case CilElementType.Void;

            case CilElementType.TypedByRef:
                goto case CilElementType.Void;

            case CilElementType.Array:
                result = ParseArraySignature(buffer, ref index);
                break;

            case CilElementType.Class:
                result = ParseClassSignature(buffer, ref index);
                break;

            case CilElementType.FunctionPtr:
                result = ParseFunctionPointer(buffer, ref index);
                break;

            case CilElementType.GenericInst:
                result = ParseGenericInstance(buffer, ref index);
                break;

            case CilElementType.MVar:
                result = ParseMVar(buffer, ref index);
                break;

            case CilElementType.Ptr:
                result = ParsePointer(buffer, ref index);
                break;

            case CilElementType.SZArray:
                result = ParseSZArraySignature(buffer, ref index);
                break;

            case CilElementType.ValueType:
                result = ParseValueType(buffer, ref index);
                break;

            case CilElementType.Var:
                result = ParseVar(buffer, ref index);
                break;

            case CilElementType.ByRef:
                result = ParseReference(buffer, ref index);
                break;

            default:

                throw new NotSupportedException("Unsupported CIL element type: " + type);
            }

            return(result);
        }
Beispiel #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstantRow"/> struct.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="parent">The parent.</param>
 /// <param name="value">The value BLOB idx.</param>
 public ConstantRow(CilElementType type, Token parent, HeapIndexToken value)
 {
     this.type   = type;
     this.parent = parent;
     this.value  = value;
 }
 private void ParseModifier(SignatureReader reader)
 {
     CilElementType value = (CilElementType)reader.PeekByte();
     if (value == CilElementType.Pinned)
     {
         this.modifier = value;
         reader.SkipByte();
     }
 }
Beispiel #16
0
 public TypeSigType(Token token, CilElementType type) :
     base(type)
 {
     this.token = token;
 }
Beispiel #17
0
        //public static readonly BuiltInSigType Ptr = new BuiltInSigType(CilElementType.Ptr);

        internal BuiltInSigType(CilElementType type) :
            base(type)
        {
        }
Beispiel #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SigType"/> class.
 /// </summary>
 /// <param name="type">The type.</param>
 public SigType(CilElementType type)
 {
     this.Type = type;
 }
Beispiel #19
0
 //public static readonly BuiltInSigType Ptr = new BuiltInSigType(CilElementType.Ptr);
 internal BuiltInSigType(CilElementType type)
     : base(type)
 {
 }
 /// <summary>
 /// Gets the return registers.
 /// </summary>
 /// <param name="returnType">Type of the return.</param>
 /// <returns></returns>
 Register[] ICallingConvention.GetReturnRegisters(CilElementType returnType)
 {
     return null;
 }
        /// <summary>
        /// Converts a <see cref="CilElementType"/> to <see cref="ConvType"/>.
        /// </summary>
        /// <param name="cet">The CilElementType to convert.</param>
        /// <returns>The equivalent ConvType.</returns>
        /// <exception cref="T:System.NotSupportedException"><paramref name="cet"/> can't be converted.</exception>
        private ConvType ConvTypeFromCilType(CilElementType cet)
        {
            switch (cet) {
                case CilElementType.Char:
                    return ConvType.U2;
                case CilElementType.I1:
                    return ConvType.I1;
                case CilElementType.I2:
                    return ConvType.I2;
                case CilElementType.I4:
                    return ConvType.I4;
                case CilElementType.I8:
                    return ConvType.I8;
                case CilElementType.U1:
                    return ConvType.U1;
                case CilElementType.U2:
                    return ConvType.U2;
                case CilElementType.U4:
                    return ConvType.U4;
                case CilElementType.U8:
                    return ConvType.U8;
                case CilElementType.R4:
                    return ConvType.R4;
                case CilElementType.R8:
                    return ConvType.R8;
                case CilElementType.I:
                    return ConvType.I;
                case CilElementType.U:
                    return ConvType.U;
                case CilElementType.Ptr:
                    return ConvType.Ptr;
            }

            // Requested CilElementType is not supported
            throw new NotSupportedException ();
        }
Beispiel #22
0
 public TypeSigType(Token token, CilElementType type)
     : base(type)
 {
     this.token = token;
 }
Beispiel #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstantRow"/> struct.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="parent">The parent.</param>
 /// <param name="valueBlobIdx">The value BLOB idx.</param>
 public ConstantRow(CilElementType type, TokenTypes parent, TokenTypes valueBlobIdx)
 {
     _type         = type;
     _parent       = parent;
     _valueBlobIdx = valueBlobIdx;
 }
 private static bool MustSignExtendOnLoad(CilElementType elementType)
 {
     return (elementType == CilElementType.I1 || elementType == CilElementType.I2);
 }
 public BuiltInSigType(string typeName, CilElementType type) :
     base(type)
 {
     this.typeName = typeName;
 }
 private static bool MustZeroExtendOnLoad(CilElementType elementType)
 {
     return (elementType == CilElementType.U1 || elementType == CilElementType.U2 || elementType == CilElementType.Char);
 }
Beispiel #27
0
 public BuiltInSigType(string typeName, CilElementType type)
     : base(type)
 {
     this.typeName = typeName;
 }
Beispiel #28
0
        /// <summary>
        /// Parses the type signature.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public static SigType ParseTypeSignature(SignatureReader reader)
        {
            CilElementType type = (CilElementType)reader.ReadByte();

            switch (type)
            {
            case CilElementType.Void:
                return(BuiltInSigType.Void);

            case CilElementType.Boolean:
                return(BuiltInSigType.Boolean);

            case CilElementType.Char:
                return(BuiltInSigType.Char);

            case CilElementType.I1:
                return(BuiltInSigType.SByte);

            case CilElementType.U1:
                return(BuiltInSigType.Byte);

            case CilElementType.I2:
                return(BuiltInSigType.Int16);

            case CilElementType.U2:
                return(BuiltInSigType.UInt16);

            case CilElementType.I4:
                return(BuiltInSigType.Int32);

            case CilElementType.U4:
                return(BuiltInSigType.UInt32);

            case CilElementType.I8:
                return(BuiltInSigType.Int64);

            case CilElementType.U8:
                return(BuiltInSigType.UInt64);

            case CilElementType.R4:
                return(BuiltInSigType.Single);

            case CilElementType.R8:
                return(BuiltInSigType.Double);

            case CilElementType.String:
                return(BuiltInSigType.String);

            case CilElementType.Object:
                return(BuiltInSigType.Object);

            case CilElementType.I:
                return(BuiltInSigType.IntPtr);

            case CilElementType.U:
                return(BuiltInSigType.UIntPtr);

            case CilElementType.TypedByRef:
                return(BuiltInSigType.TypedByRef);

            case CilElementType.Array:
                return(ParseArraySignature(reader));

            case CilElementType.Class:
                return(ParseClassSignature(reader));

            case CilElementType.FunctionPtr:
                return(ParseFunctionPointer(reader));

            case CilElementType.GenericInst:
                return(ParseGenericInstance(reader));

            case CilElementType.MVar:
                return(ParseMVar(reader));

            case CilElementType.Ptr:
                return(ParsePointer(reader));

            case CilElementType.SZArray:
                return(ParseSZArraySignature(reader));

            case CilElementType.ValueType:
                return(ParseValueType(reader));

            case CilElementType.Var:
                return(ParseVar(reader));

            case CilElementType.ByRef:
                return(ParseReference(reader));

            default:
                throw new NotSupportedException(@"Unsupported CIL element type: " + type);
            }
        }
Beispiel #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstantRow"/> struct.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="parent">The parent.</param>
 /// <param name="value">The value BLOB idx.</param>
 public ConstantRow(CilElementType type, Token parent, HeapIndexToken value)
 {
     this.type = type;
     this.parent = parent;
     this.value = value;
 }
Beispiel #30
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SigType"/> class.
 /// </summary>
 /// <param name="type">The type.</param>
 public SigType(CilElementType type)
 {
     this.type = type;
 }
 private static bool MustSignExtendOnLoad(CilElementType elementType)
 {
     return(elementType == CilElementType.I1 || elementType == CilElementType.I2);
 }
Beispiel #32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConstantRow"/> struct.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="parent">The parent.</param>
 /// <param name="valueBlobIdx">The value BLOB idx.</param>
 public ConstantRow(CilElementType type, TokenTypes parent, TokenTypes valueBlobIdx)
 {
     _type = type;
     _parent = parent;
     _valueBlobIdx = valueBlobIdx;
 }
 private static bool MustZeroExtendOnLoad(CilElementType elementType)
 {
     return(elementType == CilElementType.U1 || elementType == CilElementType.U2 || elementType == CilElementType.Char);
 }
        /// <summary>
        /// Gets the return registers.
        /// </summary>
        /// <param name="returnType">Type of the return.</param>
        /// <returns></returns>
        Register[] ICallingConvention.GetReturnRegisters(CilElementType returnType)
        {
            if (returnType == CilElementType.Void)
                return ReturnVoidRegisters;

            if (returnType == CilElementType.R4 || returnType == CilElementType.R8)
                return ReturnFPRegisters;

            if (returnType == CilElementType.I8 || returnType == CilElementType.U8)
                return Return64BitRegisters;

            return Return32BitRegisters;
        }
        private StackTypeCode FromSigType(CilElementType type)
        {
            switch (type)
            {
                case CilElementType.I1: goto case CilElementType.U4;
                case CilElementType.I2: goto case CilElementType.U4;
                case CilElementType.I4: goto case CilElementType.U4;
                case CilElementType.U1: goto case CilElementType.U4;
                case CilElementType.U2: goto case CilElementType.U4;
                case CilElementType.U4: return StackTypeCode.Int32;
            }

            throw new NotSupportedException();
        }