relocInfo.hpp revision 2273:1d1603768966
1130803Smarcel/*
2130803Smarcel * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3130803Smarcel * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4130803Smarcel *
5130803Smarcel * This code is free software; you can redistribute it and/or modify it
6130803Smarcel * under the terms of the GNU General Public License version 2 only, as
7130803Smarcel * published by the Free Software Foundation.
8130803Smarcel *
9130803Smarcel * This code is distributed in the hope that it will be useful, but WITHOUT
10130803Smarcel * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11130803Smarcel * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12130803Smarcel * version 2 for more details (a copy is included in the LICENSE file that
13130803Smarcel * accompanied this code).
14130803Smarcel *
15130803Smarcel * You should have received a copy of the GNU General Public License version
16130803Smarcel * 2 along with this work; if not, write to the Free Software Foundation,
17130803Smarcel * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18130803Smarcel *
19130803Smarcel * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20130803Smarcel * or visit www.oracle.com if you need additional information or have any
21130803Smarcel * questions.
22130803Smarcel *
23130803Smarcel */
24130803Smarcel
25130803Smarcel#ifndef SHARE_VM_CODE_RELOCINFO_HPP
26130803Smarcel#define SHARE_VM_CODE_RELOCINFO_HPP
27130803Smarcel
28130803Smarcel#include "memory/allocation.hpp"
29130803Smarcel#include "utilities/top.hpp"
30130803Smarcel
31130803Smarcel// Types in this file:
32130803Smarcel//    relocInfo
33130803Smarcel//      One element of an array of halfwords encoding compressed relocations.
34130803Smarcel//      Also, the source of relocation types (relocInfo::oop_type, ...).
35130803Smarcel//    Relocation
36130803Smarcel//      A flyweight object representing a single relocation.
37130803Smarcel//      It is fully unpacked from the compressed relocation array.
38130803Smarcel//    oop_Relocation, ... (subclasses of Relocation)
39130803Smarcel//      The location of some type-specific operations (oop_addr, ...).
40130803Smarcel//      Also, the source of relocation specs (oop_Relocation::spec, ...).
41130803Smarcel//    RelocationHolder
42130803Smarcel//      A ValueObj type which acts as a union holding a Relocation object.
43130803Smarcel//      Represents a relocation spec passed into a CodeBuffer during assembly.
44130803Smarcel//    RelocIterator
45130803Smarcel//      A StackObj which iterates over the relocations associated with
46130803Smarcel//      a range of code addresses.  Can be used to operate a copy of code.
47130803Smarcel//    PatchingRelocIterator
48130803Smarcel//      Specialized subtype of RelocIterator which removes breakpoints
49130803Smarcel//      temporarily during iteration, then restores them.
50130803Smarcel//    BoundRelocation
51130803Smarcel//      An _internal_ type shared by packers and unpackers of relocations.
52130803Smarcel//      It pastes together a RelocationHolder with some pointers into
53130803Smarcel//      code and relocInfo streams.
54130803Smarcel
55130803Smarcel
56130803Smarcel// Notes on relocType:
57130803Smarcel//
58130803Smarcel// These hold enough information to read or write a value embedded in
59130803Smarcel// the instructions of an CodeBlob.  They're used to update:
60130803Smarcel//
61130803Smarcel//   1) embedded oops     (isOop()          == true)
62130803Smarcel//   2) inline caches     (isIC()           == true)
63130803Smarcel//   3) runtime calls     (isRuntimeCall()  == true)
64130803Smarcel//   4) internal word ref (isInternalWord() == true)
65130803Smarcel//   5) external word ref (isExternalWord() == true)
66130803Smarcel//
67130803Smarcel// when objects move (GC) or if code moves (compacting the code heap).
68130803Smarcel// They are also used to patch the code (if a call site must change)
69130803Smarcel//
70130803Smarcel// A relocInfo is represented in 16 bits:
71130803Smarcel//   4 bits indicating the relocation type
72130803Smarcel//  12 bits indicating the offset from the previous relocInfo address
73130803Smarcel//
74130803Smarcel// The offsets accumulate along the relocInfo stream to encode the
75130803Smarcel// address within the CodeBlob, which is named RelocIterator::addr().
76130803Smarcel// The address of a particular relocInfo always points to the first
77130803Smarcel// byte of the relevant instruction (and not to any of its subfields
78130803Smarcel// or embedded immediate constants).
79130803Smarcel//
80130803Smarcel// The offset value is scaled appropriately for the target machine.
81130803Smarcel// (See relocInfo_<arch>.hpp for the offset scaling.)
82130803Smarcel//
83130803Smarcel// On some machines, there may also be a "format" field which may provide
84130803Smarcel// additional information about the format of the instruction stream
85130803Smarcel// at the corresponding code address.  The format value is usually zero.
86130803Smarcel// Any machine (such as Intel) whose instructions can sometimes contain
87130803Smarcel// more than one relocatable constant needs format codes to distinguish
88130803Smarcel// which operand goes with a given relocation.
89130803Smarcel//
90130803Smarcel// If the target machine needs N format bits, the offset has 12-N bits,
91130803Smarcel// the format is encoded between the offset and the type, and the
92130803Smarcel// relocInfo_<arch>.hpp file has manifest constants for the format codes.
93130803Smarcel//
94130803Smarcel// If the type is "data_prefix_tag" then the offset bits are further encoded,
95130803Smarcel// and in fact represent not a code-stream offset but some inline data.
96130803Smarcel// The data takes the form of a counted sequence of halfwords, which
97130803Smarcel// precedes the actual relocation record.  (Clients never see it directly.)
98130803Smarcel// The interpetation of this extra data depends on the relocation type.
99130803Smarcel//
100130803Smarcel// On machines that have 32-bit immediate fields, there is usually
101130803Smarcel// little need for relocation "prefix" data, because the instruction stream
102130803Smarcel// is a perfectly reasonable place to store the value.  On machines in
103130803Smarcel// which 32-bit values must be "split" across instructions, the relocation
104130803Smarcel// data is the "true" specification of the value, which is then applied
105130803Smarcel// to some field of the instruction (22 or 13 bits, on SPARC).
106130803Smarcel//
107130803Smarcel// Whenever the location of the CodeBlob changes, any PC-relative
108130803Smarcel// relocations, and any internal_word_type relocations, must be reapplied.
109130803Smarcel// After the GC runs, oop_type relocations must be reapplied.
110130803Smarcel//
111130803Smarcel//
112130803Smarcel// Here are meanings of the types:
113130803Smarcel//
114130803Smarcel// relocInfo::none -- a filler record
115130803Smarcel//   Value:  none
116130803Smarcel//   Instruction: The corresponding code address is ignored
117130803Smarcel//   Data:  Any data prefix and format code are ignored
118130803Smarcel//   (This means that any relocInfo can be disabled by setting
119130803Smarcel//   its type to none.  See relocInfo::remove.)
120130803Smarcel//
121130803Smarcel// relocInfo::oop_type -- a reference to an oop
122130803Smarcel//   Value:  an oop, or else the address (handle) of an oop
123130803Smarcel//   Instruction types: memory (load), set (load address)
124130803Smarcel//   Data:  []       an oop stored in 4 bytes of instruction
125130803Smarcel//          [n]      n is the index of an oop in the CodeBlob's oop pool
126130803Smarcel//          [[N]n l] and l is a byte offset to be applied to the oop
127130803Smarcel//          [Nn Ll]  both index and offset may be 32 bits if necessary
128130803Smarcel//   Here is a special hack, used only by the old compiler:
129130803Smarcel//          [[N]n 00] the value is the __address__ of the nth oop in the pool
130130803Smarcel//   (Note that the offset allows optimal references to class variables.)
131130803Smarcel//
132130803Smarcel// relocInfo::internal_word_type -- an address within the same CodeBlob
133130803Smarcel// relocInfo::section_word_type -- same, but can refer to another section
134130803Smarcel//   Value:  an address in the CodeBlob's code or constants section
135130803Smarcel//   Instruction types: memory (load), set (load address)
136130803Smarcel//   Data:  []     stored in 4 bytes of instruction
137130803Smarcel//          [[L]l] a relative offset (see [About Offsets] below)
138130803Smarcel//   In the case of section_word_type, the offset is relative to a section
139130803Smarcel//   base address, and the section number (e.g., SECT_INSTS) is encoded
140130803Smarcel//   into the low two bits of the offset L.
141130803Smarcel//
142130803Smarcel// relocInfo::external_word_type -- a fixed address in the runtime system
143130803Smarcel//   Value:  an address
144130803Smarcel//   Instruction types: memory (load), set (load address)
145130803Smarcel//   Data:  []   stored in 4 bytes of instruction
146130803Smarcel//          [n]  the index of a "well-known" stub (usual case on RISC)
147130803Smarcel//          [Ll] a 32-bit address
148130803Smarcel//
149130803Smarcel// relocInfo::runtime_call_type -- a fixed subroutine in the runtime system
150130803Smarcel//   Value:  an address
151130803Smarcel//   Instruction types: PC-relative call (or a PC-relative branch)
152130803Smarcel//   Data:  []   stored in 4 bytes of instruction
153130803Smarcel//
154130803Smarcel// relocInfo::static_call_type -- a static call
155130803Smarcel//   Value:  an CodeBlob, a stub, or a fixup routine
156130803Smarcel//   Instruction types: a call
157130803Smarcel//   Data:  []
158130803Smarcel//   The identity of the callee is extracted from debugging information.
159130803Smarcel//   //%note reloc_3
160130803Smarcel//
161130803Smarcel// relocInfo::virtual_call_type -- a virtual call site (which includes an inline
162130803Smarcel//                                 cache)
163130803Smarcel//   Value:  an CodeBlob, a stub, the interpreter, or a fixup routine
164130803Smarcel//   Instruction types: a call, plus some associated set-oop instructions
165130803Smarcel//   Data:  []       the associated set-oops are adjacent to the call
166130803Smarcel//          [n]      n is a relative offset to the first set-oop
167130803Smarcel//          [[N]n l] and l is a limit within which the set-oops occur
168130803Smarcel//          [Nn Ll]  both n and l may be 32 bits if necessary
169130803Smarcel//   The identity of the callee is extracted from debugging information.
170130803Smarcel//
171130803Smarcel// relocInfo::opt_virtual_call_type -- a virtual call site that is statically bound
172130803Smarcel//
173130803Smarcel//    Same info as a static_call_type. We use a special type, so the handling of
174130803Smarcel//    virtuals and statics are separated.
175130803Smarcel//
176130803Smarcel//
177130803Smarcel//   The offset n points to the first set-oop.  (See [About Offsets] below.)
178130803Smarcel//   In turn, the set-oop instruction specifies or contains an oop cell devoted
179130803Smarcel//   exclusively to the IC call, which can be patched along with the call.
180130803Smarcel//
181130803Smarcel//   The locations of any other set-oops are found by searching the relocation
182130803Smarcel//   information starting at the first set-oop, and continuing until all
183130803Smarcel//   relocations up through l have been inspected.  The value l is another
184130803Smarcel//   relative offset.  (Both n and l are relative to the call's first byte.)
185130803Smarcel//
186130803Smarcel//   The limit l of the search is exclusive.  However, if it points within
187130803Smarcel//   the call (e.g., offset zero), it is adjusted to point after the call and
188130803Smarcel//   any associated machine-specific delay slot.
189130803Smarcel//
190130803Smarcel//   Since the offsets could be as wide as 32-bits, these conventions
191130803Smarcel//   put no restrictions whatever upon code reorganization.
192130803Smarcel//
193130803Smarcel//   The compiler is responsible for ensuring that transition from a clean
194130803Smarcel//   state to a monomorphic compiled state is MP-safe.  This implies that
195130803Smarcel//   the system must respond well to intermediate states where a random
196130803Smarcel//   subset of the set-oops has been correctly from the clean state
197130803Smarcel//   upon entry to the VEP of the compiled method.  In the case of a
198130803Smarcel//   machine (Intel) with a single set-oop instruction, the 32-bit
199130803Smarcel//   immediate field must not straddle a unit of memory coherence.
200130803Smarcel//   //%note reloc_3
201130803Smarcel//
202130803Smarcel// relocInfo::breakpoint_type -- a conditional breakpoint in the code
203130803Smarcel//   Value:  none
204130803Smarcel//   Instruction types: any whatsoever
205130803Smarcel//   Data:  [b [T]t  i...]
206130803Smarcel//   The b is a bit-packed word representing the breakpoint's attributes.
207130803Smarcel//   The t is a target address which the breakpoint calls (when it is enabled).
208130803Smarcel//   The i... is a place to store one or two instruction words overwritten
209130803Smarcel//   by a trap, so that the breakpoint may be subsequently removed.
210130803Smarcel//
211130803Smarcel// relocInfo::static_stub_type -- an extra stub for each static_call_type
212130803Smarcel//   Value:  none
213130803Smarcel//   Instruction types: a virtual call:  { set_oop; jump; }
214130803Smarcel//   Data:  [[N]n]  the offset of the associated static_call reloc
215130803Smarcel//   This stub becomes the target of a static call which must be upgraded
216130803Smarcel//   to a virtual call (because the callee is interpreted).
217130803Smarcel//   See [About Offsets] below.
218130803Smarcel//   //%note reloc_2
219130803Smarcel//
220130803Smarcel// For example:
221130803Smarcel//
222130803Smarcel//   INSTRUCTIONS                        RELOC: TYPE    PREFIX DATA
223130803Smarcel//   ------------                               ----    -----------
224130803Smarcel// sethi      %hi(myObject),  R               oop_type [n(myObject)]
225130803Smarcel// ld      [R+%lo(myObject)+fldOffset], R2    oop_type [n(myObject) fldOffset]
226130803Smarcel// add R2, 1, R2
227130803Smarcel// st  R2, [R+%lo(myObject)+fldOffset]        oop_type [n(myObject) fldOffset]
228130803Smarcel//%note reloc_1
229130803Smarcel//
230130803Smarcel// This uses 4 instruction words, 8 relocation halfwords,
231130803Smarcel// and an entry (which is sharable) in the CodeBlob's oop pool,
232130803Smarcel// for a total of 36 bytes.
233130803Smarcel//
234130803Smarcel// Note that the compiler is responsible for ensuring the "fldOffset" when
235130803Smarcel// added to "%lo(myObject)" does not overflow the immediate fields of the
236130803Smarcel// memory instructions.
237130803Smarcel//
238130803Smarcel//
239130803Smarcel// [About Offsets] Relative offsets are supplied to this module as
240130803Smarcel// positive byte offsets, but they may be internally stored scaled
241130803Smarcel// and/or negated, depending on what is most compact for the target
242130803Smarcel// system.  Since the object pointed to by the offset typically
243130803Smarcel// precedes the relocation address, it is profitable to store
244130803Smarcel// these negative offsets as positive numbers, but this decision
245130803Smarcel// is internal to the relocation information abstractions.
246130803Smarcel//
247130803Smarcel
248130803Smarcelclass Relocation;
249130803Smarcelclass CodeBuffer;
250130803Smarcelclass CodeSection;
251130803Smarcelclass RelocIterator;
252130803Smarcel
253130803Smarcelclass relocInfo VALUE_OBJ_CLASS_SPEC {
254130803Smarcel  friend class RelocIterator;
255130803Smarcel public:
256130803Smarcel  enum relocType {
257130803Smarcel    none                    =  0, // Used when no relocation should be generated
258130803Smarcel    oop_type                =  1, // embedded oop
259130803Smarcel    virtual_call_type       =  2, // a standard inline cache call for a virtual send
260130803Smarcel    opt_virtual_call_type   =  3, // a virtual call that has been statically bound (i.e., no IC cache)
261130803Smarcel    static_call_type        =  4, // a static send
262130803Smarcel    static_stub_type        =  5, // stub-entry for static send  (takes care of interpreter case)
263130803Smarcel    runtime_call_type       =  6, // call to fixed external routine
264130803Smarcel    external_word_type      =  7, // reference to fixed external address
265130803Smarcel    internal_word_type      =  8, // reference within the current code blob
266130803Smarcel    section_word_type       =  9, // internal, but a cross-section reference
267130803Smarcel    poll_type               = 10, // polling instruction for safepoints
268130803Smarcel    poll_return_type        = 11, // polling instruction for safepoints at return
269130803Smarcel    breakpoint_type         = 12, // an initialization barrier or safepoint
270130803Smarcel    yet_unused_type         = 13, // Still unused
271130803Smarcel    yet_unused_type_2       = 14, // Still unused
272130803Smarcel    data_prefix_tag         = 15, // tag for a prefix (carries data arguments)
273130803Smarcel    type_mask               = 15  // A mask which selects only the above values
274130803Smarcel  };
275130803Smarcel
276130803Smarcel protected:
277130803Smarcel  unsigned short _value;
278130803Smarcel
279130803Smarcel  enum RawBitsToken { RAW_BITS };
280130803Smarcel  relocInfo(relocType type, RawBitsToken ignore, int bits)
281130803Smarcel    : _value((type << nontype_width) + bits) { }
282130803Smarcel
283130803Smarcel  relocInfo(relocType type, RawBitsToken ignore, int off, int f)
284130803Smarcel    : _value((type << nontype_width) + (off / (unsigned)offset_unit) + (f << offset_width)) { }
285130803Smarcel
286130803Smarcel public:
287130803Smarcel  // constructor
288130803Smarcel  relocInfo(relocType type, int offset, int format = 0)
289130803Smarcel#ifndef ASSERT
290130803Smarcel  {
291130803Smarcel    (*this) = relocInfo(type, RAW_BITS, offset, format);
292130803Smarcel  }
293130803Smarcel#else
294130803Smarcel  // Put a bunch of assertions out-of-line.
295130803Smarcel  ;
296130803Smarcel#endif
297130803Smarcel
298130803Smarcel  #define APPLY_TO_RELOCATIONS(visitor) \
299130803Smarcel    visitor(oop) \
300130803Smarcel    visitor(virtual_call) \
301130803Smarcel    visitor(opt_virtual_call) \
302130803Smarcel    visitor(static_call) \
303130803Smarcel    visitor(static_stub) \
304130803Smarcel    visitor(runtime_call) \
305130803Smarcel    visitor(external_word) \
306130803Smarcel    visitor(internal_word) \
307130803Smarcel    visitor(poll) \
308130803Smarcel    visitor(poll_return) \
309130803Smarcel    visitor(breakpoint) \
310130803Smarcel    visitor(section_word) \
311130803Smarcel
312130803Smarcel
313130803Smarcel public:
314130803Smarcel  enum {
315130803Smarcel    value_width             = sizeof(unsigned short) * BitsPerByte,
316130803Smarcel    type_width              = 4,   // == log2(type_mask+1)
317130803Smarcel    nontype_width           = value_width - type_width,
318130803Smarcel    datalen_width           = nontype_width-1,
319130803Smarcel    datalen_tag             = 1 << datalen_width,  // or-ed into _value
320130803Smarcel    datalen_limit           = 1 << datalen_width,
321130803Smarcel    datalen_mask            = (1 << datalen_width)-1
322130803Smarcel  };
323130803Smarcel
324130803Smarcel  // accessors
325130803Smarcel public:
326130803Smarcel  relocType  type()       const { return (relocType)((unsigned)_value >> nontype_width); }
327130803Smarcel  int  format()           const { return format_mask==0? 0: format_mask &
328130803Smarcel                                         ((unsigned)_value >> offset_width); }
329130803Smarcel  int  addr_offset()      const { assert(!is_prefix(), "must have offset");
330130803Smarcel                                  return (_value & offset_mask)*offset_unit; }
331130803Smarcel
332130803Smarcel protected:
333130803Smarcel  const short* data()     const { assert(is_datalen(), "must have data");
334130803Smarcel                                  return (const short*)(this + 1); }
335130803Smarcel  int          datalen()  const { assert(is_datalen(), "must have data");
336130803Smarcel                                  return (_value & datalen_mask); }
337130803Smarcel  int         immediate() const { assert(is_immediate(), "must have immed");
338130803Smarcel                                  return (_value & datalen_mask); }
339130803Smarcel public:
340130803Smarcel  static int addr_unit()        { return offset_unit; }
341130803Smarcel  static int offset_limit()     { return (1 << offset_width) * offset_unit; }
342130803Smarcel
343130803Smarcel  void set_type(relocType type);
344130803Smarcel  void set_format(int format);
345130803Smarcel
346130803Smarcel  void remove() { set_type(none); }
347130803Smarcel
348130803Smarcel protected:
349130803Smarcel  bool is_none()                const { return type() == none; }
350130803Smarcel  bool is_prefix()              const { return type() == data_prefix_tag; }
351130803Smarcel  bool is_datalen()             const { assert(is_prefix(), "must be prefix");
352130803Smarcel                                        return (_value & datalen_tag) != 0; }
353130803Smarcel  bool is_immediate()           const { assert(is_prefix(), "must be prefix");
354130803Smarcel                                        return (_value & datalen_tag) == 0; }
355130803Smarcel
356130803Smarcel public:
357130803Smarcel  // Occasionally records of type relocInfo::none will appear in the stream.
358130803Smarcel  // We do not bother to filter these out, but clients should ignore them.
359130803Smarcel  // These records serve as "filler" in three ways:
360130803Smarcel  //  - to skip large spans of unrelocated code (this is rare)
361130803Smarcel  //  - to pad out the relocInfo array to the required oop alignment
362130803Smarcel  //  - to disable old relocation information which is no longer applicable
363130803Smarcel
364130803Smarcel  inline friend relocInfo filler_relocInfo();
365130803Smarcel
366130803Smarcel  // Every non-prefix relocation may be preceded by at most one prefix,
367130803Smarcel  // which supplies 1 or more halfwords of associated data.  Conventionally,
368130803Smarcel  // an int is represented by 0, 1, or 2 halfwords, depending on how
369130803Smarcel  // many bits are required to represent the value.  (In addition,
370130803Smarcel  // if the sole halfword is a 10-bit unsigned number, it is made
371130803Smarcel  // "immediate" in the prefix header word itself.  This optimization
372130803Smarcel  // is invisible outside this module.)
373130803Smarcel
374130803Smarcel  inline friend relocInfo prefix_relocInfo(int datalen = 0);
375130803Smarcel
376130803Smarcel protected:
377130803Smarcel  // an immediate relocInfo optimizes a prefix with one 10-bit unsigned value
378130803Smarcel  static relocInfo immediate_relocInfo(int data0) {
379130803Smarcel    assert(fits_into_immediate(data0), "data0 in limits");
380130803Smarcel    return relocInfo(relocInfo::data_prefix_tag, RAW_BITS, data0);
381130803Smarcel  }
382130803Smarcel  static bool fits_into_immediate(int data0) {
383130803Smarcel    return (data0 >= 0 && data0 < datalen_limit);
384130803Smarcel  }
385130803Smarcel
386130803Smarcel public:
387130803Smarcel  // Support routines for compilers.
388130803Smarcel
389130803Smarcel  // This routine takes an infant relocInfo (unprefixed) and
390130803Smarcel  // edits in its prefix, if any.  It also updates dest.locs_end.
391130803Smarcel  void initialize(CodeSection* dest, Relocation* reloc);
392130803Smarcel
393130803Smarcel  // This routine updates a prefix and returns the limit pointer.
394130803Smarcel  // It tries to compress the prefix from 32 to 16 bits, and if
395130803Smarcel  // successful returns a reduced "prefix_limit" pointer.
396130803Smarcel  relocInfo* finish_prefix(short* prefix_limit);
397130803Smarcel
398130803Smarcel  // bit-packers for the data array:
399130803Smarcel
400130803Smarcel  // As it happens, the bytes within the shorts are ordered natively,
401130803Smarcel  // but the shorts within the word are ordered big-endian.
402130803Smarcel  // This is an arbitrary choice, made this way mainly to ease debugging.
403130803Smarcel  static int data0_from_int(jint x)         { return x >> value_width; }
404130803Smarcel  static int data1_from_int(jint x)         { return (short)x; }
405130803Smarcel  static jint jint_from_data(short* data) {
406130803Smarcel    return (data[0] << value_width) + (unsigned short)data[1];
407130803Smarcel  }
408130803Smarcel
409130803Smarcel  static jint short_data_at(int n, short* data, int datalen) {
410130803Smarcel    return datalen > n ? data[n] : 0;
411130803Smarcel  }
412130803Smarcel
413130803Smarcel  static jint jint_data_at(int n, short* data, int datalen) {
414130803Smarcel    return datalen > n+1 ? jint_from_data(&data[n]) : short_data_at(n, data, datalen);
415130803Smarcel  }
416130803Smarcel
417130803Smarcel  // Update methods for relocation information
418130803Smarcel  // (since code is dynamically patched, we also need to dynamically update the relocation info)
419130803Smarcel  // Both methods takes old_type, so it is able to performe sanity checks on the information removed.
420130803Smarcel  static void change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type);
421130803Smarcel  static void remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type);
422130803Smarcel
423130803Smarcel  // Machine dependent stuff
424130803Smarcel#ifdef TARGET_ARCH_x86
425130803Smarcel# include "relocInfo_x86.hpp"
426130803Smarcel#endif
427130803Smarcel#ifdef TARGET_ARCH_sparc
428130803Smarcel# include "relocInfo_sparc.hpp"
429130803Smarcel#endif
430130803Smarcel#ifdef TARGET_ARCH_zero
431130803Smarcel# include "relocInfo_zero.hpp"
432130803Smarcel#endif
433130803Smarcel#ifdef TARGET_ARCH_arm
434130803Smarcel# include "relocInfo_arm.hpp"
435130803Smarcel#endif
436130803Smarcel#ifdef TARGET_ARCH_ppc
437130803Smarcel# include "relocInfo_ppc.hpp"
438130803Smarcel#endif
439130803Smarcel
440130803Smarcel
441130803Smarcel protected:
442130803Smarcel  // Derived constant, based on format_width which is PD:
443130803Smarcel  enum {
444130803Smarcel    offset_width       = nontype_width - format_width,
445130803Smarcel    offset_mask        = (1<<offset_width) - 1,
446130803Smarcel    format_mask        = (1<<format_width) - 1
447130803Smarcel  };
448130803Smarcel public:
449130803Smarcel  enum {
450130803Smarcel    // Conservatively large estimate of maximum length (in shorts)
451130803Smarcel    // of any relocation record (probably breakpoints are largest).
452130803Smarcel    // Extended format is length prefix, data words, and tag/offset suffix.
453130803Smarcel    length_limit       = 1 + 1 + (3*BytesPerWord/BytesPerShort) + 1,
454130803Smarcel    have_format        = format_width > 0
455130803Smarcel  };
456130803Smarcel};
457130803Smarcel
458130803Smarcel#define FORWARD_DECLARE_EACH_CLASS(name)              \
459130803Smarcelclass name##_Relocation;
460130803SmarcelAPPLY_TO_RELOCATIONS(FORWARD_DECLARE_EACH_CLASS)
461130803Smarcel#undef FORWARD_DECLARE_EACH_CLASS
462130803Smarcel
463130803Smarcel
464130803Smarcel
465130803Smarcelinline relocInfo filler_relocInfo() {
466130803Smarcel  return relocInfo(relocInfo::none, relocInfo::offset_limit() - relocInfo::offset_unit);
467130803Smarcel}
468130803Smarcel
469130803Smarcelinline relocInfo prefix_relocInfo(int datalen) {
470130803Smarcel  assert(relocInfo::fits_into_immediate(datalen), "datalen in limits");
471130803Smarcel  return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);
472130803Smarcel}
473130803Smarcel
474130803Smarcel
475130803Smarcel// Holder for flyweight relocation objects.
476130803Smarcel// Although the flyweight subclasses are of varying sizes,
477130803Smarcel// the holder is "one size fits all".
478130803Smarcelclass RelocationHolder VALUE_OBJ_CLASS_SPEC {
479130803Smarcel  friend class Relocation;
480130803Smarcel  friend class CodeSection;
481130803Smarcel
482130803Smarcel private:
483130803Smarcel  // this preallocated memory must accommodate all subclasses of Relocation
484130803Smarcel  // (this number is assertion-checked in Relocation::operator new)
485130803Smarcel  enum { _relocbuf_size = 5 };
486130803Smarcel  void* _relocbuf[ _relocbuf_size ];
487130803Smarcel
488130803Smarcel public:
489130803Smarcel  Relocation* reloc() const { return (Relocation*) &_relocbuf[0]; }
490130803Smarcel  inline relocInfo::relocType type() const;
491130803Smarcel
492130803Smarcel  // Add a constant offset to a relocation.  Helper for class Address.
493130803Smarcel  RelocationHolder plus(int offset) const;
494130803Smarcel
495130803Smarcel  inline RelocationHolder();                // initializes type to none
496130803Smarcel
497130803Smarcel  inline RelocationHolder(Relocation* r);   // make a copy
498130803Smarcel
499130803Smarcel  static const RelocationHolder none;
500130803Smarcel};
501130803Smarcel
502130803Smarcel// A RelocIterator iterates through the relocation information of a CodeBlob.
503130803Smarcel// It is a variable BoundRelocation which is able to take on successive
504130803Smarcel// values as it is advanced through a code stream.
505130803Smarcel// Usage:
506130803Smarcel//   RelocIterator iter(nm);
507130803Smarcel//   while (iter.next()) {
508130803Smarcel//     iter.reloc()->some_operation();
509130803Smarcel//   }
510130803Smarcel// or:
511130803Smarcel//   RelocIterator iter(nm);
512130803Smarcel//   while (iter.next()) {
513130803Smarcel//     switch (iter.type()) {
514130803Smarcel//      case relocInfo::oop_type          :
515130803Smarcel//      case relocInfo::ic_type           :
516130803Smarcel//      case relocInfo::prim_type         :
517130803Smarcel//      case relocInfo::uncommon_type     :
518130803Smarcel//      case relocInfo::runtime_call_type :
519130803Smarcel//      case relocInfo::internal_word_type:
520130803Smarcel//      case relocInfo::external_word_type:
521130803Smarcel//      ...
522130803Smarcel//     }
523130803Smarcel//   }
524130803Smarcel
525130803Smarcelclass RelocIterator : public StackObj {
526130803Smarcel  enum { SECT_LIMIT = 3 };  // must be equal to CodeBuffer::SECT_LIMIT, checked in ctor
527130803Smarcel  friend class Relocation;
528130803Smarcel  friend class relocInfo;       // for change_reloc_info_for_address only
529130803Smarcel  typedef relocInfo::relocType relocType;
530130803Smarcel
531130803Smarcel private:
532130803Smarcel  address    _limit;   // stop producing relocations after this _addr
533130803Smarcel  relocInfo* _current; // the current relocation information
534130803Smarcel  relocInfo* _end;     // end marker; we're done iterating when _current == _end
535130803Smarcel  nmethod*   _code;    // compiled method containing _addr
536130803Smarcel  address    _addr;    // instruction to which the relocation applies
537130803Smarcel  short      _databuf; // spare buffer for compressed data
538130803Smarcel  short*     _data;    // pointer to the relocation's data
539130803Smarcel  short      _datalen; // number of halfwords in _data
540130803Smarcel  char       _format;  // position within the instruction
541130803Smarcel
542130803Smarcel  // Base addresses needed to compute targets of section_word_type relocs.
543130803Smarcel  address    _section_start[SECT_LIMIT];
544130803Smarcel  address    _section_end  [SECT_LIMIT];
545130803Smarcel
546130803Smarcel  void set_has_current(bool b) {
547130803Smarcel    _datalen = !b ? -1 : 0;
548130803Smarcel    debug_only(_data = NULL);
549130803Smarcel  }
550130803Smarcel  void set_current(relocInfo& ri) {
551130803Smarcel    _current = &ri;
552130803Smarcel    set_has_current(true);
553130803Smarcel  }
554130803Smarcel
555130803Smarcel  RelocationHolder _rh; // where the current relocation is allocated
556130803Smarcel
557130803Smarcel  relocInfo* current() const { assert(has_current(), "must have current");
558130803Smarcel                               return _current; }
559130803Smarcel
560130803Smarcel  void set_limits(address begin, address limit);
561130803Smarcel
562130803Smarcel  void advance_over_prefix();    // helper method
563130803Smarcel
564130803Smarcel  void initialize_misc();
565130803Smarcel
566130803Smarcel  void initialize(nmethod* nm, address begin, address limit);
567130803Smarcel
568130803Smarcel  friend class PatchingRelocIterator;
569130803Smarcel  // make an uninitialized one, for PatchingRelocIterator:
570130803Smarcel  RelocIterator() { initialize_misc(); }
571130803Smarcel
572130803Smarcel public:
573130803Smarcel  // constructor
574130803Smarcel  RelocIterator(nmethod* nm,     address begin = NULL, address limit = NULL);
575130803Smarcel  RelocIterator(CodeSection* cb, address begin = NULL, address limit = NULL);
576130803Smarcel
577130803Smarcel  // get next reloc info, return !eos
578130803Smarcel  bool next() {
579130803Smarcel    _current++;
580130803Smarcel    assert(_current <= _end, "must not overrun relocInfo");
581130803Smarcel    if (_current == _end) {
582130803Smarcel      set_has_current(false);
583130803Smarcel      return false;
584130803Smarcel    }
585130803Smarcel    set_has_current(true);
586130803Smarcel
587130803Smarcel    if (_current->is_prefix()) {
588130803Smarcel      advance_over_prefix();
589130803Smarcel      assert(!current()->is_prefix(), "only one prefix at a time");
590130803Smarcel    }
591130803Smarcel
592130803Smarcel    _addr += _current->addr_offset();
593130803Smarcel
594130803Smarcel    if (_limit != NULL && _addr >= _limit) {
595130803Smarcel      set_has_current(false);
596130803Smarcel      return false;
597130803Smarcel    }
598130803Smarcel
599130803Smarcel    if (relocInfo::have_format)  _format = current()->format();
600130803Smarcel    return true;
601130803Smarcel  }
602130803Smarcel
603130803Smarcel  // accessors
604130803Smarcel  address      limit()        const { return _limit; }
605130803Smarcel  void     set_limit(address x);
606130803Smarcel  relocType    type()         const { return current()->type(); }
607130803Smarcel  int          format()       const { return (relocInfo::have_format) ? current()->format() : 0; }
608130803Smarcel  address      addr()         const { return _addr; }
609130803Smarcel  nmethod*     code()         const { return _code; }
610130803Smarcel  short*       data()         const { return _data; }
611130803Smarcel  int          datalen()      const { return _datalen; }
612130803Smarcel  bool     has_current()      const { return _datalen >= 0; }
613130803Smarcel
614130803Smarcel  void       set_addr(address addr) { _addr = addr; }
615130803Smarcel  bool   addr_in_const()      const;
616130803Smarcel
617130803Smarcel  address section_start(int n) const {
618130803Smarcel    assert(_section_start[n], "must be initialized");
619130803Smarcel    return _section_start[n];
620130803Smarcel  }
621130803Smarcel  address section_end(int n) const {
622130803Smarcel    assert(_section_end[n], "must be initialized");
623130803Smarcel    return _section_end[n];
624130803Smarcel  }
625130803Smarcel
626130803Smarcel  // The address points to the affected displacement part of the instruction.
627130803Smarcel  // For RISC, this is just the whole instruction.
628130803Smarcel  // For Intel, this is an unaligned 32-bit word.
629130803Smarcel
630130803Smarcel  // type-specific relocation accessors:  oop_Relocation* oop_reloc(), etc.
631130803Smarcel  #define EACH_TYPE(name)                               \
632130803Smarcel  inline name##_Relocation* name##_reloc();
633130803Smarcel  APPLY_TO_RELOCATIONS(EACH_TYPE)
634130803Smarcel  #undef EACH_TYPE
635130803Smarcel  // generic relocation accessor; switches on type to call the above
636130803Smarcel  Relocation* reloc();
637130803Smarcel
638130803Smarcel  // CodeBlob's have relocation indexes for faster random access:
639130803Smarcel  static int locs_and_index_size(int code_size, int locs_size);
640130803Smarcel  // Store an index into [dest_start+dest_count..dest_end).
641130803Smarcel  // At dest_start[0..dest_count] is the actual relocation information.
642130803Smarcel  // Everything else up to dest_end is free space for the index.
643130803Smarcel  static void create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end);
644130803Smarcel
645130803Smarcel#ifndef PRODUCT
646130803Smarcel public:
647130803Smarcel  void print();
648130803Smarcel  void print_current();
649130803Smarcel#endif
650130803Smarcel};
651130803Smarcel
652130803Smarcel
653130803Smarcel// A Relocation is a flyweight object allocated within a RelocationHolder.
654130803Smarcel// It represents the relocation data of relocation record.
655130803Smarcel// So, the RelocIterator unpacks relocInfos into Relocations.
656130803Smarcel
657130803Smarcelclass Relocation VALUE_OBJ_CLASS_SPEC {
658130803Smarcel  friend class RelocationHolder;
659130803Smarcel  friend class RelocIterator;
660130803Smarcel
661130803Smarcel private:
662130803Smarcel  static void guarantee_size();
663130803Smarcel
664130803Smarcel  // When a relocation has been created by a RelocIterator,
665130803Smarcel  // this field is non-null.  It allows the relocation to know
666130803Smarcel  // its context, such as the address to which it applies.
667130803Smarcel  RelocIterator* _binding;
668130803Smarcel
669130803Smarcel protected:
670130803Smarcel  RelocIterator* binding() const {
671130803Smarcel    assert(_binding != NULL, "must be bound");
672130803Smarcel    return _binding;
673130803Smarcel  }
674130803Smarcel  void set_binding(RelocIterator* b) {
675130803Smarcel    assert(_binding == NULL, "must be unbound");
676130803Smarcel    _binding = b;
677130803Smarcel    assert(_binding != NULL, "must now be bound");
678130803Smarcel  }
679130803Smarcel
680130803Smarcel  Relocation() {
681130803Smarcel    _binding = NULL;
682130803Smarcel  }
683130803Smarcel
684130803Smarcel  static RelocationHolder newHolder() {
685130803Smarcel    return RelocationHolder();
686130803Smarcel  }
687130803Smarcel
688130803Smarcel public:
689130803Smarcel  void* operator new(size_t size, const RelocationHolder& holder) {
690130803Smarcel    if (size > sizeof(holder._relocbuf)) guarantee_size();
691130803Smarcel    assert((void* const *)holder.reloc() == &holder._relocbuf[0], "ptrs must agree");
692130803Smarcel    return holder.reloc();
693130803Smarcel  }
694130803Smarcel
695130803Smarcel  // make a generic relocation for a given type (if possible)
696130803Smarcel  static RelocationHolder spec_simple(relocInfo::relocType rtype);
697130803Smarcel
698130803Smarcel  // here is the type-specific hook which writes relocation data:
699130803Smarcel  virtual void pack_data_to(CodeSection* dest) { }
700130803Smarcel
701130803Smarcel  // here is the type-specific hook which reads (unpacks) relocation data:
702130803Smarcel  virtual void unpack_data() {
703130803Smarcel    assert(datalen()==0 || type()==relocInfo::none, "no data here");
704130803Smarcel  }
705130803Smarcel
706130803Smarcel protected:
707130803Smarcel  // Helper functions for pack_data_to() and unpack_data().
708130803Smarcel
709130803Smarcel  // Most of the compression logic is confined here.
710130803Smarcel  // (The "immediate data" mechanism of relocInfo works independently
711130803Smarcel  // of this stuff, and acts to further compress most 1-word data prefixes.)
712130803Smarcel
713130803Smarcel  // A variable-width int is encoded as a short if it will fit in 16 bits.
714130803Smarcel  // The decoder looks at datalen to decide whether to unpack short or jint.
715130803Smarcel  // Most relocation records are quite simple, containing at most two ints.
716130803Smarcel
717130803Smarcel  static bool is_short(jint x) { return x == (short)x; }
718130803Smarcel  static short* add_short(short* p, int x)  { *p++ = x; return p; }
719130803Smarcel  static short* add_jint (short* p, jint x) {
720130803Smarcel    *p++ = relocInfo::data0_from_int(x); *p++ = relocInfo::data1_from_int(x);
721130803Smarcel    return p;
722130803Smarcel  }
723130803Smarcel  static short* add_var_int(short* p, jint x) {   // add a variable-width int
724130803Smarcel    if (is_short(x))  p = add_short(p, x);
725130803Smarcel    else              p = add_jint (p, x);
726130803Smarcel    return p;
727130803Smarcel  }
728130803Smarcel
729130803Smarcel  static short* pack_1_int_to(short* p, jint x0) {
730130803Smarcel    // Format is one of:  [] [x] [Xx]
731130803Smarcel    if (x0 != 0)  p = add_var_int(p, x0);
732130803Smarcel    return p;
733130803Smarcel  }
734130803Smarcel  int unpack_1_int() {
735130803Smarcel    assert(datalen() <= 2, "too much data");
736130803Smarcel    return relocInfo::jint_data_at(0, data(), datalen());
737130803Smarcel  }
738130803Smarcel
739130803Smarcel  // With two ints, the short form is used only if both ints are short.
740130803Smarcel  short* pack_2_ints_to(short* p, jint x0, jint x1) {
741130803Smarcel    // Format is one of:  [] [x y?] [Xx Y?y]
742130803Smarcel    if (x0 == 0 && x1 == 0) {
743130803Smarcel      // no halfwords needed to store zeroes
744130803Smarcel    } else if (is_short(x0) && is_short(x1)) {
745130803Smarcel      // 1-2 halfwords needed to store shorts
746130803Smarcel      p = add_short(p, x0); if (x1!=0) p = add_short(p, x1);
747130803Smarcel    } else {
748130803Smarcel      // 3-4 halfwords needed to store jints
749130803Smarcel      p = add_jint(p, x0);             p = add_var_int(p, x1);
750130803Smarcel    }
751130803Smarcel    return p;
752130803Smarcel  }
753130803Smarcel  void unpack_2_ints(jint& x0, jint& x1) {
754130803Smarcel    int    dlen = datalen();
755130803Smarcel    short* dp  = data();
756130803Smarcel    if (dlen <= 2) {
757130803Smarcel      x0 = relocInfo::short_data_at(0, dp, dlen);
758130803Smarcel      x1 = relocInfo::short_data_at(1, dp, dlen);
759130803Smarcel    } else {
760130803Smarcel      assert(dlen <= 4, "too much data");
761130803Smarcel      x0 = relocInfo::jint_data_at(0, dp, dlen);
762130803Smarcel      x1 = relocInfo::jint_data_at(2, dp, dlen);
763130803Smarcel    }
764130803Smarcel  }
765130803Smarcel
766130803Smarcel protected:
767130803Smarcel  // platform-dependent utilities for decoding and patching instructions
768130803Smarcel  void       pd_set_data_value       (address x, intptr_t off, bool verify_only = false); // a set or mem-ref
769130803Smarcel  void       pd_verify_data_value    (address x, intptr_t off) { pd_set_data_value(x, off, true); }
770130803Smarcel  address    pd_call_destination     (address orig_addr = NULL);
771130803Smarcel  void       pd_set_call_destination (address x);
772130803Smarcel  void       pd_swap_in_breakpoint   (address x, short* instrs, int instrlen);
773130803Smarcel  void       pd_swap_out_breakpoint  (address x, short* instrs, int instrlen);
774130803Smarcel  static int pd_breakpoint_size      ();
775130803Smarcel
776130803Smarcel  // this extracts the address of an address in the code stream instead of the reloc data
777130803Smarcel  address* pd_address_in_code       ();
778130803Smarcel
779130803Smarcel  // this extracts an address from the code stream instead of the reloc data
780130803Smarcel  address  pd_get_address_from_code ();
781130803Smarcel
782130803Smarcel  // these convert from byte offsets, to scaled offsets, to addresses
783130803Smarcel  static jint scaled_offset(address x, address base) {
784130803Smarcel    int byte_offset = x - base;
785130803Smarcel    int offset = -byte_offset / relocInfo::addr_unit();
786130803Smarcel    assert(address_from_scaled_offset(offset, base) == x, "just checkin'");
787130803Smarcel    return offset;
788130803Smarcel  }
789130803Smarcel  static jint scaled_offset_null_special(address x, address base) {
790130803Smarcel    // Some relocations treat offset=0 as meaning NULL.
791130803Smarcel    // Handle this extra convention carefully.
792130803Smarcel    if (x == NULL)  return 0;
793130803Smarcel    assert(x != base, "offset must not be zero");
794130803Smarcel    return scaled_offset(x, base);
795130803Smarcel  }
796130803Smarcel  static address address_from_scaled_offset(jint offset, address base) {
797130803Smarcel    int byte_offset = -( offset * relocInfo::addr_unit() );
798130803Smarcel    return base + byte_offset;
799130803Smarcel  }
800130803Smarcel
801130803Smarcel  // these convert between indexes and addresses in the runtime system
802130803Smarcel  static int32_t runtime_address_to_index(address runtime_address);
803130803Smarcel  static address index_to_runtime_address(int32_t index);
804130803Smarcel
805130803Smarcel  // helpers for mapping between old and new addresses after a move or resize
806130803Smarcel  address old_addr_for(address newa, const CodeBuffer* src, CodeBuffer* dest);
807130803Smarcel  address new_addr_for(address olda, const CodeBuffer* src, CodeBuffer* dest);
808130803Smarcel  void normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections = false);
809130803Smarcel
810130803Smarcel public:
811130803Smarcel  // accessors which only make sense for a bound Relocation
812130803Smarcel  address  addr()         const { return binding()->addr(); }
813130803Smarcel  nmethod* code()         const { return binding()->code(); }
814130803Smarcel  bool     addr_in_const() const { return binding()->addr_in_const(); }
815130803Smarcel protected:
816130803Smarcel  short*   data()         const { return binding()->data(); }
817130803Smarcel  int      datalen()      const { return binding()->datalen(); }
818130803Smarcel  int      format()       const { return binding()->format(); }
819130803Smarcel
820130803Smarcel public:
821130803Smarcel  virtual relocInfo::relocType type()            { return relocInfo::none; }
822130803Smarcel
823130803Smarcel  // is it a call instruction?
824130803Smarcel  virtual bool is_call()                         { return false; }
825130803Smarcel
826130803Smarcel  // is it a data movement instruction?
827130803Smarcel  virtual bool is_data()                         { return false; }
828130803Smarcel
829130803Smarcel  // some relocations can compute their own values
830130803Smarcel  virtual address  value();
831130803Smarcel
832130803Smarcel  // all relocations are able to reassert their values
833130803Smarcel  virtual void set_value(address x);
834130803Smarcel
835130803Smarcel  virtual void clear_inline_cache()              { }
836130803Smarcel
837130803Smarcel  // This method assumes that all virtual/static (inline) caches are cleared (since for static_call_type and
838130803Smarcel  // ic_call_type is not always posisition dependent (depending on the state of the cache)). However, this is
839130803Smarcel  // probably a reasonable assumption, since empty caches simplifies code reloacation.
840130803Smarcel  virtual void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { }
841130803Smarcel
842130803Smarcel  void print();
843130803Smarcel};
844130803Smarcel
845130803Smarcel
846130803Smarcel// certain inlines must be deferred until class Relocation is defined:
847130803Smarcel
848130803Smarcelinline RelocationHolder::RelocationHolder() {
849130803Smarcel  // initialize the vtbl, just to keep things type-safe
850130803Smarcel  new(*this) Relocation();
851130803Smarcel}
852130803Smarcel
853130803Smarcel
854130803Smarcelinline RelocationHolder::RelocationHolder(Relocation* r) {
855130803Smarcel  // wordwise copy from r (ok if it copies garbage after r)
856130803Smarcel  for (int i = 0; i < _relocbuf_size; i++) {
857130803Smarcel    _relocbuf[i] = ((void**)r)[i];
858130803Smarcel  }
859130803Smarcel}
860130803Smarcel
861130803Smarcel
862130803SmarcelrelocInfo::relocType RelocationHolder::type() const {
863130803Smarcel  return reloc()->type();
864130803Smarcel}
865130803Smarcel
866130803Smarcel// A DataRelocation always points at a memory or load-constant instruction..
867130803Smarcel// It is absolute on most machines, and the constant is split on RISCs.
868130803Smarcel// The specific subtypes are oop, external_word, and internal_word.
869130803Smarcel// By convention, the "value" does not include a separately reckoned "offset".
870130803Smarcelclass DataRelocation : public Relocation {
871130803Smarcel public:
872130803Smarcel  bool          is_data()                      { return true; }
873130803Smarcel
874130803Smarcel  // both target and offset must be computed somehow from relocation data
875130803Smarcel  virtual int    offset()                      { return 0; }
876130803Smarcel  address         value()                      = 0;
877130803Smarcel  void        set_value(address x)             { set_value(x, offset()); }
878130803Smarcel  void        set_value(address x, intptr_t o) {
879130803Smarcel    if (addr_in_const())
880130803Smarcel      *(address*)addr() = x;
881130803Smarcel    else
882130803Smarcel      pd_set_data_value(x, o);
883130803Smarcel  }
884130803Smarcel  void        verify_value(address x) {
885130803Smarcel    if (addr_in_const())
886130803Smarcel      assert(*(address*)addr() == x, "must agree");
887130803Smarcel    else
888130803Smarcel      pd_verify_data_value(x, offset());
889130803Smarcel  }
890130803Smarcel
891130803Smarcel  // The "o" (displacement) argument is relevant only to split relocations
892130803Smarcel  // on RISC machines.  In some CPUs (SPARC), the set-hi and set-lo ins'ns
893130803Smarcel  // can encode more than 32 bits between them.  This allows compilers to
894130803Smarcel  // share set-hi instructions between addresses that differ by a small
895130803Smarcel  // offset (e.g., different static variables in the same class).
896130803Smarcel  // On such machines, the "x" argument to set_value on all set-lo
897130803Smarcel  // instructions must be the same as the "x" argument for the
898130803Smarcel  // corresponding set-hi instructions.  The "o" arguments for the
899130803Smarcel  // set-hi instructions are ignored, and must not affect the high-half
900130803Smarcel  // immediate constant.  The "o" arguments for the set-lo instructions are
901130803Smarcel  // added into the low-half immediate constant, and must not overflow it.
902130803Smarcel};
903130803Smarcel
904130803Smarcel// A CallRelocation always points at a call instruction.
905130803Smarcel// It is PC-relative on most machines.
906130803Smarcelclass CallRelocation : public Relocation {
907130803Smarcel public:
908130803Smarcel  bool is_call() { return true; }
909130803Smarcel
910130803Smarcel  address  destination()                    { return pd_call_destination(); }
911130803Smarcel  void     set_destination(address x); // pd_set_call_destination
912130803Smarcel
913130803Smarcel  void     fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
914130803Smarcel  address  value()                          { return destination();  }
915130803Smarcel  void     set_value(address x)             { set_destination(x); }
916130803Smarcel};
917130803Smarcel
918130803Smarcelclass oop_Relocation : public DataRelocation {
919130803Smarcel  relocInfo::relocType type() { return relocInfo::oop_type; }
920130803Smarcel
921130803Smarcel public:
922130803Smarcel  // encode in one of these formats:  [] [n] [n l] [Nn l] [Nn Ll]
923130803Smarcel  // an oop in the CodeBlob's oop pool
924130803Smarcel  static RelocationHolder spec(int oop_index, int offset = 0) {
925130803Smarcel    assert(oop_index > 0, "must be a pool-resident oop");
926130803Smarcel    RelocationHolder rh = newHolder();
927130803Smarcel    new(rh) oop_Relocation(oop_index, offset);
928130803Smarcel    return rh;
929130803Smarcel  }
930130803Smarcel  // an oop in the instruction stream
931130803Smarcel  static RelocationHolder spec_for_immediate() {
932130803Smarcel    const int oop_index = 0;
933130803Smarcel    const int offset    = 0;    // if you want an offset, use the oop pool
934130803Smarcel    RelocationHolder rh = newHolder();
935130803Smarcel    new(rh) oop_Relocation(oop_index, offset);
936130803Smarcel    return rh;
937130803Smarcel  }
938130803Smarcel
939130803Smarcel private:
940130803Smarcel  jint _oop_index;                  // if > 0, index into CodeBlob::oop_at
941130803Smarcel  jint _offset;                     // byte offset to apply to the oop itself
942130803Smarcel
943130803Smarcel  oop_Relocation(int oop_index, int offset) {
944130803Smarcel    _oop_index = oop_index; _offset = offset;
945130803Smarcel  }
946130803Smarcel
947130803Smarcel  friend class RelocIterator;
948130803Smarcel  oop_Relocation() { }
949130803Smarcel
950130803Smarcel public:
951130803Smarcel  int oop_index() { return _oop_index; }
952130803Smarcel  int offset()    { return _offset; }
953130803Smarcel
954130803Smarcel  // data is packed in "2_ints" format:  [i o] or [Ii Oo]
955130803Smarcel  void pack_data_to(CodeSection* dest);
956130803Smarcel  void unpack_data();
957130803Smarcel
958130803Smarcel  void fix_oop_relocation();        // reasserts oop value
959130803Smarcel
960130803Smarcel  void verify_oop_relocation();
961130803Smarcel
962130803Smarcel  address value()  { return (address) *oop_addr(); }
963130803Smarcel
964130803Smarcel  bool oop_is_immediate()  { return oop_index() == 0; }
965130803Smarcel
966130803Smarcel  oop* oop_addr();                  // addr or &pool[jint_data]
967130803Smarcel  oop  oop_value();                 // *oop_addr
968130803Smarcel  // Note:  oop_value transparently converts Universe::non_oop_word to NULL.
969130803Smarcel};
970130803Smarcel
971130803Smarcelclass virtual_call_Relocation : public CallRelocation {
972130803Smarcel  relocInfo::relocType type() { return relocInfo::virtual_call_type; }
973130803Smarcel
974130803Smarcel public:
975130803Smarcel  // "first_oop" points to the first associated set-oop.
976130803Smarcel  // The oop_limit helps find the last associated set-oop.
977130803Smarcel  // (See comments at the top of this file.)
978130803Smarcel  static RelocationHolder spec(address first_oop, address oop_limit = NULL) {
979130803Smarcel    RelocationHolder rh = newHolder();
980130803Smarcel    new(rh) virtual_call_Relocation(first_oop, oop_limit);
981130803Smarcel    return rh;
982130803Smarcel  }
983130803Smarcel
984130803Smarcel  virtual_call_Relocation(address first_oop, address oop_limit) {
985130803Smarcel    _first_oop = first_oop; _oop_limit = oop_limit;
986130803Smarcel    assert(first_oop != NULL, "first oop address must be specified");
987130803Smarcel  }
988130803Smarcel
989130803Smarcel private:
990130803Smarcel  address _first_oop;               // location of first set-oop instruction
991130803Smarcel  address _oop_limit;               // search limit for set-oop instructions
992130803Smarcel
993130803Smarcel  friend class RelocIterator;
994130803Smarcel  virtual_call_Relocation() { }
995130803Smarcel
996130803Smarcel
997130803Smarcel public:
998130803Smarcel  address first_oop();
999130803Smarcel  address oop_limit();
1000130803Smarcel
1001130803Smarcel  // data is packed as scaled offsets in "2_ints" format:  [f l] or [Ff Ll]
1002130803Smarcel  // oop_limit is set to 0 if the limit falls somewhere within the call.
1003130803Smarcel  // When unpacking, a zero oop_limit is taken to refer to the end of the call.
1004130803Smarcel  // (This has the effect of bringing in the call's delay slot on SPARC.)
1005130803Smarcel  void pack_data_to(CodeSection* dest);
1006130803Smarcel  void unpack_data();
1007130803Smarcel
1008130803Smarcel  void clear_inline_cache();
1009130803Smarcel
1010130803Smarcel  // Figure out where an ic_call is hiding, given a set-oop or call.
1011130803Smarcel  // Either ic_call or first_oop must be non-null; the other is deduced.
1012130803Smarcel  // Code if non-NULL must be the nmethod, else it is deduced.
1013130803Smarcel  // The address of the patchable oop is also deduced.
1014130803Smarcel  // The returned iterator will enumerate over the oops and the ic_call,
1015130803Smarcel  // as well as any other relocations that happen to be in that span of code.
1016130803Smarcel  // Recognize relevant set_oops with:  oop_reloc()->oop_addr() == oop_addr.
1017130803Smarcel  static RelocIterator parse_ic(nmethod* &nm, address &ic_call, address &first_oop, oop* &oop_addr, bool *is_optimized);
1018130803Smarcel};
1019130803Smarcel
1020130803Smarcel
1021130803Smarcelclass opt_virtual_call_Relocation : public CallRelocation {
1022130803Smarcel  relocInfo::relocType type() { return relocInfo::opt_virtual_call_type; }
1023130803Smarcel
1024130803Smarcel public:
1025130803Smarcel  static RelocationHolder spec() {
1026130803Smarcel    RelocationHolder rh = newHolder();
1027130803Smarcel    new(rh) opt_virtual_call_Relocation();
1028130803Smarcel    return rh;
1029130803Smarcel  }
1030130803Smarcel
1031130803Smarcel private:
1032130803Smarcel  friend class RelocIterator;
1033130803Smarcel  opt_virtual_call_Relocation() { }
1034130803Smarcel
1035130803Smarcel public:
1036130803Smarcel  void clear_inline_cache();
1037130803Smarcel
1038130803Smarcel  // find the matching static_stub
1039130803Smarcel  address static_stub();
1040130803Smarcel};
1041130803Smarcel
1042130803Smarcel
1043130803Smarcelclass static_call_Relocation : public CallRelocation {
1044130803Smarcel  relocInfo::relocType type() { return relocInfo::static_call_type; }
1045130803Smarcel
1046130803Smarcel public:
1047130803Smarcel  static RelocationHolder spec() {
1048130803Smarcel    RelocationHolder rh = newHolder();
1049130803Smarcel    new(rh) static_call_Relocation();
1050130803Smarcel    return rh;
1051130803Smarcel  }
1052130803Smarcel
1053130803Smarcel private:
1054130803Smarcel  friend class RelocIterator;
1055130803Smarcel  static_call_Relocation() { }
1056130803Smarcel
1057130803Smarcel public:
1058130803Smarcel  void clear_inline_cache();
1059130803Smarcel
1060130803Smarcel  // find the matching static_stub
1061130803Smarcel  address static_stub();
1062130803Smarcel};
1063130803Smarcel
1064130803Smarcelclass static_stub_Relocation : public Relocation {
1065130803Smarcel  relocInfo::relocType type() { return relocInfo::static_stub_type; }
1066130803Smarcel
1067130803Smarcel public:
1068130803Smarcel  static RelocationHolder spec(address static_call) {
1069130803Smarcel    RelocationHolder rh = newHolder();
1070130803Smarcel    new(rh) static_stub_Relocation(static_call);
1071130803Smarcel    return rh;
1072130803Smarcel  }
1073
1074 private:
1075  address _static_call;             // location of corresponding static_call
1076
1077  static_stub_Relocation(address static_call) {
1078    _static_call = static_call;
1079  }
1080
1081  friend class RelocIterator;
1082  static_stub_Relocation() { }
1083
1084 public:
1085  void clear_inline_cache();
1086
1087  address static_call() { return _static_call; }
1088
1089  // data is packed as a scaled offset in "1_int" format:  [c] or [Cc]
1090  void pack_data_to(CodeSection* dest);
1091  void unpack_data();
1092};
1093
1094class runtime_call_Relocation : public CallRelocation {
1095  relocInfo::relocType type() { return relocInfo::runtime_call_type; }
1096
1097 public:
1098  static RelocationHolder spec() {
1099    RelocationHolder rh = newHolder();
1100    new(rh) runtime_call_Relocation();
1101    return rh;
1102  }
1103
1104 private:
1105  friend class RelocIterator;
1106  runtime_call_Relocation() { }
1107
1108 public:
1109};
1110
1111class external_word_Relocation : public DataRelocation {
1112  relocInfo::relocType type() { return relocInfo::external_word_type; }
1113
1114 public:
1115  static RelocationHolder spec(address target) {
1116    assert(target != NULL, "must not be null");
1117    RelocationHolder rh = newHolder();
1118    new(rh) external_word_Relocation(target);
1119    return rh;
1120  }
1121
1122  // Use this one where all 32/64 bits of the target live in the code stream.
1123  // The target must be an intptr_t, and must be absolute (not relative).
1124  static RelocationHolder spec_for_immediate() {
1125    RelocationHolder rh = newHolder();
1126    new(rh) external_word_Relocation(NULL);
1127    return rh;
1128  }
1129
1130 private:
1131  address _target;                  // address in runtime
1132
1133  external_word_Relocation(address target) {
1134    _target = target;
1135  }
1136
1137  friend class RelocIterator;
1138  external_word_Relocation() { }
1139
1140 public:
1141  // data is packed as a well-known address in "1_int" format:  [a] or [Aa]
1142  // The function runtime_address_to_index is used to turn full addresses
1143  // to short indexes, if they are pre-registered by the stub mechanism.
1144  // If the "a" value is 0 (i.e., _target is NULL), the address is stored
1145  // in the code stream.  See external_word_Relocation::target().
1146  void pack_data_to(CodeSection* dest);
1147  void unpack_data();
1148
1149  void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
1150  address  target();        // if _target==NULL, fetch addr from code stream
1151  address  value()          { return target(); }
1152};
1153
1154class internal_word_Relocation : public DataRelocation {
1155  relocInfo::relocType type() { return relocInfo::internal_word_type; }
1156
1157 public:
1158  static RelocationHolder spec(address target) {
1159    assert(target != NULL, "must not be null");
1160    RelocationHolder rh = newHolder();
1161    new(rh) internal_word_Relocation(target);
1162    return rh;
1163  }
1164
1165  // use this one where all the bits of the target can fit in the code stream:
1166  static RelocationHolder spec_for_immediate() {
1167    RelocationHolder rh = newHolder();
1168    new(rh) internal_word_Relocation(NULL);
1169    return rh;
1170  }
1171
1172  internal_word_Relocation(address target) {
1173    _target  = target;
1174    _section = -1;  // self-relative
1175  }
1176
1177 protected:
1178  address _target;                  // address in CodeBlob
1179  int     _section;                 // section providing base address, if any
1180
1181  friend class RelocIterator;
1182  internal_word_Relocation() { }
1183
1184  // bit-width of LSB field in packed offset, if section >= 0
1185  enum { section_width = 2 }; // must equal CodeBuffer::sect_bits
1186
1187 public:
1188  // data is packed as a scaled offset in "1_int" format:  [o] or [Oo]
1189  // If the "o" value is 0 (i.e., _target is NULL), the offset is stored
1190  // in the code stream.  See internal_word_Relocation::target().
1191  // If _section is not -1, it is appended to the low bits of the offset.
1192  void pack_data_to(CodeSection* dest);
1193  void unpack_data();
1194
1195  void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
1196  address  target();        // if _target==NULL, fetch addr from code stream
1197  int      section()        { return _section;   }
1198  address  value()          { return target();   }
1199};
1200
1201class section_word_Relocation : public internal_word_Relocation {
1202  relocInfo::relocType type() { return relocInfo::section_word_type; }
1203
1204 public:
1205  static RelocationHolder spec(address target, int section) {
1206    RelocationHolder rh = newHolder();
1207    new(rh) section_word_Relocation(target, section);
1208    return rh;
1209  }
1210
1211  section_word_Relocation(address target, int section) {
1212    assert(target != NULL, "must not be null");
1213    assert(section >= 0, "must be a valid section");
1214    _target  = target;
1215    _section = section;
1216  }
1217
1218  //void pack_data_to -- inherited
1219  void unpack_data();
1220
1221 private:
1222  friend class RelocIterator;
1223  section_word_Relocation() { }
1224};
1225
1226
1227class poll_Relocation : public Relocation {
1228  bool          is_data()                      { return true; }
1229  relocInfo::relocType type() { return relocInfo::poll_type; }
1230  void     fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
1231};
1232
1233class poll_return_Relocation : public Relocation {
1234  bool          is_data()                      { return true; }
1235  relocInfo::relocType type() { return relocInfo::poll_return_type; }
1236  void     fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
1237};
1238
1239
1240class breakpoint_Relocation : public Relocation {
1241  relocInfo::relocType type() { return relocInfo::breakpoint_type; }
1242
1243  enum {
1244    // attributes which affect the interpretation of the data:
1245    removable_attr = 0x0010,   // buffer [i...] allows for undoing the trap
1246    internal_attr  = 0x0020,   // the target is an internal addr (local stub)
1247    settable_attr  = 0x0040,   // the target is settable
1248
1249    // states which can change over time:
1250    enabled_state  = 0x0100,   // breakpoint must be active in running code
1251    active_state   = 0x0200,   // breakpoint instruction actually in code
1252
1253    kind_mask      = 0x000F,   // mask for extracting kind
1254    high_bit       = 0x4000    // extra bit which is always set
1255  };
1256
1257 public:
1258  enum {
1259    // kinds:
1260    initialization = 1,
1261    safepoint      = 2
1262  };
1263
1264  // If target is NULL, 32 bits are reserved for a later set_target().
1265  static RelocationHolder spec(int kind, address target = NULL, bool internal_target = false) {
1266    RelocationHolder rh = newHolder();
1267    new(rh) breakpoint_Relocation(kind, target, internal_target);
1268    return rh;
1269  }
1270
1271 private:
1272  // We require every bits value to NOT to fit into relocInfo::datalen_width,
1273  // because we are going to actually store state in the reloc, and so
1274  // cannot allow it to be compressed (and hence copied by the iterator).
1275
1276  short   _bits;                  // bit-encoded kind, attrs, & state
1277  address _target;
1278
1279  breakpoint_Relocation(int kind, address target, bool internal_target);
1280
1281  friend class RelocIterator;
1282  breakpoint_Relocation() { }
1283
1284  short    bits()       const { return _bits; }
1285  short&   live_bits()  const { return data()[0]; }
1286  short*   instrs()     const { return data() + datalen() - instrlen(); }
1287  int      instrlen()   const { return removable() ? pd_breakpoint_size() : 0; }
1288
1289  void set_bits(short x) {
1290    assert(live_bits() == _bits, "must be the only mutator of reloc info");
1291    live_bits() = _bits = x;
1292  }
1293
1294 public:
1295  address  target()     const;
1296  void set_target(address x);
1297
1298  int  kind()           const { return  bits() & kind_mask; }
1299  bool enabled()        const { return (bits() &  enabled_state) != 0; }
1300  bool active()         const { return (bits() &   active_state) != 0; }
1301  bool internal()       const { return (bits() &  internal_attr) != 0; }
1302  bool removable()      const { return (bits() & removable_attr) != 0; }
1303  bool settable()       const { return (bits() &  settable_attr) != 0; }
1304
1305  void set_enabled(bool b);     // to activate, you must also say set_active
1306  void set_active(bool b);      // actually inserts bpt (must be enabled 1st)
1307
1308  // data is packed as 16 bits, followed by the target (1 or 2 words), followed
1309  // if necessary by empty storage for saving away original instruction bytes.
1310  void pack_data_to(CodeSection* dest);
1311  void unpack_data();
1312
1313  // during certain operations, breakpoints must be out of the way:
1314  void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
1315    assert(!active(), "cannot perform relocation on enabled breakpoints");
1316  }
1317};
1318
1319
1320// We know all the xxx_Relocation classes, so now we can define these:
1321#define EACH_CASE(name)                                         \
1322inline name##_Relocation* RelocIterator::name##_reloc() {       \
1323  assert(type() == relocInfo::name##_type, "type must agree");  \
1324  /* The purpose of the placed "new" is to re-use the same */   \
1325  /* stack storage for each new iteration. */                   \
1326  name##_Relocation* r = new(_rh) name##_Relocation();          \
1327  r->set_binding(this);                                         \
1328  r->name##_Relocation::unpack_data();                          \
1329  return r;                                                     \
1330}
1331APPLY_TO_RELOCATIONS(EACH_CASE);
1332#undef EACH_CASE
1333
1334inline RelocIterator::RelocIterator(nmethod* nm, address begin, address limit) {
1335  initialize(nm, begin, limit);
1336}
1337
1338// if you are going to patch code, you should use this subclass of
1339// RelocIterator
1340class PatchingRelocIterator : public RelocIterator {
1341 private:
1342  RelocIterator _init_state;
1343
1344  void prepass();               // deactivates all breakpoints
1345  void postpass();              // reactivates all enabled breakpoints
1346
1347  // do not copy these puppies; it would have unpredictable side effects
1348  // these are private and have no bodies defined because they should not be called
1349  PatchingRelocIterator(const RelocIterator&);
1350  void        operator=(const RelocIterator&);
1351
1352 public:
1353  PatchingRelocIterator(nmethod* nm, address begin = NULL, address limit = NULL)
1354    : RelocIterator(nm, begin, limit)                { prepass();  }
1355
1356  ~PatchingRelocIterator()                           { postpass(); }
1357};
1358
1359#endif // SHARE_VM_CODE_RELOCINFO_HPP
1360