Search in sources :

Example 11 with ContextMap

use of com.jopdesign.dfa.framework.ContextMap in project jop by jop-devel.

the class DFATool method runLocalAnalysis.

public <K, V> Map runLocalAnalysis(Analysis<K, V> localAnalysis, ExecutionContext scope) {
    Interpreter<K, V> interpreter = new Interpreter<K, V>(localAnalysis, this);
    if (scope == null)
        throw new AssertionError("No such method: " + scope);
    Context context = new Context();
    MethodCode entryCode = scope.getMethodInfo().getCode();
    context.stackPtr = entryCode.getMaxLocals();
    context.setMethodInfo(scope.getMethodInfo());
    context.setCallString(scope.getCallString());
    localAnalysis.initialize(scope.getMethodInfo(), context);
    /* Here used to be a extremely-hard-to-trackdown bug!
         * Without the boolean parameters, getInstructionList() will dispose
         * the CFG, a very bad idea during WCET analysis which relies on
         * pointer equality for CFG edges :(
         */
    InstructionHandle entry = entryCode.getInstructionList(false, false).getStart();
    interpreter.interpret(context, entry, new LinkedHashMap<InstructionHandle, ContextMap<K, V>>(), true);
    return localAnalysis.getResult();
}
Also used : Context(com.jopdesign.dfa.framework.Context) ExecutionContext(com.jopdesign.common.code.ExecutionContext) Interpreter(com.jopdesign.dfa.framework.Interpreter) MethodCode(com.jopdesign.common.MethodCode) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ContextMap(com.jopdesign.dfa.framework.ContextMap)

Example 12 with ContextMap

use of com.jopdesign.dfa.framework.ContextMap in project jop by jop-devel.

the class DFATool method runReceiverAnalysis.

public Map<InstructionHandle, ContextMap<CallString, Set<String>>> runReceiverAnalysis(int callstringLength) {
    CallStringReceiverTypes recTys = new CallStringReceiverTypes(callstringLength);
    @SuppressWarnings({ "unchecked" }) Map<InstructionHandle, ContextMap<CallString, Set<String>>> receiverResults = runAnalysis(recTys);
    setReceivers(recTys);
    return receiverResults;
}
Also used : CallString(com.jopdesign.common.code.CallString) CallStringReceiverTypes(com.jopdesign.dfa.analyses.CallStringReceiverTypes) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ContextMap(com.jopdesign.dfa.framework.ContextMap)

Example 13 with ContextMap

use of com.jopdesign.dfa.framework.ContextMap in project jop by jop-devel.

the class DFATool method dumpDFA.

