1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)relocInfo.hpp        1.87 07/06/19 09:08:11 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 // Types in this file:
  29 //    relocInfo
  30 //      One element of an array of halfwords encoding compressed relocations.
  31 //      Also, the source of relocation types (relocInfo::oop_type, ...).
  32 //    Relocation
  33 //      A flyweight object representing a single relocation.
  34 //      It is fully unpacked from the compressed relocation array.
  35 //    oop_Relocation, ... (subclasses of Relocation)
  36 //      The location of some type-specific operations (oop_addr, ...).
  37 //      Also, the source of relocation specs (oop_Relocation::spec, ...).
  38 //    RelocationHolder
  39 //      A ValueObj type which acts as a union holding a Relocation object.
  40 //      Represents a relocation spec passed into a CodeBuffer during assembly.
  41 //    RelocIterator
  42 //      A StackObj which iterates over the relocations associated with
  43 //      a range of code addresses.  Can be used to operate a copy of code.
  44 //    PatchingRelocIterator
  45 //      Specialized subtype of RelocIterator which removes breakpoints
  46 //      temporarily during iteration, then restores them.
  47 //    BoundRelocation
  48 //      An _internal_ type shared by packers and unpackers of relocations.
  49 //      It pastes together a RelocationHolder with some pointers into
  50 //      code and relocInfo streams.
  51 
  52 
  53 // Notes on relocType:
  54 //
  55 // These hold enough information to read or write a value embedded in
  56 // the instructions of an CodeBlob.  They're used to update:
  57 //
  58 //   1) embedded oops     (isOop()          == true)
  59 //   2) inline caches     (isIC()           == true)
  60 //   3) runtime calls     (isRuntimeCall()  == true)
  61 //   4) internal word ref (isInternalWord() == true)
  62 //   5) external word ref (isExternalWord() == true)
  63 //
  64 // when objects move (GC) or if code moves (compacting the code heap).
  65 // They are also used to patch the code (if a call site must change)
  66 //
  67 // A relocInfo is represented in 16 bits:
  68 //   4 bits indicating the relocation type
  69 //  12 bits indicating the offset from the previous relocInfo address
  70 //
  71 // The offsets accumulate along the relocInfo stream to encode the
  72 // address within the CodeBlob, which is named RelocIterator::addr().
  73 // The address of a particular relocInfo always points to the first
  74 // byte of the relevant instruction (and not to any of its subfields
  75 // or embedded immediate constants).
  76 //
  77 // The offset value is scaled appropriately for the target machine.
  78 // (See relocInfo_<arch>.hpp for the offset scaling.)
  79 //
  80 // On some machines, there may also be a "format" field which may provide
  81 // additional information about the format of the instruction stream
  82 // at the corresponding code address.  The format value is usually zero.
  83 // Any machine (such as Intel) whose instructions can sometimes contain
  84 // more than one relocatable constant needs format codes to distinguish
  85 // which operand goes with a given relocation.
  86 //
  87 // If the target machine needs N format bits, the offset has 12-N bits,
  88 // the format is encoded between the offset and the type, and the
  89 // relocInfo_<arch>.hpp file has manifest constants for the format codes.
  90 //
  91 // If the type is "data_prefix_tag" then the offset bits are further encoded,
  92 // and in fact represent not a code-stream offset but some inline data.
  93 // The data takes the form of a counted sequence of halfwords, which
  94 // precedes the actual relocation record.  (Clients never see it directly.)
  95 // The interpetation of this extra data depends on the relocation type.
  96 //
  97 // On machines that have 32-bit immediate fields, there is usually
  98 // little need for relocation "prefix" data, because the instruction stream
  99 // is a perfectly reasonable place to store the value.  On machines in
 100 // which 32-bit values must be "split" across instructions, the relocation
 101 // data is the "true" specification of the value, which is then applied
 102 // to some field of the instruction (22 or 13 bits, on SPARC).
 103 //
 104 // Whenever the location of the CodeBlob changes, any PC-relative
 105 // relocations, and any internal_word_type relocations, must be reapplied.
 106 // After the GC runs, oop_type relocations must be reapplied.
 107 //
 108 //
 109 // Here are meanings of the types:
 110 //
 111 // relocInfo::none -- a filler record
 112 //   Value:  none
 113 //   Instruction: The corresponding code address is ignored
 114 //   Data:  Any data prefix and format code are ignored
 115 //   (This means that any relocInfo can be disabled by setting
 116 //   its type to none.  See relocInfo::remove.)
 117 //
 118 // relocInfo::oop_type -- a reference to an oop
 119 //   Value:  an oop, or else the address (handle) of an oop
 120 //   Instruction types: memory (load), set (load address)
 121 //   Data:  []       an oop stored in 4 bytes of instruction
 122 //          [n]      n is the index of an oop in the CodeBlob's oop pool
 123 //          [[N]n l] and l is a byte offset to be applied to the oop
 124 //          [Nn Ll]  both index and offset may be 32 bits if necessary
 125 //   Here is a special hack, used only by the old compiler:
 126 //          [[N]n 00] the value is the __address__ of the nth oop in the pool
 127 //   (Note that the offset allows optimal references to class variables.)
 128 //
 129 // relocInfo::internal_word_type -- an address within the same CodeBlob
 130 // relocInfo::section_word_type -- same, but can refer to another section
 131 //   Value:  an address in the CodeBlob's code or constants section
 132 //   Instruction types: memory (load), set (load address)
 133 //   Data:  []     stored in 4 bytes of instruction
 134 //          [[L]l] a relative offset (see [About Offsets] below)
 135 //   In the case of section_word_type, the offset is relative to a section
 136 //   base address, and the section number (e.g., SECT_INSTS) is encoded
 137 //   into the low two bits of the offset L.
 138 //
 139 // relocInfo::external_word_type -- a fixed address in the runtime system
 140 //   Value:  an address
 141 //   Instruction types: memory (load), set (load address)
 142 //   Data:  []   stored in 4 bytes of instruction
 143 //          [n]  the index of a "well-known" stub (usual case on RISC)
 144 //          [Ll] a 32-bit address
 145 //
 146 // relocInfo::runtime_call_type -- a fixed subroutine in the runtime system
 147 //   Value:  an address
 148 //   Instruction types: PC-relative call (or a PC-relative branch)
 149 //   Data:  []   stored in 4 bytes of instruction
 150 //
 151 // relocInfo::static_call_type -- a static call
 152 //   Value:  an CodeBlob, a stub, or a fixup routine
 153 //   Instruction types: a call
 154 //   Data:  []
 155 //   The identity of the callee is extracted from debugging information.
 156 //   //%note reloc_3
 157 //
 158 // relocInfo::virtual_call_type -- a virtual call site (which includes an inline 
 159 //                                 cache)
 160 //   Value:  an CodeBlob, a stub, the interpreter, or a fixup routine
 161 //   Instruction types: a call, plus some associated set-oop instructions
 162 //   Data:  []       the associated set-oops are adjacent to the call
 163 //          [n]      n is a relative offset to the first set-oop
 164 //          [[N]n l] and l is a limit within which the set-oops occur
 165 //          [Nn Ll]  both n and l may be 32 bits if necessary
 166 //   The identity of the callee is extracted from debugging information.
 167 //
 168 // relocInfo::opt_virtual_call_type -- a virtual call site that is statically bound
 169 //
 170 //    Same info as a static_call_type. We use a special type, so the handling of
 171 //    virtuals and statics are separated.
 172 //
 173 //
 174 //   The offset n points to the first set-oop.  (See [About Offsets] below.)
 175 //   In turn, the set-oop instruction specifies or contains an oop cell devoted
 176 //   exclusively to the IC call, which can be patched along with the call.
 177 //
 178 //   The locations of any other set-oops are found by searching the relocation
 179 //   information starting at the first set-oop, and continuing until all
 180 //   relocations up through l have been inspected.  The value l is another
 181 //   relative offset.  (Both n and l are relative to the call's first byte.)
 182 //
 183 //   The limit l of the search is exclusive.  However, if it points within
 184 //   the call (e.g., offset zero), it is adjusted to point after the call and
 185 //   any associated machine-specific delay slot.
 186 //
 187 //   Since the offsets could be as wide as 32-bits, these conventions
 188 //   put no restrictions whatever upon code reorganization.
 189 //
 190 //   The compiler is responsible for ensuring that transition from a clean
 191 //   state to a monomorphic compiled state is MP-safe.  This implies that
 192 //   the system must respond well to intermediate states where a random
 193 //   subset of the set-oops has been correctly from the clean state
 194 //   upon entry to the VEP of the compiled method.  In the case of a
 195 //   machine (Intel) with a single set-oop instruction, the 32-bit
 196 //   immediate field must not straddle a unit of memory coherence.
 197 //   //%note reloc_3
 198 //
 199 // relocInfo::breakpoint_type -- a conditional breakpoint in the code
 200 //   Value:  none
 201 //   Instruction types: any whatsoever
 202 //   Data:  [b [T]t  i...]
 203 //   The b is a bit-packed word representing the breakpoint's attributes.
 204 //   The t is a target address which the breakpoint calls (when it is enabled).
 205 //   The i... is a place to store one or two instruction words overwritten
 206 //   by a trap, so that the breakpoint may be subsequently removed.
 207 //
 208 // relocInfo::static_stub_type -- an extra stub for each static_call_type
 209 //   Value:  none
 210 //   Instruction types: a virtual call:  { set_oop; jump; }
 211 //   Data:  [[N]n]  the offset of the associated static_call reloc
 212 //   This stub becomes the target of a static call which must be upgraded
 213 //   to a virtual call (because the callee is interpreted).
 214 //   See [About Offsets] below.
 215 //   //%note reloc_2
 216 //
 217 // For example:
 218 //
 219 //   INSTRUCTIONS                        RELOC: TYPE    PREFIX DATA
 220 //   ------------                               ----    -----------
 221 // sethi      %hi(myObject),  R               oop_type [n(myObject)]
 222 // ld      [R+%lo(myObject)+fldOffset], R2    oop_type [n(myObject) fldOffset]
 223 // add R2, 1, R2
 224 // st  R2, [R+%lo(myObject)+fldOffset]        oop_type [n(myObject) fldOffset]
 225 //%note reloc_1
 226 //
 227 // This uses 4 instruction words, 8 relocation halfwords,
 228 // and an entry (which is sharable) in the CodeBlob's oop pool,
 229 // for a total of 36 bytes.
 230 // 
 231 // Note that the compiler is responsible for ensuring the "fldOffset" when
 232 // added to "%lo(myObject)" does not overflow the immediate fields of the
 233 // memory instructions.
 234 //
 235 //
 236 // [About Offsets] Relative offsets are supplied to this module as
 237 // positive byte offsets, but they may be internally stored scaled
 238 // and/or negated, depending on what is most compact for the target
 239 // system.  Since the object pointed to by the offset typically
 240 // precedes the relocation address, it is profitable to store
 241 // these negative offsets as positive numbers, but this decision
 242 // is internal to the relocation information abstractions.
 243 //
 244 
 245 class Relocation;
 246 class CodeBuffer;
 247 class CodeSection;
 248 class RelocIterator;
 249 
 250 class relocInfo VALUE_OBJ_CLASS_SPEC {
 251   friend class RelocIterator;
 252  public:
 253   enum relocType {
 254     none                    =  0, // Used when no relocation should be generated
 255     oop_type                =  1, // embedded oop
 256     virtual_call_type       =  2, // a standard inline cache call for a virtual send
 257     opt_virtual_call_type   =  3, // a virtual call that has been statically bound (i.e., no IC cache)
 258     static_call_type        =  4, // a static send 
 259     static_stub_type        =  5, // stub-entry for static send  (takes care of interpreter case)    
 260     runtime_call_type       =  6, // call to fixed external routine
 261     external_word_type      =  7, // reference to fixed external address
 262     internal_word_type      =  8, // reference within the current code blob
 263     section_word_type       =  9, // internal, but a cross-section reference
 264     poll_type               = 10, // polling instruction for safepoints
 265     poll_return_type        = 11, // polling instruction for safepoints at return
 266     breakpoint_type         = 12, // an initialization barrier or safepoint
 267     yet_unused_type         = 13, // Still unused
 268     yet_unused_type_2       = 14, // Still unused
 269     data_prefix_tag         = 15, // tag for a prefix (carries data arguments)
 270     type_mask               = 15  // A mask which selects only the above values
 271   };
 272 
 273  protected:
 274   unsigned short _value;
 275 
 276   enum RawBitsToken { RAW_BITS };
 277   relocInfo(relocType type, RawBitsToken ignore, int bits)
 278     : _value((type << nontype_width) + bits) { }
 279 
 280   relocInfo(relocType type, RawBitsToken ignore, int off, int f)
 281     : _value((type << nontype_width) + (off / (unsigned)offset_unit) + (f << offset_width)) { }
 282 
 283  public:
 284   // constructor
 285   relocInfo(relocType type, int offset, int format = 0)
 286 #ifndef ASSERT
 287   {
 288     (*this) = relocInfo(type, RAW_BITS, offset, format);
 289   }
 290 #else
 291   // Put a bunch of assertions out-of-line.
 292   ;
 293 #endif
 294 
 295   #define APPLY_TO_RELOCATIONS(visitor) \
 296     visitor(oop) \
 297     visitor(virtual_call) \
 298     visitor(opt_virtual_call) \
 299     visitor(static_call) \
 300     visitor(static_stub) \
 301     visitor(runtime_call) \
 302     visitor(external_word) \
 303     visitor(internal_word) \
 304     visitor(poll) \
 305     visitor(poll_return) \
 306     visitor(breakpoint) \
 307     visitor(section_word) \
 308 
 309 
 310  public:
 311   enum {
 312     value_width             = sizeof(unsigned short) * BitsPerByte,
 313     type_width              = 4,   // == log2(type_mask+1)
 314     nontype_width           = value_width - type_width,
 315     datalen_width           = nontype_width-1,
 316     datalen_tag             = 1 << datalen_width,  // or-ed into _value
 317     datalen_limit           = 1 << datalen_width,
 318     datalen_mask            = (1 << datalen_width)-1
 319   };
 320 
 321   // accessors
 322  public: 
 323   relocType  type()       const { return (relocType)((unsigned)_value >> nontype_width); }
 324   int  format()           const { return format_mask==0? 0: format_mask &
 325                                          ((unsigned)_value >> offset_width); }
 326   int  addr_offset()      const { assert(!is_prefix(), "must have offset");
 327                                   return (_value & offset_mask)*offset_unit; }
 328 
 329  protected:
 330   const short* data()     const { assert(is_datalen(), "must have data");
 331                                   return (const short*)(this + 1); }
 332   int          datalen()  const { assert(is_datalen(), "must have data");
 333                                   return (_value & datalen_mask); }
 334   int         immediate() const { assert(is_immediate(), "must have immed");
 335                                   return (_value & datalen_mask); }
 336  public:
 337   static int addr_unit()        { return offset_unit; }
 338   static int offset_limit()     { return (1 << offset_width) * offset_unit; }
 339 
 340   void set_type(relocType type);
 341   void set_format(int format);
 342 
 343   void remove() { set_type(none); }
 344 
 345  protected:
 346   bool is_none()                const { return type() == none; }
 347   bool is_prefix()              const { return type() == data_prefix_tag; }
 348   bool is_datalen()             const { assert(is_prefix(), "must be prefix");
 349                                         return (_value & datalen_tag) != 0; }
 350   bool is_immediate()           const { assert(is_prefix(), "must be prefix");
 351                                         return (_value & datalen_tag) == 0; }
 352 
 353  public:
 354   // Occasionally records of type relocInfo::none will appear in the stream.
 355   // We do not bother to filter these out, but clients should ignore them.
 356   // These records serve as "filler" in three ways:
 357   //  - to skip large spans of unrelocated code (this is rare)
 358   //  - to pad out the relocInfo array to the required oop alignment
 359   //  - to disable old relocation information which is no longer applicable
 360 
 361   inline friend relocInfo filler_relocInfo();
 362 
 363   // Every non-prefix relocation may be preceded by at most one prefix,
 364   // which supplies 1 or more halfwords of associated data.  Conventionally,
 365   // an int is represented by 0, 1, or 2 halfwords, depending on how
 366   // many bits are required to represent the value.  (In addition,
 367   // if the sole halfword is a 10-bit unsigned number, it is made
 368   // "immediate" in the prefix header word itself.  This optimization
 369   // is invisible outside this module.)
 370 
 371   inline friend relocInfo prefix_relocInfo(int datalen = 0);
 372 
 373  protected:
 374   // an immediate relocInfo optimizes a prefix with one 10-bit unsigned value
 375   static relocInfo immediate_relocInfo(int data0) {
 376     assert(fits_into_immediate(data0), "data0 in limits");
 377     return relocInfo(relocInfo::data_prefix_tag, RAW_BITS, data0);
 378   }
 379   static bool fits_into_immediate(int data0) {
 380     return (data0 >= 0 && data0 < datalen_limit);
 381   }
 382 
 383  public:
 384   // Support routines for compilers.
 385 
 386   // This routine takes an infant relocInfo (unprefixed) and
 387   // edits in its prefix, if any.  It also updates dest.locs_end.
 388   void initialize(CodeSection* dest, Relocation* reloc);
 389 
 390   // This routine updates a prefix and returns the limit pointer.
 391   // It tries to compress the prefix from 32 to 16 bits, and if
 392   // successful returns a reduced "prefix_limit" pointer.
 393   relocInfo* finish_prefix(short* prefix_limit);
 394 
 395   // bit-packers for the data array:
 396 
 397   // As it happens, the bytes within the shorts are ordered natively,
 398   // but the shorts within the word are ordered big-endian.
 399   // This is an arbitrary choice, made this way mainly to ease debugging.
 400   static int data0_from_int(jint x)         { return x >> value_width; }
 401   static int data1_from_int(jint x)         { return (short)x; }
 402   static jint jint_from_data(short* data) {
 403     return (data[0] << value_width) + (unsigned short)data[1];
 404   }
 405 
 406   static jint short_data_at(int n, short* data, int datalen) {
 407     return datalen > n ? data[n] : 0;
 408   }
 409 
 410   static jint jint_data_at(int n, short* data, int datalen) {
 411     return datalen > n+1 ? jint_from_data(&data[n]) : short_data_at(n, data, datalen);
 412   }
 413 
 414   // Update methods for relocation information
 415   // (since code is dynamically patched, we also need to dynamically update the relocation info)
 416   // Both methods takes old_type, so it is able to performe sanity checks on the information removed.
 417   static void change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type);
 418   static void remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type);
 419 
 420   // Machine dependent stuff
 421   #include "incls/_relocInfo_pd.hpp.incl"
 422 
 423  protected:
 424   // Derived constant, based on format_width which is PD:
 425   enum {
 426     offset_width       = nontype_width - format_width,
 427     offset_mask        = (1<<offset_width) - 1,
 428     format_mask        = (1<<format_width) - 1
 429   };
 430  public:
 431   enum {
 432     // Conservatively large estimate of maximum length (in shorts)
 433     // of any relocation record (probably breakpoints are largest).
 434     // Extended format is length prefix, data words, and tag/offset suffix.
 435     length_limit       = 1 + 1 + (3*BytesPerWord/BytesPerShort) + 1,
 436     have_format        = format_width > 0
 437   };
 438 };
 439 
 440 #define FORWARD_DECLARE_EACH_CLASS(name)              \
 441 class name##_Relocation;
 442 APPLY_TO_RELOCATIONS(FORWARD_DECLARE_EACH_CLASS)
 443 #undef FORWARD_DECLARE_EACH_CLASS
 444 
 445 
 446 
 447 inline relocInfo filler_relocInfo() {
 448   return relocInfo(relocInfo::none, relocInfo::offset_limit() - relocInfo::offset_unit);
 449 }
 450 
 451 inline relocInfo prefix_relocInfo(int datalen) {
 452   assert(relocInfo::fits_into_immediate(datalen), "datalen in limits");
 453   return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);
 454 }
 455 
 456 
 457 // Holder for flyweight relocation objects.
 458 // Although the flyweight subclasses are of varying sizes,
 459 // the holder is "one size fits all".
 460 class RelocationHolder VALUE_OBJ_CLASS_SPEC {
 461   friend class Relocation;
 462   friend class CodeSection;
 463 
 464  private:
 465   // this preallocated memory must accommodate all subclasses of Relocation
 466   // (this number is assertion-checked in Relocation::operator new)
 467   enum { _relocbuf_size = 5 };
 468   void* _relocbuf[ _relocbuf_size ];
 469 
 470  public:
 471   Relocation* reloc() const { return (Relocation*) &_relocbuf[0]; }
 472   inline relocInfo::relocType type() const;
 473 
 474   // Add a constant offset to a relocation.  Helper for class Address.
 475   RelocationHolder plus(int offset) const;
 476 
 477   inline RelocationHolder();                // initializes type to none
 478 
 479   inline RelocationHolder(Relocation* r);   // make a copy
 480 
 481   static const RelocationHolder none;
 482 };
 483 
 484 // A RelocIterator iterates through the relocation information of a CodeBlob.
 485 // It is a variable BoundRelocation which is able to take on successive
 486 // values as it is advanced through a code stream.
 487 // Usage:
 488 //   RelocIterator iter(nm);
 489 //   while (iter.next()) {
 490 //     iter.reloc()->some_operation();
 491 //   }
 492 // or:
 493 //   RelocIterator iter(nm);
 494 //   while (iter.next()) {
 495 //     switch (iter.type()) {
 496 //      case relocInfo::oop_type          :
 497 //      case relocInfo::ic_type           :
 498 //      case relocInfo::prim_type         :
 499 //      case relocInfo::uncommon_type     :
 500 //      case relocInfo::runtime_call_type :
 501 //      case relocInfo::internal_word_type: 
 502 //      case relocInfo::external_word_type: 
 503 //      ...
 504 //     }
 505 //   }
 506 
 507 class RelocIterator : public StackObj {
 508   enum { SECT_CONSTS = 2,
 509          SECT_LIMIT = 3 };  // must be equal to CodeBuffer::SECT_LIMIT
 510   friend class Relocation;
 511   friend class relocInfo;       // for change_reloc_info_for_address only
 512   typedef relocInfo::relocType relocType;
 513 
 514  private:
 515   address    _limit;   // stop producing relocations after this _addr
 516   relocInfo* _current; // the current relocation information
 517   relocInfo* _end;     // end marker; we're done iterating when _current == _end
 518   CodeBlob*  _code;    // compiled method containing _addr
 519   address    _addr;    // instruction to which the relocation applies
 520   short      _databuf; // spare buffer for compressed data
 521   short*     _data;    // pointer to the relocation's data
 522   short      _datalen; // number of halfwords in _data
 523   char       _format;  // position within the instruction
 524 
 525   // Base addresses needed to compute targets of section_word_type relocs.
 526   address    _section_start[SECT_LIMIT];
 527 
 528   void set_has_current(bool b) {
 529     _datalen = !b ? -1 : 0;
 530     debug_only(_data = NULL);
 531   }
 532   void set_current(relocInfo& ri) {
 533     _current = &ri;
 534     set_has_current(true);
 535   }
 536 
 537   RelocationHolder _rh; // where the current relocation is allocated
 538 
 539   relocInfo* current() const { assert(has_current(), "must have current");
 540                                return _current; }
 541 
 542   void set_limits(address begin, address limit);
 543 
 544   void advance_over_prefix();    // helper method
 545 
 546   void initialize_misc() {
 547     set_has_current(false);
 548     for (int i = 0; i < SECT_LIMIT; i++) {
 549       _section_start[i] = NULL;  // these will be lazily computed, if needed
 550     }
 551   }
 552 
 553   address compute_section_start(int n) const;  // out-of-line helper
 554 
 555   void initialize(CodeBlob* nm, address begin, address limit);
 556 
 557   friend class PatchingRelocIterator;
 558   // make an uninitialized one, for PatchingRelocIterator:
 559   RelocIterator() { initialize_misc(); }
 560 
 561  public:
 562   // constructor
 563   RelocIterator(CodeBlob* cb,    address begin = NULL, address limit = NULL);
 564   RelocIterator(CodeSection* cb, address begin = NULL, address limit = NULL);
 565 
 566   // get next reloc info, return !eos
 567   bool next() {
 568     _current++;
 569     assert(_current <= _end, "must not overrun relocInfo");
 570     if (_current == _end) {
 571       set_has_current(false);
 572       return false;
 573     }
 574     set_has_current(true);
 575 
 576     if (_current->is_prefix()) {
 577       advance_over_prefix();
 578       assert(!current()->is_prefix(), "only one prefix at a time");
 579     }
 580 
 581     _addr += _current->addr_offset();
 582 
 583     if (_limit != NULL && _addr >= _limit) {
 584       set_has_current(false);
 585       return false;
 586     }
 587 
 588     if (relocInfo::have_format)  _format = current()->format();
 589     return true;
 590   }
 591 
 592   // accessors  
 593   address      limit()        const { return _limit; }
 594   void     set_limit(address x);
 595   relocType    type()         const { return current()->type(); }
 596   int          format()       const { return (relocInfo::have_format) ? current()->format() : 0; }
 597   address      addr()         const { return _addr; }
 598   CodeBlob*    code()         const { return _code; }
 599   short*       data()         const { return _data; }
 600   int          datalen()      const { return _datalen; }
 601   bool     has_current()      const { return _datalen >= 0; }
 602 
 603   void       set_addr(address addr) { _addr = addr; }
 604   bool   addr_in_const()      const { return addr() >= section_start(SECT_CONSTS); }
 605 
 606   address section_start(int n) const {
 607     address res = _section_start[n];
 608     return (res != NULL) ? res : compute_section_start(n);
 609   }
 610 
 611   // The address points to the affected displacement part of the instruction.
 612   // For RISC, this is just the whole instruction.
 613   // For Intel, this is an unaligned 32-bit word.
 614 
 615   // type-specific relocation accessors:  oop_Relocation* oop_reloc(), etc.
 616   #define EACH_TYPE(name)                               \
 617   inline name##_Relocation* name##_reloc();
 618   APPLY_TO_RELOCATIONS(EACH_TYPE)
 619   #undef EACH_TYPE
 620   // generic relocation accessor; switches on type to call the above
 621   Relocation* reloc();
 622 
 623   // CodeBlob's have relocation indexes for faster random access:
 624   static int locs_and_index_size(int code_size, int locs_size);
 625   // Store an index into [dest_start+dest_count..dest_end).
 626   // At dest_start[0..dest_count] is the actual relocation information.
 627   // Everything else up to dest_end is free space for the index.
 628   static void create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end);
 629 
 630 #ifndef PRODUCT
 631  public:
 632   void print();  
 633   void print_current();  
 634 #endif
 635 };
 636 
 637 
 638 // A Relocation is a flyweight object allocated within a RelocationHolder.
 639 // It represents the relocation data of relocation record.
 640 // So, the RelocIterator unpacks relocInfos into Relocations.
 641 
 642 class Relocation VALUE_OBJ_CLASS_SPEC {
 643   friend class RelocationHolder;
 644   friend class RelocIterator;
 645 
 646  private:
 647   static void guarantee_size();
 648 
 649   // When a relocation has been created by a RelocIterator,
 650   // this field is non-null.  It allows the relocation to know
 651   // its context, such as the address to which it applies.
 652   RelocIterator* _binding;
 653 
 654  protected:
 655   RelocIterator* binding() const {
 656     assert(_binding != NULL, "must be bound");
 657     return _binding;
 658   }
 659   void set_binding(RelocIterator* b) {
 660     assert(_binding == NULL, "must be unbound");
 661     _binding = b;
 662     assert(_binding != NULL, "must now be bound");
 663   }
 664 
 665   Relocation() {
 666     _binding = NULL;
 667   }
 668 
 669   static RelocationHolder newHolder() {
 670     return RelocationHolder();
 671   }
 672 
 673  public:
 674   void* operator new(size_t size, const RelocationHolder& holder) {
 675     if (size > sizeof(holder._relocbuf)) guarantee_size();
 676     assert((void* const *)holder.reloc() == &holder._relocbuf[0], "ptrs must agree");
 677     return holder.reloc();
 678   }
 679 
 680   // make a generic relocation for a given type (if possible)
 681   static RelocationHolder spec_simple(relocInfo::relocType rtype);
 682 
 683   // here is the type-specific hook which writes relocation data:
 684   virtual void pack_data_to(CodeSection* dest) { }
 685 
 686   // here is the type-specific hook which reads (unpacks) relocation data:
 687   virtual void unpack_data() {
 688     assert(datalen()==0 || type()==relocInfo::none, "no data here");
 689   }
 690 
 691  protected:
 692   // Helper functions for pack_data_to() and unpack_data().
 693 
 694   // Most of the compression logic is confined here.
 695   // (The "immediate data" mechanism of relocInfo works independently
 696   // of this stuff, and acts to further compress most 1-word data prefixes.)
 697 
 698   // A variable-width int is encoded as a short if it will fit in 16 bits.
 699   // The decoder looks at datalen to decide whether to unpack short or jint.
 700   // Most relocation records are quite simple, containing at most two ints.
 701 
 702   static bool is_short(jint x) { return x == (short)x; }
 703   static short* add_short(short* p, int x)  { *p++ = x; return p; }
 704   static short* add_jint (short* p, jint x) {
 705     *p++ = relocInfo::data0_from_int(x); *p++ = relocInfo::data1_from_int(x);
 706     return p;
 707   }
 708   static short* add_var_int(short* p, jint x) {   // add a variable-width int
 709     if (is_short(x))  p = add_short(p, x);
 710     else              p = add_jint (p, x);
 711     return p;
 712   }
 713 
 714   static short* pack_1_int_to(short* p, jint x0) {
 715     // Format is one of:  [] [x] [Xx]
 716     if (x0 != 0)  p = add_var_int(p, x0);
 717     return p;
 718   }
 719   int unpack_1_int() {
 720     assert(datalen() <= 2, "too much data");
 721     return relocInfo::jint_data_at(0, data(), datalen());
 722   }
 723 
 724   // With two ints, the short form is used only if both ints are short.
 725   short* pack_2_ints_to(short* p, jint x0, jint x1) {
 726     // Format is one of:  [] [x y?] [Xx Y?y]
 727     if (x0 == 0 && x1 == 0) {
 728       // no halfwords needed to store zeroes
 729     } else if (is_short(x0) && is_short(x1)) {
 730       // 1-2 halfwords needed to store shorts
 731       p = add_short(p, x0); if (x1!=0) p = add_short(p, x1);
 732     } else {
 733       // 3-4 halfwords needed to store jints
 734       p = add_jint(p, x0);             p = add_var_int(p, x1);
 735     }
 736     return p;
 737   }
 738   void unpack_2_ints(jint& x0, jint& x1) {
 739     int    dlen = datalen();
 740     short* dp  = data();
 741     if (dlen <= 2) {
 742       x0 = relocInfo::short_data_at(0, dp, dlen);
 743       x1 = relocInfo::short_data_at(1, dp, dlen);
 744     } else {
 745       assert(dlen <= 4, "too much data");
 746       x0 = relocInfo::jint_data_at(0, dp, dlen);
 747       x1 = relocInfo::jint_data_at(2, dp, dlen);
 748     }
 749   }
 750 
 751  protected:
 752   // platform-dependent utilities for decoding and patching instructions
 753   void       pd_set_data_value       (address x, intptr_t off); // a set or mem-ref
 754   address    pd_call_destination     (address orig_addr = NULL);
 755   void       pd_set_call_destination (address x);
 756   void       pd_swap_in_breakpoint   (address x, short* instrs, int instrlen);
 757   void       pd_swap_out_breakpoint  (address x, short* instrs, int instrlen);
 758   static int pd_breakpoint_size      ();
 759 
 760   // this extracts the address of an address in the code stream instead of the reloc data
 761   address* pd_address_in_code       ();
 762 
 763   // this extracts an address from the code stream instead of the reloc data
 764   address  pd_get_address_from_code ();
 765 
 766   // these convert from byte offsets, to scaled offsets, to addresses
 767   static jint scaled_offset(address x, address base) {
 768     int byte_offset = x - base;
 769     int offset = -byte_offset / relocInfo::addr_unit();
 770     assert(address_from_scaled_offset(offset, base) == x, "just checkin'");
 771     return offset;
 772   }
 773   static jint scaled_offset_null_special(address x, address base) {
 774     // Some relocations treat offset=0 as meaning NULL.
 775     // Handle this extra convention carefully.
 776     if (x == NULL)  return 0;
 777     assert(x != base, "offset must not be zero");
 778     return scaled_offset(x, base);
 779   }
 780   static address address_from_scaled_offset(jint offset, address base) {
 781     int byte_offset = -( offset * relocInfo::addr_unit() );
 782     return base + byte_offset;
 783   }
 784 
 785   // these convert between indexes and addresses in the runtime system
 786   static int32_t runtime_address_to_index(address runtime_address);
 787   static address index_to_runtime_address(int32_t index);
 788 
 789   // helpers for mapping between old and new addresses after a move or resize
 790   address old_addr_for(address newa, const CodeBuffer* src, CodeBuffer* dest);
 791   address new_addr_for(address olda, const CodeBuffer* src, CodeBuffer* dest);
 792   void normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections = false);
 793  
 794  public:
 795   // accessors which only make sense for a bound Relocation
 796   address   addr()         const { return binding()->addr(); }
 797   CodeBlob* code()         const { return binding()->code(); }
 798   bool      addr_in_const() const { return binding()->addr_in_const(); }
 799  protected:
 800   short*   data()         const { return binding()->data(); }
 801   int      datalen()      const { return binding()->datalen(); }
 802   int      format()       const { return binding()->format(); }
 803 
 804  public:
 805   virtual relocInfo::relocType type()            { return relocInfo::none; }
 806 
 807   // is it a call instruction?
 808   virtual bool is_call()                         { return false; }
 809 
 810   // is it a data movement instruction?
 811   virtual bool is_data()                         { return false; }
 812 
 813   // some relocations can compute their own values
 814   virtual address  value();
 815 
 816   // all relocations are able to reassert their values
 817   virtual void set_value(address x);
 818 
 819   virtual void clear_inline_cache()              { }
 820 
 821   // This method assumes that all virtual/static (inline) caches are cleared (since for static_call_type and
 822   // ic_call_type is not always posisition dependent (depending on the state of the cache)). However, this is
 823   // probably a reasonable assumption, since empty caches simplifies code reloacation.
 824   virtual void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { }
 825 
 826   void print();
 827 };
 828 
 829 
 830 // certain inlines must be deferred until class Relocation is defined:
 831 
 832 inline RelocationHolder::RelocationHolder() {
 833   // initialize the vtbl, just to keep things type-safe
 834   new(*this) Relocation();
 835 }
 836 
 837 
 838 inline RelocationHolder::RelocationHolder(Relocation* r) {
 839   // wordwise copy from r (ok if it copies garbage after r)
 840   for (int i = 0; i < _relocbuf_size; i++) {
 841     _relocbuf[i] = ((void**)r)[i];
 842   }
 843 }
 844 
 845 
 846 relocInfo::relocType RelocationHolder::type() const {
 847   return reloc()->type();
 848 }
 849 
 850 // A DataRelocation always points at a memory or load-constant instruction..
 851 // It is absolute on most machines, and the constant is split on RISCs.
 852 // The specific subtypes are oop, external_word, and internal_word.
 853 // By convention, the "value" does not include a separately reckoned "offset".
 854 class DataRelocation : public Relocation {
 855  public:
 856   bool          is_data()                      { return true; }
 857 
 858   // both target and offset must be computed somehow from relocation data
 859   virtual int    offset()                      { return 0; }
 860   address         value()                      = 0;
 861   void        set_value(address x)             { set_value(x, offset()); }
 862   void        set_value(address x, intptr_t o) { 
 863     if (addr_in_const())
 864       *(address*)addr() = x;
 865     else 
 866       pd_set_data_value(x, o); 
 867   }
 868 
 869   // The "o" (displacement) argument is relevant only to split relocations
 870   // on RISC machines.  In some CPUs (SPARC), the set-hi and set-lo ins'ns
 871   // can encode more than 32 bits between them.  This allows compilers to
 872   // share set-hi instructions between addresses that differ by a small
 873   // offset (e.g., different static variables in the same class).
 874   // On such machines, the "x" argument to set_value on all set-lo
 875   // instructions must be the same as the "x" argument for the
 876   // corresponding set-hi instructions.  The "o" arguments for the
 877   // set-hi instructions are ignored, and must not affect the high-half
 878   // immediate constant.  The "o" arguments for the set-lo instructions are
 879   // added into the low-half immediate constant, and must not overflow it.
 880 };
 881 
 882 // A CallRelocation always points at a call instruction.
 883 // It is PC-relative on most machines.
 884 class CallRelocation : public Relocation {
 885  public:
 886   bool is_call() { return true; }
 887 
 888   address  destination()                    { return pd_call_destination(); }
 889   void     set_destination(address x); // pd_set_call_destination
 890 
 891   void     fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
 892   address  value()                          { return destination();  }
 893   void     set_value(address x)             { set_destination(x); }
 894 };
 895 
 896 class oop_Relocation : public DataRelocation {
 897   relocInfo::relocType type() { return relocInfo::oop_type; }
 898 
 899  public:
 900   // encode in one of these formats:  [] [n] [n l] [Nn l] [Nn Ll]
 901   // an oop in the CodeBlob's oop pool
 902   static RelocationHolder spec(int oop_index, int offset = 0) {
 903     assert(oop_index > 0, "must be a pool-resident oop");
 904     RelocationHolder rh = newHolder();
 905     new(rh) oop_Relocation(oop_index, offset);
 906     return rh;
 907   }
 908   // an oop in the instruction stream
 909   static RelocationHolder spec_for_immediate() {
 910     const int oop_index = 0;
 911     const int offset    = 0;    // if you want an offset, use the oop pool
 912     RelocationHolder rh = newHolder();
 913     new(rh) oop_Relocation(oop_index, offset);
 914     return rh;
 915   }
 916 
 917  private:
 918   jint _oop_index;                  // if > 0, index into CodeBlob::oop_at
 919   jint _offset;                     // byte offset to apply to the oop itself
 920 
 921   oop_Relocation(int oop_index, int offset) {
 922     _oop_index = oop_index; _offset = offset;
 923   }
 924 
 925   friend class RelocIterator;
 926   oop_Relocation() { }
 927 
 928  public:
 929   int oop_index() { return _oop_index; }
 930   int offset()    { return _offset; }
 931 
 932   // data is packed in "2_ints" format:  [i o] or [Ii Oo]
 933   void pack_data_to(CodeSection* dest);
 934   void unpack_data();
 935 
 936   void fix_oop_relocation();        // reasserts oop value
 937 
 938   address value()  { return (address) *oop_addr(); }
 939 
 940   bool oop_is_immediate()  { return oop_index() == 0; }
 941 
 942   oop* oop_addr();                  // addr or &pool[jint_data]
 943   oop  oop_value();                 // *oop_addr
 944   // Note:  oop_value transparently converts Universe::non_oop_word to NULL.
 945 };
 946 
 947 class virtual_call_Relocation : public CallRelocation {
 948   relocInfo::relocType type() { return relocInfo::virtual_call_type; }
 949 
 950  public:
 951   // "first_oop" points to the first associated set-oop.
 952   // The oop_limit helps find the last associated set-oop.
 953   // (See comments at the top of this file.)
 954   static RelocationHolder spec(address first_oop, address oop_limit = NULL) {
 955     RelocationHolder rh = newHolder();
 956     new(rh) virtual_call_Relocation(first_oop, oop_limit);
 957     return rh;
 958   }
 959 
 960   virtual_call_Relocation(address first_oop, address oop_limit) {
 961     _first_oop = first_oop; _oop_limit = oop_limit;
 962     assert(first_oop != NULL, "first oop address must be specified");
 963   }
 964 
 965  private:
 966   address _first_oop;               // location of first set-oop instruction
 967   address _oop_limit;               // search limit for set-oop instructions
 968 
 969   friend class RelocIterator; 
 970   virtual_call_Relocation() { }
 971 
 972 
 973  public:
 974   address first_oop();
 975   address oop_limit();
 976 
 977   // data is packed as scaled offsets in "2_ints" format:  [f l] or [Ff Ll]
 978   // oop_limit is set to 0 if the limit falls somewhere within the call.
 979   // When unpacking, a zero oop_limit is taken to refer to the end of the call.
 980   // (This has the effect of bringing in the call's delay slot on SPARC.)
 981   void pack_data_to(CodeSection* dest);
 982   void unpack_data();
 983 
 984   void clear_inline_cache();
 985 
 986   // Figure out where an ic_call is hiding, given a set-oop or call.
 987   // Either ic_call or first_oop must be non-null; the other is deduced.
 988   // Code if non-NULL must be the CodeBlob, else it is deduced.
 989   // The address of the patchable oop is also deduced.
 990   // The returned iterator will enumerate over the oops and the ic_call,
 991   // as well as any other relocations that happen to be in that span of code.
 992   // Recognize relevant set_oops with:  oop_reloc()->oop_addr() == oop_addr.
 993   static RelocIterator parse_ic(CodeBlob* &code, address &ic_call, address &first_oop, oop* &oop_addr, bool *is_optimized);
 994 };
 995 
 996 
 997 class opt_virtual_call_Relocation : public CallRelocation {
 998   relocInfo::relocType type() { return relocInfo::opt_virtual_call_type; }
 999 
1000  public:
1001   static RelocationHolder spec() {
1002     RelocationHolder rh = newHolder();
1003     new(rh) opt_virtual_call_Relocation();
1004     return rh;
1005   }
1006 
1007  private:
1008   friend class RelocIterator; 
1009   opt_virtual_call_Relocation() { }
1010 
1011  public:
1012   void clear_inline_cache();
1013 
1014   // find the matching static_stub
1015   address static_stub();
1016 };
1017 
1018 
1019 class static_call_Relocation : public CallRelocation {
1020   relocInfo::relocType type() { return relocInfo::static_call_type; }
1021 
1022  public:
1023   static RelocationHolder spec() {
1024     RelocationHolder rh = newHolder();
1025     new(rh) static_call_Relocation();
1026     return rh;
1027   }
1028 
1029  private:
1030   friend class RelocIterator; 
1031   static_call_Relocation() { }
1032 
1033  public:
1034   void clear_inline_cache();
1035 
1036   // find the matching static_stub
1037   address static_stub();
1038 };
1039 
1040 class static_stub_Relocation : public Relocation {
1041   relocInfo::relocType type() { return relocInfo::static_stub_type; }
1042 
1043  public:
1044   static RelocationHolder spec(address static_call) {
1045     RelocationHolder rh = newHolder();
1046     new(rh) static_stub_Relocation(static_call);
1047     return rh;
1048   }
1049 
1050  private:
1051   address _static_call;             // location of corresponding static_call
1052 
1053   static_stub_Relocation(address static_call) {
1054     _static_call = static_call;
1055   }
1056 
1057   friend class RelocIterator; 
1058   static_stub_Relocation() { }
1059 
1060  public:
1061   void clear_inline_cache();
1062 
1063   address static_call() { return _static_call; }
1064 
1065   // data is packed as a scaled offset in "1_int" format:  [c] or [Cc]
1066   void pack_data_to(CodeSection* dest);
1067   void unpack_data();
1068 };
1069 
1070 class runtime_call_Relocation : public CallRelocation {
1071   relocInfo::relocType type() { return relocInfo::runtime_call_type; }
1072 
1073  public:
1074   static RelocationHolder spec() {
1075     RelocationHolder rh = newHolder();
1076     new(rh) runtime_call_Relocation();
1077     return rh;
1078   }
1079 
1080  private:
1081   friend class RelocIterator; 
1082   runtime_call_Relocation() { }
1083 
1084  public:
1085 };
1086 
1087 class external_word_Relocation : public DataRelocation {
1088   relocInfo::relocType type() { return relocInfo::external_word_type; }
1089 
1090  public:
1091   static RelocationHolder spec(address target) {
1092     assert(target != NULL, "must not be null");
1093     RelocationHolder rh = newHolder();
1094     new(rh) external_word_Relocation(target);
1095     return rh;
1096   }
1097 
1098   // Use this one where all 32/64 bits of the target live in the code stream.
1099   // The target must be an intptr_t, and must be absolute (not relative).
1100   static RelocationHolder spec_for_immediate() {
1101     RelocationHolder rh = newHolder();
1102     new(rh) external_word_Relocation(NULL);
1103     return rh;
1104   }
1105 
1106  private:
1107   address _target;                  // address in runtime
1108 
1109   external_word_Relocation(address target) {
1110     _target = target;
1111   }
1112 
1113   friend class RelocIterator; 
1114   external_word_Relocation() { }
1115 
1116  public:
1117   // data is packed as a well-known address in "1_int" format:  [a] or [Aa]
1118   // The function runtime_address_to_index is used to turn full addresses
1119   // to short indexes, if they are pre-registered by the stub mechanism.
1120   // If the "a" value is 0 (i.e., _target is NULL), the address is stored
1121   // in the code stream.  See external_word_Relocation::target().
1122   void pack_data_to(CodeSection* dest);
1123   void unpack_data();
1124 
1125   void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
1126   address  target();        // if _target==NULL, fetch addr from code stream
1127   address  value()          { return target(); }
1128 };
1129 
1130 class internal_word_Relocation : public DataRelocation {
1131   relocInfo::relocType type() { return relocInfo::internal_word_type; }
1132 
1133  public:
1134   static RelocationHolder spec(address target) {
1135     assert(target != NULL, "must not be null");
1136     RelocationHolder rh = newHolder();
1137     new(rh) internal_word_Relocation(target);
1138     return rh;
1139   }
1140 
1141   // use this one where all the bits of the target can fit in the code stream:
1142   static RelocationHolder spec_for_immediate() {
1143     RelocationHolder rh = newHolder();
1144     new(rh) internal_word_Relocation(NULL);
1145     return rh;
1146   }
1147 
1148   internal_word_Relocation(address target) {
1149     _target  = target;
1150     _section = -1;  // self-relative
1151   }
1152 
1153  protected:
1154   address _target;                  // address in CodeBlob
1155   int     _section;                 // section providing base address, if any
1156 
1157   friend class RelocIterator; 
1158   internal_word_Relocation() { }
1159 
1160   // bit-width of LSB field in packed offset, if section >= 0
1161   enum { section_width = 2 }; // must equal CodeBuffer::sect_bits
1162 
1163  public:
1164   // data is packed as a scaled offset in "1_int" format:  [o] or [Oo]
1165   // If the "o" value is 0 (i.e., _target is NULL), the offset is stored
1166   // in the code stream.  See internal_word_Relocation::target().
1167   // If _section is not -1, it is appended to the low bits of the offset.
1168   void pack_data_to(CodeSection* dest);
1169   void unpack_data();
1170 
1171   void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
1172   address  target();        // if _target==NULL, fetch addr from code stream
1173   int      section()        { return _section;   }
1174   address  value()          { return target();   }
1175 };
1176 
1177 class section_word_Relocation : public internal_word_Relocation {
1178   relocInfo::relocType type() { return relocInfo::section_word_type; }
1179   
1180  public:
1181   static RelocationHolder spec(address target, int section) {
1182     RelocationHolder rh = newHolder();
1183     new(rh) section_word_Relocation(target, section);
1184     return rh;
1185   }
1186 
1187   section_word_Relocation(address target, int section) {
1188     assert(target != NULL, "must not be null");
1189     assert(section >= 0, "must be a valid section");
1190     _target  = target;
1191     _section = section;
1192   }
1193 
1194   //void pack_data_to -- inherited
1195   void unpack_data();
1196 
1197  private:
1198   friend class RelocIterator; 
1199   section_word_Relocation() { }
1200 };
1201 
1202 
1203 class poll_Relocation : public Relocation {
1204   bool          is_data()                      { return true; }
1205   relocInfo::relocType type() { return relocInfo::poll_type; }
1206 };
1207 
1208 class poll_return_Relocation : public Relocation {
1209   bool          is_data()                      { return true; }
1210   relocInfo::relocType type() { return relocInfo::poll_return_type; }
1211 };
1212 
1213 
1214 class breakpoint_Relocation : public Relocation {
1215   relocInfo::relocType type() { return relocInfo::breakpoint_type; }
1216 
1217   enum {
1218     // attributes which affect the interpretation of the data:
1219     removable_attr = 0x0010,   // buffer [i...] allows for undoing the trap
1220     internal_attr  = 0x0020,   // the target is an internal addr (local stub)
1221     settable_attr  = 0x0040,   // the target is settable
1222 
1223     // states which can change over time:
1224     enabled_state  = 0x0100,   // breakpoint must be active in running code
1225     active_state   = 0x0200,   // breakpoint instruction actually in code
1226 
1227     kind_mask      = 0x000F,   // mask for extracting kind
1228     high_bit       = 0x4000    // extra bit which is always set
1229   };
1230 
1231  public:
1232   enum {
1233     // kinds:
1234     initialization = 1,
1235     safepoint      = 2
1236   };
1237 
1238   // If target is NULL, 32 bits are reserved for a later set_target().
1239   static RelocationHolder spec(int kind, address target = NULL, bool internal_target = false) {
1240     RelocationHolder rh = newHolder();
1241     new(rh) breakpoint_Relocation(kind, target, internal_target);
1242     return rh;
1243   }
1244 
1245  private:
1246   // We require every bits value to NOT to fit into relocInfo::datalen_width,
1247   // because we are going to actually store state in the reloc, and so
1248   // cannot allow it to be compressed (and hence copied by the iterator).
1249 
1250   short   _bits;                  // bit-encoded kind, attrs, & state
1251   address _target;
1252 
1253   breakpoint_Relocation(int kind, address target, bool internal_target);
1254 
1255   friend class RelocIterator; 
1256   breakpoint_Relocation() { }
1257 
1258   short    bits()       const { return _bits; }
1259   short&   live_bits()  const { return data()[0]; }
1260   short*   instrs()     const { return data() + datalen() - instrlen(); }
1261   int      instrlen()   const { return removable() ? pd_breakpoint_size() : 0; }
1262 
1263   void set_bits(short x) {
1264     assert(live_bits() == _bits, "must be the only mutator of reloc info");
1265     live_bits() = _bits = x;
1266   }
1267 
1268  public:
1269   address  target()     const;
1270   void set_target(address x);
1271 
1272   int  kind()           const { return  bits() & kind_mask; }
1273   bool enabled()        const { return (bits() &  enabled_state) != 0; }
1274   bool active()         const { return (bits() &   active_state) != 0; }
1275   bool internal()       const { return (bits() &  internal_attr) != 0; }
1276   bool removable()      const { return (bits() & removable_attr) != 0; }
1277   bool settable()       const { return (bits() &  settable_attr) != 0; }
1278 
1279   void set_enabled(bool b);     // to activate, you must also say set_active
1280   void set_active(bool b);      // actually inserts bpt (must be enabled 1st)
1281 
1282   // data is packed as 16 bits, followed by the target (1 or 2 words), followed
1283   // if necessary by empty storage for saving away original instruction bytes.
1284   void pack_data_to(CodeSection* dest);
1285   void unpack_data();
1286 
1287   // during certain operations, breakpoints must be out of the way:
1288   void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
1289     assert(!active(), "cannot perform relocation on enabled breakpoints");
1290   }
1291 };
1292 
1293 
1294 // We know all the xxx_Relocation classes, so now we can define these:
1295 #define EACH_CASE(name)                                         \
1296 inline name##_Relocation* RelocIterator::name##_reloc() {       \
1297   assert(type() == relocInfo::name##_type, "type must agree");  \
1298   /* The purpose of the placed "new" is to re-use the same */   \
1299   /* stack storage for each new iteration. */                   \
1300   name##_Relocation* r = new(_rh) name##_Relocation();          \
1301   r->set_binding(this);                                         \
1302   r->name##_Relocation::unpack_data();                          \
1303   return r;                                                     \
1304 }
1305 APPLY_TO_RELOCATIONS(EACH_CASE);
1306 #undef EACH_CASE
1307 
1308 inline RelocIterator::RelocIterator(CodeBlob* cb, address begin, address limit) {
1309   initialize(cb, begin, limit);
1310 }
1311 
1312 // if you are going to patch code, you should use this subclass of
1313 // RelocIterator
1314 class PatchingRelocIterator : public RelocIterator {
1315  private:
1316   RelocIterator _init_state;
1317 
1318   void prepass();               // deactivates all breakpoints
1319   void postpass();              // reactivates all enabled breakpoints
1320 
1321   // do not copy these puppies; it would have unpredictable side effects
1322   // these are private and have no bodies defined because they should not be called
1323   PatchingRelocIterator(const RelocIterator&);
1324   void        operator=(const RelocIterator&);
1325 
1326  public:
1327   PatchingRelocIterator(CodeBlob* cb, address begin =NULL, address limit =NULL)
1328     : RelocIterator(cb, begin, limit)                { prepass();  }
1329 
1330   ~PatchingRelocIterator()                           { postpass(); }
1331 };
1332