Search in sources :

Example 26 with Rop

use of com.android.dx.rop.code.Rop in project J2ME-Loader by nikita36078.

the class RopToDop method dopFor.

/**
 * Returns the dalvik opcode appropriate for the given register-based
 * instruction.
 *
 * @param insn {@code non-null;} the original instruction
 * @return the corresponding dalvik opcode; one of the constants in
 * {@link Dops}
 */
public static Dop dopFor(Insn insn) {
    Rop rop = insn.getOpcode();
    /*
         * First, just try looking up the rop in the MAP of easy
         * cases.
         */
    Dop result = MAP.get(rop);
    if (result != null) {
        return result;
    }
    switch(rop.getOpcode()) {
        case RegOps.MOVE_EXCEPTION:
            return Dops.MOVE_EXCEPTION;
        case RegOps.INVOKE_STATIC:
            return Dops.INVOKE_STATIC;
        case RegOps.INVOKE_VIRTUAL:
            return Dops.INVOKE_VIRTUAL;
        case RegOps.INVOKE_SUPER:
            return Dops.INVOKE_SUPER;
        case RegOps.INVOKE_DIRECT:
            return Dops.INVOKE_DIRECT;
        case RegOps.INVOKE_INTERFACE:
            return Dops.INVOKE_INTERFACE;
        case RegOps.INVOKE_POLYMORPHIC:
            return Dops.INVOKE_POLYMORPHIC;
        case RegOps.NEW_ARRAY:
            return Dops.NEW_ARRAY;
        case RegOps.FILLED_NEW_ARRAY:
            return Dops.FILLED_NEW_ARRAY;
        case RegOps.FILL_ARRAY_DATA:
            return Dops.FILL_ARRAY_DATA;
        case RegOps.MOVE_RESULT:
            {
                RegisterSpec resultReg = insn.getResult();
                if (resultReg == null) {
                    return Dops.NOP;
                } else {
                    switch(resultReg.getBasicType()) {
                        case Type.BT_INT:
                        case Type.BT_FLOAT:
                        case Type.BT_BOOLEAN:
                        case Type.BT_BYTE:
                        case Type.BT_CHAR:
                        case Type.BT_SHORT:
                            return Dops.MOVE_RESULT;
                        case Type.BT_LONG:
                        case Type.BT_DOUBLE:
                            return Dops.MOVE_RESULT_WIDE;
                        case Type.BT_OBJECT:
                            return Dops.MOVE_RESULT_OBJECT;
                        default:
                            {
                                throw new RuntimeException("Unexpected basic type");
                            }
                    }
                }
            }
        case RegOps.GET_FIELD:
            {
                CstFieldRef ref = (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
                int basicType = ref.getBasicType();
                switch(basicType) {
                    case Type.BT_BOOLEAN:
                        return Dops.IGET_BOOLEAN;
                    case Type.BT_BYTE:
                        return Dops.IGET_BYTE;
                    case Type.BT_CHAR:
                        return Dops.IGET_CHAR;
                    case Type.BT_SHORT:
                        return Dops.IGET_SHORT;
                    case Type.BT_INT:
                        return Dops.IGET;
                }
                break;
            }
        case RegOps.PUT_FIELD:
            {
                CstFieldRef ref = (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
                int basicType = ref.getBasicType();
                switch(basicType) {
                    case Type.BT_BOOLEAN:
                        return Dops.IPUT_BOOLEAN;
                    case Type.BT_BYTE:
                        return Dops.IPUT_BYTE;
                    case Type.BT_CHAR:
                        return Dops.IPUT_CHAR;
                    case Type.BT_SHORT:
                        return Dops.IPUT_SHORT;
                    case Type.BT_INT:
                        return Dops.IPUT;
                }
                break;
            }
        case RegOps.GET_STATIC:
            {
                CstFieldRef ref = (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
                int basicType = ref.getBasicType();
                switch(basicType) {
                    case Type.BT_BOOLEAN:
                        return Dops.SGET_BOOLEAN;
                    case Type.BT_BYTE:
                        return Dops.SGET_BYTE;
                    case Type.BT_CHAR:
                        return Dops.SGET_CHAR;
                    case Type.BT_SHORT:
                        return Dops.SGET_SHORT;
                    case Type.BT_INT:
                        return Dops.SGET;
                }
                break;
            }
        case RegOps.PUT_STATIC:
            {
                CstFieldRef ref = (CstFieldRef) ((ThrowingCstInsn) insn).getConstant();
                int basicType = ref.getBasicType();
                switch(basicType) {
                    case Type.BT_BOOLEAN:
                        return Dops.SPUT_BOOLEAN;
                    case Type.BT_BYTE:
                        return Dops.SPUT_BYTE;
                    case Type.BT_CHAR:
                        return Dops.SPUT_CHAR;
                    case Type.BT_SHORT:
                        return Dops.SPUT_SHORT;
                    case Type.BT_INT:
                        return Dops.SPUT;
                }
                break;
            }
        case RegOps.CONST:
            {
                Constant cst = ((ThrowingCstInsn) insn).getConstant();
                if (cst instanceof CstType) {
                    return Dops.CONST_CLASS;
                } else if (cst instanceof CstString) {
                    return Dops.CONST_STRING;
                }
                break;
            }
    }
    throw new RuntimeException("unknown rop: " + rop);
}
Also used : Rop(com.android.dx.rop.code.Rop) ThrowingCstInsn(com.android.dx.rop.code.ThrowingCstInsn) Constant(com.android.dx.rop.cst.Constant) CstType(com.android.dx.rop.cst.CstType) CstFieldRef(com.android.dx.rop.cst.CstFieldRef) CstString(com.android.dx.rop.cst.CstString) RegisterSpec(com.android.dx.rop.code.RegisterSpec)

Example 27 with Rop

use of com.android.dx.rop.code.Rop in project J2ME-Loader by nikita36078.

the class ConstCollector method run.

/**
 * 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);
}
Also used : PlainCstInsn(com.android.dx.rop.code.PlainCstInsn) PlainInsn(com.android.dx.rop.code.PlainInsn) Rop(com.android.dx.rop.code.Rop) ThrowingCstInsn(com.android.dx.rop.code.ThrowingCstInsn) TypedConstant(com.android.dx.rop.cst.TypedConstant) HashMap(java.util.HashMap) RegisterSpec(com.android.dx.rop.code.RegisterSpec)

Example 28 with Rop

use of com.android.dx.rop.code.Rop in project J2ME-Loader by nikita36078.

the class EscapeAnalysis method processRegister.

/**
 * 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);
            }
        }
    }
}
Also used : Rop(com.android.dx.rop.code.Rop) ArrayList(java.util.ArrayList) RegisterSpec(com.android.dx.rop.code.RegisterSpec)

Example 29 with Rop

use of com.android.dx.rop.code.Rop in project J2ME-Loader by nikita36078.

the class EscapeAnalysis method insertPlainInsnBefore.

/**
 * Inserts a new PlainInsn before the given instruction.
 * TODO: move this somewhere more appropriate
 *
 * @param insn {@code non-null;} instruction to insert before
 * @param newSources {@code non-null;} sources of new instruction
 * @param newResult {@code non-null;} result of new instruction
 * @param newOpcode opcode of new instruction
 * @param cst {@code null-ok;} constant for new instruction, if any
 */
private void insertPlainInsnBefore(SsaInsn insn, RegisterSpecList newSources, RegisterSpec newResult, int newOpcode, Constant cst) {
    Insn originalRopInsn = insn.getOriginalRopInsn();
    Rop newRop;
    if (newOpcode == RegOps.MOVE_RESULT_PSEUDO) {
        newRop = Rops.opMoveResultPseudo(newResult.getType());
    } else {
        newRop = Rops.ropFor(newOpcode, newResult, newSources, cst);
    }
    Insn newRopInsn;
    if (cst == null) {
        newRopInsn = new PlainInsn(newRop, originalRopInsn.getPosition(), newResult, newSources);
    } else {
        newRopInsn = new PlainCstInsn(newRop, originalRopInsn.getPosition(), newResult, newSources, cst);
    }
    NormalSsaInsn newInsn = new NormalSsaInsn(newRopInsn, insn.getBlock());
    List<SsaInsn> insns = insn.getBlock().getInsns();
    insns.add(insns.lastIndexOf(insn), newInsn);
    ssaMeth.onInsnAdded(newInsn);
}
Also used : PlainCstInsn(com.android.dx.rop.code.PlainCstInsn) PlainInsn(com.android.dx.rop.code.PlainInsn) ThrowingCstInsn(com.android.dx.rop.code.ThrowingCstInsn) ThrowingInsn(com.android.dx.rop.code.ThrowingInsn) PlainInsn(com.android.dx.rop.code.PlainInsn) FillArrayDataInsn(com.android.dx.rop.code.FillArrayDataInsn) Insn(com.android.dx.rop.code.Insn) PlainCstInsn(com.android.dx.rop.code.PlainCstInsn) Rop(com.android.dx.rop.code.Rop)

Example 30 with Rop

use of com.android.dx.rop.code.Rop in project J2ME-Loader by nikita36078.

the class EscapeAnalysis method insertThrowingInsnBefore.

/**
 * Inserts a new ThrowingInsn before the given instruction.
 * TODO: move this somewhere more appropriate
 *
 * @param insn {@code non-null;} instruction to insert before
 * @param newSources {@code non-null;} sources of new instruction
 * @param newResult {@code non-null;} result of new instruction
 * @param newOpcode opcode of new instruction
 * @param cst {@code null-ok;} constant for new instruction, if any
 */
private void insertThrowingInsnBefore(SsaInsn insn, RegisterSpecList newSources, RegisterSpec newResult, int newOpcode, Constant cst) {
    Insn origRopInsn = insn.getOriginalRopInsn();
    Rop newRop = Rops.ropFor(newOpcode, newResult, newSources, cst);
    Insn newRopInsn;
    if (cst == null) {
        newRopInsn = new ThrowingInsn(newRop, origRopInsn.getPosition(), newSources, StdTypeList.EMPTY);
    } else {
        newRopInsn = new ThrowingCstInsn(newRop, origRopInsn.getPosition(), newSources, StdTypeList.EMPTY, cst);
    }
    NormalSsaInsn newInsn = new NormalSsaInsn(newRopInsn, insn.getBlock());
    List<SsaInsn> insns = insn.getBlock().getInsns();
    insns.add(insns.lastIndexOf(insn), newInsn);
    ssaMeth.onInsnAdded(newInsn);
}
Also used : ThrowingCstInsn(com.android.dx.rop.code.ThrowingCstInsn) ThrowingInsn(com.android.dx.rop.code.ThrowingInsn) PlainInsn(com.android.dx.rop.code.PlainInsn) FillArrayDataInsn(com.android.dx.rop.code.FillArrayDataInsn) Insn(com.android.dx.rop.code.Insn) PlainCstInsn(com.android.dx.rop.code.PlainCstInsn) Rop(com.android.dx.rop.code.Rop) ThrowingCstInsn(com.android.dx.rop.code.ThrowingCstInsn) ThrowingInsn(com.android.dx.rop.code.ThrowingInsn)

Aggregations

Rop (com.android.dx.rop.code.Rop)36 PlainInsn (com.android.dx.rop.code.PlainInsn)23 PlainCstInsn (com.android.dx.rop.code.PlainCstInsn)19 Insn (com.android.dx.rop.code.Insn)16 ThrowingCstInsn (com.android.dx.rop.code.ThrowingCstInsn)15 RegisterSpec (com.android.dx.rop.code.RegisterSpec)14 ThrowingInsn (com.android.dx.rop.code.ThrowingInsn)11 RegisterSpecList (com.android.dx.rop.code.RegisterSpecList)9 FillArrayDataInsn (com.android.dx.rop.code.FillArrayDataInsn)8 Constant (com.android.dx.rop.cst.Constant)6 CstInteger (com.android.dx.rop.cst.CstInteger)6 SourcePosition (com.android.dx.rop.code.SourcePosition)4 SwitchInsn (com.android.dx.rop.code.SwitchInsn)4 CstFieldRef (com.android.dx.rop.cst.CstFieldRef)4 CstType (com.android.dx.rop.cst.CstType)4 TypedConstant (com.android.dx.rop.cst.TypedConstant)4 TypeBearer (com.android.dx.rop.type.TypeBearer)4 SsaInsn (com.android.dx.ssa.SsaInsn)4 BasicBlock (com.android.dx.rop.code.BasicBlock)2 CstInsn (com.android.dx.rop.code.CstInsn)2