Search in sources :

Example 16 with CstString

use of com.taobao.android.dx.rop.cst.CstString in project atlas by alibaba.

the class DebugInfoEncoder method entryAnnotationString.

/**
     * Returns a string representation of this LocalList entry that is
     * appropriate for emitting as an annotation.
     *
     * @param e {@code non-null;} entry
     * @return {@code non-null;} annotation string
     */
private String entryAnnotationString(LocalList.Entry e) {
    StringBuilder sb = new StringBuilder();
    sb.append(RegisterSpec.PREFIX);
    sb.append(e.getRegister());
    sb.append(' ');
    CstString name = e.getName();
    if (name == null) {
        sb.append("null");
    } else {
        sb.append(name.toHuman());
    }
    sb.append(' ');
    CstType type = e.getType();
    if (type == null) {
        sb.append("null");
    } else {
        sb.append(type.toHuman());
    }
    CstString signature = e.getSignature();
    if (signature != null) {
        sb.append(' ');
        sb.append(signature.toHuman());
    }
    return sb.toString();
}
Also used : CstType(com.taobao.android.dx.rop.cst.CstType) CstString(com.taobao.android.dx.rop.cst.CstString)

Example 17 with CstString

use of com.taobao.android.dx.rop.cst.CstString in project atlas by alibaba.

the class DebugInfoEncoder method emitHeader.

/**
     * Emits the header sequence, which consists of LEB128-encoded initial
     * line number and string indicies for names of all non-"this" arguments.
     *
     * @param sortedPositions positions, sorted by ascending address
     * @param methodArgs local list entries for method argumens arguments,
     * in left-to-right order omitting "this"
     * @throws IOException
     */
private void emitHeader(ArrayList<PositionList.Entry> sortedPositions, ArrayList<LocalList.Entry> methodArgs) throws IOException {
    boolean annotate = (annotateTo != null) || (debugPrint != null);
    int mark = output.getCursor();
    // Start by initializing the line number register.
    if (sortedPositions.size() > 0) {
        PositionList.Entry entry = sortedPositions.get(0);
        line = entry.getPosition().getLine();
    }
    output.writeUleb128(line);
    if (annotate) {
        annotate(output.getCursor() - mark, "line_start: " + line);
    }
    int curParam = getParamBase();
    // paramTypes will not include 'this'
    StdTypeList paramTypes = desc.getParameterTypes();
    int szParamTypes = paramTypes.size();
    /*
         * Initialize lastEntryForReg to have an initial
         * entry for the 'this' pointer.
         */
    if (!isStatic) {
        for (LocalList.Entry arg : methodArgs) {
            if (curParam == arg.getRegister()) {
                lastEntryForReg[curParam] = arg;
                break;
            }
        }
        curParam++;
    }
    // Write out the number of parameter entries that will follow.
    mark = output.getCursor();
    output.writeUleb128(szParamTypes);
    if (annotate) {
        annotate(output.getCursor() - mark, String.format("parameters_size: %04x", szParamTypes));
    }
    /*
         * Then emit the string indicies of all the method parameters.
         * Note that 'this', if applicable, is excluded.
         */
    for (int i = 0; i < szParamTypes; i++) {
        Type pt = paramTypes.get(i);
        LocalList.Entry found = null;
        mark = output.getCursor();
        for (LocalList.Entry arg : methodArgs) {
            if (curParam == arg.getRegister()) {
                found = arg;
                if (arg.getSignature() != null) {
                    /*
                         * Parameters with signatures will be re-emitted
                         * in complete as LOCAL_START_EXTENDED's below.
                         */
                    emitStringIndex(null);
                } else {
                    emitStringIndex(arg.getName());
                }
                lastEntryForReg[curParam] = arg;
                break;
            }
        }
        if (found == null) {
            /*
                 * Emit a null symbol for "unnamed." This is common
                 * for, e.g., synthesized methods and inner-class
                 * this$0 arguments.
                 */
            emitStringIndex(null);
        }
        if (annotate) {
            String parameterName = (found == null || found.getSignature() != null) ? "<unnamed>" : found.getName().toHuman();
            annotate(output.getCursor() - mark, "parameter " + parameterName + " " + RegisterSpec.PREFIX + curParam);
        }
        curParam += pt.getCategory();
    }
    for (LocalList.Entry arg : lastEntryForReg) {
        if (arg == null) {
            continue;
        }
        CstString signature = arg.getSignature();
        if (signature != null) {
            emitLocalStartExtended(arg);
        }
    }
}
Also used : LocalList(com.taobao.android.dx.dex.code.LocalList) CstType(com.taobao.android.dx.rop.cst.CstType) Type(com.taobao.android.dx.rop.type.Type) StdTypeList(com.taobao.android.dx.rop.type.StdTypeList) PositionList(com.taobao.android.dx.dex.code.PositionList) CstString(com.taobao.android.dx.rop.cst.CstString) CstString(com.taobao.android.dx.rop.cst.CstString)

