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 | class ValueStack: public CompilationResourceObj { |
duke@435 | 26 | private: |
duke@435 | 27 | IRScope* _scope; // the enclosing scope |
duke@435 | 28 | bool _lock_stack; // indicates that this ValueStack is for an exception site |
duke@435 | 29 | Values _locals; // the locals |
duke@435 | 30 | Values _stack; // the expression stack |
duke@435 | 31 | Values _locks; // the monitor stack (holding the locked values) |
duke@435 | 32 | |
duke@435 | 33 | Value check(ValueTag tag, Value t) { |
duke@435 | 34 | assert(tag == t->type()->tag() || tag == objectTag && t->type()->tag() == addressTag, "types must correspond"); |
duke@435 | 35 | return t; |
duke@435 | 36 | } |
duke@435 | 37 | |
duke@435 | 38 | Value check(ValueTag tag, Value t, Value h) { |
duke@435 | 39 | assert(h->as_HiWord()->lo_word() == t, "incorrect stack pair"); |
duke@435 | 40 | return check(tag, t); |
duke@435 | 41 | } |
duke@435 | 42 | |
duke@435 | 43 | // helper routine |
duke@435 | 44 | static void apply(Values list, void f(Value*)); |
duke@435 | 45 | |
duke@435 | 46 | public: |
duke@435 | 47 | // creation |
duke@435 | 48 | ValueStack(IRScope* scope, int locals_size, int max_stack_size); |
duke@435 | 49 | |
duke@435 | 50 | // merging |
duke@435 | 51 | ValueStack* copy(); // returns a copy of this w/ cleared locals |
duke@435 | 52 | ValueStack* copy_locks(); // returns a copy of this w/ cleared locals and stack |
duke@435 | 53 | // Note that when inlining of methods with exception |
duke@435 | 54 | // handlers is enabled, this stack may have a |
duke@435 | 55 | // non-empty expression stack (size defined by |
duke@435 | 56 | // scope()->lock_stack_size()) |
duke@435 | 57 | bool is_same(ValueStack* s); // returns true if this & s's types match (w/o checking locals) |
duke@435 | 58 | bool is_same_across_scopes(ValueStack* s); // same as is_same but returns true even if stacks are in different scopes (used for block merging w/inlining) |
duke@435 | 59 | |
duke@435 | 60 | // accessors |
duke@435 | 61 | IRScope* scope() const { return _scope; } |
duke@435 | 62 | bool is_lock_stack() const { return _lock_stack; } |
duke@435 | 63 | int locals_size() const { return _locals.length(); } |
duke@435 | 64 | int stack_size() const { return _stack.length(); } |
duke@435 | 65 | int locks_size() const { return _locks.length(); } |
duke@435 | 66 | int max_stack_size() const { return _stack.capacity(); } |
duke@435 | 67 | bool stack_is_empty() const { return _stack.is_empty(); } |
duke@435 | 68 | bool no_active_locks() const { return _locks.is_empty(); } |
duke@435 | 69 | ValueStack* caller_state() const; |
duke@435 | 70 | |
duke@435 | 71 | // locals access |
duke@435 | 72 | void clear_locals(); // sets all locals to NULL; |
duke@435 | 73 | |
duke@435 | 74 | // Kill local i. Also kill local i+1 if i was a long or double. |
duke@435 | 75 | void invalidate_local(int i) { |
duke@435 | 76 | Value x = _locals.at(i); |
duke@435 | 77 | if (x != NULL && x->type()->is_double_word()) { |
duke@435 | 78 | assert(_locals.at(i + 1)->as_HiWord()->lo_word() == x, "locals inconsistent"); |
duke@435 | 79 | _locals.at_put(i + 1, NULL); |
duke@435 | 80 | } |
duke@435 | 81 | _locals.at_put(i, NULL); |
duke@435 | 82 | } |
duke@435 | 83 | |
duke@435 | 84 | |
duke@435 | 85 | Value load_local(int i) const { |
duke@435 | 86 | Value x = _locals.at(i); |
duke@435 | 87 | if (x != NULL && x->type()->is_illegal()) return NULL; |
duke@435 | 88 | assert(x == NULL || x->as_HiWord() == NULL, "index points to hi word"); |
duke@435 | 89 | assert(x == NULL || x->type()->is_illegal() || x->type()->is_single_word() || x == _locals.at(i+1)->as_HiWord()->lo_word(), "locals inconsistent"); |
duke@435 | 90 | return x; |
duke@435 | 91 | } |
duke@435 | 92 | |
duke@435 | 93 | Value local_at(int i) const { return _locals.at(i); } |
duke@435 | 94 | |
duke@435 | 95 | // Store x into local i. |
duke@435 | 96 | void store_local(int i, Value x) { |
duke@435 | 97 | // Kill the old value |
duke@435 | 98 | invalidate_local(i); |
duke@435 | 99 | _locals.at_put(i, x); |
duke@435 | 100 | |
duke@435 | 101 | // Writing a double word can kill other locals |
duke@435 | 102 | if (x != NULL && x->type()->is_double_word()) { |
duke@435 | 103 | // If x + i was the start of a double word local then kill i + 2. |
duke@435 | 104 | Value x2 = _locals.at(i + 1); |
duke@435 | 105 | if (x2 != NULL && x2->type()->is_double_word()) { |
duke@435 | 106 | _locals.at_put(i + 2, NULL); |
duke@435 | 107 | } |
duke@435 | 108 | |
duke@435 | 109 | // If x is a double word local, also update i + 1. |
duke@435 | 110 | #ifdef ASSERT |
duke@435 | 111 | _locals.at_put(i + 1, x->hi_word()); |
duke@435 | 112 | #else |
duke@435 | 113 | _locals.at_put(i + 1, NULL); |
duke@435 | 114 | #endif |
duke@435 | 115 | } |
duke@435 | 116 | // If x - 1 was the start of a double word local then kill i - 1. |
duke@435 | 117 | if (i > 0) { |
duke@435 | 118 | Value prev = _locals.at(i - 1); |
duke@435 | 119 | if (prev != NULL && prev->type()->is_double_word()) { |
duke@435 | 120 | _locals.at_put(i - 1, NULL); |
duke@435 | 121 | } |
duke@435 | 122 | } |
duke@435 | 123 | } |
duke@435 | 124 | |
duke@435 | 125 | void replace_locals(ValueStack* with); |
duke@435 | 126 | |
duke@435 | 127 | // stack access |
duke@435 | 128 | Value stack_at(int i) const { |
duke@435 | 129 | Value x = _stack.at(i); |
duke@435 | 130 | assert(x->as_HiWord() == NULL, "index points to hi word"); |
duke@435 | 131 | assert(x->type()->is_single_word() || |
duke@435 | 132 | x->subst() == _stack.at(i+1)->as_HiWord()->lo_word(), "stack inconsistent"); |
duke@435 | 133 | return x; |
duke@435 | 134 | } |
duke@435 | 135 | |
duke@435 | 136 | Value stack_at_inc(int& i) const { |
duke@435 | 137 | Value x = stack_at(i); |
duke@435 | 138 | i += x->type()->size(); |
duke@435 | 139 | return x; |
duke@435 | 140 | } |
duke@435 | 141 | |
duke@435 | 142 | // pinning support |
duke@435 | 143 | void pin_stack_for_linear_scan(); |
duke@435 | 144 | |
duke@435 | 145 | // iteration |
duke@435 | 146 | void values_do(void f(Value*)); |
duke@435 | 147 | |
duke@435 | 148 | // untyped manipulation (for dup_x1, etc.) |
duke@435 | 149 | void clear_stack() { _stack.clear(); } |
duke@435 | 150 | void truncate_stack(int size) { _stack.trunc_to(size); } |
duke@435 | 151 | void raw_push(Value t) { _stack.push(t); } |
duke@435 | 152 | Value raw_pop() { return _stack.pop(); } |
duke@435 | 153 | |
duke@435 | 154 | // typed manipulation |
duke@435 | 155 | void ipush(Value t) { _stack.push(check(intTag , t)); } |
duke@435 | 156 | void fpush(Value t) { _stack.push(check(floatTag , t)); } |
duke@435 | 157 | void apush(Value t) { _stack.push(check(objectTag , t)); } |
duke@435 | 158 | void rpush(Value t) { _stack.push(check(addressTag, t)); } |
duke@435 | 159 | #ifdef ASSERT |
duke@435 | 160 | // in debug mode, use HiWord for 2-word values |
duke@435 | 161 | void lpush(Value t) { _stack.push(check(longTag , t)); _stack.push(new HiWord(t)); } |
duke@435 | 162 | void dpush(Value t) { _stack.push(check(doubleTag , t)); _stack.push(new HiWord(t)); } |
duke@435 | 163 | #else |
duke@435 | 164 | // in optimized mode, use NULL for 2-word values |
duke@435 | 165 | void lpush(Value t) { _stack.push(check(longTag , t)); _stack.push(NULL); } |
duke@435 | 166 | void dpush(Value t) { _stack.push(check(doubleTag , t)); _stack.push(NULL); } |
duke@435 | 167 | #endif // ASSERT |
duke@435 | 168 | |
duke@435 | 169 | void push(ValueType* type, Value t) { |
duke@435 | 170 | switch (type->tag()) { |
duke@435 | 171 | case intTag : ipush(t); return; |
duke@435 | 172 | case longTag : lpush(t); return; |
duke@435 | 173 | case floatTag : fpush(t); return; |
duke@435 | 174 | case doubleTag : dpush(t); return; |
duke@435 | 175 | case objectTag : apush(t); return; |
duke@435 | 176 | case addressTag: rpush(t); return; |
duke@435 | 177 | } |
duke@435 | 178 | ShouldNotReachHere(); |
duke@435 | 179 | } |
duke@435 | 180 | |
duke@435 | 181 | Value ipop() { return check(intTag , _stack.pop()); } |
duke@435 | 182 | Value fpop() { return check(floatTag , _stack.pop()); } |
duke@435 | 183 | Value apop() { return check(objectTag , _stack.pop()); } |
duke@435 | 184 | Value rpop() { return check(addressTag, _stack.pop()); } |
duke@435 | 185 | #ifdef ASSERT |
duke@435 | 186 | // in debug mode, check for HiWord consistency |
duke@435 | 187 | Value lpop() { Value h = _stack.pop(); return check(longTag , _stack.pop(), h); } |
duke@435 | 188 | Value dpop() { Value h = _stack.pop(); return check(doubleTag, _stack.pop(), h); } |
duke@435 | 189 | #else |
duke@435 | 190 | // in optimized mode, ignore HiWord since it is NULL |
duke@435 | 191 | Value lpop() { _stack.pop(); return check(longTag , _stack.pop()); } |
duke@435 | 192 | Value dpop() { _stack.pop(); return check(doubleTag, _stack.pop()); } |
duke@435 | 193 | #endif // ASSERT |
duke@435 | 194 | |
duke@435 | 195 | Value pop(ValueType* type) { |
duke@435 | 196 | switch (type->tag()) { |
duke@435 | 197 | case intTag : return ipop(); |
duke@435 | 198 | case longTag : return lpop(); |
duke@435 | 199 | case floatTag : return fpop(); |
duke@435 | 200 | case doubleTag : return dpop(); |
duke@435 | 201 | case objectTag : return apop(); |
duke@435 | 202 | case addressTag: return rpop(); |
duke@435 | 203 | } |
duke@435 | 204 | ShouldNotReachHere(); |
duke@435 | 205 | return NULL; |
duke@435 | 206 | } |
duke@435 | 207 | |
duke@435 | 208 | Values* pop_arguments(int argument_size); |
duke@435 | 209 | |
duke@435 | 210 | // locks access |
duke@435 | 211 | int lock (IRScope* scope, Value obj); |
duke@435 | 212 | int unlock(); |
duke@435 | 213 | Value lock_at(int i) const { return _locks.at(i); } |
duke@435 | 214 | |
duke@435 | 215 | // Inlining support |
duke@435 | 216 | ValueStack* push_scope(IRScope* scope); // "Push" new scope, returning new resulting stack |
duke@435 | 217 | // Preserves stack and locks, destroys locals |
duke@435 | 218 | ValueStack* pop_scope(); // "Pop" topmost scope, returning new resulting stack |
duke@435 | 219 | // Preserves stack and locks, destroys locals |
duke@435 | 220 | |
duke@435 | 221 | // SSA form IR support |
duke@435 | 222 | void setup_phi_for_stack(BlockBegin* b, int index); |
duke@435 | 223 | void setup_phi_for_local(BlockBegin* b, int index); |
duke@435 | 224 | |
duke@435 | 225 | // debugging |
duke@435 | 226 | void print() PRODUCT_RETURN; |
duke@435 | 227 | void verify() PRODUCT_RETURN; |
duke@435 | 228 | }; |
duke@435 | 229 | |
duke@435 | 230 | |
duke@435 | 231 | |
duke@435 | 232 | // Macro definitions for simple iteration of stack and local values of a ValueStack |
duke@435 | 233 | // The macros can be used like a for-loop. All variables (state, index and value) |
duke@435 | 234 | // must be defined before the loop. |
duke@435 | 235 | // When states are nested because of inlining, the stack of the innermost state |
duke@435 | 236 | // cumulates also the stack of the nested states. In contrast, the locals of all |
duke@435 | 237 | // states must be iterated each. |
duke@435 | 238 | // Use the following code pattern to iterate all stack values and all nested local values: |
duke@435 | 239 | // |
duke@435 | 240 | // ValueStack* state = ... // state that is iterated |
duke@435 | 241 | // int index; // current loop index (overwritten in loop) |
duke@435 | 242 | // Value value; // value at current loop index (overwritten in loop) |
duke@435 | 243 | // |
duke@435 | 244 | // for_each_stack_value(state, index, value { |
duke@435 | 245 | // do something with value and index |
duke@435 | 246 | // } |
duke@435 | 247 | // |
duke@435 | 248 | // for_each_state(state) { |
duke@435 | 249 | // for_each_local_value(state, index, value) { |
duke@435 | 250 | // do something with value and index |
duke@435 | 251 | // } |
duke@435 | 252 | // } |
duke@435 | 253 | // as an invariant, state is NULL now |
duke@435 | 254 | |
duke@435 | 255 | |
duke@435 | 256 | // construct a unique variable name with the line number where the macro is used |
duke@435 | 257 | #define temp_var3(x) temp__ ## x |
duke@435 | 258 | #define temp_var2(x) temp_var3(x) |
duke@435 | 259 | #define temp_var temp_var2(__LINE__) |
duke@435 | 260 | |
duke@435 | 261 | #define for_each_state(state) \ |
duke@435 | 262 | for (; state != NULL; state = state->caller_state()) |
duke@435 | 263 | |
duke@435 | 264 | #define for_each_local_value(state, index, value) \ |
duke@435 | 265 | int temp_var = state->locals_size(); \ |
duke@435 | 266 | for (index = 0; \ |
duke@435 | 267 | index < temp_var && (value = state->local_at(index), true); \ |
duke@435 | 268 | index += (value == NULL || value->type()->is_illegal() ? 1 : value->type()->size())) \ |
duke@435 | 269 | if (value != NULL) |
duke@435 | 270 | |
duke@435 | 271 | |
duke@435 | 272 | #define for_each_stack_value(state, index, value) \ |
duke@435 | 273 | int temp_var = state->stack_size(); \ |
duke@435 | 274 | for (index = 0; \ |
duke@435 | 275 | index < temp_var && (value = state->stack_at(index), true); \ |
duke@435 | 276 | index += value->type()->size()) |
duke@435 | 277 | |
duke@435 | 278 | |
duke@435 | 279 | #define for_each_lock_value(state, index, value) \ |
duke@435 | 280 | int temp_var = state->locks_size(); \ |
duke@435 | 281 | for (index = 0; \ |
duke@435 | 282 | index < temp_var && (value = state->lock_at(index), true); \ |
duke@435 | 283 | index++) \ |
duke@435 | 284 | if (value != NULL) |
duke@435 | 285 | |
duke@435 | 286 | |
duke@435 | 287 | // Macro definition for simple iteration of all state values of a ValueStack |
duke@435 | 288 | // Because the code cannot be executed in a single loop, the code must be passed |
duke@435 | 289 | // as a macro parameter. |
duke@435 | 290 | // Use the following code pattern to iterate all stack values and all nested local values: |
duke@435 | 291 | // |
duke@435 | 292 | // ValueStack* state = ... // state that is iterated |
duke@435 | 293 | // for_each_state_value(state, value, |
duke@435 | 294 | // do something with value (note that this is a macro parameter) |
duke@435 | 295 | // ); |
duke@435 | 296 | |
duke@435 | 297 | #define for_each_state_value(v_state, v_value, v_code) \ |
duke@435 | 298 | { \ |
duke@435 | 299 | int cur_index; \ |
duke@435 | 300 | ValueStack* cur_state = v_state; \ |
duke@435 | 301 | Value v_value; \ |
duke@435 | 302 | { \ |
duke@435 | 303 | for_each_stack_value(cur_state, cur_index, v_value) { \ |
duke@435 | 304 | v_code; \ |
duke@435 | 305 | } \ |
duke@435 | 306 | } \ |
duke@435 | 307 | for_each_state(cur_state) { \ |
duke@435 | 308 | for_each_local_value(cur_state, cur_index, v_value) { \ |
duke@435 | 309 | v_code; \ |
duke@435 | 310 | } \ |
duke@435 | 311 | } \ |
duke@435 | 312 | } |
duke@435 | 313 | |
duke@435 | 314 | |
duke@435 | 315 | // Macro definition for simple iteration of all phif functions of a block, i.e all |
duke@435 | 316 | // phi functions of the ValueStack where the block matches. |
duke@435 | 317 | // Use the following code pattern to iterate all phi functions of a block: |
duke@435 | 318 | // |
duke@435 | 319 | // BlockBegin* block = ... // block that is iterated |
duke@435 | 320 | // for_each_phi_function(block, phi, |
duke@435 | 321 | // do something with the phi function phi (note that this is a macro parameter) |
duke@435 | 322 | // ); |
duke@435 | 323 | |
duke@435 | 324 | #define for_each_phi_fun(v_block, v_phi, v_code) \ |
duke@435 | 325 | { \ |
duke@435 | 326 | int cur_index; \ |
duke@435 | 327 | ValueStack* cur_state = v_block->state(); \ |
duke@435 | 328 | Value value; \ |
duke@435 | 329 | { \ |
duke@435 | 330 | for_each_stack_value(cur_state, cur_index, value) { \ |
duke@435 | 331 | Phi* v_phi = value->as_Phi(); \ |
duke@435 | 332 | if (v_phi != NULL && v_phi->block() == v_block) { \ |
duke@435 | 333 | v_code; \ |
duke@435 | 334 | } \ |
duke@435 | 335 | } \ |
duke@435 | 336 | } \ |
duke@435 | 337 | { \ |
duke@435 | 338 | for_each_local_value(cur_state, cur_index, value) { \ |
duke@435 | 339 | Phi* v_phi = value->as_Phi(); \ |
duke@435 | 340 | if (v_phi != NULL && v_phi->block() == v_block) { \ |
duke@435 | 341 | v_code; \ |
duke@435 | 342 | } \ |
duke@435 | 343 | } \ |
duke@435 | 344 | } \ |
duke@435 | 345 | } |