Пример #1
0
 private void EvaluateWhileStatement(BoundWhileStatement node)
 {
     while ((bool)EvaluateExpression(node.Condition))
     {
         EvaluateStatement(node.Body);
     }
 }
Пример #2
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            /*
             *  {
             *      let upperBound = <UpperBound>
             *      <variable> = <lowerBound>
             *      while <variable> < <UpperBound>
             *      {
             *          <statement>
             *          <variable> = <variable> + 1
             *      }
             *  }
             */

            var upperBoundSymbol      = new VariableSymbol("upperBound", true, node.UpperBound.Type, true);
            var upperBoundDeclaration = new BoundVariableDeclaration(upperBoundSymbol, node.UpperBound);
            var upperBoundExpression  = new BoundVariableExpression(upperBoundSymbol);
            var variableDeclaration   = new BoundVariableDeclaration(node.Variable, node.LowerBound); // LowerBound shouldn't be rewriten?
            var variableExpression    = new BoundVariableExpression(node.Variable);
            var conditionOperator     = BoundBinaryOperator.Bind(SyntaxKind.LessToken, TypeSymbol.Int, TypeSymbol.Int);
            var condition             = new BoundBinaryExpression(variableExpression, conditionOperator, upperBoundExpression); // UpperBound shouldn't be rewriten?

            var incrementOperator   = BoundBinaryOperator.Bind(SyntaxKind.PlusToken, node.Variable.Type, TypeSymbol.Int);
            var increment           = new BoundBinaryExpression(variableExpression, incrementOperator, new BoundLiteralExpression(1));
            var incrementAssignment = new BoundAssignmentExpression(node.Variable, increment);
            var incrementStatement  = new BoundExpressionStatement(incrementAssignment);

            var whileBlockStatement = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(node.Statement, incrementStatement));
            var whileStatement      = new BoundWhileStatement(condition, whileBlockStatement);

            var result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(upperBoundDeclaration, variableDeclaration, whileStatement));

            return(RewriteStatement(result));
        }
Пример #3
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node)
        {
            // while <condition>
            //      <body>
            //
            // ----->
            //
            // goto continue
            // body:
            // <body>
            // continue:
            // gotoTrue <condition> body
            // break:

            var bodyLabel = GenerateLabel();
            var result    = Block(
                node.Syntax,
                Goto(node.Syntax, node.ContinueLabel),
                Label(node.Syntax, bodyLabel),
                node.Body,
                Label(node.Syntax, node.ContinueLabel),
                GotoTrue(node.Syntax, bodyLabel, node.Condition),
                Label(node.Syntax, node.BreakLabel)
                );

            return(RewriteStatement(result));
        }
Пример #4
0
 public override BoundNode VisitWhileStatement(BoundWhileStatement node)
 {
     AddAll(node.Locals);
     base.VisitWhileStatement(node);
     RemoveAll(node.Locals);
     return(null);
 }
Пример #5
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node)
        {
            // goto continue
            // body:
            // <body>
            // continue:
            // goto body if <condition>
            // break:

            var bodyLabel = CreateLabel();

            var gotoContinue = new BoundGotoStatement(node.ContinueLabel, node.IsValid);
            var gotoBody     = new BoundConditionalGotoStatement(bodyLabel, node.Condition, jumpIfFalse: false, node.IsValid);


            var continueLabelStmt = new BoundLabelStatement(node.ContinueLabel, node.IsValid);
            var bodyLabelStmt     = new BoundLabelStatement(bodyLabel, node.IsValid);
            var breakLabelStmt    = new BoundLabelStatement(node.BreakLabel, node.IsValid);


            var res = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                  gotoContinue,
                                                  bodyLabelStmt,
                                                  node.Body,
                                                  continueLabelStmt,
                                                  gotoBody,
                                                  breakLabelStmt
                                                  ), node.IsValid);

            return(RewriteStatement(res));
        }
