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