Search in sources :

Example 1 with Select

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

the class IntermediateGraphFactory method visitMultiBranchIntermediate.

@Override
public void visitMultiBranchIntermediate(MultiBranchIntermediate line) {
    Select select = (Select) line.getInstruction().getInstruction();
    Set<Case> cases = new HashSet<Case>();
    InstructionHandle[] handles = select.getTargets();
    int[] matches = select.getMatchs();
    for (int i = 0, j = handles.length; i < j; i++) {
        InstructionHandle ih = handles[i];
        int match = matches[i];
        Resolved resolved = new Resolved(line.getInstruction(), BasicType.INT, "" + match);
        Case caseEntry = new Case(line.getInstruction(), ih, resolved);
        cases.add(caseEntry);
    }
    if (select.getTarget() != null) {
        DefaultCase defaultCase = new DefaultCase(line.getInstruction(), select.getTarget());
        line.setDefaultCase(defaultCase);
    }
    //now, create the graph.
    line.setCases(cases);
    //now, create the graph.
    igc.getGraph().addVertex(line);
    if (line.getDefaultCase() != null) {
        CaseIntermediate si = new CaseIntermediate(line.getInstruction(), line.getDefaultCase());
        igc.getGraph().addVertex(si);
        //add an edge.
        igc.getGraph().addEdge(line, si);
        //add edge from outcome to edge.
        LOG.debug(si);
        AbstractIntermediate target = ilc.getNext(line.getDefaultCase().getTarget().getPosition());
        LOG.debug("TargeT:" + target);
        igc.getGraph().addVertex(target);
        igc.getGraph().addEdge(si, target);
    }
    for (Case caseVal : line.getCases()) {
        CaseIntermediate si = new CaseIntermediate(line.getInstruction(), caseVal);
        igc.getGraph().addVertex(si);
        //add an edge.
        igc.getGraph().addEdge(line, si);
        //add edge from outcome to edge.
        AbstractIntermediate target = ilc.getNext(caseVal.getTarget().getPosition());
        igc.getGraph().addVertex(target);
        igc.getGraph().addEdge(si, target);
    }
}
Also used : AbstractIntermediate(org.candle.decompiler.intermediate.code.AbstractIntermediate) DefaultCase(org.candle.decompiler.intermediate.expression.DefaultCase) Select(org.apache.bcel.generic.Select) CaseIntermediate(org.candle.decompiler.intermediate.code.CaseIntermediate) Resolved(org.candle.decompiler.intermediate.expression.Resolved) InstructionHandle(org.apache.bcel.generic.InstructionHandle) HashSet(java.util.HashSet) Case(org.candle.decompiler.intermediate.expression.Case) DefaultCase(org.candle.decompiler.intermediate.expression.DefaultCase)

Example 2 with Select

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

the class InstructionGraphFactory method process.