Пример #6
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            //For Statements are now all While Statements
            var variableDeclaration   = new BoundVariableDeclaration(node.Variable, node.LowerBound);
            var variableExpression    = new BoundVariableExpression(node.Variable);
            var upperBoundSymbol      = new VariableSymbol("upperBound", true, typeof(int));
            var upperBoundDeclaration = new BoundVariableDeclaration(upperBoundSymbol, node.UpperBound);
            var condition             = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualsToken, typeof(int), typeof(int)),
                new BoundVariableExpression(upperBoundSymbol)
                );
            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, typeof(int), typeof(int)),
                        new BoundLiteralExpression(1)
                        )
                    )
                );
            var whileBody      = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(node.Body, increment));
            var whileStatement = new BoundWhileStatement(condition, whileBody);
            var result         = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                             variableDeclaration,
                                                             upperBoundDeclaration,
                                                             whileStatement
                                                             ));

            return(RewriteStatement(result));
        }
Пример #7
0
 public virtual BoundExpression InstrumentWhileStatementCondition(BoundWhileStatement original, BoundExpression rewrittenCondition, SyntheticBoundNodeFactory factory)
 {
     Debug.Assert(!original.WasCompilerGenerated);
     Debug.Assert(original.Syntax.Kind() == SyntaxKind.WhileStatement);
     Debug.Assert(factory != null);
     return(rewrittenCondition);
 }
Пример #8
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node)
        {
            // while <condition>
            //      <bode>
            //
            // ----->
            //
            // goto continue

            // <body>
            // continue:
            // gotoTrue <condition> body
            // Break
            //



            var gotoContinue           = new BoundGotoStatment(node.ContinueLabel);
            var bodyLabelStatement     = new BoundLabelStatement(node.BodyLabel);
            var continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);
            var gotoTrue            = new BoundConditionalGotoStatment(node.BodyLabel, node.Condition);
            var breakLabelStatement = new BoundLabelStatement(node.BreakLabel);

            var result = new BoundBlockStatemnet(ImmutableArray.Create <BoundStatement>(
                                                     gotoContinue,
                                                     bodyLabelStatement,
                                                     node.Body,
                                                     continueLabelStatement,
                                                     gotoTrue,
                                                     breakLabelStatement
                                                     ));


            return(RewriteStatement(result));
        }
Пример #9
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node)
        {
            // while <condition> <body>
            // --- to --->
            // {
            // continue:
            // gotoFalse <condition> break
            // <body>
            // goto continue
            // break:
            // }

            var continueLabel = node.ContinueLabel;
            var breakLabel    = node.BreakLabel;

            var continueStatement  = new BoundLabelStatement(continueLabel);
            var gotoFalseStatement = new BoundConditionalGotoStatement(breakLabel, node.Condition, jumpIfTrue: false);
            var gotoContinue       = new BoundGotoStatement(continueLabel);
            var breakStatement     = new BoundLabelStatement(breakLabel);

            var result = new BoundBlockStatement(
                continueStatement,
                gotoFalseStatement,
                node.Body,
                gotoContinue,
                breakStatement);

            return(RewriteStatement(result));
        }
Пример #10
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node)
        {
            // while <condition>
            //    <body>
            //
            // --->
            //
            // goto check
            // continue:
            //    <body>
            // check:
            //    gotoTrue <condition> continue
            // break:
            //

            var checkLabel = GenerateLabel("Check");

            var gotoCheck = new BoundGotoStatement(checkLabel);
            var continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);
            var checkLabelStatement    = new BoundLabelStatement(checkLabel);
            var gotoTrue            = new BoundConditionalGotoStatement(node.ContinueLabel, node.Condition);
            var breakLabelStatement = new BoundLabelStatement(node.BreakLabel);

            var result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                     gotoCheck,
                                                     continueLabelStatement,
                                                     node.Body,
                                                     checkLabelStatement,
                                                     gotoTrue,
                                                     breakLabelStatement
                                                     ));

            return(RewriteStatement(result));
        }
Пример #11
0
 public override BoundNode VisitWhileStatement(BoundWhileStatement node)
 {
     var newLocals = RewriteLocals(node.Locals);
     BoundExpression condition = (BoundExpression)this.Visit(node.Condition);
     BoundStatement body = (BoundStatement)this.Visit(node.Body);
     return node.Update(newLocals, condition, body, node.BreakLabel, node.ContinueLabel);
 }