public String dumpDFA(MethodInfo method) {
    if (getLoopBounds() == null)
        return "n/a";
    if (method.isAbstract()) {
        return "n/a";
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream os = new PrintStream(baos);
    Map<InstructionHandle, ContextMap<CallString, Pair<ValueMapping, ValueMapping>>> results = getLoopBounds().getResult();
    if (results == null)
        return "n/a";
    AnalysisResultSerialization<Pair<ValueMapping, ValueMapping>> printer = new AnalysisResultSerialization<Pair<ValueMapping, ValueMapping>>();
    for (Entry<InstructionHandle, ContextMap<CallString, Pair<ValueMapping, ValueMapping>>> ihEntry : results.entrySet()) {
        for (Entry<CallString, Pair<ValueMapping, ValueMapping>> csEntry : ihEntry.getValue().entrySet()) {
            if (ihEntry.getValue().getContext().getMethodInfo().equals(method)) {
                printer.addResult(method, ihEntry.getKey().getPosition(), csEntry.getKey(), csEntry.getValue());
            }
        }
    }
    printer.dump(os);
    try {
        return baos.toString("UTF-8");
    } catch (UnsupportedEncodingException e) {
        return baos.toString();
    }
}
Also used : PrintStream(java.io.PrintStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ContextMap(com.jopdesign.dfa.framework.ContextMap) ValueMapping(com.jopdesign.dfa.analyses.ValueMapping) CallString(com.jopdesign.common.code.CallString) AnalysisResultSerialization(com.jopdesign.dfa.framework.AnalysisResultSerialization) Pair(com.jopdesign.common.graphutils.Pair)

Example 14 with ContextMap

use of com.jopdesign.dfa.framework.ContextMap in project jop by jop-devel.

the class CallStringReceiverTypes method doInvokeVirtual.

private void doInvokeVirtual(String receiver, MethodInfo method, InstructionHandle stmt, Context context, ContextMap<CallString, Set<TypeMapping>> input, Interpreter<CallString, Set<TypeMapping>> interpreter, Map<InstructionHandle, ContextMap<CallString, Set<TypeMapping>>> state, ContextMap<CallString, Set<TypeMapping>> result) {
    String methodImplName = method.getClassName() + "." + method.getMethodSignature();
    recordReceiver(stmt, context, methodImplName);
    //		LineNumberTable lines = p.getMethods().get(context.method).getLineNumberTable(context.constPool);
    //		int sourceLine = lines.getSourceLine(stmt.getPosition());
    //		String invokeId = context.method+"\t"+":"+sourceLine+"."+stmt.getPosition();
    // set up new context
    int varPtr = context.stackPtr - MethodHelper.getArgSize(method);
    Context c = new Context(context);
    c.stackPtr = method.getCode().getMaxLocals();
    if (method.isSynchronized()) {
        c.syncLevel = context.syncLevel + 1;
    }
    c.setMethodInfo(method);
    c.callString = c.callString.push(context.getMethodInfo(), stmt, callStringLength);
    boolean threaded = false;
    DFATool p = interpreter.getDFATool();
    if (p.getAppInfo().getClassInfo(receiver).isSubclassOf(p.getAppInfo().getClassInfo("joprt.RtThread")) && "run()V".equals(method.getMethodSignature())) {
        c.createThread();
        threaded = true;
    }
    // carry only minimal information with call
    Set<TypeMapping> in = input.get(context.callString);
    Set<TypeMapping> out = new LinkedHashSet<TypeMapping>();
    ContextMap<CallString, Set<TypeMapping>> tmpresult = new ContextMap<CallString, Set<TypeMapping>>(c, new LinkedHashMap<CallString, Set<TypeMapping>>());
    tmpresult.put(c.callString, out);
    for (TypeMapping m : in) {
        if (m.stackLoc < 0) {
            out.add(m);
        }
        if (m.stackLoc > varPtr) {
            out.add(new TypeMapping(m.stackLoc - varPtr, m.type));
        }
        if (m.stackLoc == varPtr) {
            // add "this"
            ClassInfo staticClass = p.getAppInfo().getClassInfo(receiver);
            ClassInfo dynamicClass = null;
            try {
                dynamicClass = p.getAppInfo().getClassInfo(m.type.split("@")[0]);
            } catch (MissingClassError ex) {
                logger.error("doInvokeVirtual - trying to improve type of " + staticClass + ": " + ex);
            // TODO: maybe we should throw an exception/error instead?
            }
            if (dynamicClass != null && dynamicClass.isSubclassOf(staticClass)) {
                out.add(new TypeMapping(0, m.type));
            }
        }
    }
    InstructionHandle entry = p.getEntryHandle(method);
    state.put(entry, join(state.get(entry), tmpresult));
    // interpret method
    Map<InstructionHandle, ContextMap<CallString, Set<TypeMapping>>> r = interpreter.interpret(c, entry, state, false);
    // pull out relevant information from call
    InstructionHandle exit = p.getExitHandle(method);
    if (r.get(exit) != null) {
        Set<TypeMapping> returned = r.get(exit).get(c.callString);
        if (returned != null) {
            filterReturnSet(returned, result.get(context.callString), varPtr);
        }
    }
    // update all threads
    if (threaded) {
        threads.put(methodImplName, new ContextMap<CallString, Set<TypeMapping>>(c, result));
        updateThreads(result, interpreter, state);
    }
}
Also used : Context(com.jopdesign.dfa.framework.Context) LinkedHashSet(java.util.LinkedHashSet) DFATool(com.jopdesign.dfa.DFATool) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) CallString(com.jopdesign.common.code.CallString) ContextMap(com.jopdesign.dfa.framework.ContextMap) InstructionHandle(org.apache.bcel.generic.InstructionHandle) CallString(com.jopdesign.common.code.CallString) ClassInfo(com.jopdesign.common.ClassInfo) MissingClassError(com.jopdesign.common.misc.MissingClassError)

Example 15 with ContextMap

use of com.jopdesign.dfa.framework.ContextMap in project jop by jop-devel.

the class CallStringReceiverTypes method join.

public ContextMap<CallString, Set<TypeMapping>> join(ContextMap<CallString, Set<TypeMapping>> s1, ContextMap<CallString, Set<TypeMapping>> s2) {
    if (s1 == null) {
        return new ContextMap<CallString, Set<TypeMapping>>(s2);
    }
    if (s2 == null) {
        return new ContextMap<CallString, Set<TypeMapping>>(s1);
    }
    ContextMap<CallString, Set<TypeMapping>> result = new ContextMap<CallString, Set<TypeMapping>>(new Context(s2.getContext()), new LinkedHashMap<CallString, Set<TypeMapping>>());
    result.putAll(s1);
    result.putAll(s2);
    Set<TypeMapping> a = s1.get(s2.getContext().callString);
    Set<TypeMapping> b = s2.get(s2.getContext().callString);
    Set<TypeMapping> merged = new LinkedHashSet<TypeMapping>();
    if (a != null) {
        merged.addAll(a);
    }
    if (b == null) {
        logger.error("RT join: failed to find DF information for " + s2.getContext().callString.toStringVerbose(false));
    }
    merged.addAll(b);
    result.put(s2.getContext().callString, merged);
    if (result.getContext().stackPtr < 0) {
        result.getContext().stackPtr = s2.getContext().stackPtr;
    }
    if (result.getContext().syncLevel < 0) {
        result.getContext().syncLevel = s2.getContext().syncLevel;
    }
    result.getContext().threaded = Context.isThreaded();
    return result;
}
Also used : Context(com.jopdesign.dfa.framework.Context) LinkedHashSet(java.util.LinkedHashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ContextMap(com.jopdesign.dfa.framework.ContextMap) CallString(com.jopdesign.common.code.CallString)

Aggregations

ContextMap (com.jopdesign.dfa.framework.ContextMap)19 CallString (com.jopdesign.common.code.CallString)18 Context (com.jopdesign.dfa.framework.Context)16 Set (java.util.Set)10 InstructionHandle (org.apache.bcel.generic.InstructionHandle)10 LinkedHashSet (java.util.LinkedHashSet)9 DFATool (com.jopdesign.dfa.DFATool)8 LinkedHashMap (java.util.LinkedHashMap)7 MethodInfo (com.jopdesign.common.MethodInfo)6 Map (java.util.Map)6 ReferenceType (org.apache.bcel.generic.ReferenceType)4 Type (org.apache.bcel.generic.Type)4 AppInfoError (com.jopdesign.common.misc.AppInfoError)3 GETFIELD (org.apache.bcel.generic.GETFIELD)3 GETSTATIC (org.apache.bcel.generic.GETSTATIC)3 Instruction (org.apache.bcel.generic.Instruction)3 LDC (org.apache.bcel.generic.LDC)3 LoadInstruction (org.apache.bcel.generic.LoadInstruction)3 PUTFIELD (org.apache.bcel.generic.PUTFIELD)3 PUTSTATIC (org.apache.bcel.generic.PUTSTATIC)3