soot.jimple.BinopExpr Java Examples

The following examples show how to use soot.jimple.BinopExpr. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleVeryBusyExpressions.java    From JAADAS with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Performs kills by generating a killSet and then performing<br/>
 * outSet <- inSet - killSet<br/>
 * The kill set is generated by iterating over the def-boxes
 * of the unit. For each local defined in the unit we iterate
 * over the binopExps in the inSet, and check whether they use
 * that local. If so, it is added to the kill set.
 * @param inSet the set flowing into the unit
 * @param u the unit being flown through
 * @param outSet the set flowing out of the unit
 */
private void kill(FlowSet inSet, Unit u, FlowSet outSet) {
	FlowSet kills = emptySet.clone();
	for (ValueBox defBox : u.getDefBoxes()) {

		if (defBox.getValue() instanceof Local) {
			Iterator<BinopExpr> inIt = inSet.iterator();
			while (inIt.hasNext()) {
				BinopExpr e = inIt.next();
				Iterator<ValueBox> eIt = e.getUseBoxes().iterator();
				while (eIt.hasNext()) {
					ValueBox useBox = eIt.next();
					if (useBox.getValue() instanceof Local &&
							useBox.getValue().equivTo(defBox.getValue()))
						kills.add(e);
				}
			}
		}
	}
	inSet.difference(kills, outSet);
}
 
Example #2
Source File: UnitThrowAnalysis.java    From JAADAS with GNU General Public License v3.0 6 votes vote down vote up
private void caseBinopDivExpr(BinopExpr expr) {
    // Factors out code common to caseDivExpr and caseRemExpr.
    // The checks against constant divisors would perhaps be
    // better performed in a later pass, post-constant-propagation.
    Value divisor = expr.getOp2();
    Type divisorType = divisor.getType();
    if (divisorType instanceof UnknownType) {
	result = result.add(mgr.ARITHMETIC_EXCEPTION);
    } else if ((divisorType instanceof IntegerType) &&
	((! (divisor instanceof IntConstant)) ||
	 (((IntConstant) divisor).equals(INT_CONSTANT_ZERO)))) {
	result = result.add(mgr.ARITHMETIC_EXCEPTION);
    } else if ((divisorType == LongType.v()) &&
	       ((! (divisor instanceof LongConstant)) ||
		(((LongConstant) divisor).equals(LONG_CONSTANT_ZERO)))) {
	result = result.add(mgr.ARITHMETIC_EXCEPTION);
    }
    caseBinopExpr(expr);
}
 
Example #3
Source File: SimplifyExpressions.java    From JAADAS with GNU General Public License v3.0 6 votes vote down vote up
public void outExprOrRefValueBox(ValueBox vb){
	//System.out.println("here"+vb);
	Value v = vb.getValue();
	if(! (v instanceof BinopExpr )){
		return;
	}

	BinopExpr binop = (BinopExpr)v;
	if(DEBUG)
		System.out.println("calling getResult");
	NumericConstant constant = getResult(binop);
	
	if(constant ==null)
		return;
	if(DEBUG)
		System.out.println("Changin"+vb+" to...."+constant);
	vb.setValue(constant);
}
 
Example #4
Source File: DavaBody.java    From JAADAS with GNU General Public License v3.0 6 votes vote down vote up
private void javafy_expr(ValueBox vb) {
	Expr e = (Expr) vb.getValue();

	if (e instanceof BinopExpr)
		javafy_binop_expr(vb);
	else if (e instanceof UnopExpr)
		javafy_unop_expr(vb);
	else if (e instanceof CastExpr)
		javafy_cast_expr(vb);
	else if (e instanceof NewArrayExpr)
		javafy_newarray_expr(vb);
	else if (e instanceof NewMultiArrayExpr)
		javafy_newmultiarray_expr(vb);
	else if (e instanceof InstanceOfExpr)
		javafy_instanceof_expr(vb);
	else if (e instanceof InvokeExpr)
		javafy_invoke_expr(vb);
	else if (e instanceof NewExpr)
		javafy_new_expr(vb);
}
 