Пример #12
0
        protected override BoundStatement RewriteForStatement(BoundForStatement statement)
        {
            /*  for(<assignment>, <condition>, <step>) <body>
             *
             *  <assignment>
             *  while (<condition>) {
             *    body
             *    step
             *  }
             */

            var whileStatements = ImmutableArray.CreateBuilder <BoundStatement>();

            whileStatements.AddRange(new BoundStatement[] {
                statement.Body,
                new BoundLabel(statement.ContinueLabel),
                new BoundExpressionStatement(statement.Step)
            });

            var whileStatement = new BoundWhileStatement(statement.Condition, new BoundBlock(whileStatements.ToImmutable()), statement.BreakLabel, statement.ContinueLabel, false);

            var finalStatements = ImmutableArray.CreateBuilder <BoundStatement>();

            finalStatements.AddRange(new BoundStatement[] {
                statement.Assignment,
                whileStatement
            });

            return(RewriteStatement(new BoundBlock(finalStatements.ToImmutable())));
        }
Пример #13
0
 public override BoundNode VisitWhileStatement(BoundWhileStatement node)
 {
     if (IsInside)
     {
         _labelsInside.Add(node.BreakLabel);
     }
     return(base.VisitWhileStatement(node));
 }
Пример #14
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            /*
             *  for <var> = <lower> to <upper>
             *      <body>
             *
             *  ----->
             *  {
             *      var <var> = <lower>
             *      let upperBound = <upper>
             *      while (<var> <= <upperBound>)
             *      {
             *          <body>
             *          <var> = <var> + 1
             *      }
             *  }
             */
            // var <var> = <lower>
            var variableDeclaration = new BoundVariableDeclaration(node.Variable, node.LowerBound);

            // let upperBound = <upper>
            var upperBoundSymbol      = new VariableSymbol("upperBound", true, TypeSymbol.Int);
            var upperBoundDeclaration = new BoundVariableDeclaration(upperBoundSymbol, node.UpperBound);

            // <var>
            var variableExpression = new BoundVariableExpression(node.Variable);

            //<var> <= <upperBound>
            var condition = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualsToken, TypeSymbol.Int, TypeSymbol.Int),
                new BoundVariableExpression(upperBoundSymbol)
                );

            // <var> = <var> + 1
            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, TypeSymbol.Int, TypeSymbol.Int),
                        new BoundLiteralExpression(1)
                        )
                    )
                );

            var whileBlock     = new BoundBlockStatement(ImmutableArray.Create(node.Body, increment));
            var whileStatement = new BoundWhileStatement(condition, whileBlock);

            var result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                     variableDeclaration,
                                                     upperBoundDeclaration,
                                                     whileStatement));

            return(RewriteStatement(result));
        }
Пример #15
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            // for <var> = <lower> to <upper>
            //     <body>
            //
            // ---->
            // {
            //      var <var> = <lower>
            //      let upperBound = <upper>
            //      while (<var> <= upperBound)
            //      {
            //          <body>
            //          continue:
            //          <var> = <var> + 1
            //      }
            // }

            var variableDeclaration   = new BoundVariableDeclaration(node.Variable, node.LowerBound);
            var variableExpression    = new BoundVariableExpression(node.Variable);
            var upperBoundSymbol      = new LocalVariableSymbol("upperBound", true, TypeSymbol.Int);
            var upperBoundDeclaration = new BoundVariableDeclaration(upperBoundSymbol, node.UpperBound);
            var condition             = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualsToken, TypeSymbol.Int, TypeSymbol.Int),
                new BoundVariableExpression(upperBoundSymbol)
                );
            var continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);
            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, TypeSymbol.Int, TypeSymbol.Int),
                        new BoundLiteralExpression(1)
                        )
                    )
                );
            var whileBody = new BoundBlockStatement(
                ImmutableArray.Create <BoundStatement>(
                    node.Body,
                    continueLabelStatement,
                    increment
                    )
                );
            var whileStatement = new BoundWhileStatement(condition, whileBody, node.BreakLabel, GenerateLabel());
            var result         = new BoundBlockStatement(
                ImmutableArray.Create <BoundStatement>(
                    variableDeclaration,
                    upperBoundDeclaration,
                    whileStatement
                    )
                );

            return(RewriteStatement(result));
        }
