Search in sources :

Example 1 with FloatConstant

use of soot.jimple.FloatConstant in project soot by Sable.

the class ConstantFieldValueFinder method valuesForPrimTypeFields.

/*
	 * This method gives values to all the fields in all the classes if they can be determined statically
	 * We only care about fields which have primitive types
	 */
private void valuesForPrimTypeFields() {
    // go through all the classes
    Iterator classIt = appClasses.iterator();
    while (classIt.hasNext()) {
        SootClass s = (SootClass) classIt.next();
        debug("\nvaluesforPrimTypeFields", "Processing class " + s.getName());
        String declaringClass = s.getName();
        Iterator fieldIt = s.getFields().iterator();
        while (fieldIt.hasNext()) {
            SootField f = (SootField) fieldIt.next();
            String fieldName = f.getName();
            Type fieldType = f.getType();
            if (!(fieldType instanceof PrimType))
                continue;
            String combined = declaringClass + combiner + fieldName;
            classNameFieldNameToSootFieldMapping.put(combined, f);
            Object value = null;
            // check for constant value tags
            if (fieldType instanceof DoubleType && f.hasTag("DoubleConstantValueTag")) {
                double val = ((DoubleConstantValueTag) f.getTag("DoubleConstantValueTag")).getDoubleValue();
                value = new Double(val);
            } else if (fieldType instanceof FloatType && f.hasTag("FloatConstantValueTag")) {
                float val = ((FloatConstantValueTag) f.getTag("FloatConstantValueTag")).getFloatValue();
                value = new Float(val);
            } else if (fieldType instanceof LongType && f.hasTag("LongConstantValueTag")) {
                long val = ((LongConstantValueTag) f.getTag("LongConstantValueTag")).getLongValue();
                value = new Long(val);
            } else if (fieldType instanceof CharType && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                value = new Integer(val);
            } else if (fieldType instanceof BooleanType && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                if (val == 0)
                    value = new Boolean(false);
                else
                    value = new Boolean(true);
            } else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) && f.hasTag("IntegerConstantValueTag")) {
                int val = ((IntegerConstantValueTag) f.getTag("IntegerConstantValueTag")).getIntValue();
                value = new Integer(val);
            }
            // if there was a constant value tag we have its value now
            if (value != null) {
                debug("TAGGED value found for field: " + combined);
                primTypeFieldValueToUse.put(combined, value);
                // continue with next field
                continue;
            }
            // see if the field was never assigned in which case it gets default values
            Object temp = fieldToValues.get(combined);
            if (temp == null) {
                if (fieldType instanceof DoubleType)
                    value = new Double(0);
                else if (fieldType instanceof FloatType)
                    value = new Float(0);
                else if (fieldType instanceof LongType)
                    value = new Long(0);
                else if (fieldType instanceof BooleanType)
                    value = new Boolean(false);
                else if ((fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) || fieldType instanceof CharType) {
                    value = new Integer(0);
                } else
                    throw new DecompilationException("Unknown primitive type...please report to developer");
                primTypeFieldValueToUse.put(combined, value);
                debug("DEFAULT value for field: " + combined);
                // continue with next field
                continue;
            }
            // havent got a tag with value and havent use default since SOME method did define the field atleast once
            // there was some value assigned!!!!!!!!!
            debug("CHECKING USER ASSIGNED VALUES FOR: " + combined);
            ArrayList values = (ArrayList) temp;
            // check if they are all constants and that too the same constant
            Iterator it = values.iterator();
            NumericConstant tempConstant = null;
            while (it.hasNext()) {
                Value val = (Value) it.next();
                if (!(val instanceof NumericConstant)) {
                    tempConstant = null;
                    debug("Not numeric constant hence giving up");
                    break;
                }
                if (tempConstant == null) {
                    tempConstant = (NumericConstant) val;
                } else {
                    // check that this value is the same as previous
                    if (!tempConstant.equals(val)) {
                        tempConstant = null;
                        break;
                    }
                }
            }
            if (tempConstant == null) {
                // continue with next field cant do anything about this one
                continue;
            }
            if (tempConstant instanceof LongConstant) {
                Long tempVal = new Long(((LongConstant) tempConstant).value);
                if (tempVal.compareTo(new Long(0)) == 0)
                    primTypeFieldValueToUse.put(combined, tempVal);
                else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else if (tempConstant instanceof DoubleConstant) {
                Double tempVal = new Double(((DoubleConstant) tempConstant).value);
                if (tempVal.compareTo(new Double(0)) == 0)
                    primTypeFieldValueToUse.put(combined, tempVal);
                else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else if (tempConstant instanceof FloatConstant) {
                Float tempVal = new Float(((FloatConstant) tempConstant).value);
                if (tempVal.compareTo(new Float(0)) == 0)
                    primTypeFieldValueToUse.put(combined, tempVal);
                else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else if (tempConstant instanceof IntConstant) {
                Integer tempVal = new Integer(((IntConstant) tempConstant).value);
                if (tempVal.compareTo(new Integer(0)) == 0) {
                    SootField tempField = classNameFieldNameToSootFieldMapping.get(combined);
                    if (tempField.getType() instanceof BooleanType) {
                        primTypeFieldValueToUse.put(combined, new Boolean(false));
                    // System.out.println("puttingvalue false for"+combined);
                    } else {
                        primTypeFieldValueToUse.put(combined, tempVal);
                    // System.out.println("puttingvalue 0 for"+combined);
                    }
                } else
                    debug("Not assigning the agreed value since that is not the default value for " + combined);
            } else {
                throw new DecompilationException("Un handled Numberic Constant....report to programmer");
            }
        }
    // all fields of the class
    }
// all classes
}
Also used : DoubleConstant(soot.jimple.DoubleConstant) LongType(soot.LongType) FloatConstant(soot.jimple.FloatConstant) ArrayList(java.util.ArrayList) DecompilationException(soot.dava.DecompilationException) ByteType(soot.ByteType) FloatType(soot.FloatType) IntType(soot.IntType) Iterator(java.util.Iterator) PrimType(soot.PrimType) LongConstantValueTag(soot.tagkit.LongConstantValueTag) IntConstant(soot.jimple.IntConstant) LongConstant(soot.jimple.LongConstant) ShortType(soot.ShortType) BooleanType(soot.BooleanType) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) SootClass(soot.SootClass) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) ShortType(soot.ShortType) CharType(soot.CharType) LongType(soot.LongType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) PrimType(soot.PrimType) DoubleType(soot.DoubleType) NumericConstant(soot.jimple.NumericConstant) Value(soot.Value) SootField(soot.SootField) CharType(soot.CharType)

