com.android.dx.rop.cst.CstInteger Java Examples

The following examples show how to use com.android.dx.rop.cst.CstInteger. 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: FirstFitLocalCombiningAllocator.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the parameter index for SSA registers that are method parameters.
 * {@code -1} is returned for non-parameter registers.
 *
 * @param ssaReg {@code >=0;} SSA register to look up
 * @return parameter index or {@code -1} if not a parameter
 */
private int getParameterIndexForReg(int ssaReg) {
    SsaInsn defInsn = ssaMeth.getDefinitionForRegister(ssaReg);
    if (defInsn == null) {
        return -1;
    }

    Rop opcode = defInsn.getOpcode();

    // opcode == null for phi insns.
    if (opcode != null && opcode.getOpcode() == RegOps.MOVE_PARAM) {
        CstInsn origInsn = (CstInsn) defInsn.getOriginalRopInsn();
        return  ((CstInteger) origInsn.getConstant()).getValue();
    }

    return -1;
}
 
Example #2
Source File: BootstrapMethodArgumentsList.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the bootstrap argument at the indicated position.
 *
 * @param n position of argument to set
 * @param cst {@code Constant} instance
 */
public void set(int n, Constant cst) {
    // The set of permitted types is defined by the JVMS 8, section 4.7.23.
    if (cst instanceof CstString ||
        cst instanceof CstType ||
        cst instanceof CstInteger ||
        cst instanceof CstLong ||
        cst instanceof CstFloat ||
        cst instanceof CstDouble ||
        cst instanceof CstMethodHandle ||
        cst instanceof CstProtoRef) {
        set0(n, cst);
    } else {
        Class<?> klass = cst.getClass();
        throw new IllegalArgumentException("bad type for bootstrap argument: " + klass);
    }
}
 
Example #3
Source File: AttConstantValue.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param constantValue {@code non-null;} the constant value, which must
 * be an instance of one of: {@code CstString},
 * {@code CstInteger}, {@code CstLong},
 * {@code CstFloat}, or {@code CstDouble}
 */
public AttConstantValue(TypedConstant constantValue) {
    super(ATTRIBUTE_NAME);

    if (!((constantValue instanceof CstString) ||
           (constantValue instanceof CstInteger) ||
           (constantValue instanceof CstLong) ||
           (constantValue instanceof CstFloat) ||
           (constantValue instanceof CstDouble))) {
        if (constantValue == null) {
            throw new NullPointerException("constantValue == null");
        }
        throw new IllegalArgumentException("bad type for constantValue");
    }

    this.constantValue = constantValue;
}
 
Example #4
Source File: FirstFitLocalCombiningAllocator.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the parameter index for SSA registers that are method parameters.
 * {@code -1} is returned for non-parameter registers.
 *
 * @param ssaReg {@code >=0;} SSA register to look up
 * @return parameter index or {@code -1} if not a parameter
 */
private int getParameterIndexForReg(int ssaReg) {
    SsaInsn defInsn = ssaMeth.getDefinitionForRegister(ssaReg);
    if (defInsn == null) {
        return -1;
    }

    Rop opcode = defInsn.getOpcode();

    // opcode == null for phi insns.
    if (opcode != null && opcode.getOpcode() == RegOps.MOVE_PARAM) {
        CstInsn origInsn = (CstInsn) defInsn.getOriginalRopInsn();
        return  ((CstInteger) origInsn.getConstant()).getValue();
    }

    return -1;
}
 
Example #5
Source File: AttConstantValue.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param constantValue {@code non-null;} the constant value, which must
 * be an instance of one of: {@code CstString},
 * {@code CstInteger}, {@code CstLong},
 * {@code CstFloat}, or {@code CstDouble}
 */
public AttConstantValue(TypedConstant constantValue) {
    super(ATTRIBUTE_NAME);

    if (!((constantValue instanceof CstString) ||
           (constantValue instanceof CstInteger) ||
           (constantValue instanceof CstLong) ||
           (constantValue instanceof CstFloat) ||
           (constantValue instanceof CstDouble))) {
        if (constantValue == null) {
            throw new NullPointerException("constantValue == null");
        }
        throw new IllegalArgumentException("bad type for constantValue");
    }

    this.constantValue = constantValue;
}
 
