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_OPTO_RUNTIME_HPP
 26 #define SHARE_OPTO_RUNTIME_HPP
 27 
 28 #include "code/codeBlob.hpp"
 29 #include "opto/machnode.hpp"
 30 #include "opto/optoreg.hpp"
 31 #include "opto/type.hpp"
 32 #include "runtime/rtmLocking.hpp"
 33 #include "runtime/deoptimization.hpp"
 34 #include "runtime/vframe.hpp"
 35 
 36 //------------------------------OptoRuntime------------------------------------
 37 // Opto compiler runtime routines
 38 //
 39 // These are all generated from Ideal graphs.  They are called with the
 40 // Java calling convention.  Internally they call C++.  They are made once at
 41 // startup time and Opto compiles calls to them later.
 42 // Things are broken up into quads: the signature they will be called with,
 43 // the address of the generated code, the corresponding C++ code and an
 44 // nmethod.
 45 
 46 // The signature (returned by "xxx_Type()") is used at startup time by the
 47 // Generator to make the generated code "xxx_Java".  Opto compiles calls
 48 // to the generated code "xxx_Java".  When the compiled code gets executed,
 49 // it calls the C++ code "xxx_C".  The generated nmethod is saved in the
 50 // CodeCache.  Exception handlers use the nmethod to get the callee-save
 51 // register OopMaps.
 52 class CallInfo;
 53 
 54 //
 55 // NamedCounters are tagged counters which can be used for profiling
 56 // code in various ways.  Currently they are used by the lock coarsening code
 57 //
 58 
 59 class NamedCounter : public CHeapObj<mtCompiler> {
 60 public:
 61     enum CounterTag {
 62     NoTag,
 63     LockCounter,
 64     EliminatedLockCounter,
 65     RTMLockingCounter
 66   };
 67 
 68 private:
 69   const char *  _name;
 70   int           _count;
 71   CounterTag    _tag;
 72   NamedCounter* _next;
 73 
 74  public:
 75   NamedCounter(const char *n, CounterTag tag = NoTag):
 76     _name(n == nullptr ? nullptr : os::strdup(n)),
 77     _count(0),
 78     _tag(tag),
 79     _next(nullptr) {}
 80 
 81   ~NamedCounter() {
 82     if (_name != nullptr) {
 83       os::free((void*)_name);
 84     }
 85   }
 86 
 87   const char * name() const     { return _name; }
 88   int count() const             { return _count; }
 89   address addr()                { return (address)&_count; }
 90   CounterTag tag() const        { return _tag; }
 91   void set_tag(CounterTag tag)  { _tag = tag; }
 92 
 93   NamedCounter* next() const    { return _next; }
 94   void set_next(NamedCounter* next) {
 95     assert(_next == nullptr || next == nullptr, "already set");
 96     _next = next;
 97   }
 98 
 99 };
