klassVtable.cpp 49.7 KB
Newer Older
D
duke 已提交
1
/*
X
xdono 已提交
2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

#include "incls/_precompiled.incl"
#include "incls/_klassVtable.cpp.incl"

inline instanceKlass* klassVtable::ik() const {
  Klass* k = _klass()->klass_part();
  assert(k->oop_is_instance(), "not an instanceKlass");
  return (instanceKlass*)k;
}


// this function computes the vtable size (including the size needed for miranda
// methods) and the number of miranda methods in this class
// Note on Miranda methods: Let's say there is a class C that implements
// interface I.  Let's say there is a method m in I that neither C nor any
// of its super classes implement (i.e there is no method of any access, with
// the same name and signature as m), then m is a Miranda method which is
// entered as a public abstract method in C's vtable.  From then on it should
// treated as any other public method in C for method over-ride purposes.
void klassVtable::compute_vtable_size_and_num_mirandas(int &vtable_length,
                                                       int &num_miranda_methods,
                                                       klassOop super,
                                                       objArrayOop methods,
                                                       AccessFlags class_flags,
                                                       oop classloader,
                                                       symbolOop classname,
                                                       objArrayOop local_interfaces
                                                       ) {

  No_Safepoint_Verifier nsv;

  // set up default result values
  vtable_length = 0;
  num_miranda_methods = 0;

  // start off with super's vtable length
  instanceKlass* sk = (instanceKlass*)super->klass_part();
  vtable_length = super == NULL ? 0 : sk->vtable_length();

  // go thru each method in the methods table to see if it needs a new entry
  int len = methods->length();
  for (int i = 0; i < len; i++) {
    assert(methods->obj_at(i)->is_method(), "must be a methodOop");
    methodOop m = methodOop(methods->obj_at(i));

    if (needs_new_vtable_entry(m, super, classloader, classname, class_flags)) {
      vtable_length += vtableEntry::size(); // we need a new entry
    }
  }

  // compute the number of mirandas methods that must be added to the end
  num_miranda_methods = get_num_mirandas(super, methods, local_interfaces);
  vtable_length += (num_miranda_methods * vtableEntry::size());

  if (Universe::is_bootstrapping() && vtable_length == 0) {
    // array classes don't have their superclass set correctly during
    // bootstrapping
    vtable_length = Universe::base_vtable_size();
  }

  if (super == NULL && !Universe::is_bootstrapping() &&
      vtable_length != Universe::base_vtable_size()) {
    // Someone is attempting to redefine java.lang.Object incorrectly.  The
    // only way this should happen is from
    // SystemDictionary::resolve_from_stream(), which will detect this later
    // and throw a security exception.  So don't assert here to let
    // the exception occur.
    vtable_length = Universe::base_vtable_size();
  }
  assert(super != NULL || vtable_length == Universe::base_vtable_size(),
         "bad vtable size for class Object");
  assert(vtable_length % vtableEntry::size() == 0, "bad vtable length");
  assert(vtable_length >= Universe::base_vtable_size(), "vtable too small");
}

int klassVtable::index_of(methodOop m, int len) const {
  assert(m->vtable_index() >= 0, "do not ask this of non-vtable methods");
  return m->vtable_index();
}

int klassVtable::initialize_from_super(KlassHandle super) {
  if (super.is_null()) {
    return 0;
  } else {
    // copy methods from superKlass
    // can't inherit from array class, so must be instanceKlass
    assert(super->oop_is_instance(), "must be instance klass");
    instanceKlass* sk = (instanceKlass*)super()->klass_part();
    klassVtable* superVtable = sk->vtable();
    assert(superVtable->length() <= _length, "vtable too short");
#ifdef ASSERT
    superVtable->verify(tty, true);
#endif
    superVtable->copy_vtable_to(table());
#ifndef PRODUCT
    if (PrintVtables && Verbose) {
      tty->print_cr("copy vtable from %s to %s size %d", sk->internal_name(), klass()->internal_name(), _length);
    }
#endif
    return superVtable->length();
  }
}

// Revised lookup semantics   introduced 1.3 (Kestral beta)
void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {

  // Note:  Arrays can have intermediate array supers.  Use java_super to skip them.
  KlassHandle super (THREAD, klass()->java_super());
  int nofNewEntries = 0;


  if (PrintVtables && !klass()->oop_is_array()) {
    ResourceMark rm(THREAD);
    tty->print_cr("Initializing: %s", _klass->name()->as_C_string());
  }

#ifdef ASSERT
  oop* end_of_obj = (oop*)_klass() + _klass()->size();
  oop* end_of_vtable = (oop*)&table()[_length];
  assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
#endif

  if (Universe::is_bootstrapping()) {
    // just clear everything
    for (int i = 0; i < _length; i++) table()[i].clear();
    return;
  }

  int super_vtable_len = initialize_from_super(super);
  if (klass()->oop_is_array()) {
    assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
  } else {
    assert(_klass->oop_is_instance(), "must be instanceKlass");

    objArrayHandle methods(THREAD, ik()->methods());
    int len = methods()->length();
    int initialized = super_vtable_len;

    // update_super_vtable can stop for gc - ensure using handles
    for (int i = 0; i < len; i++) {
      HandleMark hm(THREAD);
      assert(methods()->obj_at(i)->is_method(), "must be a methodOop");
      methodHandle mh(THREAD, (methodOop)methods()->obj_at(i));

      bool needs_new_entry = update_super_vtable(ik(), mh, super_vtable_len, checkconstraints, CHECK);

      if (needs_new_entry) {
        put_method_at(mh(), initialized);
        mh()->set_vtable_index(initialized); // set primary vtable index
        initialized++;
      }
    }

    // add miranda methods; it will also update the value of initialized
    fill_in_mirandas(initialized);

    // In class hierachieswhere the accesibility is not increasing (i.e., going from private ->
    // package_private -> publicprotected), the vtable might actually be smaller than our initial
    // calculation.
    assert(initialized <= _length, "vtable initialization failed");
    for(;initialized < _length; initialized++) {
      put_method_at(NULL, initialized);
    }
    NOT_PRODUCT(verify(tty, true));
  }
}

// Interates through the vtables to find the broadest access level. This
// will always be monotomic for valid Java programs - but not neccesarily
// for incompatible class files.
klassVtable::AccessType klassVtable::vtable_accessibility_at(int i) {
  // This vtable is not implementing the specific method
  if (i >= length()) return acc_private;

  // Compute AccessType for current method. public or protected we are done.
  methodOop m = method_at(i);
  if (m->is_protected() || m->is_public()) return acc_publicprotected;

  AccessType acc = m->is_package_private() ? acc_package_private : acc_private;

  // Compute AccessType for method in super classes
  klassOop super = klass()->super();
  AccessType super_acc = (super != NULL) ? instanceKlass::cast(klass()->super())->vtable()->vtable_accessibility_at(i)
                                         : acc_private;

  // Merge
  return (AccessType)MAX2((int)acc, (int)super_acc);
}


// Update child's copy of super vtable for overrides
// OR return true if a new vtable entry is required
// Only called for instanceKlass's, i.e. not for arrays
// If that changed, could not use _klass as handle for klass
bool klassVtable::update_super_vtable(instanceKlass* klass, methodHandle target_method, int super_vtable_len, bool checkconstraints, TRAPS) {
  ResourceMark rm;
  bool allocate_new = true;
  assert(klass->oop_is_instance(), "must be instanceKlass");

  // Initialize the method's vtable index to "nonvirtual".
  // If we allocate a vtable entry, we will update it to a non-negative number.
  target_method()->set_vtable_index(methodOopDesc::nonvirtual_vtable_index);

  // Static and <init> methods are never in
  if (target_method()->is_static() || target_method()->name() ==  vmSymbols::object_initializer_name()) {
    return false;
  }

  if (klass->is_final() || target_method()->is_final()) {
    // a final method never needs a new entry; final methods can be statically
    // resolved and they have to be present in the vtable only if they override
    // a super's method, in which case they re-use its entry
    allocate_new = false;
  }

  // we need a new entry if there is no superclass
  if (klass->super() == NULL) {
    return allocate_new;
  }

  // private methods always have a new entry in the vtable
  if (target_method()->is_private()) {
    return allocate_new;
  }

  // search through the vtable and update overridden entries
  // Since check_signature_loaders acquires SystemDictionary_lock
  // which can block for gc, once we are in this loop, use handles, not
  // unhandled oops unless they are reinitialized for each loop
  // handles for name, signature, klass, target_method
  // not for match_method, holder

  symbolHandle name(THREAD,target_method()->name());
  symbolHandle signature(THREAD,target_method()->signature());
  for(int i = 0; i < super_vtable_len; i++) {
    methodOop match_method = method_at(i);
    // Check if method name matches
    if (match_method->name() == name() && match_method->signature() == signature()) {

      instanceKlass* holder = (THREAD, instanceKlass::cast(match_method->method_holder()));

      // Check if the match_method is accessable from current class

      bool same_package_init = false;
      bool same_package_flag = false;
      bool simple_match = match_method->is_public()  || match_method->is_protected();
      if (!simple_match) {
        same_package_init = true;
        same_package_flag = holder->is_same_class_package(_klass->class_loader(), _klass->name());

        simple_match = match_method->is_package_private() && same_package_flag;
      }
      // match_method is the superclass' method. Note we can't override
      // and shouldn't access superclass' ACC_PRIVATE methods
      // (although they have been copied into our vtable)
      // A simple form of this statement is:
      // if ( (match_method->is_public()  || match_method->is_protected()) ||
      //    (match_method->is_package_private() && holder->is_same_class_package(klass->class_loader(), klass->name()))) {
      //
      // The complexity is introduced it avoid recomputing 'is_same_class_package' which is expensive.
      if (simple_match) {
        // Check if target_method and match_method has same level of accessibility. The accesibility of the
        // match method is the "most-general" visibility of all entries at it's particular vtable index for
        // all superclasses. This check must be done before we override the current entry in the vtable.
        AccessType at = vtable_accessibility_at(i);
        bool same_access = false;

        if (  (at == acc_publicprotected && (target_method()->is_public() || target_method()->is_protected())
           || (at == acc_package_private && (target_method()->is_package_private() &&
                                            (( same_package_init && same_package_flag) ||
                                             (!same_package_init && holder->is_same_class_package(_klass->class_loader(), _klass->name()))))))) {
           same_access = true;
        }

        if (checkconstraints) {
        // Override vtable entry if passes loader constraint check
        // if loader constraint checking requested
        // No need to visit his super, since he and his super
        // have already made any needed loader constraints.
        // Since loader constraints are transitive, it is enough
        // to link to the first super, and we get all the others.
          symbolHandle signature(THREAD, target_method()->signature());
          Handle this_loader(THREAD, _klass->class_loader());
          instanceKlassHandle super_klass(THREAD, _klass->super());
          Handle super_loader(THREAD, super_klass->class_loader());

          if (this_loader() != super_loader()) {
            ResourceMark rm(THREAD);
            char* failed_type_name =
              SystemDictionary::check_signature_loaders(signature, this_loader,
                                                        super_loader, true,
                                                        CHECK_(false));
            if (failed_type_name != NULL) {
              const char* msg = "loader constraint violation: when resolving "
                "overridden method \"%s\" the class loader (instance"
                " of %s) of the current class, %s, and its superclass loader "
                "(instance of %s), have different Class objects for the type "
                "%s used in the signature";
              char* sig = target_method()->name_and_sig_as_C_string();
              const char* loader1 = SystemDictionary::loader_name(this_loader());
              char* current = _klass->name()->as_C_string();
              const char* loader2 = SystemDictionary::loader_name(super_loader());
              size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
                strlen(current) + strlen(loader2) + strlen(failed_type_name);
              char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
              jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
                           failed_type_name);
              THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
            }
          }
        }
        put_method_at(target_method(), i);


        if (same_access) {
          // target and match has same accessiblity - share entry
          allocate_new = false;
          target_method()->set_vtable_index(i);
#ifndef PRODUCT
          if (PrintVtables && Verbose) {
            AccessType targetacc;
            if (target_method()->is_protected() ||
                 target_method()->is_public()) {
               targetacc =  acc_publicprotected;
            } else {
              targetacc = target_method()->is_package_private() ? acc_package_private : acc_private;
            }
            tty->print_cr("overriding with %s::%s index %d, original flags: %x overriders flags: %x",
             _klass->internal_name(), (target_method() != NULL) ?
             target_method()->name()->as_C_string() : "<NULL>", i,
             at, targetacc);
          }
#endif /*PRODUCT*/
        } else {
#ifndef PRODUCT
          if (PrintVtables && Verbose) {
            AccessType targetacc;
            if (target_method()->is_protected() ||
                 target_method()->is_public()) {
               targetacc =  acc_publicprotected;
            } else {
              targetacc = target_method()->is_package_private() ? acc_package_private : acc_private;
            }
            tty->print_cr("override %s %s::%s at index %d, original flags: %x overriders flags: %x",
            allocate_new ? "+ new" : "only",
            _klass->internal_name(), (target_method() != NULL) ?
            target_method()->name()->as_C_string() : "<NULL>", i,
            at, targetacc);
           }
#endif /*PRODUCT*/
        }
      }
    }
  }
  return allocate_new;
}



