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                = 5,
  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          value_value = 2,
  72          narrowoop_value = 4,
  73          callee_saved_value = 8,
  74          derived_oop_value= 16 };
  75 
  76   // Constructors
  77   OopMapValue () { set_value(0); set_content_reg(VMRegImpl::Bad()); }
  78   OopMapValue (VMReg reg, oop_types t) { set_reg_type(reg,t); }
  79   OopMapValue (VMReg reg, oop_types t, VMReg reg2) { set_reg_type(reg,t); set_content_reg(reg2); }
  80   OopMapValue (CompressedReadStream* stream) { read_from(stream); }
  81 
  82   // Archiving
  83   void write_on(CompressedWriteStream* stream) {
  84     stream->write_int(value());
  85     if(is_callee_saved() || is_derived_oop()) {
  86       stream->write_int(content_reg()->value());
  87     }
  88   }
  89 
  90   void read_from(CompressedReadStream* stream) {
  91     set_value(stream->read_int());
  92     if(is_callee_saved() || is_derived_oop()) {
  93       set_content_reg(VMRegImpl::as_VMReg(stream->read_int(), true));
  94     }
  95   }
  96 
  97   // Querying
  98   bool is_oop()               { return mask_bits(value(), type_mask_in_place) == oop_value; }
  99   bool is_value()             { return mask_bits(value(), type_mask_in_place) == value_value; }
 100   bool is_narrowoop()           { return mask_bits(value(), type_mask_in_place) == narrowoop_value; }
 101   bool is_callee_saved()      { return mask_bits(value(), type_mask_in_place) == callee_saved_value; }
 102   bool is_derived_oop()       { return mask_bits(value(), type_mask_in_place) == derived_oop_value; }
 103 
 104   void set_oop()              { set_value((value() & register_mask_in_place) | oop_value); }
 105   void set_value()            { set_value((value() & register_mask_in_place) | value_value); }
 106   void set_narrowoop()          { set_value((value() & register_mask_in_place) | narrowoop_value); }
 107   void set_callee_saved()     { set_value((value() & register_mask_in_place) | callee_saved_value); }
 108   void set_derived_oop()      { set_value((value() & register_mask_in_place) | derived_oop_value); }
 109 
 110   VMReg reg() const { return VMRegImpl::as_VMReg(mask_bits(value(), register_mask_in_place) >> register_shift); }
 111   oop_types type() const      { return (oop_types)mask_bits(value(), type_mask_in_place); }
 112 
 113   static bool legal_vm_reg_name(VMReg p) {
 114     return (p->value()  == (p->value() & register_mask));
 115   }
 116 
 117   void set_reg_type(VMReg p, oop_types t) {
 118     set_value((p->value() << register_shift) | t);
 119     assert(reg() == p, "sanity check" );
 120     assert(type() == t, "sanity check" );
 121   }
 122 
 123 
 124   VMReg content_reg() const       { return VMRegImpl::as_VMReg(_content_reg, true); }
 125   void set_content_reg(VMReg r)   { _content_reg = r->value(); }
 126 
 127   // Physical location queries
 128   bool is_register_loc()      { return reg()->is_reg(); }
 129   bool is_stack_loc()         { return reg()->is_stack(); }
 130 
 131   // Returns offset from sp.
 132   int stack_offset() {
 133     assert(is_stack_loc(), "must be stack location");
 134     return reg()->reg2stack();
 135   }
 136 
 137   void print_on(outputStream* st) const;
 138   void print() const { print_on(tty); }
 139 };
 140 
 141 
 142 class OopMap: public ResourceObj {
 143   friend class OopMapStream;
 144   friend class VMStructs;
 145  private:
 146   int  _pc_offset; // offset in the code that this OopMap corresponds to
 147   int  _omv_count; // number of OopMapValues in the stream
 148   CompressedWriteStream* _write_stream;
 149 
 150   debug_only( OopMapValue::oop_types* _locs_used; int _locs_length;)
 151 
 152   // Accessors
 153   int omv_count() const                       { return _omv_count; }
 154   void set_omv_count(int value)               { _omv_count = value; }
 155   void increment_count()                      { _omv_count++; }
 156   CompressedWriteStream* write_stream() const { return _write_stream; }
 157   void set_write_stream(CompressedWriteStream* value) { _write_stream = value; }
 158 
 159  private:
 160   enum DeepCopyToken { _deep_copy_token };
 161   OopMap(DeepCopyToken, OopMap* source);  // used only by deep_copy
 162 
 163  public:
 164   OopMap(int frame_size, int arg_count);
 165 
 166   // pc-offset handling
 167   int offset() const     { return _pc_offset; }
 168   void set_offset(int o) { _pc_offset = o; }
 169   int count() const { return _omv_count; }
 170   int data_size() const  { return write_stream()->position(); }
 171   address data() const { return write_stream()->buffer(); }
 172 
 173   // Check to avoid double insertion
 174   debug_only(OopMapValue::oop_types locs_used( int indx ) { return _locs_used[indx]; })
 175 
 176   // Construction
 177   // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd
 178   // slots to hold 4-byte values like ints and floats in the LP64 build.
 179   void set_oop  ( VMReg local);
 180   void set_value( VMReg local);
 181   void set_narrowoop(VMReg local);
 182   void set_dead ( VMReg local);
 183   void set_callee_saved( VMReg local, VMReg caller_machine_register );
 184   void set_derived_oop ( VMReg local, VMReg derived_from_local_register );
 185   void set_xxx(VMReg reg, OopMapValue::oop_types x, VMReg optional);
 186 
 187   int heap_size() const;
 188   void copy_data_to(address addr) const;
 189   OopMap* deep_copy();
 190 
 191   bool has_derived_pointer() const PRODUCT_RETURN0;
 192 
 193   bool legal_vm_reg_name(VMReg local) {
 194      return OopMapValue::legal_vm_reg_name(local);
 195   }
 196 
 197   // Printing
 198   void print_on(outputStream* st) const;
 199   void print() const { print_on(tty); }
 200   bool equals(const OopMap* other) const;
 201 };
 202 
 203 
 204 class OopMapSet : public ResourceObj {
 205   friend class VMStructs;
 206  private:
 207   int _om_count;
 208   int _om_size;
 209   OopMap** _om_data;
 210 
 211   int om_count() const              { return _om_count; }
 212   void set_om_count(int value)      { _om_count = value; }
 213   void increment_count()            { _om_count++; }
 214   int om_size() const               { return _om_size; }
 215   void set_om_size(int value)       { _om_size = value; }
 216   OopMap** om_data() const          { return _om_data; }
 217   void set_om_data(OopMap** value)  { _om_data = value; }
 218   void grow_om_data();
 219   void set(int index,OopMap* value) { assert((index == 0) || ((index > 0) && (index < om_size())),"bad index"); _om_data[index] = value; }
 220 
 221  public:
 222   OopMapSet();
 223 
 224   // returns the number of OopMaps in this OopMapSet
 225   int size() const            { return _om_count; }
 226   // returns the OopMap at a given index
 227   OopMap* at(int index) const { assert((index >= 0) && (index <= om_count()),"bad index"); return _om_data[index]; }
 228 
 229   // Collect OopMaps.
 230   void add_gc_map(int pc, OopMap* map);
 231 
 232   // Returns the only oop map. Used for reconstructing
 233   // Adapter frames during deoptimization
 234   OopMap* singular_oop_map();
 235 
 236   // returns OopMap in that is anchored to the pc
 237   OopMap* find_map_at_offset(int pc_offset) const;
 238 
 239   int heap_size() const;
 240 
 241   // Methods oops_do() and all_do() filter out NULL oops and
 242   // oop == Universe::narrow_oop_base() before passing oops
 243   // to closures.
 244 
 245   // Iterates through frame for a compiled method
 246   static void oops_do            (const frame* fr,
 247                                   const RegisterMap* reg_map, OopClosure* f);
 248   static void update_register_map(const frame* fr, RegisterMap *reg_map);
 249 
 250   // Iterates through frame for a compiled method for dead ones and values, too
 251   static void all_do(const frame* fr, const RegisterMap* reg_map,
 252                      OopClosure* oop_fn,
 253                      void derived_oop_fn(oop* base, oop* derived),
 254                      OopClosure* value_fn);
 255 
 256   // Printing
 257   void print_on(outputStream* st) const;
 258   void print() const { print_on(tty); }
 259 };
 260 
 261 class ImmutableOopMapBuilder;
 262 
 263 class ImmutableOopMap {
 264   friend class OopMapStream;
 265   friend class VMStructs;
 266 #ifdef ASSERT
 267   friend class ImmutableOopMapBuilder;
 268 #endif
 269 private:
 270   int _count; // contains the number of entries in this OopMap
 271 
 272   address data_addr() const { return (address) this + sizeof(ImmutableOopMap); }
 273 public:
 274   ImmutableOopMap(const OopMap* oopmap);
 275 
 276   bool has_derived_pointer() const PRODUCT_RETURN0;
 277   int count() const { return _count; }
 278 #ifdef ASSERT
 279   int nr_of_bytes() const; // this is an expensive operation, only used in debug builds
 280 #endif
 281 
 282   // Printing
 283   void print_on(outputStream* st) const;
 284   void print() const { print_on(tty); }
 285 };
 286 
 287 class ImmutableOopMapSet;
 288 class ImmutableOopMap;
 289 class OopMapSet;
 290 
 291 class ImmutableOopMapPair {
 292   friend class VMStructs;
 293 private:
 294   int _pc_offset; // program counter offset from the beginning of the method
 295   int _oopmap_offset; // offset in the data in the ImmutableOopMapSet where the ImmutableOopMap is located
 296 public:
 297   ImmutableOopMapPair(int pc_offset, int oopmap_offset) : _pc_offset(pc_offset), _oopmap_offset(oopmap_offset) {
 298     assert(pc_offset >= 0 && oopmap_offset >= 0, "check");
 299   }
 300   const ImmutableOopMap* get_from(const ImmutableOopMapSet* set) const;
 301 
 302   int pc_offset() const { return _pc_offset; }
 303   int oopmap_offset() const { return _oopmap_offset; }
 304 };
 305 
 306 class ImmutableOopMapSet {
 307   friend class VMStructs;
 308 private:
 309   int _count; // nr of ImmutableOopMapPairs in the Set
 310   int _size; // nr of bytes including ImmutableOopMapSet itself
 311 
 312   address data() const { return (address) this + sizeof(*this) + sizeof(ImmutableOopMapPair) * _count; }
 313 
 314 public:
 315   ImmutableOopMapSet(const OopMapSet* oopmap_set, int size) : _count(oopmap_set->size()), _size(size) {}
 316 
 317   ImmutableOopMap* oopmap_at_offset(int offset) const {
 318     assert(offset >= 0 && offset < _size, "must be within boundaries");
 319     address addr = data() + offset;
 320     return (ImmutableOopMap*) addr;
 321   }
 322 
 323   ImmutableOopMapPair* get_pairs() const { return (ImmutableOopMapPair*) ((address) this + sizeof(*this)); }
 324 
 325   static ImmutableOopMapSet* build_from(const OopMapSet* oopmap_set);
 326 
 327   const ImmutableOopMap* find_map_at_offset(int pc_offset) const;
 328 
 329   const ImmutableOopMapPair* pair_at(int index) const { assert(index >= 0 && index < _count, "check"); return &get_pairs()[index]; }
 330 
 331   int count() const { return _count; }
 332   int nr_of_bytes() const { return _size; }
 333 
 334   void print_on(outputStream* st) const;
 335   void print() const { print_on(tty); }
 336 };
 337 
 338 class OopMapStream : public StackObj {
 339  private:
 340   CompressedReadStream* _stream;
 341   int _mask;
 342   int _size;
 343   int _position;
 344   bool _valid_omv;
 345   OopMapValue _omv;
 346   void find_next();
 347 
 348  public:
 349   OopMapStream(OopMap* oop_map, int oop_types_mask = OopMapValue::type_mask_in_place);
 350   OopMapStream(const ImmutableOopMap* oop_map, int oop_types_mask = OopMapValue::type_mask_in_place);
 351   bool is_done()                        { if(!_valid_omv) { find_next(); } return !_valid_omv; }
 352   void next()                           { find_next(); }
 353   OopMapValue current()                 { return _omv; }
 354 #ifdef ASSERT
 355   int stream_position() const           { return _stream->position(); }
 356 #endif
 357 };
 358 
 359 class ImmutableOopMapBuilder {
 360 private:
 361   class Mapping;
 362 
 363 private:
 364   const OopMapSet* _set;
 365   const OopMap* _empty;
 366   const OopMap* _last;
 367   int _empty_offset;
 368   int _last_offset;
 369   int _offset;
 370   int _required;
 371   Mapping* _mapping;
 372   ImmutableOopMapSet* _new_set;
 373 
 374   /* Used for bookkeeping when building ImmutableOopMaps */
 375   class Mapping : public ResourceObj {
 376   public:
 377     enum kind_t { OOPMAP_UNKNOWN = 0, OOPMAP_NEW = 1, OOPMAP_EMPTY = 2, OOPMAP_DUPLICATE = 3 };
 378 
 379     kind_t _kind;
 380     int _offset;
 381     int _size;
 382     const OopMap* _map;
 383     const OopMap* _other;
 384 
 385     Mapping() : _kind(OOPMAP_UNKNOWN), _offset(-1), _size(-1), _map(NULL) {}
 386 
 387     void set(kind_t kind, int offset, int size, const OopMap* map = 0, const OopMap* other = 0) {
 388       _kind = kind;
 389       _offset = offset;
 390       _size = size;
 391       _map = map;
 392       _other = other;
 393     }
 394   };
 395 
 396 public:
 397   ImmutableOopMapBuilder(const OopMapSet* set);
 398 
 399   int heap_size();
 400   ImmutableOopMapSet* build();
 401   ImmutableOopMapSet* generate_into(address buffer);
 402 private:
 403   bool is_empty(const OopMap* map) const {
 404     return map->count() == 0;
 405   }
 406 
 407   bool is_last_duplicate(const OopMap* map) {
 408     if (_last != NULL && _last->count() > 0 && _last->equals(map)) {
 409       return true;
 410     }
 411     return false;
 412   }
 413 
 414 #ifdef ASSERT
 415   void verify(address buffer, int size, const ImmutableOopMapSet* set);
 416 #endif
 417 
 418   bool has_empty() const {
 419     return _empty_offset != -1;
 420   }
 421 
 422   int size_for(const OopMap* map) const;
 423   void fill_pair(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set);
 424   int fill_map(ImmutableOopMapPair* pair, const OopMap* map, int offset, const ImmutableOopMapSet* set);
 425   void fill(ImmutableOopMapSet* set, int size);
 426 };
 427 
 428 
 429 // Derived pointer support. This table keeps track of all derived points on a
 430 // stack.  It is cleared before each scavenge/GC.  During the traversal of all
 431 // oops, it is filled in with references to all locations that contains a
 432 // derived oop (assumed to be very few).  When the GC is complete, the derived
 433 // pointers are updated based on their base pointers new value and an offset.
 434 #if defined(COMPILER2) || INCLUDE_JVMCI
 435 class DerivedPointerTable : public AllStatic {
 436   friend class VMStructs;
 437  private:
 438    static GrowableArray<DerivedPointerEntry*>* _list;
 439    static bool _active;                      // do not record pointers for verify pass etc.
 440  public:
 441   static void clear();                       // Called before scavenge/GC
 442   static void add(oop *derived, oop *base);  // Called during scavenge/GC
 443   static void update_pointers();             // Called after  scavenge/GC
 444   static bool is_empty()                     { return _list == NULL || _list->is_empty(); }
 445   static bool is_active()                    { return _active; }
 446   static void set_active(bool value)         { _active = value; }
 447 };
 448 
 449 // A utility class to temporarily "deactivate" the DerivedPointerTable.
 450 // (Note: clients are responsible for any MT-safety issues)
 451 class DerivedPointerTableDeactivate: public StackObj {
 452  private:
 453   bool _active;
 454  public:
 455   DerivedPointerTableDeactivate() {
 456     _active = DerivedPointerTable::is_active();
 457     if (_active) {
 458       DerivedPointerTable::set_active(false);
 459     }
 460   }
 461 
 462   ~DerivedPointerTableDeactivate() {
 463     assert(!DerivedPointerTable::is_active(),
 464            "Inconsistency: not MT-safe");
 465     if (_active) {
 466       DerivedPointerTable::set_active(true);
 467     }
 468   }
 469 };
 470 #endif // COMPILER2 || JVMCI
 471 
 472 #endif // SHARE_VM_COMPILER_OOPMAP_HPP