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 32 85 86 public class PushLabeledBlockIn extends DepthFirstAdapter{ 87 88 public PushLabeledBlockIn(){ 89 } 90 public PushLabeledBlockIn(boolean verbose){ 91 super(verbose); 92 } 93 94 public void caseASTStatementSequenceNode(ASTStatementSequenceNode node){ 95 } 96 97 98 99 public void outASTLabeledBlockNode(ASTLabeledBlockNode node){ 100 String label = node.get_Label().toString(); 101 List subBodies = (List)node.get_SubBodies(); 102 if(subBodies.size()!=1){ 103 return; 104 } 105 List subBody = (List)subBodies.get(0); 106 int nodeNumber=checkForBreak(subBody,label); 107 if(nodeNumber>-1){ 108 if(subBody.size()<nodeNumber){ 111 throw new RuntimeException ("Please submit this benchmark as a bug"); 113 } 114 115 if(nodeNumber+1!=subBody.size()){ 118 return; 120 } 121 122 ASTNode temp = (ASTNode)subBody.get(nodeNumber); 124 if(!(temp instanceof ASTLabeledNode)){ 125 return; 127 } 128 129 ASTLabeledNode tempNode = (ASTLabeledNode)temp; 130 String innerLabel = tempNode.get_Label().toString(); 132 if (innerLabel!=null){ 133 136 if(subBody.size()==1){ 137 152 153 boolean done=replaceBreakLabels(temp,label,innerLabel); 155 if(done){ 156 node.set_Label(new SETNodeLabel()); 158 G.v().ASTTransformations_modified = true; 159 } 160 } 161 162 return; 163 } 164 else{ 165 SETNodeLabel newLabel = new SETNodeLabel(); 168 newLabel.set_Name(label); 169 tempNode.set_Label(newLabel); 170 node.set_Label(new SETNodeLabel()); 171 G.v().ASTTransformations_modified = true; 172 } 173 174 } 175 } 176 private boolean replaceBreakLabels(ASTNode node,String toReplace, String replaceWith){ 177 boolean toReturn=false; 178 List subBodies=node.get_SubBodies(); 179 Iterator subIt = subBodies.iterator(); 180 while(subIt.hasNext()){ 181 List subBody=null; 182 if(node instanceof ASTTryNode){ 183 ASTTryNode.container subBodyContainer = (ASTTryNode.container) subIt.next(); 184 subBody=(List)subBodyContainer.o; 185 } 186 else 187 subBody=(List)subIt.next(); 188 189 Iterator it = subBody.iterator(); 190 while(it.hasNext()){ 191 ASTNode temp = (ASTNode)it.next(); 192 if(temp instanceof ASTStatementSequenceNode){ 194 ASTStatementSequenceNode stmtSeq = (ASTStatementSequenceNode)temp; 195 List statements = stmtSeq.getStatements(); 196 Iterator stmtIt = statements.iterator(); 197 while(stmtIt.hasNext()){ 198 AugmentedStmt as = (AugmentedStmt)stmtIt.next(); 199 Stmt s = as.get_Stmt(); 200 String labelBroken = isAbrupt(s); 201 if(labelBroken != null){ if(labelBroken.compareTo(toReplace)==0){ 203 replaceLabel(s,replaceWith); 206 toReturn=true; 207 } 208 } 209 } 210 } else{ 212 boolean returnVal=replaceBreakLabels(temp,toReplace,replaceWith); 214 if(returnVal) 215 toReturn=true; 216 } 217 } } 219 return toReturn; 220 } 221 222 223 224 private int checkForBreak(List ASTNodeBody,String outerLabel){ 225 Iterator it = ASTNodeBody.iterator(); 226 int nodeNumber=0; 227 while(it.hasNext()){ 228 ASTNode temp = (ASTNode)it.next(); 229 if(temp instanceof ASTStatementSequenceNode){ 231 ASTStatementSequenceNode stmtSeq = (ASTStatementSequenceNode)temp; 232 List statements = stmtSeq.getStatements(); 233 Iterator stmtIt = statements.iterator(); 234 while(stmtIt.hasNext()){ 235 AugmentedStmt as = (AugmentedStmt)stmtIt.next(); 236 Stmt s = as.get_Stmt(); 237 String labelBroken = breaksLabel(s); 238 if(labelBroken != null && outerLabel!=null){ if(labelBroken.compareTo(outerLabel)==0){ 240 return nodeNumber; 242 } 243 } 244 } 245 } else{ 247 List subBodies=(List)temp.get_SubBodies(); 250 Iterator subIt = subBodies.iterator(); 251 while(subIt.hasNext()){ 252 253 254 if(temp instanceof ASTTryNode){ 255 ASTTryNode.container subBody = (ASTTryNode.container) subIt.next(); 256 if(checkForBreak((List)subBody.o,outerLabel)> (-1) ){ 257 return nodeNumber; 259 } 260 } 261 else{ 262 if(checkForBreak((List)subIt.next(),outerLabel)> (-1) ){ 263 return nodeNumber; 265 } 266 } 267 } 268 } 269 nodeNumber++; 270 } 272 return -1; 273 } 274 275 276 281 private String breaksLabel(Stmt stmt){ 282 if(!(stmt instanceof DAbruptStmt)){ 283 return null; 285 } 286 DAbruptStmt abStmt = (DAbruptStmt)stmt; 287 if(!abStmt.is_Break()){ 288 return null; 290 } 291 SETNodeLabel label = abStmt.getLabel(); 292 return label.toString(); 293 } 294 295 296 301 private String isAbrupt(Stmt stmt){ 302 if(!(stmt instanceof DAbruptStmt)){ 303 return null; 305 } 306 DAbruptStmt abStmt = (DAbruptStmt)stmt; 307 if(abStmt.is_Break() || abStmt.is_Continue()){ 308 SETNodeLabel label = abStmt.getLabel(); 309 return label.toString(); 310 } 311 else 312 return null; 313 } 314 315 316 private void replaceLabel(Stmt s, String replaceWith){ 317 DAbruptStmt abStmt = (DAbruptStmt)s; 319 SETNodeLabel label = abStmt.getLabel(); 320 label.set_Name(replaceWith); 321 } 322 } 323 324 325 326 327 328 329 330 | Popular Tags |