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/frame.hpp"
 32 #include "runtime/handles.hpp"
 33 #include "runtime/javaFrameAnchor.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 _relocation_size; }
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 };
326 
327 //----------------------------------------------------------------------------------------------------
328 // RuntimeBlob: used for non-compiled method code (adapters, stubs, blobs)
329 
330 class RuntimeBlob : public CodeBlob {
331   friend class VMStructs;
332  public:
333 
334   // Creation
335   // a) simple CodeBlob
336   RuntimeBlob(const char* name, CodeBlobKind kind, int size, uint16_t header_size)
337     : CodeBlob(name, kind, size, header_size)
338   {}
339 
340   // b) full CodeBlob
341   // frame_complete is the offset from the beginning of the instructions
342   // to where the frame setup (from stackwalk viewpoint) is complete.
343   RuntimeBlob(
344     const char* name,
345     CodeBlobKind kind,
346     CodeBuffer* cb,
347     int         size,
348     uint16_t    header_size,
349     int16_t     frame_complete,
350     int         frame_size,
351     OopMapSet*  oop_maps,
352     bool        caller_must_gc_arguments = false
353   );
354 
355   static void free(RuntimeBlob* blob);
356 
357   // Deal with Disassembler, VTune, Forte, JvmtiExport, MemoryService.
358   static void trace_new_stub(RuntimeBlob* blob, const char* name1, const char* name2 = "");
359 
360   class Vptr : public CodeBlob::Vptr {
361   };
362 };
363 
364 class WhiteBox;
365 //----------------------------------------------------------------------------------------------------
366 // BufferBlob: used to hold non-relocatable machine code such as the interpreter, stubroutines, etc.
367 
368 class BufferBlob: public RuntimeBlob {
369   friend class VMStructs;
370   friend class AdapterBlob;
371   friend class VtableBlob;
372   friend class MethodHandlesAdapterBlob;
373   friend class BufferedInlineTypeBlob;
374   friend class UpcallStub;
375   friend class WhiteBox;
376 
377  private:
378   // Creation support
379   BufferBlob(const char* name, CodeBlobKind kind, int size, uint16_t header_size = sizeof(BufferBlob));
380   BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size = sizeof(BufferBlob));
381   BufferBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size, uint16_t header_size, int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments = false);
382 
383   void* operator new(size_t s, unsigned size) throw();
384 
385  public:
386   // Creation
387   static BufferBlob* create(const char* name, uint buffer_size);
388   static BufferBlob* create(const char* name, CodeBuffer* cb);
389 
390   static void free(BufferBlob* buf);
391 
392   void print_on_impl(outputStream* st) const;
393   void print_value_on_impl(outputStream* st) const;
394 
395   class Vptr : public RuntimeBlob::Vptr {
396     void print_on(const CodeBlob* instance, outputStream* st) const override {
397       ((const BufferBlob*)instance)->print_on_impl(st);
398     }
399     void print_value_on(const CodeBlob* instance, outputStream* st) const override {
400       ((const BufferBlob*)instance)->print_value_on_impl(st);
401     }
402   };
403 
404   static const Vptr _vpntr;
405 };
406 
407 
408 //----------------------------------------------------------------------------------------------------
409 // AdapterBlob: used to hold C2I/I2C adapters
410 
411 class AdapterBlob: public BufferBlob {
412 public:
413   static const int ENTRY_COUNT = 7;
414 private:
415   AdapterBlob(int size, CodeBuffer* cb, int entry_offset[ENTRY_COUNT], int frame_complete, int frame_size, OopMapSet* oop_maps, bool caller_must_gc_arguments = false);
416 
417   // _i2c_offset is always 0 so no need to store it
418   int _c2i_offset;
419   int _c2i_inline_offset;
420   int _c2i_inline_ro_offset;
421   int _c2i_unverified_offset;
422   int _c2i_unverified_inline_offset;
423   int _c2i_no_clinit_check_offset;
424 public:
425   // Creation
426   static AdapterBlob* create(CodeBuffer* cb,
427                              int entry_offset[ENTRY_COUNT],
428                              int frame_complete,
429                              int frame_size,
430                              OopMapSet* oop_maps,
431                              bool caller_must_gc_arguments = false);
432 
433   bool caller_must_gc_arguments(JavaThread* thread) const { return true; }
434   static AdapterBlob* create(CodeBuffer* cb, int entry_offset[ENTRY_COUNT]);
435   void get_offsets(int entry_offset[ENTRY_COUNT]);
436 };
437 
438 //---------------------------------------------------------------------------------------------------
439 class VtableBlob: public BufferBlob {
440 private:
441   VtableBlob(const char*, int);
442 
443   void* operator new(size_t s, unsigned size) throw();
444 
445 public:
446   // Creation
447   static VtableBlob* create(const char* name, int buffer_size);
448 };
449 
450 //----------------------------------------------------------------------------------------------------
451 // MethodHandlesAdapterBlob: used to hold MethodHandles adapters
452 
453 class MethodHandlesAdapterBlob: public BufferBlob {
454 private:
455   MethodHandlesAdapterBlob(int size): BufferBlob("MethodHandles adapters", CodeBlobKind::MHAdapter, size) {}
456 
457 public:
458   // Creation
459   static MethodHandlesAdapterBlob* create(int buffer_size);
460 };
461 
462 //----------------------------------------------------------------------------------------------------
463 // BufferedInlineTypeBlob : used for pack/unpack handlers
464 
465 class BufferedInlineTypeBlob: public BufferBlob {
466 private:
467   const int _pack_fields_off;
468   const int _pack_fields_jobject_off;
469   const int _unpack_fields_off;
470 
471   BufferedInlineTypeBlob(int size, CodeBuffer* cb, int pack_fields_off, int pack_fields_jobject_off, int unpack_fields_off);
472 
473 public:
474   // Creation
475   static BufferedInlineTypeBlob* create(CodeBuffer* cb, int pack_fields_off, int pack_fields_jobject_off, int unpack_fields_off);
476 
477   address pack_fields() const { return code_begin() + _pack_fields_off; }
478   address pack_fields_jobject() const { return code_begin() + _pack_fields_jobject_off; }
479   address unpack_fields() const { return code_begin() + _unpack_fields_off; }
480 };
481 
482 //----------------------------------------------------------------------------------------------------
483 // RuntimeStub: describes stubs used by compiled code to call a (static) C++ runtime routine
484 
485 class RuntimeStub: public RuntimeBlob {
486   friend class VMStructs;
487  private:
488   // Creation support
489   RuntimeStub(
490     const char* name,
491     CodeBuffer* cb,
492     int         size,
493     int16_t     frame_complete,
494     int         frame_size,
495     OopMapSet*  oop_maps,
496     bool        caller_must_gc_arguments
497   );
498 
499   void* operator new(size_t s, unsigned size) throw();
500 
501  public:
502   static const int ENTRY_COUNT = 1;
503   // Creation
504   static RuntimeStub* new_runtime_stub(
505     const char* stub_name,
506     CodeBuffer* cb,
507     int16_t     frame_complete,
508     int         frame_size,
509     OopMapSet*  oop_maps,
510     bool        caller_must_gc_arguments,
511     bool        alloc_fail_is_fatal=true
512   );
513 
514   static void free(RuntimeStub* stub) { RuntimeBlob::free(stub); }
515 
516   address entry_point() const         { return code_begin(); }
517 
518   void print_on_impl(outputStream* st) const;
519   void print_value_on_impl(outputStream* st) const;
520 
521   class Vptr : public RuntimeBlob::Vptr {
522     void print_on(const CodeBlob* instance, outputStream* st) const override {
523       instance->as_runtime_stub()->print_on_impl(st);
524     }
525     void print_value_on(const CodeBlob* instance, outputStream* st) const override {
526       instance->as_runtime_stub()->print_value_on_impl(st);
527     }
528   };
529 
530   static const Vptr _vpntr;
531 };
532 
533 
534 //----------------------------------------------------------------------------------------------------
535 // Super-class for all blobs that exist in only one instance. Implements default behaviour.
536 
537 class SingletonBlob: public RuntimeBlob {
538   friend class VMStructs;
539 
540  protected:
541   void* operator new(size_t s, unsigned size, bool alloc_fail_is_fatal=true) throw();
542 
543  public:
544    SingletonBlob(
545      const char*  name,
546      CodeBlobKind kind,
547      CodeBuffer*  cb,
548      int          size,
549      uint16_t     header_size,
550      int          frame_size,
551      OopMapSet*   oop_maps
552    )
553    : RuntimeBlob(name, kind, cb, size, header_size, CodeOffsets::frame_never_safe, frame_size, oop_maps)
554   {};
555 
556   address entry_point()                          { return code_begin(); }
557 
558   void print_on_impl(outputStream* st) const;
559   void print_value_on_impl(outputStream* st) const;
560 
561   class Vptr : public RuntimeBlob::Vptr {
562     void print_on(const CodeBlob* instance, outputStream* st) const override {
563       ((const SingletonBlob*)instance)->print_on_impl(st);
564     }
565     void print_value_on(const CodeBlob* instance, outputStream* st) const override {
566       ((const SingletonBlob*)instance)->print_value_on_impl(st);
567     }
568   };
569 
570   static const Vptr _vpntr;
571 };
572 
573 
574 //----------------------------------------------------------------------------------------------------
575 // DeoptimizationBlob
576 
577 class DeoptimizationBlob: public SingletonBlob {
578   friend class VMStructs;
579   friend class JVMCIVMStructs;
580  private:
581   int _unpack_offset;
582   int _unpack_with_exception;
583   int _unpack_with_reexecution;
584 
585   int _unpack_with_exception_in_tls;
586 
587 #if INCLUDE_JVMCI
588   // Offsets when JVMCI calls uncommon_trap.
589   int _uncommon_trap_offset;
590   int _implicit_exception_uncommon_trap_offset;
591 #endif
592 
593   // Creation support
594   DeoptimizationBlob(
595     CodeBuffer* cb,
596     int         size,
597     OopMapSet*  oop_maps,
598     int         unpack_offset,
599     int         unpack_with_exception_offset,
600     int         unpack_with_reexecution_offset,
601     int         frame_size
602   );
603 
604  public:
605   static const int ENTRY_COUNT = 4 JVMTI_ONLY(+ 2);
606   // Creation
607   static DeoptimizationBlob* create(
608     CodeBuffer* cb,
609     OopMapSet*  oop_maps,
610     int         unpack_offset,
611     int         unpack_with_exception_offset,
612     int         unpack_with_reexecution_offset,
613     int         frame_size
614   );
615 
616   address unpack() const                         { return code_begin() + _unpack_offset;           }
617   address unpack_with_exception() const          { return code_begin() + _unpack_with_exception;   }
618   address unpack_with_reexecution() const        { return code_begin() + _unpack_with_reexecution; }
619 
620   // Alternate entry point for C1 where the exception and issuing pc
621   // are in JavaThread::_exception_oop and JavaThread::_exception_pc
622   // instead of being in registers.  This is needed because C1 doesn't
623   // model exception paths in a way that keeps these registers free so
624   // there may be live values in those registers during deopt.
625   void set_unpack_with_exception_in_tls_offset(int offset) {
626     _unpack_with_exception_in_tls = offset;
627     assert(code_contains(code_begin() + _unpack_with_exception_in_tls), "must be PC inside codeblob");
628   }
629   address unpack_with_exception_in_tls() const   { return code_begin() + _unpack_with_exception_in_tls; }
630 
631 #if INCLUDE_JVMCI
632   // Offsets when JVMCI calls uncommon_trap.
633   void set_uncommon_trap_offset(int offset) {
634     _uncommon_trap_offset = offset;
635     assert(contains(code_begin() + _uncommon_trap_offset), "must be PC inside codeblob");
636   }
637   address uncommon_trap() const                  { return code_begin() + _uncommon_trap_offset; }
638 
639   void set_implicit_exception_uncommon_trap_offset(int offset) {
640     _implicit_exception_uncommon_trap_offset = offset;
641     assert(contains(code_begin() + _implicit_exception_uncommon_trap_offset), "must be PC inside codeblob");
642   }
643   address implicit_exception_uncommon_trap() const { return code_begin() + _implicit_exception_uncommon_trap_offset; }
644 #endif // INCLUDE_JVMCI
645 
646   void print_value_on_impl(outputStream* st) const;
647 
648   class Vptr : public SingletonBlob::Vptr {
649     void print_value_on(const CodeBlob* instance, outputStream* st) const override {
650       ((const DeoptimizationBlob*)instance)->print_value_on_impl(st);
651     }
652   };
653 
654   static const Vptr _vpntr;
655 };
656 
657 
658 //----------------------------------------------------------------------------------------------------
659 // UncommonTrapBlob (currently only used by Compiler 2)
660 
661 #ifdef COMPILER2
662 
663 class UncommonTrapBlob: public SingletonBlob {
664   friend class VMStructs;
665  private:
666   // Creation support
667   UncommonTrapBlob(
668     CodeBuffer* cb,
669     int         size,
670     OopMapSet*  oop_maps,
671     int         frame_size
672   );
673 
674  public:
675   // Creation
676   static UncommonTrapBlob* create(
677     CodeBuffer* cb,
678     OopMapSet*  oop_maps,
679     int         frame_size
680   );
681 };
682 
683 
684 //----------------------------------------------------------------------------------------------------
685 // ExceptionBlob: used for exception unwinding in compiled code (currently only used by Compiler 2)
686 
687 class ExceptionBlob: public SingletonBlob {
688   friend class VMStructs;
689  private:
690   // Creation support
691   ExceptionBlob(
692     CodeBuffer* cb,
693     int         size,
694     OopMapSet*  oop_maps,
695     int         frame_size
696   );
697 
698  public:
699   // Creation
700   static ExceptionBlob* create(
701     CodeBuffer* cb,
702     OopMapSet*  oop_maps,
703     int         frame_size
704   );
705 
706   void post_restore_impl() {
707     trace_new_stub(this, "ExceptionBlob");
708   }
709 
710   class Vptr : public SingletonBlob::Vptr {
711     void post_restore(CodeBlob* instance) const override {
712       ((ExceptionBlob*)instance)->post_restore_impl();
713     }
714   };
715 
716   static const Vptr _vpntr;
717 };
718 #endif // COMPILER2
719 
720 
721 //----------------------------------------------------------------------------------------------------
722 // SafepointBlob: handles illegal_instruction exceptions during a safepoint
723 
724 class SafepointBlob: public SingletonBlob {
725   friend class VMStructs;
726  private:
727   // Creation support
728   SafepointBlob(
729     CodeBuffer* cb,
730     int         size,
731     OopMapSet*  oop_maps,
732     int         frame_size
733   );
734 
735  public:
736   static const int ENTRY_COUNT = 1;
737   // Creation
738   static SafepointBlob* create(
739     CodeBuffer* cb,
740     OopMapSet*  oop_maps,
741     int         frame_size
742   );
743 };
744 
745 //----------------------------------------------------------------------------------------------------
746 
747 class UpcallLinker;
748 
749 // A (Panama) upcall stub. Not used by JNI.
750 class UpcallStub: public RuntimeBlob {
751   friend class VMStructs;
752   friend class UpcallLinker;
753  private:
754   jobject _receiver;
755   ByteSize _frame_data_offset;
756 
757   UpcallStub(const char* name, CodeBuffer* cb, int size, jobject receiver, ByteSize frame_data_offset);
758 
759   void* operator new(size_t s, unsigned size) throw();
760 
761   struct FrameData {
762     JavaFrameAnchor jfa;
763     JavaThread* thread;
764     JNIHandleBlock* old_handles;
765     JNIHandleBlock* new_handles;
766   };
767 
768   // defined in frame_ARCH.cpp
769   FrameData* frame_data_for_frame(const frame& frame) const;
770  public:
771   // Creation
772   static UpcallStub* create(const char* name, CodeBuffer* cb, jobject receiver, ByteSize frame_data_offset);
773 
774   static void free(UpcallStub* blob);
775 
776   jobject receiver() { return _receiver; }
777 
778   JavaFrameAnchor* jfa_for_frame(const frame& frame) const;
779 
780   // GC support
781   void oops_do(OopClosure* f, const frame& frame);
782 
783   void print_on_impl(outputStream* st) const;
784   void print_value_on_impl(outputStream* st) const;
785 
786   class Vptr : public RuntimeBlob::Vptr {
787     void print_on(const CodeBlob* instance, outputStream* st) const override {
788       instance->as_upcall_stub()->print_on_impl(st);
789     }
790     void print_value_on(const CodeBlob* instance, outputStream* st) const override {
791       instance->as_upcall_stub()->print_value_on_impl(st);
792     }
793   };
794 
795   static const Vptr _vpntr;
796 };
797 
798 #endif // SHARE_CODE_CODEBLOB_HPP