Search in sources :

Example 36 with IntList

use of com.android.dx.util.IntList in project buck by facebook.

the class SsaDumper method endParsingMember.

/** {@inheritDoc} */
@Override
public void endParsingMember(ByteArray bytes, int offset, String name, String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }
    if (!shouldDumpMethod(name)) {
        return;
    }
    if ((member.getAccessFlags() & (AccessFlags.ACC_ABSTRACT | AccessFlags.ACC_NATIVE)) != 0) {
        return;
    }
    ConcreteMethod meth = new ConcreteMethod((Method) member, classFile, true, true);
    TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
    RopMethod rmeth = Ropper.convert(meth, advice, classFile.getMethods());
    SsaMethod ssaMeth = null;
    boolean isStatic = AccessFlags.isStatic(meth.getAccessFlags());
    int paramWidth = computeParamWidth(meth, isStatic);
    if (args.ssaStep == null) {
        ssaMeth = Optimizer.debugNoRegisterAllocation(rmeth, paramWidth, isStatic, true, advice, EnumSet.allOf(Optimizer.OptionalStep.class));
    } else if ("edge-split".equals(args.ssaStep)) {
        ssaMeth = Optimizer.debugEdgeSplit(rmeth, paramWidth, isStatic, true, advice);
    } else if ("phi-placement".equals(args.ssaStep)) {
        ssaMeth = Optimizer.debugPhiPlacement(rmeth, paramWidth, isStatic, true, advice);
    } else if ("renaming".equals(args.ssaStep)) {
        ssaMeth = Optimizer.debugRenaming(rmeth, paramWidth, isStatic, true, advice);
    } else if ("dead-code".equals(args.ssaStep)) {
        ssaMeth = Optimizer.debugDeadCodeRemover(rmeth, paramWidth, isStatic, true, advice);
    }
    StringBuffer sb = new StringBuffer(2000);
    sb.append("first ");
    sb.append(Hex.u2(ssaMeth.blockIndexToRopLabel(ssaMeth.getEntryBlockIndex())));
    sb.append('\n');
    ArrayList<SsaBasicBlock> blocks = ssaMeth.getBlocks();
    ArrayList<SsaBasicBlock> sortedBlocks = (ArrayList<SsaBasicBlock>) blocks.clone();
    Collections.sort(sortedBlocks, SsaBasicBlock.LABEL_COMPARATOR);
    for (SsaBasicBlock block : sortedBlocks) {
        sb.append("block ").append(Hex.u2(block.getRopLabel())).append('\n');
        BitSet preds = block.getPredecessors();
        for (int i = preds.nextSetBit(0); i >= 0; i = preds.nextSetBit(i + 1)) {
            sb.append("  pred ");
            sb.append(Hex.u2(ssaMeth.blockIndexToRopLabel(i)));
            sb.append('\n');
        }
        sb.append("  live in:" + block.getLiveInRegs());
        sb.append("\n");
        for (SsaInsn insn : block.getInsns()) {
            sb.append("  ");
            sb.append(insn.toHuman());
            sb.append('\n');
        }
        if (block.getSuccessors().cardinality() == 0) {
            sb.append("  returns\n");
        } else {
            int primary = block.getPrimarySuccessorRopLabel();
            IntList succLabelList = block.getRopLabelSuccessorList();
            int szSuccLabels = succLabelList.size();
            for (int i = 0; i < szSuccLabels; i++) {
                sb.append("  next ");
                sb.append(Hex.u2(succLabelList.get(i)));
                if (szSuccLabels != 1 && primary == succLabelList.get(i)) {
                    sb.append(" *");
                }
                sb.append('\n');
            }
        }
        sb.append("  live out:" + block.getLiveOutRegs());
        sb.append("\n");
    }
    suppressDump = false;
    setAt(bytes, 0);
    parsed(bytes, 0, bytes.size(), sb.toString());
    suppressDump = true;
}
Also used : RopMethod(com.android.dx.rop.code.RopMethod) Optimizer(com.android.dx.ssa.Optimizer) SsaBasicBlock(com.android.dx.ssa.SsaBasicBlock) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) DexTranslationAdvice(com.android.dx.rop.code.DexTranslationAdvice) TranslationAdvice(com.android.dx.rop.code.TranslationAdvice) ConcreteMethod(com.android.dx.cf.code.ConcreteMethod) RopMethod(com.android.dx.rop.code.RopMethod) SsaMethod(com.android.dx.ssa.SsaMethod) Method(com.android.dx.cf.iface.Method) IntList(com.android.dx.util.IntList) ConcreteMethod(com.android.dx.cf.code.ConcreteMethod) SsaMethod(com.android.dx.ssa.SsaMethod) SsaInsn(com.android.dx.ssa.SsaInsn)

Example 37 with IntList

use of com.android.dx.util.IntList in project buck by facebook.