Example #5
Source File: IfTestInstruction.java    From JAADAS with GNU General Public License v3.0 6 votes vote down vote up
protected IfStmt ifStatement(DexBody body) {
      Instruction22t i = (Instruction22t) instruction;
      Local one = body.getRegisterLocal(i.getRegisterA());
      Local other = body.getRegisterLocal(i.getRegisterB());
      BinopExpr condition = getComparisonExpr(one, other);
      jif = (JIfStmt)Jimple.v().newIfStmt(condition, targetInstruction.getUnit());
      // setUnit() is called in ConditionalJumpInstruction

if (IDalvikTyper.ENABLE_DVKTYPER) {
    Debug.printDbg(IDalvikTyper.DEBUG, "constraint if: "+ jif +" condition: "+ condition);
    DalvikTyper.v().addConstraint(condition.getOp1Box(), condition.getOp2Box());
      }
      
      
      return jif;
      
  }
 
Example #6
Source File: SimpleVeryBusyExpressions.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Performs gens by iterating over the units use-boxes.
 * If the value of a use-box is a binopExp then we add
 * it to the outSet.
 * @param outSet the set flowing out of the unit
 * @param u the unit being flown through
 */
private void gen(FlowSet outSet, Unit u) {
	for (ValueBox useBox : u.getUseBoxes()) {

		if (useBox.getValue() instanceof BinopExpr)
			outSet.add(useBox.getValue());
	}
}
 
Example #7
Source File: IfTestzInstruction.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
protected IfStmt ifStatement(DexBody body) {
      Instruction21t i = (Instruction21t) instruction;
      BinopExpr condition = getComparisonExpr(body, i.getRegisterA());
      jif = (JIfStmt) Jimple.v().newIfStmt(condition,
                                  targetInstruction.getUnit());
      // setUnit() is called in ConditionalJumpInstruction
      
      
if (IDalvikTyper.ENABLE_DVKTYPER) {
	Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ jif);
         int op = instruction.getOpcode().value;
         switch (op) {
         case 0x38:
         case 0x39:
           //DalvikTyper.v().addConstraint(condition.getOp1Box(), condition.getOp2Box());
           break;
         case 0x3a:
         case 0x3b:
         case 0x3c:
         case 0x3d:
           DalvikTyper.v().setType(condition.getOp1Box(), BooleanType.v(), true);
           break;
         default:
           throw new RuntimeException("error: unknown op: 0x"+ Integer.toHexString(op));
         }
      }

return jif;
      
  }
 
Example #8
Source File: Binop2addrInstruction.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public void jimplify (DexBody body) {
     if(!(instruction instanceof Instruction12x))
         throw new IllegalArgumentException("Expected Instruction12x but got: "+instruction.getClass());

     Instruction12x binOp2AddrInstr = (Instruction12x)instruction;
     int dest = binOp2AddrInstr.getRegisterA();

     Local source1 = body.getRegisterLocal(binOp2AddrInstr.getRegisterA());
     Local source2 = body.getRegisterLocal(binOp2AddrInstr.getRegisterB());

     expr = getExpression(source1, source2);

     assign = Jimple.v().newAssignStmt(body.getRegisterLocal(dest), expr);
     assign.addTag(getTag());

     setUnit(assign);
     addTags(assign);
     body.add(assign);
     
     if (IDalvikTyper.ENABLE_DVKTYPER) {
Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ assign);
       BinopExpr bexpr = (BinopExpr)expr;
       short op = instruction.getOpcode().value;
       DalvikTyper.v().setType(bexpr.getOp1Box(), op1BinType[op-0xb0], true);
       DalvikTyper.v().setType(bexpr.getOp2Box(), op2BinType[op-0xb0], true);
       DalvikTyper.v().setType(assign.getLeftOpBox(), resBinType[op-0xb0], false);
     }
 }
 
