1 package soot.dava.toolkits.base.AST.interProcedural; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 7 import soot.BooleanType; 8 import soot.ByteType; 9 import soot.CharType; 10 import soot.DoubleType; 11 import soot.FloatType; 12 import soot.IntType; 13 import soot.LongType; 14 import soot.PrimType; 15 import soot.ShortType; 16 import soot.SootClass; 17 import soot.SootField; 18 import soot.SootMethod; 19 import soot.Type; 20 21 import soot.Value; 22 import soot.dava.DavaBody; 23 import soot.dava.DecompilationException; 24 25 import soot.dava.internal.AST.ASTNode; 26 import soot.dava.internal.javaRep.DIntConstant; 27 import soot.dava.toolkits.base.AST.traversals.AllDefinitionsFinder; 28 import soot.grimp.internal.GAssignStmt; 29 import soot.jimple.DefinitionStmt; 30 import soot.jimple.DoubleConstant; 31 import soot.jimple.FieldRef; 32 import soot.jimple.FloatConstant; 33 import soot.jimple.LongConstant; 34 35 import soot.tagkit.DoubleConstantValueTag; 36 import soot.tagkit.FloatConstantValueTag; 37 import soot.tagkit.IntegerConstantValueTag; 38 import soot.tagkit.LongConstantValueTag; 39 import soot.tagkit.StringConstantValueTag; 40 import soot.util.Chain; 41 42 57 public class RedundantFieldUseEliminator { 58 public final boolean DEBUG = true; 59 60 String combiner = "$p$g"; 61 62 HashMap fieldToValues = new HashMap (); 63 64 Chain appClasses; 65 66 public RedundantFieldUseEliminator(Chain classes){ 67 appClasses = classes; 68 } 69 70 71 public void applyAnalysis(){ 72 73 76 debug("RedundantFielduseEliminator -- applyAnalyses","computing Method Summaries"); 77 computeFieldToValuesAssignedList(); 78 valuesForPrimTypeFields(); 79 80 } 81 82 86 public void valuesForPrimTypeFields(){ 87 88 HashMap primTypeFieldValueToUse = new HashMap (); 89 90 Iterator classIt = appClasses.iterator(); 92 while(classIt.hasNext()){ 93 SootClass s = (SootClass) classIt.next(); 94 debug("\nvaluesforPrimTypeFields","Processing class "+s.getName()); 95 96 97 String declaringClass = s.getName(); 98 Iterator fieldIt = s.getFields().iterator(); 99 while(fieldIt.hasNext()){ 100 SootField f = (SootField)fieldIt.next(); 101 102 String fieldName = f.getName(); 103 104 Type fieldType = f.getType(); 105 if(! (fieldType instanceof PrimType ) ) 106 continue; 107 108 String combined = declaringClass + combiner + fieldName; 109 110 Object value=null; 111 112 if(fieldType instanceof DoubleType && f.hasTag("DoubleConstantValueTag")){ 114 double val = ((DoubleConstantValueTag)f.getTag("DoubleConstantValueTag")).getDoubleValue(); 115 value = new Double (val); 116 } 117 else if (fieldType instanceof FloatType && f.hasTag("FloatConstantValueTag")){ 118 float val = ((FloatConstantValueTag)f.getTag("FloatConstantValueTag")).getFloatValue(); 119 value = new Float (val); 120 } 121 else if (fieldType instanceof LongType && f.hasTag("LongConstantValueTag")){ 122 long val = ((LongConstantValueTag)f.getTag("LongConstantValueTag")).getLongValue(); 123 value = new Long (val); 124 } 125 else if (fieldType instanceof CharType && f.hasTag("IntegerConstantValueTag")){ 126 int val = ((IntegerConstantValueTag)f.getTag("IntegerConstantValueTag")).getIntValue(); 127 value = new Integer (val); 128 } 129 else if (fieldType instanceof BooleanType && f.hasTag("IntegerConstantValueTag")){ 130 int val = ((IntegerConstantValueTag)f.getTag("IntegerConstantValueTag")).getIntValue(); 131 if (val ==0) 132 value = new Boolean (false); 133 else 134 value = new Boolean (true); 135 } 136 else if ( (fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) && 137 f.hasTag("IntegerConstantValueTag")){ 138 int val = ((IntegerConstantValueTag)f.getTag("IntegerConstantValueTag")).getIntValue(); 139 value = new Integer (val); 140 } 141 142 143 if(value != null){ 145 debug("TAGGED value found for tag"+combined); 146 primTypeFieldValueToUse.put(combined,value); 147 } 148 else{ 149 151 Object temp = fieldToValues.get(combined); 153 if(temp == null){ 154 156 158 if(fieldType instanceof DoubleType ) 159 value = new Double (0); 160 else if (fieldType instanceof FloatType ) 161 value = new Float (0); 162 else if (fieldType instanceof LongType ) 163 value = new Long (0); 164 else if (fieldType instanceof BooleanType) 165 value = new Boolean (false); 166 else if ( (fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) || fieldType instanceof CharType){ 167 value = new Integer (0); 168 } 169 else 170 throw new DecompilationException("Unknown primitive type...please report to developer"); 171 172 primTypeFieldValueToUse.put(combined,value); 173 debug("DEFAULT value found for tag"+combined); 174 } 175 else{ 176 debug("CHECKING USER ASSIGNED VALUES FOR"+combined); 178 179 } 180 } 181 182 183 184 185 186 187 188 189 190 192 194 195 197 199 200 201 202 203 204 205 } 206 } 207 } 208 209 213 public void computeFieldToValuesAssignedList(){ 214 Iterator classIt = appClasses.iterator(); 216 while(classIt.hasNext()){ 217 SootClass s = (SootClass) classIt.next(); 218 debug("\ncomputeMethodSummaries","Processing class "+s.getName()); 219 220 Iterator methodIt = s.methodIterator(); 222 while (methodIt.hasNext()) { 223 SootMethod m = (SootMethod) methodIt.next(); 224 DavaBody body = (DavaBody)m.getActiveBody(); 225 ASTNode AST = (ASTNode) body.getUnits().getFirst(); 226 227 AllDefinitionsFinder defFinder = new AllDefinitionsFinder(); 229 AST.apply(defFinder); 230 Iterator allDefIt = defFinder.getAllDefs().iterator(); 231 232 while(allDefIt.hasNext()){ 234 DefinitionStmt stmt = (DefinitionStmt)allDefIt.next(); 235 Value left = stmt.getLeftOp(); 237 238 241 if(! (left instanceof FieldRef) ){ 242 continue; 243 } 244 245 debug("computeMethodSummaries method: "+m.getName(),"Field ref is: "+left); 247 249 FieldRef ref = (FieldRef)left; 250 SootField field = ref.getField(); 251 252 253 256 if(!( field.getType() instanceof PrimType)) 257 continue; 258 259 260 261 String fieldName = field.getName(); 262 String declaringClass = field.getDeclaringClass().getName(); 263 264 debug("\tField Name: "+ fieldName); 265 debug("\tField DeclaringClass: "+ declaringClass); 266 267 String combined = declaringClass + combiner + fieldName; 269 Object temp = fieldToValues.get(combined); 270 271 ArrayList valueList; 272 if(temp == null){ 273 valueList = new ArrayList (); 275 fieldToValues.put(combined,valueList); 276 } 277 else{ 278 valueList = (ArrayList )temp; 279 } 280 281 valueList.add(stmt.getRightOp()); 282 } } } } 286 287 288 289 public void debug(String methodName, String debug){ 290 if(DEBUG) 291 System.out.println(methodName+ " DEBUG: "+debug); 292 } 293 294 295 public void debug(String debug){ 296 if(DEBUG) 297 System.out.println("DEBUG: "+debug); 298 } 299 300 } 301 | Popular Tags |