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