the class Frame method makeNewSubroutineStartFrame.

/**
     * Makes a frame for a subroutine start block, given that this is the
     * ending frame of one of the subroutine's calling blocks. Subroutine
     * calls may be nested and thus may have nested locals state, so we
     * start with an initial state as seen by the subroutine, but keep track
     * of the individual locals states that will be expected when the individual
     * subroutine calls return.
     *
     * @param subLabel label of subroutine start block
     * @param callerLabel {@code >=0;} label of the caller block where this frame
     * came from.
     * @return a new instance to begin a called subroutine.
     */
public Frame makeNewSubroutineStartFrame(int subLabel, int callerLabel) {
    IntList newSubroutines = subroutines.mutableCopy();
    newSubroutines.add(subLabel);
    Frame newFrame = new Frame(locals.getPrimary(), stack, IntList.makeImmutable(subLabel));
    return newFrame.mergeWithSubroutineCaller(this, subLabel, callerLabel);
}
Also used : IntList(com.android.dx.util.IntList)

Example 38 with IntList

use of com.android.dx.util.IntList in project buck by facebook.

the class Frame method mergeWith.

/**
     * Merges two frames. If the merged result is the same as this frame,
     * then this instance is returned.
     *
     * @param other {@code non-null;} another frame
     * @return {@code non-null;} the result of merging the two frames
     */
public Frame mergeWith(Frame other) {
    LocalsArray resultLocals;
    ExecutionStack resultStack;
    IntList resultSubroutines;
    resultLocals = getLocals().merge(other.getLocals());
    resultStack = getStack().merge(other.getStack());
    resultSubroutines = mergeSubroutineLists(other.subroutines);
    resultLocals = adjustLocalsForSubroutines(resultLocals, resultSubroutines);
    if ((resultLocals == getLocals()) && (resultStack == getStack()) && subroutines == resultSubroutines) {
        return this;
    }
    return new Frame(resultLocals, resultStack, resultSubroutines);
}
Also used : IntList(com.android.dx.util.IntList)

Example 39 with IntList

use of com.android.dx.util.IntList in project buck by facebook.

the class Frame method mergeWithSubroutineCaller.

/**
     * Merges this frame with the frame of a subroutine caller at
     * {@code predLabel}. Only called on the frame at the first
     * block of a subroutine.
     *
     * @param other {@code non-null;} another frame
     * @param subLabel label of subroutine start block
     * @param predLabel label of calling block
     * @return {@code non-null;} the result of merging the two frames
     */
public Frame mergeWithSubroutineCaller(Frame other, int subLabel, int predLabel) {
    LocalsArray resultLocals;
    ExecutionStack resultStack;
    resultLocals = getLocals().mergeWithSubroutineCaller(other.getLocals(), predLabel);
    resultStack = getStack().merge(other.getStack());
    IntList newOtherSubroutines = other.subroutines.mutableCopy();
    newOtherSubroutines.add(subLabel);
    newOtherSubroutines.setImmutable();
    if ((resultLocals == getLocals()) && (resultStack == getStack()) && subroutines.equals(newOtherSubroutines)) {
        return this;
    }
    IntList resultSubroutines;
    if (subroutines.equals(newOtherSubroutines)) {
        resultSubroutines = subroutines;
    } else {
        /*
             * The new subroutines list should be the deepest of the two
             * lists being merged, but the postfix of the resultant list
             * must be equal to the shorter list.
             */
        IntList nonResultSubroutines;
        if (subroutines.size() > newOtherSubroutines.size()) {
            resultSubroutines = subroutines;
            nonResultSubroutines = newOtherSubroutines;
        } else {
            resultSubroutines = newOtherSubroutines;
            nonResultSubroutines = subroutines;
        }
        int szResult = resultSubroutines.size();
        int szNonResult = nonResultSubroutines.size();
        for (int i = szNonResult - 1; i >= 0; i--) {
            if (nonResultSubroutines.get(i) != resultSubroutines.get(i + (szResult - szNonResult))) {
                throw new RuntimeException("Incompatible merged subroutines");
            }
        }
    }
    return new Frame(resultLocals, resultStack, resultSubroutines);
}
Also used : IntList(com.android.dx.util.IntList)

Example 40 with IntList

use of com.android.dx.util.IntList in project buck by facebook.

the class RopTranslator method pickOrder.

/**
     * Picks an order for the blocks by doing "trace" analysis.
     */
