src/share/vm/oops/cpCacheOop.cpp

Tue, 11 May 2010 14:35:43 -0700

author
prr
date
Tue, 11 May 2010 14:35:43 -0700
changeset 1840
fb57d4cf76c2
parent 1494
389049f3f393
child 1862
cd5dbf694d45
permissions
-rw-r--r--

6931180: Migration to recent versions of MS Platform SDK
6951582: Build problems on win64
Summary: Changes to enable building JDK7 with Microsoft Visual Studio 2010
Reviewed-by: ohair, art, ccheung, dcubed

     1 /*
     2  * Copyright 1998-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_cpCacheOop.cpp.incl"
    29 // Implememtation of ConstantPoolCacheEntry
    31 void ConstantPoolCacheEntry::initialize_entry(int index) {
    32   assert(0 < index && index < 0x10000, "sanity check");
    33   _indices = index;
    34   assert(constant_pool_index() == index, "");
    35 }
    37 void ConstantPoolCacheEntry::initialize_secondary_entry(int main_index) {
    38   assert(0 <= main_index && main_index < 0x10000, "sanity check");
    39   _indices = (main_index << 16);
    40   assert(main_entry_index() == main_index, "");
    41 }
    43 int ConstantPoolCacheEntry::as_flags(TosState state, bool is_final,
    44                     bool is_vfinal, bool is_volatile,
    45                     bool is_method_interface, bool is_method) {
    46   int f = state;
    48   assert( state < number_of_states, "Invalid state in as_flags");
    50   f <<= 1;
    51   if (is_final) f |= 1;
    52   f <<= 1;
    53   if (is_vfinal) f |= 1;
    54   f <<= 1;
    55   if (is_volatile) f |= 1;
    56   f <<= 1;
    57   if (is_method_interface) f |= 1;
    58   f <<= 1;
    59   if (is_method) f |= 1;
    60   f <<= ConstantPoolCacheEntry::hotSwapBit;
    61   // Preserve existing flag bit values
    62 #ifdef ASSERT
    63   int old_state = ((_flags >> tosBits) & 0x0F);
    64   assert(old_state == 0 || old_state == state,
    65          "inconsistent cpCache flags state");
    66 #endif
    67   return (_flags | f) ;
    68 }
    70 void ConstantPoolCacheEntry::set_bytecode_1(Bytecodes::Code code) {
    71 #ifdef ASSERT
    72   // Read once.
    73   volatile Bytecodes::Code c = bytecode_1();
    74   assert(c == 0 || c == code || code == 0, "update must be consistent");
    75 #endif
    76   // Need to flush pending stores here before bytecode is written.
    77   OrderAccess::release_store_ptr(&_indices, _indices | ((u_char)code << 16));
    78 }
    80 void ConstantPoolCacheEntry::set_bytecode_2(Bytecodes::Code code) {
    81 #ifdef ASSERT
    82   // Read once.
    83   volatile Bytecodes::Code c = bytecode_2();
    84   assert(c == 0 || c == code || code == 0, "update must be consistent");
    85 #endif
    86   // Need to flush pending stores here before bytecode is written.
    87   OrderAccess::release_store_ptr(&_indices, _indices | ((u_char)code << 24));
    88 }
    90 #ifdef ASSERT
    91 // It is possible to have two different dummy methodOops created
    92 // when the resolve code for invoke interface executes concurrently
    93 // Hence the assertion below is weakened a bit for the invokeinterface
    94 // case.
    95 bool ConstantPoolCacheEntry::same_methodOop(oop cur_f1, oop f1) {
    96   return (cur_f1 == f1 || ((methodOop)cur_f1)->name() ==
    97          ((methodOop)f1)->name() || ((methodOop)cur_f1)->signature() ==
    98          ((methodOop)f1)->signature());
    99 }
   100 #endif
   102 // Note that concurrent update of both bytecodes can leave one of them
   103 // reset to zero.  This is harmless; the interpreter will simply re-resolve
   104 // the damaged entry.  More seriously, the memory synchronization is needed
   105 // to flush other fields (f1, f2) completely to memory before the bytecodes
   106 // are updated, lest other processors see a non-zero bytecode but zero f1/f2.
   107 void ConstantPoolCacheEntry::set_field(Bytecodes::Code get_code,
   108                                        Bytecodes::Code put_code,
   109                                        KlassHandle field_holder,
   110                                        int orig_field_index,
   111                                        int field_offset,
   112                                        TosState field_type,
   113                                        bool is_final,
   114                                        bool is_volatile) {
   115   set_f1(field_holder());
   116   set_f2(field_offset);
   117   // The field index is used by jvm/ti and is the index into fields() array
   118   // in holder instanceKlass.  This is scaled by instanceKlass::next_offset.
   119   assert((orig_field_index % instanceKlass::next_offset) == 0, "wierd index");
   120   const int field_index = orig_field_index / instanceKlass::next_offset;
   121   assert(field_index <= field_index_mask,
   122          "field index does not fit in low flag bits");
   123   set_flags(as_flags(field_type, is_final, false, is_volatile, false, false) |
   124             (field_index & field_index_mask));
   125   set_bytecode_1(get_code);
   126   set_bytecode_2(put_code);
   127   NOT_PRODUCT(verify(tty));
   128 }
   130 int  ConstantPoolCacheEntry::field_index() const {
   131   return (_flags & field_index_mask) * instanceKlass::next_offset;
   132 }
   134 void ConstantPoolCacheEntry::set_method(Bytecodes::Code invoke_code,
   135                                         methodHandle method,
   136                                         int vtable_index) {
   138   assert(method->interpreter_entry() != NULL, "should have been set at this point");
   139   assert(!method->is_obsolete(),  "attempt to write obsolete method to cpCache");
   140   bool change_to_virtual = (invoke_code == Bytecodes::_invokeinterface);
   142   int byte_no = -1;
   143   bool needs_vfinal_flag = false;
   144   switch (invoke_code) {
   145     case Bytecodes::_invokedynamic:
   146     case Bytecodes::_invokevirtual:
   147     case Bytecodes::_invokeinterface: {
   148         if (method->can_be_statically_bound()) {
   149           set_f2((intptr_t)method());
   150           needs_vfinal_flag = true;
   151         } else {
   152           assert(vtable_index >= 0, "valid index");
   153           set_f2(vtable_index);
   154         }
   155         byte_no = 2;
   156         break;
   157     }
   158     case Bytecodes::_invokespecial:
   159       // Preserve the value of the vfinal flag on invokevirtual bytecode
   160       // which may be shared with this constant pool cache entry.
   161       needs_vfinal_flag = is_resolved(Bytecodes::_invokevirtual) && is_vfinal();
   162       // fall through
   163     case Bytecodes::_invokestatic:
   164       set_f1(method());
   165       byte_no = 1;
   166       break;
   167     default:
   168       ShouldNotReachHere();
   169       break;
   170   }
   172   set_flags(as_flags(as_TosState(method->result_type()),
   173                      method->is_final_method(),
   174                      needs_vfinal_flag,
   175                      false,
   176                      change_to_virtual,
   177                      true)|
   178             method()->size_of_parameters());
   180   // Note:  byte_no also appears in TemplateTable::resolve.
   181   if (byte_no == 1) {
   182     set_bytecode_1(invoke_code);
   183   } else if (byte_no == 2)  {
   184     if (change_to_virtual) {
   185       // NOTE: THIS IS A HACK - BE VERY CAREFUL!!!
   186       //
   187       // Workaround for the case where we encounter an invokeinterface, but we
   188       // should really have an _invokevirtual since the resolved method is a
   189       // virtual method in java.lang.Object. This is a corner case in the spec
   190       // but is presumably legal. javac does not generate this code.
   191       //
   192       // We set bytecode_1() to _invokeinterface, because that is the
   193       // bytecode # used by the interpreter to see if it is resolved.
   194       // We set bytecode_2() to _invokevirtual.
   195       // See also interpreterRuntime.cpp. (8/25/2000)
   196       // Only set resolved for the invokeinterface case if method is public.
   197       // Otherwise, the method needs to be reresolved with caller for each
   198       // interface call.
   199       if (method->is_public()) set_bytecode_1(invoke_code);
   200       set_bytecode_2(Bytecodes::_invokevirtual);
   201     } else {
   202       set_bytecode_2(invoke_code);
   203     }
   204   } else {
   205     ShouldNotReachHere();
   206   }
   207   NOT_PRODUCT(verify(tty));
   208 }
   211 void ConstantPoolCacheEntry::set_interface_call(methodHandle method, int index) {
   212   klassOop interf = method->method_holder();
   213   assert(instanceKlass::cast(interf)->is_interface(), "must be an interface");
   214   set_f1(interf);
   215   set_f2(index);
   216   set_flags(as_flags(as_TosState(method->result_type()), method->is_final_method(), false, false, false, true) | method()->size_of_parameters());
   217   set_bytecode_1(Bytecodes::_invokeinterface);
   218 }
   221 void ConstantPoolCacheEntry::set_dynamic_call(Handle call_site, int extra_data) {
   222   methodOop method = (methodOop) java_dyn_CallSite::vmmethod(call_site());
   223   assert(method->is_method(), "must be initialized properly");
   224   int param_size = method->size_of_parameters();
   225   assert(param_size >= 1, "method argument size must include MH.this");
   226   param_size -= 1;              // do not count MH.this; it is not stacked for invokedynamic
   227   if (Atomic::cmpxchg_ptr(call_site(), &_f1, NULL) == NULL) {
   228     // racing threads might be trying to install their own favorites
   229     set_f1(call_site());
   230   }
   231   set_f2(extra_data);
   232   set_flags(as_flags(as_TosState(method->result_type()), method->is_final_method(), false, false, false, true) | param_size);
   233   // do not do set_bytecode on a secondary CP cache entry
   234   //set_bytecode_1(Bytecodes::_invokedynamic);
   235 }
   238 class LocalOopClosure: public OopClosure {
   239  private:
   240   void (*_f)(oop*);
   242  public:
   243   LocalOopClosure(void f(oop*))        { _f = f; }
   244   virtual void do_oop(oop* o)          { _f(o); }
   245   virtual void do_oop(narrowOop *o)    { ShouldNotReachHere(); }
   246 };
   249 void ConstantPoolCacheEntry::oops_do(void f(oop*)) {
   250   LocalOopClosure blk(f);
   251   oop_iterate(&blk);
   252 }
   255 void ConstantPoolCacheEntry::oop_iterate(OopClosure* blk) {
   256   assert(in_words(size()) == 4, "check code below - may need adjustment");
   257   // field[1] is always oop or NULL
   258   blk->do_oop((oop*)&_f1);
   259   if (is_vfinal()) {
   260     blk->do_oop((oop*)&_f2);
   261   }
   262 }
   265 void ConstantPoolCacheEntry::oop_iterate_m(OopClosure* blk, MemRegion mr) {
   266   assert(in_words(size()) == 4, "check code below - may need adjustment");
   267   // field[1] is always oop or NULL
   268   if (mr.contains((oop *)&_f1)) blk->do_oop((oop*)&_f1);
   269   if (is_vfinal()) {
   270     if (mr.contains((oop *)&_f2)) blk->do_oop((oop*)&_f2);
   271   }
   272 }
   275 void ConstantPoolCacheEntry::follow_contents() {
   276   assert(in_words(size()) == 4, "check code below - may need adjustment");
   277   // field[1] is always oop or NULL
   278   MarkSweep::mark_and_push((oop*)&_f1);
   279   if (is_vfinal()) {
   280     MarkSweep::mark_and_push((oop*)&_f2);
   281   }
   282 }
   284 #ifndef SERIALGC
   285 void ConstantPoolCacheEntry::follow_contents(ParCompactionManager* cm) {
   286   assert(in_words(size()) == 4, "check code below - may need adjustment");
   287   // field[1] is always oop or NULL
   288   PSParallelCompact::mark_and_push(cm, (oop*)&_f1);
   289   if (is_vfinal()) {
   290     PSParallelCompact::mark_and_push(cm, (oop*)&_f2);
   291   }
   292 }
   293 #endif // SERIALGC
   295 void ConstantPoolCacheEntry::adjust_pointers() {
   296   assert(in_words(size()) == 4, "check code below - may need adjustment");
   297   // field[1] is always oop or NULL
   298   MarkSweep::adjust_pointer((oop*)&_f1);
   299   if (is_vfinal()) {
   300     MarkSweep::adjust_pointer((oop*)&_f2);
   301   }
   302 }
   304 #ifndef SERIALGC
   305 void ConstantPoolCacheEntry::update_pointers() {
   306   assert(in_words(size()) == 4, "check code below - may need adjustment");
   307   // field[1] is always oop or NULL
   308   PSParallelCompact::adjust_pointer((oop*)&_f1);
   309   if (is_vfinal()) {
   310     PSParallelCompact::adjust_pointer((oop*)&_f2);
   311   }
   312 }
   314 void ConstantPoolCacheEntry::update_pointers(HeapWord* beg_addr,
   315                                              HeapWord* end_addr) {
   316   assert(in_words(size()) == 4, "check code below - may need adjustment");
   317   // field[1] is always oop or NULL
   318   PSParallelCompact::adjust_pointer((oop*)&_f1, beg_addr, end_addr);
   319   if (is_vfinal()) {
   320     PSParallelCompact::adjust_pointer((oop*)&_f2, beg_addr, end_addr);
   321   }
   322 }
   323 #endif // SERIALGC
   325 // RedefineClasses() API support:
   326 // If this constantPoolCacheEntry refers to old_method then update it
   327 // to refer to new_method.
   328 bool ConstantPoolCacheEntry::adjust_method_entry(methodOop old_method,
   329        methodOop new_method, bool * trace_name_printed) {
   331   if (is_vfinal()) {
   332     // virtual and final so f2() contains method ptr instead of vtable index
   333     if (f2() == (intptr_t)old_method) {
   334       // match old_method so need an update
   335       _f2 = (intptr_t)new_method;
   336       if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
   337         if (!(*trace_name_printed)) {
   338           // RC_TRACE_MESG macro has an embedded ResourceMark
   339           RC_TRACE_MESG(("adjust: name=%s",
   340             Klass::cast(old_method->method_holder())->external_name()));
   341           *trace_name_printed = true;
   342         }
   343         // RC_TRACE macro has an embedded ResourceMark
   344         RC_TRACE(0x00400000, ("cpc vf-entry update: %s(%s)",
   345           new_method->name()->as_C_string(),
   346           new_method->signature()->as_C_string()));
   347       }
   349       return true;
   350     }
   352     // f1() is not used with virtual entries so bail out
   353     return false;
   354   }
   356   if ((oop)_f1 == NULL) {
   357     // NULL f1() means this is a virtual entry so bail out
   358     // We are assuming that the vtable index does not need change.
   359     return false;
   360   }
   362   if ((oop)_f1 == old_method) {
   363     _f1 = new_method;
   364     if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
   365       if (!(*trace_name_printed)) {
   366         // RC_TRACE_MESG macro has an embedded ResourceMark
   367         RC_TRACE_MESG(("adjust: name=%s",
   368           Klass::cast(old_method->method_holder())->external_name()));
   369         *trace_name_printed = true;
   370       }
   371       // RC_TRACE macro has an embedded ResourceMark
   372       RC_TRACE(0x00400000, ("cpc entry update: %s(%s)",
   373         new_method->name()->as_C_string(),
   374         new_method->signature()->as_C_string()));
   375     }
   377     return true;
   378   }
   380   return false;
   381 }
   383 bool ConstantPoolCacheEntry::is_interesting_method_entry(klassOop k) {
   384   if (!is_method_entry()) {
   385     // not a method entry so not interesting by default
   386     return false;
   387   }
   389   methodOop m = NULL;
   390   if (is_vfinal()) {
   391     // virtual and final so _f2 contains method ptr instead of vtable index
   392     m = (methodOop)_f2;
   393   } else if ((oop)_f1 == NULL) {
   394     // NULL _f1 means this is a virtual entry so also not interesting
   395     return false;
   396   } else {
   397     if (!((oop)_f1)->is_method()) {
   398       // _f1 can also contain a klassOop for an interface
   399       return false;
   400     }
   401     m = (methodOop)_f1;
   402   }
   404   assert(m != NULL && m->is_method(), "sanity check");
   405   if (m == NULL || !m->is_method() || m->method_holder() != k) {
   406     // robustness for above sanity checks or method is not in
   407     // the interesting class
   408     return false;
   409   }
   411   // the method is in the interesting class so the entry is interesting
   412   return true;
   413 }
   415 void ConstantPoolCacheEntry::print(outputStream* st, int index) const {
   416   // print separator
   417   if (index == 0) tty->print_cr("                 -------------");
   418   // print entry
   419   tty->print_cr("%3d  (%08x)  ", index, this);
   420   if (is_secondary_entry())
   421     tty->print_cr("[%5d|secondary]", main_entry_index());
   422   else
   423     tty->print_cr("[%02x|%02x|%5d]", bytecode_2(), bytecode_1(), constant_pool_index());
   424   tty->print_cr("                 [   %08x]", (address)(oop)_f1);
   425   tty->print_cr("                 [   %08x]", _f2);
   426   tty->print_cr("                 [   %08x]", _flags);
   427   tty->print_cr("                 -------------");
   428 }
   430 void ConstantPoolCacheEntry::verify(outputStream* st) const {
   431   // not implemented yet
   432 }
   434 // Implementation of ConstantPoolCache
   436 void constantPoolCacheOopDesc::initialize(intArray& inverse_index_map) {
   437   assert(inverse_index_map.length() == length(), "inverse index map must have same length as cache");
   438   for (int i = 0; i < length(); i++) {
   439     ConstantPoolCacheEntry* e = entry_at(i);
   440     int original_index = inverse_index_map[i];
   441     if ((original_index & Rewriter::_secondary_entry_tag) != 0) {
   442       int main_index = (original_index - Rewriter::_secondary_entry_tag);
   443       assert(!entry_at(main_index)->is_secondary_entry(), "valid main index");
   444       e->initialize_secondary_entry(main_index);
   445     } else {
   446       e->initialize_entry(original_index);
   447     }
   448     assert(entry_at(i) == e, "sanity");
   449   }
   450 }
   452 // RedefineClasses() API support:
   453 // If any entry of this constantPoolCache points to any of
   454 // old_methods, replace it with the corresponding new_method.
   455 void constantPoolCacheOopDesc::adjust_method_entries(methodOop* old_methods, methodOop* new_methods,
   456                                                      int methods_length, bool * trace_name_printed) {
   458   if (methods_length == 0) {
   459     // nothing to do if there are no methods
   460     return;
   461   }
   463   // get shorthand for the interesting class
   464   klassOop old_holder = old_methods[0]->method_holder();
   466   for (int i = 0; i < length(); i++) {
   467     if (!entry_at(i)->is_interesting_method_entry(old_holder)) {
   468       // skip uninteresting methods
   469       continue;
   470     }
   472     // The constantPoolCache contains entries for several different
   473     // things, but we only care about methods. In fact, we only care
   474     // about methods in the same class as the one that contains the
   475     // old_methods. At this point, we have an interesting entry.
   477     for (int j = 0; j < methods_length; j++) {
   478       methodOop old_method = old_methods[j];
   479       methodOop new_method = new_methods[j];
   481       if (entry_at(i)->adjust_method_entry(old_method, new_method,
   482           trace_name_printed)) {
   483         // current old_method matched this entry and we updated it so
   484         // break out and get to the next interesting entry if there one
   485         break;
   486       }
   487     }
   488   }
   489 }

mercurial