src/share/vm/oops/cpCache.cpp

Thu, 21 Mar 2013 09:27:54 +0100

author
roland
date
Thu, 21 Mar 2013 09:27:54 +0100
changeset 4860
46f6f063b272
parent 4712
3efdfd6ddbf2
child 4984
c115fac239eb
permissions
-rw-r--r--

7153771: array bound check elimination for c1
Summary: when possible optimize out array bound checks, inserting predicates when needed.
Reviewed-by: never, kvn, twisti
Contributed-by: thomaswue <thomas.wuerthinger@oracle.com>

     1 /*
     2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "gc_implementation/shared/markSweep.inline.hpp"
    27 #include "interpreter/interpreter.hpp"
    28 #include "interpreter/rewriter.hpp"
    29 #include "memory/universe.inline.hpp"
    30 #include "oops/cpCache.hpp"
    31 #include "oops/objArrayOop.hpp"
    32 #include "oops/oop.inline.hpp"
    33 #include "prims/jvmtiRedefineClassesTrace.hpp"
    34 #include "prims/methodHandles.hpp"
    35 #include "runtime/handles.inline.hpp"
    36 #include "utilities/macros.hpp"
    37 #if INCLUDE_ALL_GCS
    38 # include "gc_implementation/parallelScavenge/psPromotionManager.hpp"
    39 #endif // INCLUDE_ALL_GCS
    42 // Implememtation of ConstantPoolCacheEntry
    44 void ConstantPoolCacheEntry::initialize_entry(int index) {
    45   assert(0 < index && index < 0x10000, "sanity check");
    46   _indices = index;
    47   _f1 = NULL;
    48   _f2 = _flags = 0;
    49   assert(constant_pool_index() == index, "");
    50 }
    52 int ConstantPoolCacheEntry::make_flags(TosState state,
    53                                        int option_bits,
    54                                        int field_index_or_method_params) {
    55   assert(state < number_of_states, "Invalid state in make_flags");
    56   int f = ((int)state << tos_state_shift) | option_bits | field_index_or_method_params;
    57   // Preserve existing flag bit values
    58   // The low bits are a field offset, or else the method parameter size.
    59 #ifdef ASSERT
    60   TosState old_state = flag_state();
    61   assert(old_state == (TosState)0 || old_state == state,
    62          "inconsistent cpCache flags state");
    63 #endif
    64   return (_flags | f) ;
    65 }
    67 void ConstantPoolCacheEntry::set_bytecode_1(Bytecodes::Code code) {
    68 #ifdef ASSERT
    69   // Read once.
    70   volatile Bytecodes::Code c = bytecode_1();
    71   assert(c == 0 || c == code || code == 0, "update must be consistent");
    72 #endif
    73   // Need to flush pending stores here before bytecode is written.
    74   OrderAccess::release_store_ptr(&_indices, _indices | ((u_char)code << bytecode_1_shift));
    75 }
    77 void ConstantPoolCacheEntry::set_bytecode_2(Bytecodes::Code code) {
    78 #ifdef ASSERT
    79   // Read once.
    80   volatile Bytecodes::Code c = bytecode_2();
    81   assert(c == 0 || c == code || code == 0, "update must be consistent");
    82 #endif
    83   // Need to flush pending stores here before bytecode is written.
    84   OrderAccess::release_store_ptr(&_indices, _indices | ((u_char)code << bytecode_2_shift));
    85 }
    87 // Sets f1, ordering with previous writes.
    88 void ConstantPoolCacheEntry::release_set_f1(Metadata* f1) {
    89   assert(f1 != NULL, "");
    90   OrderAccess::release_store_ptr((HeapWord*) &_f1, f1);
    91 }
    93 // Sets flags, but only if the value was previously zero.
    94 bool ConstantPoolCacheEntry::init_flags_atomic(intptr_t flags) {
    95   intptr_t result = Atomic::cmpxchg_ptr(flags, &_flags, 0);
    96   return (result == 0);
    97 }
    99 // Note that concurrent update of both bytecodes can leave one of them
   100 // reset to zero.  This is harmless; the interpreter will simply re-resolve
   101 // the damaged entry.  More seriously, the memory synchronization is needed
   102 // to flush other fields (f1, f2) completely to memory before the bytecodes
   103 // are updated, lest other processors see a non-zero bytecode but zero f1/f2.
   104 void ConstantPoolCacheEntry::set_field(Bytecodes::Code get_code,
   105                                        Bytecodes::Code put_code,
   106                                        KlassHandle field_holder,
   107                                        int field_index,
   108                                        int field_offset,
   109                                        TosState field_type,
   110                                        bool is_final,
   111                                        bool is_volatile,
   112                                        Klass* root_klass) {
   113   set_f1(field_holder());
   114   set_f2(field_offset);
   115   assert((field_index & field_index_mask) == field_index,
   116          "field index does not fit in low flag bits");
   117   set_field_flags(field_type,
   118                   ((is_volatile ? 1 : 0) << is_volatile_shift) |
   119                   ((is_final    ? 1 : 0) << is_final_shift),
   120                   field_index);
   121   set_bytecode_1(get_code);
   122   set_bytecode_2(put_code);
   123   NOT_PRODUCT(verify(tty));
   124 }
   126 void ConstantPoolCacheEntry::set_parameter_size(int value) {
   127   // This routine is called only in corner cases where the CPCE is not yet initialized.
   128   // See AbstractInterpreter::deopt_continue_after_entry.
   129   assert(_flags == 0 || parameter_size() == 0 || parameter_size() == value,
   130          err_msg("size must not change: parameter_size=%d, value=%d", parameter_size(), value));
   131   // Setting the parameter size by itself is only safe if the
   132   // current value of _flags is 0, otherwise another thread may have
   133   // updated it and we don't want to overwrite that value.  Don't
   134   // bother trying to update it once it's nonzero but always make
   135   // sure that the final parameter size agrees with what was passed.
   136   if (_flags == 0) {
   137     Atomic::cmpxchg_ptr((value & parameter_size_mask), &_flags, 0);
   138   }
   139   guarantee(parameter_size() == value,
   140             err_msg("size must not change: parameter_size=%d, value=%d", parameter_size(), value));
   141 }
   143 void ConstantPoolCacheEntry::set_method(Bytecodes::Code invoke_code,
   144                                         methodHandle method,
   145                                         int vtable_index) {
   146   assert(method->interpreter_entry() != NULL, "should have been set at this point");
   147   assert(!method->is_obsolete(),  "attempt to write obsolete method to cpCache");
   149   int byte_no = -1;
   150   bool change_to_virtual = false;
   152   switch (invoke_code) {
   153     case Bytecodes::_invokeinterface:
   154       // We get here from InterpreterRuntime::resolve_invoke when an invokeinterface
   155       // instruction somehow links to a non-interface method (in Object).
   156       // In that case, the method has no itable index and must be invoked as a virtual.
   157       // Set a flag to keep track of this corner case.
   158       change_to_virtual = true;
   160       // ...and fall through as if we were handling invokevirtual:
   161     case Bytecodes::_invokevirtual:
   162       {
   163         if (method->can_be_statically_bound()) {
   164           // set_f2_as_vfinal_method checks if is_vfinal flag is true.
   165           set_method_flags(as_TosState(method->result_type()),
   166                            (                             1      << is_vfinal_shift) |
   167                            ((method->is_final_method() ? 1 : 0) << is_final_shift)  |
   168                            ((change_to_virtual         ? 1 : 0) << is_forced_virtual_shift),
   169                            method()->size_of_parameters());
   170           set_f2_as_vfinal_method(method());
   171         } else {
   172           assert(vtable_index >= 0, "valid index");
   173           assert(!method->is_final_method(), "sanity");
   174           set_method_flags(as_TosState(method->result_type()),
   175                            ((change_to_virtual ? 1 : 0) << is_forced_virtual_shift),
   176                            method()->size_of_parameters());
   177           set_f2(vtable_index);
   178         }
   179         byte_no = 2;
   180         break;
   181       }
   183     case Bytecodes::_invokespecial:
   184     case Bytecodes::_invokestatic:
   185       // Note:  Read and preserve the value of the is_vfinal flag on any
   186       // invokevirtual bytecode shared with this constant pool cache entry.
   187       // It is cheap and safe to consult is_vfinal() at all times.
   188       // Once is_vfinal is set, it must stay that way, lest we get a dangling oop.
   189       set_method_flags(as_TosState(method->result_type()),
   190                        ((is_vfinal()               ? 1 : 0) << is_vfinal_shift) |
   191                        ((method->is_final_method() ? 1 : 0) << is_final_shift),
   192                        method()->size_of_parameters());
   193       set_f1(method());
   194       byte_no = 1;
   195       break;
   196     default:
   197       ShouldNotReachHere();
   198       break;
   199   }
   201   // Note:  byte_no also appears in TemplateTable::resolve.
   202   if (byte_no == 1) {
   203     assert(invoke_code != Bytecodes::_invokevirtual &&
   204            invoke_code != Bytecodes::_invokeinterface, "");
   205     set_bytecode_1(invoke_code);
   206   } else if (byte_no == 2)  {
   207     if (change_to_virtual) {
   208       assert(invoke_code == Bytecodes::_invokeinterface, "");
   209       // NOTE: THIS IS A HACK - BE VERY CAREFUL!!!
   210       //
   211       // Workaround for the case where we encounter an invokeinterface, but we
   212       // should really have an _invokevirtual since the resolved method is a
   213       // virtual method in java.lang.Object. This is a corner case in the spec
   214       // but is presumably legal. javac does not generate this code.
   215       //
   216       // We set bytecode_1() to _invokeinterface, because that is the
   217       // bytecode # used by the interpreter to see if it is resolved.
   218       // We set bytecode_2() to _invokevirtual.
   219       // See also interpreterRuntime.cpp. (8/25/2000)
   220       // Only set resolved for the invokeinterface case if method is public.
   221       // Otherwise, the method needs to be reresolved with caller for each
   222       // interface call.
   223       if (method->is_public()) set_bytecode_1(invoke_code);
   224     } else {
   225       assert(invoke_code == Bytecodes::_invokevirtual, "");
   226     }
   227     // set up for invokevirtual, even if linking for invokeinterface also:
   228     set_bytecode_2(Bytecodes::_invokevirtual);
   229   } else {
   230     ShouldNotReachHere();
   231   }
   232   NOT_PRODUCT(verify(tty));
   233 }
   236 void ConstantPoolCacheEntry::set_interface_call(methodHandle method, int index) {
   237   InstanceKlass* interf = method->method_holder();
   238   assert(interf->is_interface(), "must be an interface");
   239   assert(!method->is_final_method(), "interfaces do not have final methods; cannot link to one here");
   240   set_f1(interf);
   241   set_f2(index);
   242   set_method_flags(as_TosState(method->result_type()),
   243                    0,  // no option bits
   244                    method()->size_of_parameters());
   245   set_bytecode_1(Bytecodes::_invokeinterface);
   246 }
   249 void ConstantPoolCacheEntry::set_method_handle(constantPoolHandle cpool, const CallInfo &call_info) {
   250   set_method_handle_common(cpool, Bytecodes::_invokehandle, call_info);
   251 }
   253 void ConstantPoolCacheEntry::set_dynamic_call(constantPoolHandle cpool, const CallInfo &call_info) {
   254   set_method_handle_common(cpool, Bytecodes::_invokedynamic, call_info);
   255 }
   257 void ConstantPoolCacheEntry::set_method_handle_common(constantPoolHandle cpool,
   258                                                       Bytecodes::Code invoke_code,
   259                                                       const CallInfo &call_info) {
   260   // NOTE: This CPCE can be the subject of data races.
   261   // There are three words to update: flags, refs[f2], f1 (in that order).
   262   // Writers must store all other values before f1.
   263   // Readers must test f1 first for non-null before reading other fields.
   264   // Competing writers must acquire exclusive access via a lock.
   265   // A losing writer waits on the lock until the winner writes f1 and leaves
   266   // the lock, so that when the losing writer returns, he can use the linked
   267   // cache entry.
   269   MonitorLockerEx ml(cpool->lock());
   270   if (!is_f1_null()) {
   271     return;
   272   }
   274   const methodHandle adapter = call_info.resolved_method();
   275   const Handle appendix      = call_info.resolved_appendix();
   276   const Handle method_type   = call_info.resolved_method_type();
   277   const bool has_appendix    = appendix.not_null();
   278   const bool has_method_type = method_type.not_null();
   280   // Write the flags.
   281   set_method_flags(as_TosState(adapter->result_type()),
   282                    ((has_appendix    ? 1 : 0) << has_appendix_shift   ) |
   283                    ((has_method_type ? 1 : 0) << has_method_type_shift) |
   284                    (                   1      << is_final_shift       ),
   285                    adapter->size_of_parameters());
   287   if (TraceInvokeDynamic) {
   288     tty->print_cr("set_method_handle bc=%d appendix="PTR_FORMAT"%s method_type="PTR_FORMAT"%s method="PTR_FORMAT" ",
   289                   invoke_code,
   290                   (intptr_t)appendix(),    (has_appendix    ? "" : " (unused)"),
   291                   (intptr_t)method_type(), (has_method_type ? "" : " (unused)"),
   292                   (intptr_t)adapter());
   293     adapter->print();
   294     if (has_appendix)  appendix()->print();
   295   }
   297   // Method handle invokes and invokedynamic sites use both cp cache words.
   298   // refs[f2], if not null, contains a value passed as a trailing argument to the adapter.
   299   // In the general case, this could be the call site's MethodType,
   300   // for use with java.lang.Invokers.checkExactType, or else a CallSite object.
   301   // f1 contains the adapter method which manages the actual call.
   302   // In the general case, this is a compiled LambdaForm.
   303   // (The Java code is free to optimize these calls by binding other
   304   // sorts of methods and appendices to call sites.)
   305   // JVM-level linking is via f1, as if for invokespecial, and signatures are erased.
   306   // The appendix argument (if any) is added to the signature, and is counted in the parameter_size bits.
   307   // Even with the appendix, the method will never take more than 255 parameter slots.
   308   //
   309   // This means that given a call site like (List)mh.invoke("foo"),
   310   // the f1 method has signature '(Ljl/Object;Ljl/invoke/MethodType;)Ljl/Object;',
   311   // not '(Ljava/lang/String;)Ljava/util/List;'.
   312   // The fact that String and List are involved is encoded in the MethodType in refs[f2].
   313   // This allows us to create fewer method oops, while keeping type safety.
   314   //
   316   objArrayHandle resolved_references = cpool->resolved_references();
   317   // Store appendix, if any.
   318   if (has_appendix) {
   319     const int appendix_index = f2_as_index() + _indy_resolved_references_appendix_offset;
   320     assert(appendix_index >= 0 && appendix_index < resolved_references->length(), "oob");
   321     assert(resolved_references->obj_at(appendix_index) == NULL, "init just once");
   322     resolved_references->obj_at_put(appendix_index, appendix());
   323   }
   325   // Store MethodType, if any.
   326   if (has_method_type) {
   327     const int method_type_index = f2_as_index() + _indy_resolved_references_method_type_offset;
   328     assert(method_type_index >= 0 && method_type_index < resolved_references->length(), "oob");
   329     assert(resolved_references->obj_at(method_type_index) == NULL, "init just once");
   330     resolved_references->obj_at_put(method_type_index, method_type());
   331   }
   333   release_set_f1(adapter());  // This must be the last one to set (see NOTE above)!
   335   // The interpreter assembly code does not check byte_2,
   336   // but it is used by is_resolved, method_if_resolved, etc.
   337   set_bytecode_1(invoke_code);
   338   NOT_PRODUCT(verify(tty));
   339   if (TraceInvokeDynamic) {
   340     this->print(tty, 0);
   341   }
   342 }
   344 Method* ConstantPoolCacheEntry::method_if_resolved(constantPoolHandle cpool) {
   345   // Decode the action of set_method and set_interface_call
   346   Bytecodes::Code invoke_code = bytecode_1();
   347   if (invoke_code != (Bytecodes::Code)0) {
   348     Metadata* f1 = (Metadata*)_f1;
   349     if (f1 != NULL) {
   350       switch (invoke_code) {
   351       case Bytecodes::_invokeinterface:
   352         assert(f1->is_klass(), "");
   353         return klassItable::method_for_itable_index((Klass*)f1, f2_as_index());
   354       case Bytecodes::_invokestatic:
   355       case Bytecodes::_invokespecial:
   356         assert(!has_appendix(), "");
   357       case Bytecodes::_invokehandle:
   358       case Bytecodes::_invokedynamic:
   359         assert(f1->is_method(), "");
   360         return (Method*)f1;
   361       }
   362     }
   363   }
   364   invoke_code = bytecode_2();
   365   if (invoke_code != (Bytecodes::Code)0) {
   366     switch (invoke_code) {
   367     case Bytecodes::_invokevirtual:
   368       if (is_vfinal()) {
   369         // invokevirtual
   370         Method* m = f2_as_vfinal_method();
   371         assert(m->is_method(), "");
   372         return m;
   373       } else {
   374         int holder_index = cpool->uncached_klass_ref_index_at(constant_pool_index());
   375         if (cpool->tag_at(holder_index).is_klass()) {
   376           Klass* klass = cpool->resolved_klass_at(holder_index);
   377           if (!klass->oop_is_instance())
   378             klass = SystemDictionary::Object_klass();
   379           return InstanceKlass::cast(klass)->method_at_vtable(f2_as_index());
   380         }
   381       }
   382       break;
   383     }
   384   }
   385   return NULL;
   386 }
   389 oop ConstantPoolCacheEntry::appendix_if_resolved(constantPoolHandle cpool) {
   390   if (is_f1_null() || !has_appendix())
   391     return NULL;
   392   const int ref_index = f2_as_index() + _indy_resolved_references_appendix_offset;
   393   objArrayOop resolved_references = cpool->resolved_references();
   394   return resolved_references->obj_at(ref_index);
   395 }
   398 oop ConstantPoolCacheEntry::method_type_if_resolved(constantPoolHandle cpool) {
   399   if (is_f1_null() || !has_method_type())
   400     return NULL;
   401   const int ref_index = f2_as_index() + _indy_resolved_references_method_type_offset;
   402   objArrayOop resolved_references = cpool->resolved_references();
   403   return resolved_references->obj_at(ref_index);
   404 }
   407 #if INCLUDE_JVMTI
   408 // RedefineClasses() API support:
   409 // If this ConstantPoolCacheEntry refers to old_method then update it
   410 // to refer to new_method.
   411 bool ConstantPoolCacheEntry::adjust_method_entry(Method* old_method,
   412        Method* new_method, bool * trace_name_printed) {
   414   if (is_vfinal()) {
   415     // virtual and final so _f2 contains method ptr instead of vtable index
   416     if (f2_as_vfinal_method() == old_method) {
   417       // match old_method so need an update
   418       // NOTE: can't use set_f2_as_vfinal_method as it asserts on different values
   419       _f2 = (intptr_t)new_method;
   420       if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
   421         if (!(*trace_name_printed)) {
   422           // RC_TRACE_MESG macro has an embedded ResourceMark
   423           RC_TRACE_MESG(("adjust: name=%s",
   424             old_method->method_holder()->external_name()));
   425           *trace_name_printed = true;
   426         }
   427         // RC_TRACE macro has an embedded ResourceMark
   428         RC_TRACE(0x00400000, ("cpc vf-entry update: %s(%s)",
   429           new_method->name()->as_C_string(),
   430           new_method->signature()->as_C_string()));
   431       }
   433       return true;
   434     }
   436     // f1() is not used with virtual entries so bail out
   437     return false;
   438   }
   440   if (_f1 == NULL) {
   441     // NULL f1() means this is a virtual entry so bail out
   442     // We are assuming that the vtable index does not need change.
   443     return false;
   444   }
   446   if (_f1 == old_method) {
   447     _f1 = new_method;
   448     if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
   449       if (!(*trace_name_printed)) {
   450         // RC_TRACE_MESG macro has an embedded ResourceMark
   451         RC_TRACE_MESG(("adjust: name=%s",
   452           old_method->method_holder()->external_name()));
   453         *trace_name_printed = true;
   454       }
   455       // RC_TRACE macro has an embedded ResourceMark
   456       RC_TRACE(0x00400000, ("cpc entry update: %s(%s)",
   457         new_method->name()->as_C_string(),
   458         new_method->signature()->as_C_string()));
   459     }
   461     return true;
   462   }
   464   return false;
   465 }
   467 // a constant pool cache entry should never contain old or obsolete methods
   468 bool ConstantPoolCacheEntry::check_no_old_or_obsolete_entries() {
   469   if (is_vfinal()) {
   470     // virtual and final so _f2 contains method ptr instead of vtable index
   471     Metadata* f2 = (Metadata*)_f2;
   472     // Return false if _f2 refers to an old or an obsolete method.
   473     // _f2 == NULL || !_f2->is_method() are just as unexpected here.
   474     return (f2 != NULL NOT_PRODUCT(&& f2->is_valid()) && f2->is_method() &&
   475             !((Method*)f2)->is_old() && !((Method*)f2)->is_obsolete());
   476   } else if (_f1 == NULL ||
   477              (NOT_PRODUCT(_f1->is_valid() &&) !_f1->is_method())) {
   478     // _f1 == NULL || !_f1->is_method() are OK here
   479     return true;
   480   }
   481   // return false if _f1 refers to an old or an obsolete method
   482   return (NOT_PRODUCT(_f1->is_valid() &&) _f1->is_method() &&
   483           !((Method*)_f1)->is_old() && !((Method*)_f1)->is_obsolete());
   484 }
   486 bool ConstantPoolCacheEntry::is_interesting_method_entry(Klass* k) {
   487   if (!is_method_entry()) {
   488     // not a method entry so not interesting by default
   489     return false;
   490   }
   492   Method* m = NULL;
   493   if (is_vfinal()) {
   494     // virtual and final so _f2 contains method ptr instead of vtable index
   495     m = f2_as_vfinal_method();
   496   } else if (is_f1_null()) {
   497     // NULL _f1 means this is a virtual entry so also not interesting
   498     return false;
   499   } else {
   500     if (!(_f1->is_method())) {
   501       // _f1 can also contain a Klass* for an interface
   502       return false;
   503     }
   504     m = f1_as_method();
   505   }
   507   assert(m != NULL && m->is_method(), "sanity check");
   508   if (m == NULL || !m->is_method() || (k != NULL && m->method_holder() != k)) {
   509     // robustness for above sanity checks or method is not in
   510     // the interesting class
   511     return false;
   512   }
   514   // the method is in the interesting class so the entry is interesting
   515   return true;
   516 }
   517 #endif // INCLUDE_JVMTI
   519 void ConstantPoolCacheEntry::print(outputStream* st, int index) const {
   520   // print separator
   521   if (index == 0) st->print_cr("                 -------------");
   522   // print entry
   523   st->print("%3d  ("PTR_FORMAT")  ", index, (intptr_t)this);
   524   st->print_cr("[%02x|%02x|%5d]", bytecode_2(), bytecode_1(),
   525                constant_pool_index());
   526   st->print_cr("                 [   "PTR_FORMAT"]", (intptr_t)_f1);
   527   st->print_cr("                 [   "PTR_FORMAT"]", (intptr_t)_f2);
   528   st->print_cr("                 [   "PTR_FORMAT"]", (intptr_t)_flags);
   529   st->print_cr("                 -------------");
   530 }
   532 void ConstantPoolCacheEntry::verify(outputStream* st) const {
   533   // not implemented yet
   534 }
   536 // Implementation of ConstantPoolCache
   538 ConstantPoolCache* ConstantPoolCache::allocate(ClassLoaderData* loader_data,
   539                                      int length,
   540                                      const intStack& index_map,
   541                                      const intStack& invokedynamic_map, TRAPS) {
   542   int size = ConstantPoolCache::size(length);
   544   return new (loader_data, size, false, THREAD) ConstantPoolCache(length, index_map, invokedynamic_map);
   545 }
   547 void ConstantPoolCache::initialize(const intArray& inverse_index_map,
   548                                    const intArray& invokedynamic_references_map) {
   549   assert(inverse_index_map.length() == length(), "inverse index map must have same length as cache");
   550   for (int i = 0; i < length(); i++) {
   551     ConstantPoolCacheEntry* e = entry_at(i);
   552     int original_index = inverse_index_map[i];
   553     e->initialize_entry(original_index);
   554     assert(entry_at(i) == e, "sanity");
   555   }
   556   for (int ref = 0; ref < invokedynamic_references_map.length(); ref++) {
   557     const int cpci = invokedynamic_references_map[ref];
   558     if (cpci >= 0) {
   559 #ifdef ASSERT
   560       // invokedynamic and invokehandle have more entries; check if they
   561       // all point to the same constant pool cache entry.
   562       for (int entry = 1; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) {
   563         const int cpci_next = invokedynamic_references_map[ref + entry];
   564         assert(cpci == cpci_next, err_msg_res("%d == %d", cpci, cpci_next));
   565       }
   566 #endif
   567       entry_at(cpci)->initialize_resolved_reference_index(ref);
   568       ref += ConstantPoolCacheEntry::_indy_resolved_references_entries - 1;  // skip extra entries
   569     }
   570   }
   571 }
   573 #if INCLUDE_JVMTI
   574 // RedefineClasses() API support:
   575 // If any entry of this ConstantPoolCache points to any of
   576 // old_methods, replace it with the corresponding new_method.
   577 void ConstantPoolCache::adjust_method_entries(Method** old_methods, Method** new_methods,
   578                                                      int methods_length, bool * trace_name_printed) {
   580   if (methods_length == 0) {
   581     // nothing to do if there are no methods
   582     return;
   583   }
   585   // get shorthand for the interesting class
   586   Klass* old_holder = old_methods[0]->method_holder();
   588   for (int i = 0; i < length(); i++) {
   589     if (!entry_at(i)->is_interesting_method_entry(old_holder)) {
   590       // skip uninteresting methods
   591       continue;
   592     }
   594     // The ConstantPoolCache contains entries for several different
   595     // things, but we only care about methods. In fact, we only care
   596     // about methods in the same class as the one that contains the
   597     // old_methods. At this point, we have an interesting entry.
   599     for (int j = 0; j < methods_length; j++) {
   600       Method* old_method = old_methods[j];
   601       Method* new_method = new_methods[j];
   603       if (entry_at(i)->adjust_method_entry(old_method, new_method,
   604           trace_name_printed)) {
   605         // current old_method matched this entry and we updated it so
   606         // break out and get to the next interesting entry if there one
   607         break;
   608       }
   609     }
   610   }
   611 }
   613 // the constant pool cache should never contain old or obsolete methods
   614 bool ConstantPoolCache::check_no_old_or_obsolete_entries() {
   615   for (int i = 1; i < length(); i++) {
   616     if (entry_at(i)->is_interesting_method_entry(NULL) &&
   617         !entry_at(i)->check_no_old_or_obsolete_entries()) {
   618       return false;
   619     }
   620   }
   621   return true;
   622 }
   624 void ConstantPoolCache::dump_cache() {
   625   for (int i = 1; i < length(); i++) {
   626     if (entry_at(i)->is_interesting_method_entry(NULL)) {
   627       entry_at(i)->print(tty, i);
   628     }
   629   }
   630 }
   631 #endif // INCLUDE_JVMTI
   634 // Printing
   636 void ConstantPoolCache::print_on(outputStream* st) const {
   637   assert(is_constantPoolCache(), "obj must be constant pool cache");
   638   st->print_cr(internal_name());
   639   // print constant pool cache entries
   640   for (int i = 0; i < length(); i++) entry_at(i)->print(st, i);
   641 }
   643 void ConstantPoolCache::print_value_on(outputStream* st) const {
   644   assert(is_constantPoolCache(), "obj must be constant pool cache");
   645   st->print("cache [%d]", length());
   646   print_address_on(st);
   647   st->print(" for ");
   648   constant_pool()->print_value_on(st);
   649 }
   652 // Verification
   654 void ConstantPoolCache::verify_on(outputStream* st) {
   655   guarantee(is_constantPoolCache(), "obj must be constant pool cache");
   656   // print constant pool cache entries
   657   for (int i = 0; i < length(); i++) entry_at(i)->verify(st);
   658 }

mercurial