aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_C1_C1_VALUESTACK_HPP aoqi@0: #define SHARE_VM_C1_C1_VALUESTACK_HPP aoqi@0: aoqi@0: #include "c1/c1_Instruction.hpp" aoqi@0: aoqi@0: class ValueStack: public CompilationResourceObj { aoqi@0: public: aoqi@0: enum Kind { aoqi@0: Parsing, // During abstract interpretation in GraphBuilder aoqi@0: CallerState, // Caller state when inlining aoqi@0: StateBefore, // Before before execution of instruction aoqi@0: StateAfter, // After execution of instruction aoqi@0: ExceptionState, // Exception handling of instruction aoqi@0: EmptyExceptionState, // Exception handling of instructions not covered by an xhandler aoqi@0: BlockBeginState // State of BlockBegin instruction with phi functions of this block aoqi@0: }; aoqi@0: aoqi@0: private: aoqi@0: IRScope* _scope; // the enclosing scope aoqi@0: ValueStack* _caller_state; aoqi@0: int _bci; aoqi@0: Kind _kind; aoqi@0: aoqi@0: Values _locals; // the locals aoqi@0: Values _stack; // the expression stack aoqi@0: Values _locks; // the monitor stack (holding the locked values) aoqi@0: aoqi@0: Value check(ValueTag tag, Value t) { aoqi@0: assert(tag == t->type()->tag() || tag == objectTag && t->type()->tag() == addressTag, "types must correspond"); aoqi@0: return t; aoqi@0: } aoqi@0: aoqi@0: Value check(ValueTag tag, Value t, Value h) { aoqi@0: assert(h == NULL, "hi-word of doubleword value must be NULL"); aoqi@0: return check(tag, t); aoqi@0: } aoqi@0: aoqi@0: // helper routine aoqi@0: static void apply(Values list, ValueVisitor* f); aoqi@0: aoqi@0: // for simplified copying aoqi@0: ValueStack(ValueStack* copy_from, Kind kind, int bci); aoqi@0: aoqi@0: public: aoqi@0: // creation aoqi@0: ValueStack(IRScope* scope, ValueStack* caller_state); aoqi@0: aoqi@0: ValueStack* copy() { return new ValueStack(this, _kind, _bci); } aoqi@0: ValueStack* copy(Kind new_kind, int new_bci) { return new ValueStack(this, new_kind, new_bci); } aoqi@0: ValueStack* copy_for_parsing() { return new ValueStack(this, Parsing, -99); } aoqi@0: aoqi@0: void set_caller_state(ValueStack* s) { aoqi@0: assert(kind() == EmptyExceptionState || aoqi@0: (Compilation::current()->env()->jvmti_can_access_local_variables() && kind() == ExceptionState), aoqi@0: "only EmptyExceptionStates can be modified"); aoqi@0: _caller_state = s; aoqi@0: } aoqi@0: aoqi@0: bool is_same(ValueStack* s); // returns true if this & s's types match (w/o checking locals) aoqi@0: aoqi@0: // accessors aoqi@0: IRScope* scope() const { return _scope; } aoqi@0: ValueStack* caller_state() const { return _caller_state; } aoqi@0: int bci() const { return _bci; } aoqi@0: Kind kind() const { return _kind; } aoqi@0: aoqi@0: int locals_size() const { return _locals.length(); } aoqi@0: int stack_size() const { return _stack.length(); } aoqi@0: int locks_size() const { return _locks.length(); } aoqi@0: bool stack_is_empty() const { return _stack.is_empty(); } aoqi@0: bool no_active_locks() const { return _locks.is_empty(); } aoqi@0: int total_locks_size() const; aoqi@0: aoqi@0: // locals access aoqi@0: void clear_locals(); // sets all locals to NULL; aoqi@0: aoqi@0: void invalidate_local(int i) { aoqi@0: assert(_locals.at(i)->type()->is_single_word() || aoqi@0: _locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL"); aoqi@0: _locals.at_put(i, NULL); aoqi@0: } aoqi@0: aoqi@0: Value local_at(int i) const { aoqi@0: Value x = _locals.at(i); aoqi@0: assert(x == NULL || x->type()->is_single_word() || aoqi@0: _locals.at(i + 1) == NULL, "hi-word of doubleword value must be NULL"); aoqi@0: return x; aoqi@0: } aoqi@0: aoqi@0: void store_local(int i, Value x) { aoqi@0: // When overwriting local i, check if i - 1 was the start of a aoqi@0: // double word local and kill it. aoqi@0: if (i > 0) { aoqi@0: Value prev = _locals.at(i - 1); aoqi@0: if (prev != NULL && prev->type()->is_double_word()) { aoqi@0: _locals.at_put(i - 1, NULL); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: _locals.at_put(i, x); aoqi@0: if (x->type()->is_double_word()) { aoqi@0: // hi-word of doubleword value is always NULL aoqi@0: _locals.at_put(i + 1, NULL); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // stack access aoqi@0: Value stack_at(int i) const { aoqi@0: Value x = _stack.at(i); aoqi@0: assert(x->type()->is_single_word() || aoqi@0: _stack.at(i + 1) == NULL, "hi-word of doubleword value must be NULL"); aoqi@0: return x; aoqi@0: } aoqi@0: aoqi@0: Value stack_at_inc(int& i) const { aoqi@0: Value x = stack_at(i); aoqi@0: i += x->type()->size(); aoqi@0: return x; aoqi@0: } aoqi@0: aoqi@0: void stack_at_put(int i, Value x) { aoqi@0: _stack.at_put(i, x); aoqi@0: } aoqi@0: aoqi@0: // pinning support aoqi@0: void pin_stack_for_linear_scan(); aoqi@0: aoqi@0: // iteration aoqi@0: void values_do(ValueVisitor* f); aoqi@0: aoqi@0: // untyped manipulation (for dup_x1, etc.) aoqi@0: void truncate_stack(int size) { _stack.trunc_to(size); } aoqi@0: void raw_push(Value t) { _stack.push(t); } aoqi@0: Value raw_pop() { return _stack.pop(); } aoqi@0: aoqi@0: // typed manipulation aoqi@0: void ipush(Value t) { _stack.push(check(intTag , t)); } aoqi@0: void fpush(Value t) { _stack.push(check(floatTag , t)); } aoqi@0: void apush(Value t) { _stack.push(check(objectTag , t)); } aoqi@0: void rpush(Value t) { _stack.push(check(addressTag, t)); } aoqi@0: void lpush(Value t) { _stack.push(check(longTag , t)); _stack.push(NULL); } aoqi@0: void dpush(Value t) { _stack.push(check(doubleTag , t)); _stack.push(NULL); } aoqi@0: aoqi@0: void push(ValueType* type, Value t) { aoqi@0: switch (type->tag()) { aoqi@0: case intTag : ipush(t); return; aoqi@0: case longTag : lpush(t); return; aoqi@0: case floatTag : fpush(t); return; aoqi@0: case doubleTag : dpush(t); return; aoqi@0: case objectTag : apush(t); return; aoqi@0: case addressTag: rpush(t); return; aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: Value ipop() { return check(intTag , _stack.pop()); } aoqi@0: Value fpop() { return check(floatTag , _stack.pop()); } aoqi@0: Value apop() { return check(objectTag , _stack.pop()); } aoqi@0: Value rpop() { return check(addressTag, _stack.pop()); } aoqi@0: Value lpop() { Value h = _stack.pop(); return check(longTag , _stack.pop(), h); } aoqi@0: Value dpop() { Value h = _stack.pop(); return check(doubleTag, _stack.pop(), h); } aoqi@0: aoqi@0: Value pop(ValueType* type) { aoqi@0: switch (type->tag()) { aoqi@0: case intTag : return ipop(); aoqi@0: case longTag : return lpop(); aoqi@0: case floatTag : return fpop(); aoqi@0: case doubleTag : return dpop(); aoqi@0: case objectTag : return apop(); aoqi@0: case addressTag: return rpop(); aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: Values* pop_arguments(int argument_size); aoqi@0: aoqi@0: // locks access aoqi@0: int lock (Value obj); aoqi@0: int unlock(); aoqi@0: Value lock_at(int i) const { return _locks.at(i); } aoqi@0: aoqi@0: // SSA form IR support aoqi@0: void setup_phi_for_stack(BlockBegin* b, int index); aoqi@0: void setup_phi_for_local(BlockBegin* b, int index); aoqi@0: aoqi@0: // debugging aoqi@0: void print() PRODUCT_RETURN; aoqi@0: void verify() PRODUCT_RETURN; aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: aoqi@0: // Macro definitions for simple iteration of stack and local values of a ValueStack aoqi@0: // The macros can be used like a for-loop. All variables (state, index and value) aoqi@0: // must be defined before the loop. aoqi@0: // When states are nested because of inlining, the stack of the innermost state aoqi@0: // cumulates also the stack of the nested states. In contrast, the locals of all aoqi@0: // states must be iterated each. aoqi@0: // Use the following code pattern to iterate all stack values and all nested local values: aoqi@0: // aoqi@0: // ValueStack* state = ... // state that is iterated aoqi@0: // int index; // current loop index (overwritten in loop) aoqi@0: // Value value; // value at current loop index (overwritten in loop) aoqi@0: // aoqi@0: // for_each_stack_value(state, index, value { aoqi@0: // do something with value and index aoqi@0: // } aoqi@0: // aoqi@0: // for_each_state(state) { aoqi@0: // for_each_local_value(state, index, value) { aoqi@0: // do something with value and index aoqi@0: // } aoqi@0: // } aoqi@0: // as an invariant, state is NULL now aoqi@0: aoqi@0: aoqi@0: // construct a unique variable name with the line number where the macro is used aoqi@0: #define temp_var3(x) temp__ ## x aoqi@0: #define temp_var2(x) temp_var3(x) aoqi@0: #define temp_var temp_var2(__LINE__) aoqi@0: aoqi@0: #define for_each_state(state) \ aoqi@0: for (; state != NULL; state = state->caller_state()) aoqi@0: aoqi@0: #define for_each_local_value(state, index, value) \ aoqi@0: int temp_var = state->locals_size(); \ aoqi@0: for (index = 0; \ aoqi@0: index < temp_var && (value = state->local_at(index), true); \ aoqi@0: index += (value == NULL || value->type()->is_illegal() ? 1 : value->type()->size())) \ aoqi@0: if (value != NULL) aoqi@0: aoqi@0: aoqi@0: #define for_each_stack_value(state, index, value) \ aoqi@0: int temp_var = state->stack_size(); \ aoqi@0: for (index = 0; \ aoqi@0: index < temp_var && (value = state->stack_at(index), true); \ aoqi@0: index += value->type()->size()) aoqi@0: aoqi@0: aoqi@0: #define for_each_lock_value(state, index, value) \ aoqi@0: int temp_var = state->locks_size(); \ aoqi@0: for (index = 0; \ aoqi@0: index < temp_var && (value = state->lock_at(index), true); \ aoqi@0: index++) \ aoqi@0: if (value != NULL) aoqi@0: aoqi@0: aoqi@0: // Macro definition for simple iteration of all state values of a ValueStack aoqi@0: // Because the code cannot be executed in a single loop, the code must be passed aoqi@0: // as a macro parameter. aoqi@0: // Use the following code pattern to iterate all stack values and all nested local values: aoqi@0: // aoqi@0: // ValueStack* state = ... // state that is iterated aoqi@0: // for_each_state_value(state, value, aoqi@0: // do something with value (note that this is a macro parameter) aoqi@0: // ); aoqi@0: aoqi@0: #define for_each_state_value(v_state, v_value, v_code) \ aoqi@0: { \ aoqi@0: int cur_index; \ aoqi@0: ValueStack* cur_state = v_state; \ aoqi@0: Value v_value; \ aoqi@0: for_each_state(cur_state) { \ aoqi@0: { \ aoqi@0: for_each_local_value(cur_state, cur_index, v_value) { \ aoqi@0: v_code; \ aoqi@0: } \ aoqi@0: } \ aoqi@0: { \ aoqi@0: for_each_stack_value(cur_state, cur_index, v_value) { \ aoqi@0: v_code; \ aoqi@0: } \ aoqi@0: } \ aoqi@0: } \ aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Macro definition for simple iteration of all phif functions of a block, i.e all aoqi@0: // phi functions of the ValueStack where the block matches. aoqi@0: // Use the following code pattern to iterate all phi functions of a block: aoqi@0: // aoqi@0: // BlockBegin* block = ... // block that is iterated aoqi@0: // for_each_phi_function(block, phi, aoqi@0: // do something with the phi function phi (note that this is a macro parameter) aoqi@0: // ); aoqi@0: aoqi@0: #define for_each_phi_fun(v_block, v_phi, v_code) \ aoqi@0: { \ aoqi@0: int cur_index; \ aoqi@0: ValueStack* cur_state = v_block->state(); \ aoqi@0: Value value; \ aoqi@0: { \ aoqi@0: for_each_stack_value(cur_state, cur_index, value) { \ aoqi@0: Phi* v_phi = value->as_Phi(); \ aoqi@0: if (v_phi != NULL && v_phi->block() == v_block) { \ aoqi@0: v_code; \ aoqi@0: } \ aoqi@0: } \ aoqi@0: } \ aoqi@0: { \ aoqi@0: for_each_local_value(cur_state, cur_index, value) { \ aoqi@0: Phi* v_phi = value->as_Phi(); \ aoqi@0: if (v_phi != NULL && v_phi->block() == v_block) { \ aoqi@0: v_code; \ aoqi@0: } \ aoqi@0: } \ aoqi@0: } \ aoqi@0: } aoqi@0: aoqi@0: #endif // SHARE_VM_C1_C1_VALUESTACK_HPP