1 /*
  2  * Copyright (c) 1997, 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 #include "precompiled.hpp"
 26 #include "asm/macroAssembler.hpp"
 27 #include "asm/macroAssembler.inline.hpp"
 28 #include "cds/metaspaceShared.hpp"
 29 #include "compiler/disassembler.hpp"
 30 #include "interpreter/bytecodeHistogram.hpp"
 31 #include "interpreter/bytecodeStream.hpp"
 32 #include "interpreter/interpreter.hpp"
 33 #include "interpreter/interpreterRuntime.hpp"
 34 #include "interpreter/interp_masm.hpp"
 35 #include "interpreter/templateTable.hpp"
 36 #include "memory/allocation.inline.hpp"
 37 #include "memory/resourceArea.hpp"
 38 #include "oops/arrayOop.hpp"
 39 #include "oops/constantPool.inline.hpp"
 40 #include "oops/cpCache.inline.hpp"
 41 #include "oops/methodData.hpp"
 42 #include "oops/method.inline.hpp"
 43 #include "oops/oop.inline.hpp"
 44 #include "prims/jvmtiExport.hpp"
 45 #include "prims/methodHandles.hpp"
 46 #include "runtime/handles.inline.hpp"
 47 #include "runtime/sharedRuntime.hpp"
 48 #include "runtime/stubRoutines.hpp"
 49 #include "runtime/timer.hpp"
 50 
 51 # define __ _masm->
 52 
 53 //------------------------------------------------------------------------------------------------------------------------
 54 // Implementation of platform independent aspects of Interpreter
 55 
 56 void AbstractInterpreter::initialize() {
 57   // make sure 'imported' classes are initialized
 58   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) BytecodeCounter::reset();
 59   if (PrintBytecodeHistogram)                                BytecodeHistogram::reset();
 60   if (PrintBytecodePairHistogram)                            BytecodePairHistogram::reset();
 61 }
 62 
 63 void AbstractInterpreter::print() {
 64   tty->cr();
 65   tty->print_cr("----------------------------------------------------------------------");
 66   tty->print_cr("Interpreter");
 67   tty->cr();
 68   tty->print_cr("code size        = %6dK bytes", (int)_code->used_space()/1024);
 69   tty->print_cr("total space      = %6dK bytes", (int)_code->total_space()/1024);
 70   tty->print_cr("wasted space     = %6dK bytes", (int)_code->available_space()/1024);
 71   tty->cr();
 72   tty->print_cr("# of codelets    = %6d"      , _code->number_of_stubs());
 73   if (_code->number_of_stubs() != 0) {
 74     tty->print_cr("avg codelet size = %6d bytes", _code->used_space() / _code->number_of_stubs());
 75     tty->cr();
 76   }
 77   _should_print_instructions = PrintInterpreter;
 78   _code->print();
 79   _should_print_instructions = false;
 80   tty->print_cr("----------------------------------------------------------------------");
 81   tty->cr();
 82 }
 83 
 84 
 85 //------------------------------------------------------------------------------------------------------------------------
 86 // Implementation of interpreter
 87 
 88 StubQueue* AbstractInterpreter::_code                                       = nullptr;
 89 bool       AbstractInterpreter::_notice_safepoints                          = false;
 90 address    AbstractInterpreter::_rethrow_exception_entry                    = nullptr;
 91 
 92 address    AbstractInterpreter::_slow_signature_handler;
 93 address    AbstractInterpreter::_entry_table            [AbstractInterpreter::number_of_method_entries];
 94 address    AbstractInterpreter::_native_abi_to_tosca    [AbstractInterpreter::number_of_result_handlers];
 95 
 96 bool       AbstractInterpreter::_should_print_instructions = false;
 97 
 98 //------------------------------------------------------------------------------------------------------------------------
 99 // Generation of complete interpreter
100 
101 AbstractInterpreterGenerator::AbstractInterpreterGenerator() {
102   _masm                      = nullptr;
103 }
104 
105 
106 //------------------------------------------------------------------------------------------------------------------------
107 // Entry points
108 
109 AbstractInterpreter::MethodKind AbstractInterpreter::method_kind(const methodHandle& m) {
110   // Abstract method?
111   if (m->is_abstract()) return abstract;
112 
113   // Method handle primitive?
114   vmIntrinsics::ID iid = m->intrinsic_id();
115   if (iid != vmIntrinsics::_none) {
116     if (m->is_method_handle_intrinsic()) {
117       assert(MethodHandles::is_signature_polymorphic(iid), "must match an intrinsic");
118       MethodKind kind = (MethodKind)(method_handle_invoke_FIRST +
119                                     vmIntrinsics::as_int(iid) -
120                                     static_cast<int>(vmIntrinsics::FIRST_MH_SIG_POLY));
121       assert(kind <= method_handle_invoke_LAST, "parallel enum ranges");
122       return kind;
123     }
124 
125     switch (iid) {
126 #ifndef ZERO
127       // Use optimized stub code for CRC32 native methods.
128       case vmIntrinsics::_updateCRC32:       return java_util_zip_CRC32_update;
129       case vmIntrinsics::_updateBytesCRC32:  return java_util_zip_CRC32_updateBytes;
130       case vmIntrinsics::_updateByteBufferCRC32: return java_util_zip_CRC32_updateByteBuffer;
131       // Use optimized stub code for CRC32C methods.
132       case vmIntrinsics::_updateBytesCRC32C: return java_util_zip_CRC32C_updateBytes;
133       case vmIntrinsics::_updateDirectByteBufferCRC32C: return java_util_zip_CRC32C_updateDirectByteBuffer;
134       case vmIntrinsics::_intBitsToFloat:    return java_lang_Float_intBitsToFloat;
135       case vmIntrinsics::_floatToRawIntBits: return java_lang_Float_floatToRawIntBits;
136       case vmIntrinsics::_longBitsToDouble:  return java_lang_Double_longBitsToDouble;
137       case vmIntrinsics::_doubleToRawLongBits: return java_lang_Double_doubleToRawLongBits;
138       case vmIntrinsics::_float16ToFloat:    return java_lang_Float_float16ToFloat;
139       case vmIntrinsics::_floatToFloat16:    return java_lang_Float_floatToFloat16;
140       case vmIntrinsics::_currentThread:     return java_lang_Thread_currentThread;
141 #endif // ZERO
142       case vmIntrinsics::_dsin:              return java_lang_math_sin;
143       case vmIntrinsics::_dcos:              return java_lang_math_cos;
144       case vmIntrinsics::_dtan:              return java_lang_math_tan;
145       case vmIntrinsics::_dtanh:             return java_lang_math_tanh;
146       case vmIntrinsics::_dabs:              return java_lang_math_abs;
147       case vmIntrinsics::_dlog:              return java_lang_math_log;
148       case vmIntrinsics::_dlog10:            return java_lang_math_log10;
149       case vmIntrinsics::_dpow:              return java_lang_math_pow;
150       case vmIntrinsics::_dexp:              return java_lang_math_exp;
151       case vmIntrinsics::_fmaD:              return java_lang_math_fmaD;
152       case vmIntrinsics::_fmaF:              return java_lang_math_fmaF;
153       case vmIntrinsics::_dsqrt:             return java_lang_math_sqrt;
154       case vmIntrinsics::_dsqrt_strict:      return java_lang_math_sqrt_strict;
155       case vmIntrinsics::_Reference_get:     return java_lang_ref_reference_get;
156       case vmIntrinsics::_Object_init:
157         if (m->code_size() == 1) {
158           // We need to execute the special return bytecode to check for
159           // finalizer registration so create a normal frame.
160           return zerolocals;
161         }
162         break;
163       default: break;
164     }
165   }
166 
167   // Native method?
168   if (m->is_native()) {
169     if (m->is_continuation_native_intrinsic()) {
170       // This entry will never be called.  The real entry gets generated later, like for MH intrinsics.
171       return abstract;
172     }
173     assert(!m->is_method_handle_intrinsic(), "overlapping bits here, watch out");
174     return m->is_synchronized() ? native_synchronized : native;
175   }
176 
177   // Synchronized?
178   if (m->is_synchronized()) {
179     return zerolocals_synchronized;
180   }
181 
182   // Empty method?
183   if (m->is_empty_method()) {
184     return empty;
185   }
186 
187   // Getter method?
188   if (m->is_getter()) {
189     return getter;
190   }
191 
192   // Setter method?
193   if (m->is_setter()) {
194     return setter;
195   }
196 
197   // Note: for now: zero locals for all non-empty methods
198   return zerolocals;
199 }
200 
201 vmIntrinsics::ID AbstractInterpreter::method_intrinsic(MethodKind kind) {
202   switch (kind) {
203   case java_lang_math_sin         : return vmIntrinsics::_dsin;
204   case java_lang_math_cos         : return vmIntrinsics::_dcos;
205   case java_lang_math_tan         : return vmIntrinsics::_dtan;
206   case java_lang_math_tanh        : return vmIntrinsics::_dtanh;
207   case java_lang_math_abs         : return vmIntrinsics::_dabs;
208   case java_lang_math_log         : return vmIntrinsics::_dlog;
209   case java_lang_math_log10       : return vmIntrinsics::_dlog10;
210   case java_lang_math_sqrt        : return vmIntrinsics::_dsqrt;
211   case java_lang_math_sqrt_strict : return vmIntrinsics::_dsqrt_strict;
212   case java_lang_math_pow         : return vmIntrinsics::_dpow;
213   case java_lang_math_exp         : return vmIntrinsics::_dexp;
214   case java_lang_math_fmaD        : return vmIntrinsics::_fmaD;
215   case java_lang_math_fmaF        : return vmIntrinsics::_fmaF;
216   case java_lang_ref_reference_get: return vmIntrinsics::_Reference_get;
217   case java_util_zip_CRC32_update : return vmIntrinsics::_updateCRC32;
218   case java_util_zip_CRC32_updateBytes
219                                   : return vmIntrinsics::_updateBytesCRC32;
220   case java_util_zip_CRC32_updateByteBuffer
221                                   : return vmIntrinsics::_updateByteBufferCRC32;
222   case java_util_zip_CRC32C_updateBytes
223                                   : return vmIntrinsics::_updateBytesCRC32C;
224   case java_util_zip_CRC32C_updateDirectByteBuffer
225                                   : return vmIntrinsics::_updateDirectByteBufferCRC32C;
226   case java_lang_Thread_currentThread
227                                   : return vmIntrinsics::_currentThread;
228   case java_lang_Float_intBitsToFloat
229                                   : return vmIntrinsics::_intBitsToFloat;
230   case java_lang_Float_floatToRawIntBits
231                                   : return vmIntrinsics::_floatToRawIntBits;
232   case java_lang_Double_longBitsToDouble
233                                   : return vmIntrinsics::_longBitsToDouble;
234   case java_lang_Double_doubleToRawLongBits
235                                   : return vmIntrinsics::_doubleToRawLongBits;
236   case java_lang_Float_float16ToFloat
237                                   : return vmIntrinsics::_float16ToFloat;
238   case java_lang_Float_floatToFloat16
239                                   : return vmIntrinsics::_floatToFloat16;
240 
241   default:
242     fatal("unexpected method intrinsic kind: %d", kind);
243     break;
244   }
245   return vmIntrinsics::_none;
246 }
247 
248 void AbstractInterpreter::set_entry_for_kind(MethodKind kind, address entry) {
249   assert(kind >= method_handle_invoke_FIRST &&
250          kind <= method_handle_invoke_LAST, "late initialization only for MH entry points");
251   assert(_entry_table[kind] == _entry_table[abstract], "previous value must be AME entry");
252   _entry_table[kind] = entry;
253 }
254 
255 // Return true if the interpreter can prove that the given bytecode has
256 // not yet been executed (in Java semantics, not in actual operation).
257 bool AbstractInterpreter::is_not_reached(const methodHandle& method, int bci) {
258   BytecodeStream s(method, bci);
259   Bytecodes::Code code = s.next();
260 
261   if (Bytecodes::is_invoke(code)) {
262     assert(!Bytecodes::must_rewrite(code), "invokes aren't rewritten");
263     ConstantPool* cpool = method()->constants();
264 
265     Bytecode invoke_bc(s.bytecode());
266 
267     switch (code) {
268       case Bytecodes::_invokedynamic: {
269         assert(invoke_bc.has_index_u4(code), "sanity");
270         int method_index = invoke_bc.get_index_u4(code);
271         return cpool->resolved_indy_entry_at(method_index)->is_resolved();
272       }
273       case Bytecodes::_invokevirtual:   // fall-through
274       case Bytecodes::_invokeinterface: // fall-through
275       case Bytecodes::_invokespecial:   // fall-through
276       case Bytecodes::_invokestatic: {
277         if (cpool->has_preresolution()) {
278           return false; // might have been reached
279         }
280         assert(!invoke_bc.has_index_u4(code), "sanity");
281         int method_index = invoke_bc.get_index_u2(code);
282         constantPoolHandle cp(Thread::current(), cpool);
283         Method* resolved_method = ConstantPool::method_at_if_loaded(cp, method_index);
284         return (resolved_method == nullptr);
285       }
286       default: ShouldNotReachHere();
287     }
288   } else if (!Bytecodes::must_rewrite(code)) {
289     // might have been reached
290     return false;
291   }
292 
293   // the bytecode might not be rewritten if the method is an accessor, etc.
294   address ientry = method->interpreter_entry();
295   if (ientry != entry_for_kind(AbstractInterpreter::zerolocals) &&
296       ientry != entry_for_kind(AbstractInterpreter::zerolocals_synchronized))
297     return false;  // interpreter does not run this method!
298 
299   // otherwise, we can be sure this bytecode has never been executed
300   return true;
301 }
302 
303 
304 #ifndef PRODUCT
305 void AbstractInterpreter::print_method_kind(MethodKind kind) {
306   switch (kind) {
307     case zerolocals             : tty->print("zerolocals"             ); break;
308     case zerolocals_synchronized: tty->print("zerolocals_synchronized"); break;
309     case native                 : tty->print("native"                 ); break;
310     case native_synchronized    : tty->print("native_synchronized"    ); break;
311     case empty                  : tty->print("empty"                  ); break;
312     case getter                 : tty->print("getter"                 ); break;
313     case setter                 : tty->print("setter"                 ); break;
314     case abstract               : tty->print("abstract"               ); break;
315     case java_lang_math_sin     : tty->print("java_lang_math_sin"     ); break;
316     case java_lang_math_cos     : tty->print("java_lang_math_cos"     ); break;
317     case java_lang_math_tan     : tty->print("java_lang_math_tan"     ); break;
318     case java_lang_math_tanh    : tty->print("java_lang_math_tanh"    ); break;
319     case java_lang_math_abs     : tty->print("java_lang_math_abs"     ); break;
320     case java_lang_math_log     : tty->print("java_lang_math_log"     ); break;
321     case java_lang_math_log10   : tty->print("java_lang_math_log10"   ); break;
322     case java_lang_math_pow     : tty->print("java_lang_math_pow"     ); break;
323     case java_lang_math_exp     : tty->print("java_lang_math_exp"     ); break;
324     case java_lang_math_fmaD    : tty->print("java_lang_math_fmaD"    ); break;
325     case java_lang_math_fmaF    : tty->print("java_lang_math_fmaF"    ); break;
326     case java_lang_math_sqrt    : tty->print("java_lang_math_sqrt"    ); break;
327     case java_lang_math_sqrt_strict           : tty->print("java_lang_math_sqrt_strict"); break;
328     case java_util_zip_CRC32_update           : tty->print("java_util_zip_CRC32_update"); break;
329     case java_util_zip_CRC32_updateBytes      : tty->print("java_util_zip_CRC32_updateBytes"); break;
330     case java_util_zip_CRC32_updateByteBuffer : tty->print("java_util_zip_CRC32_updateByteBuffer"); break;
331     case java_util_zip_CRC32C_updateBytes     : tty->print("java_util_zip_CRC32C_updateBytes"); break;
332     case java_util_zip_CRC32C_updateDirectByteBuffer: tty->print("java_util_zip_CRC32C_updateDirectByteByffer"); break;
333     case java_lang_ref_reference_get          : tty->print("java_lang_ref_reference_get"); break;
334     case java_lang_Thread_currentThread       : tty->print("java_lang_Thread_currentThread"); break;
335     case java_lang_Float_intBitsToFloat       : tty->print("java_lang_Float_intBitsToFloat"); break;
336     case java_lang_Float_floatToRawIntBits    : tty->print("java_lang_Float_floatToRawIntBits"); break;
337     case java_lang_Double_longBitsToDouble    : tty->print("java_lang_Double_longBitsToDouble"); break;
338     case java_lang_Double_doubleToRawLongBits : tty->print("java_lang_Double_doubleToRawLongBits"); break;
339     case java_lang_Float_float16ToFloat       : tty->print("java_lang_Float_float16ToFloat"); break;
340     case java_lang_Float_floatToFloat16       : tty->print("java_lang_Float_floatToFloat16"); break;
341     default:
342       if (kind >= method_handle_invoke_FIRST &&
343           kind <= method_handle_invoke_LAST) {
344         const char* kind_name = vmIntrinsics::name_at(method_handle_intrinsic(kind));
345         if (kind_name[0] == '_')  kind_name = &kind_name[1];  // '_invokeExact' => 'invokeExact'
346         tty->print("method_handle_%s", kind_name);
347         break;
348       }
349       ShouldNotReachHere();
350       break;
351   }
352 }
353 #endif // PRODUCT
354 
355 
356 //------------------------------------------------------------------------------------------------------------------------
357 // Deoptimization support
358 
359 /**
360  * If a deoptimization happens, this function returns the point of next bytecode to continue execution.
361  */
362 address AbstractInterpreter::deopt_continue_after_entry(Method* method, address bcp, int callee_parameters, bool is_top_frame) {
363   assert(method->contains(bcp), "just checkin'");
364 
365   // Get the original and rewritten bytecode.
366   Bytecodes::Code code = Bytecodes::java_code_at(method, bcp);
367   assert(!Interpreter::bytecode_should_reexecute(code), "should not reexecute");
368 
369   const int bci = method->bci_from(bcp);
370 
371   // compute continuation length
372   const int length = Bytecodes::length_at(method, bcp);
373 
374   // compute result type
375   BasicType type = T_ILLEGAL;
376 
377   switch (code) {
378     case Bytecodes::_invokevirtual  :
379     case Bytecodes::_invokespecial  :
380     case Bytecodes::_invokestatic   :
381     case Bytecodes::_invokeinterface: {
382       Thread *thread = Thread::current();
383       ResourceMark rm(thread);
384       methodHandle mh(thread, method);
385       type = Bytecode_invoke(mh, bci).result_type();
386       // since the cache entry might not be initialized:
387       // (NOT needed for the old calling convention)
388       if (!is_top_frame) {
389         int index = Bytes::get_native_u2(bcp+1);
390         method->constants()->cache()->resolved_method_entry_at(index)->set_num_parameters(callee_parameters);
391       }
392       break;
393     }
394 
395    case Bytecodes::_invokedynamic: {
396       Thread *thread = Thread::current();
397       ResourceMark rm(thread);
398       methodHandle mh(thread, method);
399       type = Bytecode_invoke(mh, bci).result_type();
400       // since the cache entry might not be initialized:
401       // (NOT needed for the old calling convention)
402       if (!is_top_frame) {
403         int index = Bytes::get_native_u4(bcp+1);
404         method->constants()->resolved_indy_entry_at(index)->set_num_parameters(callee_parameters);
405       }
406       break;
407     }
408 
409     case Bytecodes::_ldc   :
410     case Bytecodes::_ldc_w : // fall through
411     case Bytecodes::_ldc2_w:
412       {
413         Thread *thread = Thread::current();
414         ResourceMark rm(thread);
415         methodHandle mh(thread, method);
416         type = Bytecode_loadconstant(mh, bci).result_type();
417         break;
418       }
419 
420     default:
421       type = Bytecodes::result_type(code);
422       break;
423   }
424 
425   // return entry point for computed continuation state & bytecode length
426   return
427     is_top_frame
428     ? Interpreter::deopt_entry (as_TosState(type), length)
429     : Interpreter::return_entry(as_TosState(type), length, code);
430 }
431 
432 // If deoptimization happens, this function returns the point where the interpreter reexecutes
433 // the bytecode.
434 // Note: Bytecodes::_athrow is a special case in that it does not return
435 //       Interpreter::deopt_entry(vtos, 0) like others
436 address AbstractInterpreter::deopt_reexecute_entry(Method* method, address bcp) {
437   assert(method->contains(bcp), "just checkin'");
438   Bytecodes::Code code   = Bytecodes::java_code_at(method, bcp);
439 #if defined(COMPILER1) || INCLUDE_JVMCI
440   if(code == Bytecodes::_athrow ) {
441     return Interpreter::rethrow_exception_entry();
442   }
443 #endif /* COMPILER1 || INCLUDE_JVMCI */
444   return Interpreter::deopt_entry(vtos, 0);
445 }
446 
447 // If deoptimization happens, the interpreter should reexecute these bytecodes.
448 // This function mainly helps the compilers to set up the reexecute bit.
449 bool AbstractInterpreter::bytecode_should_reexecute(Bytecodes::Code code) {
450   switch (code) {
451     case Bytecodes::_lookupswitch:
452     case Bytecodes::_tableswitch:
453     case Bytecodes::_fast_binaryswitch:
454     case Bytecodes::_fast_linearswitch:
455     // recompute conditional expression folded into _if<cond>
456     case Bytecodes::_lcmp      :
457     case Bytecodes::_fcmpl     :
458     case Bytecodes::_fcmpg     :
459     case Bytecodes::_dcmpl     :
460     case Bytecodes::_dcmpg     :
461     case Bytecodes::_ifnull    :
462     case Bytecodes::_ifnonnull :
463     case Bytecodes::_goto      :
464     case Bytecodes::_goto_w    :
465     case Bytecodes::_ifeq      :
466     case Bytecodes::_ifne      :
467     case Bytecodes::_iflt      :
468     case Bytecodes::_ifge      :
469     case Bytecodes::_ifgt      :
470     case Bytecodes::_ifle      :
471     case Bytecodes::_if_icmpeq :
472     case Bytecodes::_if_icmpne :
473     case Bytecodes::_if_icmplt :
474     case Bytecodes::_if_icmpge :
475     case Bytecodes::_if_icmpgt :
476     case Bytecodes::_if_icmple :
477     case Bytecodes::_if_acmpeq :
478     case Bytecodes::_if_acmpne :
479     // special cases
480     case Bytecodes::_getfield  :
481     case Bytecodes::_putfield  :
482     case Bytecodes::_getstatic :
483     case Bytecodes::_putstatic :
484     case Bytecodes::_aastore   :
485 #ifdef COMPILER1
486     //special case of reexecution
487     case Bytecodes::_athrow    :
488 #endif
489       return true;
490 
491     default:
492       return false;
493   }
494 }
495 
496 void AbstractInterpreter::initialize_method_handle_entries() {
497   // method handle entry kinds are generated later in MethodHandlesAdapterGenerator::generate:
498   for (int i = method_handle_invoke_FIRST; i <= method_handle_invoke_LAST; i++) {
499     MethodKind kind = (MethodKind) i;
500     _entry_table[kind] = _entry_table[Interpreter::abstract];
501   }
502 }