1 /*
   2  * Copyright (c) 1998, 2010, 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  */
  24 
  25 #ifndef SHARE_VM_COMPILER_OOPMAP_HPP
  26 #define SHARE_VM_COMPILER_OOPMAP_HPP
  27 
  28 #include "code/compressedStream.hpp"
  29 #include "code/vmreg.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "utilities/growableArray.hpp"
  32 
  33 // Interface for generating the frame map for compiled code.  A frame map
  34 // describes for a specific pc whether each register and frame stack slot is:
  35 //   Oop         - A GC root for current frame
  36 //   Dead        - Dead; can be Zapped for debugging
  37 //   CalleeXX    - Callee saved; also describes which caller register is saved
  38 //   DerivedXX   - A derived oop; original oop is described.
  39 //
  40 // OopMapValue describes a single OopMap entry
  41 
  42 class frame;
  43 class RegisterMap;
  44 class DerivedPointerEntry;
  45 
  46 class OopMapValue: public StackObj {
  47   friend class VMStructs;
  48 private:
  49   short _value;
  50   int value() const                                 { return _value; }
  51   void set_value(int value)                         { _value = value; }
  52   short _content_reg;
  53 
  54 public:
  55   // Constants
  56   enum { type_bits                = 4,
  57          register_bits            = BitsPerShort - type_bits };
  58 
  59   enum { type_shift               = 0,
  60          register_shift           = type_bits };
  61 
  62   enum { type_mask                = right_n_bits(type_bits),
  63          type_mask_in_place       = type_mask << type_shift,
  64          register_mask            = right_n_bits(register_bits),
  65          register_mask_in_place   = register_mask << register_shift };
  66 
  67   enum oop_types {              // must fit in type_bits
  68          unused_value =0,       // powers of 2, for masking OopMapStream
  69          oop_value = 1,
  70          narrowoop_value = 2,
  71          callee_saved_value = 4,
  72          derived_oop_value= 8 };
  73 
  74   // Constructors
  75   OopMapValue () { set_value(0); set_content_reg(VMRegImpl::Bad()); }
  76   OopMapValue (VMReg reg, oop_types t) { set_reg_type(reg, t); set_content_reg(VMRegImpl::Bad()); }
  77   OopMapValue (VMReg reg, oop_types t, VMReg reg2) { set_reg_type(reg, t); set_content_reg(reg2); }
  78   OopMapValue (CompressedReadStream* stream) { read_from(stream); }
  79 
  80   // Archiving
  81   void write_on(CompressedWriteStream* stream) {
  82     stream->write_int(value());
  83     if(is_callee_saved() || is_derived_oop()) {
  84       stream->write_int(content_reg()->value());
  85     }
  86   }
  87 
  88   void read_from(CompressedReadStream* stream) {
  89     set_value(stream->read_int());
  90     if (is_callee_saved() || is_derived_oop()) {
  91       set_content_reg(VMRegImpl::as_VMReg(stream->read_int(), true));
  92     }
  93   }
  94 
  95   // Querying
  96   bool is_oop()               { return mask_bits(value(), type_mask_in_place) == oop_value; }
  97   bool is_narrowoop()           { return mask_bits(value(), type_mask_in_place) == narrowoop_value; }
  98   bool is_callee_saved()      { return mask_bits(value(), type_mask_in_place) == callee_saved_value; }
  99   bool is_derived_oop()       { return mask_bits(value(), type_mask_in_place) == derived_oop_value; }
 100 
 101   void set_oop()              { set_value((value() & register_mask_in_place) | oop_value); }
 102   void set_narrowoop()          { set_value((value() & register_mask_in_place) | narrowoop_value); }
 103   void set_callee_saved()     { set_value((value() & register_mask_in_place) | callee_saved_value); }
 104   void set_derived_oop()      { set_value((value() & register_mask_in_place) | derived_oop_value); }
 105 
 106   VMReg reg() const { return VMRegImpl::as_VMReg(mask_bits(value(), register_mask_in_place) >> register_shift); }
 107   oop_types type() const      { return (oop_types)mask_bits(value(), type_mask_in_place); }
 108 
 109   static bool legal_vm_reg_name(VMReg p) {
 110     return (p->value()  == (p->value() & register_mask));
 111   }
 112 
 113   void set_reg_type(VMReg p, oop_types t) {
 114     set_value((p->value() << register_shift) | t);
 115     assert(reg() == p, "sanity check" );
 116     assert(type() == t, "sanity check" );
 117   }
 118 
 119 
 120   VMReg content_reg() const       { return VMRegImpl::as_VMReg(_content_reg, true); }
 121   void set_content_reg(VMReg r)   { _content_reg = r->value(); }
 122 
 123   // Physical location queries
 124   bool is_register_loc()      { return reg()->is_reg(); }
 125   bool is_stack_loc()         { return reg()->is_stack(); }
 126 
 127   // Returns offset from sp.
 128   int stack_offset() {
 129     assert(is_stack_loc(), "must be stack location");
 130     return reg()->reg2stack();
 131   }
 132 
 133   void print_on(outputStream* st) const;
 134   void print() const { print_on(tty); }
 135 };
 136 
 137 
 138 class OopMap: public ResourceObj {
 139   friend class OopMapStream;
 140   friend class VMStructs;
 141  private:
 142   int  _pc_offset; // offset in the code that this OopMap corresponds to
 143   int  _omv_count; // number of OopMapValues in the stream
 144   CompressedWriteStream* _write_stream;
 145 
 146   debug_only( OopMapValue::oop_types* _locs_used; int _locs_length;)
 147 
 148   // Accessors
 149   int omv_count() const                       { return _omv_count; }
 150   void set_omv_count(int value)               { _omv_count = value; }
 151   void increment_count()                      { _omv_count++; }
 152   CompressedWriteStream* write_stream() const { return _write_stream; }
 153   void set_write_stream(CompressedWriteStream* value) { _write_stream = value; }
 154 
 155  private:
 156   enum DeepCopyToken { _deep_copy_token };
 157   OopMap(DeepCopyToken, OopMap* source);  // used only by deep_copy
 158 
 159  public:
 160   OopMap(int frame_size, int arg_count);
 161 
 162   // pc-offset handling
 163   int offset() const     { return _pc_offset; }
 164   void set_offset(int o) { _pc_offset = o; }
 165   int count() const { return _omv_count; }
 166   int data_size() const  { return write_stream()->position(); }
 167   address data() const { return write_stream()->buffer(); }
 168 
 169   // Check to avoid double insertion
 170   debug_only(OopMapValue::oop_types locs_used( int indx ) { return _locs_used[indx]; })
 171 
 172   // Construction
 173   // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd
 174   // slots to hold 4-byte values like ints and floats in the LP64 build.
 175   void set_oop  ( VMReg local);
 176   void set_value( VMReg local);
 177   void set_narrowoop(VMReg local);
 178   void set_dead ( VMReg local);
 179   void set_callee_saved( VMReg local, VMReg caller_machine_register );
 180   void set_derived_oop ( VMReg local, VMReg derived_from_local_register );
 181   void set_xxx(VMReg reg, OopMapValue::oop_types x, VMReg optional);
 182 
 183   int heap_size() const;
 184   void copy_data_to(address addr) const;
 185   OopMap* deep_copy();
 186 
 187   bool legal_vm_reg_name(VMReg local) {
 188      return OopMapValue::legal_vm_reg_name(local);
 189   }
 190 
 191   // Printing
 192   void print_on(outputStream* st) const;
 193   void print() const { print_on(tty); }
 194   bool equals(const OopMap* other) const;
 195 };
 196 
 197 
 198 class OopMapSet : public ResourceObj {
 199   friend class VMStructs;
 200  private:
 201   int _om_count;
 202   int _om_size;
 203   OopMap** _om_data;
 204 
 205   int om_count() const              { return _om_count; }
 206   void set_om_count(int value)      { _om_count = value; }
 207   void increment_count()            { _om_count++; }
 208   int om_size() const               { return _om_size; }
 209   void set_om_size(int value)       { _om_size = value; }
 210   OopMap** om_data() const          { return _om_data; }
 211   void set_om_data(OopMap** value)  { _om_data = value; }
 212   void grow_om_data();
 213   void set(int index,OopMap* value) { assert((index == 0) || ((index > 0) && (index < om_size())),"bad index"); _om_data[index] = value; }
 214 
 215  public:
 216   OopMapSet();
 217 
 218   // returns the number of OopMaps in this OopMapSet
 219   int size() const            { return _om_count; }
 220   // returns the OopMap at a given index
 221   OopMap* at(int index) const { assert((index >= 0) && (index <= om_count()),"bad index"); return _om_data[index]; }
 222 
 223   // Collect OopMaps.
 224   void add_gc_map(int pc, OopMap* map);
 225 
 226   // Returns the only oop map. Used for reconstructing
 227   // Adapter frames during deoptimization
 228   OopMap* singular_oop_map();
 229 
 230   // returns OopMap in that is anchored to the pc
 231   OopMap* find_map_at_offset(int pc_offset) const;
 232 
 233   int heap_size() const;
 234 
 235   // Methods oops_do() and all_do() filter out NULL oops and
 236   // oop == Universe::narrow_oop_base() before passing oops
 237   // to closures.
 238 
 239   // Iterates through frame for a compiled method
 240   static void oops_do            (const frame* fr,
 241                                   const RegisterMap* reg_map, OopClosure* f);
 242   static void update_register_map(const frame* fr, RegisterMap *reg_map);
 243 
 244   // Iterates through frame for a compiled method for dead ones and values, too
 245   static void all_do(const frame* fr, const RegisterMap* reg_map,
 246                      OopClosure* oop_fn,
 247                      void derived_oop_fn(oop* base, oop* derived),
 248                      OopClosure* value_fn);
 249 
 250   // Printing
 251   void print_on(outputStream* st) const;
 252   void print() const { print_on(tty); }
 253 };
 254 
 255 class ImmutableOopMapBuilder;
 256 
 257 class ImmutableOopMap {
 258   friend class OopMapStream;
 259   friend class VMStructs;
 260 #ifdef ASSERT
 261   friend class ImmutableOopMapBuilder;
 262 #endif
 263 private:
 264   int _count; // contains the number of entries in this OopMap
 265 
 266   address data_addr() const { return (address) this + sizeof(ImmutableOopMap); }
 267 public:
 268   ImmutableOopMap(const OopMap* oopmap);
 269 
 270   bool has_derived_pointer() const PRODUCT_RETURN0;
 271   int count() const { return _count; }
 272 #ifdef ASSERT
 273   int nr_of_bytes() const; // this is an expensive operation, only used in debug builds
 274 #endif
 275 
 276   // Printing
 277   void print_on(outputStream* st) const;
 278   void print() const { print_on(tty); }
 279 };
 280 
 281 class ImmutableOopMapSet;
 282 class ImmutableOopMap;
 283 class OopMapSet;
 284 
 285 class ImmutableOopMapPair {
 286   friend class VMStructs;
 287 private:
 288   int _pc_offset; // program counter offset from the beginning of the method
 289   int _oopmap_offset; // offset in the data in the ImmutableOopMapSet where the ImmutableOopMap is located
 290 public:
 291   ImmutableOopMapPair(int pc_offset, int oopmap_offset) : _pc_offset(pc_offset), _oopmap_offset(oopmap_offset) {
 292     assert(pc_offset >= 0 && oopmap_offset >= 0, "check");
 293   }
 294   const ImmutableOopMap* get_from(const ImmutableOopMapSet* set) const;
 295 
 296   int pc_offset() const { return _pc_offset; }
 297   int oopmap_offset() const { return _oopmap_offset; }
 298 };
 299 
 300 class ImmutableOopMapSet {
 301   friend class VMStructs;
 302 private:
 303   int _count; // nr of ImmutableOopMapPairs in the Set
 304   int _size; // nr of bytes including ImmutableOopMapSet itself
 305 
 306   address data() const { return (address) this + sizeof(*this) + sizeof(ImmutableOopMapPair) * _count; }
 307 
 308 public:
 309   ImmutableOopMapSet(const OopMapSet* oopmap_set, int size) : _count(oopmap_set->size()), _size(size) {}
 310 
 311   ImmutableOopMap* oopmap_at_offset(int offset) const {
 312     assert(offset >= 0 && offset < _size, "must be within boundaries");
 313     address addr = data() + offset;
 314     return (ImmutableOopMap*) addr;
 315   }
 316 
 317   ImmutableOopMapPair* get_pairs() const { return (ImmutableOopMapPair*) ((address) this + sizeof(*this)); }
 318 
 319   static ImmutableOopMapSet* build_from(const OopMapSet* oopmap_set);
 320 
 321   const ImmutableOopMap* find_map_at_offset(int pc_offset) const;
 322 
 323   const ImmutableOopMapPair* pair_at(int index) const { assert(index >= 0 && index < _count, "check"); return &get_pairs()[index]; }
 324 
 325   int count() const { return _count; }
 326   int nr_of_bytes() const { return _size; }
 327 
 328   void print_on(outputStream* st) const;
 329   void print() const { print_on(tty); }
 330 };
 331 
 332 class OopMapStream : public StackObj {
 333  private:
 334   CompressedReadStream* _stream;
 335   int _mask;
 336   int _size;
 337   int _position;
 338   bool _valid_omv;
 339   OopMapValue _omv;
 340   void find_next();
 341 
 342  public:
 343   OopMapStream(OopMap* oop_map, int oop_types_mask = OopMapValue::type_mask_in_place);
 344   OopMapStream(const ImmutableOopMap* oop_map, int oop_types_mask = OopMapValue::type_mask_in_place);
 345   bool is_done()                        { if(!_valid_omv) { find_next(); } return !_valid_omv; }
 346   void next()                           { find_next(); }
 347   OopMapValue current()                 { return _omv; }
 348 #ifdef ASSERT
 349   int stream_position() const           { return _stream->position(); }
 350 #endif
 351 };
 352 
 353 
 354 // Derived pointer support. This table keeps track of all derived points on a
 355 // stack.  It is cleared before each scavenge/GC.  During the traversal of all
 356 // oops, it is filled in with references to all locations that contains a
 357 // derived oop (assumed to be very few).  When the GC is complete, the derived
 358 // pointers are updated based on their base pointers new value and an offset.
 359 #ifdef COMPILER2
 360 class DerivedPointerTable : public AllStatic {
 361   friend class VMStructs;
 362  private:
 363    static GrowableArray<DerivedPointerEntry*>* _list;
 364    static bool _active;                      // do not record pointers for verify pass etc.
 365  public:
 366   static void clear();                       // Called before scavenge/GC
 367   static void add(oop *derived, oop *base);  // Called during scavenge/GC
 368   static void update_pointers();             // Called after  scavenge/GC
 369   static bool is_empty()                     { return _list == NULL || _list->is_empty(); }
 370   static bool is_active()                    { return _active; }
 371   static void set_active(bool value)         { _active = value; }
 372 };
 373 
 374 // A utility class to temporarily "deactivate" the DerivedPointerTable.
 375 // (Note: clients are responsible for any MT-safety issues)
 376 class DerivedPointerTableDeactivate: public StackObj {
 377  private:
 378   bool _active;
 379  public:
 380   DerivedPointerTableDeactivate() {
 381     _active = DerivedPointerTable::is_active();
 382     if (_active) {
 383       DerivedPointerTable::set_active(false);
 384     }
 385   }
 386 
 387   ~DerivedPointerTableDeactivate() {
 388     assert(!DerivedPointerTable::is_active(),
 389            "Inconsistency: not MT-safe");
 390     if (_active) {
 391       DerivedPointerTable::set_active(true);
 392     }
 393   }
 394 };
 395 #endif // COMPILER2
 396 
 397 #endif // SHARE_VM_COMPILER_OOPMAP_HPP