Example #6
Source File: FirstFitLocalCombiningAllocator.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the parameter index for SSA registers that are method parameters.
 * {@code -1} is returned for non-parameter registers.
 *
 * @param ssaReg {@code >=0;} SSA register to look up
 * @return parameter index or {@code -1} if not a parameter
 */
private int getParameterIndexForReg(int ssaReg) {
    SsaInsn defInsn = ssaMeth.getDefinitionForRegister(ssaReg);
    if (defInsn == null) {
        return -1;
    }

    Rop opcode = defInsn.getOpcode();

    // opcode == null for phi insns.
    if (opcode != null && opcode.getOpcode() == RegOps.MOVE_PARAM) {
        CstInsn origInsn = (CstInsn) defInsn.getOriginalRopInsn();
        return  ((CstInteger) origInsn.getConstant()).getValue();
    }

    return -1;
}
 
Example #7
Source File: FirstFitLocalCombiningAllocator.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the parameter index for SSA registers that are method parameters.
 * {@code -1} is returned for non-parameter registers.
 *
 * @param ssaReg {@code >=0;} SSA register to look up
 * @return parameter index or {@code -1} if not a parameter
 */
private int getParameterIndexForReg(int ssaReg) {
    SsaInsn defInsn = ssaMeth.getDefinitionForRegister(ssaReg);
    if (defInsn == null) {
        return -1;
    }

    Rop opcode = defInsn.getOpcode();

    // opcode == null for phi insns.
    if (opcode != null && opcode.getOpcode() == RegOps.MOVE_PARAM) {
        CstInsn origInsn = (CstInsn) defInsn.getOriginalRopInsn();
        return  ((CstInteger) origInsn.getConstant()).getValue();
    }

    return -1;
}
 
Example #8
Source File: BootstrapMethodArgumentsList.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the bootstrap argument at the indicated position.
 *
 * @param n position of argument to set
 * @param cst {@code Constant} instance
 */
public void set(int n, Constant cst) {
    // The set of permitted types is defined by the JVMS 8, section 4.7.23.
    if (cst instanceof CstString ||
        cst instanceof CstType ||
        cst instanceof CstInteger ||
        cst instanceof CstLong ||
        cst instanceof CstFloat ||
        cst instanceof CstDouble ||
        cst instanceof CstMethodHandle ||
        cst instanceof CstProtoRef) {
        set0(n, cst);
    } else {
        Class<?> klass = cst.getClass();
        throw new IllegalArgumentException("bad type for bootstrap argument: " + klass);
    }
}
 
Example #9
Source File: AttConstantValue.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param constantValue {@code non-null;} the constant value, which must
 * be an instance of one of: {@code CstString},
 * {@code CstInteger}, {@code CstLong},
 * {@code CstFloat}, or {@code CstDouble}
 */
public AttConstantValue(TypedConstant constantValue) {
    super(ATTRIBUTE_NAME);

    if (!((constantValue instanceof CstString) ||
           (constantValue instanceof CstInteger) ||
           (constantValue instanceof CstLong) ||
           (constantValue instanceof CstFloat) ||
           (constantValue instanceof CstDouble))) {
        if (constantValue == null) {
            throw new NullPointerException("constantValue == null");
        }
        throw new IllegalArgumentException("bad type for constantValue");
    }

    this.constantValue = constantValue;
}
 
Example #10
Source File: AttConstantValue.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param constantValue {@code non-null;} the constant value, which must
 * be an instance of one of: {@code CstString},
 * {@code CstInteger}, {@code CstLong},
 * {@code CstFloat}, or {@code CstDouble}
 */
