public static bool NeedsLoopScope(this Statement stmt, LoopBlock loop)
        {
            LoopControlDetector lcd = new LoopControlDetector(loop);

            stmt.Accept(lcd);
            return(lcd.Result);
        }
        public void GroupSelectedBlocks()
        {
            if (!SelectedBlocks.Any())
            {
                return;
            }

            var          relatedSegments = Enumerable.Select(SelectedBlocks, b => b.SegmentContext).Distinct().ToList();
            MusicSegment segmentForGroup = (relatedSegments.Count == 1 ? relatedSegments.Single().GetModel() : model.MusicSegments[0]);

            var group = new LoopBlock(model);

            group.SegmentContext = segmentForGroup;
            group.StartTime      = SelectedBlocks.Min(b => b.StartTime);
            foreach (var b in SelectedBlocks)
            {
                // create an independent copy of the block so transforming its time to local reference frame does not screw up undo
                Block newChild = Block.FromXML(model, b.GetModel().ToXML());

                group.AddChild(newChild, true);
            }

            using (ActionManager.CreateTransaction())
            {
                DeleteSelectedBlocks();
                ActionManager.RecordAdd(model.Blocks, group);
            }

            SelectBlock(BlockViewModel.FromModel(this, group), CompositionMode.None);
        }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     Check(stmt);
     stmt.Body.Accept(this);
     if (stmt.Trailer != null)
         stmt.Trailer.Accept(this);
 }
Beispiel #4
0
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     stmt.Body.Accept(this);
     if (stmt.Trailer != null)
     {
         stmt.Trailer.Accept(this);
     }
 }
Beispiel #5
0
        public void AcceptLoopBlock(LoopBlock stmt)
        {
            BranchLabel header = CreateLabelForNextInstruction(stmt);

            CloseBB();
            stmt.Body.Accept(this);
            Emit(ISet.Goto(header), stmt, CloseBB(), 0);
            BeginBB();
        }
Beispiel #6
0
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     _loopMap[stmt] = Loop();
     {
         stmt.Body.Accept(this);
     }
     EndLoop();
     LabelLastStmt(stmt);
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     stmt.Body = stmt.Body.Forify();
     if (stmt.Trailer != null)
     {
         stmt.Trailer = stmt.Trailer.Forify();
     }
     Result = stmt;
 }
Beispiel #8
0
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     _loopMap[stmt] = Loop();
     {
         stmt.Body.Accept(this);
     }
     EndLoop();
     LabelLastStmt(stmt);
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     _loopStack.Push(stmt);
     stmt.Body.Accept(this);
     _loopStack.Pop();
     if (stmt.Trailer != null)
     {
         stmt.Trailer.Accept(this);
     }
 }
