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