Example #9
Source File: BinopLitInstruction.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public void jimplify (DexBody body) {
     if(!(instruction instanceof Instruction22s) && !(instruction instanceof Instruction22b))
         throw new IllegalArgumentException("Expected Instruction22s or Instruction22b but got: "+instruction.getClass());

     NarrowLiteralInstruction binOpLitInstr = (NarrowLiteralInstruction) this.instruction;
     int dest = ((TwoRegisterInstruction) instruction).getRegisterA();
     int source = ((TwoRegisterInstruction) instruction).getRegisterB();

     Local source1 = body.getRegisterLocal(source);

     IntConstant constant = IntConstant.v((int)binOpLitInstr.getNarrowLiteral());

     expr = getExpression(source1, constant);

     assign = Jimple.v().newAssignStmt(body.getRegisterLocal(dest), expr);
     assign.addTag(getTag());

     setUnit(assign);
     addTags(assign);
     body.add(assign);
     
     if (IDalvikTyper.ENABLE_DVKTYPER) {
Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ assign);
         int op = (int)instruction.getOpcode().value;
       if (op >= 0xd8) {
         op -= 0xd8;
       } else {
         op -= 0xd0;
       }
       BinopExpr bexpr = (BinopExpr)expr;
       //body.dvkTyper.setType((op == 1) ? bexpr.getOp2Box() : bexpr.getOp1Box(), op1BinType[op]);
       DalvikTyper.v().setType(((JAssignStmt)assign).leftBox, op1BinType[op], false);
     }
 }
 
Example #10
Source File: BinopInstruction.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public void jimplify (DexBody body) {
     if(!(instruction instanceof Instruction23x))
         throw new IllegalArgumentException("Expected Instruction23x but got: "+instruction.getClass());

     Instruction23x binOpInstr = (Instruction23x)instruction;
     int dest = binOpInstr.getRegisterA();

     Local source1 = body.getRegisterLocal(binOpInstr.getRegisterB());
     Local source2 = body.getRegisterLocal(binOpInstr.getRegisterC());

     expr = getExpression(source1, source2);

     assign = Jimple.v().newAssignStmt(body.getRegisterLocal(dest), expr);
     assign.addTag(getTag());
     
     setUnit(assign);
     addTags(assign);
     body.add(assign);
     
     if (IDalvikTyper.ENABLE_DVKTYPER) {
Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ assign);
       int op = (int)instruction.getOpcode().value;
       BinopExpr bexpr = (BinopExpr)expr;
       JAssignStmt jassign = (JAssignStmt)assign;
       DalvikTyper.v().setType(bexpr.getOp1Box(), op1BinType[op-0x90], true);
       DalvikTyper.v().setType(bexpr.getOp2Box(), op2BinType[op-0x90], true);
       DalvikTyper.v().setType(jassign.leftBox, resBinType[op-0x90], false);
     }
 }
 
Example #11
Source File: NullCheckEliminator.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public void internalTransform(Body body, String phaseName, Map<String,String> options) {

	// really, the analysis should be able to use its own results to determine
	// that some branches are dead, but since it doesn't we just iterate.
	boolean changed;
	do {
	    changed=false;

	    NullnessAnalysis analysis=analysisFactory.newAnalysis(new ExceptionalUnitGraph(body));
	    
	    Chain<Unit> units=body.getUnits();
	    Stmt s;
	    for(s=(Stmt) units.getFirst();s!=null;s=(Stmt) units.getSuccOf(s)) {
		if(!(s instanceof IfStmt)) continue;
		IfStmt is=(IfStmt) s;
		Value c=is.getCondition();
		if(!(c instanceof EqExpr || c instanceof NeExpr)) continue;
		BinopExpr e=(BinopExpr) c;
		Immediate i=null;
		if(e.getOp1() instanceof NullConstant) i=(Immediate) e.getOp2();
		if(e.getOp2() instanceof NullConstant) i=(Immediate) e.getOp1();
		if(i==null) continue;
		boolean alwaysNull = analysis.isAlwaysNullBefore(s, i);
		boolean alwaysNonNull = analysis.isAlwaysNonNullBefore(s, i);
		int elim=0; // -1 => condition is false, 1 => condition is true
		if(alwaysNonNull) elim=c instanceof EqExpr ? -1 : 1;
		if(alwaysNull) elim=c instanceof EqExpr ? 1 : -1;
		Stmt newstmt=null;
		if(elim==-1) newstmt=Jimple.v().newNopStmt();
		if(elim==1) newstmt=Jimple.v().newGotoStmt(is.getTarget());
		if(newstmt!=null) {
		    units.swapWith(s,newstmt);
		    s=newstmt;
		    changed=true;
		}
	    }
	} while(changed);
    }
 
