Search in sources :

Example 1 with InstructionTargeter

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

the class InstructionInterpreter method interpret.

private void interpret(InstructionList il, Map<InstructionHandle, T> start, boolean initialize) {
    if (initialize) {
        InstructionHandle ih = il.getStart();
        // set initial value for all instructions
        while (ih != null) {
            results.put(ih, analysis.bottom());
            ih = ih.getNext();
        }
        results.putAll(start);
    } else {
        // Set initial values for new instructions
        for (InstructionHandle ih = il.getStart(); ih != null; ih = ih.getNext()) {
            if (results.containsKey(ih))
                continue;
            if (ih.getPrev() == null) {
                // entry instruction, must always be merged with the initial state once, even
                // if this has an ingoing edge
                results.put(ih, analysis.initial(ih));
            } else {
                results.put(ih, analysis.bottom());
                // check for exception handler entry
                if (ih.hasTargeters()) {
                    for (InstructionTargeter targeter : ih.getTargeters()) {
                        if (targeter instanceof CodeExceptionGen && ((CodeExceptionGen) targeter).getHandlerPC().equals(ih)) {
                            results.put(ih, analysis.initial((CodeExceptionGen) targeter));
                            break;
                        }
                    }
                }
            }
        }
    }
    LinkedList<Edge> worklist = new LinkedList<Edge>();
    // setup the worklist
    for (InstructionHandle ih : start.keySet()) {
        if (initialize) {
            // when initializing, we start from the initial values, not from ingoing edges
            worklist.addAll(getOutEdges(ih));
        } else {
            // we continue from existing results
            List<Edge> inEdges = getInEdges(il, ih);
            if (inEdges.isEmpty()) {
                // entry instruction without ingoing edges, nothing to continue from
                worklist.addAll(getOutEdges(ih));
            } else {
                worklist.addAll(inEdges);
            }
        }
    }
    while (!worklist.isEmpty()) {
        Edge edge = worklist.removeFirst();
        InstructionHandle tail = edge.getTail();
        InstructionHandle head = edge.getHead();
        T tailValue = results.get(tail);
        T headValue = results.get(head);
        T transferred = analysis.transfer(tailValue, edge);
        if (!analysis.compare(transferred, headValue)) {
            T newValue = analysis.join(transferred, headValue);
            results.put(head, newValue);
            if (edge.getType() == EdgeType.EXIT_EDGE) {
                continue;
            }
            List<Edge> outEdges = getOutEdges(head);
            for (Edge outEdge : outEdges) {
                if (worklist.contains(outEdge)) {
                    continue;
                }
                if (outEdges.size() > 1) {
                    worklist.addLast(outEdge);
                } else {
                    worklist.addFirst(outEdge);
                }
            }
        }
    }
}
Also used : RET(org.apache.bcel.generic.RET) InstructionTargeter(org.apache.bcel.generic.InstructionTargeter) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen) InstructionHandle(org.apache.bcel.generic.InstructionHandle) LinkedList(java.util.LinkedList)

Example 2 with InstructionTargeter

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

the class MethodCode method getEndingExceptionRanges.

public List<CodeExceptionGen> getEndingExceptionRanges(InstructionHandle ih) {
    List<CodeExceptionGen> list = new ArrayList<CodeExceptionGen>();
    InstructionTargeter[] targeters = ih.getTargeters();
    if (targeters == null)
        return list;
    for (InstructionTargeter t : targeters) {
        if (t instanceof CodeExceptionGen) {
            CodeExceptionGen ceg = (CodeExceptionGen) t;
            if (ceg.getEndPC().equals(ih)) {
                list.add(ceg);
            }
        }
    }
    return list;
}
Also used : InstructionTargeter(org.apache.bcel.generic.InstructionTargeter) ArrayList(java.util.ArrayList) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen)

Example 3 with InstructionTargeter

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

the class MethodCode method getStartingExceptionRanges.

/**
     * Get a list of all exception ranges which start at this instruction.
     * @param ih the instruction to check
     * @return a list of all exception ranges with this instruction as start instruction, or an empty list.
     */
