com.android.dx.rop.code.ThrowingCstInsn Java Examples

The following examples show how to use com.android.dx.rop.code.ThrowingCstInsn. 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: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Copies the constant value {@code value} to {@code target}. The constant
 * must be a primitive, String, Class, TypeId, or null.
 */
public <T> void loadConstant(Local<T> target, T value) {
    Rop rop = value == null
            ? Rops.CONST_OBJECT_NOTHROW
            : Rops.opConst(target.type.ropType);
    if (rop.getBranchingness() == BRANCH_NONE) {
        addInstruction(new PlainCstInsn(rop, sourcePosition, target.spec(),
                RegisterSpecList.EMPTY, Constants.getConstant(value)));
    } else {
        addInstruction(new ThrowingCstInsn(rop, sourcePosition,
                RegisterSpecList.EMPTY, catches, Constants.getConstant(value)));
        moveResult(target, true);
    }
}
 
Example #2
Source File: Code.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
private <D, R> void invoke(Rop rop, MethodId<D, R> method, Local<? super R> target,
        Local<? extends D> object, Local<?>... args) {
    addInstruction(new ThrowingCstInsn(rop, sourcePosition, concatenate(object, args),
            catches, method.constant));
    if (target != null) {
        moveResult(target, false);
    }
}
 
Example #3
Source File: Code.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
/**
 * Calls the constructor {@code constructor} using {@code args} and assigns
 * the new instance to {@code target}.
 */
public <T> void newInstance(Local<T> target, MethodId<T, Void> constructor, Local<?>... args) {
    if (target == null) {
        throw new IllegalArgumentException();
    }
    addInstruction(new ThrowingCstInsn(Rops.NEW_INSTANCE, sourcePosition,
            RegisterSpecList.EMPTY, catches, constructor.declaringType.constant));
    moveResult(target, true);
    invokeDirect(constructor, null, target, args);
}
 
Example #4
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Calls the constructor {@code constructor} using {@code args} and assigns
 * the new instance to {@code target}.
 */
public <T> void newInstance(Local<T> target, MethodId<T, Void> constructor, Local<?>... args) {
    if (target == null) {
        throw new IllegalArgumentException();
    }
    addInstruction(new ThrowingCstInsn(Rops.NEW_INSTANCE, sourcePosition,
            RegisterSpecList.EMPTY, catches, constructor.declaringType.constant));
    moveResult(target, true);
    invokeDirect(constructor, null, target, args);
}
 
Example #5
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private <D, R> void invoke(Rop rop, MethodId<D, R> method, Local<? super R> target,
                           Local<? extends D> object, Local<?>... args) {
    addInstruction(new ThrowingCstInsn(rop, sourcePosition, concatenate(object, args),
            catches, method.constant));
    if (target != null) {
        moveResult(target, false);
    }
}
 
Example #6
Source File: Code.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
private void loadConstantInternal(Local target, Object value) {
    Rop rop = value == null
              ? Rops.CONST_OBJECT_NOTHROW
              : Rops.opConst(target.type.ropType);
    if (rop.getBranchingness() == BRANCH_NONE) {
        addInstruction(new PlainCstInsn(rop, sourcePosition, target.spec(),
                                        RegisterSpecList.EMPTY, Constants.getConstant(value)));
    } else {
        addInstruction(new ThrowingCstInsn(rop, sourcePosition,
                                           RegisterSpecList.EMPTY, catches, Constants.getConstant(value)));
        moveResult(target, true);
    }
}
 
Example #7
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitThrowingCstInsn(ThrowingCstInsn insn) {
    super.visitThrowingCstInsn(insn);
    addIntroductionIfNecessary(insn);
}
 
Example #8
Source File: ConstCollector.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Applies the optimization.
 */