Example 2 with FloatConstant

use of soot.jimple.FloatConstant in project soot by Sable.

the class ConstraintCollector method caseAssignStmt.

public void caseAssignStmt(AssignStmt stmt) {
    Value l = stmt.getLeftOp();
    Value r = stmt.getRightOp();
    TypeVariable left = null;
    TypeVariable right = null;
    if (l instanceof ArrayRef) {
        ArrayRef ref = (ArrayRef) l;
        Value base = ref.getBase();
        Value index = ref.getIndex();
        TypeVariable baseType = resolver.typeVariable((Local) base);
        baseType.makeElement();
        left = baseType.element();
        if (index instanceof Local) {
            if (uses) {
                resolver.typeVariable((Local) index).addParent(resolver.typeVariable(IntType.v()));
            }
        }
    } else if (l instanceof Local) {
        left = resolver.typeVariable((Local) l);
    } else if (l instanceof InstanceFieldRef) {
        InstanceFieldRef ref = (InstanceFieldRef) l;
        if (uses) {
            TypeVariable baseType = resolver.typeVariable((Local) ref.getBase());
            baseType.addParent(resolver.typeVariable(ref.getField().getDeclaringClass()));
            left = resolver.typeVariable(ref.getField().getType());
        }
    } else if (l instanceof StaticFieldRef) {
        if (uses) {
            StaticFieldRef ref = (StaticFieldRef) l;
            left = resolver.typeVariable(ref.getField().getType());
        }
    } else {
        throw new RuntimeException("Unhandled assignment left hand side type: " + l.getClass());
    }
    if (r instanceof ArrayRef) {
        ArrayRef ref = (ArrayRef) r;
        Value base = ref.getBase();
        Value index = ref.getIndex();
        TypeVariable baseType = resolver.typeVariable((Local) base);
        baseType.makeElement();
        right = baseType.element();
        if (index instanceof Local) {
            if (uses) {
                resolver.typeVariable((Local) index).addParent(resolver.typeVariable(IntType.v()));
            }
        }
    } else if (r instanceof DoubleConstant) {
        right = resolver.typeVariable(DoubleType.v());
    } else if (r instanceof FloatConstant) {
        right = resolver.typeVariable(FloatType.v());
    } else if (r instanceof IntConstant) {
        right = resolver.typeVariable(IntType.v());
    } else if (r instanceof LongConstant) {
        right = resolver.typeVariable(LongType.v());
    } else if (r instanceof NullConstant) {
        right = resolver.typeVariable(NullType.v());
    } else if (r instanceof StringConstant) {
        right = resolver.typeVariable(RefType.v("java.lang.String"));
    } else if (r instanceof ClassConstant) {
        right = resolver.typeVariable(RefType.v("java.lang.Class"));
    } else if (r instanceof BinopExpr) {
        // ******** BINOP EXPR ********
        BinopExpr be = (BinopExpr) r;
        Value lv = be.getOp1();
        Value rv = be.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());
        }
        if ((be instanceof AddExpr) || (be instanceof SubExpr) || (be instanceof MulExpr) || (be instanceof DivExpr) || (be instanceof RemExpr) || (be instanceof AndExpr) || (be instanceof OrExpr) || (be instanceof XorExpr)) {
            if (uses) {
                TypeVariable common = resolver.typeVariable();
                rop.addParent(common);
                lop.addParent(common);
            }
            if (left != null) {
                rop.addParent(left);
                lop.addParent(left);
            }
        } else if ((be instanceof ShlExpr) || (be instanceof ShrExpr) || (be instanceof UshrExpr)) {
            if (uses) {
                rop.addParent(resolver.typeVariable(IntType.v()));
            }
            right = lop;
        } else if ((be instanceof CmpExpr) || (be instanceof CmpgExpr) || (be instanceof CmplExpr) || (be instanceof EqExpr) || (be instanceof GeExpr) || (be instanceof GtExpr) || (be instanceof LeExpr) || (be instanceof LtExpr) || (be instanceof NeExpr)) {
            if (uses) {
                TypeVariable common = resolver.typeVariable();
                rop.addParent(common);
                lop.addParent(common);
            }
            right = resolver.typeVariable(IntType.v());
        } else {
            throw new RuntimeException("Unhandled binary expression type: " + be.getClass());
        }
    } else if (r instanceof CastExpr) {
        CastExpr ce = (CastExpr) r;
        right = resolver.typeVariable(ce.getCastType());
    } else if (r instanceof InstanceOfExpr) {
        right = resolver.typeVariable(IntType.v());
    } else if (r instanceof InvokeExpr) {
        InvokeExpr ie = (InvokeExpr) r;
        handleInvokeExpr(ie);
        right = resolver.typeVariable(ie.getMethodRef().returnType());
    } else if (r instanceof NewArrayExpr) {
        NewArrayExpr nae = (NewArrayExpr) r;
        Type baseType = nae.getBaseType();
        if (baseType instanceof ArrayType) {
            right = resolver.typeVariable(ArrayType.v(((ArrayType) baseType).baseType, ((ArrayType) baseType).numDimensions + 1));
        } else {
            right = resolver.typeVariable(ArrayType.v(baseType, 1));
        }
        if (uses) {
            Value size = nae.getSize();
            if (size instanceof Local) {
                TypeVariable var = resolver.typeVariable((Local) size);
                var.addParent(resolver.typeVariable(IntType.v()));
            }
        }
    } else if (r instanceof NewExpr) {
        NewExpr na = (NewExpr) r;
        right = resolver.typeVariable(na.getBaseType());
    } else if (r instanceof NewMultiArrayExpr) {
        NewMultiArrayExpr nmae = (NewMultiArrayExpr) r;
        right = resolver.typeVariable(nmae.getBaseType());
        if (uses) {
            for (int i = 0; i < nmae.getSizeCount(); i++) {
                Value size = nmae.getSize(i);
                if (size instanceof Local) {
                    TypeVariable var = resolver.typeVariable((Local) size);
                    var.addParent(resolver.typeVariable(IntType.v()));
                }
            }
        }
    } else if (r instanceof LengthExpr) {
        LengthExpr le = (LengthExpr) r;
        if (uses) {
            if (le.getOp() instanceof Local) {
                resolver.typeVariable((Local) le.getOp()).makeElement();
            }
        }
        right = resolver.typeVariable(IntType.v());
    } else if (r instanceof NegExpr) {
        NegExpr ne = (NegExpr) r;
        if (ne.getOp() instanceof Local) {
            right = resolver.typeVariable((Local) ne.getOp());
        } else if (ne.getOp() instanceof DoubleConstant) {
            right = resolver.typeVariable(DoubleType.v());
        } else if (ne.getOp() instanceof FloatConstant) {
            right = resolver.typeVariable(FloatType.v());
        } else if (ne.getOp() instanceof IntConstant) {
            right = resolver.typeVariable(IntType.v());
        } else if (ne.getOp() instanceof LongConstant) {
            right = resolver.typeVariable(LongType.v());
        } else {
            throw new RuntimeException("Unhandled neg expression operand type: " + ne.getOp().getClass());
        }
    } else if (r instanceof Local) {
        right = resolver.typeVariable((Local) r);
    } else if (r instanceof InstanceFieldRef) {
        InstanceFieldRef ref = (InstanceFieldRef) r;
        if (uses) {
            TypeVariable baseType = resolver.typeVariable((Local) ref.getBase());
            baseType.addParent(resolver.typeVariable(ref.getField().getDeclaringClass()));
        }
        right = resolver.typeVariable(ref.getField().getType());
    } else if (r instanceof StaticFieldRef) {
        StaticFieldRef ref = (StaticFieldRef) r;
        right = resolver.typeVariable(ref.getField().getType());
    } else {
        throw new RuntimeException("Unhandled assignment right hand side type: " + r.getClass());
    }
    if (left != null && right != null) {
        right.addParent(left);
    }
}
Also used : MulExpr(soot.jimple.MulExpr) AndExpr(soot.jimple.AndExpr) DoubleConstant(soot.jimple.DoubleConstant) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) FloatConstant(soot.jimple.FloatConstant) GtExpr(soot.jimple.GtExpr) LtExpr(soot.jimple.LtExpr) NegExpr(soot.jimple.NegExpr) GeExpr(soot.jimple.GeExpr) UshrExpr(soot.jimple.UshrExpr) LeExpr(soot.jimple.LeExpr) ArrayRef(soot.jimple.ArrayRef) ArrayType(soot.ArrayType) InterfaceInvokeExpr(soot.jimple.InterfaceInvokeExpr) DynamicInvokeExpr(soot.jimple.DynamicInvokeExpr) VirtualInvokeExpr(soot.jimple.VirtualInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) SpecialInvokeExpr(soot.jimple.SpecialInvokeExpr) StaticInvokeExpr(soot.jimple.StaticInvokeExpr) InstanceFieldRef(soot.jimple.InstanceFieldRef) CastExpr(soot.jimple.CastExpr) IntConstant(soot.jimple.IntConstant) ShlExpr(soot.jimple.ShlExpr) LongConstant(soot.jimple.LongConstant) XorExpr(soot.jimple.XorExpr) NeExpr(soot.jimple.NeExpr) LengthExpr(soot.jimple.LengthExpr) SubExpr(soot.jimple.SubExpr) Local(soot.Local) NullConstant(soot.jimple.NullConstant) AddExpr(soot.jimple.AddExpr) InstanceOfExpr(soot.jimple.InstanceOfExpr) OrExpr(soot.jimple.OrExpr) StaticFieldRef(soot.jimple.StaticFieldRef) DivExpr(soot.jimple.DivExpr) RefType(soot.RefType) Type(soot.Type) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) LongType(soot.LongType) NullType(soot.NullType) ArrayType(soot.ArrayType) NewArrayExpr(soot.jimple.NewArrayExpr) RemExpr(soot.jimple.RemExpr) ShrExpr(soot.jimple.ShrExpr) CmpExpr(soot.jimple.CmpExpr) EqExpr(soot.jimple.EqExpr) CmpgExpr(soot.jimple.CmpgExpr) Value(soot.Value) NewExpr(soot.jimple.NewExpr) StringConstant(soot.jimple.StringConstant) CmplExpr(soot.jimple.CmplExpr) ClassConstant(soot.jimple.ClassConstant) BinopExpr(soot.jimple.BinopExpr)

