1 package gov.nasa.jpf.tools; 20 21 import gov.nasa.jpf.jvm.Types; 22 23 import java.io.PrintWriter ; 24 25 import java.lang.reflect.*; 26 27 import java.util.ArrayList ; 28 29 30 35 public class GenPeer { 36 static final String SYS_PKG = "gov.nasa.jpf.jvm"; 37 static final String MJI_ENV = "gov.nasa.jpf.jvm.MJIEnv"; 38 static final String INDENT = " "; 39 static final String METHOD_PREFIX = "public static"; 40 static final String ENV_ARG = "MJIEnv env"; 41 static final String OBJ_ARG = "int robj"; 42 static final String CLS_ARG = "int rcls"; 43 static final String REF_TYPE = "int"; 44 static final String NULL = "MJIEnv.NULL"; 45 static final String EXEC_COND = "$isExecutable_"; 46 static final String DETERM_COND = "$isDeterministic_"; 47 static String clsName; 48 static String [] mths; 49 50 static boolean isSystemPkg; 52 static boolean allMethods; 53 static boolean mangleNames; 54 static boolean clinit; 55 static boolean execCond; 56 static boolean determCond; 57 58 public static void main (String [] args) { 59 if ((args.length == 0) || !readOptions(args)) { 60 showUsage(); 61 62 return; 63 } 64 65 PrintWriter pw = new PrintWriter (System.out, true); 66 Class cls = getClass(clsName); 67 68 if (cls != null) { 69 printNativePeer(cls, pw); 70 } 71 } 72 73 static Class getClass (String cname) { 74 Class clazz = null; 75 76 try { 77 clazz = Class.forName(cname); 78 } catch (ClassNotFoundException cnfx) { 79 System.err.println("target class not found: " + cname); 80 } catch (Throwable x) { 81 x.printStackTrace(); 82 } 83 84 return clazz; 85 } 86 87 static boolean isMJICandidate (Method m) { 88 if (allMethods) { 89 return true; 90 } 91 92 if (mths != null) { 93 String name = m.getName(); 94 95 for (int i = 0; i < mths.length; i++) { 96 if (name.equals(mths[i])) { 97 return true; 98 } 99 } 100 } else { 101 if ((m.getModifiers() & Modifier.NATIVE) != 0) { 102 return true; 103 } 104 } 105 106 return false; 107 } 108 109 static void getMangledName (Method m) { 110 StringBuffer sb = new StringBuffer (50); 111 112 sb.append(m.getName()); 113 sb.append("__"); 114 } 115 116 static boolean isPrimitiveType (String t) { 117 return ("int".equals(t) || "long".equals(t) || "boolean".equals(t) || 118 "void".equals(t) || "byte".equals(t) || "char".equals(t) || "short".equals(t) || 120 "float".equals(t) || "double".equals(t)); 121 } 122 123 static void printClinit (PrintWriter pw) { 124 pw.print(INDENT); 125 pw.print(METHOD_PREFIX); 126 pw.print(" void $clinit ("); 127 pw.print(ENV_ARG); 128 pw.print(", "); 129 pw.print(CLS_ARG); 130 pw.println(") {"); 131 pw.print(INDENT); 132 pw.println("}"); 133 } 134 135 static void printFooter (Class cls, PrintWriter pw) { 136 pw.println("}"); 137 } 138 139 static void printHeader (Class cls, PrintWriter pw) { 140 if (isSystemPkg) { 141 pw.print("package "); 142 pw.print(SYS_PKG); 143 pw.println(';'); 144 pw.println(); 145 146 pw.print("import "); 147 pw.print(MJI_ENV); 148 pw.println(";"); 149 pw.println(); 150 } 151 152 String cname = cls.getName().replace('.', '_'); 153 154 pw.print("class "); 155 pw.print("JPF_"); 156 pw.print(cname); 157 pw.println(" {"); 158 } 159 160 static void printMethodBody (String rt, String t, PrintWriter pw) { 161 if (!"void".equals(rt)) { 162 pw.print(INDENT); 163 pw.print(INDENT); 164 pw.print(rt); 165 166 if ((rt == REF_TYPE) && (rt != t)) { 167 pw.print(" r"); 168 pw.print(t); 169 pw.print(" = "); 170 pw.print(NULL); 171 pw.println(";"); 172 173 pw.print(INDENT); 174 pw.print(INDENT); 175 pw.print("return r"); 176 pw.print(t); 177 pw.println(";"); 178 } else { 179 pw.print(" v = ("); 180 pw.print(rt); 181 pw.println(")0;"); 182 183 pw.print(INDENT); 184 pw.print(INDENT); 185 pw.println("return v;"); 186 } 187 } 188 } 189 190 static void printMethodName (Method m, PrintWriter pw) { 191 String name = null; 192 193 if (mangleNames) { 194 name = Types.getJNIMangledMethodName(m); 195 } else { 196 name = m.getName(); 197 } 198 199 pw.print(name); 200 } 201 202 static void printMethodStub (String condPrefix, Method m, PrintWriter pw) { 203 String t = null; 204 String rt; 205 206 pw.print(INDENT); 207 pw.print(METHOD_PREFIX); 208 pw.print(' '); 209 210 if (condPrefix == null) { 211 t = rt = stripType(m.getReturnType().getName()); 212 213 if (!isPrimitiveType(rt)) { 214 rt = REF_TYPE; 215 } 216 } else { 217 rt = "boolean"; 218 } 219 220 pw.print(rt); 221 222 pw.print(' '); 223 224 if (condPrefix != null) { 225 pw.print(condPrefix); 226 } 227 228 printMethodName(m, pw); 229 pw.print(" ("); 230 231 printStdArgs(m, pw); 232 printTargetArgs(m, pw); 233 234 pw.println(") {"); 235 236 if (condPrefix == null) { 237 printMethodBody(rt, stripType(null, t), pw); 238 } else { 239 pw.print(INDENT); 240 pw.print(INDENT); 241 pw.println("return true;"); 242 } 243 244 pw.print(INDENT); 245 pw.println('}'); 246 } 247 248 static void printNativePeer (Class cls, PrintWriter pw) { 249 Method[] mths = cls.getDeclaredMethods(); 250 251 printHeader(cls, pw); 252 253 if (clinit) { 254 printClinit(pw); 255 } 256 257 for (int i = 0; i < mths.length; i++) { 258 Method m = mths[i]; 259 260 if (isMJICandidate(m)) { 261 if (determCond) { 262 pw.println(); 263 printMethodStub(DETERM_COND, m, pw); 264 } 265 266 if (execCond) { 267 pw.println(); 268 printMethodStub(EXEC_COND, m, pw); 269 } 270 271 pw.println(); 272 printMethodStub(null, m, pw); 273 } 274 } 275 276 printFooter(cls, pw); 277 } 278 279 static void printStdArgs (Method m, PrintWriter pw) { 280 pw.print(ENV_ARG); 281 pw.print(", "); 282 283 if ((m.getModifiers() & Modifier.STATIC) != 0) { 284 pw.print(CLS_ARG); 285 } else { 286 pw.print(OBJ_ARG); 287 } 288 } 289 290 static void printTargetArgs (Method m, PrintWriter pw) { 291 Class [] pt = m.getParameterTypes(); 292 293 for (int i = 0; i < pt.length; i++) { 294 String t = stripType(pt[i].getName()); 295 boolean isPrim = isPrimitiveType(t); 296 297 pw.print(", "); 298 299 if (isPrim) { 300 pw.print(t); 301 pw.print(" v"); 302 pw.print(i); 303 } else { 304 pw.print(REF_TYPE); 305 pw.print(" r"); 306 pw.print(stripType(null, t)); 307 pw.print(i); 308 } 309 } 310 } 311 312 static String [] readNames (String [] args, int i) { 313 ArrayList a = null; 314 315 for (; (i < args.length) && (args[i].charAt(0) != '-'); i++) { 316 if (a == null) { 317 a = new ArrayList (); 318 } 319 320 a.add(args[i]); 321 } 322 323 if (a != null) { 324 String [] names = new String [a.size()]; 325 a.toArray(names); 326 327 return names; 328 } 329 330 return null; 331 } 332 333 static boolean readOptions (String [] args) { 334 for (int i = 0; i < args.length; i++) { 335 String arg = args[i]; 336 337 if ("-s".equals(arg)) { 338 isSystemPkg = true; 339 } else if ("-m".equals(arg)) { 340 mangleNames = true; 341 } else if ("-a".equals(arg)) { 342 allMethods = true; 343 } else if ("-ci".equals(arg)) { 344 clinit = true; 345 } else if ("-dc".equals(arg)) { 346 determCond = true; 347 } else if ("-ec".equals(arg)) { 348 execCond = true; 349 } else if (arg.charAt(0) != '-') { 350 if (clsName == null) { 352 clsName = arg; 353 } else { 354 mths = readNames(args, i); 355 i += mths.length; 356 } 357 } else { 358 System.err.println("unknown option: " + arg); 359 showUsage(); 360 361 return false; 362 } 363 } 364 365 return (clsName != null); 366 } 367 368 static void showUsage () { 369 System.out.println( 370 "usage: 'GenPeer [<option>..] <className> [<method>..]'"); 371 System.out.println("options: -s : system peer class (gov.nasa.jpf.jvm)"); 372 System.out.println(" -ci : create <clinit> MJI method"); 373 System.out.println(" -m : create mangled method names"); 374 System.out.println( 375 " -a : create MJI methods for all target class methods"); 376 System.out.println( 377 " -dc : create isDeterministic condition methods"); 378 System.out.println(" -de : create isExecutable condition methods"); 379 System.out.println(" -nd : mark methods as non-deterministic"); 380 } 381 382 static String stripType (String s) { 383 return stripType("java.lang", s); 384 } 385 386 static String stripType (String prefix, String s) { 387 int i = s.lastIndexOf('.') + 1; 388 int l = s.length() - 1; 389 390 if (s.charAt(l) == ';') { 391 s = s.substring(0, l); 392 } 393 394 if (prefix == null) { 395 if (i == 0) { 396 return s; 397 } else { 398 return s.substring(i); 399 } 400 } else { 401 if (s.startsWith(prefix) && (prefix.length() + 1 == i)) { 402 return s.substring(i); 403 } else { 404 return s; 405 } 406 } 407 } 408 } | Popular Tags |