hotspot/src/share/vm/oops/klassVtable.cpp
changeset 21556 e75cd34a59e0
parent 20391 7b146c5ebb18
child 21770 e8932d2fda2c
equal deleted inserted replaced
21555:2f66a8ee6265 21556:e75cd34a59e0
    84   GrowableArray<Method*> new_mirandas(20);
    84   GrowableArray<Method*> new_mirandas(20);
    85   // compute the number of mirandas methods that must be added to the end
    85   // compute the number of mirandas methods that must be added to the end
    86   get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces);
    86   get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces);
    87   *num_new_mirandas = new_mirandas.length();
    87   *num_new_mirandas = new_mirandas.length();
    88 
    88 
    89   vtable_length += *num_new_mirandas * vtableEntry::size();
    89   // Interfaces do not need interface methods in their vtables
       
    90   // This includes miranda methods and during later processing, default methods
       
    91   if (!class_flags.is_interface()) {
       
    92     vtable_length += *num_new_mirandas * vtableEntry::size();
       
    93   }
    90 
    94 
    91   if (Universe::is_bootstrapping() && vtable_length == 0) {
    95   if (Universe::is_bootstrapping() && vtable_length == 0) {
    92     // array classes don't have their superclass set correctly during
    96     // array classes don't have their superclass set correctly during
    93     // bootstrapping
    97     // bootstrapping
    94     vtable_length = Universe::base_vtable_size();
    98     vtable_length = Universe::base_vtable_size();
   222         }
   226         }
   223       }
   227       }
   224     }
   228     }
   225 
   229 
   226     // add miranda methods; it will also return the updated initialized
   230     // add miranda methods; it will also return the updated initialized
   227     initialized = fill_in_mirandas(initialized);
   231     // Interfaces do not need interface methods in their vtables
       
   232     // This includes miranda methods and during later processing, default methods
       
   233     if (!ik()->is_interface()) {
       
   234       initialized = fill_in_mirandas(initialized);
       
   235     }
   228 
   236 
   229     // In class hierarchies where the accessibility is not increasing (i.e., going from private ->
   237     // In class hierarchies where the accessibility is not increasing (i.e., going from private ->
   230     // package_private -> public/protected), the vtable might actually be smaller than our initial
   238     // package_private -> public/protected), the vtable might actually be smaller than our initial
   231     // calculation.
   239     // calculation.
   232     assert(initialized <= _length, "vtable initialization failed");
   240     assert(initialized <= _length, "vtable initialization failed");
   262           tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
   270           tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
   263            supersuperklass->internal_name(),
   271            supersuperklass->internal_name(),
   264            _klass->internal_name(), sig, vtable_index);
   272            _klass->internal_name(), sig, vtable_index);
   265            super_method->access_flags().print_on(tty);
   273            super_method->access_flags().print_on(tty);
   266            if (super_method->is_default_method()) {
   274            if (super_method->is_default_method()) {
   267              tty->print("default");
   275              tty->print("default ");
   268            }
   276            }
   269            tty->print("overriders flags: ");
   277            tty->print("overriders flags: ");
   270            target_method->access_flags().print_on(tty);
   278            target_method->access_flags().print_on(tty);
   271            if (target_method->is_default_method()) {
   279            if (target_method->is_default_method()) {
   272              tty->print("default");
   280              tty->print("default ");
   273            }
   281            }
   274         }
   282         }
   275 #endif /*PRODUCT*/
   283 #endif /*PRODUCT*/
   276         break; // return found superk
   284         break; // return found superk
   277       }
   285       }
   330   } else if (klass->is_interface()) {
   338   } else if (klass->is_interface()) {
   331     allocate_new = false;  // see note below in needs_new_vtable_entry
   339     allocate_new = false;  // see note below in needs_new_vtable_entry
   332     // An interface never allocates new vtable slots, only inherits old ones.
   340     // An interface never allocates new vtable slots, only inherits old ones.
   333     // This method will either be assigned its own itable index later,
   341     // This method will either be assigned its own itable index later,
   334     // or be assigned an inherited vtable index in the loop below.
   342     // or be assigned an inherited vtable index in the loop below.
   335     // default methods store their vtable indices in the inheritors default_vtable_indices
   343     // default methods inherited by classes store their vtable indices
   336     assert (default_index == -1, "interfaces don't store resolved default methods");
   344     // in the inheritor's default_vtable_indices
   337     target_method()->set_vtable_index(Method::pending_itable_index);
   345     // default methods inherited by interfaces may already have a
       
   346     // valid itable index, if so, don't change it
       
   347     // overpass methods in an interface will be assigned an itable index later
       
   348     // by an inheriting class
       
   349     if (!is_default || !target_method()->has_itable_index()) {
       
   350       target_method()->set_vtable_index(Method::pending_itable_index);
       
   351     }
   338   }
   352   }
   339 
   353 
   340   // we need a new entry if there is no superclass
   354   // we need a new entry if there is no superclass
   341   if (klass->super() == NULL) {
   355   if (klass->super() == NULL) {
   342     return allocate_new;
   356     return allocate_new;
   439           char* sig = target_method()->name_and_sig_as_C_string();
   453           char* sig = target_method()->name_and_sig_as_C_string();
   440           tty->print("overriding with %s::%s index %d, original flags: ",
   454           tty->print("overriding with %s::%s index %d, original flags: ",
   441            target_klass->internal_name(), sig, i);
   455            target_klass->internal_name(), sig, i);
   442            super_method->access_flags().print_on(tty);
   456            super_method->access_flags().print_on(tty);
   443            if (super_method->is_default_method()) {
   457            if (super_method->is_default_method()) {
   444              tty->print("default");
   458              tty->print("default ");
   445            }
   459            }
   446            if (super_method->is_overpass()) {
   460            if (super_method->is_overpass()) {
   447              tty->print("overpass");
   461              tty->print("overpass");
   448            }
   462            }
   449            tty->print("overriders flags: ");
   463            tty->print("overriders flags: ");
   450            target_method->access_flags().print_on(tty);
   464            target_method->access_flags().print_on(tty);
   451            if (target_method->is_default_method()) {
   465            if (target_method->is_default_method()) {
   452              tty->print("default");
   466              tty->print("default ");
   453            }
   467            }
   454            if (target_method->is_overpass()) {
   468            if (target_method->is_overpass()) {
   455              tty->print("overpass");
   469              tty->print("overpass");
   456            }
   470            }
   457            tty->cr();
   471            tty->cr();
   466           char* sig = target_method()->name_and_sig_as_C_string();
   480           char* sig = target_method()->name_and_sig_as_C_string();
   467           tty->print("NOT overriding with %s::%s index %d, original flags: ",
   481           tty->print("NOT overriding with %s::%s index %d, original flags: ",
   468            target_klass->internal_name(), sig,i);
   482            target_klass->internal_name(), sig,i);
   469            super_method->access_flags().print_on(tty);
   483            super_method->access_flags().print_on(tty);
   470            if (super_method->is_default_method()) {
   484            if (super_method->is_default_method()) {
   471              tty->print("default");
   485              tty->print("default ");
   472            }
   486            }
   473            if (super_method->is_overpass()) {
   487            if (super_method->is_overpass()) {
   474              tty->print("overpass");
   488              tty->print("overpass");
   475            }
   489            }
   476            tty->print("overriders flags: ");
   490            tty->print("overriders flags: ");
   477            target_method->access_flags().print_on(tty);
   491            target_method->access_flags().print_on(tty);
   478            if (target_method->is_default_method()) {
   492            if (target_method->is_default_method()) {
   479              tty->print("default");
   493              tty->print("default ");
   480            }
   494            }
   481            if (target_method->is_overpass()) {
   495            if (target_method->is_overpass()) {
   482              tty->print("overpass");
   496              tty->print("overpass");
   483            }
   497            }
   484            tty->cr();
   498            tty->cr();
   492 
   506 
   493 void klassVtable::put_method_at(Method* m, int index) {
   507 void klassVtable::put_method_at(Method* m, int index) {
   494 #ifndef PRODUCT
   508 #ifndef PRODUCT
   495   if (PrintVtables && Verbose) {
   509   if (PrintVtables && Verbose) {
   496     ResourceMark rm;
   510     ResourceMark rm;
   497     tty->print_cr("adding %s::%s at index %d", _klass->internal_name(),
   511     const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
   498       (m != NULL) ? m->name()->as_C_string() : "<NULL>", index);
   512     tty->print("adding %s at index %d, flags: ", sig, index);
       
   513     if (m != NULL) {
       
   514       m->access_flags().print_on(tty);
       
   515       if (m->is_default_method()) {
       
   516         tty->print("default ");
       
   517       }
       
   518       if (m->is_overpass()) {
       
   519         tty->print("overpass");
       
   520       }
       
   521     }
       
   522     tty->cr();
   499   }
   523   }
   500 #endif
   524 #endif
   501   table()[index].set(m);
   525   table()[index].set(m);
   502 }
   526 }
   503 
   527 
   629 
   653 
   630   // miranda methods are public abstract instance interface methods in a class's vtable
   654   // miranda methods are public abstract instance interface methods in a class's vtable
   631   if (mhk->is_interface()) {
   655   if (mhk->is_interface()) {
   632     assert(m->is_public(), "should be public");
   656     assert(m->is_public(), "should be public");
   633     assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
   657     assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
   634     assert(is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super()), "should be a miranda_method");
   658     // the search could find a miranda or a default method
   635     return true;
   659     if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) {
       
   660       return true;
       
   661     }
   636   }
   662   }
   637   return false;
   663   return false;
   638 }
   664 }
   639 
   665 
   640 // check if a method is a miranda method, given a class's methods table,
   666 // check if a method is a miranda method, given a class's methods table,
   642 // "miranda" means not static, not defined by this class.
   668 // "miranda" means not static, not defined by this class.
   643 // private methods in interfaces do not belong in the miranda list.
   669 // private methods in interfaces do not belong in the miranda list.
   644 // the caller must make sure that the method belongs to an interface implemented by the class
   670 // the caller must make sure that the method belongs to an interface implemented by the class
   645 // Miranda methods only include public interface instance methods
   671 // Miranda methods only include public interface instance methods
   646 // Not private methods, not static methods, not default == concrete abstract
   672 // Not private methods, not static methods, not default == concrete abstract
       
   673 // Miranda methods also do not include overpass methods in interfaces
   647 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,
   674 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,
   648                              Array<Method*>* default_methods, Klass* super) {
   675                              Array<Method*>* default_methods, Klass* super) {
   649   if (m->is_static() || m->is_private()) {
   676   if (m->is_static() || m->is_private() || m->is_overpass()) {
   650     return false;
   677     return false;
   651   }
   678   }
   652   Symbol* name = m->name();
   679   Symbol* name = m->name();
   653   Symbol* signature = m->signature();
   680   Symbol* signature = m->signature();
   654   if (InstanceKlass::find_method(class_methods, name, signature) == NULL) {
   681   if (InstanceKlass::find_method(class_methods, name, signature) == NULL) {
   742 }
   769 }
   743 
   770 
   744 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
   771 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
   745 // and append them into the vtable starting at index initialized,
   772 // and append them into the vtable starting at index initialized,
   746 // return the new value of initialized.
   773 // return the new value of initialized.
       
   774 // Miranda methods use vtable entries, but do not get assigned a vtable_index
       
   775 // The vtable_index is discovered by searching from the end of the vtable
   747 int klassVtable::fill_in_mirandas(int initialized) {
   776 int klassVtable::fill_in_mirandas(int initialized) {
   748   GrowableArray<Method*> mirandas(20);
   777   GrowableArray<Method*> mirandas(20);
   749   get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
   778   get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
   750                ik()->default_methods(), ik()->local_interfaces());
   779                ik()->default_methods(), ik()->local_interfaces());
   751   for (int i = 0; i < mirandas.length(); i++) {
   780   for (int i = 0; i < mirandas.length(); i++) {
   756         char* sig = meth->name_and_sig_as_C_string();
   785         char* sig = meth->name_and_sig_as_C_string();
   757         tty->print("fill in mirandas with %s index %d, flags: ",
   786         tty->print("fill in mirandas with %s index %d, flags: ",
   758           sig, initialized);
   787           sig, initialized);
   759         meth->access_flags().print_on(tty);
   788         meth->access_flags().print_on(tty);
   760         if (meth->is_default_method()) {
   789         if (meth->is_default_method()) {
   761           tty->print("default");
   790           tty->print("default ");
   762         }
   791         }
   763         tty->cr();
   792         tty->cr();
   764       }
   793       }
   765     }
   794     }
   766     put_method_at(mirandas.at(i), initialized);
   795     put_method_at(mirandas.at(i), initialized);
   856     Method* m = unchecked_method_at(i);
   885     Method* m = unchecked_method_at(i);
   857     if (m != NULL) {
   886     if (m != NULL) {
   858       tty->print("      (%5d)  ", i);
   887       tty->print("      (%5d)  ", i);
   859       m->access_flags().print_on(tty);
   888       m->access_flags().print_on(tty);
   860       if (m->is_default_method()) {
   889       if (m->is_default_method()) {
   861         tty->print("default");
   890         tty->print("default ");
   862       }
   891       }
   863       if (m->is_overpass()) {
   892       if (m->is_overpass()) {
   864         tty->print("overpass");
   893         tty->print("overpass");
   865       }
   894       }
   866       tty->print(" --  ");
   895       tty->print(" --  ");
   975   for (int i = 0; i < nof_methods; i++) {
  1004   for (int i = 0; i < nof_methods; i++) {
   976     Method* m = methods->at(i);
  1005     Method* m = methods->at(i);
   977     if (interface_method_needs_itable_index(m)) {
  1006     if (interface_method_needs_itable_index(m)) {
   978       assert(!m->is_final_method(), "no final interface methods");
  1007       assert(!m->is_final_method(), "no final interface methods");
   979       // If m is already assigned a vtable index, do not disturb it.
  1008       // If m is already assigned a vtable index, do not disturb it.
       
  1009       if (TraceItables && Verbose) {
       
  1010         ResourceMark rm;
       
  1011         const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
       
  1012         if (m->has_vtable_index()) {
       
  1013           tty->print("itable index %d for method: %s, flags: ", m->vtable_index(), sig);
       
  1014         } else {
       
  1015           tty->print("itable index %d for method: %s, flags: ", ime_num, sig);
       
  1016         }
       
  1017         if (m != NULL) {
       
  1018           m->access_flags().print_on(tty);
       
  1019           if (m->is_default_method()) {
       
  1020             tty->print("default ");
       
  1021           }
       
  1022           if (m->is_overpass()) {
       
  1023             tty->print("overpass");
       
  1024           }
       
  1025         }
       
  1026         tty->cr();
       
  1027       }
   980       if (!m->has_vtable_index()) {
  1028       if (!m->has_vtable_index()) {
   981         assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable");
  1029         assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable");
   982         m->set_itable_index(ime_num);
  1030         m->set_itable_index(ime_num);
   983         // Progress to next itable entry
  1031         // Progress to next itable entry
   984         ime_num++;
  1032         ime_num++;
  1077                     interf_h()->internal_name(), ime_num, sig,
  1125                     interf_h()->internal_name(), ime_num, sig,
  1078                     target()->method_holder()->internal_name());
  1126                     target()->method_holder()->internal_name());
  1079           tty->print("target_method flags: ");
  1127           tty->print("target_method flags: ");
  1080           target()->access_flags().print_on(tty);
  1128           target()->access_flags().print_on(tty);
  1081           if (target()->is_default_method()) {
  1129           if (target()->is_default_method()) {
  1082             tty->print("default");
  1130             tty->print("default ");
  1083           }
  1131           }
  1084           tty->cr();
  1132           tty->cr();
  1085         }
  1133         }
  1086       }
  1134       }
  1087     }
  1135     }
  1156     Method* m = ime->method();
  1204     Method* m = ime->method();
  1157     if (m != NULL) {
  1205     if (m != NULL) {
  1158       tty->print("      (%5d)  ", i);
  1206       tty->print("      (%5d)  ", i);
  1159       m->access_flags().print_on(tty);
  1207       m->access_flags().print_on(tty);
  1160       if (m->is_default_method()) {
  1208       if (m->is_default_method()) {
  1161         tty->print("default");
  1209         tty->print("default ");
  1162       }
  1210       }
  1163       tty->print(" --  ");
  1211       tty->print(" --  ");
  1164       m->print_name(tty);
  1212       m->print_name(tty);
  1165       tty->cr();
  1213       tty->cr();
  1166     }
  1214     }