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