Example #12
Source File: Walker.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public void outABinopExpr(ABinopExpr node)
   {
Value right = (Value) mProductions.removeLast();
BinopExpr expr = (BinopExpr) mProductions.removeLast();
Value left = (Value) mProductions.removeLast();


       expr.setOp1(left);
       expr.setOp2(right);
mProductions.addLast(expr);
   }
 
Example #13
Source File: ConstraintCollector.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public void caseIfStmt(IfStmt stmt) {
	if (uses) {
		ConditionExpr cond = (ConditionExpr) stmt.getCondition();

		BinopExpr expr = cond;
		Value lv = expr.getOp1();
		Value rv = expr.getOp2();

		TypeVariable lop;
		TypeVariable rop;

		// ******** LEFT ********
		if (lv instanceof Local) {
			lop = resolver.typeVariable((Local) lv);
		} else if (lv instanceof DoubleConstant) {
			lop = resolver.typeVariable(DoubleType.v());
		} else if (lv instanceof FloatConstant) {
			lop = resolver.typeVariable(FloatType.v());
		} else if (lv instanceof IntConstant) {
			lop = resolver.typeVariable(IntType.v());
		} else if (lv instanceof LongConstant) {
			lop = resolver.typeVariable(LongType.v());
		} else if (lv instanceof NullConstant) {
			lop = resolver.typeVariable(NullType.v());
		} else if (lv instanceof StringConstant) {
			lop = resolver.typeVariable(RefType.v("java.lang.String"));
		} else if (lv instanceof ClassConstant) {
			lop = resolver.typeVariable(RefType.v("java.lang.Class"));
		} else {
			throw new RuntimeException("Unhandled binary expression left operand type: " + lv.getClass());
		}

		// ******** RIGHT ********
		if (rv instanceof Local) {
			rop = resolver.typeVariable((Local) rv);
		} else if (rv instanceof DoubleConstant) {
			rop = resolver.typeVariable(DoubleType.v());
		} else if (rv instanceof FloatConstant) {
			rop = resolver.typeVariable(FloatType.v());
		} else if (rv instanceof IntConstant) {
			rop = resolver.typeVariable(IntType.v());
		} else if (rv instanceof LongConstant) {
			rop = resolver.typeVariable(LongType.v());
		} else if (rv instanceof NullConstant) {
			rop = resolver.typeVariable(NullType.v());
		} else if (rv instanceof StringConstant) {
			rop = resolver.typeVariable(RefType.v("java.lang.String"));
		} else if (rv instanceof ClassConstant) {
			rop = resolver.typeVariable(RefType.v("java.lang.Class"));
		} else {
			throw new RuntimeException("Unhandled binary expression right operand type: " + rv.getClass());
		}

		TypeVariable common = resolver.typeVariable();
		rop.addParent(common);
		lop.addParent(common);
	}
}
 
Example #14
Source File: UseChecker.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public void caseIfStmt(IfStmt stmt)
{
	this.handleBinopExpr((BinopExpr)stmt.getCondition(), stmt,
		BooleanType.v());
}
 
