src/share/vm/interpreter/interpreter.cpp

Sun, 23 May 2010 01:38:26 -0700

author
jrose
date
Sun, 23 May 2010 01:38:26 -0700
changeset 1920
ab102d5d923e
parent 1570
e66fd840cb6b
child 1934
e9ff18c4ace7
permissions
-rw-r--r--

6939207: refactor constant pool index processing
Summary: Factored cleanup of instruction decode which prepares for enhanced ldc semantics.
Reviewed-by: twisti

duke@435 1 /*
jrose@1920 2 * Copyright 1997-2010 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_interpreter.cpp.incl"
duke@435 27
duke@435 28 # define __ _masm->
duke@435 29
duke@435 30
duke@435 31 //------------------------------------------------------------------------------------------------------------------------
duke@435 32 // Implementation of InterpreterCodelet
duke@435 33
duke@435 34 void InterpreterCodelet::initialize(const char* description, Bytecodes::Code bytecode) {
duke@435 35 _description = description;
duke@435 36 _bytecode = bytecode;
duke@435 37 }
duke@435 38
duke@435 39
duke@435 40 void InterpreterCodelet::verify() {
duke@435 41 }
duke@435 42
duke@435 43
duke@435 44 void InterpreterCodelet::print() {
duke@435 45 if (PrintInterpreter) {
duke@435 46 tty->cr();
duke@435 47 tty->print_cr("----------------------------------------------------------------------");
duke@435 48 }
duke@435 49
duke@435 50 if (description() != NULL) tty->print("%s ", description());
duke@435 51 if (bytecode() >= 0 ) tty->print("%d %s ", bytecode(), Bytecodes::name(bytecode()));
duke@435 52 tty->print_cr("[" INTPTR_FORMAT ", " INTPTR_FORMAT "] %d bytes",
duke@435 53 code_begin(), code_end(), code_size());
duke@435 54
duke@435 55 if (PrintInterpreter) {
duke@435 56 tty->cr();
duke@435 57 Disassembler::decode(code_begin(), code_end(), tty);
duke@435 58 }
duke@435 59 }
duke@435 60
duke@435 61
duke@435 62 //------------------------------------------------------------------------------------------------------------------------
duke@435 63 // Implementation of platform independent aspects of Interpreter
duke@435 64
duke@435 65 void AbstractInterpreter::initialize() {
duke@435 66 if (_code != NULL) return;
duke@435 67
duke@435 68 // make sure 'imported' classes are initialized
duke@435 69 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) BytecodeCounter::reset();
duke@435 70 if (PrintBytecodeHistogram) BytecodeHistogram::reset();
duke@435 71 if (PrintBytecodePairHistogram) BytecodePairHistogram::reset();
duke@435 72
duke@435 73 InvocationCounter::reinitialize(DelayCompilationDuringStartup);
duke@435 74
duke@435 75 }
duke@435 76
duke@435 77 void AbstractInterpreter::print() {
duke@435 78 tty->cr();
duke@435 79 tty->print_cr("----------------------------------------------------------------------");
duke@435 80 tty->print_cr("Interpreter");
duke@435 81 tty->cr();
duke@435 82 tty->print_cr("code size = %6dK bytes", (int)_code->used_space()/1024);
duke@435 83 tty->print_cr("total space = %6dK bytes", (int)_code->total_space()/1024);
duke@435 84 tty->print_cr("wasted space = %6dK bytes", (int)_code->available_space()/1024);
duke@435 85 tty->cr();
duke@435 86 tty->print_cr("# of codelets = %6d" , _code->number_of_stubs());
duke@435 87 tty->print_cr("avg codelet size = %6d bytes", _code->used_space() / _code->number_of_stubs());
duke@435 88 tty->cr();
duke@435 89 _code->print();
duke@435 90 tty->print_cr("----------------------------------------------------------------------");
duke@435 91 tty->cr();
duke@435 92 }
duke@435 93
duke@435 94
duke@435 95 void interpreter_init() {
duke@435 96 Interpreter::initialize();
duke@435 97 #ifndef PRODUCT
duke@435 98 if (TraceBytecodes) BytecodeTracer::set_closure(BytecodeTracer::std_closure());
duke@435 99 #endif // PRODUCT
duke@435 100 // need to hit every safepoint in order to call zapping routine
duke@435 101 // register the interpreter
duke@435 102 VTune::register_stub(
duke@435 103 "Interpreter",
duke@435 104 AbstractInterpreter::code()->code_start(),
duke@435 105 AbstractInterpreter::code()->code_end()
duke@435 106 );
duke@435 107 Forte::register_stub(
duke@435 108 "Interpreter",
duke@435 109 AbstractInterpreter::code()->code_start(),
duke@435 110 AbstractInterpreter::code()->code_end()
duke@435 111 );
duke@435 112
duke@435 113 // notify JVMTI profiler
duke@435 114 if (JvmtiExport::should_post_dynamic_code_generated()) {
duke@435 115 JvmtiExport::post_dynamic_code_generated("Interpreter",
duke@435 116 AbstractInterpreter::code()->code_start(),
duke@435 117 AbstractInterpreter::code()->code_end());
duke@435 118 }
duke@435 119 }
duke@435 120
duke@435 121 //------------------------------------------------------------------------------------------------------------------------
duke@435 122 // Implementation of interpreter
duke@435 123
duke@435 124 StubQueue* AbstractInterpreter::_code = NULL;
duke@435 125 bool AbstractInterpreter::_notice_safepoints = false;
duke@435 126 address AbstractInterpreter::_rethrow_exception_entry = NULL;
duke@435 127
duke@435 128 address AbstractInterpreter::_native_entry_begin = NULL;
duke@435 129 address AbstractInterpreter::_native_entry_end = NULL;
duke@435 130 address AbstractInterpreter::_slow_signature_handler;
duke@435 131 address AbstractInterpreter::_entry_table [AbstractInterpreter::number_of_method_entries];
duke@435 132 address AbstractInterpreter::_native_abi_to_tosca [AbstractInterpreter::number_of_result_handlers];
duke@435 133
duke@435 134 //------------------------------------------------------------------------------------------------------------------------
duke@435 135 // Generation of complete interpreter
duke@435 136
duke@435 137 AbstractInterpreterGenerator::AbstractInterpreterGenerator(StubQueue* _code) {
duke@435 138 _masm = NULL;
duke@435 139 }
duke@435 140
duke@435 141
duke@435 142 static const BasicType types[Interpreter::number_of_result_handlers] = {
duke@435 143 T_BOOLEAN,
duke@435 144 T_CHAR ,
duke@435 145 T_BYTE ,
duke@435 146 T_SHORT ,
duke@435 147 T_INT ,
duke@435 148 T_LONG ,
duke@435 149 T_VOID ,
duke@435 150 T_FLOAT ,
duke@435 151 T_DOUBLE ,
duke@435 152 T_OBJECT
duke@435 153 };
duke@435 154
duke@435 155 void AbstractInterpreterGenerator::generate_all() {
duke@435 156
duke@435 157
duke@435 158 { CodeletMark cm(_masm, "slow signature handler");
duke@435 159 Interpreter::_slow_signature_handler = generate_slow_signature_handler();
duke@435 160 }
duke@435 161
duke@435 162 }
duke@435 163
duke@435 164 //------------------------------------------------------------------------------------------------------------------------
duke@435 165 // Entry points
duke@435 166
duke@435 167 AbstractInterpreter::MethodKind AbstractInterpreter::method_kind(methodHandle m) {
duke@435 168 // Abstract method?
duke@435 169 if (m->is_abstract()) return abstract;
duke@435 170
jrose@1145 171 // Invoker for method handles?
jrose@1145 172 if (m->is_method_handle_invoke()) return method_handle;
jrose@1145 173
duke@435 174 // Native method?
duke@435 175 // Note: This test must come _before_ the test for intrinsic
duke@435 176 // methods. See also comments below.
duke@435 177 if (m->is_native()) {
jrose@1145 178 assert(!m->is_method_handle_invoke(), "overlapping bits here, watch out");
duke@435 179 return m->is_synchronized() ? native_synchronized : native;
duke@435 180 }
duke@435 181
duke@435 182 // Synchronized?
duke@435 183 if (m->is_synchronized()) {
duke@435 184 return zerolocals_synchronized;
duke@435 185 }
duke@435 186
duke@435 187 if (RegisterFinalizersAtInit && m->code_size() == 1 &&
duke@435 188 m->intrinsic_id() == vmIntrinsics::_Object_init) {
duke@435 189 // We need to execute the special return bytecode to check for
duke@435 190 // finalizer registration so create a normal frame.
duke@435 191 return zerolocals;
duke@435 192 }
duke@435 193
duke@435 194 // Empty method?
duke@435 195 if (m->is_empty_method()) {
duke@435 196 return empty;
duke@435 197 }
duke@435 198
duke@435 199 // Accessor method?
duke@435 200 if (m->is_accessor()) {
duke@435 201 assert(m->size_of_parameters() == 1, "fast code for accessors assumes parameter size = 1");
duke@435 202 return accessor;
duke@435 203 }
duke@435 204
duke@435 205 // Special intrinsic method?
duke@435 206 // Note: This test must come _after_ the test for native methods,
duke@435 207 // otherwise we will run into problems with JDK 1.2, see also
duke@435 208 // AbstractInterpreterGenerator::generate_method_entry() for
duke@435 209 // for details.
duke@435 210 switch (m->intrinsic_id()) {
duke@435 211 case vmIntrinsics::_dsin : return java_lang_math_sin ;
duke@435 212 case vmIntrinsics::_dcos : return java_lang_math_cos ;
duke@435 213 case vmIntrinsics::_dtan : return java_lang_math_tan ;
duke@435 214 case vmIntrinsics::_dabs : return java_lang_math_abs ;
duke@435 215 case vmIntrinsics::_dsqrt : return java_lang_math_sqrt ;
duke@435 216 case vmIntrinsics::_dlog : return java_lang_math_log ;
duke@435 217 case vmIntrinsics::_dlog10: return java_lang_math_log10;
duke@435 218 }
duke@435 219
duke@435 220 // Note: for now: zero locals for all non-empty methods
duke@435 221 return zerolocals;
duke@435 222 }
duke@435 223
duke@435 224
duke@435 225 // Return true if the interpreter can prove that the given bytecode has
duke@435 226 // not yet been executed (in Java semantics, not in actual operation).
duke@435 227 bool AbstractInterpreter::is_not_reached(methodHandle method, int bci) {
duke@435 228 address bcp = method->bcp_from(bci);
jrose@1920 229 Bytecodes::Code code = Bytecodes::code_at(bcp, method());
duke@435 230
jrose@1920 231 if (!Bytecode_at(bcp)->must_rewrite(code)) {
duke@435 232 // might have been reached
duke@435 233 return false;
duke@435 234 }
duke@435 235
duke@435 236 // the bytecode might not be rewritten if the method is an accessor, etc.
duke@435 237 address ientry = method->interpreter_entry();
duke@435 238 if (ientry != entry_for_kind(AbstractInterpreter::zerolocals) &&
duke@435 239 ientry != entry_for_kind(AbstractInterpreter::zerolocals_synchronized))
duke@435 240 return false; // interpreter does not run this method!
duke@435 241
duke@435 242 // otherwise, we can be sure this bytecode has never been executed
duke@435 243 return true;
duke@435 244 }
duke@435 245
duke@435 246
duke@435 247 #ifndef PRODUCT
duke@435 248 void AbstractInterpreter::print_method_kind(MethodKind kind) {
duke@435 249 switch (kind) {
duke@435 250 case zerolocals : tty->print("zerolocals" ); break;
duke@435 251 case zerolocals_synchronized: tty->print("zerolocals_synchronized"); break;
duke@435 252 case native : tty->print("native" ); break;
duke@435 253 case native_synchronized : tty->print("native_synchronized" ); break;
duke@435 254 case empty : tty->print("empty" ); break;
duke@435 255 case accessor : tty->print("accessor" ); break;
duke@435 256 case abstract : tty->print("abstract" ); break;
jrose@1145 257 case method_handle : tty->print("method_handle" ); break;
duke@435 258 case java_lang_math_sin : tty->print("java_lang_math_sin" ); break;
duke@435 259 case java_lang_math_cos : tty->print("java_lang_math_cos" ); break;
duke@435 260 case java_lang_math_tan : tty->print("java_lang_math_tan" ); break;
duke@435 261 case java_lang_math_abs : tty->print("java_lang_math_abs" ); break;
duke@435 262 case java_lang_math_sqrt : tty->print("java_lang_math_sqrt" ); break;
duke@435 263 case java_lang_math_log : tty->print("java_lang_math_log" ); break;
duke@435 264 case java_lang_math_log10 : tty->print("java_lang_math_log10" ); break;
duke@435 265 default : ShouldNotReachHere();
duke@435 266 }
duke@435 267 }
duke@435 268 #endif // PRODUCT
duke@435 269
duke@435 270 static BasicType constant_pool_type(methodOop method, int index) {
duke@435 271 constantTag tag = method->constants()->tag_at(index);
duke@435 272 if (tag.is_int ()) return T_INT;
duke@435 273 else if (tag.is_float ()) return T_FLOAT;
duke@435 274 else if (tag.is_long ()) return T_LONG;
duke@435 275 else if (tag.is_double ()) return T_DOUBLE;
duke@435 276 else if (tag.is_string ()) return T_OBJECT;
duke@435 277 else if (tag.is_unresolved_string()) return T_OBJECT;
duke@435 278 else if (tag.is_klass ()) return T_OBJECT;
duke@435 279 else if (tag.is_unresolved_klass ()) return T_OBJECT;
duke@435 280 ShouldNotReachHere();
duke@435 281 return T_ILLEGAL;
duke@435 282 }
duke@435 283
duke@435 284
duke@435 285 //------------------------------------------------------------------------------------------------------------------------
duke@435 286 // Deoptimization support
duke@435 287
cfang@1335 288 // If deoptimization happens, this function returns the point of next bytecode to continue execution
cfang@1335 289 address AbstractInterpreter::deopt_continue_after_entry(methodOop method, address bcp, int callee_parameters, bool is_top_frame) {
duke@435 290 assert(method->contains(bcp), "just checkin'");
duke@435 291 Bytecodes::Code code = Bytecodes::java_code_at(bcp);
cfang@1335 292 assert(!Interpreter::bytecode_should_reexecute(code), "should not reexecute");
duke@435 293 int bci = method->bci_from(bcp);
duke@435 294 int length = -1; // initial value for debugging
duke@435 295 // compute continuation length
duke@435 296 length = Bytecodes::length_at(bcp);
duke@435 297 // compute result type
duke@435 298 BasicType type = T_ILLEGAL;
cfang@1335 299
duke@435 300 switch (code) {
duke@435 301 case Bytecodes::_invokevirtual :
duke@435 302 case Bytecodes::_invokespecial :
duke@435 303 case Bytecodes::_invokestatic :
duke@435 304 case Bytecodes::_invokeinterface: {
duke@435 305 Thread *thread = Thread::current();
duke@435 306 ResourceMark rm(thread);
duke@435 307 methodHandle mh(thread, method);
duke@435 308 type = Bytecode_invoke_at(mh, bci)->result_type(thread);
duke@435 309 // since the cache entry might not be initialized:
duke@435 310 // (NOT needed for the old calling convension)
duke@435 311 if (!is_top_frame) {
duke@435 312 int index = Bytes::get_native_u2(bcp+1);
duke@435 313 method->constants()->cache()->entry_at(index)->set_parameter_size(callee_parameters);
duke@435 314 }
duke@435 315 break;
duke@435 316 }
duke@435 317
jrose@1494 318 case Bytecodes::_invokedynamic: {
jrose@1494 319 Thread *thread = Thread::current();
jrose@1494 320 ResourceMark rm(thread);
jrose@1494 321 methodHandle mh(thread, method);
jrose@1494 322 type = Bytecode_invoke_at(mh, bci)->result_type(thread);
jrose@1494 323 // since the cache entry might not be initialized:
jrose@1494 324 // (NOT needed for the old calling convension)
jrose@1494 325 if (!is_top_frame) {
jrose@1494 326 int index = Bytes::get_native_u4(bcp+1);
twisti@1570 327 method->constants()->cache()->secondary_entry_at(index)->set_parameter_size(callee_parameters);
jrose@1494 328 }
jrose@1494 329 break;
jrose@1494 330 }
jrose@1494 331
duke@435 332 case Bytecodes::_ldc :
duke@435 333 type = constant_pool_type( method, *(bcp+1) );
duke@435 334 break;
duke@435 335
duke@435 336 case Bytecodes::_ldc_w : // fall through
duke@435 337 case Bytecodes::_ldc2_w:
duke@435 338 type = constant_pool_type( method, Bytes::get_Java_u2(bcp+1) );
duke@435 339 break;
duke@435 340
duke@435 341 default:
duke@435 342 type = Bytecodes::result_type(code);
duke@435 343 break;
duke@435 344 }
duke@435 345
duke@435 346 // return entry point for computed continuation state & bytecode length
duke@435 347 return
duke@435 348 is_top_frame
duke@435 349 ? Interpreter::deopt_entry (as_TosState(type), length)
duke@435 350 : Interpreter::return_entry(as_TosState(type), length);
duke@435 351 }
duke@435 352
cfang@1335 353 // If deoptimization happens, this function returns the point where the interpreter reexecutes
cfang@1335 354 // the bytecode.
cfang@1335 355 // Note: Bytecodes::_athrow is a special case in that it does not return
cfang@1335 356 // Interpreter::deopt_entry(vtos, 0) like others
cfang@1335 357 address AbstractInterpreter::deopt_reexecute_entry(methodOop method, address bcp) {
cfang@1335 358 assert(method->contains(bcp), "just checkin'");
cfang@1335 359 Bytecodes::Code code = Bytecodes::java_code_at(bcp);
cfang@1335 360 #ifdef COMPILER1
cfang@1335 361 if(code == Bytecodes::_athrow ) {
cfang@1335 362 return Interpreter::rethrow_exception_entry();
cfang@1335 363 }
cfang@1335 364 #endif /* COMPILER1 */
cfang@1335 365 return Interpreter::deopt_entry(vtos, 0);
cfang@1335 366 }
cfang@1335 367
cfang@1335 368 // If deoptimization happens, the interpreter should reexecute these bytecodes.
cfang@1335 369 // This function mainly helps the compilers to set up the reexecute bit.
cfang@1335 370 bool AbstractInterpreter::bytecode_should_reexecute(Bytecodes::Code code) {
cfang@1335 371 switch (code) {
cfang@1335 372 case Bytecodes::_lookupswitch:
cfang@1335 373 case Bytecodes::_tableswitch:
cfang@1335 374 case Bytecodes::_fast_binaryswitch:
cfang@1335 375 case Bytecodes::_fast_linearswitch:
cfang@1335 376 // recompute condtional expression folded into _if<cond>
cfang@1335 377 case Bytecodes::_lcmp :
cfang@1335 378 case Bytecodes::_fcmpl :
cfang@1335 379 case Bytecodes::_fcmpg :
cfang@1335 380 case Bytecodes::_dcmpl :
cfang@1335 381 case Bytecodes::_dcmpg :
cfang@1335 382 case Bytecodes::_ifnull :
cfang@1335 383 case Bytecodes::_ifnonnull :
cfang@1335 384 case Bytecodes::_goto :
cfang@1335 385 case Bytecodes::_goto_w :
cfang@1335 386 case Bytecodes::_ifeq :
cfang@1335 387 case Bytecodes::_ifne :
cfang@1335 388 case Bytecodes::_iflt :
cfang@1335 389 case Bytecodes::_ifge :
cfang@1335 390 case Bytecodes::_ifgt :
cfang@1335 391 case Bytecodes::_ifle :
cfang@1335 392 case Bytecodes::_if_icmpeq :
cfang@1335 393 case Bytecodes::_if_icmpne :
cfang@1335 394 case Bytecodes::_if_icmplt :
cfang@1335 395 case Bytecodes::_if_icmpge :
cfang@1335 396 case Bytecodes::_if_icmpgt :
cfang@1335 397 case Bytecodes::_if_icmple :
cfang@1335 398 case Bytecodes::_if_acmpeq :
cfang@1335 399 case Bytecodes::_if_acmpne :
cfang@1335 400 // special cases
cfang@1335 401 case Bytecodes::_getfield :
cfang@1335 402 case Bytecodes::_putfield :
cfang@1335 403 case Bytecodes::_getstatic :
cfang@1335 404 case Bytecodes::_putstatic :
cfang@1335 405 case Bytecodes::_aastore :
cfang@1335 406 #ifdef COMPILER1
cfang@1335 407 //special case of reexecution
cfang@1335 408 case Bytecodes::_athrow :
cfang@1335 409 #endif
cfang@1335 410 return true;
cfang@1335 411
cfang@1335 412 default:
cfang@1335 413 return false;
cfang@1335 414 }
cfang@1335 415 }
cfang@1335 416
duke@435 417 void AbstractInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
duke@435 418 // Quick & dirty stack overflow checking: bang the stack & handle trap.
duke@435 419 // Note that we do the banging after the frame is setup, since the exception
duke@435 420 // handling code expects to find a valid interpreter frame on the stack.
duke@435 421 // Doing the banging earlier fails if the caller frame is not an interpreter
duke@435 422 // frame.
duke@435 423 // (Also, the exception throwing code expects to unlock any synchronized
duke@435 424 // method receiever, so do the banging after locking the receiver.)
duke@435 425
duke@435 426 // Bang each page in the shadow zone. We can't assume it's been done for
duke@435 427 // an interpreter frame with greater than a page of locals, so each page
duke@435 428 // needs to be checked. Only true for non-native.
duke@435 429 if (UseStackBanging) {
duke@435 430 const int start_page = native_call ? StackShadowPages : 1;
duke@435 431 const int page_size = os::vm_page_size();
duke@435 432 for (int pages = start_page; pages <= StackShadowPages ; pages++) {
duke@435 433 __ bang_stack_with_offset(pages*page_size);
duke@435 434 }
duke@435 435 }
duke@435 436 }

mercurial