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