Example #15
Source File: CmpInstruction.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public void jimplify (DexBody body) {
      if(!(instruction instanceof Instruction23x))
          throw new IllegalArgumentException("Expected Instruction23x but got: "+instruction.getClass());

      Instruction23x cmpInstr = (Instruction23x)instruction;
      int dest = cmpInstr.getRegisterA();

      Local first = body.getRegisterLocal(cmpInstr.getRegisterB());
      Local second = body.getRegisterLocal(cmpInstr.getRegisterC());

      //Expr cmpExpr;
      //Type type = null
      Opcode opcode = instruction.getOpcode();
      switch (opcode) {
      case CMPL_DOUBLE:
        setTag (new DoubleOpTag());
        type = DoubleType.v();
        cmpExpr = Jimple.v().newCmplExpr(first, second);
        break;
      case CMPL_FLOAT:
        setTag (new FloatOpTag());
        type = FloatType.v();
          cmpExpr = Jimple.v().newCmplExpr(first, second);
          break;
      case CMPG_DOUBLE:
        setTag (new DoubleOpTag());
        type = DoubleType.v();
        cmpExpr = Jimple.v().newCmpgExpr(first, second);
        break;
      case CMPG_FLOAT:
        setTag (new FloatOpTag());
        type = FloatType.v();
          cmpExpr = Jimple.v().newCmpgExpr(first, second);
          break;
      case CMP_LONG:
        setTag (new LongOpTag());
        type = LongType.v();
        cmpExpr = Jimple.v().newCmpExpr(first, second);
        break;
      default:
          throw new RuntimeException("no opcode for CMP: 0x"+ Integer.toHexString(opcode.value));
      }

      assign = Jimple.v().newAssignStmt(body.getRegisterLocal(dest), cmpExpr);
      assign.addTag(getTag());

      setUnit(assign);
      addTags(assign);
      body.add(assign);
      
if (IDalvikTyper.ENABLE_DVKTYPER) {
	Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ assign);
        getTag().getName();
        BinopExpr bexpr = (BinopExpr)cmpExpr;
        DalvikTyper.v().setType(bexpr.getOp1Box(), type, true);
        DalvikTyper.v().setType(bexpr.getOp2Box(), type, true);
        DalvikTyper.v().setType(((JAssignStmt)assign).leftBox, IntType.v(), false);
      }
  }
 
Example #16
Source File: UseChecker.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void handleBinopExpr(BinopExpr be, Stmt stmt, Type tlhs)
{
	Value opl = be.getOp1(), opr = be.getOp2();
	Type tl = AugEvalFunction.eval_(this.tg, opl, stmt, this.jb),
		tr = AugEvalFunction.eval_(this.tg, opr, stmt, this.jb);

	if ( be instanceof AddExpr
		|| be instanceof SubExpr
		|| be instanceof MulExpr
		|| be instanceof DivExpr
		|| be instanceof RemExpr
		|| be instanceof GeExpr
		|| be instanceof GtExpr
		|| be instanceof LeExpr
		|| be instanceof LtExpr
		|| be instanceof ShlExpr
		|| be instanceof ShrExpr
		|| be instanceof UshrExpr )
	{
		if ( tlhs instanceof IntegerType )
		{
			be.setOp1(this.uv.visit(opl, IntType.v(), stmt));
			be.setOp2(this.uv.visit(opr, IntType.v(), stmt));
		}
	}
	else if ( be instanceof CmpExpr
		|| be instanceof CmpgExpr
		|| be instanceof CmplExpr )
	{
		// No checks in the original assigner
	}
	else if ( be instanceof AndExpr
		|| be instanceof OrExpr
		|| be instanceof XorExpr )
	{
		be.setOp1(this.uv.visit(opl, tlhs, stmt));
		be.setOp2(this.uv.visit(opr, tlhs, stmt));
	}
	else if ( be instanceof EqExpr
		|| be instanceof NeExpr )
	{
		if ( tl instanceof BooleanType && tr instanceof BooleanType )
		{ }
		else if ( tl instanceof Integer1Type || tr instanceof Integer1Type )
		{ }
		else if ( tl instanceof IntegerType )
		{
			be.setOp1(this.uv.visit(opl, IntType.v(), stmt));
			be.setOp2(this.uv.visit(opr, IntType.v(), stmt));
		}
	}
}
 
Example #17
Source File: DavaBody.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void javafy_binop_expr(ValueBox vb) {
	BinopExpr boe = (BinopExpr) vb.getValue();

	ValueBox leftOpBox = boe.getOp1Box(), rightOpBox = boe.getOp2Box();
	Value leftOp = leftOpBox.getValue(), rightOp = rightOpBox.getValue();

	if (rightOp instanceof IntConstant) {
		if ((leftOp instanceof IntConstant) == false) {
			javafy(leftOpBox);
			leftOp = leftOpBox.getValue();

			if (boe instanceof ConditionExpr)
				rightOpBox.setValue(DIntConstant.v(
						((IntConstant) rightOp).value, leftOp.getType()));
			else
				rightOpBox.setValue(DIntConstant.v(
						((IntConstant) rightOp).value, null));
		}
	} 
	else if (leftOp instanceof IntConstant) {
		javafy(rightOpBox);
		rightOp = rightOpBox.getValue();

		if (boe instanceof ConditionExpr)
			leftOpBox.setValue(DIntConstant.v(((IntConstant) leftOp).value,
					rightOp.getType()));
		else
			leftOpBox.setValue(DIntConstant.v(((IntConstant) leftOp).value,
					null));
	} else {
		javafy(rightOpBox);
		rightOp = rightOpBox.getValue();

		javafy(leftOpBox);
		leftOp = leftOpBox.getValue();
	}

	if (boe instanceof CmpExpr)
		vb.setValue(new DCmpExpr(leftOp, rightOp));

	else if (boe instanceof CmplExpr)
		vb.setValue(new DCmplExpr(leftOp, rightOp));

	else if (boe instanceof CmpgExpr)
		vb.setValue(new DCmpgExpr(leftOp, rightOp));
}
 
