1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)cpCacheOop.hpp       1.74 07/05/29 09:44:19 JVM"
   3 #endif
   4 /*
   5  * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 // A ConstantPoolCacheEntry describes an individual entry of the constant
  29 // pool cache. There's 2 principal kinds of entries: field entries for in-
  30 // stance & static field access, and method entries for invokes. Some of
  31 // the entry layout is shared and looks as follows:
  32 //
  33 // bit number |31                0|
  34 // bit length |-8--|-8--|---16----|
  35 // --------------------------------
  36 // _indices   [ b2 | b1 |  index  ]
  37 // _f1        [  entry specific   ]
  38 // _f2        [  entry specific   ]
  39 // _flags     [t|f|vf|v|m|h|unused|field_index] (for field entries)
  40 // bit length |4|1|1 |1|1|0|---7--|----16-----]
  41 // _flags     [t|f|vf|v|m|h|unused|eidx|psze] (for method entries)
  42 // bit length |4|1|1 |1|1|1|---7--|-8--|-8--]
  43 
  44 // --------------------------------
  45 //
  46 // with:
  47 // index  = original constant pool index
  48 // b1     = bytecode 1
  49 // b2     = bytecode 2
  50 // psze   = parameters size (method entries only)
  51 // eidx   = interpreter entry index (method entries only)
  52 // field_index = index into field information in holder instanceKlass
  53 //          The index max is 0xffff (max number of fields in constant pool)
  54 //          and is multiplied by (instanceKlass::next_offset) when accessing.
  55 // t      = TosState (see below)
  56 // f      = field is marked final (see below)
  57 // vf     = virtual, final (method entries only : is_vfinal())
  58 // v      = field is volatile (see below)
  59 // m      = invokeinterface used for method in class Object (see below)
  60 // h      = RedefineClasses/Hotswap bit (see below)
  61 //
  62 // The flags after TosState have the following interpretation:
  63 // bit 27: f flag  true if field is marked final
  64 // bit 26: vf flag true if virtual final method
  65 // bit 25: v flag true if field is volatile (only for fields)
  66 // bit 24: m flag true if invokeinterface used for method in class Object
  67 // bit 23: 0 for fields, 1 for methods
  68 //
  69 // The flags 31, 30, 29, 28 together build a 4 bit number 0 to 8 with the
  70 // following mapping to the TosState states:
  71 //
  72 // btos: 0
  73 // ctos: 1
  74 // stos: 2
  75 // itos: 3
  76 // ltos: 4
  77 // ftos: 5
  78 // dtos: 6
  79 // atos: 7
  80 // vtos: 8
  81 //
  82 // Entry specific: field entries:
  83 // _indices = get (b1 section) and put (b2 section) bytecodes, original constant pool index
  84 // _f1      = field holder
  85 // _f2      = field offset in words
  86 // _flags   = field type information, original field index in field holder
  87 //            (field_index section)
  88 //
  89 // Entry specific: method entries:
  90 // _indices = invoke code for f1 (b1 section), invoke code for f2 (b2 section),
  91 //            original constant pool index
  92 // _f1      = method for all but virtual calls, unused by virtual calls
  93 //            (note: for interface calls, which are essentially virtual,
  94 //             contains klassOop for the corresponding interface.
  95 // _f2      = method/vtable index for virtual calls only, unused by all other
  96 //            calls.  The vf flag indicates this is a method pointer not an
  97 //            index.
  98 // _flags   = field type info (f section),
  99 //            virtual final entry (vf),
 100 //            interpreter entry index (eidx section),
 101 //            parameter size (psze section)
 102 //
 103 // Note: invokevirtual & invokespecial bytecodes can share the same constant
 104 //       pool entry and thus the same constant pool cache entry. All invoke
 105 //       bytecodes but invokevirtual use only _f1 and the corresponding b1
 106 //       bytecode, while invokevirtual uses only _f2 and the corresponding
 107 //       b2 bytecode.  The value of _flags is shared for both types of entries.
 108 //
 109 // The fields are volatile so that they are stored in the order written in the
 110 // source code.  The _indices field with the bytecode must be written last.
 111 
 112 class ConstantPoolCacheEntry VALUE_OBJ_CLASS_SPEC {
 113   friend class VMStructs;
 114  private:
 115   volatile intx     _indices;  // constant pool index & rewrite bytecodes
 116   volatile oop      _f1;       // entry specific oop field
 117   volatile intx     _f2;       // entry specific int/oop field
 118   volatile intx     _flags;    // flags
 119 
 120 
 121 #ifdef ASSERT
 122   bool same_methodOop(oop cur_f1, oop f1);
 123 #endif
 124 
 125   void set_bytecode_1(Bytecodes::Code code);
 126   void set_bytecode_2(Bytecodes::Code code);
 127   void set_f1(oop f1)                            {
 128     oop existing_f1 = _f1; // read once
 129     assert(existing_f1 == NULL || existing_f1 == f1, "illegal field change");
 130     oop_store(&_f1, f1);
 131   }
 132   void set_f2(intx f2)                           { assert(_f2 == 0    || _f2 == f2, "illegal field change"); _f2 = f2; }
 133   int as_flags(TosState state, bool is_final, bool is_vfinal, bool is_volatile,
 134                bool is_method_interface, bool is_method);
 135   void set_flags(intx flags)                     { _flags = flags; }
 136 
 137  public:
 138   // specific bit values in flag field
 139   // Note: the interpreter knows this layout!
 140   enum FlagBitValues {
 141     hotSwapBit    = 23,
 142     methodInterface = 24,
 143     volatileField = 25,
 144     vfinalMethod  = 26,
 145     finalField    = 27
 146   }; 
 147 
 148   enum { field_index_mask = 0xFFFF };
 149 
 150   // start of type bits in flags
 151   // Note: the interpreter knows this layout!
 152   enum FlagValues {
 153     tosBits      = 28 
 154   };
 155 
 156   // Initialization
 157   void set_initial_state(int index);             // sets entry to initial state
 158 
 159   void set_field(                                // sets entry to resolved field state
 160     Bytecodes::Code get_code,                    // the bytecode used for reading the field
 161     Bytecodes::Code put_code,                    // the bytecode used for writing the field
 162     KlassHandle     field_holder,                // the object/klass holding the field
 163     int             orig_field_index,            // the original field index in the field holder
 164     int             field_offset,                // the field offset in words in the field holder
 165     TosState        field_type,                  // the (machine) field type    
 166     bool            is_final,                     // the field is final 
 167     bool            is_volatile                  // the field is volatile 
 168   );
 169 
 170   void set_method(                               // sets entry to resolved method entry
 171     Bytecodes::Code invoke_code,                 // the bytecode used for invoking the method
 172     methodHandle    method,                      // the method/prototype if any (NULL, otherwise)
 173     int             vtable_index                 // the vtable index if any, else negative
 174   );
 175 
 176   void set_interface_call(
 177     methodHandle method,                         // Resolved method    
 178     int index                                    // Method index into interface
 179   );               
 180 
 181   void set_parameter_size(int value) {
 182     assert(parameter_size() == 0 || parameter_size() == value, 
 183            "size must not change");
 184     // Setting the parameter size by itself is only safe if the
 185     // current value of _flags is 0, otherwise another thread may have
 186     // updated it and we don't want to overwrite that value.  Don't
 187     // bother trying to update it once it's nonzero but always make
 188     // sure that the final parameter size agrees with what was passed.
 189     if (_flags == 0) {
 190       Atomic::cmpxchg_ptr((value & 0xFF), &_flags, 0);
 191     }
 192     guarantee(parameter_size() == value, "size must not change");
 193   }
 194 
 195   // Which bytecode number (1 or 2) in the index field is valid for this bytecode?
 196   // Returns -1 if neither is valid.
 197   static int bytecode_number(Bytecodes::Code code) {
 198     switch (code) {
 199       case Bytecodes::_getstatic       :    // fall through
 200       case Bytecodes::_getfield        :    // fall through
 201       case Bytecodes::_invokespecial   :    // fall through
 202       case Bytecodes::_invokestatic    :    // fall through
 203       case Bytecodes::_invokeinterface : return 1;
 204       case Bytecodes::_putstatic       :    // fall through
 205       case Bytecodes::_putfield        :    // fall through
 206       case Bytecodes::_invokevirtual   : return 2;
 207       default                          : break;
 208     }
 209     return -1;
 210   }
 211 
 212   // Has this bytecode been resolved? Only valid for invokes and get/put field/static.
 213   bool is_resolved(Bytecodes::Code code) const {
 214     switch (bytecode_number(code)) {
 215       case 1:  return (bytecode_1() == code);
 216       case 2:  return (bytecode_2() == code);
 217     }
 218     return false;      // default: not resolved
 219   }
 220 
 221   // Accessors
 222   int constant_pool_index() const                { return _indices & 0xFFFF; }
 223   Bytecodes::Code bytecode_1() const             { return Bytecodes::cast((_indices >> 16) & 0xFF); }
 224   Bytecodes::Code bytecode_2() const             { return Bytecodes::cast((_indices >> 24) & 0xFF); }
 225   volatile oop  f1() const                       { return _f1; }
 226   intx f2() const                                { return _f2; }
 227   int  field_index() const;
 228   int  parameter_size() const                    { return _flags & 0xFF; }
 229   bool is_vfinal() const                         { return ((_flags & (1 << vfinalMethod)) == (1 << vfinalMethod)); }
 230   bool is_volatile() const                       { return ((_flags & (1 << volatileField)) == (1 << volatileField)); }
 231   bool is_methodInterface() const                { return ((_flags & (1 << methodInterface)) == (1 << methodInterface)); }
 232   bool is_byte() const                           { return (((uintx) _flags >> tosBits) == btos); }
 233   bool is_char() const                           { return (((uintx) _flags >> tosBits) == ctos); }
 234   bool is_short() const                          { return (((uintx) _flags >> tosBits) == stos); }
 235   bool is_int() const                            { return (((uintx) _flags >> tosBits) == itos); }
 236   bool is_long() const                           { return (((uintx) _flags >> tosBits) == ltos); }
 237   bool is_float() const                          { return (((uintx) _flags >> tosBits) == ftos); }
 238   bool is_double() const                         { return (((uintx) _flags >> tosBits) == dtos); }
 239   bool is_object() const                         { return (((uintx) _flags >> tosBits) == atos); }
 240   TosState flag_state() const                    { assert( ( (_flags >> tosBits) & 0x0F ) < number_of_states, "Invalid state in as_flags");
 241                                                    return (TosState)((_flags >> tosBits) & 0x0F); }
 242 
 243   // Code generation support
 244   static WordSize size()                         { return in_WordSize(sizeof(ConstantPoolCacheEntry) / HeapWordSize); }
 245   static ByteSize indices_offset()               { return byte_offset_of(ConstantPoolCacheEntry, _indices); }
 246   static ByteSize f1_offset()                    { return byte_offset_of(ConstantPoolCacheEntry, _f1); }
 247   static ByteSize f2_offset()                    { return byte_offset_of(ConstantPoolCacheEntry, _f2); }
 248   static ByteSize flags_offset()                 { return byte_offset_of(ConstantPoolCacheEntry, _flags); }
 249 
 250   // GC Support
 251   void oops_do(void f(oop*));
 252   void oop_iterate(OopClosure* blk);
 253   void oop_iterate_m(OopClosure* blk, MemRegion mr);
 254   void follow_contents();
 255   void adjust_pointers();
 256 
 257 #ifndef SERIALGC
 258   // Parallel Old
 259   void follow_contents(ParCompactionManager* cm);
 260 #endif // SERIALGC
 261 
 262   void update_pointers();
 263   void update_pointers(HeapWord* beg_addr, HeapWord* end_addr);
 264 
 265   // RedefineClasses() API support:
 266   // If this constantPoolCacheEntry refers to old_method then update it
 267   // to refer to new_method.
 268   // trace_name_printed is set to true if the current call has
 269   // printed the klass name so that other routines in the adjust_*
 270   // group don't print the klass name.
 271   bool adjust_method_entry(methodOop old_method, methodOop new_method,
 272          bool * trace_name_printed);
 273   bool is_interesting_method_entry(klassOop k);
 274   bool is_field_entry() const                    { return (_flags & (1 << hotSwapBit)) == 0; }
 275   bool is_method_entry() const                   { return (_flags & (1 << hotSwapBit)) != 0; }
 276 
 277   // Debugging & Printing
 278   void print (outputStream* st, int index) const;
 279   void verify(outputStream* st) const;
 280 
 281   static void verify_tosBits() {
 282     assert(tosBits == 28, "interpreter now assumes tosBits is 28");
 283   }
 284 };
 285 
 286 
 287 // A constant pool cache is a runtime data structure set aside to a constant pool. The cache
 288 // holds interpreter runtime information for all field access and invoke bytecodes. The cache
 289 // is created and initialized before a class is actively used (i.e., initialized), the indivi-
 290 // dual cache entries are filled at resolution (i.e., "link") time (see also: rewriter.*).
 291 
 292 class constantPoolCacheOopDesc: public arrayOopDesc {
 293   friend class VMStructs;
 294  private:
 295   constantPoolOop _constant_pool;                // the corresponding constant pool
 296 
 297   // Sizing
 298   static int header_size()                       { return sizeof(constantPoolCacheOopDesc) / HeapWordSize; }
 299   static int object_size(int length)             { return align_object_size(header_size() + length * in_words(ConstantPoolCacheEntry::size())); }
 300   int object_size()                              { return object_size(length()); }
 301 
 302   // Helpers
 303   constantPoolOop*        constant_pool_addr()   { return &_constant_pool; }
 304   ConstantPoolCacheEntry* base() const           { return (ConstantPoolCacheEntry*)((address)this + in_bytes(base_offset())); }
 305 
 306   friend class constantPoolCacheKlass;
 307 
 308  public:
 309   // Initialization
 310   void initialize(intArray& inverse_index_map);
 311 
 312   // Accessors
 313   void set_constant_pool(constantPoolOop pool)   { oop_store_without_check((oop*)&_constant_pool, (oop)pool); }
 314   constantPoolOop constant_pool() const          { return _constant_pool; }
 315   ConstantPoolCacheEntry* entry_at(int i) const  { assert(0 <= i && i < length(), "index out of bounds"); return base() + i; }
 316 
 317   // Code generation
 318   static ByteSize base_offset()                  { return in_ByteSize(sizeof(constantPoolCacheOopDesc)); }
 319 
 320   // RedefineClasses() API support:
 321   // If any entry of this constantPoolCache points to any of
 322   // old_methods, replace it with the corresponding new_method.
 323   // trace_name_printed is set to true if the current call has
 324   // printed the klass name so that other routines in the adjust_*
 325   // group don't print the klass name.
 326   void adjust_method_entries(methodOop* old_methods, methodOop* new_methods,
 327                              int methods_length, bool * trace_name_printed);
 328 };
 329