void klassVtable::put_method_at(methodOop m, int index) {
  assert(m->is_oop_or_null(), "Not an oop or null");
#ifndef PRODUCT
  if (PrintVtables && Verbose) {
    tty->print_cr("adding %s::%s at index %d", _klass->internal_name(),
      (m != NULL) ? m->name()->as_C_string() : "<NULL>", index);
  }
  assert(unchecked_method_at(index)->is_oop_or_null(), "Not an oop or null");
#endif
  table()[index].set(m);
}

// Find out if a method "m" with superclass "super", loader "classloader" and
// name "classname" needs a new vtable entry.  Let P be a class package defined
// by "classloader" and "classname".
// NOTE: The logic used here is very similar to the one used for computing
// the vtables indices for a method. We cannot directly use that function because,
// when the Universe is boostrapping, a super's vtable might not be initialized.
bool klassVtable::needs_new_vtable_entry(methodOop target_method,
                                         klassOop super,
                                         oop classloader,
                                         symbolOop classname,
                                         AccessFlags class_flags) {
  if ((class_flags.is_final() || target_method->is_final()) ||
      // a final method never needs a new entry; final methods can be statically
      // resolved and they have to be present in the vtable only if they override
      // a super's method, in which case they re-use its entry
      (target_method->is_static()) ||
      // static methods don't need to be in vtable
      (target_method->name() ==  vmSymbols::object_initializer_name())
      // <init> is never called dynamically-bound
      ) {
    return false;
  }

  // we need a new entry if there is no superclass
  if (super == NULL) {
    return true;
  }

  // private methods always have a new entry in the vtable
  if (target_method->is_private()) {
    return true;
  }

  // search through the super class hierarchy to see if we need
  // a new entry
  symbolOop name = target_method->name();
  symbolOop signature = target_method->signature();
  klassOop k = super;
  methodOop match_method = NULL;
  instanceKlass *holder = NULL;
  while (k != NULL) {
    // lookup through the hierarchy for a method with matching name and sign.
    match_method = instanceKlass::cast(k)->lookup_method(name, signature);
    if (match_method == NULL) {
      break; // we still have to search for a matching miranda method
    }
    // get the class holding the matching method
    holder = instanceKlass::cast(match_method->method_holder());

    if (!match_method->is_static()) { // we want only instance method matches
      if ((target_method->is_public() || target_method->is_protected()) &&
          (match_method->is_public()  || match_method->is_protected())) {
        // target and match are public/protected; we do not need a new entry
        return false;
      }

      if (target_method->is_package_private() &&
          match_method->is_package_private() &&
          holder->is_same_class_package(classloader, classname)) {
        // target and match are P private; we do not need a new entry
        return false;
      }
    }

    k = holder->super(); // haven't found a match yet; continue to look
  }

  // if the target method is public or protected it may have a matching
  // miranda method in the super, whose entry it should re-use.
  if (target_method->is_public() || target_method->is_protected()) {
    instanceKlass *sk = instanceKlass::cast(super);
    if (sk->has_miranda_methods()) {
      if (sk->lookup_method_in_all_interfaces(name, signature) != NULL) {
        return false;  // found a matching miranda; we do not need a new entry
      }
    }
  }

  return true; // found no match; we need a new entry
}

