rewriter.cpp revision 726
0N/A/*
579N/A * Copyright 1998-2009 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A *
0N/A */
0N/A
0N/A# include "incls/_precompiled.incl"
0N/A# include "incls/_rewriter.cpp.incl"
0N/A
726N/A// Computes a CPC map (new_index -> original_index) for constant pool entries
0N/A// that are referred to by the interpreter at runtime via the constant pool cache.
726N/A// Also computes a CP map (original_index -> new_index).
726N/A// Marks entries in CP which require additional processing.
726N/Avoid Rewriter::compute_index_maps() {
726N/A const int length = _pool->length();
726N/A init_cp_map(length);
0N/A for (int i = 0; i < length; i++) {
726N/A int tag = _pool->tag_at(i).value();
726N/A switch (tag) {
726N/A case JVM_CONSTANT_InterfaceMethodref:
0N/A case JVM_CONSTANT_Fieldref : // fall through
0N/A case JVM_CONSTANT_Methodref : // fall through
726N/A add_cp_cache_entry(i);
726N/A break;
0N/A }
0N/A }
726N/A
726N/A guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1),
726N/A "all cp cache indexes fit in a u2");
0N/A}
0N/A
0N/A
726N/Aint Rewriter::add_extra_cp_cache_entry(int main_entry) {
726N/A // Hack: We put it on the map as an encoded value.
726N/A // The only place that consumes this is ConstantPoolCacheEntry::set_initial_state
726N/A int encoded = constantPoolCacheOopDesc::encode_secondary_index(main_entry);
726N/A int plain_secondary_index = _cp_cache_map.append(encoded);
726N/A return constantPoolCacheOopDesc::encode_secondary_index(plain_secondary_index);
726N/A}
726N/A
726N/A
726N/A
726N/A// Creates a constant pool cache given a CPC map
542N/A// This creates the constant pool cache initially in a state
542N/A// that is unsafe for concurrent GC processing but sets it to
542N/A// a safe mode before the constant pool cache is returned.
726N/Avoid Rewriter::make_constant_pool_cache(TRAPS) {
726N/A const int length = _cp_cache_map.length();
726N/A constantPoolCacheOop cache =
726N/A oopFactory::new_constantPoolCache(length, methodOopDesc::IsUnsafeConc, CHECK);
726N/A cache->initialize(_cp_cache_map);
726N/A _pool->set_cache(cache);
726N/A cache->set_constant_pool(_pool());
0N/A}
0N/A
0N/A
0N/A
0N/A// The new finalization semantics says that registration of
0N/A// finalizable objects must be performed on successful return from the
0N/A// Object.<init> constructor. We could implement this trivially if
0N/A// <init> were never rewritten but since JVMTI allows this to occur, a
0N/A// more complicated solution is required. A special return bytecode
0N/A// is used only by Object.<init> to signal the finalization
0N/A// registration point. Additionally local 0 must be preserved so it's
0N/A// available to pass to the registration function. For simplicty we
0N/A// require that local 0 is never overwritten so it's available as an
0N/A// argument for registration.
0N/A
0N/Avoid Rewriter::rewrite_Object_init(methodHandle method, TRAPS) {
0N/A RawBytecodeStream bcs(method);
0N/A while (!bcs.is_last_bytecode()) {
0N/A Bytecodes::Code opcode = bcs.raw_next();
0N/A switch (opcode) {
0N/A case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break;
0N/A
0N/A case Bytecodes::_istore:
0N/A case Bytecodes::_lstore:
0N/A case Bytecodes::_fstore:
0N/A case Bytecodes::_dstore:
0N/A case Bytecodes::_astore:
0N/A if (bcs.get_index() != 0) continue;
0N/A
0N/A // fall through
0N/A case Bytecodes::_istore_0:
0N/A case Bytecodes::_lstore_0:
0N/A case Bytecodes::_fstore_0:
0N/A case Bytecodes::_dstore_0:
0N/A case Bytecodes::_astore_0:
0N/A THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
0N/A "can't overwrite local 0 in Object.<init>");
0N/A break;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
726N/A// Rewrite a classfile-order CP index into a native-order CPC index.
726N/Aint Rewriter::rewrite_member_reference(address bcp, int offset) {
726N/A address p = bcp + offset;
726N/A int cp_index = Bytes::get_Java_u2(p);
726N/A int cache_index = cp_entry_to_cp_cache(cp_index);
726N/A Bytes::put_native_u2(p, cache_index);
726N/A return cp_index;
726N/A}
726N/A
726N/A
726N/Avoid Rewriter::rewrite_invokedynamic(address bcp, int offset, int delete_me) {
726N/A address p = bcp + offset;
726N/A assert(p[-1] == Bytecodes::_invokedynamic, "");
726N/A int cp_index = Bytes::get_Java_u2(p);
726N/A int cpc = maybe_add_cp_cache_entry(cp_index); // add lazily
726N/A int cpc2 = add_extra_cp_cache_entry(cpc);
726N/A
726N/A // Replace the trailing four bytes with a CPC index for the dynamic
726N/A // call site. Unlike other CPC entries, there is one per bytecode,
726N/A // not just one per distinct CP entry. In other words, the
726N/A // CPC-to-CP relation is many-to-one for invokedynamic entries.
726N/A // This means we must use a larger index size than u2 to address
726N/A // all these entries. That is the main reason invokedynamic
726N/A // must have a five-byte instruction format. (Of course, other JVM
726N/A // implementations can use the bytes for other purposes.)
726N/A Bytes::put_native_u4(p, cpc2);
726N/A // Note: We use native_u4 format exclusively for 4-byte indexes.
726N/A}
726N/A
726N/A
0N/A// Rewrites a method given the index_map information
726N/Avoid Rewriter::scan_method(methodOop method) {
0N/A
0N/A int nof_jsrs = 0;
0N/A bool has_monitor_bytecodes = false;
0N/A
0N/A {
0N/A // We cannot tolerate a GC in this block, because we've
0N/A // cached the bytecodes in 'code_base'. If the methodOop
0N/A // moves, the bytecodes will also move.
0N/A No_Safepoint_Verifier nsv;
0N/A Bytecodes::Code c;
0N/A
0N/A // Bytecodes and their length
0N/A const address code_base = method->code_base();
0N/A const int code_length = method->code_size();
0N/A
0N/A int bc_length;
0N/A for (int bci = 0; bci < code_length; bci += bc_length) {
0N/A address bcp = code_base + bci;
726N/A int prefix_length = 0;
0N/A c = (Bytecodes::Code)(*bcp);
0N/A
0N/A // Since we have the code, see if we can get the length
0N/A // directly. Some more complicated bytecodes will report
0N/A // a length of zero, meaning we need to make another method
0N/A // call to calculate the length.
0N/A bc_length = Bytecodes::length_for(c);
0N/A if (bc_length == 0) {
0N/A bc_length = Bytecodes::length_at(bcp);
0N/A
0N/A // length_at will put us at the bytecode after the one modified
0N/A // by 'wide'. We don't currently examine any of the bytecodes
0N/A // modified by wide, but in case we do in the future...
0N/A if (c == Bytecodes::_wide) {
726N/A prefix_length = 1;
0N/A c = (Bytecodes::Code)bcp[1];
0N/A }
0N/A }
0N/A
0N/A assert(bc_length != 0, "impossible bytecode length");
0N/A
0N/A switch (c) {
0N/A case Bytecodes::_lookupswitch : {
0N/A#ifndef CC_INTERP
0N/A Bytecode_lookupswitch* bc = Bytecode_lookupswitch_at(bcp);
0N/A bc->set_code(
0N/A bc->number_of_pairs() < BinarySwitchThreshold
0N/A ? Bytecodes::_fast_linearswitch
0N/A : Bytecodes::_fast_binaryswitch
0N/A );
0N/A#endif
0N/A break;
0N/A }
0N/A case Bytecodes::_getstatic : // fall through
0N/A case Bytecodes::_putstatic : // fall through
0N/A case Bytecodes::_getfield : // fall through
0N/A case Bytecodes::_putfield : // fall through
0N/A case Bytecodes::_invokevirtual : // fall through
0N/A case Bytecodes::_invokespecial : // fall through
726N/A case Bytecodes::_invokestatic :
726N/A case Bytecodes::_invokeinterface:
726N/A rewrite_member_reference(bcp, prefix_length+1);
0N/A break;
726N/A case Bytecodes::_invokedynamic:
726N/A rewrite_invokedynamic(bcp, prefix_length+1, int(sizeof"@@@@DELETE ME"));
726N/A break;
0N/A case Bytecodes::_jsr : // fall through
0N/A case Bytecodes::_jsr_w : nof_jsrs++; break;
0N/A case Bytecodes::_monitorenter : // fall through
0N/A case Bytecodes::_monitorexit : has_monitor_bytecodes = true; break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Update access flags
0N/A if (has_monitor_bytecodes) {
0N/A method->set_has_monitor_bytecodes();
0N/A }
0N/A
0N/A // The present of a jsr bytecode implies that the method might potentially
0N/A // have to be rewritten, so we run the oopMapGenerator on the method
0N/A if (nof_jsrs > 0) {
0N/A method->set_has_jsrs();
726N/A // Second pass will revisit this method.
726N/A assert(method->has_jsrs(), "");
726N/A }
726N/A}
0N/A
726N/A// After constant pool is created, revisit methods containing jsrs.
726N/AmethodHandle Rewriter::rewrite_jsrs(methodHandle method, TRAPS) {
726N/A ResolveOopMapConflicts romc(method);
726N/A methodHandle original_method = method;
726N/A method = romc.do_potential_rewrite(CHECK_(methodHandle()));
726N/A if (method() != original_method()) {
726N/A // Insert invalid bytecode into original methodOop and set
726N/A // interpreter entrypoint, so that a executing this method
726N/A // will manifest itself in an easy recognizable form.
726N/A address bcp = original_method->bcp_from(0);
726N/A *bcp = (u1)Bytecodes::_shouldnotreachhere;
726N/A int kind = Interpreter::method_kind(original_method);
726N/A original_method->set_interpreter_kind(kind);
0N/A }
0N/A
726N/A // Update monitor matching info.
726N/A if (romc.monitor_safe()) {
726N/A method->set_guaranteed_monitor_matching();
726N/A }
0N/A
0N/A return method;
0N/A}
0N/A
0N/A
0N/Avoid Rewriter::rewrite(instanceKlassHandle klass, TRAPS) {
0N/A ResourceMark rm(THREAD);
726N/A Rewriter rw(klass, CHECK);
726N/A // (That's all, folks.)
726N/A}
726N/A
726N/ARewriter::Rewriter(instanceKlassHandle klass, TRAPS)
726N/A : _klass(klass),
726N/A // gather starting points
726N/A _pool( THREAD, klass->constants()),
726N/A _methods(THREAD, klass->methods())
726N/A{
726N/A assert(_pool->cache() == NULL, "constant pool cache must not be set yet");
0N/A
0N/A // determine index maps for methodOop rewriting
726N/A compute_index_maps();
0N/A
726N/A if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) {
726N/A int i = _methods->length();
0N/A while (i-- > 0) {
726N/A methodOop method = (methodOop)_methods->obj_at(i);
0N/A if (method->intrinsic_id() == vmIntrinsics::_Object_init) {
0N/A // rewrite the return bytecodes of Object.<init> to register the
0N/A // object for finalization if needed.
0N/A methodHandle m(THREAD, method);
0N/A rewrite_Object_init(m, CHECK);
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
726N/A // rewrite methods, in two passes
726N/A int i, len = _methods->length();
726N/A
726N/A for (i = len; --i >= 0; ) {
726N/A methodOop method = (methodOop)_methods->obj_at(i);
726N/A scan_method(method);
726N/A }
726N/A
726N/A // allocate constant pool cache, now that we've seen all the bytecodes
726N/A make_constant_pool_cache(CHECK);
726N/A
726N/A for (i = len; --i >= 0; ) {
726N/A methodHandle m(THREAD, (methodOop)_methods->obj_at(i));
726N/A
726N/A if (m->has_jsrs()) {
726N/A m = rewrite_jsrs(m, CHECK);
0N/A // Method might have gotten rewritten.
726N/A _methods->obj_at_put(i, m());
0N/A }
726N/A
726N/A // Set up method entry points for compiler and interpreter.
726N/A m->link_method(m, CHECK);
0N/A }
0N/A}