public AttConstantValue(TypedConstant constantValue) {
    super(ATTRIBUTE_NAME);

    if (!((constantValue instanceof CstString) ||
           (constantValue instanceof CstInteger) ||
           (constantValue instanceof CstLong) ||
           (constantValue instanceof CstFloat) ||
           (constantValue instanceof CstDouble))) {
        if (constantValue == null) {
            throw new NullPointerException("constantValue == null");
        }
        throw new IllegalArgumentException("bad type for constantValue");
    }

    this.constantValue = constantValue;
}
 
Example #11
Source File: AnnotationUtils.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code InnerClass} annotation.
 *
 * @param name {@code null-ok;} the original name of the class, or
 * {@code null} to represent an anonymous class
 * @param accessFlags the original access flags
 * @return {@code non-null;} the annotation
 */
public static Annotation makeInnerClass(CstString name, int accessFlags) {
    Annotation result = new Annotation(INNER_CLASS_TYPE, SYSTEM);
    Constant nameCst = (name != null) ? name : CstKnownNull.THE_ONE;

    result.put(new NameValuePair(NAME_STRING, nameCst));
    result.put(new NameValuePair(ACCESS_FLAGS_STRING,
                    CstInteger.make(accessFlags)));
    result.setImmutable();
    return result;
}
 
Example #12
Source File: RopTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
public void visitPlainCstInsn(PlainCstInsn insn) {
    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    Rop rop = insn.getOpcode();
    int ropOpcode = rop.getOpcode();
    DalvInsn di;

    if (rop.getBranchingness() != Rop.BRANCH_NONE) {
        throw new RuntimeException("shouldn't happen");
    }

    if (ropOpcode == RegOps.MOVE_PARAM) {
        if (!paramsAreInOrder) {
            /*
             * Parameters are not in order at the top of the reg space.
             * We need to add moves.
             */

            RegisterSpec dest = insn.getResult();
            int param =
                ((CstInteger) insn.getConstant()).getValue();
            RegisterSpec source =
                RegisterSpec.make(regCount - paramSize + param,
                        dest.getType());
            di = new SimpleInsn(opcode, pos,
                                RegisterSpecList.make(dest, source));
            addOutput(di);
        }
    } else {
        // No moves required for the parameters
        RegisterSpecList regs = getRegs(insn);
        di = new CstInsn(opcode, pos, regs, insn.getConstant());
        addOutput(di);
    }
}
 
Example #13
Source File: CfTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #processFields}, which translates constants into
 * more specific types if necessary.
 *
 * @param constant {@code non-null;} the constant in question
 * @param type {@code non-null;} the desired type
 */
private static TypedConstant coerceConstant(TypedConstant constant,
        Type type) {
    Type constantType = constant.getType();

    if (constantType.equals(type)) {
        return constant;
    }

    switch (type.getBasicType()) {
        case Type.BT_BOOLEAN: {
            return CstBoolean.make(((CstInteger) constant).getValue());
        }
        case Type.BT_BYTE: {
            return CstByte.make(((CstInteger) constant).getValue());
        }
        case Type.BT_CHAR: {
            return CstChar.make(((CstInteger) constant).getValue());
        }
        case Type.BT_SHORT: {
            return CstShort.make(((CstInteger) constant).getValue());
        }
        default: {
            throw new UnsupportedOperationException("can't coerce " +
                    constant + " to " + type);
        }
    }
}
 
Example #14
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitPlainCstInsn(PlainCstInsn insn) {
    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    Rop rop = insn.getOpcode();
    int ropOpcode = rop.getOpcode();
    DalvInsn di;

    if (rop.getBranchingness() != Rop.BRANCH_NONE) {
        throw new RuntimeException("shouldn't happen");
    }

    if (ropOpcode == RegOps.MOVE_PARAM) {
        if (!paramsAreInOrder) {
            /*
             * Parameters are not in order at the top of the reg space.
             * We need to add moves.
             */

            RegisterSpec dest = insn.getResult();
            int param =
                ((CstInteger) insn.getConstant()).getValue();
            RegisterSpec source =
                RegisterSpec.make(regCount - paramSize + param,
                        dest.getType());
            di = new SimpleInsn(opcode, pos,
                                RegisterSpecList.make(dest, source));
            addOutput(di);
        }
    } else {
        // No moves required for the parameters
        RegisterSpecList regs = getRegs(insn);
        di = new CstInsn(opcode, pos, regs, insn.getConstant());
        addOutput(di);
    }
}
 