Example #18
Source File: CP.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void handleMathematical(CPFlowSet toReturn, Local left, Value right, Object killedValue) {

		// if right expr is a local or field
		Object value = isANotTopConstantInInputSet(toReturn, right);
		if (value != null) {
			// right was a local or field with a value other than top
			// dont send value SEND A CLONE OF VALUE.....IMPORTANT!!!!
			Object toSend = CPHelper.wrapperClassCloner(value);

			if (toSend != null) {
				addOrUpdate(toReturn, left, toSend);
			}

			// return if value was != null as this means that the left was a
			// primitive local assigned some value from the right
			return;
		}

		// if we get here we know that right is not a local or field whose value
		// we could find in the set
		if (right instanceof BinopExpr) {
			Value op1 = ((BinopExpr) right).getOp1();
			Value op2 = ((BinopExpr) right).getOp2();

			Object op1Val = CPHelper.isAConstantValue(op1);
			Object op2Val = CPHelper.isAConstantValue(op2);

			if (op1Val == null)
				op1Val = isANotTopConstantInInputSet(toReturn, op1);

			if (op2Val == null)
				op2Val = isANotTopConstantInInputSet(toReturn, op2);

			if (op1 == left) {
				// System.out.println("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>OP1 is the same as LHS");
				op1Val = killedValue;
			}

			if (op2 == left) {
				// System.out.println("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>OP2 is the same as LHS");
				op2Val = killedValue;
			}

			if (op1Val != null && op2Val != null) {
				// System.out.println("found constant values for both operands of binary expression");
				if (left.getType() instanceof IntType && op1Val instanceof Integer && op2Val instanceof Integer) {
					// only caring about operations on two integers and result
					// is an integer

					int op1IntValue = ((Integer) op1Val).intValue();
					int op2IntValue = ((Integer) op2Val).intValue();

					String tempStr = ((BinopExpr) right).getSymbol();
					if (tempStr.length() > 1) {
						char symbol = tempStr.charAt(1);
						// System.out.println("found symbol "+symbol+" for the operands of binary expression");
						int newValue = 0;
						boolean set = false;
						switch (symbol) {
						case '+':
							// System.out.println("Adding");
							newValue = op1IntValue + op2IntValue;
							set = true;
							break;
						case '-':
							// System.out.println("Subtracting");
							newValue = op1IntValue - op2IntValue;
							set = true;

							break;

						case '*':
							// System.out.println("Multiplying");
							newValue = op1IntValue * op2IntValue;
							set = true;

							break;
						}

						if (set) {
							// we have our new value
							Integer newValueObject = new Integer(newValue);
							addOrUpdate(toReturn, left, newValueObject);
							return;
						}
					}
				}
			} else {
				// System.out.println("atleast one value is not constant so cant simplify expression");
			}
		}
		// System.out.println("DefinitionStmt checked right expr for mathematical stuff"+toReturn.toString());
	}
 
