Example #1
0
            public List <BasicBlock> Build(BoundBlockStatement block)
            {
                foreach (var statement in block.Statements)
                {
                    switch (statement.Kind)
                    {
                    case BoundNodeKind.LabelStatement:
                        StartBlock();
                        _statements.Add(statement);
                        break;

                    case BoundNodeKind.GotoStatement:
                    case BoundNodeKind.ConditionalGotoStatement:
                    case BoundNodeKind.ReturnStatement:
                        _statements.Add(statement);
                        StartBlock();
                        break;

                    case BoundNodeKind.VariableDeclaration:
                    case BoundNodeKind.ExpressionStatement:
                        _statements.Add(statement);
                        break;

                    default:
                        throw new Exception($"Unexpected statement: {statement.Kind}");
                    }
                }

                EndBlock();
                return(_blocks.ToList());
            }
Example #2
0
        protected virtual BoundStatement RewriteBlockStatement(BoundBlockStatement node)
        {
            ImmutableArray <BoundStatement> .Builder builder = null;

            for (var i = 0; i < node.Statements.Length; i++)
            {
                var oldStatement = node.Statements[i];
                var newStatement = RewriteStatement(oldStatement);

                if (newStatement != oldStatement)
                {
                    if (builder == null)
                    {
                        builder = ImmutableArray.CreateBuilder <BoundStatement>(node.Statements.Length);
                        for (var j = 0; j < i; j++)
                        {
                            builder.Add(node.Statements[j]);
                        }
                    }
                }
                if (builder != null)
                {
                    builder.Add(newStatement);
                }
            }
            if (builder == null)
            {
                return(node);
            }

            return(new BoundBlockStatement(builder.MoveToImmutable()));
        }
Example #3
0
        public static ControlFlowGraph Create(BoundBlockStatement body)
        {
            var basicBlockBuilder = new BasicBlockBuilder();
            var blocks            = basicBlockBuilder.Build(body);

            var graphBuilder = new GraphBuilder();

            return(graphBuilder.Build(blocks));
        }
Example #4
0
        private static void WriteBlockStatement(BoundBlockStatement node, IndentedTextWriter writer)
        {
            writer.WritePunctuation(SyntaxKind.OpenBraceToken);
            writer.WriteLine();
            writer.Indent++;

            foreach (var s in node.Statements)
            {
                s.WriteTo(writer);
            }

            writer.Indent--;
            writer.WritePunctuation(SyntaxKind.CloseBraceToken);
            writer.WriteLine();
        }
Example #5
0
        public static bool AllPathsReturn(BoundBlockStatement body)
        {
            var graph = Create(body);

            foreach (var branch in graph.End.Incoming)
            {
                var lastStatement = branch.From.Statements.LastOrDefault();
                if (lastStatement == null || lastStatement.Kind != BoundNodeKind.ReturnStatement)
                {
                    return(false);
                }
            }

            return(true);
        }
Example #6
0
        protected virtual BoundStatement RewriteBlockStatement(BoundStatement node)
        {
            ImmutableArray <BoundStatement> .Builder?builder = null;

            var boundStatements = node switch
            {
                BoundBlockStatement bbs => bbs.Statements,
                BoundMemberBlockStatement bmbs => bmbs.Statements,
                _ => throw new Exception($"Unexpected block statement type '{node.Kind}'."),
            };

            for (var i = 0; i < boundStatements.Length; i++)
            {
                var oldStatement = boundStatements[i];
                var newStatement = RewriteStatement(oldStatement);
                if (newStatement != oldStatement)
                {
                    if (builder == null)
                    {
                        builder = ImmutableArray.CreateBuilder <BoundStatement>(boundStatements.Length);

                        for (var j = 0; j < i; j++)
                        {
                            builder.Add(boundStatements[j]);
                        }
                    }
                }

                builder?.Add(newStatement);
            }

            if (builder == null)
            {
                return(node);
            }

            return(new BoundBlockStatement(node.Syntax, builder.MoveToImmutable()));
        }