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