Search in sources :

Example 1 with FieldInfo

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

the class AllocationWcetModel method getObjectFields.

public List<Type> getObjectFields(String className) {
    List<Type> l = new LinkedList<Type>();
    ClassInfo cli = project.getAppInfo().getClassInfo(className);
    if (cli.getSuperClassName() != null) {
        l.addAll(getObjectFields(cli.getSuperClassName()));
    }
    for (FieldInfo f : cli.getFields()) {
        if (!f.isStatic()) {
            l.add(f.getType());
        }
    }
    return l;
}
Also used : Type(org.apache.bcel.generic.Type) ObjectType(org.apache.bcel.generic.ObjectType) LinkedList(java.util.LinkedList) FieldInfo(com.jopdesign.common.FieldInfo) ClassInfo(com.jopdesign.common.ClassInfo)

Example 2 with FieldInfo

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

the class InlineHelper method prepareInlining.

/**
 * Prepare the invokee for inlining into the invokesite, by widening access restrictions or renaming
 * methods to prevent incorrect method resolving.
 * <p>
 * This may change the code of the invokee, so this needs to be done before inlining the code.
 * The CFG of the invokee will be removed.
 * </p><p>
 * This code assumes that {@link #canInline(CallString, InvokeSite, MethodInfo)} returned true for this invoke.
 * </p>
 *
 * @param invoker the method where the code will be inlined to.
 * @param invokee the method to inline.
 */
public void prepareInlining(MethodInfo invoker, MethodInfo invokee) {
    MethodCode code = invokee.getCode();
    InstructionList il = code.getInstructionList();
    for (InstructionHandle ih : il.getInstructionHandles()) {
        Instruction instr = ih.getInstruction();
        if (instr instanceof InvokeInstruction) {
            InvokeSite invokeSite = code.getInvokeSite(ih);
            MethodRef ref = invokeSite.getInvokeeRef();
            MethodInfo method = ref.getMethodInfo();
            // we already checked that everything can be resolved
            // nothing special to do for invokespecial here (checkInvokeSpecial only skips, no special return codes)
            // check what we need to do
            CheckResult rs = checkNeedsPublic(invoker, invokee, ref.getClassInfo(), method);
            if (rs == CheckResult.NEEDS_PUBLIC) {
                makePublic(method);
            }
            if (rs == CheckResult.NEEDS_PUBLIC_RENAME) {
                if (method.isPrivate()) {
                // TODO check the class for invokers, change to invokevirtual
                } else {
                // if the method is package visible, we need to rename all overriding methods
                // too (but not methods from subclasses in different packages which do not override this)
                // TODO update overriding methods
                // TODO need to update all possible call sites
                }
                makePublic(method);
                throw new AppInfoError("Implement me!");
            }
        } else if (instr instanceof FieldInstruction) {
            FieldRef ref = code.getFieldRef(ih);
            FieldInfo field = ref.getFieldInfo();
            // we already checked that everything can be resolved
            // check if fields need to be set to public
            CheckResult rs = checkNeedsPublic(invoker, invokee, ref.getClassInfo(), field);
            if (rs == CheckResult.NEEDS_PUBLIC) {
                makePublic(field);
            }
            if (rs == CheckResult.NEEDS_PUBLIC_RENAME) {
                throw new AppInfoError("Invalid returncode: renaming of fields not required");
            }
        }
    }
}
Also used : FieldRef(com.jopdesign.common.type.FieldRef) InstructionList(org.apache.bcel.generic.InstructionList) InvokeInstruction(org.apache.bcel.generic.InvokeInstruction) ConstantPushInstruction(org.apache.bcel.generic.ConstantPushInstruction) Instruction(org.apache.bcel.generic.Instruction) FieldInstruction(org.apache.bcel.generic.FieldInstruction) LocalVariableInstruction(org.apache.bcel.generic.LocalVariableInstruction) InstructionHandle(org.apache.bcel.generic.InstructionHandle) AppInfoError(com.jopdesign.common.misc.AppInfoError) InvokeInstruction(org.apache.bcel.generic.InvokeInstruction) MethodRef(com.jopdesign.common.type.MethodRef) MethodInfo(com.jopdesign.common.MethodInfo) InvokeSite(com.jopdesign.common.code.InvokeSite) FieldInstruction(org.apache.bcel.generic.FieldInstruction) MethodCode(com.jopdesign.common.MethodCode) FieldInfo(com.jopdesign.common.FieldInfo)

Example 3 with FieldInfo

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

the class InlineHelper method checkCode.

/**
 * check the code of the invoked method if it contains instructions which prevent inlining.
 *
 * @param invoker the method into which the invokee will be inlined.
 * @param invokee the invoked method.
 * @return true if the code can be inlined and {@link #prepareInlining(MethodInfo, MethodInfo)} will succeed.
 */
