Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.IfInstruction


             ih != null;
             ih = ih.getNext()) {
            Instruction inst = ih.getInstruction();

            if (inst instanceof IfInstruction) {
                IfInstruction oldIfInst = (IfInstruction)inst;
                BranchHandle oldIfHandle = (BranchHandle)ih;
                InstructionHandle target = oldIfInst.getTarget();
                int relativeTargetOffset = target.getPosition()
                                               - oldIfHandle.getPosition();

                // Consider the worst case scenario in which the conditional
                // branch and its target are separated by all the instructions
                // in the method that might increase in size.  If that results
                // in a relative offset that cannot be represented as a 32-bit
                // signed quantity, rewrite the instruction as described above.
                if ((relativeTargetOffset - maxOffsetChange
                             < MIN_BRANCH_TARGET_OFFSET)
                        || (relativeTargetOffset + maxOffsetChange
                                    > MAX_BRANCH_TARGET_OFFSET)) {
                    // Invert the logic of the IF instruction, and append
                    // that to the InstructionList following the original IF
                    // instruction
                    InstructionHandle nextHandle = oldIfHandle.getNext();
                    IfInstruction invertedIfInst = oldIfInst.negate();
                    BranchHandle invertedIfHandle = il.append(oldIfHandle,
                                                              invertedIfInst);

                    // Append an unconditional branch to the target of the
                    // original IF instruction after the new IF instruction
View Full Code Here


             ih != null;
             ih = ih.getNext()) {
            Instruction inst = ih.getInstruction();

            if (inst instanceof IfInstruction) {
                IfInstruction oldIfInst = (IfInstruction)inst;
                BranchHandle oldIfHandle = (BranchHandle)ih;
                InstructionHandle target = oldIfInst.getTarget();
                int relativeTargetOffset = target.getPosition()
                                               - oldIfHandle.getPosition();

                // Consider the worst case scenario in which the conditional
                // branch and its target are separated by all the instructions
                // in the method that might increase in size.  If that results
                // in a relative offset that cannot be represented as a 32-bit
                // signed quantity, rewrite the instruction as described above.
                if ((relativeTargetOffset - maxOffsetChange
                             < MIN_BRANCH_TARGET_OFFSET)
                        || (relativeTargetOffset + maxOffsetChange
                                    > MAX_BRANCH_TARGET_OFFSET)) {
                    // Invert the logic of the IF instruction, and append
                    // that to the InstructionList following the original IF
                    // instruction
                    InstructionHandle nextHandle = oldIfHandle.getNext();
                    IfInstruction invertedIfInst = oldIfInst.negate();
                    BranchHandle invertedIfHandle = il.append(oldIfHandle,
                                                              invertedIfInst);

                    // Append an unconditional branch to the target of the
                    // original IF instruction after the new IF instruction
View Full Code Here

                Location branch = findLocation(cfg, getPC() + 1);
                if (produced == null || branch == null) {
                    return;
                }

                IfInstruction branchInstruction = (IfInstruction) branch.getHandle().getInstruction();

                IsNullValueDataflow isNullValueDataflow = getClassContext().getIsNullValueDataflow(getMethod());
                ValueNumberDataflow valueNumberDataflow = getClassContext().getValueNumberDataflow(getMethod());
                UnconditionalValueDerefDataflow unconditionalValueDerefDataflow = getClassContext()
                        .getUnconditionalValueDerefDataflow(getMethod());
                ValueNumberFrame valueNumberFact = valueNumberDataflow.getFactAtLocation(produced);
                IsNullValueFrame isNullFact = isNullValueDataflow.getFactAtLocation(produced);
                ValueNumber value = valueNumberFact.getTopValue();
                if (isNullFact.getTopValue().isDefinitelyNotNull()) {
                    return;
                }
                if (DEBUG) {
                    System.out.println("Produced: " + produced);
                    System.out.println(valueNumberFact);
                    System.out.println(isNullFact);
                    System.out.println("value: " + value);
                    System.out.println("branch: " + branch);
                    System.out.println("instruction: " + branchInstruction);
                    System.out.println("target: " + branchInstruction.getTarget());
                    System.out.println("next: " + branch.getHandle().getNext());
                }
                Location guaranteed = findLocation(cfg, nullGuaranteesBranch ? branchInstruction.getTarget() : branch.getHandle()
                        .getNext());
                if (guaranteed == null) {
                    return;
                }
View Full Code Here

                    }

                }
            }
            if (i instanceof IfInstruction) {
                IfInstruction ii = (IfInstruction) i;
                InstructionHandle target = ii.getTarget();
                InstructionHandle next = head.getNext();
                if (target.equals(next)) {
                    int consumed = ii.consumeStack(methodGen.getConstantPool());
                    if (consumed != 1 && consumed != 2) {
                        throw new IllegalStateException();
                    }
                    head.swapInstruction(consumed == 1 ? new POP() : new POP2());
                }

            }
            if (i instanceof IFNULL || i instanceof IFNONNULL) {
                IfInstruction ii = (IfInstruction) i;
                InstructionHandle target = ii.getTarget();
                InstructionHandle next1 = head.getNext(); // ICONST
                if (next1 == null) {
                    break;
                }
                if (next1.getInstruction() instanceof ICONST) {
                    InstructionHandle next2 = next1.getNext(); // GOTO
                    if (next2 == null) {
                        break;
                    }
                    InstructionHandle next3 = next2.getNext(); // ICONST
                    if (next3== null) {
                        break;
                    }
                    InstructionHandle next4 = next3.getNext();
                    if (next4 == null) {
                        break;
                    }
                    if (target.equals(next3&& next2.getInstruction() instanceof GOTO
                            && next3.getInstruction() instanceof ICONST && next1.getTargeters().length == 0
                            && next2.getTargeters().length == 0 && next3.getTargeters().length == 1
                            && next4.getTargeters().length == 1) {
                        int c1 = ((ICONST) next1.getInstruction()).getValue().intValue();
                        GOTO g = (GOTO) next2.getInstruction();
                        int c2 = ((ICONST) next3.getInstruction()).getValue().intValue();
                        if (g.getTarget().equals(next4) && (c1 == 1 && c2 == 0 || c1 == 0 && c2 == 1)) {
                            boolean nullIsTrue = i instanceof IFNULL && c2 == 1 || i instanceof IFNONNULL && c2 == 0;

                            if (nullIsTrue) {
                                // System.out.println("Found NULL2Z instruction");
                                head.swapInstruction(new NULL2Z());

                            } else {
                                // System.out.println("Found NONNULL2Z instruction");
                                head.swapInstruction(new NONNULL2Z());
                            }
                            next3.removeAllTargeters();
                            next4.removeAllTargeters();
                            next1.swapInstruction(new NOP());
                            next2.swapInstruction(new NOP());
                            next3.swapInstruction(new NOP());
                        }
                    }
                }

            }
            if (i instanceof ACONST_NULL) {
                InstructionHandle next = head.getNext();
                assert next != null;
                InstructionHandle next2 = next.getNext();
                if (next2 != null && next.getInstruction() instanceof ALOAD) {
                    Instruction check = next2.getInstruction();
                    if (check instanceof IF_ACMPNE || check instanceof IF_ACMPEQ) {
                        // need to update
                        head.swapInstruction(new NOP());
                        IfInstruction ifTest = (IfInstruction) check;
                        if (check instanceof IF_ACMPNE) {
                            next2.swapInstruction(new IFNONNULL(ifTest.getTarget()));
                        } else {
                            next2.swapInstruction(new IFNULL(ifTest.getTarget()));
                        }
                    }
                }
            }
            head = head.getNext();
View Full Code Here

TOP

Related Classes of org.apache.bcel.generic.IfInstruction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.