2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.jvm.bytecode;
20 import gov.nasa.jpf.vm.Instruction;
21 import gov.nasa.jpf.vm.BooleanChoiceGenerator;
22 import gov.nasa.jpf.vm.KernelState;
23 import gov.nasa.jpf.vm.MethodInfo;
24 import gov.nasa.jpf.vm.StackFrame;
25 import gov.nasa.jpf.vm.SystemState;
26 import gov.nasa.jpf.vm.ThreadInfo;
29 * abstraction for all comparison instructions
31 public abstract class IfInstruction extends Instruction implements JVMInstruction {
32 protected int targetPosition; // insn position at jump insnIndex
33 protected Instruction target; // jump target
35 protected boolean conditionValue; /** value of last evaluation of branch condition */
37 protected IfInstruction(int targetPosition){
38 this.targetPosition = targetPosition;
42 * return which branch was taken. Only useful after instruction got executed
43 * WATCH OUT - 'true' means the jump condition is met, which logically is
46 public boolean getConditionValue() {
47 return conditionValue;
51 * Added so that SimpleIdleFilter can detect do-while loops when
52 * the while statement evaluates to true.
55 public boolean isBackJump () {
56 return (conditionValue) && (targetPosition <= position);
60 * retrieve value of jump condition from operand stack
61 * (not ideal to have this public, but some listeners might need it for
62 * skipping the insn, plus we require it for subclass factorization)
64 public abstract boolean popConditionValue(StackFrame frame);
66 public Instruction getTarget() {
68 target = mi.getInstructionAt(targetPosition);
74 public Instruction execute (ThreadInfo ti) {
75 StackFrame frame = ti.getModifiableTopFrame();
77 conditionValue = popConditionValue(frame);
86 * use this as a delegatee in overridden executes of derived IfInstructions
87 * (e.g. for symbolic execution)
89 protected Instruction executeBothBranches (SystemState ss, KernelState ks, ThreadInfo ti){
90 if (!ti.isFirstStepInsn()) {
91 BooleanChoiceGenerator cg = new BooleanChoiceGenerator(ti.getVM().getConfig(), "ifAll");
92 if (ss.setNextChoiceGenerator(cg)){
96 StackFrame frame = ti.getModifiableTopFrame();
97 // some listener did override the CG, fallback to normal operation
98 conditionValue = popConditionValue(frame);
107 BooleanChoiceGenerator cg = ss.getCurrentChoiceGenerator("ifAll", BooleanChoiceGenerator.class);
108 assert (cg != null) : "no BooleanChoiceGenerator";
110 StackFrame frame = ti.getModifiableTopFrame();
111 popConditionValue(frame); // we are not interested in concrete values
113 conditionValue = cg.getNextChoice();
115 if (conditionValue) {
125 public String toString () {
126 return getMnemonic() + " " + targetPosition;
130 public int getLength() {
131 return 3; // usually opcode, bb1, bb2
135 public void accept(JVMInstructionVisitor insVisitor) {
136 insVisitor.visit(this);
140 public Instruction typeSafeClone(MethodInfo mi) {
141 IfInstruction clone = null;
144 clone = (IfInstruction) super.clone();
146 // reset the method that this insn belongs to
150 } catch (CloneNotSupportedException e) {