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

The following examples show how to use com.android.dx.rop.code.Rop. 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 J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitThrowingInsn(ThrowingInsn insn) {
    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    Rop rop = insn.getOpcode();
    RegisterSpec realResult;

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

    realResult = getNextMoveResultPseudo();

    if (opcode.hasResult() != (realResult != null)) {
        throw new RuntimeException(
                "Insn with result/move-result-pseudo mismatch" + insn);
    }

    addOutput(lastAddress);

    DalvInsn di = new SimpleInsn(opcode, pos,
            getRegs(insn, realResult));

    addOutput(di);
}
 
Example #2
Source File: NormalSsaInsn.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * TODO: Increase the scope of this.
 */
@Override
public boolean hasSideEffect() {
    Rop opcode = getOpcode();

    if (opcode.getBranchingness() != Rop.BRANCH_NONE) {
        return true;
    }

    boolean hasLocalSideEffect
        = Optimizer.getPreserveLocals() && getLocalAssignment() != null;

    switch (opcode.getOpcode()) {
        case RegOps.MOVE_RESULT:
        case RegOps.MOVE:
        case RegOps.CONST:
            return hasLocalSideEffect;
        default:
            return true;
    }
}
 
Example #3
Source File: NormalSsaInsn.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * TODO: Increase the scope of this.
 */
@Override
public boolean hasSideEffect() {
    Rop opcode = getOpcode();

    if (opcode.getBranchingness() != Rop.BRANCH_NONE) {
        return true;
    }

    boolean hasLocalSideEffect
        = Optimizer.getPreserveLocals() && getLocalAssignment() != null;

    switch (opcode.getOpcode()) {
        case RegOps.MOVE_RESULT:
        case RegOps.MOVE:
        case RegOps.CONST:
            return hasLocalSideEffect;
        default:
            return true;
    }
}
 
Example #4
Source File: RopTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitThrowingInsn(ThrowingInsn insn) {
    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    Rop rop = insn.getOpcode();
    RegisterSpec realResult;

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

    realResult = getNextMoveResultPseudo();

    if (opcode.hasResult() != (realResult != null)) {
        throw new RuntimeException(
                "Insn with result/move-result-pseudo mismatch" + insn);
    }

    addOutput(lastAddress);

    DalvInsn di = new SimpleInsn(opcode, pos,
            getRegs(insn, realResult));

    addOutput(di);
}
 
Example #5
Source File: RopTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitFillArrayDataInsn(FillArrayDataInsn insn) {
    SourcePosition pos = insn.getPosition();
    Constant cst = insn.getConstant();
    ArrayList<Constant> values = insn.getInitValues();
    Rop rop = insn.getOpcode();

    if (rop.getBranchingness() != Rop.BRANCH_NONE) {
        throw new RuntimeException("shouldn't happen");
    }
    CodeAddress dataAddress = new CodeAddress(pos);
    ArrayData dataInsn =
        new ArrayData(pos, lastAddress, values, cst);

    TargetInsn fillArrayDataInsn =
        new TargetInsn(Dops.FILL_ARRAY_DATA, pos, getRegs(insn),
                dataAddress);

    addOutput(lastAddress);
    addOutput(fillArrayDataInsn);

    addOutputSuffix(new OddSpacer(pos));
    addOutputSuffix(dataAddress);
    addOutputSuffix(dataInsn);
}
 
Example #6
Source File: RopperMachine.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Sets or updates the information about the return block.
 *
 * @param op {@code non-null;} the opcode to use
 * @param pos {@code non-null;} the position to use
 */
private void updateReturnOp(Rop op, SourcePosition pos) {
    if (op == null) {
        throw new NullPointerException("op == null");
    }

    if (pos == null) {
        throw new NullPointerException("pos == null");
    }

    if (returnOp == null) {
        returnOp = op;
        returnPosition = pos;
    } else {
        if (returnOp != op) {
            throw new SimException("return op mismatch: " + op + ", " +
                                   returnOp);
        }

        if (pos.getLine() > returnPosition.getLine()) {
            // Pick the largest line number to be the "canonical" return.
            returnPosition = pos;
        }
    }
}
 