// Support for miranda methods

// get the vtable index of a miranda method with matching "name" and "signature"
int klassVtable::index_of_miranda(symbolOop name, symbolOop signature) {
  // search from the bottom, might be faster
  for (int i = (length() - 1); i >= 0; i--) {
    methodOop m = table()[i].method();
    if (is_miranda_entry_at(i) &&
        m->name() == name && m->signature() == signature) {
      return i;
    }
  }
  return methodOopDesc::invalid_vtable_index;
}

// check if an entry is miranda
bool klassVtable::is_miranda_entry_at(int i) {
  methodOop m = method_at(i);
  klassOop method_holder = m->method_holder();
  instanceKlass *mhk = instanceKlass::cast(method_holder);

  // miranda methods are interface methods in a class's vtable
  if (mhk->is_interface()) {
    assert(m->is_public() && m->is_abstract(), "should be public and abstract");
    assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
    assert(is_miranda(m, ik()->methods(), ik()->super()), "should be a miranda_method");
    return true;
  }
  return false;
}

// check if a method is a miranda method, given a class's methods table and it's super
// the caller must make sure that the method belongs to an interface implemented by the class
bool klassVtable::is_miranda(methodOop m, objArrayOop class_methods, klassOop super) {
  symbolOop name = m->name();
  symbolOop signature = m->signature();
  if (instanceKlass::find_method(class_methods, name, signature) == NULL) {
     // did not find it in the method table of the current class
    if (super == NULL) {
      // super doesn't exist
      return true;
    } else {
      if (instanceKlass::cast(super)->lookup_method(name, signature) == NULL) {
        // super class hierarchy does not implement it
        return true;
      }
    }
  }
  return false;
}