public List<CodeExceptionGen> getStartingExceptionRanges(InstructionHandle ih) {
    List<CodeExceptionGen> list = new ArrayList<CodeExceptionGen>();
    InstructionTargeter[] targeters = ih.getTargeters();
    if (targeters == null)
        return list;
    for (InstructionTargeter t : targeters) {
        if (t instanceof CodeExceptionGen) {
            CodeExceptionGen ceg = (CodeExceptionGen) t;
            if (ceg.getStartPC().equals(ih)) {
                list.add(ceg);
            }
        }
    }
    return list;
}
Also used : InstructionTargeter(org.apache.bcel.generic.InstructionTargeter) ArrayList(java.util.ArrayList) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen)

Example 4 with InstructionTargeter

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

the class MethodCode method getTargetingExceptionRanges.

public List<CodeExceptionGen> getTargetingExceptionRanges(InstructionHandle ih) {
    List<CodeExceptionGen> list = new ArrayList<CodeExceptionGen>();
    InstructionTargeter[] targeters = ih.getTargeters();
    if (targeters == null)
        return list;
    for (InstructionTargeter t : targeters) {
        if (t instanceof CodeExceptionGen) {
            CodeExceptionGen ceg = (CodeExceptionGen) t;
            if (ceg.getHandlerPC().equals(ih)) {
                list.add(ceg);
            }
        }
    }
    return list;
}
Also used : InstructionTargeter(org.apache.bcel.generic.InstructionTargeter) ArrayList(java.util.ArrayList) CodeExceptionGen(org.apache.bcel.generic.CodeExceptionGen)

Example 5 with InstructionTargeter

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

the class PeepholeOptimizer method optimizeBoolExpressions.

/**
     * Optimize some boolean expressions.
     * <p>
     * This code is taken directly from the BCEL manual, chapter 3.3.8.
     * </p>
     *
     * @param il the instruction list to optimize.
     */
private void optimizeBoolExpressions(InstructionList il) {
    InstructionFinder f = new InstructionFinder(il);
    String pat = "IfInstruction ICONST_0 GOTO ICONST_1 NOP(IFEQ|IFNE)";
    CodeConstraint constraint = new CodeConstraint() {

        @Override
        public boolean checkCode(InstructionHandle[] match) {
            IfInstruction if1 = (IfInstruction) match[0].getInstruction();
            GOTO g = (GOTO) match[2].getInstruction();
            return (if1.getTarget() == match[3]) && (g.getTarget() == match[4]);
        }
    };
    for (Iterator e = f.search(pat, constraint); e.hasNext(); ) {
        InstructionHandle[] match = (InstructionHandle[]) e.next();
        IfInstruction if1 = (IfInstruction) match[0].getInstruction();
        IfInstruction if2 = (IfInstruction) match[5].getInstruction();
        // Update target
        if1.setTarget(if2.getTarget());
        try {
            il.delete(match[1], match[5]);
        } catch (TargetLostException ex) {
            for (InstructionHandle target : ex.getTargets()) {
                for (InstructionTargeter t : target.getTargeters()) {
                    t.updateTarget(target, match[0]);
                }
            }
        }
        matchBoolExpressions++;
    }
}
Also used : GOTO(org.apache.bcel.generic.GOTO) InstructionTargeter(org.apache.bcel.generic.InstructionTargeter) IfInstruction(org.apache.bcel.generic.IfInstruction) Iterator(java.util.Iterator) CodeConstraint(org.apache.bcel.util.InstructionFinder.CodeConstraint) InstructionFinder(org.apache.bcel.util.InstructionFinder) TargetLostException(org.apache.bcel.generic.TargetLostException) InstructionHandle(org.apache.bcel.generic.InstructionHandle)

Aggregations

InstructionTargeter (org.apache.bcel.generic.InstructionTargeter)5 CodeExceptionGen (org.apache.bcel.generic.CodeExceptionGen)4 ArrayList (java.util.ArrayList)3 InstructionHandle (org.apache.bcel.generic.InstructionHandle)2 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 GOTO (org.apache.bcel.generic.GOTO)1 IfInstruction (org.apache.bcel.generic.IfInstruction)1 RET (org.apache.bcel.generic.RET)1 TargetLostException (org.apache.bcel.generic.TargetLostException)1 InstructionFinder (org.apache.bcel.util.InstructionFinder)1 CodeConstraint (org.apache.bcel.util.InstructionFinder.CodeConstraint)1