private void run() {
    int regSz = ssaMeth.getRegCount();

    ArrayList<TypedConstant> constantList
            = getConstsSortedByCountUse();

    int toCollect = Math.min(constantList.size(), MAX_COLLECTED_CONSTANTS);

    SsaBasicBlock start = ssaMeth.getEntryBlock();

    // Constant to new register containing the constant
    HashMap<TypedConstant, RegisterSpec> newRegs
            = new HashMap<TypedConstant, RegisterSpec> (toCollect);

    for (int i = 0; i < toCollect; i++) {
        TypedConstant cst = constantList.get(i);
        RegisterSpec result
                = RegisterSpec.make(ssaMeth.makeNewSsaReg(), cst);

        Rop constRop = Rops.opConst(cst);

        if (constRop.getBranchingness() == Rop.BRANCH_NONE) {
            start.addInsnToHead(
                    new PlainCstInsn(Rops.opConst(cst),
                            SourcePosition.NO_INFO, result,
                            RegisterSpecList.EMPTY, cst));
        } else {
            // We need two new basic blocks along with the new insn
            SsaBasicBlock entryBlock = ssaMeth.getEntryBlock();
            SsaBasicBlock successorBlock
                    = entryBlock.getPrimarySuccessor();

            // Insert a block containing the const insn.
            SsaBasicBlock constBlock
                    = entryBlock.insertNewSuccessor(successorBlock);

            constBlock.replaceLastInsn(
                    new ThrowingCstInsn(constRop, SourcePosition.NO_INFO,
                            RegisterSpecList.EMPTY,
                            StdTypeList.EMPTY, cst));

            // Insert a block containing the move-result-pseudo insn.

            SsaBasicBlock resultBlock
                    = constBlock.insertNewSuccessor(successorBlock);
            PlainInsn insn
                = new PlainInsn(
                        Rops.opMoveResultPseudo(result.getTypeBearer()),
                        SourcePosition.NO_INFO,
                        result, RegisterSpecList.EMPTY);

            resultBlock.addInsnToHead(insn);
        }

        newRegs.put(cst, result);
    }

    updateConstUses(newRegs, regSz);
}
 
Example #9
Source File: RopTranslator.java    From buck with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitThrowingCstInsn(ThrowingCstInsn insn) {
    super.visitThrowingCstInsn(insn);
    addIntroductionIfNecessary(insn);
}
 
Example #10
Source File: Code.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
/**
 * Assigns {@code target} to a newly allocated array of length {@code
 * length}. The array's type is the same as {@code target}'s type.
 */
public <T> void newArray(Local<T> target, Local<Integer> length) {
    addInstruction(new ThrowingCstInsn(Rops.opNewArray(target.type.ropType), sourcePosition,
            RegisterSpecList.make(length.spec()), catches, target.type.constant));
    moveResult(target, true);
}
 
Example #11
Source File: Code.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
/**
 * Tests if the value in {@code source} is assignable to {@code type}. If it
 * is, {@code target} is assigned to 1; otherwise {@code target} is assigned
 * to 0.
 */
public void instanceOfType(Local<?> target, Local<?> source, TypeId<?> type) {
    addInstruction(new ThrowingCstInsn(Rops.INSTANCE_OF, sourcePosition,
            RegisterSpecList.make(source.spec()), catches, type.constant));
    moveResult(target, true);
}
 
Example #12
Source File: Code.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
/**
 * Copies the value in {@code source} to the static field {@code fieldId}.
 */
public <V> void sput(FieldId<?, V> fieldId, Local<? extends V> source) {
    addInstruction(new ThrowingCstInsn(Rops.opPutStatic(source.type.ropType), sourcePosition,
            RegisterSpecList.make(source.spec()), catches, fieldId.constant));
}
 
Example #13
Source File: Code.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
/**
 * Copies the value in the static field {@code fieldId} to {@code target}.
 */
public <V> void sget(FieldId<?, ? extends V> fieldId, Local<V> target) {
    addInstruction(new ThrowingCstInsn(Rops.opGetStatic(target.type.ropType), sourcePosition,
            RegisterSpecList.EMPTY, catches, fieldId.constant));
    moveResult(target, true);
}
 
Example #14
Source File: Code.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
/**
  * Copies the value in {@code source} to the instance field {@code fieldId}
  * of {@code instance}.
  */
public <D, V> void iput(FieldId<D, V> fieldId, Local<? extends D> instance, Local<? extends V> source) {
     addInstruction(new ThrowingCstInsn(Rops.opPutField(source.type.ropType), sourcePosition,
             RegisterSpecList.make(source.spec(), instance.spec()), catches, fieldId.constant));
 }
 
Example #15
Source File: Code.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
/**
 * Copies the value in instance field {@code fieldId} of {@code instance} to
 * {@code target}.
 */
public <D, V> void iget(FieldId<D, ? extends V> fieldId, Local<V> target, Local<D> instance) {
    addInstruction(new ThrowingCstInsn(Rops.opGetField(target.type.ropType), sourcePosition,
            RegisterSpecList.make(instance.spec()), catches, fieldId.constant));
    moveResult(target, true);
}
 