Пример #16
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            var initializer            = new BoundExpressionStatement(node.Initializer);
            var loop                   = new BoundExpressionStatement(node.Loop);
            var continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);

            var whileBlock     = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(node.Body, continueLabelStatement, loop));
            var whileStatement = new BoundWhileStatement(node.Condition, whileBlock, node.BreakLabel, GenerateLabel());
            var result         = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(initializer, whileStatement));

            return(RewriteStatement(result));
        }
Пример #17
0
        protected virtual BoundStatement RewriteWhileStatement(BoundWhileStatement statement)
        {
            var cond = RewriteExpression(statement.Condition);
            var body = RewriteStatement(statement.Body);

            if (cond == statement.Condition && body == statement.Body)
            {
                return(statement);
            }

            return(new BoundWhileStatement(cond, body, statement.BreakLabel, statement.ContinueLabel));
        }
Пример #18
0
        private BoundStatement RewriteWhileStatement(
            BoundWhileStatement loop,
            ImmutableArray <LocalSymbol> locals,
            BoundExpression rewrittenCondition,
            BoundStatement rewrittenBody,
            GeneratedLabelSymbol breakLabel,
            GeneratedLabelSymbol continueLabel,
            bool hasErrors)
        {
            if (locals.IsEmpty)
            {
                return(RewriteWhileStatement(loop, rewrittenCondition, rewrittenBody, breakLabel, continueLabel, hasErrors));
            }

            // We need to enter scope-block from the top, that is where an instance of a display class will be created
            // if any local is captured within a lambda.

            // while (condition)
            //   body;
            //
            // becomes
            //
            // continue:
            // {
            //     GotoIfFalse condition break;
            //     body
            //     goto continue;
            // }
            // break:

            SyntaxNode     syntax = loop.Syntax;
            BoundStatement continueLabelStatement  = new BoundLabelStatement(syntax, continueLabel);
            BoundStatement ifNotConditionGotoBreak = new BoundConditionalGoto(rewrittenCondition.Syntax, rewrittenCondition, false, breakLabel);

            if (this.Instrument && !loop.WasCompilerGenerated)
            {
                ifNotConditionGotoBreak = _instrumenter.InstrumentWhileStatementConditionalGotoStartOrBreak(loop, ifNotConditionGotoBreak);
                continueLabelStatement  = new BoundSequencePoint(null, continueLabelStatement);
            }

            return(BoundStatementList.Synthesized(syntax, hasErrors,
                                                  continueLabelStatement,
                                                  new BoundBlock(syntax,
                                                                 locals,
                                                                 ImmutableArray.Create(
                                                                     ifNotConditionGotoBreak,
                                                                     rewrittenBody,
                                                                     new BoundGotoStatement(syntax, continueLabel))),
                                                  new BoundLabelStatement(syntax, breakLabel)));
        }
Пример #19
0
        protected override BoundStatement RewriteForToStatement(BoundForToStatement node)
        {
            // for <var> = <lower> to <upper> <body>
            // --- to --->
            // {
            // var <var> = <lower>
            // let upperBound = <upper>
            // while <var> <= upperBound
            //     <body>
            //     continue:
            //     <var> = <var> + 1
            // }

            var variableDeclaration
                = new BoundVariableDeclarationStatement(node.Variable, node.LowerBound);

            var upperBoundSymbol = new LocalVariableSymbol("upperBound", true, TypeSymbol.Int);
            var upperBoundDeclaration
                = new BoundVariableDeclarationStatement(upperBoundSymbol, node.UpperBound);
            var upperBoundExpression = new BoundVariableExpression(upperBoundSymbol);

            var condition = new BoundBinaryExpression(
                new BoundVariableExpression(node.Variable),
                BoundBinaryOperator.Bind(Lexing.TokenKind.LessOrEquals, TypeSymbol.Int, TypeSymbol.Int),
                upperBoundExpression);

            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        new BoundVariableExpression(node.Variable),
                        BoundBinaryOperator.Bind(Lexing.TokenKind.Plus, TypeSymbol.Int, TypeSymbol.Int),
                        new BoundLiteralExpression(1))));

            var whileBlock =
                new BoundBlockStatement(
                    node.Body,
                    increment);

            var whileStatement
                = new BoundWhileStatement(condition, whileBlock, node.BreakLabel, node.ContinueLabel);

            var result = new BoundBlockStatement(
                variableDeclaration,
                upperBoundDeclaration,
                whileStatement);

            return(RewriteStatement(result));
        }