Example #19
Source File: SimplifyExpressions.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public NumericConstant getResult(BinopExpr binop){
	if(DEBUG)
		System.out.println("Binop expr"+binop);
	Value leftOp = binop.getOp1();
	Value rightOp = binop.getOp2();
	
	int op = 0;
	if(binop instanceof AddExpr){
		op=1;
	}
	else if(binop instanceof SubExpr || 
			binop instanceof DCmpExpr || binop instanceof DCmpgExpr
			|| binop instanceof DCmplExpr){
		op=2;
	}
	else if(binop instanceof MulExpr){
		op=3;
	}

	if(op == 0){
		if(DEBUG){
			System.out.println("not add sub or mult");
			System.out.println(binop.getClass().getName());
		}			
		return null;
	}
	NumericConstant constant = null;
	if(leftOp instanceof LongConstant  && rightOp instanceof LongConstant){
		if(DEBUG)
			System.out.println("long constants!!");
		if(op ==1)
			constant = ((LongConstant)leftOp).add((LongConstant)rightOp);
		else if(op ==2)
			constant = ((LongConstant)leftOp).subtract((LongConstant)rightOp);
		else if (op ==3)
			constant = ((LongConstant)leftOp).multiply((LongConstant)rightOp);
	}
	else if(leftOp instanceof DoubleConstant  && rightOp instanceof DoubleConstant){
		if(DEBUG)
			System.out.println("double constants!!");
		if(op ==1)
			constant = ((DoubleConstant)leftOp).add((DoubleConstant)rightOp);
		else if(op ==2)
			constant = ((DoubleConstant)leftOp).subtract((DoubleConstant)rightOp);
		else if (op ==3)
			constant = ((DoubleConstant)leftOp).multiply((DoubleConstant)rightOp);

	}
	else if(leftOp instanceof FloatConstant  && rightOp instanceof FloatConstant){
		if(DEBUG)
			System.out.println("Float constants!!");
		if(op ==1)
			constant = ((FloatConstant)leftOp).add((FloatConstant)rightOp);
		else if(op ==2)
			constant = ((FloatConstant)leftOp).subtract((FloatConstant)rightOp);
		else if (op ==3)
			constant = ((FloatConstant)leftOp).multiply((FloatConstant)rightOp);
	}
	else if(leftOp instanceof IntConstant  && rightOp instanceof IntConstant){
		if(DEBUG)
			System.out.println("Integer constants!!");
		if(op ==1)
			constant = ((IntConstant)leftOp).add((IntConstant)rightOp);
		else if(op ==2)
			constant = ((IntConstant)leftOp).subtract((IntConstant)rightOp);
		else if (op ==3)
			constant = ((IntConstant)leftOp).multiply((IntConstant)rightOp);
	}
	
	return constant;
}
 
Example #20
Source File: AsmMethodSource.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void convertBinopInsn(InsnNode insn) {
	int op = insn.getOpcode();
	boolean dword = op == DADD || op == LADD ||
			op == DSUB || op == LSUB ||
			op == DMUL || op == LMUL ||
			op == DDIV || op == LDIV ||
			op == DREM || op == LREM ||
			op == LSHL || op == LSHR ||
			op == LUSHR || op == LAND ||
			op == LOR || op == LXOR ||
			op == LCMP || op == DCMPL ||
			op == DCMPG;
	StackFrame frame = getFrame(insn);
	Operand[] out = frame.out();
	Operand opr;
	if (out == null) {
		Operand op2 = (dword && op != LSHL && op != LSHR && op != LUSHR) ?
				popImmediateDual() : popImmediate();
		Operand op1 = dword ? popImmediateDual() : popImmediate();
		Value v1 = op1.stackOrValue();
		Value v2 = op2.stackOrValue();
		BinopExpr binop;
		if (op >= IADD && op <= DADD)
			binop = Jimple.v().newAddExpr(v1, v2);
		else if (op >= ISUB && op <= DSUB)
			binop = Jimple.v().newSubExpr(v1, v2);
		else if (op >= IMUL && op <= DMUL)
			binop = Jimple.v().newMulExpr(v1, v2);
		else if (op >= IDIV && op <= DDIV)
			binop = Jimple.v().newDivExpr(v1, v2);
		else if (op >= IREM && op <= DREM)
			binop = Jimple.v().newRemExpr(v1, v2);
		else if (op >= ISHL && op <= LSHL)
			binop = Jimple.v().newShlExpr(v1, v2);
		else if (op >= ISHR && op <= LSHR)
			binop = Jimple.v().newShrExpr(v1, v2);
		else if (op >= IUSHR && op <= LUSHR)
			binop = Jimple.v().newUshrExpr(v1, v2);
		else if (op >= IAND && op <= LAND)
			binop = Jimple.v().newAndExpr(v1, v2);
		else if (op >= IOR && op <= LOR)
			binop = Jimple.v().newOrExpr(v1, v2);
		else if (op >= IXOR && op <= LXOR)
			binop = Jimple.v().newXorExpr(v1, v2);
		else if (op == LCMP)
			binop = Jimple.v().newCmpExpr(v1, v2);
		else if (op == FCMPL || op == DCMPL)
			binop = Jimple.v().newCmplExpr(v1, v2);
		else if (op == FCMPG || op == DCMPG)
			binop = Jimple.v().newCmpgExpr(v1, v2);
		else
			throw new AssertionError("Unknown binop: " + op);
		op1.addBox(binop.getOp1Box());
		op2.addBox(binop.getOp2Box());
		opr = new Operand(insn, binop);
		frame.in(op2, op1);
		frame.boxes(binop.getOp2Box(), binop.getOp1Box());
		frame.out(opr);
	} else {
		opr = out[0];
		if (dword) {
			if (op != LSHL && op != LSHR && op != LUSHR)
				frame.mergeIn(popDual(), popDual());
			else
				frame.mergeIn(pop(), popDual());
		} else {
			frame.mergeIn(pop(), pop());
		}
	}
	if (dword && (op < LCMP || op > DCMPG))
		pushDual(opr);
	else
		push(opr);
}
 
