Search in sources :

Example 31 with MethodInfo

use of com.jopdesign.common.MethodInfo in project jop by jop-devel.

the class ExecFrequencyProvider method getExecCount.

public long getExecCount(ExecutionContext context) {
    CallString cs = context.getCallString();
    if (cs.isEmpty()) {
        return getExecCount(context.getMethodInfo());
    }
    // if we have a callstring, we need to start at the beginning of the callstring, then multiply the
    // frequencies up to the invoked method along the callstring
    long count = getExecCount(cs.first().getInvoker());
    for (int i = 0; i < cs.length(); i++) {
        InvokeSite is = cs.get(i);
        MethodInfo invokee = i + 1 < cs.length() ? cs.get(i + 1).getInvoker() : context.getMethodInfo();
        count *= getExecFrequency(is, invokee);
    }
    return count;
}
Also used : MethodInfo(com.jopdesign.common.MethodInfo) InvokeSite(com.jopdesign.common.code.InvokeSite) CallString(com.jopdesign.common.code.CallString)

Example 32 with MethodInfo

use of com.jopdesign.common.MethodInfo in project jop by jop-devel.

the class UnusedCodeRemover method removeUnusedMembers.

/**
     * Remove all unused classes, methods and fields.
     */
private void removeUnusedMembers() {
    AppInfo appInfo = AppInfo.getSingleton();
    // we cannot modify the lists while iterating through it
    List<ClassInfo> unusedClasses = new LinkedList<ClassInfo>();
    List<FieldInfo> unusedFields = new LinkedList<FieldInfo>();
    List<MethodInfo> unusedMethods = new LinkedList<MethodInfo>();
    int fields = 0;
    int methods = 0;
    for (ClassInfo cls : appInfo.getClassInfos()) {
        if (ucf.getMark(cls) == Mark.UNUSED) {
            unusedClasses.add(cls);
            logger.debug("Removing unused class " + cls);
            continue;
        }
        unusedFields.clear();
        unusedMethods.clear();
        if (appInfo.isHwObject(cls)) {
            // Do not remove fields from hardware objects, else the mapping gets broken and
            // chaos takes over!
            logger.debug("Skipping fields of used hardware object " + cls);
        } else {
            for (FieldInfo f : cls.getFields()) {
                if (ucf.getMark(f) == Mark.UNUSED) {
                    unusedFields.add(f);
                }
            }
        }
        for (MethodInfo m : cls.getMethods()) {
            Mark mark = ucf.getMark(m);
            if (mark == Mark.UNUSED) {
                unusedMethods.add(m);
            }
            if (mark == Mark.MARKED && !m.isNative() && !m.isAbstract()) {
                logger.info("Making unused method " + m + " abstract");
                m.setAbstract(true);
                m.getClassInfo().setAbstract(true);
            }
        }
        for (FieldInfo f : unusedFields) {
            fields += removeField(f);
        }
        for (MethodInfo m : unusedMethods) {
            methods += removeMethod(m);
        }
    }
    appInfo.removeClasses(unusedClasses);
    int classes = unusedClasses.size();
    logger.info("Removed " + classes + (classes == 1 ? " class, " : " classes, ") + fields + (fields == 1 ? " field, " : " fields, ") + methods + (methods == 1 ? " method" : " methods"));
}
Also used : Mark(com.jopdesign.common.tools.UsedCodeFinder.Mark) MethodInfo(com.jopdesign.common.MethodInfo) LinkedList(java.util.LinkedList) FieldInfo(com.jopdesign.common.FieldInfo) AppInfo(com.jopdesign.common.AppInfo) ClassInfo(com.jopdesign.common.ClassInfo)

Example 33 with MethodInfo

use of com.jopdesign.common.MethodInfo in project jop by jop-devel.

the class JOPModel method getJavaImplementation.

public MethodInfo getJavaImplementation(AppInfo ai, MethodInfo context, Instruction instr) {
    ClassInfo receiver = ai.getClassInfo(JVM_CLASS);
    String methodName = "f_" + JopInstr.name(getNativeOpCode(context, instr));
    Set<MethodInfo> mi = receiver.getMethodByName(methodName);
    if (!mi.isEmpty()) {
        if (mi.size() > 1) {
            throw new JavaClassFormatError("JVM class " + JVM_CLASS + " has more than one implementation of " + methodName);
        }
        return mi.iterator().next();
    }
    return null;
}
Also used : JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) MethodInfo(com.jopdesign.common.MethodInfo) ClassInfo(com.jopdesign.common.ClassInfo)

Example 34 with MethodInfo

use of com.jopdesign.common.MethodInfo in project jop by jop-devel.

the class ClinitOrder method visitClass.

@Override
public boolean visitClass(ClassInfo classInfo) {
    MethodInfo mi = classInfo.getMethodInfo(clinitSig);
    if (mi != null) {
        Set<ClassInfo> depends = findDependencies(mi, false);
        clinit.put(classInfo, depends);
    }
    return true;
}
Also used : MethodInfo(com.jopdesign.common.MethodInfo) ConstantMethodInfo(com.jopdesign.common.type.ConstantMethodInfo) ClassInfo(com.jopdesign.common.ClassInfo)

