protected virtual AstNode _Optimize(AstNodeExprCast Cast)
        {
            //Console.WriteLine("Optimize.AstNodeExprCast: {0} : {1}", Cast.CastedType, Cast.Expr);

            // Dummy cast
            if (Cast.CastedType == Cast.Expr.Type)
            {
                //Console.WriteLine("Dummy Cast");
                return(Cast.Expr);
            }
            // Double Cast
            else if (Cast.Expr is AstNodeExprCast)
            {
                //Console.WriteLine("Double Cast");
                var FirstCastType  = (Cast.Expr as AstNodeExprCast).CastedType;
                var SecondCastType = Cast.CastedType;
                if (FirstCastType.IsPrimitive && SecondCastType.IsPrimitive)
                {
                    if (AstUtils.GetTypeSize(FirstCastType) >= AstUtils.GetTypeSize(SecondCastType))
                    {
                        return(Optimize(new AstNodeExprCast(Cast.CastedType, (Cast.Expr as AstNodeExprCast).Expr)));
                    }
                }
            }
            // Cast to immediate
            else if (Cast.Expr is AstNodeExprImm)
            {
                //Console.WriteLine("Cast to immediate");
                return(new AstNodeExprImm(AstUtils.CastType((Cast.Expr as AstNodeExprImm).Value, Cast.CastedType)));
            }

            return(Cast);
        }
        protected virtual void _Generate(AstNodeExprCast cast)
        {
            var castedType = cast.CastedType;

            Generate(cast.Expr);

            if (cast.Explicit)
            {
                _GenerateCastToType(castedType);
            }
        }
 protected virtual void _Generate(AstNodeExprCast cast)
 {
     if (cast.Explicit)
     {
         Output.Write("(");
         Output.Write("(" + cast.CastedType.Name + ")");
         Generate(cast.Expr);
         Output.Write(")");
     }
     else
     {
         Generate(cast.Expr);
     }
 }
Exemple #4
0
        protected virtual AstNode _Optimize(AstNodeExprCast cast)
        {
            //Console.WriteLine("Optimize.AstNodeExprCast: {0} : {1}", Cast.CastedType, Cast.Expr);

            // Dummy cast
            if (cast.CastedType == cast.Expr.Type)
            {
                //Console.WriteLine("Dummy Cast");
                return(cast.Expr);
            }

            // Double Cast
            var expr = cast.Expr as AstNodeExprCast;

            if (expr != null)
            {
                //Console.WriteLine("Double Cast");
                var firstCastType  = expr.CastedType;
                var secondCastType = cast.CastedType;
                if (firstCastType.IsPrimitive && secondCastType.IsPrimitive)
                {
                    if (AstUtils.GetTypeSize(firstCastType) >= AstUtils.GetTypeSize(secondCastType))
                    {
                        return(Optimize(new AstNodeExprCast(cast.CastedType, expr.Expr)));
                    }
                }

                return(cast);
            }

            // Cast to immediate
            var imm = cast.Expr as AstNodeExprImm;

            if (imm != null)
            {
                //Console.WriteLine("Cast to immediate");
                return(new AstNodeExprImm(AstUtils.CastType(imm.Value, cast.CastedType)));
            }

            return(cast);
        }