private static bool CollapseIfElse(IfHelper.IfNode rtnode) { if (rtnode.edgetypes[0] == 0) { IfHelper.IfNode ifbranch = rtnode.succs[0]; if (ifbranch.succs.Count == 2) { // if-else branch if (ifbranch.succs[0].value == rtnode.succs[1].value) { IfStatement ifparent = (IfStatement)rtnode.value; IfStatement ifchild = (IfStatement)ifbranch.value; if ((ifchild.GetFirst().GetExprents().Count == 0)) { ifparent.GetFirst().RemoveSuccessor(ifparent.GetIfEdge()); ifchild.GetFirst().RemoveSuccessor(ifchild.GetIfEdge()); ifparent.GetStats().RemoveWithKey(ifchild.id); if (ifbranch.edgetypes[1] == 1 && ifbranch.edgetypes[0] == 1) { // target null ifparent.SetIfstat(null); StatEdge ifedge = ifchild.GetAllSuccessorEdges()[0]; ifchild.RemoveSuccessor(ifedge); ifedge.SetSource(ifparent.GetFirst()); ifparent.GetFirst().AddSuccessor(ifedge); ifparent.SetIfEdge(ifedge); } else { throw new Exception("inconsistent if structure!"); } // merge if conditions IfExprent statexpr = ifparent.GetHeadexprent(); List <Exprent> lstOperands = new List <Exprent>(); lstOperands.Add(statexpr.GetCondition()); lstOperands.Add(new FunctionExprent(FunctionExprent.Function_Bool_Not, ifchild.GetHeadexprent ().GetCondition(), null)); statexpr.SetCondition(new FunctionExprent(FunctionExprent.Function_Cadd, lstOperands , null)); statexpr.AddBytecodeOffsets(ifchild.GetHeadexprent().bytecode); return(true); } } } } return(false); }
private static bool CollapseIfIf(IfHelper.IfNode rtnode) { if (rtnode.edgetypes[0] == 0) { IfHelper.IfNode ifbranch = rtnode.succs[0]; if (ifbranch.succs.Count == 2) { // if-if branch if (ifbranch.succs[1].value == rtnode.succs[1].value) { IfStatement ifparent = (IfStatement)rtnode.value; IfStatement ifchild = (IfStatement)ifbranch.value; Statement ifinner = ifbranch.succs[0].value; if ((ifchild.GetFirst().GetExprents().Count == 0)) { ifparent.GetFirst().RemoveSuccessor(ifparent.GetIfEdge()); ifchild.RemoveSuccessor(ifchild.GetAllSuccessorEdges()[0]); ifparent.GetStats().RemoveWithKey(ifchild.id); if (ifbranch.edgetypes[0] == 1) { // target null ifparent.SetIfstat(null); StatEdge ifedge = ifchild.GetIfEdge(); ifchild.GetFirst().RemoveSuccessor(ifedge); ifedge.SetSource(ifparent.GetFirst()); if (ifedge.closure == ifchild) { ifedge.closure = null; } ifparent.GetFirst().AddSuccessor(ifedge); ifparent.SetIfEdge(ifedge); } else { ifchild.GetFirst().RemoveSuccessor(ifchild.GetIfEdge()); StatEdge ifedge = new StatEdge(StatEdge.Type_Regular, ifparent.GetFirst(), ifinner ); ifparent.GetFirst().AddSuccessor(ifedge); ifparent.SetIfEdge(ifedge); ifparent.SetIfstat(ifinner); ifparent.GetStats().AddWithKey(ifinner, ifinner.id); ifinner.SetParent(ifparent); if (!(ifinner.GetAllSuccessorEdges().Count == 0)) { StatEdge edge = ifinner.GetAllSuccessorEdges()[0]; if (edge.closure == ifchild) { edge.closure = null; } } } // merge if conditions IfExprent statexpr = ifparent.GetHeadexprent(); List <Exprent> lstOperands = new List <Exprent>(); lstOperands.Add(statexpr.GetCondition()); lstOperands.Add(ifchild.GetHeadexprent().GetCondition()); statexpr.SetCondition(new FunctionExprent(FunctionExprent.Function_Cadd, lstOperands , null)); statexpr.AddBytecodeOffsets(ifchild.GetHeadexprent().bytecode); return(true); } } } } return(false); }