Java Code Examples for com.android.dx.rop.code.Rop#BRANCH_IF

The following examples show how to use com.android.dx.rop.code.Rop#BRANCH_IF . 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: RopTranslator.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Helper for {@link #outputInstructions}, which does the processing
 * and output of one block.
 *
 * @param block {@code non-null;} the block to process and output
 * @param nextLabel {@code >= -1;} the next block that will be processed, or
 * {@code -1} if there is no next block
 */
private void outputBlock(BasicBlock block, int nextLabel) {
    // Append the code address for this block.
    CodeAddress startAddress = addresses.getStart(block);
    output.add(startAddress);

    // Append the local variable state for the block.
    if (locals != null) {
        RegisterSpecSet starts = locals.getStarts(block);
        output.add(new LocalSnapshot(startAddress.getPosition(),
                                     starts));
    }

    /*
     * Choose and append an output instruction for each original
     * instruction.
     */
    translationVisitor.setBlock(block, addresses.getLast(block));
    block.getInsns().forEach(translationVisitor);

    // Insert the block end code address.
    output.add(addresses.getEnd(block));

    // Set up for end-of-block activities.

    int succ = block.getPrimarySuccessor();
    Insn lastInsn = block.getLastInsn();

    /*
     * Check for (and possibly correct for) a non-optimal choice of
     * which block will get output next.
     */

    if ((succ >= 0) && (succ != nextLabel)) {
        /*
         * The block has a "primary successor" and that primary
         * successor isn't the next block to be output.
         */
        Rop lastRop = lastInsn.getOpcode();
        if ((lastRop.getBranchingness() == Rop.BRANCH_IF) &&
                (block.getSecondarySuccessor() == nextLabel)) {
            /*
             * The block ends with an "if" of some sort, and its
             * secondary successor (the "then") is in fact the
             * next block to output. So, reverse the sense of
             * the test, so that we can just emit the next block
             * without an interstitial goto.
             */
            output.reverseBranch(1, addresses.getStart(succ));
        } else {
            /*
             * Our only recourse is to add a goto here to get the
             * flow to be correct.
             */
            TargetInsn insn =
                new TargetInsn(Dops.GOTO, lastInsn.getPosition(),
                        RegisterSpecList.EMPTY,
                        addresses.getStart(succ));
            output.add(insn);
        }
    }
}
 
Example 2
Source File: RopTranslator.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitPlainInsn(PlainInsn insn) {
    Rop rop = insn.getOpcode();
    if (rop.getOpcode() == RegOps.MARK_LOCAL) {
        /*
         * Ignore these. They're dealt with by
         * the LocalVariableAwareTranslationVisitor
         */
        return;
    }
    if (rop.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
        // These get skipped
        return;
    }

    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    DalvInsn di;

    switch (rop.getBranchingness()) {
        case Rop.BRANCH_NONE:
        case Rop.BRANCH_RETURN:
        case Rop.BRANCH_THROW: {
            di = new SimpleInsn(opcode, pos, getRegs(insn));
            break;
        }
        case Rop.BRANCH_GOTO: {
            /*
             * Code in the main translation loop will emit a
             * goto if necessary (if the branch isn't to the
             * immediately subsequent block).
             */
            return;
        }
        case Rop.BRANCH_IF: {
            int target = block.getSuccessors().get(1);
            di = new TargetInsn(opcode, pos, getRegs(insn),
                                addresses.getStart(target));
            break;
        }
        default: {
            throw new RuntimeException("shouldn't happen");
        }
    }

    addOutput(di);
}
 
