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 #include "memory/resourceArea.hpp"
  34 
  35 #ifndef CC_INTERP
  36 
  37 # define __ _masm->
  38 
  39 void TemplateInterpreter::initialize() {
  40   if (_code != NULL) return;
  41   // assertions
  42   assert((int)Bytecodes::number_of_codes <= (int)DispatchTable::length,
  43          "dispatch table too small");
  44 
  45   AbstractInterpreter::initialize();
  46 
  47   TemplateTable::initialize();
  48 
  49   // generate interpreter
  50   { ResourceMark rm;
  51     TraceTime timer("Interpreter generation", TraceStartupTime);
  52     int code_size = InterpreterCodeSize;
  53     NOT_PRODUCT(code_size *= 4;)  // debug uses extra interpreter code space
  54 #if INCLUDE_JVMTI
  55     if (CodeCacheExtensions::saving_generated_interpreter()) {
  56       // May requires several versions of the codelets.
  57       // Final size will automatically be optimized.
  58       code_size *= 2;
  59     }
  60 #endif
  61     _code = new StubQueue(new InterpreterCodeletInterface, code_size, NULL,
  62                           "Interpreter");
  63     TemplateInterpreterGenerator g(_code);
  64   }
  65   if (PrintInterpreter) {
  66     if (CodeCacheExtensions::saving_generated_interpreter() &&
  67         CodeCacheExtensions::use_pregenerated_interpreter()) {
  68       ResourceMark rm;
  69       tty->print("Printing the newly generated interpreter first");
  70       print();
  71       tty->print("Printing the pregenerated interpreter next");
  72     }
  73   }
  74 
  75   // Install the pregenerated interpreter code before printing it
  76   CodeCacheExtensions::complete_step(CodeCacheExtensionsSteps::TemplateInterpreter);
  77 
  78   if (PrintInterpreter) {
  79     ResourceMark rm;
  80     print();
  81   }
  82 
  83   // initialize dispatch table
  84   _active_table = _normal_table;
  85 }
  86 
  87 //------------------------------------------------------------------------------------------------------------------------
  88 // Implementation of EntryPoint
  89 
  90 EntryPoint::EntryPoint() {
  91   assert(number_of_states == 9, "check the code below");
  92   _entry[btos] = NULL;
  93   _entry[ctos] = NULL;
  94   _entry[stos] = NULL;
  95   _entry[atos] = NULL;
  96   _entry[itos] = NULL;
  97   _entry[ltos] = NULL;
  98   _entry[ftos] = NULL;
  99   _entry[dtos] = NULL;
 100   _entry[vtos] = NULL;
 101 }
 102 
 103 
 104 EntryPoint::EntryPoint(address bentry, address centry, address sentry, address aentry, address ientry, address lentry, address fentry, address dentry, address ventry) {
 105   assert(number_of_states == 9, "check the code below");
 106   _entry[btos] = bentry;
 107   _entry[ctos] = centry;
 108   _entry[stos] = sentry;
 109   _entry[atos] = aentry;
 110   _entry[itos] = ientry;
 111   _entry[ltos] = lentry;
 112   _entry[ftos] = fentry;
 113   _entry[dtos] = dentry;
 114   _entry[vtos] = ventry;
 115 }
 116 
 117 
 118 void EntryPoint::set_entry(TosState state, address entry) {
 119   assert(0 <= state && state < number_of_states, "state out of bounds");
 120   _entry[state] = entry;
 121 }
 122 
 123 
 124 address EntryPoint::entry(TosState state) const {
 125   assert(0 <= state && state < number_of_states, "state out of bounds");
 126   return _entry[state];
 127 }
 128 
 129 
 130 void EntryPoint::print() {
 131   tty->print("[");
 132   for (int i = 0; i < number_of_states; i++) {
 133     if (i > 0) tty->print(", ");
 134     tty->print(INTPTR_FORMAT, p2i(_entry[i]));
 135   }
 136   tty->print("]");
 137 }
 138 
 139 
 140 bool EntryPoint::operator == (const EntryPoint& y) {
 141   int i = number_of_states;
 142   while (i-- > 0) {
 143     if (_entry[i] != y._entry[i]) return false;
 144   }
 145   return true;
 146 }
 147 
 148 
 149 //------------------------------------------------------------------------------------------------------------------------
 150 // Implementation of DispatchTable
 151 
 152 EntryPoint DispatchTable::entry(int i) const {
 153   assert(0 <= i && i < length, "index out of bounds");
 154   return
 155     EntryPoint(
 156       _table[btos][i],
 157       _table[ctos][i],
 158       _table[stos][i],
 159       _table[atos][i],
 160       _table[itos][i],
 161       _table[ltos][i],
 162       _table[ftos][i],
 163       _table[dtos][i],
 164       _table[vtos][i]
 165     );
 166 }
 167 
 168 
 169 void DispatchTable::set_entry(int i, EntryPoint& entry) {
 170   assert(0 <= i && i < length, "index out of bounds");
 171   assert(number_of_states == 9, "check the code below");
 172   _table[btos][i] = entry.entry(btos);
 173   _table[ctos][i] = entry.entry(ctos);
 174   _table[stos][i] = entry.entry(stos);
 175   _table[atos][i] = entry.entry(atos);
 176   _table[itos][i] = entry.entry(itos);
 177   _table[ltos][i] = entry.entry(ltos);
 178   _table[ftos][i] = entry.entry(ftos);
 179   _table[dtos][i] = entry.entry(dtos);
 180   _table[vtos][i] = entry.entry(vtos);
 181 }
 182 
 183 
 184 bool DispatchTable::operator == (DispatchTable& y) {
 185   int i = length;
 186   while (i-- > 0) {
 187     EntryPoint t = y.entry(i); // for compiler compatibility (BugId 4150096)
 188     if (!(entry(i) == t)) return false;
 189   }
 190   return true;
 191 }
 192 
 193 address    TemplateInterpreter::_remove_activation_entry                    = NULL;
 194 address    TemplateInterpreter::_remove_activation_preserving_args_entry    = NULL;
 195 
 196 
 197 address    TemplateInterpreter::_throw_ArrayIndexOutOfBoundsException_entry = NULL;
 198 address    TemplateInterpreter::_throw_ArrayStoreException_entry            = NULL;
 199 address    TemplateInterpreter::_throw_ArithmeticException_entry            = NULL;
 200 address    TemplateInterpreter::_throw_ClassCastException_entry             = NULL;
 201 address    TemplateInterpreter::_throw_NullPointerException_entry           = NULL;
 202 address    TemplateInterpreter::_throw_StackOverflowError_entry             = NULL;
 203 address    TemplateInterpreter::_throw_exception_entry                      = NULL;
 204 
 205 #ifndef PRODUCT
 206 EntryPoint TemplateInterpreter::_trace_code;
 207 #endif // !PRODUCT
 208 EntryPoint TemplateInterpreter::_return_entry[TemplateInterpreter::number_of_return_entries];
 209 EntryPoint TemplateInterpreter::_earlyret_entry;
 210 EntryPoint TemplateInterpreter::_deopt_entry [TemplateInterpreter::number_of_deopt_entries ];
 211 EntryPoint TemplateInterpreter::_continuation_entry;
 212 EntryPoint TemplateInterpreter::_safept_entry;
 213 
 214 address TemplateInterpreter::_invoke_return_entry[TemplateInterpreter::number_of_return_addrs];
 215 address TemplateInterpreter::_invokeinterface_return_entry[TemplateInterpreter::number_of_return_addrs];
 216 address TemplateInterpreter::_invokedynamic_return_entry[TemplateInterpreter::number_of_return_addrs];
 217 
 218 DispatchTable TemplateInterpreter::_active_table;
 219 DispatchTable TemplateInterpreter::_normal_table;
 220 DispatchTable TemplateInterpreter::_safept_table;
 221 address    TemplateInterpreter::_wentry_point[DispatchTable::length];
 222 
 223 
 224 //------------------------------------------------------------------------------------------------------------------------
 225 // Entry points
 226 
 227 /**
 228  * Returns the return entry table for the given invoke bytecode.
 229  */
 230 address* TemplateInterpreter::invoke_return_entry_table_for(Bytecodes::Code code) {
 231   switch (code) {
 232   case Bytecodes::_invokestatic:
 233   case Bytecodes::_invokespecial:
 234   case Bytecodes::_invokevirtual:
 235   case Bytecodes::_invokehandle:
 236     return Interpreter::invoke_return_entry_table();
 237   case Bytecodes::_invokeinterface:
 238     return Interpreter::invokeinterface_return_entry_table();
 239   case Bytecodes::_invokedynamic:
 240     return Interpreter::invokedynamic_return_entry_table();
 241   default:
 242     fatal("invalid bytecode: %s", Bytecodes::name(code));
 243     return NULL;
 244   }
 245 }
 246 
 247 /**
 248  * Returns the return entry address for the given top-of-stack state and bytecode.
 249  */
 250 address TemplateInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
 251   guarantee(0 <= length && length < Interpreter::number_of_return_entries, "illegal length");
 252   const int index = TosState_as_index(state);
 253   switch (code) {
 254   case Bytecodes::_invokestatic:
 255   case Bytecodes::_invokespecial:
 256   case Bytecodes::_invokevirtual:
 257   case Bytecodes::_invokehandle:
 258     return _invoke_return_entry[index];
 259   case Bytecodes::_invokeinterface:
 260     return _invokeinterface_return_entry[index];
 261   case Bytecodes::_invokedynamic:
 262     return _invokedynamic_return_entry[index];
 263   default:
 264     assert(!Bytecodes::is_invoke(code), "invoke instructions should be handled separately: %s", Bytecodes::name(code));
 265     return _return_entry[length].entry(state);
 266   }
 267 }
 268 
 269 
 270 address TemplateInterpreter::deopt_entry(TosState state, int length) {
 271   guarantee(0 <= length && length < Interpreter::number_of_deopt_entries, "illegal length");
 272   return _deopt_entry[length].entry(state);
 273 }
 274 
 275 //------------------------------------------------------------------------------------------------------------------------
 276 // Suport for invokes
 277 
 278 int TemplateInterpreter::TosState_as_index(TosState state) {
 279   assert( state < number_of_states , "Invalid state in TosState_as_index");
 280   assert(0 <= (int)state && (int)state < TemplateInterpreter::number_of_return_addrs, "index out of bounds");
 281   return (int)state;
 282 }
 283 
 284 
 285 //------------------------------------------------------------------------------------------------------------------------
 286 // Safepoint suppport
 287 
 288 static inline void copy_table(address* from, address* to, int size) {
 289   // Copy non-overlapping tables. The copy has to occur word wise for MT safety.
 290   while (size-- > 0) *to++ = *from++;
 291 }
 292 
 293 void TemplateInterpreter::notice_safepoints() {
 294   if (!_notice_safepoints) {
 295     // switch to safepoint dispatch table
 296     _notice_safepoints = true;
 297     copy_table((address*)&_safept_table, (address*)&_active_table, sizeof(_active_table) / sizeof(address));
 298   }
 299 }
 300 
 301 // switch from the dispatch table which notices safepoints back to the
 302 // normal dispatch table.  So that we can notice single stepping points,
 303 // keep the safepoint dispatch table if we are single stepping in JVMTI.
 304 // Note that the should_post_single_step test is exactly as fast as the
 305 // JvmtiExport::_enabled test and covers both cases.
 306 void TemplateInterpreter::ignore_safepoints() {
 307   if (_notice_safepoints) {
 308     if (!JvmtiExport::should_post_single_step()) {
 309       // switch to normal dispatch table
 310       _notice_safepoints = false;
 311       copy_table((address*)&_normal_table, (address*)&_active_table, sizeof(_active_table) / sizeof(address));
 312     }
 313   }
 314 }
 315 
 316 //------------------------------------------------------------------------------------------------------------------------
 317 // Deoptimization support
 318 
 319 // If deoptimization happens, this function returns the point of next bytecode to continue execution
 320 address TemplateInterpreter::deopt_continue_after_entry(Method* method, address bcp, int callee_parameters, bool is_top_frame) {
 321   return AbstractInterpreter::deopt_continue_after_entry(method, bcp, callee_parameters, is_top_frame);
 322 }
 323 
 324 // If deoptimization happens, this function returns the point where the interpreter reexecutes
 325 // the bytecode.
 326 // Note: Bytecodes::_athrow (C1 only) and Bytecodes::_return are the special cases
 327 //       that do not return "Interpreter::deopt_entry(vtos, 0)"
 328 address TemplateInterpreter::deopt_reexecute_entry(Method* method, address bcp) {
 329   assert(method->contains(bcp), "just checkin'");
 330   Bytecodes::Code code   = Bytecodes::java_code_at(method, bcp);
 331   if (code == Bytecodes::_return) {
 332     // This is used for deopt during registration of finalizers
 333     // during Object.<init>.  We simply need to resume execution at
 334     // the standard return vtos bytecode to pop the frame normally.
 335     // reexecuting the real bytecode would cause double registration
 336     // of the finalizable object.
 337     return _normal_table.entry(Bytecodes::_return).entry(vtos);
 338   } else {
 339     return AbstractInterpreter::deopt_reexecute_entry(method, bcp);
 340   }
 341 }
 342 
 343 // If deoptimization happens, the interpreter should reexecute this bytecode.
 344 // This function mainly helps the compilers to set up the reexecute bit.
 345 bool TemplateInterpreter::bytecode_should_reexecute(Bytecodes::Code code) {
 346   if (code == Bytecodes::_return) {
 347     //Yes, we consider Bytecodes::_return as a special case of reexecution
 348     return true;
 349   } else {
 350     return AbstractInterpreter::bytecode_should_reexecute(code);
 351   }
 352 }
 353 
 354 InterpreterCodelet* TemplateInterpreter::codelet_containing(address pc) {
 355   return (InterpreterCodelet*)_code->stub_containing(pc);
 356 }
 357 
 358 #endif // !CC_INTERP