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