1 /*
  2  * Copyright (c) 1998, 2024, 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/deoptimization.hpp"
 33 #include "runtime/stubDeclarations.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   };
 66 
 67 private:
 68   const char *  _name;
 69   int           _count;
 70   CounterTag    _tag;
 71   NamedCounter* _next;
 72 
 73  public:
 74   NamedCounter(const char *n, CounterTag tag = NoTag):
 75     _name(n == nullptr ? nullptr : os::strdup(n)),
 76     _count(0),
 77     _tag(tag),
 78     _next(nullptr) {}
 79 
 80   ~NamedCounter() {
 81     if (_name != nullptr) {
 82       os::free((void*)_name);
 83     }
 84   }
 85 
 86   const char * name() const     { return _name; }
 87   int count() const             { return _count; }
 88   address addr()                { return (address)&_count; }
 89   CounterTag tag() const        { return _tag; }
 90   void set_tag(CounterTag tag)  { _tag = tag; }
 91 
 92   NamedCounter* next() const    { return _next; }
 93   void set_next(NamedCounter* next) {
 94     assert(_next == nullptr || next == nullptr, "already set");
 95     _next = next;
 96   }
 97 
 98 };
 99 
100 typedef const TypeFunc*(*TypeFunc_generator)();
101 
102 // define OptoStubId enum tags: uncommon_trap_id etc
103 
104 #define C2_BLOB_ID_ENUM_DECLARE(name, type) STUB_ID_NAME(name),
105 #define C2_STUB_ID_ENUM_DECLARE(name, f, t, r) STUB_ID_NAME(name),
106 #define C2_JVMTI_STUB_ID_ENUM_DECLARE(name) STUB_ID_NAME(name),
107 enum class OptoStubId :int {
108   NO_STUBID = -1,
109   C2_STUBS_DO(C2_BLOB_ID_ENUM_DECLARE, C2_STUB_ID_ENUM_DECLARE, C2_JVMTI_STUB_ID_ENUM_DECLARE)
110   NUM_STUBIDS
111 };
112 #undef C2_BLOB_ID_ENUM_DECLARE
113 #undef C2_STUB_ID_ENUM_DECLARE
114 #undef C2_JVMTI_STUB_ID_ENUM_DECLARE
115 
116 class OptoRuntime : public AllStatic {
117   friend class Matcher;  // allow access to stub names
118   friend class SCAddressTable;
119  private:
120   // declare opto stub address/blob holder static fields
121 #define C2_BLOB_FIELD_DECLARE(name, type) \
122   static type        BLOB_FIELD_NAME(name);
123 #define C2_STUB_FIELD_NAME(name) _ ## name ## _Java
124 #define C2_STUB_FIELD_DECLARE(name, f, t, r) \
125   static address     C2_STUB_FIELD_NAME(name) ;
126 #define C2_JVMTI_STUB_FIELD_DECLARE(name) \
127   static address     STUB_FIELD_NAME(name);
128 
129   C2_STUBS_DO(C2_BLOB_FIELD_DECLARE, C2_STUB_FIELD_DECLARE, C2_JVMTI_STUB_FIELD_DECLARE)
130 
131 #undef C2_BLOB_FIELD_DECLARE
132 #undef C2_STUB_FIELD_NAME
133 #undef C2_STUB_FIELD_DECLARE
134 #undef C2_JVMTI_STUB_FIELD_DECLARE
135 
136   // Stub names indexed by sharedStubId
137   static const char *_stub_names[];
138 
139   // define stubs
140   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);
141 
142   static address _vtable_must_compile_Java;
143 
144   //
145   // Implementation of runtime methods
146   // =================================
147 
148   // Allocate storage for a Java instance.
149   static void new_instance_C(Klass* instance_klass, JavaThread* current);
150 
151   // Allocate storage for a objArray or typeArray
152   static void new_array_C(Klass* array_klass, int len, JavaThread* current);
153   static void new_array_nozero_C(Klass* array_klass, int len, JavaThread* current);
154 
155   // Allocate storage for a multi-dimensional arrays
156   // Note: needs to be fixed for arbitrary number of dimensions
157   static void multianewarray2_C(Klass* klass, int len1, int len2, JavaThread* current);
158   static void multianewarray3_C(Klass* klass, int len1, int len2, int len3, JavaThread* current);
159   static void multianewarray4_C(Klass* klass, int len1, int len2, int len3, int len4, JavaThread* current);
160   static void multianewarray5_C(Klass* klass, int len1, int len2, int len3, int len4, int len5, JavaThread* current);
161   static void multianewarrayN_C(Klass* klass, arrayOopDesc* dims, JavaThread* current);
162 
163   // local methods passed as arguments to stub generator that forward
164   // control to corresponding JRT methods of SharedRuntime
165   static void slow_arraycopy_C(oopDesc* src,  jint src_pos,
166                                oopDesc* dest, jint dest_pos,
167                                jint length, JavaThread* thread);
168   static void complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current);
169 
170 public:
171   static void monitor_notify_C(oopDesc* obj, JavaThread* current);
172   static void monitor_notifyAll_C(oopDesc* obj, JavaThread* current);
173 
174 private:
175 
176   // Implicit exception support
177   static void throw_null_exception_C(JavaThread* thread);
178 
179   // Exception handling
180   static address handle_exception_C       (JavaThread* current);
181   static address handle_exception_C_helper(JavaThread* current, nmethod*& nm);
182   static address rethrow_C                (oopDesc* exception, JavaThread *thread, address return_pc );
183   static void deoptimize_caller_frame     (JavaThread *thread);
184   static void deoptimize_caller_frame     (JavaThread *thread, bool doit);
185   static bool is_deoptimized_caller_frame (JavaThread *thread);
186 
187   // CodeBlob support
188   // ===================================================================
189 
190   static void generate_uncommon_trap_blob(void);
191   static void generate_exception_blob();
192 
193   static void register_finalizer_C(oopDesc* obj, JavaThread* current);
194   static void class_init_barrier_C(Klass* k, JavaThread* current);
195  public:
196 
197   static bool is_callee_saved_register(MachRegisterNumbers reg);
198 
199   // One time only generate runtime code stubs. Returns true
200   // when runtime stubs have been generated successfully and
201   // false otherwise.
202   static bool generate(ciEnv* env);
203 
204   // Returns the name of a stub
205   static const char* stub_name(address entry);
206 
207   // Returns the name associated with a given stub id
208   static const char* stub_name(OptoStubId id) {
209     assert(id > OptoStubId::NO_STUBID && id < OptoStubId::NUM_STUBIDS, "stub id out of range");
210     return _stub_names[(int)id];
211   }
212 
213   // access to runtime stubs entry points for java code
214   static address new_instance_Java()                     { return _new_instance_Java; }
215   static address new_array_Java()                        { return _new_array_Java; }
216   static address new_array_nozero_Java()                 { return _new_array_nozero_Java; }
217   static address multianewarray2_Java()                  { return _multianewarray2_Java; }
218   static address multianewarray3_Java()                  { return _multianewarray3_Java; }
219   static address multianewarray4_Java()                  { return _multianewarray4_Java; }
220   static address multianewarray5_Java()                  { return _multianewarray5_Java; }
221   static address multianewarrayN_Java()                  { return _multianewarrayN_Java; }
222   static address vtable_must_compile_stub()              { return _vtable_must_compile_Java; }
223   static address complete_monitor_locking_Java()         { return _complete_monitor_locking_Java; }
224   static address monitor_notify_Java()                   { return _monitor_notify_Java; }
225   static address monitor_notifyAll_Java()                { return _monitor_notifyAll_Java; }
226 
227   static address slow_arraycopy_Java()                   { return _slow_arraycopy_Java; }
228   static address register_finalizer_Java()               { return _register_finalizer_Java; }
229   static address class_init_barrier_Java()               { return _class_init_barrier_Java; }
230 #if INCLUDE_JVMTI
231   static address notify_jvmti_vthread_start()            { return _notify_jvmti_vthread_start; }
232   static address notify_jvmti_vthread_end()              { return _notify_jvmti_vthread_end; }
233   static address notify_jvmti_vthread_mount()            { return _notify_jvmti_vthread_mount; }
234   static address notify_jvmti_vthread_unmount()          { return _notify_jvmti_vthread_unmount; }
235 #endif
236 
237   static UncommonTrapBlob* uncommon_trap_blob()                  { return _uncommon_trap_blob; }
238   static ExceptionBlob*    exception_blob()                      { return _exception_blob; }
239 
240   // Implicit exception support
241   static void throw_div0_exception_C      (JavaThread* thread);
242   static void throw_stack_overflow_error_C(JavaThread* thread);
243 
244   // Exception handling
245   static address rethrow_stub()             { return _rethrow_Java; }
246 
247 
248   // Type functions
249   // ======================================================
250 
251   static const TypeFunc* new_instance_Type(); // object allocation (slow case)
252   static const TypeFunc* new_array_Type ();   // [a]newarray (slow case)
253   static const TypeFunc* new_array_nozero_Type ();   // [a]newarray (slow case)
254   static const TypeFunc* multianewarray_Type(int ndim); // multianewarray
255   static const TypeFunc* multianewarray2_Type(); // multianewarray
256   static const TypeFunc* multianewarray3_Type(); // multianewarray
257   static const TypeFunc* multianewarray4_Type(); // multianewarray
258   static const TypeFunc* multianewarray5_Type(); // multianewarray
259   static const TypeFunc* multianewarrayN_Type(); // multianewarray
260   static const TypeFunc* complete_monitor_enter_Type();
261   static const TypeFunc* complete_monitor_locking_Type();
262   static const TypeFunc* complete_monitor_exit_Type();
263   static const TypeFunc* monitor_notify_Type();
264   static const TypeFunc* monitor_notifyAll_Type();
265   static const TypeFunc* uncommon_trap_Type();
266   static const TypeFunc* athrow_Type();
267   static const TypeFunc* rethrow_Type();
268   static const TypeFunc* Math_D_D_Type();  // sin,cos & friends
269   static const TypeFunc* Math_DD_D_Type(); // mod,pow & friends
270   static const TypeFunc* Math_Vector_Vector_Type(uint num_arg, const TypeVect* in_type, const TypeVect* out_type);
271   static const TypeFunc* modf_Type();
272   static const TypeFunc* l2f_Type();
273   static const TypeFunc* void_long_Type();
274   static const TypeFunc* void_void_Type();
275 
276   static const TypeFunc* jfr_write_checkpoint_Type();
277 
278   static const TypeFunc* flush_windows_Type();
279 
280   // arraycopy routine types
281   static const TypeFunc* fast_arraycopy_Type(); // bit-blasters
282   static const TypeFunc* checkcast_arraycopy_Type();
283   static const TypeFunc* generic_arraycopy_Type();
284   static const TypeFunc* slow_arraycopy_Type();   // the full routine
285 
286   static const TypeFunc* make_setmemory_Type();
287 
288   static const TypeFunc* array_fill_Type();
289 
290   static const TypeFunc* array_sort_Type();
291   static const TypeFunc* array_partition_Type();
292   static const TypeFunc* aescrypt_block_Type();
293   static const TypeFunc* cipherBlockChaining_aescrypt_Type();
294   static const TypeFunc* electronicCodeBook_aescrypt_Type();
295   static const TypeFunc* counterMode_aescrypt_Type();
296   static const TypeFunc* galoisCounterMode_aescrypt_Type();
297 
298   static const TypeFunc* digestBase_implCompress_Type(bool is_sha3);
299   static const TypeFunc* digestBase_implCompressMB_Type(bool is_sha3);
300 
301   static const TypeFunc* multiplyToLen_Type();
302   static const TypeFunc* montgomeryMultiply_Type();
303   static const TypeFunc* montgomerySquare_Type();
304 
305   static const TypeFunc* squareToLen_Type();
306 
307   static const TypeFunc* mulAdd_Type();
308 
309   static const TypeFunc* bigIntegerShift_Type();
310 
311   static const TypeFunc* vectorizedMismatch_Type();
312 
313   static const TypeFunc* ghash_processBlocks_Type();
314   static const TypeFunc* chacha20Block_Type();
315   static const TypeFunc* base64_encodeBlock_Type();
316   static const TypeFunc* base64_decodeBlock_Type();
317   static const TypeFunc* string_IndexOf_Type();
318   static const TypeFunc* poly1305_processBlocks_Type();
319   static const TypeFunc* intpoly_montgomeryMult_P256_Type();
320   static const TypeFunc* intpoly_assign_Type();
321 
322   static const TypeFunc* updateBytesCRC32_Type();
323   static const TypeFunc* updateBytesCRC32C_Type();
324 
325   static const TypeFunc* updateBytesAdler32_Type();
326 
327   // leaf on stack replacement interpreter accessor types
328   static const TypeFunc* osr_end_Type();
329 
330   static const TypeFunc* register_finalizer_Type();
331 
332   static const TypeFunc* class_init_barrier_Type();
333 
334   JFR_ONLY(static const TypeFunc* class_id_load_barrier_Type();)
335 #if INCLUDE_JVMTI
336   static const TypeFunc* notify_jvmti_vthread_Type();
337 #endif
338 
339   // runtime upcalls support
340   static const TypeFunc* runtime_up_call_Type();
341 
342   // Dtrace support
343   static const TypeFunc* dtrace_method_entry_exit_Type();
344   static const TypeFunc* dtrace_object_alloc_Type();
345 
346  private:
347  static NamedCounter * volatile _named_counters;
348 
349  public:
350  // helper function which creates a named counter labeled with the
351  // if they are available
352  static NamedCounter* new_named_counter(JVMState* jvms, NamedCounter::CounterTag tag);
353 
354  // dumps all the named counters
355  static void          print_named_counters();
356 
357  public:
358   static void init_counters();
359   static void print_counters_on(outputStream* st);
360 };
361 
362 #endif // SHARE_OPTO_RUNTIME_HPP