1 19 20 package soot.dava.toolkits.base.AST.transformations; 21 22 import soot.dava.toolkits.base.AST.analysis.*; 23 import soot.dava.internal.javaRep.*; 24 import soot.dava.internal.AST.*; 25 import soot.*; 26 import soot.jimple.*; 27 import soot.grimp.internal.*; 28 29 33 public class BooleanConditionSimplification extends DepthFirstAdapter{ 34 35 public BooleanConditionSimplification(boolean verbose){ 36 super(verbose); 37 } 38 39 40 public void caseASTStatementSequenceNode(ASTStatementSequenceNode node){ 41 } 42 43 public BooleanConditionSimplification(){ 44 } 45 50 public void outASTIfNode(ASTIfNode node){ 51 ASTCondition condition = node.get_Condition(); 52 if(condition instanceof ASTBinaryCondition){ 53 ConditionExpr condExpr = ((ASTBinaryCondition)condition).getConditionExpr(); 54 Value unary = checkBooleanUse(condExpr); 55 if(unary != null){ 56 node.set_Condition(new ASTUnaryCondition(unary)); 57 } 58 } 59 } 60 61 public void outASTIfElseNode(ASTIfElseNode node){ 62 ASTCondition condition = node.get_Condition(); 63 if(condition instanceof ASTBinaryCondition){ 64 ConditionExpr condExpr = ((ASTBinaryCondition)condition).getConditionExpr(); 65 Value unary = checkBooleanUse(condExpr); 66 if(unary != null){ 67 node.set_Condition(new ASTUnaryCondition(unary)); 68 } 69 } 70 } 71 72 public void outASTWhileNode(ASTWhileNode node){ 73 ASTCondition condition = node.get_Condition(); 74 if(condition instanceof ASTBinaryCondition){ 75 ConditionExpr condExpr = ((ASTBinaryCondition)condition).getConditionExpr(); 76 Value unary = checkBooleanUse(condExpr); 77 if(unary != null){ 78 node.set_Condition(new ASTUnaryCondition(unary)); 79 } 80 } 81 } 82 83 public void outASTDoWhileNode(ASTDoWhileNode node){ 84 ASTCondition condition = node.get_Condition(); 85 if(condition instanceof ASTBinaryCondition){ 86 ConditionExpr condExpr = ((ASTBinaryCondition)condition).getConditionExpr(); 87 Value unary = checkBooleanUse(condExpr); 88 if(unary != null){ 89 node.set_Condition(new ASTUnaryCondition(unary)); 90 } 91 } 92 } 93 94 private Value checkBooleanUse(ConditionExpr condition){ 95 if(condition instanceof NeExpr || condition instanceof EqExpr){ 97 Value op1 = condition.getOp1(); 98 Value op2 = condition.getOp2(); 99 if(op1 instanceof DIntConstant){ 100 Type op1Type = ((DIntConstant)op1).type; 101 if( op1Type instanceof BooleanType){ 102 return decideCondition(op2,((DIntConstant)op1).toString(),condition); 103 } 104 } 105 else if(op2 instanceof DIntConstant){ 106 Type op2Type = ((DIntConstant)op2).type; 107 if( op2Type instanceof BooleanType){ 108 return decideCondition(op1,((DIntConstant)op2).toString(),condition); 109 } 110 } 111 112 else 113 return null; } 115 return null; } 117 118 125 private Value decideCondition(Value A, String truthString, ConditionExpr condition){ 126 int truthValue =0; 127 boolean notEqual=false; 128 129 if(truthString.compareTo("false")==0) 131 truthValue=0; 132 else if(truthString.compareTo("true")==0) 133 truthValue=1; 134 else 135 throw new RuntimeException (); 136 137 138 139 if(condition instanceof NeExpr ){ 141 notEqual=true; 142 } 143 else if( condition instanceof EqExpr){ 144 notEqual=false; 145 } 146 else 147 throw new RuntimeException (); 148 149 150 if( notEqual && truthValue==0){ return A; 153 } 154 else if(notEqual && truthValue==1){ if(A instanceof DNotExpr){ return ((DNotExpr)A).getOp(); 157 } 158 else 159 return (new DNotExpr(A)); 160 } 161 else if(!notEqual && truthValue==0){ if(A instanceof DNotExpr){ return ((DNotExpr)A).getOp(); 164 } 165 else 166 return new DNotExpr(A); 167 } 168 else if(!notEqual && truthValue==1){ return A; 170 } 171 else 172 throw new RuntimeException (); 173 } 174 175 } | Popular Tags |