duke@435: /* trims@1907: * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_methodComparator.cpp.incl" duke@435: duke@435: BytecodeStream *MethodComparator::_s_old; duke@435: BytecodeStream *MethodComparator::_s_new; duke@435: constantPoolOop MethodComparator::_old_cp; duke@435: constantPoolOop MethodComparator::_new_cp; duke@435: BciMap *MethodComparator::_bci_map; duke@435: bool MethodComparator::_switchable_test; duke@435: GrowableArray *MethodComparator::_fwd_jmps; duke@435: duke@435: bool MethodComparator::methods_EMCP(methodOop old_method, methodOop new_method) { duke@435: if (old_method->code_size() != new_method->code_size()) duke@435: return false; duke@435: if (check_stack_and_locals_size(old_method, new_method) != 0) { duke@435: // RC_TRACE macro has an embedded ResourceMark duke@435: RC_TRACE(0x00800000, ("Methods %s non-comparable with diagnosis %d", duke@435: old_method->name()->as_C_string(), duke@435: check_stack_and_locals_size(old_method, new_method))); duke@435: return false; duke@435: } duke@435: duke@435: _old_cp = old_method->constants(); duke@435: _new_cp = new_method->constants(); duke@435: BytecodeStream s_old(old_method); duke@435: BytecodeStream s_new(new_method); duke@435: _s_old = &s_old; duke@435: _s_new = &s_new; duke@435: _switchable_test = false; duke@435: Bytecodes::Code c_old, c_new; duke@435: duke@435: while ((c_old = s_old.next()) >= 0) { duke@435: if ((c_new = s_new.next()) < 0 || c_old != c_new) duke@435: return false; duke@435: duke@435: if (! args_same(c_old, c_new)) duke@435: return false; duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: duke@435: bool MethodComparator::methods_switchable(methodOop old_method, methodOop new_method, duke@435: BciMap &bci_map) { duke@435: if (old_method->code_size() > new_method->code_size()) duke@435: // Something has definitely been deleted in the new method, compared to the old one. duke@435: return false; duke@435: duke@435: if (! check_stack_and_locals_size(old_method, new_method)) duke@435: return false; duke@435: duke@435: _old_cp = old_method->constants(); duke@435: _new_cp = new_method->constants(); duke@435: BytecodeStream s_old(old_method); duke@435: BytecodeStream s_new(new_method); duke@435: _s_old = &s_old; duke@435: _s_new = &s_new; duke@435: _bci_map = &bci_map; duke@435: _switchable_test = true; duke@435: GrowableArray fwd_jmps(16); duke@435: _fwd_jmps = &fwd_jmps; duke@435: Bytecodes::Code c_old, c_new; duke@435: duke@435: while ((c_old = s_old.next()) >= 0) { duke@435: if ((c_new = s_new.next()) < 0) duke@435: return false; duke@435: if (! (c_old == c_new && args_same(c_old, c_new))) { duke@435: int old_bci = s_old.bci(); duke@435: int new_st_bci = s_new.bci(); duke@435: bool found_match = false; duke@435: do { duke@435: c_new = s_new.next(); duke@435: if (c_new == c_old && args_same(c_old, c_new)) { duke@435: found_match = true; duke@435: break; duke@435: } duke@435: } while (c_new >= 0); duke@435: if (! found_match) duke@435: return false; duke@435: int new_end_bci = s_new.bci(); duke@435: bci_map.store_fragment_location(old_bci, new_st_bci, new_end_bci); duke@435: } duke@435: } duke@435: duke@435: // Now we can test all forward jumps duke@435: for (int i = 0; i < fwd_jmps.length() / 2; i++) { duke@435: if (! bci_map.old_and_new_locations_same(fwd_jmps.at(i*2), fwd_jmps.at(i*2+1))) { duke@435: RC_TRACE(0x00800000, duke@435: ("Fwd jump miss: old dest = %d, calc new dest = %d, act new dest = %d", duke@435: fwd_jmps.at(i*2), bci_map.new_bci_for_old(fwd_jmps.at(i*2)), duke@435: fwd_jmps.at(i*2+1))); duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: return true; duke@435: } duke@435: duke@435: duke@435: bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) { duke@435: // BytecodeStream returns the correct standard Java bytecodes for various "fast" duke@435: // bytecode versions, so we don't have to bother about them here.. duke@435: switch (c_old) { duke@435: case Bytecodes::_new : // fall through duke@435: case Bytecodes::_anewarray : // fall through duke@435: case Bytecodes::_multianewarray : // fall through duke@435: case Bytecodes::_checkcast : // fall through duke@435: case Bytecodes::_instanceof : { duke@435: u2 cpi_old = _s_old->get_index_big(); duke@435: u2 cpi_new = _s_new->get_index_big(); duke@435: if ((_old_cp->klass_at_noresolve(cpi_old) != _new_cp->klass_at_noresolve(cpi_new))) duke@435: return false; duke@435: if (c_old == Bytecodes::_multianewarray && duke@435: *(jbyte*)(_s_old->bcp() + 3) != *(jbyte*)(_s_new->bcp() + 3)) duke@435: return false; duke@435: break; duke@435: } duke@435: duke@435: case Bytecodes::_getstatic : // fall through duke@435: case Bytecodes::_putstatic : // fall through duke@435: case Bytecodes::_getfield : // fall through duke@435: case Bytecodes::_putfield : // fall through duke@435: case Bytecodes::_invokevirtual : // fall through duke@435: case Bytecodes::_invokespecial : // fall through duke@435: case Bytecodes::_invokestatic : // fall through duke@435: case Bytecodes::_invokeinterface : { jrose@1161: u2 cpci_old = _s_old->get_index_int(); jrose@1161: u2 cpci_new = _s_new->get_index_int(); duke@435: // Check if the names of classes, field/method names and signatures at these indexes duke@435: // are the same. Indices which are really into constantpool cache (rather than constant duke@435: // pool itself) are accepted by the constantpool query routines below. duke@435: if ((_old_cp->klass_ref_at_noresolve(cpci_old) != _new_cp->klass_ref_at_noresolve(cpci_new)) || duke@435: (_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) || duke@435: (_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new))) duke@435: return false; duke@435: break; duke@435: } duke@435: duke@435: case Bytecodes::_ldc : // fall through duke@435: case Bytecodes::_ldc_w : { duke@435: u2 cpi_old, cpi_new; duke@435: if (c_old == Bytecodes::_ldc) { duke@435: cpi_old = _s_old->bcp()[1]; duke@435: cpi_new = _s_new->bcp()[1]; duke@435: } else { duke@435: cpi_old = _s_old->get_index_big(); duke@435: cpi_new = _s_new->get_index_big(); duke@435: } duke@435: constantTag tag_old = _old_cp->tag_at(cpi_old); duke@435: constantTag tag_new = _new_cp->tag_at(cpi_new); duke@435: if (tag_old.is_int() || tag_old.is_float()) { duke@435: if (tag_old.value() != tag_new.value()) duke@435: return false; duke@435: if (tag_old.is_int()) { duke@435: if (_old_cp->int_at(cpi_old) != _new_cp->int_at(cpi_new)) duke@435: return false; duke@435: } else { duke@435: if (_old_cp->float_at(cpi_old) != _new_cp->float_at(cpi_new)) duke@435: return false; duke@435: } duke@435: } else if (tag_old.is_string() || tag_old.is_unresolved_string()) { duke@435: if (! (tag_new.is_unresolved_string() || tag_new.is_string())) duke@435: return false; duke@435: if (strcmp(_old_cp->string_at_noresolve(cpi_old), duke@435: _new_cp->string_at_noresolve(cpi_new)) != 0) duke@435: return false; duke@435: } else { // tag_old should be klass - 4881222 duke@435: if (! (tag_new.is_unresolved_klass() || tag_new.is_klass())) duke@435: return false; duke@435: if (_old_cp->klass_at_noresolve(cpi_old) != duke@435: _new_cp->klass_at_noresolve(cpi_new)) duke@435: return false; duke@435: } duke@435: break; duke@435: } duke@435: duke@435: case Bytecodes::_ldc2_w : { duke@435: u2 cpi_old = _s_old->get_index_big(); duke@435: u2 cpi_new = _s_new->get_index_big(); duke@435: constantTag tag_old = _old_cp->tag_at(cpi_old); duke@435: constantTag tag_new = _new_cp->tag_at(cpi_new); duke@435: if (tag_old.value() != tag_new.value()) duke@435: return false; duke@435: if (tag_old.is_long()) { duke@435: if (_old_cp->long_at(cpi_old) != _new_cp->long_at(cpi_new)) duke@435: return false; duke@435: } else { duke@435: if (_old_cp->double_at(cpi_old) != _new_cp->double_at(cpi_new)) duke@435: return false; duke@435: } duke@435: break; duke@435: } duke@435: duke@435: case Bytecodes::_bipush : duke@435: if (_s_old->bcp()[1] != _s_new->bcp()[1]) duke@435: return false; duke@435: break; duke@435: duke@435: case Bytecodes::_sipush : duke@435: if (_s_old->get_index_big() != _s_new->get_index_big()) duke@435: return false; duke@435: break; duke@435: duke@435: case Bytecodes::_aload : // fall through duke@435: case Bytecodes::_astore : // fall through duke@435: case Bytecodes::_dload : // fall through duke@435: case Bytecodes::_dstore : // fall through duke@435: case Bytecodes::_fload : // fall through duke@435: case Bytecodes::_fstore : // fall through duke@435: case Bytecodes::_iload : // fall through duke@435: case Bytecodes::_istore : // fall through duke@435: case Bytecodes::_lload : // fall through duke@435: case Bytecodes::_lstore : // fall through duke@435: case Bytecodes::_ret : duke@435: if (_s_old->is_wide() != _s_new->is_wide()) duke@435: return false; duke@435: if (_s_old->get_index() != _s_new->get_index()) duke@435: return false; duke@435: break; duke@435: duke@435: case Bytecodes::_goto : // fall through duke@435: case Bytecodes::_if_acmpeq : // fall through duke@435: case Bytecodes::_if_acmpne : // fall through duke@435: case Bytecodes::_if_icmpeq : // fall through duke@435: case Bytecodes::_if_icmpne : // fall through duke@435: case Bytecodes::_if_icmplt : // fall through duke@435: case Bytecodes::_if_icmpge : // fall through duke@435: case Bytecodes::_if_icmpgt : // fall through duke@435: case Bytecodes::_if_icmple : // fall through duke@435: case Bytecodes::_ifeq : // fall through duke@435: case Bytecodes::_ifne : // fall through duke@435: case Bytecodes::_iflt : // fall through duke@435: case Bytecodes::_ifge : // fall through duke@435: case Bytecodes::_ifgt : // fall through duke@435: case Bytecodes::_ifle : // fall through duke@435: case Bytecodes::_ifnonnull : // fall through duke@435: case Bytecodes::_ifnull : // fall through duke@435: case Bytecodes::_jsr : { duke@435: short old_ofs = (short) _s_old->get_index_big(); duke@435: short new_ofs = (short) _s_new->get_index_big(); duke@435: if (_switchable_test) { duke@435: int old_dest = _s_old->bci() + old_ofs; duke@435: int new_dest = _s_new->bci() + new_ofs; duke@435: if (old_ofs < 0 && new_ofs < 0) { duke@435: if (! _bci_map->old_and_new_locations_same(old_dest, new_dest)) duke@435: return false; duke@435: } else if (old_ofs > 0 && new_ofs > 0) { duke@435: _fwd_jmps->append(old_dest); duke@435: _fwd_jmps->append(new_dest); duke@435: } else { duke@435: return false; duke@435: } duke@435: } else { duke@435: if (old_ofs != new_ofs) duke@435: return false; duke@435: } duke@435: break; duke@435: } duke@435: duke@435: case Bytecodes::_iinc : duke@435: if (_s_old->is_wide() != _s_new->is_wide()) duke@435: return false; duke@435: if (! _s_old->is_wide()) { duke@435: if (_s_old->get_index_big() != _s_new->get_index_big()) duke@435: return false; duke@435: } else { duke@435: if (Bytes::get_Java_u4(_s_old->bcp() + 1) != Bytes::get_Java_u4(_s_new->bcp() + 1)) duke@435: return false; duke@435: } duke@435: break; duke@435: duke@435: case Bytecodes::_goto_w : // fall through duke@435: case Bytecodes::_jsr_w : { duke@435: int old_ofs = (int) Bytes::get_Java_u4(_s_old->bcp() + 1); duke@435: int new_ofs = (int) Bytes::get_Java_u4(_s_new->bcp() + 1); duke@435: if (_switchable_test) { duke@435: int old_dest = _s_old->bci() + old_ofs; duke@435: int new_dest = _s_new->bci() + new_ofs; duke@435: if (old_ofs < 0 && new_ofs < 0) { duke@435: if (! _bci_map->old_and_new_locations_same(old_dest, new_dest)) duke@435: return false; duke@435: } else if (old_ofs > 0 && new_ofs > 0) { duke@435: _fwd_jmps->append(old_dest); duke@435: _fwd_jmps->append(new_dest); duke@435: } else { duke@435: return false; duke@435: } duke@435: } else { duke@435: if (old_ofs != new_ofs) duke@435: return false; duke@435: } duke@435: break; duke@435: } duke@435: duke@435: case Bytecodes::_lookupswitch : // fall through duke@435: case Bytecodes::_tableswitch : { duke@435: if (_switchable_test) { duke@435: address aligned_bcp_old = (address) round_to((intptr_t)_s_old->bcp() + 1, jintSize); duke@435: address aligned_bcp_new = (address) round_to((intptr_t)_s_new->bcp() + 1, jintSize); duke@435: int default_old = (int) Bytes::get_Java_u4(aligned_bcp_old); duke@435: int default_new = (int) Bytes::get_Java_u4(aligned_bcp_new); duke@435: _fwd_jmps->append(_s_old->bci() + default_old); duke@435: _fwd_jmps->append(_s_new->bci() + default_new); duke@435: if (c_old == Bytecodes::_lookupswitch) { duke@435: int npairs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize); duke@435: int npairs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize); duke@435: if (npairs_old != npairs_new) duke@435: return false; duke@435: for (int i = 0; i < npairs_old; i++) { duke@435: int match_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i)*jintSize); duke@435: int match_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i)*jintSize); duke@435: if (match_old != match_new) duke@435: return false; duke@435: int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (2+2*i+1)*jintSize); duke@435: int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (2+2*i+1)*jintSize); duke@435: _fwd_jmps->append(_s_old->bci() + ofs_old); duke@435: _fwd_jmps->append(_s_new->bci() + ofs_new); duke@435: } duke@435: } else if (c_old == Bytecodes::_tableswitch) { duke@435: int lo_old = (int) Bytes::get_Java_u4(aligned_bcp_old + jintSize); duke@435: int lo_new = (int) Bytes::get_Java_u4(aligned_bcp_new + jintSize); duke@435: if (lo_old != lo_new) duke@435: return false; duke@435: int hi_old = (int) Bytes::get_Java_u4(aligned_bcp_old + 2*jintSize); duke@435: int hi_new = (int) Bytes::get_Java_u4(aligned_bcp_new + 2*jintSize); duke@435: if (hi_old != hi_new) duke@435: return false; duke@435: for (int i = 0; i < hi_old - lo_old + 1; i++) { duke@435: int ofs_old = (int) Bytes::get_Java_u4(aligned_bcp_old + (3+i)*jintSize); duke@435: int ofs_new = (int) Bytes::get_Java_u4(aligned_bcp_new + (3+i)*jintSize); duke@435: _fwd_jmps->append(_s_old->bci() + ofs_old); duke@435: _fwd_jmps->append(_s_new->bci() + ofs_new); duke@435: } duke@435: } duke@435: } else { // !_switchable_test, can use fast rough compare duke@435: int len_old = _s_old->next_bcp() - _s_old->bcp(); duke@435: int len_new = _s_new->next_bcp() - _s_new->bcp(); duke@435: if (len_old != len_new) duke@435: return false; duke@435: if (memcmp(_s_old->bcp(), _s_new->bcp(), len_old) != 0) duke@435: return false; duke@435: } duke@435: break; duke@435: } duke@435: } duke@435: duke@435: return true; duke@435: } duke@435: duke@435: duke@435: int MethodComparator::check_stack_and_locals_size(methodOop old_method, methodOop new_method) { duke@435: if (old_method->max_stack() != new_method->max_stack()) { duke@435: return 1; duke@435: } else if (old_method->max_locals() != new_method->max_locals()) { duke@435: return 2; duke@435: } else if (old_method->size_of_parameters() != new_method->size_of_parameters()) { duke@435: return 3; duke@435: } else return 0; duke@435: }