Example 3 with FloatConstant

use of soot.jimple.FloatConstant in project soot by Sable.

the class ConstraintCollector method caseIfStmt.

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);
    }
}
Also used : LongConstant(soot.jimple.LongConstant) DoubleConstant(soot.jimple.DoubleConstant) FloatConstant(soot.jimple.FloatConstant) Local(soot.Local) NullConstant(soot.jimple.NullConstant) ConditionExpr(soot.jimple.ConditionExpr) Value(soot.Value) IntConstant(soot.jimple.IntConstant) StringConstant(soot.jimple.StringConstant) ClassConstant(soot.jimple.ClassConstant) BinopExpr(soot.jimple.BinopExpr)

Example 4 with FloatConstant

use of soot.jimple.FloatConstant in project soot by Sable.

the class ConstraintCollector method caseAssignStmt.

public void caseAssignStmt(AssignStmt stmt) {
    Value l = stmt.getLeftOp();
    Value r = stmt.getRightOp();
    TypeVariable left = null;
    TypeVariable right = null;
    if (l instanceof ArrayRef) {
        ArrayRef ref = (ArrayRef) l;
        Type baset = ((Local) ref.getBase()).getType();
        if (baset instanceof ArrayType) {
            ArrayType base = (ArrayType) baset;
            Value index = ref.getIndex();
            if (uses) {
                if ((base.numDimensions == 1) && (base.baseType instanceof IntegerType)) {
                    left = resolver.typeVariable(base.baseType);
                }
                if (index instanceof Local) {
                    resolver.typeVariable((Local) index).addParent(resolver.INT);
                }
            }
        }
    } else if (l instanceof Local) {
        if (((Local) l).getType() instanceof IntegerType) {
            left = resolver.typeVariable((Local) l);
        }
    } else if (l instanceof InstanceFieldRef) {
        if (uses) {
            InstanceFieldRef ref = (InstanceFieldRef) l;
            Type fieldType = ref.getFieldRef().type();
            if (fieldType instanceof IntegerType) {
                left = resolver.typeVariable(ref.getFieldRef().type());
            }
        }
    } else if (l instanceof StaticFieldRef) {
        if (uses) {
            StaticFieldRef ref = (StaticFieldRef) l;
            Type fieldType = ref.getFieldRef().type();
            if (fieldType instanceof IntegerType) {
                left = resolver.typeVariable(ref.getFieldRef().type());
            }
        }
    } else {
        throw new RuntimeException("Unhandled assignment left hand side type: " + l.getClass());
    }
    if (r instanceof ArrayRef) {
        ArrayRef ref = (ArrayRef) r;
        Type baset = ((Local) ref.getBase()).getType();
        if (!(baset instanceof NullType)) {
            Value index = ref.getIndex();
            // Be careful, dex can do some weird object/array casting
            if (baset instanceof ArrayType) {
                ArrayType base = (ArrayType) baset;
                if ((base.numDimensions == 1) && (base.baseType instanceof IntegerType)) {
                    right = resolver.typeVariable(base.baseType);
                }
            } else if (baset instanceof IntegerType)
                right = resolver.typeVariable(baset);
            if (uses)
                if (index instanceof Local)
                    resolver.typeVariable((Local) index).addParent(resolver.INT);
        }
    } else if (r instanceof DoubleConstant) {
    } else if (r instanceof FloatConstant) {
    } else if (r instanceof IntConstant) {
        int value = ((IntConstant) r).value;
        if (value < -32768) {
            right = resolver.INT;
        } else if (value < -128) {
            right = resolver.SHORT;
        } else if (value < 0) {
            right = resolver.BYTE;
        } else if (value < 2) {
            right = resolver.R0_1;
        } else if (value < 128) {
            right = resolver.R0_127;
        } else if (value < 32768) {
            right = resolver.R0_32767;
        } else if (value < 65536) {
            right = resolver.CHAR;
        } else {
            right = resolver.INT;
        }
    } else if (r instanceof LongConstant) {
    } else if (r instanceof NullConstant) {
    } else if (r instanceof StringConstant) {
    } else if (r instanceof ClassConstant) {
    } else if (r instanceof BinopExpr) {
        // ******** BINOP EXPR ********
        BinopExpr be = (BinopExpr) r;
        Value lv = be.getOp1();
        Value rv = be.getOp2();
        TypeVariable lop = null;
        TypeVariable rop = null;
        // ******** LEFT ********
        if (lv instanceof Local) {
            if (((Local) lv).getType() instanceof IntegerType) {
                lop = resolver.typeVariable((Local) lv);
            }
        } else if (lv instanceof DoubleConstant) {
        } else if (lv instanceof FloatConstant) {
        } else if (lv instanceof IntConstant) {
            int value = ((IntConstant) lv).value;
            if (value < -32768) {
                lop = resolver.INT;
            } else if (value < -128) {
                lop = resolver.SHORT;
            } else if (value < 0) {
                lop = resolver.BYTE;
            } else if (value < 2) {
                lop = resolver.R0_1;
            } else if (value < 128) {
                lop = resolver.R0_127;
            } else if (value < 32768) {
                lop = resolver.R0_32767;
            } else if (value < 65536) {
                lop = resolver.CHAR;
            } else {
                lop = resolver.INT;
            }
        } else if (lv instanceof LongConstant) {
        } else if (lv instanceof NullConstant) {
        } else if (lv instanceof StringConstant) {
        } else if (lv instanceof ClassConstant) {
        } else {
            throw new RuntimeException("Unhandled binary expression left operand type: " + lv.getClass());
        }
        // ******** RIGHT ********
        if (rv instanceof Local) {
            if (((Local) rv).getType() instanceof IntegerType) {
                rop = resolver.typeVariable((Local) rv);
            }
        } else if (rv instanceof DoubleConstant) {
        } else if (rv instanceof FloatConstant) {
        } else if (rv instanceof IntConstant) {
            int value = ((IntConstant) rv).value;
            if (value < -32768) {
                rop = resolver.INT;
            } else if (value < -128) {
                rop = resolver.SHORT;
            } else if (value < 0) {
                rop = resolver.BYTE;
            } else if (value < 2) {
                rop = resolver.R0_1;
            } else if (value < 128) {
                rop = resolver.R0_127;
            } else if (value < 32768) {
                rop = resolver.R0_32767;
            } else if (value < 65536) {
                rop = resolver.CHAR;
            } else {
                rop = resolver.INT;
            }
        } else if (rv instanceof LongConstant) {
        } else if (rv instanceof NullConstant) {
        } else if (rv instanceof StringConstant) {
        } else if (rv instanceof ClassConstant) {
        } else {
            throw new RuntimeException("Unhandled binary expression right operand type: " + rv.getClass());
        }
        if ((be instanceof AddExpr) || (be instanceof SubExpr) || (be instanceof DivExpr) || (be instanceof RemExpr) || (be instanceof MulExpr)) {
            if (lop != null && rop != null) {
                if (uses) {
                    if (lop.type() == null) {
                        lop.addParent(resolver.INT);
                    }
                    if (rop.type() == null) {
                        rop.addParent(resolver.INT);
                    }
                }
                right = resolver.INT;
            }
        } else if ((be instanceof AndExpr) || (be instanceof OrExpr) || (be instanceof XorExpr)) {
            if (lop != null && rop != null) {
                TypeVariable common = resolver.typeVariable();
                if (rop != null)
                    rop.addParent(common);
                if (lop != null)
                    lop.addParent(common);
                right = common;
            }
        } else if (be instanceof ShlExpr) {
            if (uses) {
                if (lop != null && lop.type() == null) {
                    lop.addParent(resolver.INT);
                }
                if (rop.type() == null) {
                    rop.addParent(resolver.INT);
                }
            }
            right = (lop == null) ? null : resolver.INT;
        } else if ((be instanceof ShrExpr) || (be instanceof UshrExpr)) {
            if (uses) {
                if (lop != null && lop.type() == null) {
                    lop.addParent(resolver.INT);
                }
                if (rop.type() == null) {
                    rop.addParent(resolver.INT);
                }
            }
            right = lop;
        } else if ((be instanceof CmpExpr) || (be instanceof CmpgExpr) || (be instanceof CmplExpr)) {
            right = resolver.BYTE;
        } else if ((be instanceof EqExpr) || (be instanceof GeExpr) || (be instanceof GtExpr) || (be instanceof LeExpr) || (be instanceof LtExpr) || (be instanceof NeExpr)) {
            if (uses) {
                TypeVariable common = resolver.typeVariable();
                if (rop != null)
                    rop.addParent(common);
                if (lop != null)
                    lop.addParent(common);
            }
            right = resolver.BOOLEAN;
        } else {
            throw new RuntimeException("Unhandled binary expression type: " + be.getClass());
        }
    } else if (r instanceof CastExpr) {
        CastExpr ce = (CastExpr) r;
        if (ce.getCastType() instanceof IntegerType) {
            right = resolver.typeVariable(ce.getCastType());
        }
    } else if (r instanceof InstanceOfExpr) {
        right = resolver.BOOLEAN;
    } else if (r instanceof InvokeExpr) {
        InvokeExpr ie = (InvokeExpr) r;
        handleInvokeExpr(ie);
        if (ie.getMethodRef().returnType() instanceof IntegerType) {
            right = resolver.typeVariable(ie.getMethodRef().returnType());
        }
    } else if (r instanceof NewArrayExpr) {
        NewArrayExpr nae = (NewArrayExpr) r;
        if (uses) {
            Value size = nae.getSize();
            if (size instanceof Local) {
                TypeVariable var = resolver.typeVariable((Local) size);
                var.addParent(resolver.INT);
            }
        }
    } else if (r instanceof NewExpr) {
    } else if (r instanceof NewMultiArrayExpr) {
        NewMultiArrayExpr nmae = (NewMultiArrayExpr) r;
        if (uses) {
            for (int i = 0; i < nmae.getSizeCount(); i++) {
                Value size = nmae.getSize(i);
                if (size instanceof Local) {
                    TypeVariable var = resolver.typeVariable((Local) size);
                    var.addParent(resolver.INT);
                }
            }
        }
    } else if (r instanceof LengthExpr) {
        right = resolver.INT;
    } else if (r instanceof NegExpr) {
        NegExpr ne = (NegExpr) r;
        if (ne.getOp() instanceof Local) {
            Local local = (Local) ne.getOp();
            if (local.getType() instanceof IntegerType) {
                if (uses) {
                    resolver.typeVariable(local).addParent(resolver.INT);
                }
                TypeVariable v = resolver.typeVariable();
                v.addChild(resolver.BYTE);
                v.addChild(resolver.typeVariable(local));
                right = v;
            }
        } else if (ne.getOp() instanceof DoubleConstant) {
        } else if (ne.getOp() instanceof FloatConstant) {
        } else if (ne.getOp() instanceof IntConstant) {
            int value = ((IntConstant) ne.getOp()).value;
            if (value < -32768) {
                right = resolver.INT;
            } else if (value < -128) {
                right = resolver.SHORT;
            } else if (value < 0) {
                right = resolver.BYTE;
            } else if (value < 2) {
                right = resolver.BYTE;
            } else if (value < 128) {
                right = resolver.BYTE;
            } else if (value < 32768) {
                right = resolver.SHORT;
            } else if (value < 65536) {
                right = resolver.INT;
            } else {
                right = resolver.INT;
            }
        } else if (ne.getOp() instanceof LongConstant) {
        } else {
            throw new RuntimeException("Unhandled neg expression operand type: " + ne.getOp().getClass());
        }
    } else if (r instanceof Local) {
        Local local = (Local) r;
        if (local.getType() instanceof IntegerType) {
            right = resolver.typeVariable(local);
        }
    } else if (r instanceof InstanceFieldRef) {
        InstanceFieldRef ref = (InstanceFieldRef) r;
        if (ref.getFieldRef().type() instanceof IntegerType) {
            right = resolver.typeVariable(ref.getFieldRef().type());
        }
    } else if (r instanceof StaticFieldRef) {
        StaticFieldRef ref = (StaticFieldRef) r;
        if (ref.getFieldRef().type() instanceof IntegerType) {
            right = resolver.typeVariable(ref.getFieldRef().type());
        }
    } else {
        throw new RuntimeException("Unhandled assignment right hand side type: " + r.getClass());
    }
    if (left != null && right != null && (left.type() == null || right.type() == null)) {
        right.addParent(left);
    }
}
Also used : MulExpr(soot.jimple.MulExpr) AndExpr(soot.jimple.AndExpr) DoubleConstant(soot.jimple.DoubleConstant) NewMultiArrayExpr(soot.jimple.NewMultiArrayExpr) FloatConstant(soot.jimple.FloatConstant) GtExpr(soot.jimple.GtExpr) LtExpr(soot.jimple.LtExpr) NegExpr(soot.jimple.NegExpr) GeExpr(soot.jimple.GeExpr) UshrExpr(soot.jimple.UshrExpr) LeExpr(soot.jimple.LeExpr) ArrayRef(soot.jimple.ArrayRef) ArrayType(soot.ArrayType) DynamicInvokeExpr(soot.jimple.DynamicInvokeExpr) InvokeExpr(soot.jimple.InvokeExpr) InstanceFieldRef(soot.jimple.InstanceFieldRef) CastExpr(soot.jimple.CastExpr) IntConstant(soot.jimple.IntConstant) ShlExpr(soot.jimple.ShlExpr) LongConstant(soot.jimple.LongConstant) XorExpr(soot.jimple.XorExpr) NeExpr(soot.jimple.NeExpr) LengthExpr(soot.jimple.LengthExpr) SubExpr(soot.jimple.SubExpr) Local(soot.Local) NullConstant(soot.jimple.NullConstant) AddExpr(soot.jimple.AddExpr) InstanceOfExpr(soot.jimple.InstanceOfExpr) OrExpr(soot.jimple.OrExpr) StaticFieldRef(soot.jimple.StaticFieldRef) IntegerType(soot.IntegerType) Type(soot.Type) NullType(soot.NullType) ArrayType(soot.ArrayType) IntegerType(soot.IntegerType) DivExpr(soot.jimple.DivExpr) NewArrayExpr(soot.jimple.NewArrayExpr) RemExpr(soot.jimple.RemExpr) ShrExpr(soot.jimple.ShrExpr) CmpExpr(soot.jimple.CmpExpr) EqExpr(soot.jimple.EqExpr) CmpgExpr(soot.jimple.CmpgExpr) Value(soot.Value) NewExpr(soot.jimple.NewExpr) NullType(soot.NullType) StringConstant(soot.jimple.StringConstant) CmplExpr(soot.jimple.CmplExpr) ClassConstant(soot.jimple.ClassConstant) BinopExpr(soot.jimple.BinopExpr)

