1 /*
  2  * Copyright (c) 1999, 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 #include "precompiled.hpp"
 26 #include "ci/ciInstanceKlass.hpp"
 27 #include "ci/ciObjArrayKlass.hpp"
 28 #include "ci/ciSymbol.hpp"
 29 #include "ci/ciUtilities.inline.hpp"
 30 #include "oops/inlineKlass.inline.hpp"
 31 #include "oops/objArrayKlass.hpp"
 32 #include "runtime/signature.hpp"
 33 
 34 // ciObjArrayKlass
 35 //
 36 // This class represents a Klass* in the HotSpot virtual machine
 37 // whose Klass part is an ObjArrayKlass.
 38 
 39 // ------------------------------------------------------------------
 40 // ciObjArrayKlass::ciObjArrayKlass
 41 //
 42 // Constructor for loaded object array klasses.
 43 ciObjArrayKlass::ciObjArrayKlass(Klass* k) : ciArrayKlass(k) {
 44   assert(get_Klass()->is_objArray_klass(), "wrong type");
 45   Klass* element_Klass = get_ObjArrayKlass()->bottom_klass();
 46   _base_element_klass = CURRENT_ENV->get_klass(element_Klass);
 47   assert(_base_element_klass->is_instance_klass() ||
 48          _base_element_klass->is_type_array_klass(), "bad base klass");
 49   if (dimension() == 1) {
 50     _element_klass = _base_element_klass;
 51   } else {
 52     _element_klass = NULL;
 53   }
 54   if (!ciObjectFactory::is_initialized()) {
 55     assert(_element_klass->is_java_lang_Object(), "only arrays of object are shared");
 56   }
 57   _null_free = k->name()->is_Q_array_signature() && k->name()->char_at(1) == JVM_SIGNATURE_INLINE_TYPE;
 58 }
 59 
 60 // ------------------------------------------------------------------
 61 // ciObjArrayKlass::ciObjArrayKlass
 62 //
 63 // Constructor for unloaded object array klasses.
 64 ciObjArrayKlass::ciObjArrayKlass(ciSymbol* array_name,
 65                                  ciKlass* base_element_klass,
 66                                  int dimension)
 67   : ciArrayKlass(array_name,
 68                  dimension, T_OBJECT) {
 69   _base_element_klass = base_element_klass;
 70   assert(_base_element_klass->is_instance_klass() ||
 71          _base_element_klass->is_type_array_klass() ||
 72          _base_element_klass->is_flat_array_klass(), "bad base klass");
 73   if (dimension == 1) {
 74     _element_klass = base_element_klass;
 75   } else {
 76     _element_klass = NULL;
 77   }
 78   _null_free = array_name->is_Q_array_signature() && array_name->char_at(1) == JVM_SIGNATURE_INLINE_TYPE;
 79 }
 80 
 81 // ------------------------------------------------------------------
 82 // ciObjArrayKlass::element_klass
 83 //
 84 // What is the one-level element type of this array?
 85 ciKlass* ciObjArrayKlass::element_klass() {
 86   if (_element_klass == NULL) {
 87     assert(dimension() > 1, "_element_klass should not be NULL");
 88     // Produce the element klass.
 89     if (is_loaded()) {
 90       VM_ENTRY_MARK;
 91       Klass* element_Klass = get_ObjArrayKlass()->element_klass();
 92       _element_klass = CURRENT_THREAD_ENV->get_klass(element_Klass);
 93     } else {
 94       VM_ENTRY_MARK;
 95       // We are an unloaded array klass.  Attempt to fetch our
 96       // element klass by name.
 97       _element_klass = CURRENT_THREAD_ENV->get_klass_by_name_impl(
 98                           this,
 99                           constantPoolHandle(),
100                           construct_array_name(base_element_klass()->name(),
101                                                dimension() - 1),
102                           false);
103     }
104   }
105   return _element_klass;
106 }
107 
108 // ------------------------------------------------------------------
109 // ciObjArrayKlass::construct_array_name
110 //
111 // Build an array name from an element name and a dimension.
112 ciSymbol* ciObjArrayKlass::construct_array_name(ciSymbol* element_name,
113                                                 int dimension) {
114   EXCEPTION_CONTEXT;
115   int element_len = element_name->utf8_length();
116   int buflen = dimension + element_len + 3;  // '['+ + 'L'? + (element) + ';'? + '\0'
117   char* name = CURRENT_THREAD_ENV->name_buffer(buflen);
118   int pos = 0;
119   for ( ; pos < dimension; pos++) {
120     name[pos] = JVM_SIGNATURE_ARRAY;
121   }
122   Symbol* base_name_sym = element_name->get_symbol();
123   assert(base_name_sym->char_at(0) != JVM_SIGNATURE_INLINE_TYPE, "unloaded array klass element should not have Q-type");
124   if (Signature::is_array(base_name_sym) ||
125       Signature::has_envelope(base_name_sym)) {
126     strncpy(&name[pos], (char*)element_name->base(), element_len);
127     name[pos + element_len] = '\0';
128   } else {
129     name[pos++] = JVM_SIGNATURE_CLASS;
130     strncpy(&name[pos], (char*)element_name->base(), element_len);
131     name[pos + element_len] = JVM_SIGNATURE_ENDCLASS;
132     name[pos + element_len + 1] = '\0';
133   }
134   return ciSymbol::make(name);
135 }
136 
137 // ------------------------------------------------------------------
138 // ciObjArrayKlass::make_impl
139 //
140 // Implementation of make.
141 ciObjArrayKlass* ciObjArrayKlass::make_impl(ciKlass* element_klass) {
142   if (element_klass->is_loaded()) {
143     EXCEPTION_CONTEXT;
144     // The element klass is loaded
145     Klass* array = element_klass->get_Klass()->array_klass(THREAD);
146     if (HAS_PENDING_EXCEPTION) {
147       CLEAR_PENDING_EXCEPTION;
148       CURRENT_THREAD_ENV->record_out_of_memory_failure();
149       return ciEnv::unloaded_ciobjarrayklass();
150     }
151     return CURRENT_THREAD_ENV->get_obj_array_klass(array);
152   }
153 
154   // The array klass was unable to be made or the element klass was not loaded.
155   ciSymbol* array_name = construct_array_name(element_klass->name(), 1);
156   if (array_name == ciEnv::unloaded_cisymbol()) {
157     return ciEnv::unloaded_ciobjarrayklass();
158   }
159   return
160     CURRENT_ENV->get_unloaded_klass(element_klass, array_name)
161                         ->as_obj_array_klass();
162 }
163 
164 // ------------------------------------------------------------------
165 // ciObjArrayKlass::make
166 //
167 // Make an array klass corresponding to the specified primitive type.
168 ciObjArrayKlass* ciObjArrayKlass::make(ciKlass* element_klass) {
169   GUARDED_VM_ENTRY(return make_impl(element_klass);)
170 }
171 
172 ciKlass* ciObjArrayKlass::exact_klass() {
173   // Even if MyValue is exact, [LMyValue is not exact due to [QMyValue <: [LMyValue.
174   if (!is_elem_null_free() && (!is_loaded() || element_klass()->is_inlinetype())) {
175     return NULL;
176   }
177   ciType* base = base_element_type();
178   if (base->is_instance_klass()) {
179     ciInstanceKlass* ik = base->as_instance_klass();
180     if (ik->exact_klass() != NULL) {
181       return this;
182     }
183   } else if (base->is_primitive_type()) {
184     return this;
185   }
186   return NULL;
187 }