1 /*
   2  * Copyright (c) 2000, 2017, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.runtime;
  26 
  27 import java.util.Observer;
  28 import sun.jvm.hotspot.types.TypeDataBase;
  29 
  30 
  31 /** Encapsulates the BasicType enum in globalDefinitions.hpp in the
  32     VM. */
  33 
  34 public class BasicType {
  35   private static int tBoolean;
  36   private static int tChar;
  37   private static int tFloat;
  38   private static int tDouble;
  39   private static int tByte;
  40   private static int tShort;
  41   private static int tInt;
  42   private static int tLong;
  43   private static int tObject;
  44   private static int tArray;
  45   private static int tVoid;
  46   private static int tAddress;
  47   private static int tNarrowOop;
  48   private static int tMetadata;
  49   private static int tNarrowKlass;
  50   private static int tConflict;
  51   private static int tIllegal;
  52 
  53   public static BasicType T_BOOLEAN;
  54   public static BasicType T_CHAR;
  55   public static BasicType T_FLOAT;
  56   public static BasicType T_DOUBLE;
  57   public static BasicType T_BYTE;
  58   public static BasicType T_SHORT;
  59   public static BasicType T_INT;
  60   public static BasicType T_LONG;
  61   public static BasicType T_OBJECT;
  62   public static BasicType T_ARRAY;
  63   public static BasicType T_VOID;
  64   public static BasicType T_ADDRESS;
  65   public static BasicType T_NARROWOOP;
  66   public static BasicType T_METADATA;
  67   public static BasicType T_NARROWKLASS;
  68   public static BasicType T_CONFLICT;
  69   public static BasicType T_ILLEGAL;
  70 
  71   static {
  72     VM.registerVMInitializedObserver(
  73         (o, d) -> initialize(VM.getVM().getTypeDataBase()));
  74   }
  75 
  76   private static synchronized void initialize(TypeDataBase db) {
  77     tBoolean     = db.lookupIntConstant("T_BOOLEAN").intValue();
  78     tChar        = db.lookupIntConstant("T_CHAR").intValue();
  79     tFloat       = db.lookupIntConstant("T_FLOAT").intValue();
  80     tDouble      = db.lookupIntConstant("T_DOUBLE").intValue();
  81     tByte        = db.lookupIntConstant("T_BYTE").intValue();
  82     tShort       = db.lookupIntConstant("T_SHORT").intValue();
  83     tInt         = db.lookupIntConstant("T_INT").intValue();
  84     tLong        = db.lookupIntConstant("T_LONG").intValue();
  85     tObject      = db.lookupIntConstant("T_OBJECT").intValue();
  86     tArray       = db.lookupIntConstant("T_ARRAY").intValue();
  87     tVoid        = db.lookupIntConstant("T_VOID").intValue();
  88     tAddress     = db.lookupIntConstant("T_ADDRESS").intValue();
  89     tNarrowOop   = db.lookupIntConstant("T_NARROWOOP").intValue();
  90     tMetadata    = db.lookupIntConstant("T_METADATA").intValue();
  91     tNarrowKlass = db.lookupIntConstant("T_NARROWKLASS").intValue();
  92     tConflict    = db.lookupIntConstant("T_CONFLICT").intValue();
  93     tIllegal     = db.lookupIntConstant("T_ILLEGAL").intValue();
  94 
  95     T_BOOLEAN = new BasicType(tBoolean);
  96     T_CHAR = new BasicType(tChar);
  97     T_FLOAT = new BasicType(tFloat);
  98     T_DOUBLE = new BasicType(tDouble);
  99     T_BYTE = new BasicType(tByte);
 100     T_SHORT = new BasicType(tShort);
 101     T_INT = new BasicType(tInt);
 102     T_LONG = new BasicType(tLong);
 103     T_OBJECT = new BasicType(tObject);
 104     T_ARRAY = new BasicType(tArray);
 105     T_VOID = new BasicType(tVoid);
 106     T_ADDRESS = new BasicType(tAddress);
 107     T_NARROWOOP = new BasicType(tNarrowOop);
 108     T_METADATA = new BasicType(tMetadata);
 109     T_NARROWKLASS = new BasicType(tNarrowKlass);
 110     T_CONFLICT = new BasicType(tConflict);
 111     T_ILLEGAL = new BasicType(tIllegal);
 112   }
 113 
 114   public static int getTBoolean() {
 115     return tBoolean;
 116   }
 117 
 118   public static int getTChar() {
 119     return tChar;
 120   }
 121 
 122   public static int getTFloat() {
 123     return tFloat;
 124   }
 125 
 126   public static int getTDouble() {
 127     return tDouble;
 128   }
 129 
 130   public static int getTByte() {
 131     return tByte;
 132   }
 133 
 134   public static int getTShort() {
 135     return tShort;
 136   }
 137 
 138   public static int getTInt() {
 139     return tInt;
 140   }
 141 
 142   public static int getTLong() {
 143     return tLong;
 144   }
 145 
 146   public static int getTObject() {
 147     return tObject;
 148   }
 149 
 150   public static int getTArray() {
 151     return tArray;
 152   }
 153 
 154   public static int getTVoid() {
 155     return tVoid;
 156   }
 157 
 158   public static int getTAddress() {
 159     return tAddress;
 160   }
 161 
 162   public static int getTNarrowOop() {
 163     return tNarrowOop;
 164   }
 165 
 166   public static int getTMetadata() {
 167     return tMetadata;
 168   }
 169 
 170   public static int getTNarrowKlass() {
 171     return tNarrowKlass;
 172   }
 173 
 174   /** For stack value type with conflicting contents */
 175   public static int getTConflict() {
 176     return tConflict;
 177   }
 178 
 179   public static int getTIllegal() {
 180     return tIllegal;
 181   }
 182 
 183   public static BasicType intToBasicType(int i) {
 184     if (i == tBoolean) {
 185       return T_BOOLEAN;
 186     } else if (i == tChar) {
 187       return T_CHAR;
 188     } else if (i == tFloat) {
 189       return T_FLOAT;
 190     } else if (i == tDouble) {
 191       return T_DOUBLE;
 192     } else if (i == tByte) {
 193       return T_BYTE;
 194     } else if (i == tShort) {
 195       return T_SHORT;
 196     } else if (i == tInt) {
 197       return T_INT;
 198     } else if (i == tLong) {
 199       return T_LONG;
 200     } else if (i == tObject) {
 201       return T_OBJECT;
 202     } else if (i == tArray) {
 203       return T_ARRAY;
 204     } else if (i == tVoid) {
 205       return T_VOID;
 206     } else if (i == tAddress) {
 207       return T_ADDRESS;
 208     } else if (i == tNarrowOop) {
 209       return T_NARROWOOP;
 210     } else if (i == tMetadata) {
 211       return T_METADATA;
 212     } else if (i == tNarrowKlass) {
 213       return T_NARROWKLASS;
 214     } else {
 215       return T_ILLEGAL;
 216     }
 217   }
 218 
 219   public static BasicType charToBasicType(char c) {
 220     switch( c ) {
 221     case 'B': return T_BYTE;
 222     case 'C': return T_CHAR;
 223     case 'D': return T_DOUBLE;
 224     case 'F': return T_FLOAT;
 225     case 'I': return T_INT;
 226     case 'J': return T_LONG;
 227     case 'S': return T_SHORT;
 228     case 'Z': return T_BOOLEAN;
 229     case 'V': return T_VOID;
 230     case 'L': return T_OBJECT;
 231     case '[': return T_ARRAY;
 232     }
 233     return T_ILLEGAL;
 234   }
 235 
 236   public static int charToType(char c) {
 237     return charToBasicType(c).getType();
 238   }
 239 
 240   public int getType() {
 241     return type;
 242   }
 243 
 244   public String getName() {
 245     if (type == tBoolean) {
 246       return "boolean";
 247     } else if (type == tChar) {
 248       return "char";
 249     } else if (type == tFloat) {
 250       return "float";
 251     } else if (type == tDouble) {
 252       return "double";
 253     } else if (type == tByte) {
 254       return "byte";
 255     } else if (type == tShort) {
 256       return "short";
 257     } else if (type == tInt) {
 258       return "int";
 259     } else if (type == tLong) {
 260       return "long";
 261     } else if (type == tObject) {
 262       return "object";
 263     } else if (type == tArray) {
 264       return "array";
 265     } else if (type == tVoid) {
 266       return "void";
 267     } else if (type == tAddress) {
 268       return "address";
 269     } else if (type == tNarrowOop) {
 270       return "narrow oop";
 271     } else if (type == tMetadata) {
 272       return "metadata";
 273     } else if (type == tNarrowKlass) {
 274       return "narrow klass";
 275     } else if (type == tConflict) {
 276       return "conflict";
 277     } else {
 278       return "ILLEGAL TYPE";
 279     }
 280   }
 281 
 282   //-- Internals only below this point
 283   private BasicType(int type) {
 284     this.type = type;
 285   }
 286 
 287   private int type;
 288 }