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