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, |
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; |
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 |
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) + |
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) { |