1 /*
  2  * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_ASM_CODEBUFFER_HPP
 26 #define SHARE_ASM_CODEBUFFER_HPP
 27 
 28 #include "code/oopRecorder.hpp"
 29 #include "code/relocInfo.hpp"
 30 #include "compiler/compiler_globals.hpp"
 31 #include "runtime/os.hpp"
 32 #include "utilities/align.hpp"
 33 #include "utilities/debug.hpp"
 34 #include "utilities/growableArray.hpp"
 35 #include "utilities/linkedlist.hpp"
 36 #include "utilities/resizeableResourceHash.hpp"
 37 #include "utilities/macros.hpp"
 38 
 39 template <typename T>
 40 static inline void put_native(address p, T x) {
 41     memcpy((void*)p, &x, sizeof x);
 42 }
 43 
 44 class PhaseCFG;
 45 class Compile;
 46 class BufferBlob;
 47 class CodeBuffer;
 48 class Label;
 49 class ciMethod;
 50 class SharedStubToInterpRequest;
 51 
 52 class CodeOffsets: public StackObj {
 53 public:
 54   enum Entries { Entry,
 55                  Verified_Entry,
 56                  Frame_Complete, // Offset in the code where the frame setup is (for forte stackwalks) is complete
 57                  OSR_Entry,
 58                  Exceptions,     // Offset where exception handler lives
 59                  Deopt,          // Offset where deopt handler lives
 60                  DeoptMH,        // Offset where MethodHandle deopt handler lives
 61                  UnwindHandler,  // Offset to default unwind handler
 62                  max_Entries };
 63 
 64   // special value to note codeBlobs where profile (forte) stack walking is
 65   // always dangerous and suspect.
 66 
 67   enum { frame_never_safe = -1 };
 68 
 69 private:
 70   int _values[max_Entries];
 71 
 72 public:
 73   CodeOffsets() {
 74     _values[Entry         ] = 0;
 75     _values[Verified_Entry] = 0;
 76     _values[Frame_Complete] = frame_never_safe;
 77     _values[OSR_Entry     ] = 0;
 78     _values[Exceptions    ] = -1;
 79     _values[Deopt         ] = -1;
 80     _values[DeoptMH       ] = -1;
 81     _values[UnwindHandler ] = -1;
 82   }
 83 
 84   int value(Entries e) { return _values[e]; }
 85   void set_value(Entries e, int val) { _values[e] = val; }
 86 };
 87 
 88 // This class represents a stream of code and associated relocations.
 89 // There are a few in each CodeBuffer.
 90 // They are filled concurrently, and concatenated at the end.
 91 class CodeSection {
 92   friend class CodeBuffer;
 93   friend class AOTCodeReader;
 94  public:
 95   typedef int csize_t;  // code size type; would be size_t except for history
 96 
 97  private:
 98   address     _start;           // first byte of contents (instructions)
 99   address     _mark;            // user mark, usually an instruction beginning
100   address     _end;             // current end address
101   address     _limit;           // last possible (allocated) end address
102   relocInfo*  _locs_start;      // first byte of relocation information
103   relocInfo*  _locs_end;        // first byte after relocation information
104   relocInfo*  _locs_limit;      // first byte after relocation information buf
105   address     _locs_point;      // last relocated position (grows upward)
106   bool        _locs_own;        // did I allocate the locs myself?
107   bool        _scratch_emit;    // Buffer is used for scratch emit, don't relocate.
108   int         _skipped_instructions_size;
109   int8_t      _index;           // my section number (SECT_INST, etc.)
110   CodeBuffer* _outer;           // enclosing CodeBuffer
111 
112   // (Note:  _locs_point used to be called _last_reloc_offset.)
113 
114   CodeSection() {
115     _start         = nullptr;
116     _mark          = nullptr;
117     _end           = nullptr;
118     _limit         = nullptr;
119     _locs_start    = nullptr;
120     _locs_end      = nullptr;
121     _locs_limit    = nullptr;
122     _locs_point    = nullptr;
123     _locs_own      = false;
124     _scratch_emit  = false;
125     _skipped_instructions_size = 0;
126     DEBUG_ONLY(_index = -1);
127     DEBUG_ONLY(_outer = (CodeBuffer*)badAddress);
128   }
129 
130   void initialize_outer(CodeBuffer* outer, int8_t index) {
131     _outer = outer;
132     _index = index;
133   }
134 
135   void initialize(address start, csize_t size = 0) {
136     assert(_start == nullptr, "only one init step, please");
137     _start         = start;
138     _mark          = nullptr;
139     _end           = start;
140 
141     _limit         = start + size;
142     _locs_point    = start;
143   }
144 
145   void initialize_locs(int locs_capacity);
146   void expand_locs(int new_capacity);
147   void initialize_locs_from(const CodeSection* source_cs);
148 
149   // helper for CodeBuffer::expand()
150   void take_over_code_from(CodeSection* cs) {
151     _start      = cs->_start;
152     _mark       = cs->_mark;
153     _end        = cs->_end;
154     _limit      = cs->_limit;
155     _locs_point = cs->_locs_point;
156     _skipped_instructions_size = cs->_skipped_instructions_size;
157   }
158 
159  public:
160   address     start() const         { return _start; }
161   address     mark() const          { return _mark; }
162   address     end() const           { return _end; }
163   address     limit() const         { return _limit; }
164   csize_t     size() const          { return (csize_t)(_end - _start); }
165   csize_t     mark_off() const      { assert(_mark != nullptr, "not an offset");
166                                       return (csize_t)(_mark - _start); }
167   csize_t     capacity() const      { return (csize_t)(_limit - _start); }
168   csize_t     remaining() const     { return (csize_t)(_limit - _end); }
169 
170   relocInfo*  locs_start() const    { return _locs_start; }
171   relocInfo*  locs_end() const      { return _locs_end; }
172   int         locs_count() const    { return (int)(_locs_end - _locs_start); }
173   relocInfo*  locs_limit() const    { return _locs_limit; }
174   address     locs_point() const    { return _locs_point; }
175   csize_t     locs_point_off() const{ return (csize_t)(_locs_point - _start); }
176   csize_t     locs_capacity() const { return (csize_t)(_locs_limit - _locs_start); }
177 
178   int8_t      index() const         { return _index; }
179   bool        is_allocated() const  { return _start != nullptr; }
180   bool        is_empty() const      { return _start == _end; }
181   bool        has_locs() const      { return _locs_end != nullptr; }
182 
183   // Mark scratch buffer.
184   void        set_scratch_emit()    { _scratch_emit = true; }
185   void        clear_scratch_emit()  { _scratch_emit = false; }
186   bool        scratch_emit()        { return _scratch_emit; }
187 
188   CodeBuffer* outer() const         { return _outer; }
189 
190   // is a given address in this section?  (2nd version is end-inclusive)
191   bool contains(address pc) const   { return pc >= _start && pc <  _end; }
192   bool contains2(address pc) const  { return pc >= _start && pc <= _end; }
193   bool allocates(address pc) const  { return pc >= _start && pc <  _limit; }
194   bool allocates2(address pc) const { return pc >= _start && pc <= _limit; }
195 
196   // checks if two CodeSections are disjoint
197   //
198   // limit is an exclusive address and can be the start of another
199   // section.
200   bool disjoint(CodeSection* cs) const { return cs->_limit <= _start || cs->_start >= _limit; }
201 
202   void    set_end(address pc)       { assert(allocates2(pc), "not in CodeBuffer memory: " INTPTR_FORMAT " <= " INTPTR_FORMAT " <= " INTPTR_FORMAT, p2i(_start), p2i(pc), p2i(_limit)); _end = pc; }
203   void    set_mark(address pc)      { assert(contains2(pc), "not in codeBuffer");
204                                       _mark = pc; }
205   void    set_mark()                { _mark = _end; }
206   void    clear_mark()              { _mark = nullptr; }
207 
208   void    set_locs_end(relocInfo* p) {
209     assert(p <= locs_limit(), "locs data fits in allocated buffer");
210     _locs_end = p;
211   }
212   void    set_locs_point(address pc) {
213     assert(pc >= locs_point(), "relocation addr may not decrease");
214     assert(allocates2(pc),     "relocation addr " INTPTR_FORMAT " must be in this section from " INTPTR_FORMAT " to " INTPTR_FORMAT, p2i(pc), p2i(_start), p2i(_limit));
215     _locs_point = pc;
216   }
217 
218   void register_skipped(int size) {
219     _skipped_instructions_size += size;
220   }
221 
222   // Code emission
223   void emit_int8(uint8_t x1) {
224     address curr = end();
225     *((uint8_t*)  curr++) = x1;
226     set_end(curr);
227   }
228 
229   template <typename T>
230   void emit_native(T x) { put_native(end(), x); set_end(end() + sizeof x); }
231 
232   void emit_int16(uint16_t x) { emit_native(x); }
233   void emit_int16(uint8_t x1, uint8_t x2) {
234     address curr = end();
235     *((uint8_t*)  curr++) = x1;
236     *((uint8_t*)  curr++) = x2;
237     set_end(curr);
238   }
239 
240   void emit_int24(uint8_t x1, uint8_t x2, uint8_t x3)  {
241     address curr = end();
242     *((uint8_t*)  curr++) = x1;
243     *((uint8_t*)  curr++) = x2;
244     *((uint8_t*)  curr++) = x3;
245     set_end(curr);
246   }
247 
248   void emit_int32(uint32_t x) { emit_native(x); }
249   void emit_int32(uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4)  {
250     address curr = end();
251     *((uint8_t*)  curr++) = x1;
252     *((uint8_t*)  curr++) = x2;
253     *((uint8_t*)  curr++) = x3;
254     *((uint8_t*)  curr++) = x4;
255     set_end(curr);
256   }
257 
258   void emit_int64(uint64_t x)  { emit_native(x); }
259   void emit_float(jfloat  x)   { emit_native(x); }
260   void emit_double(jdouble x)  { emit_native(x); }
261   void emit_address(address x) { emit_native(x); }
262 
263   // Share a scratch buffer for relocinfo.  (Hacky; saves a resource allocation.)
264   void initialize_shared_locs(relocInfo* buf, int length);
265 
266   // Manage labels and their addresses.
267   address target(Label& L, address branch_pc);
268 
269   // Emit a relocation.
270   void relocate(address at, RelocationHolder const& rspec, int format = 0);
271   void relocate(address at,    relocInfo::relocType rtype, int format = 0, jint method_index = 0);
272 
273   int alignment() const;
274 
275   // Slop between sections, used only when allocating temporary BufferBlob buffers.
276   static csize_t end_slop()         { return MAX2((int)sizeof(jdouble), (int)CodeEntryAlignment); }
277 
278   csize_t align_at_start(csize_t off) const {
279     return (csize_t) align_up(off, alignment());
280   }
281 
282   // Ensure there's enough space left in the current section.
283   // Return true if there was an expansion.
284   bool maybe_expand_to_ensure_remaining(csize_t amount);
285 
286 #ifndef PRODUCT
287   void decode();
288   void print_on(outputStream* st, const char* name);
289 #endif //PRODUCT
290 };
291 
292 
293 #ifndef PRODUCT
294 
295 // ----- CHeapString -----------------------------------------------------------
296 
297 class CHeapString : public CHeapObj<mtCode> {
298  public:
299   CHeapString(const char* str) : _string(os::strdup(str)) {}
300   ~CHeapString();
301   const char* string() const { return _string; }
302 
303  private:
304   const char* _string;
305 };
306 
307 // ----- AsmRemarkCollection ---------------------------------------------------
308 
309 class AsmRemarkCollection : public CHeapObj<mtCode> {
310  public:
311   AsmRemarkCollection() : _ref_cnt(1), _remarks(nullptr), _next(nullptr) {}
312  ~AsmRemarkCollection() {
313     assert(is_empty(), "Must 'clear()' before deleting!");
314     assert(_ref_cnt == 0, "No uses must remain when deleting!");
315   }
316   AsmRemarkCollection* reuse() {
317     precond(_ref_cnt > 0);
318     return _ref_cnt++, this;
319   }
320 
321   const char* insert(uint offset, const char* remark);
322   const char* lookup(uint offset) const;
323   const char* next(uint offset) const;
324 
325   bool is_empty() const { return _remarks == nullptr; }
326   uint clear();
327 
328   template<typename Function>
329   bool iterate(Function function) const { // lambda enabled API
330     if (_remarks != nullptr) {
331       Cell* tmp = _remarks;
332       do {
333         if(!function(tmp->offset, tmp->string())) {
334           return false;
335         }
336         tmp = tmp->next;
337       } while (tmp != _remarks);
338     }
339     return true;
340   }
341 
342  private:
343   struct Cell : CHeapString {
344     Cell(const char* remark, uint offset) :
345         CHeapString(remark), offset(offset), prev(nullptr), next(nullptr) {}
346     void push_back(Cell* cell) {
347       Cell* head = this;
348       Cell* tail = prev;
349       tail->next = cell;
350       cell->next = head;
351       cell->prev = tail;
352       prev = cell;
353     }
354     uint offset;
355     Cell* prev;
356     Cell* next;
357   };
358   uint  _ref_cnt;
359   Cell* _remarks;
360   // Using a 'mutable' iteration pointer to allow 'const' on lookup/next (that
361   // does not change the state of the list per se), supportig a simplistic
362   // iteration scheme.
363   mutable Cell* _next;
364 };
365 
366 // ----- DbgStringCollection ---------------------------------------------------
367 
368 class DbgStringCollection : public CHeapObj<mtCode> {
369  public:
370   DbgStringCollection() : _ref_cnt(1), _strings(nullptr) {}
371  ~DbgStringCollection() {
372     assert(is_empty(), "Must 'clear()' before deleting!");
373     assert(_ref_cnt == 0, "No uses must remain when deleting!");
374   }
375   DbgStringCollection* reuse() {
376     precond(_ref_cnt > 0);
377     return _ref_cnt++, this;
378   }
379 
380   const char* insert(const char* str);
381   const char* lookup(const char* str) const;
382 
383   bool is_empty() const { return _strings == nullptr; }
384   uint clear();
385 
386   template<typename Function>
387   bool iterate(Function function) const { // lambda enabled API
388     if (_strings != nullptr) {
389       Cell* tmp = _strings;
390       do {
391         if (!function(tmp->string())) {
392           return false;
393         }
394         tmp = tmp->next;
395       } while (tmp != _strings);
396     }
397     return true;
398   }
399 
400  private:
401   struct Cell : CHeapString {
402     Cell(const char* dbgstr) :
403         CHeapString(dbgstr), prev(nullptr), next(nullptr) {}
404     void push_back(Cell* cell) {
405       Cell* head = this;
406       Cell* tail = prev;
407       tail->next = cell;
408       cell->next = head;
409       cell->prev = tail;
410       prev = cell;
411     }
412     Cell* prev;
413     Cell* next;
414   };
415   uint  _ref_cnt;
416   Cell* _strings;
417 };
418 
419 // The assumption made here is that most code remarks (or comments) added to
420 // the generated assembly code are unique, i.e. there is very little gain in
421 // trying to share the strings between the different offsets tracked in a
422 // buffer (or blob).
423 
424 class AsmRemarks {
425  public:
426   AsmRemarks();
427  ~AsmRemarks();
428 
429   static void init(AsmRemarks& asm_remarks);
430 
431   const char* insert(uint offset, const char* remstr);
432 
433   bool is_empty() const;
434 
435   void share(const AsmRemarks &src);
436   void clear();
437   uint print(uint offset, outputStream* strm = tty) const;
438 
439   // For testing purposes only.
440   const AsmRemarkCollection* ref() const { return _remarks; }
441 
442   template<typename Function>
443   inline bool iterate(Function function) const { return _remarks->iterate(function); }
444 
445 private:
446   AsmRemarkCollection* _remarks;
447 };
448 
449 // The assumption made here is that the number of debug strings (with a fixed
450 // address requirement) is a rather small set per compilation unit.
451 
452 class DbgStrings {
453  public:
454   DbgStrings();
455  ~DbgStrings();
456 
457   static void init(DbgStrings& dbg_strings);
458 
459   const char* insert(const char* dbgstr);
460 
461   bool is_empty() const;
462 
463   void share(const DbgStrings &src);
464   void clear();
465 
466   // For testing purposes only.
467   const DbgStringCollection* ref() const { return _strings; }
468 
469   template<typename Function>
470   bool iterate(Function function) const { return _strings->iterate(function); }
471 
472 private:
473   DbgStringCollection* _strings;
474 };
475 #endif // not PRODUCT
476 
477 
478 #ifdef ASSERT
479 #include "utilities/copy.hpp"
480 
481 class Scrubber {
482  public:
483   Scrubber(void* addr, size_t size) : _addr(addr), _size(size) {}
484  ~Scrubber() {
485     Copy::fill_to_bytes(_addr, _size, badResourceValue);
486   }
487  private:
488   void*  _addr;
489   size_t _size;
490 };
491 #endif // ASSERT
492 
493 typedef GrowableArray<SharedStubToInterpRequest> SharedStubToInterpRequests;
494 
495 // A CodeBuffer describes a memory space into which assembly
496 // code is generated.  This memory space usually occupies the
497 // interior of a single BufferBlob, but in some cases it may be
498 // an arbitrary span of memory, even outside the code cache.
499 //
500 // A code buffer comes in two variants:
501 //
502 // (1) A CodeBuffer referring to an already allocated piece of memory:
503 //     This is used to direct 'static' code generation (e.g. for interpreter
504 //     or stubroutine generation, etc.).  This code comes with NO relocation
505 //     information.
506 //
507 // (2) A CodeBuffer referring to a piece of memory allocated when the
508 //     CodeBuffer is allocated.  This is used for nmethod generation.
509 //
510 // The memory can be divided up into several parts called sections.
511 // Each section independently accumulates code (or data) an relocations.
512 // Sections can grow (at the expense of a reallocation of the BufferBlob
513 // and recopying of all active sections).  When the buffered code is finally
514 // written to an nmethod (or other CodeBlob), the contents (code, data,
515 // and relocations) of the sections are padded to an alignment and concatenated.
516 // Instructions and data in one section can contain relocatable references to
517 // addresses in a sibling section.
518 
519 class CodeBuffer: public StackObj DEBUG_ONLY(COMMA private Scrubber) {
520   friend class CodeSection;
521   friend class StubCodeGenerator;
522   friend class AOTCodeReader;
523 
524  private:
525   // CodeBuffers must be allocated on the stack except for a single
526   // special case during expansion which is handled internally.  This
527   // is done to guarantee proper cleanup of resources.
528   void* operator new(size_t size) throw() { return resource_allocate_bytes(size); }
529   void  operator delete(void* p)          { ShouldNotCallThis(); }
530 
531  public:
532   typedef int csize_t;  // code size type; would be size_t except for history
533   enum : int8_t {
534     // Here is the list of all possible sections.  The order reflects
535     // the final layout.
536     SECT_FIRST = 0,
537     SECT_CONSTS = SECT_FIRST, // Non-instruction data:  Floats, jump tables, etc.
538     SECT_INSTS,               // Executable instructions.
539     SECT_STUBS,               // Outbound trampolines for supporting call sites.
540     SECT_LIMIT, SECT_NONE = -1
541   };
542 
543   typedef LinkedListImpl<int> Offsets;
544   typedef ResizeableResourceHashtable<address, Offsets, AnyObj::C_HEAP, mtCompiler> SharedTrampolineRequests;
545 
546  private:
547   enum {
548     sect_bits = 2,      // assert (SECT_LIMIT <= (1<<sect_bits))
549     sect_mask = (1<<sect_bits)-1
550   };
551 
552   const char*  _name;
553 
554   CodeSection  _consts;             // constants, jump tables
555   CodeSection  _insts;              // instructions (the main section)
556   CodeSection  _stubs;              // stubs (call site support), deopt, exception handling
557 
558   CodeBuffer*  _before_expand;  // dead buffer, from before the last expansion
559 
560   BufferBlob*  _blob;           // optional buffer in CodeCache for generated code
561   address      _total_start;    // first address of combined memory buffer
562   csize_t      _total_size;     // size in bytes of combined memory buffer
563 
564   OopRecorder* _oop_recorder;
565 
566   OopRecorder  _default_oop_recorder;  // override with initialize_oop_recorder
567   Arena*       _overflow_arena;
568 
569   address      _last_insn;      // used to merge consecutive memory barriers, loads or stores.
570   address      _last_label;     // record last bind label address, it's also the start of current bb.
571 
572   SharedStubToInterpRequests* _shared_stub_to_interp_requests; // used to collect requests for shared iterpreter stubs
573   SharedTrampolineRequests*   _shared_trampoline_requests;     // used to collect requests for shared trampolines
574   bool         _finalize_stubs; // Indicate if we need to finalize stubs to make CodeBuffer final.
575 
576   int          _const_section_alignment;
577 
578 #ifndef PRODUCT
579   AsmRemarks   _asm_remarks;
580   DbgStrings   _dbg_strings;
581   bool         _collect_comments; // Indicate if we need to collect block comments at all.
582   address      _decode_begin;     // start address for decode
583   address      decode_begin();
584 #endif
585 
586   void initialize_misc(const char * name) {
587     // all pointers other than code_start/end and those inside the sections
588     assert(name != nullptr, "must have a name");
589     _name            = name;
590     _before_expand   = nullptr;
591     _blob            = nullptr;
592     _total_start     = nullptr;
593     _total_size      = 0;
594     _oop_recorder    = nullptr;
595     _overflow_arena  = nullptr;
596     _last_insn       = nullptr;
597     _last_label      = nullptr;
598     _finalize_stubs  = false;
599     _shared_stub_to_interp_requests = nullptr;
600     _shared_trampoline_requests = nullptr;
601 
602     _consts.initialize_outer(this, SECT_CONSTS);
603     _insts.initialize_outer(this,  SECT_INSTS);
604     _stubs.initialize_outer(this,  SECT_STUBS);
605 
606     // Default is to align on 8 bytes. A compiler can change this
607     // if larger alignment (e.g., 32-byte vector masks) is required.
608     _const_section_alignment = (int) sizeof(jdouble);
609 
610 #ifndef PRODUCT
611     _decode_begin    = nullptr;
612     // Collect block comments, but restrict collection to cases where a disassembly is output.
613     _collect_comments = ( PrintAssembly
614                        || PrintStubCode
615                        || PrintMethodHandleStubs
616                        || PrintInterpreter
617                        || PrintSignatureHandlers
618                        || UnlockDiagnosticVMOptions
619                         );
620 #endif
621   }
622 
623   void initialize(address code_start, csize_t code_size) {
624     _total_start = code_start;
625     _total_size  = code_size;
626     // Initialize the main section:
627     _insts.initialize(code_start, code_size);
628     assert(!_stubs.is_allocated(),  "no garbage here");
629     assert(!_consts.is_allocated(), "no garbage here");
630     _oop_recorder = &_default_oop_recorder;
631   }
632 
633   void initialize_section_size(CodeSection* cs, csize_t size);
634 
635   // helper for CodeBuffer::expand()
636   void take_over_code_from(CodeBuffer* cs);
637 
638   // ensure sections are disjoint, ordered, and contained in the blob
639   void verify_section_allocation();
640 
641   // copies combined relocations to the blob, returns bytes copied
642   // (if target is null, it is a dry run only, just for sizing)
643   csize_t copy_relocations_to(CodeBlob* blob) const;
644 
645   // copies combined code to the blob (assumes relocs are already in there)
646   void copy_code_to(CodeBlob* blob);
647 
648   // moves code sections to new buffer (assumes relocs are already in there)
649   void relocate_code_to(CodeBuffer* cb) const;
650 
651   // adjust some internal address during expand
652   void adjust_internal_address(address from, address to);
653 
654   // set up a model of the final layout of my contents
655   void compute_final_layout(CodeBuffer* dest) const;
656 
657   // Expand the given section so at least 'amount' is remaining.
658   // Creates a new, larger BufferBlob, and rewrites the code & relocs.
659   void expand(CodeSection* which_cs, csize_t amount);
660 
661   // Helper for expand.
662   csize_t figure_expanded_capacities(CodeSection* which_cs, csize_t amount, csize_t* new_capacity);
663 
664  public:
665   // (1) code buffer referring to pre-allocated instruction memory
666   CodeBuffer(address code_start, csize_t code_size)
667     DEBUG_ONLY(: Scrubber(this, sizeof(*this)))
668   {
669     assert(code_start != nullptr, "sanity");
670     initialize_misc("static buffer");
671     initialize(code_start, code_size);
672     DEBUG_ONLY(verify_section_allocation();)
673   }
674 
675   // (2) CodeBuffer referring to pre-allocated CodeBlob.
676   CodeBuffer(CodeBlob* blob);
677 
678   // (3) code buffer allocating codeBlob memory for code & relocation
679   // info but with lazy initialization.  The name must be something
680   // informative.
681   CodeBuffer(const char* name)
682     DEBUG_ONLY(: Scrubber(this, sizeof(*this)))
683   {
684     initialize_misc(name);
685   }
686 
687   // (4) code buffer allocating codeBlob memory for code & relocation
688   // info.  The name must be something informative and code_size must
689   // include both code and stubs sizes.
690   CodeBuffer(const char* name, csize_t code_size, csize_t locs_size)
691     DEBUG_ONLY(: Scrubber(this, sizeof(*this)))
692   {
693     initialize_misc(name);
694     initialize(code_size, locs_size);
695   }
696 
697   ~CodeBuffer();
698 
699   // Initialize a CodeBuffer constructed using constructor 3.  Using
700   // constructor 4 is equivalent to calling constructor 3 and then
701   // calling this method.  It's been factored out for convenience of
702   // construction.
703   void initialize(csize_t code_size, csize_t locs_size);
704 
705   CodeSection* consts() { return &_consts; }
706   CodeSection* insts() { return &_insts; }
707   CodeSection* stubs() { return &_stubs; }
708 
709   const CodeSection* insts() const { return &_insts; }
710 
711   // present sections in order; return null at end; consts is #0, etc.
712   CodeSection* code_section(int n) {
713     // This makes the slightly questionable but portable assumption
714     // that the various members (_consts, _insts, _stubs, etc.) are
715     // adjacent in the layout of CodeBuffer.
716     CodeSection* cs = &_consts + n;
717     assert(cs->index() == n || !cs->is_allocated(), "sanity");
718     return cs;
719   }
720   const CodeSection* code_section(int n) const {  // yucky const stuff
721     return ((CodeBuffer*)this)->code_section(n);
722   }
723   static const char* code_section_name(int n);
724   int section_index_of(address addr) const;
725   bool contains(address addr) const {
726     // handy for debugging
727     return section_index_of(addr) > SECT_NONE;
728   }
729 
730   // A stable mapping between 'locators' (small ints) and addresses.
731   static int locator_pos(int locator)   { return locator >> sect_bits; }
732   static int locator_sect(int locator)  { return locator &  sect_mask; }
733   static int locator(int pos, int sect) { return (pos << sect_bits) | sect; }
734   int        locator(address addr) const;
735   address    locator_address(int locator) const {
736     if (locator < 0)  return nullptr;
737     address start = code_section(locator_sect(locator))->start();
738     return start + locator_pos(locator);
739   }
740 
741   // Heuristic for pre-packing the taken/not-taken bit of a predicted branch.
742   bool is_backward_branch(Label& L);
743 
744   // Properties
745   const char* name() const                  { return _name; }
746   CodeBuffer* before_expand() const         { return _before_expand; }
747   BufferBlob* blob() const                  { return _blob; }
748   void    set_blob(BufferBlob* blob);
749   void   free_blob();                       // Free the blob, if we own one.
750 
751   // Properties relative to the insts section:
752   address       insts_begin() const      { return _insts.start();      }
753   address       insts_end() const        { return _insts.end();        }
754   void      set_insts_end(address end)   {        _insts.set_end(end); }
755   address       insts_mark() const       { return _insts.mark();       }
756   void      set_insts_mark()             {        _insts.set_mark();   }
757 
758   // is there anything in the buffer other than the current section?
759   bool    is_pure() const                { return insts_size() == total_content_size(); }
760 
761   // size in bytes of output so far in the insts sections
762   csize_t insts_size() const             { return _insts.size(); }
763 
764   // same as insts_size(), except that it asserts there is no non-code here
765   csize_t pure_insts_size() const        { assert(is_pure(), "no non-code");
766                                            return insts_size(); }
767   // capacity in bytes of the insts sections
768   csize_t insts_capacity() const         { return _insts.capacity(); }
769 
770   // number of bytes remaining in the insts section
771   csize_t insts_remaining() const        { return _insts.remaining(); }
772 
773   // is a given address in the insts section?  (2nd version is end-inclusive)
774   bool insts_contains(address pc) const  { return _insts.contains(pc); }
775   bool insts_contains2(address pc) const { return _insts.contains2(pc); }
776 
777   // Record any extra oops required to keep embedded metadata alive
778   void finalize_oop_references(const methodHandle& method);
779 
780   // Allocated size in all sections, when aligned and concatenated
781   // (this is the eventual state of the content in its final
782   // CodeBlob).
783   csize_t total_content_size() const;
784 
785   // Combined offset (relative to start of first section) of given
786   // section, as eventually found in the final CodeBlob.
787   csize_t total_offset_of(const CodeSection* cs) const;
788 
789   // allocated size of all relocation data, including index, rounded up
790   csize_t total_relocation_size() const;
791 
792   int total_skipped_instructions_size() const;
793 
794   csize_t copy_relocations_to(address buf, csize_t buf_limit, bool only_inst) const;
795 
796   // allocated size of any and all recorded oops
797   csize_t total_oop_size() const {
798     OopRecorder* recorder = oop_recorder();
799     return (recorder == nullptr)? 0: recorder->oop_size();
800   }
801 
802   // allocated size of any and all recorded metadata
803   csize_t total_metadata_size() const {
804     OopRecorder* recorder = oop_recorder();
805     return (recorder == nullptr)? 0: recorder->metadata_size();
806   }
807 
808   // Configuration functions, called immediately after the CB is constructed.
809   // The section sizes are subtracted from the original insts section.
810   // Note:  Call them in reverse section order, because each steals from insts.
811   void initialize_consts_size(csize_t size)            { initialize_section_size(&_consts,  size); }
812   void initialize_stubs_size(csize_t size)             { initialize_section_size(&_stubs,   size); }
813   // Override default oop recorder.
814   void initialize_oop_recorder(OopRecorder* r);
815 
816   OopRecorder* oop_recorder() const { return _oop_recorder; }
817 
818   address last_insn() const { return _last_insn; }
819   void set_last_insn(address a) { _last_insn = a; }
820   void clear_last_insn() { set_last_insn(nullptr); }
821 
822   address last_label() const { return _last_label; }
823   void set_last_label(address a) { _last_label = a; }
824 
825 #ifndef PRODUCT
826   AsmRemarks &asm_remarks() { return _asm_remarks; }
827   DbgStrings &dbg_strings() { return _dbg_strings; }
828 
829   void clear_strings() {
830     _asm_remarks.clear();
831     _dbg_strings.clear();
832   }
833 #endif
834 
835   // Code generation
836   void relocate(address at, RelocationHolder const& rspec, int format = 0) {
837     _insts.relocate(at, rspec, format);
838   }
839   void relocate(address at,    relocInfo::relocType rtype, int format = 0) {
840     _insts.relocate(at, rtype, format);
841   }
842 
843   // Management of overflow storage for binding of Labels.
844   GrowableArray<int>* create_patch_overflow();
845 
846   // NMethod generation
847   void copy_code_and_locs_to(CodeBlob* blob) {
848     assert(blob != nullptr, "sane");
849     copy_relocations_to(blob);
850     copy_code_to(blob);
851   }
852   void copy_values_to(nmethod* nm) {
853     if (!oop_recorder()->is_unused()) {
854       oop_recorder()->copy_values_to(nm);
855     }
856   }
857 
858   void block_comment(ptrdiff_t offset, const char* comment) PRODUCT_RETURN;
859   const char* code_string(const char* str) PRODUCT_RETURN_(return nullptr;);
860 
861   // Log a little info about section usage in the CodeBuffer
862   void log_section_sizes(const char* name);
863 
864   // Make a set of stubs final. It can create/optimize stubs.
865   bool finalize_stubs();
866 
867   // Request for a shared stub to the interpreter
868   void shared_stub_to_interp_for(ciMethod* callee, csize_t call_offset);
869 
870   void set_const_section_alignment(int align) {
871     _const_section_alignment = align_up(align, HeapWordSize);
872   }
873 
874 #ifndef PRODUCT
875  public:
876   // Printing / Decoding
877   // decodes from decode_begin() to code_end() and sets decode_begin to end
878   void    decode();
879   void    print_on(outputStream* st);
880 #endif
881   // Directly disassemble code buffer.
882   void    decode(address start, address end);
883 
884   // The following header contains architecture-specific implementations
885 #include CPU_HEADER(codeBuffer)
886 
887 };
888 
889 // A Java method can have calls of Java methods which can be statically bound.
890 // Calls of Java methods need stubs to the interpreter. Calls sharing the same Java method
891 // can share a stub to the interpreter.
892 // A SharedStubToInterpRequest is a request for a shared stub to the interpreter.
893 class SharedStubToInterpRequest : public ResourceObj {
894  private:
895   ciMethod* _shared_method;
896   CodeBuffer::csize_t _call_offset; // The offset of the call in CodeBuffer
897 
898  public:
899   SharedStubToInterpRequest(ciMethod* method = nullptr, CodeBuffer::csize_t call_offset = -1) : _shared_method(method),
900       _call_offset(call_offset) {}
901 
902   ciMethod* shared_method() const { return _shared_method; }
903   CodeBuffer::csize_t call_offset() const { return _call_offset; }
904 };
905 
906 inline bool CodeSection::maybe_expand_to_ensure_remaining(csize_t amount) {
907   if (remaining() < amount) { _outer->expand(this, amount); return true; }
908   return false;
909 }
910 
911 #endif // SHARE_ASM_CODEBUFFER_HPP