vframeArray.cpp revision 0:a61af66fc99e
1181905Sed/* 2181905Sed * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. 3181905Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4181905Sed * 5181905Sed * This code is free software; you can redistribute it and/or modify it 6181905Sed * under the terms of the GNU General Public License version 2 only, as 7181905Sed * published by the Free Software Foundation. 8181905Sed * 9181905Sed * This code is distributed in the hope that it will be useful, but WITHOUT 10181905Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11181905Sed * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12181905Sed * version 2 for more details (a copy is included in the LICENSE file that 13181905Sed * accompanied this code). 14181905Sed * 15181905Sed * You should have received a copy of the GNU General Public License version 16181905Sed * 2 along with this work; if not, write to the Free Software Foundation, 17181905Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18181905Sed * 19181905Sed * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20181905Sed * CA 95054 USA or visit www.sun.com if you need additional information or 21181905Sed * have any questions. 22181905Sed * 23181905Sed */ 24181905Sed 25181905Sed# include "incls/_precompiled.incl" 26181905Sed# include "incls/_vframeArray.cpp.incl" 27181905Sed 28181905Sed 29181905Sedint vframeArrayElement:: bci(void) const { return (_bci == SynchronizationEntryBCI ? 0 : _bci); } 30181905Sed 31181905Sedvoid vframeArrayElement::free_monitors(JavaThread* jt) { 32181905Sed if (_monitors != NULL) { 33181905Sed MonitorChunk* chunk = _monitors; 34181905Sed _monitors = NULL; 35181905Sed jt->remove_monitor_chunk(chunk); 36181905Sed delete chunk; 37181905Sed } 38181905Sed} 39181905Sed 40181905Sedvoid vframeArrayElement::fill_in(compiledVFrame* vf) { 41188497Sed 42181905Sed// Copy the information from the compiled vframe to the 43181905Sed// interpreter frame we will be creating to replace vf 44181905Sed 45181905Sed _method = vf->method(); 46181905Sed _bci = vf->raw_bci(); 47181905Sed 48181905Sed int index; 49181905Sed 50181905Sed // Get the monitors off-stack 51181905Sed 52181905Sed GrowableArray<MonitorInfo*>* list = vf->monitors(); 53181905Sed if (list->is_empty()) { 54181905Sed _monitors = NULL; 55181905Sed } else { 56181905Sed 57181905Sed // Allocate monitor chunk 58181905Sed _monitors = new MonitorChunk(list->length()); 59181905Sed vf->thread()->add_monitor_chunk(_monitors); 60181905Sed 61181905Sed // Migrate the BasicLocks from the stack to the monitor chunk 62181905Sed for (index = 0; index < list->length(); index++) { 63181905Sed MonitorInfo* monitor = list->at(index); 64181905Sed assert(monitor->owner() == NULL || (!monitor->owner()->is_unlocked() && !monitor->owner()->has_bias_pattern()), "object must be null or locked, and unbiased"); 65181905Sed BasicObjectLock* dest = _monitors->at(index); 66181905Sed dest->set_obj(monitor->owner()); 67181905Sed monitor->lock()->move_to(monitor->owner(), dest->lock()); 68181905Sed } 69181905Sed } 70181905Sed 71181905Sed // Convert the vframe locals and expressions to off stack 72181905Sed // values. Because we will not gc all oops can be converted to 73181905Sed // intptr_t (i.e. a stack slot) and we are fine. This is 74181905Sed // good since we are inside a HandleMark and the oops in our 75181905Sed // collection would go away between packing them here and 76181905Sed // unpacking them in unpack_on_stack. 77181905Sed 78181905Sed // First the locals go off-stack 79181905Sed 80181905Sed // FIXME this seems silly it creates a StackValueCollection 81181905Sed // in order to get the size to then copy them and 82181905Sed // convert the types to intptr_t size slots. Seems like it 83181905Sed // could do it in place... Still uses less memory than the 84223576Sed // old way though 85188497Sed 86188497Sed StackValueCollection *locs = vf->locals(); 87181905Sed _locals = new StackValueCollection(locs->size()); 88181905Sed for(index = 0; index < locs->size(); index++) { 89240412Semaste StackValue* value = locs->at(index); 90181905Sed switch(value->type()) { 91181905Sed case T_OBJECT: 92 // preserve object type 93 _locals->add( new StackValue((intptr_t) (value->get_obj()()), T_OBJECT )); 94 break; 95 case T_CONFLICT: 96 // A dead local. Will be initialized to null/zero. 97 _locals->add( new StackValue()); 98 break; 99 case T_INT: 100 _locals->add( new StackValue(value->get_int())); 101 break; 102 default: 103 ShouldNotReachHere(); 104 } 105 } 106 107 // Now the expressions off-stack 108 // Same silliness as above 109 110 StackValueCollection *exprs = vf->expressions(); 111 _expressions = new StackValueCollection(exprs->size()); 112 for(index = 0; index < exprs->size(); index++) { 113 StackValue* value = exprs->at(index); 114 switch(value->type()) { 115 case T_OBJECT: 116 // preserve object type 117 _expressions->add( new StackValue((intptr_t) (value->get_obj()()), T_OBJECT )); 118 break; 119 case T_CONFLICT: 120 // A dead stack element. Will be initialized to null/zero. 121 // This can occur when the compiler emits a state in which stack 122 // elements are known to be dead (because of an imminent exception). 123 _expressions->add( new StackValue()); 124 break; 125 case T_INT: 126 _expressions->add( new StackValue(value->get_int())); 127 break; 128 default: 129 ShouldNotReachHere(); 130 } 131 } 132} 133 134int unpack_counter = 0; 135 136void vframeArrayElement::unpack_on_stack(int callee_parameters, 137 int callee_locals, 138 frame* caller, 139 bool is_top_frame, 140 int exec_mode) { 141 JavaThread* thread = (JavaThread*) Thread::current(); 142 143 // Look at bci and decide on bcp and continuation pc 144 address bcp; 145 // C++ interpreter doesn't need a pc since it will figure out what to do when it 146 // begins execution 147 address pc; 148 bool use_next_mdp; // true if we should use the mdp associated with the next bci 149 // rather than the one associated with bcp 150 if (raw_bci() == SynchronizationEntryBCI) { 151 // We are deoptimizing while hanging in prologue code for synchronized method 152 bcp = method()->bcp_from(0); // first byte code 153 pc = Interpreter::deopt_entry(vtos, 0); // step = 0 since we don't skip current bytecode 154 use_next_mdp = false; 155 } else { 156 bcp = method()->bcp_from(bci()); 157 pc = Interpreter::continuation_for(method(), bcp, callee_parameters, is_top_frame, use_next_mdp); 158 } 159 assert(Bytecodes::is_defined(*bcp), "must be a valid bytecode"); 160 161 // Monitorenter and pending exceptions: 162 // 163 // For Compiler2, there should be no pending exception when deoptimizing at monitorenter 164 // because there is no safepoint at the null pointer check (it is either handled explicitly 165 // or prior to the monitorenter) and asynchronous exceptions are not made "pending" by the 166 // runtime interface for the slow case (see JRT_ENTRY_FOR_MONITORENTER). If an asynchronous 167 // exception was processed, the bytecode pointer would have to be extended one bytecode beyond 168 // the monitorenter to place it in the proper exception range. 169 // 170 // For Compiler1, deoptimization can occur while throwing a NullPointerException at monitorenter, 171 // in which case bcp should point to the monitorenter since it is within the exception's range. 172 173 assert(*bcp != Bytecodes::_monitorenter || is_top_frame, "a _monitorenter must be a top frame"); 174 // TIERED Must know the compiler of the deoptee QQQ 175 COMPILER2_PRESENT(guarantee(*bcp != Bytecodes::_monitorenter || exec_mode != Deoptimization::Unpack_exception, 176 "shouldn't get exception during monitorenter");) 177 178 int popframe_preserved_args_size_in_bytes = 0; 179 int popframe_preserved_args_size_in_words = 0; 180 if (is_top_frame) { 181 JvmtiThreadState *state = thread->jvmti_thread_state(); 182 if (JvmtiExport::can_pop_frame() && 183 (thread->has_pending_popframe() || thread->popframe_forcing_deopt_reexecution())) { 184 if (thread->has_pending_popframe()) { 185 // Pop top frame after deoptimization 186#ifndef CC_INTERP 187 pc = Interpreter::remove_activation_preserving_args_entry(); 188#else 189 // Do an uncommon trap type entry. c++ interpreter will know 190 // to pop frame and preserve the args 191 pc = Interpreter::deopt_entry(vtos, 0); 192 use_next_mdp = false; 193#endif 194 } else { 195 // Reexecute invoke in top frame 196 pc = Interpreter::deopt_entry(vtos, 0); 197 use_next_mdp = false; 198 popframe_preserved_args_size_in_bytes = in_bytes(thread->popframe_preserved_args_size()); 199 // Note: the PopFrame-related extension of the expression stack size is done in 200 // Deoptimization::fetch_unroll_info_helper 201 popframe_preserved_args_size_in_words = in_words(thread->popframe_preserved_args_size_in_words()); 202 } 203 } else if (JvmtiExport::can_force_early_return() && state != NULL && state->is_earlyret_pending()) { 204 // Force early return from top frame after deoptimization 205#ifndef CC_INTERP 206 pc = Interpreter::remove_activation_early_entry(state->earlyret_tos()); 207#else 208 // TBD: Need to implement ForceEarlyReturn for CC_INTERP (ia64) 209#endif 210 } else { 211 // Possibly override the previous pc computation of the top (youngest) frame 212 switch (exec_mode) { 213 case Deoptimization::Unpack_deopt: 214 // use what we've got 215 break; 216 case Deoptimization::Unpack_exception: 217 // exception is pending 218 pc = SharedRuntime::raw_exception_handler_for_return_address(pc); 219 // [phh] We're going to end up in some handler or other, so it doesn't 220 // matter what mdp we point to. See exception_handler_for_exception() 221 // in interpreterRuntime.cpp. 222 break; 223 case Deoptimization::Unpack_uncommon_trap: 224 case Deoptimization::Unpack_reexecute: 225 // redo last byte code 226 pc = Interpreter::deopt_entry(vtos, 0); 227 use_next_mdp = false; 228 break; 229 default: 230 ShouldNotReachHere(); 231 } 232 } 233 } 234 235 // Setup the interpreter frame 236 237 assert(method() != NULL, "method must exist"); 238 int temps = expressions()->size(); 239 240 int locks = monitors() == NULL ? 0 : monitors()->number_of_monitors(); 241 242 Interpreter::layout_activation(method(), 243 temps + callee_parameters, 244 popframe_preserved_args_size_in_words, 245 locks, 246 callee_parameters, 247 callee_locals, 248 caller, 249 iframe(), 250 is_top_frame); 251 252 // Update the pc in the frame object and overwrite the temporary pc 253 // we placed in the skeletal frame now that we finally know the 254 // exact interpreter address we should use. 255 256 _frame.patch_pc(thread, pc); 257 258 assert (!method()->is_synchronized() || locks > 0, "synchronized methods must have monitors"); 259 260 BasicObjectLock* top = iframe()->interpreter_frame_monitor_begin(); 261 for (int index = 0; index < locks; index++) { 262 top = iframe()->previous_monitor_in_interpreter_frame(top); 263 BasicObjectLock* src = _monitors->at(index); 264 top->set_obj(src->obj()); 265 src->lock()->move_to(src->obj(), top->lock()); 266 } 267 if (ProfileInterpreter) { 268 iframe()->interpreter_frame_set_mdx(0); // clear out the mdp. 269 } 270 iframe()->interpreter_frame_set_bcx((intptr_t)bcp); // cannot use bcp because frame is not initialized yet 271 if (ProfileInterpreter) { 272 methodDataOop mdo = method()->method_data(); 273 if (mdo != NULL) { 274 int bci = iframe()->interpreter_frame_bci(); 275 if (use_next_mdp) ++bci; 276 address mdp = mdo->bci_to_dp(bci); 277 iframe()->interpreter_frame_set_mdp(mdp); 278 } 279 } 280 281 // Unpack expression stack 282 // If this is an intermediate frame (i.e. not top frame) then this 283 // only unpacks the part of the expression stack not used by callee 284 // as parameters. The callee parameters are unpacked as part of the 285 // callee locals. 286 int i; 287 for(i = 0; i < expressions()->size(); i++) { 288 StackValue *value = expressions()->at(i); 289 intptr_t* addr = iframe()->interpreter_frame_expression_stack_at(i); 290 switch(value->type()) { 291 case T_INT: 292 *addr = value->get_int(); 293 break; 294 case T_OBJECT: 295 *addr = value->get_int(T_OBJECT); 296 break; 297 case T_CONFLICT: 298 // A dead stack slot. Initialize to null in case it is an oop. 299 *addr = NULL_WORD; 300 break; 301 default: 302 ShouldNotReachHere(); 303 } 304 if (TaggedStackInterpreter) { 305 // Write tag to the stack 306 iframe()->interpreter_frame_set_expression_stack_tag(i, 307 frame::tag_for_basic_type(value->type())); 308 } 309 } 310 311 312 // Unpack the locals 313 for(i = 0; i < locals()->size(); i++) { 314 StackValue *value = locals()->at(i); 315 intptr_t* addr = iframe()->interpreter_frame_local_at(i); 316 switch(value->type()) { 317 case T_INT: 318 *addr = value->get_int(); 319 break; 320 case T_OBJECT: 321 *addr = value->get_int(T_OBJECT); 322 break; 323 case T_CONFLICT: 324 // A dead location. If it is an oop then we need a NULL to prevent GC from following it 325 *addr = NULL_WORD; 326 break; 327 default: 328 ShouldNotReachHere(); 329 } 330 if (TaggedStackInterpreter) { 331 // Write tag to stack 332 iframe()->interpreter_frame_set_local_tag(i, 333 frame::tag_for_basic_type(value->type())); 334 } 335 } 336 337 if (is_top_frame && JvmtiExport::can_pop_frame() && thread->popframe_forcing_deopt_reexecution()) { 338 // An interpreted frame was popped but it returns to a deoptimized 339 // frame. The incoming arguments to the interpreted activation 340 // were preserved in thread-local storage by the 341 // remove_activation_preserving_args_entry in the interpreter; now 342 // we put them back into the just-unpacked interpreter frame. 343 // Note that this assumes that the locals arena grows toward lower 344 // addresses. 345 if (popframe_preserved_args_size_in_words != 0) { 346 void* saved_args = thread->popframe_preserved_args(); 347 assert(saved_args != NULL, "must have been saved by interpreter"); 348#ifdef ASSERT 349 int stack_words = Interpreter::stackElementWords(); 350 assert(popframe_preserved_args_size_in_words <= 351 iframe()->interpreter_frame_expression_stack_size()*stack_words, 352 "expression stack size should have been extended"); 353#endif // ASSERT 354 int top_element = iframe()->interpreter_frame_expression_stack_size()-1; 355 intptr_t* base; 356 if (frame::interpreter_frame_expression_stack_direction() < 0) { 357 base = iframe()->interpreter_frame_expression_stack_at(top_element); 358 } else { 359 base = iframe()->interpreter_frame_expression_stack(); 360 } 361 Copy::conjoint_bytes(saved_args, 362 base, 363 popframe_preserved_args_size_in_bytes); 364 thread->popframe_free_preserved_args(); 365 } 366 } 367 368#ifndef PRODUCT 369 if (TraceDeoptimization && Verbose) { 370 ttyLocker ttyl; 371 tty->print_cr("[%d Interpreted Frame]", ++unpack_counter); 372 iframe()->print_on(tty); 373 RegisterMap map(thread); 374 vframe* f = vframe::new_vframe(iframe(), &map, thread); 375 f->print(); 376 iframe()->interpreter_frame_print_on(tty); 377 378 tty->print_cr("locals size %d", locals()->size()); 379 tty->print_cr("expression size %d", expressions()->size()); 380 381 method()->print_value(); 382 tty->cr(); 383 // method()->print_codes(); 384 } else if (TraceDeoptimization) { 385 tty->print(" "); 386 method()->print_value(); 387 Bytecodes::Code code = Bytecodes::java_code_at(bcp); 388 int bci = method()->bci_from(bcp); 389 tty->print(" - %s", Bytecodes::name(code)); 390 tty->print(" @ bci %d ", bci); 391 tty->print_cr("sp = " PTR_FORMAT, iframe()->sp()); 392 } 393#endif // PRODUCT 394 395 // The expression stack and locals are in the resource area don't leave 396 // a dangling pointer in the vframeArray we leave around for debug 397 // purposes 398 399 _locals = _expressions = NULL; 400 401} 402 403int vframeArrayElement::on_stack_size(int callee_parameters, 404 int callee_locals, 405 bool is_top_frame, 406 int popframe_extra_stack_expression_els) const { 407 assert(method()->max_locals() == locals()->size(), "just checking"); 408 int locks = monitors() == NULL ? 0 : monitors()->number_of_monitors(); 409 int temps = expressions()->size(); 410 return Interpreter::size_activation(method(), 411 temps + callee_parameters, 412 popframe_extra_stack_expression_els, 413 locks, 414 callee_parameters, 415 callee_locals, 416 is_top_frame); 417} 418 419 420 421vframeArray* vframeArray::allocate(JavaThread* thread, int frame_size, GrowableArray<compiledVFrame*>* chunk, 422 RegisterMap *reg_map, frame sender, frame caller, frame self) { 423 424 // Allocate the vframeArray 425 vframeArray * result = (vframeArray*) AllocateHeap(sizeof(vframeArray) + // fixed part 426 sizeof(vframeArrayElement) * (chunk->length() - 1), // variable part 427 "vframeArray::allocate"); 428 result->_frames = chunk->length(); 429 result->_owner_thread = thread; 430 result->_sender = sender; 431 result->_caller = caller; 432 result->_original = self; 433 result->set_unroll_block(NULL); // initialize it 434 result->fill_in(thread, frame_size, chunk, reg_map); 435 return result; 436} 437 438void vframeArray::fill_in(JavaThread* thread, 439 int frame_size, 440 GrowableArray<compiledVFrame*>* chunk, 441 const RegisterMap *reg_map) { 442 // Set owner first, it is used when adding monitor chunks 443 444 _frame_size = frame_size; 445 for(int i = 0; i < chunk->length(); i++) { 446 element(i)->fill_in(chunk->at(i)); 447 } 448 449 // Copy registers for callee-saved registers 450 if (reg_map != NULL) { 451 for(int i = 0; i < RegisterMap::reg_count; i++) { 452#ifdef AMD64 453 // The register map has one entry for every int (32-bit value), so 454 // 64-bit physical registers have two entries in the map, one for 455 // each half. Ignore the high halves of 64-bit registers, just like 456 // frame::oopmapreg_to_location does. 457 // 458 // [phh] FIXME: this is a temporary hack! This code *should* work 459 // correctly w/o this hack, possibly by changing RegisterMap::pd_location 460 // in frame_amd64.cpp and the values of the phantom high half registers 461 // in amd64.ad. 462 // if (VMReg::Name(i) < SharedInfo::stack0 && is_even(i)) { 463 intptr_t* src = (intptr_t*) reg_map->location(VMRegImpl::as_VMReg(i)); 464 _callee_registers[i] = src != NULL ? *src : NULL_WORD; 465 // } else { 466 // jint* src = (jint*) reg_map->location(VMReg::Name(i)); 467 // _callee_registers[i] = src != NULL ? *src : NULL_WORD; 468 // } 469#else 470 jint* src = (jint*) reg_map->location(VMRegImpl::as_VMReg(i)); 471 _callee_registers[i] = src != NULL ? *src : NULL_WORD; 472#endif 473 if (src == NULL) { 474 set_location_valid(i, false); 475 } else { 476 set_location_valid(i, true); 477 jint* dst = (jint*) register_location(i); 478 *dst = *src; 479 } 480 } 481 } 482} 483 484void vframeArray::unpack_to_stack(frame &unpack_frame, int exec_mode) { 485 // stack picture 486 // unpack_frame 487 // [new interpreter frames ] (frames are skeletal but walkable) 488 // caller_frame 489 // 490 // This routine fills in the missing data for the skeletal interpreter frames 491 // in the above picture. 492 493 // Find the skeletal interpreter frames to unpack into 494 RegisterMap map(JavaThread::current(), false); 495 // Get the youngest frame we will unpack (last to be unpacked) 496 frame me = unpack_frame.sender(&map); 497 int index; 498 for (index = 0; index < frames(); index++ ) { 499 *element(index)->iframe() = me; 500 // Get the caller frame (possibly skeletal) 501 me = me.sender(&map); 502 } 503 504 frame caller_frame = me; 505 506 // Do the unpacking of interpreter frames; the frame at index 0 represents the top activation, so it has no callee 507 508 // Unpack the frames from the oldest (frames() -1) to the youngest (0) 509 510 for (index = frames() - 1; index >= 0 ; index--) { 511 int callee_parameters = index == 0 ? 0 : element(index-1)->method()->size_of_parameters(); 512 int callee_locals = index == 0 ? 0 : element(index-1)->method()->max_locals(); 513 element(index)->unpack_on_stack(callee_parameters, 514 callee_locals, 515 &caller_frame, 516 index == 0, 517 exec_mode); 518 if (index == frames() - 1) { 519 Deoptimization::unwind_callee_save_values(element(index)->iframe(), this); 520 } 521 caller_frame = *element(index)->iframe(); 522 } 523 524 525 deallocate_monitor_chunks(); 526} 527 528void vframeArray::deallocate_monitor_chunks() { 529 JavaThread* jt = JavaThread::current(); 530 for (int index = 0; index < frames(); index++ ) { 531 element(index)->free_monitors(jt); 532 } 533} 534 535#ifndef PRODUCT 536 537bool vframeArray::structural_compare(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk) { 538 if (owner_thread() != thread) return false; 539 int index = 0; 540#if 0 // FIXME can't do this comparison 541 542 // Compare only within vframe array. 543 for (deoptimizedVFrame* vf = deoptimizedVFrame::cast(vframe_at(first_index())); vf; vf = vf->deoptimized_sender_or_null()) { 544 if (index >= chunk->length() || !vf->structural_compare(chunk->at(index))) return false; 545 index++; 546 } 547 if (index != chunk->length()) return false; 548#endif 549 550 return true; 551} 552 553#endif 554 555address vframeArray::register_location(int i) const { 556 assert(0 <= i && i < RegisterMap::reg_count, "index out of bounds"); 557 return (address) & _callee_registers[i]; 558} 559 560 561#ifndef PRODUCT 562 563// Printing 564 565// Note: we cannot have print_on as const, as we allocate inside the method 566void vframeArray::print_on_2(outputStream* st) { 567 st->print_cr(" - sp: " INTPTR_FORMAT, sp()); 568 st->print(" - thread: "); 569 Thread::current()->print(); 570 st->print_cr(" - frame size: %d", frame_size()); 571 for (int index = 0; index < frames() ; index++ ) { 572 element(index)->print(st); 573 } 574} 575 576void vframeArrayElement::print(outputStream* st) { 577 st->print_cr(" - interpreter_frame -> sp: ", INTPTR_FORMAT, iframe()->sp()); 578} 579 580void vframeArray::print_value_on(outputStream* st) const { 581 st->print_cr("vframeArray [%d] ", frames()); 582} 583 584 585#endif 586