duke@435: /* duke@435: * Copyright 1998-2005 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_rewriter.cpp.incl" duke@435: duke@435: duke@435: // Computes an index_map (new_index -> original_index) for contant pool entries duke@435: // that are referred to by the interpreter at runtime via the constant pool cache. duke@435: void Rewriter::compute_index_maps(constantPoolHandle pool, intArray*& index_map, intStack*& inverse_index_map) { duke@435: const int length = pool->length(); duke@435: index_map = new intArray(length, -1); duke@435: // Choose an initial value large enough that we don't get frequent duke@435: // calls to grow(). duke@435: inverse_index_map = new intStack(length / 2); duke@435: for (int i = 0; i < length; i++) { duke@435: switch (pool->tag_at(i).value()) { duke@435: case JVM_CONSTANT_Fieldref : // fall through duke@435: case JVM_CONSTANT_Methodref : // fall through duke@435: case JVM_CONSTANT_InterfaceMethodref: { duke@435: index_map->at_put(i, inverse_index_map->length()); duke@435: inverse_index_map->append(i); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // Creates a constant pool cache given an inverse_index_map duke@435: constantPoolCacheHandle Rewriter::new_constant_pool_cache(intArray& inverse_index_map, TRAPS) { duke@435: const int length = inverse_index_map.length(); duke@435: constantPoolCacheOop cache = oopFactory::new_constantPoolCache(length, CHECK_(constantPoolCacheHandle())); duke@435: cache->initialize(inverse_index_map); duke@435: return constantPoolCacheHandle(THREAD, cache); duke@435: } duke@435: duke@435: duke@435: duke@435: // The new finalization semantics says that registration of duke@435: // finalizable objects must be performed on successful return from the duke@435: // Object. constructor. We could implement this trivially if duke@435: // were never rewritten but since JVMTI allows this to occur, a duke@435: // more complicated solution is required. A special return bytecode duke@435: // is used only by Object. to signal the finalization duke@435: // registration point. Additionally local 0 must be preserved so it's duke@435: // available to pass to the registration function. For simplicty we duke@435: // require that local 0 is never overwritten so it's available as an duke@435: // argument for registration. duke@435: duke@435: void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) { duke@435: RawBytecodeStream bcs(method); duke@435: while (!bcs.is_last_bytecode()) { duke@435: Bytecodes::Code opcode = bcs.raw_next(); duke@435: switch (opcode) { duke@435: case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break; duke@435: duke@435: case Bytecodes::_istore: duke@435: case Bytecodes::_lstore: duke@435: case Bytecodes::_fstore: duke@435: case Bytecodes::_dstore: duke@435: case Bytecodes::_astore: duke@435: if (bcs.get_index() != 0) continue; duke@435: duke@435: // fall through duke@435: case Bytecodes::_istore_0: duke@435: case Bytecodes::_lstore_0: duke@435: case Bytecodes::_fstore_0: duke@435: case Bytecodes::_dstore_0: duke@435: case Bytecodes::_astore_0: duke@435: THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), duke@435: "can't overwrite local 0 in Object."); duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // Rewrites a method given the index_map information duke@435: methodHandle Rewriter::rewrite_method(methodHandle method, intArray& index_map, TRAPS) { duke@435: duke@435: int nof_jsrs = 0; duke@435: bool has_monitor_bytecodes = false; duke@435: duke@435: { duke@435: // We cannot tolerate a GC in this block, because we've duke@435: // cached the bytecodes in 'code_base'. If the methodOop duke@435: // moves, the bytecodes will also move. duke@435: No_Safepoint_Verifier nsv; duke@435: Bytecodes::Code c; duke@435: duke@435: // Bytecodes and their length duke@435: const address code_base = method->code_base(); duke@435: const int code_length = method->code_size(); duke@435: duke@435: int bc_length; duke@435: for (int bci = 0; bci < code_length; bci += bc_length) { duke@435: address bcp = code_base + bci; duke@435: c = (Bytecodes::Code)(*bcp); duke@435: duke@435: // Since we have the code, see if we can get the length duke@435: // directly. Some more complicated bytecodes will report duke@435: // a length of zero, meaning we need to make another method duke@435: // call to calculate the length. duke@435: bc_length = Bytecodes::length_for(c); duke@435: if (bc_length == 0) { duke@435: bc_length = Bytecodes::length_at(bcp); duke@435: duke@435: // length_at will put us at the bytecode after the one modified duke@435: // by 'wide'. We don't currently examine any of the bytecodes duke@435: // modified by wide, but in case we do in the future... duke@435: if (c == Bytecodes::_wide) { duke@435: c = (Bytecodes::Code)bcp[1]; duke@435: } duke@435: } duke@435: duke@435: assert(bc_length != 0, "impossible bytecode length"); duke@435: duke@435: switch (c) { duke@435: case Bytecodes::_lookupswitch : { duke@435: #ifndef CC_INTERP duke@435: Bytecode_lookupswitch* bc = Bytecode_lookupswitch_at(bcp); duke@435: bc->set_code( duke@435: bc->number_of_pairs() < BinarySwitchThreshold duke@435: ? Bytecodes::_fast_linearswitch duke@435: : Bytecodes::_fast_binaryswitch duke@435: ); duke@435: #endif duke@435: break; 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: { duke@435: address p = bcp + 1; duke@435: Bytes::put_native_u2(p, index_map[Bytes::get_Java_u2(p)]); duke@435: break; duke@435: } duke@435: case Bytecodes::_jsr : // fall through duke@435: case Bytecodes::_jsr_w : nof_jsrs++; break; duke@435: case Bytecodes::_monitorenter : // fall through duke@435: case Bytecodes::_monitorexit : has_monitor_bytecodes = true; break; duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Update access flags duke@435: if (has_monitor_bytecodes) { duke@435: method->set_has_monitor_bytecodes(); duke@435: } duke@435: duke@435: // The present of a jsr bytecode implies that the method might potentially duke@435: // have to be rewritten, so we run the oopMapGenerator on the method duke@435: if (nof_jsrs > 0) { duke@435: method->set_has_jsrs(); duke@435: ResolveOopMapConflicts romc(method); duke@435: methodHandle original_method = method; duke@435: method = romc.do_potential_rewrite(CHECK_(methodHandle())); duke@435: if (method() != original_method()) { duke@435: // Insert invalid bytecode into original methodOop and set duke@435: // interpreter entrypoint, so that a executing this method duke@435: // will manifest itself in an easy recognizable form. duke@435: address bcp = original_method->bcp_from(0); duke@435: *bcp = (u1)Bytecodes::_shouldnotreachhere; duke@435: int kind = Interpreter::method_kind(original_method); duke@435: original_method->set_interpreter_kind(kind); duke@435: } duke@435: duke@435: // Update monitor matching info. duke@435: if (romc.monitor_safe()) { duke@435: method->set_guaranteed_monitor_matching(); duke@435: } duke@435: } duke@435: duke@435: // Setup method entrypoints for compiler and interpreter duke@435: method->link_method(method, CHECK_(methodHandle())); duke@435: duke@435: return method; duke@435: } duke@435: duke@435: duke@435: void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) { duke@435: // gather starting points duke@435: ResourceMark rm(THREAD); duke@435: constantPoolHandle pool (THREAD, klass->constants()); duke@435: objArrayHandle methods (THREAD, klass->methods()); duke@435: assert(pool->cache() == NULL, "constant pool cache must not be set yet"); duke@435: duke@435: // determine index maps for methodOop rewriting duke@435: intArray* index_map = NULL; duke@435: intStack* inverse_index_map = NULL; duke@435: compute_index_maps(pool, index_map, inverse_index_map); duke@435: duke@435: // allocate constant pool cache duke@435: constantPoolCacheHandle cache = new_constant_pool_cache(*inverse_index_map, CHECK); duke@435: pool->set_cache(cache()); duke@435: cache->set_constant_pool(pool()); duke@435: duke@435: if (RegisterFinalizersAtInit && klass->name() == vmSymbols::java_lang_Object()) { duke@435: int i = methods->length(); duke@435: while (i-- > 0) { duke@435: methodOop method = (methodOop)methods->obj_at(i); duke@435: if (method->intrinsic_id() == vmIntrinsics::_Object_init) { duke@435: // rewrite the return bytecodes of Object. to register the duke@435: // object for finalization if needed. duke@435: methodHandle m(THREAD, method); duke@435: rewrite_Object_init(m, CHECK); duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: duke@435: // rewrite methods duke@435: { int i = methods->length(); duke@435: while (i-- > 0) { duke@435: methodHandle m(THREAD, (methodOop)methods->obj_at(i)); duke@435: m = rewrite_method(m, *index_map, CHECK); duke@435: // Method might have gotten rewritten. duke@435: methods->obj_at_put(i, m()); duke@435: } duke@435: } duke@435: }