Example #16
Source File: ConstCollector.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/**
 * Applies the optimization.
 */
private void run() {
    int regSz = ssaMeth.getRegCount();

    ArrayList<TypedConstant> constantList
            = getConstsSortedByCountUse();

    int toCollect = Math.min(constantList.size(), MAX_COLLECTED_CONSTANTS);

    SsaBasicBlock start = ssaMeth.getEntryBlock();

    // Constant to new register containing the constant
    HashMap<TypedConstant, RegisterSpec> newRegs
            = new HashMap<TypedConstant, RegisterSpec> (toCollect);

    for (int i = 0; i < toCollect; i++) {
        TypedConstant cst = constantList.get(i);
        RegisterSpec result
                = RegisterSpec.make(ssaMeth.makeNewSsaReg(), cst);

        Rop constRop = Rops.opConst(cst);

        if (constRop.getBranchingness() == Rop.BRANCH_NONE) {
            start.addInsnToHead(
                    new PlainCstInsn(Rops.opConst(cst),
                            SourcePosition.NO_INFO, result,
                            RegisterSpecList.EMPTY, cst));
        } else {
            // We need two new basic blocks along with the new insn
            SsaBasicBlock entryBlock = ssaMeth.getEntryBlock();
            SsaBasicBlock successorBlock
                    = entryBlock.getPrimarySuccessor();

            // Insert a block containing the const insn.
            SsaBasicBlock constBlock
                    = entryBlock.insertNewSuccessor(successorBlock);

            constBlock.replaceLastInsn(
                    new ThrowingCstInsn(constRop, SourcePosition.NO_INFO,
                            RegisterSpecList.EMPTY,
                            StdTypeList.EMPTY, cst));

            // Insert a block containing the move-result-pseudo insn.

            SsaBasicBlock resultBlock
                    = constBlock.insertNewSuccessor(successorBlock);
            PlainInsn insn
                = new PlainInsn(
                        Rops.opMoveResultPseudo(result.getTypeBearer()),
                        SourcePosition.NO_INFO,
                        result, RegisterSpecList.EMPTY);

            resultBlock.addInsnToHead(insn);
        }

        newRegs.put(cst, result);
    }

    updateConstUses(newRegs, regSz);
}
 
Example #17
Source File: RopTranslator.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitThrowingCstInsn(ThrowingCstInsn insn) {
    super.visitThrowingCstInsn(insn);
    addIntroductionIfNecessary(insn);
}
 
Example #18
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Assigns {@code target} to a newly allocated array of length {@code
 * length}. The array's type is the same as {@code target}'s type.
 */
public <T> void newArray(Local<T> target, Local<Integer> length) {
    addInstruction(new ThrowingCstInsn(Rops.opNewArray(target.type.ropType), sourcePosition,
            RegisterSpecList.make(length.spec()), catches, target.type.constant));
    moveResult(target, true);
}
 
Example #19
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Tests if the value in {@code source} is assignable to {@code type}. If it
 * is, {@code target} is assigned to 1; otherwise {@code target} is assigned
 * to 0.
 */
public void instanceOfType(Local<?> target, Local<?> source, TypeId<?> type) {
    addInstruction(new ThrowingCstInsn(Rops.INSTANCE_OF, sourcePosition,
            RegisterSpecList.make(source.spec()), catches, type.constant));
    moveResult(target, true);
}
 
Example #20
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Copies the value in {@code source} to the static field {@code fieldId}.
 */
public <V> void sput(FieldId<?, V> fieldId, Local<? extends V> source) {
    addInstruction(new ThrowingCstInsn(Rops.opPutStatic(source.type.ropType), sourcePosition,
            RegisterSpecList.make(source.spec()), catches, fieldId.constant));
}
 
Example #21
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Copies the value in the static field {@code fieldId} to {@code target}.
 */
public <V> void sget(FieldId<?, ? extends V> fieldId, Local<V> target) {
    addInstruction(new ThrowingCstInsn(Rops.opGetStatic(target.type.ropType), sourcePosition,
            RegisterSpecList.EMPTY, catches, fieldId.constant));
    moveResult(target, true);
}
 
Example #22
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Copies the value in {@code source} to the instance field {@code fieldId}
 * of {@code instance}.
 */
public <D, V> void iput(FieldId<D, V> fieldId, Local<? extends D> instance, Local<? extends V> source) {
    addInstruction(new ThrowingCstInsn(Rops.opPutField(source.type.ropType), sourcePosition,
            RegisterSpecList.make(source.spec(), instance.spec()), catches, fieldId.constant));
}
 
