1 /*
   2  * Copyright (c) 1999, 2006, 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 class ValueStack: public CompilationResourceObj {
  26  public:
  27   enum Kind {
  28     Parsing,             // During abstract interpretation in GraphBuilder
  29     CallerState,         // Caller state when inlining
  30     StateBefore,         // Before before execution of instruction
  31     StateAfter,          // After execution of instruction
  32     ExceptionState,      // Exception handling of instruction
  33     EmptyExceptionState, // Exception handling of instructions not covered by an xhandler
  34     BlockBeginState      // State of BlockBegin instruction with phi functions of this block
  35   };
  36 
  37  private:
  38   IRScope* _scope;                               // the enclosing scope
  39   ValueStack* _caller_state;
  40   int      _bci;
  41   Kind     _kind;
  42 
  43   Values   _locals;                              // the locals
  44   Values   _stack;                               // the expression stack
  45   Values   _locks;                               // the monitor stack (holding the locked values)
  46 
  47   Value check(ValueTag tag, Value t) {
  48     assert(tag == t->type()->tag() || tag == objectTag && t->type()->tag() == addressTag, "types must correspond");
  49     return t;
  50   }
  51 
  52   Value check(ValueTag tag, Value t, Value h) {
  53     assert(h == NULL, "hi-word of doubleword value must be NULL");
  54     return check(tag, t);
  55   }
  56 
  57   // helper routine
  58   static void apply(Values list, ValueVisitor* f);
  59 
  60   // for simplified copying
  61   ValueStack(ValueStack* copy_from, Kind kind, int bci);
  62 
  63  public:
  64   // creation
  65   ValueStack(IRScope* scope, ValueStack* caller_state);
  66 
  67   ValueStack* copy()                             { return new ValueStack(this, _kind, _bci); }
  68   ValueStack* copy(Kind new_kind, int new_bci)   { return new ValueStack(this, new_kind, new_bci); }
  69   ValueStack* copy_for_parsing()                 { return new ValueStack(this, Parsing, -99); }
  70 
  71   void set_caller_state(ValueStack* s)           {
  72     assert(kind() == EmptyExceptionState ||
  73            (Compilation::current()->env()->jvmti_can_access_local_variables() && kind() == ExceptionState),
  74            "only EmptyExceptionStates can be modified");
  75     _caller_state = s;
  76   }
  77 
  78   bool is_same(ValueStack* s);                   // returns true if this & s's types match (w/o checking locals)
  79 
  80   // accessors
  81   IRScope* scope() const                         { return _scope; }
  82   ValueStack* caller_state() const               { return _caller_state; }
  83   int bci() const                                { return _bci; }
  84   Kind kind() const                              { return _kind; }
  85 
  86   int locals_size() const                        { return _locals.length(); }
  87   int stack_size() const                         { return _stack.length(); }
  88   int locks_size() const                         { return _locks.length(); }
  89   bool stack_is_empty() const                    { return _stack.is_empty(); }
  90   bool no_active_locks() const                   { return _locks.is_empty(); }
  91   int total_locks_size() const;
  92 
  93   // locals access
  94   void clear_locals();                           // sets all locals to NULL;
  95 
  96   void invalidate_local(int i) {
  97     assert(_locals.at(i)->type()->is_single_word() ||
  98            _locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
  99     _locals.at_put(i, NULL);
 100   }
 101 
 102   Value local_at(int i) const {
 103     Value x = _locals.at(i);
 104     assert(x == NULL || x->type()->is_single_word() ||
 105            _locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
 106     return x;
 107   }
 108 
 109   void store_local(int i, Value x) {
 110     // When overwriting local i, check if i - 1 was the start of a
 111     // double word local and kill it.
 112     if (i > 0) {
 113       Value prev = _locals.at(i - 1);
 114       if (prev != NULL && prev->type()->is_double_word()) {
 115         _locals.at_put(i - 1, NULL);
 116       }
 117     }
 118 
 119     _locals.at_put(i, x);
 120     if (x->type()->is_double_word()) {
 121       // hi-word of doubleword value is always NULL
 122       _locals.at_put(i + 1, NULL);
 123     }
 124   }
 125 
 126   // stack access
 127   Value stack_at(int i) const {
 128     Value x = _stack.at(i);
 129     assert(x->type()->is_single_word() ||
 130            _stack.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
 131     return x;
 132   }
 133 
 134   Value stack_at_inc(int& i) const {
 135     Value x = stack_at(i);
 136     i += x->type()->size();
 137     return x;
 138   }
 139 
 140   // pinning support
 141   void pin_stack_for_linear_scan();
 142 
 143   // iteration
 144   void values_do(ValueVisitor* f);
 145 
 146   // untyped manipulation (for dup_x1, etc.)
 147   void truncate_stack(int size)                  { _stack.trunc_to(size); }
 148   void raw_push(Value t)                         { _stack.push(t); }
 149   Value raw_pop()                                { return _stack.pop(); }
 150 
 151   // typed manipulation
 152   void ipush(Value t)                            { _stack.push(check(intTag    , t)); }
 153   void fpush(Value t)                            { _stack.push(check(floatTag  , t)); }
 154   void apush(Value t)                            { _stack.push(check(objectTag , t)); }
 155   void rpush(Value t)                            { _stack.push(check(addressTag, t)); }
 156   void lpush(Value t)                            { _stack.push(check(longTag   , t)); _stack.push(NULL); }
 157   void dpush(Value t)                            { _stack.push(check(doubleTag , t)); _stack.push(NULL); }
 158 
 159   void push(ValueType* type, Value t) {
 160     switch (type->tag()) {
 161       case intTag    : ipush(t); return;
 162       case longTag   : lpush(t); return;
 163       case floatTag  : fpush(t); return;
 164       case doubleTag : dpush(t); return;
 165       case objectTag : apush(t); return;
 166       case addressTag: rpush(t); return;
 167     }
 168     ShouldNotReachHere();
 169   }
 170 
 171   Value ipop()                                   { return check(intTag    , _stack.pop()); }
 172   Value fpop()                                   { return check(floatTag  , _stack.pop()); }
 173   Value apop()                                   { return check(objectTag , _stack.pop()); }
 174   Value rpop()                                   { return check(addressTag, _stack.pop()); }
 175   Value lpop()                                   { Value h = _stack.pop(); return check(longTag  , _stack.pop(), h); }
 176   Value dpop()                                   { Value h = _stack.pop(); return check(doubleTag, _stack.pop(), h); }
 177 
 178   Value pop(ValueType* type) {
 179     switch (type->tag()) {
 180       case intTag    : return ipop();
 181       case longTag   : return lpop();
 182       case floatTag  : return fpop();
 183       case doubleTag : return dpop();
 184       case objectTag : return apop();
 185       case addressTag: return rpop();
 186     }
 187     ShouldNotReachHere();
 188     return NULL;
 189   }
 190 
 191   Values* pop_arguments(int argument_size);
 192 
 193   // locks access
 194   int lock  (Value obj);
 195   int unlock();
 196   Value lock_at(int i) const                     { return _locks.at(i); }
 197 
 198   // SSA form IR support
 199   void setup_phi_for_stack(BlockBegin* b, int index);
 200   void setup_phi_for_local(BlockBegin* b, int index);
 201 
 202   // debugging
 203   void print()  PRODUCT_RETURN;
 204   void verify() PRODUCT_RETURN;
 205 };
 206 
 207 
 208 
 209 // Macro definitions for simple iteration of stack and local values of a ValueStack
 210 // The macros can be used like a for-loop. All variables (state, index and value)
 211 // must be defined before the loop.
 212 // When states are nested because of inlining, the stack of the innermost state
 213 // cumulates also the stack of the nested states. In contrast, the locals of all
 214 // states must be iterated each.
 215 // Use the following code pattern to iterate all stack values and all nested local values:
 216 //
 217 // ValueStack* state = ...   // state that is iterated
 218 // int index;                // current loop index (overwritten in loop)
 219 // Value value;              // value at current loop index (overwritten in loop)
 220 //
 221 // for_each_stack_value(state, index, value {
 222 //   do something with value and index
 223 // }
 224 //
 225 // for_each_state(state) {
 226 //   for_each_local_value(state, index, value) {
 227 //     do something with value and index
 228 //   }
 229 // }
 230 // as an invariant, state is NULL now
 231 
 232 
 233 // construct a unique variable name with the line number where the macro is used
 234 #define temp_var3(x) temp__ ## x
 235 #define temp_var2(x) temp_var3(x)
 236 #define temp_var     temp_var2(__LINE__)
 237 
 238 #define for_each_state(state)  \
 239   for (; state != NULL; state = state->caller_state())
 240 
 241 #define for_each_local_value(state, index, value)                                              \
 242   int temp_var = state->locals_size();                                                         \
 243   for (index = 0;                                                                              \
 244        index < temp_var && (value = state->local_at(index), true);                             \
 245        index += (value == NULL || value->type()->is_illegal() ? 1 : value->type()->size()))    \
 246     if (value != NULL)
 247 
 248 
 249 #define for_each_stack_value(state, index, value)                                              \
 250   int temp_var = state->stack_size();                                                          \
 251   for (index = 0;                                                                              \
 252        index < temp_var && (value = state->stack_at(index), true);                             \
 253        index += value->type()->size())
 254 
 255 
 256 #define for_each_lock_value(state, index, value)                                               \
 257   int temp_var = state->locks_size();                                                          \
 258   for (index = 0;                                                                              \
 259        index < temp_var && (value = state->lock_at(index), true);                              \
 260        index++)                                                                                \
 261     if (value != NULL)
 262 
 263 
 264 // Macro definition for simple iteration of all state values of a ValueStack
 265 // Because the code cannot be executed in a single loop, the code must be passed
 266 // as a macro parameter.
 267 // Use the following code pattern to iterate all stack values and all nested local values:
 268 //
 269 // ValueStack* state = ...   // state that is iterated
 270 // for_each_state_value(state, value,
 271 //   do something with value (note that this is a macro parameter)
 272 // );
 273 
 274 #define for_each_state_value(v_state, v_value, v_code)                                         \
 275 {                                                                                              \
 276   int cur_index;                                                                               \
 277   ValueStack* cur_state = v_state;                                                             \
 278   Value v_value;                                                                               \
 279   for_each_state(cur_state) {                                                                  \
 280     {                                                                                            \
 281       for_each_local_value(cur_state, cur_index, v_value) {                                      \
 282         v_code;                                                                                  \
 283       }                                                                                          \
 284     }                                                                                          \
 285     {                                                                                            \
 286       for_each_stack_value(cur_state, cur_index, v_value) {                                      \
 287         v_code;                                                                                  \
 288       }                                                                                          \
 289     }                                                                                            \
 290   }                                                                                            \
 291 }
 292 
 293 
 294 // Macro definition for simple iteration of all phif functions of a block, i.e all
 295 // phi functions of the ValueStack where the block matches.
 296 // Use the following code pattern to iterate all phi functions of a block:
 297 //
 298 // BlockBegin* block = ...   // block that is iterated
 299 // for_each_phi_function(block, phi,
 300 //   do something with the phi function phi (note that this is a macro parameter)
 301 // );
 302 
 303 #define for_each_phi_fun(v_block, v_phi, v_code)                                               \
 304 {                                                                                              \
 305   int cur_index;                                                                               \
 306   ValueStack* cur_state = v_block->state();                                                    \
 307   Value value;                                                                                 \
 308   {                                                                                            \
 309     for_each_stack_value(cur_state, cur_index, value) {                                        \
 310       Phi* v_phi = value->as_Phi();                                                      \
 311       if (v_phi != NULL && v_phi->block() == v_block) {                                        \
 312         v_code;                                                                                \
 313       }                                                                                        \
 314     }                                                                                          \
 315   }                                                                                            \
 316   {                                                                                            \
 317     for_each_local_value(cur_state, cur_index, value) {                                        \
 318       Phi* v_phi = value->as_Phi();                                                      \
 319       if (v_phi != NULL && v_phi->block() == v_block) {                                        \
 320         v_code;                                                                                \
 321       }                                                                                        \
 322     }                                                                                          \
 323   }                                                                                            \
 324 }