Example #21
Source File: UnitThrowAnalysis.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void caseBinopExpr(BinopExpr expr) {
    result = result.add(mightThrow(expr.getOp1()));
    result = result.add(mightThrow(expr.getOp2()));
}
 
Example #22
Source File: ValueTemplatePrinter.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void printBinaryExpr(BinopExpr v) {
	String className = v.getClass().getSimpleName();
	if(className.charAt(0)=='J') className = className.substring(1);

	String oldName = varName;
	
	Value left = v.getOp1();
	String v1 = printValueAssignment(left, "left");
	
	Value right = v.getOp2();
	String v2 = printValueAssignment(right, "right");
	
	p.println("Value "+oldName+" = Jimple.v().new"+className+"("+v1+","+v2+");");
	
	varName = oldName;		
}
 
Example #23
Source File: SignAnalysis.java    From vasco with GNU Lesser General Public License v2.1 4 votes vote down vote up
private Sign signOf(Value value, Map<Local, Sign> dfv) {
	if (value instanceof Local) {
		// if the value is a local variable, then look-up the data-flow map
		Local local = (Local) value;
		if (dfv.containsKey(local)) {
			return dfv.get(local);
		} else {
			return Sign.TOP;
		}
	} else if (value instanceof NumericConstant) {
		// If the value is a constant, we can get a definite sign
		NumericConstant num = (NumericConstant) value;
		NumericConstant zero = IntConstant.v(0);
		NumericConstant one = IntConstant.v(1);
		if (num.lessThan(zero).equals(one)) {
			return NEGATIVE;
		} else if (num.greaterThan(zero).equals(one)) {
			return POSITIVE;
		} else {
			return ZERO;
		}
	} else if (value instanceof BinopExpr) {
		BinopExpr expr = (BinopExpr) value;
		Value op1 = expr.getOp1();
		Value op2 = expr.getOp2();
		Sign sign1 = signOf(op1, dfv);
		Sign sign2 = signOf(op2, dfv);
		if (value instanceof AddExpr) {
			// Handle arithmetic plus
			return sign1.plus(sign2);
		} else if (value instanceof MulExpr) {
			// Handle arithmetic multiplication
			return sign1.mult(sign2);
		} else {
			// We do not handle other types of binary expressions
			return BOTTOM;
		}
	} else if (value instanceof UnopExpr) { 
		if (value instanceof AbstractNegExpr) {
			// Handle unary minus
			Value op = ((AbstractNegExpr) value).getOp();
			Sign sign = signOf(op, dfv);
			return sign.negate();
		} else {
			// We do not handle other types of binary expressions
			return BOTTOM;
		}
	} else {
		// We do not handle other types of compound expressions
		return BOTTOM;
	}
}