Example 18 with CstString

use of com.taobao.android.dx.rop.cst.CstString in project atlas by alibaba.

the class ConstCollector method getConstsSortedByCountUse.

/**
     * Gets all of the collectable constant values used in this method,
     * sorted by most used first. Skips non-collectable consts, such as
     * non-string object constants
     *
     * @return {@code non-null;} list of constants in most-to-least used order
     */
private ArrayList<TypedConstant> getConstsSortedByCountUse() {
    int regSz = ssaMeth.getRegCount();
    final HashMap<TypedConstant, Integer> countUses = new HashMap<TypedConstant, Integer>();
    /*
         * Each collected constant can be used by just one local
         * (used only if COLLECT_ONE_LOCAL is true).
         */
    final HashSet<TypedConstant> usedByLocal = new HashSet<TypedConstant>();
    // Count how many times each const value is used.
    for (int i = 0; i < regSz; i++) {
        SsaInsn insn = ssaMeth.getDefinitionForRegister(i);
        if (insn == null || insn.getOpcode() == null)
            continue;
        RegisterSpec result = insn.getResult();
        TypeBearer typeBearer = result.getTypeBearer();
        if (!typeBearer.isConstant())
            continue;
        TypedConstant cst = (TypedConstant) typeBearer;
        // Find defining instruction for move-result-pseudo instructions
        if (insn.getOpcode().getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
            int pred = insn.getBlock().getPredecessors().nextSetBit(0);
            ArrayList<SsaInsn> predInsns;
            predInsns = ssaMeth.getBlocks().get(pred).getInsns();
            insn = predInsns.get(predInsns.size() - 1);
        }
        if (insn.canThrow()) {
            /*
                 * Don't move anything other than strings -- the risk
                 * of changing where an exception is thrown is too high.
                 */
            if (!(cst instanceof CstString) || !COLLECT_STRINGS) {
                continue;
            }
            /*
                 * We can't move any throwable const whose throw will be
                 * caught, so don't count them.
                 */
            if (insn.getBlock().getSuccessors().cardinality() > 1) {
                continue;
            }
        }
        /*
             * TODO: Might be nice to try and figure out which local
             * wins most when collected.
             */
        if (ssaMeth.isRegALocal(result)) {
            if (!COLLECT_ONE_LOCAL) {
                continue;
            } else {
                if (usedByLocal.contains(cst)) {
                    // Count one local usage only.
                    continue;
                } else {
                    usedByLocal.add(cst);
                }
            }
        }
        Integer has = countUses.get(cst);
        if (has == null) {
            countUses.put(cst, 1);
        } else {
            countUses.put(cst, has + 1);
        }
    }
    // Collect constants that have been reused.
    ArrayList<TypedConstant> constantList = new ArrayList<TypedConstant>();
    for (Map.Entry<TypedConstant, Integer> entry : countUses.entrySet()) {
        if (entry.getValue() > 1) {
            constantList.add(entry.getKey());
        }
    }
    // Sort by use, with most used at the beginning of the list.
    Collections.sort(constantList, new Comparator<Constant>() {

        public int compare(Constant a, Constant b) {
            int ret;
            ret = countUses.get(b) - countUses.get(a);
            if (ret == 0) {
                /*
                     * Provide sorting determinisim for constants with same
                     * usage count.
                     */
                ret = a.compareTo(b);
            }
            return ret;
        }

        @Override
        public boolean equals(Object obj) {
            return obj == this;
        }
    });
    return constantList;
}
Also used : HashMap(java.util.HashMap) Constant(com.taobao.android.dx.rop.cst.Constant) TypedConstant(com.taobao.android.dx.rop.cst.TypedConstant) CstString(com.taobao.android.dx.rop.cst.CstString) ArrayList(java.util.ArrayList) TypedConstant(com.taobao.android.dx.rop.cst.TypedConstant) TypeBearer(com.taobao.android.dx.rop.type.TypeBearer) HashMap(java.util.HashMap) Map(java.util.Map) RegisterSpec(com.taobao.android.dx.rop.code.RegisterSpec) HashSet(java.util.HashSet)

