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