1 19 20 25 26 27 28 package soot.jimple.toolkits.scalar; 29 30 import soot.util.*; 31 import soot.*; 32 import soot.jimple.*; 33 import java.io.*; 34 import java.util.*; 35 36 37 public class Evaluator { 38 39 public static boolean isValueConstantValued(Value op) { 40 41 if (op instanceof Constant) 42 return true; 43 else if ((op instanceof UnopExpr)) { 44 if (isValueConstantValued(((UnopExpr)op).getOp())) 45 return true; 46 } 47 else if (op instanceof BinopExpr) 48 { 49 50 if (op instanceof DivExpr || op instanceof RemExpr) 51 { 52 if (!isValueConstantValued(((BinopExpr)op).getOp1()) || 53 !isValueConstantValued(((BinopExpr)op).getOp2())) 54 return false; 55 56 Value c1 = getConstantValueOf(((BinopExpr)op).getOp1()); 57 Value c2 = getConstantValueOf(((BinopExpr)op).getOp2()); 58 59 60 if (c2 instanceof IntConstant && ((IntConstant)c2).value == 0) 61 return false; 62 63 if (c2 instanceof LongConstant && 64 ((LongConstant)c2).value == 0) 65 return false; 66 } 67 68 if (isValueConstantValued(((BinopExpr)op).getOp1()) && 69 isValueConstantValued(((BinopExpr)op).getOp2())) 70 return true; 71 } 72 return false; 73 } 75 77 public static Value getConstantValueOf(Value op) { 78 79 if (!isValueConstantValued(op)) 80 return null; 81 82 if (op instanceof Constant) 83 return op; 84 else if (op instanceof UnopExpr) { 85 Value c = getConstantValueOf(((UnopExpr)op).getOp()); 86 if (op instanceof NegExpr) 87 return ((NumericConstant)c).negate(); 88 } 89 else if (op instanceof BinopExpr) { 90 Value c1 = getConstantValueOf(((BinopExpr)op).getOp1()); 91 Value c2 = getConstantValueOf(((BinopExpr)op).getOp2()); 92 if (op instanceof AddExpr) 93 return ((NumericConstant)c1).add((NumericConstant)c2); 94 else if (op instanceof SubExpr) 95 return ((NumericConstant)c1).subtract((NumericConstant)c2); 96 else if (op instanceof MulExpr) 97 return ((NumericConstant)c1).multiply((NumericConstant)c2); 98 else if (op instanceof DivExpr) 100 return ((NumericConstant)c1).divide((NumericConstant)c2); 101 else if (op instanceof RemExpr) 102 return ((NumericConstant)c1).remainder((NumericConstant)c2); 103 else if (op instanceof EqExpr || op instanceof NeExpr) 104 { 105 if (c1 instanceof NumericConstant) 106 { 107 if (op instanceof EqExpr) 108 return ((NumericConstant)c1).equalEqual 109 ((NumericConstant)c2); 110 else if (op instanceof NeExpr) 111 return ((NumericConstant)c1).notEqual 112 ((NumericConstant)c2); 113 } 114 else if (c1 instanceof StringConstant) 115 { 116 boolean equality = ((StringConstant)c1).equals 117 ((StringConstant)c2); 118 119 boolean truth = (op instanceof EqExpr) ? equality : 120 !equality; 121 122 IntConstant beauty = IntConstant.v(truth ? 1 : 0); 124 return beauty; 125 } 126 else if (c1 instanceof NullConstant) 127 return IntConstant.v 128 (((NullConstant)c1).equals(c2) ? 1 : 0); 129 throw new RuntimeException 130 ("constant neither numeric nor string"); 131 } 132 else if (op instanceof GtExpr) 133 return ((NumericConstant)c1).greaterThan((NumericConstant)c2); 134 else if (op instanceof GeExpr) 135 return ((NumericConstant)c1).greaterThanOrEqual((NumericConstant)c2); 136 else if (op instanceof LtExpr) 137 return ((NumericConstant)c1).lessThan((NumericConstant)c2); 138 else if (op instanceof LeExpr) 139 return ((NumericConstant)c1).lessThanOrEqual((NumericConstant)c2); 140 else if (op instanceof AndExpr) 141 return ((ArithmeticConstant)c1).and((ArithmeticConstant)c2); 142 else if (op instanceof OrExpr) 143 return ((ArithmeticConstant)c1).or((ArithmeticConstant)c2); 144 else if (op instanceof XorExpr) 145 return ((ArithmeticConstant)c1).xor((ArithmeticConstant)c2); 146 else if (op instanceof ShlExpr) 147 return ((ArithmeticConstant)c1).shiftLeft((ArithmeticConstant)c2); 148 else if (op instanceof ShrExpr) 149 return ((ArithmeticConstant)c1).shiftRight((ArithmeticConstant)c2); 150 else if (op instanceof UshrExpr) 151 return ((ArithmeticConstant)c1).unsignedShiftRight((ArithmeticConstant)c2); 152 else if (op instanceof CmpExpr) { 153 if ((c1 instanceof LongConstant) && 154 (c2 instanceof LongConstant)) 155 return ((LongConstant)c1).cmp((LongConstant)c2); 156 else throw new IllegalArgumentException ( 157 "CmpExpr: LongConstant(s) expected"); 158 } 159 else if ((op instanceof CmpgExpr) || (op instanceof CmplExpr)) { 160 if ((c1 instanceof RealConstant) && 161 (c2 instanceof RealConstant)) { 162 163 if(op instanceof CmpgExpr) 164 return ((RealConstant) c1).cmpg((RealConstant) c2); 165 else if(op instanceof CmplExpr) 166 return ((RealConstant) c1).cmpl((RealConstant) c2); 167 168 } 169 else throw new IllegalArgumentException ( 170 "CmpExpr: RealConstant(s) expected"); 171 } 172 else 173 throw new RuntimeException ("unknown binop: " + op); 174 } 175 176 throw new RuntimeException ("couldn't getConstantValueOf of: " + op); 177 } 179 } 181 182 183 184 185 | Popular Tags |