Example 35 with MethodInfo

use of com.jopdesign.common.MethodInfo in project jop by jop-devel.

the class ClinitOrder method findDependencies.

private Set<ClassInfo> findDependencies(MethodInfo method, boolean inRec) {
    //		System.out.println("find dep. in "+cli.clazz.getClassName()+":"+mi.getMethod().getName());
    Set<ClassInfo> depends = new HashSet<ClassInfo>();
    if (method.isNative() || method.isAbstract()) {
        // subclasses???? :-(
        return depends;
    }
    ClassInfo classInfo = method.getClassInfo();
    ConstantPoolGen cpoolgen = method.getConstantPoolGen();
    InstructionList il = method.getCode().getInstructionList();
    InstructionFinder f = new InstructionFinder(il);
    // TODO can we encounter an empty instruction list?
    //if(il.getStart() == null) {
    //    return depends;
    //}
    // find instructions that access the constant pool
    // collect all indices to constants in ClassInfo
    String cpInstr = "CPInstruction";
    for (Iterator it = f.search(cpInstr); it.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) it.next();
        InstructionHandle first = match[0];
        CPInstruction ii = (CPInstruction) first.getInstruction();
        int idx = ii.getIndex();
        Constant co = cpoolgen.getConstant(idx);
        ClassRef classRef = null;
        Set addDepends = null;
        ClassInfo clinfo;
        MethodInfo minfo;
        switch(co.getTag()) {
            case Constants.CONSTANT_Class:
                classRef = classInfo.getConstantInfo(co).getClassRef();
                clinfo = classRef.getClassInfo();
                if (clinfo != null) {
                    minfo = clinfo.getMethodInfo("<init>()V");
                    if (minfo != null) {
                        addDepends = findDependencies(minfo, true);
                    }
                }
                break;
            case Constants.CONSTANT_Fieldref:
            case Constants.CONSTANT_InterfaceMethodref:
                classRef = classInfo.getConstantInfo(co).getClassRef();
                break;
            case Constants.CONSTANT_Methodref:
                ConstantMethodInfo mref = (ConstantMethodInfo) classInfo.getConstantInfo(co);
                classRef = mref.getClassRef();
                minfo = mref.getMethodRef().getMethodInfo();
                if (minfo != null) {
                    addDepends = findDependencies(minfo, true);
                }
                break;
        }
        if (classRef != null) {
            ClassInfo clinf = classRef.getClassInfo();
            if (clinf != null) {
                if (clinf.getMethodInfo(clinitSig) != null) {
                    // don't add myself as dependency
                    if (!clinf.equals(method.getClassInfo())) {
                        depends.add(clinf);
                    }
                }
            }
        }
        if (addDepends != null) {
            for (Object addDepend : addDepends) {
                ClassInfo addCli = (ClassInfo) addDepend;
                if (addCli.equals(method.getClassInfo())) {
                    throw new JavaClassFormatError("cyclic indirect <clinit> dependency");
                }
                depends.add(addCli);
            }
        }
    }
    return depends;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ClassRef(com.jopdesign.common.type.ClassRef) InstructionList(org.apache.bcel.generic.InstructionList) Constant(org.apache.bcel.classfile.Constant) InstructionFinder(org.apache.bcel.util.InstructionFinder) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ConstantMethodInfo(com.jopdesign.common.type.ConstantMethodInfo) ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) CPInstruction(org.apache.bcel.generic.CPInstruction) JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) Iterator(java.util.Iterator) MethodInfo(com.jopdesign.common.MethodInfo) ConstantMethodInfo(com.jopdesign.common.type.ConstantMethodInfo) HashSet(java.util.HashSet) ClassInfo(com.jopdesign.common.ClassInfo)

Aggregations

MethodInfo (com.jopdesign.common.MethodInfo)108 LinkedHashSet (java.util.LinkedHashSet)21 InstructionHandle (org.apache.bcel.generic.InstructionHandle)20 ClassInfo (com.jopdesign.common.ClassInfo)19 ExecutionContext (com.jopdesign.common.code.ExecutionContext)16 CFGNode (com.jopdesign.common.code.ControlFlowGraph.CFGNode)13 ArrayList (java.util.ArrayList)13 CallString (com.jopdesign.common.code.CallString)12 ControlFlowGraph (com.jopdesign.common.code.ControlFlowGraph)12 HashMap (java.util.HashMap)10 Set (java.util.Set)10 LinkedHashMap (java.util.LinkedHashMap)9 Instruction (org.apache.bcel.generic.Instruction)9 FieldInfo (com.jopdesign.common.FieldInfo)8 MethodCode (com.jopdesign.common.MethodCode)8 AppInfo (com.jopdesign.common.AppInfo)7 ContextEdge (com.jopdesign.common.code.CallGraph.ContextEdge)7 InvokeSite (com.jopdesign.common.code.InvokeSite)7 MemberID (com.jopdesign.common.type.MemberID)7 Context (com.jopdesign.dfa.framework.Context)7