Пример #20
0
        public override object VisitWhileStatement(BoundWhileStatement node, object arg)
        {
            // while (node.Condition) { node.Body; node.ContinueLabel: } node.BreakLabel:
            LoopHead(node);
            VisitCondition(node.Condition);
            var bodyState          = new FlowAnalysisLocalState(this.state.Reachable && !IsConstantFalse(node.Condition), this.state.AssignedWhenTrue);
            var breakState         = new FlowAnalysisLocalState(this.state.Reachable && !IsConstantTrue(node.Condition), this.state.AssignedWhenFalse);
            var oldPendingBranches = SavePending();

            this.state = bodyState;
            VisitStatement(node.Body);
            ResolveContinues(node.ContinueLabel);
            LoopTail(node);
            ResolveBreaks(oldPendingBranches, breakState, node.BreakLabel);
            return(null);
        }
Пример #21
0
        private void EmitWhileStatement(BoundWhileStatement statement)
        {
            string startOfLoopLabel = this.GenerateJumpLabel();
            string endOfLoopLabel   = this.GenerateJumpLabel();

            this.instructions.Add(new TransientLabelInstruction(startOfLoopLabel, statement.Syntax.WhileToken.Range));
            this.EmitExpression(statement.Condition);
            this.instructions.Add(new TransientConditionalGoToInstruction(null, endOfLoopLabel, statement.Syntax.Condition.Range));

            this.EmitBlockStatement(statement.Body);

            TextRange endOfLoopRange = this.instructions.Last().Range;

            this.instructions.Add(new TransientUnconditionalGoToInstruction(startOfLoopLabel, endOfLoopRange));
            this.instructions.Add(new TransientLabelInstruction(endOfLoopLabel, endOfLoopRange));
        }
Пример #22
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node)
        {
            /*
             *  check:
             *  gotoIfFalse <condition> end
             *  <statement>
             *  goto check
             *  end:
             */

            var checkLabelStatement  = new BoundLabelStatement(GenerateLabel());
            var endLabelStatement    = new BoundLabelStatement(GenerateLabel());
            var gotoIfFalseStatement = new BoundConditionalGotoStatement(endLabelStatement.Label, node.Condition, false);
            var gotoCheckStatement   = new BoundGotoStatement(checkLabelStatement.Label);
            var statements           = ImmutableArray.Create <BoundStatement>(checkLabelStatement, gotoIfFalseStatement, node.Statement, gotoCheckStatement, endLabelStatement);
            var result = new BoundBlockStatement(statements);

            return(RewriteStatement(result));
        }
Пример #23
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            // for <var> = <lower> to <upper>
            //      <body>
            //
            // ---->
            //
            // {
            //      var <var> = <lower>
            //      while (<var> <= <upper>)
            //      {
            //          <body>
            //          <var> = <var> + 1
            //      }
            // }

            var variableDeclaration   = new BoundVeriableDeclaration(node.Variable, node.LowerBound);
            var variableExpression    = new BoundVariableExpression(node.Variable);
            var upperBoundSybmle      = new VariableSymble("upperBound", true, typeof(int));
            var upperBoundDeclaration = new BoundVeriableDeclaration(upperBoundSybmle, node.UpperBound);
            var condition             = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualToken, typeof(int), typeof(int)),
                new BoundVariableExpression(upperBoundSybmle)
                );
            var increment = new BoundExpressionStatemnet(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, typeof(int), typeof(int)),
                        new BoundLiteralExpression(1)
                        )
                    )
                );
            var whileBody      = new BoundBlockStatemnet(ImmutableArray.Create <BoundStatement>(node.Body, increment));
            var whileStatement = new BoundWhileStatement(condition, whileBody);
            var result         = new BoundBlockStatemnet(ImmutableArray.Create <BoundStatement>(
                                                             variableDeclaration, upperBoundDeclaration, whileStatement));

            return(RewriteStatement(result));
        }