void klassVtable::add_new_mirandas_to_list(GrowableArray<methodOop>* list_of_current_mirandas,
                                           objArrayOop current_interface_methods,
                                           objArrayOop class_methods,
                                           klassOop super) {
  // iterate thru the current interface's method to see if it a miranda
  int num_methods = current_interface_methods->length();
  for (int i = 0; i < num_methods; i++) {
    methodOop im = methodOop(current_interface_methods->obj_at(i));
    bool is_duplicate = false;
    int num_of_current_mirandas = list_of_current_mirandas->length();
    // check for duplicate mirandas in different interfaces we implement
    for (int j = 0; j < num_of_current_mirandas; j++) {
      methodOop miranda = list_of_current_mirandas->at(j);
      if ((im->name() == miranda->name()) &&
          (im->signature() == miranda->signature())) {
        is_duplicate = true;
        break;
      }
    }

    if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
      if (is_miranda(im, class_methods, super)) { // is it a miranda at all?
        instanceKlass *sk = instanceKlass::cast(super);
        // check if it is a duplicate of a super's miranda
        if (sk->lookup_method_in_all_interfaces(im->name(), im->signature()) == NULL) {
          list_of_current_mirandas->append(im);
        }
      }
    }
  }
}

void klassVtable::get_mirandas(GrowableArray<methodOop>* mirandas,
                               klassOop super, objArrayOop class_methods,
                               objArrayOop local_interfaces) {
  assert((mirandas->length() == 0) , "current mirandas must be 0");

  // iterate thru the local interfaces looking for a miranda
  int num_local_ifs = local_interfaces->length();
  for (int i = 0; i < num_local_ifs; i++) {
    instanceKlass *ik = instanceKlass::cast(klassOop(local_interfaces->obj_at(i)));
    add_new_mirandas_to_list(mirandas, ik->methods(), class_methods, super);
    // iterate thru each local's super interfaces
    objArrayOop super_ifs = ik->transitive_interfaces();
    int num_super_ifs = super_ifs->length();
    for (int j = 0; j < num_super_ifs; j++) {
      instanceKlass *sik = instanceKlass::cast(klassOop(super_ifs->obj_at(j)));
      add_new_mirandas_to_list(mirandas, sik->methods(), class_methods, super);
    }
  }
}

// get number of mirandas
int klassVtable::get_num_mirandas(klassOop super, objArrayOop class_methods, objArrayOop local_interfaces) {
  ResourceMark rm;
  GrowableArray<methodOop>* mirandas = new GrowableArray<methodOop>(20);
  get_mirandas(mirandas, super, class_methods, local_interfaces);
  return mirandas->length();
}

// fill in mirandas
void klassVtable::fill_in_mirandas(int& initialized) {
  ResourceMark rm;
  GrowableArray<methodOop>* mirandas = new GrowableArray<methodOop>(20);
  instanceKlass *this_ik = ik();
  get_mirandas(mirandas, this_ik->super(), this_ik->methods(), this_ik->local_interfaces());
  int num_mirandas = mirandas->length();
  for (int i = 0; i < num_mirandas; i++) {
    put_method_at(mirandas->at(i), initialized);
    initialized++;
  }
}

void klassVtable::copy_vtable_to(vtableEntry* start) {
  Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
}

void klassVtable::adjust_method_entries(methodOop* old_methods, methodOop* new_methods,
                                        int methods_length, bool * trace_name_printed) {
  // search the vtable for uses of either obsolete or EMCP methods
  for (int j = 0; j < methods_length; j++) {
    methodOop old_method = old_methods[j];
    methodOop new_method = new_methods[j];

    // In the vast majority of cases we could get the vtable index
    // by using:  old_method->vtable_index()
    // However, there are rare cases, eg. sun.awt.X11.XDecoratedPeer.getX()
    // in sun.awt.X11.XFramePeer where methods occur more than once in the
    // vtable, so, alas, we must do an exhaustive search.
    for (int index = 0; index < length(); index++) {
      if (unchecked_method_at(index) == old_method) {
        put_method_at(new_method, index);

        if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
          if (!(*trace_name_printed)) {
            // RC_TRACE_MESG macro has an embedded ResourceMark
            RC_TRACE_MESG(("adjust: name=%s",
                           Klass::cast(old_method->method_holder())->external_name()));
            *trace_name_printed = true;
          }
          // RC_TRACE macro has an embedded ResourceMark
          RC_TRACE(0x00100000, ("vtable method update: %s(%s)",
                                new_method->name()->as_C_string(),
                                new_method->signature()->as_C_string()));
        }
      }
    }
  }
}


// Garbage collection
void klassVtable::oop_follow_contents() {
  int len = length();
  for (int i = 0; i < len; i++) {
    MarkSweep::mark_and_push(adr_method_at(i));
  }
}