Example #23
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Copies the value in instance field {@code fieldId} of {@code instance} to
 * {@code target}.
 */
public <D, V> void iget(FieldId<D, ? extends V> fieldId, Local<V> target, Local<D> instance) {
    addInstruction(new ThrowingCstInsn(Rops.opGetField(target.type.ropType), sourcePosition,
            RegisterSpecList.make(instance.spec()), catches, fieldId.constant));
    moveResult(target, true);
}
 
Example #24
Source File: ConstCollector.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Applies the optimization.
 */
private void run() {
    int regSz = ssaMeth.getRegCount();

    ArrayList<TypedConstant> constantList
            = getConstsSortedByCountUse();

    int toCollect = Math.min(constantList.size(), MAX_COLLECTED_CONSTANTS);

    SsaBasicBlock start = ssaMeth.getEntryBlock();

    // Constant to new register containing the constant
    HashMap<TypedConstant, RegisterSpec> newRegs
            = new HashMap<TypedConstant, RegisterSpec> (toCollect);

    for (int i = 0; i < toCollect; i++) {
        TypedConstant cst = constantList.get(i);
        RegisterSpec result
                = RegisterSpec.make(ssaMeth.makeNewSsaReg(), cst);

        Rop constRop = Rops.opConst(cst);

        if (constRop.getBranchingness() == Rop.BRANCH_NONE) {
            start.addInsnToHead(
                    new PlainCstInsn(Rops.opConst(cst),
                            SourcePosition.NO_INFO, result,
                            RegisterSpecList.EMPTY, cst));
        } else {
            // We need two new basic blocks along with the new insn
            SsaBasicBlock entryBlock = ssaMeth.getEntryBlock();
            SsaBasicBlock successorBlock
                    = entryBlock.getPrimarySuccessor();

            // Insert a block containing the const insn.
            SsaBasicBlock constBlock
                    = entryBlock.insertNewSuccessor(successorBlock);

            constBlock.replaceLastInsn(
                    new ThrowingCstInsn(constRop, SourcePosition.NO_INFO,
                            RegisterSpecList.EMPTY,
                            StdTypeList.EMPTY, cst));

            // Insert a block containing the move-result-pseudo insn.

            SsaBasicBlock resultBlock
                    = constBlock.insertNewSuccessor(successorBlock);
            PlainInsn insn
                = new PlainInsn(
                        Rops.opMoveResultPseudo(result.getTypeBearer()),
                        SourcePosition.NO_INFO,
                        result, RegisterSpecList.EMPTY);

            resultBlock.addInsnToHead(insn);
        }

        newRegs.put(cst, result);
    }

    updateConstUses(newRegs, regSz);
}
 
Example #25
Source File: RopTranslator.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitThrowingCstInsn(ThrowingCstInsn insn) {
    super.visitThrowingCstInsn(insn);
    addIntroductionIfNecessary(insn);
}
 
Example #26
Source File: ConstCollector.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Applies the optimization.
 */
private void run() {
    int regSz = ssaMeth.getRegCount();

    ArrayList<TypedConstant> constantList
            = getConstsSortedByCountUse();

    int toCollect = Math.min(constantList.size(), MAX_COLLECTED_CONSTANTS);

    SsaBasicBlock start = ssaMeth.getEntryBlock();

    // Constant to new register containing the constant
    HashMap<TypedConstant, RegisterSpec> newRegs
            = new HashMap<TypedConstant, RegisterSpec> (toCollect);

    for (int i = 0; i < toCollect; i++) {
        TypedConstant cst = constantList.get(i);
        RegisterSpec result
                = RegisterSpec.make(ssaMeth.makeNewSsaReg(), cst);

        Rop constRop = Rops.opConst(cst);

        if (constRop.getBranchingness() == Rop.BRANCH_NONE) {
            start.addInsnToHead(
                    new PlainCstInsn(Rops.opConst(cst),
                            SourcePosition.NO_INFO, result,
                            RegisterSpecList.EMPTY, cst));
        } else {
            // We need two new basic blocks along with the new insn
            SsaBasicBlock entryBlock = ssaMeth.getEntryBlock();
            SsaBasicBlock successorBlock
                    = entryBlock.getPrimarySuccessor();

            // Insert a block containing the const insn.
            SsaBasicBlock constBlock
                    = entryBlock.insertNewSuccessor(successorBlock);

            constBlock.replaceLastInsn(
                    new ThrowingCstInsn(constRop, SourcePosition.NO_INFO,
                            RegisterSpecList.EMPTY,
                            StdTypeList.EMPTY, cst));

            // Insert a block containing the move-result-pseudo insn.

            SsaBasicBlock resultBlock
                    = constBlock.insertNewSuccessor(successorBlock);
            PlainInsn insn
                = new PlainInsn(
                        Rops.opMoveResultPseudo(result.getTypeBearer()),
                        SourcePosition.NO_INFO,
                        result, RegisterSpecList.EMPTY);

            resultBlock.addInsnToHead(insn);
        }

        newRegs.put(cst, result);
    }

    updateConstUses(newRegs, regSz);
}
 