private void pickOrder() {
    BasicBlockList blocks = method.getBlocks();
    int sz = blocks.size();
    int maxLabel = blocks.getMaxLabel();
    int[] workSet = Bits.makeBitSet(maxLabel);
    int[] tracebackSet = Bits.makeBitSet(maxLabel);
    for (int i = 0; i < sz; i++) {
        BasicBlock one = blocks.get(i);
        Bits.set(workSet, one.getLabel());
    }
    int[] order = new int[sz];
    int at = 0;
    /*
         * Starting with the designated "first label" (that is, the
         * first block of the method), add that label to the order,
         * and then pick its first as-yet unordered successor to
         * immediately follow it, giving top priority to the primary
         * (aka default) successor (if any). Keep following successors
         * until the trace runs out of possibilities. Then, continue
         * by finding an unordered chain containing the first as-yet
         * unordered block, and adding it to the order, and so on.
         */
    for (int label = method.getFirstLabel(); label != -1; label = Bits.findFirst(workSet, 0)) {
        /*
             * Attempt to trace backward from the chosen block to an
             * as-yet unordered predecessor which lists the chosen
             * block as its primary successor, and so on, until we
             * fail to find such an unordered predecessor. Start the
             * trace with that block. Note that the first block in the
             * method has no predecessors, so in that case this loop
             * will simply terminate with zero iterations and without
             * picking a new starter block.
             */
        traceBack: for (; ; ) {
            IntList preds = method.labelToPredecessors(label);
            int psz = preds.size();
            for (int i = 0; i < psz; i++) {
                int predLabel = preds.get(i);
                if (Bits.get(tracebackSet, predLabel)) {
                    /*
                         * We found a predecessor loop; stop tracing back
                         * from here.
                         */
                    break;
                }
                if (!Bits.get(workSet, predLabel)) {
                    // This one's already ordered.
                    continue;
                }
                BasicBlock pred = blocks.labelToBlock(predLabel);
                if (pred.getPrimarySuccessor() == label) {
                    // Found one!
                    label = predLabel;
                    Bits.set(tracebackSet, label);
                    continue traceBack;
                }
            }
            // Failed to find a better block to start the trace.
            break;
        }
        /*
             * Trace a path from the chosen block to one of its
             * unordered successors (hopefully the primary), and so
             * on, until we run out of unordered successors.
             */
        while (label != -1) {
            Bits.clear(workSet, label);
            Bits.clear(tracebackSet, label);
            order[at] = label;
            at++;
            BasicBlock one = blocks.labelToBlock(label);
            BasicBlock preferredBlock = blocks.preferredSuccessorOf(one);
            if (preferredBlock == null) {
                break;
            }
            int preferred = preferredBlock.getLabel();
            int primary = one.getPrimarySuccessor();
            if (Bits.get(workSet, preferred)) {
                /*
                     * Order the current block's preferred successor
                     * next, as it has yet to be scheduled.
                     */
                label = preferred;
            } else if ((primary != preferred) && (primary >= 0) && Bits.get(workSet, primary)) {
                /*
                     * The primary is available, so use that.
                     */
                label = primary;
            } else {
                /*
                     * There's no obvious candidate, so pick the first
                     * one that's available, if any.
                     */
                IntList successors = one.getSuccessors();
                int ssz = successors.size();
                label = -1;
                for (int i = 0; i < ssz; i++) {
                    int candidate = successors.get(i);
                    if (Bits.get(workSet, candidate)) {
                        label = candidate;
                        break;
                    }
                }
            }
        }
    }
    if (at != sz) {
        // There was a duplicate block label.
        throw new RuntimeException("shouldn't happen");
    }
    this.order = order;
}
Also used : BasicBlock(com.android.dx.rop.code.BasicBlock) BasicBlockList(com.android.dx.rop.code.BasicBlockList) IntList(com.android.dx.util.IntList)

Aggregations

IntList (com.android.dx.util.IntList)56 BasicBlock (com.android.dx.rop.code.BasicBlock)20 CstType (com.android.dx.rop.cst.CstType)7 Insn (com.android.dx.rop.code.Insn)5 RopMethod (com.android.dx.rop.code.RopMethod)5 BasicBlockList (com.android.dx.rop.code.BasicBlockList)4 PlainCstInsn (com.android.dx.rop.code.PlainCstInsn)4 PlainInsn (com.android.dx.rop.code.PlainInsn)4 RegisterSpec (com.android.dx.rop.code.RegisterSpec)4 SourcePosition (com.android.dx.rop.code.SourcePosition)4 ThrowingCstInsn (com.android.dx.rop.code.ThrowingCstInsn)4 ThrowingInsn (com.android.dx.rop.code.ThrowingInsn)4 Type (com.android.dx.rop.type.Type)4 TypeList (com.android.dx.rop.type.TypeList)4 DexTranslationAdvice (com.android.dx.rop.code.DexTranslationAdvice)3 InsnList (com.android.dx.rop.code.InsnList)3 TranslationAdvice (com.android.dx.rop.code.TranslationAdvice)3 SsaBasicBlock (com.android.dx.ssa.SsaBasicBlock)3 ArrayList (java.util.ArrayList)3 BitSet (java.util.BitSet)3