1 32 33 package com.jeantessier.classreader; 34 35 import java.util.*; 36 37 public class Instruction { 38 private static String [] opcode = new String [256]; 39 private static int[] length = new int[256]; 40 41 static { 42 opcode[0x00] = "nop"; 43 length[0x00] = 1; 44 opcode[0x01] = "aconst_null"; 45 length[0x01] = 1; 46 opcode[0x02] = "iconst_m1"; 47 length[0x02] = 1; 48 opcode[0x03] = "iconst_0"; 49 length[0x03] = 1; 50 opcode[0x04] = "iconst_1"; 51 length[0x04] = 1; 52 opcode[0x05] = "iconst_2"; 53 length[0x05] = 1; 54 opcode[0x06] = "iconst_3"; 55 length[0x06] = 1; 56 opcode[0x07] = "iconst_4"; 57 length[0x07] = 1; 58 opcode[0x08] = "iconst_5"; 59 length[0x08] = 1; 60 opcode[0x09] = "lconst_0"; 61 length[0x09] = 1; 62 opcode[0x0a] = "lconst_1"; 63 length[0x0a] = 1; 64 opcode[0x0b] = "fconst_0"; 65 length[0x0b] = 1; 66 opcode[0x0c] = "fconst_1"; 67 length[0x0c] = 1; 68 opcode[0x0d] = "fconst_2"; 69 length[0x0d] = 1; 70 opcode[0x0e] = "dconst_0"; 71 length[0x0e] = 1; 72 opcode[0x0f] = "dconst_1"; 73 length[0x0f] = 1; 74 75 opcode[0x10] = "bipush"; 76 length[0x10] = 2; 77 opcode[0x11] = "sipush"; 78 length[0x11] = 3; 79 opcode[0x12] = "ldc"; 80 length[0x12] = 2; 81 opcode[0x13] = "ldc_w"; 82 length[0x13] = 3; 83 opcode[0x14] = "ldc2_w"; 84 length[0x14] = 3; 85 opcode[0x15] = "iload"; 86 length[0x15] = 2; 87 opcode[0x16] = "lload"; 88 length[0x16] = 2; 89 opcode[0x17] = "fload"; 90 length[0x17] = 2; 91 opcode[0x18] = "dload"; 92 length[0x18] = 2; 93 opcode[0x19] = "aload"; 94 length[0x19] = 2; 95 opcode[0x1a] = "iload_0"; 96 length[0x1a] = 1; 97 opcode[0x1b] = "iload_1"; 98 length[0x1b] = 1; 99 opcode[0x1c] = "iload_2"; 100 length[0x1c] = 1; 101 opcode[0x1d] = "iload_3"; 102 length[0x1d] = 1; 103 opcode[0x1e] = "lload_0"; 104 length[0x1e] = 1; 105 opcode[0x1f] = "lload_1"; 106 length[0x1f] = 1; 107 108 opcode[0x20] = "lload_2"; 109 length[0x20] = 1; 110 opcode[0x21] = "lload_3"; 111 length[0x21] = 1; 112 opcode[0x22] = "fload_0"; 113 length[0x22] = 1; 114 opcode[0x23] = "fload_1"; 115 length[0x23] = 1; 116 opcode[0x24] = "fload_2"; 117 length[0x24] = 1; 118 opcode[0x25] = "fload_3"; 119 length[0x25] = 1; 120 opcode[0x26] = "dload_0"; 121 length[0x26] = 1; 122 opcode[0x27] = "dload_1"; 123 length[0x27] = 1; 124 opcode[0x28] = "dload_2"; 125 length[0x28] = 1; 126 opcode[0x29] = "dload_3"; 127 length[0x29] = 1; 128 opcode[0x2a] = "aload_0"; 129 length[0x2a] = 1; 130 opcode[0x2b] = "aload_1"; 131 length[0x2b] = 1; 132 opcode[0x2c] = "aload_2"; 133 length[0x2c] = 1; 134 opcode[0x2d] = "aload_3"; 135 length[0x2d] = 1; 136 opcode[0x2e] = "iaload"; 137 length[0x2e] = 1; 138 opcode[0x2f] = "laload"; 139 length[0x2f] = 1; 140 141 opcode[0x30] = "faload"; 142 length[0x30] = 1; 143 opcode[0x31] = "daload"; 144 length[0x31] = 1; 145 opcode[0x32] = "aaload"; 146 length[0x32] = 1; 147 opcode[0x33] = "baload"; 148 length[0x33] = 1; 149 opcode[0x34] = "caload"; 150 length[0x34] = 1; 151 opcode[0x35] = "saload"; 152 length[0x35] = 1; 153 opcode[0x36] = "istore"; 154 length[0x36] = 2; 155 opcode[0x37] = "lstore"; 156 length[0x37] = 2; 157 opcode[0x38] = "fstore"; 158 length[0x38] = 2; 159 opcode[0x39] = "dstore"; 160 length[0x39] = 2; 161 opcode[0x3a] = "astore"; 162 length[0x3a] = 2; 163 opcode[0x3b] = "istore_0"; 164 length[0x3b] = 1; 165 opcode[0x3c] = "istore_1"; 166 length[0x3c] = 1; 167 opcode[0x3d] = "istore_2"; 168 length[0x3d] = 1; 169 opcode[0x3e] = "istore_3"; 170 length[0x3e] = 1; 171 opcode[0x3f] = "lstore_0"; 172 length[0x3f] = 1; 173 174 opcode[0x40] = "lstore_1"; 175 length[0x40] = 1; 176 opcode[0x41] = "lstore_2"; 177 length[0x41] = 1; 178 opcode[0x42] = "lstore_3"; 179 length[0x42] = 1; 180 opcode[0x43] = "fstore_0"; 181 length[0x43] = 1; 182 opcode[0x44] = "fstore_1"; 183 length[0x44] = 1; 184 opcode[0x45] = "fstore_2"; 185 length[0x45] = 1; 186 opcode[0x46] = "fstore_3"; 187 length[0x46] = 1; 188 opcode[0x47] = "dstore_0"; 189 length[0x47] = 1; 190 opcode[0x48] = "dstore_1"; 191 length[0x48] = 1; 192 opcode[0x49] = "dstore_2"; 193 length[0x49] = 1; 194 opcode[0x4a] = "dstore_3"; 195 length[0x4a] = 1; 196 opcode[0x4b] = "astore_0"; 197 length[0x4b] = 1; 198 opcode[0x4c] = "astore_1"; 199 length[0x4c] = 1; 200 opcode[0x4d] = "astore_2"; 201 length[0x4d] = 1; 202 opcode[0x4e] = "astore_3"; 203 length[0x4e] = 1; 204 opcode[0x4f] = "iastore"; 205 length[0x4f] = 1; 206 207 opcode[0x50] = "lastore"; 208 length[0x50] = 1; 209 opcode[0x51] = "fastore"; 210 length[0x51] = 1; 211 opcode[0x52] = "dastore"; 212 length[0x52] = 1; 213 opcode[0x53] = "aastore"; 214 length[0x53] = 1; 215 opcode[0x54] = "bastore"; 216 length[0x54] = 1; 217 opcode[0x55] = "castore"; 218 length[0x55] = 1; 219 opcode[0x56] = "sastore"; 220 length[0x56] = 1; 221 opcode[0x57] = "pop"; 222 length[0x57] = 1; 223 opcode[0x58] = "pop2"; 224 length[0x58] = 1; 225 opcode[0x59] = "dup"; 226 length[0x59] = 1; 227 opcode[0x5a] = "dup_x1"; 228 length[0x5a] = 1; 229 opcode[0x5b] = "dub_x2"; 230 length[0x5b] = 1; 231 opcode[0x5c] = "dup2"; 232 length[0x5c] = 1; 233 opcode[0x5d] = "dup2_x1"; 234 length[0x5d] = 1; 235 opcode[0x5e] = "dup2_x2"; 236 length[0x5e] = 1; 237 opcode[0x5f] = "swap"; 238 length[0x5f] = 1; 239 240 opcode[0x60] = "iadd"; 241 length[0x60] = 1; 242 opcode[0x61] = "ladd"; 243 length[0x61] = 1; 244 opcode[0x62] = "fadd"; 245 length[0x62] = 1; 246 opcode[0x63] = "dadd"; 247 length[0x63] = 1; 248 opcode[0x64] = "isub"; 249 length[0x64] = 1; 250 opcode[0x65] = "lsub"; 251 length[0x65] = 1; 252 opcode[0x66] = "fsub"; 253 length[0x66] = 1; 254 opcode[0x67] = "dsub"; 255 length[0x67] = 1; 256 opcode[0x68] = "imul"; 257 length[0x68] = 1; 258 opcode[0x69] = "lmul"; 259 length[0x69] = 1; 260 opcode[0x6a] = "fmul"; 261 length[0x6a] = 1; 262 opcode[0x6b] = "dmul"; 263 length[0x6b] = 1; 264 opcode[0x6c] = "idiv"; 265 length[0x6c] = 1; 266 opcode[0x6d] = "ldiv"; 267 length[0x6d] = 1; 268 opcode[0x6e] = "fdiv"; 269 length[0x6e] = 1; 270 opcode[0x6f] = "ddiv"; 271 length[0x6f] = 1; 272 273 opcode[0x70] = "irem"; 274 length[0x70] = 1; 275 opcode[0x71] = "lrem"; 276 length[0x71] = 1; 277 opcode[0x72] = "frem"; 278 length[0x72] = 1; 279 opcode[0x73] = "drem"; 280 length[0x73] = 1; 281 opcode[0x74] = "ineg"; 282 length[0x74] = 1; 283 opcode[0x75] = "lneg"; 284 length[0x75] = 1; 285 opcode[0x76] = "fneg"; 286 length[0x76] = 1; 287 opcode[0x77] = "dneg"; 288 length[0x77] = 1; 289 opcode[0x78] = "ishl"; 290 length[0x78] = 1; 291 opcode[0x79] = "lshl"; 292 length[0x79] = 1; 293 opcode[0x7a] = "ishr"; 294 length[0x7a] = 1; 295 opcode[0x7b] = "lshr"; 296 length[0x7b] = 1; 297 opcode[0x7c] = "iushr"; 298 length[0x7c] = 1; 299 opcode[0x7d] = "lushr"; 300 length[0x7d] = 1; 301 opcode[0x7e] = "iand"; 302 length[0x7e] = 1; 303 opcode[0x7f] = "land"; 304 length[0x7f] = 1; 305 306 opcode[0x80] = "ior"; 307 length[0x80] = 1; 308 opcode[0x81] = "lor"; 309 length[0x81] = 1; 310 opcode[0x82] = "ixor"; 311 length[0x82] = 1; 312 opcode[0x83] = "lxor"; 313 length[0x83] = 1; 314 opcode[0x84] = "iinc"; 315 length[0x84] = 3; 316 opcode[0x85] = "i2l"; 317 length[0x85] = 1; 318 opcode[0x86] = "i2f"; 319 length[0x86] = 1; 320 opcode[0x87] = "i2d"; 321 length[0x87] = 1; 322 opcode[0x88] = "l2i"; 323 length[0x88] = 1; 324 opcode[0x89] = "l2f"; 325 length[0x89] = 1; 326 opcode[0x8a] = "l2d"; 327 length[0x8a] = 1; 328 opcode[0x8b] = "f2i"; 329 length[0x8b] = 1; 330 opcode[0x8c] = "f2l"; 331 length[0x8c] = 1; 332 opcode[0x8d] = "f2d"; 333 length[0x8d] = 1; 334 opcode[0x8e] = "d2i"; 335 length[0x8e] = 1; 336 opcode[0x8f] = "d2l"; 337 length[0x8f] = 1; 338 339 opcode[0x90] = "d2f"; 340 length[0x90] = 1; 341 opcode[0x91] = "i2b"; 342 length[0x91] = 1; 343 opcode[0x92] = "i2c"; 344 length[0x92] = 1; 345 opcode[0x93] = "i2s"; 346 length[0x93] = 1; 347 opcode[0x94] = "lcmp"; 348 length[0x94] = 1; 349 opcode[0x95] = "fcmpl"; 350 length[0x95] = 1; 351 opcode[0x96] = "fcmpg"; 352 length[0x96] = 1; 353 opcode[0x97] = "dcmpl"; 354 length[0x97] = 1; 355 opcode[0x98] = "dcmpg"; 356 length[0x98] = 1; 357 opcode[0x99] = "ifeq"; 358 length[0x99] = 3; 359 opcode[0x9a] = "ifne"; 360 length[0x9a] = 3; 361 opcode[0x9b] = "iflt"; 362 length[0x9b] = 3; 363 opcode[0x9c] = "ifge"; 364 length[0x9c] = 3; 365 opcode[0x9d] = "ifgt"; 366 length[0x9d] = 3; 367 opcode[0x9e] = "ifle"; 368 length[0x9e] = 3; 369 opcode[0x9f] = "if_icmpeq"; 370 length[0x9f] = 3; 371 372 opcode[0xa0] = "if_icmpne"; 373 length[0xa0] = 3; 374 opcode[0xa1] = "if_icmplt"; 375 length[0xa1] = 3; 376 opcode[0xa2] = "if_icmpge"; 377 length[0xa2] = 3; 378 opcode[0xa3] = "if_icmpgt"; 379 length[0xa3] = 3; 380 opcode[0xa4] = "if_icmple"; 381 length[0xa4] = 3; 382 opcode[0xa5] = "if_acmpeq"; 383 length[0xa5] = 3; 384 opcode[0xa6] = "if_acmpne"; 385 length[0xa6] = 3; 386 opcode[0xa7] = "goto"; 387 length[0xa7] = 3; 388 opcode[0xa8] = "jsr"; 389 length[0xa8] = 3; 390 opcode[0xa9] = "ret"; 391 length[0xa9] = 2; 392 opcode[0xaa] = "tableswitch"; 393 length[0xaa] = -1; 394 opcode[0xab] = "lookupswitch"; 395 length[0xab] = -1; 396 opcode[0xac] = "ireturn"; 397 length[0xac] = 1; 398 opcode[0xad] = "lreturn"; 399 length[0xad] = 1; 400 opcode[0xae] = "freturn"; 401 length[0xae] = 1; 402 opcode[0xaf] = "dreturn"; 403 length[0xaf] = 1; 404 405 opcode[0xb0] = "areturn"; 406 length[0xb0] = 1; 407 opcode[0xb1] = "return"; 408 length[0xb1] = 1; 409 opcode[0xb2] = "getstatic"; 410 length[0xb2] = 3; 411 opcode[0xb3] = "putstatic"; 412 length[0xb3] = 3; 413 opcode[0xb4] = "getfield"; 414 length[0xb4] = 3; 415 opcode[0xb5] = "putfield"; 416 length[0xb5] = 3; 417 opcode[0xb6] = "invokevirtual"; 418 length[0xb6] = 3; 419 opcode[0xb7] = "invokespecial"; 420 length[0xb7] = 3; 421 opcode[0xb8] = "invokestatic"; 422 length[0xb8] = 3; 423 opcode[0xb9] = "invokeinterface"; 424 length[0xb9] = 5; 425 opcode[0xba] = "xxxunusedxxx"; 426 length[0xba] = 1; 427 opcode[0xbb] = "new"; 428 length[0xbb] = 3; 429 opcode[0xbc] = "newarray"; 430 length[0xbc] = 2; 431 opcode[0xbd] = "anewarray"; 432 length[0xbd] = 3; 433 opcode[0xbe] = "arraylength"; 434 length[0xbe] = 1; 435 opcode[0xbf] = "athrow"; 436 length[0xbf] = 1; 437 438 opcode[0xc0] = "checkcast"; 439 length[0xc0] = 3; 440 opcode[0xc1] = "instanceof"; 441 length[0xc1] = 3; 442 opcode[0xc2] = "monitorenter"; 443 length[0xc2] = 1; 444 opcode[0xc3] = "monitorexit"; 445 length[0xc3] = 1; 446 opcode[0xc4] = "wide"; 447 length[0xc4] = -1; 448 opcode[0xc5] = "multianewarray"; 449 length[0xc5] = 4; 450 opcode[0xc6] = "ifnull"; 451 length[0xc6] = 3; 452 opcode[0xc7] = "ifnonnull"; 453 length[0xc7] = 3; 454 opcode[0xc8] = "goto_w"; 455 length[0xc8] = 5; 456 opcode[0xc9] = "jsr_w"; 457 length[0xc9] = 5; 458 opcode[0xca] = "wide"; 459 length[0xca] = 1; 460 opcode[0xcb] = "xxxundefinedxxx"; 461 length[0xcb] = 1; 462 opcode[0xcc] = "xxxundefinedxxx"; 463 length[0xcc] = 1; 464 opcode[0xcd] = "xxxundefinedxxx"; 465 length[0xcd] = 1; 466 opcode[0xce] = "xxxundefinedxxx"; 467 length[0xce] = 1; 468 opcode[0xcf] = "xxxundefinedxxx"; 469 length[0xcf] = 1; 470 471 opcode[0xd0] = "xxxundefinedxxx"; 472 length[0xd0] = 1; 473 opcode[0xd1] = "xxxundefinedxxx"; 474 length[0xd1] = 1; 475 opcode[0xd2] = "xxxundefinedxxx"; 476 length[0xd2] = 1; 477 opcode[0xd3] = "xxxundefinedxxx"; 478 length[0xd3] = 1; 479 opcode[0xd4] = "xxxundefinedxxx"; 480 length[0xd4] = 1; 481 opcode[0xd5] = "xxxundefinedxxx"; 482 length[0xd5] = 1; 483 opcode[0xd6] = "xxxundefinedxxx"; 484 length[0xd6] = 1; 485 opcode[0xd7] = "xxxundefinedxxx"; 486 length[0xd7] = 1; 487 opcode[0xd8] = "xxxundefinedxxx"; 488 length[0xd8] = 1; 489 opcode[0xd9] = "xxxundefinedxxx"; 490 length[0xd9] = 1; 491 opcode[0xda] = "xxxundefinedxxx"; 492 length[0xda] = 1; 493 opcode[0xdb] = "xxxundefinedxxx"; 494 length[0xdb] = 1; 495 opcode[0xdc] = "xxxundefinedxxx"; 496 length[0xdc] = 1; 497 opcode[0xdd] = "xxxundefinedxxx"; 498 length[0xdd] = 1; 499 opcode[0xde] = "xxxundefinedxxx"; 500 length[0xde] = 1; 501 opcode[0xdf] = "xxxundefinedxxx"; 502 length[0xdf] = 1; 503 504 opcode[0xe0] = "xxxundefinedxxx"; 505 length[0xe0] = 1; 506 opcode[0xe1] = "xxxundefinedxxx"; 507 length[0xe1] = 1; 508 opcode[0xe2] = "xxxundefinedxxx"; 509 length[0xe2] = 1; 510 opcode[0xe3] = "xxxundefinedxxx"; 511 length[0xe3] = 1; 512 opcode[0xe4] = "xxxundefinedxxx"; 513 length[0xe4] = 1; 514 opcode[0xe5] = "xxxundefinedxxx"; 515 length[0xe5] = 1; 516 opcode[0xe6] = "xxxundefinedxxx"; 517 length[0xe6] = 1; 518 opcode[0xe7] = "xxxundefinedxxx"; 519 length[0xe7] = 1; 520 opcode[0xe8] = "xxxundefinedxxx"; 521 length[0xe8] = 1; 522 opcode[0xe9] = "xxxundefinedxxx"; 523 length[0xe9] = 1; 524 opcode[0xea] = "xxxundefinedxxx"; 525 length[0xea] = 1; 526 opcode[0xeb] = "xxxundefinedxxx"; 527 length[0xeb] = 1; 528 opcode[0xec] = "xxxundefinedxxx"; 529 length[0xec] = 1; 530 opcode[0xed] = "xxxundefinedxxx"; 531 length[0xed] = 1; 532 opcode[0xee] = "xxxundefinedxxx"; 533 length[0xee] = 1; 534 opcode[0xef] = "xxxundefinedxxx"; 535 length[0xef] = 1; 536 537 opcode[0xf0] = "xxxundefinedxxx"; 538 length[0xf0] = 1; 539 opcode[0xf1] = "xxxundefinedxxx"; 540 length[0xf1] = 1; 541 opcode[0xf2] = "xxxundefinedxxx"; 542 length[0xf2] = 1; 543 opcode[0xf3] = "xxxundefinedxxx"; 544 length[0xf3] = 1; 545 opcode[0xf4] = "xxxundefinedxxx"; 546 length[0xf4] = 1; 547 opcode[0xf5] = "xxxundefinedxxx"; 548 length[0xf5] = 1; 549 opcode[0xf6] = "xxxundefinedxxx"; 550 length[0xf6] = 1; 551 opcode[0xf7] = "xxxundefinedxxx"; 552 length[0xf7] = 1; 553 opcode[0xf8] = "xxxundefinedxxx"; 554 length[0xf8] = 1; 555 opcode[0xf9] = "xxxundefinedxxx"; 556 length[0xf9] = 1; 557 opcode[0xfa] = "xxxundefinedxxx"; 558 length[0xfa] = 1; 559 opcode[0xfb] = "xxxundefinedxxx"; 560 length[0xfb] = 1; 561 opcode[0xfc] = "xxxundefinedxxx"; 562 length[0xfc] = 1; 563 opcode[0xfd] = "xxxundefinedxxx"; 564 length[0xfd] = 1; 565 opcode[0xfe] = "impdep1"; 566 length[0xfe] = 1; 567 opcode[0xff] = "impdep2"; 568 length[0xff] = 1; 569 } 570 571 private byte[] code; 572 private int start; 573 574 public Instruction(byte[] code, int start) { 575 this.code = code; 576 this.start = start; 577 } 578 579 public byte[] getCode() { 580 return code; 581 } 582 583 public int getStart() { 584 return start; 585 } 586 587 public int getOpcode() { 588 return (code[start] & 0xff); 589 } 590 591 public static String getMnemonic(int instruction) { 592 return opcode[instruction]; 593 } 594 595 public String getMnemonic() { 596 String result = getMnemonic(getOpcode()); 597 598 if (getOpcode() == 0xc4 ) { 599 result += " " + getMnemonic(code[start+1] & 0xff); 600 } 601 602 return result; 603 } 604 605 public int getLength() { 606 int result = length[getOpcode()]; 607 608 int padding, low, high, npairs; 609 610 switch (getOpcode()) { 611 case 0xaa: 612 padding = 3 - (start % 4); 614 low = 615 ((code[start+padding+5] & 0xff) << 24) | 616 ((code[start+padding+6] & 0xff) << 16) | 617 ((code[start+padding+7] & 0xff) << 8) | 618 ((code[start+padding+8] & 0xff)); 619 high = 620 ((code[start+padding+9] & 0xff) << 24) | 621 ((code[start+padding+10] & 0xff) << 16) | 622 ((code[start+padding+11] & 0xff) << 8) | 623 ((code[start+padding+12] & 0xff)); 624 result = 625 1 + padding + 12 + (high - low + 1) * 4; break; 630 631 case 0xab: 632 padding = 3 - (start % 4); 634 npairs = 635 ((code[start+padding+5] & 0xff) << 24) | 636 ((code[start+padding+6] & 0xff) << 16) | 637 ((code[start+padding+7] & 0xff) << 8) | 638 ((code[start+padding+8] & 0xff)); 639 result = 640 1 + padding + 8 + (npairs * 8); break; 645 646 case 0xc4: 647 if ((code[start+1] & 0xff) == 0x84 ) { 649 result = 6; 650 } else { 651 result = 4; 652 } 653 break; 654 655 default: 656 break; 658 } 659 660 return result; 661 } 662 663 public String toString() { 664 return getMnemonic(); 665 } 666 } 667 | Popular Tags |