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

The following examples show how to use com.android.dx.rop.code.RopMethod. 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: CodeStatistics.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the ROP statistics.
 *
 * @param nonOptRmeth non-optimized method
 * @param rmeth optimized method
 */
public void updateRopStatistics(RopMethod nonOptRmeth,
        RopMethod rmeth) {
    int oldCountInsns
            = nonOptRmeth.getBlocks().getEffectiveInstructionCount();
    int oldCountRegs = nonOptRmeth.getBlocks().getRegCount();

    if (DEBUG) {
        System.err.println("insns (old/new): "
                + oldCountInsns + "/"
                + rmeth.getBlocks().getEffectiveInstructionCount()
                + " regs (o/n):" + oldCountRegs
                + "/"  +  rmeth.getBlocks().getRegCount());
    }

    int newCountInsns
            = rmeth.getBlocks().getEffectiveInstructionCount();

    runningDeltaInsns
        += (newCountInsns - oldCountInsns);

    runningDeltaRegisters
        += (rmeth.getBlocks().getRegCount() - oldCountRegs);

    runningTotalInsns += newCountInsns;
}
 
Example #2
Source File: CodeStatistics.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the ROP statistics.
 *
 * @param nonOptRmeth non-optimized method
 * @param rmeth optimized method
 */
public void updateRopStatistics(RopMethod nonOptRmeth,
        RopMethod rmeth) {
    int oldCountInsns
            = nonOptRmeth.getBlocks().getEffectiveInstructionCount();
    int oldCountRegs = nonOptRmeth.getBlocks().getRegCount();

    if (DEBUG) {
        System.err.println("insns (old/new): "
                + oldCountInsns + "/"
                + rmeth.getBlocks().getEffectiveInstructionCount()
                + " regs (o/n):" + oldCountRegs
                + "/"  +  rmeth.getBlocks().getRegCount());
    }

    int newCountInsns
            = rmeth.getBlocks().getEffectiveInstructionCount();

    runningDeltaInsns
        += (newCountInsns - oldCountInsns);

    runningDeltaRegisters
        += (rmeth.getBlocks().getRegCount() - oldCountRegs);

    runningTotalInsns += newCountInsns;
}
 
Example #3
Source File: Ropper.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the resulting {@link RopMethod} from the instance.
 *
 * @return {@code non-null;} the method object
 */
private RopMethod getRopMethod() {

    // Construct the final list of blocks.

    int sz = result.size();
    BasicBlockList bbl = new BasicBlockList(sz);
    for (int i = 0; i < sz; i++) {
        bbl.set(i, result.get(i));
    }
    bbl.setImmutable();

    // Construct the method object to wrap it all up.

    /*
     * Note: The parameter assignment block is always the first
     * that should be executed, hence the second argument to the
     * constructor.
     */
    return new RopMethod(bbl, getSpecialLabel(PARAM_ASSIGNMENT));
}
 
Example #4
Source File: SsaConverter.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an SSA representation, edge-split and with phi
 * functions placed.
 *
 * @param rmeth input
 * @param paramWidth the total width, in register-units, of the method's
 * parameters
 * @param isStatic {@code true} if this method has no {@code this}
 * pointer argument
 * @return output in SSA form
 */
public static SsaMethod convertToSsaMethod(RopMethod rmeth,
        int paramWidth, boolean isStatic) {
    SsaMethod result
        = SsaMethod.newFromRopMethod(rmeth, paramWidth, isStatic);

    edgeSplit(result);

    LocalVariableInfo localInfo = LocalVariableExtractor.extract(result);

    placePhiFunctions(result, localInfo, 0);
    new SsaRenamer(result).run();

    /*
     * The exit block, added here, is not considered for edge splitting
     * or phi placement since no actual control flows to it.
     */
    result.makeExitBlock();

    return result;
}
 
