relocInfo.hpp 49.8 KB
Newer Older
D
duke 已提交
1
/*
X
xdono 已提交
2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

// Types in this file:
//    relocInfo
//      One element of an array of halfwords encoding compressed relocations.
//      Also, the source of relocation types (relocInfo::oop_type, ...).
//    Relocation
//      A flyweight object representing a single relocation.
//      It is fully unpacked from the compressed relocation array.
//    oop_Relocation, ... (subclasses of Relocation)
//      The location of some type-specific operations (oop_addr, ...).
//      Also, the source of relocation specs (oop_Relocation::spec, ...).
//    RelocationHolder
//      A ValueObj type which acts as a union holding a Relocation object.
//      Represents a relocation spec passed into a CodeBuffer during assembly.
//    RelocIterator
//      A StackObj which iterates over the relocations associated with
//      a range of code addresses.  Can be used to operate a copy of code.
//    PatchingRelocIterator
//      Specialized subtype of RelocIterator which removes breakpoints
//      temporarily during iteration, then restores them.
//    BoundRelocation
//      An _internal_ type shared by packers and unpackers of relocations.
//      It pastes together a RelocationHolder with some pointers into
//      code and relocInfo streams.


// Notes on relocType:
//
// These hold enough information to read or write a value embedded in
// the instructions of an CodeBlob.  They're used to update:
//
//   1) embedded oops     (isOop()          == true)
//   2) inline caches     (isIC()           == true)
//   3) runtime calls     (isRuntimeCall()  == true)
//   4) internal word ref (isInternalWord() == true)
//   5) external word ref (isExternalWord() == true)
//
// when objects move (GC) or if code moves (compacting the code heap).
// They are also used to patch the code (if a call site must change)
//
// A relocInfo is represented in 16 bits:
//   4 bits indicating the relocation type
//  12 bits indicating the offset from the previous relocInfo address
//
// The offsets accumulate along the relocInfo stream to encode the
// address within the CodeBlob, which is named RelocIterator::addr().
// The address of a particular relocInfo always points to the first
// byte of the relevant instruction (and not to any of its subfields
// or embedded immediate constants).
//
// The offset value is scaled appropriately for the target machine.
// (See relocInfo_<arch>.hpp for the offset scaling.)
//
// On some machines, there may also be a "format" field which may provide
// additional information about the format of the instruction stream
// at the corresponding code address.  The format value is usually zero.
// Any machine (such as Intel) whose instructions can sometimes contain
// more than one relocatable constant needs format codes to distinguish
// which operand goes with a given relocation.
//
// If the target machine needs N format bits, the offset has 12-N bits,
// the format is encoded between the offset and the type, and the
// relocInfo_<arch>.hpp file has manifest constants for the format codes.
//
// If the type is "data_prefix_tag" then the offset bits are further encoded,
// and in fact represent not a code-stream offset but some inline data.
// The data takes the form of a counted sequence of halfwords, which
// precedes the actual relocation record.  (Clients never see it directly.)
// The interpetation of this extra data depends on the relocation type.
//
// On machines that have 32-bit immediate fields, there is usually
// little need for relocation "prefix" data, because the instruction stream
// is a perfectly reasonable place to store the value.  On machines in
// which 32-bit values must be "split" across instructions, the relocation
// data is the "true" specification of the value, which is then applied
// to some field of the instruction (22 or 13 bits, on SPARC).
//
// Whenever the location of the CodeBlob changes, any PC-relative
// relocations, and any internal_word_type relocations, must be reapplied.
// After the GC runs, oop_type relocations must be reapplied.
//
//
// Here are meanings of the types:
//
// relocInfo::none -- a filler record
//   Value:  none
//   Instruction: The corresponding code address is ignored
//   Data:  Any data prefix and format code are ignored
//   (This means that any relocInfo can be disabled by setting
//   its type to none.  See relocInfo::remove.)
//
// relocInfo::oop_type -- a reference to an oop
//   Value:  an oop, or else the address (handle) of an oop
//   Instruction types: memory (load), set (load address)
//   Data:  []       an oop stored in 4 bytes of instruction
//          [n]      n is the index of an oop in the CodeBlob's oop pool
//          [[N]n l] and l is a byte offset to be applied to the oop
//          [Nn Ll]  both index and offset may be 32 bits if necessary
//   Here is a special hack, used only by the old compiler:
//          [[N]n 00] the value is the __address__ of the nth oop in the pool
//   (Note that the offset allows optimal references to class variables.)
//
// relocInfo::internal_word_type -- an address within the same CodeBlob
// relocInfo::section_word_type -- same, but can refer to another section
//   Value:  an address in the CodeBlob's code or constants section
//   Instruction types: memory (load), set (load address)
//   Data:  []     stored in 4 bytes of instruction
//          [[L]l] a relative offset (see [About Offsets] below)
//   In the case of section_word_type, the offset is relative to a section
//   base address, and the section number (e.g., SECT_INSTS) is encoded
//   into the low two bits of the offset L.
//
// relocInfo::external_word_type -- a fixed address in the runtime system
//   Value:  an address
//   Instruction types: memory (load), set (load address)
//   Data:  []   stored in 4 bytes of instruction
//          [n]  the index of a "well-known" stub (usual case on RISC)
//          [Ll] a 32-bit address
//
// relocInfo::runtime_call_type -- a fixed subroutine in the runtime system
//   Value:  an address
//   Instruction types: PC-relative call (or a PC-relative branch)
//   Data:  []   stored in 4 bytes of instruction
//
// relocInfo::static_call_type -- a static call
//   Value:  an CodeBlob, a stub, or a fixup routine
//   Instruction types: a call
//   Data:  []
//   The identity of the callee is extracted from debugging information.
//   //%note reloc_3
//
// relocInfo::virtual_call_type -- a virtual call site (which includes an inline
//                                 cache)
//   Value:  an CodeBlob, a stub, the interpreter, or a fixup routine
//   Instruction types: a call, plus some associated set-oop instructions
//   Data:  []       the associated set-oops are adjacent to the call
//          [n]      n is a relative offset to the first set-oop
//          [[N]n l] and l is a limit within which the set-oops occur
//          [Nn Ll]  both n and l may be 32 bits if necessary
//   The identity of the callee is extracted from debugging information.
//
// relocInfo::opt_virtual_call_type -- a virtual call site that is statically bound
//
//    Same info as a static_call_type. We use a special type, so the handling of
//    virtuals and statics are separated.
//
//
//   The offset n points to the first set-oop.  (See [About Offsets] below.)
//   In turn, the set-oop instruction specifies or contains an oop cell devoted
//   exclusively to the IC call, which can be patched along with the call.
//
//   The locations of any other set-oops are found by searching the relocation
//   information starting at the first set-oop, and continuing until all
//   relocations up through l have been inspected.  The value l is another
//   relative offset.  (Both n and l are relative to the call's first byte.)
//
//   The limit l of the search is exclusive.  However, if it points within
//   the call (e.g., offset zero), it is adjusted to point after the call and
//   any associated machine-specific delay slot.
//
//   Since the offsets could be as wide as 32-bits, these conventions
//   put no restrictions whatever upon code reorganization.
//
//   The compiler is responsible for ensuring that transition from a clean
//   state to a monomorphic compiled state is MP-safe.  This implies that
//   the system must respond well to intermediate states where a random
//   subset of the set-oops has been correctly from the clean state
//   upon entry to the VEP of the compiled method.  In the case of a
//   machine (Intel) with a single set-oop instruction, the 32-bit
//   immediate field must not straddle a unit of memory coherence.
//   //%note reloc_3
//
// relocInfo::breakpoint_type -- a conditional breakpoint in the code
//   Value:  none
//   Instruction types: any whatsoever
//   Data:  [b [T]t  i...]
//   The b is a bit-packed word representing the breakpoint's attributes.
//   The t is a target address which the breakpoint calls (when it is enabled).
//   The i... is a place to store one or two instruction words overwritten
//   by a trap, so that the breakpoint may be subsequently removed.
//
// relocInfo::static_stub_type -- an extra stub for each static_call_type
//   Value:  none
//   Instruction types: a virtual call:  { set_oop; jump; }
//   Data:  [[N]n]  the offset of the associated static_call reloc
//   This stub becomes the target of a static call which must be upgraded
//   to a virtual call (because the callee is interpreted).
//   See [About Offsets] below.
//   //%note reloc_2
//
// For example:
//
//   INSTRUCTIONS                        RELOC: TYPE    PREFIX DATA
//   ------------                               ----    -----------
// sethi      %hi(myObject),  R               oop_type [n(myObject)]
// ld      [R+%lo(myObject)+fldOffset], R2    oop_type [n(myObject) fldOffset]
// add R2, 1, R2
// st  R2, [R+%lo(myObject)+fldOffset]        oop_type [n(myObject) fldOffset]
//%note reloc_1
//
// This uses 4 instruction words, 8 relocation halfwords,
// and an entry (which is sharable) in the CodeBlob's oop pool,
// for a total of 36 bytes.
//
// Note that the compiler is responsible for ensuring the "fldOffset" when
// added to "%lo(myObject)" does not overflow the immediate fields of the
// memory instructions.
//
//
// [About Offsets] Relative offsets are supplied to this module as
// positive byte offsets, but they may be internally stored scaled
// and/or negated, depending on what is most compact for the target
// system.  Since the object pointed to by the offset typically
// precedes the relocation address, it is profitable to store
// these negative offsets as positive numbers, but this decision
// is internal to the relocation information abstractions.
//

class Relocation;
class CodeBuffer;
class CodeSection;
class RelocIterator;

class relocInfo VALUE_OBJ_CLASS_SPEC {
  friend class RelocIterator;
 public:
  enum relocType {
    none                    =  0, // Used when no relocation should be generated
    oop_type                =  1, // embedded oop
    virtual_call_type       =  2, // a standard inline cache call for a virtual send
    opt_virtual_call_type   =  3, // a virtual call that has been statically bound (i.e., no IC cache)
    static_call_type        =  4, // a static send
    static_stub_type        =  5, // stub-entry for static send  (takes care of interpreter case)
    runtime_call_type       =  6, // call to fixed external routine
    external_word_type      =  7, // reference to fixed external address
    internal_word_type      =  8, // reference within the current code blob
    section_word_type       =  9, // internal, but a cross-section reference
    poll_type               = 10, // polling instruction for safepoints
    poll_return_type        = 11, // polling instruction for safepoints at return
    breakpoint_type         = 12, // an initialization barrier or safepoint
    yet_unused_type         = 13, // Still unused
    yet_unused_type_2       = 14, // Still unused
    data_prefix_tag         = 15, // tag for a prefix (carries data arguments)
    type_mask               = 15  // A mask which selects only the above values
  };

 protected:
  unsigned short _value;

  enum RawBitsToken { RAW_BITS };
  relocInfo(relocType type, RawBitsToken ignore, int bits)
    : _value((type << nontype_width) + bits) { }

  relocInfo(relocType type, RawBitsToken ignore, int off, int f)
    : _value((type << nontype_width) + (off / (unsigned)offset_unit) + (f << offset_width)) { }

 public:
  // constructor
  relocInfo(relocType type, int offset, int format = 0)
#ifndef ASSERT
  {
    (*this) = relocInfo(type, RAW_BITS, offset, format);
  }
#else
  // Put a bunch of assertions out-of-line.
  ;
#endif

  #define APPLY_TO_RELOCATIONS(visitor) \
    visitor(oop) \
    visitor(virtual_call) \
    visitor(opt_virtual_call) \
    visitor(static_call) \
    visitor(static_stub) \
    visitor(runtime_call) \
    visitor(external_word) \
    visitor(internal_word) \
    visitor(poll) \
    visitor(poll_return) \
    visitor(breakpoint) \
    visitor(section_word) \


 public:
  enum {
    value_width             = sizeof(unsigned short) * BitsPerByte,
    type_width              = 4,   // == log2(type_mask+1)
    nontype_width           = value_width - type_width,
    datalen_width           = nontype_width-1,
    datalen_tag             = 1 << datalen_width,  // or-ed into _value
    datalen_limit           = 1 << datalen_width,
    datalen_mask            = (1 << datalen_width)-1
  };

  // accessors
 public:
  relocType  type()       const { return (relocType)((unsigned)_value >> nontype_width); }
  int  format()           const { return format_mask==0? 0: format_mask &
                                         ((unsigned)_value >> offset_width); }
  int  addr_offset()      const { assert(!is_prefix(), "must have offset");
                                  return (_value & offset_mask)*offset_unit; }

 protected:
  const short* data()     const { assert(is_datalen(), "must have data");
                                  return (const short*)(this + 1); }
  int          datalen()  const { assert(is_datalen(), "must have data");
                                  return (_value & datalen_mask); }
  int         immediate() const { assert(is_immediate(), "must have immed");
                                  return (_value & datalen_mask); }
 public:
  static int addr_unit()        { return offset_unit; }
  static int offset_limit()     { return (1 << offset_width) * offset_unit; }

  void set_type(relocType type);
  void set_format(int format);

  void remove() { set_type(none); }

 protected:
  bool is_none()                const { return type() == none; }
  bool is_prefix()              const { return type() == data_prefix_tag; }
  bool is_datalen()             const { assert(is_prefix(), "must be prefix");
                                        return (_value & datalen_tag) != 0; }
  bool is_immediate()           const { assert(is_prefix(), "must be prefix");
                                        return (_value & datalen_tag) == 0; }

 public:
  // Occasionally records of type relocInfo::none will appear in the stream.
  // We do not bother to filter these out, but clients should ignore them.
  // These records serve as "filler" in three ways:
  //  - to skip large spans of unrelocated code (this is rare)
  //  - to pad out the relocInfo array to the required oop alignment
  //  - to disable old relocation information which is no longer applicable

  inline friend relocInfo filler_relocInfo();

  // Every non-prefix relocation may be preceded by at most one prefix,
  // which supplies 1 or more halfwords of associated data.  Conventionally,
  // an int is represented by 0, 1, or 2 halfwords, depending on how
  // many bits are required to represent the value.  (In addition,
  // if the sole halfword is a 10-bit unsigned number, it is made
  // "immediate" in the prefix header word itself.  This optimization
  // is invisible outside this module.)

  inline friend relocInfo prefix_relocInfo(int datalen = 0);

 protected:
  // an immediate relocInfo optimizes a prefix with one 10-bit unsigned value
  static relocInfo immediate_relocInfo(int data0) {
    assert(fits_into_immediate(data0), "data0 in limits");
    return relocInfo(relocInfo::data_prefix_tag, RAW_BITS, data0);
  }
  static bool fits_into_immediate(int data0) {
    return (data0 >= 0 && data0 < datalen_limit);
  }

 public:
  // Support routines for compilers.

  // This routine takes an infant relocInfo (unprefixed) and
  // edits in its prefix, if any.  It also updates dest.locs_end.
  void initialize(CodeSection* dest, Relocation* reloc);

  // This routine updates a prefix and returns the limit pointer.
  // It tries to compress the prefix from 32 to 16 bits, and if
  // successful returns a reduced "prefix_limit" pointer.
  relocInfo* finish_prefix(short* prefix_limit);

  // bit-packers for the data array:

  // As it happens, the bytes within the shorts are ordered natively,
  // but the shorts within the word are ordered big-endian.
  // This is an arbitrary choice, made this way mainly to ease debugging.
  static int data0_from_int(jint x)         { return x >> value_width; }
  static int data1_from_int(jint x)         { return (short)x; }
  static jint jint_from_data(short* data) {
    return (data[0] << value_width) + (unsigned short)data[1];
  }

  static jint short_data_at(int n, short* data, int datalen) {
    return datalen > n ? data[n] : 0;
  }

  static jint jint_data_at(int n, short* data, int datalen) {
    return datalen > n+1 ? jint_from_data(&data[n]) : short_data_at(n, data, datalen);
  }

  // Update methods for relocation information
  // (since code is dynamically patched, we also need to dynamically update the relocation info)
  // Both methods takes old_type, so it is able to performe sanity checks on the information removed.
  static void change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type);
  static void remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type);

  // Machine dependent stuff
  #include "incls/_relocInfo_pd.hpp.incl"

 protected:
  // Derived constant, based on format_width which is PD:
  enum {
    offset_width       = nontype_width - format_width,
    offset_mask        = (1<<offset_width) - 1,
    format_mask        = (1<<format_width) - 1
  };
 public:
  enum {
    // Conservatively large estimate of maximum length (in shorts)
    // of any relocation record (probably breakpoints are largest).
    // Extended format is length prefix, data words, and tag/offset suffix.
    length_limit       = 1 + 1 + (3*BytesPerWord/BytesPerShort) + 1,
    have_format        = format_width > 0
  };
};

#define FORWARD_DECLARE_EACH_CLASS(name)              \
class name##_Relocation;
APPLY_TO_RELOCATIONS(FORWARD_DECLARE_EACH_CLASS)
#undef FORWARD_DECLARE_EACH_CLASS



inline relocInfo filler_relocInfo() {
  return relocInfo(relocInfo::none, relocInfo::offset_limit() - relocInfo::offset_unit);
}

inline relocInfo prefix_relocInfo(int datalen) {
  assert(relocInfo::fits_into_immediate(datalen), "datalen in limits");
  return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);
}


// Holder for flyweight relocation objects.
// Although the flyweight subclasses are of varying sizes,
// the holder is "one size fits all".
class RelocationHolder VALUE_OBJ_CLASS_SPEC {
  friend class Relocation;
  friend class CodeSection;

 private:
  // this preallocated memory must accommodate all subclasses of Relocation
  // (this number is assertion-checked in Relocation::operator new)
  enum { _relocbuf_size = 5 };
  void* _relocbuf[ _relocbuf_size ];

 public:
  Relocation* reloc() const { return (Relocation*) &_relocbuf[0]; }
  inline relocInfo::relocType type() const;

  // Add a constant offset to a relocation.  Helper for class Address.
  RelocationHolder plus(int offset) const;

  inline RelocationHolder();                // initializes type to none

  inline RelocationHolder(Relocation* r);   // make a copy

  static const RelocationHolder none;
};

// A RelocIterator iterates through the relocation information of a CodeBlob.
// It is a variable BoundRelocation which is able to take on successive
// values as it is advanced through a code stream.
// Usage:
//   RelocIterator iter(nm);
//   while (iter.next()) {
//     iter.reloc()->some_operation();
//   }
// or:
//   RelocIterator iter(nm);
//   while (iter.next()) {
//     switch (iter.type()) {
//      case relocInfo::oop_type          :
//      case relocInfo::ic_type           :
//      case relocInfo::prim_type         :
//      case relocInfo::uncommon_type     :
//      case relocInfo::runtime_call_type :
//      case relocInfo::internal_word_type:
//      case relocInfo::external_word_type:
//      ...
//     }
//   }

class RelocIterator : public StackObj {
  enum { SECT_CONSTS = 2,
         SECT_LIMIT = 3 };  // must be equal to CodeBuffer::SECT_LIMIT
  friend class Relocation;
  friend class relocInfo;       // for change_reloc_info_for_address only
  typedef relocInfo::relocType relocType;

 private:
  address    _limit;   // stop producing relocations after this _addr
  relocInfo* _current; // the current relocation information
  relocInfo* _end;     // end marker; we're done iterating when _current == _end
515
  nmethod*   _code;    // compiled method containing _addr
D
duke 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
  address    _addr;    // instruction to which the relocation applies
  short      _databuf; // spare buffer for compressed data
  short*     _data;    // pointer to the relocation's data
  short      _datalen; // number of halfwords in _data
  char       _format;  // position within the instruction

  // Base addresses needed to compute targets of section_word_type relocs.
  address    _section_start[SECT_LIMIT];

  void set_has_current(bool b) {
    _datalen = !b ? -1 : 0;
    debug_only(_data = NULL);
  }
  void set_current(relocInfo& ri) {
    _current = &ri;
    set_has_current(true);
  }

  RelocationHolder _rh; // where the current relocation is allocated

  relocInfo* current() const { assert(has_current(), "must have current");
                               return _current; }

  void set_limits(address begin, address limit);

  void advance_over_prefix();    // helper method

  void initialize_misc() {
    set_has_current(false);
    for (int i = 0; i < SECT_LIMIT; i++) {
      _section_start[i] = NULL;  // these will be lazily computed, if needed
    }
  }

  address compute_section_start(int n) const;  // out-of-line helper

552
  void initialize(nmethod* nm, address begin, address limit);
D
duke 已提交
553 554 555 556 557 558 559

  friend class PatchingRelocIterator;
  // make an uninitialized one, for PatchingRelocIterator:
  RelocIterator() { initialize_misc(); }

 public:
  // constructor
560
  RelocIterator(nmethod* nm,     address begin = NULL, address limit = NULL);
D
duke 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
  RelocIterator(CodeSection* cb, address begin = NULL, address limit = NULL);

  // get next reloc info, return !eos
  bool next() {
    _current++;
    assert(_current <= _end, "must not overrun relocInfo");
    if (_current == _end) {
      set_has_current(false);
      return false;
    }
    set_has_current(true);

    if (_current->is_prefix()) {
      advance_over_prefix();
      assert(!current()->is_prefix(), "only one prefix at a time");
    }

    _addr += _current->addr_offset();

    if (_limit != NULL && _addr >= _limit) {
      set_has_current(false);
      return false;
    }

    if (relocInfo::have_format)  _format = current()->format();
    return true;
  }

  // accessors
  address      limit()        const { return _limit; }
  void     set_limit(address x);
  relocType    type()         const { return current()->type(); }
  int          format()       const { return (relocInfo::have_format) ? current()->format() : 0; }
  address      addr()         const { return _addr; }
595
  nmethod*     code()         const { return _code; }
D
duke 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
  short*       data()         const { return _data; }
  int          datalen()      const { return _datalen; }
  bool     has_current()      const { return _datalen >= 0; }

  void       set_addr(address addr) { _addr = addr; }
  bool   addr_in_const()      const { return addr() >= section_start(SECT_CONSTS); }

  address section_start(int n) const {
    address res = _section_start[n];
    return (res != NULL) ? res : compute_section_start(n);
  }

  // The address points to the affected displacement part of the instruction.
  // For RISC, this is just the whole instruction.
  // For Intel, this is an unaligned 32-bit word.

  // type-specific relocation accessors:  oop_Relocation* oop_reloc(), etc.
  #define EACH_TYPE(name)                               \
  inline name##_Relocation* name##_reloc();
  APPLY_TO_RELOCATIONS(EACH_TYPE)
  #undef EACH_TYPE
  // generic relocation accessor; switches on type to call the above
  Relocation* reloc();

  // CodeBlob's have relocation indexes for faster random access:
  static int locs_and_index_size(int code_size, int locs_size);
  // Store an index into [dest_start+dest_count..dest_end).
  // At dest_start[0..dest_count] is the actual relocation information.
  // Everything else up to dest_end is free space for the index.
  static void create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end);

#ifndef PRODUCT
 public:
  void print();
  void print_current();
#endif
};


// A Relocation is a flyweight object allocated within a RelocationHolder.
// It represents the relocation data of relocation record.
// So, the RelocIterator unpacks relocInfos into Relocations.

class Relocation VALUE_OBJ_CLASS_SPEC {
  friend class RelocationHolder;
  friend class RelocIterator;

 private:
  static void guarantee_size();

  // When a relocation has been created by a RelocIterator,
  // this field is non-null.  It allows the relocation to know
  // its context, such as the address to which it applies.
  RelocIterator* _binding;

 protected:
  RelocIterator* binding() const {
    assert(_binding != NULL, "must be bound");
    return _binding;
  }
  void set_binding(RelocIterator* b) {
    assert(_binding == NULL, "must be unbound");
    _binding = b;
    assert(_binding != NULL, "must now be bound");
  }

  Relocation() {
    _binding = NULL;
  }

  static RelocationHolder newHolder() {
    return RelocationHolder();
  }

 public:
  void* operator new(size_t size, const RelocationHolder& holder) {
    if (size > sizeof(holder._relocbuf)) guarantee_size();
    assert((void* const *)holder.reloc() == &holder._relocbuf[0], "ptrs must agree");
    return holder.reloc();
  }

  // make a generic relocation for a given type (if possible)
  static RelocationHolder spec_simple(relocInfo::relocType rtype);

  // here is the type-specific hook which writes relocation data:
  virtual void pack_data_to(CodeSection* dest) { }

  // here is the type-specific hook which reads (unpacks) relocation data:
  virtual void unpack_data() {
    assert(datalen()==0 || type()==relocInfo::none, "no data here");
  }

 protected:
  // Helper functions for pack_data_to() and unpack_data().

  // Most of the compression logic is confined here.
  // (The "immediate data" mechanism of relocInfo works independently
  // of this stuff, and acts to further compress most 1-word data prefixes.)

  // A variable-width int is encoded as a short if it will fit in 16 bits.
  // The decoder looks at datalen to decide whether to unpack short or jint.
  // Most relocation records are quite simple, containing at most two ints.

  static bool is_short(jint x) { return x == (short)x; }
  static short* add_short(short* p, int x)  { *p++ = x; return p; }
  static short* add_jint (short* p, jint x) {
    *p++ = relocInfo::data0_from_int(x); *p++ = relocInfo::data1_from_int(x);
    return p;
  }
  static short* add_var_int(short* p, jint x) {   // add a variable-width int
    if (is_short(x))  p = add_short(p, x);
    else              p = add_jint (p, x);
    return p;
  }

  static short* pack_1_int_to(short* p, jint x0) {
    // Format is one of:  [] [x] [Xx]
    if (x0 != 0)  p = add_var_int(p, x0);
    return p;
  }
  int unpack_1_int() {
    assert(datalen() <= 2, "too much data");
    return relocInfo::jint_data_at(0, data(), datalen());
  }

  // With two ints, the short form is used only if both ints are short.
  short* pack_2_ints_to(short* p, jint x0, jint x1) {
    // Format is one of:  [] [x y?] [Xx Y?y]
    if (x0 == 0 && x1 == 0) {
      // no halfwords needed to store zeroes
    } else if (is_short(x0) && is_short(x1)) {
      // 1-2 halfwords needed to store shorts
      p = add_short(p, x0); if (x1!=0) p = add_short(p, x1);
    } else {
      // 3-4 halfwords needed to store jints
      p = add_jint(p, x0);             p = add_var_int(p, x1);
    }
    return p;
  }
  void unpack_2_ints(jint& x0, jint& x1) {
    int    dlen = datalen();
    short* dp  = data();
    if (dlen <= 2) {
      x0 = relocInfo::short_data_at(0, dp, dlen);
      x1 = relocInfo::short_data_at(1, dp, dlen);
    } else {
      assert(dlen <= 4, "too much data");
      x0 = relocInfo::jint_data_at(0, dp, dlen);
      x1 = relocInfo::jint_data_at(2, dp, dlen);
    }
  }

 protected:
  // platform-dependent utilities for decoding and patching instructions
  void       pd_set_data_value       (address x, intptr_t off); // a set or mem-ref
  address    pd_call_destination     (address orig_addr = NULL);
  void       pd_set_call_destination (address x);
  void       pd_swap_in_breakpoint   (address x, short* instrs, int instrlen);
  void       pd_swap_out_breakpoint  (address x, short* instrs, int instrlen);
  static int pd_breakpoint_size      ();

  // this extracts the address of an address in the code stream instead of the reloc data
  address* pd_address_in_code       ();

  // this extracts an address from the code stream instead of the reloc data
  address  pd_get_address_from_code ();

  // these convert from byte offsets, to scaled offsets, to addresses
  static jint scaled_offset(address x, address base) {
    int byte_offset = x - base;
    int offset = -byte_offset / relocInfo::addr_unit();
    assert(address_from_scaled_offset(offset, base) == x, "just checkin'");
    return offset;
  }
  static jint scaled_offset_null_special(address x, address base) {
    // Some relocations treat offset=0 as meaning NULL.
    // Handle this extra convention carefully.
    if (x == NULL)  return 0;
    assert(x != base, "offset must not be zero");
    return scaled_offset(x, base);
  }
  static address address_from_scaled_offset(jint offset, address base) {
    int byte_offset = -( offset * relocInfo::addr_unit() );
    return base + byte_offset;
  }

  // these convert between indexes and addresses in the runtime system
  static int32_t runtime_address_to_index(address runtime_address);
  static address index_to_runtime_address(int32_t index);

  // helpers for mapping between old and new addresses after a move or resize
  address old_addr_for(address newa, const CodeBuffer* src, CodeBuffer* dest);
  address new_addr_for(address olda, const CodeBuffer* src, CodeBuffer* dest);
  void normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections = false);

 public:
  // accessors which only make sense for a bound Relocation
793 794 795
  address  addr()         const { return binding()->addr(); }
  nmethod* code()         const { return binding()->code(); }
  bool     addr_in_const() const { return binding()->addr_in_const(); }
D
duke 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
 protected:
  short*   data()         const { return binding()->data(); }
  int      datalen()      const { return binding()->datalen(); }
  int      format()       const { return binding()->format(); }

 public:
  virtual relocInfo::relocType type()            { return relocInfo::none; }

  // is it a call instruction?
  virtual bool is_call()                         { return false; }

  // is it a data movement instruction?
  virtual bool is_data()                         { return false; }

  // some relocations can compute their own values
  virtual address  value();

  // all relocations are able to reassert their values
  virtual void set_value(address x);

  virtual void clear_inline_cache()              { }

  // This method assumes that all virtual/static (inline) caches are cleared (since for static_call_type and
  // ic_call_type is not always posisition dependent (depending on the state of the cache)). However, this is
  // probably a reasonable assumption, since empty caches simplifies code reloacation.
  virtual void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { }

  void print();
};


// certain inlines must be deferred until class Relocation is defined:

inline RelocationHolder::RelocationHolder() {
  // initialize the vtbl, just to keep things type-safe
  new(*this) Relocation();
}


inline RelocationHolder::RelocationHolder(Relocation* r) {
  // wordwise copy from r (ok if it copies garbage after r)
  for (int i = 0; i < _relocbuf_size; i++) {
    _relocbuf[i] = ((void**)r)[i];
  }
}


relocInfo::relocType RelocationHolder::type() const {
  return reloc()->type();
}

// A DataRelocation always points at a memory or load-constant instruction..
// It is absolute on most machines, and the constant is split on RISCs.
// The specific subtypes are oop, external_word, and internal_word.
// By convention, the "value" does not include a separately reckoned "offset".
class DataRelocation : public Relocation {
 public:
  bool          is_data()                      { return true; }

  // both target and offset must be computed somehow from relocation data
  virtual int    offset()                      { return 0; }
  address         value()                      = 0;
  void        set_value(address x)             { set_value(x, offset()); }
  void        set_value(address x, intptr_t o) {
    if (addr_in_const())
      *(address*)addr() = x;
    else
      pd_set_data_value(x, o);
  }

  // The "o" (displacement) argument is relevant only to split relocations
  // on RISC machines.  In some CPUs (SPARC), the set-hi and set-lo ins'ns
  // can encode more than 32 bits between them.  This allows compilers to
  // share set-hi instructions between addresses that differ by a small
  // offset (e.g., different static variables in the same class).
  // On such machines, the "x" argument to set_value on all set-lo
  // instructions must be the same as the "x" argument for the
  // corresponding set-hi instructions.  The "o" arguments for the
  // set-hi instructions are ignored, and must not affect the high-half
  // immediate constant.  The "o" arguments for the set-lo instructions are
  // added into the low-half immediate constant, and must not overflow it.
};

// A CallRelocation always points at a call instruction.
// It is PC-relative on most machines.
class CallRelocation : public Relocation {
 public:
  bool is_call() { return true; }

  address  destination()                    { return pd_call_destination(); }
  void     set_destination(address x); // pd_set_call_destination

  void     fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
  address  value()                          { return destination();  }
  void     set_value(address x)             { set_destination(x); }
};

class oop_Relocation : public DataRelocation {
  relocInfo::relocType type() { return relocInfo::oop_type; }

 public:
  // encode in one of these formats:  [] [n] [n l] [Nn l] [Nn Ll]
  // an oop in the CodeBlob's oop pool
  static RelocationHolder spec(int oop_index, int offset = 0) {
    assert(oop_index > 0, "must be a pool-resident oop");
    RelocationHolder rh = newHolder();
    new(rh) oop_Relocation(oop_index, offset);
    return rh;
  }
  // an oop in the instruction stream
  static RelocationHolder spec_for_immediate() {
    const int oop_index = 0;
    const int offset    = 0;    // if you want an offset, use the oop pool
    RelocationHolder rh = newHolder();
    new(rh) oop_Relocation(oop_index, offset);
    return rh;
  }

 private:
  jint _oop_index;                  // if > 0, index into CodeBlob::oop_at
  jint _offset;                     // byte offset to apply to the oop itself

  oop_Relocation(int oop_index, int offset) {
    _oop_index = oop_index; _offset = offset;
  }

  friend class RelocIterator;
  oop_Relocation() { }

 public:
  int oop_index() { return _oop_index; }
  int offset()    { return _offset; }

  // data is packed in "2_ints" format:  [i o] or [Ii Oo]
  void pack_data_to(CodeSection* dest);
  void unpack_data();

  void fix_oop_relocation();        // reasserts oop value

  address value()  { return (address) *oop_addr(); }

  bool oop_is_immediate()  { return oop_index() == 0; }

  oop* oop_addr();                  // addr or &pool[jint_data]
  oop  oop_value();                 // *oop_addr
  // Note:  oop_value transparently converts Universe::non_oop_word to NULL.
};

class virtual_call_Relocation : public CallRelocation {
  relocInfo::relocType type() { return relocInfo::virtual_call_type; }

 public:
  // "first_oop" points to the first associated set-oop.
  // The oop_limit helps find the last associated set-oop.
  // (See comments at the top of this file.)
  static RelocationHolder spec(address first_oop, address oop_limit = NULL) {
    RelocationHolder rh = newHolder();
    new(rh) virtual_call_Relocation(first_oop, oop_limit);
    return rh;
  }

  virtual_call_Relocation(address first_oop, address oop_limit) {
    _first_oop = first_oop; _oop_limit = oop_limit;
    assert(first_oop != NULL, "first oop address must be specified");
  }

 private:
  address _first_oop;               // location of first set-oop instruction
  address _oop_limit;               // search limit for set-oop instructions

  friend class RelocIterator;
  virtual_call_Relocation() { }


 public:
  address first_oop();
  address oop_limit();

  // data is packed as scaled offsets in "2_ints" format:  [f l] or [Ff Ll]
  // oop_limit is set to 0 if the limit falls somewhere within the call.
  // When unpacking, a zero oop_limit is taken to refer to the end of the call.
  // (This has the effect of bringing in the call's delay slot on SPARC.)
  void pack_data_to(CodeSection* dest);
  void unpack_data();

  void clear_inline_cache();

  // Figure out where an ic_call is hiding, given a set-oop or call.
  // Either ic_call or first_oop must be non-null; the other is deduced.
985
  // Code if non-NULL must be the nmethod, else it is deduced.
D
duke 已提交
986 987 988 989
  // The address of the patchable oop is also deduced.
  // The returned iterator will enumerate over the oops and the ic_call,
  // as well as any other relocations that happen to be in that span of code.
  // Recognize relevant set_oops with:  oop_reloc()->oop_addr() == oop_addr.
990
  static RelocIterator parse_ic(nmethod* &nm, address &ic_call, address &first_oop, oop* &oop_addr, bool *is_optimized);
D
duke 已提交
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
};


class opt_virtual_call_Relocation : public CallRelocation {
  relocInfo::relocType type() { return relocInfo::opt_virtual_call_type; }

 public:
  static RelocationHolder spec() {
    RelocationHolder rh = newHolder();
    new(rh) opt_virtual_call_Relocation();
    return rh;
  }

 private:
  friend class RelocIterator;
  opt_virtual_call_Relocation() { }

 public:
  void clear_inline_cache();

  // find the matching static_stub
  address static_stub();
};


class static_call_Relocation : public CallRelocation {
  relocInfo::relocType type() { return relocInfo::static_call_type; }

 public:
  static RelocationHolder spec() {
    RelocationHolder rh = newHolder();
    new(rh) static_call_Relocation();
    return rh;
  }

 private:
  friend class RelocIterator;
  static_call_Relocation() { }

 public:
  void clear_inline_cache();

  // find the matching static_stub
  address static_stub();
};

class static_stub_Relocation : public Relocation {
  relocInfo::relocType type() { return relocInfo::static_stub_type; }

 public:
  static RelocationHolder spec(address static_call) {
    RelocationHolder rh = newHolder();
    new(rh) static_stub_Relocation(static_call);
    return rh;
  }

 private:
  address _static_call;             // location of corresponding static_call

  static_stub_Relocation(address static_call) {
    _static_call = static_call;
  }

  friend class RelocIterator;
  static_stub_Relocation() { }

 public:
  void clear_inline_cache();

  address static_call() { return _static_call; }

  // data is packed as a scaled offset in "1_int" format:  [c] or [Cc]
  void pack_data_to(CodeSection* dest);
  void unpack_data();
};

class runtime_call_Relocation : public CallRelocation {
  relocInfo::relocType type() { return relocInfo::runtime_call_type; }

 public:
  static RelocationHolder spec() {
    RelocationHolder rh = newHolder();
    new(rh) runtime_call_Relocation();
    return rh;
  }

 private:
  friend class RelocIterator;
  runtime_call_Relocation() { }

 public:
};

class external_word_Relocation : public DataRelocation {
  relocInfo::relocType type() { return relocInfo::external_word_type; }

 public:
  static RelocationHolder spec(address target) {
    assert(target != NULL, "must not be null");
    RelocationHolder rh = newHolder();
    new(rh) external_word_Relocation(target);
    return rh;
  }

  // Use this one where all 32/64 bits of the target live in the code stream.
  // The target must be an intptr_t, and must be absolute (not relative).
  static RelocationHolder spec_for_immediate() {
    RelocationHolder rh = newHolder();
    new(rh) external_word_Relocation(NULL);
    return rh;
  }

 private:
  address _target;                  // address in runtime

  external_word_Relocation(address target) {
    _target = target;
  }

  friend class RelocIterator;
  external_word_Relocation() { }

 public:
  // data is packed as a well-known address in "1_int" format:  [a] or [Aa]
  // The function runtime_address_to_index is used to turn full addresses
  // to short indexes, if they are pre-registered by the stub mechanism.
  // If the "a" value is 0 (i.e., _target is NULL), the address is stored
  // in the code stream.  See external_word_Relocation::target().
  void pack_data_to(CodeSection* dest);
  void unpack_data();

  void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
  address  target();        // if _target==NULL, fetch addr from code stream
  address  value()          { return target(); }
};

class internal_word_Relocation : public DataRelocation {
  relocInfo::relocType type() { return relocInfo::internal_word_type; }

 public:
  static RelocationHolder spec(address target) {
    assert(target != NULL, "must not be null");
    RelocationHolder rh = newHolder();
    new(rh) internal_word_Relocation(target);
    return rh;
  }

  // use this one where all the bits of the target can fit in the code stream:
  static RelocationHolder spec_for_immediate() {
    RelocationHolder rh = newHolder();
    new(rh) internal_word_Relocation(NULL);
    return rh;
  }

  internal_word_Relocation(address target) {
    _target  = target;
    _section = -1;  // self-relative
  }

 protected:
  address _target;                  // address in CodeBlob
  int     _section;                 // section providing base address, if any

  friend class RelocIterator;
  internal_word_Relocation() { }

  // bit-width of LSB field in packed offset, if section >= 0
  enum { section_width = 2 }; // must equal CodeBuffer::sect_bits

 public:
  // data is packed as a scaled offset in "1_int" format:  [o] or [Oo]
  // If the "o" value is 0 (i.e., _target is NULL), the offset is stored
  // in the code stream.  See internal_word_Relocation::target().
  // If _section is not -1, it is appended to the low bits of the offset.
  void pack_data_to(CodeSection* dest);
  void unpack_data();

  void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
  address  target();        // if _target==NULL, fetch addr from code stream
  int      section()        { return _section;   }
  address  value()          { return target();   }
};

class section_word_Relocation : public internal_word_Relocation {
  relocInfo::relocType type() { return relocInfo::section_word_type; }

 public:
  static RelocationHolder spec(address target, int section) {
    RelocationHolder rh = newHolder();
    new(rh) section_word_Relocation(target, section);
    return rh;
  }

  section_word_Relocation(address target, int section) {
    assert(target != NULL, "must not be null");
    assert(section >= 0, "must be a valid section");
    _target  = target;
    _section = section;
  }

  //void pack_data_to -- inherited
  void unpack_data();

 private:
  friend class RelocIterator;
  section_word_Relocation() { }
};


class poll_Relocation : public Relocation {
  bool          is_data()                      { return true; }
  relocInfo::relocType type() { return relocInfo::poll_type; }
1203
  void     fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
D
duke 已提交
1204 1205 1206 1207 1208
};

class poll_return_Relocation : public Relocation {
  bool          is_data()                      { return true; }
  relocInfo::relocType type() { return relocInfo::poll_return_type; }
1209
  void     fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
D
duke 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
};


class breakpoint_Relocation : public Relocation {
  relocInfo::relocType type() { return relocInfo::breakpoint_type; }

  enum {
    // attributes which affect the interpretation of the data:
    removable_attr = 0x0010,   // buffer [i...] allows for undoing the trap
    internal_attr  = 0x0020,   // the target is an internal addr (local stub)
    settable_attr  = 0x0040,   // the target is settable

    // states which can change over time:
    enabled_state  = 0x0100,   // breakpoint must be active in running code
    active_state   = 0x0200,   // breakpoint instruction actually in code

    kind_mask      = 0x000F,   // mask for extracting kind
    high_bit       = 0x4000    // extra bit which is always set
  };

 public:
  enum {
    // kinds:
    initialization = 1,
    safepoint      = 2
  };

  // If target is NULL, 32 bits are reserved for a later set_target().
  static RelocationHolder spec(int kind, address target = NULL, bool internal_target = false) {
    RelocationHolder rh = newHolder();
    new(rh) breakpoint_Relocation(kind, target, internal_target);
    return rh;
  }

 private:
  // We require every bits value to NOT to fit into relocInfo::datalen_width,
  // because we are going to actually store state in the reloc, and so
  // cannot allow it to be compressed (and hence copied by the iterator).

  short   _bits;                  // bit-encoded kind, attrs, & state
  address _target;

  breakpoint_Relocation(int kind, address target, bool internal_target);

  friend class RelocIterator;
  breakpoint_Relocation() { }

  short    bits()       const { return _bits; }
  short&   live_bits()  const { return data()[0]; }
  short*   instrs()     const { return data() + datalen() - instrlen(); }
  int      instrlen()   const { return removable() ? pd_breakpoint_size() : 0; }

  void set_bits(short x) {
    assert(live_bits() == _bits, "must be the only mutator of reloc info");
    live_bits() = _bits = x;
  }

 public:
  address  target()     const;
  void set_target(address x);

  int  kind()           const { return  bits() & kind_mask; }
  bool enabled()        const { return (bits() &  enabled_state) != 0; }
  bool active()         const { return (bits() &   active_state) != 0; }
  bool internal()       const { return (bits() &  internal_attr) != 0; }
  bool removable()      const { return (bits() & removable_attr) != 0; }
  bool settable()       const { return (bits() &  settable_attr) != 0; }

  void set_enabled(bool b);     // to activate, you must also say set_active
  void set_active(bool b);      // actually inserts bpt (must be enabled 1st)

  // data is packed as 16 bits, followed by the target (1 or 2 words), followed
  // if necessary by empty storage for saving away original instruction bytes.
  void pack_data_to(CodeSection* dest);
  void unpack_data();

  // during certain operations, breakpoints must be out of the way:
  void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
    assert(!active(), "cannot perform relocation on enabled breakpoints");
  }
};


// We know all the xxx_Relocation classes, so now we can define these:
#define EACH_CASE(name)                                         \
inline name##_Relocation* RelocIterator::name##_reloc() {       \
  assert(type() == relocInfo::name##_type, "type must agree");  \
  /* The purpose of the placed "new" is to re-use the same */   \
  /* stack storage for each new iteration. */                   \
  name##_Relocation* r = new(_rh) name##_Relocation();          \
  r->set_binding(this);                                         \
  r->name##_Relocation::unpack_data();                          \
  return r;                                                     \
}
APPLY_TO_RELOCATIONS(EACH_CASE);
#undef EACH_CASE

1307 1308
inline RelocIterator::RelocIterator(nmethod* nm, address begin, address limit) {
  initialize(nm, begin, limit);
D
duke 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
}

// if you are going to patch code, you should use this subclass of
// RelocIterator
class PatchingRelocIterator : public RelocIterator {
 private:
  RelocIterator _init_state;

  void prepass();               // deactivates all breakpoints
  void postpass();              // reactivates all enabled breakpoints

  // do not copy these puppies; it would have unpredictable side effects
  // these are private and have no bodies defined because they should not be called
  PatchingRelocIterator(const RelocIterator&);
  void        operator=(const RelocIterator&);

 public:
1326 1327
  PatchingRelocIterator(nmethod* nm, address begin = NULL, address limit = NULL)
    : RelocIterator(nm, begin, limit)                { prepass();  }
D
duke 已提交
1328 1329 1330

  ~PatchingRelocIterator()                           { postpass(); }
};