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