Beispiel #1
0
        private static VariableBridge KnownSourceSize(RewriteDesign design, ValueBridge currentLength, VariableBridge resultVariable, int enlarging)
        {
            var logVariable = CreateGlobalVariable(design, Int,
                                                   "LinqRewrite".Access("Core", "IntExtensions", "Log2")
                                                   .Invoke(Parenthesize(design.SourceSize).Cast(SyntaxKind.UIntKeyword)) - Constants.MinArraySizeLog);

            if (enlarging != 1)
            {
                design.PreUseAdd(logVariable.Assign(
                                     SyntaxFactory.ConditionalExpression(logVariable.GThan(enlarging),
                                                                         logVariable - logVariable % enlarging,
                                                                         IntValue(enlarging))));
            }
            else
            {
                design.PreUseAdd(logVariable.Assign(
                                     SyntaxFactory.ConditionalExpression(logVariable.GThan(1),
                                                                         logVariable,
                                                                         IntValue(1))));
            }

            var currentLengthVariable = CreateGlobalVariable(design, Int, currentLength);

            design.ForAdd(If(design.Indexer >= currentLengthVariable,
                             "LinqRewrite".Access("Core", "EnlargeExtensions", "LogEnlargeArray")
                             .Invoke(enlarging,
                                     design.SourceSize,
                                     RefArg(resultVariable),
                                     RefArg(logVariable),
                                     OutArg(currentLengthVariable))));

            design.ForAdd(resultVariable[design.Indexer].Assign(design.LastValue));
            return(resultVariable);
        }
Beispiel #2
0
        private static void CheckBounds(RewriteDesign design, ref RewrittenValueBridge takeValue)
        {
            if (design.Unchecked)
            {
                return;
            }
            var takeIntPass   = TryGetInt(takeValue, out var takeInt);
            var resultIntPass = TryGetInt(design.ResultSize, out var resultInt);

            if (takeIntPass)
            {
                if (takeInt < 0)
                {
                    takeValue = new RewrittenValueBridge(IntValue(0));
                }
                if (!resultIntPass)
                {
                    if (design.ResultSize == null)
                    {
                        return;
                    }
                    var takeVariable = CreateGlobalVariable(design, Int);
                    design.PreUseAdd(If(takeValue > design.ResultSize,
                                        takeVariable.Assign(design.ResultSize),
                                        takeVariable.Assign(takeValue)));
                    takeValue = new RewrittenValueBridge(takeVariable);
                }
                else if (takeInt > resultInt)
                {
                    takeValue = new RewrittenValueBridge(design.ResultSize);
                }
            }
            else if (design.ResultSize != null)
            {
                var takeVariable = CreateGlobalVariable(design, Int);
                design.PreUseAdd(If(takeValue < 0, takeVariable.Assign(IntValue(0)),
                                    If(takeValue > design.ResultSize, takeVariable.Assign(design.ResultSize),
                                       takeVariable.Assign(takeValue))));
                takeValue = new RewrittenValueBridge(takeVariable);
            }
            else
            {
                var takeVariable = CreateGlobalVariable(design, Int);
                design.PreUseAdd(If(takeValue < 0, takeVariable.Assign(IntValue(0)),
                                    takeVariable.Assign(takeValue)));
                takeValue = new RewrittenValueBridge(takeVariable);
            }
        }
Beispiel #3
0
        public static bool AssertLesser(RewriteDesign design, ValueBridge smaller, ValueBridge bigger, bool initialCheck = true, bool preCheck = false)
        {
            if (design.Unchecked)
            {
                return(true);
            }
            bool biggerPass  = ExpressionSimplifier.TryGetDouble(bigger, out double biggerD);
            bool smallerPass = ExpressionSimplifier.TryGetDouble(smaller, out double smallerD);

            if (biggerPass && smallerPass)
            {
                if (smallerD < biggerD)
                {
                    return(true);
                }
                InitialError(design, "System.InvalidOperationException", "Index out of range");
                return(false);
            }
            if (preCheck)
            {
                return(true);
            }
            if (initialCheck)
            {
                design.PreUseAdd(If(smaller >= bigger, Throw("System.InvalidOperationException", "Index out of range")));
            }
            else
            {
                design.FinalAdd(If(smaller >= bigger, Throw("System.InvalidOperationException", "Index out of range")));
            }
            return(true);
        }
        public static LocalVariable CreateGlobalVariable(RewriteDesign design, TypeSyntax type, ValueBridge initial)
        {
            string variable = "v" + VariableIndex++ % Constants.VariablesPeek;
            var    created  = new LocalVariable(variable, type)
            {
                IsGlobal = true, IsUsed = true
            };

            design.Variables.Add(created);

            design.InitialAdd(LocalVariableCreation(variable, type));
            design.PreUseAdd(((ValueBridge)variable).Assign(initial));
            design.SimpleEnumeration = false;
            return(created);
        }