Example #7
Source File: RopperMachine.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Sets or updates the information about the return block.
 *
 * @param op {@code non-null;} the opcode to use
 * @param pos {@code non-null;} the position to use
 */
private void updateReturnOp(Rop op, SourcePosition pos) {
    if (op == null) {
        throw new NullPointerException("op == null");
    }

    if (pos == null) {
        throw new NullPointerException("pos == null");
    }

    if (returnOp == null) {
        returnOp = op;
        returnPosition = pos;
    } else {
        if (returnOp != op) {
            throw new SimException("return op mismatch: " + op + ", " +
                                   returnOp);
        }

        if (pos.getLine() > returnPosition.getLine()) {
            // Pick the largest line number to be the "canonical" return.
            returnPosition = pos;
        }
    }
}
 
Example #8
Source File: EscapeAnalysis.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate through all the uses of a new object.
 *
 * @param result {@code non-null;} register where new object is stored
 * @param escSet {@code non-null;} EscapeSet for the new object
 */
private void processRegister(RegisterSpec result, EscapeSet escSet) {
    ArrayList<RegisterSpec> regWorklist = new ArrayList<RegisterSpec>();
    regWorklist.add(result);

    // Go through the worklist
    while (!regWorklist.isEmpty()) {
        int listSize = regWorklist.size() - 1;
        RegisterSpec def = regWorklist.remove(listSize);
        List<SsaInsn> useList = ssaMeth.getUseListForRegister(def.getReg());

        // Handle all the uses of this register
        for (SsaInsn use : useList) {
            Rop useOpcode = use.getOpcode();

            if (useOpcode == null) {
                // Handle phis
                processPhiUse(use, escSet, regWorklist);
            } else {
                // Handle other opcodes
                processUse(def, use, escSet, regWorklist);
            }
        }
    }
}
 
Example #9
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 #10
Source File: RopTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitInvokePolymorphicInsn(InvokePolymorphicInsn insn) {
    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    Rop rop = insn.getOpcode();

    if (rop.getBranchingness() != Rop.BRANCH_THROW) {
        throw new RuntimeException("Expected BRANCH_THROW got " + rop.getBranchingness());
    } else if (!rop.isCallLike()) {
        throw new RuntimeException("Expected call-like operation");
    }

    addOutput(lastAddress);

    RegisterSpecList regs = insn.getSources();
    Constant[] constants = new Constant[] {
        insn.getPolymorphicMethod(),
        insn.getCallSiteProto()
        };
    DalvInsn di = new MultiCstInsn(opcode, pos, regs, constants);

    addOutput(di);
}
 
Example #11
Source File: RopTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitThrowingInsn(ThrowingInsn insn) {
    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    Rop rop = insn.getOpcode();
    RegisterSpec realResult;

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

    realResult = getNextMoveResultPseudo();

    if (opcode.hasResult() != (realResult != null)) {
        throw new RuntimeException(
                "Insn with result/move-result-pseudo mismatch" + insn);
    }

    addOutput(lastAddress);

    DalvInsn di = new SimpleInsn(opcode, pos,
            getRegs(insn, realResult));

    addOutput(di);
}
 
Example #12
Source File: RopTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitFillArrayDataInsn(FillArrayDataInsn insn) {
    SourcePosition pos = insn.getPosition();
    Constant cst = insn.getConstant();
    ArrayList<Constant> values = insn.getInitValues();
    Rop rop = insn.getOpcode();

    if (rop.getBranchingness() != Rop.BRANCH_NONE) {
        throw new RuntimeException("shouldn't happen");
    }
    CodeAddress dataAddress = new CodeAddress(pos);
    ArrayData dataInsn =
        new ArrayData(pos, lastAddress, values, cst);

    TargetInsn fillArrayDataInsn =
        new TargetInsn(Dops.FILL_ARRAY_DATA, pos, getRegs(insn),
                dataAddress);

    addOutput(lastAddress);
    addOutput(fillArrayDataInsn);

    addOutputSuffix(new OddSpacer(pos));
    addOutputSuffix(dataAddress);
    addOutputSuffix(dataInsn);
}
 