Example #15
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Checks to see if the move-param instructions that occur in this
 * method happen to slot the params in an order at the top of the
 * stack frame that matches dalvik's calling conventions. This will
 * alway result in "true" for methods that have run through the
 * SSA optimizer.
 *
 * @param paramSize size, in register units, of all the parameters
 * to this method
 */
private static boolean calculateParamsAreInOrder(RopMethod method,
        final int paramSize) {
    final boolean[] paramsAreInOrder = { true };
    final int initialRegCount = method.getBlocks().getRegCount();

    /*
     * We almost could just check the first block here, but the
     * {@code cf} layer will put in a second move-param in a
     * subsequent block in the case of synchronized methods.
     */
    method.getBlocks().forEachInsn(new Insn.BaseVisitor() {
        @Override
        public void visitPlainCstInsn(PlainCstInsn insn) {
            if (insn.getOpcode().getOpcode()== RegOps.MOVE_PARAM) {
                int param =
                    ((CstInteger) insn.getConstant()).getValue();

                paramsAreInOrder[0] = paramsAreInOrder[0]
                        && ((initialRegCount - paramSize + param)
                            == insn.getResult().getReg());
            }
        }
    });

    return paramsAreInOrder[0];
}
 
Example #16
Source File: Constants.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns a rop constant for the specified value.
 *
 * @param value null, a boxed primitive, String, Class, or TypeId.
 */
static TypedConstant getConstant(Object value) {
    if (value == null) {
        return CstKnownNull.THE_ONE;
    } else if (value instanceof Boolean) {
        return CstBoolean.make((Boolean) value);
    } else if (value instanceof Byte) {
        return CstByte.make((Byte) value);
    } else if (value instanceof Character) {
        return CstChar.make((Character) value);
    } else if (value instanceof Double) {
        return CstDouble.make(Double.doubleToLongBits((Double) value));
    } else if (value instanceof Float) {
        return CstFloat.make(Float.floatToIntBits((Float) value));
    } else if (value instanceof Integer) {
        return CstInteger.make((Integer) value);
    } else if (value instanceof Long) {
        return CstLong.make((Long) value);
    } else if (value instanceof Short) {
        return CstShort.make((Short) value);
    } else if (value instanceof String) {
        return new CstString((String) value);
    } else if (value instanceof Class) {
        return new CstType(TypeId.get((Class<?>) value).ropType);
    } else if (value instanceof TypeId) {
        return new CstType(((TypeId) value).ropType);
    } else {
        throw new UnsupportedOperationException("Not a constant: " + value);
    }
}
 
Example #17
Source File: InsnFormat.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to extract the callout-argument index from an
 * appropriate instruction.
 *
 * @param insn {@code non-null;} the instruction
 * @return {@code >= 0;} the callout argument index
 */
protected static int argIndex(DalvInsn insn) {
    int arg = ((CstInteger) ((CstInsn) insn).getConstant()).getValue();

    if (arg < 0) {
        throw new IllegalArgumentException("bogus insn");
    }

    return arg;
}
 
Example #18
Source File: RopTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Checks to see if the move-param instructions that occur in this
 * method happen to slot the params in an order at the top of the
 * stack frame that matches dalvik's calling conventions. This will
 * alway result in "true" for methods that have run through the
 * SSA optimizer.
 *
 * @param paramSize size, in register units, of all the parameters
 * to this method
 */
private static boolean calculateParamsAreInOrder(RopMethod method,
        final int paramSize) {
    final boolean[] paramsAreInOrder = { true };
    final int initialRegCount = method.getBlocks().getRegCount();

    /*
     * We almost could just check the first block here, but the
     * {@code cf} layer will put in a second move-param in a
     * subsequent block in the case of synchronized methods.
     */
    method.getBlocks().forEachInsn(new Insn.BaseVisitor() {
        @Override
        public void visitPlainCstInsn(PlainCstInsn insn) {
            if (insn.getOpcode().getOpcode()== RegOps.MOVE_PARAM) {
                int param =
                    ((CstInteger) insn.getConstant()).getValue();

                paramsAreInOrder[0] = paramsAreInOrder[0]
                        && ((initialRegCount - paramSize + param)
                            == insn.getResult().getReg());
            }
        }
    });

    return paramsAreInOrder[0];
}
 
