1 19 20 package soot.dava.toolkits.base.AST.transformations; 21 22 import soot.*; 23 import java.util.*; 24 import soot.jimple.*; 25 import soot.dava.internal.SET.*; 26 import soot.dava.internal.AST.*; 27 import soot.dava.internal.asg.*; 28 import soot.dava.internal.javaRep.*; 29 import soot.dava.toolkits.base.AST.analysis.*; 30 31 66 67 public class StrengthenByIfElse{ 68 72 public static List getNewNode(ASTNode loopNode, ASTIfElseNode ifElseNode){ 73 List elseBody = ((ASTIfElseNode)ifElseNode).getElseBody(); 75 if(elseBody.size()!=1){ 76 return null; 78 } 79 ASTNode tempNode = (ASTNode)elseBody.get(0); 80 if(!(tempNode instanceof ASTStatementSequenceNode)){ 81 return null; 83 } 84 85 List statements = ((ASTStatementSequenceNode)tempNode).getStatements(); 86 Iterator stmtIt = statements.iterator(); 87 while(stmtIt.hasNext()){ 88 AugmentedStmt as = (AugmentedStmt)stmtIt.next(); 89 Stmt stmt = as.get_Stmt(); 90 if(stmt instanceof DAbruptStmt){ 91 DAbruptStmt abStmt = (DAbruptStmt)stmt; 93 if(!(abStmt.is_Break())){ 94 return null; 96 } 97 else{ 98 if(stmtIt.hasNext()){ 99 return null; 101 } 102 SETNodeLabel label = abStmt.getLabel(); 103 String labelBroken=label.toString(); 104 String loopLabel= ((ASTLabeledNode)loopNode).get_Label().toString(); 105 if(labelBroken != null && loopLabel != null){ if(labelBroken.compareTo(loopLabel)==0){ 107 109 if(loopNode instanceof ASTWhileNode){ 112 if(statements.size()!=1){ 113 return null; 115 } 116 } 117 118 119 ASTWhileNode newWhileNode = makeWhileNode(ifElseNode,loopNode); 121 if(newWhileNode == null){ 122 return null; 123 } 124 List toReturn = new ArrayList(); 125 toReturn.add(newWhileNode); 126 127 128 if(statements.size()!=1){ 130 132 Iterator tempIt = statements.iterator(); 133 List newStmts = new ArrayList(); 134 while(tempIt.hasNext()){ 135 Object tempStmt = tempIt.next(); 136 if(tempIt.hasNext()){ 137 newStmts.add(tempStmt); 138 } 139 } 140 toReturn.add(new ASTStatementSequenceNode(newStmts)); 141 } 142 return toReturn; 143 144 } } } } else if (stmt instanceof ReturnStmt || stmt instanceof ReturnVoidStmt){ 149 if(!(loopNode instanceof ASTUnconditionalLoopNode)){ 150 return null; 152 } 153 154 if(stmtIt.hasNext()){ 155 return null; 157 } 158 159 160 ASTWhileNode newWhileNode = makeWhileNode(ifElseNode,loopNode); 162 if(newWhileNode == null){ 163 return null; 164 } 165 List toReturn = new ArrayList(); 166 toReturn.add(newWhileNode); 167 168 Iterator tempIt = statements.iterator(); 170 List newStmts = new ArrayList(); 171 while(tempIt.hasNext()){ 172 Object tempStmt = tempIt.next(); 173 newStmts.add(tempStmt); 174 } 175 toReturn.add(new ASTStatementSequenceNode(newStmts)); 176 return toReturn; 177 } } return null; 180 } 182 183 private static ASTWhileNode makeWhileNode(ASTIfElseNode ifElseNode, ASTNode loopNode){ 184 ASTCondition outerCond = null; 185 ASTCondition innerCond = ifElseNode.get_Condition(); 186 ASTCondition newCond = null; 187 188 if(loopNode instanceof ASTWhileNode){ 189 outerCond=((ASTWhileNode)loopNode).get_Condition(); 190 newCond = new ASTAndCondition(outerCond,innerCond); 191 } 192 else if (loopNode instanceof ASTUnconditionalLoopNode){ 193 newCond = innerCond; 194 } 195 else{ 196 return null; 198 } 199 List loopBody = ifElseNode.getIfBody(); 200 SETNodeLabel newLabel = ((ASTLabeledNode)loopNode).get_Label(); 201 202 return new ASTWhileNode(newLabel,newCond,loopBody); 204 } 205 206 } | Popular Tags |