duke@435: /* duke@435: * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: class ValueStack: public CompilationResourceObj { duke@435: private: duke@435: IRScope* _scope; // the enclosing scope duke@435: bool _lock_stack; // indicates that this ValueStack is for an exception site duke@435: Values _locals; // the locals duke@435: Values _stack; // the expression stack duke@435: Values _locks; // the monitor stack (holding the locked values) duke@435: duke@435: Value check(ValueTag tag, Value t) { duke@435: assert(tag == t->type()->tag() || tag == objectTag && t->type()->tag() == addressTag, "types must correspond"); duke@435: return t; duke@435: } duke@435: duke@435: Value check(ValueTag tag, Value t, Value h) { duke@435: assert(h->as_HiWord()->lo_word() == t, "incorrect stack pair"); duke@435: return check(tag, t); duke@435: } duke@435: duke@435: // helper routine duke@435: static void apply(Values list, void f(Value*)); duke@435: duke@435: public: duke@435: // creation duke@435: ValueStack(IRScope* scope, int locals_size, int max_stack_size); duke@435: duke@435: // merging duke@435: ValueStack* copy(); // returns a copy of this w/ cleared locals duke@435: ValueStack* copy_locks(); // returns a copy of this w/ cleared locals and stack duke@435: // Note that when inlining of methods with exception duke@435: // handlers is enabled, this stack may have a duke@435: // non-empty expression stack (size defined by duke@435: // scope()->lock_stack_size()) duke@435: bool is_same(ValueStack* s); // returns true if this & s's types match (w/o checking locals) duke@435: bool is_same_across_scopes(ValueStack* s); // same as is_same but returns true even if stacks are in different scopes (used for block merging w/inlining) duke@435: duke@435: // accessors duke@435: IRScope* scope() const { return _scope; } duke@435: bool is_lock_stack() const { return _lock_stack; } duke@435: int locals_size() const { return _locals.length(); } duke@435: int stack_size() const { return _stack.length(); } duke@435: int locks_size() const { return _locks.length(); } duke@435: int max_stack_size() const { return _stack.capacity(); } duke@435: bool stack_is_empty() const { return _stack.is_empty(); } duke@435: bool no_active_locks() const { return _locks.is_empty(); } duke@435: ValueStack* caller_state() const; duke@435: duke@435: // locals access duke@435: void clear_locals(); // sets all locals to NULL; duke@435: duke@435: // Kill local i. Also kill local i+1 if i was a long or double. duke@435: void invalidate_local(int i) { duke@435: Value x = _locals.at(i); duke@435: if (x != NULL && x->type()->is_double_word()) { duke@435: assert(_locals.at(i + 1)->as_HiWord()->lo_word() == x, "locals inconsistent"); duke@435: _locals.at_put(i + 1, NULL); duke@435: } duke@435: _locals.at_put(i, NULL); duke@435: } duke@435: duke@435: duke@435: Value load_local(int i) const { duke@435: Value x = _locals.at(i); duke@435: if (x != NULL && x->type()->is_illegal()) return NULL; duke@435: assert(x == NULL || x->as_HiWord() == NULL, "index points to hi word"); duke@435: assert(x == NULL || x->type()->is_illegal() || x->type()->is_single_word() || x == _locals.at(i+1)->as_HiWord()->lo_word(), "locals inconsistent"); duke@435: return x; duke@435: } duke@435: duke@435: Value local_at(int i) const { return _locals.at(i); } duke@435: duke@435: // Store x into local i. duke@435: void store_local(int i, Value x) { duke@435: // Kill the old value duke@435: invalidate_local(i); duke@435: _locals.at_put(i, x); duke@435: duke@435: // Writing a double word can kill other locals duke@435: if (x != NULL && x->type()->is_double_word()) { duke@435: // If x + i was the start of a double word local then kill i + 2. duke@435: Value x2 = _locals.at(i + 1); duke@435: if (x2 != NULL && x2->type()->is_double_word()) { duke@435: _locals.at_put(i + 2, NULL); duke@435: } duke@435: duke@435: // If x is a double word local, also update i + 1. duke@435: #ifdef ASSERT duke@435: _locals.at_put(i + 1, x->hi_word()); duke@435: #else duke@435: _locals.at_put(i + 1, NULL); duke@435: #endif duke@435: } duke@435: // If x - 1 was the start of a double word local then kill i - 1. duke@435: if (i > 0) { duke@435: Value prev = _locals.at(i - 1); duke@435: if (prev != NULL && prev->type()->is_double_word()) { duke@435: _locals.at_put(i - 1, NULL); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void replace_locals(ValueStack* with); duke@435: duke@435: // stack access duke@435: Value stack_at(int i) const { duke@435: Value x = _stack.at(i); duke@435: assert(x->as_HiWord() == NULL, "index points to hi word"); duke@435: assert(x->type()->is_single_word() || duke@435: x->subst() == _stack.at(i+1)->as_HiWord()->lo_word(), "stack inconsistent"); duke@435: return x; duke@435: } duke@435: duke@435: Value stack_at_inc(int& i) const { duke@435: Value x = stack_at(i); duke@435: i += x->type()->size(); duke@435: return x; duke@435: } duke@435: duke@435: // pinning support duke@435: void pin_stack_for_linear_scan(); duke@435: duke@435: // iteration duke@435: void values_do(void f(Value*)); duke@435: duke@435: // untyped manipulation (for dup_x1, etc.) duke@435: void clear_stack() { _stack.clear(); } duke@435: void truncate_stack(int size) { _stack.trunc_to(size); } duke@435: void raw_push(Value t) { _stack.push(t); } duke@435: Value raw_pop() { return _stack.pop(); } duke@435: duke@435: // typed manipulation duke@435: void ipush(Value t) { _stack.push(check(intTag , t)); } duke@435: void fpush(Value t) { _stack.push(check(floatTag , t)); } duke@435: void apush(Value t) { _stack.push(check(objectTag , t)); } duke@435: void rpush(Value t) { _stack.push(check(addressTag, t)); } duke@435: #ifdef ASSERT duke@435: // in debug mode, use HiWord for 2-word values duke@435: void lpush(Value t) { _stack.push(check(longTag , t)); _stack.push(new HiWord(t)); } duke@435: void dpush(Value t) { _stack.push(check(doubleTag , t)); _stack.push(new HiWord(t)); } duke@435: #else duke@435: // in optimized mode, use NULL for 2-word values duke@435: void lpush(Value t) { _stack.push(check(longTag , t)); _stack.push(NULL); } duke@435: void dpush(Value t) { _stack.push(check(doubleTag , t)); _stack.push(NULL); } duke@435: #endif // ASSERT duke@435: duke@435: void push(ValueType* type, Value t) { duke@435: switch (type->tag()) { duke@435: case intTag : ipush(t); return; duke@435: case longTag : lpush(t); return; duke@435: case floatTag : fpush(t); return; duke@435: case doubleTag : dpush(t); return; duke@435: case objectTag : apush(t); return; duke@435: case addressTag: rpush(t); return; duke@435: } duke@435: ShouldNotReachHere(); duke@435: } duke@435: duke@435: Value ipop() { return check(intTag , _stack.pop()); } duke@435: Value fpop() { return check(floatTag , _stack.pop()); } duke@435: Value apop() { return check(objectTag , _stack.pop()); } duke@435: Value rpop() { return check(addressTag, _stack.pop()); } duke@435: #ifdef ASSERT duke@435: // in debug mode, check for HiWord consistency duke@435: Value lpop() { Value h = _stack.pop(); return check(longTag , _stack.pop(), h); } duke@435: Value dpop() { Value h = _stack.pop(); return check(doubleTag, _stack.pop(), h); } duke@435: #else duke@435: // in optimized mode, ignore HiWord since it is NULL duke@435: Value lpop() { _stack.pop(); return check(longTag , _stack.pop()); } duke@435: Value dpop() { _stack.pop(); return check(doubleTag, _stack.pop()); } duke@435: #endif // ASSERT duke@435: duke@435: Value pop(ValueType* type) { duke@435: switch (type->tag()) { duke@435: case intTag : return ipop(); duke@435: case longTag : return lpop(); duke@435: case floatTag : return fpop(); duke@435: case doubleTag : return dpop(); duke@435: case objectTag : return apop(); duke@435: case addressTag: return rpop(); duke@435: } duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: duke@435: Values* pop_arguments(int argument_size); duke@435: duke@435: // locks access duke@435: int lock (IRScope* scope, Value obj); duke@435: int unlock(); duke@435: Value lock_at(int i) const { return _locks.at(i); } duke@435: duke@435: // Inlining support duke@435: ValueStack* push_scope(IRScope* scope); // "Push" new scope, returning new resulting stack duke@435: // Preserves stack and locks, destroys locals duke@435: ValueStack* pop_scope(); // "Pop" topmost scope, returning new resulting stack duke@435: // Preserves stack and locks, destroys locals duke@435: duke@435: // SSA form IR support duke@435: void setup_phi_for_stack(BlockBegin* b, int index); duke@435: void setup_phi_for_local(BlockBegin* b, int index); duke@435: duke@435: // debugging duke@435: void print() PRODUCT_RETURN; duke@435: void verify() PRODUCT_RETURN; duke@435: }; duke@435: duke@435: duke@435: duke@435: // Macro definitions for simple iteration of stack and local values of a ValueStack duke@435: // The macros can be used like a for-loop. All variables (state, index and value) duke@435: // must be defined before the loop. duke@435: // When states are nested because of inlining, the stack of the innermost state duke@435: // cumulates also the stack of the nested states. In contrast, the locals of all duke@435: // states must be iterated each. duke@435: // Use the following code pattern to iterate all stack values and all nested local values: duke@435: // duke@435: // ValueStack* state = ... // state that is iterated duke@435: // int index; // current loop index (overwritten in loop) duke@435: // Value value; // value at current loop index (overwritten in loop) duke@435: // duke@435: // for_each_stack_value(state, index, value { duke@435: // do something with value and index duke@435: // } duke@435: // duke@435: // for_each_state(state) { duke@435: // for_each_local_value(state, index, value) { duke@435: // do something with value and index duke@435: // } duke@435: // } duke@435: // as an invariant, state is NULL now duke@435: duke@435: duke@435: // construct a unique variable name with the line number where the macro is used duke@435: #define temp_var3(x) temp__ ## x duke@435: #define temp_var2(x) temp_var3(x) duke@435: #define temp_var temp_var2(__LINE__) duke@435: duke@435: #define for_each_state(state) \ duke@435: for (; state != NULL; state = state->caller_state()) duke@435: duke@435: #define for_each_local_value(state, index, value) \ duke@435: int temp_var = state->locals_size(); \ duke@435: for (index = 0; \ duke@435: index < temp_var && (value = state->local_at(index), true); \ duke@435: index += (value == NULL || value->type()->is_illegal() ? 1 : value->type()->size())) \ duke@435: if (value != NULL) duke@435: duke@435: duke@435: #define for_each_stack_value(state, index, value) \ duke@435: int temp_var = state->stack_size(); \ duke@435: for (index = 0; \ duke@435: index < temp_var && (value = state->stack_at(index), true); \ duke@435: index += value->type()->size()) duke@435: duke@435: duke@435: #define for_each_lock_value(state, index, value) \ duke@435: int temp_var = state->locks_size(); \ duke@435: for (index = 0; \ duke@435: index < temp_var && (value = state->lock_at(index), true); \ duke@435: index++) \ duke@435: if (value != NULL) duke@435: duke@435: duke@435: // Macro definition for simple iteration of all state values of a ValueStack duke@435: // Because the code cannot be executed in a single loop, the code must be passed duke@435: // as a macro parameter. duke@435: // Use the following code pattern to iterate all stack values and all nested local values: duke@435: // duke@435: // ValueStack* state = ... // state that is iterated duke@435: // for_each_state_value(state, value, duke@435: // do something with value (note that this is a macro parameter) duke@435: // ); duke@435: duke@435: #define for_each_state_value(v_state, v_value, v_code) \ duke@435: { \ duke@435: int cur_index; \ duke@435: ValueStack* cur_state = v_state; \ duke@435: Value v_value; \ duke@435: { \ duke@435: for_each_stack_value(cur_state, cur_index, v_value) { \ duke@435: v_code; \ duke@435: } \ duke@435: } \ duke@435: for_each_state(cur_state) { \ duke@435: for_each_local_value(cur_state, cur_index, v_value) { \ duke@435: v_code; \ duke@435: } \ duke@435: } \ duke@435: } duke@435: duke@435: duke@435: // Macro definition for simple iteration of all phif functions of a block, i.e all duke@435: // phi functions of the ValueStack where the block matches. duke@435: // Use the following code pattern to iterate all phi functions of a block: duke@435: // duke@435: // BlockBegin* block = ... // block that is iterated duke@435: // for_each_phi_function(block, phi, duke@435: // do something with the phi function phi (note that this is a macro parameter) duke@435: // ); duke@435: duke@435: #define for_each_phi_fun(v_block, v_phi, v_code) \ duke@435: { \ duke@435: int cur_index; \ duke@435: ValueStack* cur_state = v_block->state(); \ duke@435: Value value; \ duke@435: { \ duke@435: for_each_stack_value(cur_state, cur_index, value) { \ duke@435: Phi* v_phi = value->as_Phi(); \ duke@435: if (v_phi != NULL && v_phi->block() == v_block) { \ duke@435: v_code; \ duke@435: } \ duke@435: } \ duke@435: } \ duke@435: { \ duke@435: for_each_local_value(cur_state, cur_index, value) { \ duke@435: Phi* v_phi = value->as_Phi(); \ duke@435: if (v_phi != NULL && v_phi->block() == v_block) { \ duke@435: v_code; \ duke@435: } \ duke@435: } \ duke@435: } \ duke@435: }