duke@435: /* dcubed@4562: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_OOPS_KLASSVTABLE_HPP stefank@2314: #define SHARE_VM_OOPS_KLASSVTABLE_HPP stefank@2314: stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "oops/oopsHierarchy.hpp" stefank@2314: #include "runtime/handles.hpp" stefank@2314: #include "utilities/growableArray.hpp" stefank@2314: coleenp@4037: // A klassVtable abstracts the variable-length vtable that is embedded in InstanceKlass coleenp@4142: // and ArrayKlass. klassVtable objects are used just as convenient transient accessors to the vtable, duke@435: // not to actually hold the vtable data. duke@435: // Note: the klassVtable should not be accessed before the class has been verified duke@435: // (until that point, the vtable is uninitialized). duke@435: duke@435: // Currently a klassVtable contains a direct reference to the vtable data, and is therefore duke@435: // not preserved across GCs. duke@435: duke@435: class vtableEntry; duke@435: duke@435: class klassVtable : public ResourceObj { duke@435: KlassHandle _klass; // my klass duke@435: int _tableOffset; // offset of start of vtable data within klass duke@435: int _length; // length of vtable (number of entries) duke@435: #ifndef PRODUCT duke@435: int _verify_count; // to make verify faster duke@435: #endif duke@435: duke@435: // Ordering important, so greater_than (>) can be used as an merge operator. duke@435: enum AccessType { duke@435: acc_private = 0, duke@435: acc_package_private = 1, duke@435: acc_publicprotected = 2 duke@435: }; duke@435: duke@435: public: duke@435: klassVtable(KlassHandle h_klass, void* base, int length) : _klass(h_klass) { duke@435: _tableOffset = (address)base - (address)h_klass(); _length = length; duke@435: } duke@435: duke@435: // accessors duke@435: vtableEntry* table() const { return (vtableEntry*)(address(_klass()) + _tableOffset); } duke@435: KlassHandle klass() const { return _klass; } duke@435: int length() const { return _length; } coleenp@4037: inline Method* method_at(int i) const; coleenp@4037: inline Method* unchecked_method_at(int i) const; coleenp@4037: inline Method** adr_method_at(int i) const; duke@435: duke@435: // searching; all methods return -1 if not found coleenp@4037: int index_of(Method* m) const { return index_of(m, _length); } coleenp@2497: int index_of_miranda(Symbol* name, Symbol* signature); duke@435: duke@435: void initialize_vtable(bool checkconstraints, TRAPS); // initialize vtable of a new klass duke@435: coleenp@2777: // CDS/RedefineClasses support - clear vtables so they can be reinitialized coleenp@2777: // at dump time. Clearing gives us an easy way to tell if the vtable has coleenp@2777: // already been reinitialized at dump time (see dump.cpp). Vtables can coleenp@2777: // be initialized at run time by RedefineClasses so dumping the right order coleenp@2777: // is necessary. coleenp@2777: void clear_vtable(); coleenp@2777: bool is_initialized(); coleenp@2777: coleenp@2777: // computes vtable length (in words) and the number of miranda methods kamg@4245: static void compute_vtable_size_and_num_mirandas( kamg@4245: int* vtable_length, int* num_new_mirandas, kamg@4245: GrowableArray* all_mirandas, Klass* super, kamg@4245: Array* methods, AccessFlags class_flags, Handle classloader, kamg@4245: Symbol* classname, Array* local_interfaces, TRAPS); duke@435: dcubed@4562: #if INCLUDE_JVMTI duke@435: // RedefineClasses() API support: duke@435: // If any entry of this vtable points to any of old_methods, duke@435: // replace it with the corresponding new_method. duke@435: // trace_name_printed is set to true if the current call has duke@435: // printed the klass name so that other routines in the adjust_* duke@435: // group don't print the klass name. acorn@5848: bool adjust_default_method(int vtable_index, Method* old_method, Method* new_method); coleenp@4037: void adjust_method_entries(Method** old_methods, Method** new_methods, duke@435: int methods_length, bool * trace_name_printed); dcubed@4562: bool check_no_old_or_obsolete_entries(); dcubed@4562: void dump_vtable(); dcubed@4562: #endif // INCLUDE_JVMTI duke@435: duke@435: // Debugging code duke@435: void print() PRODUCT_RETURN; duke@435: void verify(outputStream* st, bool force = false); duke@435: static void print_statistics() PRODUCT_RETURN; duke@435: duke@435: protected: duke@435: friend class vtableEntry; duke@435: private: acorn@1087: enum { VTABLE_TRANSITIVE_OVERRIDE_VERSION = 51 } ; duke@435: void copy_vtable_to(vtableEntry* start); duke@435: int initialize_from_super(KlassHandle super); coleenp@4037: int index_of(Method* m, int len) const; // same as index_of, but search only up to len coleenp@4037: void put_method_at(Method* m, int index); coleenp@4037: static bool needs_new_vtable_entry(methodHandle m, Klass* super, Handle classloader, Symbol* classname, AccessFlags access_flags, TRAPS); duke@435: acorn@5848: bool update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, int default_index, bool checkconstraints, TRAPS); coleenp@4037: InstanceKlass* find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method, int vtable_index, coleenp@2497: Handle target_loader, Symbol* target_classname, Thread* THREAD); duke@435: duke@435: // support for miranda methods duke@435: bool is_miranda_entry_at(int i); drchase@5732: int fill_in_mirandas(int initialized); acorn@5848: static bool is_miranda(Method* m, Array* class_methods, acorn@5848: Array* default_methods, Klass* super); kamg@4245: static void add_new_mirandas_to_lists( kamg@4245: GrowableArray* new_mirandas, kamg@4245: GrowableArray* all_mirandas, acorn@5848: Array* current_interface_methods, acorn@5848: Array* class_methods, acorn@5848: Array* default_methods, kamg@4245: Klass* super); kamg@4245: static void get_mirandas( kamg@4245: GrowableArray* new_mirandas, kamg@4245: GrowableArray* all_mirandas, Klass* super, acorn@5848: Array* class_methods, acorn@5848: Array* default_methods, acorn@5848: Array* local_interfaces); duke@435: void verify_against(outputStream* st, klassVtable* vt, int index); coleenp@4037: inline InstanceKlass* ik() const; duke@435: }; duke@435: duke@435: duke@435: // private helper class for klassVtable duke@435: // description of entry points: duke@435: // destination is interpreted: duke@435: // from_compiled_code_entry_point -> c2iadapter duke@435: // from_interpreter_entry_point -> interpreter entry point duke@435: // destination is compiled: duke@435: // from_compiled_code_entry_point -> nmethod entry point duke@435: // from_interpreter_entry_point -> i2cadapter duke@435: class vtableEntry VALUE_OBJ_CLASS_SPEC { twisti@5726: friend class VMStructs; twisti@5726: duke@435: public: duke@435: // size in words duke@435: static int size() { duke@435: return sizeof(vtableEntry) / sizeof(HeapWord); duke@435: } duke@435: static int method_offset_in_bytes() { return offset_of(vtableEntry, _method); } coleenp@4037: Method* method() const { return _method; } duke@435: duke@435: private: coleenp@4037: Method* _method; coleenp@4037: void set(Method* method) { assert(method != NULL, "use clear"); _method = method; } duke@435: void clear() { _method = NULL; } duke@435: void print() PRODUCT_RETURN; duke@435: void verify(klassVtable* vt, outputStream* st); duke@435: duke@435: friend class klassVtable; duke@435: }; duke@435: duke@435: coleenp@4037: inline Method* klassVtable::method_at(int i) const { duke@435: assert(i >= 0 && i < _length, "index out of bounds"); duke@435: assert(table()[i].method() != NULL, "should not be null"); coleenp@4037: assert(((Metadata*)table()[i].method())->is_method(), "should be method"); duke@435: return table()[i].method(); duke@435: } duke@435: coleenp@4037: inline Method* klassVtable::unchecked_method_at(int i) const { duke@435: assert(i >= 0 && i < _length, "index out of bounds"); duke@435: return table()[i].method(); duke@435: } duke@435: coleenp@4037: inline Method** klassVtable::adr_method_at(int i) const { duke@435: // Allow one past the last entry to be referenced; useful for loop bounds. duke@435: assert(i >= 0 && i <= _length, "index out of bounds"); coleenp@4037: return (Method**)(address(table() + i) + vtableEntry::method_offset_in_bytes()); duke@435: } duke@435: duke@435: // -------------------------------------------------------------------------------- duke@435: class klassItable; duke@435: class itableMethodEntry; duke@435: duke@435: class itableOffsetEntry VALUE_OBJ_CLASS_SPEC { duke@435: private: coleenp@4037: Klass* _interface; duke@435: int _offset; duke@435: public: coleenp@4037: Klass* interface_klass() const { return _interface; } duke@435: int offset() const { return _offset; } duke@435: coleenp@4037: static itableMethodEntry* method_entry(Klass* k, int offset) { return (itableMethodEntry*)(((address)k) + offset); } coleenp@4037: itableMethodEntry* first_method_entry(Klass* k) { return method_entry(k, _offset); } duke@435: coleenp@4037: void initialize(Klass* interf, int offset) { _interface = interf; _offset = offset; } duke@435: duke@435: // Static size and offset accessors duke@435: static int size() { return sizeof(itableOffsetEntry) / HeapWordSize; } // size in words duke@435: static int interface_offset_in_bytes() { return offset_of(itableOffsetEntry, _interface); } duke@435: static int offset_offset_in_bytes() { return offset_of(itableOffsetEntry, _offset); } duke@435: duke@435: friend class klassItable; duke@435: }; duke@435: duke@435: duke@435: class itableMethodEntry VALUE_OBJ_CLASS_SPEC { duke@435: private: coleenp@4037: Method* _method; duke@435: duke@435: public: coleenp@4037: Method* method() const { return _method; } duke@435: duke@435: void clear() { _method = NULL; } duke@435: coleenp@4037: void initialize(Method* method); duke@435: duke@435: // Static size and offset accessors duke@435: static int size() { return sizeof(itableMethodEntry) / HeapWordSize; } // size in words duke@435: static int method_offset_in_bytes() { return offset_of(itableMethodEntry, _method); } duke@435: duke@435: friend class klassItable; duke@435: }; duke@435: duke@435: // duke@435: // Format of an itable duke@435: // duke@435: // ---- offset table --- coleenp@4037: // Klass* of interface 1 \ duke@435: // offset to vtable from start of oop / offset table entry duke@435: // ... coleenp@4037: // Klass* of interface n \ duke@435: // offset to vtable from start of oop / offset table entry duke@435: // --- vtable for interface 1 --- coleenp@4037: // Method* \ duke@435: // compiler entry point / method table entry duke@435: // ... coleenp@4037: // Method* \ duke@435: // compiler entry point / method table entry duke@435: // -- vtable for interface 2 --- duke@435: // ... duke@435: // duke@435: class klassItable : public ResourceObj { duke@435: private: duke@435: instanceKlassHandle _klass; // my klass duke@435: int _table_offset; // offset of start of itable data within klass (in words) duke@435: int _size_offset_table; // size of offset table (in itableOffset entries) duke@435: int _size_method_table; // size of methodtable (in itableMethodEntry entries) duke@435: duke@435: void initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS); duke@435: public: duke@435: klassItable(instanceKlassHandle klass); duke@435: duke@435: itableOffsetEntry* offset_entry(int i) { assert(0 <= i && i <= _size_offset_table, "index out of bounds"); duke@435: return &((itableOffsetEntry*)vtable_start())[i]; } duke@435: duke@435: itableMethodEntry* method_entry(int i) { assert(0 <= i && i <= _size_method_table, "index out of bounds"); duke@435: return &((itableMethodEntry*)method_start())[i]; } duke@435: dcubed@451: int size_offset_table() { return _size_offset_table; } duke@435: duke@435: // Initialization duke@435: void initialize_itable(bool checkconstraints, TRAPS); duke@435: duke@435: // Updates coleenp@4037: void initialize_with_method(Method* m); duke@435: dcubed@4562: #if INCLUDE_JVMTI duke@435: // RedefineClasses() API support: duke@435: // if any entry of this itable points to any of old_methods, duke@435: // replace it with the corresponding new_method. duke@435: // trace_name_printed is set to true if the current call has duke@435: // printed the klass name so that other routines in the adjust_* duke@435: // group don't print the klass name. coleenp@4037: void adjust_method_entries(Method** old_methods, Method** new_methods, duke@435: int methods_length, bool * trace_name_printed); dcubed@4562: bool check_no_old_or_obsolete_entries(); dcubed@4562: void dump_itable(); dcubed@4562: #endif // INCLUDE_JVMTI duke@435: duke@435: // Setup of itable acorn@5848: static int assign_itable_indices_for_interface(Klass* klass); drchase@5732: static int method_count_for_interface(Klass* klass); coleenp@4037: static int compute_itable_size(Array* transitive_interfaces); duke@435: static void setup_itable_offset_table(instanceKlassHandle klass); duke@435: duke@435: // Resolving of method to index coleenp@4037: static Method* method_for_itable_index(Klass* klass, int itable_index); duke@435: duke@435: // Debugging/Statistics duke@435: static void print_statistics() PRODUCT_RETURN; duke@435: private: duke@435: intptr_t* vtable_start() const { return ((intptr_t*)_klass()) + _table_offset; } duke@435: intptr_t* method_start() const { return vtable_start() + _size_offset_table * itableOffsetEntry::size(); } duke@435: duke@435: // Helper methods duke@435: static int calc_itable_size(int num_interfaces, int num_methods) { return (num_interfaces * itableOffsetEntry::size()) + (num_methods * itableMethodEntry::size()); } duke@435: duke@435: // Statistics duke@435: NOT_PRODUCT(static int _total_classes;) // Total no. of classes with itables duke@435: NOT_PRODUCT(static long _total_size;) // Total no. of bytes used for itables duke@435: duke@435: static void update_stats(int size) PRODUCT_RETURN NOT_PRODUCT({ _total_classes++; _total_size += size; }) duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_OOPS_KLASSVTABLE_HPP