1 /*
   2  * Copyright (c) 1997, 2010, 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 #ifndef SHARE_VM_RUNTIME_VFRAMEARRAY_HPP
  26 #define SHARE_VM_RUNTIME_VFRAMEARRAY_HPP
  27 
  28 #include "oops/arrayOop.hpp"
  29 #include "runtime/deoptimization.hpp"
  30 #include "runtime/frame.inline.hpp"
  31 #include "runtime/monitorChunk.hpp"
  32 #include "utilities/growableArray.hpp"
  33 
  34 // A vframeArray is an array used for momentarily storing off stack Java method activations
  35 // during deoptimization. Essentially it is an array of vframes where each vframe
  36 // data is stored off stack. This structure will never exist across a safepoint so
  37 // there is no need to gc any oops that are stored in the structure.
  38 
  39 
  40 class LocalsClosure;
  41 class ExpressionStackClosure;
  42 class MonitorStackClosure;
  43 class MonitorArrayElement;
  44 class StackValueCollection;
  45 
  46 // A vframeArrayElement is an element of a vframeArray. Each element
  47 // represent an interpreter frame which will eventually be created.
  48 
  49 class vframeArrayElement : public _ValueObj {
  50   private:
  51 
  52     frame _frame;                                                // the interpreter frame we will unpack into
  53     int  _bci;                                                   // raw bci for this vframe
  54     bool _reexecute;                                             // whether sould we reexecute this bytecode
  55     methodOop  _method;                                          // the method for this vframe
  56     MonitorChunk* _monitors;                                     // active monitors for this vframe
  57     StackValueCollection* _locals;
  58     StackValueCollection* _expressions;
  59 
  60   public:
  61 
  62   frame* iframe(void)                { return &_frame; }
  63 
  64   int bci(void) const;
  65 
  66   int raw_bci(void) const            { return _bci; }
  67   bool should_reexecute(void) const  { return _reexecute; }
  68 
  69   methodOop method(void) const       { return _method; }
  70 
  71   MonitorChunk* monitors(void) const { return _monitors; }
  72 
  73   void free_monitors(JavaThread* jt);
  74 
  75   StackValueCollection* locals(void) const             { return _locals; }
  76 
  77   StackValueCollection* expressions(void) const        { return _expressions; }
  78 
  79   void fill_in(compiledVFrame* vf);
  80 
  81   // Formerly part of deoptimizedVFrame
  82 
  83 
  84   // Returns the on stack word size for this frame
  85   // callee_parameters is the number of callee locals residing inside this frame
  86   int on_stack_size(int caller_actual_parameters,
  87                     int callee_parameters,
  88                     int callee_locals,
  89                     bool is_top_frame,
  90                     int popframe_extra_stack_expression_els) const;
  91 
  92   // Unpacks the element to skeletal interpreter frame
  93   void unpack_on_stack(int caller_actual_parameters,
  94                        int callee_parameters,
  95                        int callee_locals,
  96                        frame* caller,
  97                        bool is_top_frame,
  98                        int exec_mode);
  99 
 100 #ifndef PRODUCT
 101   void print(outputStream* st);
 102 #endif /* PRODUCT */
 103 };
 104 
 105 // this can be a ResourceObj if we don't save the last one...
 106 // but it does make debugging easier even if we can't look
 107 // at the data in each vframeElement
 108 
 109 class vframeArray: public CHeapObj {
 110  private:
 111 
 112 
 113   // Here is what a vframeArray looks like in memory
 114 
 115   /*
 116       fixed part
 117         description of the original frame
 118         _frames - number of vframes in this array
 119         adapter info
 120         callee register save area
 121       variable part
 122         vframeArrayElement   [ 0 ]
 123         ...
 124         vframeArrayElement   [_frames - 1]
 125 
 126   */
 127 
 128   JavaThread*                  _owner_thread;
 129   vframeArray*                 _next;
 130   frame                        _original;          // the original frame of the deoptee
 131   frame                        _caller;            // caller of root frame in vframeArray
 132   frame                        _sender;
 133 
 134   Deoptimization::UnrollBlock* _unroll_block;
 135   int                          _frame_size;
 136 
 137   int                          _frames; // number of javavframes in the array (does not count any adapter)
 138 
 139   intptr_t                     _callee_registers[RegisterMap::reg_count];
 140   unsigned char                _valid[RegisterMap::reg_count];
 141 
 142   vframeArrayElement           _elements[1];   // First variable section.
 143 
 144   void fill_in_element(int index, compiledVFrame* vf);
 145 
 146   bool is_location_valid(int i) const        { return _valid[i] != 0; }
 147   void set_location_valid(int i, bool valid) { _valid[i] = valid; }
 148 
 149  public:
 150 
 151 
 152   // Tells whether index is within bounds.
 153   bool is_within_bounds(int index) const        { return 0 <= index && index < frames(); }
 154 
 155   // Accessores for instance variable
 156   int frames() const                            { return _frames;   }
 157 
 158   static vframeArray* allocate(JavaThread* thread, int frame_size, GrowableArray<compiledVFrame*>* chunk,
 159                                RegisterMap* reg_map, frame sender, frame caller, frame self);
 160 
 161 
 162   vframeArrayElement* element(int index)        { assert(is_within_bounds(index), "Bad index"); return &_elements[index]; }
 163 
 164   // Allocates a new vframe in the array and fills the array with vframe information in chunk
 165   void fill_in(JavaThread* thread, int frame_size, GrowableArray<compiledVFrame*>* chunk, const RegisterMap *reg_map);
 166 
 167   // Returns the owner of this vframeArray
 168   JavaThread* owner_thread() const           { return _owner_thread; }
 169 
 170   // Accessors for next
 171   vframeArray* next() const                  { return _next; }
 172   void set_next(vframeArray* value)          { _next = value; }
 173 
 174   // Accessors for sp
 175   intptr_t* sp() const                       { return _original.sp(); }
 176 
 177   intptr_t* unextended_sp() const            { return _original.unextended_sp(); }
 178 
 179   address original_pc() const                { return _original.pc(); }
 180 
 181   frame original() const                     { return _original; }
 182 
 183   frame caller() const                       { return _caller; }
 184 
 185   frame sender() const                       { return _sender; }
 186 
 187   // Accessors for unroll block
 188   Deoptimization::UnrollBlock* unroll_block() const         { return _unroll_block; }
 189   void set_unroll_block(Deoptimization::UnrollBlock* block) { _unroll_block = block; }
 190 
 191   // Returns the size of the frame that got deoptimized
 192   int frame_size() const { return _frame_size; }
 193 
 194   // Unpack the array on the stack passed in stack interval
 195   void unpack_to_stack(frame &unpack_frame, int exec_mode, int caller_actual_parameters);
 196 
 197   // Deallocates monitor chunks allocated during deoptimization.
 198   // This should be called when the array is not used anymore.
 199   void deallocate_monitor_chunks();
 200 
 201 
 202 
 203   // Accessor for register map
 204   address register_location(int i) const;
 205 
 206   void print_on_2(outputStream* st) PRODUCT_RETURN;
 207   void print_value_on(outputStream* st) const PRODUCT_RETURN;
 208 
 209 #ifndef PRODUCT
 210   // Comparing
 211   bool structural_compare(JavaThread* thread, GrowableArray<compiledVFrame*>* chunk);
 212 #endif
 213 
 214 };
 215 
 216 #endif // SHARE_VM_RUNTIME_VFRAMEARRAY_HPP