Пример #24
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node)
        {
            var bodyLabel = GenerateLabel();

            var gotoContinue           = new BoundGotoStatement(node.ContinueLabel);
            var bodyLabelStatement     = new BoundLabelStatement(bodyLabel);
            var continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);
            var gotoTrue            = new BoundConditionalGotoStatement(bodyLabel, node.Condition);
            var breakLabelStatement = new BoundLabelStatement(node.BreakLabel);

            var result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                     gotoContinue,
                                                     bodyLabelStatement,
                                                     node.Body,
                                                     continueLabelStatement,
                                                     gotoTrue,
                                                     breakLabelStatement
                                                     ));

            return(RewriteStatement(result));
        }
Пример #25
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            var condition = node.Condition;

            var continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);
            var increment = new BoundExpressionStatement(
                node.Action
                );
            var whileBody = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                        node.Body,
                                                        continueLabelStatement,
                                                        increment)
                                                    );
            var whileStatement = new BoundWhileStatement(condition, whileBody, node.BreakLabel, GenerateLabel());
            var result         = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                             node.Variable,
                                                             whileStatement
                                                             ));

            return(RewriteStatement(result));
        }
Пример #26
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement statement)
        {
            /*  while(<condition>) <body>
             *
             *  branch conditionCheck
             *  #start
             *  <body>
             *  #conditionCheck
             *  conditional branch <condition> start
             */

            var startLabel          = CreateNextLabel();
            var conditionCheckLabel = CreateNextLabel();

            var firstBranch = new BoundBranchStatement(conditionCheckLabel);
            var condBranch  = new BoundConditionalBranchStatement(startLabel, statement.Condition, true);

            var statements = ImmutableArray.CreateBuilder <BoundStatement>();

            statements.AddRange(new BoundStatement[] {
                firstBranch,
                new BoundLabel(startLabel),
                statement.Body
            });

            if (statement.AddContinue)
            {
                statements.Add(new BoundLabel(statement.ContinueLabel));
            }

            statements.AddRange(new BoundStatement[] {
                new BoundLabel(conditionCheckLabel),
                condBranch,
                new BoundLabel(statement.BreakLabel)
            });

            var rewrite = RewriteStatement(new BoundBlock(statements.ToImmutable()));

            return(rewrite);
        }
Пример #27
0
        public override BoundNode VisitWhileStatement(BoundWhileStatement node)
        {
            Debug.Assert(node != null);

            var rewrittenCondition = (BoundExpression)Visit(node.Condition);
            var rewrittenBody      = (BoundStatement)Visit(node.Body);

            TextSpan conditionSequencePointSpan = default(TextSpan);

            if (this.generateDebugInfo)
            {
                if (!node.WasCompilerGenerated)
                {
                    WhileStatementSyntax whileSyntax = (WhileStatementSyntax)node.Syntax;
                    conditionSequencePointSpan = TextSpan.FromBounds(
                        whileSyntax.WhileKeyword.Span.Start,
                        whileSyntax.CloseParenToken.Span.End);
                }
            }

            return(RewriteWhileStatement(node.Syntax, rewrittenCondition, conditionSequencePointSpan, rewrittenBody, node.BreakLabel, node.ContinueLabel, node.HasErrors));
        }
Пример #28
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            BoundVariableDeclaration variableDeclaration      = new BoundVariableDeclaration(node.Variable, node.LowerBound);
            BoundVariableExpression  variableExpression       = new BoundVariableExpression(node.Variable);
            VariableSymbol           upperVariableSymbol      = new LocalVariableSymbol("upperBound", true, TypeSymbol.Int);
            BoundVariableDeclaration upperVariableDeclaration = new BoundVariableDeclaration(upperVariableSymbol, node.UpperBound);

            BoundBinaryExpression condition = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualsToken, TypeSymbol.Int, TypeSymbol.Int),
                new BoundVariableExpression(upperVariableSymbol)
                );
            BoundLabelStatement      continueLabelStatement = new BoundLabelStatement(node.ContinueLabel);
            BoundExpressionStatement increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(SyntaxKind.PlusToken, TypeSymbol.Int, TypeSymbol.Int),
                        new BoundLiteralExpression(1)
                        )
                    )
                );

            BoundBlockStatement whileBody = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                                        node.Body,
                                                                        continueLabelStatement,
                                                                        increment
                                                                        ));
            BoundWhileStatement whileStatement = new BoundWhileStatement(condition, whileBody, node.BreakLabel, GenerateLabel());

            BoundBlockStatement result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(
                                                                     variableDeclaration,
                                                                     upperVariableDeclaration,
                                                                     whileStatement
                                                                     ));

            return(RewriteStatement(result));
        }