Example 3
Source File: RopTranslator.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Helper for {@link #outputInstructions}, which does the processing
 * and output of one block.
 *
 * @param block {@code non-null;} the block to process and output
 * @param nextLabel {@code >= -1;} the next block that will be processed, or
 * {@code -1} if there is no next block
 */
private void outputBlock(BasicBlock block, int nextLabel) {
    // Append the code address for this block.
    CodeAddress startAddress = addresses.getStart(block);
    output.add(startAddress);

    // Append the local variable state for the block.
    if (locals != null) {
        RegisterSpecSet starts = locals.getStarts(block);
        output.add(new LocalSnapshot(startAddress.getPosition(),
                                     starts));
    }

    /*
     * Choose and append an output instruction for each original
     * instruction.
     */
    translationVisitor.setBlock(block, addresses.getLast(block));
    block.getInsns().forEach(translationVisitor);

    // Insert the block end code address.
    output.add(addresses.getEnd(block));

    // Set up for end-of-block activities.

    int succ = block.getPrimarySuccessor();
    Insn lastInsn = block.getLastInsn();

    /*
     * Check for (and possibly correct for) a non-optimal choice of
     * which block will get output next.
     */

    if ((succ >= 0) && (succ != nextLabel)) {
        /*
         * The block has a "primary successor" and that primary
         * successor isn't the next block to be output.
         */
        Rop lastRop = lastInsn.getOpcode();
        if ((lastRop.getBranchingness() == Rop.BRANCH_IF) &&
                (block.getSecondarySuccessor() == nextLabel)) {
            /*
             * The block ends with an "if" of some sort, and its
             * secondary successor (the "then") is in fact the
             * next block to output. So, reverse the sense of
             * the test, so that we can just emit the next block
             * without an interstitial goto.
             */
            output.reverseBranch(1, addresses.getStart(succ));
        } else {
            /*
             * Our only recourse is to add a goto here to get the
             * flow to be correct.
             */
            TargetInsn insn =
                new TargetInsn(Dops.GOTO, lastInsn.getPosition(),
                        RegisterSpecList.EMPTY,
                        addresses.getStart(succ));
            output.add(insn);
        }
    }
}
 
Example 4
Source File: RopTranslator.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitPlainInsn(PlainInsn insn) {
    Rop rop = insn.getOpcode();
    if (rop.getOpcode() == RegOps.MARK_LOCAL) {
        /*
         * Ignore these. They're dealt with by
         * the LocalVariableAwareTranslationVisitor
         */
        return;
    }
    if (rop.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
        // These get skipped
        return;
    }

    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    DalvInsn di;

    switch (rop.getBranchingness()) {
        case Rop.BRANCH_NONE:
        case Rop.BRANCH_RETURN:
        case Rop.BRANCH_THROW: {
            di = new SimpleInsn(opcode, pos, getRegs(insn));
            break;
        }
        case Rop.BRANCH_GOTO: {
            /*
             * Code in the main translation loop will emit a
             * goto if necessary (if the branch isn't to the
             * immediately subsequent block).
             */
            return;
        }
        case Rop.BRANCH_IF: {
            int target = block.getSuccessors().get(1);
            di = new TargetInsn(opcode, pos, getRegs(insn),
                                addresses.getStart(target));
            break;
        }
        default: {
            throw new RuntimeException("shouldn't happen");
        }
    }

    addOutput(di);
}
 
Example 5
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @param branch the branches to follow; interpretation depends on the
 *               instruction's branchingness.
 */
private void addInstruction(Insn insn, Label branch) {
    if (currentLabel == null || !currentLabel.marked) {
        throw new IllegalStateException("no current label");
    }
    currentLabel.instructions.add(insn);

    switch (insn.getOpcode().getBranchingness()) {
        case BRANCH_NONE:
            if (branch != null) {
                throw new IllegalArgumentException("unexpected branch: " + branch);
            }
            return;

        case BRANCH_RETURN:
            if (branch != null) {
                throw new IllegalArgumentException("unexpected branch: " + branch);
            }
            currentLabel = null;
            break;

        case BRANCH_GOTO:
            if (branch == null) {
                throw new IllegalArgumentException("branch == null");
            }
            currentLabel.primarySuccessor = branch;
            currentLabel = null;
            break;

        case Rop.BRANCH_IF:
            if (branch == null) {
                throw new IllegalArgumentException("branch == null");
            }
            splitCurrentLabel(branch, Collections.<Label>emptyList());
            break;

        case Rop.BRANCH_THROW:
            if (branch != null) {
                throw new IllegalArgumentException("unexpected branch: " + branch);
            }
            splitCurrentLabel(null, new ArrayList<Label>(catchLabels));
            break;

        default:
            throw new IllegalArgumentException();
    }
}
 
Example 6
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/**
 * Helper for {@link #outputInstructions}, which does the processing
 * and output of one block.
 *
 * @param block {@code non-null;} the block to process and output
 * @param nextLabel {@code >= -1;} the next block that will be processed, or
 * {@code -1} if there is no next block
 */
private void outputBlock(BasicBlock block, int nextLabel) {
    // Append the code address for this block.
    CodeAddress startAddress = addresses.getStart(block);
    output.add(startAddress);

    // Append the local variable state for the block.
    if (locals != null) {
        RegisterSpecSet starts = locals.getStarts(block);
        output.add(new LocalSnapshot(startAddress.getPosition(),
                                     starts));
    }

    /*
     * Choose and append an output instruction for each original
     * instruction.
     */
    translationVisitor.setBlock(block, addresses.getLast(block));
    block.getInsns().forEach(translationVisitor);

    // Insert the block end code address.
    output.add(addresses.getEnd(block));

    // Set up for end-of-block activities.

    int succ = block.getPrimarySuccessor();
    Insn lastInsn = block.getLastInsn();

    /*
     * Check for (and possibly correct for) a non-optimal choice of
     * which block will get output next.
     */

    if ((succ >= 0) && (succ != nextLabel)) {
        /*
         * The block has a "primary successor" and that primary
         * successor isn't the next block to be output.
         */
        Rop lastRop = lastInsn.getOpcode();
        if ((lastRop.getBranchingness() == Rop.BRANCH_IF) &&
                (block.getSecondarySuccessor() == nextLabel)) {
            /*
             * The block ends with an "if" of some sort, and its
             * secondary successor (the "then") is in fact the
             * next block to output. So, reverse the sense of
             * the test, so that we can just emit the next block
             * without an interstitial goto.
             */
            output.reverseBranch(1, addresses.getStart(succ));
        } else {
            /*
             * Our only recourse is to add a goto here to get the
             * flow to be correct.
             */
            TargetInsn insn =
                new TargetInsn(Dops.GOTO, lastInsn.getPosition(),
                        RegisterSpecList.EMPTY,
                        addresses.getStart(succ));
            output.add(insn);
        }
    }
}
 
Example 7
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
      @Override
public void visitPlainInsn(PlainInsn insn) {
          Rop rop = insn.getOpcode();
          if (rop.getOpcode() == RegOps.MARK_LOCAL) {
              /*
               * Ignore these. They're dealt with by
               * the LocalVariableAwareTranslationVisitor
               */
              return;
          }
          if (rop.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
              // These get skipped
              return;
          }

          SourcePosition pos = insn.getPosition();
          Dop opcode = RopToDop.dopFor(insn);
          DalvInsn di;

          switch (rop.getBranchingness()) {
              case Rop.BRANCH_NONE:
              case Rop.BRANCH_RETURN:
              case Rop.BRANCH_THROW: {
                  di = new SimpleInsn(opcode, pos, getRegs(insn));
                  break;
              }
              case Rop.BRANCH_GOTO: {
                  /*
                   * Code in the main translation loop will emit a
                   * goto if necessary (if the branch isn't to the
                   * immediately subsequent block).
                   */
                  return;
              }
              case Rop.BRANCH_IF: {
                  int target = block.getSuccessors().get(1);
                  di = new TargetInsn(opcode, pos, getRegs(insn),
                                      addresses.getStart(target));
                  break;
              }
              default: {
                  throw new RuntimeException("shouldn't happen");
              }
          }

          addOutput(di);
      }
 
Example 8
Source File: Code.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
/**
 * @param branch the branches to follow; interpretation depends on the
 *     instruction's branchingness.
 */
private void addInstruction(Insn insn, Label branch) {
    if (currentLabel == null || !currentLabel.marked) {
        throw new IllegalStateException("no current label");
    }
    currentLabel.instructions.add(insn);

    switch (insn.getOpcode().getBranchingness()) {
    case BRANCH_NONE:
        if (branch != null) {
            throw new IllegalArgumentException("unexpected branch: " + branch);
        }
        return;

    case BRANCH_RETURN:
        if (branch != null) {
            throw new IllegalArgumentException("unexpected branch: " + branch);
        }
        currentLabel = null;
        break;

    case BRANCH_GOTO:
        if (branch == null) {
            throw new IllegalArgumentException("branch == null");
        }
        currentLabel.primarySuccessor = branch;
        currentLabel = null;
        break;

    case Rop.BRANCH_IF:
        if (branch == null) {
            throw new IllegalArgumentException("branch == null");
        }
        splitCurrentLabel(branch, Collections.<Label>emptyList());
        break;

    case Rop.BRANCH_THROW:
        if (branch != null) {
            throw new IllegalArgumentException("unexpected branch: " + branch);
        }
        splitCurrentLabel(null, new ArrayList<Label>(catchLabels));
        break;

    default:
        throw new IllegalArgumentException();
    }
}
 
Example 9
Source File: RopTranslator.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Helper for {@link #outputInstructions}, which does the processing
 * and output of one block.
 *
 * @param block {@code non-null;} the block to process and output
 * @param nextLabel {@code >= -1;} the next block that will be processed, or
 * {@code -1} if there is no next block
 */
private void outputBlock(BasicBlock block, int nextLabel) {
    // Append the code address for this block.
    CodeAddress startAddress = addresses.getStart(block);
    output.add(startAddress);

    // Append the local variable state for the block.
    if (locals != null) {
        RegisterSpecSet starts = locals.getStarts(block);
        output.add(new LocalSnapshot(startAddress.getPosition(),
                                     starts));
    }

    /*
     * Choose and append an output instruction for each original
     * instruction.
     */
    translationVisitor.setBlock(block, addresses.getLast(block));
    block.getInsns().forEach(translationVisitor);

    // Insert the block end code address.
    output.add(addresses.getEnd(block));

    // Set up for end-of-block activities.

    int succ = block.getPrimarySuccessor();
    Insn lastInsn = block.getLastInsn();

    /*
     * Check for (and possibly correct for) a non-optimal choice of
     * which block will get output next.
     */

    if ((succ >= 0) && (succ != nextLabel)) {
        /*
         * The block has a "primary successor" and that primary
         * successor isn't the next block to be output.
         */
        Rop lastRop = lastInsn.getOpcode();
        if ((lastRop.getBranchingness() == Rop.BRANCH_IF) &&
                (block.getSecondarySuccessor() == nextLabel)) {
            /*
             * The block ends with an "if" of some sort, and its
             * secondary successor (the "then") is in fact the
             * next block to output. So, reverse the sense of
             * the test, so that we can just emit the next block
             * without an interstitial goto.
             */
            output.reverseBranch(1, addresses.getStart(succ));
        } else {
            /*
             * Our only recourse is to add a goto here to get the
             * flow to be correct.
             */
            TargetInsn insn =
                new TargetInsn(Dops.GOTO, lastInsn.getPosition(),
                        RegisterSpecList.EMPTY,
                        addresses.getStart(succ));
            output.add(insn);
        }
    }
}
 
Example 10
Source File: RopTranslator.java    From buck with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public void visitPlainInsn(PlainInsn insn) {
    Rop rop = insn.getOpcode();
    if (rop.getOpcode() == RegOps.MARK_LOCAL) {
        /*
         * Ignore these. They're dealt with by
         * the LocalVariableAwareTranslationVisitor
         */
        return;
    }
    if (rop.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
        // These get skipped
        return;
    }

    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    DalvInsn di;

    switch (rop.getBranchingness()) {
        case Rop.BRANCH_NONE:
        case Rop.BRANCH_RETURN:
        case Rop.BRANCH_THROW: {
            di = new SimpleInsn(opcode, pos, getRegs(insn));
            break;
        }
        case Rop.BRANCH_GOTO: {
            /*
             * Code in the main translation loop will emit a
             * goto if necessary (if the branch isn't to the
             * immediately subsequent block).
             */
            return;
        }
        case Rop.BRANCH_IF: {
            int target = block.getSuccessors().get(1);
            di = new TargetInsn(opcode, pos, getRegs(insn),
                                addresses.getStart(target));
            break;
        }
        default: {
            throw new RuntimeException("shouldn't happen");
        }
    }

    addOutput(di);
}