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