public InstructionGraphContext process() {
    ListenableDirectedGraph<InstructionHandle, IntermediateEdge> instructionHandleGraph = new ListenableDirectedGraph<InstructionHandle, IntermediateEdge>(IntermediateEdge.class);
    InstructionGraphContext igc = new InstructionGraphContext(instructionHandleGraph);
    for (InstructionHandle instructionHandle : instructionList.getInstructionHandles()) {
        InstructionHandle iv = instructionHandle;
        instructionHandleGraph.addVertex(iv);
    }
    Iterator<InstructionHandle> iter = instructionList.iterator();
    while (iter.hasNext()) {
        InstructionHandle ih = iter.next();
        InstructionHandle sourceVertex = igc.getPositionMap().get(ih.getPosition());
        if (ih.getInstruction() instanceof Select) {
            for (InstructionHandle targetVertex : ((Select) ih.getInstruction()).getTargets()) {
                instructionHandleGraph.addEdge(sourceVertex, targetVertex);
            }
            InstructionHandle targetVertex = ((Select) ih.getInstruction()).getTarget();
            if (targetVertex != null) {
                instructionHandleGraph.addEdge(sourceVertex, targetVertex);
            }
        } else if (ih instanceof BranchHandle) {
            //if this is an unconditional branch, only add the branch between the instruction and it's target.
            InstructionHandle targetVertex = igc.getPositionMap().get(((BranchHandle) ih).getTarget().getPosition());
            instructionHandleGraph.addEdge(sourceVertex, targetVertex);
        }
        if (!(ih.getInstruction() instanceof UnconditionalBranch)) {
            if (ih.getNext() != null) {
                InstructionHandle targetVertex = igc.getPositionMap().get((ih).getNext().getPosition());
                instructionHandleGraph.addEdge(sourceVertex, targetVertex);
            }
        }
    }
    /*
		//HAPPY PATH
		Set<InstructionHandle> happy = new HashSet<InstructionHandle>();
		InstructionHandle root = createVertex(instructionList.getStart());
		BreadthFirstIterator<InstructionHandle, IntermediateEdge> bfi = new BreadthFirstIterator<InstructionHandle, IntermediateEdge>(instructionHandleGraph, root);
		while(bfi.hasNext()) {
			happy.add(bfi.next());
		}
		
		InstructionTryCatch itc = new InstructionTryCatch(igc, this.exceptions);
		
		
		NamedDirectedSubgraph happyPath = new NamedDirectedSubgraph("Main", instructionHandleGraph, happy, null);
		
		
		for(CodeExceptionGen ceg : exceptions) {
			Set<InstructionHandle> cegG = new HashSet<InstructionHandle>();
			System.out.println("X"+ceg.getStartPC() + " -> "+ceg.getEndPC()+ ", "+ceg.getHandlerPC());
			
			for(InstructionHandle iv : instructionHandleGraph.vertexSet()) {
				
				if(iv.getPosition() <= ceg.getEndPC().getPosition() && iv.getPosition() >= ceg.getStartPC().getPosition()) {
					System.out.println("Position : "+iv.getPosition() );
					cegG.add(iv);
				}
			}
			
			subs.add(new NamedDirectedSubgraph("ExceptionHandler"+ceg.getCatchType().toString(), instructionHandleGraph, cegG, null));
		}
		
		*/
    return igc;
}
Also used : UnconditionalBranch(org.apache.bcel.generic.UnconditionalBranch) Select(org.apache.bcel.generic.Select) BranchHandle(org.apache.bcel.generic.BranchHandle) ListenableDirectedGraph(org.jgrapht.graph.ListenableDirectedGraph) IntermediateEdge(org.candle.decompiler.intermediate.graph.edge.IntermediateEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 3 with Select

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

the class InstructionInterpreter method getInEdges.

private List<Edge> getInEdges(InstructionList il, InstructionHandle ih) {
    List<Edge> edges = new LinkedList<Edge>();
    InstructionHandle prev = ih.getPrev();
    if (prev != null) {
        // check if we can fall through from prev instruction
        Instruction instr = prev.getInstruction();
        if (!(instr instanceof UnconditionalBranch || instr instanceof Select || instr instanceof ReturnInstruction)) {
            if (instr instanceof BranchInstruction) {
                edges.add(new Edge(prev, ih, EdgeType.FALSE_EDGE));
            } else {
                edges.add(new Edge(prev, ih, EdgeType.NORMAL_EDGE));
            }
        }
    }
    // flow graph explicitly
    for (InstructionHandle targeter = il.getStart(); targeter != null; targeter = targeter.getNext()) {
        //      for the instruction for the exception handler
        if (!(targeter.getInstruction() instanceof BranchInstruction))
            continue;
        BranchInstruction bi = (BranchInstruction) targeter.getInstruction();
        if (bi.containsTarget(ih)) {
            edges.add(new Edge(targeter, ih, EdgeType.TRUE_EDGE));
        }
    }
    return edges;
}
Also used : ReturnInstruction(org.apache.bcel.generic.ReturnInstruction) UnconditionalBranch(org.apache.bcel.generic.UnconditionalBranch) BranchInstruction(org.apache.bcel.generic.BranchInstruction) Select(org.apache.bcel.generic.Select) BranchInstruction(org.apache.bcel.generic.BranchInstruction) Instruction(org.apache.bcel.generic.Instruction) ReturnInstruction(org.apache.bcel.generic.ReturnInstruction) LinkedList(java.util.LinkedList) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Example 4 with Select

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

the class InstructionInterpreter method getOutEdges.

private List<Edge> getOutEdges(InstructionHandle ih) {
    List<Edge> edges = new LinkedList<Edge>();
    Instruction instr = ih.getInstruction();
    if (instr instanceof BranchInstruction) {
        if (instr instanceof Select) {
            Select s = (Select) instr;
            InstructionHandle[] target = s.getTargets();
            for (InstructionHandle aTarget : target) {
                edges.add(new Edge(ih, aTarget, EdgeType.TRUE_EDGE));
            }
            edges.add(new Edge(ih, s.getTarget(), EdgeType.FALSE_EDGE));
        } else {
            BranchInstruction b = (BranchInstruction) instr;
            edges.add(new Edge(ih, b.getTarget(), EdgeType.TRUE_EDGE));
        }
    }
    // Check if we can fall through to the next instruction
    if (ih.getNext() != null && !(instr instanceof UnconditionalBranch || instr instanceof Select || instr instanceof ReturnInstruction)) {
        if (instr instanceof BranchInstruction) {
            edges.add(new Edge(ih, ih.getNext(), EdgeType.FALSE_EDGE));
        } else {
            edges.add(new Edge(ih, ih.getNext(), EdgeType.NORMAL_EDGE));
        }
    }
    if (instr instanceof ReturnInstruction) {
        edges.add(new Edge(ih, getExitInstruction(), EdgeType.EXIT_EDGE));
    }
    if (instr instanceof ATHROW) {
    // TODO should we handle this somehow? Insert edges to the exception handlers or to an return-by-exception
    //      exit instruction?
    // for now, just ignore them
    }
    //      but for now, we just ignore them too.. in a safe way :)
    if (instr instanceof RET || instr instanceof JSR || instr instanceof JSR_W) {
        throw new JavaClassFormatError("Unsupported instruction " + instr + " in " + methodInfo);
    }
    return edges;
}
Also used : RET(org.apache.bcel.generic.RET) UnconditionalBranch(org.apache.bcel.generic.UnconditionalBranch) BranchInstruction(org.apache.bcel.generic.BranchInstruction) JSR(org.apache.bcel.generic.JSR) ATHROW(org.apache.bcel.generic.ATHROW) BranchInstruction(org.apache.bcel.generic.BranchInstruction) Instruction(org.apache.bcel.generic.Instruction) ReturnInstruction(org.apache.bcel.generic.ReturnInstruction) LinkedList(java.util.LinkedList) InstructionHandle(org.apache.bcel.generic.InstructionHandle) ReturnInstruction(org.apache.bcel.generic.ReturnInstruction) JavaClassFormatError(com.jopdesign.common.misc.JavaClassFormatError) Select(org.apache.bcel.generic.Select) JSR_W(org.apache.bcel.generic.JSR_W)

Example 5 with Select

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

the class DFATool method loadMethod.

private void loadMethod(MethodInfo method) {
    MethodCode mcode = method.getCode();
    // TODO is there a better way to get an instruction handle? Do we need to keep the list somehow?
    InstructionHandle exit;
    exit = (InstructionHandle) method.getCustomValue(KEY_NOP);
    // we reuse the NOP if it exists, so that we can have multiple DFAs running at the same time
    if (exit == null) {
        exit = new InstructionList(new NOP()).getStart();
        // We do not really want to modify the REAL instruction list and append exit
        // (SH) Fixed :) Yep, we need the NOP somewhere, else doInvoke() will collect the wrong result state.
        //      But we can eliminate the NOP by adding the instruction not to the list, but instead to the
        //      MethodInfo, and also retrieve it from there.
        method.setCustomValue(KEY_NOP, exit);
    }
    this.getStatements().add(exit);
    // We do not modify the code, so we leave existing CFGs alone, just make sure the instruction list is uptodate
    InstructionList il = mcode.getInstructionList(true, false);
    // we need correct positions for the DFA cache serialization stuff
    il.setPositions();
    for (Iterator<?> l = il.iterator(); l.hasNext(); ) {
        InstructionHandle handle = (InstructionHandle) l.next();
        this.getStatements().add(handle);
        Instruction instr = handle.getInstruction();
        if (instr instanceof BranchInstruction) {
            if (instr instanceof Select) {
                Select s = (Select) instr;
                InstructionHandle[] target = s.getTargets();
                for (InstructionHandle aTarget : target) {
                    this.getFlow().addEdge(new FlowEdge(handle, aTarget, FlowEdge.TRUE_EDGE));
                }
                this.getFlow().addEdge(new FlowEdge(handle, s.getTarget(), FlowEdge.FALSE_EDGE));
            } else {
                BranchInstruction b = (BranchInstruction) instr;
                this.getFlow().addEdge(new FlowEdge(handle, b.getTarget(), FlowEdge.TRUE_EDGE));
            }
        }
        if (handle.getNext() != null && !(instr instanceof UnconditionalBranch || instr instanceof Select || instr instanceof ReturnInstruction)) {
            if (instr instanceof BranchInstruction) {
                this.getFlow().addEdge(new FlowEdge(handle, handle.getNext(), FlowEdge.FALSE_EDGE));
            } else {
                this.getFlow().addEdge(new FlowEdge(handle, handle.getNext(), FlowEdge.NORMAL_EDGE));
            }
        }
        if (instr instanceof ReturnInstruction) {
            this.getFlow().addEdge(new FlowEdge(handle, exit, FlowEdge.NORMAL_EDGE));
        }
    }
}
Also used : FlowEdge(com.jopdesign.dfa.framework.FlowEdge) ReturnInstruction(org.apache.bcel.generic.ReturnInstruction) UnconditionalBranch(org.apache.bcel.generic.UnconditionalBranch) BranchInstruction(org.apache.bcel.generic.BranchInstruction) InstructionList(org.apache.bcel.generic.InstructionList) Select(org.apache.bcel.generic.Select) MethodCode(com.jopdesign.common.MethodCode) Instruction(org.apache.bcel.generic.Instruction) BranchInstruction(org.apache.bcel.generic.BranchInstruction) ReturnInstruction(org.apache.bcel.generic.ReturnInstruction) InstructionHandle(org.apache.bcel.generic.InstructionHandle) NOP(org.apache.bcel.generic.NOP)

Aggregations

InstructionHandle (org.apache.bcel.generic.InstructionHandle)5 Select (org.apache.bcel.generic.Select)5 UnconditionalBranch (org.apache.bcel.generic.UnconditionalBranch)4 BranchInstruction (org.apache.bcel.generic.BranchInstruction)3 Instruction (org.apache.bcel.generic.Instruction)3 ReturnInstruction (org.apache.bcel.generic.ReturnInstruction)3 LinkedList (java.util.LinkedList)2 MethodCode (com.jopdesign.common.MethodCode)1 JavaClassFormatError (com.jopdesign.common.misc.JavaClassFormatError)1 FlowEdge (com.jopdesign.dfa.framework.FlowEdge)1 HashSet (java.util.HashSet)1 ATHROW (org.apache.bcel.generic.ATHROW)1 BranchHandle (org.apache.bcel.generic.BranchHandle)1 InstructionList (org.apache.bcel.generic.InstructionList)1 JSR (org.apache.bcel.generic.JSR)1 JSR_W (org.apache.bcel.generic.JSR_W)1 NOP (org.apache.bcel.generic.NOP)1 RET (org.apache.bcel.generic.RET)1 AbstractIntermediate (org.candle.decompiler.intermediate.code.AbstractIntermediate)1 CaseIntermediate (org.candle.decompiler.intermediate.code.CaseIntermediate)1