Example #13
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 #14
Source File: EscapeAnalysis.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate through all the uses of a new object.
 *
 * @param result {@code non-null;} register where new object is stored
 * @param escSet {@code non-null;} EscapeSet for the new object
 */
private void processRegister(RegisterSpec result, EscapeSet escSet) {
    ArrayList<RegisterSpec> regWorklist = new ArrayList<RegisterSpec>();
    regWorklist.add(result);

    // Go through the worklist
    while (!regWorklist.isEmpty()) {
        int listSize = regWorklist.size() - 1;
        RegisterSpec def = regWorklist.remove(listSize);
        List<SsaInsn> useList = ssaMeth.getUseListForRegister(def.getReg());

        // Handle all the uses of this register
        for (SsaInsn use : useList) {
            Rop useOpcode = use.getOpcode();

            if (useOpcode == null) {
                // Handle phis
                processPhiUse(use, escSet, regWorklist);
            } else {
                // Handle other opcodes
                processUse(def, use, escSet, regWorklist);
            }
        }
    }
}
 
Example #15
Source File: EscapeAnalysis.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate through all the uses of a new object.
 *
 * @param result {@code non-null;} register where new object is stored
 * @param escSet {@code non-null;} EscapeSet for the new object
 */
private void processRegister(RegisterSpec result, EscapeSet escSet) {
    ArrayList<RegisterSpec> regWorklist = new ArrayList<RegisterSpec>();
    regWorklist.add(result);

    // Go through the worklist
    while (!regWorklist.isEmpty()) {
        int listSize = regWorklist.size() - 1;
        RegisterSpec def = regWorklist.remove(listSize);
        List<SsaInsn> useList = ssaMeth.getUseListForRegister(def.getReg());

        // Handle all the uses of this register
        for (SsaInsn use : useList) {
            Rop useOpcode = use.getOpcode();

            if (useOpcode == null) {
                // Handle phis
                processPhiUse(use, escSet, regWorklist);
            } else {
                // Handle other opcodes
                processUse(def, use, escSet, regWorklist);
            }
        }
    }
}
 
Example #16
Source File: NormalSsaInsn.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * TODO: Increase the scope of this.
 */
@Override
public boolean hasSideEffect() {
    Rop opcode = getOpcode();

    if (opcode.getBranchingness() != Rop.BRANCH_NONE) {
        return true;
    }

    boolean hasLocalSideEffect
        = Optimizer.getPreserveLocals() && getLocalAssignment() != null;

    switch (opcode.getOpcode()) {
        case RegOps.MOVE_RESULT:
        case RegOps.MOVE:
        case RegOps.CONST:
            return hasLocalSideEffect;
        default:
            return true;
    }
}
 
Example #17
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 #18
Source File: RopTranslator.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
public void visitThrowingInsn(ThrowingInsn insn) {
    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    Rop rop = insn.getOpcode();
    RegisterSpec realResult;

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

    realResult = getNextMoveResultPseudo();

    if (opcode.hasResult() != (realResult != null)) {
        throw new RuntimeException(
                "Insn with result/move-result-pseudo mismatch" + insn);
    }

    addOutput(lastAddress);

    DalvInsn di = new SimpleInsn(opcode, pos,
            getRegs(insn, realResult));

    addOutput(di);
}
 
Example #19
Source File: RopperMachine.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Sets or updates the information about the return block.
 *
 * @param op {@code non-null;} the opcode to use
 * @param pos {@code non-null;} the position to use
 */
private void updateReturnOp(Rop op, SourcePosition pos) {
    if (op == null) {
        throw new NullPointerException("op == null");
    }

    if (pos == null) {
        throw new NullPointerException("pos == null");
    }

    if (returnOp == null) {
        returnOp = op;
        returnPosition = pos;
    } else {
        if (returnOp != op) {
            throw new SimException("return op mismatch: " + op + ", " +
                                   returnOp);
        }

        if (pos.getLine() > returnPosition.getLine()) {
            // Pick the largest line number to be the "canonical" return.
            returnPosition = pos;
        }
    }
}
 
Example #20
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
      @Override
