1 /*
  2  * Copyright (c) 1999, 2024, 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/ciKlass.hpp"
 27 #include "ci/ciSymbol.hpp"
 28 #include "ci/ciUtilities.inline.hpp"
 29 #include "oops/klass.inline.hpp"
 30 #include "oops/oop.inline.hpp"
 31 
 32 // ciKlass
 33 //
 34 // This class represents a Klass* in the HotSpot virtual
 35 // machine.
 36 
 37 // ------------------------------------------------------------------
 38 // ciKlass::ciKlass
 39 ciKlass::ciKlass(Klass* k) : ciType(k) {
 40   assert(get_Klass()->is_klass(), "wrong type");
 41   Klass* klass = get_Klass();
 42   _layout_helper = klass->layout_helper();
 43   Symbol* klass_name = klass->name();
 44   assert(klass_name != nullptr, "wrong ciKlass constructor");
 45   _name = CURRENT_ENV->get_symbol(klass_name);
 46 }
 47 
 48 // ------------------------------------------------------------------
 49 // ciKlass::ciKlass
 50 //
 51 // Nameless klass variant.
 52 ciKlass::ciKlass(Klass* k, ciSymbol* name) : ciType(k) {
 53   assert(get_Klass()->is_klass(), "wrong type");
 54   _name = name;
 55   _layout_helper = Klass::_lh_neutral_value;
 56 }
 57 
 58 // ------------------------------------------------------------------
 59 // ciKlass::ciKlass
 60 //
 61 // Unloaded klass variant.
 62 ciKlass::ciKlass(ciSymbol* name, BasicType bt) : ciType(bt) {
 63   _name = name;
 64   _layout_helper = Klass::_lh_neutral_value;
 65 }
 66 
 67 // ------------------------------------------------------------------
 68 // ciKlass::is_subtype_of
 69 bool ciKlass::is_subtype_of(ciKlass* that) {
 70   assert(this->is_loaded(), "must be loaded: %s", this->name()->as_quoted_ascii());
 71   assert(that->is_loaded(), "must be loaded: %s", that->name()->as_quoted_ascii());
 72 
 73   // Check to see if the klasses are identical.
 74   if (this == that) {
 75     return true;
 76   }
 77 
 78   bool is_subtype;
 79   GUARDED_VM_ENTRY(is_subtype = get_Klass()->is_subtype_of(that->get_Klass());)
 80 
 81   // Ensure consistency with ciInstanceKlass::has_subklass().
 82   assert(!that->is_instance_klass() || // array klasses are irrelevant
 83           that->is_interface()      || // has_subklass is always false for interfaces
 84          !is_subtype || that->as_instance_klass()->has_subklass(), "inconsistent");
 85 
 86   return is_subtype;
 87 }
 88 
 89 // ------------------------------------------------------------------
 90 // ciKlass::is_subclass_of
 91 bool ciKlass::is_subclass_of(ciKlass* that) {
 92   assert(this->is_loaded(), "must be loaded: %s", this->name()->as_quoted_ascii());
 93   assert(that->is_loaded(), "must be loaded: %s", that->name()->as_quoted_ascii());
 94 
 95   // Check to see if the klasses are identical.
 96   if (this == that) {
 97     return true;
 98   }
 99 
100   bool is_subclass;
101   GUARDED_VM_ENTRY(is_subclass = get_Klass()->is_subclass_of(that->get_Klass());)
102 
103   // Ensure consistency with ciInstanceKlass::has_subklass().
104   assert(!that->is_instance_klass() || // array klasses are irrelevant
105           that->is_interface()      || // has_subklass is always false for interfaces
106          !is_subclass || that->as_instance_klass()->has_subklass(), "inconsistent");
107 
108   return is_subclass;
109 }
110 
111 // ------------------------------------------------------------------
112 // ciKlass::super_depth
113 juint ciKlass::super_depth() {
114   assert(is_loaded(), "must be loaded");
115 
116   VM_ENTRY_MARK;
117   Klass* this_klass = get_Klass();
118   return this_klass->super_depth();
119 }
120 
121 // ------------------------------------------------------------------
122 // ciKlass::super_check_offset
123 juint ciKlass::super_check_offset() {
124   assert(is_loaded(), "must be loaded");
125 
126   VM_ENTRY_MARK;
127   Klass* this_klass = get_Klass();
128   return this_klass->super_check_offset();
129 }
130 
131 // ------------------------------------------------------------------
132 // ciKlass::super_of_depth
133 ciKlass* ciKlass::super_of_depth(juint i) {
134   assert(is_loaded(), "must be loaded");
135 
136   VM_ENTRY_MARK;
137   Klass* this_klass = get_Klass();
138   Klass* super = this_klass->primary_super_of_depth(i);
139   return (super != nullptr) ? CURRENT_THREAD_ENV->get_klass(super) : nullptr;
140 }
141 
142 // ------------------------------------------------------------------
143 // ciKlass::least_common_ancestor
144 //
145 // Get the shared parent of two klasses.
146 //
147 // Implementation note: this method currently goes "over the wall"
148 // and does all of the work on the VM side.  It could be rewritten
149 // to use the super() method and do all of the work (aside from the
150 // lazy computation of super()) in native mode.  This may be
151 // worthwhile if the compiler is repeatedly requesting the same lca
152 // computation or possibly if most of the superklasses have already
153 // been created as ciObjects anyway.  Something to think about...
154 ciKlass*
155 ciKlass::least_common_ancestor(ciKlass* that) {
156   assert(is_loaded() && that->is_loaded(), "must be loaded");
157   // Check to see if the klasses are identical.
158   if (this == that) {
159     return this;
160   }
161 
162   VM_ENTRY_MARK;
163   Klass* this_klass = get_Klass();
164   Klass* that_klass = that->get_Klass();
165   Klass* lca        = this_klass->LCA(that_klass);
166 
167   // Many times the LCA will be either this_klass or that_klass.
168   // Treat these as special cases.
169   if (lca == that_klass) {
170     assert(this->is_subtype_of(that), "sanity");
171     return that;
172   }
173   if (this_klass == lca) {
174     assert(that->is_subtype_of(this), "sanity");
175     return this;
176   }
177 
178   // Create the ciInstanceKlass for the lca.
179   ciKlass* result =
180     CURRENT_THREAD_ENV->get_klass(lca);
181 
182   assert(this->is_subtype_of(result) && that->is_subtype_of(result), "sanity");
183   return result;
184 }
185 
186 // ------------------------------------------------------------------
187 // ciKlass::find_klass
188 //
189 // Find a klass using this klass's class loader.
190 ciKlass* ciKlass::find_klass(ciSymbol* klass_name) {
191   assert(is_loaded(), "cannot find_klass through an unloaded klass");
192   return CURRENT_ENV->get_klass_by_name(this,
193                                         klass_name, false);
194 }
195 
196 // ------------------------------------------------------------------
197 // ciKlass::java_mirror
198 //
199 // Get the instance of java.lang.Class corresponding to this klass.
200 // If it is an unloaded instance or array klass, return an unloaded
201 // mirror object of type Class.
202 ciInstance* ciKlass::java_mirror() {
203   GUARDED_VM_ENTRY(
204     if (!is_loaded())
205       return ciEnv::current()->get_unloaded_klass_mirror(this);
206     oop java_mirror = get_Klass()->java_mirror();
207     return CURRENT_ENV->get_instance(java_mirror);
208   )
209 }
210 
211 // ------------------------------------------------------------------
212 // ciKlass::modifier_flags
213 jint ciKlass::modifier_flags() {
214   assert(is_loaded(), "not loaded");
215   GUARDED_VM_ENTRY(
216     return get_Klass()->modifier_flags();
217   )
218 }
219 
220 // ------------------------------------------------------------------
221 // ciKlass::access_flags
222 jint ciKlass::access_flags() {
223   assert(is_loaded(), "not loaded");
224   GUARDED_VM_ENTRY(
225     return get_Klass()->access_flags().as_int();
226   )
227 }
228 









229 // ------------------------------------------------------------------
230 // ciKlass::misc_flags
231 klass_flags_t ciKlass::misc_flags() {
232   assert(is_loaded(), "not loaded");
233   GUARDED_VM_ENTRY(
234     return get_Klass()->misc_flags();
235   )
236 }
237 
238 // ------------------------------------------------------------------
239 // ciKlass::print_impl
240 //
241 // Implementation of the print method
242 void ciKlass::print_impl(outputStream* st) {
243   st->print(" name=");
244   print_name_on(st);
245   st->print(" loaded=%s", (is_loaded() ? "true" : "false"));
246 }
247 
248 // ------------------------------------------------------------------
249 // ciKlass::print_name
250 //
251 // Print the name of this klass
252 void ciKlass::print_name_on(outputStream* st) {
253   name()->print_symbol_on(st);
254 }
255 
256 const char* ciKlass::external_name() const {
257   GUARDED_VM_ENTRY(
258     return get_Klass()->external_name();
259   )
260 }
--- EOF ---