Search in sources :

Example 1 with Insn10x

use of soot.toDex.instructions.Insn10x in project soot by Sable.

the class ExprVisitor method castPrimitive.

private void castPrimitive(Register sourceReg, Value source, Type castSootType) {
    PrimitiveType castType = PrimitiveType.getByName(castSootType.toString());
    // than sorry and it's easy to fix it here.
    if (castType == PrimitiveType.INT && source.getType() instanceof NullType)
        source = IntConstant.v(0);
    // select fitting conversion opcode, depending on the source and cast
    // type
    Type srcType = source.getType();
    if (srcType instanceof RefType)
        throw new RuntimeException("Trying to cast reference type " + srcType + " to a primitive");
    PrimitiveType sourceType = PrimitiveType.getByName(srcType.toString());
    if (castType == PrimitiveType.BOOLEAN) {
        // there is no "-to-boolean" opcode, so just pretend to move an int
        // to an int
        castType = PrimitiveType.INT;
        sourceType = PrimitiveType.INT;
    }
    if (shouldCastFromInt(sourceType, castType)) {
        // pretend to cast from int since that is OK
        sourceType = PrimitiveType.INT;
        Opcode opc = getCastOpc(sourceType, castType);
        stmtV.addInsn(new Insn12x(opc, destinationReg, sourceReg), origStmt);
    } else if (isMoveCompatible(sourceType, castType)) {
        /*
			 * no actual cast needed, just move the reg content if regs differ
			 */
        if (destinationReg.getNumber() != sourceReg.getNumber()) {
            stmtV.addInsn(StmtVisitor.buildMoveInsn(destinationReg, sourceReg), origStmt);
        } else // one for jumps
        if (!origStmt.getBoxesPointingToThis().isEmpty())
            stmtV.addInsn(new Insn10x(Opcode.NOP), origStmt);
    } else if (needsCastThroughInt(sourceType, castType)) {
        /*
			 * an unsupported "dest = (cast) src" is broken down to
			 * "tmp = (int) src" and "dest = (cast) tmp", using a tmp reg to not
			 * mess with the original reg types
			 */
        Opcode castToIntOpc = getCastOpc(sourceType, PrimitiveType.INT);
        Opcode castFromIntOpc = getCastOpc(PrimitiveType.INT, castType);
        Register tmp = regAlloc.asTmpReg(IntType.v());
        stmtV.addInsn(new Insn12x(castToIntOpc, tmp, sourceReg), origStmt);
        stmtV.addInsn(new Insn12x(castFromIntOpc, destinationReg, tmp.clone()), origStmt);
    } else {
        // the leftover simple cases, where we just cast as stated
        Opcode opc = getCastOpc(sourceType, castType);
        stmtV.addInsn(new Insn12x(opc, destinationReg, sourceReg), origStmt);
    }
}
Also used : RefType(soot.RefType) RefType(soot.RefType) Type(soot.Type) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) LongType(soot.LongType) NullType(soot.NullType) ArrayType(soot.ArrayType) IntegerType(soot.IntegerType) PrimType(soot.PrimType) Insn12x(soot.toDex.instructions.Insn12x) Opcode(org.jf.dexlib2.Opcode) Insn10x(soot.toDex.instructions.Insn10x) NullType(soot.NullType)

Aggregations

Opcode (org.jf.dexlib2.Opcode)1 ArrayType (soot.ArrayType)1 DoubleType (soot.DoubleType)1 FloatType (soot.FloatType)1 IntType (soot.IntType)1 IntegerType (soot.IntegerType)1 LongType (soot.LongType)1 NullType (soot.NullType)1 PrimType (soot.PrimType)1 RefType (soot.RefType)1 Type (soot.Type)1 Insn10x (soot.toDex.instructions.Insn10x)1 Insn12x (soot.toDex.instructions.Insn12x)1