Exemple #1
0
        public object Neg(object v, TypeDescriptor resultType)
        {
            object result;

            if (TryCallIntrinsicOp("op_UnaryNegation", out result, v))
            {
                return(result);
            }

            TypeDescriptor  nt   = TypeDescriptor.GetTypeOf(v);
            EResultCategory rcat = GetResultCategory(nt);

            switch (rcat)
            {
            case EResultCategory.SignedIntegral:
            {
                long l = TypeConversions.ToLong(v);
                return(-l);
            }

            case EResultCategory.UnsignedIntegral:
            {
                throw new ArgumentException();
            }

            case EResultCategory.FloatingPoint:
            {
                double d = TypeConversions.ToDouble(v);
                return(-d);
            }

            default:
                throw new NotImplementedException();
            }
        }
Exemple #2
0
 public object Slice(object v, object lo, object hi, TypeDescriptor resultType)
 {
     /*if (v is FixPointValue)
      * {
      *  FixPointValue fpv = (FixPointValue)v;
      *  BigInteger bi = fpv.UnscaledValue;
      *  string bin = bi.ToString(2);
      *  int ilo = (int)lo;
      *  int ihi = (int)hi;
      *  string slice = bin.Substring(ilo, ihi - ilo + 1);
      *  BigInteger bir = new BigInteger(slice, 2);
      *  return new FixPointValue(bir, false, ihi - ilo + 1, 0);
      * }
      * else*/if (v is long)
     {
         long lv  = (long)v;
         int  ilo = (int)TypeConversions.ToLong(lo);
         int  ihi = (int)TypeConversions.ToLong(hi);
         lv >>= ilo;
         //long mask = (1L << (ihi - ilo + 1)) - 1;
         //lv &= mask;
         return(lv);
     }
     else
     {
         throw new NotImplementedException();
     }
 }
Exemple #3
0
        /// <summary>
        /// Converts this instance to a fixed array reference.
        /// </summary>
        public FixedArrayRef AsFixed()
        {
            LiteralReference lr = ArrayExpr as LiteralReference;

            if (lr == null)
            {
                return(null);
            }
            object obj;

            if (!lr.ReferencedObject.IsConst(out obj))
            {
                return(null);
            }
            Array array = obj as Array;

            if (array == null)
            {
                return(null);
            }
            long[]     constIndices = new long[Indices.Length];
            IEvaluator eval         = new DefaultEvaluator();

            for (int i = 0; i < Indices.Length; i++)
            {
                Expression index = Indices[i];
                if (!index.IsConst())
                {
                    constIndices = null;
                    break;
                }
                constIndices[i] = TypeConversions.ToLong(index.Eval(eval));
            }
            return(new FixedArrayRef(lr.ReferencedObject, array, constIndices));
        }
Exemple #4
0
        public static object DefaultEval(ArrayRef arrayRef, IEvaluator eval)
        {
            Array array = (Array)arrayRef.ArrayExpr.Eval(eval);

            long[] indices = arrayRef.Indices.Select(i =>
                                                     TypeConversions.ToLong(i.Eval(eval))).ToArray();
            return(array.GetValue(indices));
        }
 public static IVRange ToRange(object obj)
 {
     if (obj is IVRange)
     {
         return((IVRange)obj);
     }
     else
     {
         long v = TypeConversions.ToLong(obj);
         return(new IVRange(v, v));
     }
 }
Exemple #6
0
        public object Div(object v1, object v2, TypeDescriptor resultType)
        {
            object oresult;

            if (TryCallIntrinsicOp("op_Division", out oresult, v1, v2))
            {
                return(oresult);
            }

            TypeDescriptor  t1   = TypeDescriptor.GetTypeOf(v1);
            TypeDescriptor  t2   = TypeDescriptor.GetTypeOf(v2);
            EResultCategory rcat = GetResultCategory(t1);

            switch (rcat)
            {
            case EResultCategory.SignedIntegral:
            {
                long l1 = TypeConversions.ToLong(v1);
                long l2 = TypeConversions.ToLong(v2);
                return(l1 / l2);
            }

            case EResultCategory.UnsignedIntegral:
            {
                ulong u1 = TypeConversions.ToULong(v1);
                ulong u2 = TypeConversions.ToULong(v2);
                return(u1 / u2);
            }

            case EResultCategory.FloatingPoint:
            {
                double d1 = TypeConversions.ToDouble(v1);
                double d2 = TypeConversions.ToDouble(v2);
                return(d1 / d2);
            }

            default:
                throw new NotImplementedException();
            }
        }