public void visitFillArrayDataInsn(FillArrayDataInsn insn) {
          SourcePosition pos = insn.getPosition();
          Constant cst = insn.getConstant();
          ArrayList<Constant> values = insn.getInitValues();
          Rop rop = insn.getOpcode();

          if (rop.getBranchingness() != Rop.BRANCH_NONE) {
              throw new RuntimeException("shouldn't happen");
          }
          CodeAddress dataAddress = new CodeAddress(pos);
          ArrayData dataInsn =
              new ArrayData(pos, lastAddress, values, cst);

          TargetInsn fillArrayDataInsn =
              new TargetInsn(Dops.FILL_ARRAY_DATA, pos, getRegs(insn),
                      dataAddress);

          addOutput(lastAddress);
          addOutput(fillArrayDataInsn);

          addOutputSuffix(new OddSpacer(pos));
          addOutputSuffix(dataAddress);
          addOutputSuffix(dataInsn);
      }
 
Example #21
Source File: RopperMachine.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Sets or updates the information about the return block.
 *
 * @param op {@code non-null;} the opcode to use
 * @param pos {@code non-null;} the position to use
 */
private void updateReturnOp(Rop op, SourcePosition pos) {
    if (op == null) {
        throw new NullPointerException("op == null");
    }

    if (pos == null) {
        throw new NullPointerException("pos == null");
    }

    if (returnOp == null) {
        returnOp = op;
        returnPosition = pos;
    } else {
        if (returnOp != op) {
            throw new SimException("return op mismatch: " + op + ", " +
                                   returnOp);
        }

        if (pos.getLine() > returnPosition.getLine()) {
            // Pick the largest line number to be the "canonical" return.
            returnPosition = pos;
        }
    }
}
 
Example #22
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitInvokePolymorphicInsn(InvokePolymorphicInsn insn) {
    SourcePosition pos = insn.getPosition();
    Dop opcode = RopToDop.dopFor(insn);
    Rop rop = insn.getOpcode();

    if (rop.getBranchingness() != Rop.BRANCH_THROW) {
        throw new RuntimeException("Expected BRANCH_THROW got " + rop.getBranchingness());
    } else if (!rop.isCallLike()) {
        throw new RuntimeException("Expected call-like operation");
    }

    addOutput(lastAddress);

    RegisterSpecList regs = insn.getSources();
    Constant[] constants = new Constant[] {
        insn.getInvokeMethod(),
        insn.getCallSiteProto()
        };
    DalvInsn di = new MultiCstInsn(opcode, pos, regs, constants);

    addOutput(di);
}
 
Example #23
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 #24
Source File: Code.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Compare floats or doubles. This stores -1 in {@code target} if {@code
 * a < b}, 0 in {@code target} if {@code a == b} and 1 in target if {@code
 * a > b}. This stores {@code nanValue} in {@code target} if either value
 * is {@code NaN}.
 */
public <T extends Number> void compareFloatingPoint(
        Local<Integer> target, Local<T> a, Local<T> b, int nanValue) {
    Rop rop;
    if (nanValue == 1) {
        rop = Rops.opCmpg(a.type.ropType);
    } else if (nanValue == -1) {
        rop = Rops.opCmpl(a.type.ropType);
    } else {
        throw new IllegalArgumentException("expected 1 or -1 but was " + nanValue);
    }
    addInstruction(new PlainInsn(rop, sourcePosition, target.spec(),
            RegisterSpecList.make(a.spec(), b.spec())));
}
 
Example #25
Source File: LiteralOpUpgrader.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to replace an instruction with a const instruction. The given
 * instruction must have a constant result for it to be replaced.
 *
 * @param insn {@code non-null;} instruction to try to replace
 * @return true if the instruction was replaced
 */
