annotate src/share/vm/oops/klassVtable.hpp @ 5297:b2e698d2276c

8014013: CallInfo structure no longer accurately reports the result of a LinkResolver operation Summary: Enhance method resolution and resulting data structures, plus some refactoring. Reviewed-by: twisti, acorn, jrose
author drchase
date Fri, 13 Sep 2013 22:38:02 -0400
parents 69f26e8e09f9
children ac9cb1d5a202
rev   line source
duke@0 1 /*
dcubed@4127 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #ifndef SHARE_VM_OOPS_KLASSVTABLE_HPP
stefank@1879 26 #define SHARE_VM_OOPS_KLASSVTABLE_HPP
stefank@1879 27
stefank@1879 28 #include "memory/allocation.hpp"
stefank@1879 29 #include "oops/oopsHierarchy.hpp"
stefank@1879 30 #include "runtime/handles.hpp"
stefank@1879 31 #include "utilities/growableArray.hpp"
stefank@1879 32
coleenp@3602 33 // A klassVtable abstracts the variable-length vtable that is embedded in InstanceKlass
coleenp@3707 34 // and ArrayKlass. klassVtable objects are used just as convenient transient accessors to the vtable,
duke@0 35 // not to actually hold the vtable data.
duke@0 36 // Note: the klassVtable should not be accessed before the class has been verified
duke@0 37 // (until that point, the vtable is uninitialized).
duke@0 38
duke@0 39 // Currently a klassVtable contains a direct reference to the vtable data, and is therefore
duke@0 40 // not preserved across GCs.
duke@0 41
duke@0 42 class vtableEntry;
duke@0 43
duke@0 44 class klassVtable : public ResourceObj {
duke@0 45 KlassHandle _klass; // my klass
duke@0 46 int _tableOffset; // offset of start of vtable data within klass
duke@0 47 int _length; // length of vtable (number of entries)
duke@0 48 #ifndef PRODUCT
duke@0 49 int _verify_count; // to make verify faster
duke@0 50 #endif
duke@0 51
duke@0 52 // Ordering important, so greater_than (>) can be used as an merge operator.
duke@0 53 enum AccessType {
duke@0 54 acc_private = 0,
duke@0 55 acc_package_private = 1,
duke@0 56 acc_publicprotected = 2
duke@0 57 };
duke@0 58
duke@0 59 public:
duke@0 60 klassVtable(KlassHandle h_klass, void* base, int length) : _klass(h_klass) {
duke@0 61 _tableOffset = (address)base - (address)h_klass(); _length = length;
duke@0 62 }
duke@0 63
duke@0 64 // accessors
duke@0 65 vtableEntry* table() const { return (vtableEntry*)(address(_klass()) + _tableOffset); }
duke@0 66 KlassHandle klass() const { return _klass; }
duke@0 67 int length() const { return _length; }
coleenp@3602 68 inline Method* method_at(int i) const;
coleenp@3602 69 inline Method* unchecked_method_at(int i) const;
coleenp@3602 70 inline Method** adr_method_at(int i) const;
duke@0 71
duke@0 72 // searching; all methods return -1 if not found
coleenp@3602 73 int index_of(Method* m) const { return index_of(m, _length); }
coleenp@2062 74 int index_of_miranda(Symbol* name, Symbol* signature);
duke@0 75
duke@0 76 void initialize_vtable(bool checkconstraints, TRAPS); // initialize vtable of a new klass
duke@0 77
coleenp@2342 78 // CDS/RedefineClasses support - clear vtables so they can be reinitialized
coleenp@2342 79 // at dump time. Clearing gives us an easy way to tell if the vtable has
coleenp@2342 80 // already been reinitialized at dump time (see dump.cpp). Vtables can
coleenp@2342 81 // be initialized at run time by RedefineClasses so dumping the right order
coleenp@2342 82 // is necessary.
coleenp@2342 83 void clear_vtable();
coleenp@2342 84 bool is_initialized();
coleenp@2342 85
coleenp@2342 86 // computes vtable length (in words) and the number of miranda methods
kamg@3810 87 static void compute_vtable_size_and_num_mirandas(
kamg@3810 88 int* vtable_length, int* num_new_mirandas,
kamg@3810 89 GrowableArray<Method*>* all_mirandas, Klass* super,
kamg@3810 90 Array<Method*>* methods, AccessFlags class_flags, Handle classloader,
kamg@3810 91 Symbol* classname, Array<Klass*>* local_interfaces, TRAPS);
duke@0 92
dcubed@4127 93 #if INCLUDE_JVMTI
duke@0 94 // RedefineClasses() API support:
duke@0 95 // If any entry of this vtable points to any of old_methods,
duke@0 96 // replace it with the corresponding new_method.
duke@0 97 // trace_name_printed is set to true if the current call has
duke@0 98 // printed the klass name so that other routines in the adjust_*
duke@0 99 // group don't print the klass name.
coleenp@3602 100 void adjust_method_entries(Method** old_methods, Method** new_methods,
duke@0 101 int methods_length, bool * trace_name_printed);
dcubed@4127 102 bool check_no_old_or_obsolete_entries();
dcubed@4127 103 void dump_vtable();
dcubed@4127 104 #endif // INCLUDE_JVMTI
duke@0 105
duke@0 106 // Debugging code
duke@0 107 void print() PRODUCT_RETURN;
duke@0 108 void verify(outputStream* st, bool force = false);
duke@0 109 static void print_statistics() PRODUCT_RETURN;
duke@0 110
duke@0 111 protected:
duke@0 112 friend class vtableEntry;
duke@0 113 private:
acorn@652 114 enum { VTABLE_TRANSITIVE_OVERRIDE_VERSION = 51 } ;
duke@0 115 void copy_vtable_to(vtableEntry* start);
duke@0 116 int initialize_from_super(KlassHandle super);
coleenp@3602 117 int index_of(Method* m, int len) const; // same as index_of, but search only up to len
coleenp@3602 118 void put_method_at(Method* m, int index);
coleenp@3602 119 static bool needs_new_vtable_entry(methodHandle m, Klass* super, Handle classloader, Symbol* classname, AccessFlags access_flags, TRAPS);
duke@0 120
coleenp@3602 121 bool update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, bool checkconstraints, TRAPS);
coleenp@3602 122 InstanceKlass* find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method, int vtable_index,
coleenp@2062 123 Handle target_loader, Symbol* target_classname, Thread* THREAD);
duke@0 124
duke@0 125 // support for miranda methods
duke@0 126 bool is_miranda_entry_at(int i);
drchase@5297 127 int fill_in_mirandas(int initialized);
coleenp@3602 128 static bool is_miranda(Method* m, Array<Method*>* class_methods, Klass* super);
kamg@3810 129 static void add_new_mirandas_to_lists(
kamg@3810 130 GrowableArray<Method*>* new_mirandas,
kamg@3810 131 GrowableArray<Method*>* all_mirandas,
kamg@3810 132 Array<Method*>* current_interface_methods, Array<Method*>* class_methods,
kamg@3810 133 Klass* super);
kamg@3810 134 static void get_mirandas(
kamg@3810 135 GrowableArray<Method*>* new_mirandas,
kamg@3810 136 GrowableArray<Method*>* all_mirandas, Klass* super,
kamg@3810 137 Array<Method*>* class_methods, Array<Klass*>* local_interfaces);
duke@0 138
duke@0 139 void verify_against(outputStream* st, klassVtable* vt, int index);
coleenp@3602 140 inline InstanceKlass* ik() const;
duke@0 141 };
duke@0 142
duke@0 143
duke@0 144 // private helper class for klassVtable
duke@0 145 // description of entry points:
duke@0 146 // destination is interpreted:
duke@0 147 // from_compiled_code_entry_point -> c2iadapter
duke@0 148 // from_interpreter_entry_point -> interpreter entry point
duke@0 149 // destination is compiled:
duke@0 150 // from_compiled_code_entry_point -> nmethod entry point
duke@0 151 // from_interpreter_entry_point -> i2cadapter
duke@0 152 class vtableEntry VALUE_OBJ_CLASS_SPEC {
twisti@5291 153 friend class VMStructs;
twisti@5291 154
duke@0 155 public:
duke@0 156 // size in words
duke@0 157 static int size() {
duke@0 158 return sizeof(vtableEntry) / sizeof(HeapWord);
duke@0 159 }
duke@0 160 static int method_offset_in_bytes() { return offset_of(vtableEntry, _method); }
coleenp@3602 161 Method* method() const { return _method; }
duke@0 162
duke@0 163 private:
coleenp@3602 164 Method* _method;
coleenp@3602 165 void set(Method* method) { assert(method != NULL, "use clear"); _method = method; }
duke@0 166 void clear() { _method = NULL; }
duke@0 167 void print() PRODUCT_RETURN;
duke@0 168 void verify(klassVtable* vt, outputStream* st);
duke@0 169
duke@0 170 friend class klassVtable;
duke@0 171 };
duke@0 172
duke@0 173
coleenp@3602 174 inline Method* klassVtable::method_at(int i) const {
duke@0 175 assert(i >= 0 && i < _length, "index out of bounds");
duke@0 176 assert(table()[i].method() != NULL, "should not be null");
coleenp@3602 177 assert(((Metadata*)table()[i].method())->is_method(), "should be method");
duke@0 178 return table()[i].method();
duke@0 179 }
duke@0 180
coleenp@3602 181 inline Method* klassVtable::unchecked_method_at(int i) const {
duke@0 182 assert(i >= 0 && i < _length, "index out of bounds");
duke@0 183 return table()[i].method();
duke@0 184 }
duke@0 185
coleenp@3602 186 inline Method** klassVtable::adr_method_at(int i) const {
duke@0 187 // Allow one past the last entry to be referenced; useful for loop bounds.
duke@0 188 assert(i >= 0 && i <= _length, "index out of bounds");
coleenp@3602 189 return (Method**)(address(table() + i) + vtableEntry::method_offset_in_bytes());
duke@0 190 }
duke@0 191
duke@0 192 // --------------------------------------------------------------------------------
duke@0 193 class klassItable;
duke@0 194 class itableMethodEntry;
duke@0 195
duke@0 196 class itableOffsetEntry VALUE_OBJ_CLASS_SPEC {
duke@0 197 private:
coleenp@3602 198 Klass* _interface;
duke@0 199 int _offset;
duke@0 200 public:
coleenp@3602 201 Klass* interface_klass() const { return _interface; }
duke@0 202 int offset() const { return _offset; }
duke@0 203
coleenp@3602 204 static itableMethodEntry* method_entry(Klass* k, int offset) { return (itableMethodEntry*)(((address)k) + offset); }
coleenp@3602 205 itableMethodEntry* first_method_entry(Klass* k) { return method_entry(k, _offset); }
duke@0 206
coleenp@3602 207 void initialize(Klass* interf, int offset) { _interface = interf; _offset = offset; }
duke@0 208
duke@0 209 // Static size and offset accessors
duke@0 210 static int size() { return sizeof(itableOffsetEntry) / HeapWordSize; } // size in words
duke@0 211 static int interface_offset_in_bytes() { return offset_of(itableOffsetEntry, _interface); }
duke@0 212 static int offset_offset_in_bytes() { return offset_of(itableOffsetEntry, _offset); }
duke@0 213
duke@0 214 friend class klassItable;
duke@0 215 };
duke@0 216
duke@0 217
duke@0 218 class itableMethodEntry VALUE_OBJ_CLASS_SPEC {
duke@0 219 private:
coleenp@3602 220 Method* _method;
duke@0 221
duke@0 222 public:
coleenp@3602 223 Method* method() const { return _method; }
duke@0 224
duke@0 225 void clear() { _method = NULL; }
duke@0 226
coleenp@3602 227 void initialize(Method* method);
duke@0 228
duke@0 229 // Static size and offset accessors
duke@0 230 static int size() { return sizeof(itableMethodEntry) / HeapWordSize; } // size in words
duke@0 231 static int method_offset_in_bytes() { return offset_of(itableMethodEntry, _method); }
duke@0 232
duke@0 233 friend class klassItable;
duke@0 234 };
duke@0 235
duke@0 236 //
duke@0 237 // Format of an itable
duke@0 238 //
duke@0 239 // ---- offset table ---
coleenp@3602 240 // Klass* of interface 1 \
duke@0 241 // offset to vtable from start of oop / offset table entry
duke@0 242 // ...
coleenp@3602 243 // Klass* of interface n \
duke@0 244 // offset to vtable from start of oop / offset table entry
duke@0 245 // --- vtable for interface 1 ---
coleenp@3602 246 // Method* \
duke@0 247 // compiler entry point / method table entry
duke@0 248 // ...
coleenp@3602 249 // Method* \
duke@0 250 // compiler entry point / method table entry
duke@0 251 // -- vtable for interface 2 ---
duke@0 252 // ...
duke@0 253 //
duke@0 254 class klassItable : public ResourceObj {
duke@0 255 private:
duke@0 256 instanceKlassHandle _klass; // my klass
duke@0 257 int _table_offset; // offset of start of itable data within klass (in words)
duke@0 258 int _size_offset_table; // size of offset table (in itableOffset entries)
duke@0 259 int _size_method_table; // size of methodtable (in itableMethodEntry entries)
duke@0 260
duke@0 261 void initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS);
duke@0 262 public:
duke@0 263 klassItable(instanceKlassHandle klass);
duke@0 264
duke@0 265 itableOffsetEntry* offset_entry(int i) { assert(0 <= i && i <= _size_offset_table, "index out of bounds");
duke@0 266 return &((itableOffsetEntry*)vtable_start())[i]; }
duke@0 267
duke@0 268 itableMethodEntry* method_entry(int i) { assert(0 <= i && i <= _size_method_table, "index out of bounds");
duke@0 269 return &((itableMethodEntry*)method_start())[i]; }
duke@0 270
dcubed@16 271 int size_offset_table() { return _size_offset_table; }
duke@0 272
duke@0 273 // Initialization
duke@0 274 void initialize_itable(bool checkconstraints, TRAPS);
duke@0 275
duke@0 276 // Updates
coleenp@3602 277 void initialize_with_method(Method* m);
duke@0 278
dcubed@4127 279 #if INCLUDE_JVMTI
duke@0 280 // RedefineClasses() API support:
duke@0 281 // if any entry of this itable points to any of old_methods,
duke@0 282 // replace it with the corresponding new_method.
duke@0 283 // trace_name_printed is set to true if the current call has
duke@0 284 // printed the klass name so that other routines in the adjust_*
duke@0 285 // group don't print the klass name.
coleenp@3602 286 void adjust_method_entries(Method** old_methods, Method** new_methods,
duke@0 287 int methods_length, bool * trace_name_printed);
dcubed@4127 288 bool check_no_old_or_obsolete_entries();
dcubed@4127 289 void dump_itable();
dcubed@4127 290 #endif // INCLUDE_JVMTI
duke@0 291
duke@0 292 // Setup of itable
drchase@5297 293 static int assign_itable_indexes_for_interface(Klass* klass);
drchase@5297 294 static int method_count_for_interface(Klass* klass);
coleenp@3602 295 static int compute_itable_size(Array<Klass*>* transitive_interfaces);
duke@0 296 static void setup_itable_offset_table(instanceKlassHandle klass);
duke@0 297
duke@0 298 // Resolving of method to index
coleenp@3602 299 static Method* method_for_itable_index(Klass* klass, int itable_index);
duke@0 300
duke@0 301 // Debugging/Statistics
duke@0 302 static void print_statistics() PRODUCT_RETURN;
duke@0 303 private:
duke@0 304 intptr_t* vtable_start() const { return ((intptr_t*)_klass()) + _table_offset; }
duke@0 305 intptr_t* method_start() const { return vtable_start() + _size_offset_table * itableOffsetEntry::size(); }
duke@0 306
duke@0 307 // Helper methods
duke@0 308 static int calc_itable_size(int num_interfaces, int num_methods) { return (num_interfaces * itableOffsetEntry::size()) + (num_methods * itableMethodEntry::size()); }
duke@0 309
duke@0 310 // Statistics
duke@0 311 NOT_PRODUCT(static int _total_classes;) // Total no. of classes with itables
duke@0 312 NOT_PRODUCT(static long _total_size;) // Total no. of bytes used for itables
duke@0 313
duke@0 314 static void update_stats(int size) PRODUCT_RETURN NOT_PRODUCT({ _total_classes++; _total_size += size; })
duke@0 315 };
stefank@1879 316
stefank@1879 317 #endif // SHARE_VM_OOPS_KLASSVTABLE_HPP