Example #5
Source File: Optimizer.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Runs optimization algorthims over this method, and returns a new
 * instance of RopMethod with the changes.
 *
 * @param rmeth method to process
 * @param paramWidth the total width, in register-units, of this method's
 * parameters
 * @param isStatic true if this method has no 'this' pointer argument.
 * @param inPreserveLocals true if local variable info should be preserved,
 * at the cost of some registers and insns
 * @param inAdvice {@code non-null;} translation advice
 * @param steps set of optional optimization steps to run
 * @return optimized method
 */
public static RopMethod optimize(RopMethod rmeth, int paramWidth,
        boolean isStatic, boolean inPreserveLocals,
        TranslationAdvice inAdvice, EnumSet<OptionalStep> steps) {
    SsaMethod ssaMeth = null;

    preserveLocals = inPreserveLocals;
    advice = inAdvice;

    ssaMeth = SsaConverter.convertToSsaMethod(rmeth, paramWidth, isStatic);
    runSsaFormSteps(ssaMeth, steps);

    RopMethod resultMeth = SsaToRop.convertToRopMethod(ssaMeth, false);

    if (resultMeth.getBlocks().getRegCount()
            > advice.getMaxOptimalRegisterCount()) {
        // Try to see if we can squeeze it under the register count bar
        resultMeth = optimizeMinimizeRegisters(rmeth, paramWidth, isStatic,
                steps);
    }
    return resultMeth;
}
 
