Tue, 09 Mar 2010 20:16:19 +0100
6919934: JSR 292 needs to support x86 C1
Summary: This implements JSR 292 support for C1 x86.
Reviewed-by: never, jrose, kvn
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | #include "incls/_precompiled.incl" |
duke@435 | 26 | #include "incls/_c1_ValueStack.cpp.incl" |
duke@435 | 27 | |
duke@435 | 28 | |
duke@435 | 29 | // Implementation of ValueStack |
duke@435 | 30 | |
duke@435 | 31 | ValueStack::ValueStack(IRScope* scope, int locals_size, int max_stack_size) |
duke@435 | 32 | : _scope(scope) |
duke@435 | 33 | , _locals(locals_size, NULL) |
duke@435 | 34 | , _stack(max_stack_size) |
duke@435 | 35 | , _lock_stack(false) |
duke@435 | 36 | , _locks(1) |
duke@435 | 37 | { |
duke@435 | 38 | assert(scope != NULL, "scope must exist"); |
duke@435 | 39 | } |
duke@435 | 40 | |
duke@435 | 41 | ValueStack* ValueStack::copy() { |
duke@435 | 42 | ValueStack* s = new ValueStack(scope(), locals_size(), max_stack_size()); |
duke@435 | 43 | s->_stack.appendAll(&_stack); |
duke@435 | 44 | s->_locks.appendAll(&_locks); |
duke@435 | 45 | s->replace_locals(this); |
duke@435 | 46 | return s; |
duke@435 | 47 | } |
duke@435 | 48 | |
duke@435 | 49 | |
duke@435 | 50 | ValueStack* ValueStack::copy_locks() { |
duke@435 | 51 | int sz = scope()->lock_stack_size(); |
duke@435 | 52 | if (stack_size() == 0) { |
duke@435 | 53 | sz = 0; |
duke@435 | 54 | } |
duke@435 | 55 | ValueStack* s = new ValueStack(scope(), locals_size(), sz); |
duke@435 | 56 | s->_lock_stack = true; |
duke@435 | 57 | s->_locks.appendAll(&_locks); |
duke@435 | 58 | s->replace_locals(this); |
duke@435 | 59 | if (sz > 0) { |
duke@435 | 60 | assert(sz <= stack_size(), "lock stack underflow"); |
duke@435 | 61 | for (int i = 0; i < sz; i++) { |
duke@435 | 62 | s->_stack.append(_stack[i]); |
duke@435 | 63 | } |
duke@435 | 64 | } |
duke@435 | 65 | return s; |
duke@435 | 66 | } |
duke@435 | 67 | |
duke@435 | 68 | bool ValueStack::is_same(ValueStack* s) { |
duke@435 | 69 | assert(s != NULL, "state must exist"); |
duke@435 | 70 | assert(scope () == s->scope (), "scopes must correspond"); |
duke@435 | 71 | assert(locals_size() == s->locals_size(), "locals sizes must correspond"); |
duke@435 | 72 | return is_same_across_scopes(s); |
duke@435 | 73 | } |
duke@435 | 74 | |
duke@435 | 75 | |
duke@435 | 76 | bool ValueStack::is_same_across_scopes(ValueStack* s) { |
duke@435 | 77 | assert(s != NULL, "state must exist"); |
duke@435 | 78 | assert(stack_size () == s->stack_size (), "stack sizes must correspond"); |
duke@435 | 79 | assert(locks_size () == s->locks_size (), "locks sizes must correspond"); |
duke@435 | 80 | // compare each stack element with the corresponding stack element of s |
duke@435 | 81 | int index; |
duke@435 | 82 | Value value; |
duke@435 | 83 | for_each_stack_value(this, index, value) { |
duke@435 | 84 | if (value->type()->tag() != s->stack_at(index)->type()->tag()) return false; |
duke@435 | 85 | } |
duke@435 | 86 | for_each_lock_value(this, index, value) { |
duke@435 | 87 | if (value != s->lock_at(index)) return false; |
duke@435 | 88 | } |
duke@435 | 89 | return true; |
duke@435 | 90 | } |
duke@435 | 91 | |
duke@435 | 92 | |
duke@435 | 93 | ValueStack* ValueStack::caller_state() const { |
duke@435 | 94 | return scope()->caller_state(); |
duke@435 | 95 | } |
duke@435 | 96 | |
duke@435 | 97 | |
duke@435 | 98 | void ValueStack::clear_locals() { |
duke@435 | 99 | for (int i = _locals.length() - 1; i >= 0; i--) { |
duke@435 | 100 | _locals.at_put(i, NULL); |
duke@435 | 101 | } |
duke@435 | 102 | } |
duke@435 | 103 | |
duke@435 | 104 | |
duke@435 | 105 | void ValueStack::replace_locals(ValueStack* with) { |
duke@435 | 106 | assert(locals_size() == with->locals_size(), "number of locals must match"); |
duke@435 | 107 | for (int i = locals_size() - 1; i >= 0; i--) { |
duke@435 | 108 | _locals.at_put(i, with->_locals.at(i)); |
duke@435 | 109 | } |
duke@435 | 110 | } |
duke@435 | 111 | |
duke@435 | 112 | void ValueStack::pin_stack_for_linear_scan() { |
duke@435 | 113 | for_each_state_value(this, v, |
duke@435 | 114 | if (v->as_Constant() == NULL && v->as_Local() == NULL) { |
duke@435 | 115 | v->pin(Instruction::PinStackForStateSplit); |
duke@435 | 116 | } |
duke@435 | 117 | ); |
duke@435 | 118 | } |
duke@435 | 119 | |
duke@435 | 120 | |
duke@435 | 121 | // apply function to all values of a list; factored out from values_do(f) |
duke@435 | 122 | void ValueStack::apply(Values list, void f(Value*)) { |
duke@435 | 123 | for (int i = 0; i < list.length(); i++) { |
duke@435 | 124 | Value* va = list.adr_at(i); |
duke@435 | 125 | Value v0 = *va; |
duke@435 | 126 | if (v0 != NULL) { |
duke@435 | 127 | if (!v0->type()->is_illegal()) { |
duke@435 | 128 | assert(v0->as_HiWord() == NULL, "should never see HiWord during traversal"); |
duke@435 | 129 | f(va); |
duke@435 | 130 | #ifdef ASSERT |
duke@435 | 131 | Value v1 = *va; |
duke@435 | 132 | if (v0 != v1) { |
duke@435 | 133 | assert(v1->type()->is_illegal() || v0->type()->tag() == v1->type()->tag(), "types must match"); |
duke@435 | 134 | if (v0->type()->is_double_word()) { |
duke@435 | 135 | list.at_put(i + 1, v0->hi_word()); |
duke@435 | 136 | } |
duke@435 | 137 | } |
duke@435 | 138 | #endif |
duke@435 | 139 | if (v0->type()->is_double_word()) i++; |
duke@435 | 140 | } |
duke@435 | 141 | } |
duke@435 | 142 | } |
duke@435 | 143 | } |
duke@435 | 144 | |
duke@435 | 145 | |
duke@435 | 146 | void ValueStack::values_do(void f(Value*)) { |
duke@435 | 147 | apply(_stack, f); |
duke@435 | 148 | apply(_locks, f); |
duke@435 | 149 | |
duke@435 | 150 | ValueStack* state = this; |
duke@435 | 151 | for_each_state(state) { |
duke@435 | 152 | apply(state->_locals, f); |
duke@435 | 153 | } |
duke@435 | 154 | } |
duke@435 | 155 | |
duke@435 | 156 | |
duke@435 | 157 | Values* ValueStack::pop_arguments(int argument_size) { |
duke@435 | 158 | assert(stack_size() >= argument_size, "stack too small or too many arguments"); |
duke@435 | 159 | int base = stack_size() - argument_size; |
duke@435 | 160 | Values* args = new Values(argument_size); |
duke@435 | 161 | for (int i = base; i < stack_size();) args->push(stack_at_inc(i)); |
duke@435 | 162 | truncate_stack(base); |
duke@435 | 163 | return args; |
duke@435 | 164 | } |
duke@435 | 165 | |
duke@435 | 166 | |
duke@435 | 167 | int ValueStack::lock(IRScope* scope, Value obj) { |
duke@435 | 168 | _locks.push(obj); |
duke@435 | 169 | scope->set_min_number_of_locks(locks_size()); |
duke@435 | 170 | return locks_size() - 1; |
duke@435 | 171 | } |
duke@435 | 172 | |
duke@435 | 173 | |
duke@435 | 174 | int ValueStack::unlock() { |
duke@435 | 175 | _locks.pop(); |
duke@435 | 176 | return locks_size(); |
duke@435 | 177 | } |
duke@435 | 178 | |
duke@435 | 179 | |
duke@435 | 180 | ValueStack* ValueStack::push_scope(IRScope* scope) { |
duke@435 | 181 | assert(scope->caller() == _scope, "scopes must have caller/callee relationship"); |
duke@435 | 182 | ValueStack* res = new ValueStack(scope, |
duke@435 | 183 | scope->method()->max_locals(), |
duke@435 | 184 | max_stack_size() + scope->method()->max_stack()); |
duke@435 | 185 | // Preserves stack and monitors. |
duke@435 | 186 | res->_stack.appendAll(&_stack); |
duke@435 | 187 | res->_locks.appendAll(&_locks); |
duke@435 | 188 | assert(res->_stack.size() <= res->max_stack_size(), "stack overflow"); |
duke@435 | 189 | return res; |
duke@435 | 190 | } |
duke@435 | 191 | |
duke@435 | 192 | |
duke@435 | 193 | ValueStack* ValueStack::pop_scope() { |
duke@435 | 194 | assert(_scope->caller() != NULL, "scope must have caller"); |
duke@435 | 195 | IRScope* scope = _scope->caller(); |
duke@435 | 196 | int max_stack = max_stack_size() - _scope->method()->max_stack(); |
duke@435 | 197 | assert(max_stack >= 0, "stack underflow"); |
duke@435 | 198 | ValueStack* res = new ValueStack(scope, |
duke@435 | 199 | scope->method()->max_locals(), |
duke@435 | 200 | max_stack); |
duke@435 | 201 | // Preserves stack and monitors. Restores local and store state from caller scope. |
duke@435 | 202 | res->_stack.appendAll(&_stack); |
duke@435 | 203 | res->_locks.appendAll(&_locks); |
duke@435 | 204 | ValueStack* caller = caller_state(); |
duke@435 | 205 | if (caller != NULL) { |
duke@435 | 206 | for (int i = 0; i < caller->_locals.length(); i++) { |
duke@435 | 207 | res->_locals.at_put(i, caller->_locals.at(i)); |
duke@435 | 208 | } |
duke@435 | 209 | assert(res->_locals.length() == res->scope()->method()->max_locals(), "just checking"); |
duke@435 | 210 | } |
duke@435 | 211 | assert(res->_stack.size() <= res->max_stack_size(), "stack overflow"); |
duke@435 | 212 | return res; |
duke@435 | 213 | } |
duke@435 | 214 | |
duke@435 | 215 | |
duke@435 | 216 | void ValueStack::setup_phi_for_stack(BlockBegin* b, int index) { |
duke@435 | 217 | assert(stack_at(index)->as_Phi() == NULL || stack_at(index)->as_Phi()->block() != b, "phi function already created"); |
duke@435 | 218 | |
duke@435 | 219 | ValueType* t = stack_at(index)->type(); |
duke@435 | 220 | Value phi = new Phi(t, b, -index - 1); |
duke@435 | 221 | _stack[index] = phi; |
duke@435 | 222 | |
duke@435 | 223 | #ifdef ASSERT |
duke@435 | 224 | if (t->is_double_word()) { |
duke@435 | 225 | _stack[index + 1] = phi->hi_word(); |
duke@435 | 226 | } |
duke@435 | 227 | #endif |
duke@435 | 228 | } |
duke@435 | 229 | |
duke@435 | 230 | void ValueStack::setup_phi_for_local(BlockBegin* b, int index) { |
duke@435 | 231 | assert(local_at(index)->as_Phi() == NULL || local_at(index)->as_Phi()->block() != b, "phi function already created"); |
duke@435 | 232 | |
duke@435 | 233 | ValueType* t = local_at(index)->type(); |
duke@435 | 234 | Value phi = new Phi(t, b, index); |
duke@435 | 235 | store_local(index, phi); |
duke@435 | 236 | } |
duke@435 | 237 | |
duke@435 | 238 | #ifndef PRODUCT |
duke@435 | 239 | void ValueStack::print() { |
duke@435 | 240 | if (stack_is_empty()) { |
duke@435 | 241 | tty->print_cr("empty stack"); |
duke@435 | 242 | } else { |
duke@435 | 243 | InstructionPrinter ip; |
duke@435 | 244 | for (int i = 0; i < stack_size();) { |
duke@435 | 245 | Value t = stack_at_inc(i); |
duke@435 | 246 | tty->print("%2d ", i); |
duke@435 | 247 | ip.print_instr(t); |
duke@435 | 248 | tty->cr(); |
duke@435 | 249 | } |
duke@435 | 250 | } |
duke@435 | 251 | if (!no_active_locks()) { |
duke@435 | 252 | InstructionPrinter ip; |
duke@435 | 253 | for (int i = 0; i < locks_size(); i--) { |
duke@435 | 254 | Value t = lock_at(i); |
duke@435 | 255 | tty->print("lock %2d ", i); |
duke@435 | 256 | if (t == NULL) { |
duke@435 | 257 | tty->print("this"); |
duke@435 | 258 | } else { |
duke@435 | 259 | ip.print_instr(t); |
duke@435 | 260 | } |
duke@435 | 261 | tty->cr(); |
duke@435 | 262 | } |
duke@435 | 263 | } |
duke@435 | 264 | if (locals_size() > 0) { |
duke@435 | 265 | InstructionPrinter ip; |
duke@435 | 266 | for (int i = 0; i < locals_size();) { |
duke@435 | 267 | Value l = _locals[i]; |
duke@435 | 268 | tty->print("local %d ", i); |
duke@435 | 269 | if (l == NULL) { |
duke@435 | 270 | tty->print("null"); |
duke@435 | 271 | i ++; |
duke@435 | 272 | } else { |
duke@435 | 273 | ip.print_instr(l); |
duke@435 | 274 | if (l->type()->is_illegal() || l->type()->is_single_word()) i ++; else i += 2; |
duke@435 | 275 | } |
duke@435 | 276 | tty->cr(); |
duke@435 | 277 | } |
duke@435 | 278 | } |
duke@435 | 279 | } |
duke@435 | 280 | |
duke@435 | 281 | |
duke@435 | 282 | void ValueStack::verify() { |
duke@435 | 283 | Unimplemented(); |
duke@435 | 284 | } |
duke@435 | 285 | #endif // PRODUCT |