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