Example #6
Source File: StdCatchBuilder.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance. It merely holds onto its parameters for
 * a subsequent call to {@link #build}.
 *
 * @param method {@code non-null;} method to build the list for
 * @param order {@code non-null;} block output order
 * @param addresses {@code non-null;} address objects for each block
 */
public StdCatchBuilder(RopMethod method, int[] order,
        BlockAddresses addresses) {
    if (method == null) {
        throw new NullPointerException("method == null");
    }

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

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

    this.method = method;
    this.order = order;
    this.addresses = addresses;
}
 
Example #7
Source File: SsaConverter.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an SSA representation, edge-split and with phi
 * functions placed.
 *
 * @param rmeth input
 * @param paramWidth the total width, in register-units, of the method's
 * parameters
 * @param isStatic {@code true} if this method has no {@code this}
 * pointer argument
 * @return output in SSA form
 */
public static SsaMethod convertToSsaMethod(RopMethod rmeth,
        int paramWidth, boolean isStatic) {
    SsaMethod result
        = SsaMethod.newFromRopMethod(rmeth, paramWidth, isStatic);

    edgeSplit(result);

    LocalVariableInfo localInfo = LocalVariableExtractor.extract(result);

    placePhiFunctions(result, localInfo, 0);
    new SsaRenamer(result).run();

    /*
     * The exit block, added here, is not considered for edge splitting
     * or phi placement since no actual control flows to it.
     */
    result.makeExitBlock();

    return result;
}
 
Example #8
Source File: StdCatchBuilder.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance. It merely holds onto its parameters for
 * a subsequent call to {@link #build}.
 *
 * @param method {@code non-null;} method to build the list for
 * @param order {@code non-null;} block output order
 * @param addresses {@code non-null;} address objects for each block
 */
public StdCatchBuilder(RopMethod method, int[] order,
        BlockAddresses addresses) {
    if (method == null) {
        throw new NullPointerException("method == null");
    }

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

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

    this.method = method;
    this.order = order;
    this.addresses = addresses;
}
 
Example #9
Source File: BlockAddresses.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the address arrays.
 */
private void setupArrays(RopMethod method) {
    BasicBlockList blocks = method.getBlocks();
    int sz = blocks.size();

    for (int i = 0; i < sz; i++) {
        BasicBlock one = blocks.get(i);
        int label = one.getLabel();
        Insn insn = one.getInsns().get(0);

        starts[label] = new CodeAddress(insn.getPosition());

        SourcePosition pos = one.getLastInsn().getPosition();

        lasts[label] = new CodeAddress(pos);
        ends[label] = new CodeAddress(pos);
    }
}
 
Example #10
Source File: Optimizer.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Runs optimization algorthims over this method, and returns a new
 * instance of RopMethod with the changes.
 *
 * @param rmeth method to process
 * @param paramWidth the total width, in register-units, of this method's
 * parameters
 * @param isStatic true if this method has no 'this' pointer argument.
 * @param inPreserveLocals true if local variable info should be preserved,
 * at the cost of some registers and insns
 * @param inAdvice {@code non-null;} translation advice
 * @param steps set of optional optimization steps to run
 * @return optimized method
 */
public static RopMethod optimize(RopMethod rmeth, int paramWidth,
        boolean isStatic, boolean inPreserveLocals,
        TranslationAdvice inAdvice, EnumSet<OptionalStep> steps) {
    SsaMethod ssaMeth = null;

    preserveLocals = inPreserveLocals;
    advice = inAdvice;

    ssaMeth = SsaConverter.convertToSsaMethod(rmeth, paramWidth, isStatic);
    runSsaFormSteps(ssaMeth, steps);

    RopMethod resultMeth = SsaToRop.convertToRopMethod(ssaMeth, false);

    if (resultMeth.getBlocks().getRegCount()
            > advice.getMaxOptimalRegisterCount()) {
        // Try to see if we can squeeze it under the register count bar
        resultMeth = optimizeMinimizeRegisters(rmeth, paramWidth, isStatic,
                steps);
    }
    return resultMeth;
}
 
Example #11
Source File: Optimizer.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the optimizer with a strategy to minimize the number of rop-form
 * registers used by the end result. Dex bytecode does not have instruction
 * forms that take register numbers larger than 15 for all instructions.
 * If we've produced a method that uses more than 16 registers, try again
 * with a different strategy to see if we can get under the bar. The end
 * result will be much more efficient.
 *
 * @param rmeth method to process
 * @param paramWidth the total width, in register-units, of this method's
 * parameters
 * @param isStatic true if this method has no 'this' pointer argument.
 * @param steps set of optional optimization steps to run
 * @return optimized method
 */
private static RopMethod optimizeMinimizeRegisters(RopMethod rmeth,
        int paramWidth, boolean isStatic,
        EnumSet<OptionalStep> steps) {
    SsaMethod ssaMeth;
    RopMethod resultMeth;

    ssaMeth = SsaConverter.convertToSsaMethod(
            rmeth, paramWidth, isStatic);

    EnumSet<OptionalStep> newSteps = steps.clone();

    /*
     * CONST_COLLECTOR trades insns for registers, which is not an
     * appropriate strategy here.
     */
    newSteps.remove(OptionalStep.CONST_COLLECTOR);

    runSsaFormSteps(ssaMeth, newSteps);

    resultMeth = SsaToRop.convertToRopMethod(ssaMeth, true);
    return resultMeth;
}
 
Example #12
Source File: Optimizer.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the optimizer with a strategy to minimize the number of rop-form
 * registers used by the end result. Dex bytecode does not have instruction
 * forms that take register numbers larger than 15 for all instructions.
 * If we've produced a method that uses more than 16 registers, try again
 * with a different strategy to see if we can get under the bar. The end
 * result will be much more efficient.
 *
 * @param rmeth method to process
 * @param paramWidth the total width, in register-units, of this method's
 * parameters
 * @param isStatic true if this method has no 'this' pointer argument.
 * @param steps set of optional optimization steps to run
 * @return optimized method
 */
private static RopMethod optimizeMinimizeRegisters(RopMethod rmeth,
        int paramWidth, boolean isStatic,
        EnumSet<OptionalStep> steps) {
    SsaMethod ssaMeth;
    RopMethod resultMeth;

    ssaMeth = SsaConverter.convertToSsaMethod(
            rmeth, paramWidth, isStatic);

    EnumSet<OptionalStep> newSteps = steps.clone();

    /*
     * CONST_COLLECTOR trades insns for registers, which is not an
     * appropriate strategy here.
     */
    newSteps.remove(OptionalStep.CONST_COLLECTOR);

    runSsaFormSteps(ssaMeth, newSteps);

    resultMeth = SsaToRop.convertToRopMethod(ssaMeth, true);
    return resultMeth;
}
 
Example #13
Source File: Optimizer.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Runs optimization algorthims over this method, and returns a new
 * instance of RopMethod with the changes.
 *
 * @param rmeth method to process
 * @param paramWidth the total width, in register-units, of this method's
 * parameters
 * @param isStatic true if this method has no 'this' pointer argument.
 * @param inPreserveLocals true if local variable info should be preserved,
 * at the cost of some registers and insns
 * @param inAdvice {@code non-null;} translation advice
 * @param steps set of optional optimization steps to run
 * @return optimized method
 */
public static RopMethod optimize(RopMethod rmeth, int paramWidth,
        boolean isStatic, boolean inPreserveLocals,
        TranslationAdvice inAdvice, EnumSet<OptionalStep> steps) {
    SsaMethod ssaMeth = null;

    preserveLocals = inPreserveLocals;
    advice = inAdvice;

    ssaMeth = SsaConverter.convertToSsaMethod(rmeth, paramWidth, isStatic);
    runSsaFormSteps(ssaMeth, steps);

    RopMethod resultMeth = SsaToRop.convertToRopMethod(ssaMeth, false);

    if (resultMeth.getBlocks().getRegCount()
            > advice.getMaxOptimalRegisterCount()) {
        // Try to see if we can squeeze it under the register count bar
        resultMeth = optimizeMinimizeRegisters(rmeth, paramWidth, isStatic,
                steps);
    }
    return resultMeth;
}
 
Example #14
Source File: SsaMethod.java    From Box with Apache License 2.0 6 votes vote down vote up
private void convertRopToSsaBlocks(RopMethod rmeth) {
    BasicBlockList ropBlocks = rmeth.getBlocks();
    int sz = ropBlocks.size();

    blocks = new ArrayList<SsaBasicBlock>(sz + 2);

    for (int i = 0; i < sz; i++) {
        SsaBasicBlock sbb = SsaBasicBlock.newFromRop(rmeth, i, this);
        blocks.add(sbb);
    }

    // Add an no-op entry block.
    int origEntryBlockIndex = rmeth.getBlocks()
            .indexOfLabel(rmeth.getFirstLabel());

    SsaBasicBlock entryBlock
            = blocks.get(origEntryBlockIndex).insertNewPredecessor();

    entryBlockIndex = entryBlock.getIndex();
    exitBlockIndex = -1; // This gets made later.
}
 
Example #15
Source File: Optimizer.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the optimizer with a strategy to minimize the number of rop-form
 * registers used by the end result. Dex bytecode does not have instruction
 * forms that take register numbers larger than 15 for all instructions.
 * If we've produced a method that uses more than 16 registers, try again
 * with a different strategy to see if we can get under the bar. The end
 * result will be much more efficient.
 *
 * @param rmeth method to process
 * @param paramWidth the total width, in register-units, of this method's
 * parameters
 * @param isStatic true if this method has no 'this' pointer argument.
 * @param steps set of optional optimization steps to run
 * @return optimized method
 */
private static RopMethod optimizeMinimizeRegisters(RopMethod rmeth,
        int paramWidth, boolean isStatic,
        EnumSet<OptionalStep> steps) {
    SsaMethod ssaMeth;
    RopMethod resultMeth;

    ssaMeth = SsaConverter.convertToSsaMethod(
            rmeth, paramWidth, isStatic);

    EnumSet<OptionalStep> newSteps = steps.clone();

    /*
     * CONST_COLLECTOR trades insns for registers, which is not an
     * appropriate strategy here.
     */
    newSteps.remove(OptionalStep.CONST_COLLECTOR);

    runSsaFormSteps(ssaMeth, newSteps);

    resultMeth = SsaToRop.convertToRopMethod(ssaMeth, true);
    return resultMeth;
}
 
Example #16
Source File: BlockAddresses.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the address arrays.
 */
private void setupArrays(RopMethod method) {
    BasicBlockList blocks = method.getBlocks();
    int sz = blocks.size();

    for (int i = 0; i < sz; i++) {
        BasicBlock one = blocks.get(i);
        int label = one.getLabel();
        Insn insn = one.getInsns().get(0);

        starts[label] = new CodeAddress(insn.getPosition());

        SourcePosition pos = one.getLastInsn().getPosition();

        lasts[label] = new CodeAddress(pos);
        ends[label] = new CodeAddress(pos);
    }
}
 
Example #17
Source File: Optimizer.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the optimizer with a strategy to minimize the number of rop-form
 * registers used by the end result. Dex bytecode does not have instruction
 * forms that take register numbers larger than 15 for all instructions.
 * If we've produced a method that uses more than 16 registers, try again
 * with a different strategy to see if we can get under the bar. The end
 * result will be much more efficient.
 *
 * @param rmeth method to process
 * @param paramWidth the total width, in register-units, of this method's
 * parameters
 * @param isStatic true if this method has no 'this' pointer argument.
 * @param steps set of optional optimization steps to run
 * @return optimized method
 */
private static RopMethod optimizeMinimizeRegisters(RopMethod rmeth,
        int paramWidth, boolean isStatic,
        EnumSet<OptionalStep> steps) {
    SsaMethod ssaMeth;
    RopMethod resultMeth;

    ssaMeth = SsaConverter.convertToSsaMethod(
            rmeth, paramWidth, isStatic);

    EnumSet<OptionalStep> newSteps = steps.clone();

    /*
     * CONST_COLLECTOR trades insns for registers, which is not an
     * appropriate strategy here.
     */
    newSteps.remove(OptionalStep.CONST_COLLECTOR);

    runSsaFormSteps(ssaMeth, newSteps);

    resultMeth = SsaToRop.convertToRopMethod(ssaMeth, true);
    return resultMeth;
}
 
Example #18
Source File: SsaMethod.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * @param ropMethod rop-form method to convert from
 * @param paramWidth the total width, in register-units, of the
 * method's parameters
 * @param isStatic {@code true} if this method has no {@code this}
 * pointer argument
 */
public static SsaMethod newFromRopMethod(RopMethod ropMethod,
        int paramWidth, boolean isStatic) {
    SsaMethod result = new SsaMethod(ropMethod, paramWidth, isStatic);

    result.convertRopToSsaBlocks(ropMethod);

    return result;
}
 
Example #19
Source File: Optimizer.java    From Box with Apache License 2.0 5 votes vote down vote up
public static SsaMethod debugRenaming(RopMethod rmeth, int paramWidth,
        boolean isStatic, boolean inPreserveLocals,
        TranslationAdvice inAdvice) {

    preserveLocals = inPreserveLocals;
    advice = inAdvice;

    return SsaConverter.convertToSsaMethod(rmeth, paramWidth, isStatic);
}
 
Example #20
Source File: Ropper.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link ConcreteMethod} to a {@link RopMethod}.
 *
 * @param method {@code non-null;} method to convert
 * @param advice {@code non-null;} translation advice to use
 * @param methods {@code non-null;} list of methods defined by the class
 *     that defines {@code method}.
 * @return {@code non-null;} the converted instance
 */
public static RopMethod convert(ConcreteMethod method,
        TranslationAdvice advice, MethodList methods, DexOptions dexOptions) {
    try {
        Ropper r = new Ropper(method, advice, methods, dexOptions);
        r.doit();
        return r.getRopMethod();
    } catch (SimException ex) {
        ex.addContext("...while working on method " +
                      method.getNat().toHuman());
        throw ex;
    }
}
 
Example #21
Source File: SsaBasicBlock.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new SSA basic block from a ROP form basic block.
 *
 * @param rmeth original method
 * @param basicBlockIndex index this block will have
 * @param parent method of this block predecessor set will be
 * updated
 * @return new instance
 */
public static SsaBasicBlock newFromRop(RopMethod rmeth,
        int basicBlockIndex, final SsaMethod parent) {
    BasicBlockList ropBlocks = rmeth.getBlocks();
    BasicBlock bb = ropBlocks.get(basicBlockIndex);
    SsaBasicBlock result =
        new SsaBasicBlock(basicBlockIndex, bb.getLabel(), parent);
    InsnList ropInsns = bb.getInsns();

    result.insns.ensureCapacity(ropInsns.size());

    for (int i = 0, sz = ropInsns.size() ; i < sz ; i++) {
        result.insns.add(new NormalSsaInsn (ropInsns.get(i), result));
    }

    result.predecessors = SsaMethod.bitSetFromLabelList(
            ropBlocks,
            rmeth.labelToPredecessors(bb.getLabel()));

    result.successors
            = SsaMethod.bitSetFromLabelList(ropBlocks, bb.getSuccessors());

    result.successorList
            = SsaMethod.indexListFromLabelList(ropBlocks,
                bb.getSuccessors());

    if (result.successorList.size() != 0) {
        int primarySuccessor = bb.getPrimarySuccessor();

        result.primarySuccessor = (primarySuccessor < 0)
                ? -1 : ropBlocks.indexOfLabel(primarySuccessor);
    }

    return result;
}
 
Example #22
Source File: SsaConverter.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an SSA represention with only the edge-splitter run.
 *
 * @param rmeth method to process
 * @param paramWidth width of all arguments in the method
 * @param isStatic {@code true} if this method has no {@code this}
 * pointer argument
 * @return an SSA represention with only the edge-splitter run
 */
public static SsaMethod testEdgeSplit (RopMethod rmeth, int paramWidth,
        boolean isStatic) {
    SsaMethod result;

    result = SsaMethod.newFromRopMethod(rmeth, paramWidth, isStatic);

    edgeSplit(result);
    return result;
}
 
Example #23
Source File: CfTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Helper that updates the dex statistics.
 */
private static void updateDexStatistics(DxContext context, CfOptions cfOptions, DexOptions dexOptions,
        RopMethod optRmeth, RopMethod nonOptRmeth,
        LocalVariableInfo locals, int paramSize, int originalByteCount) {
    /*
     * Run rop->dex again on optimized vs. non-optimized method to
     * collect statistics. We have to totally convert both ways,
     * since converting the "real" method getting added to the
     * file would corrupt it (by messing with its constant pool
     * indices).
     */

    DalvCode optCode = RopTranslator.translate(optRmeth,
            cfOptions.positionInfo, locals, paramSize, dexOptions);
    DalvCode nonOptCode = RopTranslator.translate(nonOptRmeth,
            cfOptions.positionInfo, locals, paramSize, dexOptions);

    /*
     * Fake out the indices, so code.getInsns() can work well enough
     * for the current purpose.
     */

    DalvCode.AssignIndicesCallback callback =
        new DalvCode.AssignIndicesCallback() {
            @Override
public int getIndex(Constant cst) {
                // Everything is at index 0!
                return 0;
            }
        };

    optCode.assignIndices(callback);
    nonOptCode.assignIndices(callback);

    context.codeStatistics.updateDexStatistics(nonOptCode, optCode);
    context.codeStatistics.updateOriginalByteCount(originalByteCount);
}
 
Example #24
Source File: OptimizerOptions.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Compares the output of the optimizer run normally with a run skipping
 * some optional steps. Results are printed to stderr.
 *
 * @param nonOptRmeth {@code non-null;} origional rop method
 * @param paramSize {@code >= 0;} parameter size of method
 * @param isStatic true if this method has no 'this' pointer argument.
 * @param args {@code non-null;} translator arguments
 * @param advice {@code non-null;} translation advice
 * @param rmeth {@code non-null;} method with all optimization steps run.
 */
public void compareOptimizerStep(RopMethod nonOptRmeth,
        int paramSize, boolean isStatic, CfOptions args,
        TranslationAdvice advice, RopMethod rmeth) {
    EnumSet<Optimizer.OptionalStep> steps;

    steps = EnumSet.allOf(Optimizer.OptionalStep.class);

    // This is the step to skip.
    steps.remove(Optimizer.OptionalStep.CONST_COLLECTOR);

    RopMethod skipRopMethod
            = Optimizer.optimize(nonOptRmeth,
                    paramSize, isStatic, args.localInfo, advice, steps);

    int normalInsns
            = rmeth.getBlocks().getEffectiveInstructionCount();
    int skipInsns
            = skipRopMethod.getBlocks().getEffectiveInstructionCount();

    System.err.printf(
            "optimize step regs:(%d/%d/%.2f%%)"
            + " insns:(%d/%d/%.2f%%)\n",
            rmeth.getBlocks().getRegCount(),
            skipRopMethod.getBlocks().getRegCount(),
            100.0 * ((skipRopMethod.getBlocks().getRegCount()
                    - rmeth.getBlocks().getRegCount())
                    / (float) skipRopMethod.getBlocks().getRegCount()),
            normalInsns, skipInsns,
            100.0 * ((skipInsns - normalInsns) / (float) skipInsns));
}
 
Example #25
Source File: SsaMethod.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * @param ropMethod rop-form method to convert from
 * @param paramWidth the total width, in register-units, of the
 * method's parameters
 * @param isStatic {@code true} if this method has no {@code this}
 * pointer argument
 */
public static SsaMethod newFromRopMethod(RopMethod ropMethod,
        int paramWidth, boolean isStatic) {
    SsaMethod result = new SsaMethod(ropMethod, paramWidth, isStatic);

    result.convertRopToSsaBlocks(ropMethod);

    return result;
}
 
Example #26
Source File: BlockAddresses.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} the method to have block addresses for
 */