Example 5 with FloatConstant

use of soot.jimple.FloatConstant in project soot by Sable.

the class SimplifyExpressions method getResult.

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;
}
Also used : MulExpr(soot.jimple.MulExpr) LongConstant(soot.jimple.LongConstant) DoubleConstant(soot.jimple.DoubleConstant) SubExpr(soot.jimple.SubExpr) FloatConstant(soot.jimple.FloatConstant) AddExpr(soot.jimple.AddExpr) DCmpExpr(soot.dava.internal.javaRep.DCmpExpr) DCmplExpr(soot.dava.internal.javaRep.DCmplExpr) DCmpgExpr(soot.dava.internal.javaRep.DCmpgExpr) NumericConstant(soot.jimple.NumericConstant) Value(soot.Value) IntConstant(soot.jimple.IntConstant)

Aggregations

DoubleConstant (soot.jimple.DoubleConstant)13 FloatConstant (soot.jimple.FloatConstant)13 IntConstant (soot.jimple.IntConstant)13 LongConstant (soot.jimple.LongConstant)13 Value (soot.Value)11 NullConstant (soot.jimple.NullConstant)9 StringConstant (soot.jimple.StringConstant)9 Local (soot.Local)8 ClassConstant (soot.jimple.ClassConstant)8 BinopExpr (soot.jimple.BinopExpr)6 IntType (soot.IntType)5 Type (soot.Type)5 ArrayType (soot.ArrayType)4 DoubleType (soot.DoubleType)4 FloatType (soot.FloatType)4 IntegerType (soot.IntegerType)4 LongType (soot.LongType)4 NullType (soot.NullType)4 AddExpr (soot.jimple.AddExpr)4 BooleanType (soot.BooleanType)3