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