1 /*
   2  * Copyright (c) 1998, 2009, 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 // Interface for generating the frame map for compiled code.  A frame map
  26 // describes for a specific pc whether each register and frame stack slot is:
  27 //   Oop         - A GC root for current frame
  28 //   Value       - Live non-oop, non-float value: int, either half of double
  29 //   Dead        - Dead; can be Zapped for debugging
  30 //   CalleeXX    - Callee saved; also describes which caller register is saved
  31 //   DerivedXX   - A derived oop; original oop is described.
  32 //
  33 // OopMapValue describes a single OopMap entry
  34 
  35 class frame;
  36 class RegisterMap;
  37 class DerivedPointerEntry;
  38 
  39 class OopMapValue: public StackObj {
  40   friend class VMStructs;
  41 private:
  42   short _value;
  43   int value() const                                 { return _value; }
  44   void set_value(int value)                         { _value = value; }
  45   short _content_reg;
  46 
  47 public:
  48   // Constants
  49   enum { type_bits                = 5,
  50          register_bits            = BitsPerShort - type_bits };
  51 
  52   enum { type_shift               = 0,
  53          register_shift           = type_bits };
  54 
  55   enum { type_mask                = right_n_bits(type_bits),
  56          type_mask_in_place       = type_mask << type_shift,
  57          register_mask            = right_n_bits(register_bits),
  58          register_mask_in_place   = register_mask << register_shift };
  59 
  60   enum oop_types {              // must fit in type_bits
  61          unused_value =0,       // powers of 2, for masking OopMapStream
  62          oop_value = 1,
  63          value_value = 2,
  64          narrowoop_value = 4,
  65          callee_saved_value = 8,
  66          derived_oop_value= 16 };
  67 
  68   // Constructors
  69   OopMapValue () { set_value(0); set_content_reg(VMRegImpl::Bad()); }
  70   OopMapValue (VMReg reg, oop_types t) { set_reg_type(reg,t); }
  71   OopMapValue (VMReg reg, oop_types t, VMReg reg2) { set_reg_type(reg,t); set_content_reg(reg2); }
  72   OopMapValue (CompressedReadStream* stream) { read_from(stream); }
  73 
  74   // Archiving
  75   void write_on(CompressedWriteStream* stream) {
  76     stream->write_int(value());
  77     if(is_callee_saved() || is_derived_oop()) {
  78       stream->write_int(content_reg()->value());
  79     }
  80   }
  81 
  82   void read_from(CompressedReadStream* stream) {
  83     set_value(stream->read_int());
  84     if(is_callee_saved() || is_derived_oop()) {
  85       set_content_reg(VMRegImpl::as_VMReg(stream->read_int(), true));
  86     }
  87   }
  88 
  89   // Querying
  90   bool is_oop()               { return mask_bits(value(), type_mask_in_place) == oop_value; }
  91   bool is_value()             { return mask_bits(value(), type_mask_in_place) == value_value; }
  92   bool is_narrowoop()           { return mask_bits(value(), type_mask_in_place) == narrowoop_value; }
  93   bool is_callee_saved()      { return mask_bits(value(), type_mask_in_place) == callee_saved_value; }
  94   bool is_derived_oop()       { return mask_bits(value(), type_mask_in_place) == derived_oop_value; }
  95 
  96   void set_oop()              { set_value((value() & register_mask_in_place) | oop_value); }
  97   void set_value()            { set_value((value() & register_mask_in_place) | value_value); }
  98   void set_narrowoop()          { set_value((value() & register_mask_in_place) | narrowoop_value); }
  99   void set_callee_saved()     { set_value((value() & register_mask_in_place) | callee_saved_value); }
 100   void set_derived_oop()      { set_value((value() & register_mask_in_place) | derived_oop_value); }
 101 
 102   VMReg reg() const { return VMRegImpl::as_VMReg(mask_bits(value(), register_mask_in_place) >> register_shift); }
 103   oop_types type() const      { return (oop_types)mask_bits(value(), type_mask_in_place); }
 104 
 105   static bool legal_vm_reg_name(VMReg p) {
 106     return (p->value()  == (p->value() & register_mask));
 107   }
 108 
 109   void set_reg_type(VMReg p, oop_types t) {
 110     set_value((p->value() << register_shift) | t);
 111     assert(reg() == p, "sanity check" );
 112     assert(type() == t, "sanity check" );
 113   }
 114 
 115 
 116   VMReg content_reg() const       { return VMRegImpl::as_VMReg(_content_reg, true); }
 117   void set_content_reg(VMReg r)   { _content_reg = r->value(); }
 118 
 119   // Physical location queries
 120   bool is_register_loc()      { return reg()->is_reg(); }
 121   bool is_stack_loc()         { return reg()->is_stack(); }
 122 
 123   // Returns offset from sp.
 124   int stack_offset() {
 125     assert(is_stack_loc(), "must be stack location");
 126     return reg()->reg2stack();
 127   }
 128 
 129   void print_on(outputStream* st) const;
 130   void print() const { print_on(tty); }
 131 };
 132 
 133 
 134 class OopMap: public ResourceObj {
 135   friend class OopMapStream;
 136   friend class VMStructs;
 137  private:
 138   int  _pc_offset;
 139   int  _omv_count;
 140   int  _omv_data_size;
 141   unsigned char* _omv_data;
 142   CompressedWriteStream* _write_stream;
 143 
 144   debug_only( OopMapValue::oop_types* _locs_used; int _locs_length;)
 145 
 146   // Accessors
 147   unsigned char* omv_data() const             { return _omv_data; }
 148   void set_omv_data(unsigned char* value)     { _omv_data = value; }
 149   int omv_data_size() const                   { return _omv_data_size; }
 150   void set_omv_data_size(int value)           { _omv_data_size = value; }
 151   int omv_count() const                       { return _omv_count; }
 152   void set_omv_count(int value)               { _omv_count = value; }
 153   void increment_count()                      { _omv_count++; }
 154   CompressedWriteStream* write_stream() const { return _write_stream; }
 155   void set_write_stream(CompressedWriteStream* value) { _write_stream = value; }
 156 
 157  private:
 158   enum DeepCopyToken { _deep_copy_token };
 159   OopMap(DeepCopyToken, OopMap* source);  // used only by deep_copy
 160 
 161  public:
 162   OopMap(int frame_size, int arg_count);
 163 
 164   // pc-offset handling
 165   int offset() const     { return _pc_offset; }
 166   void set_offset(int o) { _pc_offset = o; }
 167 
 168   // Check to avoid double insertion
 169   debug_only(OopMapValue::oop_types locs_used( int indx ) { return _locs_used[indx]; })
 170 
 171   // Construction
 172   // frame_size units are stack-slots (4 bytes) NOT intptr_t; we can name odd
 173   // slots to hold 4-byte values like ints and floats in the LP64 build.
 174   void set_oop  ( VMReg local);
 175   void set_value( VMReg local);
 176   void set_narrowoop(VMReg local);
 177   void set_dead ( VMReg local);
 178   void set_callee_saved( VMReg local, VMReg caller_machine_register );
 179   void set_derived_oop ( VMReg local, VMReg derived_from_local_register );
 180   void set_xxx(VMReg reg, OopMapValue::oop_types x, VMReg optional);
 181 
 182   int heap_size() const;
 183   void copy_to(address addr);
 184   OopMap* deep_copy();
 185 
 186   bool has_derived_pointer() const PRODUCT_RETURN0;
 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 };
 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   void copy_to(address addr);
 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 
 257 class OopMapStream : public StackObj {
 258  private:
 259   CompressedReadStream* _stream;
 260   int _mask;
 261   int _size;
 262   int _position;
 263   bool _valid_omv;
 264   OopMapValue _omv;
 265   void find_next();
 266 
 267  public:
 268   OopMapStream(OopMap* oop_map);
 269   OopMapStream(OopMap* oop_map, int oop_types_mask);
 270   bool is_done()                        { if(!_valid_omv) { find_next(); } return !_valid_omv; }
 271   void next()                           { find_next(); }
 272   OopMapValue current()                 { return _omv; }
 273 };
 274 
 275 
 276 // Derived pointer support. This table keeps track of all derived points on a
 277 // stack.  It is cleared before each scavenge/GC.  During the traversal of all
 278 // oops, it is filled in with references to all locations that contains a
 279 // derived oop (assumed to be very few).  When the GC is complete, the derived
 280 // pointers are updated based on their base pointers new value and an offset.
 281 #ifdef COMPILER2
 282 class DerivedPointerTable : public AllStatic {
 283   friend class VMStructs;
 284  private:
 285    static GrowableArray<DerivedPointerEntry*>* _list;
 286    static bool _active;                      // do not record pointers for verify pass etc.
 287  public:
 288   static void clear();                       // Called before scavenge/GC
 289   static void add(oop *derived, oop *base);  // Called during scavenge/GC
 290   static void update_pointers();             // Called after  scavenge/GC
 291   static bool is_empty()                     { return _list == NULL || _list->is_empty(); }
 292   static bool is_active()                    { return _active; }
 293   static void set_active(bool value)         { _active = value; }
 294 };
 295 
 296 // A utility class to temporarily "deactivate" the DerivedPointerTable.
 297 // (Note: clients are responsible for any MT-safety issues)
 298 class DerivedPointerTableDeactivate: public StackObj {
 299  private:
 300   bool _active;
 301  public:
 302   DerivedPointerTableDeactivate() {
 303     _active = DerivedPointerTable::is_active();
 304     if (_active) {
 305       DerivedPointerTable::set_active(false);
 306     }
 307   }
 308 
 309   ~DerivedPointerTableDeactivate() {
 310     assert(!DerivedPointerTable::is_active(),
 311            "Inconsistency: not MT-safe");
 312     if (_active) {
 313       DerivedPointerTable::set_active(true);
 314     }
 315   }
 316 };
 317 #endif // COMPILER2