duke@435: /* stefank@2314: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. 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: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "c1/c1_IR.hpp" stefank@2314: #include "c1/c1_InstructionPrinter.hpp" stefank@2314: #include "c1/c1_ValueStack.hpp" duke@435: duke@435: duke@435: // Implementation of ValueStack duke@435: roland@2174: ValueStack::ValueStack(IRScope* scope, ValueStack* caller_state) duke@435: : _scope(scope) roland@2174: , _caller_state(caller_state) roland@2174: , _bci(-99) roland@2174: , _kind(Parsing) roland@2174: , _locals(scope->method()->max_locals(), NULL) roland@2174: , _stack(scope->method()->max_stack()) roland@2174: , _locks() duke@435: { roland@2174: verify(); duke@435: } duke@435: roland@2174: roland@2174: ValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci) roland@2174: : _scope(copy_from->scope()) roland@2174: , _caller_state(copy_from->caller_state()) roland@2174: , _bci(bci) roland@2174: , _kind(kind) roland@2174: , _locals() roland@2174: , _stack() roland@2174: , _locks(copy_from->locks_size()) roland@2174: { roland@2174: assert(kind != EmptyExceptionState || !Compilation::current()->env()->jvmti_can_access_local_variables(), "need locals"); roland@2174: if (kind != EmptyExceptionState) { roland@2174: // only allocate space if we need to copy the locals-array roland@2174: _locals = Values(copy_from->locals_size()); roland@2174: _locals.appendAll(©_from->_locals); roland@2174: } roland@2174: roland@2174: if (kind != ExceptionState && kind != EmptyExceptionState) { roland@2174: if (kind == Parsing) { roland@2174: // stack will be modified, so reserve enough space to avoid resizing roland@2174: _stack = Values(scope()->method()->max_stack()); roland@2174: } else { roland@2174: // stack will not be modified, so do not waste space roland@2174: _stack = Values(copy_from->stack_size()); roland@2174: } roland@2174: _stack.appendAll(©_from->_stack); roland@2174: } roland@2174: roland@2174: _locks.appendAll(©_from->_locks); roland@2174: roland@2174: verify(); duke@435: } duke@435: duke@435: roland@2174: bool ValueStack::is_same(ValueStack* s) { roland@2174: if (scope() != s->scope()) return false; roland@2174: if (caller_state() != s->caller_state()) return false; duke@435: roland@2174: if (locals_size() != s->locals_size()) return false; roland@2174: if (stack_size() != s->stack_size()) return false; roland@2174: if (locks_size() != s->locks_size()) return false; duke@435: duke@435: // compare each stack element with the corresponding stack element of s duke@435: int index; duke@435: Value value; duke@435: for_each_stack_value(this, index, value) { duke@435: if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false; duke@435: } duke@435: for_each_lock_value(this, index, value) { duke@435: if (value != s->lock_at(index)) return false; duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: void ValueStack::clear_locals() { duke@435: for (int i = _locals.length() - 1; i >= 0; i--) { duke@435: _locals.at_put(i, NULL); duke@435: } duke@435: } duke@435: duke@435: duke@435: void ValueStack::pin_stack_for_linear_scan() { duke@435: for_each_state_value(this, v, duke@435: if (v->as_Constant() == NULL && v->as_Local() == NULL) { duke@435: v->pin(Instruction::PinStackForStateSplit); duke@435: } duke@435: ); duke@435: } duke@435: duke@435: duke@435: // apply function to all values of a list; factored out from values_do(f) iveresov@1939: void ValueStack::apply(Values list, ValueVisitor* f) { duke@435: for (int i = 0; i < list.length(); i++) { duke@435: Value* va = list.adr_at(i); duke@435: Value v0 = *va; roland@2174: if (v0 != NULL && !v0->type()->is_illegal()) { roland@2174: f->visit(va); duke@435: #ifdef ASSERT roland@2174: Value v1 = *va; roland@2174: assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match"); roland@2174: assert(!v1->type()->is_double_word() || list.at(i + 1) == NULL, "hi-word of doubleword value must be NULL"); duke@435: #endif roland@2174: if (v0->type()->is_double_word()) i++; duke@435: } duke@435: } duke@435: } duke@435: duke@435: iveresov@1939: void ValueStack::values_do(ValueVisitor* f) { duke@435: ValueStack* state = this; duke@435: for_each_state(state) { duke@435: apply(state->_locals, f); roland@2174: apply(state->_stack, f); roland@2174: apply(state->_locks, f); duke@435: } duke@435: } duke@435: duke@435: duke@435: Values* ValueStack::pop_arguments(int argument_size) { duke@435: assert(stack_size() >= argument_size, "stack too small or too many arguments"); duke@435: int base = stack_size() - argument_size; duke@435: Values* args = new Values(argument_size); duke@435: for (int i = base; i < stack_size();) args->push(stack_at_inc(i)); duke@435: truncate_stack(base); duke@435: return args; duke@435: } duke@435: duke@435: roland@2174: int ValueStack::total_locks_size() const { roland@2174: int num_locks = 0; roland@2174: const ValueStack* state = this; roland@2174: for_each_state(state) { roland@2174: num_locks += state->locks_size(); roland@2174: } roland@2174: return num_locks; roland@2174: } roland@2174: roland@2174: int ValueStack::lock(Value obj) { duke@435: _locks.push(obj); roland@2174: int num_locks = total_locks_size(); roland@2174: scope()->set_min_number_of_locks(num_locks); roland@2174: return num_locks - 1; duke@435: } duke@435: duke@435: duke@435: int ValueStack::unlock() { duke@435: _locks.pop(); roland@2174: return total_locks_size(); duke@435: } duke@435: duke@435: duke@435: void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) { duke@435: assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created"); duke@435: duke@435: ValueType* t = stack_at(index)->type(); duke@435: Value phi = new Phi(t, b, -index - 1); duke@435: _stack[index] = phi; duke@435: roland@2174: assert(!t->is_double_word() || _stack.at(index + 1) == NULL, "hi-word of doubleword value must be NULL"); duke@435: } duke@435: duke@435: void ValueStack::setup_phi_for_local(BlockBegin* b, int index) { duke@435: assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created"); duke@435: duke@435: ValueType* t = local_at(index)->type(); duke@435: Value phi = new Phi(t, b, index); duke@435: store_local(index, phi); duke@435: } duke@435: duke@435: #ifndef PRODUCT roland@2174: duke@435: void ValueStack::print() { roland@2174: scope()->method()->print_name(); duke@435: if (stack_is_empty()) { duke@435: tty->print_cr("empty stack"); duke@435: } else { duke@435: InstructionPrinter ip; duke@435: for (int i = 0; i < stack_size();) { duke@435: Value t = stack_at_inc(i); duke@435: tty->print("%2d ", i); roland@2174: tty->print("%c%d ", t->type()->tchar(), t->id()); duke@435: ip.print_instr(t); duke@435: tty->cr(); duke@435: } duke@435: } duke@435: if (!no_active_locks()) { duke@435: InstructionPrinter ip; roland@2174: for (int i = 0; i < locks_size(); i++) { duke@435: Value t = lock_at(i); duke@435: tty->print("lock %2d ", i); duke@435: if (t == NULL) { duke@435: tty->print("this"); duke@435: } else { roland@2174: tty->print("%c%d ", t->type()->tchar(), t->id()); duke@435: ip.print_instr(t); duke@435: } duke@435: tty->cr(); duke@435: } duke@435: } duke@435: if (locals_size() > 0) { duke@435: InstructionPrinter ip; duke@435: for (int i = 0; i < locals_size();) { duke@435: Value l = _locals[i]; duke@435: tty->print("local %d ", i); duke@435: if (l == NULL) { duke@435: tty->print("null"); duke@435: i ++; duke@435: } else { roland@2174: tty->print("%c%d ", l->type()->tchar(), l->id()); duke@435: ip.print_instr(l); duke@435: if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2; duke@435: } duke@435: tty->cr(); duke@435: } duke@435: } roland@2174: roland@2174: if (caller_state() != NULL) { roland@2174: caller_state()->print(); roland@2174: } duke@435: } duke@435: duke@435: duke@435: void ValueStack::verify() { roland@2174: assert(scope() != NULL, "scope must exist"); roland@2174: if (caller_state() != NULL) { roland@2174: assert(caller_state()->scope() == scope()->caller(), "invalid caller scope"); roland@2174: caller_state()->verify(); roland@2174: } roland@2174: roland@2174: if (kind() == Parsing) { roland@2174: assert(bci() == -99, "bci not defined during parsing"); roland@2174: } else { roland@2174: assert(bci() >= -1, "bci out of range"); roland@2174: assert(bci() < scope()->method()->code_size(), "bci out of range"); roland@2174: assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode"); roland@2174: assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid"); roland@2174: } roland@2174: roland@2174: int i; roland@2174: for (i = 0; i < stack_size(); i++) { roland@2174: Value v = _stack.at(i); roland@2174: if (v == NULL) { roland@2174: assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are NULL on stack"); roland@2174: } else if (v->type()->is_double_word()) { roland@2174: assert(_stack.at(i + 1) == NULL, "hi-word must be NULL"); roland@2174: } roland@2174: } roland@2174: roland@2174: for (i = 0; i < locals_size(); i++) { roland@2174: Value v = _locals.at(i); roland@2174: if (v != NULL && v->type()->is_double_word()) { roland@2174: assert(_locals.at(i + 1) == NULL, "hi-word must be NULL"); roland@2174: } roland@2174: } roland@2174: roland@2174: for_each_state_value(this, v, roland@2174: assert(v != NULL, "just test if state-iteration succeeds"); roland@2174: ); duke@435: } duke@435: #endif // PRODUCT