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