Search in sources :

Example 51 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project candle-decompiler by bradsdavis.

the class ConditionEdgeEnhancer method process.

@Override
public void process(InstructionHandle ih) {
    if (ih instanceof BranchHandle) {
        // ok, now we need to replace existing successor edges appropriately.
        BranchHandle bh = (BranchHandle) ih;
        List<InstructionHandle> successors = igc.getSuccessors(ih);
        TreeSet<InstructionHandle> orderedSuccessors = new TreeSet<InstructionHandle>(new InstructionComparator());
        orderedSuccessors.addAll(successors);
        if (successors.size() == 2) {
            // lowest will be true condition....
            IntermediateEdge truePath = igc.getGraph().getEdge(ih, orderedSuccessors.first());
            ConditionEdge trueCondition = createConditionalEdge(truePath, true);
            igc.getGraph().removeEdge(truePath);
            igc.getGraph().addEdge(ih, orderedSuccessors.first(), trueCondition);
            // highest will be false condition....
            IntermediateEdge falsePath = igc.getGraph().getEdge(ih, orderedSuccessors.last());
            ConditionEdge falseCondition = createConditionalEdge(falsePath, false);
            igc.getGraph().removeEdge(falsePath);
            igc.getGraph().addEdge(ih, orderedSuccessors.last(), falseCondition);
        }
    }
}
Also used : InstructionComparator(org.candle.decompiler.instruction.graph.vertex.InstructionComparator) TreeSet(java.util.TreeSet) BranchHandle(org.apache.bcel.generic.BranchHandle) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ConditionEdge(org.candle.decompiler.intermediate.graph.edge.ConditionEdge)

Example 52 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project candle-decompiler by bradsdavis.

the class ContinuousLoop method process.

@Override
public void process(InstructionHandle ih) {
    List<InstructionHandle> preds = Graphs.predecessorListOf(igc.getGraph(), ih);
    // only more than one predecessor.
    if (preds.size() < 2) {
        return;
    }
    for (InstructionHandle pred : preds) {
        IntermediateEdge ie = igc.getGraph().getEdge(pred, ih);
        if (ie.getType() == EdgeType.BACK) {
            if (!igc.hasIntermediate(ih)) {
                // this is probably a while(true);
                ContinuousWhileIntermediate cwi = new ContinuousWhileIntermediate(ih);
                igc.addIntermediate(ih, cwi);
                break;
            }
        }
    }
}
Also used : ContinuousWhileIntermediate(org.candle.decompiler.intermediate.code.loop.ContinuousWhileIntermediate) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 53 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project candle-decompiler by bradsdavis.

the class InstructionGraphWriter method process.

@Override
public void process() {
    if (directory == null) {
        return;
    }
    File a = new File(directory.getAbsolutePath() + File.separator + name);
    LOG.debug("Instruction Graph: " + a.getAbsolutePath());
    Writer x;
    try {
        x = new FileWriter(a);
        DOTExporter<InstructionHandle, IntermediateEdge> f = new DOTExporter<InstructionHandle, IntermediateEdge>(new IntegerNameProvider<InstructionHandle>(), new InstructionLabelProvider(), new IntermediateEdgeProvider(), null, new InstructionEdgeAttributeProvider());
        f.export(x, igc.getGraph());
    } catch (IOException e) {
        e.printStackTrace();
    }
    LOG.debug("End Instruction Graph ======");
}
Also used : DOTExporter(org.jgrapht.ext.DOTExporter) InstructionLabelProvider(org.candle.decompiler.instruction.graph.vertex.InstructionLabelProvider) IntermediateEdgeProvider(org.candle.decompiler.intermediate.graph.edge.IntermediateEdgeProvider) FileWriter(java.io.FileWriter) InstructionEdgeAttributeProvider(org.candle.decompiler.instruction.graph.edge.InstructionEdgeAttributeProvider) IOException(java.io.IOException) File(java.io.File) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) FileWriter(java.io.FileWriter) Writer(java.io.Writer) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 54 with InstructionHandle

use of org.apache.bcel.generic.InstructionHandle in project candle-decompiler by bradsdavis.

the class LoopHeader method process.

@Override
public void process(InstructionHandle ih) {
    // check to see whether a predecessor is a back edge.
    List<InstructionHandle> preds = Graphs.predecessorListOf(igc.getGraph(), ih);
    // only more than one predecessor.
    if (preds.size() < 2) {
        return;
    }
    for (InstructionHandle pred : preds) {
        IntermediateEdge ie = igc.getGraph().getEdge(pred, ih);
        if (ie.getType() == EdgeType.BACK) {
            LOG.debug("Has back edge.");
            splitLoopHeader(ih);
            break;
        }
    }
}
Also used : IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 55 with InstructionHandle

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

the class FindUsedConstants method find.

private void find(Method method) {
    MethodGen mg = new MethodGen(method, clazz.getClassName(), cpool);
    InstructionList il = mg.getInstructionList();
    InstructionFinder f = new InstructionFinder(il);
    // 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 = cpool.getConstant(idx);
        int len = 1;
        switch(co.getTag()) {
            case Constants.CONSTANT_Long:
            case Constants.CONSTANT_Double:
                len = 2;
                break;
        }
        // we don't need the field references in the cpool anymore
        if (co.getTag() != Constants.CONSTANT_Fieldref) {
            getCli().addUsedConst(idx, len);
        }
    // also modify the index!
    //			Constant cnst = cpool.getConstant(ii.getIndex());
    //			int newIndex = addConstant(cnst);
    //System.out.println(ii+" -> "+newIndex);
    //			ii.setIndex(newIndex);			
    }
    il.dispose();
    CodeExceptionGen[] et = mg.getExceptionHandlers();
    for (int i = 0; i < et.length; i++) {
        ObjectType ctype = et[i].getCatchType();
        if (ctype != null) {
            getCli().addUsedConst(cpool.lookupClass(ctype.getClassName()), 1);
        }
    }
}
Also used : InstructionList(org.apache.bcel.generic.InstructionList) Constant(org.apache.bcel.classfile.Constant) InstructionFinder(org.apache.bcel.util.InstructionFinder) MethodGen(org.apache.bcel.generic.MethodGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) CPInstruction(org.apache.bcel.generic.CPInstruction) ObjectType(org.apache.bcel.generic.ObjectType) Iterator(java.util.Iterator) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen)

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