1 /*
  2  * Copyright (c) 1998, 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_CODE_CODEBLOB_HPP
 26 #define SHARE_CODE_CODEBLOB_HPP
 27 
 28 #include "asm/codeBuffer.hpp"
 29 #include "compiler/compilerDefinitions.hpp"
 30 #include "compiler/oopMap.hpp"
 31 #include "runtime/javaFrameAnchor.hpp"
 32 #include "runtime/frame.hpp"
 33 #include "runtime/handles.hpp"
 34 #include "utilities/align.hpp"
 35 #include "utilities/macros.hpp"
 36 
 37 class ImmutableOopMap;
 38 class ImmutableOopMapSet;
 39 class JNIHandleBlock;
 40 class OopMapSet;
 41 
 42 // CodeBlob Types
 43 // Used in the CodeCache to assign CodeBlobs to different CodeHeaps
 44 enum class CodeBlobType {
 45   MethodNonProfiled   = 0,    // Execution level 1 and 4 (non-profiled) nmethods (including native nmethods)
 46   MethodProfiled      = 1,    // Execution level 2 and 3 (profiled) nmethods
 47   NonNMethod          = 2,    // Non-nmethods like Buffers, Adapters and Runtime Stubs
 48   All                 = 3,    // All types (No code cache segmentation)
 49   NumTypes            = 4     // Number of CodeBlobTypes
 50 };
 51 
 52 // CodeBlob - superclass for all entries in the CodeCache.
 53 //
 54 // Subtypes are:
 55 //  nmethod              : JIT Compiled Java methods
 56 //  RuntimeBlob          : Non-compiled method code; generated glue code
 57 //   BufferBlob          : Used for non-relocatable code such as interpreter, stubroutines, etc.
 58 //    AdapterBlob        : Used to hold C2I/I2C adapters
 59 //    VtableBlob         : Used for holding vtable chunks
 60 //    MethodHandlesAdapterBlob : Used to hold MethodHandles adapters
 61 //    BufferedInlineTypeBlob   : used for pack/unpack handlers
 62 //   RuntimeStub         : Call to VM runtime methods
 63 //   SingletonBlob       : Super-class for all blobs that exist in only one instance
 64 //    DeoptimizationBlob : Used for deoptimization
 65 //    SafepointBlob      : Used to handle illegal instruction exceptions
 66 //    ExceptionBlob      : Used for stack unrolling
 67 //    UncommonTrapBlob   : Used to handle uncommon traps
 68 //   UpcallStub  : Used for upcalls from native code
 69 //
 70 //
 71 // Layout in the CodeCache:
 72 //   - header
 73 //   - content space
 74 //     - instruction space
 75 // Outside of the CodeCache:
 76 //   - mutable_data
 77 //     - relocation info
 78 //     - additional data for subclasses
 79 
 80 enum class CodeBlobKind : u1 {
 81   None,
 82   Nmethod,
 83   Buffer,
 84   Adapter,
 85   Vtable,
 86   MHAdapter,
 87   BufferedInlineType,
 88   RuntimeStub,
 89   Deoptimization,
 90   Safepoint,
 91 #ifdef COMPILER2
 92   Exception,
 93   UncommonTrap,
 94 #endif
 95   Upcall,
 96   Number_Of_Kinds
 97 };
 98 
 99 class UpcallStub;      // for as_upcall_stub()
100 class RuntimeStub;     // for as_runtime_stub()
101 class JavaFrameAnchor; // for UpcallStub::jfa_for_frame
102 class AdapterBlob;
103 class ExceptionBlob;
104 class DeoptimizationBlob;
105 class SafepointBlob;
106 class UncommonTrapBlob;
107 
108 class CodeBlob {
109   friend class VMStructs;
110   friend class JVMCIVMStructs;
111 
112 private:
113   void restore_mutable_data(address reloc_data);
114 
115 protected:
116   // order fields from large to small to minimize padding between fields
117   ImmutableOopMapSet* _oop_maps;   // OopMap for this CodeBlob
118   const char*         _name;
119   address             _mutable_data;
120 
121   int      _size;                  // total size of CodeBlob in bytes
122   int      _relocation_size;       // size of relocation (could be bigger than 64Kb)
123   int      _content_offset;        // offset to where content region begins (this includes consts, insts, stubs)
124   int      _code_offset;           // offset to where instructions region begins (this includes insts, stubs)
125   int      _data_offset;           // offset to where data region begins
126   int      _frame_size;            // size of stack frame in words (NOT slots. On x64 these are 64bit words)
127   int      _mutable_data_size;
128 
129   S390_ONLY(int _ctable_offset;)
130 
131   uint16_t _header_size;           // size of header (depends on subclass)
132   int16_t  _frame_complete_offset; // instruction offsets in [0.._frame_complete_offset) have
133                                    // not finished setting up their frame. Beware of pc's in
134                                    // that range. There is a similar range(s) on returns
135                                    // which we don't detect.
136 
137   CodeBlobKind _kind;              // Kind of this code blob
138 
139   bool _caller_must_gc_arguments;
140 
141 #ifndef PRODUCT
142   AsmRemarks _asm_remarks;
143   DbgStrings _dbg_strings;
144 #endif
145 
146   void print_on_impl(outputStream* st) const;
147   void print_value_on_impl(outputStream* st) const;
148 
149   class Vptr {
150    public:
151     virtual void print_on(const CodeBlob* instance, outputStream* st) const = 0;
152     virtual void print_value_on(const CodeBlob* instance, outputStream* st) const = 0;
153     virtual void prepare_for_archiving(CodeBlob* instance) const {
154       instance->prepare_for_archiving_impl();
155     };
156     virtual void post_restore(CodeBlob* instance) const {
157       instance->post_restore_impl();
158     };
159   };
160 
161   static const Vptr* vptr(CodeBlobKind kind);
162   const Vptr* vptr() const;
163 
164   CodeBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size,
165            int16_t frame_complete_offset, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments,
166            int mutable_data_size);
167 
168   // Simple CodeBlob used for simple BufferBlob.
169   CodeBlob(const char* name, CodeBlobKind kind, int size, uint16_t header_size);
170 
171 
172   void operator delete(void* p) { }
173 
174   void prepare_for_archiving_impl();
175   void post_restore_impl();
176 
177 public:
178 
179   ~CodeBlob() {
180     assert(_oop_maps == nullptr, "Not flushed");
181   }
182 
183   // Returns the space needed for CodeBlob
184   static unsigned int allocation_size(CodeBuffer* cb, int header_size);
185   static unsigned int align_code_offset(int offset);
186 
187   // Deletion
188   void purge();
189 
190   // Typing
191   bool is_nmethod() const                     { return _kind == CodeBlobKind::Nmethod; }
192   bool is_buffer_blob() const                 { return _kind == CodeBlobKind::Buffer; }
193   bool is_runtime_stub() const                { return _kind == CodeBlobKind::RuntimeStub; }
194   bool is_deoptimization_stub() const         { return _kind == CodeBlobKind::Deoptimization; }
195 #ifdef COMPILER2
196   bool is_uncommon_trap_stub() const          { return _kind == CodeBlobKind::UncommonTrap; }
197   bool is_exception_stub() const              { return _kind == CodeBlobKind::Exception; }
198 #else
199   bool is_uncommon_trap_stub() const          { return false; }
200   bool is_exception_stub() const              { return false; }
201 #endif
202   bool is_safepoint_stub() const              { return _kind == CodeBlobKind::Safepoint; }
203   bool is_adapter_blob() const                { return _kind == CodeBlobKind::Adapter; }
204   bool is_vtable_blob() const                 { return _kind == CodeBlobKind::Vtable; }
205   bool is_method_handles_adapter_blob() const { return _kind == CodeBlobKind::MHAdapter; }
206   bool is_buffered_inline_type_blob() const   { return _kind == CodeBlobKind::BufferedInlineType; }
207   bool is_upcall_stub() const                 { return _kind == CodeBlobKind::Upcall; }
208 
209   // Casting
210   nmethod* as_nmethod_or_null() const         { return is_nmethod() ? (nmethod*) this : nullptr; }
211   nmethod* as_nmethod() const                 { assert(is_nmethod(), "must be nmethod"); return (nmethod*) this; }
212   CodeBlob* as_codeblob() const               { return (CodeBlob*) this; }
213   AdapterBlob* as_adapter_blob() const        { assert(is_adapter_blob(), "must be adapter blob"); return (AdapterBlob*) this; }
214   ExceptionBlob* as_exception_blob() const    { assert(is_exception_stub(), "must be exception stub"); return (ExceptionBlob*) this; }
215   DeoptimizationBlob* as_deoptimization_blob() const { assert(is_deoptimization_stub(), "must be deopt stub"); return (DeoptimizationBlob*) this; }
216   SafepointBlob* as_safepoint_blob() const    { assert(is_safepoint_stub(), "must be safepoint stub"); return (SafepointBlob*) this; }
217   UpcallStub* as_upcall_stub() const          { assert(is_upcall_stub(), "must be upcall stub"); return (UpcallStub*) this; }
218   RuntimeStub* as_runtime_stub() const        { assert(is_runtime_stub(), "must be runtime blob"); return (RuntimeStub*) this; }
219   UncommonTrapBlob* as_uncommon_trap_blob() const { assert(is_uncommon_trap_stub(), "must be uncommon trap stub"); return (UncommonTrapBlob*) this; }
220 
221   // Boundaries
222   address    header_begin() const             { return (address)    this; }
223   address    header_end() const               { return ((address)   this) + _header_size; }
224   address    content_begin() const            { return (address)    header_begin() + _content_offset; }
225   address    content_end() const              { return (address)    header_begin() + _data_offset; }
226   address    code_begin() const               { return (address)    header_begin() + _code_offset; }
227   address    code_end() const                 { return (address)    header_begin() + _data_offset; }
228   address    data_begin() const               { return (address)    header_begin() + _data_offset; }
229   address    data_end() const                 { return (address)    header_begin() + _size; }
230   address    blob_end() const                 { return (address)    header_begin() + _size; }
231   // code_end == content_end is true for all types of blobs for now, it is also checked in the constructor
232 
233   int mutable_data_size() const               { return _mutable_data_size; }
234   address mutable_data_begin() const          { return _mutable_data; }
235   address mutable_data_end() const            { return _mutable_data + _mutable_data_size; }
236 
237   relocInfo* relocation_begin() const         { return (relocInfo*)_mutable_data; }
238   relocInfo* relocation_end() const           { return (relocInfo*)((address)relocation_begin() + _relocation_size); }
239 
240   // Offsets
241   int content_offset() const                  { return _content_offset; }
242   int code_offset() const                     { return _code_offset; }
243 
244   // This field holds the beginning of the const section in the old code buffer.
245   // It is needed to fix relocations of pc-relative loads when resizing the
246   // the constant pool or moving it.
247   S390_ONLY(address ctable_begin() const { return header_begin() + _ctable_offset; })
248   void set_ctable_begin(address ctable) { S390_ONLY(_ctable_offset = ctable - header_begin();) }
249 
250   // Sizes
251   int size() const               { return _size; }
252   int header_size() const        { return _header_size; }
253   int relocation_size() const    { return pointer_delta_as_int((address) relocation_end(), (address) relocation_begin()); }
254   int content_size() const       { return pointer_delta_as_int(content_end(), content_begin()); }
255   int code_size() const          { return pointer_delta_as_int(code_end(), code_begin()); }
256 
257   // Only used from CodeCache::free_unused_tail() after the Interpreter blob was trimmed
258   void adjust_size(size_t used) {
259     _size = (int)used;
260     _data_offset = _size;
261   }
262 
263   // Containment
264   bool blob_contains(address addr) const         { return header_begin()       <= addr && addr < blob_end();       }
265   bool code_contains(address addr) const         { return code_begin()         <= addr && addr < code_end();       }
266   bool contains(address addr) const              { return content_begin()      <= addr && addr < content_end();    }
267   bool is_frame_complete_at(address addr) const  { return _frame_complete_offset != CodeOffsets::frame_never_safe &&
268                                                           code_contains(addr) && addr >= code_begin() + _frame_complete_offset; }
269   int frame_complete_offset() const              { return _frame_complete_offset; }
270 
271   // OopMap for frame
272   ImmutableOopMapSet* oop_maps() const           { return _oop_maps; }
273   void set_oop_maps(OopMapSet* p);
274   void set_oop_maps(ImmutableOopMapSet* p)       { _oop_maps = p; }
275 
276   const ImmutableOopMap* oop_map_for_slot(int slot, address return_address) const;
277   const ImmutableOopMap* oop_map_for_return_address(address return_address) const;
278 
279   // Frame support. Sizes are in word units.
280   int  frame_size() const                        { return _frame_size; }
281   void set_frame_size(int size)                  { _frame_size = size; }
282 
283   // Returns true, if the next frame is responsible for GC'ing oops passed as arguments
284   bool caller_must_gc_arguments(JavaThread* thread) const { return _caller_must_gc_arguments; }
285 
286   // Naming
287   const char* name() const                       { return _name; }
288   void set_name(const char* name)                { _name = name; }
289 
290   // Debugging
291   void verify();
292   void print() const;
293   void print_on(outputStream* st) const;
294   void print_value_on(outputStream* st) const;
295 
296   void dump_for_addr(address addr, outputStream* st, bool verbose) const;
297   void print_code_on(outputStream* st);
298 
299   // Print to stream, any comments associated with offset.
300   void print_block_comment(outputStream* stream, address block_begin) const;
301 
302 #ifndef PRODUCT
303   AsmRemarks &asm_remarks() { return _asm_remarks; }
304   DbgStrings &dbg_strings() { return _dbg_strings; }
305 
306   void use_remarks(AsmRemarks &remarks) { _asm_remarks.share(remarks); }
307   void use_strings(DbgStrings &strings) { _dbg_strings.share(strings); }
308 #endif
309 
310   void copy_to(address buffer) {
311     memcpy(buffer, this, this->size());
312   }
313 
314   // methods to archive a blob into AOT code cache
315   void prepare_for_archiving();
316   static void archive_blob(CodeBlob* blob, address archive_buffer);
317 
318   // methods to restore a blob from AOT code cache into the CodeCache
319   void post_restore();
320   CodeBlob* restore(address code_cache_buffer, const char* name, address archived_reloc_data, ImmutableOopMapSet* archived_oop_maps);
321   static CodeBlob* create(CodeBlob* archived_blob,
322                           const char* name,
323                           address archived_reloc_data,
324                           ImmutableOopMapSet* archived_oop_maps
325 #ifndef PRODUCT
326                           , AsmRemarks& archived_asm_remarks
327                           , DbgStrings& archived_dbg_strings
328 #endif // PRODUCT
329                          );
330 };
331 
332 //----------------------------------------------------------------------------------------------------
333 // RuntimeBlob: used for non-compiled method code (adapters, stubs, blobs)
334 
335 class RuntimeBlob : public CodeBlob {
336   friend class VMStructs;
337  public:
338 
339   // Creation
340   // a) simple CodeBlob
341   RuntimeBlob(const char* name, CodeBlobKind kind, int size, uint16_t header_size)
342     : CodeBlob(name, kind, size, header_size)
343   {}
344 
345   // b) full CodeBlob
346   // frame_complete is the offset from the beginning of the instructions
347   // to where the frame setup (from stackwalk viewpoint) is complete.
348   RuntimeBlob(
349     const char* name,
350     CodeBlobKind kind,
351     CodeBuffer* cb,
352     int         size,
353     uint16_t    header_size,
354     int16_t     frame_complete,
355     int         frame_size,
356     OopMapSet*  oop_maps,
357     bool        caller_must_gc_arguments = false
358   );
359 
360   static void free(RuntimeBlob* blob);
361 
362   // Deal with Disassembler, VTune, Forte, JvmtiExport, MemoryService.
363   static void trace_new_stub(RuntimeBlob* blob, const char* name1, const char* name2 = "");
364 
365   class Vptr : public CodeBlob::Vptr {
366   };
367 };
368 
369 class WhiteBox;
370 //----------------------------------------------------------------------------------------------------
371 // BufferBlob: used to hold non-relocatable machine code such as the interpreter, stubroutines, etc.
372 
373 class BufferBlob: public RuntimeBlob {
374   friend class VMStructs;
375   friend class AdapterBlob;
376   friend class VtableBlob;
377   friend class MethodHandlesAdapterBlob;
378   friend class BufferedInlineTypeBlob;
379   friend class UpcallStub;
380   friend class WhiteBox;
381 
382  private:
383   // Creation support
384   BufferBlob(const char* name, CodeBlobKind kind, int size);
385   BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, int header_size);
386   BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments = false);
387 
388   void* operator new(size_t s, unsigned size) throw();
389 
390  public:
391   // Creation
392   static BufferBlob* create(const char* name, uint buffer_size);
393   static BufferBlob* create(const char* name, CodeBuffer* cb);
394 
395   static void free(BufferBlob* buf);
396 
397   void print_on_impl(outputStream* st) const;
398   void print_value_on_impl(outputStream* st) const;
399 
400   class Vptr : public RuntimeBlob::Vptr {
401     void print_on(const CodeBlob* instance, outputStream* st) const override {
402       ((const BufferBlob*)instance)->print_on_impl(st);
403     }
404     void print_value_on(const CodeBlob* instance, outputStream* st) const override {
405       ((const BufferBlob*)instance)->print_value_on_impl(st);
406     }
407   };
408 
409   static const Vptr _vpntr;
410 };
411 
412 
413 //----------------------------------------------------------------------------------------------------
414 // AdapterBlob: used to hold C2I/I2C adapters
415 
416 class AdapterBlob: public BufferBlob {
417 private:
418   AdapterBlob(int size, CodeBuffer* cb, int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments = false);
419 
420 public:
421   // Creation
422   static AdapterBlob* create(CodeBuffer* cb,
423                              int frame_complete,
424                              int frame_size,
425                              OopMapSet* oop_maps,
426                              bool caller_must_gc_arguments = false);
427 
428   bool caller_must_gc_arguments(JavaThread* thread) const { return true; }
429 };
430 
431 //---------------------------------------------------------------------------------------------------
432 class VtableBlob: public BufferBlob {
433 private:
434   VtableBlob(const char*, int);
435 
436   void* operator new(size_t s, unsigned size) throw();
437 
438 public:
439   // Creation
440   static VtableBlob* create(const char* name, int buffer_size);
441 };
442 
443 //----------------------------------------------------------------------------------------------------
444 // MethodHandlesAdapterBlob: used to hold MethodHandles adapters
445 
446 class MethodHandlesAdapterBlob: public BufferBlob {
447 private:
448   MethodHandlesAdapterBlob(int size): BufferBlob("MethodHandles adapters", CodeBlobKind::MHAdapter, size) {}
449 
450 public:
451   // Creation
452   static MethodHandlesAdapterBlob* create(int buffer_size);
453 };
454 
455 //----------------------------------------------------------------------------------------------------
456 // BufferedInlineTypeBlob : used for pack/unpack handlers
457 
458 class BufferedInlineTypeBlob: public BufferBlob {
459 private:
460   const int _pack_fields_off;
461   const int _pack_fields_jobject_off;
462   const int _unpack_fields_off;
463 
464   BufferedInlineTypeBlob(int size, CodeBuffer* cb, int pack_fields_off, int pack_fields_jobject_off, int unpack_fields_off);
465 
466 public:
467   // Creation
468   static BufferedInlineTypeBlob* create(CodeBuffer* cb, int pack_fields_off, int pack_fields_jobject_off, int unpack_fields_off);
469 
470   address pack_fields() const { return code_begin() + _pack_fields_off; }
471   address pack_fields_jobject() const { return code_begin() + _pack_fields_jobject_off; }
472   address unpack_fields() const { return code_begin() + _unpack_fields_off; }
473 };
474 
475 //----------------------------------------------------------------------------------------------------
476 // RuntimeStub: describes stubs used by compiled code to call a (static) C++ runtime routine
477 
478 class RuntimeStub: public RuntimeBlob {
479   friend class VMStructs;
480  private:
481   // Creation support
482   RuntimeStub(
483     const char* name,
484     CodeBuffer* cb,
485     int         size,
486     int16_t     frame_complete,
487     int         frame_size,
488     OopMapSet*  oop_maps,
489     bool        caller_must_gc_arguments
490   );
491 
492   void* operator new(size_t s, unsigned size) throw();
493 
494  public:
495   // Creation
496   static RuntimeStub* new_runtime_stub(
497     const char* stub_name,
498     CodeBuffer* cb,
499     int16_t     frame_complete,
500     int         frame_size,
501     OopMapSet*  oop_maps,
502     bool        caller_must_gc_arguments,
503     bool        alloc_fail_is_fatal=true
504   );
505 
506   static void free(RuntimeStub* stub) { RuntimeBlob::free(stub); }
507 
508   address entry_point() const         { return code_begin(); }
509 
510   void print_on_impl(outputStream* st) const;
511   void print_value_on_impl(outputStream* st) const;
512 
513   class Vptr : public RuntimeBlob::Vptr {
514     void print_on(const CodeBlob* instance, outputStream* st) const override {
515       instance->as_runtime_stub()->print_on_impl(st);
516     }
517     void print_value_on(const CodeBlob* instance, outputStream* st) const override {
518       instance->as_runtime_stub()->print_value_on_impl(st);
519     }
520   };
521 
522   static const Vptr _vpntr;
523 };
524 
525 
526 //----------------------------------------------------------------------------------------------------
527 // Super-class for all blobs that exist in only one instance. Implements default behaviour.
528 
529 class SingletonBlob: public RuntimeBlob {
530   friend class VMStructs;
531 
532  protected:
533   void* operator new(size_t s, unsigned size, bool alloc_fail_is_fatal=true) throw();
534 
535  public:
536    SingletonBlob(
537      const char*  name,
538      CodeBlobKind kind,
539      CodeBuffer*  cb,
540      int          size,
541      uint16_t     header_size,
542      int          frame_size,
543      OopMapSet*   oop_maps
544    )
545    : RuntimeBlob(name, kind, cb, size, header_size, CodeOffsets::frame_never_safe, frame_size, oop_maps)
546   {};
547 
548   address entry_point()                          { return code_begin(); }
549 
550   void print_on_impl(outputStream* st) const;
551   void print_value_on_impl(outputStream* st) const;
552 
553   class Vptr : public RuntimeBlob::Vptr {
554     void print_on(const CodeBlob* instance, outputStream* st) const override {
555       ((const SingletonBlob*)instance)->print_on_impl(st);
556     }
557     void print_value_on(const CodeBlob* instance, outputStream* st) const override {
558       ((const SingletonBlob*)instance)->print_value_on_impl(st);
559     }
560   };
561 
562   static const Vptr _vpntr;
563 };
564 
565 
566 //----------------------------------------------------------------------------------------------------
567 // DeoptimizationBlob
568 
569 class DeoptimizationBlob: public SingletonBlob {
570   friend class VMStructs;
571   friend class JVMCIVMStructs;
572  private:
573   int _unpack_offset;
574   int _unpack_with_exception;
575   int _unpack_with_reexecution;
576 
577   int _unpack_with_exception_in_tls;
578 
579 #if INCLUDE_JVMCI
580   // Offsets when JVMCI calls uncommon_trap.
581   int _uncommon_trap_offset;
582   int _implicit_exception_uncommon_trap_offset;
583 #endif
584 
585   // Creation support
586   DeoptimizationBlob(
587     CodeBuffer* cb,
588     int         size,
589     OopMapSet*  oop_maps,
590     int         unpack_offset,
591     int         unpack_with_exception_offset,
592     int         unpack_with_reexecution_offset,
593     int         frame_size
594   );
595 
596  public:
597   // Creation
598   static DeoptimizationBlob* create(
599     CodeBuffer* cb,
600     OopMapSet*  oop_maps,
601     int         unpack_offset,
602     int         unpack_with_exception_offset,
603     int         unpack_with_reexecution_offset,
604     int         frame_size
605   );
606 
607   address unpack() const                         { return code_begin() + _unpack_offset;           }
608   address unpack_with_exception() const          { return code_begin() + _unpack_with_exception;   }
609   address unpack_with_reexecution() const        { return code_begin() + _unpack_with_reexecution; }
610 
611   // Alternate entry point for C1 where the exception and issuing pc
612   // are in JavaThread::_exception_oop and JavaThread::_exception_pc
613   // instead of being in registers.  This is needed because C1 doesn't
614   // model exception paths in a way that keeps these registers free so
615   // there may be live values in those registers during deopt.
616   void set_unpack_with_exception_in_tls_offset(int offset) {
617     _unpack_with_exception_in_tls = offset;
618     assert(code_contains(code_begin() + _unpack_with_exception_in_tls), "must be PC inside codeblob");
619   }
620   address unpack_with_exception_in_tls() const   { return code_begin() + _unpack_with_exception_in_tls; }
621 
622 #if INCLUDE_JVMCI
623   // Offsets when JVMCI calls uncommon_trap.
624   void set_uncommon_trap_offset(int offset) {
625     _uncommon_trap_offset = offset;
626     assert(contains(code_begin() + _uncommon_trap_offset), "must be PC inside codeblob");
627   }
628   address uncommon_trap() const                  { return code_begin() + _uncommon_trap_offset; }
629 
630   void set_implicit_exception_uncommon_trap_offset(int offset) {
631     _implicit_exception_uncommon_trap_offset = offset;
632     assert(contains(code_begin() + _implicit_exception_uncommon_trap_offset), "must be PC inside codeblob");
633   }
634   address implicit_exception_uncommon_trap() const { return code_begin() + _implicit_exception_uncommon_trap_offset; }
635 #endif // INCLUDE_JVMCI
636 
637   void print_value_on_impl(outputStream* st) const;
638 
639   class Vptr : public SingletonBlob::Vptr {
640     void print_value_on(const CodeBlob* instance, outputStream* st) const override {
641       ((const DeoptimizationBlob*)instance)->print_value_on_impl(st);
642     }
643   };
644 
645   static const Vptr _vpntr;
646 };
647 
648 
649 //----------------------------------------------------------------------------------------------------
650 // UncommonTrapBlob (currently only used by Compiler 2)
651 
652 #ifdef COMPILER2
653 
654 class UncommonTrapBlob: public SingletonBlob {
655   friend class VMStructs;
656  private:
657   // Creation support
658   UncommonTrapBlob(
659     CodeBuffer* cb,
660     int         size,
661     OopMapSet*  oop_maps,
662     int         frame_size
663   );
664 
665  public:
666   // Creation
667   static UncommonTrapBlob* create(
668     CodeBuffer* cb,
669     OopMapSet*  oop_maps,
670     int         frame_size
671   );
672 };
673 
674 
675 //----------------------------------------------------------------------------------------------------
676 // ExceptionBlob: used for exception unwinding in compiled code (currently only used by Compiler 2)
677 
678 class ExceptionBlob: public SingletonBlob {
679   friend class VMStructs;
680  private:
681   // Creation support
682   ExceptionBlob(
683     CodeBuffer* cb,
684     int         size,
685     OopMapSet*  oop_maps,
686     int         frame_size
687   );
688 
689  public:
690   // Creation
691   static ExceptionBlob* create(
692     CodeBuffer* cb,
693     OopMapSet*  oop_maps,
694     int         frame_size
695   );
696 
697   void post_restore_impl() {
698     trace_new_stub(this, "ExceptionBlob");
699   }
700 
701   class Vptr : public SingletonBlob::Vptr {
702     void post_restore(CodeBlob* instance) const override {
703       ((ExceptionBlob*)instance)->post_restore_impl();
704     }
705   };
706 
707   static const Vptr _vpntr;
708 };
709 #endif // COMPILER2
710 
711 
712 //----------------------------------------------------------------------------------------------------
713 // SafepointBlob: handles illegal_instruction exceptions during a safepoint
714 
715 class SafepointBlob: public SingletonBlob {
716   friend class VMStructs;
717  private:
718   // Creation support
719   SafepointBlob(
720     CodeBuffer* cb,
721     int         size,
722     OopMapSet*  oop_maps,
723     int         frame_size
724   );
725 
726  public:
727   // Creation
728   static SafepointBlob* create(
729     CodeBuffer* cb,
730     OopMapSet*  oop_maps,
731     int         frame_size
732   );
733 };
734 
735 //----------------------------------------------------------------------------------------------------
736 
737 class UpcallLinker;
738 
739 // A (Panama) upcall stub. Not used by JNI.
740 class UpcallStub: public RuntimeBlob {
741   friend class VMStructs;
742   friend class UpcallLinker;
743  private:
744   jobject _receiver;
745   ByteSize _frame_data_offset;
746 
747   UpcallStub(const char* name, CodeBuffer* cb, int size, jobject receiver, ByteSize frame_data_offset);
748 
749   void* operator new(size_t s, unsigned size) throw();
750 
751   struct FrameData {
752     JavaFrameAnchor jfa;
753     JavaThread* thread;
754     JNIHandleBlock* old_handles;
755     JNIHandleBlock* new_handles;
756   };
757 
758   // defined in frame_ARCH.cpp
759   FrameData* frame_data_for_frame(const frame& frame) const;
760  public:
761   // Creation
762   static UpcallStub* create(const char* name, CodeBuffer* cb, jobject receiver, ByteSize frame_data_offset);
763 
764   static void free(UpcallStub* blob);
765 
766   jobject receiver() { return _receiver; }
767 
768   JavaFrameAnchor* jfa_for_frame(const frame& frame) const;
769 
770   // GC support
771   void oops_do(OopClosure* f, const frame& frame);
772 
773   void print_on_impl(outputStream* st) const;
774   void print_value_on_impl(outputStream* st) const;
775 
776   class Vptr : public RuntimeBlob::Vptr {
777     void print_on(const CodeBlob* instance, outputStream* st) const override {
778       instance->as_upcall_stub()->print_on_impl(st);
779     }
780     void print_value_on(const CodeBlob* instance, outputStream* st) const override {
781       instance->as_upcall_stub()->print_value_on_impl(st);
782     }
783   };
784 
785   static const Vptr _vpntr;
786 };
787 
788 #endif // SHARE_CODE_CODEBLOB_HPP