Beispiel #10
0
 /// <summary>
 /// Transforms a loop block. The default implementation clones the loop.
 /// </summary>
 /// <param name="stmt">loop block statement</param>
 public virtual void AcceptLoopBlock(LoopBlock stmt)
 {
     _loopMap[stmt] = Loop();
     {
         stmt.Body.Accept(this);
     }
     EndLoop();
     CopyAttributesToLastStatement(stmt);
     LabelLastStmt(stmt);
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     stmt.Body.Accept(this);
     if (stmt.CounterVariable != null)
     {
         RequireType(stmt.CounterVariable.Type, _design);
     }
     if (stmt.HeadCondition != null)
     {
         Resolve(stmt.HeadCondition);
     }
     if (stmt.Initializer != null)
     {
         stmt.Initializer.Accept(this);
     }
     if (stmt.Trailer != null)
     {
         stmt.Trailer.Accept(this);
     }
 }
        public void AcceptCompoundStatement(CompoundStatement stmt)
        {
            LoopBlock forLoop = stmt.AsForLoop(EForLoopLevel.Strict);

            if (forLoop != null)
            {
                Result = forLoop;
                if (forLoop.Trailer != null)
                {
                    forLoop.Trailer = forLoop.Trailer.Forify();
                }
            }
            else
            {
                for (int i = 0; i < stmt.Statements.Count; i++)
                {
                    stmt.Statements[i] = stmt.Statements[i].Forify();
                }
                Result = stmt;
            }
        }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     IsEmpty = false;
 }
 public LoopControlDetector(LoopBlock loop)
 {
     _loop = loop;
     Result = false;
 }
 /// <summary>
 /// Transforms a loop block. The default implementation clones the loop.
 /// </summary>
 /// <param name="stmt">loop block statement</param>
 public virtual void AcceptLoopBlock(LoopBlock stmt)
 {
     _loopMap[stmt] = Loop();
     {
         stmt.Body.Accept(this);
     }
     EndLoop();
     CopyAttributesToLastStatement(stmt);
     LabelLastStmt(stmt);
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     throw new InvalidOperationException();
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
 }
        public void AcceptLoopBlock(LoopBlock stmt)
        {
            IfStatement cond = stmt.Body.AsSingleStatement() as IfStatement;

            if (cond == null)
            {
                return;
            }

            if (cond.Conditions.Count != 1 ||
                cond.Branches.Count != 2)
            {
                return;
            }

            IList <Statement> trueBranch = cond.Branches[0].AsStatementList();

            if (trueBranch.Count == 0 ||
                !trueBranch.Last().Equals(new ContinueLoopStatement()
            {
                Loop = stmt
            }))
            {
                return;
            }

            IList <Statement> falseBranch = cond.Branches[1].AsStatementList();

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

#if false
            BreakLoopStatement breaker = new BreakLoopStatement()
            {
                Loop = stmt
            };
            Statement trailer = cond.Branches[1].Clone;
            trailer.RemoveAll(breaker);
#endif

            Statement         trailer = cond.Branches[1].Clone;
            BreakLoopReplacer blr     = new BreakLoopReplacer(stmt);
            trailer.Accept(blr);

            if (trailer.NeedsLoopScope(stmt))
            {
                return;
            }
#if false
            CompoundStatement trailer = new CompoundStatement();
            trailer.Statements.AddRange(falseBranch.Take(falseBranch.Count - 1));
            if (trailer.NeedsLoopScope(stmt))
            {
                return;
            }

            BreakLoopStatement breaker = falseBranch.Last() as BreakLoopStatement;
            if (breaker == null)
            {
                return;
            }

            if (breaker.Loop.IsAncestor(stmt))
            {
                /* Consider the following situation:
                 *
                 * L1: loop
                 *   some outer loop work
                 *   L2: loop
                 *     if someCondition then
                 *       do something
                 *       continue L2
                 *     else
                 *       do something different
                 *       break L1
                 *     end if
                 *   end loop L2
                 * end loop L1
                 *
                 * As the inner break statement breaks the outer loop, this loop
                 * must be transformed into the following code:
                 *
                 * L1: loop
                 *   some outer loop work
                 *   while someCondition loop
                 *     do something
                 *   end while
                 *   do something different
                 *   break L1
                 * end loop
                 * */

                if (breaker.Loop != stmt)
                {
                    trailer.Statements.Add(breaker);
                }
            }
            else
            {
                return;
            }
#endif

            CompoundStatement newBody = new CompoundStatement();
            newBody.Statements.AddRange(trueBranch.Take(trueBranch.Count - 1));

            LoopBlock whileBlock = (LoopBlock)stmt.Clone;
            whileBlock.HeadCondition = cond.Conditions[0];
            whileBlock.Body          = newBody;
            whileBlock.Trailer       = trailer;

            Result = whileBlock;
        }
 public BreakLoopReplacer(LoopBlock offScopeLoop)
 {
     _offScopeLoop = offScopeLoop;
 }
Beispiel #20
0
 public void Break(LoopBlock loop)
 {
     throw new NotSupportedException();
 }
        public void AcceptLoopBlock(LoopBlock stmt)
        {
            IfStatement cond = stmt.Body.AsSingleStatement() as IfStatement;
            if (cond == null)
                return;

            if (cond.Conditions.Count != 1 ||
                cond.Branches.Count != 2)
                return;

            IList<Statement> trueBranch = cond.Branches[0].AsStatementList();
            if (trueBranch.Count == 0 ||
                !trueBranch.Last().Equals(new ContinueLoopStatement() { Loop = stmt }))
                return;

            IList<Statement> falseBranch = cond.Branches[1].AsStatementList();
            if (falseBranch.Count == 0)
                return;

#if false
            BreakLoopStatement breaker = new BreakLoopStatement() { Loop = stmt };
            Statement trailer = cond.Branches[1].Clone;
            trailer.RemoveAll(breaker);
#endif

            Statement trailer = cond.Branches[1].Clone;
            BreakLoopReplacer blr = new BreakLoopReplacer(stmt);
            trailer.Accept(blr);

            if (trailer.NeedsLoopScope(stmt))
                return;
#if false
            CompoundStatement trailer = new CompoundStatement();
            trailer.Statements.AddRange(falseBranch.Take(falseBranch.Count - 1));
            if (trailer.NeedsLoopScope(stmt))
                return;

            BreakLoopStatement breaker = falseBranch.Last() as BreakLoopStatement;
            if (breaker == null)
                return;

            if (breaker.Loop.IsAncestor(stmt))
            {
                /* Consider the following situation:
                 * 
                 * L1: loop
                 *   some outer loop work
                 *   L2: loop
                 *     if someCondition then
                 *       do something
                 *       continue L2
                 *     else
                 *       do something different
                 *       break L1
                 *     end if
                 *   end loop L2
                 * end loop L1
                 * 
                 * As the inner break statement breaks the outer loop, this loop
                 * must be transformed into the following code:
                 * 
                 * L1: loop
                 *   some outer loop work
                 *   while someCondition loop
                 *     do something
                 *   end while
                 *   do something different
                 *   break L1
                 * end loop
                 * */

                if (breaker.Loop != stmt)
                {
                    trailer.Statements.Add(breaker);
                }
            }
            else
                return;
#endif

            CompoundStatement newBody = new CompoundStatement();
            newBody.Statements.AddRange(trueBranch.Take(trueBranch.Count - 1));

            LoopBlock whileBlock = (LoopBlock)stmt.Clone;
            whileBlock.HeadCondition = cond.Conditions[0];
            whileBlock.Body = newBody;
            whileBlock.Trailer = trailer;

            Result = whileBlock;
        }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     _loopStack.Push(stmt);
     stmt.Body.Accept(this);
     _loopStack.Pop();
     if (stmt.Trailer != null)
         stmt.Trailer.Accept(this);
 }
 public BreakLoopReplacer(LoopBlock offScopeLoop)
 {
     _offScopeLoop = offScopeLoop;
 }
 public static bool NeedsLoopScope(this Statement stmt, LoopBlock loop)
 {
     LoopControlDetector lcd = new LoopControlDetector(loop);
     stmt.Accept(lcd);
     return lcd.Result;
 }
 public LoopControlDetector(LoopBlock loop)
 {
     _loop  = loop;
     Result = false;
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     IsEmpty = false;
 }
