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