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