private void insertAfter(JPegStmt node, JPegStmt after) { // System.out.println("node: "+node); // System.out.println("after: "+after); // System.out.println("succs of node: "+getSuccsOf(node)); // this must be done first because the succs of node will be chanaged lately List succOfAfter = new ArrayList(); succOfAfter.addAll(getSuccsOf(node)); unitToSuccs.put(after, succOfAfter); Iterator succsIt = getSuccsOf(node).iterator(); while (succsIt.hasNext()) { Object succ = succsIt.next(); List pred = getPredsOf(succ); pred.remove(node); pred.add(after); } List succOfNode = new ArrayList(); succOfNode.add(after); unitToSuccs.put(node, succOfNode); List predOfAfter = new ArrayList(); predOfAfter.add(node); unitToPreds.put(after, predOfAfter); // buildPredecessor(Chain pegChain); }
public String toString() { Iterator it = iterator(); StringBuffer buf = new StringBuffer(); while (it.hasNext()) { JPegStmt u = (JPegStmt) it.next(); buf.append("u is: " + u + "\n"); List l = new ArrayList(); l.addAll(getPredsOf(u)); buf.append("preds: " + l + "\n"); // buf.append(u.toString() + '\n'); l = new ArrayList(); l.addAll(getSuccsOf(u)); buf.append("succs: " + l + "\n"); } return buf.toString(); }
private void insertBefore(JPegStmt node, JPegStmt enter) { // build preds of before List predOfBefore = new ArrayList(); predOfBefore.addAll(getPredsOf(node)); unitToPreds.put(enter, predOfBefore); // System.out.println("put into unitToPreds enter: "+enter); // System.out.println("put into unitToPreds value: "+predOfBefore); Iterator predsIt = getPredsOf(node).iterator(); // build succs of former preds of node while (predsIt.hasNext()) { Object pred = predsIt.next(); List succ = getSuccsOf(pred); succ.remove(node); succ.add(enter); // System.out.println("in unitToPred pred: "+pred); // System.out.println("in unitToPred value is: "+succ); } List succOfBefore = new ArrayList(); succOfBefore.add(node); unitToSuccs.put(enter, succOfBefore); // System.out.println("put into unitToSuccs enter: "+enter); // System.out.println("put into unitToSuccs value: "+succOfBefore); List predOfNode = new ArrayList(); predOfNode.add(enter); unitToPreds.put(node, predOfNode); // System.out.println("put into unitToPreds enter: "+node); // System.out.println("put into unitToPreds value: "+predOfNode); // buildPreds(); }
/** * This method pushes all newExpr down to be the stmt directly before every invoke of the init * only if they are in the types list */ public void internalTransform(Body b, String phaseName, Map options) { JimpleBody body = (JimpleBody) b; if (Options.v().verbose()) G.v().out.println("[" + body.getMethod().getName() + "] Folding Jimple constructors..."); Chain units = body.getUnits(); List<Unit> stmtList = new ArrayList<Unit>(); stmtList.addAll(units); Iterator<Unit> it = stmtList.iterator(); Iterator<Unit> nextStmtIt = stmtList.iterator(); // start ahead one nextStmtIt.next(); SmartLocalDefs localDefs = SmartLocalDefsPool.v().getSmartLocalDefsFor(body); UnitGraph graph = localDefs.getGraph(); LocalUses localUses = new SimpleLocalUses(graph, localDefs); /* fold in NewExpr's with specialinvoke's */ while (it.hasNext()) { Stmt s = (Stmt) it.next(); if (!(s instanceof AssignStmt)) continue; /* this should be generalized to ArrayRefs */ // only deal with stmts that are an local = newExpr Value lhs = ((AssignStmt) s).getLeftOp(); if (!(lhs instanceof Local)) continue; Value rhs = ((AssignStmt) s).getRightOp(); if (!(rhs instanceof NewExpr)) continue; // check if very next statement is invoke --> // this indicates there is no control flow between // new and invoke and should do nothing if (nextStmtIt.hasNext()) { Stmt next = (Stmt) nextStmtIt.next(); if (next instanceof InvokeStmt) { InvokeStmt invoke = (InvokeStmt) next; if (invoke.getInvokeExpr() instanceof SpecialInvokeExpr) { SpecialInvokeExpr invokeExpr = (SpecialInvokeExpr) invoke.getInvokeExpr(); if (invokeExpr.getBase() == lhs) { break; } } } } // check if new is in the types list - only process these if (!types.contains(((NewExpr) rhs).getType())) continue; List lu = localUses.getUsesOf(s); Iterator luIter = lu.iterator(); boolean MadeNewInvokeExpr = false; while (luIter.hasNext()) { Unit use = ((UnitValueBoxPair) (luIter.next())).unit; if (!(use instanceof InvokeStmt)) continue; InvokeStmt is = (InvokeStmt) use; if (!(is.getInvokeExpr() instanceof SpecialInvokeExpr) || lhs != ((SpecialInvokeExpr) is.getInvokeExpr()).getBase()) continue; // make a new one here AssignStmt constructStmt = Jimple.v() .newAssignStmt(((DefinitionStmt) s).getLeftOp(), ((DefinitionStmt) s).getRightOp()); constructStmt.setRightOp(Jimple.v().newNewExpr(((NewExpr) rhs).getBaseType())); MadeNewInvokeExpr = true; // redirect jumps use.redirectJumpsToThisTo(constructStmt); // insert new one here units.insertBefore(constructStmt, use); constructStmt.addTag(s.getTag("SourceLnPosTag")); } if (MadeNewInvokeExpr) { units.remove(s); } } }
protected void buildSuccsForInlining(JPegStmt stmt, Chain chain, PegGraph inlinee) { // System.out.println("entering buildSuccsForInlining..."); Tag tag = (Tag) stmt.getTags().get(0); // System.out.println("stmt is: "+tag+" "+stmt); /*connect heads of inlinee with the preds of invokeStmt and * delete stmt from the succs list from the preds */ Iterator predIt = getPredsOf(stmt).iterator(); // System.out.println("preds list: "+getPredsOf(stmt)); // System.out.println("preds size: "+getPredsOf(stmt).size()); Iterator headsIt = inlinee.getHeads().iterator(); { // System.out.println("heads: "+inlinee.getHeads()); while (predIt.hasNext()) { JPegStmt pred = (JPegStmt) predIt.next(); // System.out.println("pred: "+pred); List succList = (List) getSuccsOf(pred); // System.out.println("succList of pred: "+succList); int pos = succList.indexOf(stmt); // System.out.println("remove : "+stmt + " from succList: \n"+succList+ "\n of pred" ); // remove invokeStmt succList.remove(pos); while (headsIt.hasNext()) { succList.add(headsIt.next()); } unitToSuccs.put(pred, succList); } { while (headsIt.hasNext()) { Object head = headsIt.next(); List predsOfHeads = new ArrayList(); predsOfHeads.addAll(getPredsOf(head)); unitToPreds.put(head, predsOfHeads); } } /* { predIt = getPredsOf(stmt).iterator(); while (predIt.hasNext()){ JPegStmt s = (JPegStmt)predIt.next(); if (unitToSuccs.containsKey(s)){ Iterator succIt = ((List) unitToSuccs.get(s)).iterator(); while(succIt.hasNext()){ //Object successor = succIt.next(); JPegStmt successor = (JPegStmt)succIt.next(); List predList = (List) unitToPreds.get(successor); if (predList != null) { try { predList.add(s); } catch(NullPointerException e) { System.out.println(s + "successor: " + successor); throw e; } } } } } }*/ } /*connect tails of inlinee with the succ of invokeStmt and * delete stmt from the */ Iterator tailsIt = inlinee.getTails().iterator(); { // System.out.println("tails: "+inlinee.getTails()); while (tailsIt.hasNext()) { Iterator succIt = getSuccsOf(stmt).iterator(); JPegStmt tail = (JPegStmt) tailsIt.next(); List succList = null; if (unitToSuccs.containsKey(tail)) { // System.out.println("error: unitToSucc containsKey: "+tail); succList = (List) getSuccsOf(tail); // System.out.println("succList: "+succList); } else { succList = new ArrayList(); } while (succIt.hasNext()) { JPegStmt succ = (JPegStmt) succIt.next(); succList.add(succ); // System.out.println("succ: "+succ); // remove stmt from the preds list of the succs of itself. List predListOfSucc = getPredsOf(succ); if (predListOfSucc == null) { System.err.println("Error: predListOfSucc is null!"); System.exit(1); } else { if (predListOfSucc.size() != 0) { int pos = predListOfSucc.indexOf(stmt); if (pos > 0 || pos == 0) { // System.out.println("remove stmt: "+stmt+" from the preds list"+predListOfSucc+" // of the succ"); predListOfSucc.remove(pos); } // System.out.println("remove(from PRED): "); } } unitToPreds.put(succ, predListOfSucc); } unitToSuccs.put(tail, succList); // System.out.println("put: "+tail); // System.out.println("succList: "+succList+ "into unitToSucc"); } } // add Nov 1 { tailsIt = inlinee.getTails().iterator(); while (tailsIt.hasNext()) { JPegStmt s = (JPegStmt) tailsIt.next(); if (unitToSuccs.containsKey(s)) { Iterator succIt = unitToSuccs.get(s).iterator(); while (succIt.hasNext()) { // Object successor = succIt.next(); JPegStmt successor = (JPegStmt) succIt.next(); List<JPegStmt> predList = unitToPreds.get(successor); if (predList != null && !predList.contains(s)) { try { predList.add(s); /* Tag tag = (Tag)successor.getTags().get(0); System.out.println("add "+s+" to predlist of "+tag+" "+successor); */ } catch (NullPointerException e) { System.out.println(s + "successor: " + successor); throw e; } } } } } } // end add Nov 1 // System.out.println("stmt: "+stmt); // remove stmt from allNodes and mainPegChain // System.out.println("mainPegChain contains stmt: "+mainPegChain.contains(stmt)); // testPegChain(); if (!allNodes.contains(stmt)) { System.err.println("fail to find begin node in allNodes!"); System.exit(1); } else { allNodes.remove(stmt); // System.out.println("remove from allNode: "+stmt); } if (!chain.contains(stmt)) { System.err.println("Error! Chain does not contains stmt (extending point)!"); System.exit(1); } else { if (!chain.remove(stmt)) { System.err.println("fail to remove invoke stmt in from Chain!"); System.exit(1); } } /* if (!mainPegChain.contains(stmt)){ boolean find = false; //System.out.println("main chain does not contain AFTER"); Set maps = startToThread.entrySet(); for(Iterator iter=maps.iterator(); iter.hasNext();){ Map.Entry entry = (Map.Entry)iter.next(); Object startNode = entry.getKey(); Iterator runIt = ((List)entry.getValue()).iterator(); while (runIt.hasNext()){ Chain chain=(Chain)runIt.next(); if (chain.contains(stmt)) { find = true; if (!chain.remove(stmt)){ System.err.println("fail to remove begin node in from mainPegChain!"); System.exit(1); } break; } } if (find == false){ System.err.println("fail to find stmt: "+stmt+" in chains!"); System.exit(1); } } //this.toString(); } else{ if (!mainPegChain.remove(stmt)) { System.err.println("fail to remove begin node in from mainPegChain!"); System.exit(1); } else{ // System.out.println("remove(from mainchain): "+stmt); } } */ // remove stmt from unitToSuccs and unitToPreds if (unitToSuccs.containsKey(stmt)) { unitToSuccs.remove(stmt); } if (unitToPreds.containsKey(stmt)) { unitToPreds.remove(stmt); } }