#ifndef SERIALGC
void klassVtable::oop_follow_contents(ParCompactionManager* cm) {
  int len = length();
  for (int i = 0; i < len; i++) {
    PSParallelCompact::mark_and_push(cm, adr_method_at(i));
  }
}
#endif // SERIALGC

void klassVtable::oop_adjust_pointers() {
  int len = length();
  for (int i = 0; i < len; i++) {
    MarkSweep::adjust_pointer(adr_method_at(i));
  }
}

#ifndef SERIALGC
void klassVtable::oop_update_pointers(ParCompactionManager* cm) {
  const int n = length();
  for (int i = 0; i < n; i++) {
    PSParallelCompact::adjust_pointer(adr_method_at(i));
  }
}

void klassVtable::oop_update_pointers(ParCompactionManager* cm,
                                      HeapWord* beg_addr, HeapWord* end_addr) {
  const int n = length();
  const int entry_size = vtableEntry::size();

  int beg_idx = 0;
  HeapWord* const method_0 = (HeapWord*)adr_method_at(0);
  if (beg_addr > method_0) {
    // it's safe to use cast, as we have guarantees on vtable size to be sane
    beg_idx = int((pointer_delta(beg_addr, method_0) + entry_size - 1) / entry_size);
  }

  oop* const beg_oop = adr_method_at(beg_idx);
  oop* const end_oop = MIN2((oop*)end_addr, adr_method_at(n));
  for (oop* cur_oop = beg_oop; cur_oop < end_oop; cur_oop += entry_size) {
    PSParallelCompact::adjust_pointer(cur_oop);
  }
}
#endif // SERIALGC

// Iterators
void klassVtable::oop_oop_iterate(OopClosure* blk) {
  int len = length();
  for (int i = 0; i < len; i++) {
    blk->do_oop(adr_method_at(i));
  }
}

void klassVtable::oop_oop_iterate_m(OopClosure* blk, MemRegion mr) {
  int len = length();
  int i;
  for (i = 0; i < len; i++) {
    if ((HeapWord*)adr_method_at(i) >= mr.start()) break;
  }
  for (; i < len; i++) {
    oop* adr = adr_method_at(i);
    if ((HeapWord*)adr < mr.end()) blk->do_oop(adr);
  }
}

//-----------------------------------------------------------------------------------------
// Itable code

// Initialize a itableMethodEntry
void itableMethodEntry::initialize(methodOop m) {
  if (m == NULL) return;

  _method = m;
}

klassItable::klassItable(instanceKlassHandle klass) {
  _klass = klass;

  if (klass->itable_length() > 0) {
    itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();
    if (offset_entry  != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized
      // First offset entry points to the first method_entry
      intptr_t* method_entry  = (intptr_t *)(((address)klass->as_klassOop()) + offset_entry->offset());
      intptr_t* end         = klass->end_of_itable();

      _table_offset      = (intptr_t*)offset_entry - (intptr_t*)klass->as_klassOop();
      _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
      _size_method_table = (end - method_entry)                  / itableMethodEntry::size();
      assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
      return;
    }
  }

738
  // The length of the itable was either zero, or it has not yet been initialized.
D
duke 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
  _table_offset      = 0;
  _size_offset_table = 0;
  _size_method_table = 0;
}

// Garbage Collection

void klassItable::oop_follow_contents() {
  // offset table
  itableOffsetEntry* ioe = offset_entry(0);
  for(int i = 0; i < _size_offset_table; i++) {
    MarkSweep::mark_and_push((oop*)&ioe->_interface);
    ioe++;
  }

  // method table
  itableMethodEntry* ime = method_entry(0);
  for(int j = 0; j < _size_method_table; j++) {
    MarkSweep::mark_and_push((oop*)&ime->_method);
    ime++;
  }
}

#ifndef SERIALGC
void klassItable::oop_follow_contents(ParCompactionManager* cm) {
  // offset table
  itableOffsetEntry* ioe = offset_entry(0);
  for(int i = 0; i < _size_offset_table; i++) {
    PSParallelCompact::mark_and_push(cm, (oop*)&ioe->_interface);
    ioe++;
  }

  // method table
  itableMethodEntry* ime = method_entry(0);
  for(int j = 0; j < _size_method_table; j++) {
    PSParallelCompact::mark_and_push(cm, (oop*)&ime->_method);
    ime++;
  }
}
#endif // SERIALGC

void klassItable::oop_adjust_pointers() {
  // offset table
  itableOffsetEntry* ioe = offset_entry(0);
  for(int i = 0; i < _size_offset_table; i++) {
    MarkSweep::adjust_pointer((oop*)&ioe->_interface);
    ioe++;
  }

  // method table
  itableMethodEntry* ime = method_entry(0);
  for(int j = 0; j < _size_method_table; j++) {
    MarkSweep::adjust_pointer((oop*)&ime->_method);
    ime++;
  }
}

#ifndef SERIALGC
void klassItable::oop_update_pointers(ParCompactionManager* cm) {
  // offset table
  itableOffsetEntry* ioe = offset_entry(0);
  for(int i = 0; i < _size_offset_table; i++) {
    PSParallelCompact::adjust_pointer((oop*)&ioe->_interface);
    ioe++;
  }

  // method table
  itableMethodEntry* ime = method_entry(0);
  for(int j = 0; j < _size_method_table; j++) {
    PSParallelCompact::adjust_pointer((oop*)&ime->_method);
    ime++;
  }
}

void klassItable::oop_update_pointers(ParCompactionManager* cm,
                                      HeapWord* beg_addr, HeapWord* end_addr) {
  // offset table
  itableOffsetEntry* ioe = offset_entry(0);
  for(int i = 0; i < _size_offset_table; i++) {
    oop* p = (oop*)&ioe->_interface;
    PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
    ioe++;
  }

  // method table
  itableMethodEntry* ime = method_entry(0);
  for(int j = 0; j < _size_method_table; j++) {
    oop* p = (oop*)&ime->_method;
    PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
    ime++;
  }
}
#endif // SERIALGC