public BlockAddresses(RopMethod method) {
    BasicBlockList blocks = method.getBlocks();
    int maxLabel = blocks.getMaxLabel();

    this.starts = new CodeAddress[maxLabel];
    this.lasts = new CodeAddress[maxLabel];
    this.ends = new CodeAddress[maxLabel];

    setupArrays(method);
}
 
Example #27
Source File: SsaConverter.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an SSA represention with only the steps through the
 * phi placement run.
 *
 * @param rmeth method to process
 * @param paramWidth width of all arguments in the method
 * @param isStatic {@code true} if this method has no {@code this}
 * pointer argument
 * @return an SSA represention with only the edge-splitter run
 */
public static SsaMethod testPhiPlacement (RopMethod rmeth, int paramWidth,
        boolean isStatic) {
    SsaMethod result;

    result = SsaMethod.newFromRopMethod(rmeth, paramWidth, isStatic);

    edgeSplit(result);

    LocalVariableInfo localInfo = LocalVariableExtractor.extract(result);

    placePhiFunctions(result, localInfo, 0);
    return result;
}
 
Example #28
Source File: CfTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Helper that updates the dex statistics.
 */
private static void updateDexStatistics(DxContext context, CfOptions cfOptions, DexOptions dexOptions,
        RopMethod optRmeth, RopMethod nonOptRmeth,
        LocalVariableInfo locals, int paramSize, int originalByteCount) {
    /*
     * Run rop->dex again on optimized vs. non-optimized method to
     * collect statistics. We have to totally convert both ways,
     * since converting the "real" method getting added to the
     * file would corrupt it (by messing with its constant pool
     * indices).
     */

    DalvCode optCode = RopTranslator.translate(optRmeth,
            cfOptions.positionInfo, locals, paramSize, dexOptions);
    DalvCode nonOptCode = RopTranslator.translate(nonOptRmeth,
            cfOptions.positionInfo, locals, paramSize, dexOptions);

    /*
     * Fake out the indices, so code.getInsns() can work well enough
     * for the current purpose.
     */

    DalvCode.AssignIndicesCallback callback =
        new DalvCode.AssignIndicesCallback() {
            @Override
            public int getIndex(Constant cst) {
                // Everything is at index 0!
                return 0;
            }
        };

    optCode.assignIndices(callback);
    nonOptCode.assignIndices(callback);

    context.codeStatistics.updateDexStatistics(nonOptCode, optCode);
    context.codeStatistics.updateOriginalByteCount(originalByteCount);
}
 
