annotate src/share/vm/prims/methodComparator.cpp @ 1485:ab102d5d923e

6939207: refactor constant pool index processing Summary: Factored cleanup of instruction decode which prepares for enhanced ldc semantics. Reviewed-by: twisti
author jrose
date Sun, 23 May 2010 01:38:26 -0700
parents bd02caa94611
children beb77f0d41b3
rev   line source
duke@0 1 /*
jrose@1485 2 * Copyright 2000-2010 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 # include "incls/_precompiled.incl"
duke@0 26 # include "incls/_methodComparator.cpp.incl"
duke@0 27
duke@0 28 BytecodeStream *MethodComparator::_s_old;
duke@0 29 BytecodeStream *MethodComparator::_s_new;
duke@0 30 constantPoolOop MethodComparator::_old_cp;
duke@0 31 constantPoolOop MethodComparator::_new_cp;
duke@0 32 BciMap *MethodComparator::_bci_map;
duke@0 33 bool MethodComparator::_switchable_test;
duke@0 34 GrowableArray<int> *MethodComparator::_fwd_jmps;
duke@0 35
duke@0 36 bool MethodComparator::methods_EMCP(methodOop old_method, methodOop new_method) {
duke@0 37 if (old_method->code_size() != new_method->code_size())
duke@0 38 return false;
duke@0 39 if (check_stack_and_locals_size(old_method, new_method) != 0) {
duke@0 40 // RC_TRACE macro has an embedded ResourceMark
duke@0 41 RC_TRACE(0x00800000, ("Methods %s non-comparable with diagnosis %d",
duke@0 42 old_method->name()->as_C_string(),
duke@0 43 check_stack_and_locals_size(old_method, new_method)));
duke@0 44 return false;
duke@0 45 }
duke@0 46
duke@0 47 _old_cp = old_method->constants();
duke@0 48 _new_cp = new_method->constants();
duke@0 49 BytecodeStream s_old(old_method);
duke@0 50 BytecodeStream s_new(new_method);
duke@0 51 _s_old = &s_old;
duke@0 52 _s_new = &s_new;
duke@0 53 _switchable_test = false;
duke@0 54 Bytecodes::Code c_old, c_new;
duke@0 55
duke@0 56 while ((c_old = s_old.next()) >= 0) {
duke@0 57 if ((c_new = s_new.next()) < 0 || c_old != c_new)
duke@0 58 return false;
duke@0 59
duke@0 60 if (! args_same(c_old, c_new))
duke@0 61 return false;
duke@0 62 }
duke@0 63 return true;
duke@0 64 }
duke@0 65
duke@0 66
duke@0 67 bool MethodComparator::methods_switchable(methodOop old_method, methodOop new_method,
duke@0 68 BciMap &bci_map) {
duke@0 69 if (old_method->code_size() > new_method->code_size())
duke@0 70 // Something has definitely been deleted in the new method, compared to the old one.
duke@0 71 return false;
duke@0 72
duke@0 73 if (! check_stack_and_locals_size(old_method, new_method))
duke@0 74 return false;
duke@0 75
duke@0 76 _old_cp = old_method->constants();
duke@0 77 _new_cp = new_method->constants();
duke@0 78 BytecodeStream s_old(old_method);
duke@0 79 BytecodeStream s_new(new_method);
duke@0 80 _s_old = &s_old;
duke@0 81 _s_new = &s_new;
duke@0 82 _bci_map = &bci_map;
duke@0 83 _switchable_test = true;
duke@0 84 GrowableArray<int> fwd_jmps(16);
duke@0 85 _fwd_jmps = &fwd_jmps;
duke@0 86 Bytecodes::Code c_old, c_new;
duke@0 87
duke@0 88 while ((c_old = s_old.next()) >= 0) {
duke@0 89 if ((c_new = s_new.next()) < 0)
duke@0 90 return false;
duke@0 91 if (! (c_old == c_new && args_same(c_old, c_new))) {
duke@0 92 int old_bci = s_old.bci();
duke@0 93 int new_st_bci = s_new.bci();
duke@0 94 bool found_match = false;
duke@0 95 do {
duke@0 96 c_new = s_new.next();
duke@0 97 if (c_new == c_old && args_same(c_old, c_new)) {
duke@0 98 found_match = true;
duke@0 99 break;
duke@0 100 }
duke@0 101 } while (c_new >= 0);
duke@0 102 if (! found_match)
duke@0 103 return false;
duke@0 104 int new_end_bci = s_new.bci();
duke@0 105 bci_map.store_fragment_location(old_bci, new_st_bci, new_end_bci);
duke@0 106 }
duke@0 107 }
duke@0 108
duke@0 109 // Now we can test all forward jumps
duke@0 110 for (int i = 0; i < fwd_jmps.length() / 2; i++) {
duke@0 111 if (! bci_map.old_and_new_locations_same(fwd_jmps.at(i*2), fwd_jmps.at(i*2+1))) {
duke@0 112 RC_TRACE(0x00800000,
duke@0 113 ("Fwd jump miss: old dest = %d, calc new dest = %d, act new dest = %d",
duke@0 114 fwd_jmps.at(i*2), bci_map.new_bci_for_old(fwd_jmps.at(i*2)),
duke@0 115 fwd_jmps.at(i*2+1)));
duke@0 116 return false;
duke@0 117 }
duke@0 118 }
duke@0 119
duke@0 120 return true;
duke@0 121 }
duke@0 122
duke@0 123
duke@0 124 bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
duke@0 125 // BytecodeStream returns the correct standard Java bytecodes for various "fast"
duke@0 126 // bytecode versions, so we don't have to bother about them here..
duke@0 127 switch (c_old) {
duke@0 128 case Bytecodes::_new : // fall through
duke@0 129 case Bytecodes::_anewarray : // fall through
duke@0 130 case Bytecodes::_multianewarray : // fall through
duke@0 131 case Bytecodes::_checkcast : // fall through
duke@0 132 case Bytecodes::_instanceof : {
jrose@1485 133 u2 cpi_old = _s_old->get_index_u2();
jrose@1485 134 u2 cpi_new = _s_new->get_index_u2();
duke@0 135 if ((_old_cp->klass_at_noresolve(cpi_old) != _new_cp->klass_at_noresolve(cpi_new)))
duke@0 136 return false;
duke@0 137 if (c_old == Bytecodes::_multianewarray &&
duke@0 138 *(jbyte*)(_s_old->bcp() + 3) != *(jbyte*)(_s_new->bcp() + 3))
duke@0 139 return false;
duke@0 140 break;
duke@0 141 }
duke@0 142
duke@0 143 case Bytecodes::_getstatic : // fall through
duke@0 144 case Bytecodes::_putstatic : // fall through
duke@0 145 case Bytecodes::_getfield : // fall through
duke@0 146 case Bytecodes::_putfield : // fall through
duke@0 147 case Bytecodes::_invokevirtual : // fall through
duke@0 148 case Bytecodes::_invokespecial : // fall through
duke@0 149 case Bytecodes::_invokestatic : // fall through
jrose@1485 150 case Bytecodes::_invokedynamic : // fall through
duke@0 151 case Bytecodes::_invokeinterface : {
jrose@1485 152 u2 cpci_old = _s_old->has_index_u4() ? _s_old->get_index_u4() : _s_old->get_index_u2();
jrose@1485 153 u2 cpci_new = _s_new->has_index_u4() ? _s_new->get_index_u4() : _s_new->get_index_u2();
duke@0 154 // Check if the names of classes, field/method names and signatures at these indexes
duke@0 155 // are the same. Indices which are really into constantpool cache (rather than constant
duke@0 156 // pool itself) are accepted by the constantpool query routines below.
duke@0 157 if ((_old_cp->klass_ref_at_noresolve(cpci_old) != _new_cp->klass_ref_at_noresolve(cpci_new)) ||
duke@0 158 (_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) ||
duke@0 159 (_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new)))
duke@0 160 return false;
duke@0 161 break;
duke@0 162 }
duke@0 163
duke@0 164 case Bytecodes::_ldc : // fall through
duke@0 165 case Bytecodes::_ldc_w : {
duke@0 166 u2 cpi_old, cpi_new;
duke@0 167 if (c_old == Bytecodes::_ldc) {
duke@0 168 cpi_old = _s_old->bcp()[1];
duke@0 169 cpi_new = _s_new->bcp()[1];
duke@0 170 } else {
jrose@1485 171 cpi_old = _s_old->get_index_u2();
jrose@1485 172 cpi_new = _s_new->get_index_u2();
duke@0 173 }
duke@0 174 constantTag tag_old = _old_cp->tag_at(cpi_old);
duke@0 175 constantTag tag_new = _new_cp->tag_at(cpi_new);
duke@0 176 if (tag_old.is_int() || tag_old.is_float()) {
duke@0 177 if (tag_old.value() != tag_new.value())
duke@0 178 return false;
duke@0 179 if (tag_old.is_int()) {
duke@0 180 if (_old_cp->int_at(cpi_old) != _new_cp->int_at(cpi_new))
duke@0 181 return false;
duke@0 182 } else {
duke@0 183 if (_old_cp->float_at(cpi_old) != _new_cp->float_at(cpi_new))
duke@0 184 return false;
duke@0 185 }
duke@0 186 } else if (tag_old.is_string() || tag_old.is_unresolved_string()) {
duke@0 187 if (! (tag_new.is_unresolved_string() || tag_new.is_string()))
duke@0 188 return false;
duke@0 189 if (strcmp(_old_cp->string_at_noresolve(cpi_old),
duke@0 190 _new_cp->string_at_noresolve(cpi_new)) != 0)
duke@0 191 return false;
duke@0 192 } else { // tag_old should be klass - 4881222
duke@0 193 if (! (tag_new.is_unresolved_klass() || tag_new.is_klass()))
duke@0 194 return false;
duke@0 195 if (_old_cp->klass_at_noresolve(cpi_old) !=
duke@0 196 _new_cp->klass_at_noresolve(cpi_new))
duke@0 197 return false;
duke@0 198 }
duke@0 199 break;
duke@0 200 }
duke@0 201
duke@0 202 case Bytecodes::_ldc2_w : {
jrose@1485 203 u2 cpi_old = _s_old->get_index_u2();
jrose@1485 204 u2 cpi_new = _s_new->get_index_u2();
duke@0 205 constantTag tag_old = _old_cp->tag_at(cpi_old);
duke@0 206 constantTag tag_new = _new_cp->tag_at(cpi_new);
duke@0 207 if (tag_old.value() != tag_new.value())
duke@0 208 return false;
duke@0 209 if (tag_old.is_long()) {
duke@0 210 if (_old_cp->long_at(cpi_old) != _new_cp->long_at(cpi_new))
duke@0 211 return false;
duke@0 212 } else {
duke@0 213 if (_old_cp->double_at(cpi_old) != _new_cp->double_at(cpi_new))
duke@0 214 return false;
duke@0 215 }
duke@0 216 break;
duke@0 217 }
duke@0 218
duke@0 219 case Bytecodes::_bipush :
duke@0 220 if (_s_old->bcp()[1] != _s_new->bcp()[1])
duke@0 221 return false;
duke@0 222 break;
duke@0 223
duke@0 224 case Bytecodes::_sipush :
jrose@1485 225 if (_s_old->get_index_u2() != _s_new->get_index_u2())
duke@0 226 return false;
duke@0 227 break;
duke@0 228
duke@0 229 case Bytecodes::_aload : // fall through
duke@0 230 case Bytecodes::_astore : // fall through
duke@0 231 case Bytecodes::_dload : // fall through
duke@0 232 case Bytecodes::_dstore : // fall through
duke@0 233 case Bytecodes::_fload : // fall through
duke@0 234 case Bytecodes::_fstore : // fall through
duke@0 235 case Bytecodes::_iload : // fall through
duke@0 236 case Bytecodes::_istore : // fall through
duke@0 237 case Bytecodes::_lload : // fall through
duke@0 238 case Bytecodes::_lstore : // fall through
duke@0 239 case Bytecodes::_ret :
duke@0 240 if (_s_old->is_wide() != _s_new->is_wide())
duke@0 241 return false;
duke@0 242 if (_s_old->get_index() != _s_new->get_index())
duke@0 243 return false;
duke@0 244 break;
duke@0 245
duke@0 246 case Bytecodes::_goto : // fall through
duke@0 247 case Bytecodes::_if_acmpeq : // fall through
duke@0 248 case Bytecodes::_if_acmpne : // fall through
duke@0 249 case Bytecodes::_if_icmpeq : // fall through
duke@0 250 case Bytecodes::_if_icmpne : // fall through
duke@0 251 case Bytecodes::_if_icmplt : // fall through
duke@0 252 case Bytecodes::_if_icmpge : // fall through
duke@0 253 case Bytecodes::_if_icmpgt : // fall through
duke@0 254 case Bytecodes::_if_icmple : // fall through
duke@0 255 case Bytecodes::_ifeq : // fall through
duke@0 256 case Bytecodes::_ifne : // fall through
duke@0 257 case Bytecodes::_iflt : // fall through
duke@0 258 case Bytecodes::_ifge : // fall through
duke@0 259 case Bytecodes::_ifgt : // fall through
duke@0 260 case Bytecodes::_ifle : // fall through
duke@0 261 case Bytecodes::_ifnonnull : // fall through
duke@0 262 case Bytecodes::_ifnull : // fall through
duke@0 263 case Bytecodes::_jsr : {
jrose@1485 264 short old_ofs = (short) _s_old->get_index_u2();
jrose@1485 265 short new_ofs = (short) _s_new->get_index_u2();
duke@0 266 if (_switchable_test) {
duke@0 267 int old_dest = _s_old->bci() + old_ofs;
duke@0 268 int new_dest = _s_new->bci() + new_ofs;
duke@0 269 if (old_ofs < 0 && new_ofs < 0) {
duke@0 270 if (! _bci_map->old_and_new_locations_same(old_dest, new_dest))
duke@0 271 return false;
duke@0 272 } else if (old_ofs > 0 && new_ofs > 0) {
duke@0 273 _fwd_jmps->append(old_dest);
duke@0 274 _fwd_jmps->append(new_dest);
duke@0 275 } else {
duke@0 276 return false;
duke@0 277 }
duke@0 278 } else {
duke@0 279 if (old_ofs != new_ofs)
duke@0 280 return false;
duke@0 281 }
duke@0 282 break;
duke@0 283 }
duke@0 284
duke@0 285 case Bytecodes::_iinc :
duke@0 286 if (_s_old->is_wide() != _s_new->is_wide())
duke@0 287 return false;
duke@0 288 if (! _s_old->is_wide()) {
jrose@1485 289 // We could use get_index_u1 and get_constant_u1, but it's simpler to grab both bytes at once:
jrose@1485 290 if (Bytes::get_Java_u2(_s_old->bcp() + 1) != Bytes::get_Java_u2(_s_new->bcp() + 1))
duke@0 291 return false;
duke@0 292 } else {
jrose@1485 293 // We could use get_index_u2 and get_constant_u2, but it's simpler to grab all four bytes at once:
duke@0 294 if (Bytes::get_Java_u4(_s_old->bcp() + 1) != Bytes::get_Java_u4(_s_new->bcp() + 1))
duke@0 295 return false;
duke@0 296 }
duke@0 297 break;
duke@0 298
duke@0 299 case Bytecodes::_goto_w : // fall through
duke@0 300 case Bytecodes::_jsr_w : {
duke@0 301 int old_ofs = (int) Bytes::get_Java_u4(_s_old->bcp() + 1);
duke@0 302 int new_ofs = (int) Bytes::get_Java_u4(_s_new->bcp() + 1);
duke@0 303 if (_switchable_test) {
duke@0 304 int old_dest = _s_old->bci() + old_ofs;
duke@0 305 int new_dest = _s_new->bci() + new_ofs;
duke@0 306 if (old_ofs < 0 && new_ofs < 0) {
duke@0 307 if (! _bci_map->old_and_new_locations_same(old_dest, new_dest))
duke@0 308 return false;
duke@0 309 } else if (old_ofs > 0 && new_ofs > 0) {
duke@0 310 _fwd_jmps->append(old_dest);
duke@0 311 _fwd_jmps->append(new_dest);
duke@0 312 } else {
duke@0 313 return false;
duke@0 314 }
duke@0 315 } else {
duke@0 316 if (old_ofs != new_ofs)
duke@0 317 return false;
duke@0 318 }
duke@0 319 break;
duke@0 320 }
duke@0 321
duke@0 322 case Bytecodes::_lookupswitch : // fall through
duke@0 323 case Bytecodes::_tableswitch : {
duke@0 324 if (_switchable_test) {
duke@0 325 address aligned_bcp_old = (address) round_to((intptr_t)_s_old->bcp() + 1, jintSize);
duke@0 326 address aligned_bcp_new = (address) round_to((intptr_t)_s_new->bcp() + 1, jintSize);
duke@0 327 int default_old = (int) Bytes::get_Java_u4(aligned_bcp_old);
duke@0 328 int default_new = (int) Bytes::get_Java_u4(aligned_bcp_new);
duke@0 329 _fwd_jmps->append(_s_old->bci() + default_old);
duke@0 330 _fwd_jmps->append(_s_new->bci() + default_new);
duke@0 331 if (c_old == Bytecodes::_lookupswitch) {
duke@0 332 int npairs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize);
duke@0 333 int npairs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize);
duke@0 334 if (npairs_old != npairs_new)
duke@0 335 return false;
duke@0 336 for (int i = 0; i < npairs_old; i++) {
duke@0 337 int match_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i)*jintSize);
duke@0 338 int match_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i)*jintSize);
duke@0 339 if (match_old != match_new)
duke@0 340 return false;
duke@0 341 int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i+1)*jintSize);
duke@0 342 int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i+1)*jintSize);
duke@0 343 _fwd_jmps->append(_s_old->bci() + ofs_old);
duke@0 344 _fwd_jmps->append(_s_new->bci() + ofs_new);
duke@0 345 }
duke@0 346 } else if (c_old == Bytecodes::_tableswitch) {
duke@0 347 int lo_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize);
duke@0 348 int lo_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize);
duke@0 349 if (lo_old != lo_new)
duke@0 350 return false;
duke@0 351 int hi_old = (int) Bytes::get_Java_u4(aligned_bcp_old + 2*jintSize);
duke@0 352 int hi_new = (int) Bytes::get_Java_u4(aligned_bcp_new + 2*jintSize);
duke@0 353 if (hi_old != hi_new)
duke@0 354 return false;
duke@0 355 for (int i = 0; i < hi_old - lo_old + 1; i++) {
duke@0 356 int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (3+i)*jintSize);
duke@0 357 int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (3+i)*jintSize);
duke@0 358 _fwd_jmps->append(_s_old->bci() + ofs_old);
duke@0 359 _fwd_jmps->append(_s_new->bci() + ofs_new);
duke@0 360 }
duke@0 361 }
duke@0 362 } else { // !_switchable_test, can use fast rough compare
jrose@1485 363 int len_old = _s_old->instruction_size();
jrose@1485 364 int len_new = _s_new->instruction_size();
duke@0 365 if (len_old != len_new)
duke@0 366 return false;
duke@0 367 if (memcmp(_s_old->bcp(), _s_new->bcp(), len_old) != 0)
duke@0 368 return false;
duke@0 369 }
duke@0 370 break;
duke@0 371 }
duke@0 372 }
duke@0 373
duke@0 374 return true;
duke@0 375 }
duke@0 376
duke@0 377
duke@0 378 int MethodComparator::check_stack_and_locals_size(methodOop old_method, methodOop new_method) {
duke@0 379 if (old_method->max_stack() != new_method->max_stack()) {
duke@0 380 return 1;
duke@0 381 } else if (old_method->max_locals() != new_method->max_locals()) {
duke@0 382 return 2;
duke@0 383 } else if (old_method->size_of_parameters() != new_method->size_of_parameters()) {
duke@0 384 return 3;
duke@0 385 } else return 0;
duke@0 386 }