1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/c1/c1_ValueStack.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,285 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "c1/c1_IR.hpp" 1.30 +#include "c1/c1_InstructionPrinter.hpp" 1.31 +#include "c1/c1_ValueStack.hpp" 1.32 + 1.33 + 1.34 +// Implementation of ValueStack 1.35 + 1.36 +ValueStack::ValueStack(IRScope* scope, ValueStack* caller_state) 1.37 +: _scope(scope) 1.38 +, _caller_state(caller_state) 1.39 +, _bci(-99) 1.40 +, _kind(Parsing) 1.41 +, _locals(scope->method()->max_locals(), NULL) 1.42 +, _stack(scope->method()->max_stack()) 1.43 +, _locks() 1.44 +{ 1.45 + verify(); 1.46 +} 1.47 + 1.48 + 1.49 +ValueStack::ValueStack(ValueStack* copy_from, Kind kind, int bci) 1.50 + : _scope(copy_from->scope()) 1.51 + , _caller_state(copy_from->caller_state()) 1.52 + , _bci(bci) 1.53 + , _kind(kind) 1.54 + , _locals() 1.55 + , _stack() 1.56 + , _locks(copy_from->locks_size()) 1.57 +{ 1.58 + assert(kind != EmptyExceptionState || !Compilation::current()->env()->jvmti_can_access_local_variables(), "need locals"); 1.59 + if (kind != EmptyExceptionState) { 1.60 + // only allocate space if we need to copy the locals-array 1.61 + _locals = Values(copy_from->locals_size()); 1.62 + _locals.appendAll(©_from->_locals); 1.63 + } 1.64 + 1.65 + if (kind != ExceptionState && kind != EmptyExceptionState) { 1.66 + if (kind == Parsing) { 1.67 + // stack will be modified, so reserve enough space to avoid resizing 1.68 + _stack = Values(scope()->method()->max_stack()); 1.69 + } else { 1.70 + // stack will not be modified, so do not waste space 1.71 + _stack = Values(copy_from->stack_size()); 1.72 + } 1.73 + _stack.appendAll(©_from->_stack); 1.74 + } 1.75 + 1.76 + _locks.appendAll(©_from->_locks); 1.77 + 1.78 + verify(); 1.79 +} 1.80 + 1.81 + 1.82 +bool ValueStack::is_same(ValueStack* s) { 1.83 + if (scope() != s->scope()) return false; 1.84 + if (caller_state() != s->caller_state()) return false; 1.85 + 1.86 + if (locals_size() != s->locals_size()) return false; 1.87 + if (stack_size() != s->stack_size()) return false; 1.88 + if (locks_size() != s->locks_size()) return false; 1.89 + 1.90 + // compare each stack element with the corresponding stack element of s 1.91 + int index; 1.92 + Value value; 1.93 + for_each_stack_value(this, index, value) { 1.94 + if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false; 1.95 + } 1.96 + for_each_lock_value(this, index, value) { 1.97 + if (value != s->lock_at(index)) return false; 1.98 + } 1.99 + return true; 1.100 +} 1.101 + 1.102 +void ValueStack::clear_locals() { 1.103 + for (int i = _locals.length() - 1; i >= 0; i--) { 1.104 + _locals.at_put(i, NULL); 1.105 + } 1.106 +} 1.107 + 1.108 + 1.109 +void ValueStack::pin_stack_for_linear_scan() { 1.110 + for_each_state_value(this, v, 1.111 + if (v->as_Constant() == NULL && v->as_Local() == NULL) { 1.112 + v->pin(Instruction::PinStackForStateSplit); 1.113 + } 1.114 + ); 1.115 +} 1.116 + 1.117 + 1.118 +// apply function to all values of a list; factored out from values_do(f) 1.119 +void ValueStack::apply(Values list, ValueVisitor* f) { 1.120 + for (int i = 0; i < list.length(); i++) { 1.121 + Value* va = list.adr_at(i); 1.122 + Value v0 = *va; 1.123 + if (v0 != NULL && !v0->type()->is_illegal()) { 1.124 + f->visit(va); 1.125 +#ifdef ASSERT 1.126 + Value v1 = *va; 1.127 + assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match"); 1.128 + assert(!v1->type()->is_double_word() || list.at(i + 1) == NULL, "hi-word of doubleword value must be NULL"); 1.129 +#endif 1.130 + if (v0->type()->is_double_word()) i++; 1.131 + } 1.132 + } 1.133 +} 1.134 + 1.135 + 1.136 +void ValueStack::values_do(ValueVisitor* f) { 1.137 + ValueStack* state = this; 1.138 + for_each_state(state) { 1.139 + apply(state->_locals, f); 1.140 + apply(state->_stack, f); 1.141 + apply(state->_locks, f); 1.142 + } 1.143 +} 1.144 + 1.145 + 1.146 +Values* ValueStack::pop_arguments(int argument_size) { 1.147 + assert(stack_size() >= argument_size, "stack too small or too many arguments"); 1.148 + int base = stack_size() - argument_size; 1.149 + Values* args = new Values(argument_size); 1.150 + for (int i = base; i < stack_size();) args->push(stack_at_inc(i)); 1.151 + truncate_stack(base); 1.152 + return args; 1.153 +} 1.154 + 1.155 + 1.156 +int ValueStack::total_locks_size() const { 1.157 + int num_locks = 0; 1.158 + const ValueStack* state = this; 1.159 + for_each_state(state) { 1.160 + num_locks += state->locks_size(); 1.161 + } 1.162 + return num_locks; 1.163 +} 1.164 + 1.165 +int ValueStack::lock(Value obj) { 1.166 + _locks.push(obj); 1.167 + int num_locks = total_locks_size(); 1.168 + scope()->set_min_number_of_locks(num_locks); 1.169 + return num_locks - 1; 1.170 +} 1.171 + 1.172 + 1.173 +int ValueStack::unlock() { 1.174 + _locks.pop(); 1.175 + return total_locks_size(); 1.176 +} 1.177 + 1.178 + 1.179 +void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) { 1.180 + assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created"); 1.181 + 1.182 + ValueType* t = stack_at(index)->type(); 1.183 + Value phi = new Phi(t, b, -index - 1); 1.184 + _stack[index] = phi; 1.185 + 1.186 + assert(!t->is_double_word() || _stack.at(index + 1) == NULL, "hi-word of doubleword value must be NULL"); 1.187 +} 1.188 + 1.189 +void ValueStack::setup_phi_for_local(BlockBegin* b, int index) { 1.190 + assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created"); 1.191 + 1.192 + ValueType* t = local_at(index)->type(); 1.193 + Value phi = new Phi(t, b, index); 1.194 + store_local(index, phi); 1.195 +} 1.196 + 1.197 +#ifndef PRODUCT 1.198 + 1.199 +void ValueStack::print() { 1.200 + scope()->method()->print_name(); 1.201 + tty->cr(); 1.202 + if (stack_is_empty()) { 1.203 + tty->print_cr("empty stack"); 1.204 + } else { 1.205 + InstructionPrinter ip; 1.206 + for (int i = 0; i < stack_size();) { 1.207 + Value t = stack_at_inc(i); 1.208 + tty->print("%2d ", i); 1.209 + tty->print("%c%d ", t->type()->tchar(), t->id()); 1.210 + ip.print_instr(t); 1.211 + tty->cr(); 1.212 + } 1.213 + } 1.214 + if (!no_active_locks()) { 1.215 + InstructionPrinter ip; 1.216 + for (int i = 0; i < locks_size(); i++) { 1.217 + Value t = lock_at(i); 1.218 + tty->print("lock %2d ", i); 1.219 + if (t == NULL) { 1.220 + tty->print("this"); 1.221 + } else { 1.222 + tty->print("%c%d ", t->type()->tchar(), t->id()); 1.223 + ip.print_instr(t); 1.224 + } 1.225 + tty->cr(); 1.226 + } 1.227 + } 1.228 + if (locals_size() > 0) { 1.229 + InstructionPrinter ip; 1.230 + for (int i = 0; i < locals_size();) { 1.231 + Value l = _locals[i]; 1.232 + tty->print("local %d ", i); 1.233 + if (l == NULL) { 1.234 + tty->print("null"); 1.235 + i ++; 1.236 + } else { 1.237 + tty->print("%c%d ", l->type()->tchar(), l->id()); 1.238 + ip.print_instr(l); 1.239 + if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2; 1.240 + } 1.241 + tty->cr(); 1.242 + } 1.243 + } 1.244 + 1.245 + if (caller_state() != NULL) { 1.246 + caller_state()->print(); 1.247 + } 1.248 +} 1.249 + 1.250 + 1.251 +void ValueStack::verify() { 1.252 + assert(scope() != NULL, "scope must exist"); 1.253 + if (caller_state() != NULL) { 1.254 + assert(caller_state()->scope() == scope()->caller(), "invalid caller scope"); 1.255 + caller_state()->verify(); 1.256 + } 1.257 + 1.258 + if (kind() == Parsing) { 1.259 + assert(bci() == -99, "bci not defined during parsing"); 1.260 + } else { 1.261 + assert(bci() >= -1, "bci out of range"); 1.262 + assert(bci() < scope()->method()->code_size(), "bci out of range"); 1.263 + assert(bci() == SynchronizationEntryBCI || Bytecodes::is_defined(scope()->method()->java_code_at_bci(bci())), "make sure bci points at a real bytecode"); 1.264 + assert(scope()->method()->liveness_at_bci(bci()).is_valid(), "liveness at bci must be valid"); 1.265 + } 1.266 + 1.267 + int i; 1.268 + for (i = 0; i < stack_size(); i++) { 1.269 + Value v = _stack.at(i); 1.270 + if (v == NULL) { 1.271 + assert(_stack.at(i - 1)->type()->is_double_word(), "only hi-words are NULL on stack"); 1.272 + } else if (v->type()->is_double_word()) { 1.273 + assert(_stack.at(i + 1) == NULL, "hi-word must be NULL"); 1.274 + } 1.275 + } 1.276 + 1.277 + for (i = 0; i < locals_size(); i++) { 1.278 + Value v = _locals.at(i); 1.279 + if (v != NULL && v->type()->is_double_word()) { 1.280 + assert(_locals.at(i + 1) == NULL, "hi-word must be NULL"); 1.281 + } 1.282 + } 1.283 + 1.284 + for_each_state_value(this, v, 1.285 + assert(v != NULL, "just test if state-iteration succeeds"); 1.286 + ); 1.287 +} 1.288 +#endif // PRODUCT