// Iterators
void klassItable::oop_oop_iterate(OopClosure* blk) {
  // offset table
  itableOffsetEntry* ioe = offset_entry(0);
  for(int i = 0; i < _size_offset_table; i++) {
    blk->do_oop((oop*)&ioe->_interface);
    ioe++;
  }

  // method table
  itableMethodEntry* ime = method_entry(0);
  for(int j = 0; j < _size_method_table; j++) {
    blk->do_oop((oop*)&ime->_method);
    ime++;
  }
}

void klassItable::oop_oop_iterate_m(OopClosure* blk, MemRegion mr) {
  // offset table
  itableOffsetEntry* ioe = offset_entry(0);
  for(int i = 0; i < _size_offset_table; i++) {
    oop* adr = (oop*)&ioe->_interface;
    if (mr.contains(adr)) blk->do_oop(adr);
    ioe++;
  }

  // method table
  itableMethodEntry* ime = method_entry(0);
  for(int j = 0; j < _size_method_table; j++) {
    oop* adr = (oop*)&ime->_method;
    if (mr.contains(adr)) blk->do_oop(adr);
    ime++;
  }
}


static int initialize_count = 0;

// Initialization
void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
873 874 875 876 877 878 879 880 881
  // Cannot be setup doing bootstrapping, interfaces don't have
  // itables, and klass with only ones entry have empty itables
  if (Universe::is_bootstrapping() ||
      _klass->is_interface() ||
      _klass->itable_length() == itableOffsetEntry::size()) return;

  // There's alway an extra itable entry so we can null-terminate it.
  guarantee(size_offset_table() >= 1, "too small");
  int num_interfaces = size_offset_table() - 1;
D
duke 已提交
882
  if (num_interfaces > 0) {
883 884
    if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count,
                                    _klass->name()->as_C_string());
D
duke 已提交
885 886 887 888 889 890 891 892 893 894 895 896


    // Interate through all interfaces
    int i;
    for(i = 0; i < num_interfaces; i++) {
      itableOffsetEntry* ioe = offset_entry(i);
      KlassHandle interf_h (THREAD, ioe->interface_klass());
      assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
      initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
    }

  }
897 898 899
  // Check that the last entry is empty
  itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
  guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
D
duke 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
}


void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) {
  objArrayHandle methods(THREAD, instanceKlass::cast(interf_h())->methods());
  int nof_methods = methods()->length();
  HandleMark hm;
  KlassHandle klass = _klass;
  assert(nof_methods > 0, "at least one method must exist for interface to be in vtable")
  Handle interface_loader (THREAD, instanceKlass::cast(interf_h())->class_loader());
  int ime_num = 0;

  // Skip first methodOop if it is a class initializer
  int i = ((methodOop)methods()->obj_at(0))->name() != vmSymbols::class_initializer_name() ? 0 : 1;

  // m, method_name, method_signature, klass reset each loop so they
  // don't need preserving across check_signature_loaders call
  // methods needs a handle in case of gc from check_signature_loaders
  for(; i < nof_methods; i++) {
    methodOop m = (methodOop)methods()->obj_at(i);
    symbolOop method_name = m->name();
    symbolOop method_signature = m->signature();

    // This is same code as in Linkresolver::lookup_instance_method_in_klasses
    methodOop target = klass->uncached_lookup_method(method_name, method_signature);
    while (target != NULL && target->is_static()) {
      // continue with recursive lookup through the superclass
      klassOop super = Klass::cast(target->method_holder())->super();
      target = (super == NULL) ? methodOop(NULL) : Klass::cast(super)->uncached_lookup_method(method_name, method_signature);
    }
    if (target == NULL || !target->is_public() || target->is_abstract()) {
      // Entry do not resolve. Leave it empty
    } else {
      // Entry did resolve, check loader constraints before initializing
      // if checkconstraints requested
      methodHandle  target_h (THREAD, target); // preserve across gc
      if (checkconstraints) {
        Handle method_holder_loader (THREAD, instanceKlass::cast(target->method_holder())->class_loader());
        if (method_holder_loader() != interface_loader()) {
          ResourceMark rm(THREAD);
          char* failed_type_name =
            SystemDictionary::check_signature_loaders(method_signature,
                                                      method_holder_loader,
                                                      interface_loader,
                                                      true, CHECK);
          if (failed_type_name != NULL) {
            const char* msg = "loader constraint violation in interface "
              "itable initialization: when resolving method \"%s\" the class"
              " loader (instance of %s) of the current class, %s, "
              "and the class loader (instance of %s) for interface "
              "%s have different Class objects for the type %s "
              "used in the signature";
            char* sig = target_h()->name_and_sig_as_C_string();
            const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
            char* current = klass->name()->as_C_string();
            const char* loader2 = SystemDictionary::loader_name(interface_loader());
            char* iface = instanceKlass::cast(interf_h())->name()->as_C_string();
            size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
              strlen(current) + strlen(loader2) + strlen(iface) +
              strlen(failed_type_name);
            char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
            jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
                         iface, failed_type_name);
            THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
          }
        }
      }

      // ime may have moved during GC so recalculate address
      itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target_h());
    }
    // Progress to next entry
    ime_num++;
  }
}

976
// Update entry for specific methodOop
D
duke 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
void klassItable::initialize_with_method(methodOop m) {
  itableMethodEntry* ime = method_entry(0);
  for(int i = 0; i < _size_method_table; i++) {
    if (ime->method() == m) {
      ime->initialize(m);
    }
    ime++;
  }
}

