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