private boolean tryReplacingWithConstant(NormalSsaInsn insn) {
    Insn originalRopInsn = insn.getOriginalRopInsn();
    Rop opcode = originalRopInsn.getOpcode();
    RegisterSpec result = insn.getResult();

    if (result != null && !ssaMeth.isRegALocal(result) &&
            opcode.getOpcode() != RegOps.CONST) {
        TypeBearer type = insn.getResult().getTypeBearer();
        if (type.isConstant() && type.getBasicType() == Type.BT_INT) {
            // Replace the instruction with a constant
            replacePlainInsn(insn, RegisterSpecList.EMPTY,
                    RegOps.CONST, (Constant) type);

            // Remove the source as well if this is a move-result-pseudo
            if (opcode.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
                int pred = insn.getBlock().getPredecessors().nextSetBit(0);
                ArrayList<SsaInsn> predInsns =
                        ssaMeth.getBlocks().get(pred).getInsns();
                NormalSsaInsn sourceInsn =
                        (NormalSsaInsn) predInsns.get(predInsns.size()-1);
                replacePlainInsn(sourceInsn, RegisterSpecList.EMPTY,
                        RegOps.GOTO, null);
            }
            return true;
        }
    }
    return false;
}
 
Example #26
Source File: SsaBasicBlock.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces the last insn in this block. The provided insn must have
 * some branchingness.
 *
 * @param insn {@code non-null;} rop-form insn to add, which must branch.
 */
public void replaceLastInsn(Insn insn) {
    if (insn.getOpcode().getBranchingness() == Rop.BRANCH_NONE) {
        throw new IllegalArgumentException("last insn must branch");
    }

    SsaInsn oldInsn = insns.get(insns.size() - 1);
    SsaInsn newInsn = SsaInsn.makeFromRop(insn, this);

    insns.set(insns.size() - 1, newInsn);

    parent.onInsnRemoved(oldInsn);
    parent.onInsnAdded(newInsn);
}
 
Example #27
Source File: SsaToRop.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Validates that a basic block is a valid end predecessor. It must
 * end in a RETURN or a THROW. Throws a runtime exception on error.
 *
 * @param b {@code non-null;} block to validate
 * @throws RuntimeException on error
 */
private void verifyValidExitPredecessor(SsaBasicBlock b) {
    ArrayList<SsaInsn> insns = b.getInsns();
    SsaInsn lastInsn = insns.get(insns.size() - 1);
    Rop opcode = lastInsn.getOpcode();

    if (opcode.getBranchingness() != Rop.BRANCH_RETURN
            && opcode != Rops.THROW) {
        throw new RuntimeException("Exit predecessor must end"
                + " in valid exit statement.");
    }
}
 
Example #28
Source File: Code.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
private Rop getCastRop(com.android.dx.rop.type.Type sourceType,
        com.android.dx.rop.type.Type targetType) {
    if (sourceType.getBasicType() == BT_INT) {
        switch (targetType.getBasicType()) {
        case BT_SHORT:
            return Rops.TO_SHORT;
        case BT_CHAR:
            return Rops.TO_CHAR;
        case BT_BYTE:
            return Rops.TO_BYTE;
        }
    }
    return Rops.opConv(targetType, sourceType);
}
 
Example #29
Source File: LiteralOpUpgrader.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to replace an instruction with a const instruction. The given
 * instruction must have a constant result for it to be replaced.
 *
 * @param insn {@code non-null;} instruction to try to replace
 * @return true if the instruction was replaced
 */
private boolean tryReplacingWithConstant(NormalSsaInsn insn) {
    Insn originalRopInsn = insn.getOriginalRopInsn();
    Rop opcode = originalRopInsn.getOpcode();
    RegisterSpec result = insn.getResult();

    if (result != null && !ssaMeth.isRegALocal(result) &&
            opcode.getOpcode() != RegOps.CONST) {
        TypeBearer type = insn.getResult().getTypeBearer();
        if (type.isConstant() && type.getBasicType() == Type.BT_INT) {
            // Replace the instruction with a constant
            replacePlainInsn(insn, RegisterSpecList.EMPTY,
                    RegOps.CONST, (Constant) type);

            // Remove the source as well if this is a move-result-pseudo
            if (opcode.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
                int pred = insn.getBlock().getPredecessors().nextSetBit(0);
                ArrayList<SsaInsn> predInsns =
                        ssaMeth.getBlocks().get(pred).getInsns();
                NormalSsaInsn sourceInsn =
                        (NormalSsaInsn) predInsns.get(predInsns.size()-1);
                replacePlainInsn(sourceInsn, RegisterSpecList.EMPTY,
                        RegOps.GOTO, null);
            }
            return true;
        }
    }
    return false;
}
 
Example #30
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);
    }
}