vframeArray.cpp revision 6760:22b98ab2a69f
1/* 2 * Copyright (c) 1997, 2014, 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 "classfile/vmSymbols.hpp" 27#include "code/vmreg.inline.hpp" 28#include "interpreter/bytecode.hpp" 29#include "interpreter/interpreter.hpp" 30#include "memory/allocation.inline.hpp" 31#include "memory/resourceArea.hpp" 32#include "memory/universe.inline.hpp" 33#include "oops/methodData.hpp" 34#include "oops/oop.inline.hpp" 35#include "prims/jvmtiThreadState.hpp" 36#include "runtime/handles.inline.hpp" 37#include "runtime/monitorChunk.hpp" 38#include "runtime/sharedRuntime.hpp" 39#include "runtime/vframe.hpp" 40#include "runtime/vframeArray.hpp" 41#include "runtime/vframe_hp.hpp" 42#include "utilities/events.hpp" 43#ifdef COMPILER2 44#include "opto/runtime.hpp" 45#endif 46 47PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 48 49int vframeArrayElement:: bci(void) const { return (_bci == SynchronizationEntryBCI ? 0 : _bci); } 50 51void vframeArrayElement::free_monitors(JavaThread* jt) { 52 if (_monitors != NULL) { 53 MonitorChunk* chunk = _monitors; 54 _monitors = NULL; 55 jt->remove_monitor_chunk(chunk); 56 delete chunk; 57 } 58} 59 60void vframeArrayElement::fill_in(compiledVFrame* vf) { 61 62// Copy the information from the compiled vframe to the 63// interpreter frame we will be creating to replace vf 64 65 _method = vf->method(); 66 _bci = vf->raw_bci(); 67 _reexecute = vf->should_reexecute(); 68 69 int index; 70 71 // Get the monitors off-stack 72 73 GrowableArray<MonitorInfo*>* list = vf->monitors(); 74 if (list->is_empty()) { 75 _monitors = NULL; 76 } else { 77 78 // Allocate monitor chunk 79 _monitors = new MonitorChunk(list->length()); 80 vf->thread()->add_monitor_chunk(_monitors); 81 82 // Migrate the BasicLocks from the stack to the monitor chunk 83 for (index = 0; index < list->length(); index++) { 84 MonitorInfo* monitor = list->at(index); 85 assert(!monitor->owner_is_scalar_replaced(), "object should be reallocated already"); 86 assert(monitor->owner() == NULL || (!monitor->owner()->is_unlocked() && !monitor->owner()->has_bias_pattern()), "object must be null or locked, and unbiased"); 87 BasicObjectLock* dest = _monitors->at(index); 88 dest->set_obj(monitor->owner()); 89 monitor->lock()->move_to(monitor->owner(), dest->lock()); 90 } 91 } 92 93 // Convert the vframe locals and expressions to off stack 94 // values. Because we will not gc all oops can be converted to 95 // intptr_t (i.e. a stack slot) and we are fine. This is 96 // good since we are inside a HandleMark and the oops in our 97 // collection would go away between packing them here and 98 // unpacking them in unpack_on_stack. 99 100 // First the locals go off-stack 101 102 // FIXME this seems silly it creates a StackValueCollection 103 // in order to get the size to then copy them and 104 // convert the types to intptr_t size slots. Seems like it 105 // could do it in place... Still uses less memory than the 106 // old way though 107 108 StackValueCollection *locs = vf->locals(); 109 _locals = new StackValueCollection(locs->size()); 110 for(index = 0; index < locs->size(); index++) { 111 StackValue* value = locs->at(index); 112 switch(value->type()) { 113 case T_OBJECT: 114 assert(!value->obj_is_scalar_replaced(), "object should be reallocated already"); 115 // preserve object type 116 _locals->add( new StackValue(cast_from_oop<intptr_t>((value->get_obj()())), T_OBJECT )); 117 break; 118 case T_CONFLICT: 119 // A dead local. Will be initialized to null/zero. 120 _locals->add( new StackValue()); 121 break; 122 case T_INT: 123 _locals->add( new StackValue(value->get_int())); 124 break; 125 default: 126 ShouldNotReachHere(); 127 } 128 } 129 130 // Now the expressions off-stack 131 // Same silliness as above 132 133 StackValueCollection *exprs = vf->expressions(); 134 _expressions = new StackValueCollection(exprs->size()); 135 for(index = 0; index < exprs->size(); index++) { 136 StackValue* value = exprs->at(index); 137 switch(value->type()) { 138 case T_OBJECT: 139 assert(!value->obj_is_scalar_replaced(), "object should be reallocated already"); 140 // preserve object type 141 _expressions->add( new StackValue(cast_from_oop<intptr_t>((value->get_obj()())), T_OBJECT )); 142 break; 143 case T_CONFLICT: 144 // A dead stack element. Will be initialized to null/zero. 145 // This can occur when the compiler emits a state in which stack 146 // elements are known to be dead (because of an imminent exception). 147 _expressions->add( new StackValue()); 148 break; 149 case T_INT: 150 _expressions->add( new StackValue(value->get_int())); 151 break; 152 default: 153 ShouldNotReachHere(); 154 } 155 } 156} 157 158int unpack_counter = 0; 159 160void vframeArrayElement::unpack_on_stack(int caller_actual_parameters, 161 int callee_parameters, 162 int callee_locals, 163 frame* caller, 164 bool is_top_frame, 165 bool is_bottom_frame, 166 int exec_mode) { 167 JavaThread* thread = (JavaThread*) Thread::current(); 168 169 // Look at bci and decide on bcp and continuation pc 170 address bcp; 171 // C++ interpreter doesn't need a pc since it will figure out what to do when it 172 // begins execution 173 address pc; 174 bool use_next_mdp = false; // true if we should use the mdp associated with the next bci 175 // rather than the one associated with bcp 176 if (raw_bci() == SynchronizationEntryBCI) { 177 // We are deoptimizing while hanging in prologue code for synchronized method 178 bcp = method()->bcp_from(0); // first byte code 179 pc = Interpreter::deopt_entry(vtos, 0); // step = 0 since we don't skip current bytecode 180 } else if (should_reexecute()) { //reexecute this bytecode 181 assert(is_top_frame, "reexecute allowed only for the top frame"); 182 bcp = method()->bcp_from(bci()); 183 pc = Interpreter::deopt_reexecute_entry(method(), bcp); 184 } else { 185 bcp = method()->bcp_from(bci()); 186 pc = Interpreter::deopt_continue_after_entry(method(), bcp, callee_parameters, is_top_frame); 187 use_next_mdp = true; 188 } 189 assert(Bytecodes::is_defined(*bcp), "must be a valid bytecode"); 190 191 // Monitorenter and pending exceptions: 192 // 193 // For Compiler2, there should be no pending exception when deoptimizing at monitorenter 194 // because there is no safepoint at the null pointer check (it is either handled explicitly 195 // or prior to the monitorenter) and asynchronous exceptions are not made "pending" by the 196 // runtime interface for the slow case (see JRT_ENTRY_FOR_MONITORENTER). If an asynchronous 197 // exception was processed, the bytecode pointer would have to be extended one bytecode beyond 198 // the monitorenter to place it in the proper exception range. 199 // 200 // For Compiler1, deoptimization can occur while throwing a NullPointerException at monitorenter, 201 // in which case bcp should point to the monitorenter since it is within the exception's range. 202 203 assert(*bcp != Bytecodes::_monitorenter || is_top_frame, "a _monitorenter must be a top frame"); 204 assert(thread->deopt_nmethod() != NULL, "nmethod should be known"); 205 guarantee(!(thread->deopt_nmethod()->is_compiled_by_c2() && 206 *bcp == Bytecodes::_monitorenter && 207 exec_mode == Deoptimization::Unpack_exception), 208 "shouldn't get exception during monitorenter"); 209 210 int popframe_preserved_args_size_in_bytes = 0; 211 int popframe_preserved_args_size_in_words = 0; 212 if (is_top_frame) { 213 JvmtiThreadState *state = thread->jvmti_thread_state(); 214 if (JvmtiExport::can_pop_frame() && 215 (thread->has_pending_popframe() || thread->popframe_forcing_deopt_reexecution())) { 216 if (thread->has_pending_popframe()) { 217 // Pop top frame after deoptimization 218#ifndef CC_INTERP 219 pc = Interpreter::remove_activation_preserving_args_entry(); 220#else 221 // Do an uncommon trap type entry. c++ interpreter will know 222 // to pop frame and preserve the args 223 pc = Interpreter::deopt_entry(vtos, 0); 224 use_next_mdp = false; 225#endif 226 } else { 227 // Reexecute invoke in top frame 228 pc = Interpreter::deopt_entry(vtos, 0); 229 use_next_mdp = false; 230 popframe_preserved_args_size_in_bytes = in_bytes(thread->popframe_preserved_args_size()); 231 // Note: the PopFrame-related extension of the expression stack size is done in 232 // Deoptimization::fetch_unroll_info_helper 233 popframe_preserved_args_size_in_words = in_words(thread->popframe_preserved_args_size_in_words()); 234 } 235 } else if (JvmtiExport::can_force_early_return() && state != NULL && state->is_earlyret_pending()) { 236 // Force early return from top frame after deoptimization 237#ifndef CC_INTERP 238 pc = Interpreter::remove_activation_early_entry(state->earlyret_tos()); 239#endif 240 } else { 241 // Possibly override the previous pc computation of the top (youngest) frame 242 switch (exec_mode) { 243 case Deoptimization::Unpack_deopt: 244 // use what we've got 245 break; 246 case Deoptimization::Unpack_exception: 247 // exception is pending 248 pc = SharedRuntime::raw_exception_handler_for_return_address(thread, pc); 249 // [phh] We're going to end up in some handler or other, so it doesn't 250 // matter what mdp we point to. See exception_handler_for_exception() 251 // in interpreterRuntime.cpp. 252 break; 253 case Deoptimization::Unpack_uncommon_trap: 254 case Deoptimization::Unpack_reexecute: 255 // redo last byte code 256 pc = Interpreter::deopt_entry(vtos, 0); 257 use_next_mdp = false; 258 break; 259 default: 260 ShouldNotReachHere(); 261 } 262 } 263 } 264 265 // Setup the interpreter frame 266 267 assert(method() != NULL, "method must exist"); 268 int temps = expressions()->size(); 269 270 int locks = monitors() == NULL ? 0 : monitors()->number_of_monitors(); 271 272 Interpreter::layout_activation(method(), 273 temps + callee_parameters, 274 popframe_preserved_args_size_in_words, 275 locks, 276 caller_actual_parameters, 277 callee_parameters, 278 callee_locals, 279 caller, 280 iframe(), 281 is_top_frame, 282 is_bottom_frame); 283 284 // Update the pc in the frame object and overwrite the temporary pc 285 // we placed in the skeletal frame now that we finally know the 286 // exact interpreter address we should use. 287 288 _frame.patch_pc(thread, pc); 289 290 assert (!method()->is_synchronized() || locks > 0, "synchronized methods must have monitors"); 291 292 BasicObjectLock* top = iframe()->interpreter_frame_monitor_begin(); 293 for (int index = 0; index < locks; index++) { 294 top = iframe()->previous_monitor_in_interpreter_frame(top); 295 BasicObjectLock* src = _monitors->at(index); 296 top->set_obj(src->obj()); 297 src->lock()->move_to(src->obj(), top->lock()); 298 } 299 if (ProfileInterpreter) { 300 iframe()->interpreter_frame_set_mdx(0); // clear out the mdp. 301 } 302 iframe()->interpreter_frame_set_bcx((intptr_t)bcp); // cannot use bcp because frame is not initialized yet 303 if (ProfileInterpreter) { 304 MethodData* mdo = method()->method_data(); 305 if (mdo != NULL) { 306 int bci = iframe()->interpreter_frame_bci(); 307 if (use_next_mdp) ++bci; 308 address mdp = mdo->bci_to_dp(bci); 309 iframe()->interpreter_frame_set_mdp(mdp); 310 } 311 } 312 313 // Unpack expression stack 314 // If this is an intermediate frame (i.e. not top frame) then this 315 // only unpacks the part of the expression stack not used by callee 316 // as parameters. The callee parameters are unpacked as part of the 317 // callee locals. 318 int i; 319 for(i = 0; i < expressions()->size(); i++) { 320 StackValue *value = expressions()->at(i); 321 intptr_t* addr = iframe()->interpreter_frame_expression_stack_at(i); 322 switch(value->type()) { 323 case T_INT: 324 *addr = value->get_int(); 325 break; 326 case T_OBJECT: 327 *addr = value->get_int(T_OBJECT); 328 break; 329 case T_CONFLICT: 330 // A dead stack slot. Initialize to null in case it is an oop. 331 *addr = NULL_WORD; 332 break; 333 default: 334 ShouldNotReachHere(); 335 } 336 } 337 338 339 // Unpack the locals 340 for(i = 0; i < locals()->size(); i++) { 341 StackValue *value = locals()->at(i); 342 intptr_t* addr = iframe()->interpreter_frame_local_at(i); 343 switch(value->type()) { 344 case T_INT: 345 *addr = value->get_int(); 346 break; 347 case T_OBJECT: 348 *addr = value->get_int(T_OBJECT); 349 break; 350 case T_CONFLICT: 351 // A dead location. If it is an oop then we need a NULL to prevent GC from following it 352 *addr = NULL_WORD; 353 break; 354 default: 355 ShouldNotReachHere(); 356 } 357 } 358 359 if (is_top_frame && JvmtiExport::can_pop_frame() && thread->popframe_forcing_deopt_reexecution()) { 360 // An interpreted frame was popped but it returns to a deoptimized 361 // frame. The incoming arguments to the interpreted activation 362 // were preserved in thread-local storage by the 363 // remove_activation_preserving_args_entry in the interpreter; now 364 // we put them back into the just-unpacked interpreter frame. 365 // Note that this assumes that the locals arena grows toward lower 366 // addresses. 367 if (popframe_preserved_args_size_in_words != 0) { 368 void* saved_args = thread->popframe_preserved_args(); 369 assert(saved_args != NULL, "must have been saved by interpreter"); 370#ifdef ASSERT 371 assert(popframe_preserved_args_size_in_words <= 372 iframe()->interpreter_frame_expression_stack_size()*Interpreter::stackElementWords, 373 "expression stack size should have been extended"); 374#endif // ASSERT 375 int top_element = iframe()->interpreter_frame_expression_stack_size()-1; 376 intptr_t* base; 377 if (frame::interpreter_frame_expression_stack_direction() < 0) { 378 base = iframe()->interpreter_frame_expression_stack_at(top_element); 379 } else { 380 base = iframe()->interpreter_frame_expression_stack(); 381 } 382 Copy::conjoint_jbytes(saved_args, 383 base, 384 popframe_preserved_args_size_in_bytes); 385 thread->popframe_free_preserved_args(); 386 } 387 } 388 389#ifndef PRODUCT 390 if (TraceDeoptimization && Verbose) { 391 ttyLocker ttyl; 392 tty->print_cr("[%d Interpreted Frame]", ++unpack_counter); 393 iframe()->print_on(tty); 394 RegisterMap map(thread); 395 vframe* f = vframe::new_vframe(iframe(), &map, thread); 396 f->print(); 397 398 tty->print_cr("locals size %d", locals()->size()); 399 tty->print_cr("expression size %d", expressions()->size()); 400 401 method()->print_value(); 402 tty->cr(); 403 // method()->print_codes(); 404 } else if (TraceDeoptimization) { 405 tty->print(" "); 406 method()->print_value(); 407 Bytecodes::Code code = Bytecodes::java_code_at(method(), bcp); 408 int bci = method()->bci_from(bcp); 409 tty->print(" - %s", Bytecodes::name(code)); 410 tty->print(" @ bci %d ", bci); 411 tty->print_cr("sp = " PTR_FORMAT, iframe()->sp()); 412 } 413#endif // PRODUCT 414 415 // The expression stack and locals are in the resource area don't leave 416 // a dangling pointer in the vframeArray we leave around for debug 417 // purposes 418 419 _locals = _expressions = NULL; 420 421} 422 423int vframeArrayElement::on_stack_size(int callee_parameters, 424 int callee_locals, 425 bool is_top_frame, 426 int popframe_extra_stack_expression_els) const { 427 assert(method()->max_locals() == locals()->size(), "just checking"); 428 int locks = monitors() == NULL ? 0 : monitors()->number_of_monitors(); 429 int temps = expressions()->size(); 430 return Interpreter::size_activation(method()->max_stack(), 431 temps + callee_parameters, 432 popframe_extra_stack_expression_els, 433 locks, 434 callee_parameters, 435 callee_locals, 436 is_top_frame); 437} 438 439 440 441vframeArray* vframeArray::allocate(JavaThread* thread, int frame_size, GrowableArray<compiledVFrame*>* chunk, 442 RegisterMap *reg_map, frame sender, frame caller, frame self) { 443 444 // Allocate the vframeArray 445 vframeArray * result = (vframeArray*) AllocateHeap(sizeof(vframeArray) + // fixed part 446 sizeof(vframeArrayElement) * (chunk->length() - 1), // variable part 447 mtCompiler); 448 result->_frames = chunk->length(); 449 result->_owner_thread = thread; 450 result->_sender = sender; 451 result->_caller = caller; 452 result->_original = self; 453 result->set_unroll_block(NULL); // initialize it 454 result->fill_in(thread, frame_size, chunk, reg_map); 455 return result; 456} 457 458void vframeArray::fill_in(JavaThread* thread, 459 int frame_size, 460 GrowableArray<compiledVFrame*>* chunk, 461 const RegisterMap *reg_map) { 462 // Set owner first, it is used when adding monitor chunks 463 464 _frame_size = frame_size; 465 for(int i = 0; i < chunk->length(); i++) { 466 element(i)->fill_in(chunk->at(i)); 467 } 468 469 // Copy registers for callee-saved registers 470 if (reg_map != NULL) { 471 for(int i = 0; i < RegisterMap::reg_count; i++) { 472#ifdef AMD64 473 // The register map has one entry for every int (32-bit value), so 474 // 64-bit physical registers have two entries in the map, one for 475 // each half. Ignore the high halves of 64-bit registers, just like 476 // frame::oopmapreg_to_location does. 477 // 478 // [phh] FIXME: this is a temporary hack! This code *should* work 479 // correctly w/o this hack, possibly by changing RegisterMap::pd_location 480 // in frame_amd64.cpp and the values of the phantom high half registers 481 // in amd64.ad. 482 // if (VMReg::Name(i) < SharedInfo::stack0 && is_even(i)) { 483 intptr_t* src = (intptr_t*) reg_map->location(VMRegImpl::as_VMReg(i)); 484 _callee_registers[i] = src != NULL ? *src : NULL_WORD; 485 // } else { 486 // jint* src = (jint*) reg_map->location(VMReg::Name(i)); 487 // _callee_registers[i] = src != NULL ? *src : NULL_WORD; 488 // } 489#else 490 jint* src = (jint*) reg_map->location(VMRegImpl::as_VMReg(i)); 491 _callee_registers[i] = src != NULL ? *src : NULL_WORD; 492#endif 493 if (src == NULL) { 494 set_location_valid(i, false); 495 } else { 496 set_location_valid(i, true); 497 jint* dst = (jint*) register_location(i); 498 *dst = *src; 499 } 500 } 501 } 502} 503 504void vframeArray::unpack_to_stack(frame &unpack_frame, int exec_mode, int caller_actual_parameters) { 505 // stack picture 506 // unpack_frame 507 // [new interpreter frames ] (frames are skeletal but walkable) 508 // caller_frame 509 // 510 // This routine fills in the missing data for the skeletal interpreter frames 511 // in the above picture. 512 513 // Find the skeletal interpreter frames to unpack into 514 JavaThread* THREAD = JavaThread::current(); 515 RegisterMap map(THREAD, false); 516 // Get the youngest frame we will unpack (last to be unpacked) 517 frame me = unpack_frame.sender(&map); 518 int index; 519 for (index = 0; index < frames(); index++ ) { 520 *element(index)->iframe() = me; 521 // Get the caller frame (possibly skeletal) 522 me = me.sender(&map); 523 } 524 525 // Do the unpacking of interpreter frames; the frame at index 0 represents the top activation, so it has no callee 526 // Unpack the frames from the oldest (frames() -1) to the youngest (0) 527 frame* caller_frame = &me; 528 for (index = frames() - 1; index >= 0 ; index--) { 529 vframeArrayElement* elem = element(index); // caller 530 int callee_parameters, callee_locals; 531 if (index == 0) { 532 callee_parameters = callee_locals = 0; 533 } else { 534 methodHandle caller = elem->method(); 535 methodHandle callee = element(index - 1)->method(); 536 Bytecode_invoke inv(caller, elem->bci()); 537 // invokedynamic instructions don't have a class but obviously don't have a MemberName appendix. 538 // NOTE: Use machinery here that avoids resolving of any kind. 539 const bool has_member_arg = 540 !inv.is_invokedynamic() && MethodHandles::has_member_arg(inv.klass(), inv.name()); 541 callee_parameters = callee->size_of_parameters() + (has_member_arg ? 1 : 0); 542 callee_locals = callee->max_locals(); 543 } 544 elem->unpack_on_stack(caller_actual_parameters, 545 callee_parameters, 546 callee_locals, 547 caller_frame, 548 index == 0, 549 index == frames() - 1, 550 exec_mode); 551 if (index == frames() - 1) { 552 Deoptimization::unwind_callee_save_values(elem->iframe(), this); 553 } 554 caller_frame = elem->iframe(); 555 caller_actual_parameters = callee_parameters; 556 } 557 deallocate_monitor_chunks(); 558} 559 560void vframeArray::deallocate_monitor_chunks() { 561 JavaThread* jt = JavaThread::current(); 562 for (int index = 0; index < frames(); index++ ) { 563 element(index)->free_monitors(jt); 564 } 565} 566 567#ifndef PRODUCT 568 569bool vframeArray::structural_compare(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk) { 570 if (owner_thread() != thread) return false; 571 int index = 0; 572#if 0 // FIXME can't do this comparison 573 574 // Compare only within vframe array. 575 for (deoptimizedVFrame* vf = deoptimizedVFrame::cast(vframe_at(first_index())); vf; vf = vf->deoptimized_sender_or_null()) { 576 if (index >= chunk->length() || !vf->structural_compare(chunk->at(index))) return false; 577 index++; 578 } 579 if (index != chunk->length()) return false; 580#endif 581 582 return true; 583} 584 585#endif 586 587address vframeArray::register_location(int i) const { 588 assert(0 <= i && i < RegisterMap::reg_count, "index out of bounds"); 589 return (address) & _callee_registers[i]; 590} 591 592 593#ifndef PRODUCT 594 595// Printing 596 597// Note: we cannot have print_on as const, as we allocate inside the method 598void vframeArray::print_on_2(outputStream* st) { 599 st->print_cr(" - sp: " INTPTR_FORMAT, sp()); 600 st->print(" - thread: "); 601 Thread::current()->print(); 602 st->print_cr(" - frame size: %d", frame_size()); 603 for (int index = 0; index < frames() ; index++ ) { 604 element(index)->print(st); 605 } 606} 607 608void vframeArrayElement::print(outputStream* st) { 609 st->print_cr(" - interpreter_frame -> sp: " INTPTR_FORMAT, iframe()->sp()); 610} 611 612void vframeArray::print_value_on(outputStream* st) const { 613 st->print_cr("vframeArray [%d] ", frames()); 614} 615 616 617#endif 618