49 }
50
51 bool klassVtable::is_preinitialized_vtable() {
52 return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
53 }
54
55
56 // this function computes the vtable size (including the size needed for miranda
57 // methods) and the number of miranda methods in this class.
58 // Note on Miranda methods: Let's say there is a class C that implements
59 // interface I, and none of C's superclasses implements I.
60 // Let's say there is an abstract method m in I that neither C
61 // nor any of its super classes implement (i.e there is no method of any access,
62 // with the same name and signature as m), then m is a Miranda method which is
63 // entered as a public abstract method in C's vtable. From then on it should
64 // treated as any other public method in C for method over-ride purposes.
65 void klassVtable::compute_vtable_size_and_num_mirandas(
66 int* vtable_length_ret, int* num_new_mirandas,
67 GrowableArray<Method*>* all_mirandas, const Klass* super,
68 Array<Method*>* methods, AccessFlags class_flags, u2 major_version,
69 Handle classloader, Symbol* classname, Array<Klass*>* local_interfaces,
70 TRAPS) {
71 NoSafepointVerifier nsv;
72
73 // set up default result values
74 int vtable_length = 0;
75
76 // start off with super's vtable length
77 vtable_length = super == NULL ? 0 : super->vtable_length();
78
79 // go thru each method in the methods table to see if it needs a new entry
80 int len = methods->length();
81 for (int i = 0; i < len; i++) {
82 assert(methods->at(i)->is_method(), "must be a Method*");
83 methodHandle mh(THREAD, methods->at(i));
84
85 if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, major_version, THREAD)) {
86 vtable_length += vtableEntry::size(); // we need a new entry
87 }
88 }
89
149 assert(superVtable.length() <= _length, "vtable too short");
150 #ifdef ASSERT
151 superVtable.verify(tty, true);
152 #endif
153 superVtable.copy_vtable_to(table());
154 if (log_develop_is_enabled(Trace, vtables)) {
155 ResourceMark rm;
156 log_develop_trace(vtables)("copy vtable from %s to %s size %d",
157 super->internal_name(), klass()->internal_name(),
158 _length);
159 }
160 return superVtable.length();
161 }
162 }
163
164 //
165 // Revised lookup semantics introduced 1.3 (Kestrel beta)
166 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
167
168 // Note: Arrays can have intermediate array supers. Use java_super to skip them.
169 Klass* super = _klass->java_super();
170 int nofNewEntries = 0;
171
172 bool is_shared = _klass->is_shared();
173
174 if (!_klass->is_array_klass()) {
175 ResourceMark rm(THREAD);
176 log_develop_debug(vtables)("Initializing: %s", _klass->name()->as_C_string());
177 }
178
179 #ifdef ASSERT
180 oop* end_of_obj = (oop*)_klass + _klass->size();
181 oop* end_of_vtable = (oop*)&table()[_length];
182 assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
183 #endif
184
185 if (Universe::is_bootstrapping()) {
186 assert(!is_shared, "sanity");
187 // just clear everything
188 for (int i = 0; i < _length; i++) table()[i].clear();
189 return;
854 if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
855 if (is_miranda(im, class_methods, default_methods, super, is_interface)) { // is it a miranda at all?
856 const InstanceKlass *sk = InstanceKlass::cast(super);
857 // check if it is a duplicate of a super's miranda
858 if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::find_defaults) == NULL) {
859 new_mirandas->append(im);
860 }
861 if (all_mirandas != NULL) {
862 all_mirandas->append(im);
863 }
864 }
865 }
866 }
867 }
868
869 void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,
870 GrowableArray<Method*>* all_mirandas,
871 const Klass* super,
872 Array<Method*>* class_methods,
873 Array<Method*>* default_methods,
874 Array<Klass*>* local_interfaces,
875 bool is_interface) {
876 assert((new_mirandas->length() == 0) , "current mirandas must be 0");
877
878 // iterate thru the local interfaces looking for a miranda
879 int num_local_ifs = local_interfaces->length();
880 for (int i = 0; i < num_local_ifs; i++) {
881 InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i));
882 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
883 ik->methods(), class_methods,
884 default_methods, super, is_interface);
885 // iterate thru each local's super interfaces
886 Array<Klass*>* super_ifs = ik->transitive_interfaces();
887 int num_super_ifs = super_ifs->length();
888 for (int j = 0; j < num_super_ifs; j++) {
889 InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
890 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
891 sik->methods(), class_methods,
892 default_methods, super, is_interface);
893 }
894 }
895 }
896
897 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
898 // and append them into the vtable starting at index initialized,
899 // return the new value of initialized.
900 // Miranda methods use vtable entries, but do not get assigned a vtable_index
901 // The vtable_index is discovered by searching from the end of the vtable
902 int klassVtable::fill_in_mirandas(int initialized, TRAPS) {
903 ResourceMark rm(THREAD);
904 GrowableArray<Method*> mirandas(20);
905 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
906 ik()->default_methods(), ik()->local_interfaces(),
907 klass()->is_interface());
908 for (int i = 0; i < mirandas.length(); i++) {
909 if (log_develop_is_enabled(Trace, vtables)) {
1065 _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
1066 _size_method_table = (end - method_entry) / itableMethodEntry::size();
1067 assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
1068 return;
1069 }
1070 }
1071
1072 // The length of the itable was either zero, or it has not yet been initialized.
1073 _table_offset = 0;
1074 _size_offset_table = 0;
1075 _size_method_table = 0;
1076 }
1077
1078 static int initialize_count = 0;
1079
1080 // Initialization
1081 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1082 if (_klass->is_interface()) {
1083 // This needs to go after vtable indices are assigned but
1084 // before implementors need to know the number of itable indices.
1085 assign_itable_indices_for_interface(_klass, THREAD);
1086 }
1087
1088 // Cannot be setup doing bootstrapping, interfaces don't have
1089 // itables, and klass with only ones entry have empty itables
1090 if (Universe::is_bootstrapping() ||
1091 _klass->is_interface() ||
1092 _klass->itable_length() == itableOffsetEntry::size()) return;
1093
1094 // There's alway an extra itable entry so we can null-terminate it.
1095 guarantee(size_offset_table() >= 1, "too small");
1096 int num_interfaces = size_offset_table() - 1;
1097 if (num_interfaces > 0) {
1098 ResourceMark rm(THREAD);
1099 log_develop_debug(itables)("%3d: Initializing itables for %s", ++initialize_count,
1100 _klass->name()->as_C_string());
1101
1102
1103 // Iterate through all interfaces
1104 int i;
1105 for(i = 0; i < num_interfaces; i++) {
1106 itableOffsetEntry* ioe = offset_entry(i);
1107 HandleMark hm(THREAD);
1108 Klass* interf = ioe->interface_klass();
1109 assert(interf != NULL && ioe->offset() != 0, "bad offset entry in itable");
1110 initialize_itable_for_interface(ioe->offset(), interf, checkconstraints, CHECK);
1111 }
1112
1113 }
1114 // Check that the last entry is empty
1115 itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1116 guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1117 }
1118
1119
1120 inline bool interface_method_needs_itable_index(Method* m) {
1121 if (m->is_static()) return false; // e.g., Stream.empty
1122 if (m->is_initializer()) return false; // <init> or <clinit>
1123 if (m->is_private()) return false; // uses direct call
1124 // If an interface redeclares a method from java.lang.Object,
1125 // it should already have a vtable index, don't touch it.
1126 // e.g., CharSequence.toString (from initialize_vtable)
1127 // if (m->has_vtable_index()) return false; // NO!
1128 return true;
1129 }
1130
1131 int klassItable::assign_itable_indices_for_interface(Klass* klass, TRAPS) {
1132 // an interface does not have an itable, but its methods need to be numbered
1133 ResourceMark rm(THREAD);
1134 log_develop_debug(itables)("%3d: Initializing itable indices for interface %s",
1135 ++initialize_count, klass->name()->as_C_string());
1136 Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1137 int nof_methods = methods->length();
1138 int ime_num = 0;
1139 for (int i = 0; i < nof_methods; i++) {
1140 Method* m = methods->at(i);
1141 if (interface_method_needs_itable_index(m)) {
1142 assert(!m->is_final_method(), "no final interface methods");
1143 // If m is already assigned a vtable index, do not disturb it.
1144 if (log_develop_is_enabled(Trace, itables)) {
1145 LogTarget(Trace, itables) lt;
1146 LogStream ls(lt);
1147 assert(m != NULL, "methods can never be null");
1148 const char* sig = m->name_and_sig_as_C_string();
1149 if (m->has_vtable_index()) {
1150 ls.print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig);
1151 } else {
1152 ls.print("itable index %d for method: %s, flags: ", ime_num, sig);
1153 }
1154 m->print_linkage_flags(&ls);
1155 ls.cr();
1156 }
1157 if (!m->has_vtable_index()) {
1158 // A shared method could have an initialized itable_index that
1159 // is < 0.
1160 assert(m->vtable_index() == Method::pending_itable_index ||
1161 m->is_shared(),
1162 "set by initialize_vtable");
1163 m->set_itable_index(ime_num);
1164 // Progress to next itable entry
1165 ime_num++;
1166 }
1167 }
1168 }
1169 assert(ime_num == method_count_for_interface(klass), "proper sizing");
1170 return ime_num;
1171 }
1172
1173 int klassItable::method_count_for_interface(Klass* interf) {
1174 assert(interf->is_instance_klass(), "must be");
1175 assert(interf->is_interface(), "must be");
1176 Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
1177 int nof_methods = methods->length();
1178 int length = 0;
1179 while (nof_methods > 0) {
1180 Method* m = methods->at(nof_methods-1);
1181 if (m->has_itable_index()) {
1182 length = m->itable_index() + 1;
1183 break;
1184 }
1185 nof_methods -= 1;
1186 }
1187 #ifdef ASSERT
1188 int nof_methods_copy = nof_methods;
1189 while (nof_methods_copy > 0) {
1190 Method* mm = methods->at(--nof_methods_copy);
1191 assert(!mm->has_itable_index() || mm->itable_index() < length, "");
1192 }
1193 #endif //ASSERT
1194 // return the rightmost itable index, plus one; or 0 if no methods have
1195 // itable indices
1196 return length;
1197 }
1198
1199
1200 void klassItable::initialize_itable_for_interface(int method_table_offset, Klass* interf, bool checkconstraints, TRAPS) {
1201 Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
1202 int nof_methods = methods->length();
1203 HandleMark hm;
1204 Handle interface_loader (THREAD, InstanceKlass::cast(interf)->class_loader());
1205
1206 int ime_count = method_count_for_interface(interf);
1207 for (int i = 0; i < nof_methods; i++) {
1208 Method* m = methods->at(i);
1209 methodHandle target;
1210 if (m->has_itable_index()) {
1211 // This search must match the runtime resolution, i.e. selection search for invokeinterface
1212 // to correctly enforce loader constraints for interface method inheritance.
1213 // Private methods are skipped as a private class method can never be the implementation
1214 // of an interface method.
1215 // Invokespecial does not perform selection based on the receiver, so it does not use
1216 // the cached itable.
1217 target = LinkResolver::lookup_instance_method_in_klasses(_klass, m->name(), m->signature(),
1218 Klass::skip_private, CHECK);
1219 }
1220 if (target == NULL || !target->is_public() || target->is_abstract() || target->is_overpass()) {
1221 assert(target == NULL || !target->is_overpass() || target->is_public(),
1222 "Non-public overpass method!");
1223 // Entry does not resolve. Leave it empty for AbstractMethodError or other error.
1224 if (!(target == NULL) && !target->is_public()) {
1332 for (int i = 0; i < _size_method_table; i++) {
1333 Method* m = ime->method();
1334 if (m != NULL) {
1335 tty->print(" (%5d) ", i);
1336 m->access_flags().print_on(tty);
1337 if (m->is_default_method()) {
1338 tty->print("default ");
1339 }
1340 tty->print(" -- ");
1341 m->print_name(tty);
1342 tty->cr();
1343 }
1344 ime++;
1345 }
1346 }
1347 #endif // INCLUDE_JVMTI
1348
1349 // Setup
1350 class InterfaceVisiterClosure : public StackObj {
1351 public:
1352 virtual void doit(Klass* intf, int method_count) = 0;
1353 };
1354
1355 // Visit all interfaces with at least one itable method
1356 void visit_all_interfaces(Array<Klass*>* transitive_intf, InterfaceVisiterClosure *blk) {
1357 // Handle array argument
1358 for(int i = 0; i < transitive_intf->length(); i++) {
1359 Klass* intf = transitive_intf->at(i);
1360 assert(intf->is_interface(), "sanity check");
1361
1362 // Find no. of itable methods
1363 int method_count = 0;
1364 // method_count = klassItable::method_count_for_interface(intf);
1365 Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
1366 if (methods->length() > 0) {
1367 for (int i = methods->length(); --i >= 0; ) {
1368 if (interface_method_needs_itable_index(methods->at(i))) {
1369 method_count++;
1370 }
1371 }
1372 }
1373
1374 // Visit all interfaces which either have any methods or can participate in receiver type check.
1375 // We do not bother to count methods in transitive interfaces, although that would allow us to skip
1376 // this step in the rare case of a zero-method interface extending another zero-method interface.
1377 if (method_count > 0 || InstanceKlass::cast(intf)->transitive_interfaces()->length() > 0) {
1378 blk->doit(intf, method_count);
1379 }
1380 }
1381 }
1382
1383 class CountInterfacesClosure : public InterfaceVisiterClosure {
1384 private:
1385 int _nof_methods;
1386 int _nof_interfaces;
1387 public:
1388 CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; }
1389
1390 int nof_methods() const { return _nof_methods; }
1391 int nof_interfaces() const { return _nof_interfaces; }
1392
1393 void doit(Klass* intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; }
1394 };
1395
1396 class SetupItableClosure : public InterfaceVisiterClosure {
1397 private:
1398 itableOffsetEntry* _offset_entry;
1399 itableMethodEntry* _method_entry;
1400 address _klass_begin;
1401 public:
1402 SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) {
1403 _klass_begin = klass_begin;
1404 _offset_entry = offset_entry;
1405 _method_entry = method_entry;
1406 }
1407
1408 itableMethodEntry* method_entry() const { return _method_entry; }
1409
1410 void doit(Klass* intf, int method_count) {
1411 int offset = ((address)_method_entry) - _klass_begin;
1412 _offset_entry->initialize(intf, offset);
1413 _offset_entry++;
1414 _method_entry += method_count;
1415 }
1416 };
1417
1418 int klassItable::compute_itable_size(Array<Klass*>* transitive_interfaces) {
1419 // Count no of interfaces and total number of interface methods
1420 CountInterfacesClosure cic;
1421 visit_all_interfaces(transitive_interfaces, &cic);
1422
1423 // There's alway an extra itable entry so we can null-terminate it.
1424 int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods());
1425
1426 // Statistics
1427 update_stats(itable_size * wordSize);
1428
1429 return itable_size;
1430 }
1431
1432
1433 // Fill out offset table and interface klasses into the itable space
1434 void klassItable::setup_itable_offset_table(InstanceKlass* klass) {
1435 if (klass->itable_length() == 0) return;
1436 assert(!klass->is_interface(), "Should have zero length itable");
1437
1438 // Count no of interfaces and total number of interface methods
1451 // Fill-out offset table
1452 itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();
1453 itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);
1454 intptr_t* end = klass->end_of_itable();
1455 assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");
1456 assert((oop*)(end) == (oop*)(ime + nof_methods), "wrong offset calculation (2)");
1457
1458 // Visit all interfaces and initialize itable offset table
1459 SetupItableClosure sic((address)klass, ioe, ime);
1460 visit_all_interfaces(klass->transitive_interfaces(), &sic);
1461
1462 #ifdef ASSERT
1463 ime = sic.method_entry();
1464 oop* v = (oop*) klass->end_of_itable();
1465 assert( (oop*)(ime) == v, "wrong offset calculation (2)");
1466 #endif
1467 }
1468
1469
1470 // inverse to itable_index
1471 Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) {
1472 assert(InstanceKlass::cast(intf)->is_interface(), "sanity check");
1473 assert(intf->verify_itable_index(itable_index), "");
1474 Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
1475
1476 if (itable_index < 0 || itable_index >= method_count_for_interface(intf))
1477 return NULL; // help caller defend against bad indices
1478
1479 int index = itable_index;
1480 Method* m = methods->at(index);
1481 int index2 = -1;
1482 while (!m->has_itable_index() ||
1483 (index2 = m->itable_index()) != itable_index) {
1484 assert(index2 < itable_index, "monotonic");
1485 if (++index == methods->length())
1486 return NULL;
1487 m = methods->at(index);
1488 }
1489 assert(m->itable_index() == itable_index, "correct inverse");
1490
1491 return m;
1492 }
|
49 }
50
51 bool klassVtable::is_preinitialized_vtable() {
52 return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
53 }
54
55
56 // this function computes the vtable size (including the size needed for miranda
57 // methods) and the number of miranda methods in this class.
58 // Note on Miranda methods: Let's say there is a class C that implements
59 // interface I, and none of C's superclasses implements I.
60 // Let's say there is an abstract method m in I that neither C
61 // nor any of its super classes implement (i.e there is no method of any access,
62 // with the same name and signature as m), then m is a Miranda method which is
63 // entered as a public abstract method in C's vtable. From then on it should
64 // treated as any other public method in C for method over-ride purposes.
65 void klassVtable::compute_vtable_size_and_num_mirandas(
66 int* vtable_length_ret, int* num_new_mirandas,
67 GrowableArray<Method*>* all_mirandas, const Klass* super,
68 Array<Method*>* methods, AccessFlags class_flags, u2 major_version,
69 Handle classloader, Symbol* classname, Array<InstanceKlass*>* local_interfaces,
70 TRAPS) {
71 NoSafepointVerifier nsv;
72
73 // set up default result values
74 int vtable_length = 0;
75
76 // start off with super's vtable length
77 vtable_length = super == NULL ? 0 : super->vtable_length();
78
79 // go thru each method in the methods table to see if it needs a new entry
80 int len = methods->length();
81 for (int i = 0; i < len; i++) {
82 assert(methods->at(i)->is_method(), "must be a Method*");
83 methodHandle mh(THREAD, methods->at(i));
84
85 if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, major_version, THREAD)) {
86 vtable_length += vtableEntry::size(); // we need a new entry
87 }
88 }
89
149 assert(superVtable.length() <= _length, "vtable too short");
150 #ifdef ASSERT
151 superVtable.verify(tty, true);
152 #endif
153 superVtable.copy_vtable_to(table());
154 if (log_develop_is_enabled(Trace, vtables)) {
155 ResourceMark rm;
156 log_develop_trace(vtables)("copy vtable from %s to %s size %d",
157 super->internal_name(), klass()->internal_name(),
158 _length);
159 }
160 return superVtable.length();
161 }
162 }
163
164 //
165 // Revised lookup semantics introduced 1.3 (Kestrel beta)
166 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
167
168 // Note: Arrays can have intermediate array supers. Use java_super to skip them.
169 InstanceKlass* super = _klass->java_super();
170 int nofNewEntries = 0;
171
172 bool is_shared = _klass->is_shared();
173
174 if (!_klass->is_array_klass()) {
175 ResourceMark rm(THREAD);
176 log_develop_debug(vtables)("Initializing: %s", _klass->name()->as_C_string());
177 }
178
179 #ifdef ASSERT
180 oop* end_of_obj = (oop*)_klass + _klass->size();
181 oop* end_of_vtable = (oop*)&table()[_length];
182 assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
183 #endif
184
185 if (Universe::is_bootstrapping()) {
186 assert(!is_shared, "sanity");
187 // just clear everything
188 for (int i = 0; i < _length; i++) table()[i].clear();
189 return;
854 if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
855 if (is_miranda(im, class_methods, default_methods, super, is_interface)) { // is it a miranda at all?
856 const InstanceKlass *sk = InstanceKlass::cast(super);
857 // check if it is a duplicate of a super's miranda
858 if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::find_defaults) == NULL) {
859 new_mirandas->append(im);
860 }
861 if (all_mirandas != NULL) {
862 all_mirandas->append(im);
863 }
864 }
865 }
866 }
867 }
868
869 void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,
870 GrowableArray<Method*>* all_mirandas,
871 const Klass* super,
872 Array<Method*>* class_methods,
873 Array<Method*>* default_methods,
874 Array<InstanceKlass*>* local_interfaces,
875 bool is_interface) {
876 assert((new_mirandas->length() == 0) , "current mirandas must be 0");
877
878 // iterate thru the local interfaces looking for a miranda
879 int num_local_ifs = local_interfaces->length();
880 for (int i = 0; i < num_local_ifs; i++) {
881 InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i));
882 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
883 ik->methods(), class_methods,
884 default_methods, super, is_interface);
885 // iterate thru each local's super interfaces
886 Array<InstanceKlass*>* super_ifs = ik->transitive_interfaces();
887 int num_super_ifs = super_ifs->length();
888 for (int j = 0; j < num_super_ifs; j++) {
889 InstanceKlass *sik = super_ifs->at(j);
890 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
891 sik->methods(), class_methods,
892 default_methods, super, is_interface);
893 }
894 }
895 }
896
897 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
898 // and append them into the vtable starting at index initialized,
899 // return the new value of initialized.
900 // Miranda methods use vtable entries, but do not get assigned a vtable_index
901 // The vtable_index is discovered by searching from the end of the vtable
902 int klassVtable::fill_in_mirandas(int initialized, TRAPS) {
903 ResourceMark rm(THREAD);
904 GrowableArray<Method*> mirandas(20);
905 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
906 ik()->default_methods(), ik()->local_interfaces(),
907 klass()->is_interface());
908 for (int i = 0; i < mirandas.length(); i++) {
909 if (log_develop_is_enabled(Trace, vtables)) {
1065 _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
1066 _size_method_table = (end - method_entry) / itableMethodEntry::size();
1067 assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
1068 return;
1069 }
1070 }
1071
1072 // The length of the itable was either zero, or it has not yet been initialized.
1073 _table_offset = 0;
1074 _size_offset_table = 0;
1075 _size_method_table = 0;
1076 }
1077
1078 static int initialize_count = 0;
1079
1080 // Initialization
1081 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1082 if (_klass->is_interface()) {
1083 // This needs to go after vtable indices are assigned but
1084 // before implementors need to know the number of itable indices.
1085 assign_itable_indices_for_interface(InstanceKlass::cast(_klass), THREAD);
1086 }
1087
1088 // Cannot be setup doing bootstrapping, interfaces don't have
1089 // itables, and klass with only ones entry have empty itables
1090 if (Universe::is_bootstrapping() ||
1091 _klass->is_interface() ||
1092 _klass->itable_length() == itableOffsetEntry::size()) return;
1093
1094 // There's alway an extra itable entry so we can null-terminate it.
1095 guarantee(size_offset_table() >= 1, "too small");
1096 int num_interfaces = size_offset_table() - 1;
1097 if (num_interfaces > 0) {
1098 ResourceMark rm(THREAD);
1099 log_develop_debug(itables)("%3d: Initializing itables for %s", ++initialize_count,
1100 _klass->name()->as_C_string());
1101
1102
1103 // Iterate through all interfaces
1104 int i;
1105 for(i = 0; i < num_interfaces; i++) {
1106 itableOffsetEntry* ioe = offset_entry(i);
1107 HandleMark hm(THREAD);
1108 Klass* interf = ioe->interface_klass();
1109 assert(interf != NULL && ioe->offset() != 0, "bad offset entry in itable");
1110 initialize_itable_for_interface(ioe->offset(), InstanceKlass::cast(interf), checkconstraints, CHECK);
1111 }
1112
1113 }
1114 // Check that the last entry is empty
1115 itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1116 guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1117 }
1118
1119
1120 inline bool interface_method_needs_itable_index(Method* m) {
1121 if (m->is_static()) return false; // e.g., Stream.empty
1122 if (m->is_initializer()) return false; // <init> or <clinit>
1123 if (m->is_private()) return false; // uses direct call
1124 // If an interface redeclares a method from java.lang.Object,
1125 // it should already have a vtable index, don't touch it.
1126 // e.g., CharSequence.toString (from initialize_vtable)
1127 // if (m->has_vtable_index()) return false; // NO!
1128 return true;
1129 }
1130
1131 int klassItable::assign_itable_indices_for_interface(InstanceKlass* klass, TRAPS) {
1132 // an interface does not have an itable, but its methods need to be numbered
1133 ResourceMark rm(THREAD);
1134 log_develop_debug(itables)("%3d: Initializing itable indices for interface %s",
1135 ++initialize_count, klass->name()->as_C_string());
1136 Array<Method*>* methods = klass->methods();
1137 int nof_methods = methods->length();
1138 int ime_num = 0;
1139 for (int i = 0; i < nof_methods; i++) {
1140 Method* m = methods->at(i);
1141 if (interface_method_needs_itable_index(m)) {
1142 assert(!m->is_final_method(), "no final interface methods");
1143 // If m is already assigned a vtable index, do not disturb it.
1144 if (log_develop_is_enabled(Trace, itables)) {
1145 LogTarget(Trace, itables) lt;
1146 LogStream ls(lt);
1147 assert(m != NULL, "methods can never be null");
1148 const char* sig = m->name_and_sig_as_C_string();
1149 if (m->has_vtable_index()) {
1150 ls.print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig);
1151 } else {
1152 ls.print("itable index %d for method: %s, flags: ", ime_num, sig);
1153 }
1154 m->print_linkage_flags(&ls);
1155 ls.cr();
1156 }
1157 if (!m->has_vtable_index()) {
1158 // A shared method could have an initialized itable_index that
1159 // is < 0.
1160 assert(m->vtable_index() == Method::pending_itable_index ||
1161 m->is_shared(),
1162 "set by initialize_vtable");
1163 m->set_itable_index(ime_num);
1164 // Progress to next itable entry
1165 ime_num++;
1166 }
1167 }
1168 }
1169 assert(ime_num == method_count_for_interface(klass), "proper sizing");
1170 return ime_num;
1171 }
1172
1173 int klassItable::method_count_for_interface(InstanceKlass* interf) {
1174 assert(interf->is_interface(), "must be");
1175 Array<Method*>* methods = interf->methods();
1176 int nof_methods = methods->length();
1177 int length = 0;
1178 while (nof_methods > 0) {
1179 Method* m = methods->at(nof_methods-1);
1180 if (m->has_itable_index()) {
1181 length = m->itable_index() + 1;
1182 break;
1183 }
1184 nof_methods -= 1;
1185 }
1186 #ifdef ASSERT
1187 int nof_methods_copy = nof_methods;
1188 while (nof_methods_copy > 0) {
1189 Method* mm = methods->at(--nof_methods_copy);
1190 assert(!mm->has_itable_index() || mm->itable_index() < length, "");
1191 }
1192 #endif //ASSERT
1193 // return the rightmost itable index, plus one; or 0 if no methods have
1194 // itable indices
1195 return length;
1196 }
1197
1198
1199 void klassItable::initialize_itable_for_interface(int method_table_offset, InstanceKlass* interf, bool checkconstraints, TRAPS) {
1200 assert(interf->is_interface(), "must be");
1201 Array<Method*>* methods = interf->methods();
1202 int nof_methods = methods->length();
1203 HandleMark hm;
1204 Handle interface_loader (THREAD, interf->class_loader());
1205
1206 int ime_count = method_count_for_interface(interf);
1207 for (int i = 0; i < nof_methods; i++) {
1208 Method* m = methods->at(i);
1209 methodHandle target;
1210 if (m->has_itable_index()) {
1211 // This search must match the runtime resolution, i.e. selection search for invokeinterface
1212 // to correctly enforce loader constraints for interface method inheritance.
1213 // Private methods are skipped as a private class method can never be the implementation
1214 // of an interface method.
1215 // Invokespecial does not perform selection based on the receiver, so it does not use
1216 // the cached itable.
1217 target = LinkResolver::lookup_instance_method_in_klasses(_klass, m->name(), m->signature(),
1218 Klass::skip_private, CHECK);
1219 }
1220 if (target == NULL || !target->is_public() || target->is_abstract() || target->is_overpass()) {
1221 assert(target == NULL || !target->is_overpass() || target->is_public(),
1222 "Non-public overpass method!");
1223 // Entry does not resolve. Leave it empty for AbstractMethodError or other error.
1224 if (!(target == NULL) && !target->is_public()) {
1332 for (int i = 0; i < _size_method_table; i++) {
1333 Method* m = ime->method();
1334 if (m != NULL) {
1335 tty->print(" (%5d) ", i);
1336 m->access_flags().print_on(tty);
1337 if (m->is_default_method()) {
1338 tty->print("default ");
1339 }
1340 tty->print(" -- ");
1341 m->print_name(tty);
1342 tty->cr();
1343 }
1344 ime++;
1345 }
1346 }
1347 #endif // INCLUDE_JVMTI
1348
1349 // Setup
1350 class InterfaceVisiterClosure : public StackObj {
1351 public:
1352 virtual void doit(InstanceKlass* intf, int method_count) = 0;
1353 };
1354
1355 // Visit all interfaces with at least one itable method
1356 void visit_all_interfaces(Array<InstanceKlass*>* transitive_intf, InterfaceVisiterClosure *blk) {
1357 // Handle array argument
1358 for(int i = 0; i < transitive_intf->length(); i++) {
1359 InstanceKlass* intf = transitive_intf->at(i);
1360 assert(intf->is_interface(), "sanity check");
1361
1362 // Find no. of itable methods
1363 int method_count = 0;
1364 // method_count = klassItable::method_count_for_interface(intf);
1365 Array<Method*>* methods = intf->methods();
1366 if (methods->length() > 0) {
1367 for (int i = methods->length(); --i >= 0; ) {
1368 if (interface_method_needs_itable_index(methods->at(i))) {
1369 method_count++;
1370 }
1371 }
1372 }
1373
1374 // Visit all interfaces which either have any methods or can participate in receiver type check.
1375 // We do not bother to count methods in transitive interfaces, although that would allow us to skip
1376 // this step in the rare case of a zero-method interface extending another zero-method interface.
1377 if (method_count > 0 || intf->transitive_interfaces()->length() > 0) {
1378 blk->doit(intf, method_count);
1379 }
1380 }
1381 }
1382
1383 class CountInterfacesClosure : public InterfaceVisiterClosure {
1384 private:
1385 int _nof_methods;
1386 int _nof_interfaces;
1387 public:
1388 CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; }
1389
1390 int nof_methods() const { return _nof_methods; }
1391 int nof_interfaces() const { return _nof_interfaces; }
1392
1393 void doit(InstanceKlass* intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; }
1394 };
1395
1396 class SetupItableClosure : public InterfaceVisiterClosure {
1397 private:
1398 itableOffsetEntry* _offset_entry;
1399 itableMethodEntry* _method_entry;
1400 address _klass_begin;
1401 public:
1402 SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) {
1403 _klass_begin = klass_begin;
1404 _offset_entry = offset_entry;
1405 _method_entry = method_entry;
1406 }
1407
1408 itableMethodEntry* method_entry() const { return _method_entry; }
1409
1410 void doit(InstanceKlass* intf, int method_count) {
1411 int offset = ((address)_method_entry) - _klass_begin;
1412 _offset_entry->initialize(intf, offset);
1413 _offset_entry++;
1414 _method_entry += method_count;
1415 }
1416 };
1417
1418 int klassItable::compute_itable_size(Array<InstanceKlass*>* transitive_interfaces) {
1419 // Count no of interfaces and total number of interface methods
1420 CountInterfacesClosure cic;
1421 visit_all_interfaces(transitive_interfaces, &cic);
1422
1423 // There's alway an extra itable entry so we can null-terminate it.
1424 int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods());
1425
1426 // Statistics
1427 update_stats(itable_size * wordSize);
1428
1429 return itable_size;
1430 }
1431
1432
1433 // Fill out offset table and interface klasses into the itable space
1434 void klassItable::setup_itable_offset_table(InstanceKlass* klass) {
1435 if (klass->itable_length() == 0) return;
1436 assert(!klass->is_interface(), "Should have zero length itable");
1437
1438 // Count no of interfaces and total number of interface methods
1451 // Fill-out offset table
1452 itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();
1453 itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);
1454 intptr_t* end = klass->end_of_itable();
1455 assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");
1456 assert((oop*)(end) == (oop*)(ime + nof_methods), "wrong offset calculation (2)");
1457
1458 // Visit all interfaces and initialize itable offset table
1459 SetupItableClosure sic((address)klass, ioe, ime);
1460 visit_all_interfaces(klass->transitive_interfaces(), &sic);
1461
1462 #ifdef ASSERT
1463 ime = sic.method_entry();
1464 oop* v = (oop*) klass->end_of_itable();
1465 assert( (oop*)(ime) == v, "wrong offset calculation (2)");
1466 #endif
1467 }
1468
1469
1470 // inverse to itable_index
1471 Method* klassItable::method_for_itable_index(InstanceKlass* intf, int itable_index) {
1472 assert(intf->is_interface(), "sanity check");
1473 assert(intf->verify_itable_index(itable_index), "");
1474 Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
1475
1476 if (itable_index < 0 || itable_index >= method_count_for_interface(intf))
1477 return NULL; // help caller defend against bad indices
1478
1479 int index = itable_index;
1480 Method* m = methods->at(index);
1481 int index2 = -1;
1482 while (!m->has_itable_index() ||
1483 (index2 = m->itable_index()) != itable_index) {
1484 assert(index2 < itable_index, "monotonic");
1485 if (++index == methods->length())
1486 return NULL;
1487 m = methods->at(index);
1488 }
1489 assert(m->itable_index() == itable_index, "correct inverse");
1490
1491 return m;
1492 }
|