void klassItable::adjust_method_entries(methodOop* old_methods, methodOop* new_methods,
                                        int methods_length, bool * trace_name_printed) {
  // search the itable for uses of either obsolete or EMCP methods
  for (int j = 0; j < methods_length; j++) {
    methodOop old_method = old_methods[j];
    methodOop new_method = new_methods[j];
    itableMethodEntry* ime = method_entry(0);

995 996 997 998
    // The itable can describe more than one interface and the same
    // method signature can be specified by more than one interface.
    // This means we have to do an exhaustive search to find all the
    // old_method references.
D
duke 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    for (int i = 0; i < _size_method_table; i++) {
      if (ime->method() == old_method) {
        ime->initialize(new_method);

        if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
          if (!(*trace_name_printed)) {
            // RC_TRACE_MESG macro has an embedded ResourceMark
            RC_TRACE_MESG(("adjust: name=%s",
              Klass::cast(old_method->method_holder())->external_name()));
            *trace_name_printed = true;
          }
          // RC_TRACE macro has an embedded ResourceMark
          RC_TRACE(0x00200000, ("itable method update: %s(%s)",
            new_method->name()->as_C_string(),
            new_method->signature()->as_C_string()));
        }
      }
      ime++;
    }
  }
}


// Setup
class InterfaceVisiterClosure : public StackObj {
 public:
  virtual void doit(klassOop intf, int method_count) = 0;
};

// Visit all interfaces with at-least one method (excluding <clinit>)
void visit_all_interfaces(objArrayOop transitive_intf, InterfaceVisiterClosure *blk) {
  // Handle array argument
  for(int i = 0; i < transitive_intf->length(); i++) {
    klassOop intf = (klassOop)transitive_intf->obj_at(i);
    assert(Klass::cast(intf)->is_interface(), "sanity check");

    // Find no. of methods excluding a <clinit>
    int method_count = instanceKlass::cast(intf)->methods()->length();
    if (method_count > 0) {
      methodOop m = (methodOop)instanceKlass::cast(intf)->methods()->obj_at(0);
      assert(m != NULL && m->is_method(), "sanity check");
      if (m->name() == vmSymbols::object_initializer_name()) {
        method_count--;
      }
    }

    // Only count interfaces with at least one method
    if (method_count > 0) {
      blk->doit(intf, method_count);
    }
  }
}

class CountInterfacesClosure : public InterfaceVisiterClosure {
 private:
  int _nof_methods;
  int _nof_interfaces;
 public:
   CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; }

   int nof_methods() const    { return _nof_methods; }
   int nof_interfaces() const { return _nof_interfaces; }

   void doit(klassOop intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; }
};

class SetupItableClosure : public InterfaceVisiterClosure  {
 private:
  itableOffsetEntry* _offset_entry;
  itableMethodEntry* _method_entry;
  address            _klass_begin;
 public:
  SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) {
    _klass_begin  = klass_begin;
    _offset_entry = offset_entry;
    _method_entry = method_entry;
  }

  itableMethodEntry* method_entry() const { return _method_entry; }

  void doit(klassOop intf, int method_count) {
    int offset = ((address)_method_entry) - _klass_begin;
    _offset_entry->initialize(intf, offset);
    _offset_entry++;
    _method_entry += method_count;
  }
};

int klassItable::compute_itable_size(objArrayHandle transitive_interfaces) {
  // Count no of interfaces and total number of interface methods
  CountInterfacesClosure cic;
  visit_all_interfaces(transitive_interfaces(), &cic);

1092 1093
  // There's alway an extra itable entry so we can null-terminate it.
  int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods());
D
duke 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

  // Statistics
  update_stats(itable_size * HeapWordSize);

  return itable_size;
}


// Fill out offset table and interface klasses into the itable space
void klassItable::setup_itable_offset_table(instanceKlassHandle klass) {
  if (klass->itable_length() == 0) return;
  assert(!klass->is_interface(), "Should have zero length itable");

  // Count no of interfaces and total number of interface methods
  CountInterfacesClosure cic;
  visit_all_interfaces(klass->transitive_interfaces(), &cic);
  int nof_methods    = cic.nof_methods();
  int nof_interfaces = cic.nof_interfaces();

1113 1114
  // Add one extra entry so we can null-terminate the table
  nof_interfaces++;
D
duke 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123

  assert(compute_itable_size(objArrayHandle(klass->transitive_interfaces())) ==
         calc_itable_size(nof_interfaces, nof_methods),
         "mismatch calculation of itable size");

  // Fill-out offset table
  itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();
  itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);
  intptr_t* end               = klass->end_of_itable();
1124 1125
  assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_static_fields(), "wrong offset calculation (1)");
  assert((oop*)(end) == (oop*)(ime + nof_methods),                      "wrong offset calculation (2)");
D
duke 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322

  // Visit all interfaces and initialize itable offset table
  SetupItableClosure sic((address)klass->as_klassOop(), ioe, ime);
  visit_all_interfaces(klass->transitive_interfaces(), &sic);

#ifdef ASSERT
  ime  = sic.method_entry();
  oop* v = (oop*) klass->end_of_itable();
  assert( (oop*)(ime) == v, "wrong offset calculation (2)");
#endif
}


// m must be a method in an interface
int klassItable::compute_itable_index(methodOop m) {
  klassOop intf = m->method_holder();
  assert(instanceKlass::cast(intf)->is_interface(), "sanity check");
  objArrayOop methods = instanceKlass::cast(intf)->methods();
  int index = 0;
  while(methods->obj_at(index) != m) {
    index++;
    assert(index < methods->length(), "should find index for resolve_invoke");
  }
  // Adjust for <clinit>, which is left out of table if first method
  if (methods->length() > 0 && ((methodOop)methods->obj_at(0))->name() == vmSymbols::class_initializer_name()) {
    index--;
  }
  return index;
}

