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 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "code/codeCache.hpp"
29 #include "code/compiledMethod.inline.hpp"
30 #include "code/compiledIC.hpp"
31 #include "code/icBuffer.hpp"
32 #include "code/nmethod.hpp"
33 #include "code/pcDesc.hpp"
34 #include "code/scopeDesc.hpp"
35 #include "code/vtableStubs.hpp"
36 #include "compiler/compileBroker.hpp"
37 #include "compiler/oopMap.hpp"
38 #include "gc/g1/heapRegion.hpp"
39 #include "gc/shared/barrierSet.hpp"
40 #include "gc/shared/collectedHeap.hpp"
41 #include "gc/shared/gcLocker.hpp"
42 #include "interpreter/bytecode.hpp"
43 #include "interpreter/interpreter.hpp"
44 #include "interpreter/linkResolver.hpp"
45 #include "logging/log.hpp"
46 #include "logging/logStream.hpp"
47 #include "memory/oopFactory.hpp"
48 #include "memory/resourceArea.hpp"
49 #include "oops/objArrayKlass.hpp"
50 #include "oops/oop.inline.hpp"
51 #include "oops/typeArrayOop.inline.hpp"
52 #include "opto/ad.hpp"
53 #include "opto/addnode.hpp"
54 #include "opto/callnode.hpp"
55 #include "opto/cfgnode.hpp"
56 #include "opto/graphKit.hpp"
57 #include "opto/machnode.hpp"
58 #include "opto/matcher.hpp"
59 #include "opto/memnode.hpp"
60 #include "opto/mulnode.hpp"
61 #include "opto/output.hpp"
62 #include "opto/runtime.hpp"
63 #include "opto/subnode.hpp"
64 #include "prims/jvmtiExport.hpp"
65 #include "runtime/atomic.hpp"
66 #include "runtime/frame.inline.hpp"
67 #include "runtime/handles.inline.hpp"
68 #include "runtime/interfaceSupport.inline.hpp"
69 #include "runtime/javaCalls.hpp"
70 #include "runtime/sharedRuntime.hpp"
71 #include "runtime/signature.hpp"
72 #include "runtime/stackWatermarkSet.hpp"
73 #include "runtime/threadCritical.hpp"
74 #include "runtime/vframe.hpp"
75 #include "runtime/vframeArray.hpp"
76 #include "runtime/vframe_hp.hpp"
77 #include "utilities/copy.hpp"
78 #include "utilities/preserveException.hpp"
79 
80 
81 // For debugging purposes:
82 //  To force FullGCALot inside a runtime function, add the following two lines
83 //
84 //  Universe::release_fullgc_alot_dummy();
85 //  MarkSweep::invoke(0, "Debugging");
86 //
87 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
88 
89 
90 
91 
92 // Compiled code entry points
93 address OptoRuntime::_new_instance_Java                           = NULL;
94 address OptoRuntime::_new_array_Java                              = NULL;
95 address OptoRuntime::_new_array_nozero_Java                       = NULL;
96 address OptoRuntime::_multianewarray2_Java                        = NULL;
97 address OptoRuntime::_multianewarray3_Java                        = NULL;
98 address OptoRuntime::_multianewarray4_Java                        = NULL;
99 address OptoRuntime::_multianewarray5_Java                        = NULL;
100 address OptoRuntime::_multianewarrayN_Java                        = NULL;
101 address OptoRuntime::_vtable_must_compile_Java                    = NULL;
102 address OptoRuntime::_complete_monitor_locking_Java               = NULL;
103 address OptoRuntime::_monitor_notify_Java                         = NULL;
104 address OptoRuntime::_monitor_notifyAll_Java                      = NULL;
105 address OptoRuntime::_rethrow_Java                                = NULL;
106 
107 address OptoRuntime::_slow_arraycopy_Java                         = NULL;
108 address OptoRuntime::_register_finalizer_Java                     = NULL;
109 
110 ExceptionBlob* OptoRuntime::_exception_blob;
111 
112 // This should be called in an assertion at the start of OptoRuntime routines
113 // which are entered from compiled code (all of them)
114 #ifdef ASSERT
check_compiled_frame(JavaThread * thread)115 static bool check_compiled_frame(JavaThread* thread) {
116   assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
117   RegisterMap map(thread, false);
118   frame caller = thread->last_frame().sender(&map);
119   assert(caller.is_compiled_frame(), "not being called from compiled like code");
120   return true;
121 }
122 #endif // ASSERT
123 
124 
125 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, save_arg_regs, return_pc) \
126   var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, save_arg_regs, return_pc); \
127   if (var == NULL) { return false; }
128 
generate(ciEnv * env)129 bool OptoRuntime::generate(ciEnv* env) {
130 
131   generate_exception_blob();
132 
133   // Note: tls: Means fetching the return oop out of the thread-local storage
134   //
135   //   variable/name                       type-function-gen              , runtime method                  ,fncy_jp, tls,save_args,retpc
136   // -------------------------------------------------------------------------------------------------------------------------------
137   gen(env, _new_instance_Java              , new_instance_Type            , new_instance_C                  ,    0 , true , false, false);
138   gen(env, _new_array_Java                 , new_array_Type               , new_array_C                     ,    0 , true , false, false);
139   gen(env, _new_array_nozero_Java          , new_array_Type               , new_array_nozero_C              ,    0 , true , false, false);
140   gen(env, _multianewarray2_Java           , multianewarray2_Type         , multianewarray2_C               ,    0 , true , false, false);
141   gen(env, _multianewarray3_Java           , multianewarray3_Type         , multianewarray3_C               ,    0 , true , false, false);
142   gen(env, _multianewarray4_Java           , multianewarray4_Type         , multianewarray4_C               ,    0 , true , false, false);
143   gen(env, _multianewarray5_Java           , multianewarray5_Type         , multianewarray5_C               ,    0 , true , false, false);
144   gen(env, _multianewarrayN_Java           , multianewarrayN_Type         , multianewarrayN_C               ,    0 , true , false, false);
145   gen(env, _complete_monitor_locking_Java  , complete_monitor_enter_Type  , SharedRuntime::complete_monitor_locking_C, 0, false, false, false);
146   gen(env, _monitor_notify_Java            , monitor_notify_Type          , monitor_notify_C                ,    0 , false, false, false);
147   gen(env, _monitor_notifyAll_Java         , monitor_notify_Type          , monitor_notifyAll_C             ,    0 , false, false, false);
148   gen(env, _rethrow_Java                   , rethrow_Type                 , rethrow_C                       ,    2 , true , false, true );
149 
150   gen(env, _slow_arraycopy_Java            , slow_arraycopy_Type          , SharedRuntime::slow_arraycopy_C ,    0 , false, false, false);
151   gen(env, _register_finalizer_Java        , register_finalizer_Type      , register_finalizer              ,    0 , false, false, false);
152 
153   return true;
154 }
155 
156 #undef gen
157 
158 
159 // Helper method to do generation of RunTimeStub's
generate_stub(ciEnv * env,TypeFunc_generator gen,address C_function,const char * name,int is_fancy_jump,bool pass_tls,bool save_argument_registers,bool return_pc)160 address OptoRuntime::generate_stub( ciEnv* env,
161                                     TypeFunc_generator gen, address C_function,
162                                     const char *name, int is_fancy_jump,
163                                     bool pass_tls,
164                                     bool save_argument_registers,
165                                     bool return_pc) {
166 
167   // Matching the default directive, we currently have no method to match.
168   DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompileBroker::compiler(CompLevel_full_optimization));
169   ResourceMark rm;
170   Compile C( env, gen, C_function, name, is_fancy_jump, pass_tls, save_argument_registers, return_pc, directive);
171   DirectivesStack::release(directive);
172   return  C.stub_entry_point();
173 }
174 
stub_name(address entry)175 const char* OptoRuntime::stub_name(address entry) {
176 #ifndef PRODUCT
177   CodeBlob* cb = CodeCache::find_blob(entry);
178   RuntimeStub* rs =(RuntimeStub *)cb;
179   assert(rs != NULL && rs->is_runtime_stub(), "not a runtime stub");
180   return rs->name();
181 #else
182   // Fast implementation for product mode (maybe it should be inlined too)
183   return "runtime stub";
184 #endif
185 }
186 
187 
188 //=============================================================================
189 // Opto compiler runtime routines
190 //=============================================================================
191 
192 
193 //=============================allocation======================================
194 // We failed the fast-path allocation.  Now we need to do a scavenge or GC
195 // and try allocation again.
196 
197 // object allocation
198 JRT_BLOCK_ENTRY(void, OptoRuntime::new_instance_C(Klass* klass, JavaThread* thread))
199   JRT_BLOCK;
200 #ifndef PRODUCT
201   SharedRuntime::_new_instance_ctr++;         // new instance requires GC
202 #endif
203   assert(check_compiled_frame(thread), "incorrect caller");
204 
205   // These checks are cheap to make and support reflective allocation.
206   int lh = klass->layout_helper();
207   if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
208     Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
209     klass->check_valid_for_instantiation(false, THREAD);
210     if (!HAS_PENDING_EXCEPTION) {
211       InstanceKlass::cast(klass)->initialize(THREAD);
212     }
213   }
214 
215   if (!HAS_PENDING_EXCEPTION) {
216     // Scavenge and allocate an instance.
217     Handle holder(THREAD, klass->klass_holder()); // keep the klass alive
218     oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
219     thread->set_vm_result(result);
220 
221     // Pass oops back through thread local storage.  Our apparent type to Java
222     // is that we return an oop, but we can block on exit from this routine and
223     // a GC can trash the oop in C's return register.  The generated stub will
224     // fetch the oop from TLS after any possible GC.
225   }
226 
227   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
228   JRT_BLOCK_END;
229 
230   // inform GC that we won't do card marks for initializing writes.
231   SharedRuntime::on_slowpath_allocation_exit(thread);
232 JRT_END
233 
234 
235 // array allocation
236 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread *thread))
237   JRT_BLOCK;
238 #ifndef PRODUCT
239   SharedRuntime::_new_array_ctr++;            // new array requires GC
240 #endif
241   assert(check_compiled_frame(thread), "incorrect caller");
242 
243   // Scavenge and allocate an instance.
244   oop result;
245 
246   if (array_type->is_typeArray_klass()) {
247     // The oopFactory likes to work with the element type.
248     // (We could bypass the oopFactory, since it doesn't add much value.)
249     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
250     result = oopFactory::new_typeArray(elem_type, len, THREAD);
251   } else {
252     // Although the oopFactory likes to work with the elem_type,
253     // the compiler prefers the array_type, since it must already have
254     // that latter value in hand for the fast path.
255     Handle holder(THREAD, array_type->klass_holder()); // keep the array klass alive
256     Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
257     result = oopFactory::new_objArray(elem_type, len, THREAD);
258   }
259 
260   // Pass oops back through thread local storage.  Our apparent type to Java
261   // is that we return an oop, but we can block on exit from this routine and
262   // a GC can trash the oop in C's return register.  The generated stub will
263   // fetch the oop from TLS after any possible GC.
264   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
265   thread->set_vm_result(result);
266   JRT_BLOCK_END;
267 
268   // inform GC that we won't do card marks for initializing writes.
269   SharedRuntime::on_slowpath_allocation_exit(thread);
270 JRT_END
271 
272 // array allocation without zeroing
273 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread *thread))
274   JRT_BLOCK;
275 #ifndef PRODUCT
276   SharedRuntime::_new_array_ctr++;            // new array requires GC
277 #endif
278   assert(check_compiled_frame(thread), "incorrect caller");
279 
280   // Scavenge and allocate an instance.
281   oop result;
282 
283   assert(array_type->is_typeArray_klass(), "should be called only for type array");
284   // The oopFactory likes to work with the element type.
285   BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
286   result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
287 
288   // Pass oops back through thread local storage.  Our apparent type to Java
289   // is that we return an oop, but we can block on exit from this routine and
290   // a GC can trash the oop in C's return register.  The generated stub will
291   // fetch the oop from TLS after any possible GC.
292   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
293   thread->set_vm_result(result);
294   JRT_BLOCK_END;
295 
296 
297   // inform GC that we won't do card marks for initializing writes.
298   SharedRuntime::on_slowpath_allocation_exit(thread);
299 
300   oop result = thread->vm_result();
301   if ((len > 0) && (result != NULL) &&
302       is_deoptimized_caller_frame(thread)) {
303     // Zero array here if the caller is deoptimized.
304     int size = ((typeArrayOop)result)->object_size();
305     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
306     const size_t hs = arrayOopDesc::header_size(elem_type);
307     // Align to next 8 bytes to avoid trashing arrays's length.
308     const size_t aligned_hs = align_object_offset(hs);
309     HeapWord* obj = cast_from_oop<HeapWord*>(result);
310     if (aligned_hs > hs) {
311       Copy::zero_to_words(obj+hs, aligned_hs-hs);
312     }
313     // Optimized zeroing.
314     Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
315   }
316 
317 JRT_END
318 
319 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
320 
321 // multianewarray for 2 dimensions
322 JRT_ENTRY(void, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread *thread))
323 #ifndef PRODUCT
324   SharedRuntime::_multi2_ctr++;                // multianewarray for 1 dimension
325 #endif
326   assert(check_compiled_frame(thread), "incorrect caller");
327   assert(elem_type->is_klass(), "not a class");
328   jint dims[2];
329   dims[0] = len1;
330   dims[1] = len2;
331   Handle holder(THREAD, elem_type->klass_holder()); // keep the klass alive
332   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
333   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
334   thread->set_vm_result(obj);
335 JRT_END
336 
337 // multianewarray for 3 dimensions
338 JRT_ENTRY(void, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread *thread))
339 #ifndef PRODUCT
340   SharedRuntime::_multi3_ctr++;                // multianewarray for 1 dimension
341 #endif
342   assert(check_compiled_frame(thread), "incorrect caller");
343   assert(elem_type->is_klass(), "not a class");
344   jint dims[3];
345   dims[0] = len1;
346   dims[1] = len2;
347   dims[2] = len3;
348   Handle holder(THREAD, elem_type->klass_holder()); // keep the klass alive
349   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
350   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
351   thread->set_vm_result(obj);
352 JRT_END
353 
354 // multianewarray for 4 dimensions
355 JRT_ENTRY(void, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread *thread))
356 #ifndef PRODUCT
357   SharedRuntime::_multi4_ctr++;                // multianewarray for 1 dimension
358 #endif
359   assert(check_compiled_frame(thread), "incorrect caller");
360   assert(elem_type->is_klass(), "not a class");
361   jint dims[4];
362   dims[0] = len1;
363   dims[1] = len2;
364   dims[2] = len3;
365   dims[3] = len4;
366   Handle holder(THREAD, elem_type->klass_holder()); // keep the klass alive
367   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
368   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
369   thread->set_vm_result(obj);
370 JRT_END
371 
372 // multianewarray for 5 dimensions
373 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread *thread))
374 #ifndef PRODUCT
375   SharedRuntime::_multi5_ctr++;                // multianewarray for 1 dimension
376 #endif
377   assert(check_compiled_frame(thread), "incorrect caller");
378   assert(elem_type->is_klass(), "not a class");
379   jint dims[5];
380   dims[0] = len1;
381   dims[1] = len2;
382   dims[2] = len3;
383   dims[3] = len4;
384   dims[4] = len5;
385   Handle holder(THREAD, elem_type->klass_holder()); // keep the klass alive
386   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
387   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
388   thread->set_vm_result(obj);
389 JRT_END
390 
391 JRT_ENTRY(void, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread *thread))
392   assert(check_compiled_frame(thread), "incorrect caller");
393   assert(elem_type->is_klass(), "not a class");
394   assert(oop(dims)->is_typeArray(), "not an array");
395 
396   ResourceMark rm;
397   jint len = dims->length();
398   assert(len > 0, "Dimensions array should contain data");
399   jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
400   ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
401                                        c_dims, len);
402 
403   Handle holder(THREAD, elem_type->klass_holder()); // keep the klass alive
404   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
405   deoptimize_caller_frame(thread, HAS_PENDING_EXCEPTION);
406   thread->set_vm_result(obj);
407 JRT_END
408 
409 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread *thread))
410 
411   // Very few notify/notifyAll operations find any threads on the waitset, so
412   // the dominant fast-path is to simply return.
413   // Relatedly, it's critical that notify/notifyAll be fast in order to
414   // reduce lock hold times.
415   if (!SafepointSynchronize::is_synchronizing()) {
416     if (ObjectSynchronizer::quick_notify(obj, thread, false)) {
417       return;
418     }
419   }
420 
421   // This is the case the fast-path above isn't provisioned to handle.
422   // The fast-path is designed to handle frequently arising cases in an efficient manner.
423   // (The fast-path is just a degenerate variant of the slow-path).
424   // Perform the dreaded state transition and pass control into the slow-path.
425   JRT_BLOCK;
426   Handle h_obj(THREAD, obj);
427   ObjectSynchronizer::notify(h_obj, CHECK);
428   JRT_BLOCK_END;
429 JRT_END
430 
431 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread *thread))
432 
433   if (!SafepointSynchronize::is_synchronizing() ) {
434     if (ObjectSynchronizer::quick_notify(obj, thread, true)) {
435       return;
436     }
437   }
438 
439   // This is the case the fast-path above isn't provisioned to handle.
440   // The fast-path is designed to handle frequently arising cases in an efficient manner.
441   // (The fast-path is just a degenerate variant of the slow-path).
442   // Perform the dreaded state transition and pass control into the slow-path.
443   JRT_BLOCK;
444   Handle h_obj(THREAD, obj);
445   ObjectSynchronizer::notifyall(h_obj, CHECK);
446   JRT_BLOCK_END;
447 JRT_END
448 
new_instance_Type()449 const TypeFunc *OptoRuntime::new_instance_Type() {
450   // create input type (domain)
451   const Type **fields = TypeTuple::fields(1);
452   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Klass to be allocated
453   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
454 
455   // create result type (range)
456   fields = TypeTuple::fields(1);
457   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
458 
459   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
460 
461   return TypeFunc::make(domain, range);
462 }
463 
464 
athrow_Type()465 const TypeFunc *OptoRuntime::athrow_Type() {
466   // create input type (domain)
467   const Type **fields = TypeTuple::fields(1);
468   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Klass to be allocated
469   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
470 
471   // create result type (range)
472   fields = TypeTuple::fields(0);
473 
474   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
475 
476   return TypeFunc::make(domain, range);
477 }
478 
479 
new_array_Type()480 const TypeFunc *OptoRuntime::new_array_Type() {
481   // create input type (domain)
482   const Type **fields = TypeTuple::fields(2);
483   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
484   fields[TypeFunc::Parms+1] = TypeInt::INT;       // array size
485   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
486 
487   // create result type (range)
488   fields = TypeTuple::fields(1);
489   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
490 
491   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
492 
493   return TypeFunc::make(domain, range);
494 }
495 
multianewarray_Type(int ndim)496 const TypeFunc *OptoRuntime::multianewarray_Type(int ndim) {
497   // create input type (domain)
498   const int nargs = ndim + 1;
499   const Type **fields = TypeTuple::fields(nargs);
500   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
501   for( int i = 1; i < nargs; i++ )
502     fields[TypeFunc::Parms + i] = TypeInt::INT;       // array size
503   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+nargs, fields);
504 
505   // create result type (range)
506   fields = TypeTuple::fields(1);
507   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
508   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
509 
510   return TypeFunc::make(domain, range);
511 }
512 
multianewarray2_Type()513 const TypeFunc *OptoRuntime::multianewarray2_Type() {
514   return multianewarray_Type(2);
515 }
516 
multianewarray3_Type()517 const TypeFunc *OptoRuntime::multianewarray3_Type() {
518   return multianewarray_Type(3);
519 }
520 
multianewarray4_Type()521 const TypeFunc *OptoRuntime::multianewarray4_Type() {
522   return multianewarray_Type(4);
523 }
524 
multianewarray5_Type()525 const TypeFunc *OptoRuntime::multianewarray5_Type() {
526   return multianewarray_Type(5);
527 }
528 
multianewarrayN_Type()529 const TypeFunc *OptoRuntime::multianewarrayN_Type() {
530   // create input type (domain)
531   const Type **fields = TypeTuple::fields(2);
532   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;   // element klass
533   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;   // array of dim sizes
534   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
535 
536   // create result type (range)
537   fields = TypeTuple::fields(1);
538   fields[TypeFunc::Parms+0] = TypeRawPtr::NOTNULL; // Returned oop
539   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
540 
541   return TypeFunc::make(domain, range);
542 }
543 
uncommon_trap_Type()544 const TypeFunc *OptoRuntime::uncommon_trap_Type() {
545   // create input type (domain)
546   const Type **fields = TypeTuple::fields(1);
547   fields[TypeFunc::Parms+0] = TypeInt::INT; // trap_reason (deopt reason and action)
548   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
549 
550   // create result type (range)
551   fields = TypeTuple::fields(0);
552   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
553 
554   return TypeFunc::make(domain, range);
555 }
556 
557 //-----------------------------------------------------------------------------
558 // Monitor Handling
complete_monitor_enter_Type()559 const TypeFunc *OptoRuntime::complete_monitor_enter_Type() {
560   // create input type (domain)
561   const Type **fields = TypeTuple::fields(2);
562   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
563   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;   // Address of stack location for lock
564   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
565 
566   // create result type (range)
567   fields = TypeTuple::fields(0);
568 
569   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
570 
571   return TypeFunc::make(domain,range);
572 }
573 
574 
575 //-----------------------------------------------------------------------------
complete_monitor_exit_Type()576 const TypeFunc *OptoRuntime::complete_monitor_exit_Type() {
577   // create input type (domain)
578   const Type **fields = TypeTuple::fields(3);
579   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
580   fields[TypeFunc::Parms+1] = TypeRawPtr::BOTTOM;    // Address of stack location for lock - BasicLock
581   fields[TypeFunc::Parms+2] = TypeRawPtr::BOTTOM;    // Thread pointer (Self)
582   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+3, fields);
583 
584   // create result type (range)
585   fields = TypeTuple::fields(0);
586 
587   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
588 
589   return TypeFunc::make(domain, range);
590 }
591 
monitor_notify_Type()592 const TypeFunc *OptoRuntime::monitor_notify_Type() {
593   // create input type (domain)
594   const Type **fields = TypeTuple::fields(1);
595   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // Object to be Locked
596   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
597 
598   // create result type (range)
599   fields = TypeTuple::fields(0);
600   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
601   return TypeFunc::make(domain, range);
602 }
603 
flush_windows_Type()604 const TypeFunc* OptoRuntime::flush_windows_Type() {
605   // create input type (domain)
606   const Type** fields = TypeTuple::fields(1);
607   fields[TypeFunc::Parms+0] = NULL; // void
608   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms, fields);
609 
610   // create result type
611   fields = TypeTuple::fields(1);
612   fields[TypeFunc::Parms+0] = NULL; // void
613   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
614 
615   return TypeFunc::make(domain, range);
616 }
617 
l2f_Type()618 const TypeFunc* OptoRuntime::l2f_Type() {
619   // create input type (domain)
620   const Type **fields = TypeTuple::fields(2);
621   fields[TypeFunc::Parms+0] = TypeLong::LONG;
622   fields[TypeFunc::Parms+1] = Type::HALF;
623   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
624 
625   // create result type (range)
626   fields = TypeTuple::fields(1);
627   fields[TypeFunc::Parms+0] = Type::FLOAT;
628   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
629 
630   return TypeFunc::make(domain, range);
631 }
632 
modf_Type()633 const TypeFunc* OptoRuntime::modf_Type() {
634   const Type **fields = TypeTuple::fields(2);
635   fields[TypeFunc::Parms+0] = Type::FLOAT;
636   fields[TypeFunc::Parms+1] = Type::FLOAT;
637   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
638 
639   // create result type (range)
640   fields = TypeTuple::fields(1);
641   fields[TypeFunc::Parms+0] = Type::FLOAT;
642 
643   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
644 
645   return TypeFunc::make(domain, range);
646 }
647 
Math_D_D_Type()648 const TypeFunc *OptoRuntime::Math_D_D_Type() {
649   // create input type (domain)
650   const Type **fields = TypeTuple::fields(2);
651   // Symbol* name of class to be loaded
652   fields[TypeFunc::Parms+0] = Type::DOUBLE;
653   fields[TypeFunc::Parms+1] = Type::HALF;
654   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2, fields);
655 
656   // create result type (range)
657   fields = TypeTuple::fields(2);
658   fields[TypeFunc::Parms+0] = Type::DOUBLE;
659   fields[TypeFunc::Parms+1] = Type::HALF;
660   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
661 
662   return TypeFunc::make(domain, range);
663 }
664 
Math_DD_D_Type()665 const TypeFunc* OptoRuntime::Math_DD_D_Type() {
666   const Type **fields = TypeTuple::fields(4);
667   fields[TypeFunc::Parms+0] = Type::DOUBLE;
668   fields[TypeFunc::Parms+1] = Type::HALF;
669   fields[TypeFunc::Parms+2] = Type::DOUBLE;
670   fields[TypeFunc::Parms+3] = Type::HALF;
671   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+4, fields);
672 
673   // create result type (range)
674   fields = TypeTuple::fields(2);
675   fields[TypeFunc::Parms+0] = Type::DOUBLE;
676   fields[TypeFunc::Parms+1] = Type::HALF;
677   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
678 
679   return TypeFunc::make(domain, range);
680 }
681 
682 //-------------- currentTimeMillis, currentTimeNanos, etc
683 
void_long_Type()684 const TypeFunc* OptoRuntime::void_long_Type() {
685   // create input type (domain)
686   const Type **fields = TypeTuple::fields(0);
687   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+0, fields);
688 
689   // create result type (range)
690   fields = TypeTuple::fields(2);
691   fields[TypeFunc::Parms+0] = TypeLong::LONG;
692   fields[TypeFunc::Parms+1] = Type::HALF;
693   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+2, fields);
694 
695   return TypeFunc::make(domain, range);
696 }
697 
698 // arraycopy stub variations:
699 enum ArrayCopyType {
700   ac_fast,                      // void(ptr, ptr, size_t)
701   ac_checkcast,                 //  int(ptr, ptr, size_t, size_t, ptr)
702   ac_slow,                      // void(ptr, int, ptr, int, int)
703   ac_generic                    //  int(ptr, int, ptr, int, int)
704 };
705 
make_arraycopy_Type(ArrayCopyType act)706 static const TypeFunc* make_arraycopy_Type(ArrayCopyType act) {
707   // create input type (domain)
708   int num_args      = (act == ac_fast ? 3 : 5);
709   int num_size_args = (act == ac_fast ? 1 : act == ac_checkcast ? 2 : 0);
710   int argcnt = num_args;
711   LP64_ONLY(argcnt += num_size_args); // halfwords for lengths
712   const Type** fields = TypeTuple::fields(argcnt);
713   int argp = TypeFunc::Parms;
714   fields[argp++] = TypePtr::NOTNULL;    // src
715   if (num_size_args == 0) {
716     fields[argp++] = TypeInt::INT;      // src_pos
717   }
718   fields[argp++] = TypePtr::NOTNULL;    // dest
719   if (num_size_args == 0) {
720     fields[argp++] = TypeInt::INT;      // dest_pos
721     fields[argp++] = TypeInt::INT;      // length
722   }
723   while (num_size_args-- > 0) {
724     fields[argp++] = TypeX_X;               // size in whatevers (size_t)
725     LP64_ONLY(fields[argp++] = Type::HALF); // other half of long length
726   }
727   if (act == ac_checkcast) {
728     fields[argp++] = TypePtr::NOTNULL;  // super_klass
729   }
730   assert(argp == TypeFunc::Parms+argcnt, "correct decoding of act");
731   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
732 
733   // create result type if needed
734   int retcnt = (act == ac_checkcast || act == ac_generic ? 1 : 0);
735   fields = TypeTuple::fields(1);
736   if (retcnt == 0)
737     fields[TypeFunc::Parms+0] = NULL; // void
738   else
739     fields[TypeFunc::Parms+0] = TypeInt::INT; // status result, if needed
740   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+retcnt, fields);
741   return TypeFunc::make(domain, range);
742 }
743 
fast_arraycopy_Type()744 const TypeFunc* OptoRuntime::fast_arraycopy_Type() {
745   // This signature is simple:  Two base pointers and a size_t.
746   return make_arraycopy_Type(ac_fast);
747 }
748 
checkcast_arraycopy_Type()749 const TypeFunc* OptoRuntime::checkcast_arraycopy_Type() {
750   // An extension of fast_arraycopy_Type which adds type checking.
751   return make_arraycopy_Type(ac_checkcast);
752 }
753 
slow_arraycopy_Type()754 const TypeFunc* OptoRuntime::slow_arraycopy_Type() {
755   // This signature is exactly the same as System.arraycopy.
756   // There are no intptr_t (int/long) arguments.
757   return make_arraycopy_Type(ac_slow);
758 }
759 
generic_arraycopy_Type()760 const TypeFunc* OptoRuntime::generic_arraycopy_Type() {
761   // This signature is like System.arraycopy, except that it returns status.
762   return make_arraycopy_Type(ac_generic);
763 }
764 
765 
array_fill_Type()766 const TypeFunc* OptoRuntime::array_fill_Type() {
767   const Type** fields;
768   int argp = TypeFunc::Parms;
769   // create input type (domain): pointer, int, size_t
770   fields = TypeTuple::fields(3 LP64_ONLY( + 1));
771   fields[argp++] = TypePtr::NOTNULL;
772   fields[argp++] = TypeInt::INT;
773   fields[argp++] = TypeX_X;               // size in whatevers (size_t)
774   LP64_ONLY(fields[argp++] = Type::HALF); // other half of long length
775   const TypeTuple *domain = TypeTuple::make(argp, fields);
776 
777   // create result type
778   fields = TypeTuple::fields(1);
779   fields[TypeFunc::Parms+0] = NULL; // void
780   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
781 
782   return TypeFunc::make(domain, range);
783 }
784 
785 // for aescrypt encrypt/decrypt operations, just three pointers returning void (length is constant)
aescrypt_block_Type()786 const TypeFunc* OptoRuntime::aescrypt_block_Type() {
787   // create input type (domain)
788   int num_args      = 3;
789   int argcnt = num_args;
790   const Type** fields = TypeTuple::fields(argcnt);
791   int argp = TypeFunc::Parms;
792   fields[argp++] = TypePtr::NOTNULL;    // src
793   fields[argp++] = TypePtr::NOTNULL;    // dest
794   fields[argp++] = TypePtr::NOTNULL;    // k array
795   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
796   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
797 
798   // no result type needed
799   fields = TypeTuple::fields(1);
800   fields[TypeFunc::Parms+0] = NULL; // void
801   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
802   return TypeFunc::make(domain, range);
803 }
804 
805 /**
806  * int updateBytesCRC32(int crc, byte* b, int len)
807  */
updateBytesCRC32_Type()808 const TypeFunc* OptoRuntime::updateBytesCRC32_Type() {
809   // create input type (domain)
810   int num_args      = 3;
811   int argcnt = num_args;
812   const Type** fields = TypeTuple::fields(argcnt);
813   int argp = TypeFunc::Parms;
814   fields[argp++] = TypeInt::INT;        // crc
815   fields[argp++] = TypePtr::NOTNULL;    // src
816   fields[argp++] = TypeInt::INT;        // len
817   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
818   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
819 
820   // result type needed
821   fields = TypeTuple::fields(1);
822   fields[TypeFunc::Parms+0] = TypeInt::INT; // crc result
823   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
824   return TypeFunc::make(domain, range);
825 }
826 
827 /**
828  * int updateBytesCRC32C(int crc, byte* buf, int len, int* table)
829  */
updateBytesCRC32C_Type()830 const TypeFunc* OptoRuntime::updateBytesCRC32C_Type() {
831   // create input type (domain)
832   int num_args      = 4;
833   int argcnt = num_args;
834   const Type** fields = TypeTuple::fields(argcnt);
835   int argp = TypeFunc::Parms;
836   fields[argp++] = TypeInt::INT;        // crc
837   fields[argp++] = TypePtr::NOTNULL;    // buf
838   fields[argp++] = TypeInt::INT;        // len
839   fields[argp++] = TypePtr::NOTNULL;    // table
840   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
841   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
842 
843   // result type needed
844   fields = TypeTuple::fields(1);
845   fields[TypeFunc::Parms+0] = TypeInt::INT; // crc result
846   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
847   return TypeFunc::make(domain, range);
848 }
849 
850 /**
851 *  int updateBytesAdler32(int adler, bytes* b, int off, int len)
852 */
updateBytesAdler32_Type()853 const TypeFunc* OptoRuntime::updateBytesAdler32_Type() {
854   // create input type (domain)
855   int num_args      = 3;
856   int argcnt = num_args;
857   const Type** fields = TypeTuple::fields(argcnt);
858   int argp = TypeFunc::Parms;
859   fields[argp++] = TypeInt::INT;        // crc
860   fields[argp++] = TypePtr::NOTNULL;    // src + offset
861   fields[argp++] = TypeInt::INT;        // len
862   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
863   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
864 
865   // result type needed
866   fields = TypeTuple::fields(1);
867   fields[TypeFunc::Parms+0] = TypeInt::INT; // crc result
868   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
869   return TypeFunc::make(domain, range);
870 }
871 
872 // for cipherBlockChaining calls of aescrypt encrypt/decrypt, four pointers and a length, returning int
cipherBlockChaining_aescrypt_Type()873 const TypeFunc* OptoRuntime::cipherBlockChaining_aescrypt_Type() {
874   // create input type (domain)
875   int num_args      = 5;
876   int argcnt = num_args;
877   const Type** fields = TypeTuple::fields(argcnt);
878   int argp = TypeFunc::Parms;
879   fields[argp++] = TypePtr::NOTNULL;    // src
880   fields[argp++] = TypePtr::NOTNULL;    // dest
881   fields[argp++] = TypePtr::NOTNULL;    // k array
882   fields[argp++] = TypePtr::NOTNULL;    // r array
883   fields[argp++] = TypeInt::INT;        // src len
884   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
885   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
886 
887   // returning cipher len (int)
888   fields = TypeTuple::fields(1);
889   fields[TypeFunc::Parms+0] = TypeInt::INT;
890   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
891   return TypeFunc::make(domain, range);
892 }
893 
894 // for electronicCodeBook calls of aescrypt encrypt/decrypt, three pointers and a length, returning int
electronicCodeBook_aescrypt_Type()895 const TypeFunc* OptoRuntime::electronicCodeBook_aescrypt_Type() {
896   // create input type (domain)
897   int num_args = 4;
898   int argcnt = num_args;
899   const Type** fields = TypeTuple::fields(argcnt);
900   int argp = TypeFunc::Parms;
901   fields[argp++] = TypePtr::NOTNULL;    // src
902   fields[argp++] = TypePtr::NOTNULL;    // dest
903   fields[argp++] = TypePtr::NOTNULL;    // k array
904   fields[argp++] = TypeInt::INT;        // src len
905   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
906   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
907 
908   // returning cipher len (int)
909   fields = TypeTuple::fields(1);
910   fields[TypeFunc::Parms + 0] = TypeInt::INT;
911   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
912   return TypeFunc::make(domain, range);
913 }
914 
915 //for counterMode calls of aescrypt encrypt/decrypt, four pointers and a length, returning int
counterMode_aescrypt_Type()916 const TypeFunc* OptoRuntime::counterMode_aescrypt_Type() {
917   // create input type (domain)
918   int num_args = 7;
919   int argcnt = num_args;
920   const Type** fields = TypeTuple::fields(argcnt);
921   int argp = TypeFunc::Parms;
922   fields[argp++] = TypePtr::NOTNULL; // src
923   fields[argp++] = TypePtr::NOTNULL; // dest
924   fields[argp++] = TypePtr::NOTNULL; // k array
925   fields[argp++] = TypePtr::NOTNULL; // counter array
926   fields[argp++] = TypeInt::INT; // src len
927   fields[argp++] = TypePtr::NOTNULL; // saved_encCounter
928   fields[argp++] = TypePtr::NOTNULL; // saved used addr
929   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
930   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
931   // returning cipher len (int)
932   fields = TypeTuple::fields(1);
933   fields[TypeFunc::Parms + 0] = TypeInt::INT;
934   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
935   return TypeFunc::make(domain, range);
936 }
937 
938 /*
939  * void implCompress(byte[] buf, int ofs)
940  */
digestBase_implCompress_Type(bool is_sha3)941 const TypeFunc* OptoRuntime::digestBase_implCompress_Type(bool is_sha3) {
942   // create input type (domain)
943   int num_args = is_sha3 ? 3 : 2;
944   int argcnt = num_args;
945   const Type** fields = TypeTuple::fields(argcnt);
946   int argp = TypeFunc::Parms;
947   fields[argp++] = TypePtr::NOTNULL; // buf
948   fields[argp++] = TypePtr::NOTNULL; // state
949   if (is_sha3) fields[argp++] = TypeInt::INT; // digest_length
950   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
951   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
952 
953   // no result type needed
954   fields = TypeTuple::fields(1);
955   fields[TypeFunc::Parms+0] = NULL; // void
956   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
957   return TypeFunc::make(domain, range);
958 }
959 
960 /*
961  * int implCompressMultiBlock(byte[] b, int ofs, int limit)
962  */
digestBase_implCompressMB_Type(bool is_sha3)963 const TypeFunc* OptoRuntime::digestBase_implCompressMB_Type(bool is_sha3) {
964   // create input type (domain)
965   int num_args = is_sha3 ? 5 : 4;
966   int argcnt = num_args;
967   const Type** fields = TypeTuple::fields(argcnt);
968   int argp = TypeFunc::Parms;
969   fields[argp++] = TypePtr::NOTNULL; // buf
970   fields[argp++] = TypePtr::NOTNULL; // state
971   if (is_sha3) fields[argp++] = TypeInt::INT; // digest_length
972   fields[argp++] = TypeInt::INT;     // ofs
973   fields[argp++] = TypeInt::INT;     // limit
974   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
975   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
976 
977   // returning ofs (int)
978   fields = TypeTuple::fields(1);
979   fields[TypeFunc::Parms+0] = TypeInt::INT; // ofs
980   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
981   return TypeFunc::make(domain, range);
982 }
983 
multiplyToLen_Type()984 const TypeFunc* OptoRuntime::multiplyToLen_Type() {
985   // create input type (domain)
986   int num_args      = 6;
987   int argcnt = num_args;
988   const Type** fields = TypeTuple::fields(argcnt);
989   int argp = TypeFunc::Parms;
990   fields[argp++] = TypePtr::NOTNULL;    // x
991   fields[argp++] = TypeInt::INT;        // xlen
992   fields[argp++] = TypePtr::NOTNULL;    // y
993   fields[argp++] = TypeInt::INT;        // ylen
994   fields[argp++] = TypePtr::NOTNULL;    // z
995   fields[argp++] = TypeInt::INT;        // zlen
996   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
997   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
998 
999   // no result type needed
1000   fields = TypeTuple::fields(1);
1001   fields[TypeFunc::Parms+0] = NULL;
1002   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1003   return TypeFunc::make(domain, range);
1004 }
1005 
squareToLen_Type()1006 const TypeFunc* OptoRuntime::squareToLen_Type() {
1007   // create input type (domain)
1008   int num_args      = 4;
1009   int argcnt = num_args;
1010   const Type** fields = TypeTuple::fields(argcnt);
1011   int argp = TypeFunc::Parms;
1012   fields[argp++] = TypePtr::NOTNULL;    // x
1013   fields[argp++] = TypeInt::INT;        // len
1014   fields[argp++] = TypePtr::NOTNULL;    // z
1015   fields[argp++] = TypeInt::INT;        // zlen
1016   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1017   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1018 
1019   // no result type needed
1020   fields = TypeTuple::fields(1);
1021   fields[TypeFunc::Parms+0] = NULL;
1022   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1023   return TypeFunc::make(domain, range);
1024 }
1025 
1026 // for mulAdd calls, 2 pointers and 3 ints, returning int
mulAdd_Type()1027 const TypeFunc* OptoRuntime::mulAdd_Type() {
1028   // create input type (domain)
1029   int num_args      = 5;
1030   int argcnt = num_args;
1031   const Type** fields = TypeTuple::fields(argcnt);
1032   int argp = TypeFunc::Parms;
1033   fields[argp++] = TypePtr::NOTNULL;    // out
1034   fields[argp++] = TypePtr::NOTNULL;    // in
1035   fields[argp++] = TypeInt::INT;        // offset
1036   fields[argp++] = TypeInt::INT;        // len
1037   fields[argp++] = TypeInt::INT;        // k
1038   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1039   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1040 
1041   // returning carry (int)
1042   fields = TypeTuple::fields(1);
1043   fields[TypeFunc::Parms+0] = TypeInt::INT;
1044   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms+1, fields);
1045   return TypeFunc::make(domain, range);
1046 }
1047 
montgomeryMultiply_Type()1048 const TypeFunc* OptoRuntime::montgomeryMultiply_Type() {
1049   // create input type (domain)
1050   int num_args      = 7;
1051   int argcnt = num_args;
1052   const Type** fields = TypeTuple::fields(argcnt);
1053   int argp = TypeFunc::Parms;
1054   fields[argp++] = TypePtr::NOTNULL;    // a
1055   fields[argp++] = TypePtr::NOTNULL;    // b
1056   fields[argp++] = TypePtr::NOTNULL;    // n
1057   fields[argp++] = TypeInt::INT;        // len
1058   fields[argp++] = TypeLong::LONG;      // inv
1059   fields[argp++] = Type::HALF;
1060   fields[argp++] = TypePtr::NOTNULL;    // result
1061   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1062   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1063 
1064   // result type needed
1065   fields = TypeTuple::fields(1);
1066   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1067 
1068   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1069   return TypeFunc::make(domain, range);
1070 }
1071 
montgomerySquare_Type()1072 const TypeFunc* OptoRuntime::montgomerySquare_Type() {
1073   // create input type (domain)
1074   int num_args      = 6;
1075   int argcnt = num_args;
1076   const Type** fields = TypeTuple::fields(argcnt);
1077   int argp = TypeFunc::Parms;
1078   fields[argp++] = TypePtr::NOTNULL;    // a
1079   fields[argp++] = TypePtr::NOTNULL;    // n
1080   fields[argp++] = TypeInt::INT;        // len
1081   fields[argp++] = TypeLong::LONG;      // inv
1082   fields[argp++] = Type::HALF;
1083   fields[argp++] = TypePtr::NOTNULL;    // result
1084   assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1085   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1086 
1087   // result type needed
1088   fields = TypeTuple::fields(1);
1089   fields[TypeFunc::Parms+0] = TypePtr::NOTNULL;
1090 
1091   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1092   return TypeFunc::make(domain, range);
1093 }
1094 
bigIntegerShift_Type()1095 const TypeFunc * OptoRuntime::bigIntegerShift_Type() {
1096   int argcnt = 5;
1097   const Type** fields = TypeTuple::fields(argcnt);
1098   int argp = TypeFunc::Parms;
1099   fields[argp++] = TypePtr::NOTNULL;    // newArr
1100   fields[argp++] = TypePtr::NOTNULL;    // oldArr
1101   fields[argp++] = TypeInt::INT;        // newIdx
1102   fields[argp++] = TypeInt::INT;        // shiftCount
1103   fields[argp++] = TypeInt::INT;        // numIter
1104   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1105   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1106 
1107   // no result type needed
1108   fields = TypeTuple::fields(1);
1109   fields[TypeFunc::Parms + 0] = NULL;
1110   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1111   return TypeFunc::make(domain, range);
1112 }
1113 
vectorizedMismatch_Type()1114 const TypeFunc* OptoRuntime::vectorizedMismatch_Type() {
1115   // create input type (domain)
1116   int num_args = 4;
1117   int argcnt = num_args;
1118   const Type** fields = TypeTuple::fields(argcnt);
1119   int argp = TypeFunc::Parms;
1120   fields[argp++] = TypePtr::NOTNULL;    // obja
1121   fields[argp++] = TypePtr::NOTNULL;    // objb
1122   fields[argp++] = TypeInt::INT;        // length, number of elements
1123   fields[argp++] = TypeInt::INT;        // log2scale, element size
1124   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1125   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms + argcnt, fields);
1126 
1127   //return mismatch index (int)
1128   fields = TypeTuple::fields(1);
1129   fields[TypeFunc::Parms + 0] = TypeInt::INT;
1130   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1131   return TypeFunc::make(domain, range);
1132 }
1133 
1134 // GHASH block processing
ghash_processBlocks_Type()1135 const TypeFunc* OptoRuntime::ghash_processBlocks_Type() {
1136     int argcnt = 4;
1137 
1138     const Type** fields = TypeTuple::fields(argcnt);
1139     int argp = TypeFunc::Parms;
1140     fields[argp++] = TypePtr::NOTNULL;    // state
1141     fields[argp++] = TypePtr::NOTNULL;    // subkeyH
1142     fields[argp++] = TypePtr::NOTNULL;    // data
1143     fields[argp++] = TypeInt::INT;        // blocks
1144     assert(argp == TypeFunc::Parms+argcnt, "correct decoding");
1145     const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1146 
1147     // result type needed
1148     fields = TypeTuple::fields(1);
1149     fields[TypeFunc::Parms+0] = NULL; // void
1150     const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1151     return TypeFunc::make(domain, range);
1152 }
1153 // Base64 encode function
base64_encodeBlock_Type()1154 const TypeFunc* OptoRuntime::base64_encodeBlock_Type() {
1155   int argcnt = 6;
1156 
1157   const Type** fields = TypeTuple::fields(argcnt);
1158   int argp = TypeFunc::Parms;
1159   fields[argp++] = TypePtr::NOTNULL;    // src array
1160   fields[argp++] = TypeInt::INT;        // offset
1161   fields[argp++] = TypeInt::INT;        // length
1162   fields[argp++] = TypePtr::NOTNULL;    // dest array
1163   fields[argp++] = TypeInt::INT;       // dp
1164   fields[argp++] = TypeInt::BOOL;       // isURL
1165   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1166   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1167 
1168   // result type needed
1169   fields = TypeTuple::fields(1);
1170   fields[TypeFunc::Parms + 0] = NULL; // void
1171   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms, fields);
1172   return TypeFunc::make(domain, range);
1173 }
1174 // Base64 decode function
base64_decodeBlock_Type()1175 const TypeFunc* OptoRuntime::base64_decodeBlock_Type() {
1176   int argcnt = 6;
1177 
1178   const Type** fields = TypeTuple::fields(argcnt);
1179   int argp = TypeFunc::Parms;
1180   fields[argp++] = TypePtr::NOTNULL;    // src array
1181   fields[argp++] = TypeInt::INT;        // src offset
1182   fields[argp++] = TypeInt::INT;        // src length
1183   fields[argp++] = TypePtr::NOTNULL;    // dest array
1184   fields[argp++] = TypeInt::INT;        // dest offset
1185   fields[argp++] = TypeInt::BOOL;       // isURL
1186   assert(argp == TypeFunc::Parms + argcnt, "correct decoding");
1187   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+argcnt, fields);
1188 
1189   // result type needed
1190   fields = TypeTuple::fields(1);
1191   fields[TypeFunc::Parms + 0] = TypeInt::INT; // count of bytes written to dst
1192   const TypeTuple* range = TypeTuple::make(TypeFunc::Parms + 1, fields);
1193   return TypeFunc::make(domain, range);
1194 }
1195 
1196 //------------- Interpreter state access for on stack replacement
osr_end_Type()1197 const TypeFunc* OptoRuntime::osr_end_Type() {
1198   // create input type (domain)
1199   const Type **fields = TypeTuple::fields(1);
1200   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // OSR temp buf
1201   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1202 
1203   // create result type
1204   fields = TypeTuple::fields(1);
1205   // fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // locked oop
1206   fields[TypeFunc::Parms+0] = NULL; // void
1207   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms, fields);
1208   return TypeFunc::make(domain, range);
1209 }
1210 
1211 //-------------------------------------------------------------------------------------
1212 // register policy
1213 
is_callee_saved_register(MachRegisterNumbers reg)1214 bool OptoRuntime::is_callee_saved_register(MachRegisterNumbers reg) {
1215   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1216   switch (register_save_policy[reg]) {
1217     case 'C': return false; //SOC
1218     case 'E': return true ; //SOE
1219     case 'N': return false; //NS
1220     case 'A': return false; //AS
1221   }
1222   ShouldNotReachHere();
1223   return false;
1224 }
1225 
1226 //-----------------------------------------------------------------------
1227 // Exceptions
1228 //
1229 
1230 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1231 
1232 // The method is an entry that is always called by a C++ method not
1233 // directly from compiled code. Compiled code will call the C++ method following.
1234 // We can't allow async exception to be installed during  exception processing.
1235 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* thread, nmethod* &nm))
1236   // Do not confuse exception_oop with pending_exception. The exception_oop
1237   // is only used to pass arguments into the method. Not for general
1238   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1239   // the runtime stubs checks this on exit.
1240   assert(thread->exception_oop() != NULL, "exception oop is found");
1241   address handler_address = NULL;
1242 
1243   Handle exception(thread, thread->exception_oop());
1244   address pc = thread->exception_pc();
1245 
1246   // Clear out the exception oop and pc since looking up an
1247   // exception handler can cause class loading, which might throw an
1248   // exception and those fields are expected to be clear during
1249   // normal bytecode execution.
1250   thread->clear_exception_oop_and_pc();
1251 
1252   LogTarget(Info, exceptions) lt;
1253   if (lt.is_enabled()) {
1254     ResourceMark rm;
1255     LogStream ls(lt);
1256     trace_exception(&ls, exception(), pc, "");
1257   }
1258 
1259   // for AbortVMOnException flag
1260   Exceptions::debug_check_abort(exception);
1261 
1262 #ifdef ASSERT
1263   if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
1264     // should throw an exception here
1265     ShouldNotReachHere();
1266   }
1267 #endif
1268 
1269   // new exception handling: this method is entered only from adapters
1270   // exceptions from compiled java methods are handled in compiled code
1271   // using rethrow node
1272 
1273   nm = CodeCache::find_nmethod(pc);
1274   assert(nm != NULL, "No NMethod found");
1275   if (nm->is_native_method()) {
1276     fatal("Native method should not have path to exception handling");
1277   } else {
1278     // we are switching to old paradigm: search for exception handler in caller_frame
1279     // instead in exception handler of caller_frame.sender()
1280 
1281     if (JvmtiExport::can_post_on_exceptions()) {
1282       // "Full-speed catching" is not necessary here,
1283       // since we're notifying the VM on every catch.
1284       // Force deoptimization and the rest of the lookup
1285       // will be fine.
1286       deoptimize_caller_frame(thread);
1287     }
1288 
1289     // Check the stack guard pages.  If enabled, look for handler in this frame;
1290     // otherwise, forcibly unwind the frame.
1291     //
1292     // 4826555: use default current sp for reguard_stack instead of &nm: it's more accurate.
1293     bool force_unwind = !thread->stack_overflow_state()->reguard_stack();
1294     bool deopting = false;
1295     if (nm->is_deopt_pc(pc)) {
1296       deopting = true;
1297       RegisterMap map(thread, false);
1298       frame deoptee = thread->last_frame().sender(&map);
1299       assert(deoptee.is_deoptimized_frame(), "must be deopted");
1300       // Adjust the pc back to the original throwing pc
1301       pc = deoptee.pc();
1302     }
1303 
1304     // If we are forcing an unwind because of stack overflow then deopt is
1305     // irrelevant since we are throwing the frame away anyway.
1306 
1307     if (deopting && !force_unwind) {
1308       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1309     } else {
1310 
1311       handler_address =
1312         force_unwind ? NULL : nm->handler_for_exception_and_pc(exception, pc);
1313 
1314       if (handler_address == NULL) {
1315         bool recursive_exception = false;
1316         handler_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1317         assert (handler_address != NULL, "must have compiled handler");
1318         // Update the exception cache only when the unwind was not forced
1319         // and there didn't happen another exception during the computation of the
1320         // compiled exception handler. Checking for exception oop equality is not
1321         // sufficient because some exceptions are pre-allocated and reused.
1322         if (!force_unwind && !recursive_exception) {
1323           nm->add_handler_for_exception_and_pc(exception,pc,handler_address);
1324         }
1325       } else {
1326 #ifdef ASSERT
1327         bool recursive_exception = false;
1328         address computed_address = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, force_unwind, true, recursive_exception);
1329         vmassert(recursive_exception || (handler_address == computed_address), "Handler address inconsistency: " PTR_FORMAT " != " PTR_FORMAT,
1330                  p2i(handler_address), p2i(computed_address));
1331 #endif
1332       }
1333     }
1334 
1335     thread->set_exception_pc(pc);
1336     thread->set_exception_handler_pc(handler_address);
1337 
1338     // Check if the exception PC is a MethodHandle call site.
1339     thread->set_is_method_handle_return(nm->is_method_handle_return(pc));
1340   }
1341 
1342   // Restore correct return pc.  Was saved above.
1343   thread->set_exception_oop(exception());
1344   return handler_address;
1345 
1346 JRT_END
1347 
1348 // We are entering here from exception_blob
1349 // If there is a compiled exception handler in this method, we will continue there;
1350 // otherwise we will unwind the stack and continue at the caller of top frame method
1351 // Note we enter without the usual JRT wrapper. We will call a helper routine that
1352 // will do the normal VM entry. We do it this way so that we can see if the nmethod
1353 // we looked up the handler for has been deoptimized in the meantime. If it has been
1354 // we must not use the handler and instead return the deopt blob.
handle_exception_C(JavaThread * thread)1355 address OptoRuntime::handle_exception_C(JavaThread* thread) {
1356 //
1357 // We are in Java not VM and in debug mode we have a NoHandleMark
1358 //
1359 #ifndef PRODUCT
1360   SharedRuntime::_find_handler_ctr++;          // find exception handler
1361 #endif
1362   debug_only(NoHandleMark __hm;)
1363   nmethod* nm = NULL;
1364   address handler_address = NULL;
1365   {
1366     // Enter the VM
1367 
1368     ResetNoHandleMark rnhm;
1369     handler_address = handle_exception_C_helper(thread, nm);
1370   }
1371 
1372   // Back in java: Use no oops, DON'T safepoint
1373 
1374   // Now check to see if the handler we are returning is in a now
1375   // deoptimized frame
1376 
1377   if (nm != NULL) {
1378     RegisterMap map(thread, false);
1379     frame caller = thread->last_frame().sender(&map);
1380 #ifdef ASSERT
1381     assert(caller.is_compiled_frame(), "must be");
1382 #endif // ASSERT
1383     if (caller.is_deoptimized_frame()) {
1384       handler_address = SharedRuntime::deopt_blob()->unpack_with_exception();
1385     }
1386   }
1387   return handler_address;
1388 }
1389 
1390 //------------------------------rethrow----------------------------------------
1391 // We get here after compiled code has executed a 'RethrowNode'.  The callee
1392 // is either throwing or rethrowing an exception.  The callee-save registers
1393 // have been restored, synchronized objects have been unlocked and the callee
1394 // stack frame has been removed.  The return address was passed in.
1395 // Exception oop is passed as the 1st argument.  This routine is then called
1396 // from the stub.  On exit, we know where to jump in the caller's code.
1397 // After this C code exits, the stub will pop his frame and end in a jump
1398 // (instead of a return).  We enter the caller's default handler.
1399 //
1400 // This must be JRT_LEAF:
1401 //     - caller will not change its state as we cannot block on exit,
1402 //       therefore raw_exception_handler_for_return_address is all it takes
1403 //       to handle deoptimized blobs
1404 //
1405 // However, there needs to be a safepoint check in the middle!  So compiled
1406 // safepoints are completely watertight.
1407 //
1408 // Thus, it cannot be a leaf since it contains the NoSafepointVerifier.
1409 //
1410 // *THIS IS NOT RECOMMENDED PROGRAMMING STYLE*
1411 //
rethrow_C(oopDesc * exception,JavaThread * thread,address ret_pc)1412 address OptoRuntime::rethrow_C(oopDesc* exception, JavaThread* thread, address ret_pc) {
1413   // The frame we rethrow the exception to might not have been processed by the GC yet.
1414   // The stack watermark barrier takes care of detecting that and ensuring the frame
1415   // has updated oops.
1416   StackWatermarkSet::after_unwind(thread);
1417 
1418 #ifndef PRODUCT
1419   SharedRuntime::_rethrow_ctr++;               // count rethrows
1420 #endif
1421   assert (exception != NULL, "should have thrown a NULLPointerException");
1422 #ifdef ASSERT
1423   if (!(exception->is_a(SystemDictionary::Throwable_klass()))) {
1424     // should throw an exception here
1425     ShouldNotReachHere();
1426   }
1427 #endif
1428 
1429   thread->set_vm_result(exception);
1430   // Frame not compiled (handles deoptimization blob)
1431   return SharedRuntime::raw_exception_handler_for_return_address(thread, ret_pc);
1432 }
1433 
1434 
rethrow_Type()1435 const TypeFunc *OptoRuntime::rethrow_Type() {
1436   // create input type (domain)
1437   const Type **fields = TypeTuple::fields(1);
1438   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1439   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1440 
1441   // create result type (range)
1442   fields = TypeTuple::fields(1);
1443   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // Exception oop
1444   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+1, fields);
1445 
1446   return TypeFunc::make(domain, range);
1447 }
1448 
1449 
deoptimize_caller_frame(JavaThread * thread,bool doit)1450 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread, bool doit) {
1451   // Deoptimize the caller before continuing, as the compiled
1452   // exception handler table may not be valid.
1453   if (!StressCompiledExceptionHandlers && doit) {
1454     deoptimize_caller_frame(thread);
1455   }
1456 }
1457 
deoptimize_caller_frame(JavaThread * thread)1458 void OptoRuntime::deoptimize_caller_frame(JavaThread *thread) {
1459   // Called from within the owner thread, so no need for safepoint
1460   RegisterMap reg_map(thread);
1461   frame stub_frame = thread->last_frame();
1462   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1463   frame caller_frame = stub_frame.sender(&reg_map);
1464 
1465   // Deoptimize the caller frame.
1466   Deoptimization::deoptimize_frame(thread, caller_frame.id());
1467 }
1468 
1469 
is_deoptimized_caller_frame(JavaThread * thread)1470 bool OptoRuntime::is_deoptimized_caller_frame(JavaThread *thread) {
1471   // Called from within the owner thread, so no need for safepoint
1472   RegisterMap reg_map(thread);
1473   frame stub_frame = thread->last_frame();
1474   assert(stub_frame.is_runtime_frame() || exception_blob()->contains(stub_frame.pc()), "sanity check");
1475   frame caller_frame = stub_frame.sender(&reg_map);
1476   return caller_frame.is_deoptimized_frame();
1477 }
1478 
1479 
register_finalizer_Type()1480 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1481   // create input type (domain)
1482   const Type **fields = TypeTuple::fields(1);
1483   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1484   // // The JavaThread* is passed to each routine as the last argument
1485   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1486   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1487 
1488   // create result type (range)
1489   fields = TypeTuple::fields(0);
1490 
1491   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1492 
1493   return TypeFunc::make(domain,range);
1494 }
1495 
1496 
1497 //-----------------------------------------------------------------------------
1498 // Dtrace support.  entry and exit probes have the same signature
dtrace_method_entry_exit_Type()1499 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1500   // create input type (domain)
1501   const Type **fields = TypeTuple::fields(2);
1502   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1503   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
1504   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1505 
1506   // create result type (range)
1507   fields = TypeTuple::fields(0);
1508 
1509   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1510 
1511   return TypeFunc::make(domain,range);
1512 }
1513 
dtrace_object_alloc_Type()1514 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1515   // create input type (domain)
1516   const Type **fields = TypeTuple::fields(2);
1517   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1518   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1519 
1520   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1521 
1522   // create result type (range)
1523   fields = TypeTuple::fields(0);
1524 
1525   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1526 
1527   return TypeFunc::make(domain,range);
1528 }
1529 
1530 
1531 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* thread))
1532   assert(oopDesc::is_oop(obj), "must be a valid oop");
1533   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1534   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1535 JRT_END
1536 
1537 //-----------------------------------------------------------------------------
1538 
1539 NamedCounter * volatile OptoRuntime::_named_counters = NULL;
1540 
1541 //
1542 // dump the collected NamedCounters.
1543 //
print_named_counters()1544 void OptoRuntime::print_named_counters() {
1545   int total_lock_count = 0;
1546   int eliminated_lock_count = 0;
1547 
1548   NamedCounter* c = _named_counters;
1549   while (c) {
1550     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1551       int count = c->count();
1552       if (count > 0) {
1553         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1554         if (Verbose) {
1555           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1556         }
1557         total_lock_count += count;
1558         if (eliminated) {
1559           eliminated_lock_count += count;
1560         }
1561       }
1562     } else if (c->tag() == NamedCounter::BiasedLockingCounter) {
1563       BiasedLockingCounters* blc = ((BiasedLockingNamedCounter*)c)->counters();
1564       if (blc->nonzero()) {
1565         tty->print_cr("%s", c->name());
1566         blc->print_on(tty);
1567       }
1568 #if INCLUDE_RTM_OPT
1569     } else if (c->tag() == NamedCounter::RTMLockingCounter) {
1570       RTMLockingCounters* rlc = ((RTMLockingNamedCounter*)c)->counters();
1571       if (rlc->nonzero()) {
1572         tty->print_cr("%s", c->name());
1573         rlc->print_on(tty);
1574       }
1575 #endif
1576     }
1577     c = c->next();
1578   }
1579   if (total_lock_count > 0) {
1580     tty->print_cr("dynamic locks: %d", total_lock_count);
1581     if (eliminated_lock_count) {
1582       tty->print_cr("eliminated locks: %d (%d%%)", eliminated_lock_count,
1583                     (int)(eliminated_lock_count * 100.0 / total_lock_count));
1584     }
1585   }
1586 }
1587 
1588 //
1589 //  Allocate a new NamedCounter.  The JVMState is used to generate the
1590 //  name which consists of method@line for the inlining tree.
1591 //
1592 
new_named_counter(JVMState * youngest_jvms,NamedCounter::CounterTag tag)1593 NamedCounter* OptoRuntime::new_named_counter(JVMState* youngest_jvms, NamedCounter::CounterTag tag) {
1594   int max_depth = youngest_jvms->depth();
1595 
1596   // Visit scopes from youngest to oldest.
1597   bool first = true;
1598   stringStream st;
1599   for (int depth = max_depth; depth >= 1; depth--) {
1600     JVMState* jvms = youngest_jvms->of_depth(depth);
1601     ciMethod* m = jvms->has_method() ? jvms->method() : NULL;
1602     if (!first) {
1603       st.print(" ");
1604     } else {
1605       first = false;
1606     }
1607     int bci = jvms->bci();
1608     if (bci < 0) bci = 0;
1609     if (m != NULL) {
1610       st.print("%s.%s", m->holder()->name()->as_utf8(), m->name()->as_utf8());
1611     } else {
1612       st.print("no method");
1613     }
1614     st.print("@%d", bci);
1615     // To print linenumbers instead of bci use: m->line_number_from_bci(bci)
1616   }
1617   NamedCounter* c;
1618   if (tag == NamedCounter::BiasedLockingCounter) {
1619     c = new BiasedLockingNamedCounter(st.as_string());
1620   } else if (tag == NamedCounter::RTMLockingCounter) {
1621     c = new RTMLockingNamedCounter(st.as_string());
1622   } else {
1623     c = new NamedCounter(st.as_string(), tag);
1624   }
1625 
1626   // atomically add the new counter to the head of the list.  We only
1627   // add counters so this is safe.
1628   NamedCounter* head;
1629   do {
1630     c->set_next(NULL);
1631     head = _named_counters;
1632     c->set_next(head);
1633   } while (Atomic::cmpxchg(&_named_counters, head, c) != head);
1634   return c;
1635 }
1636 
1637 int trace_exception_counter = 0;
trace_exception(outputStream * st,oop exception_oop,address exception_pc,const char * msg)1638 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1639   trace_exception_counter++;
1640   stringStream tempst;
1641 
1642   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1643   exception_oop->print_value_on(&tempst);
1644   tempst.print(" in ");
1645   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1646   if (blob->is_compiled()) {
1647     CompiledMethod* cm = blob->as_compiled_method_or_null();
1648     cm->method()->print_value_on(&tempst);
1649   } else if (blob->is_runtime_stub()) {
1650     tempst.print("<runtime-stub>");
1651   } else {
1652     tempst.print("<unknown>");
1653   }
1654   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1655   tempst.print("]");
1656 
1657   st->print_raw_cr(tempst.as_string());
1658 }
1659