Example 19 with CstString

use of com.taobao.android.dx.rop.cst.CstString in project atlas by alibaba.

the class ProtoIdItem method makeShortForm.

/**
     * Creates the short-form of the given prototype.
     *
     * @param prototype {@code non-null;} the prototype
     * @return {@code non-null;} the short form
     */
private static CstString makeShortForm(Prototype prototype) {
    StdTypeList parameters = prototype.getParameterTypes();
    int size = parameters.size();
    StringBuilder sb = new StringBuilder(size + 1);
    sb.append(shortFormCharFor(prototype.getReturnType()));
    for (int i = 0; i < size; i++) {
        sb.append(shortFormCharFor(parameters.getType(i)));
    }
    return new CstString(sb.toString());
}
Also used : StdTypeList(com.taobao.android.dx.rop.type.StdTypeList) CstString(com.taobao.android.dx.rop.cst.CstString)

Example 20 with CstString

use of com.taobao.android.dx.rop.cst.CstString in project atlas by alibaba.

the class StringIdsSection method intern.

/**
     * Interns an element into this instance.
     *
     * @param string {@code non-null;} the string to intern
     * @return {@code non-null;} the interned string
     */
public StringIdItem intern(StringIdItem string) {
    if (string == null) {
        throw new NullPointerException("string == null");
    }
    throwIfPrepared();
    CstString value = string.getValue();
    StringIdItem already = strings.get(value);
    if (already != null) {
        return already;
    }
    strings.put(value, string);
    return string;
}
Also used : CstString(com.taobao.android.dx.rop.cst.CstString)

Aggregations

CstString (com.taobao.android.dx.rop.cst.CstString)33 CstType (com.taobao.android.dx.rop.cst.CstType)14 Constant (com.taobao.android.dx.rop.cst.Constant)12 ConstantPool (com.taobao.android.dx.rop.cst.ConstantPool)6 ByteArray (com.taobao.android.dx.util.ByteArray)6 NameValuePair (com.taobao.android.dx.rop.annotation.NameValuePair)5 RegisterSpec (com.taobao.android.dx.rop.code.RegisterSpec)5 Annotation (com.taobao.android.dx.rop.annotation.Annotation)4 CstFieldRef (com.taobao.android.dx.rop.cst.CstFieldRef)4 CstNat (com.taobao.android.dx.rop.cst.CstNat)4 Type (com.taobao.android.dx.rop.type.Type)4 ParseException (com.taobao.android.dx.cf.iface.ParseException)3 ClassDefItem (com.taobao.android.dx.dex.file.ClassDefItem)3 CstAnnotation (com.taobao.android.dx.rop.cst.CstAnnotation)3 Attribute (com.taobao.android.dx.cf.iface.Attribute)2 CstInsn (com.taobao.android.dx.dex.code.CstInsn)2 Annotations (com.taobao.android.dx.rop.annotation.Annotations)2 AnnotationsList (com.taobao.android.dx.rop.annotation.AnnotationsList)2 LocalItem (com.taobao.android.dx.rop.code.LocalItem)2 RegisterSpecList (com.taobao.android.dx.rop.code.RegisterSpecList)2