Пример #29
0
        protected override BoundStatement RewriteForStatement(BoundForStatement node)
        {
            // for <var> = <lower> to <upper>
            //  <body>
            // is rewritten to
            //  {
            //    var <Var> = <lower>
            //  while ()
            //
            var variableDeclaration = new BoundVariableDeclaration(node.Variable, node.LowerBound);
            var variableExpression  = new BoundVariableExpression(node.Variable);

            var condition = new BoundBinaryExpression(
                variableExpression,
                BoundBinaryOperator.Bind(SyntaxKind.LessOrEqualsToken, typeof(int), typeof(int)),
                node.UpperBound);

            var increment = new BoundExpressionStatement(
                new BoundAssignmentExpression(
                    node.Variable,
                    new BoundBinaryExpression(
                        variableExpression,
                        BoundBinaryOperator.Bind(
                            SyntaxKind.PlusToken,
                            typeof(int),
                            typeof(int)),
                        new BoundLiteralExpression(1)
                        )
                    )
                );

            var whileBlock = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(node.Body, increment));

            var whileStatement = new BoundWhileStatement(condition, whileBlock);

            var result = new BoundBlockStatement(ImmutableArray.Create <BoundStatement>(variableDeclaration, whileStatement));

            return(RewriteStatement(result));
        }
Пример #30
0
        protected override BoundStatement RewriteWhileStatement(BoundWhileStatement node)
        {
            // goto check:
            // continue:
            // <body>
            // check:
            // gotoTrue condition continue:
            // end:
            // -------------------------------------------------------
            // later try out
            //- begin
            //- gotoFalse <condition> end
            //- <body>
            //-  goto begin
            //- end

            var endLabel      = GenerateLabel();
            var checkLabel    = GenerateLabel();
            var continueLabel = GenerateLabel();

            var gotoCheck = new BoundGotoStatement(checkLabel);
            var gotoTrue  = new BoundConditionalGotoStatement(continueLabel, node.Condition);

            var continueLabelStatement = new BoundLabelStatement(continueLabel);
            var checkLabelStatement    = new BoundLabelStatement(checkLabel);
            var endLabelStatement      = new BoundLabelStatement(endLabel);

            var result = new BoundBlockStatement(
                ImmutableArray.Create <BoundStatement>(
                    gotoCheck,
                    continueLabelStatement,
                    node.Body,
                    checkLabelStatement,
                    gotoTrue,
                    endLabelStatement));

            return(RewriteStatement(result));
        }
Пример #31
0
 public override BoundNode VisitWhileStatement(BoundWhileStatement node)
 {
     var newLocals = RewriteLocals(node.Locals);
     BoundExpression condition = (BoundExpression)this.Visit(node.Condition);
     BoundStatement body = (BoundStatement)this.Visit(node.Body);
     return node.Update(newLocals, condition, body, node.BreakLabel, node.ContinueLabel);
 }
Пример #32
0
        public override BoundNode VisitWhileStatement(BoundWhileStatement node)
        {
            Debug.Assert(node != null);

            var rewrittenCondition = (BoundExpression)Visit(node.Condition);
            var rewrittenBody = (BoundStatement)Visit(node.Body);

            TextSpan conditionSequencePointSpan = default(TextSpan);
            if (this.generateDebugInfo)
            {
                if (!node.WasCompilerGenerated)
                {
                    WhileStatementSyntax whileSyntax = (WhileStatementSyntax)node.Syntax;
                    conditionSequencePointSpan = TextSpan.FromBounds(
                        whileSyntax.WhileKeyword.Span.Start,
                        whileSyntax.CloseParenToken.Span.End);
                }
            }

            return RewriteWhileStatement(node.Syntax, rewrittenCondition, conditionSequencePointSpan, rewrittenBody, node.BreakLabel, node.ContinueLabel, node.HasErrors);
        }