Example #19
Source File: Constants.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a rop constant for the specified value.
 *
 * @param value null, a boxed primitive, String, Class, or TypeId.
 */
static TypedConstant getConstant(Object value) {
    if (value == null) {
        return CstKnownNull.THE_ONE;
    } else if (value instanceof Boolean) {
        return CstBoolean.make((Boolean) value);
    } else if (value instanceof Byte) {
        return CstByte.make((Byte) value);
    } else if (value instanceof Character) {
        return CstChar.make((Character) value);
    } else if (value instanceof Double) {
        return CstDouble.make(Double.doubleToLongBits((Double) value));
    } else if (value instanceof Float) {
        return CstFloat.make(Float.floatToIntBits((Float) value));
    } else if (value instanceof Integer) {
        return CstInteger.make((Integer) value);
    } else if (value instanceof Long) {
        return CstLong.make((Long) value);
    } else if (value instanceof Short) {
        return CstShort.make((Short) value);
    } else if (value instanceof String) {
        return new CstString((String) value);
    } else if (value instanceof Class) {
        return new CstType(TypeId.get((Class<?>) value).ropType);
    } else if (value instanceof TypeId) {
        return new CstType(((TypeId) value).ropType);
    } else {
        throw new UnsupportedOperationException("Not a constant: " + value);
    }
}
 
Example #20
Source File: RopTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitPlainCstInsn(PlainCstInsn insn) {
    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    Rop rop = insn.getOpcode();
    int ropOpcode = rop.getOpcode();
    DalvInsn di;

    if (rop.getBranchingness() != Rop.BRANCH_NONE) {
        throw new RuntimeException("shouldn't happen");
    }

    if (ropOpcode == RegOps.MOVE_PARAM) {
        if (!paramsAreInOrder) {
            /*
             * Parameters are not in order at the top of the reg space.
             * We need to add moves.
             */

            RegisterSpec dest = insn.getResult();
            int param =
                ((CstInteger) insn.getConstant()).getValue();
            RegisterSpec source =
                RegisterSpec.make(regCount - paramSize + param,
                        dest.getType());
            di = new SimpleInsn(opcode, pos,
                                RegisterSpecList.make(dest, source));
            addOutput(di);
        }
    } else {
        // No moves required for the parameters
        RegisterSpecList regs = getRegs(insn);
        di = new CstInsn(opcode, pos, regs, insn.getConstant());
        addOutput(di);
    }
}
 
Example #21
Source File: CfTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #processFields}, which translates constants into
 * more specific types if necessary.
 *
 * @param constant {@code non-null;} the constant in question
 * @param type {@code non-null;} the desired type
 */
private static TypedConstant coerceConstant(TypedConstant constant,
        Type type) {
    Type constantType = constant.getType();

    if (constantType.equals(type)) {
        return constant;
    }

    switch (type.getBasicType()) {
        case Type.BT_BOOLEAN: {
            return CstBoolean.make(((CstInteger) constant).getValue());
        }
        case Type.BT_BYTE: {
            return CstByte.make(((CstInteger) constant).getValue());
        }
        case Type.BT_CHAR: {
            return CstChar.make(((CstInteger) constant).getValue());
        }
        case Type.BT_SHORT: {
            return CstShort.make(((CstInteger) constant).getValue());
        }
        default: {
            throw new UnsupportedOperationException("can't coerce " +
                    constant + " to " + type);
        }
    }
}
 
Example #22
Source File: AnnotationUtils.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code InnerClass} annotation.
 *
 * @param name {@code null-ok;} the original name of the class, or
 * {@code null} to represent an anonymous class
 * @param accessFlags the original access flags
 * @return {@code non-null;} the annotation
 */
