javap/src/main/java/org/apidesign/javap/Tables.java
branchjavap
changeset 167 77f7135b6eb1
parent 149 32653a09f0db
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/javap/src/main/java/org/apidesign/javap/Tables.java	Fri Nov 16 08:08:36 2012 +0100
     1.3 @@ -0,0 +1,373 @@
     1.4 +/*
     1.5 + * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +
    1.30 +package org.apidesign.javap;
    1.31 +
    1.32 +
    1.33 +
    1.34 +public class Tables implements Constants {
    1.35 +    /**
    1.36 +     * Define mnemocodes table.
    1.37 +     */
    1.38 +  static  Hashtable mnemocodes = new Hashtable(301, 0.5f);
    1.39 +  static  String opcExtNamesTab[]=new String[128];
    1.40 +  static  String opcPrivExtNamesTab[]=new String[128];
    1.41 +  static  void defineNonPriv(int opc, String mnem) {
    1.42 +        mnemocodes.put(opcExtNamesTab[opc]=mnem, new Integer(opc_nonpriv*256+opc));
    1.43 +  }
    1.44 +  static  void definePriv(int opc, String mnem) {
    1.45 +        mnemocodes.put(opcPrivExtNamesTab[opc]="priv_"+mnem, new Integer(opc_priv*256+opc));
    1.46 +  }
    1.47 +  static  void defineExt(int opc, String mnem) {
    1.48 +        defineNonPriv(opc, mnem);
    1.49 +        definePriv(opc, mnem);
    1.50 +  }
    1.51 +  static { int k;
    1.52 +        for (k=0; k<opc_wide; k++) {
    1.53 +                mnemocodes.put(opcNamesTab[k], new Integer(k));
    1.54 +        }
    1.55 +        for (k=opc_wide+1; k<opcNamesTab.length; k++) {
    1.56 +                mnemocodes.put(opcNamesTab[k], new Integer(k));
    1.57 +        }
    1.58 +        mnemocodes.put("invokenonvirtual", new Integer(opc_invokespecial));
    1.59 +
    1.60 +        mnemocodes.put("iload_w", new Integer(opc_iload_w));
    1.61 +        mnemocodes.put("lload_w", new Integer(opc_lload_w));
    1.62 +        mnemocodes.put("fload_w", new Integer(opc_fload_w));
    1.63 +        mnemocodes.put("dload_w", new Integer(opc_dload_w));
    1.64 +        mnemocodes.put("aload_w", new Integer(opc_aload_w));
    1.65 +        mnemocodes.put("istore_w", new Integer(opc_istore_w));
    1.66 +        mnemocodes.put("lstore_w", new Integer(opc_lstore_w));
    1.67 +        mnemocodes.put("fstore_w", new Integer(opc_fstore_w));
    1.68 +        mnemocodes.put("dstore_w", new Integer(opc_dstore_w));
    1.69 +        mnemocodes.put("astore_w", new Integer(opc_astore_w));
    1.70 +        mnemocodes.put("ret_w", new Integer(opc_ret_w));
    1.71 +        mnemocodes.put("iinc_w", new Integer(opc_iinc_w));
    1.72 +
    1.73 +        mnemocodes.put("nonpriv", new Integer(opc_nonpriv));
    1.74 +        mnemocodes.put("priv", new Integer(opc_priv));
    1.75 +
    1.76 +        defineExt(0, "load_ubyte");
    1.77 +        defineExt(1, "load_byte");
    1.78 +        defineExt(2, "load_char");
    1.79 +        defineExt(3, "load_short");
    1.80 +        defineExt(4, "load_word");
    1.81 +        defineExt(10, "load_char_oe");
    1.82 +        defineExt(11, "load_short_oe");
    1.83 +        defineExt(12, "load_word_oe");
    1.84 +        defineExt(16, "ncload_ubyte");
    1.85 +        defineExt(17, "ncload_byte");
    1.86 +        defineExt(18, "ncload_char");
    1.87 +        defineExt(19, "ncload_short");
    1.88 +        defineExt(20, "ncload_word");
    1.89 +        defineExt(26, "ncload_char_oe");
    1.90 +        defineExt(27, "ncload_short_oe");
    1.91 +        defineExt(28, "ncload_word_oe");
    1.92 +        defineExt(30, "cache_flush");
    1.93 +        defineExt(32, "store_byte");
    1.94 +        defineExt(34, "store_short");
    1.95 +        defineExt(36, "store_word");
    1.96 +        defineExt(42, "store_short_oe");
    1.97 +        defineExt(44, "store_word_oe");
    1.98 +        defineExt(48, "ncstore_byte");
    1.99 +        defineExt(50, "ncstore_short");
   1.100 +        defineExt(52, "ncstore_word");
   1.101 +        defineExt(58, "ncstore_short_oe");
   1.102 +        defineExt(60, "ncstore_word_oe");
   1.103 +        defineExt(62, "zero_line");
   1.104 +        defineNonPriv(5, "ret_from_sub");
   1.105 +        defineNonPriv(63, "enter_sync_method");
   1.106 +        definePriv(5, "ret_from_trap");
   1.107 +        definePriv(6, "read_dcache_tag");
   1.108 +        definePriv(7, "read_dcache_data");
   1.109 +        definePriv(14, "read_icache_tag");
   1.110 +        definePriv(15, "read_icache_data");
   1.111 +        definePriv(22, "powerdown");
   1.112 +        definePriv(23, "read_scache_data");
   1.113 +        definePriv(31, "cache_index_flush");
   1.114 +        definePriv(38, "write_dcache_tag");
   1.115 +        definePriv(39, "write_dcache_data");
   1.116 +        definePriv(46, "write_icache_tag");
   1.117 +        definePriv(47, "write_icache_data");
   1.118 +        definePriv(54, "reset");
   1.119 +        definePriv(55, "write_scache_data");
   1.120 +        for (k=0; k<32; k++) {
   1.121 +                definePriv(k+64, "read_reg_"+k);
   1.122 +        }
   1.123 +        for (k=0; k<32; k++) {
   1.124 +                definePriv(k+96, "write_reg_"+k);
   1.125 +        }
   1.126 + }
   1.127 +
   1.128 +  public static int opcLength(int opc) throws ArrayIndexOutOfBoundsException {
   1.129 +        switch (opc>>8) {
   1.130 +          case 0:
   1.131 +                return opcLengthsTab[opc];
   1.132 +          case opc_wide:
   1.133 +                switch (opc&0xFF) {
   1.134 +                  case opc_aload: case opc_astore:
   1.135 +                  case opc_fload: case opc_fstore:
   1.136 +                  case opc_iload: case opc_istore:
   1.137 +                  case opc_lload: case opc_lstore:
   1.138 +                  case opc_dload: case opc_dstore:
   1.139 +                  case opc_ret:
   1.140 +                        return  4;
   1.141 +                  case opc_iinc:
   1.142 +                        return  6;
   1.143 +                  default:
   1.144 +                        throw new ArrayIndexOutOfBoundsException();
   1.145 +                }
   1.146 +          case opc_nonpriv:
   1.147 +          case opc_priv:
   1.148 +                return 2;
   1.149 +          default:
   1.150 +                throw new ArrayIndexOutOfBoundsException();
   1.151 +        }
   1.152 +  }
   1.153 +
   1.154 +  public static String opcName(int opc) {
   1.155 +        try {
   1.156 +                switch (opc>>8) {
   1.157 +                  case 0:
   1.158 +                        return opcNamesTab[opc];
   1.159 +                  case opc_wide: {
   1.160 +                        String mnem=opcNamesTab[opc&0xFF]+"_w";
   1.161 +                        if (mnemocodes.get(mnem) == null)
   1.162 +                                return null; // non-existent opcode
   1.163 +                        return mnem;
   1.164 +                  }
   1.165 +                  case opc_nonpriv:
   1.166 +                        return opcExtNamesTab[opc&0xFF];
   1.167 +                  case opc_priv:
   1.168 +                        return opcPrivExtNamesTab[opc&0xFF];
   1.169 +                  default:
   1.170 +                        return null;
   1.171 +                }
   1.172 +        } catch (ArrayIndexOutOfBoundsException e) {
   1.173 +                switch (opc) {
   1.174 +                  case opc_nonpriv:
   1.175 +                        return "nonpriv";
   1.176 +                  case opc_priv:
   1.177 +                        return "priv";
   1.178 +                  default:
   1.179 +                        return null;
   1.180 +                }
   1.181 +        }
   1.182 +  }
   1.183 +
   1.184 +  public static int opcode(String mnem) {
   1.185 +        Integer Val=(Integer)(mnemocodes.get(mnem));
   1.186 +        if (Val == null) return -1;
   1.187 +        return Val.intValue();
   1.188 +  }
   1.189 +
   1.190 +    /**
   1.191 +     * Initialized keyword and token Hashtables
   1.192 +     */
   1.193 +  static Vector keywordNames = new Vector(40);
   1.194 +  private static void defineKeywordName(String id, int token) {
   1.195 +
   1.196 +        if (token>=keywordNames.size()) {
   1.197 +                keywordNames.setSize(token+1);
   1.198 +        }
   1.199 +        keywordNames.setElementAt(id, token);
   1.200 +  }
   1.201 +  public static String keywordName(int token) {
   1.202 +        if (token==-1) return "EOF";
   1.203 +        if (token>=keywordNames.size()) return null;
   1.204 +        return (String)keywordNames.elementAt(token);
   1.205 +  }
   1.206 +  static {
   1.207 +        defineKeywordName("ident", IDENT);
   1.208 +        defineKeywordName("STRINGVAL", STRINGVAL);
   1.209 +        defineKeywordName("intVal", INTVAL);
   1.210 +        defineKeywordName("longVal", LONGVAL);
   1.211 +        defineKeywordName("floatVal", FLOATVAL);
   1.212 +        defineKeywordName("doubleVal", DOUBLEVAL);
   1.213 +        defineKeywordName("SEMICOLON", SEMICOLON);
   1.214 +        defineKeywordName("COLON", COLON);
   1.215 +        defineKeywordName("LBRACE", LBRACE);
   1.216 +        defineKeywordName("RBRACE", RBRACE);
   1.217 +  }
   1.218 +
   1.219 +  static Hashtable keywords = new Hashtable(40);
   1.220 +  public static int keyword(String idValue) {
   1.221 +        Integer Val=(Integer)(keywords.get(idValue));
   1.222 +        if (Val == null) return IDENT;
   1.223 +        return Val.intValue();
   1.224 +  }
   1.225 +
   1.226 +  private static void defineKeyword(String id, int token) {
   1.227 +        keywords.put(id, new Integer(token));
   1.228 +        defineKeywordName(id, token);
   1.229 +  }
   1.230 +  static {
   1.231 +        // Modifier keywords
   1.232 +        defineKeyword("private", PRIVATE);
   1.233 +        defineKeyword("public", PUBLIC);
   1.234 +        defineKeyword("protected",      PROTECTED);
   1.235 +        defineKeyword("static", STATIC);
   1.236 +        defineKeyword("transient",      TRANSIENT);
   1.237 +        defineKeyword("synchronized",   SYNCHRONIZED);
   1.238 +        defineKeyword("super",  SUPER);
   1.239 +        defineKeyword("native", NATIVE);
   1.240 +        defineKeyword("abstract",       ABSTRACT);
   1.241 +        defineKeyword("volatile", VOLATILE);
   1.242 +        defineKeyword("final",  FINAL);
   1.243 +        defineKeyword("interface",INTERFACE);
   1.244 +        defineKeyword("synthetic",SYNTHETIC);
   1.245 +        defineKeyword("strict",STRICT);
   1.246 +
   1.247 +        // Declaration keywords
   1.248 +        defineKeyword("package",PACKAGE);
   1.249 +        defineKeyword("class",CLASS);
   1.250 +        defineKeyword("extends",EXTENDS);
   1.251 +        defineKeyword("implements",IMPLEMENTS);
   1.252 +        defineKeyword("const",  CONST);
   1.253 +        defineKeyword("throws",THROWS);
   1.254 +        defineKeyword("interface",INTERFACE);
   1.255 +        defineKeyword("Method",METHODREF);
   1.256 +        defineKeyword("Field",FIELDREF);
   1.257 +        defineKeyword("stack",STACK);
   1.258 +        defineKeyword("locals",LOCAL);
   1.259 +
   1.260 +        // used in switchtables
   1.261 +        defineKeyword("default",        DEFAULT);
   1.262 +
   1.263 +        // used in inner class declarations
   1.264 +        defineKeyword("InnerClass",     INNERCLASS);
   1.265 +        defineKeyword("of",     OF);
   1.266 +
   1.267 +        // misc
   1.268 +        defineKeyword("bits",BITS);
   1.269 +        defineKeyword("Infinity",INF);
   1.270 +        defineKeyword("Inf",INF);
   1.271 +        defineKeyword("NaN",NAN);
   1.272 +  }
   1.273 +
   1.274 +   /**
   1.275 +     * Define tag table.
   1.276 +     */
   1.277 +  private static Vector tagNames = new Vector(10);
   1.278 +  private static Hashtable Tags = new Hashtable(10);
   1.279 +  static {
   1.280 +        defineTag("Asciz",CONSTANT_UTF8);
   1.281 +        defineTag("int",CONSTANT_INTEGER);
   1.282 +        defineTag("float",CONSTANT_FLOAT);
   1.283 +        defineTag("long",CONSTANT_LONG);
   1.284 +        defineTag("double",CONSTANT_DOUBLE);
   1.285 +        defineTag("class",CONSTANT_CLASS);
   1.286 +        defineTag("String",CONSTANT_STRING);
   1.287 +        defineTag("Field",CONSTANT_FIELD);
   1.288 +        defineTag("Method",CONSTANT_METHOD);
   1.289 +        defineTag("InterfaceMethod",CONSTANT_INTERFACEMETHOD);
   1.290 +        defineTag("NameAndType",CONSTANT_NAMEANDTYPE);
   1.291 +  }
   1.292 +  private static void defineTag(String id, int val) {
   1.293 +        Tags.put(id, new Integer(val));
   1.294 +        if (val>=tagNames.size()) {
   1.295 +                tagNames.setSize(val+1);
   1.296 +        }
   1.297 +        tagNames.setElementAt(id, val);
   1.298 +  }
   1.299 +  public static String tagName(int tag) {
   1.300 +        if (tag>=tagNames.size()) return null;
   1.301 +        return (String)tagNames.elementAt(tag);
   1.302 +  }
   1.303 +  public static int tagValue(String idValue) {
   1.304 +        Integer Val=(Integer)(Tags.get(idValue));
   1.305 +        if (Val == null) return 0;
   1.306 +        return Val.intValue();
   1.307 +  }
   1.308 +
   1.309 +   /**
   1.310 +     * Define type table. These types used in "newarray" instruction only.
   1.311 +     */
   1.312 +  private static Vector typeNames = new Vector(10);
   1.313 +  private static Hashtable Types = new Hashtable(10);
   1.314 +  static {
   1.315 +        defineType("int",T_INT);
   1.316 +        defineType("long",T_LONG);
   1.317 +        defineType("float",T_FLOAT);
   1.318 +        defineType("double",T_DOUBLE);
   1.319 +        defineType("class",T_CLASS);
   1.320 +        defineType("boolean",T_BOOLEAN);
   1.321 +        defineType("char",T_CHAR);
   1.322 +        defineType("byte",T_BYTE);
   1.323 +        defineType("short",T_SHORT);
   1.324 +  }
   1.325 +  private static void defineType(String id, int val) {
   1.326 +        Types.put(id, new Integer(val));
   1.327 +        if (val>=typeNames.size()) {
   1.328 +                typeNames.setSize(val+1);
   1.329 +        }
   1.330 +        typeNames.setElementAt(id, val);
   1.331 +  }
   1.332 +  public static int typeValue(String idValue) {
   1.333 +        Integer Val=(Integer)(Types.get(idValue));
   1.334 +        if (Val == null) return -1;
   1.335 +        return Val.intValue();
   1.336 +  }
   1.337 +  public static String typeName(int type) {
   1.338 +        if (type>=typeNames.size()) return null;
   1.339 +        return (String)typeNames.elementAt(type);
   1.340 +  }
   1.341 +
   1.342 +   /**
   1.343 +     * Define MapTypes table.
   1.344 +     * These constants used in stackmap tables only.
   1.345 +     */
   1.346 +  private static Vector mapTypeNames = new Vector(10);
   1.347 +  private static Hashtable MapTypes = new Hashtable(10);
   1.348 +  static {
   1.349 +        defineMapType("bogus",             ITEM_Bogus);
   1.350 +        defineMapType("int",               ITEM_Integer);
   1.351 +        defineMapType("float",             ITEM_Float);
   1.352 +        defineMapType("double",            ITEM_Double);
   1.353 +        defineMapType("long",              ITEM_Long);
   1.354 +        defineMapType("null",              ITEM_Null);
   1.355 +        defineMapType("this",              ITEM_InitObject);
   1.356 +        defineMapType("CP",                ITEM_Object);
   1.357 +        defineMapType("uninitialized",     ITEM_NewObject);
   1.358 +  }
   1.359 +  private static void defineMapType(String id, int val) {
   1.360 +        MapTypes.put(id, new Integer(val));
   1.361 +        if (val>=mapTypeNames.size()) {
   1.362 +                mapTypeNames.setSize(val+1);
   1.363 +        }
   1.364 +        mapTypeNames.setElementAt(id, val);
   1.365 +  }
   1.366 +  public static int mapTypeValue(String idValue) {
   1.367 +        Integer Val=(Integer)(MapTypes.get(idValue));
   1.368 +        if (Val == null) return -1;
   1.369 +        return Val.intValue();
   1.370 +  }
   1.371 +  public static String mapTypeName(int type) {
   1.372 +        if (type>=mapTypeNames.size()) return null;
   1.373 +        return (String)mapTypeNames.elementAt(type);
   1.374 +  }
   1.375 +
   1.376 +}