1 19 20 package soot.shimple.toolkits.scalar; 21 22 import soot.*; 23 import soot.util.*; 24 import soot.jimple.*; 25 import soot.shimple.*; 26 import soot.jimple.toolkits.scalar.*; 27 import java.util.*; 28 29 37 public class SEvaluator 38 { 39 43 public static boolean isValueConstantValued(Value op) 44 { 45 if(op instanceof PhiExpr) { 46 Iterator argsIt = ((PhiExpr) op).getValues().iterator(); 47 Constant firstConstant = null; 48 49 while(argsIt.hasNext()){ 50 Value arg = (Value) argsIt.next(); 51 52 if(!(arg instanceof Constant)) 53 return false; 54 55 if(firstConstant == null) 56 firstConstant = (Constant) arg; 57 else if(!firstConstant.equals(arg)) 58 return false; 59 } 60 61 return true; 62 } 63 64 return Evaluator.isValueConstantValued(op); 65 } 66 67 71 public static Value getConstantValueOf(Value op) 72 { 73 if(!(op instanceof PhiExpr)) 74 return Evaluator.getConstantValueOf(op); 75 76 if(!(isValueConstantValued(op))) 77 return null; 78 79 return (Value) ((PhiExpr) op).getValue(0); 80 } 81 82 96 public static Constant getFuzzyConstantValueOf(Value v) 97 { 98 if(v instanceof Constant) 99 return (Constant) v; 100 101 if(v instanceof Local) 102 return BottomConstant.v(); 103 104 if(!(v instanceof Expr)) 105 return BottomConstant.v(); 106 107 Expr expr = (Expr) v; 108 Constant constant = null; 109 110 if(expr instanceof PhiExpr){ 111 PhiExpr phi = (PhiExpr) expr; 112 Iterator argsIt = phi.getValues().iterator(); 113 114 while(argsIt.hasNext()){ 115 Value arg = (Value) argsIt.next(); 116 117 if(!(arg instanceof Constant)) 118 continue; 119 120 if(arg instanceof TopConstant) 121 continue; 122 123 if(constant == null) 124 constant = (Constant) arg; 125 else if(!constant.equals(arg)){ 126 constant = BottomConstant.v(); 127 break; 128 } 129 } 130 131 if(constant == null) 132 constant = TopConstant.v(); 133 } 134 else{ 135 Iterator valueBoxesIt = expr.getUseBoxes().iterator(); 136 while(valueBoxesIt.hasNext()){ 137 Value value = ((ValueBox)valueBoxesIt.next()).getValue(); 138 139 if(value instanceof BottomConstant){ 140 constant = BottomConstant.v(); 141 break; 142 } 143 144 if(value instanceof TopConstant) 145 constant = TopConstant.v(); 146 } 147 148 if(constant == null) 149 constant = (Constant) getConstantValueOf(expr); 150 151 if(constant == null) 152 constant = BottomConstant.v(); 153 } 154 155 return constant; 156 } 157 158 166 public static Constant getFuzzyConstantValueOf(Value v, Map localToConstant) 167 { 168 if(v instanceof Constant) 169 return (Constant) v; 170 171 if(v instanceof Local) 172 return (Constant) localToConstant.get(v); 173 174 if(!(v instanceof Expr)) 175 return BottomConstant.v(); 176 177 178 179 Expr expr = (Expr) v.clone(); 180 Iterator useBoxIt = expr.getUseBoxes().iterator(); 181 182 while(useBoxIt.hasNext()){ 183 ValueBox useBox = (ValueBox) useBoxIt.next(); 184 Value use = useBox.getValue(); 185 if(use instanceof Local){ 186 Constant constant = (Constant) localToConstant.get(use); 187 if(useBox.canContainValue(constant)) 188 useBox.setValue(constant); 189 } 190 } 191 192 if(expr instanceof UnitBoxOwner) 194 ((UnitBoxOwner)expr).clearUnitBoxes(); 195 196 197 198 return(getFuzzyConstantValueOf(expr)); 199 } 200 201 204 public static abstract class MetaConstant extends Constant 205 { 206 } 207 208 211 public static class TopConstant extends MetaConstant 212 { 213 private static final TopConstant constant = new TopConstant(); 214 215 private TopConstant() {} 216 217 public static Constant v() 218 { 219 return constant; 220 } 221 222 public Type getType() 223 { 224 return UnknownType.v(); 225 } 226 227 public void apply(Switch sw) 228 { 229 throw new RuntimeException ("Not implemented."); 230 } 231 } 232 233 236 public static class BottomConstant extends MetaConstant 237 { 238 private static final BottomConstant constant = new BottomConstant(); 239 240 private BottomConstant() {} 241 242 public static Constant v() 243 { 244 return constant; 245 } 246 247 public Type getType() 248 { 249 return UnknownType.v(); 250 } 251 252 public void apply(Switch sw) 253 { 254 throw new RuntimeException ("Not implemented."); 255 } 256 } 257 } 258 259 | Popular Tags |