Beispiel #28
0
 void Awake()
 {
     _loopBlock = GetComponent <LoopBlock>();
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     throw new InvalidOperationException();            
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     Result = stmt;
 }
Beispiel #31
0
 public override void AcceptLoopBlock(LoopBlock stmt)
 {
     Success = false;
 }
        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;
        }
Beispiel #33
0
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     SimpleResult(stmt);
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     SimpleResult(stmt);
 }
Beispiel #35
0
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     Result = stmt;
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     stmt.Body.Accept(this);
 }
 public void AcceptLoopBlock(LoopBlock stmt)
 {
     stmt.Body.Accept(this);
 }
            public void AcceptLoopBlock(LoopBlock stmt)
            {
                if (stmt.Initializer != null &&
                    stmt.CounterStart != null &&
                    stmt.CounterStop != null &&
                    stmt.CounterStep != null)
                {
                    if (/*stmt.CounterStart.IsConst(_ecr) &&
                         * stmt.CounterStop.IsConst(_ecr) &&*/
                        stmt.CounterStep.IsConst(_ecr))
                    {
                        try
                        {
                            Expression minExpr, maxExpr;
                            switch (stmt.CounterDirection)
                            {
                            case LoopBlock.ECounterDirection.Increment:
                                minExpr = stmt.CounterStart;
                                maxExpr = stmt.CounterStop + stmt.CounterStep;
                                if (stmt.CounterLimitKind == LoopBlock.ELimitKind.ExcludingStopValue)
                                {
                                    maxExpr -= LiteralReference.CreateConstant((long)1);
                                }
                                break;

                            case LoopBlock.ECounterDirection.Decrement:
                                maxExpr = stmt.CounterStart;
                                minExpr = stmt.CounterStop - stmt.CounterStep;
                                if (stmt.CounterLimitKind == LoopBlock.ELimitKind.ExcludingStopValue)
                                {
                                    minExpr += LiteralReference.CreateConstant((long)1);
                                }
                                break;

                            default:
                                throw new NotImplementedException();
                            }

                            object  min       = minExpr.Eval(_eval);
                            object  max       = maxExpr.Eval(_eval);
                            IVRange minR      = IVRange.ToRange(min);
                            IVRange maxR      = IVRange.ToRange(max);
                            IVRange loopRange = IVRange.Max(minR, maxR);

                            if (_iva._ivLoopRange.ContainsKey(stmt.CounterVariable))
                            {
                                _iva._ivLoopRange[stmt.CounterVariable] =
                                    IVRange.Max(_iva._ivLoopRange[stmt.CounterVariable], loopRange);
                            }
                            else
                            {
                                _iva._ivLoopRange[stmt.CounterVariable] = loopRange;
                            }
                            _iva._inductionVars.Add(stmt.CounterVariable, stmt);
                            _ivStack.Push(stmt.CounterVariable);
                            stmt.Body.Accept(this);
                            if (stmt.Trailer != null)
                            {
                                stmt.Trailer.Accept(this);
                            }
                            _ivStack.Pop();
                            return;
                        }
                        catch (BreakEvaluationException)
                        {
                        }
                        catch (InvalidCastException)
                        {
                        }
                        catch (InvalidOperationException)
                        {
                        }
                    }
                    _iva._unconstrainedVars.Add(stmt.CounterVariable);
                }

                stmt.Body.Accept(this);
                if (stmt.Trailer != null)
                {
                    stmt.Trailer.Accept(this);
                }
            }
Beispiel #39
0
 public void Continue(LoopBlock loop)
 {
     throw new NotSupportedException();
 }