1 package gov.nasa.jpf.jvm.bytecode; 20 21 import gov.nasa.jpf.JPFException; 22 import gov.nasa.jpf.jvm.ClassInfo; 23 import gov.nasa.jpf.jvm.JVM; 24 import gov.nasa.jpf.jvm.KernelState; 25 import gov.nasa.jpf.jvm.MethodInfo; 26 import gov.nasa.jpf.jvm.SystemState; 27 import gov.nasa.jpf.jvm.ThreadInfo; 28 import gov.nasa.jpf.util.Debug; 29 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 import org.apache.bcel.classfile.ConstantPool; 34 import org.apache.bcel.generic.InstructionHandle; 35 36 39 public abstract class Instruction { 40 protected static List Unimplemented = new ArrayList (); 41 protected int position; protected int offset; 44 49 protected MethodInfo mi; 50 protected String asString; 51 protected boolean isObservable; 52 53 abstract public int getByteCode(); 54 55 public void setContext (String className, String methodName, int lineNumber, 57 int offset) { 58 } 59 60 public boolean isFirstInstruction () { 61 return (offset == 0); 62 } 63 64 67 public boolean isBackJump () { 68 return false; 69 } 70 71 public boolean isDeterministic (SystemState ss, KernelState ks, ThreadInfo ti) { 72 return true; 73 } 74 75 public boolean isExecutable (SystemState ss, KernelState ks, ThreadInfo th) { 76 return true; 77 } 78 79 public MethodInfo getMethod () { 80 return mi; 81 } 82 83 84 public Instruction getNext () { 85 return mi.getInstruction(offset + 1); 86 } 87 88 public void setObservable () { 89 isObservable = true; 90 } 91 92 93 public boolean isObservable () { 94 if (!isObservable) { 95 Instruction prev = getPrev(); 96 97 if (prev != null) { 98 if (prev instanceof INVOKESTATIC) { 99 INVOKESTATIC invoke = (INVOKESTATIC) prev; 100 isObservable |= (invoke.cname.equals("gov.nasa.jpf.jvm.Verify") && invoke.mname.equals( 101 "assertTrue(Z)V")); 102 } 103 } 104 } 105 106 return isObservable; 107 } 108 109 public int getOffset () { 110 return offset; 111 } 112 113 public int getPosition () { 114 return position; 115 } 116 117 public Instruction getPrev () { 118 if (offset > 0) { 119 return mi.getInstruction(offset - 1); 120 } else { 121 return null; 122 } 123 } 124 125 public boolean isVisible (SystemState ss, KernelState ks, ThreadInfo th) { 127 return false; 128 } 129 130 public boolean isSchedulingRelevant(SystemState ss, KernelState ks, ThreadInfo ti) { 131 return false; 132 } 133 134 public abstract Instruction execute (SystemState ss, KernelState ks, 135 ThreadInfo th); 136 137 public static Instruction create (InstructionHandle h, int o, MethodInfo m, 138 ConstantPool cp) { 139 Instruction i = null; 140 String name = null; 141 142 try { 143 name = h.getInstruction().getClass().getName(); 144 145 if (!name.startsWith("org.apache.bcel.generic.")) { 146 throw new JPFException("not a BCEL instruction type: " + name); 147 } 148 149 name = "gov.nasa.jpf.jvm.bytecode." + name.substring(24); 150 Class clazz = Class.forName(name); 151 152 i = (gov.nasa.jpf.jvm.bytecode.Instruction) clazz.newInstance(); 153 i.init(h, o, m, cp); 154 } catch (ClassNotFoundException e) { 155 if (!Unimplemented.contains(name)) { 156 Unimplemented.add(name); 157 Debug.println(Debug.WARNING, 158 "warning: unimplemented bytecode instruction: " + 159 name.substring(34)); 160 } 161 } catch (Exception e) { 162 e.printStackTrace(); 163 throw new JPFException("creation of instruction " + 164 name.substring(34) + " failed"); 165 } 166 167 return i; 168 } 169 170 public boolean Methodexamine (ThreadInfo th) { 171 return false; } 173 174 public boolean examine (SystemState ss, KernelState ks, ThreadInfo th) { 175 return false; 176 } 177 178 public boolean examineAbstraction (SystemState ss, KernelState ks, 179 ThreadInfo th) { 180 return false; 181 } 182 183 public String toString () { 184 return asString; 185 } 186 187 public String getMnemonic () { 188 String s = getClass().getName(); 189 return s.substring(26); } 191 192 public String getSourceLocation () { 193 ClassInfo ci = mi.getClassInfo(); 194 int line = mi.getLineNumber(this); 195 String file = ci.getSourceFileName(); 196 197 String s = ci.getName() + '.' + mi.getFullName() + " at "; 198 199 if (file != null) { 200 s += file; 201 s += ':'; 202 s += line; 203 } else { 204 s += "pc "; 205 s += position; 206 } 207 208 return s; 209 } 210 211 protected abstract void setPeer (org.apache.bcel.generic.Instruction i, 213 ConstantPool cp); 214 215 protected void init (InstructionHandle h, int o, MethodInfo m, 216 ConstantPool cp) { 217 position = h.getPosition(); 218 offset = o; 219 mi = m; 220 asString = h.getInstruction().toString(cp); 221 setPeer(h.getInstruction(), cp); 222 isObservable = JVM.observablePositions.contains(mi.getCompleteName() + 223 ":" + position); 224 } 225 226 242 Instruction getNext (ThreadInfo th) { 243 Instruction insn = th.getPC(); 244 245 if (insn == null) { 246 insn = this; 248 } 249 250 return insn.getNext(); 251 } 252 } 253 254 255 | Popular Tags |