public static Annotation makeInnerClass(CstString name, int accessFlags) {
    Annotation result = new Annotation(INNER_CLASS_TYPE, SYSTEM);
    Constant nameCst = (name != null) ? name : CstKnownNull.THE_ONE;

    result.put(new NameValuePair(NAME_STRING, nameCst));
    result.put(new NameValuePair(ACCESS_FLAGS_STRING,
                    CstInteger.make(accessFlags)));
    result.setImmutable();
    return result;
}
 
Example #23
Source File: AnnotationUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code InnerClass} annotation.
 *
 * @param name {@code null-ok;} the original name of the class, or
 * {@code null} to represent an anonymous class
 * @param accessFlags the original access flags
 * @return {@code non-null;} the annotation
 */
public static Annotation makeInnerClass(CstString name, int accessFlags) {
    Annotation result = new Annotation(INNER_CLASS_TYPE, SYSTEM);
    Constant nameCst = (name != null) ? name : CstKnownNull.THE_ONE;

    result.put(new NameValuePair(NAME_STRING, nameCst));
    result.put(new NameValuePair(ACCESS_FLAGS_STRING,
                    CstInteger.make(accessFlags)));
    result.setImmutable();
    return result;
}
 
Example #24
Source File: CfTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #processFields}, which translates constants into
 * more specific types if necessary.
 *
 * @param constant {@code non-null;} the constant in question
 * @param type {@code non-null;} the desired type
 */
private static TypedConstant coerceConstant(TypedConstant constant,
        Type type) {
    Type constantType = constant.getType();

    if (constantType.equals(type)) {
        return constant;
    }

    switch (type.getBasicType()) {
        case Type.BT_BOOLEAN: {
            return CstBoolean.make(((CstInteger) constant).getValue());
        }
        case Type.BT_BYTE: {
            return CstByte.make(((CstInteger) constant).getValue());
        }
        case Type.BT_CHAR: {
            return CstChar.make(((CstInteger) constant).getValue());
        }
        case Type.BT_SHORT: {
            return CstShort.make(((CstInteger) constant).getValue());
        }
        default: {
            throw new UnsupportedOperationException("can't coerce " +
                    constant + " to " + type);
        }
    }
}
 
Example #25
Source File: InsnFormat.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to extract the callout-argument index from an
 * appropriate instruction.
 *
 * @param insn {@code non-null;} the instruction
 * @return {@code >= 0;} the callout argument index
 */
protected static int argIndex(DalvInsn insn) {
    int arg = ((CstInteger) ((CstInsn) insn).getConstant()).getValue();

    if (arg < 0) {
        throw new IllegalArgumentException("bogus insn");
    }

    return arg;
}
 
Example #26
Source File: RopTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitPlainCstInsn(PlainCstInsn insn) {
    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    Rop rop = insn.getOpcode();
    int ropOpcode = rop.getOpcode();
    DalvInsn di;

    if (rop.getBranchingness() != Rop.BRANCH_NONE) {
        throw new RuntimeException("shouldn't happen");
    }

    if (ropOpcode == RegOps.MOVE_PARAM) {
        if (!paramsAreInOrder) {
            /*
             * Parameters are not in order at the top of the reg space.
             * We need to add moves.
             */

            RegisterSpec dest = insn.getResult();
            int param =
                ((CstInteger) insn.getConstant()).getValue();
            RegisterSpec source =
                RegisterSpec.make(regCount - paramSize + param,
                        dest.getType());
            di = new SimpleInsn(opcode, pos,
                                RegisterSpecList.make(dest, source));
            addOutput(di);
        }
    } else {
        // No moves required for the parameters
        RegisterSpecList regs = getRegs(insn);
        di = new CstInsn(opcode, pos, regs, insn.getConstant());
        addOutput(di);
    }
}
 
Example #27
Source File: RopTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Checks to see if the move-param instructions that occur in this
 * method happen to slot the params in an order at the top of the
 * stack frame that matches dalvik's calling conventions. This will
 * alway result in "true" for methods that have run through the
 * SSA optimizer.
 *
 * @param paramSize size, in register units, of all the parameters
 * to this method
 */