Exemple #7
0
        public object LShift(object v1, object v2, TypeDescriptor resultType)
        {
            object oresult;

            if (TryCallIntrinsicOp("op_LeftShift", out oresult, v1, v2))
            {
                return(oresult);
            }

            TypeDescriptor  t1   = TypeDescriptor.GetTypeOf(v1);
            TypeDescriptor  t2   = TypeDescriptor.GetTypeOf(v2);
            EResultCategory rcat = GetResultCategory(t1);

            switch (rcat)
            {
            case EResultCategory.SignedIntegral:
            {
                long v = TypeConversions.ToLong(v1);
                int  s = (int)TypeConversions.ToLong(v2);
                return(v << s);
            }

            case EResultCategory.UnsignedIntegral:
            {
                ulong v = TypeConversions.ToULong(v1);
                int   s = (int)TypeConversions.ToLong(v2);
                return(v << s);
            }

            case EResultCategory.FloatingPoint:
                throw new ArgumentException();

            default:
                throw new NotImplementedException();
            }
        }
        public void AcceptCompoundStatement(CompoundStatement stmt)
        {
            if (stmt.Statements.Count != 2)
            {
                return;
            }

            StoreStatement initializer = stmt.Statements[0] as StoreStatement;
            LoopBlock      loop        = stmt.Statements[1].AsWhileLoop();

            if (initializer == null || loop == null)
            {
                return;
            }

            IList <Statement> body = loop.Body.AsStatementList();

            if (body.Count == 0)
            {
                return;
            }

            StoreStatement step = body.Last() as StoreStatement;

            if (step == null)
            {
                return;
            }

            CompoundStatement newBody = new CompoundStatement();

            newBody.Statements.AddRange(body.Take(body.Count - 1));

            loop.Initializer = initializer;
            loop.Body        = newBody;
            loop.Step        = step;

            if (Level == EForLoopLevel.Strict ||
                Level == EForLoopLevel.StrictOneInc)
            {
                StoreStatement initStore = initializer;

                if (initStore.Container == null)
                {
                    return;
                }

                Variable counterVar = initStore.Container as Variable;
                if (counterVar == null)
                {
                    return;
                }

                loop.CounterVariable = counterVar;

                Type counterType = counterVar.Type.CILType;
                if (!counterType.IsEnumerable())
                {
                    return;
                }

                if (loop.Body.Modifies(counterVar))
                {
                    return;
                }

                loop.CounterStart = initStore.Value;

                if (step.Container == null)
                {
                    return;
                }

                if (!step.Container.Equals(counterVar))
                {
                    return;
                }

                Expression stepExpr = step.Value;

                Matching x       = new Matching();
                Matching mctr    = (LiteralReference)counterVar;
                Matching mctrInc = mctr + x;
                Matching mctrDec = mctr - x;
                if (((Expression.MatchFunction)mctrInc)(stepExpr))
                {
                    loop.CounterStep      = x.Result;
                    loop.CounterDirection = LoopBlock.ECounterDirection.Increment;
                }
                else if (((Expression.MatchFunction)mctrDec)(stepExpr))
                {
                    loop.CounterStep      = x.Result;
                    loop.CounterDirection = LoopBlock.ECounterDirection.Decrement;
                }
                else
                {
                    return;
                }

                BinOp cmp = loop.HeadCondition as BinOp;
                if (cmp == null)
                {
                    return;
                }

                LiteralReference lhs = cmp.Children[0] as LiteralReference;
                if (lhs == null)
                {
                    return;
                }
                if (!lhs.ReferencedObject.Equals(counterVar))
                {
                    return;
                }

                loop.CounterStop = cmp.Children[1];

                switch (loop.CounterDirection)
                {
                case LoopBlock.ECounterDirection.Decrement:
                    switch (cmp.Operation)
                    {
                    case BinOp.Kind.Gt:
                    case BinOp.Kind.NEq:
                        loop.CounterLimitKind = LoopBlock.ELimitKind.ExcludingStopValue;
                        break;

                    case BinOp.Kind.GtEq:
                        loop.CounterLimitKind = LoopBlock.ELimitKind.IncludingStopValue;
                        break;

                    default:
                        return;
                    }
                    break;

                case LoopBlock.ECounterDirection.Increment:
                    switch (cmp.Operation)
                    {
                    case BinOp.Kind.Lt:
                    case BinOp.Kind.NEq:
                        loop.CounterLimitKind = LoopBlock.ELimitKind.ExcludingStopValue;
                        break;

                    case BinOp.Kind.LtEq:
                        loop.CounterLimitKind = LoopBlock.ELimitKind.IncludingStopValue;
                        break;

                    default:
                        return;
                    }
                    break;
                }

                if (Level == EForLoopLevel.StrictOneInc)
                {
                    object inc;
                    try
                    {
                        inc = loop.CounterStep.Eval(new DefaultEvaluator());
                    }
                    catch (BreakEvaluationException)
                    {
                        return;
                    }
                    if (inc == null)
                    {
                        return;
                    }
                    long stepValue = TypeConversions.ToLong(inc);
                    if (stepValue == 1)
                    {
                        switch (loop.CounterDirection)
                        {
                        case LoopBlock.ECounterDirection.Increment:
                            loop.CounterDirection = LoopBlock.ECounterDirection.IncrementOne;
                            break;

                        case LoopBlock.ECounterDirection.Decrement:
                            loop.CounterDirection = LoopBlock.ECounterDirection.DecrementOne;
                            break;
                        }
                    }
                    else if (stepValue == -1)
                    {
                        switch (loop.CounterDirection)
                        {
                        case LoopBlock.ECounterDirection.Increment:
                            loop.CounterDirection = LoopBlock.ECounterDirection.DecrementOne;
                            break;

                        case LoopBlock.ECounterDirection.Decrement:
                            loop.CounterDirection = LoopBlock.ECounterDirection.IncrementOne;
                            break;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }

            Result = loop;
        }