use of com.taobao.android.dx.util.IntList in project atlas by alibaba.
the class BlockDumper method regularDump.
/**
* Does a regular basic block dump.
*
* @param meth {@code non-null;} method data to dump
*/
private void regularDump(ConcreteMethod meth) {
BytecodeArray code = meth.getCode();
ByteArray bytes = code.getBytes();
ByteBlockList list = BasicBlocker.identifyBlocks(meth);
int sz = list.size();
CodeObserver codeObserver = new CodeObserver(bytes, BlockDumper.this);
// Reset the dump cursor to the start of the bytecode.
setAt(bytes, 0);
suppressDump = false;
int byteAt = 0;
for (int i = 0; i < sz; i++) {
ByteBlock bb = list.get(i);
int start = bb.getStart();
int end = bb.getEnd();
if (byteAt < start) {
parsed(bytes, byteAt, start - byteAt, "dead code " + Hex.u2(byteAt) + ".." + Hex.u2(start));
}
parsed(bytes, start, 0, "block " + Hex.u2(bb.getLabel()) + ": " + Hex.u2(start) + ".." + Hex.u2(end));
changeIndent(1);
int len;
for (int j = start; j < end; j += len) {
len = code.parseInstruction(j, codeObserver);
codeObserver.setPreviousOffset(j);
}
IntList successors = bb.getSuccessors();
int ssz = successors.size();
if (ssz == 0) {
parsed(bytes, end, 0, "returns");
} else {
for (int j = 0; j < ssz; j++) {
int succ = successors.get(j);
parsed(bytes, end, 0, "next " + Hex.u2(succ));
}
}
ByteCatchList catches = bb.getCatches();
int csz = catches.size();
for (int j = 0; j < csz; j++) {
ByteCatchList.Item one = catches.get(j);
CstType exceptionClass = one.getExceptionClass();
parsed(bytes, end, 0, "catch " + ((exceptionClass == CstType.OBJECT) ? "<any>" : exceptionClass.toHuman()) + " -> " + Hex.u2(one.getHandlerPc()));
}
changeIndent(-1);
byteAt = end;
}
int end = bytes.size();
if (byteAt < end) {
parsed(bytes, byteAt, end - byteAt, "dead code " + Hex.u2(byteAt) + ".." + Hex.u2(end));
}
suppressDump = true;
}
use of com.taobao.android.dx.util.IntList in project atlas by alibaba.
the class BlockDumper method ropDump.
/**
* Does a registerizing dump.
*
* @param meth {@code non-null;} method data to dump
*/
private void ropDump(ConcreteMethod meth) {
TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
BytecodeArray code = meth.getCode();
ByteArray bytes = code.getBytes();
RopMethod rmeth = Ropper.convert(meth, advice, classFile.getMethods());
StringBuffer sb = new StringBuffer(2000);
if (optimize) {
boolean isStatic = AccessFlags.isStatic(meth.getAccessFlags());
int paramWidth = computeParamWidth(meth, isStatic);
rmeth = Optimizer.optimize(rmeth, paramWidth, isStatic, true, advice);
}
BasicBlockList blocks = rmeth.getBlocks();
int[] order = blocks.getLabelsInOrder();
sb.append("first " + Hex.u2(rmeth.getFirstLabel()) + "\n");
for (int label : order) {
BasicBlock bb = blocks.get(blocks.indexOfLabel(label));
sb.append("block ");
sb.append(Hex.u2(label));
sb.append("\n");
IntList preds = rmeth.labelToPredecessors(label);
int psz = preds.size();
for (int i = 0; i < psz; i++) {
sb.append(" pred ");
sb.append(Hex.u2(preds.get(i)));
sb.append("\n");
}
InsnList il = bb.getInsns();
int ilsz = il.size();
for (int i = 0; i < ilsz; i++) {
Insn one = il.get(i);
sb.append(" ");
sb.append(il.get(i).toHuman());
sb.append("\n");
}
IntList successors = bb.getSuccessors();
int ssz = successors.size();
if (ssz == 0) {
sb.append(" returns\n");
} else {
int primary = bb.getPrimarySuccessor();
for (int i = 0; i < ssz; i++) {
int succ = successors.get(i);
sb.append(" next ");
sb.append(Hex.u2(succ));
if ((ssz != 1) && (succ == primary)) {
sb.append(" *");
}
sb.append("\n");
}
}
}
suppressDump = false;
setAt(bytes, 0);
parsed(bytes, 0, bytes.size(), sb.toString());
suppressDump = true;
}
use of com.taobao.android.dx.util.IntList in project atlas by alibaba.
the class DotDumper method endParsingMember.
public void endParsingMember(ByteArray bytes, int offset, String name, String descriptor, Member member) {
if (!(member instanceof Method)) {
return;
}
if (!shouldDumpMethod(name)) {
return;
}
ConcreteMethod meth = new ConcreteMethod((Method) member, classFile, true, true);
TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
RopMethod rmeth = Ropper.convert(meth, advice, classFile.getMethods());
if (optimize) {
boolean isStatic = AccessFlags.isStatic(meth.getAccessFlags());
rmeth = Optimizer.optimize(rmeth, BaseDumper.computeParamWidth(meth, isStatic), isStatic, true, advice);
}
System.out.println("digraph " + name + "{");
System.out.println("\tfirst -> n" + Hex.u2(rmeth.getFirstLabel()) + ";");
BasicBlockList blocks = rmeth.getBlocks();
int sz = blocks.size();
for (int i = 0; i < sz; i++) {
BasicBlock bb = blocks.get(i);
int label = bb.getLabel();
IntList successors = bb.getSuccessors();
if (successors.size() == 0) {
System.out.println("\tn" + Hex.u2(label) + " -> returns;");
} else if (successors.size() == 1) {
System.out.println("\tn" + Hex.u2(label) + " -> n" + Hex.u2(successors.get(0)) + ";");
} else {
System.out.print("\tn" + Hex.u2(label) + " -> {");
for (int j = 0; j < successors.size(); j++) {
int successor = successors.get(j);
if (successor != bb.getPrimarySuccessor()) {
System.out.print(" n" + Hex.u2(successor) + " ");
}
}
System.out.println("};");
System.out.println("\tn" + Hex.u2(label) + " -> n" + Hex.u2(bb.getPrimarySuccessor()) + " [label=\"primary\"];");
}
}
System.out.println("}");
}
use of com.taobao.android.dx.util.IntList in project atlas by alibaba.
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;
}
use of com.taobao.android.dx.util.IntList in project atlas by alibaba.
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;
}
Aggregations