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 #ifndef SHARE_INTERPRETER_ABSTRACTINTERPRETER_HPP
 26 #define SHARE_INTERPRETER_ABSTRACTINTERPRETER_HPP
 27 
 28 #include "asm/macroAssembler.hpp"
 29 #include "classfile/vmIntrinsics.hpp"
 30 #include "code/stubs.hpp"
 31 #include "interpreter/bytecodes.hpp"
 32 #include "oops/method.hpp"
 33 #include "runtime/frame.hpp"
 34 #include "runtime/javaThread.hpp"
 35 #include "runtime/vmThread.hpp"
 36 
 37 // This file contains the platform-independent parts
 38 // of the abstract interpreter and the abstract interpreter generator.
 39 
 40 // Organization of the interpreter(s). There exists two different interpreters in hotpot
 41 // an assembly language version (aka template interpreter) and a high level language version
 42 // (aka c++ interpreter). Th division of labor is as follows:
 43 
 44 // Template Interpreter          Zero Interpreter       Functionality
 45 //
 46 // templateTable*                bytecodeInterpreter*   actual interpretation of bytecodes
 47 //
 48 // templateInterpreter*          zeroInterpreter*       generation of assembly code that creates
 49 //                                                      and manages interpreter runtime frames.
 50 //
 51 
 52 class InterpreterMacroAssembler;
 53 
 54 class AbstractInterpreter: AllStatic {
 55   friend class VMStructs;
 56   friend class ZeroInterpreterGenerator;
 57   friend class TemplateInterpreterGenerator;
 58  public:
 59   enum MethodKind {
 60     zerolocals,                                                 // method needs locals initialization
 61     zerolocals_synchronized,                                    // method needs locals initialization & is synchronized
 62     native,                                                     // native method
 63     native_synchronized,                                        // native method & is synchronized
 64     empty,                                                      // empty method (code: _return)
 65     getter,                                                     // getter method
 66     setter,                                                     // setter method
 67     abstract,                                                   // abstract method (throws an AbstractMethodException)
 68     method_handle_invoke_FIRST,                                 // java.lang.invoke.MethodHandles::invokeExact, etc.
 69     method_handle_invoke_LAST                                   = (method_handle_invoke_FIRST
 70                                                                    + (static_cast<int>(vmIntrinsics::LAST_MH_SIG_POLY)
 71                                                                       - static_cast<int>(vmIntrinsics::FIRST_MH_SIG_POLY))),
 72     java_lang_math_sin,                                         // implementation of java.lang.Math.sin   (x)
 73     java_lang_math_cos,                                         // implementation of java.lang.Math.cos   (x)
 74     java_lang_math_tan,                                         // implementation of java.lang.Math.tan   (x)
 75     java_lang_math_tanh,                                        // implementation of java.lang.Math.tanh  (x)
 76     java_lang_math_abs,                                         // implementation of java.lang.Math.abs   (x)
 77     java_lang_math_sqrt,                                        // implementation of java.lang.Math.sqrt  (x)
 78     java_lang_math_sqrt_strict,                                 // implementation of java.lang.StrictMath.sqrt(x)
 79     java_lang_math_log,                                         // implementation of java.lang.Math.log   (x)
 80     java_lang_math_log10,                                       // implementation of java.lang.Math.log10 (x)
 81     java_lang_math_pow,                                         // implementation of java.lang.Math.pow   (x,y)
 82     java_lang_math_exp,                                         // implementation of java.lang.Math.exp   (x)
 83     java_lang_math_fmaF,                                        // implementation of java.lang.Math.fma   (x, y, z)
 84     java_lang_math_fmaD,                                        // implementation of java.lang.Math.fma   (x, y, z)
 85     java_lang_ref_reference_get,                                // implementation of java.lang.ref.Reference.get()
 86     java_util_zip_CRC32_update,                                 // implementation of java.util.zip.CRC32.update()
 87     java_util_zip_CRC32_updateBytes,                            // implementation of java.util.zip.CRC32.updateBytes()
 88     java_util_zip_CRC32_updateByteBuffer,                       // implementation of java.util.zip.CRC32.updateByteBuffer()
 89     java_util_zip_CRC32C_updateBytes,                           // implementation of java.util.zip.CRC32C.updateBytes(crc, b[], off, end)
 90     java_util_zip_CRC32C_updateDirectByteBuffer,                // implementation of java.util.zip.CRC32C.updateDirectByteBuffer(crc, address, off, end)
 91     java_lang_Float_float16ToFloat,                             // implementation of java.lang.Float.float16ToFloat()
 92     java_lang_Float_floatToFloat16,                             // implementation of java.lang.Float.floatToFloat16()
 93     java_lang_Thread_currentThread,                             // implementation of java.lang.Thread.currentThread()
 94     number_of_method_entries,
 95     invalid = -1
 96   };
 97 
 98   // Conversion from the part of the above enum to vmIntrinsics::_invokeExact, etc.
 99   static vmIntrinsics::ID method_handle_intrinsic(MethodKind kind) {
100     if (kind >= method_handle_invoke_FIRST && kind <= method_handle_invoke_LAST)
101       return vmIntrinsics::ID_from(static_cast<int>(vmIntrinsics::FIRST_MH_SIG_POLY) + (kind - method_handle_invoke_FIRST));
102     else
103       return vmIntrinsics::_none;
104   }
105 
106   // Conversion from the above enum to vmIntrinsics::ID
107   static vmIntrinsics::ID method_intrinsic(MethodKind kind);
108 
109   enum SomeConstants {
110     number_of_result_handlers = 10                              // number of result handlers for native calls
111   };
112 
113  protected:
114   static StubQueue* _code;                                      // the interpreter code (codelets)
115 
116   static bool       _notice_safepoints;                         // true if safepoints are activated
117 
118   // method entry points
119   static address    _entry_table[number_of_method_entries];     // entry points for a given method
120   static address    _native_abi_to_tosca[number_of_result_handlers];  // for native method result handlers
121   static address    _slow_signature_handler;                              // the native method generic (slow) signature handler
122 
123   static address    _rethrow_exception_entry;                   // rethrows an activation in previous frame
124 
125   static bool       _should_print_instructions;                 // only with PrintInterpreter and when printing all InterpreterCodelet
126 
127   friend class      AbstractInterpreterGenerator;
128   friend class      InterpreterMacroAssembler;
129 
130  public:
131   // Initialization/debugging
132   static void       initialize();
133   static StubQueue* code()                                      { return _code; }
134   static bool       should_print_instructions()                 { return _should_print_instructions; }
135 
136 
137   // Method activation
138   static MethodKind method_kind(const methodHandle& m);
139   static address    entry_for_kind(MethodKind k)                { assert(0 <= k && k < number_of_method_entries, "illegal kind"); return _entry_table[k]; }
140   static address    entry_for_method(const methodHandle& m)     { return entry_for_kind(method_kind(m)); }
141 
142   // used for bootstrapping method handles:
143   static void       set_entry_for_kind(MethodKind k, address e);
144 
145   static void       print_method_kind(MethodKind kind)          PRODUCT_RETURN;
146 
147   // These should never be compiled since the interpreter will prefer
148   // the compiled version to the intrinsic version.
149   static bool       can_be_compiled(const methodHandle& m) {
150     switch (m->intrinsic_id()) {
151       case vmIntrinsics::_dsin  : // fall thru
152       case vmIntrinsics::_dcos  : // fall thru
153       case vmIntrinsics::_dtan  : // fall thru
154       case vmIntrinsics::_dtanh : // fall thru
155       case vmIntrinsics::_dabs  : // fall thru
156       case vmIntrinsics::_dsqrt : // fall thru
157       case vmIntrinsics::_dsqrt_strict : // fall thru
158       case vmIntrinsics::_dlog  : // fall thru
159       case vmIntrinsics::_dlog10: // fall thru
160       case vmIntrinsics::_dpow  : // fall thru
161       case vmIntrinsics::_dexp  : // fall thru
162       case vmIntrinsics::_fmaD  : // fall thru
163       case vmIntrinsics::_fmaF  : // fall thru
164       case vmIntrinsics::_floatToFloat16       : // fall thru
165       case vmIntrinsics::_float16ToFloat       : // fall thru
166       case vmIntrinsics::_Continuation_doYield : // fall thru
167         return false;
168 
169       default:
170         return true;
171     }
172   }
173 
174   // Runtime support
175 
176   // length = invoke bytecode length (to advance to next bytecode)
177   static address deopt_entry(TosState state, int length) { ShouldNotReachHere(); return nullptr; }
178   static address return_entry(TosState state, int length, Bytecodes::Code code) { ShouldNotReachHere(); return nullptr; }
179 
180   static address    rethrow_exception_entry()                   { return _rethrow_exception_entry; }
181 
182   // Activation size in words for a method that is just being called.
183   // Parameters haven't been pushed so count them too.
184   static int        size_top_interpreter_activation(Method* method);
185 
186   // Deoptimization support
187   // Compute the entry address for continuation after
188   static address deopt_continue_after_entry(Method* method,
189                                             address bcp,
190                                             int callee_parameters,
191                                             bool is_top_frame);
192   // Compute the entry address for reexecution
193   static address deopt_reexecute_entry(Method* method, address bcp);
194   // Deoptimization should reexecute this bytecode
195   static bool    bytecode_should_reexecute(Bytecodes::Code code);
196 
197   // deoptimization support
198   static int        size_activation(int max_stack,
199                                     int temps,
200                                     int extra_args,
201                                     int monitors,
202                                     int callee_params,
203                                     int callee_locals,
204                                     bool is_top_frame);
205 
206   static void      layout_activation(Method* method,
207                                      int temps,
208                                      int popframe_args,
209                                      int monitors,
210                                      int caller_actual_parameters,
211                                      int callee_params,
212                                      int callee_locals,
213                                      frame* caller,
214                                      frame* interpreter_frame,
215                                      bool is_top_frame,
216                                      bool is_bottom_frame);
217 
218   // Runtime support
219   static bool       is_not_reached(const methodHandle& method, int bci);
220   // Safepoint support
221   static void       notice_safepoints()                         { ShouldNotReachHere(); } // stops the thread when reaching a safepoint
222   static void       ignore_safepoints()                         { ShouldNotReachHere(); } // ignores safepoints
223 
224   // Support for native calls
225   static address    slow_signature_handler()                    { return _slow_signature_handler; }
226   static address    result_handler(BasicType type)              { return _native_abi_to_tosca[BasicType_as_index(type)]; }
227   static int        BasicType_as_index(BasicType type);         // computes index into result_handler_by_index table
228   // Debugging/printing
229   static void       print();                                    // prints the interpreter code
230 
231  public:
232   // Interpreter helpers
233   const static int stackElementWords   = 1;
234   const static int stackElementSize    = stackElementWords * wordSize;
235   const static int logStackElementSize = LogBytesPerWord;
236 
237   static int expr_index_at(int i) {
238     return stackElementWords * i;
239   }
240 
241   static int expr_offset_in_bytes(int i) {
242 #if !defined(ZERO) && (defined(PPC) || defined(S390))
243     return stackElementSize * i + wordSize;  // both point to one word past TOS
244 #else
245     return stackElementSize * i;
246 #endif
247   }
248 
249   static int local_index_at(int i) {
250     assert(i <= 0, "local direction already negated");
251     return stackElementWords * i;
252   }
253 
254 #if !defined(ZERO) && (defined(IA32) || defined(AMD64))
255   static Address::ScaleFactor stackElementScale() {
256     return NOT_LP64(Address::times_4) LP64_ONLY(Address::times_8);
257   }
258 #endif
259 
260   // Local values relative to locals[n]
261   static int  local_offset_in_bytes(int n) {
262     return ((frame::interpreter_frame_expression_stack_direction() * n) * stackElementSize);
263   }
264 
265   // access to stacked values according to type:
266   static oop* oop_addr_in_slot(intptr_t* slot_addr) {
267     return (oop*) slot_addr;
268   }
269   static jint* int_addr_in_slot(intptr_t* slot_addr) {
270     if ((int) sizeof(jint) < wordSize && !Endian::is_Java_byte_ordering_different())
271       // big-endian LP64
272       return (jint*)(slot_addr + 1) - 1;
273     else
274       return (jint*) slot_addr;
275   }
276   static jlong long_in_slot(intptr_t* slot_addr) {
277     if (sizeof(intptr_t) >= sizeof(jlong)) {
278       return *(jlong*) slot_addr;
279     } else {
280       return Bytes::get_native_u8((address)slot_addr);
281     }
282   }
283   static void set_long_in_slot(intptr_t* slot_addr, jlong value) {
284     if (sizeof(intptr_t) >= sizeof(jlong)) {
285       *(jlong*) slot_addr = value;
286     } else {
287       Bytes::put_native_u8((address)slot_addr, value);
288     }
289   }
290 
291   static void initialize_method_handle_entries();
292 };
293 
294 //------------------------------------------------------------------------------------------------------------------------
295 // The interpreter generator.
296 
297 class Template;
298 class AbstractInterpreterGenerator: public StackObj {
299  protected:
300   InterpreterMacroAssembler* _masm;
301 
302  public:
303   AbstractInterpreterGenerator();
304 };
305 
306 #endif // SHARE_INTERPRETER_ABSTRACTINTERPRETER_HPP