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