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