Example #27
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Performs either a numeric cast or a type cast.
 * <p>
 * <h3>Numeric Casts</h3>
 * Converts a primitive to a different representation. Numeric casts may
 * be lossy. For example, converting the double {@code 1.8d} to an integer
 * yields {@code 1}, losing the fractional part. Converting the integer
 * {@code 0x12345678} to a short yields {@code 0x5678}, losing the high
 * bytes. The following numeric casts are supported:
 * <p>
 * <p><table border="1" summary="Supported Numeric Casts">
 * <tr><th>From</th><th>To</th></tr>
 * <tr><td>int</td><td>byte, char, short, long, float, double</td></tr>
 * <tr><td>long</td><td>int, float, double</td></tr>
 * <tr><td>float</td><td>int, long, double</td></tr>
 * <tr><td>double</td><td>int, long, float</td></tr>
 * </table>
 * <p>
 * <p>For some primitive conversions it will be necessary to chain multiple
 * cast operations. For example, to go from float to short one would first
 * cast float to int and then int to short.
 * <p>
 * <p>Numeric casts never throw {@link ClassCastException}.
 * <p>
 * <h3>Type Casts</h3>
 * Checks that a reference value is assignable to the target type. If it is
 * assignable it is copied to the target local. If it is not assignable a
 * {@link ClassCastException} is thrown.
 */
public void cast(Local<?> target, Local<?> source) {
    if (source.getType().ropType.isReference()) {
        addInstruction(new ThrowingCstInsn(Rops.CHECK_CAST, sourcePosition,
                RegisterSpecList.make(source.spec()), catches, target.type.constant));
        moveResult(target, true);
    } else {
        addInstruction(new PlainInsn(getCastRop(source.type.ropType, target.type.ropType),
                sourcePosition, target.spec(), source.spec()));
    }
}
 
Example #28
Source File: Code.java    From dexmaker with Apache License 2.0 3 votes vote down vote up
/**
 * Performs either a numeric cast or a type cast.
 *
 * <h3>Numeric Casts</h3>
 * Converts a primitive to a different representation. Numeric casts may
 * be lossy. For example, converting the double {@code 1.8d} to an integer
 * yields {@code 1}, losing the fractional part. Converting the integer
 * {@code 0x12345678} to a short yields {@code 0x5678}, losing the high
 * bytes. The following numeric casts are supported:
 *
 * <p><table border="1" summary="Supported Numeric Casts">
 * <tr><th>From</th><th>To</th></tr>
 * <tr><td>int</td><td>byte, char, short, long, float, double</td></tr>
 * <tr><td>long</td><td>int, float, double</td></tr>
 * <tr><td>float</td><td>int, long, double</td></tr>
 * <tr><td>double</td><td>int, long, float</td></tr>
 * </table>
 *
 * <p>For some primitive conversions it will be necessary to chain multiple
 * cast operations. For example, to go from float to short one would first
 * cast float to int and then int to short.
 *
 * <p>Numeric casts never throw {@link ClassCastException}.
 *
 * <h3>Type Casts</h3>
 * Checks that a reference value is assignable to the target type. If it is
 * assignable it is copied to the target local. If it is not assignable a
 * {@link ClassCastException} is thrown.
 */
public void cast(Local<?> target, Local<?> source) {
    if (source.getType().ropType.isReference()) {
        addInstruction(new ThrowingCstInsn(Rops.CHECK_CAST, sourcePosition,
                RegisterSpecList.make(source.spec()), catches, target.type.constant));
        moveResult(target, true);
    } else {
        addInstruction(new PlainInsn(getCastRop(source.type.ropType, target.type.ropType),
                sourcePosition, target.spec(), source.spec()));
    }
}