private boolean checkCode(MethodInfo invoker, MethodInfo invokee) {
    MethodCode code = invokee.getCode();
    // Go through code, check for access to fields and invocations
    for (InstructionHandle ih : code.getInstructionList(true, false).getInstructionHandles()) {
        Instruction instr = ih.getInstruction();
        if (instr instanceof InvokeInstruction) {
            InvokeSite invokeSite = code.getInvokeSite(ih);
            MethodRef ref = invokeSite.getInvokeeRef();
            MethodInfo method = ref.getMethodInfo();
            if (method == null) {
                // TODO perform basic check on classnames if invoked method must already be public?
                return false;
            }
            // invokespecial is somewhat, well, special..
            if (invokeSite.isInvokeSpecial()) {
                if (checkInvokeSpecial(invoker, invokee, invokeSite, ref.getClassInfo(), method) == CheckResult.SKIP) {
                    return false;
                }
            } else {
                // check if fields need to be set to public
                if (checkNeedsPublic(invoker, invokee, ref.getClassInfo(), method) == CheckResult.SKIP) {
                    return false;
                }
            }
        } else if (instr instanceof FieldInstruction) {
            FieldRef ref = code.getFieldRef(ih);
            FieldInfo field = ref.getFieldInfo();
            if (field == null) {
                // TODO perform basic check on classnames if field is already public?
                return false;
            }
            // check if fields need to be set to public
            if (checkNeedsPublic(invoker, invokee, ref.getClassInfo(), field) == CheckResult.SKIP) {
                return false;
            }
        }
    }
    return true;
}
Also used : InvokeInstruction(org.apache.bcel.generic.InvokeInstruction) MethodRef(com.jopdesign.common.type.MethodRef) FieldRef(com.jopdesign.common.type.FieldRef) MethodInfo(com.jopdesign.common.MethodInfo) InvokeSite(com.jopdesign.common.code.InvokeSite) FieldInstruction(org.apache.bcel.generic.FieldInstruction) MethodCode(com.jopdesign.common.MethodCode) InvokeInstruction(org.apache.bcel.generic.InvokeInstruction) ConstantPushInstruction(org.apache.bcel.generic.ConstantPushInstruction) Instruction(org.apache.bcel.generic.Instruction) FieldInstruction(org.apache.bcel.generic.FieldInstruction) LocalVariableInstruction(org.apache.bcel.generic.LocalVariableInstruction) InstructionHandle(org.apache.bcel.generic.InstructionHandle) FieldInfo(com.jopdesign.common.FieldInfo)

Example 4 with FieldInfo

use of com.jopdesign.common.FieldInfo 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 5 with FieldInfo

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

the class DFATool method classForField.

public ClassInfo classForField(String className, String fieldName) {
    ClassInfo cls = getAppInfo().getClassInfo(className);
    if (cls == null) {
        logger.info("Unknown class as potential receiver of field access" + className);
        return null;
    }
    // TODO maybe we *do* want to check access here...
    FieldInfo field = cls.getFieldInfoInherited(fieldName, false);
    return field != null ? field.getClassInfo() : null;
}
Also used : FieldInfo(com.jopdesign.common.FieldInfo) ClassInfo(com.jopdesign.common.ClassInfo)

Aggregations

FieldInfo (com.jopdesign.common.FieldInfo)10 MethodInfo (com.jopdesign.common.MethodInfo)8 ClassInfo (com.jopdesign.common.ClassInfo)4 InvokeSite (com.jopdesign.common.code.InvokeSite)3 FieldRef (com.jopdesign.common.type.FieldRef)3 MethodRef (com.jopdesign.common.type.MethodRef)3 MethodCode (com.jopdesign.common.MethodCode)2 EnclosingMethod (com.jopdesign.common.bcel.EnclosingMethod)2 ConstantFieldInfo (com.jopdesign.common.type.ConstantFieldInfo)2 ConstantMethodInfo (com.jopdesign.common.type.ConstantMethodInfo)2 LinkedList (java.util.LinkedList)2 ConstantInteger (org.apache.bcel.classfile.ConstantInteger)2 Field (org.apache.bcel.classfile.Field)2 JavaClass (org.apache.bcel.classfile.JavaClass)2 Method (org.apache.bcel.classfile.Method)2 ConstantPushInstruction (org.apache.bcel.generic.ConstantPushInstruction)2 FieldInstruction (org.apache.bcel.generic.FieldInstruction)2 Instruction (org.apache.bcel.generic.Instruction)2 InstructionHandle (org.apache.bcel.generic.InstructionHandle)2 InvokeInstruction (org.apache.bcel.generic.InvokeInstruction)2