Beispiel #5
0
 public static bool AssertNotNull(RewriteDesign design, ValueBridge notNull, bool preCheck = false)
 {
     if (design.Unchecked)
     {
         return(true);
     }
     if (notNull.ToString() == "null")
     {
         InitialError(design, "System.InvalidOperationException", "Invalid null object");
         return(false);
     }
     if (preCheck)
     {
         return(true);
     }
     design.PreUseAdd(If(notNull.IsEqual(null), Throw("System.InvalidOperationException", "Invalid null object")));
     return(true);
 }
        public static LocalVariable CreateLocalVariable(RewriteDesign design, TypeBridge type, ValueBridge initial)
        {
            string variable      = "v" + VariableIndex++ % Constants.VariablesPeek;
            var    stringType    = type.ToString();
            var    foundVariable = design.Variables.FirstOrDefault(x => stringType == x.Type.ToString() && !x.IsGlobal && !x.IsUsed);

            if (foundVariable != null)
            {
                foundVariable.IsUsed = true;
                return(foundVariable);
            }
            var created = new LocalVariable(variable, type);

            design.Variables.Add(created);

            design.InitialAdd(LocalVariableCreation(variable, type));
            design.PreUseAdd(((ValueBridge)variable).Assign(initial));
            design.SimpleEnumeration = false;
            return(created);
        }
Beispiel #7
0
        private static void KnownSourceSize(RewriteDesign design)
        {
            design.Indexer = null;
            var reverseIndexerVariable = CreateGlobalVariable(design, Int, 8);
            var logVariable            = CreateGlobalVariable(design, Int,
                                                              "LinqRewrite".Access("Core", "IntExtensions", "Log2")
                                                              .Invoke(design.SourceSize.Cast(SyntaxKind.UIntKeyword)) - 3);

            design.PreUseAdd(logVariable.SubAssign(logVariable % 2));
            var currentLengthVariable = CreateGlobalVariable(design, Int, 8);
            var resultVariable        = CreateGlobalVariable(design, design.LastValue.ArrayType(), CreateArray(design.LastValue.ArrayType(), 8));

            var tmpSize = CreateGlobalVariable(design, Int);

            design.ForAdd(reverseIndexerVariable.PreDecrement());
            design.ForAdd(If(reverseIndexerVariable < 0,
                             Block(
                                 tmpSize.Assign(currentLengthVariable),
                                 "LinqRewrite".Access("Core", "EnlargeExtensions", "LogEnlargeReverseArray")
                                 .Invoke(2,
                                         design.SourceSize,
                                         RefArg(resultVariable),
                                         RefArg(logVariable),
                                         OutArg(currentLengthVariable)),
                                 reverseIndexerVariable.Assign(currentLengthVariable - tmpSize - 1))));

            design.ForAdd(resultVariable[reverseIndexerVariable].Assign(design.LastValue));

            design.Indexer = null;
            design.Iterators.All.ForEach(x => x.Complete = true);
            design.AddIterator(new CollectionValueBridge(design, resultVariable.Type, design.LastValue.Type, resultVariable, true));
            design.CurrentCollection = design.CurrentIterator.Collection;
            RewriteCollectionEnumeration.RewriteOther(design, design.CurrentCollection);

            design.ResultSize = design.SourceSize = currentLengthVariable - reverseIndexerVariable;
            design.CurrentIterator.ForFrom = reverseIndexerVariable;
            design.CurrentIterator.ForTo   = currentLengthVariable - 1;
            design.Indexer = null;
        }