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 class OopClosure;
  47 
  48 class OopMapValue: public StackObj {
  49   friend class VMStructs;
  50 private:
  51   short _value;
  52   int value() const                                 { return _value; }
  53   void set_value(int value)                         { _value = value; }
  54   short _content_reg;
  55 
  56 public:
  57   // Constants
  58   enum { type_bits                = 5,
  59          register_bits            = BitsPerShort - type_bits };
  60 
  61   enum { type_shift               = 0,
  62          register_shift           = type_bits };
  63 
  64   enum { type_mask                = right_n_bits(type_bits),
  65          type_mask_in_place       = type_mask << type_shift,
  66          register_mask            = right_n_bits(register_bits),
  67          register_mask_in_place   = register_mask << register_shift };
  68 
  69   enum oop_types {              // must fit in type_bits
  70          unused_value =0,       // powers of 2, for masking OopMapStream
  71          oop_value = 1,
  72          value_value = 2,
  73          narrowoop_value = 4,
  74          callee_saved_value = 8,
  75          derived_oop_value= 16 };
  76 
  77   // Constructors
  78   OopMapValue () { set_value(0); set_content_reg(VMRegImpl::Bad()); }
  79   OopMapValue (VMReg reg, oop_types t) { set_reg_type(reg, t); set_content_reg(VMRegImpl::Bad()); }
  80   OopMapValue (VMReg reg, oop_types t, VMReg reg2) { set_reg_type(reg, t); set_content_reg(reg2); }
  81   OopMapValue (CompressedReadStream* stream) { read_from(stream); }
  82 
  83   // Archiving
  84   void write_on(CompressedWriteStream* stream) {
  85     stream->write_int(value());
  86     if(is_callee_saved() || is_derived_oop()) {
  87       stream->write_int(content_reg()->value());
  88     }
  89   }
  90 
  91   void read_from(CompressedReadStream* stream) {
  92     set_value(stream->read_int());
  93     if (is_callee_saved() || is_derived_oop()) {
  94       set_content_reg(VMRegImpl::as_VMReg(stream->read_int(), true));
  95     }
  96   }
  97 
  98   // Querying
  99   bool is_oop()               { return mask_bits(value(), type_mask_in_place) == oop_value; }
 100   bool is_value()             { return mask_bits(value(), type_mask_in_place) == value_value; }
 101   bool is_narrowoop()           { return mask_bits(value(), type_mask_in_place) == narrowoop_value; }
 102   bool is_callee_saved()      { return mask_bits(value(), type_mask_in_place) == callee_saved_value; }
 103   bool is_derived_oop()       { return mask_bits(value(), type_mask_in_place) == derived_oop_value; }
 104 
 105   void set_oop()              { set_value((value() & register_mask_in_place) | oop_value); }
 106   void set_value()            { set_value((value() & register_mask_in_place) | value_value); }
 107   void set_narrowoop()          { set_value((value() & register_mask_in_place) | narrowoop_value); }
 108   void set_callee_saved()     { set_value((value() & register_mask_in_place) | callee_saved_value); }
 109   void set_derived_oop()      { set_value((value() & register_mask_in_place) | derived_oop_value); }
 110 
 111   VMReg reg() const { return VMRegImpl::as_VMReg(mask_bits(value(), register_mask_in_place) >> register_shift); }
 112   oop_types type() const      { return (oop_types)mask_bits(value(), type_mask_in_place); }
 113 
 114   static bool legal_vm_reg_name(VMReg p) {
 115     return (p->value()  == (p->value() & register_mask));
 116   }
 117 
 118   void set_reg_type(VMReg p, oop_types t) {
 119     set_value((p->value() << register_shift) | t);
 120     assert(reg() == p, "sanity check" );
 121     assert(type() == t, "sanity check" );
 122   }
 123 
 124 
 125   VMReg content_reg() const       { return VMRegImpl::as_VMReg(_content_reg, true); }
 126   void set_content_reg(VMReg r)   { _content_reg = r->value(); }
 127 
 128   // Physical location queries
 129   bool is_register_loc()      { return reg()->is_reg(); }
 130   bool is_stack_loc()         { return reg()->is_stack(); }
 131 
 132   // Returns offset from sp.
 133   int stack_offset() {
 134     assert(is_stack_loc(), "must be stack location");
 135     return reg()->reg2stack();
 136   }
 137 
 138   void print_on(outputStream* st) const;
 139   void print() const { print_on(tty); }
 140 };
 141 
 142 
 143 class OopMap: public ResourceObj {
 144   friend class OopMapStream;
 145   friend class VMStructs;
 146  private:
 147   int  _pc_offset;
 148   int  _omv_count;
 149   int  _omv_data_size;
 150   unsigned char* _omv_data;
 151   CompressedWriteStream* _write_stream;
 152 
 153   debug_only( OopMapValue::oop_types* _locs_used; int _locs_length;)
 154 
 155   // Accessors
 156   unsigned char* omv_data() const             { return _omv_data; }
 157   void set_omv_data(unsigned char* value)     { _omv_data = value; }
 158   int omv_data_size() const                   { return _omv_data_size; }
 159   void set_omv_data_size(int value)           { _omv_data_size = value; }
 160   int omv_count() const                       { return _omv_count; }
 161   void set_omv_count(int value)               { _omv_count = value; }
 162   void increment_count()                      { _omv_count++; }
 163   CompressedWriteStream* write_stream() const { return _write_stream; }
 164   void set_write_stream(CompressedWriteStream* value) { _write_stream = value; }
 165 
 166  private:
 167   enum DeepCopyToken { _deep_copy_token };
 168   OopMap(DeepCopyToken, OopMap* source);  // used only by deep_copy
 169 
 170  public:
 171   OopMap(int frame_size, int arg_count);
 172 
 173   // pc-offset handling
 174   int offset() const     { return _pc_offset; }
 175   void set_offset(int o) { _pc_offset = o; }
 176 
 177   // Check to avoid double insertion
 178   debug_only(OopMapValue::oop_types locs_used( int indx ) { return _locs_used[indx]; })
 179 
 180   // Construction
 181   // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd
 182   // slots to hold 4-byte values like ints and floats in the LP64 build.
 183   void set_oop  ( VMReg local);
 184   void set_value( VMReg local);
 185   void set_narrowoop(VMReg local);
 186   void set_dead ( VMReg local);
 187   void set_callee_saved( VMReg local, VMReg caller_machine_register );
 188   void set_derived_oop ( VMReg local, VMReg derived_from_local_register );
 189   void set_xxx(VMReg reg, OopMapValue::oop_types x, VMReg optional);
 190 
 191   int heap_size() const;
 192   void copy_to(address addr);
 193   OopMap* deep_copy();
 194 
 195   bool has_derived_pointer() const PRODUCT_RETURN0;
 196 
 197   bool legal_vm_reg_name(VMReg local) {
 198      return OopMapValue::legal_vm_reg_name(local);
 199   }
 200 
 201   // Printing
 202   void print_on(outputStream* st) const;
 203   void print() const { print_on(tty); }
 204 };
 205 
 206 
 207 class OopMapSet : public ResourceObj {
 208   friend class VMStructs;
 209  private:
 210   int _om_count;
 211   int _om_size;
 212   OopMap** _om_data;
 213 
 214   int om_count() const              { return _om_count; }
 215   void set_om_count(int value)      { _om_count = value; }
 216   void increment_count()            { _om_count++; }
 217   int om_size() const               { return _om_size; }
 218   void set_om_size(int value)       { _om_size = value; }
 219   OopMap** om_data() const          { return _om_data; }
 220   void set_om_data(OopMap** value)  { _om_data = value; }
 221   void grow_om_data();
 222   void set(int index,OopMap* value) { assert((index == 0) || ((index > 0) && (index < om_size())),"bad index"); _om_data[index] = value; }
 223 
 224  public:
 225   OopMapSet();
 226 
 227   // returns the number of OopMaps in this OopMapSet
 228   int size() const            { return _om_count; }
 229   // returns the OopMap at a given index
 230   OopMap* at(int index) const { assert((index >= 0) && (index <= om_count()),"bad index"); return _om_data[index]; }
 231 
 232   // Collect OopMaps.
 233   void add_gc_map(int pc, OopMap* map);
 234 
 235   // Returns the only oop map. Used for reconstructing
 236   // Adapter frames during deoptimization
 237   OopMap* singular_oop_map();
 238 
 239   // returns OopMap in that is anchored to the pc
 240   OopMap* find_map_at_offset(int pc_offset) const;
 241 
 242   int heap_size() const;
 243   void copy_to(address addr);
 244 
 245   // Methods oops_do() and all_do() filter out NULL oops and
 246   // oop == Universe::narrow_oop_base() before passing oops
 247   // to closures.
 248 
 249   // Iterates through frame for a compiled method
 250   static void oops_do            (const frame* fr,
 251                                   const RegisterMap* reg_map, OopClosure* f);
 252   static void update_register_map(const frame* fr, RegisterMap *reg_map);
 253 
 254   // Iterates through frame for a compiled method for dead ones and values, too
 255   static void all_do(const frame* fr, const RegisterMap* reg_map,
 256                      OopClosure* oop_fn,
 257                      void derived_oop_fn(oop* base, oop* derived),
 258                      OopClosure* value_fn);
 259 
 260   // Printing
 261   void print_on(outputStream* st) const;
 262   void print() const { print_on(tty); }
 263 };
 264 
 265 
 266 class OopMapStream : public StackObj {
 267  private:
 268   CompressedReadStream* _stream;
 269   int _mask;
 270   int _size;
 271   int _position;
 272   bool _valid_omv;
 273   OopMapValue _omv;
 274   void find_next();
 275 
 276  public:
 277   OopMapStream(OopMap* oop_map);
 278   OopMapStream(OopMap* oop_map, int oop_types_mask);
 279   bool is_done()                        { if(!_valid_omv) { find_next(); } return !_valid_omv; }
 280   void next()                           { find_next(); }
 281   OopMapValue current()                 { return _omv; }
 282 };
 283 
 284 
 285 // Derived pointer support. This table keeps track of all derived points on a
 286 // stack.  It is cleared before each scavenge/GC.  During the traversal of all
 287 // oops, it is filled in with references to all locations that contains a
 288 // derived oop (assumed to be very few).  When the GC is complete, the derived
 289 // pointers are updated based on their base pointers new value and an offset.
 290 #ifdef COMPILER2
 291 class DerivedPointerTable : public AllStatic {
 292   friend class VMStructs;
 293  private:
 294    static GrowableArray<DerivedPointerEntry*>* _list;
 295    static bool _active;                      // do not record pointers for verify pass etc.
 296  public:
 297   static void clear();                       // Called before scavenge/GC
 298   static void add(oop *derived, oop *base);  // Called during scavenge/GC
 299   static void update_pointers();             // Called after  scavenge/GC
 300   static bool is_empty()                     { return _list == NULL || _list->is_empty(); }
 301   static bool is_active()                    { return _active; }
 302   static void set_active(bool value)         { _active = value; }
 303 };
 304 
 305 // A utility class to temporarily "deactivate" the DerivedPointerTable.
 306 // (Note: clients are responsible for any MT-safety issues)
 307 class DerivedPointerTableDeactivate: public StackObj {
 308  private:
 309   bool _active;
 310  public:
 311   DerivedPointerTableDeactivate() {
 312     _active = DerivedPointerTable::is_active();
 313     if (_active) {
 314       DerivedPointerTable::set_active(false);
 315     }
 316   }
 317 
 318   ~DerivedPointerTableDeactivate() {
 319     assert(!DerivedPointerTable::is_active(),
 320            "Inconsistency: not MT-safe");
 321     if (_active) {
 322       DerivedPointerTable::set_active(true);
 323     }
 324   }
 325 };
 326 #endif // COMPILER2
 327 
 328 #endif // SHARE_VM_COMPILER_OOPMAP_HPP