1 /*
2  * Copyright (c) 1999, 2013, 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.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(KlassHandle h_k)38 ciKlass::ciKlass(KlassHandle h_k) : ciType(h_k) {
39   assert(get_Klass()->is_klass(), "wrong type");
40   Klass* k = get_Klass();
41   _layout_helper = k->layout_helper();
42   Symbol* klass_name = k->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(KlassHandle h_k,ciSymbol * name)51 ciKlass::ciKlass(KlassHandle h_k, ciSymbol* name) : ciType(h_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(), err_msg("must be loaded: %s", this->name()->as_quoted_ascii()));
70   assert(that->is_loaded(), err_msg("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(), err_msg("must be loaded: %s", this->name()->as_quoted_ascii()));
89   assert(that->is_loaded(), err_msg("must be loaded: %s", that->name()->as_quoted_ascii()));
90 
91   VM_ENTRY_MARK;
92   Klass* this_klass = get_Klass();
93   Klass* that_klass = that->get_Klass();
94   bool result = this_klass->is_subclass_of(that_klass);
95 
96   return result;
97 }
98 
99 // ------------------------------------------------------------------
100 // ciKlass::super_depth
super_depth()101 juint ciKlass::super_depth() {
102   assert(is_loaded(), "must be loaded");
103 
104   VM_ENTRY_MARK;
105   Klass* this_klass = get_Klass();
106   return this_klass->super_depth();
107 }
108 
109 // ------------------------------------------------------------------
110 // ciKlass::super_check_offset
super_check_offset()111 juint ciKlass::super_check_offset() {
112   assert(is_loaded(), "must be loaded");
113 
114   VM_ENTRY_MARK;
115   Klass* this_klass = get_Klass();
116   return this_klass->super_check_offset();
117 }
118 
119 // ------------------------------------------------------------------
120 // ciKlass::super_of_depth
super_of_depth(juint i)121 ciKlass* ciKlass::super_of_depth(juint i) {
122   assert(is_loaded(), "must be loaded");
123 
124   VM_ENTRY_MARK;
125   Klass* this_klass = get_Klass();
126   Klass* super = this_klass->primary_super_of_depth(i);
127   return (super != NULL) ? CURRENT_THREAD_ENV->get_klass(super) : NULL;
128 }
129 
130 // ------------------------------------------------------------------
131 // ciKlass::can_be_primary_super
can_be_primary_super()132 bool ciKlass::can_be_primary_super() {
133   assert(is_loaded(), "must be loaded");
134 
135   VM_ENTRY_MARK;
136   Klass* this_klass = get_Klass();
137   return this_klass->can_be_primary_super();
138 }
139 
140 // ------------------------------------------------------------------
141 // ciKlass::least_common_ancestor
142 //
143 // Get the shared parent of two klasses.
144 //
145 // Implementation note: this method currently goes "over the wall"
146 // and does all of the work on the VM side.  It could be rewritten
147 // to use the super() method and do all of the work (aside from the
148 // lazy computation of super()) in native mode.  This may be
149 // worthwhile if the compiler is repeatedly requesting the same lca
150 // computation or possibly if most of the superklasses have already
151 // been created as ciObjects anyway.  Something to think about...
152 ciKlass*
least_common_ancestor(ciKlass * that)153 ciKlass::least_common_ancestor(ciKlass* that) {
154   assert(is_loaded() && that->is_loaded(), "must be loaded");
155   // Check to see if the klasses are identical.
156   if (this == that) {
157     return this;
158   }
159 
160   VM_ENTRY_MARK;
161   Klass* this_klass = get_Klass();
162   Klass* that_klass = that->get_Klass();
163   Klass* lca        = this_klass->LCA(that_klass);
164 
165   // Many times the LCA will be either this_klass or that_klass.
166   // Treat these as special cases.
167   if (lca == that_klass) {
168     return that;
169   }
170   if (this_klass == lca) {
171     return this;
172   }
173 
174   // Create the ciInstanceKlass for the lca.
175   ciKlass* result =
176     CURRENT_THREAD_ENV->get_klass(lca);
177 
178   return result;
179 }
180 
181 // ------------------------------------------------------------------
182 // ciKlass::find_klass
183 //
184 // Find a klass using this klass's class loader.
find_klass(ciSymbol * klass_name)185 ciKlass* ciKlass::find_klass(ciSymbol* klass_name) {
186   assert(is_loaded(), "cannot find_klass through an unloaded klass");
187   return CURRENT_ENV->get_klass_by_name(this,
188                                         klass_name, false);
189 }
190 
191 // ------------------------------------------------------------------
192 // ciKlass::java_mirror
193 //
194 // Get the instance of java.lang.Class corresponding to this klass.
195 // If it is an unloaded instance or array klass, return an unloaded
196 // mirror object of type Class.
java_mirror()197 ciInstance* ciKlass::java_mirror() {
198   GUARDED_VM_ENTRY(
199     if (!is_loaded())
200       return ciEnv::current()->get_unloaded_klass_mirror(this);
201     oop java_mirror = get_Klass()->java_mirror();
202     return CURRENT_ENV->get_instance(java_mirror);
203   )
204 }
205 
206 // ------------------------------------------------------------------
207 // ciKlass::modifier_flags
modifier_flags()208 jint ciKlass::modifier_flags() {
209   assert(is_loaded(), "not loaded");
210   GUARDED_VM_ENTRY(
211     return get_Klass()->modifier_flags();
212   )
213 }
214 
215 // ------------------------------------------------------------------
216 // ciKlass::access_flags
access_flags()217 jint ciKlass::access_flags() {
218   assert(is_loaded(), "not loaded");
219   GUARDED_VM_ENTRY(
220     return get_Klass()->access_flags().as_int();
221   )
222 }
223 
224 // ------------------------------------------------------------------
225 // ciKlass::print_impl
226 //
227 // Implementation of the print method
print_impl(outputStream * st)228 void ciKlass::print_impl(outputStream* st) {
229   st->print(" name=");
230   print_name_on(st);
231 }
232 
233 // ------------------------------------------------------------------
234 // ciKlass::print_name
235 //
236 // Print the name of this klass
print_name_on(outputStream * st)237 void ciKlass::print_name_on(outputStream* st) {
238   name()->print_symbol_on(st);
239 }
240