src/share/vm/prims/methodComparator.cpp

Fri, 22 Oct 2010 15:59:34 -0400

author
acorn
date
Fri, 22 Oct 2010 15:59:34 -0400
changeset 2233
fa83ab460c54
parent 1957
136b78722a08
child 2268
3b2dea75431e
permissions
-rw-r--r--

6988353: refactor contended sync subsystem
Summary: reduce complexity by factoring synchronizer.cpp
Reviewed-by: dholmes, never, coleenp

duke@435 1 /*
jrose@1934 2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. 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 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_methodComparator.cpp.incl"
duke@435 27
duke@435 28 BytecodeStream *MethodComparator::_s_old;
duke@435 29 BytecodeStream *MethodComparator::_s_new;
duke@435 30 constantPoolOop MethodComparator::_old_cp;
duke@435 31 constantPoolOop MethodComparator::_new_cp;
duke@435 32 BciMap *MethodComparator::_bci_map;
duke@435 33 bool MethodComparator::_switchable_test;
duke@435 34 GrowableArray<int> *MethodComparator::_fwd_jmps;
duke@435 35
duke@435 36 bool MethodComparator::methods_EMCP(methodOop old_method, methodOop new_method) {
duke@435 37 if (old_method->code_size() != new_method->code_size())
duke@435 38 return false;
duke@435 39 if (check_stack_and_locals_size(old_method, new_method) != 0) {
duke@435 40 // RC_TRACE macro has an embedded ResourceMark
duke@435 41 RC_TRACE(0x00800000, ("Methods %s non-comparable with diagnosis %d",
duke@435 42 old_method->name()->as_C_string(),
duke@435 43 check_stack_and_locals_size(old_method, new_method)));
duke@435 44 return false;
duke@435 45 }
duke@435 46
duke@435 47 _old_cp = old_method->constants();
duke@435 48 _new_cp = new_method->constants();
duke@435 49 BytecodeStream s_old(old_method);
duke@435 50 BytecodeStream s_new(new_method);
duke@435 51 _s_old = &s_old;
duke@435 52 _s_new = &s_new;
duke@435 53 _switchable_test = false;
duke@435 54 Bytecodes::Code c_old, c_new;
duke@435 55
duke@435 56 while ((c_old = s_old.next()) >= 0) {
duke@435 57 if ((c_new = s_new.next()) < 0 || c_old != c_new)
duke@435 58 return false;
duke@435 59
duke@435 60 if (! args_same(c_old, c_new))
duke@435 61 return false;
duke@435 62 }
duke@435 63 return true;
duke@435 64 }
duke@435 65
duke@435 66
duke@435 67 bool MethodComparator::methods_switchable(methodOop old_method, methodOop new_method,
duke@435 68 BciMap &bci_map) {
duke@435 69 if (old_method->code_size() > new_method->code_size())
duke@435 70 // Something has definitely been deleted in the new method, compared to the old one.
duke@435 71 return false;
duke@435 72
duke@435 73 if (! check_stack_and_locals_size(old_method, new_method))
duke@435 74 return false;
duke@435 75
duke@435 76 _old_cp = old_method->constants();
duke@435 77 _new_cp = new_method->constants();
duke@435 78 BytecodeStream s_old(old_method);
duke@435 79 BytecodeStream s_new(new_method);
duke@435 80 _s_old = &s_old;
duke@435 81 _s_new = &s_new;
duke@435 82 _bci_map = &bci_map;
duke@435 83 _switchable_test = true;
duke@435 84 GrowableArray<int> fwd_jmps(16);
duke@435 85 _fwd_jmps = &fwd_jmps;
duke@435 86 Bytecodes::Code c_old, c_new;
duke@435 87
duke@435 88 while ((c_old = s_old.next()) >= 0) {
duke@435 89 if ((c_new = s_new.next()) < 0)
duke@435 90 return false;
duke@435 91 if (! (c_old == c_new && args_same(c_old, c_new))) {
duke@435 92 int old_bci = s_old.bci();
duke@435 93 int new_st_bci = s_new.bci();
duke@435 94 bool found_match = false;
duke@435 95 do {
duke@435 96 c_new = s_new.next();
duke@435 97 if (c_new == c_old && args_same(c_old, c_new)) {
duke@435 98 found_match = true;
duke@435 99 break;
duke@435 100 }
duke@435 101 } while (c_new >= 0);
duke@435 102 if (! found_match)
duke@435 103 return false;
duke@435 104 int new_end_bci = s_new.bci();
duke@435 105 bci_map.store_fragment_location(old_bci, new_st_bci, new_end_bci);
duke@435 106 }
duke@435 107 }
duke@435 108
duke@435 109 // Now we can test all forward jumps
duke@435 110 for (int i = 0; i < fwd_jmps.length() / 2; i++) {
duke@435 111 if (! bci_map.old_and_new_locations_same(fwd_jmps.at(i*2), fwd_jmps.at(i*2+1))) {
duke@435 112 RC_TRACE(0x00800000,
duke@435 113 ("Fwd jump miss: old dest = %d, calc new dest = %d, act new dest = %d",
duke@435 114 fwd_jmps.at(i*2), bci_map.new_bci_for_old(fwd_jmps.at(i*2)),
duke@435 115 fwd_jmps.at(i*2+1)));
duke@435 116 return false;
duke@435 117 }
duke@435 118 }
duke@435 119
duke@435 120 return true;
duke@435 121 }
duke@435 122
duke@435 123
duke@435 124 bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
duke@435 125 // BytecodeStream returns the correct standard Java bytecodes for various "fast"
duke@435 126 // bytecode versions, so we don't have to bother about them here..
duke@435 127 switch (c_old) {
duke@435 128 case Bytecodes::_new : // fall through
duke@435 129 case Bytecodes::_anewarray : // fall through
duke@435 130 case Bytecodes::_multianewarray : // fall through
duke@435 131 case Bytecodes::_checkcast : // fall through
duke@435 132 case Bytecodes::_instanceof : {
jrose@1920 133 u2 cpi_old = _s_old->get_index_u2();
jrose@1920 134 u2 cpi_new = _s_new->get_index_u2();
duke@435 135 if ((_old_cp->klass_at_noresolve(cpi_old) != _new_cp->klass_at_noresolve(cpi_new)))
duke@435 136 return false;
duke@435 137 if (c_old == Bytecodes::_multianewarray &&
duke@435 138 *(jbyte*)(_s_old->bcp() + 3) != *(jbyte*)(_s_new->bcp() + 3))
duke@435 139 return false;
duke@435 140 break;
duke@435 141 }
duke@435 142
duke@435 143 case Bytecodes::_getstatic : // fall through
duke@435 144 case Bytecodes::_putstatic : // fall through
duke@435 145 case Bytecodes::_getfield : // fall through
duke@435 146 case Bytecodes::_putfield : // fall through
duke@435 147 case Bytecodes::_invokevirtual : // fall through
duke@435 148 case Bytecodes::_invokespecial : // fall through
duke@435 149 case Bytecodes::_invokestatic : // fall through
jrose@1920 150 case Bytecodes::_invokedynamic : // fall through
duke@435 151 case Bytecodes::_invokeinterface : {
jrose@1928 152 int cpci_old = _s_old->has_index_u4() ? _s_old->get_index_u4() : _s_old->get_index_u2_cpcache();
jrose@1928 153 int cpci_new = _s_new->has_index_u4() ? _s_new->get_index_u4() : _s_new->get_index_u2_cpcache();
duke@435 154 // Check if the names of classes, field/method names and signatures at these indexes
duke@435 155 // are the same. Indices which are really into constantpool cache (rather than constant
duke@435 156 // pool itself) are accepted by the constantpool query routines below.
duke@435 157 if ((_old_cp->klass_ref_at_noresolve(cpci_old) != _new_cp->klass_ref_at_noresolve(cpci_new)) ||
duke@435 158 (_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) ||
duke@435 159 (_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new)))
duke@435 160 return false;
duke@435 161 break;
duke@435 162 }
duke@435 163
duke@435 164 case Bytecodes::_ldc : // fall through
duke@435 165 case Bytecodes::_ldc_w : {
jrose@1957 166 Bytecode_loadconstant* ldc_old = Bytecode_loadconstant_at(_s_old->method(), _s_old->bci());
jrose@1957 167 Bytecode_loadconstant* ldc_new = Bytecode_loadconstant_at(_s_new->method(), _s_new->bci());
jrose@1957 168 int cpi_old = ldc_old->pool_index();
jrose@1957 169 int cpi_new = ldc_new->pool_index();
duke@435 170 constantTag tag_old = _old_cp->tag_at(cpi_old);
duke@435 171 constantTag tag_new = _new_cp->tag_at(cpi_new);
duke@435 172 if (tag_old.is_int() || tag_old.is_float()) {
duke@435 173 if (tag_old.value() != tag_new.value())
duke@435 174 return false;
duke@435 175 if (tag_old.is_int()) {
duke@435 176 if (_old_cp->int_at(cpi_old) != _new_cp->int_at(cpi_new))
duke@435 177 return false;
duke@435 178 } else {
jrose@1929 179 // Use jint_cast to compare the bits rather than numerical values.
jrose@1929 180 // This makes a difference for NaN constants.
jrose@1929 181 if (jint_cast(_old_cp->float_at(cpi_old)) != jint_cast(_new_cp->float_at(cpi_new)))
duke@435 182 return false;
duke@435 183 }
duke@435 184 } else if (tag_old.is_string() || tag_old.is_unresolved_string()) {
duke@435 185 if (! (tag_new.is_unresolved_string() || tag_new.is_string()))
duke@435 186 return false;
duke@435 187 if (strcmp(_old_cp->string_at_noresolve(cpi_old),
duke@435 188 _new_cp->string_at_noresolve(cpi_new)) != 0)
duke@435 189 return false;
jrose@1957 190 } else if (tag_old.is_klass() || tag_old.is_unresolved_klass()) {
jrose@1957 191 // tag_old should be klass - 4881222
duke@435 192 if (! (tag_new.is_unresolved_klass() || tag_new.is_klass()))
duke@435 193 return false;
duke@435 194 if (_old_cp->klass_at_noresolve(cpi_old) !=
duke@435 195 _new_cp->klass_at_noresolve(cpi_new))
duke@435 196 return false;
jrose@1957 197 } else if (tag_old.is_method_type() && tag_new.is_method_type()) {
jrose@1957 198 int mti_old = _old_cp->method_type_index_at(cpi_old);
jrose@1957 199 int mti_new = _new_cp->method_type_index_at(cpi_new);
jrose@1957 200 if ((_old_cp->symbol_at(mti_old) != _new_cp->symbol_at(mti_new)))
jrose@1957 201 return false;
jrose@1957 202 } else if (tag_old.is_method_handle() && tag_new.is_method_handle()) {
jrose@1957 203 if (_old_cp->method_handle_ref_kind_at(cpi_old) !=
jrose@1957 204 _new_cp->method_handle_ref_kind_at(cpi_new))
jrose@1957 205 return false;
jrose@1957 206 int mhi_old = _old_cp->method_handle_index_at(cpi_old);
jrose@1957 207 int mhi_new = _new_cp->method_handle_index_at(cpi_new);
jrose@1957 208 if ((_old_cp->uncached_klass_ref_at_noresolve(mhi_old) != _new_cp->uncached_klass_ref_at_noresolve(mhi_new)) ||
jrose@1957 209 (_old_cp->uncached_name_ref_at(mhi_old) != _new_cp->uncached_name_ref_at(mhi_new)) ||
jrose@1957 210 (_old_cp->uncached_signature_ref_at(mhi_old) != _new_cp->uncached_signature_ref_at(mhi_new)))
jrose@1957 211 return false;
jrose@1957 212 } else {
jrose@1957 213 return false; // unknown tag
duke@435 214 }
duke@435 215 break;
duke@435 216 }
duke@435 217
duke@435 218 case Bytecodes::_ldc2_w : {
jrose@1920 219 u2 cpi_old = _s_old->get_index_u2();
jrose@1920 220 u2 cpi_new = _s_new->get_index_u2();
duke@435 221 constantTag tag_old = _old_cp->tag_at(cpi_old);
duke@435 222 constantTag tag_new = _new_cp->tag_at(cpi_new);
duke@435 223 if (tag_old.value() != tag_new.value())
duke@435 224 return false;
duke@435 225 if (tag_old.is_long()) {
duke@435 226 if (_old_cp->long_at(cpi_old) != _new_cp->long_at(cpi_new))
duke@435 227 return false;
duke@435 228 } else {
jrose@1929 229 // Use jlong_cast to compare the bits rather than numerical values.
jrose@1929 230 // This makes a difference for NaN constants.
jrose@1929 231 if (jlong_cast(_old_cp->double_at(cpi_old)) != jlong_cast(_new_cp->double_at(cpi_new)))
duke@435 232 return false;
duke@435 233 }
duke@435 234 break;
duke@435 235 }
duke@435 236
duke@435 237 case Bytecodes::_bipush :
duke@435 238 if (_s_old->bcp()[1] != _s_new->bcp()[1])
duke@435 239 return false;
duke@435 240 break;
duke@435 241
duke@435 242 case Bytecodes::_sipush :
jrose@1920 243 if (_s_old->get_index_u2() != _s_new->get_index_u2())
duke@435 244 return false;
duke@435 245 break;
duke@435 246
duke@435 247 case Bytecodes::_aload : // fall through
duke@435 248 case Bytecodes::_astore : // fall through
duke@435 249 case Bytecodes::_dload : // fall through
duke@435 250 case Bytecodes::_dstore : // fall through
duke@435 251 case Bytecodes::_fload : // fall through
duke@435 252 case Bytecodes::_fstore : // fall through
duke@435 253 case Bytecodes::_iload : // fall through
duke@435 254 case Bytecodes::_istore : // fall through
duke@435 255 case Bytecodes::_lload : // fall through
duke@435 256 case Bytecodes::_lstore : // fall through
duke@435 257 case Bytecodes::_ret :
duke@435 258 if (_s_old->is_wide() != _s_new->is_wide())
duke@435 259 return false;
duke@435 260 if (_s_old->get_index() != _s_new->get_index())
duke@435 261 return false;
duke@435 262 break;
duke@435 263
duke@435 264 case Bytecodes::_goto : // fall through
duke@435 265 case Bytecodes::_if_acmpeq : // fall through
duke@435 266 case Bytecodes::_if_acmpne : // fall through
duke@435 267 case Bytecodes::_if_icmpeq : // fall through
duke@435 268 case Bytecodes::_if_icmpne : // fall through
duke@435 269 case Bytecodes::_if_icmplt : // fall through
duke@435 270 case Bytecodes::_if_icmpge : // fall through
duke@435 271 case Bytecodes::_if_icmpgt : // fall through
duke@435 272 case Bytecodes::_if_icmple : // fall through
duke@435 273 case Bytecodes::_ifeq : // fall through
duke@435 274 case Bytecodes::_ifne : // fall through
duke@435 275 case Bytecodes::_iflt : // fall through
duke@435 276 case Bytecodes::_ifge : // fall through
duke@435 277 case Bytecodes::_ifgt : // fall through
duke@435 278 case Bytecodes::_ifle : // fall through
duke@435 279 case Bytecodes::_ifnonnull : // fall through
duke@435 280 case Bytecodes::_ifnull : // fall through
duke@435 281 case Bytecodes::_jsr : {
jrose@1929 282 int old_ofs = _s_old->bytecode()->get_offset_s2(c_old);
jrose@1929 283 int new_ofs = _s_new->bytecode()->get_offset_s2(c_new);
duke@435 284 if (_switchable_test) {
duke@435 285 int old_dest = _s_old->bci() + old_ofs;
duke@435 286 int new_dest = _s_new->bci() + new_ofs;
duke@435 287 if (old_ofs < 0 && new_ofs < 0) {
duke@435 288 if (! _bci_map->old_and_new_locations_same(old_dest, new_dest))
duke@435 289 return false;
duke@435 290 } else if (old_ofs > 0 && new_ofs > 0) {
duke@435 291 _fwd_jmps->append(old_dest);
duke@435 292 _fwd_jmps->append(new_dest);
duke@435 293 } else {
duke@435 294 return false;
duke@435 295 }
duke@435 296 } else {
duke@435 297 if (old_ofs != new_ofs)
duke@435 298 return false;
duke@435 299 }
duke@435 300 break;
duke@435 301 }
duke@435 302
duke@435 303 case Bytecodes::_iinc :
duke@435 304 if (_s_old->is_wide() != _s_new->is_wide())
duke@435 305 return false;
duke@435 306 if (! _s_old->is_wide()) {
jrose@1920 307 // We could use get_index_u1 and get_constant_u1, but it's simpler to grab both bytes at once:
jrose@1920 308 if (Bytes::get_Java_u2(_s_old->bcp() + 1) != Bytes::get_Java_u2(_s_new->bcp() + 1))
duke@435 309 return false;
duke@435 310 } else {
jrose@1920 311 // We could use get_index_u2 and get_constant_u2, but it's simpler to grab all four bytes at once:
duke@435 312 if (Bytes::get_Java_u4(_s_old->bcp() + 1) != Bytes::get_Java_u4(_s_new->bcp() + 1))
duke@435 313 return false;
duke@435 314 }
duke@435 315 break;
duke@435 316
duke@435 317 case Bytecodes::_goto_w : // fall through
duke@435 318 case Bytecodes::_jsr_w : {
jrose@1929 319 int old_ofs = _s_old->bytecode()->get_offset_s4(c_old);
jrose@1929 320 int new_ofs = _s_new->bytecode()->get_offset_s4(c_new);
duke@435 321 if (_switchable_test) {
duke@435 322 int old_dest = _s_old->bci() + old_ofs;
duke@435 323 int new_dest = _s_new->bci() + new_ofs;
duke@435 324 if (old_ofs < 0 && new_ofs < 0) {
duke@435 325 if (! _bci_map->old_and_new_locations_same(old_dest, new_dest))
duke@435 326 return false;
duke@435 327 } else if (old_ofs > 0 && new_ofs > 0) {
duke@435 328 _fwd_jmps->append(old_dest);
duke@435 329 _fwd_jmps->append(new_dest);
duke@435 330 } else {
duke@435 331 return false;
duke@435 332 }
duke@435 333 } else {
duke@435 334 if (old_ofs != new_ofs)
duke@435 335 return false;
duke@435 336 }
duke@435 337 break;
duke@435 338 }
duke@435 339
duke@435 340 case Bytecodes::_lookupswitch : // fall through
duke@435 341 case Bytecodes::_tableswitch : {
duke@435 342 if (_switchable_test) {
duke@435 343 address aligned_bcp_old = (address) round_to((intptr_t)_s_old->bcp() + 1, jintSize);
duke@435 344 address aligned_bcp_new = (address) round_to((intptr_t)_s_new->bcp() + 1, jintSize);
duke@435 345 int default_old = (int) Bytes::get_Java_u4(aligned_bcp_old);
duke@435 346 int default_new = (int) Bytes::get_Java_u4(aligned_bcp_new);
duke@435 347 _fwd_jmps->append(_s_old->bci() + default_old);
duke@435 348 _fwd_jmps->append(_s_new->bci() + default_new);
duke@435 349 if (c_old == Bytecodes::_lookupswitch) {
duke@435 350 int npairs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize);
duke@435 351 int npairs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize);
duke@435 352 if (npairs_old != npairs_new)
duke@435 353 return false;
duke@435 354 for (int i = 0; i < npairs_old; i++) {
duke@435 355 int match_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i)*jintSize);
duke@435 356 int match_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i)*jintSize);
duke@435 357 if (match_old != match_new)
duke@435 358 return false;
duke@435 359 int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i+1)*jintSize);
duke@435 360 int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i+1)*jintSize);
duke@435 361 _fwd_jmps->append(_s_old->bci() + ofs_old);
duke@435 362 _fwd_jmps->append(_s_new->bci() + ofs_new);
duke@435 363 }
duke@435 364 } else if (c_old == Bytecodes::_tableswitch) {
duke@435 365 int lo_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize);
duke@435 366 int lo_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize);
duke@435 367 if (lo_old != lo_new)
duke@435 368 return false;
duke@435 369 int hi_old = (int) Bytes::get_Java_u4(aligned_bcp_old + 2*jintSize);
duke@435 370 int hi_new = (int) Bytes::get_Java_u4(aligned_bcp_new + 2*jintSize);
duke@435 371 if (hi_old != hi_new)
duke@435 372 return false;
duke@435 373 for (int i = 0; i < hi_old - lo_old + 1; i++) {
duke@435 374 int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (3+i)*jintSize);
duke@435 375 int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (3+i)*jintSize);
duke@435 376 _fwd_jmps->append(_s_old->bci() + ofs_old);
duke@435 377 _fwd_jmps->append(_s_new->bci() + ofs_new);
duke@435 378 }
duke@435 379 }
duke@435 380 } else { // !_switchable_test, can use fast rough compare
jrose@1920 381 int len_old = _s_old->instruction_size();
jrose@1920 382 int len_new = _s_new->instruction_size();
duke@435 383 if (len_old != len_new)
duke@435 384 return false;
duke@435 385 if (memcmp(_s_old->bcp(), _s_new->bcp(), len_old) != 0)
duke@435 386 return false;
duke@435 387 }
duke@435 388 break;
duke@435 389 }
duke@435 390 }
duke@435 391
duke@435 392 return true;
duke@435 393 }
duke@435 394
duke@435 395
duke@435 396 int MethodComparator::check_stack_and_locals_size(methodOop old_method, methodOop new_method) {
duke@435 397 if (old_method->max_stack() != new_method->max_stack()) {
duke@435 398 return 1;
duke@435 399 } else if (old_method->max_locals() != new_method->max_locals()) {
duke@435 400 return 2;
duke@435 401 } else if (old_method->size_of_parameters() != new_method->size_of_parameters()) {
duke@435 402 return 3;
duke@435 403 } else return 0;
duke@435 404 }

mercurial