private static boolean calculateParamsAreInOrder(RopMethod method,
        final int paramSize) {
    final boolean[] paramsAreInOrder = { true };
    final int initialRegCount = method.getBlocks().getRegCount();

    /*
     * We almost could just check the first block here, but the
     * {@code cf} layer will put in a second move-param in a
     * subsequent block in the case of synchronized methods.
     */
    method.getBlocks().forEachInsn(new Insn.BaseVisitor() {
        @Override
        public void visitPlainCstInsn(PlainCstInsn insn) {
            if (insn.getOpcode().getOpcode()== RegOps.MOVE_PARAM) {
                int param =
                    ((CstInteger) insn.getConstant()).getValue();

                paramsAreInOrder[0] = paramsAreInOrder[0]
                        && ((initialRegCount - paramSize + param)
                            == insn.getResult().getReg());
            }
        }
    });

    return paramsAreInOrder[0];
}
 
Example #28
Source File: RopTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Checks to see if the move-param instructions that occur in this
 * method happen to slot the params in an order at the top of the
 * stack frame that matches dalvik's calling conventions. This will
 * alway result in "true" for methods that have run through the
 * SSA optimizer.
 *
 * @param paramSize size, in register units, of all the parameters
 * to this method
 */
private static boolean calculateParamsAreInOrder(RopMethod method,
        final int paramSize) {
    final boolean[] paramsAreInOrder = { true };
    final int initialRegCount = method.getBlocks().getRegCount();

    /*
     * We almost could just check the first block here, but the
     * {@code cf} layer will put in a second move-param in a
     * subsequent block in the case of synchronized methods.
     */
    method.getBlocks().forEachInsn(new Insn.BaseVisitor() {
        @Override
        public void visitPlainCstInsn(PlainCstInsn insn) {
            if (insn.getOpcode().getOpcode()== RegOps.MOVE_PARAM) {
                int param =
                    ((CstInteger) insn.getConstant()).getValue();

                paramsAreInOrder[0] = paramsAreInOrder[0]
                        && ((initialRegCount - paramSize + param)
                            == insn.getResult().getReg());
            }
        }
    });

    return paramsAreInOrder[0];
}
 
Example #29
Source File: AnnotationUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code InnerClass} annotation.
 *
 * @param name {@code null-ok;} the original name of the class, or
 * {@code null} to represent an anonymous class
 * @param accessFlags the original access flags
 * @return {@code non-null;} the annotation
 */
public static Annotation makeInnerClass(CstString name, int accessFlags) {
    Annotation result = new Annotation(INNER_CLASS_TYPE, SYSTEM);
    Constant nameCst = (name != null) ? name : CstKnownNull.THE_ONE;

    result.put(new NameValuePair(NAME_STRING, nameCst));
    result.put(new NameValuePair(ACCESS_FLAGS_STRING,
                    CstInteger.make(accessFlags)));
    result.setImmutable();
    return result;
}
 
Example #30
Source File: CfTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #processFields}, which translates constants into
 * more specific types if necessary.
 *
 * @param constant {@code non-null;} the constant in question
 * @param type {@code non-null;} the desired type
 */
private static TypedConstant coerceConstant(TypedConstant constant,
        Type type) {
    Type constantType = constant.getType();

    if (constantType.equals(type)) {
        return constant;
    }

    switch (type.getBasicType()) {
        case Type.BT_BOOLEAN: {
            return CstBoolean.make(((CstInteger) constant).getValue());
        }
        case Type.BT_BYTE: {
            return CstByte.make(((CstInteger) constant).getValue());
        }
        case Type.BT_CHAR: {
            return CstChar.make(((CstInteger) constant).getValue());
        }
        case Type.BT_SHORT: {
            return CstShort.make(((CstInteger) constant).getValue());
        }
        default: {
            throw new UnsupportedOperationException("can't coerce " +
                    constant + " to " + type);
        }
    }
}