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