1 /*
  2  * Copyright (c) 1997, 2021, 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 #ifndef SHARE_UTILITIES_CONSTANTTAG_HPP
 26 #define SHARE_UTILITIES_CONSTANTTAG_HPP
 27 
 28 #include "utilities/globalDefinitions.hpp"
 29 
 30 
 31 class outputStream;
 32 
 33 // constant tags in Java .class files
 34 
 35 enum {
 36   // See jvm.h for shared JVM_CONSTANT_XXX tags
 37   // NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/utilities/ConstantTag.java
 38   // Hotspot specific tags
 39   JVM_CONSTANT_Invalid                  = 0,    // For bad value initialization
 40   JVM_CONSTANT_InternalMin              = 100,  // First implementation tag (aside from bad value of course)
 41   JVM_CONSTANT_UnresolvedClass          = 100,  // Temporary tag until actual use
 42   JVM_CONSTANT_ClassIndex               = 101,  // Temporary tag while constructing constant pool, class redefinition
 43   JVM_CONSTANT_StringIndex              = 102,  // Temporary tag while constructing constant pool, class redefinition
 44   JVM_CONSTANT_UnresolvedClassInError   = 103,  // Error tag due to resolution error
 45   JVM_CONSTANT_MethodHandleInError      = 104,  // Error tag due to resolution error
 46   JVM_CONSTANT_MethodTypeInError        = 105,  // Error tag due to resolution error
 47   JVM_CONSTANT_DynamicInError           = 106,  // Error tag due to resolution error
 48   JVM_CONSTANT_InternalMax              = 106,  // Last implementation tag
 49 };
 50 
 51 class constantTag {
 52  private:
 53   jbyte _tag;
 54  public:
 55   bool is_klass() const             { return value() == JVM_CONSTANT_Class; }
 56   bool is_field () const            { return _tag == JVM_CONSTANT_Fieldref; }
 57   bool is_method() const            { return _tag == JVM_CONSTANT_Methodref; }
 58   bool is_interface_method() const  { return _tag == JVM_CONSTANT_InterfaceMethodref; }
 59   bool is_string() const            { return _tag == JVM_CONSTANT_String; }
 60   bool is_int() const               { return _tag == JVM_CONSTANT_Integer; }
 61   bool is_float() const             { return _tag == JVM_CONSTANT_Float; }
 62   bool is_long() const              { return _tag == JVM_CONSTANT_Long; }
 63   bool is_double() const            { return _tag == JVM_CONSTANT_Double; }
 64   bool is_name_and_type() const     { return _tag == JVM_CONSTANT_NameAndType; }
 65   bool is_utf8() const              { return _tag == JVM_CONSTANT_Utf8; }
 66 
 67   bool is_invalid() const           { return _tag == JVM_CONSTANT_Invalid; }
 68 
 69   bool is_unresolved_klass() const {
 70     return value() == JVM_CONSTANT_UnresolvedClass || value() == JVM_CONSTANT_UnresolvedClassInError;
 71   }
 72 
 73   bool is_unresolved_klass_in_error() const {
 74     return value() == JVM_CONSTANT_UnresolvedClassInError;
 75   }
 76 
 77   bool is_method_handle_in_error() const {
 78     return _tag == JVM_CONSTANT_MethodHandleInError;
 79   }
 80   bool is_method_type_in_error() const {
 81     return _tag == JVM_CONSTANT_MethodTypeInError;
 82   }
 83 
 84   bool is_dynamic_constant_in_error() const {
 85     return _tag == JVM_CONSTANT_DynamicInError;
 86   }
 87 
 88   bool is_in_error() const {
 89     return is_unresolved_klass_in_error() ||
 90            is_method_handle_in_error()    ||
 91            is_method_type_in_error()      ||
 92            is_dynamic_constant_in_error();
 93   }
 94 
 95   bool is_klass_index() const       { return _tag == JVM_CONSTANT_ClassIndex; }
 96   bool is_string_index() const      { return _tag == JVM_CONSTANT_StringIndex; }
 97 
 98   bool is_klass_reference() const   { return is_klass_index() || is_unresolved_klass(); }
 99   bool is_klass_or_reference() const{ return is_klass() || is_klass_reference(); }
100   bool is_field_or_method() const   { return is_field() || is_method() || is_interface_method(); }
101   bool is_symbol() const            { return is_utf8(); }
102 
103   bool is_method_type() const       { return _tag == JVM_CONSTANT_MethodType; }
104   bool is_method_handle() const     { return _tag == JVM_CONSTANT_MethodHandle; }
105   bool is_dynamic_constant() const  { return _tag == JVM_CONSTANT_Dynamic; }
106   bool is_invoke_dynamic() const    { return _tag == JVM_CONSTANT_InvokeDynamic; }
107 
108   bool has_bootstrap() const {
109     return (_tag == JVM_CONSTANT_Dynamic ||
110             _tag == JVM_CONSTANT_DynamicInError ||
111             _tag == JVM_CONSTANT_InvokeDynamic);
112   }
113 
114   bool is_loadable_constant() const {
115     return ((_tag >= JVM_CONSTANT_Integer && _tag <= JVM_CONSTANT_String) ||
116             is_method_type() || is_method_handle() || is_dynamic_constant() ||
117             is_unresolved_klass());
118   }
119 
120   constantTag() {
121     _tag = JVM_CONSTANT_Invalid;
122   }
123   constantTag(jbyte tag) {
124     assert((tag >= 0 && tag <= JVM_CONSTANT_NameAndType) ||
125            (tag >= JVM_CONSTANT_MethodHandle && tag <= JVM_CONSTANT_InvokeDynamic) ||
126            (tag >= JVM_CONSTANT_InternalMin && tag <= JVM_CONSTANT_InternalMax), "Invalid constant tag");
127     _tag = tag;
128   }
129 
130   static jbyte type2tag(BasicType bt) {
131     if (is_subword_type(bt)) {
132       bt = T_INT;
133     }
134     if (bt == T_ARRAY) {
135       bt = T_OBJECT;
136     }
137     switch (bt) {
138       case T_INT:    return JVM_CONSTANT_Integer;
139       case T_LONG:   return JVM_CONSTANT_Long;
140       case T_FLOAT:  return JVM_CONSTANT_Float;
141       case T_DOUBLE: return JVM_CONSTANT_Double;
142       case T_OBJECT: return JVM_CONSTANT_String;
143 
144       default:
145         assert(false, "not supported: %s", type2name(bt));
146         return JVM_CONSTANT_Invalid;
147     }
148   }
149 
150   jbyte value() const                { return _tag; }
151   jbyte tag() const                  { return _tag; }
152   jbyte error_value() const;
153   jbyte non_error_value() const;
154 
155   BasicType basic_type() const;        // if used with ldc, what kind of value gets pushed?
156 
157   const char* internal_name() const;  // for error reporting
158 
159   void print_on(outputStream* st) const PRODUCT_RETURN;
160 };
161 
162 #endif // SHARE_UTILITIES_CONSTANTTAG_HPP