001: package soot.dava.toolkits.base.AST.transformations;
002:
003: import soot.Value;
004: import soot.ValueBox;
005: import soot.dava.internal.javaRep.DCmpExpr;
006: import soot.dava.internal.javaRep.DCmpgExpr;
007: import soot.dava.internal.javaRep.DCmplExpr;
008: import soot.dava.toolkits.base.AST.analysis.DepthFirstAdapter;
009: import soot.jimple.AddExpr;
010: import soot.jimple.BinopExpr;
011: import soot.jimple.DoubleConstant;
012: import soot.jimple.FloatConstant;
013: import soot.jimple.IntConstant;
014: import soot.jimple.LongConstant;
015: import soot.jimple.MulExpr;
016: import soot.jimple.NumericConstant;
017: import soot.jimple.SubExpr;
018:
019: /*
020: * x = 2+3 should be simplified to x =5
021: * 4l -3l should be 1l DONE
022:
023: * Unary Condition:DONT NEED TO HANDLE IT since what would simplify
024: * in a boolean flag which is what unary conditions are
025: *
026: * Binary Codition: has a ConditionExpr stored in it not a valuebox???
027: * all other expression to be handled by caseExprOrRefValueBox
028: */
029:
030: public class SimplifyExpressions extends DepthFirstAdapter {
031: public static boolean DEBUG = false;
032:
033: public SimplifyExpressions() {
034: super ();
035: }
036:
037: public SimplifyExpressions(boolean verbose) {
038: super (verbose);
039: }
040:
041: /* public void inASTBinaryCondition(ASTBinaryCondition cond){
042: ConditionExpr condExpr = cond.getConditionExpr();
043:
044: ValueBox op1Box = condExpr.getOp1Box();
045:
046: ValueBox op2Box = condExpr.getOp2Box();
047: }
048: */
049:
050: public void outExprOrRefValueBox(ValueBox vb) {
051: //System.out.println("here"+vb);
052: Value v = vb.getValue();
053: if (!(v instanceof BinopExpr)) {
054: return;
055: }
056:
057: BinopExpr binop = (BinopExpr) v;
058: if (DEBUG)
059: System.out.println("calling getResult");
060: NumericConstant constant = getResult(binop);
061:
062: if (constant == null)
063: return;
064: if (DEBUG)
065: System.out.println("Changin" + vb + " to...." + constant);
066: vb.setValue(constant);
067: }
068:
069: public NumericConstant getResult(BinopExpr binop) {
070: if (DEBUG)
071: System.out.println("Binop expr" + binop);
072: Value leftOp = binop.getOp1();
073: Value rightOp = binop.getOp2();
074:
075: int op = 0;
076: if (binop instanceof AddExpr) {
077: op = 1;
078: } else if (binop instanceof SubExpr
079: || binop instanceof DCmpExpr
080: || binop instanceof DCmpgExpr
081: || binop instanceof DCmplExpr) {
082: op = 2;
083: } else if (binop instanceof MulExpr) {
084: op = 3;
085: }
086:
087: if (op == 0) {
088: if (DEBUG) {
089: System.out.println("not add sub or mult");
090: System.out.println(binop.getClass().getName());
091: }
092: return null;
093: }
094: NumericConstant constant = null;
095: if (leftOp instanceof LongConstant
096: && rightOp instanceof LongConstant) {
097: if (DEBUG)
098: System.out.println("long constants!!");
099: if (op == 1)
100: constant = ((LongConstant) leftOp)
101: .add((LongConstant) rightOp);
102: else if (op == 2)
103: constant = ((LongConstant) leftOp)
104: .subtract((LongConstant) rightOp);
105: else if (op == 3)
106: constant = ((LongConstant) leftOp)
107: .multiply((LongConstant) rightOp);
108: } else if (leftOp instanceof DoubleConstant
109: && rightOp instanceof DoubleConstant) {
110: if (DEBUG)
111: System.out.println("double constants!!");
112: if (op == 1)
113: constant = ((DoubleConstant) leftOp)
114: .add((DoubleConstant) rightOp);
115: else if (op == 2)
116: constant = ((DoubleConstant) leftOp)
117: .subtract((DoubleConstant) rightOp);
118: else if (op == 3)
119: constant = ((DoubleConstant) leftOp)
120: .multiply((DoubleConstant) rightOp);
121:
122: } else if (leftOp instanceof FloatConstant
123: && rightOp instanceof FloatConstant) {
124: if (DEBUG)
125: System.out.println("Float constants!!");
126: if (op == 1)
127: constant = ((FloatConstant) leftOp)
128: .add((FloatConstant) rightOp);
129: else if (op == 2)
130: constant = ((FloatConstant) leftOp)
131: .subtract((FloatConstant) rightOp);
132: else if (op == 3)
133: constant = ((FloatConstant) leftOp)
134: .multiply((FloatConstant) rightOp);
135: } else if (leftOp instanceof IntConstant
136: && rightOp instanceof IntConstant) {
137: if (DEBUG)
138: System.out.println("Integer constants!!");
139: if (op == 1)
140: constant = ((IntConstant) leftOp)
141: .add((IntConstant) rightOp);
142: else if (op == 2)
143: constant = ((IntConstant) leftOp)
144: .subtract((IntConstant) rightOp);
145: else if (op == 3)
146: constant = ((IntConstant) leftOp)
147: .multiply((IntConstant) rightOp);
148: }
149:
150: return constant;
151: }
152:
153: }
|