jdisa/main.c
changeset 95 d2a071bd1a60
equal deleted inserted replaced
94:c100ba6939e3 95:d2a071bd1a60
       
     1 /**
       
     2  * jdisa
       
     3  *
       
     4  * ByteCode Disassembler
       
     5  * See http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html
       
     6  */
       
     7 
       
     8 #include <stdio.h>
       
     9 #include <stdlib.h>
       
    10 #include <ctype.h>
       
    11 
       
    12 #define BUF_SIZE 1023
       
    13 
       
    14 typedef unsigned char uchar_t;
       
    15 
       
    16 typedef uchar_t u1[1];
       
    17 typedef uchar_t u2[2];
       
    18 typedef uchar_t u4[4];
       
    19 typedef uchar_t u8[8];
       
    20 
       
    21 struct Header {
       
    22     u4 magic;
       
    23     u2 minor_version;
       
    24     u2 major_version;
       
    25     u2 constant_pool_count;
       
    26 };
       
    27 
       
    28 typedef struct Header Header;
       
    29 
       
    30 int main (int argc, char **argv)
       
    31 {
       
    32     FILE *f;
       
    33     uchar_t buffer[BUF_SIZE];
       
    34     int i;
       
    35     size_t num;
       
    36     int OPCODE;
       
    37     Header *cf;
       
    38 
       
    39     if (argc != 2) {
       
    40         fprintf (stderr, "Usage: %s <FILE>\n", argv[0]);
       
    41         return EXIT_FAILURE;
       
    42     }
       
    43 
       
    44     if ((f = fopen (argv[1], "r")) == NULL) {
       
    45         perror ("FOPEN");
       
    46         return EXIT_FAILURE;
       
    47     }
       
    48 
       
    49     if ((cf = malloc (sizeof (Header))) == NULL)
       
    50         return EXIT_FAILURE;
       
    51     if ((num = fread (cf, 1, sizeof (Header), f)) < 1)
       
    52         return EXIT_FAILURE;
       
    53 
       
    54     printf ("  MAGIC: %2X%2X%2X%2X\n", cf->magic[0], cf->magic[1], cf->magic[2], cf->magic[3]);
       
    55     printf ("VERSION: %02d.%02d\n", cf->major_version[1], cf->major_version[0]);
       
    56     printf ("POOLCTN: %2d:%2d\n", cf->constant_pool_count[1], cf->constant_pool_count[0]);
       
    57 
       
    58     if (cf != NULL)
       
    59         free (cf);
       
    60 
       
    61     OPCODE = 1;
       
    62     while (!feof (f)) {
       
    63         num = fread (buffer, 1, sizeof (buffer), f);
       
    64         for (i = 0; i < num; i++) {
       
    65             if (OPCODE) {
       
    66                 switch (buffer[i]) {
       
    67                 case 50:
       
    68                     printf ("aaload ");
       
    69                     break;
       
    70                 case 83:
       
    71                     printf ("aastore ");
       
    72                     break;
       
    73                 case 1:
       
    74                     printf ("aconst_null ");
       
    75                     break;
       
    76                 case 25:
       
    77                     printf ("aload ");
       
    78                     break;
       
    79                 case 42:
       
    80                     printf ("aload_0 ");
       
    81                     break;
       
    82                 case 43:
       
    83                     printf ("aload_1 ");
       
    84                     break;
       
    85                 case 44:
       
    86                     printf ("aload_2 ");
       
    87                     break;
       
    88                 case 45:
       
    89                     printf ("aload_3 ");
       
    90                     break;
       
    91                 case 189:
       
    92                     printf ("anewarray ");
       
    93                     break;
       
    94                 case 176:
       
    95                     printf ("areturn ");
       
    96                     break;
       
    97                 case 190:
       
    98                     printf ("arraylength ");
       
    99                     break;
       
   100                 case 58:
       
   101                     printf ("astore ");
       
   102                     break;
       
   103                 case 75:
       
   104                     printf ("astore_0 ");
       
   105                     break;
       
   106                 case 76:
       
   107                     printf ("astore_1 ");
       
   108                     break;
       
   109                 case 77:
       
   110                     printf ("astore_2 ");
       
   111                     break;
       
   112                 case 78:
       
   113                     printf ("astore_3 ");
       
   114                     break;
       
   115                 case 191:
       
   116                     printf ("athrow ");
       
   117                     break;
       
   118                 case 51:
       
   119                     printf ("baload ");
       
   120                     break;
       
   121                 case 84:
       
   122                     printf ("bastore ");
       
   123                     break;
       
   124                 case 16:
       
   125                     printf ("bipush ");
       
   126                     break;
       
   127                 case 52:
       
   128                     printf ("caload ");
       
   129                     break;
       
   130                 case 85:
       
   131                     printf ("castore ");
       
   132                     break;
       
   133                 case 192:
       
   134                     printf ("checkcast ");
       
   135                     break;
       
   136                 case 144:
       
   137                     printf ("d2f ");
       
   138                     break;
       
   139                 case 142:
       
   140                     printf ("d2i ");
       
   141                     break;
       
   142                 case 143:
       
   143                     printf ("d2l ");
       
   144                     break;
       
   145                 case 99:
       
   146                     printf ("dadd ");
       
   147                     break;
       
   148                 case 49:
       
   149                     printf ("daload ");
       
   150                     break;
       
   151                 case 82:
       
   152                     printf ("dastore ");
       
   153                     break;
       
   154                 case 152:
       
   155                     printf ("dcmp ");
       
   156                     break;
       
   157                 case 14:
       
   158                     printf ("dconst_0 ");
       
   159                     break;
       
   160                 case 15:
       
   161                     printf ("dconst_1 ");
       
   162                     break;
       
   163                 case 111:
       
   164                     printf ("ddiv ");
       
   165                     break;
       
   166                 case 24:
       
   167                     printf ("dload ");
       
   168                     break;
       
   169                 case 38:
       
   170                     printf ("dload_0 ");
       
   171                     break;
       
   172                 case 39:
       
   173                     printf ("dload_1 ");
       
   174                     break;
       
   175                 case 40:
       
   176                     printf ("dload_2 ");
       
   177                     break;
       
   178                 case 41:
       
   179                     printf ("dload_3 ");
       
   180                     break;
       
   181                 case 107:
       
   182                     printf ("dmul ");
       
   183                     break;
       
   184                 case 119:
       
   185                     printf ("dneg ");
       
   186                     break;
       
   187                 case 115:
       
   188                     printf ("drem ");
       
   189                     break;
       
   190                 case 175:
       
   191                     printf ("dreturn ");
       
   192                     break;
       
   193                 case 57:
       
   194                     printf ("dstore ");
       
   195                     break;
       
   196                 case 71:
       
   197                     printf ("dstore_0 ");
       
   198                     break;
       
   199                 case 72:
       
   200                     printf ("dstore_1 ");
       
   201                     break;
       
   202                 case 73:
       
   203                     printf ("dstore_2 ");
       
   204                     break;
       
   205                 case 74:
       
   206                     printf ("dstore_3 ");
       
   207                     break;
       
   208                 case 103:
       
   209                     printf ("dsub ");
       
   210                     break;
       
   211                 case 89:
       
   212                     printf ("dup ");
       
   213                     break;
       
   214                 case 90:
       
   215                     printf ("dup_x1 ");
       
   216                     break;
       
   217                 case 91:
       
   218                     printf ("dup_x2 ");
       
   219                     break;
       
   220                 case 92:
       
   221                     printf ("dup2 ");
       
   222                     break;
       
   223                 case 93:
       
   224                     printf ("dup2_x1 ");
       
   225                     break;
       
   226                 case 94:
       
   227                     printf ("dup2_x2 ");
       
   228                     break;
       
   229                 case 141:
       
   230                     printf ("f2d ");
       
   231                     break;
       
   232                 case 139:
       
   233                     printf ("f2i ");
       
   234                     break;
       
   235                 case 140:
       
   236                     printf ("f2l ");
       
   237                     break;
       
   238                 case 98:
       
   239                     printf ("fadd ");
       
   240                     break;
       
   241                 case 48:
       
   242                     printf ("faload ");
       
   243                     break;
       
   244                 case 81:
       
   245                     printf ("fastore ");
       
   246                     break;
       
   247                 case 150:
       
   248                     printf ("fcmpg ");
       
   249                     break;
       
   250                 case 149:
       
   251                     printf ("fcmpl ");
       
   252                     break;
       
   253                 case 11:
       
   254                     printf ("fconst_0 ");
       
   255                     break;
       
   256                 case 12:
       
   257                     printf ("fconst_1 ");
       
   258                     break;
       
   259                 case 13:
       
   260                     printf ("fconst_2 ");
       
   261                     break;
       
   262                 case 110:
       
   263                     printf ("fdiv ");
       
   264                     break;
       
   265                 case 23:
       
   266                     printf ("fload ");
       
   267                     break;
       
   268                 case 34:
       
   269                     printf ("fload_0 ");
       
   270                     break;
       
   271                 case 35:
       
   272                     printf ("fload_1 ");
       
   273                     break;
       
   274                 case 36:
       
   275                     printf ("fload_2 ");
       
   276                     break;
       
   277                 case 37:
       
   278                     printf ("fload_3 ");
       
   279                     break;
       
   280                 case 106:
       
   281                     printf ("fmul ");
       
   282                     break;
       
   283                 case 118:
       
   284                     printf ("fneg ");
       
   285                     break;
       
   286                 case 114:
       
   287                     printf ("frem ");
       
   288                     break;
       
   289                 case 174:
       
   290                     printf ("freturn ");
       
   291                     break;
       
   292                 case 56:
       
   293                     printf ("fstore ");
       
   294                     break;
       
   295                 case 67:
       
   296                     printf ("fstore_0 ");
       
   297                     break;
       
   298                 case 68:
       
   299                     printf ("fstore_1 ");
       
   300                     break;
       
   301                 case 69:
       
   302                     printf ("fstore_2 ");
       
   303                     break;
       
   304                 case 70:
       
   305                     printf ("fstore_3 ");
       
   306                     break;
       
   307                 case 102:
       
   308                     printf ("fsub ");
       
   309                     break;
       
   310                 case 180:
       
   311                     printf ("getfield ");
       
   312                     break;
       
   313                 case 178:
       
   314                     printf ("getstatic ");
       
   315                     break;
       
   316                 case 167:
       
   317                     printf ("goto ");
       
   318                     break;
       
   319                 case 200:
       
   320                     printf ("goto_w ");
       
   321                     break;
       
   322                 case 145:
       
   323                     printf ("i2b ");
       
   324                     break;
       
   325                 case 146:
       
   326                     printf ("i2c ");
       
   327                     break;
       
   328                 case 135:
       
   329                     printf ("i2d ");
       
   330                     break;
       
   331                 case 134:
       
   332                     printf ("i2f ");
       
   333                     break;
       
   334                 case 133:
       
   335                     printf ("i2l ");
       
   336                     break;
       
   337                 case 147:
       
   338                     printf ("i2s ");
       
   339                     break;
       
   340                 case 96:
       
   341                     printf ("iadd ");
       
   342                     break;
       
   343                 case 46:
       
   344                     printf ("iaload ");
       
   345                     break;
       
   346                 case 126:
       
   347                     printf ("iand ");
       
   348                     break;
       
   349                 case 79:
       
   350                     printf ("iastore ");
       
   351                     break;
       
   352                 case 2:
       
   353                     printf ("iconst_m1 ");
       
   354                     break;
       
   355                 case 3:
       
   356                     printf ("iconst_0 ");
       
   357                     break;
       
   358                 case 4:
       
   359                     printf ("iconst_1 ");
       
   360                     break;
       
   361                 case 5:
       
   362                     printf ("iconst_2 ");
       
   363                     break;
       
   364                 case 6:
       
   365                     printf ("iconst_3 ");
       
   366                     break;
       
   367                 case 7:
       
   368                     printf ("iconst_4 ");
       
   369                     break;
       
   370                 case 8:
       
   371                     printf ("iconst_5 ");
       
   372                     break;
       
   373                 case 108:
       
   374                     printf ("idiv ");
       
   375                     break;
       
   376                 case 165:
       
   377                     printf ("if_acmpeq ");
       
   378                     break;
       
   379                 case 166:
       
   380                     printf ("if_acmpne ");
       
   381                     break;
       
   382                 case 159:
       
   383                     printf ("if_icmpeq ");
       
   384                     break;
       
   385                 case 160:
       
   386                     printf ("if_icmpne ");
       
   387                     break;
       
   388                 case 161:
       
   389                     printf ("if_icmplt ");
       
   390                     break;
       
   391                 case 162:
       
   392                     printf ("if_icmpge ");
       
   393                     break;
       
   394                 case 163:
       
   395                     printf ("if_icmpgt ");
       
   396                     break;
       
   397                 case 164:
       
   398                     printf ("if_icmple ");
       
   399                     break;
       
   400                 case 153:
       
   401                     printf ("ifeq ");
       
   402                     break;
       
   403                 case 154:
       
   404                     printf ("ifne ");
       
   405                     break;
       
   406                 case 155:
       
   407                     printf ("iflt ");
       
   408                     break;
       
   409                 case 156:
       
   410                     printf ("ifge ");
       
   411                     break;
       
   412                 case 157:
       
   413                     printf ("ifgt ");
       
   414                     break;
       
   415                 case 158:
       
   416                     printf ("ifle ");
       
   417                     break;
       
   418                 case 199:
       
   419                     printf ("ifnonnull ");
       
   420                     break;
       
   421                 case 198:
       
   422                     printf ("ifnull ");
       
   423                     break;
       
   424                 case 132:
       
   425                     printf ("iinc %d %d\n", buffer[i + 1], buffer[i + 2]);
       
   426                     i += 2;
       
   427                     continue;
       
   428                     break;
       
   429                 case 21:
       
   430                     printf ("iload ");
       
   431                     break;
       
   432                 case 26:
       
   433                     printf ("iload_0 ");
       
   434                     break;
       
   435                 case 27:
       
   436                     printf ("iload_1 ");
       
   437                     break;
       
   438                 case 28:
       
   439                     printf ("iload_2 ");
       
   440                     break;
       
   441                 case 29:
       
   442                     printf ("iload_3 ");
       
   443                     break;
       
   444                 case 104:
       
   445                     printf ("imul ");
       
   446                     break;
       
   447                 case 116:
       
   448                     printf ("ineg ");
       
   449                     break;
       
   450                 case 193:
       
   451                     printf ("instanceof ");
       
   452                     break;
       
   453                 case 185:
       
   454                     printf ("invokeinterface ");
       
   455                     break;
       
   456                 case 183:
       
   457                     printf ("invokespecial ");
       
   458                     break;
       
   459                 case 184:
       
   460                     printf ("invokestatic ");
       
   461                     break;
       
   462                 case 182:
       
   463                     printf ("invokevirtual ");
       
   464                     break;
       
   465                 case 128:
       
   466                     printf ("ior ");
       
   467                     break;
       
   468                 case 112:
       
   469                     printf ("irem ");
       
   470                     break;
       
   471                 case 172:
       
   472                     printf ("ireturn ");
       
   473                     break;
       
   474                 case 120:
       
   475                     printf ("ishl ");
       
   476                     break;
       
   477                 case 122:
       
   478                     printf ("ishr ");
       
   479                     break;
       
   480                 case 54:
       
   481                     printf ("istore ");
       
   482                     break;
       
   483                 case 59:
       
   484                     printf ("istore_0 ");
       
   485                     break;
       
   486                 case 60:
       
   487                     printf ("istore_1 ");
       
   488                     break;
       
   489                 case 61:
       
   490                     printf ("istore_2 ");
       
   491                     break;
       
   492                 case 62:
       
   493                     printf ("istore_3 ");
       
   494                     break;
       
   495                 case 100:
       
   496                     printf ("isub ");
       
   497                     break;
       
   498                 case 124:
       
   499                     printf ("iushr ");
       
   500                     break;
       
   501                 case 130:
       
   502                     printf ("ixor ");
       
   503                     break;
       
   504                 case 168:
       
   505                     printf ("jsr ");
       
   506                     break;
       
   507                 case 201:
       
   508                     printf ("jsr_w ");
       
   509                     break;
       
   510                 case 138:
       
   511                     printf ("l2d ");
       
   512                     break;
       
   513                 case 137:
       
   514                     printf ("l2f ");
       
   515                     break;
       
   516                 case 136:
       
   517                     printf ("l2i ");
       
   518                     break;
       
   519                 case 97:
       
   520                     printf ("ladd ");
       
   521                     break;
       
   522                 case 47:
       
   523                     printf ("laload ");
       
   524                     break;
       
   525                 case 127:
       
   526                     printf ("land ");
       
   527                     break;
       
   528                 case 80:
       
   529                     printf ("lastore ");
       
   530                     break;
       
   531                 case 148:
       
   532                     printf ("lcmd ");
       
   533                     break;
       
   534                 case 9:
       
   535                     printf ("lconst_0 ");
       
   536                     break;
       
   537                 case 10:
       
   538                     printf ("lconst_1 ");
       
   539                     break;
       
   540                 case 18:
       
   541                     printf ("ldc ");
       
   542                     break;
       
   543                 case 19:
       
   544                     printf ("ldc_w ");
       
   545                     break;
       
   546                 case 20:
       
   547                     printf ("ldc2_w ");
       
   548                     break;
       
   549                 case 109:
       
   550                     printf ("ldiv ");
       
   551                     break;
       
   552                 case 22:
       
   553                     printf ("lload ");
       
   554                     break;
       
   555                 case 30:
       
   556                     printf ("lload_0 ");
       
   557                     break;
       
   558                 case 31:
       
   559                     printf ("lload_1 ");
       
   560                     break;
       
   561                 case 32:
       
   562                     printf ("lload_2 ");
       
   563                     break;
       
   564                 case 33:
       
   565                     printf ("lload_3 ");
       
   566                     break;
       
   567                 case 105:
       
   568                     printf ("lmul ");
       
   569                     break;
       
   570                 case 117:
       
   571                     printf ("lneg ");
       
   572                     break;
       
   573                 case 171:
       
   574                     printf ("lookupswitch ");
       
   575                     break;
       
   576                 case 129:
       
   577                     printf ("lor ");
       
   578                     break;
       
   579                 case 113:
       
   580                     printf ("lrem ");
       
   581                     break;
       
   582                 case 173:
       
   583                     printf ("lreturn ");
       
   584                     break;
       
   585                 case 121:
       
   586                     printf ("lshl ");
       
   587                     break;
       
   588                 case 123:
       
   589                     printf ("lshr ");
       
   590                     break;
       
   591                 case 55:
       
   592                     printf ("lstore ");
       
   593                     break;
       
   594                 case 63:
       
   595                     printf ("lstore_0 ");
       
   596                     break;
       
   597                 case 64:
       
   598                     printf ("lstore_1 ");
       
   599                     break;
       
   600                 case 65:
       
   601                     printf ("lstore_2 ");
       
   602                     break;
       
   603                 case 66:
       
   604                     printf ("lstore_3 ");
       
   605                     break;
       
   606                 case 101:
       
   607                     printf ("lsub ");
       
   608                     break;
       
   609                 case 125:
       
   610                     printf ("lushr ");
       
   611                     break;
       
   612                 case 131:
       
   613                     printf ("lxor ");
       
   614                     break;
       
   615                 case 194:
       
   616                     printf ("monitorenter ");
       
   617                     break;
       
   618                 case 195:
       
   619                     printf ("monitorexit ");
       
   620                     break;
       
   621                 case 197:
       
   622                     printf ("multianewarray ");
       
   623                     break;
       
   624                 case 187:
       
   625                     printf ("new ");
       
   626                     break;
       
   627                 case 188:
       
   628                     printf ("newarray ");
       
   629                     break;
       
   630                 case 0:
       
   631                     printf ("nop ");
       
   632                     break;
       
   633                 case 87:
       
   634                     printf ("pop ");
       
   635                     break;
       
   636                 case 88:
       
   637                     printf ("pop2 ");
       
   638                     break;
       
   639                 case 181:
       
   640                     printf ("putfield ");
       
   641                     break;
       
   642                 case 179:
       
   643                     printf ("putstatic ");
       
   644                     break;
       
   645                 case 169:
       
   646                     printf ("ret ");
       
   647                     break;
       
   648                 case 177:
       
   649                     printf ("return ");
       
   650                     break;
       
   651                 case 53:
       
   652                     printf ("saload ");
       
   653                     break;
       
   654                 case 86:
       
   655                     printf ("sastore ");
       
   656                     break;
       
   657                 case 17:
       
   658                     printf ("sipush ");
       
   659                     break;
       
   660                 case 95:
       
   661                     printf ("swap ");
       
   662                     break;
       
   663                 case 170:
       
   664                     printf ("tableswitch ");
       
   665                     break;
       
   666                 case 196:
       
   667                     printf ("wide ");
       
   668                     break;
       
   669                 case 202:      /* reserved item */
       
   670                     printf ("breakpoint ");
       
   671                     break;
       
   672                 case 254:      /* reserved item */
       
   673                     printf ("impdep1 ");
       
   674                     break;
       
   675                 case 255:      /* reserved item */
       
   676                     printf ("impdep2 ");
       
   677                     break;
       
   678                 default:
       
   679                     printf ("OPCODE %02X(%d) ", buffer[i], buffer[i]);
       
   680                 }
       
   681                 OPCODE = 0;
       
   682             } else
       
   683                 switch (buffer[i]) {
       
   684                 case '\0':
       
   685                 case '>':
       
   686                 case '<':
       
   687                     printf ("\n");
       
   688                     OPCODE = 1;
       
   689                     break;
       
   690                 default:
       
   691                     if (isprint (buffer[i]))
       
   692                         printf ("%c", buffer[i]);
       
   693                     else
       
   694                         printf ("%02X ", buffer[i]);
       
   695                 }
       
   696         }
       
   697     }
       
   698 
       
   699     printf ("\n");
       
   700 
       
   701     if (f != NULL)
       
   702         fclose (f);
       
   703 
       
   704     return EXIT_SUCCESS;
       
   705 }