コード例 #1
0
        public Graph GetNextFunctionAsGLEEGraph()
        {
            Graph g = new Graph("Next");

            foreach (CFGNode Node in ListOfCfgNodes)
            {
                Node n = (Node)g.AddNode(Node.GetHashCode().ToString());
                n.Attr.Label = Node.ToString();
            }

            foreach (CFGNode Node in ListOfCfgNodes)
            {
                if (Node is CFGNodeStatement)
                {
                    CFGNodeStatement StmtNode = (CFGNodeStatement)Node;
                    CFGNode          NextNode = StmtNode.Next;
                    g.AddEdge(Node.GetHashCode().ToString(), NextNode.GetHashCode().ToString());
                }
            }

            return(g);
        }
コード例 #2
0
        private void BuildSuccesors()
        {
            foreach (CFGNode node in ListOfCfgNodes)
            {
                CommonAST nodeAST = node.GetAST();
                CFGNode   succNode;

                #region Succesors for Statements
                if (node is CFGNodeStatement)
                {
                    CFGNodeStatement stmtNode = (CFGNodeStatement)node;
                    switch (nodeAST.Type)
                    {
                    case BoolParserTokenTypes.LITERAL_goto:
                        nodeAST = (CommonAST)nodeAST.getFirstChild();
                        if (LabelToStatement.TryGetValue(nodeAST.getText(), out succNode))
                        {
                            stmtNode.AddSuccesor(succNode);
                        }
                        break;

                    case BoolParserTokenTypes.ASSIGNMENT:
                    case BoolParserTokenTypes.LITERAL_skip:
                    case BoolParserTokenTypes.LITERAL_print:
                        stmtNode.AddSuccesor(stmtNode.Next);
                        break;

                    case BoolParserTokenTypes.LITERAL_return:
                        stmtNode.AddSuccesor(stmtNode.ProcOf);
                        break;

                    case BoolParserTokenTypes.LITERAL_if:
                        nodeAST = (CommonAST)nodeAST.getFirstChild();
                        nodeAST = (CommonAST)nodeAST.getNextSibling();
                        if (AstToCfgMapping.TryGetValue(nodeAST.getFirstChild() as CommonAST, out succNode))
                        {
                            (stmtNode as CFGNodeStmtIf).TrueSuccesor = succNode;
                        }
                        nodeAST = (CommonAST)nodeAST.getNextSibling();
                        if (AstToCfgMapping.TryGetValue(nodeAST.getFirstChild() as CommonAST, out succNode))
                        {
                            (stmtNode as CFGNodeStmtIf).FalseSuccesor = succNode;
                        }
                        break;

                    case BoolParserTokenTypes.LITERAL_while:
                        nodeAST = (CommonAST)nodeAST.getFirstChild();
                        nodeAST = (CommonAST)nodeAST.getNextSibling();
                        nodeAST = (CommonAST)nodeAST.getFirstChild();
                        if (AstToCfgMapping.TryGetValue(nodeAST, out succNode))
                        {
                            (stmtNode as CFGNodeStmtWhile).TrueSuccesor = succNode;
                        }
                        (stmtNode as CFGNodeStmtWhile).FalseSuccesor = stmtNode.Next;
                        break;

                    case BoolParserTokenTypes.LITERAL_assert:
                        (stmtNode as CFGNodeStmtAssert).FalseSuccesor = ErrorNode;
                        (stmtNode as CFGNodeStmtAssert).TrueSuccesor  = stmtNode.Next;
                        break;

                    case BoolParserTokenTypes.PROCCALL:
                        nodeAST = (CommonAST)nodeAST.getFirstChild();
                        CFGNodeProcedure procNode;
                        if (ProcNameToNode.TryGetValue(nodeAST.getText(), out procNode))
                        {
                            stmtNode.AddSuccesor(procNode.FirstStmtOf);
                        }

                        //Exit points of procedures need to be filled when processing all procedure calls
                        procNode.AddSuccesor(stmtNode.Next);
                        break;

                    default:
                        System.Diagnostics.Debug.Assert(false);
                        break;
                    }
                }
                #endregion
                //Succesors for Procedure Exits are built when processing Procedure calls
            }
        }
コード例 #3
0
        private void BuildNextFunction()
        {
            foreach (CFGNode node in ListOfCfgNodes)
            {
                if (node is CFGNodeStatement)
                {
                    CFGNodeStatement stmt = (CFGNodeStatement)node;

                    CFGNode NextNode = null;

                    CommonAST ASTwalker = stmt.GetAST();
                    ASTParentByASTNode.TryGetValue(ASTwalker, out ASTwalker);

                    bool done = false;
                    while (!done && ASTwalker != null)
                    {
                        #region STMT or LSTMT
                        if (ASTwalker.Type == BoolParserTokenTypes.LITERAL_while)
                        {
                            done = true;
                            ASTParentByASTNode.TryGetValue(ASTwalker, out ASTwalker);
                            if (AstToCfgMapping.TryGetValue(ASTwalker, out NextNode))
                            {
                                stmt.Next = NextNode;
                            }
                            else
                            {
                                stmt.Next = null;
                            }
                        }
                        else
                        if ((ASTwalker.Type == BoolParserTokenTypes.STMT ||
                             ASTwalker.Type == BoolParserTokenTypes.LSTMT) &&
                            (ASTwalker.getNextSibling() != null))
                        {
                            CommonAST ASTwalkerSibling = (CommonAST)ASTwalker.getNextSibling();
                            if (ASTwalkerSibling.Type == BoolParserTokenTypes.STMT ||
                                ASTwalkerSibling.Type == BoolParserTokenTypes.LSTMT)
                            {
                                done = true;
                                if (AstToCfgMapping.TryGetValue(ASTwalkerSibling, out NextNode))
                                {
                                    stmt.Next = NextNode;
                                }
                                else
                                {
                                    stmt.Next = null;
                                }
                            }
                        }
                        #endregion

                        #region PROC
                        if (ASTwalker.Type == BoolParserTokenTypes.PROC)
                        {
                            done = true;
                            if (AstToCfgMapping.TryGetValue(ASTwalker, out NextNode))
                            {
                                stmt.Next = NextNode;
                            }
                            else
                            {
                                stmt.Next = null;
                            }
                        }
                        #endregion

                        if (!done)
                        {
                            ASTParentByASTNode.TryGetValue(ASTwalker, out ASTwalker);
                        }
                    }

                    if (stmt is CFGNodeStmtProcCall)
                    {
                        if (NextNode is CFGNodeStmtSkip)
                        {
                            (NextNode as CFGNodeStmtSkip).setPreviousProcCall(stmt as CFGNodeStmtProcCall);
                        }
                    }
                }
                else
                //We use this pass trough the CFG nodes to build "First Statement Of Procedure" function
                if (node is CFGNodeProcedure)
                {
                    CFGNodeProcedure proc = (CFGNodeProcedure)node;

                    CommonAST ASTwalker = proc.GetAST();

                    ASTwalker = (CommonAST)ASTwalker.getFirstChild();
                    while (ASTwalker.Type != BoolParserTokenTypes.SSEQ)
                    {
                        ASTwalker = (CommonAST)ASTwalker.getNextSibling();
                    }
                    ASTwalker = (CommonAST)ASTwalker.getFirstChild();

                    CFGNode FirstOf;
                    if (AstToCfgMapping.TryGetValue(ASTwalker, out FirstOf))
                    {
                        proc.FirstStmtOf = FirstOf;
                    }
                }
            }
        }