Mercurial > hg > graal-compiler
comparison src/share/vm/oops/klassVtable.cpp @ 12264: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 | 08236d966eea |
children | 36b97be47bde |
comparison
equal
deleted
inserted
replaced
12261:2c98370f2611 | 12264:b2e698d2276c |
---|---|
45 return (InstanceKlass*)k; | 45 return (InstanceKlass*)k; |
46 } | 46 } |
47 | 47 |
48 | 48 |
49 // this function computes the vtable size (including the size needed for miranda | 49 // this function computes the vtable size (including the size needed for miranda |
50 // methods) and the number of miranda methods in this class | 50 // methods) and the number of miranda methods in this class. |
51 // Note on Miranda methods: Let's say there is a class C that implements | 51 // Note on Miranda methods: Let's say there is a class C that implements |
52 // interface I. Let's say there is a method m in I that neither C nor any | 52 // interface I, and none of C's superclasses implements I. |
53 // of its super classes implement (i.e there is no method of any access, with | 53 // Let's say there is an abstract method m in I that neither C |
54 // the same name and signature as m), then m is a Miranda method which is | 54 // nor any of its super classes implement (i.e there is no method of any access, |
55 // with the same name and signature as m), then m is a Miranda method which is | |
55 // entered as a public abstract method in C's vtable. From then on it should | 56 // entered as a public abstract method in C's vtable. From then on it should |
56 // treated as any other public method in C for method over-ride purposes. | 57 // treated as any other public method in C for method over-ride purposes. |
57 void klassVtable::compute_vtable_size_and_num_mirandas( | 58 void klassVtable::compute_vtable_size_and_num_mirandas( |
58 int* vtable_length_ret, int* num_new_mirandas, | 59 int* vtable_length_ret, int* num_new_mirandas, |
59 GrowableArray<Method*>* all_mirandas, Klass* super, | 60 GrowableArray<Method*>* all_mirandas, Klass* super, |
109 | 110 |
110 *vtable_length_ret = vtable_length; | 111 *vtable_length_ret = vtable_length; |
111 } | 112 } |
112 | 113 |
113 int klassVtable::index_of(Method* m, int len) const { | 114 int klassVtable::index_of(Method* m, int len) const { |
114 assert(m->vtable_index() >= 0, "do not ask this of non-vtable methods"); | 115 assert(m->has_vtable_index(), "do not ask this of non-vtable methods"); |
115 return m->vtable_index(); | 116 return m->vtable_index(); |
116 } | 117 } |
117 | 118 |
119 // Copy super class's vtable to the first part (prefix) of this class's vtable, | |
120 // and return the number of entries copied. Expects that 'super' is the Java | |
121 // super class (arrays can have "array" super classes that must be skipped). | |
118 int klassVtable::initialize_from_super(KlassHandle super) { | 122 int klassVtable::initialize_from_super(KlassHandle super) { |
119 if (super.is_null()) { | 123 if (super.is_null()) { |
120 return 0; | 124 return 0; |
121 } else { | 125 } else { |
122 // copy methods from superKlass | 126 // copy methods from superKlass |
137 #endif | 141 #endif |
138 return superVtable->length(); | 142 return superVtable->length(); |
139 } | 143 } |
140 } | 144 } |
141 | 145 |
142 // Revised lookup semantics introduced 1.3 (Kestral beta) | 146 // |
147 // Revised lookup semantics introduced 1.3 (Kestrel beta) | |
143 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) { | 148 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) { |
144 | 149 |
145 // Note: Arrays can have intermediate array supers. Use java_super to skip them. | 150 // Note: Arrays can have intermediate array supers. Use java_super to skip them. |
146 KlassHandle super (THREAD, klass()->java_super()); | 151 KlassHandle super (THREAD, klass()->java_super()); |
147 int nofNewEntries = 0; | 152 int nofNewEntries = 0; |
148 | |
149 | 153 |
150 if (PrintVtables && !klass()->oop_is_array()) { | 154 if (PrintVtables && !klass()->oop_is_array()) { |
151 ResourceMark rm(THREAD); | 155 ResourceMark rm(THREAD); |
152 tty->print_cr("Initializing: %s", _klass->name()->as_C_string()); | 156 tty->print_cr("Initializing: %s", _klass->name()->as_C_string()); |
153 } | 157 } |
172 | 176 |
173 Array<Method*>* methods = ik()->methods(); | 177 Array<Method*>* methods = ik()->methods(); |
174 int len = methods->length(); | 178 int len = methods->length(); |
175 int initialized = super_vtable_len; | 179 int initialized = super_vtable_len; |
176 | 180 |
177 // update_inherited_vtable can stop for gc - ensure using handles | 181 // Check each of this class's methods against super; |
182 // if override, replace in copy of super vtable, otherwise append to end | |
178 for (int i = 0; i < len; i++) { | 183 for (int i = 0; i < len; i++) { |
184 // update_inherited_vtable can stop for gc - ensure using handles | |
179 HandleMark hm(THREAD); | 185 HandleMark hm(THREAD); |
180 assert(methods->at(i)->is_method(), "must be a Method*"); | 186 assert(methods->at(i)->is_method(), "must be a Method*"); |
181 methodHandle mh(THREAD, methods->at(i)); | 187 methodHandle mh(THREAD, methods->at(i)); |
182 | 188 |
183 bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, checkconstraints, CHECK); | 189 bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, checkconstraints, CHECK); |
187 mh()->set_vtable_index(initialized); // set primary vtable index | 193 mh()->set_vtable_index(initialized); // set primary vtable index |
188 initialized++; | 194 initialized++; |
189 } | 195 } |
190 } | 196 } |
191 | 197 |
192 // add miranda methods; it will also update the value of initialized | 198 // add miranda methods to end of vtable. |
193 fill_in_mirandas(&initialized); | 199 initialized = fill_in_mirandas(initialized); |
194 | 200 |
195 // In class hierarchies where the accessibility is not increasing (i.e., going from private -> | 201 // In class hierarchies where the accessibility is not increasing (i.e., going from private -> |
196 // package_private -> publicprotected), the vtable might actually be smaller than our initial | 202 // package_private -> public/protected), the vtable might actually be smaller than our initial |
197 // calculation. | 203 // calculation. |
198 assert(initialized <= _length, "vtable initialization failed"); | 204 assert(initialized <= _length, "vtable initialization failed"); |
199 for(;initialized < _length; initialized++) { | 205 for(;initialized < _length; initialized++) { |
200 put_method_at(NULL, initialized); | 206 put_method_at(NULL, initialized); |
201 } | 207 } |
246 } | 252 } |
247 | 253 |
248 return superk; | 254 return superk; |
249 } | 255 } |
250 | 256 |
251 // Methods that are "effectively" final don't need vtable entries. | |
252 bool method_is_effectively_final( | |
253 AccessFlags klass_flags, methodHandle target) { | |
254 return target->is_final() || klass_flags.is_final() && !target->is_overpass(); | |
255 } | |
256 | |
257 // Update child's copy of super vtable for overrides | 257 // Update child's copy of super vtable for overrides |
258 // OR return true if a new vtable entry is required | 258 // OR return true if a new vtable entry is required. |
259 // Only called for InstanceKlass's, i.e. not for arrays | 259 // Only called for InstanceKlass's, i.e. not for arrays |
260 // If that changed, could not use _klass as handle for klass | 260 // If that changed, could not use _klass as handle for klass |
261 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, | 261 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, int super_vtable_len, |
262 bool checkconstraints, TRAPS) { | 262 bool checkconstraints, TRAPS) { |
263 ResourceMark rm; | 263 ResourceMark rm; |
264 bool allocate_new = true; | 264 bool allocate_new = true; |
265 assert(klass->oop_is_instance(), "must be InstanceKlass"); | 265 assert(klass->oop_is_instance(), "must be InstanceKlass"); |
266 assert(klass == target_method()->method_holder(), "caller resp."); | |
266 | 267 |
267 // Initialize the method's vtable index to "nonvirtual". | 268 // Initialize the method's vtable index to "nonvirtual". |
268 // If we allocate a vtable entry, we will update it to a non-negative number. | 269 // If we allocate a vtable entry, we will update it to a non-negative number. |
269 target_method()->set_vtable_index(Method::nonvirtual_vtable_index); | 270 target_method()->set_vtable_index(Method::nonvirtual_vtable_index); |
270 | 271 |
271 // Static and <init> methods are never in | 272 // Static and <init> methods are never in |
272 if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) { | 273 if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) { |
273 return false; | 274 return false; |
274 } | 275 } |
275 | 276 |
276 if (method_is_effectively_final(klass->access_flags(), target_method)) { | 277 if (target_method->is_final_method(klass->access_flags())) { |
277 // a final method never needs a new entry; final methods can be statically | 278 // a final method never needs a new entry; final methods can be statically |
278 // resolved and they have to be present in the vtable only if they override | 279 // resolved and they have to be present in the vtable only if they override |
279 // a super's method, in which case they re-use its entry | 280 // a super's method, in which case they re-use its entry |
280 allocate_new = false; | 281 allocate_new = false; |
282 } else if (klass->is_interface()) { | |
283 allocate_new = false; // see note below in needs_new_vtable_entry | |
284 // An interface never allocates new vtable slots, only inherits old ones. | |
285 // This method will either be assigned its own itable index later, | |
286 // or be assigned an inherited vtable index in the loop below. | |
287 target_method()->set_vtable_index(Method::pending_itable_index); | |
281 } | 288 } |
282 | 289 |
283 // we need a new entry if there is no superclass | 290 // we need a new entry if there is no superclass |
284 if (klass->super() == NULL) { | 291 if (klass->super() == NULL) { |
285 return allocate_new; | 292 return allocate_new; |
409 Klass* super, | 416 Klass* super, |
410 Handle classloader, | 417 Handle classloader, |
411 Symbol* classname, | 418 Symbol* classname, |
412 AccessFlags class_flags, | 419 AccessFlags class_flags, |
413 TRAPS) { | 420 TRAPS) { |
414 | 421 if (class_flags.is_interface()) { |
415 if (method_is_effectively_final(class_flags, target_method) || | 422 // Interfaces do not use vtables, so there is no point to assigning |
423 // a vtable index to any of their methods. If we refrain from doing this, | |
424 // we can use Method::_vtable_index to hold the itable index | |
425 return false; | |
426 } | |
427 | |
428 if (target_method->is_final_method(class_flags) || | |
416 // a final method never needs a new entry; final methods can be statically | 429 // a final method never needs a new entry; final methods can be statically |
417 // resolved and they have to be present in the vtable only if they override | 430 // resolved and they have to be present in the vtable only if they override |
418 // a super's method, in which case they re-use its entry | 431 // a super's method, in which case they re-use its entry |
419 (target_method()->is_static()) || | 432 (target_method()->is_static()) || |
420 // static methods don't need to be in vtable | 433 // static methods don't need to be in vtable |
498 } | 511 } |
499 } | 512 } |
500 return Method::invalid_vtable_index; | 513 return Method::invalid_vtable_index; |
501 } | 514 } |
502 | 515 |
503 // check if an entry is miranda | 516 // check if an entry at an index is miranda |
517 // requires that method m at entry be declared ("held") by an interface. | |
504 bool klassVtable::is_miranda_entry_at(int i) { | 518 bool klassVtable::is_miranda_entry_at(int i) { |
505 Method* m = method_at(i); | 519 Method* m = method_at(i); |
506 Klass* method_holder = m->method_holder(); | 520 Klass* method_holder = m->method_holder(); |
507 InstanceKlass *mhk = InstanceKlass::cast(method_holder); | 521 InstanceKlass *mhk = InstanceKlass::cast(method_holder); |
508 | 522 |
514 return true; | 528 return true; |
515 } | 529 } |
516 return false; | 530 return false; |
517 } | 531 } |
518 | 532 |
519 // check if a method is a miranda method, given a class's methods table and it's super | 533 // check if a method is a miranda method, given a class's methods table and its super |
534 // "miranda" means not static, not defined by this class, and not defined | |
535 // in super unless it is private and therefore inaccessible to this class. | |
520 // the caller must make sure that the method belongs to an interface implemented by the class | 536 // the caller must make sure that the method belongs to an interface implemented by the class |
521 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods, Klass* super) { | 537 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods, Klass* super) { |
522 if (m->is_static()) { | 538 if (m->is_static()) { |
523 return false; | 539 return false; |
524 } | 540 } |
539 } | 555 } |
540 | 556 |
541 return false; | 557 return false; |
542 } | 558 } |
543 | 559 |
560 // Scans current_interface_methods for miranda methods that do not | |
561 // already appear in new_mirandas and are also not defined-and-non-private | |
562 // in super (superclass). These mirandas are added to all_mirandas if it is | |
563 // not null; in addition, those that are not duplicates of miranda methods | |
564 // inherited by super from its interfaces are added to new_mirandas. | |
565 // Thus, new_mirandas will be the set of mirandas that this class introduces, | |
566 // all_mirandas will be the set of all mirandas applicable to this class | |
567 // including all defined in superclasses. | |
544 void klassVtable::add_new_mirandas_to_lists( | 568 void klassVtable::add_new_mirandas_to_lists( |
545 GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas, | 569 GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas, |
546 Array<Method*>* current_interface_methods, Array<Method*>* class_methods, | 570 Array<Method*>* current_interface_methods, Array<Method*>* class_methods, |
547 Klass* super) { | 571 Klass* super) { |
548 // iterate thru the current interface's method to see if it a miranda | 572 // iterate thru the current interface's method to see if it a miranda |
597 sik->methods(), class_methods, super); | 621 sik->methods(), class_methods, super); |
598 } | 622 } |
599 } | 623 } |
600 } | 624 } |
601 | 625 |
602 // fill in mirandas | 626 // Discover miranda methods ("miranda" = "interface abstract, no binding"), |
603 void klassVtable::fill_in_mirandas(int* initialized) { | 627 // and append them into the vtable starting at index initialized, |
628 // return the new value of initialized. | |
629 int klassVtable::fill_in_mirandas(int initialized) { | |
604 GrowableArray<Method*> mirandas(20); | 630 GrowableArray<Method*> mirandas(20); |
605 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(), | 631 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(), |
606 ik()->local_interfaces()); | 632 ik()->local_interfaces()); |
607 for (int i = 0; i < mirandas.length(); i++) { | 633 for (int i = 0; i < mirandas.length(); i++) { |
608 put_method_at(mirandas.at(i), *initialized); | 634 put_method_at(mirandas.at(i), initialized); |
609 ++(*initialized); | 635 ++initialized; |
610 } | 636 } |
611 } | 637 return initialized; |
612 | 638 } |
639 | |
640 // Copy this class's vtable to the vtable beginning at start. | |
641 // Used to copy superclass vtable to prefix of subclass's vtable. | |
613 void klassVtable::copy_vtable_to(vtableEntry* start) { | 642 void klassVtable::copy_vtable_to(vtableEntry* start) { |
614 Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size()); | 643 Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size()); |
615 } | 644 } |
616 | 645 |
617 #if INCLUDE_JVMTI | 646 #if INCLUDE_JVMTI |
721 | 750 |
722 static int initialize_count = 0; | 751 static int initialize_count = 0; |
723 | 752 |
724 // Initialization | 753 // Initialization |
725 void klassItable::initialize_itable(bool checkconstraints, TRAPS) { | 754 void klassItable::initialize_itable(bool checkconstraints, TRAPS) { |
755 if (_klass->is_interface()) { | |
756 // This needs to go after vtable indexes are assigned but | |
757 // before implementors need to know the number of itable indexes. | |
758 assign_itable_indexes_for_interface(_klass()); | |
759 } | |
760 | |
726 // Cannot be setup doing bootstrapping, interfaces don't have | 761 // Cannot be setup doing bootstrapping, interfaces don't have |
727 // itables, and klass with only ones entry have empty itables | 762 // itables, and klass with only ones entry have empty itables |
728 if (Universe::is_bootstrapping() || | 763 if (Universe::is_bootstrapping() || |
729 _klass->is_interface() || | 764 _klass->is_interface() || |
730 _klass->itable_length() == itableOffsetEntry::size()) return; | 765 _klass->itable_length() == itableOffsetEntry::size()) return; |
752 itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1); | 787 itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1); |
753 guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing"); | 788 guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing"); |
754 } | 789 } |
755 | 790 |
756 | 791 |
792 inline bool interface_method_needs_itable_index(Method* m) { | |
793 if (m->is_static()) return false; // e.g., Stream.empty | |
794 if (m->is_initializer()) return false; // <init> or <clinit> | |
795 // If an interface redeclares a method from java.lang.Object, | |
796 // it should already have a vtable index, don't touch it. | |
797 // e.g., CharSequence.toString (from initialize_vtable) | |
798 // if (m->has_vtable_index()) return false; // NO! | |
799 return true; | |
800 } | |
801 | |
802 int klassItable::assign_itable_indexes_for_interface(Klass* klass) { | |
803 // an interface does not have an itable, but its methods need to be numbered | |
804 if (TraceItables) tty->print_cr("%3d: Initializing itable for interface %s", ++initialize_count, | |
805 klass->name()->as_C_string()); | |
806 Array<Method*>* methods = InstanceKlass::cast(klass)->methods(); | |
807 int nof_methods = methods->length(); | |
808 int ime_num = 0; | |
809 for (int i = 0; i < nof_methods; i++) { | |
810 Method* m = methods->at(i); | |
811 if (interface_method_needs_itable_index(m)) { | |
812 assert(!m->is_final_method(), "no final interface methods"); | |
813 // If m is already assigned a vtable index, do not disturb it. | |
814 if (!m->has_vtable_index()) { | |
815 assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable"); | |
816 m->set_itable_index(ime_num); | |
817 // Progress to next itable entry | |
818 ime_num++; | |
819 } | |
820 } | |
821 } | |
822 assert(ime_num == method_count_for_interface(klass), "proper sizing"); | |
823 return ime_num; | |
824 } | |
825 | |
826 int klassItable::method_count_for_interface(Klass* interf) { | |
827 assert(interf->oop_is_instance(), "must be"); | |
828 assert(interf->is_interface(), "must be"); | |
829 Array<Method*>* methods = InstanceKlass::cast(interf)->methods(); | |
830 int nof_methods = methods->length(); | |
831 while (nof_methods > 0) { | |
832 Method* m = methods->at(nof_methods-1); | |
833 if (m->has_itable_index()) { | |
834 int length = m->itable_index() + 1; | |
835 #ifdef ASSERT | |
836 while (nof_methods = 0) { | |
837 m = methods->at(--nof_methods); | |
838 assert(!m->has_itable_index() || m->itable_index() < length, ""); | |
839 } | |
840 #endif //ASSERT | |
841 return length; // return the rightmost itable index, plus one | |
842 } | |
843 nof_methods -= 1; | |
844 } | |
845 // no methods have itable indexes | |
846 return 0; | |
847 } | |
848 | |
849 | |
757 void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) { | 850 void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) { |
758 Array<Method*>* methods = InstanceKlass::cast(interf_h())->methods(); | 851 Array<Method*>* methods = InstanceKlass::cast(interf_h())->methods(); |
759 int nof_methods = methods->length(); | 852 int nof_methods = methods->length(); |
760 HandleMark hm; | 853 HandleMark hm; |
761 KlassHandle klass = _klass; | |
762 assert(nof_methods > 0, "at least one method must exist for interface to be in vtable"); | 854 assert(nof_methods > 0, "at least one method must exist for interface to be in vtable"); |
763 Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader()); | 855 Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader()); |
764 int ime_num = 0; | 856 |
765 | 857 int ime_count = method_count_for_interface(interf_h()); |
766 // Skip first Method* if it is a class initializer | 858 for (int i = 0; i < nof_methods; i++) { |
767 int i = methods->at(0)->is_static_initializer() ? 1 : 0; | |
768 | |
769 // m, method_name, method_signature, klass reset each loop so they | |
770 // don't need preserving across check_signature_loaders call | |
771 // methods needs a handle in case of gc from check_signature_loaders | |
772 for(; i < nof_methods; i++) { | |
773 Method* m = methods->at(i); | 859 Method* m = methods->at(i); |
774 Symbol* method_name = m->name(); | 860 methodHandle target; |
775 Symbol* method_signature = m->signature(); | 861 if (m->has_itable_index()) { |
776 | 862 LinkResolver::lookup_instance_method_in_klasses(target, _klass, m->name(), m->signature(), CHECK); |
777 // This is same code as in Linkresolver::lookup_instance_method_in_klasses | |
778 Method* target = klass->uncached_lookup_method(method_name, method_signature); | |
779 while (target != NULL && target->is_static()) { | |
780 // continue with recursive lookup through the superclass | |
781 Klass* super = target->method_holder()->super(); | |
782 target = (super == NULL) ? (Method*)NULL : super->uncached_lookup_method(method_name, method_signature); | |
783 } | 863 } |
784 if (target == NULL || !target->is_public() || target->is_abstract()) { | 864 if (target == NULL || !target->is_public() || target->is_abstract()) { |
785 // Entry do not resolve. Leave it empty | 865 // Entry do not resolve. Leave it empty |
786 } else { | 866 } else { |
787 // Entry did resolve, check loader constraints before initializing | 867 // Entry did resolve, check loader constraints before initializing |
788 // if checkconstraints requested | 868 // if checkconstraints requested |
789 methodHandle target_h (THREAD, target); // preserve across gc | |
790 if (checkconstraints) { | 869 if (checkconstraints) { |
791 Handle method_holder_loader (THREAD, target->method_holder()->class_loader()); | 870 Handle method_holder_loader (THREAD, target->method_holder()->class_loader()); |
792 if (method_holder_loader() != interface_loader()) { | 871 if (method_holder_loader() != interface_loader()) { |
793 ResourceMark rm(THREAD); | 872 ResourceMark rm(THREAD); |
794 Symbol* failed_type_symbol = | 873 Symbol* failed_type_symbol = |
795 SystemDictionary::check_signature_loaders(method_signature, | 874 SystemDictionary::check_signature_loaders(m->signature(), |
796 method_holder_loader, | 875 method_holder_loader, |
797 interface_loader, | 876 interface_loader, |
798 true, CHECK); | 877 true, CHECK); |
799 if (failed_type_symbol != NULL) { | 878 if (failed_type_symbol != NULL) { |
800 const char* msg = "loader constraint violation in interface " | 879 const char* msg = "loader constraint violation in interface " |
801 "itable initialization: when resolving method \"%s\" the class" | 880 "itable initialization: when resolving method \"%s\" the class" |
802 " loader (instance of %s) of the current class, %s, " | 881 " loader (instance of %s) of the current class, %s, " |
803 "and the class loader (instance of %s) for interface " | 882 "and the class loader (instance of %s) for interface " |
804 "%s have different Class objects for the type %s " | 883 "%s have different Class objects for the type %s " |
805 "used in the signature"; | 884 "used in the signature"; |
806 char* sig = target_h()->name_and_sig_as_C_string(); | 885 char* sig = target()->name_and_sig_as_C_string(); |
807 const char* loader1 = SystemDictionary::loader_name(method_holder_loader()); | 886 const char* loader1 = SystemDictionary::loader_name(method_holder_loader()); |
808 char* current = klass->name()->as_C_string(); | 887 char* current = _klass->name()->as_C_string(); |
809 const char* loader2 = SystemDictionary::loader_name(interface_loader()); | 888 const char* loader2 = SystemDictionary::loader_name(interface_loader()); |
810 char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string(); | 889 char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string(); |
811 char* failed_type_name = failed_type_symbol->as_C_string(); | 890 char* failed_type_name = failed_type_symbol->as_C_string(); |
812 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + | 891 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + |
813 strlen(current) + strlen(loader2) + strlen(iface) + | 892 strlen(current) + strlen(loader2) + strlen(iface) + |
819 } | 898 } |
820 } | 899 } |
821 } | 900 } |
822 | 901 |
823 // ime may have moved during GC so recalculate address | 902 // ime may have moved during GC so recalculate address |
824 itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target_h()); | 903 int ime_num = m->itable_index(); |
825 } | 904 assert(ime_num < ime_count, "oob"); |
826 // Progress to next entry | 905 itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target()); |
827 ime_num++; | 906 } |
828 } | 907 } |
829 } | 908 } |
830 | 909 |
831 // Update entry for specific Method* | 910 // Update entry for specific Method* |
832 void klassItable::initialize_with_method(Method* m) { | 911 void klassItable::initialize_with_method(Method* m) { |
911 class InterfaceVisiterClosure : public StackObj { | 990 class InterfaceVisiterClosure : public StackObj { |
912 public: | 991 public: |
913 virtual void doit(Klass* intf, int method_count) = 0; | 992 virtual void doit(Klass* intf, int method_count) = 0; |
914 }; | 993 }; |
915 | 994 |
916 // Visit all interfaces with at-least one method (excluding <clinit>) | 995 // Visit all interfaces with at least one itable method |
917 void visit_all_interfaces(Array<Klass*>* transitive_intf, InterfaceVisiterClosure *blk) { | 996 void visit_all_interfaces(Array<Klass*>* transitive_intf, InterfaceVisiterClosure *blk) { |
918 // Handle array argument | 997 // Handle array argument |
919 for(int i = 0; i < transitive_intf->length(); i++) { | 998 for(int i = 0; i < transitive_intf->length(); i++) { |
920 Klass* intf = transitive_intf->at(i); | 999 Klass* intf = transitive_intf->at(i); |
921 assert(intf->is_interface(), "sanity check"); | 1000 assert(intf->is_interface(), "sanity check"); |
922 | 1001 |
923 // Find no. of methods excluding a <clinit> | 1002 // Find no. of itable methods |
924 int method_count = InstanceKlass::cast(intf)->methods()->length(); | 1003 int method_count = 0; |
925 if (method_count > 0) { | 1004 // method_count = klassItable::method_count_for_interface(intf); |
926 Method* m = InstanceKlass::cast(intf)->methods()->at(0); | 1005 Array<Method*>* methods = InstanceKlass::cast(intf)->methods(); |
927 assert(m != NULL && m->is_method(), "sanity check"); | 1006 if (methods->length() > 0) { |
928 if (m->name() == vmSymbols::object_initializer_name()) { | 1007 for (int i = methods->length(); --i >= 0; ) { |
929 method_count--; | 1008 if (interface_method_needs_itable_index(methods->at(i))) { |
1009 method_count++; | |
1010 } | |
930 } | 1011 } |
931 } | 1012 } |
932 | 1013 |
933 // Only count interfaces with at least one method | 1014 // Only count interfaces with at least one method |
934 if (method_count > 0) { | 1015 if (method_count > 0) { |
1022 assert( (oop*)(ime) == v, "wrong offset calculation (2)"); | 1103 assert( (oop*)(ime) == v, "wrong offset calculation (2)"); |
1023 #endif | 1104 #endif |
1024 } | 1105 } |
1025 | 1106 |
1026 | 1107 |
1027 // m must be a method in an interface | 1108 // inverse to itable_index |
1028 int klassItable::compute_itable_index(Method* m) { | |
1029 InstanceKlass* intf = m->method_holder(); | |
1030 assert(intf->is_interface(), "sanity check"); | |
1031 Array<Method*>* methods = intf->methods(); | |
1032 int index = 0; | |
1033 while(methods->at(index) != m) { | |
1034 index++; | |
1035 assert(index < methods->length(), "should find index for resolve_invoke"); | |
1036 } | |
1037 // Adjust for <clinit>, which is left out of table if first method | |
1038 if (methods->length() > 0 && methods->at(0)->is_static_initializer()) { | |
1039 index--; | |
1040 } | |
1041 return index; | |
1042 } | |
1043 | |
1044 | |
1045 // inverse to compute_itable_index | |
1046 Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) { | 1109 Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) { |
1047 assert(InstanceKlass::cast(intf)->is_interface(), "sanity check"); | 1110 assert(InstanceKlass::cast(intf)->is_interface(), "sanity check"); |
1111 assert(intf->verify_itable_index(itable_index), ""); | |
1048 Array<Method*>* methods = InstanceKlass::cast(intf)->methods(); | 1112 Array<Method*>* methods = InstanceKlass::cast(intf)->methods(); |
1049 | 1113 |
1114 if (itable_index < 0 || itable_index >= method_count_for_interface(intf)) | |
1115 return NULL; // help caller defend against bad indexes | |
1116 | |
1050 int index = itable_index; | 1117 int index = itable_index; |
1051 // Adjust for <clinit>, which is left out of table if first method | |
1052 if (methods->length() > 0 && methods->at(0)->is_static_initializer()) { | |
1053 index++; | |
1054 } | |
1055 | |
1056 if (itable_index < 0 || index >= methods->length()) | |
1057 return NULL; // help caller defend against bad indexes | |
1058 | |
1059 Method* m = methods->at(index); | 1118 Method* m = methods->at(index); |
1060 assert(compute_itable_index(m) == itable_index, "correct inverse"); | 1119 int index2 = -1; |
1120 while (!m->has_itable_index() || | |
1121 (index2 = m->itable_index()) != itable_index) { | |
1122 assert(index2 < itable_index, "monotonic"); | |
1123 if (++index == methods->length()) | |
1124 return NULL; | |
1125 m = methods->at(index); | |
1126 } | |
1127 assert(m->itable_index() == itable_index, "correct inverse"); | |
1061 | 1128 |
1062 return m; | 1129 return m; |
1063 } | 1130 } |
1064 | 1131 |
1065 void klassVtable::verify(outputStream* st, bool forced) { | 1132 void klassVtable::verify(outputStream* st, bool forced) { |