Example #29
Source File: OptimizerOptions.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Compares the output of the optimizer run normally with a run skipping
 * some optional steps. Results are printed to stderr.
 *
 * @param nonOptRmeth {@code non-null;} origional rop method
 * @param paramSize {@code >= 0;} parameter size of method
 * @param isStatic true if this method has no 'this' pointer argument.
 * @param args {@code non-null;} translator arguments
 * @param advice {@code non-null;} translation advice
 * @param rmeth {@code non-null;} method with all optimization steps run.
 */
public void compareOptimizerStep(RopMethod nonOptRmeth,
        int paramSize, boolean isStatic, CfOptions args,
        TranslationAdvice advice, RopMethod rmeth) {
    EnumSet<Optimizer.OptionalStep> steps;

    steps = EnumSet.allOf(Optimizer.OptionalStep.class);

    // This is the step to skip.
    steps.remove(Optimizer.OptionalStep.CONST_COLLECTOR);

    RopMethod skipRopMethod
            = Optimizer.optimize(nonOptRmeth,
                    paramSize, isStatic, args.localInfo, advice, steps);

    int normalInsns
            = rmeth.getBlocks().getEffectiveInstructionCount();
    int skipInsns
            = skipRopMethod.getBlocks().getEffectiveInstructionCount();

    System.err.printf(
            "optimize step regs:(%d/%d/%.2f%%)"
            + " insns:(%d/%d/%.2f%%)\n",
            rmeth.getBlocks().getRegCount(),
            skipRopMethod.getBlocks().getRegCount(),
            100.0 * ((skipRopMethod.getBlocks().getRegCount()
                    - rmeth.getBlocks().getRegCount())
                    / (float) skipRopMethod.getBlocks().getRegCount()),
            normalInsns, skipInsns,
            100.0 * ((skipInsns - normalInsns) / (float) skipInsns));
}
 
Example #30
Source File: SsaToRop.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the conversion.
 *
 * @return {@code non-null;} rop-form output
 */
private RopMethod convert() {
    if (DEBUG) {
        interference.dumpToStdout();
    }

    // These are other allocators for debugging or historical comparison:
    // allocator = new NullRegisterAllocator(ssaMeth, interference);
    // allocator = new FirstFitAllocator(ssaMeth, interference);

    RegisterAllocator allocator =
        new FirstFitLocalCombiningAllocator(ssaMeth, interference,
                minimizeRegisters);

    RegisterMapper mapper = allocator.allocateRegisters();

    if (DEBUG) {
        System.out.println("Printing reg map");
        System.out.println(((BasicRegisterMapper)mapper).toHuman());
    }

    ssaMeth.setBackMode();

    ssaMeth.mapRegisters(mapper);

    removePhiFunctions();

    if (allocator.wantsParamsMovedHigh()) {
        moveParametersToHighRegisters();
    }

    removeEmptyGotos();

    RopMethod ropMethod = new RopMethod(convertBasicBlocks(),
            ssaMeth.blockIndexToRopLabel(ssaMeth.getEntryBlockIndex()));
    ropMethod = new IdenticalBlockCombiner(ropMethod).process();

    return ropMethod;
}