100 
101 class RTMLockingNamedCounter : public NamedCounter {
102  private:
103  RTMLockingCounters _counters;
104 
105  public:
106   RTMLockingNamedCounter(const char *n) :
107     NamedCounter(n, RTMLockingCounter), _counters() {}
108 
109   RTMLockingCounters* counters() { return &_counters; }
110 };
111 
112 typedef const TypeFunc*(*TypeFunc_generator)();
113 
114 class OptoRuntime : public AllStatic {
115   friend class Matcher;  // allow access to stub names
116 
117  private:
118   // define stubs
119   static address generate_stub(ciEnv* ci_env, TypeFunc_generator gen, address C_function, const char* name, int is_fancy_jump, bool pass_tls, bool return_pc);
120 
121   // References to generated stubs
122   static address _new_instance_Java;
123   static address _new_array_Java;
124   static address _new_array_nozero_Java;
125   static address _multianewarray2_Java;
126   static address _multianewarray3_Java;
127   static address _multianewarray4_Java;
128   static address _multianewarray5_Java;
129   static address _multianewarrayN_Java;
130   static address _vtable_must_compile_Java;
131   static address _complete_monitor_locking_Java;
132   static address _rethrow_Java;
133   static address _monitor_notify_Java;
134   static address _monitor_notifyAll_Java;
135 
136   static address _slow_arraycopy_Java;
137   static address _register_finalizer_Java;
138   static address _load_unknown_inline;
139 #if INCLUDE_JVMTI
140   static address _notify_jvmti_vthread_start;
141   static address _notify_jvmti_vthread_end;
142   static address _notify_jvmti_vthread_mount;
143   static address _notify_jvmti_vthread_unmount;
144 #endif
145 
146   //
147   // Implementation of runtime methods
148   // =================================
149 
150   // Allocate storage for a Java instance.
151   static void new_instance_C(Klass* instance_klass, bool is_larval, JavaThread* current);
152 
153   // Allocate storage for a objArray or typeArray
154   static void new_array_C(Klass* array_klass, int len, JavaThread* current);
155   static void new_array_nozero_C(Klass* array_klass, int len, JavaThread* current);
156 
157   // Allocate storage for a multi-dimensional arrays
158   // Note: needs to be fixed for arbitrary number of dimensions
159   static void multianewarray2_C(Klass* klass, int len1, int len2, JavaThread* current);
160   static void multianewarray3_C(Klass* klass, int len1, int len2, int len3, JavaThread* current);
161   static void multianewarray4_C(Klass* klass, int len1, int len2, int len3, int len4, JavaThread* current);
162   static void multianewarray5_C(Klass* klass, int len1, int len2, int len3, int len4, int len5, JavaThread* current);
163   static void multianewarrayN_C(Klass* klass, arrayOopDesc* dims, JavaThread* current);
164 
165 public:
166   static void monitor_notify_C(oopDesc* obj, JavaThread* current);
167   static void monitor_notifyAll_C(oopDesc* obj, JavaThread* current);
168 
169 private:
170 
171   // Implicit exception support
172   static void throw_null_exception_C(JavaThread* thread);
173 
174   // Exception handling
175   static address handle_exception_C       (JavaThread* current);
176   static address handle_exception_C_helper(JavaThread* current, nmethod*& nm);
177   static address rethrow_C                (oopDesc* exception, JavaThread *thread, address return_pc );
178   static void deoptimize_caller_frame     (JavaThread *thread);
179   static void deoptimize_caller_frame     (JavaThread *thread, bool doit);
180   static bool is_deoptimized_caller_frame (JavaThread *thread);
181 
182   // CodeBlob support
183   // ===================================================================
184 
185   static ExceptionBlob*       _exception_blob;
186   static void generate_exception_blob();
187 
188   static void register_finalizer(oopDesc* obj, JavaThread* current);
189 
190  public:
191 
192   static bool is_callee_saved_register(MachRegisterNumbers reg);
193 
194   // One time only generate runtime code stubs. Returns true
195   // when runtime stubs have been generated successfully and
196   // false otherwise.
197   static bool generate(ciEnv* env);
198 
199   // Returns the name of a stub
200   static const char* stub_name(address entry);
201 
202   // access to runtime stubs entry points for java code
203   static address new_instance_Java()                     { return _new_instance_Java; }
204   static address new_array_Java()                        { return _new_array_Java; }
205   static address new_array_nozero_Java()                 { return _new_array_nozero_Java; }
206   static address multianewarray2_Java()                  { return _multianewarray2_Java; }
207   static address multianewarray3_Java()                  { return _multianewarray3_Java; }
208   static address multianewarray4_Java()                  { return _multianewarray4_Java; }
209   static address multianewarray5_Java()                  { return _multianewarray5_Java; }
210   static address multianewarrayN_Java()                  { return _multianewarrayN_Java; }
211   static address vtable_must_compile_stub()              { return _vtable_must_compile_Java; }
212   static address complete_monitor_locking_Java()         { return _complete_monitor_locking_Java; }
213   static address monitor_notify_Java()                   { return _monitor_notify_Java; }
214   static address monitor_notifyAll_Java()                { return _monitor_notifyAll_Java; }
215 
216   static address slow_arraycopy_Java()                   { return _slow_arraycopy_Java; }
217   static address register_finalizer_Java()               { return _register_finalizer_Java; }
218   static address load_unknown_inline_Java()              { return _load_unknown_inline; }
219 #if INCLUDE_JVMTI
220   static address notify_jvmti_vthread_start()            { return _notify_jvmti_vthread_start; }
221   static address notify_jvmti_vthread_end()              { return _notify_jvmti_vthread_end; }
222   static address notify_jvmti_vthread_mount()            { return _notify_jvmti_vthread_mount; }
223   static address notify_jvmti_vthread_unmount()          { return _notify_jvmti_vthread_unmount; }
224 #endif
225 
226   static ExceptionBlob*    exception_blob()                      { return _exception_blob; }
227 
228   // Implicit exception support
229   static void throw_div0_exception_C      (JavaThread* thread);
230   static void throw_stack_overflow_error_C(JavaThread* thread);
231 
232   // Exception handling
233   static address rethrow_stub()             { return _rethrow_Java; }
234 
235 
236   // Type functions
237   // ======================================================
238 
239   static const TypeFunc* new_instance_Type(); // object allocation (slow case)
240   static const TypeFunc* new_array_Type ();   // [a]newarray (slow case)
241   static const TypeFunc* multianewarray_Type(int ndim); // multianewarray
242   static const TypeFunc* multianewarray2_Type(); // multianewarray
243   static const TypeFunc* multianewarray3_Type(); // multianewarray
244   static const TypeFunc* multianewarray4_Type(); // multianewarray
245   static const TypeFunc* multianewarray5_Type(); // multianewarray
246   static const TypeFunc* multianewarrayN_Type(); // multianewarray
247   static const TypeFunc* complete_monitor_enter_Type();
248   static const TypeFunc* complete_monitor_exit_Type();
249   static const TypeFunc* monitor_notify_Type();
250   static const TypeFunc* uncommon_trap_Type();
251   static const TypeFunc* athrow_Type();
252   static const TypeFunc* rethrow_Type();
253   static const TypeFunc* Math_D_D_Type();  // sin,cos & friends
254   static const TypeFunc* Math_DD_D_Type(); // mod,pow & friends
255   static const TypeFunc* Math_Vector_Vector_Type(uint num_arg, const TypeVect* in_type, const TypeVect* out_type);
256   static const TypeFunc* modf_Type();
257   static const TypeFunc* l2f_Type();
258   static const TypeFunc* void_long_Type();
259   static const TypeFunc* void_void_Type();
260 
261   static const TypeFunc* jfr_write_checkpoint_Type();
262 
263   static const TypeFunc* flush_windows_Type();
264 
265   // arraycopy routine types
266   static const TypeFunc* fast_arraycopy_Type(); // bit-blasters
267   static const TypeFunc* checkcast_arraycopy_Type();
268   static const TypeFunc* generic_arraycopy_Type();
269   static const TypeFunc* slow_arraycopy_Type();   // the full routine
270 
271   static const TypeFunc* array_fill_Type();
272 
273   static const TypeFunc* array_sort_Type();
274   static const TypeFunc* array_partition_Type();
275   static const TypeFunc* aescrypt_block_Type();
276   static const TypeFunc* cipherBlockChaining_aescrypt_Type();
277   static const TypeFunc* electronicCodeBook_aescrypt_Type();
278   static const TypeFunc* counterMode_aescrypt_Type();
279   static const TypeFunc* galoisCounterMode_aescrypt_Type();
280 
281   static const TypeFunc* digestBase_implCompress_Type(bool is_sha3);
282   static const TypeFunc* digestBase_implCompressMB_Type(bool is_sha3);
283 
284   static const TypeFunc* multiplyToLen_Type();
285   static const TypeFunc* montgomeryMultiply_Type();
286   static const TypeFunc* montgomerySquare_Type();
287 
288   static const TypeFunc* squareToLen_Type();
289 
290   static const TypeFunc* mulAdd_Type();
291 
292   static const TypeFunc* bigIntegerShift_Type();
293 
294   static const TypeFunc* vectorizedMismatch_Type();
295 
296   static const TypeFunc* ghash_processBlocks_Type();
297   static const TypeFunc* chacha20Block_Type();
298   static const TypeFunc* base64_encodeBlock_Type();
299   static const TypeFunc* base64_decodeBlock_Type();
300   static const TypeFunc* poly1305_processBlocks_Type();
301 
302   static const TypeFunc* updateBytesCRC32_Type();
303   static const TypeFunc* updateBytesCRC32C_Type();
304 
305   static const TypeFunc* updateBytesAdler32_Type();
306 
307   // leaf on stack replacement interpreter accessor types
308   static const TypeFunc* osr_end_Type();
309 
310   static const TypeFunc* register_finalizer_Type();
311 
312   JFR_ONLY(static const TypeFunc* class_id_load_barrier_Type();)
313 #if INCLUDE_JVMTI
314   static const TypeFunc* notify_jvmti_vthread_Type();
315 #endif
316 
317   // Dtrace support
318   static const TypeFunc* dtrace_method_entry_exit_Type();
319   static const TypeFunc* dtrace_object_alloc_Type();
320 
321   static const TypeFunc* store_inline_type_fields_Type();
322   static const TypeFunc* pack_inline_type_Type();
323 
324   static void load_unknown_inline(flatArrayOopDesc* array, int index, JavaThread* current);
325   static const TypeFunc* load_unknown_inline_type();
326   static void store_unknown_inline(instanceOopDesc* buffer, flatArrayOopDesc* array, int index);
327   static const TypeFunc* store_unknown_inline_type();
328 
329  private:
330  static NamedCounter * volatile _named_counters;
331 
332  public:
333  // helper function which creates a named counter labeled with the
334  // if they are available
335  static NamedCounter* new_named_counter(JVMState* jvms, NamedCounter::CounterTag tag);
336 
337  // dumps all the named counters
338  static void          print_named_counters();
339 
340 };
341 
342 #endif // SHARE_OPTO_RUNTIME_HPP