/// <summary>
        /// Convert the given argument if it does not match the target type.
        /// </summary>
        private static void ConvertForRetIfNeeded(AstExpression arg, XTypeReference targetType)
        {
            var argType = arg.GetResultType();

            if (!targetType.IsSame(argType))
            {
                if (targetType.IsChar())
                {
                    if (argType.IsUInt16() || argType.IsByte() || argType.IsInt16())
                    {
                        Convert(arg, AstCode.Conv_U2, targetType);
                    }
                }
                else if (targetType.IsUInt16())
                {
                    if (argType.IsChar() || argType.IsByte())
                    {
                        Convert(arg, AstCode.Int_to_ushort, targetType);
                    }
                }
                else if (targetType.IsInt16())
                {
                    if (argType.IsChar() || argType.IsByte())
                    {
                        Convert(arg, AstCode.Conv_I2, targetType);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Gets the type of stelem code to use for the given element type.
 /// </summary>
 public static AstCode GetStElemCode(this XTypeReference elementType)
 {
     if (elementType.IsByte() || elementType.IsSByte() || elementType.IsBoolean())
     {
         return(AstCode.Stelem_I1);
     }
     if (elementType.IsChar() || elementType.IsInt16() || elementType.IsUInt16())
     {
         return(AstCode.Stelem_I2);
     }
     if (elementType.IsInt32() || elementType.IsUInt32())
     {
         return(AstCode.Stelem_I4);
     }
     if (elementType.IsInt64() || elementType.IsUInt64())
     {
         return(AstCode.Stelem_I8);
     }
     if (elementType.IsFloat())
     {
         return(AstCode.Stelem_R4);
     }
     if (elementType.IsDouble())
     {
         return(AstCode.Stelem_R8);
     }
     return(AstCode.Stelem_Ref);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Generate converter for the value of an Const instruction.
 /// </summary>
 internal static Func <object, object> ConstValueConverter(this XTypeReference elementType, bool keepUnsigned)
 {
     if (elementType.IsBoolean())
     {
         return(x => Convert.ToBoolean(x) ? 1 : 0);
     }
     if (elementType.IsByte())
     {
         if (keepUnsigned)
         {
             return(x => (int)Convert.ToByte(x));
         }
         return(x => (int)((sbyte)unchecked (Convert.ToByte(x))));
     }
     if (elementType.IsSByte())
     {
         return(x => (int)(Convert.ToSByte(x)));
     }
     if (elementType.IsChar())
     {
         return(x => (int)(Convert.ToChar(x)));
     }
     if (elementType.IsUInt16())
     {
         return(x => (int)(Convert.ToUInt16(x)));
     }
     if (elementType.IsInt16())
     {
         return(x => (int)(Convert.ToInt16(x)));
     }
     if (elementType.IsInt32())
     {
         return(x => XConvert.ToInt(x));
     }
     if (elementType.IsUInt32())
     {
         return(x => XConvert.ToInt(x));                       // unchecked((int)Convert.ToUInt32(Convert.ToInt64(x) & 0xFFFFFFFF));
     }
     if (elementType.IsFloat())
     {
         return(x => Convert.ToSingle(x));
     }
     if (elementType.IsInt64())
     {
         return(x => XConvert.ToLong(x));
     }
     if (elementType.IsUInt64())
     {
         return(x => XConvert.ToLong(x));                        // unchecked((long)Convert.ToInt64(x));
     }
     if (elementType.IsDouble())
     {
         return(x => Convert.ToDouble(x));
     }
     if (elementType.IsDexObject() && !elementType.IsVoid())
     {
         return(x => x);
     }
     throw new NotSupportedException("Unknown type for constValueConverter " + elementType);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns the method name when converting an array to an IEnumerableT in compiler helper.
        ///
        /// (not sure if this is the best place for this method...)
        /// </summary>
        public static string GetAsEnumerableTMethodName(XTypeReference sourceArrayElementType)
        {
            var convertMethodName = "AsObjectEnumerable";

            if (sourceArrayElementType.IsPrimitive)
            {
                if (sourceArrayElementType.IsBoolean())
                {
                    convertMethodName = "AsBoolEnumerable";
                }
                else if (sourceArrayElementType.IsByte())
                {
                    convertMethodName = "AsByteEnumerable";
                }
                else if (sourceArrayElementType.IsSByte())
                {
                    convertMethodName = "AsSByteEnumerable";
                }
                else if (sourceArrayElementType.IsChar())
                {
                    convertMethodName = "AsCharEnumerable";
                }
                else if (sourceArrayElementType.IsInt16())
                {
                    convertMethodName = "AsInt16Enumerable";
                }
                else if (sourceArrayElementType.IsUInt16())
                {
                    convertMethodName = "AsUInt16Enumerable";
                }
                else if (sourceArrayElementType.IsInt32())
                {
                    convertMethodName = "AsInt32Enumerable";
                }
                else if (sourceArrayElementType.IsUInt32())
                {
                    convertMethodName = "AsUInt32Enumerable";
                }
                else if (sourceArrayElementType.IsInt64())
                {
                    convertMethodName = "AsInt64Enumerable";
                }
                else if (sourceArrayElementType.IsFloat())
                {
                    convertMethodName = "AsFloatEnumerable";
                }
                else if (sourceArrayElementType.IsDouble())
                {
                    convertMethodName = "AsDoubleEnumerable";
                }
                else
                {
                    throw new ArgumentOutOfRangeException("Unknown primitive array element type " + sourceArrayElementType);
                }
            }
            return(convertMethodName);
        }
        /// <summary>
        /// Convert the result of the given node to uint8/uint16 if needed.
        /// </summary>
        private static void ConvertIfNeeded(AstExpression node, XTypeReference valueType, AstExpression parent)
        {
            AstCode convCode;

            if (valueType.IsByte())
            {
                // Convert from int to uint8
                convCode = AstCode.Int_to_ubyte;
            }
            else if (valueType.IsUInt16())
            {
                // Convert from int to uint16
                convCode = AstCode.Int_to_ushort;
            }
            else if (valueType.IsChar())
            {
                // Convert from int to uint16
                convCode = AstCode.Conv_U2;
            }
            else
            {
                // Do not convert
                return;
            }

            // Avoid recursion
            if ((parent != null) && (parent.Code == convCode))
            {
                return;
            }

            // Copy load expression
            var clone = new AstExpression(node);

            // Convert node
            node.Code = convCode;
            node.Arguments.Clear();
            node.Arguments.Add(clone);
            node.Operand      = null;
            node.InferredType = valueType;

            // keep the expected type!
            node.ExpectedType  = clone.ExpectedType;
            clone.ExpectedType = valueType;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Generate an Add opcode.
        /// </summary>
        private static RCode OpcodeForType(XTypeReference type, RCode[] opcodes)
        {
            if (type.IsInt32() || type.IsUInt32() || type.IsInt16() || type.IsUInt16() || type.IsChar() || type.IsByte() || type.IsSByte() || type.IsBoolean())
            {
                return(opcodes[0]);
            }
            if (type.IsInt64() || type.IsUInt64())
            {
                return(opcodes[1]);
            }
            if (type.IsFloat())
            {
                return(opcodes[2]);
            }
            if (type.IsDouble())
            {
                return(opcodes[3]);
            }

            XTypeDefinition typeDef;

            if (type.TryResolve(out typeDef))
            {
                if (typeDef.IsEnum)
                {
                    return(OpcodeForType(typeDef.GetEnumUnderlyingType(), opcodes));
                }
            }

            throw new ArgumentException("Unsupported type " + type);
        }