protected virtual void _Generate(AstNodeExprImm item)
        {
            var itemType  = AstUtils.GetSignedType(item.Type);
            var itemValue = item.Value;

            if (itemType.IsEnum)
            {
                itemType  = itemType.GetEnumUnderlyingType();
                itemValue = AstUtils.CastType(itemValue, itemType);
            }

            if (
                itemType == typeof(int) ||
                itemType == typeof(sbyte) ||
                itemType == typeof(short) ||
                itemType == typeof(bool)
                )
            {
                var value = (int)Convert.ToInt64(itemValue);
                switch (value)
                {
                case -1:
                    Emit(OpCodes.Ldc_I4_M1);
                    break;

                case 0:
                    Emit(OpCodes.Ldc_I4_0);
                    break;

                case 1:
                    Emit(OpCodes.Ldc_I4_1);
                    break;

                case 2:
                    Emit(OpCodes.Ldc_I4_2);
                    break;

                case 3:
                    Emit(OpCodes.Ldc_I4_3);
                    break;

                case 4:
                    Emit(OpCodes.Ldc_I4_4);
                    break;

                case 5:
                    Emit(OpCodes.Ldc_I4_5);
                    break;

                case 6:
                    Emit(OpCodes.Ldc_I4_6);
                    break;

                case 7:
                    Emit(OpCodes.Ldc_I4_7);
                    break;

                case 8:
                    Emit(OpCodes.Ldc_I4_8);
                    break;

                default:
                    Emit(OpCodes.Ldc_I4, value);
                    break;
                }
            }
            else if (itemType == typeof(long) || itemType == typeof(ulong))
            {
                Emit(OpCodes.Ldc_I8, Convert.ToInt64(itemValue));
            }
            else if (itemType == typeof(IntPtr))
            {
#if false
                Emit(OpCodes.Ldc_I8, ((IntPtr)Item.Value).ToInt64());
                Emit(OpCodes.Conv_I);
#else
                if (Environment.Is64BitProcess)
                {
                    Emit(OpCodes.Ldc_I8, ((IntPtr)item.Value).ToInt64());
                    Emit(OpCodes.Conv_I);
                }
                else
                {
                    Emit(OpCodes.Ldc_I4, ((IntPtr)item.Value).ToInt32());
                    Emit(OpCodes.Conv_I);
                }
#endif
            }
            else if (itemType == typeof(float))
            {
                Emit(OpCodes.Ldc_R4, (float)item.Value);
            }
            else if (item.Value == null)
            {
                Emit(OpCodes.Ldnull);
            }
            else if (itemType == typeof(string))
            {
                Emit(OpCodes.Ldstr, (string)item.Value);
            }
            else if (itemType == typeof(Type))
            {
                Emit(OpCodes.Ldtoken, (Type)item.Value);
                Emit(OpCodes.Call, ((Func <RuntimeTypeHandle, Type>)Type.GetTypeFromHandle).Method);
                //IL_0005: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
            }
            else
            {
                throw new NotImplementedException($"Can't handle immediate type {itemType}");
            }
        }
示例#2
0
 protected virtual AstNode _Optimize(AstNodeExprImm immediate)
 {
     return(immediate);
 }
示例#3
0
 protected virtual void _Generate(AstNodeExprImm item)
 {
     Output.Write(ValueAsString(item.Value, item.Type));
 }