javap/src/main/java/org/apidesign/javap/Tables.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 16 Nov 2012 08:08:36 +0100
branchjavap
changeset 167 77f7135b6eb1
parent 149 javap/src/main/java/sun/tools/javap/Tables.java@32653a09f0db
permissions -rw-r--r--
Re-packaging javap as we are about to make significant changes to it anyway
jtulach@144
     1
/*
jtulach@144
     2
 * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
jtulach@144
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@144
     4
 *
jtulach@144
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@144
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@144
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@144
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@144
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@144
    10
 *
jtulach@144
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@144
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@144
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@144
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@144
    15
 * accompanied this code).
jtulach@144
    16
 *
jtulach@144
    17
 * You should have received a copy of the GNU General Public License version
jtulach@144
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@144
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@144
    20
 *
jtulach@144
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@144
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@144
    23
 * questions.
jtulach@144
    24
 */
jtulach@144
    25
jtulach@144
    26
jtulach@167
    27
package org.apidesign.javap;
jtulach@144
    28
jtulach@144
    29
jtulach@144
    30
jtulach@144
    31
public class Tables implements Constants {
jtulach@144
    32
    /**
jtulach@144
    33
     * Define mnemocodes table.
jtulach@144
    34
     */
jtulach@144
    35
  static  Hashtable mnemocodes = new Hashtable(301, 0.5f);
jtulach@144
    36
  static  String opcExtNamesTab[]=new String[128];
jtulach@144
    37
  static  String opcPrivExtNamesTab[]=new String[128];
jtulach@144
    38
  static  void defineNonPriv(int opc, String mnem) {
jtulach@144
    39
        mnemocodes.put(opcExtNamesTab[opc]=mnem, new Integer(opc_nonpriv*256+opc));
jtulach@144
    40
  }
jtulach@144
    41
  static  void definePriv(int opc, String mnem) {
jtulach@144
    42
        mnemocodes.put(opcPrivExtNamesTab[opc]="priv_"+mnem, new Integer(opc_priv*256+opc));
jtulach@144
    43
  }
jtulach@144
    44
  static  void defineExt(int opc, String mnem) {
jtulach@144
    45
        defineNonPriv(opc, mnem);
jtulach@144
    46
        definePriv(opc, mnem);
jtulach@144
    47
  }
jtulach@144
    48
  static { int k;
jtulach@144
    49
        for (k=0; k<opc_wide; k++) {
jtulach@144
    50
                mnemocodes.put(opcNamesTab[k], new Integer(k));
jtulach@144
    51
        }
jtulach@144
    52
        for (k=opc_wide+1; k<opcNamesTab.length; k++) {
jtulach@144
    53
                mnemocodes.put(opcNamesTab[k], new Integer(k));
jtulach@144
    54
        }
jtulach@144
    55
        mnemocodes.put("invokenonvirtual", new Integer(opc_invokespecial));
jtulach@144
    56
jtulach@144
    57
        mnemocodes.put("iload_w", new Integer(opc_iload_w));
jtulach@144
    58
        mnemocodes.put("lload_w", new Integer(opc_lload_w));
jtulach@144
    59
        mnemocodes.put("fload_w", new Integer(opc_fload_w));
jtulach@144
    60
        mnemocodes.put("dload_w", new Integer(opc_dload_w));
jtulach@144
    61
        mnemocodes.put("aload_w", new Integer(opc_aload_w));
jtulach@144
    62
        mnemocodes.put("istore_w", new Integer(opc_istore_w));
jtulach@144
    63
        mnemocodes.put("lstore_w", new Integer(opc_lstore_w));
jtulach@144
    64
        mnemocodes.put("fstore_w", new Integer(opc_fstore_w));
jtulach@144
    65
        mnemocodes.put("dstore_w", new Integer(opc_dstore_w));
jtulach@144
    66
        mnemocodes.put("astore_w", new Integer(opc_astore_w));
jtulach@144
    67
        mnemocodes.put("ret_w", new Integer(opc_ret_w));
jtulach@144
    68
        mnemocodes.put("iinc_w", new Integer(opc_iinc_w));
jtulach@144
    69
jtulach@144
    70
        mnemocodes.put("nonpriv", new Integer(opc_nonpriv));
jtulach@144
    71
        mnemocodes.put("priv", new Integer(opc_priv));
jtulach@144
    72
jtulach@144
    73
        defineExt(0, "load_ubyte");
jtulach@144
    74
        defineExt(1, "load_byte");
jtulach@144
    75
        defineExt(2, "load_char");
jtulach@144
    76
        defineExt(3, "load_short");
jtulach@144
    77
        defineExt(4, "load_word");
jtulach@144
    78
        defineExt(10, "load_char_oe");
jtulach@144
    79
        defineExt(11, "load_short_oe");
jtulach@144
    80
        defineExt(12, "load_word_oe");
jtulach@144
    81
        defineExt(16, "ncload_ubyte");
jtulach@144
    82
        defineExt(17, "ncload_byte");
jtulach@144
    83
        defineExt(18, "ncload_char");
jtulach@144
    84
        defineExt(19, "ncload_short");
jtulach@144
    85
        defineExt(20, "ncload_word");
jtulach@144
    86
        defineExt(26, "ncload_char_oe");
jtulach@144
    87
        defineExt(27, "ncload_short_oe");
jtulach@144
    88
        defineExt(28, "ncload_word_oe");
jtulach@144
    89
        defineExt(30, "cache_flush");
jtulach@144
    90
        defineExt(32, "store_byte");
jtulach@144
    91
        defineExt(34, "store_short");
jtulach@144
    92
        defineExt(36, "store_word");
jtulach@144
    93
        defineExt(42, "store_short_oe");
jtulach@144
    94
        defineExt(44, "store_word_oe");
jtulach@144
    95
        defineExt(48, "ncstore_byte");
jtulach@144
    96
        defineExt(50, "ncstore_short");
jtulach@144
    97
        defineExt(52, "ncstore_word");
jtulach@144
    98
        defineExt(58, "ncstore_short_oe");
jtulach@144
    99
        defineExt(60, "ncstore_word_oe");
jtulach@144
   100
        defineExt(62, "zero_line");
jtulach@144
   101
        defineNonPriv(5, "ret_from_sub");
jtulach@144
   102
        defineNonPriv(63, "enter_sync_method");
jtulach@144
   103
        definePriv(5, "ret_from_trap");
jtulach@144
   104
        definePriv(6, "read_dcache_tag");
jtulach@144
   105
        definePriv(7, "read_dcache_data");
jtulach@144
   106
        definePriv(14, "read_icache_tag");
jtulach@144
   107
        definePriv(15, "read_icache_data");
jtulach@144
   108
        definePriv(22, "powerdown");
jtulach@144
   109
        definePriv(23, "read_scache_data");
jtulach@144
   110
        definePriv(31, "cache_index_flush");
jtulach@144
   111
        definePriv(38, "write_dcache_tag");
jtulach@144
   112
        definePriv(39, "write_dcache_data");
jtulach@144
   113
        definePriv(46, "write_icache_tag");
jtulach@144
   114
        definePriv(47, "write_icache_data");
jtulach@144
   115
        definePriv(54, "reset");
jtulach@144
   116
        definePriv(55, "write_scache_data");
jtulach@144
   117
        for (k=0; k<32; k++) {
jtulach@144
   118
                definePriv(k+64, "read_reg_"+k);
jtulach@144
   119
        }
jtulach@144
   120
        for (k=0; k<32; k++) {
jtulach@144
   121
                definePriv(k+96, "write_reg_"+k);
jtulach@144
   122
        }
jtulach@144
   123
 }
jtulach@144
   124
jtulach@144
   125
  public static int opcLength(int opc) throws ArrayIndexOutOfBoundsException {
jtulach@144
   126
        switch (opc>>8) {
jtulach@144
   127
          case 0:
jtulach@144
   128
                return opcLengthsTab[opc];
jtulach@144
   129
          case opc_wide:
jtulach@144
   130
                switch (opc&0xFF) {
jtulach@144
   131
                  case opc_aload: case opc_astore:
jtulach@144
   132
                  case opc_fload: case opc_fstore:
jtulach@144
   133
                  case opc_iload: case opc_istore:
jtulach@144
   134
                  case opc_lload: case opc_lstore:
jtulach@144
   135
                  case opc_dload: case opc_dstore:
jtulach@144
   136
                  case opc_ret:
jtulach@144
   137
                        return  4;
jtulach@144
   138
                  case opc_iinc:
jtulach@144
   139
                        return  6;
jtulach@144
   140
                  default:
jtulach@144
   141
                        throw new ArrayIndexOutOfBoundsException();
jtulach@144
   142
                }
jtulach@144
   143
          case opc_nonpriv:
jtulach@144
   144
          case opc_priv:
jtulach@144
   145
                return 2;
jtulach@144
   146
          default:
jtulach@144
   147
                throw new ArrayIndexOutOfBoundsException();
jtulach@144
   148
        }
jtulach@144
   149
  }
jtulach@144
   150
jtulach@144
   151
  public static String opcName(int opc) {
jtulach@144
   152
        try {
jtulach@144
   153
                switch (opc>>8) {
jtulach@144
   154
                  case 0:
jtulach@144
   155
                        return opcNamesTab[opc];
jtulach@144
   156
                  case opc_wide: {
jtulach@144
   157
                        String mnem=opcNamesTab[opc&0xFF]+"_w";
jtulach@144
   158
                        if (mnemocodes.get(mnem) == null)
jtulach@144
   159
                                return null; // non-existent opcode
jtulach@144
   160
                        return mnem;
jtulach@144
   161
                  }
jtulach@144
   162
                  case opc_nonpriv:
jtulach@144
   163
                        return opcExtNamesTab[opc&0xFF];
jtulach@144
   164
                  case opc_priv:
jtulach@144
   165
                        return opcPrivExtNamesTab[opc&0xFF];
jtulach@144
   166
                  default:
jtulach@144
   167
                        return null;
jtulach@144
   168
                }
jtulach@144
   169
        } catch (ArrayIndexOutOfBoundsException e) {
jtulach@144
   170
                switch (opc) {
jtulach@144
   171
                  case opc_nonpriv:
jtulach@144
   172
                        return "nonpriv";
jtulach@144
   173
                  case opc_priv:
jtulach@144
   174
                        return "priv";
jtulach@144
   175
                  default:
jtulach@144
   176
                        return null;
jtulach@144
   177
                }
jtulach@144
   178
        }
jtulach@144
   179
  }
jtulach@144
   180
jtulach@144
   181
  public static int opcode(String mnem) {
jtulach@144
   182
        Integer Val=(Integer)(mnemocodes.get(mnem));
jtulach@144
   183
        if (Val == null) return -1;
jtulach@144
   184
        return Val.intValue();
jtulach@144
   185
  }
jtulach@144
   186
jtulach@144
   187
    /**
jtulach@144
   188
     * Initialized keyword and token Hashtables
jtulach@144
   189
     */
jtulach@144
   190
  static Vector keywordNames = new Vector(40);
jtulach@144
   191
  private static void defineKeywordName(String id, int token) {
jtulach@144
   192
jtulach@144
   193
        if (token>=keywordNames.size()) {
jtulach@144
   194
                keywordNames.setSize(token+1);
jtulach@144
   195
        }
jtulach@144
   196
        keywordNames.setElementAt(id, token);
jtulach@144
   197
  }
jtulach@144
   198
  public static String keywordName(int token) {
jtulach@144
   199
        if (token==-1) return "EOF";
jtulach@144
   200
        if (token>=keywordNames.size()) return null;
jtulach@144
   201
        return (String)keywordNames.elementAt(token);
jtulach@144
   202
  }
jtulach@144
   203
  static {
jtulach@144
   204
        defineKeywordName("ident", IDENT);
jtulach@144
   205
        defineKeywordName("STRINGVAL", STRINGVAL);
jtulach@144
   206
        defineKeywordName("intVal", INTVAL);
jtulach@144
   207
        defineKeywordName("longVal", LONGVAL);
jtulach@144
   208
        defineKeywordName("floatVal", FLOATVAL);
jtulach@144
   209
        defineKeywordName("doubleVal", DOUBLEVAL);
jtulach@144
   210
        defineKeywordName("SEMICOLON", SEMICOLON);
jtulach@144
   211
        defineKeywordName("COLON", COLON);
jtulach@144
   212
        defineKeywordName("LBRACE", LBRACE);
jtulach@144
   213
        defineKeywordName("RBRACE", RBRACE);
jtulach@144
   214
  }
jtulach@144
   215
jtulach@144
   216
  static Hashtable keywords = new Hashtable(40);
jtulach@144
   217
  public static int keyword(String idValue) {
jtulach@144
   218
        Integer Val=(Integer)(keywords.get(idValue));
jtulach@144
   219
        if (Val == null) return IDENT;
jtulach@144
   220
        return Val.intValue();
jtulach@144
   221
  }
jtulach@144
   222
jtulach@144
   223
  private static void defineKeyword(String id, int token) {
jtulach@144
   224
        keywords.put(id, new Integer(token));
jtulach@144
   225
        defineKeywordName(id, token);
jtulach@144
   226
  }
jtulach@144
   227
  static {
jtulach@144
   228
        // Modifier keywords
jtulach@144
   229
        defineKeyword("private", PRIVATE);
jtulach@144
   230
        defineKeyword("public", PUBLIC);
jtulach@144
   231
        defineKeyword("protected",      PROTECTED);
jtulach@144
   232
        defineKeyword("static", STATIC);
jtulach@144
   233
        defineKeyword("transient",      TRANSIENT);
jtulach@144
   234
        defineKeyword("synchronized",   SYNCHRONIZED);
jtulach@144
   235
        defineKeyword("super",  SUPER);
jtulach@144
   236
        defineKeyword("native", NATIVE);
jtulach@144
   237
        defineKeyword("abstract",       ABSTRACT);
jtulach@144
   238
        defineKeyword("volatile", VOLATILE);
jtulach@144
   239
        defineKeyword("final",  FINAL);
jtulach@144
   240
        defineKeyword("interface",INTERFACE);
jtulach@144
   241
        defineKeyword("synthetic",SYNTHETIC);
jtulach@144
   242
        defineKeyword("strict",STRICT);
jtulach@144
   243
jtulach@144
   244
        // Declaration keywords
jtulach@144
   245
        defineKeyword("package",PACKAGE);
jtulach@144
   246
        defineKeyword("class",CLASS);
jtulach@144
   247
        defineKeyword("extends",EXTENDS);
jtulach@144
   248
        defineKeyword("implements",IMPLEMENTS);
jtulach@144
   249
        defineKeyword("const",  CONST);
jtulach@144
   250
        defineKeyword("throws",THROWS);
jtulach@144
   251
        defineKeyword("interface",INTERFACE);
jtulach@144
   252
        defineKeyword("Method",METHODREF);
jtulach@144
   253
        defineKeyword("Field",FIELDREF);
jtulach@144
   254
        defineKeyword("stack",STACK);
jtulach@144
   255
        defineKeyword("locals",LOCAL);
jtulach@144
   256
jtulach@144
   257
        // used in switchtables
jtulach@144
   258
        defineKeyword("default",        DEFAULT);
jtulach@144
   259
jtulach@144
   260
        // used in inner class declarations
jtulach@144
   261
        defineKeyword("InnerClass",     INNERCLASS);
jtulach@144
   262
        defineKeyword("of",     OF);
jtulach@144
   263
jtulach@144
   264
        // misc
jtulach@144
   265
        defineKeyword("bits",BITS);
jtulach@144
   266
        defineKeyword("Infinity",INF);
jtulach@144
   267
        defineKeyword("Inf",INF);
jtulach@144
   268
        defineKeyword("NaN",NAN);
jtulach@144
   269
  }
jtulach@144
   270
jtulach@144
   271
   /**
jtulach@144
   272
     * Define tag table.
jtulach@144
   273
     */
jtulach@144
   274
  private static Vector tagNames = new Vector(10);
jtulach@144
   275
  private static Hashtable Tags = new Hashtable(10);
jtulach@144
   276
  static {
jtulach@144
   277
        defineTag("Asciz",CONSTANT_UTF8);
jtulach@144
   278
        defineTag("int",CONSTANT_INTEGER);
jtulach@144
   279
        defineTag("float",CONSTANT_FLOAT);
jtulach@144
   280
        defineTag("long",CONSTANT_LONG);
jtulach@144
   281
        defineTag("double",CONSTANT_DOUBLE);
jtulach@144
   282
        defineTag("class",CONSTANT_CLASS);
jtulach@144
   283
        defineTag("String",CONSTANT_STRING);
jtulach@144
   284
        defineTag("Field",CONSTANT_FIELD);
jtulach@144
   285
        defineTag("Method",CONSTANT_METHOD);
jtulach@144
   286
        defineTag("InterfaceMethod",CONSTANT_INTERFACEMETHOD);
jtulach@144
   287
        defineTag("NameAndType",CONSTANT_NAMEANDTYPE);
jtulach@144
   288
  }
jtulach@144
   289
  private static void defineTag(String id, int val) {
jtulach@144
   290
        Tags.put(id, new Integer(val));
jtulach@144
   291
        if (val>=tagNames.size()) {
jtulach@144
   292
                tagNames.setSize(val+1);
jtulach@144
   293
        }
jtulach@144
   294
        tagNames.setElementAt(id, val);
jtulach@144
   295
  }
jtulach@144
   296
  public static String tagName(int tag) {
jtulach@144
   297
        if (tag>=tagNames.size()) return null;
jtulach@144
   298
        return (String)tagNames.elementAt(tag);
jtulach@144
   299
  }
jtulach@144
   300
  public static int tagValue(String idValue) {
jtulach@144
   301
        Integer Val=(Integer)(Tags.get(idValue));
jtulach@144
   302
        if (Val == null) return 0;
jtulach@144
   303
        return Val.intValue();
jtulach@144
   304
  }
jtulach@144
   305
jtulach@144
   306
   /**
jtulach@144
   307
     * Define type table. These types used in "newarray" instruction only.
jtulach@144
   308
     */
jtulach@144
   309
  private static Vector typeNames = new Vector(10);
jtulach@144
   310
  private static Hashtable Types = new Hashtable(10);
jtulach@144
   311
  static {
jtulach@144
   312
        defineType("int",T_INT);
jtulach@144
   313
        defineType("long",T_LONG);
jtulach@144
   314
        defineType("float",T_FLOAT);
jtulach@144
   315
        defineType("double",T_DOUBLE);
jtulach@144
   316
        defineType("class",T_CLASS);
jtulach@144
   317
        defineType("boolean",T_BOOLEAN);
jtulach@144
   318
        defineType("char",T_CHAR);
jtulach@144
   319
        defineType("byte",T_BYTE);
jtulach@144
   320
        defineType("short",T_SHORT);
jtulach@144
   321
  }
jtulach@144
   322
  private static void defineType(String id, int val) {
jtulach@144
   323
        Types.put(id, new Integer(val));
jtulach@144
   324
        if (val>=typeNames.size()) {
jtulach@144
   325
                typeNames.setSize(val+1);
jtulach@144
   326
        }
jtulach@144
   327
        typeNames.setElementAt(id, val);
jtulach@144
   328
  }
jtulach@144
   329
  public static int typeValue(String idValue) {
jtulach@144
   330
        Integer Val=(Integer)(Types.get(idValue));
jtulach@144
   331
        if (Val == null) return -1;
jtulach@144
   332
        return Val.intValue();
jtulach@144
   333
  }
jtulach@144
   334
  public static String typeName(int type) {
jtulach@144
   335
        if (type>=typeNames.size()) return null;
jtulach@144
   336
        return (String)typeNames.elementAt(type);
jtulach@144
   337
  }
jtulach@144
   338
jtulach@144
   339
   /**
jtulach@144
   340
     * Define MapTypes table.
jtulach@144
   341
     * These constants used in stackmap tables only.
jtulach@144
   342
     */
jtulach@144
   343
  private static Vector mapTypeNames = new Vector(10);
jtulach@144
   344
  private static Hashtable MapTypes = new Hashtable(10);
jtulach@144
   345
  static {
jtulach@144
   346
        defineMapType("bogus",             ITEM_Bogus);
jtulach@144
   347
        defineMapType("int",               ITEM_Integer);
jtulach@144
   348
        defineMapType("float",             ITEM_Float);
jtulach@144
   349
        defineMapType("double",            ITEM_Double);
jtulach@144
   350
        defineMapType("long",              ITEM_Long);
jtulach@144
   351
        defineMapType("null",              ITEM_Null);
jtulach@144
   352
        defineMapType("this",              ITEM_InitObject);
jtulach@144
   353
        defineMapType("CP",                ITEM_Object);
jtulach@144
   354
        defineMapType("uninitialized",     ITEM_NewObject);
jtulach@144
   355
  }
jtulach@144
   356
  private static void defineMapType(String id, int val) {
jtulach@144
   357
        MapTypes.put(id, new Integer(val));
jtulach@144
   358
        if (val>=mapTypeNames.size()) {
jtulach@144
   359
                mapTypeNames.setSize(val+1);
jtulach@144
   360
        }
jtulach@144
   361
        mapTypeNames.setElementAt(id, val);
jtulach@144
   362
  }
jtulach@144
   363
  public static int mapTypeValue(String idValue) {
jtulach@144
   364
        Integer Val=(Integer)(MapTypes.get(idValue));
jtulach@144
   365
        if (Val == null) return -1;
jtulach@144
   366
        return Val.intValue();
jtulach@144
   367
  }
jtulach@144
   368
  public static String mapTypeName(int type) {
jtulach@144
   369
        if (type>=mapTypeNames.size()) return null;
jtulach@144
   370
        return (String)mapTypeNames.elementAt(type);
jtulach@144
   371
  }
jtulach@144
   372
jtulach@144
   373
}