Esempio n. 1
0
            public List <BasicBlock> Build(BoundBlockStatement block)
            {
                foreach (BoundStatement 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 statemend: '{statement.Kind}'.");
                    }
                }

                EndBlock();

                return(blocks.ToList());
            }
Esempio n. 2
0
        protected virtual BoundStatement RewriteBlockStatement(BoundBlockStatement node)
        {
            ImmutableArray <BoundStatement> .Builder builder = null;

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

                if (newStatement != oldStatement)
                {
                    if (builder == null)
                    {
                        builder = ImmutableArray.CreateBuilder <BoundStatement>(node.Statements.Length);

                        for (int 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()));
        }
Esempio n. 3
0
        public static ControlFlowGraph Create(BoundBlockStatement body)
        {
            BasicBlockBuilder basicBlockBuilder = new BasicBlockBuilder();
            List <BasicBlock> blocks            = basicBlockBuilder.Build(body);

            GraphBuilder graphBuilder = new GraphBuilder();

            return(graphBuilder.Build(blocks));
        }
Esempio n. 4
0
        public static bool AllPathsReturn(BoundBlockStatement body)
        {
            ControlFlowGraph graph = Create(body);

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

            return(true);
        }
Esempio n. 5
0
        private static void WriteBlockStatement(BoundBlockStatement node, IndentedTextWriter writer)
        {
            writer.WritePunctuation(SyntaxKind.OpenBraceToken);
            writer.WriteLine();
            writer.Indent++;

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

            writer.Indent--;
            writer.WritePunctuation(SyntaxKind.CloseBraceToken);
            writer.WriteLine();
        }
Esempio n. 6
0
 public BoundProgram(ImmutableArray <Diagnostic> diagnostics, ImmutableDictionary <FunctionSymbol, BoundBlockStatement> functions, BoundBlockStatement statement)
 {
     Diagnostics = diagnostics;
     Functions   = functions;
     Statement   = statement;
 }