void klassVtable::verify(outputStream* st, bool forced) {
  // make sure table is initialized
  if (!Universe::is_fully_initialized()) return;
#ifndef PRODUCT
  // avoid redundant verifies
  if (!forced && _verify_count == Universe::verify_count()) return;
  _verify_count = Universe::verify_count();
#endif
  oop* end_of_obj = (oop*)_klass() + _klass()->size();
  oop* end_of_vtable = (oop *)&table()[_length];
  if (end_of_vtable > end_of_obj) {
    fatal1("klass %s: klass object too short (vtable extends beyond end)",
          _klass->internal_name());
  }

  for (int i = 0; i < _length; i++) table()[i].verify(this, st);
  // verify consistency with superKlass vtable
  klassOop super = _klass->super();
  if (super != NULL) {
    instanceKlass* sk = instanceKlass::cast(super);
    klassVtable* vt = sk->vtable();
    for (int i = 0; i < vt->length(); i++) {
      verify_against(st, vt, i);
    }
  }
}

void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
  vtableEntry* vte = &vt->table()[index];
  if (vte->method()->name()      != table()[index].method()->name() ||
      vte->method()->signature() != table()[index].method()->signature()) {
    fatal("mismatched name/signature of vtable entries");
  }
}

#ifndef PRODUCT
void klassVtable::print() {
  ResourceMark rm;
  tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length());
  for (int i = 0; i < length(); i++) {
    table()[i].print();
    tty->cr();
  }
}
#endif

void vtableEntry::verify(klassVtable* vt, outputStream* st) {
  NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
  assert(method() != NULL, "must have set method");
  method()->verify();
  // we sub_type, because it could be a miranda method
  if (!vt->klass()->is_subtype_of(method()->method_holder())) {
#ifndef PRODUCT
    print();
#endif
    fatal1("vtableEntry %#lx: method is from subclass", this);
  }
}

#ifndef PRODUCT

void vtableEntry::print() {
  ResourceMark rm;
  tty->print("vtableEntry %s: ", method()->name()->as_C_string());
  if (Verbose) {
    tty->print("m %#lx ", (address)method());
  }
}

class VtableStats : AllStatic {
 public:
  static int no_klasses;                // # classes with vtables
  static int no_array_klasses;          // # array classes
  static int no_instance_klasses;       // # instanceKlasses
  static int sum_of_vtable_len;         // total # of vtable entries
  static int sum_of_array_vtable_len;   // total # of vtable entries in array klasses only
  static int fixed;                     // total fixed overhead in bytes
  static int filler;                    // overhead caused by filler bytes
  static int entries;                   // total bytes consumed by vtable entries
  static int array_entries;             // total bytes consumed by array vtable entries

  static void do_class(klassOop k) {
    Klass* kl = k->klass_part();
    klassVtable* vt = kl->vtable();
    if (vt == NULL) return;
    no_klasses++;
    if (kl->oop_is_instance()) {
      no_instance_klasses++;
      kl->array_klasses_do(do_class);
    }
    if (kl->oop_is_array()) {
      no_array_klasses++;
      sum_of_array_vtable_len += vt->length();
    }
    sum_of_vtable_len += vt->length();
  }

  static void compute() {
    SystemDictionary::classes_do(do_class);
    fixed  = no_klasses * oopSize;      // vtable length
    // filler size is a conservative approximation
    filler = oopSize * (no_klasses - no_instance_klasses) * (sizeof(instanceKlass) - sizeof(arrayKlass) - 1);
    entries = sizeof(vtableEntry) * sum_of_vtable_len;
    array_entries = sizeof(vtableEntry) * sum_of_array_vtable_len;
  }
};

int VtableStats::no_klasses = 0;
int VtableStats::no_array_klasses = 0;
int VtableStats::no_instance_klasses = 0;
int VtableStats::sum_of_vtable_len = 0;
int VtableStats::sum_of_array_vtable_len = 0;
int VtableStats::fixed = 0;
int VtableStats::filler = 0;
int VtableStats::entries = 0;
int VtableStats::array_entries = 0;

void klassVtable::print_statistics() {
  ResourceMark rm;
  HandleMark hm;
  VtableStats::compute();
  tty->print_cr("vtable statistics:");
  tty->print_cr("%6d classes (%d instance, %d array)", VtableStats::no_klasses, VtableStats::no_instance_klasses, VtableStats::no_array_klasses);
  int total = VtableStats::fixed + VtableStats::filler + VtableStats::entries;
  tty->print_cr("%6d bytes fixed overhead (refs + vtable object header)", VtableStats::fixed);
  tty->print_cr("%6d bytes filler overhead", VtableStats::filler);
  tty->print_cr("%6d bytes for vtable entries (%d for arrays)", VtableStats::entries, VtableStats::array_entries);
  tty->print_cr("%6d bytes total", total);
}

bool klassVtable::check_no_old_entries() {
  // Check that there really is no entry
  for (int i = 0; i < length(); i++) {
    methodOop m = unchecked_method_at(i);
    if (m != NULL) {
        if (m->is_old()) {
            return false;
        }
    }
  }
  return true;
}

void klassVtable::dump_vtable() {
  tty->print_cr("vtable dump --");
  for (int i = 0; i < length(); i++) {
    methodOop m = unchecked_method_at(i);
    if (m != NULL) {
      tty->print("      (%5d)  ", i);
      m->access_flags().print_on(tty);
      tty->print(" --  ");
      m->print_name(tty);
      tty->cr();
    }
  }
}

int  klassItable::_total_classes;   // Total no. of classes with itables
long klassItable::_total_size;      // Total no. of bytes used for itables

void klassItable::print_statistics() {
 tty->print_cr("itable statistics:");
 tty->print_cr("%6d classes with itables", _total_classes);
 tty->print_cr("%6d K uses for itables (average by class: %d bytes)", _total_size / K, _total_size / _total_classes);
}

#endif // PRODUCT