1 /*
  2  * Copyright (c) 1997, 2022, 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 "compiler/disassembler.hpp"
 27 #include "interpreter/interpreter.hpp"
 28 #include "interpreter/interpreterRuntime.hpp"
 29 #include "interpreter/interp_masm.hpp"
 30 #include "interpreter/templateInterpreter.hpp"
 31 #include "interpreter/templateInterpreterGenerator.hpp"
 32 #include "interpreter/templateTable.hpp"
 33 #include "oops/methodData.hpp"
 34 
 35 #define __ Disassembler::hook<InterpreterMacroAssembler>(__FILE__, __LINE__, _masm)->
 36 
 37 TemplateInterpreterGenerator::TemplateInterpreterGenerator(): AbstractInterpreterGenerator() {
 38   _unimplemented_bytecode    = NULL;
 39   _illegal_bytecode_sequence = NULL;
 40   generate_all();
 41 }
 42 
 43 static const BasicType types[Interpreter::number_of_result_handlers] = {
 44   T_BOOLEAN,
 45   T_CHAR   ,
 46   T_BYTE   ,
 47   T_SHORT  ,
 48   T_INT    ,
 49   T_LONG   ,
 50   T_VOID   ,
 51   T_FLOAT  ,
 52   T_DOUBLE ,
 53   T_OBJECT ,
 54   T_PRIMITIVE_OBJECT
 55 };
 56 
 57 void TemplateInterpreterGenerator::generate_all() {
 58   { CodeletMark cm(_masm, "slow signature handler");
 59     AbstractInterpreter::_slow_signature_handler = generate_slow_signature_handler();
 60   }
 61 
 62   { CodeletMark cm(_masm, "error exits");
 63     _unimplemented_bytecode    = generate_error_exit("unimplemented bytecode");
 64     _illegal_bytecode_sequence = generate_error_exit("illegal bytecode sequence - method not verified");
 65   }
 66 
 67 #ifndef PRODUCT
 68   if (TraceBytecodes) {
 69     CodeletMark cm(_masm, "bytecode tracing support");
 70     Interpreter::_trace_code =
 71       EntryPoint(
 72                  generate_trace_code(atos),
 73                  generate_trace_code(itos),
 74                  generate_trace_code(ltos),
 75                  generate_trace_code(ftos),
 76                  generate_trace_code(dtos),
 77                  generate_trace_code(vtos)
 78                  );
 79   }
 80 #endif // !PRODUCT
 81 
 82   { CodeletMark cm(_masm, "return entry points");
 83     Interpreter::_return_entry[0] = EntryPoint();
 84     for (int i = 1; i < Interpreter::number_of_return_entries; i++) {
 85       Interpreter::_return_entry[i] =
 86         EntryPoint(
 87                    generate_return_entry_for(atos, i, sizeof(u2)),
 88                    generate_return_entry_for(itos, i, sizeof(u2)),
 89                    generate_return_entry_for(ltos, i, sizeof(u2)),
 90                    generate_return_entry_for(ftos, i, sizeof(u2)),
 91                    generate_return_entry_for(dtos, i, sizeof(u2)),
 92                    generate_return_entry_for(vtos, i, sizeof(u2))
 93                    );
 94     }
 95   }
 96 
 97   { CodeletMark cm(_masm, "invoke return entry points");
 98     // These states are in order specified in TosState, except btos/ztos/ctos/stos which
 99     // are the same as itos since there is no top of stack optimization for these types
100     const TosState states[] = {ilgl, ilgl, ilgl, ilgl, itos, ltos, ftos, dtos, atos, vtos, ilgl};
101     const int invoke_length = Bytecodes::length_for(Bytecodes::_invokestatic);
102     const int invokeinterface_length = Bytecodes::length_for(Bytecodes::_invokeinterface);
103     const int invokedynamic_length = Bytecodes::length_for(Bytecodes::_invokedynamic);
104 
105     assert(invoke_length >= 0 && invoke_length < Interpreter::number_of_return_entries, "invariant");
106     assert(invokeinterface_length >= 0 && invokeinterface_length < Interpreter::number_of_return_entries, "invariant");
107 
108     for (int i = itos; i < Interpreter::number_of_return_addrs; i++) {
109       TosState state = states[i];
110       assert(state != ilgl, "states array is wrong above");
111 
112       // Reuse generated entry points
113       Interpreter::_invoke_return_entry[i]          = Interpreter::_return_entry[invoke_length].entry(state);
114       Interpreter::_invokeinterface_return_entry[i] = Interpreter::_return_entry[invokeinterface_length].entry(state);
115 
116       Interpreter::_invokedynamic_return_entry[i]   = generate_return_entry_for(state, invokedynamic_length, sizeof(u4));
117     }
118 
119     // set itos entry points for btos/ztos/ctos/stos
120     for (int i = 0; i < itos; i++) {
121       Interpreter::_invoke_return_entry[i]          = Interpreter::_invoke_return_entry[itos];
122       Interpreter::_invokeinterface_return_entry[i] = Interpreter::_invokeinterface_return_entry[itos];
123       Interpreter::_invokedynamic_return_entry[i]   = Interpreter::_invokedynamic_return_entry[itos];
124     }
125   }
126 
127   { CodeletMark cm(_masm, "earlyret entry points");
128     Interpreter::_earlyret_entry =
129       EntryPoint(
130                  generate_earlyret_entry_for(atos),
131                  generate_earlyret_entry_for(itos),
132                  generate_earlyret_entry_for(ltos),
133                  generate_earlyret_entry_for(ftos),
134                  generate_earlyret_entry_for(dtos),
135                  generate_earlyret_entry_for(vtos)
136                  );
137   }
138 
139   { CodeletMark cm(_masm, "result handlers for native calls");
140     // The various result converter stublets.
141     int is_generated[Interpreter::number_of_result_handlers];
142     memset(is_generated, 0, sizeof(is_generated));
143 
144     for (int i = 0; i < Interpreter::number_of_result_handlers; i++) {
145       BasicType type = types[i];
146       if (!is_generated[Interpreter::BasicType_as_index(type)]++) {
147         Interpreter::_native_abi_to_tosca[Interpreter::BasicType_as_index(type)] = generate_result_handler_for(type);
148       }
149     }
150   }
151 
152 
153   { CodeletMark cm(_masm, "safepoint entry points");
154     Interpreter::_safept_entry =
155       EntryPoint(
156                  generate_safept_entry_for(atos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
157                  generate_safept_entry_for(itos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
158                  generate_safept_entry_for(ltos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
159                  generate_safept_entry_for(ftos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
160                  generate_safept_entry_for(dtos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint)),
161                  generate_safept_entry_for(vtos, CAST_FROM_FN_PTR(address, InterpreterRuntime::at_safepoint))
162                  );
163   }
164 
165   { CodeletMark cm(_masm, "exception handling");
166     // (Note: this is not safepoint safe because thread may return to compiled code)
167     generate_throw_exception();
168   }
169 
170   { CodeletMark cm(_masm, "throw exception entrypoints");
171     Interpreter::_throw_ArrayIndexOutOfBoundsException_entry = generate_ArrayIndexOutOfBounds_handler();
172     Interpreter::_throw_ArrayStoreException_entry            = generate_klass_exception_handler("java/lang/ArrayStoreException");
173     Interpreter::_throw_ArithmeticException_entry            = generate_exception_handler("java/lang/ArithmeticException", "/ by zero");
174     Interpreter::_throw_ClassCastException_entry             = generate_ClassCastException_handler();
175     Interpreter::_throw_NullPointerException_entry           = generate_exception_handler("java/lang/NullPointerException", NULL);
176     Interpreter::_throw_StackOverflowError_entry             = generate_StackOverflowError_handler();
177   }
178 
179 
180 
181 #define method_entry(kind)                                                                   \
182   { CodeletMark cm(_masm, "method entry point (kind = " #kind ")");                          \
183     Interpreter::_entry_table[Interpreter::kind] = generate_method_entry(Interpreter::kind); \
184   }
185 
186   // all non-native method kinds
187   method_entry(zerolocals)
188   method_entry(zerolocals_synchronized)
189   method_entry(empty)
190   method_entry(getter)
191   method_entry(setter)
192   method_entry(abstract)
193   method_entry(java_lang_math_sin  )
194   method_entry(java_lang_math_cos  )
195   method_entry(java_lang_math_tan  )
196   method_entry(java_lang_math_abs  )
197   method_entry(java_lang_math_sqrt )
198   method_entry(java_lang_math_log  )
199   method_entry(java_lang_math_log10)
200   method_entry(java_lang_math_exp  )
201   method_entry(java_lang_math_pow  )
202   method_entry(java_lang_math_fmaF )
203   method_entry(java_lang_math_fmaD )
204   method_entry(java_lang_ref_reference_get)
205 #ifdef AMD64
206   method_entry(java_lang_Thread_currentThread)
207 #endif
208   AbstractInterpreter::initialize_method_handle_entries();
209 
210   // all native method kinds (must be one contiguous block)
211   Interpreter::_native_entry_begin = Interpreter::code()->code_end();
212   method_entry(native)
213   method_entry(native_synchronized)
214   Interpreter::_native_entry_end = Interpreter::code()->code_end();
215 
216   method_entry(java_util_zip_CRC32_update)
217   method_entry(java_util_zip_CRC32_updateBytes)
218   method_entry(java_util_zip_CRC32_updateByteBuffer)
219   method_entry(java_util_zip_CRC32C_updateBytes)
220   method_entry(java_util_zip_CRC32C_updateDirectByteBuffer)
221 
222   method_entry(java_lang_Float_intBitsToFloat);
223   method_entry(java_lang_Float_floatToRawIntBits);
224   method_entry(java_lang_Double_longBitsToDouble);
225   method_entry(java_lang_Double_doubleToRawLongBits);
226 
227 #undef method_entry
228 
229   // Bytecodes
230   set_entry_points_for_all_bytes();
231 
232   // installation of code in other places in the runtime
233   // (ExcutableCodeManager calls not needed to copy the entries)
234   set_safepoints_for_all_bytes();
235 
236   { CodeletMark cm(_masm, "deoptimization entry points");
237     Interpreter::_deopt_entry[0] = EntryPoint();
238     Interpreter::_deopt_entry[0].set_entry(vtos, generate_deopt_entry_for(vtos, 0));
239     for (int i = 1; i < Interpreter::number_of_deopt_entries; i++) {
240       Interpreter::_deopt_entry[i] =
241         EntryPoint(
242                    generate_deopt_entry_for(atos, i),
243                    generate_deopt_entry_for(itos, i),
244                    generate_deopt_entry_for(ltos, i),
245                    generate_deopt_entry_for(ftos, i),
246                    generate_deopt_entry_for(dtos, i),
247                    generate_deopt_entry_for(vtos, i)
248                    );
249     }
250     address return_continuation = Interpreter::_normal_table.entry(Bytecodes::_return).entry(vtos);
251     vmassert(return_continuation != NULL, "return entry not generated yet");
252     Interpreter::_deopt_reexecute_return_entry = generate_deopt_entry_for(vtos, 0, return_continuation);
253   }
254 
255 }
256 
257 //------------------------------------------------------------------------------------------------------------------------
258 
259 address TemplateInterpreterGenerator::generate_error_exit(const char* msg) {
260   address entry = __ pc();
261   __ stop(msg);
262   return entry;
263 }
264 
265 
266 //------------------------------------------------------------------------------------------------------------------------
267 
268 void TemplateInterpreterGenerator::set_entry_points_for_all_bytes() {
269   for (int i = 0; i < DispatchTable::length; i++) {
270     Bytecodes::Code code = (Bytecodes::Code)i;
271     if (Bytecodes::is_defined(code)) {
272       set_entry_points(code);
273     } else {
274       set_unimplemented(i);
275     }
276   }
277 }
278 
279 
280 void TemplateInterpreterGenerator::set_safepoints_for_all_bytes() {
281   for (int i = 0; i < DispatchTable::length; i++) {
282     Bytecodes::Code code = (Bytecodes::Code)i;
283     if (Bytecodes::is_defined(code)) Interpreter::_safept_table.set_entry(code, Interpreter::_safept_entry);
284   }
285 }
286 
287 
288 void TemplateInterpreterGenerator::set_unimplemented(int i) {
289   address e = _unimplemented_bytecode;
290   EntryPoint entry(e, e, e, e, e, e, e, e, e, e);
291   Interpreter::_normal_table.set_entry(i, entry);
292   Interpreter::_wentry_point[i] = _unimplemented_bytecode;
293 }
294 
295 
296 void TemplateInterpreterGenerator::set_entry_points(Bytecodes::Code code) {
297   CodeletMark cm(_masm, Bytecodes::name(code), code);
298   // initialize entry points
299   assert(_unimplemented_bytecode    != NULL, "should have been generated before");
300   assert(_illegal_bytecode_sequence != NULL, "should have been generated before");
301   address bep = _illegal_bytecode_sequence;
302   address zep = _illegal_bytecode_sequence;
303   address cep = _illegal_bytecode_sequence;
304   address sep = _illegal_bytecode_sequence;
305   address aep = _illegal_bytecode_sequence;
306   address iep = _illegal_bytecode_sequence;
307   address lep = _illegal_bytecode_sequence;
308   address fep = _illegal_bytecode_sequence;
309   address dep = _illegal_bytecode_sequence;
310   address vep = _unimplemented_bytecode;
311   address wep = _unimplemented_bytecode;
312   // code for short & wide version of bytecode
313   if (Bytecodes::is_defined(code)) {
314     Template* t = TemplateTable::template_for(code);
315     assert(t->is_valid(), "just checking");
316     set_short_entry_points(t, bep, cep, sep, aep, iep, lep, fep, dep, vep);
317   }
318   if (Bytecodes::wide_is_defined(code)) {
319     Template* t = TemplateTable::template_for_wide(code);
320     assert(t->is_valid(), "just checking");
321     set_wide_entry_point(t, wep);
322   }
323   // set entry points
324   EntryPoint entry(bep, zep, cep, sep, aep, iep, lep, fep, dep, vep);
325   Interpreter::_normal_table.set_entry(code, entry);
326   Interpreter::_wentry_point[code] = wep;
327 }
328 
329 
330 void TemplateInterpreterGenerator::set_wide_entry_point(Template* t, address& wep) {
331   assert(t->is_valid(), "template must exist");
332   assert(t->tos_in() == vtos, "only vtos tos_in supported for wide instructions");
333   wep = __ pc(); generate_and_dispatch(t);
334 }
335 
336 
337 void TemplateInterpreterGenerator::set_short_entry_points(Template* t, address& bep, address& cep, address& sep, address& aep, address& iep, address& lep, address& fep, address& dep, address& vep) {
338   assert(t->is_valid(), "template must exist");
339   switch (t->tos_in()) {
340     case btos:
341     case ztos:
342     case ctos:
343     case stos:
344       ShouldNotReachHere();  // btos/ctos/stos should use itos.
345       break;
346     case atos: vep = __ pc(); __ pop(atos); aep = __ pc(); generate_and_dispatch(t); break;
347     case itos: vep = __ pc(); __ pop(itos); iep = __ pc(); generate_and_dispatch(t); break;
348     case ltos: vep = __ pc(); __ pop(ltos); lep = __ pc(); generate_and_dispatch(t); break;
349     case ftos: vep = __ pc(); __ pop(ftos); fep = __ pc(); generate_and_dispatch(t); break;
350     case dtos: vep = __ pc(); __ pop(dtos); dep = __ pc(); generate_and_dispatch(t); break;
351     case vtos: set_vtos_entry_points(t, bep, cep, sep, aep, iep, lep, fep, dep, vep);     break;
352     default  : ShouldNotReachHere();                                                 break;
353   }
354 }
355 
356 
357 //------------------------------------------------------------------------------------------------------------------------
358 
359 void TemplateInterpreterGenerator::generate_and_dispatch(Template* t, TosState tos_out) {
360   if (PrintBytecodeHistogram)                                    histogram_bytecode(t);
361 #ifndef PRODUCT
362   // debugging code
363   if (CountBytecodes || TraceBytecodes || StopInterpreterAt > 0) count_bytecode();
364   if (PrintBytecodePairHistogram)                                histogram_bytecode_pair(t);
365   if (TraceBytecodes)                                            trace_bytecode(t);
366   if (StopInterpreterAt > 0)                                     stop_interpreter_at();
367   __ verify_FPU(1, t->tos_in());
368 #endif // !PRODUCT
369   int step = 0;
370   if (!t->does_dispatch()) {
371     step = t->is_wide() ? Bytecodes::wide_length_for(t->bytecode()) : Bytecodes::length_for(t->bytecode());
372     if (tos_out == ilgl) tos_out = t->tos_out();
373     // compute bytecode size
374     assert(step > 0, "just checkin'");
375     // setup stuff for dispatching next bytecode
376     if (ProfileInterpreter && VerifyDataPointer
377         && MethodData::bytecode_has_profile(t->bytecode())) {
378       __ verify_method_data_pointer();
379     }
380     __ dispatch_prolog(tos_out, step);
381   }
382   // generate template
383   t->generate(_masm);
384   // advance
385   if (t->does_dispatch()) {
386 #ifdef ASSERT
387     // make sure execution doesn't go beyond this point if code is broken
388     __ should_not_reach_here();
389 #endif // ASSERT
390   } else {
391     // dispatch to next bytecode
392     __ dispatch_epilog(tos_out, step);
393   }
394 }
395 
396 // Generate method entries
397 address TemplateInterpreterGenerator::generate_method_entry(
398                                         AbstractInterpreter::MethodKind kind) {
399   // determine code generation flags
400   bool native = false;
401   bool synchronized = false;
402   address entry_point = NULL;
403 
404   switch (kind) {
405   case Interpreter::zerolocals             :                                          break;
406   case Interpreter::zerolocals_synchronized:                synchronized = true;      break;
407   case Interpreter::native                 : native = true;                           break;
408   case Interpreter::native_synchronized    : native = true; synchronized = true;      break;
409   case Interpreter::empty                  : break;
410   case Interpreter::getter                 : break;
411   case Interpreter::setter                 : break;
412   case Interpreter::abstract               : entry_point = generate_abstract_entry(); break;
413 
414   case Interpreter::java_lang_math_sin     : // fall thru
415   case Interpreter::java_lang_math_cos     : // fall thru
416   case Interpreter::java_lang_math_tan     : // fall thru
417   case Interpreter::java_lang_math_abs     : // fall thru
418   case Interpreter::java_lang_math_log     : // fall thru
419   case Interpreter::java_lang_math_log10   : // fall thru
420   case Interpreter::java_lang_math_sqrt    : // fall thru
421   case Interpreter::java_lang_math_pow     : // fall thru
422   case Interpreter::java_lang_math_exp     : // fall thru
423   case Interpreter::java_lang_math_fmaD    : // fall thru
424   case Interpreter::java_lang_math_fmaF    : entry_point = generate_math_entry(kind);      break;
425   case Interpreter::java_lang_ref_reference_get
426                                            : entry_point = generate_Reference_get_entry(); break;
427   case Interpreter::java_util_zip_CRC32_update
428                                            : native = true; entry_point = generate_CRC32_update_entry();  break;
429   case Interpreter::java_util_zip_CRC32_updateBytes
430                                            : // fall thru
431   case Interpreter::java_util_zip_CRC32_updateByteBuffer
432                                            : native = true; entry_point = generate_CRC32_updateBytes_entry(kind); break;
433   case Interpreter::java_util_zip_CRC32C_updateBytes
434                                            : // fall thru
435   case Interpreter::java_util_zip_CRC32C_updateDirectByteBuffer
436                                            : entry_point = generate_CRC32C_updateBytes_entry(kind); break;
437 #ifdef AMD64
438   case Interpreter::java_lang_Thread_currentThread
439                                            : entry_point = generate_currentThread(); break;
440 #endif
441 
442 #ifdef IA32
443   // On x86_32 platforms, a special entry is generated for the following four methods.
444   // On other platforms the normal entry is used to enter these methods.
445   case Interpreter::java_lang_Float_intBitsToFloat
446                                            : native = true; entry_point = generate_Float_intBitsToFloat_entry(); break;
447   case Interpreter::java_lang_Float_floatToRawIntBits
448                                            : native = true; entry_point = generate_Float_floatToRawIntBits_entry(); break;
449   case Interpreter::java_lang_Double_longBitsToDouble
450                                            : native = true; entry_point = generate_Double_longBitsToDouble_entry(); break;
451   case Interpreter::java_lang_Double_doubleToRawLongBits
452                                            : native = true; entry_point = generate_Double_doubleToRawLongBits_entry(); break;
453 #else
454   case Interpreter::java_lang_Float_intBitsToFloat:
455   case Interpreter::java_lang_Float_floatToRawIntBits:
456   case Interpreter::java_lang_Double_longBitsToDouble:
457   case Interpreter::java_lang_Double_doubleToRawLongBits:
458     native = true;
459     break;
460 #endif // !IA32
461   default:
462     fatal("unexpected method kind: %d", kind);
463     break;
464   }
465 
466   if (entry_point) {
467     return entry_point;
468   }
469 
470   // We expect the normal and native entry points to be generated first so we can reuse them.
471   if (native) {
472     entry_point = Interpreter::entry_for_kind(synchronized ? Interpreter::native_synchronized : Interpreter::native);
473     if (entry_point == NULL) {
474       entry_point = generate_native_entry(synchronized);
475     }
476   } else {
477     entry_point = Interpreter::entry_for_kind(synchronized ? Interpreter::zerolocals_synchronized : Interpreter::zerolocals);
478     if (entry_point == NULL) {
479       entry_point = generate_normal_entry(synchronized);
480     }
481   }
482 
483   return entry_point;
484 }