0N/A/*
1879N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "c1/c1_IR.hpp"
1879N/A#include "c1/c1_InstructionPrinter.hpp"
1879N/A#include "c1/c1_ValueStack.hpp"
0N/A
0N/A
0N/A// Implementation of ValueStack
0N/A
1739N/AValueStack::ValueStack(IRScope* scope, ValueStack* caller_state)
0N/A: _scope(scope)
1739N/A, _caller_state(caller_state)
1739N/A, _bci(-99)
1739N/A, _kind(Parsing)
1739N/A, _locals(scope->method()->max_locals(), NULL)
1739N/A, _stack(scope->method()->max_stack())
1739N/A, _locks()
0N/A{
1739N/A verify();
0N/A}
0N/A
0N/A
1739N/AValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci)
1739N/A : _scope(copy_from->scope())
1739N/A , _caller_state(copy_from->caller_state())
1739N/A , _bci(bci)
1739N/A , _kind(kind)
1739N/A , _locals()
1739N/A , _stack()
1739N/A , _locks(copy_from->locks_size())
1739N/A{
1739N/A assert(kind != EmptyExceptionState || !Compilation::current()->env()->jvmti_can_access_local_variables(), "need locals");
1739N/A if (kind != EmptyExceptionState) {
1739N/A // only allocate space if we need to copy the locals-array
1739N/A _locals = Values(copy_from->locals_size());
1739N/A _locals.appendAll(&copy_from->_locals);
0N/A }
1739N/A
1739N/A if (kind != ExceptionState && kind != EmptyExceptionState) {
1739N/A if (kind == Parsing) {
1739N/A // stack will be modified, so reserve enough space to avoid resizing
1739N/A _stack = Values(scope()->method()->max_stack());
1739N/A } else {
1739N/A // stack will not be modified, so do not waste space
1739N/A _stack = Values(copy_from->stack_size());
0N/A }
1739N/A _stack.appendAll(&copy_from->_stack);
0N/A }
0N/A
1739N/A _locks.appendAll(&copy_from->_locks);
1739N/A
1739N/A verify();
0N/A}
0N/A
0N/A
1739N/Abool ValueStack::is_same(ValueStack* s) {
1739N/A if (scope() != s->scope()) return false;
1739N/A if (caller_state() != s->caller_state()) return false;
1739N/A
1739N/A if (locals_size() != s->locals_size()) return false;
1739N/A if (stack_size() != s->stack_size()) return false;
1739N/A if (locks_size() != s->locks_size()) return false;
1739N/A
0N/A // compare each stack element with the corresponding stack element of s
0N/A int index;
0N/A Value value;
0N/A for_each_stack_value(this, index, value) {
0N/A if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false;
0N/A }
0N/A for_each_lock_value(this, index, value) {
0N/A if (value != s->lock_at(index)) return false;
0N/A }
0N/A return true;
0N/A}
0N/A
0N/Avoid ValueStack::clear_locals() {
0N/A for (int i = _locals.length() - 1; i >= 0; i--) {
0N/A _locals.at_put(i, NULL);
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid ValueStack::pin_stack_for_linear_scan() {
0N/A for_each_state_value(this, v,
0N/A if (v->as_Constant() == NULL && v->as_Local() == NULL) {
0N/A v->pin(Instruction::PinStackForStateSplit);
0N/A }
0N/A );
0N/A}
0N/A
0N/A
0N/A// apply function to all values of a list; factored out from values_do(f)
1504N/Avoid ValueStack::apply(Values list, ValueVisitor* f) {
0N/A for (int i = 0; i < list.length(); i++) {
0N/A Value* va = list.adr_at(i);
0N/A Value v0 = *va;
1739N/A if (v0 != NULL && !v0->type()->is_illegal()) {
1739N/A f->visit(va);
0N/A#ifdef ASSERT
1739N/A Value v1 = *va;
1739N/A assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match");
1739N/A assert(!v1->type()->is_double_word() || list.at(i + 1) == NULL, "hi-word of doubleword value must be NULL");
0N/A#endif
1739N/A if (v0->type()->is_double_word()) i++;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
1504N/Avoid ValueStack::values_do(ValueVisitor* f) {
0N/A ValueStack* state = this;
0N/A for_each_state(state) {
0N/A apply(state->_locals, f);
1739N/A apply(state->_stack, f);
1739N/A apply(state->_locks, f);
0N/A }
0N/A}
0N/A
0N/A
0N/AValues* ValueStack::pop_arguments(int argument_size) {
0N/A assert(stack_size() >= argument_size, "stack too small or too many arguments");
0N/A int base = stack_size() - argument_size;
0N/A Values* args = new Values(argument_size);
0N/A for (int i = base; i < stack_size();) args->push(stack_at_inc(i));
0N/A truncate_stack(base);
0N/A return args;
0N/A}
0N/A
0N/A
1739N/Aint ValueStack::total_locks_size() const {
1739N/A int num_locks = 0;
1739N/A const ValueStack* state = this;
1739N/A for_each_state(state) {
1739N/A num_locks += state->locks_size();
1739N/A }
1739N/A return num_locks;
1739N/A}
1739N/A
1739N/Aint ValueStack::lock(Value obj) {
0N/A _locks.push(obj);
1739N/A int num_locks = total_locks_size();
1739N/A scope()->set_min_number_of_locks(num_locks);
1739N/A return num_locks - 1;
0N/A}
0N/A
0N/A
0N/Aint ValueStack::unlock() {
0N/A _locks.pop();
1739N/A return total_locks_size();
0N/A}
0N/A
0N/A
0N/Avoid ValueStack::setup_phi_for_stack(BlockBegin* b, int index) {
0N/A assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created");
0N/A
0N/A ValueType* t = stack_at(index)->type();
0N/A Value phi = new Phi(t, b, -index - 1);
0N/A _stack[index] = phi;
0N/A
1739N/A assert(!t->is_double_word() || _stack.at(index + 1) == NULL, "hi-word of doubleword value must be NULL");
0N/A}
0N/A
0N/Avoid ValueStack::setup_phi_for_local(BlockBegin* b, int index) {
0N/A assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created");
0N/A
0N/A ValueType* t = local_at(index)->type();
0N/A Value phi = new Phi(t, b, index);
0N/A store_local(index, phi);
0N/A}
0N/A
0N/A#ifndef PRODUCT
1739N/A
0N/Avoid ValueStack::print() {
1739N/A scope()->method()->print_name();
3932N/A tty->cr();
0N/A if (stack_is_empty()) {
0N/A tty->print_cr("empty stack");
0N/A } else {
0N/A InstructionPrinter ip;
0N/A for (int i = 0; i < stack_size();) {
0N/A Value t = stack_at_inc(i);
0N/A tty->print("%2d ", i);
1739N/A tty->print("%c%d ", t->type()->tchar(), t->id());
0N/A ip.print_instr(t);
0N/A tty->cr();
0N/A }
0N/A }
0N/A if (!no_active_locks()) {
0N/A InstructionPrinter ip;
1739N/A for (int i = 0; i < locks_size(); i++) {
0N/A Value t = lock_at(i);
0N/A tty->print("lock %2d ", i);
0N/A if (t == NULL) {
0N/A tty->print("this");
0N/A } else {
1739N/A tty->print("%c%d ", t->type()->tchar(), t->id());
0N/A ip.print_instr(t);
0N/A }
0N/A tty->cr();
0N/A }
0N/A }
0N/A if (locals_size() > 0) {
0N/A InstructionPrinter ip;
0N/A for (int i = 0; i < locals_size();) {
0N/A Value l = _locals[i];
0N/A tty->print("local %d ", i);
0N/A if (l == NULL) {
0N/A tty->print("null");
0N/A i ++;
0N/A } else {
1739N/A tty->print("%c%d ", l->type()->tchar(), l->id());
0N/A ip.print_instr(l);
0N/A if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2;
0N/A }
0N/A tty->cr();
0N/A }
0N/A }
1739N/A
1739N/A if (caller_state() != NULL) {
1739N/A caller_state()->print();
1739N/A }
0N/A}
0N/A
0N/A
0N/Avoid ValueStack::verify() {
1739N/A assert(scope() != NULL, "scope must exist");
1739N/A if (caller_state() != NULL) {
1739N/A assert(caller_state()->scope() == scope()->caller(), "invalid caller scope");
1739N/A caller_state()->verify();
1739N/A }
1739N/A
1739N/A if (kind() == Parsing) {
1739N/A assert(bci() == -99, "bci not defined during parsing");
1739N/A } else {
1739N/A assert(bci() >= -1, "bci out of range");
1739N/A assert(bci() < scope()->method()->code_size(), "bci out of range");
1739N/A assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode");
1739N/A assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid");
1739N/A }
1739N/A
1739N/A int i;
1739N/A for (i = 0; i < stack_size(); i++) {
1739N/A Value v = _stack.at(i);
1739N/A if (v == NULL) {
1739N/A assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are NULL on stack");
1739N/A } else if (v->type()->is_double_word()) {
1739N/A assert(_stack.at(i + 1) == NULL, "hi-word must be NULL");
1739N/A }
1739N/A }
1739N/A
1739N/A for (i = 0; i < locals_size(); i++) {
1739N/A Value v = _locals.at(i);
1739N/A if (v != NULL && v->type()->is_double_word()) {
1739N/A assert(_locals.at(i + 1) == NULL, "hi-word must be NULL");
1739N/A }
1739N/A }
1739N/A
1739N/A for_each_state_value(this, v,
1739N/A assert(v != NULL, "just test if state-iteration succeeds");
1739N/A );
0N/A}
0N/A#endif // PRODUCT