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