Search in sources :

Example 6 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project jop by jop-devel.

the class CallStringReceiverTypes method copyResults.

@Override
public void copyResults(MethodInfo newContainer, Map<InstructionHandle, InstructionHandle> newHandles) {
    for (Map.Entry<InstructionHandle, InstructionHandle> entry : newHandles.entrySet()) {
        InstructionHandle oldHandle = entry.getKey();
        InstructionHandle newHandle = entry.getValue();
        if (newHandle == null)
            continue;
        ContextMap<CallString, Set<String>> value = targets.get(oldHandle);
        // TODO this does NOT update stackPtr,.. in the new context!
        if (value != null)
            targets.put(newHandle, value.copy(newContainer));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ContextMap(com.jopdesign.dfa.framework.ContextMap) InstructionHandle(org.apache.bcel.generic.InstructionHandle) CallString(com.jopdesign.common.code.CallString)

Example 7 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project jop by jop-devel.

the class CallStringReceiverTypes method updateThreads.

private void updateThreads(Map<CallString, Set<TypeMapping>> input, Interpreter<CallString, Set<TypeMapping>> interpreter, Map<InstructionHandle, ContextMap<CallString, Set<TypeMapping>>> state) {
    DFATool p = interpreter.getDFATool();
    boolean modified = true;
    while (modified) {
        modified = false;
        Map<String, ContextMap<CallString, Set<TypeMapping>>> tmpThreads = new LinkedHashMap<String, ContextMap<CallString, Set<TypeMapping>>>();
        for (String methodName : threads.keySet()) {
            MethodInfo method = p.getMethod(methodName);
            InstructionHandle entry = p.getEntryHandle(method);
            Context c = state.get(entry).getContext();
            int varPtr = c.stackPtr - MethodHelper.getArgSize(method);
            // prepare input information
            ContextMap<CallString, Set<TypeMapping>> threadInput = new ContextMap<CallString, Set<TypeMapping>>(c, new LinkedHashMap<CallString, Set<TypeMapping>>());
            for (CallString cs : input.keySet()) {
                Set<TypeMapping> s = input.get(cs);
                Set<TypeMapping> o = new LinkedHashSet<TypeMapping>();
                filterSet(s, o, 0);
                threadInput.put(cs, o);
            }
            state.put(entry, join(threadInput, state.get(entry)));
            // save information
            ContextMap<CallString, Set<TypeMapping>> savedResult = threads.get(methodName);
            // interpret thread
            Map<InstructionHandle, ContextMap<CallString, Set<TypeMapping>>> r = interpreter.interpret(c, entry, state, false);
            // pull out relevant information from thread
            InstructionHandle exit = p.getExitHandle(method);
            ContextMap<CallString, Set<TypeMapping>> threadResult;
            if (r.get(exit) != null) {
                threadResult = new ContextMap<CallString, Set<TypeMapping>>(c, new LinkedHashMap<CallString, Set<TypeMapping>>());
                threadResult.put(c.callString, new LinkedHashSet<TypeMapping>());
                Set<TypeMapping> returned = r.get(exit).get(c.callString);
                filterReturnSet(returned, threadResult.get(c.callString), varPtr);
            } else {
                threadResult = new ContextMap<CallString, Set<TypeMapping>>(c, new LinkedHashMap<CallString, Set<TypeMapping>>());
                threadResult.put(c.callString, new LinkedHashSet<TypeMapping>());
            }
            if (!threadResult.equals(savedResult)) {
                modified = true;
            }
            tmpThreads.put(methodName, threadResult);
        }
        threads = tmpThreads;
    }
}
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) LinkedHashMap(java.util.LinkedHashMap) CallString(com.jopdesign.common.code.CallString) MethodInfo(com.jopdesign.common.MethodInfo)

Example 8 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project jop by jop-devel.

the class LoopBounds method getBound.

public int getBound(InstructionHandle instr, CallString csSuffix) {
    ContextMap<CallString, Pair<ValueMapping, ValueMapping>> r = bounds.get(instr);
    if (r == null) {
        // no bound at this point
        return -1;
    }
    // merge bound for all contexts
    int maxValue = -1;
    for (CallString callString : r.keySet()) {
        if (!callString.hasSuffix(csSuffix))
            continue;
        Pair<ValueMapping, ValueMapping> bounds = r.get(callString);
        ValueMapping first = bounds.first();
        ValueMapping second = bounds.second();
        if (first == null || second == null) {
            return -1;
        }
        InstructionHandle target = ((BranchInstruction) instr.getInstruction()).getTarget();
        if (scopes.get(target) != null) {
            if (scopes.get(target).get(callString) <= first.defscope || scopes.get(target).get(callString) <= second.defscope) {
                return -1;
            }
        }
        if (scopes.get(instr).get(callString) <= first.defscope || scopes.get(instr).get(callString) <= second.defscope) {
            return -1;
        }
        //			if (first.softinc || second.softinc) {
        //				return -1;
        //			}
        int val = ValueMapping.computeBound(first, second);
        if (val < 0) {
            // no bound for some context
            return -1;
        } else {
            // compute the maximum
            maxValue = Math.max(maxValue, val);
        }
    }
    return maxValue;
}
Also used : BranchInstruction(org.apache.bcel.generic.BranchInstruction) InstructionHandle(org.apache.bcel.generic.InstructionHandle) CallString(com.jopdesign.common.code.CallString) Pair(com.jopdesign.common.graphutils.Pair)

Example 9 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project jop by jop-devel.

the class LoopBounds method printSizeResult.

public void printSizeResult(DFATool program) {
    for (InstructionHandle instr : sizes.keySet()) {
        ContextMap<CallString, Interval[]> r = sizes.get(instr);
        Context c = r.getContext();
        LineNumberTable lines = c.getMethodInfo().getCode().getLineNumberTable();
        int sourceLine = lines.getSourceLine(instr.getPosition());
        for (CallString callString : r.keySet()) {
            Interval[] bounds = r.get(callString);
            System.out.println(c.method() + ":" + sourceLine + ":\t" + callString.toStringList() + ": ");
            System.out.println(Arrays.asList(bounds));
        }
    }
}
Also used : Context(com.jopdesign.dfa.framework.Context) InstructionHandle(org.apache.bcel.generic.InstructionHandle) LineNumberTable(org.apache.bcel.classfile.LineNumberTable) CallString(com.jopdesign.common.code.CallString)

Example 10 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project jop by jop-devel.

the class LoopBounds method doInvoke.

private void doInvoke(String methodName, InstructionHandle stmt, Context context, Map<CallString, Map<Location, ValueMapping>> input, Interpreter<CallString, Map<Location, ValueMapping>> interpreter, Map<InstructionHandle, ContextMap<CallString, Map<Location, ValueMapping>>> state, Map<CallString, Map<Location, ValueMapping>> result) {
    DFATool p = interpreter.getDFATool();
    MethodInfo method = p.getMethod(methodName);
    if (method.isNative()) {
        handleNative(method, context, input, result);
    } else {
        // 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(method, stmt, callStringLength);
        // carry only minimal information with call
        Map<Location, ValueMapping> in = input.get(context.callString);
        Map<Location, ValueMapping> out = new LinkedHashMap<Location, ValueMapping>();
        for (Location l : in.keySet()) {
            if (l.stackLoc < 0) {
                out.put(l, in.get(l));
            }
            if (l.stackLoc >= varPtr) {
                out.put(new Location(l.stackLoc - varPtr), new ValueMapping(in.get(l), false));
            }
        }
        ContextMap<CallString, Map<Location, ValueMapping>> tmpresult = new ContextMap<CallString, Map<Location, ValueMapping>>(c, new LinkedHashMap<CallString, Map<Location, ValueMapping>>());
        tmpresult.put(c.callString, out);
        InstructionHandle entry = p.getEntryHandle(method);
        state.put(entry, join(state.get(entry), tmpresult));
        // interpret method
        Map<InstructionHandle, ContextMap<CallString, Map<Location, ValueMapping>>> r = interpreter.interpret(c, entry, state, false);
        //System.out.println(">>>>>>>>");
        // pull out relevant information from call
        InstructionHandle exit = p.getExitHandle(method);
        if (r.get(exit) != null) {
            Map<Location, ValueMapping> returned = r.get(exit).get(c.callString);
            if (returned != null) {
                for (Location l : returned.keySet()) {
                    if (l.stackLoc < 0) {
                        ValueMapping m = new ValueMapping(returned.get(l), true);
                        m.join(result.get(context.callString).get(l));
                        result.get(context.callString).put(l, m);
                    }
                    if (l.stackLoc >= 0) {
                        ValueMapping m = new ValueMapping(returned.get(l), false);
                        Location loc = new Location(l.stackLoc + varPtr);
                        m.join(result.get(context.callString).get(loc));
                        result.get(context.callString).put(loc, m);
                    }
                }
            }
        }
        // add relevant information to result
        for (Location l : in.keySet()) {
            if (l.stackLoc >= 0 && l.stackLoc < context.stackPtr - MethodHelper.getArgSize(method)) {
                result.get(context.callString).put(l, new ValueMapping(in.get(l), true));
            }
        }
    }
}
Also used : Context(com.jopdesign.dfa.framework.Context) DFATool(com.jopdesign.dfa.DFATool) ContextMap(com.jopdesign.dfa.framework.ContextMap) InstructionHandle(org.apache.bcel.generic.InstructionHandle) LinkedHashMap(java.util.LinkedHashMap) CallString(com.jopdesign.common.code.CallString) MethodInfo(com.jopdesign.common.MethodInfo) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ContextMap(com.jopdesign.dfa.framework.ContextMap)

Aggregations

InstructionHandle (org.apache.bcel.generic.InstructionHandle)103 InstructionList (org.apache.bcel.generic.InstructionList)26 MethodInfo (com.jopdesign.common.MethodInfo)20 CallString (com.jopdesign.common.code.CallString)20 Instruction (org.apache.bcel.generic.Instruction)13 MethodCode (com.jopdesign.common.MethodCode)12 ContextMap (com.jopdesign.dfa.framework.ContextMap)11 IntermediateEdge (org.candle.decompiler.intermediate.graph.edge.IntermediateEdge)11 Context (com.jopdesign.dfa.framework.Context)10 InvokeInstruction (org.apache.bcel.generic.InvokeInstruction)10 Iterator (java.util.Iterator)9 BranchInstruction (org.apache.bcel.generic.BranchInstruction)9 FieldInstruction (org.apache.bcel.generic.FieldInstruction)9 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)9 ReturnInstruction (org.apache.bcel.generic.ReturnInstruction)8 InstructionFinder (org.apache.bcel.util.InstructionFinder)8 ClassInfo (com.jopdesign.common.ClassInfo)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 LinkedList (java.util.LinkedList)7