コード例 #1
0
        static void InitializeValue(ref ILValue value, DmdType valueType)
        {
            if (value is not null)
            {
                return;
            }

            if (!valueType.IsValueType)
            {
                value = new NullObjectRefILValue();
                return;
            }

            switch (DmdType.GetTypeCode(valueType))
            {
            case TypeCode.Boolean:          value = new ConstantInt32ILValue(valueType.AppDomain, 0); return;

            case TypeCode.Char:                     value = new ConstantInt32ILValue(valueType.AppDomain, 0); return;

            case TypeCode.SByte:            value = new ConstantInt32ILValue(valueType.AppDomain, 0); return;

            case TypeCode.Byte:                     value = new ConstantInt32ILValue(valueType.AppDomain, 0); return;

            case TypeCode.Int16:            value = new ConstantInt32ILValue(valueType.AppDomain, 0); return;

            case TypeCode.UInt16:           value = new ConstantInt32ILValue(valueType.AppDomain, 0); return;

            case TypeCode.Int32:            value = new ConstantInt32ILValue(valueType.AppDomain, 0); return;

            case TypeCode.UInt32:           value = new ConstantInt32ILValue(valueType.AppDomain, 0); return;

            case TypeCode.Int64:            value = new ConstantInt64ILValue(valueType.AppDomain, 0); return;

            case TypeCode.UInt64:           value = new ConstantInt64ILValue(valueType.AppDomain, 0); return;

            case TypeCode.Single:           value = new ConstantFloatILValue(valueType.AppDomain, 0); return;

            case TypeCode.Double:           value = new ConstantFloatILValue(valueType.AppDomain, 0); return;
            }
            if (valueType == valueType.AppDomain.System_IntPtr || valueType == valueType.AppDomain.System_UIntPtr)
            {
                value = valueType.AppDomain.Runtime.PointerSize == 4 ? ConstantNativeIntILValue.Create32(valueType.AppDomain, 0) : ConstantNativeIntILValue.Create64(valueType.AppDomain, 0);
                return;
            }

            throw new InvalidOperationException();
        }
コード例 #2
0
        static ILValue CreateDefaultValue(DmdType type)
        {
            switch (DmdType.GetTypeCode(type))
            {
            case TypeCode.Boolean:
            case TypeCode.Char:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:           return(new ConstantInt32ILValue(type.AppDomain, 0));

            case TypeCode.Int64:
            case TypeCode.UInt64:           return(new ConstantInt64ILValue(type.AppDomain, 0));

            case TypeCode.Single:
            case TypeCode.Double:           return(new ConstantFloatILValue(type.AppDomain, 0));
            }
            if (type == type.AppDomain.System_IntPtr || type == type.AppDomain.System_UIntPtr)
            {
                return(IntPtr.Size == 4 ? ConstantNativeIntILValue.Create32(type.AppDomain, 0) : ConstantNativeIntILValue.Create64(type.AppDomain, 0));
            }
            if (type.IsValueType)
            {
                return(new FakeValueType(type));
            }
            return(new NullObjectRefILValue());
        }
コード例 #3
0
        /// <summary>
        /// Loads a value. Returns null if it's not supported.
        /// </summary>
        /// <param name="type">Type</param>
        /// <param name="loadValueType">Type of value to load</param>
        /// <returns></returns>
        public override ILValue?LoadIndirect(DmdType type, LoadValueType loadValueType)
        {
            int pointerSize = type.AppDomain.Runtime.PointerSize;

            switch (loadValueType)
            {
            case LoadValueType.I:
                if (pointerSize == 4)
                {
                    if (offset + 4 - 1 < offset || (ulong)offset + 4 - 1 >= (ulong)data.Length)
                    {
                        return(null);
                    }
                    return(ConstantNativeIntILValue.Create32(type.AppDomain, BitConverter.ToInt32(data, (int)offset)));
                }
                else
                {
                    Debug.Assert(pointerSize == 8);
                    if (offset + 8 - 1 < offset || (ulong)offset + 8 - 1 >= (ulong)data.Length)
                    {
                        return(null);
                    }
                    return(ConstantNativeIntILValue.Create64(type.AppDomain, BitConverter.ToInt64(data, (int)offset)));
                }

            case LoadValueType.I1:
                if ((ulong)offset >= (ulong)data.Length)
                {
                    return(null);
                }
                return(new ConstantInt32ILValue(type.AppDomain.System_SByte, (sbyte)data[(int)offset]));

            case LoadValueType.I2:
                if (offset + 2 - 1 < offset || (ulong)offset + 2 - 1 >= (ulong)data.Length)
                {
                    return(null);
                }
                return(new ConstantInt32ILValue(type.AppDomain.System_Int16, BitConverter.ToInt16(data, (int)offset)));

            case LoadValueType.I4:
                if (offset + 4 - 1 < offset || (ulong)offset + 4 - 1 >= (ulong)data.Length)
                {
                    return(null);
                }
                return(new ConstantInt32ILValue(type.AppDomain.System_Int32, BitConverter.ToInt32(data, (int)offset)));

            case LoadValueType.I8:
                if (offset + 8 - 1 < offset || (ulong)offset + 8 - 1 >= (ulong)data.Length)
                {
                    return(null);
                }
                return(new ConstantInt64ILValue(type.AppDomain.System_Int64, BitConverter.ToInt64(data, (int)offset)));

            case LoadValueType.R4:
                if (offset + 4 - 1 < offset || (ulong)offset + 4 - 1 >= (ulong)data.Length)
                {
                    return(null);
                }
                return(new ConstantFloatILValue(type.AppDomain.System_Single, BitConverter.ToSingle(data, (int)offset)));

            case LoadValueType.R8:
                if (offset + 8 - 1 < offset || (ulong)offset + 8 - 1 >= (ulong)data.Length)
                {
                    return(null);
                }
                return(new ConstantFloatILValue(type.AppDomain.System_Double, BitConverter.ToDouble(data, (int)offset)));

            case LoadValueType.Ref:
                return(null);

            case LoadValueType.U1:
                if ((ulong)offset >= (ulong)data.Length)
                {
                    return(null);
                }
                return(new ConstantInt32ILValue(type.AppDomain.System_Byte, data[(int)offset]));

            case LoadValueType.U2:
                if (offset + 2 - 1 < offset || (ulong)offset + 2 - 1 >= (ulong)data.Length)
                {
                    return(null);
                }
                return(new ConstantInt32ILValue(type.AppDomain.System_UInt16, BitConverter.ToUInt16(data, (int)offset)));

            case LoadValueType.U4:
                if (offset + 4 - 1 < offset || (ulong)offset + 4 - 1 >= (ulong)data.Length)
                {
                    return(null);
                }
                return(new ConstantInt32ILValue(type.AppDomain.System_UInt32, BitConverter.ToInt32(data, (int)offset)));

            default:
                return(null);
            }
        }