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