src/share/vm/interpreter/rewriter.cpp

Tue, 28 Jul 2009 12:12:40 -0700

author
xdono
date
Tue, 28 Jul 2009 12:12:40 -0700
changeset 1279
bd02caa94611
parent 1161
be93aad57795
child 1291
75596850f863
permissions
-rw-r--r--

6862919: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 07/09
Reviewed-by: tbell, ohair

     1 /*
     2  * Copyright 1998-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 # include "incls/_precompiled.incl"
    26 # include "incls/_rewriter.cpp.incl"
    28 // Computes a CPC map (new_index -> original_index) for constant pool entries
    29 // that are referred to by the interpreter at runtime via the constant pool cache.
    30 // Also computes a CP map (original_index -> new_index).
    31 // Marks entries in CP which require additional processing.
    32 void Rewriter::compute_index_maps() {
    33   const int length  = _pool->length();
    34   init_cp_map(length);
    35   for (int i = 0; i < length; i++) {
    36     int tag = _pool->tag_at(i).value();
    37     switch (tag) {
    38       case JVM_CONSTANT_InterfaceMethodref:
    39       case JVM_CONSTANT_Fieldref          : // fall through
    40       case JVM_CONSTANT_Methodref         : // fall through
    41         add_cp_cache_entry(i);
    42         break;
    43     }
    44   }
    46   guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1),
    47             "all cp cache indexes fit in a u2");
    48 }
    51 int Rewriter::add_extra_cp_cache_entry(int main_entry) {
    52   // Hack: We put it on the map as an encoded value.
    53   // The only place that consumes this is ConstantPoolCacheEntry::set_initial_state
    54   int encoded = constantPoolCacheOopDesc::encode_secondary_index(main_entry);
    55   int plain_secondary_index = _cp_cache_map.append(encoded);
    56   return constantPoolCacheOopDesc::encode_secondary_index(plain_secondary_index);
    57 }
    61 // Creates a constant pool cache given a CPC map
    62 // This creates the constant pool cache initially in a state
    63 // that is unsafe for concurrent GC processing but sets it to
    64 // a safe mode before the constant pool cache is returned.
    65 void Rewriter::make_constant_pool_cache(TRAPS) {
    66   const int length = _cp_cache_map.length();
    67   constantPoolCacheOop cache =
    68       oopFactory::new_constantPoolCache(length, methodOopDesc::IsUnsafeConc, CHECK);
    69   cache->initialize(_cp_cache_map);
    70   _pool->set_cache(cache);
    71   cache->set_constant_pool(_pool());
    72 }
    76 // The new finalization semantics says that registration of
    77 // finalizable objects must be performed on successful return from the
    78 // Object.<init> constructor.  We could implement this trivially if
    79 // <init> were never rewritten but since JVMTI allows this to occur, a
    80 // more complicated solution is required.  A special return bytecode
    81 // is used only by Object.<init> to signal the finalization
    82 // registration point.  Additionally local 0 must be preserved so it's
    83 // available to pass to the registration function.  For simplicty we
    84 // require that local 0 is never overwritten so it's available as an
    85 // argument for registration.
    87 void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) {
    88   RawBytecodeStream bcs(method);
    89   while (!bcs.is_last_bytecode()) {
    90     Bytecodes::Code opcode = bcs.raw_next();
    91     switch (opcode) {
    92       case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break;
    94       case Bytecodes::_istore:
    95       case Bytecodes::_lstore:
    96       case Bytecodes::_fstore:
    97       case Bytecodes::_dstore:
    98       case Bytecodes::_astore:
    99         if (bcs.get_index() != 0) continue;
   101         // fall through
   102       case Bytecodes::_istore_0:
   103       case Bytecodes::_lstore_0:
   104       case Bytecodes::_fstore_0:
   105       case Bytecodes::_dstore_0:
   106       case Bytecodes::_astore_0:
   107         THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
   108                   "can't overwrite local 0 in Object.<init>");
   109         break;
   110     }
   111   }
   112 }
   115 // Rewrite a classfile-order CP index into a native-order CPC index.
   116 int Rewriter::rewrite_member_reference(address bcp, int offset) {
   117   address p = bcp + offset;
   118   int  cp_index    = Bytes::get_Java_u2(p);
   119   int  cache_index = cp_entry_to_cp_cache(cp_index);
   120   Bytes::put_native_u2(p, cache_index);
   121   return cp_index;
   122 }
   125 void Rewriter::rewrite_invokedynamic(address bcp, int offset, int delete_me) {
   126   address p = bcp + offset;
   127   assert(p[-1] == Bytecodes::_invokedynamic, "");
   128   int cp_index = Bytes::get_Java_u2(p);
   129   int cpc  = maybe_add_cp_cache_entry(cp_index);  // add lazily
   130   int cpc2 = add_extra_cp_cache_entry(cpc);
   132   // Replace the trailing four bytes with a CPC index for the dynamic
   133   // call site.  Unlike other CPC entries, there is one per bytecode,
   134   // not just one per distinct CP entry.  In other words, the
   135   // CPC-to-CP relation is many-to-one for invokedynamic entries.
   136   // This means we must use a larger index size than u2 to address
   137   // all these entries.  That is the main reason invokedynamic
   138   // must have a five-byte instruction format.  (Of course, other JVM
   139   // implementations can use the bytes for other purposes.)
   140   Bytes::put_native_u4(p, cpc2);
   141   // Note: We use native_u4 format exclusively for 4-byte indexes.
   142 }
   145 // Rewrites a method given the index_map information
   146 void Rewriter::scan_method(methodOop method) {
   148   int nof_jsrs = 0;
   149   bool has_monitor_bytecodes = false;
   151   {
   152     // We cannot tolerate a GC in this block, because we've
   153     // cached the bytecodes in 'code_base'. If the methodOop
   154     // moves, the bytecodes will also move.
   155     No_Safepoint_Verifier nsv;
   156     Bytecodes::Code c;
   158     // Bytecodes and their length
   159     const address code_base = method->code_base();
   160     const int code_length = method->code_size();
   162     int bc_length;
   163     for (int bci = 0; bci < code_length; bci += bc_length) {
   164       address bcp = code_base + bci;
   165       int prefix_length = 0;
   166       c = (Bytecodes::Code)(*bcp);
   168       // Since we have the code, see if we can get the length
   169       // directly. Some more complicated bytecodes will report
   170       // a length of zero, meaning we need to make another method
   171       // call to calculate the length.
   172       bc_length = Bytecodes::length_for(c);
   173       if (bc_length == 0) {
   174         bc_length = Bytecodes::length_at(bcp);
   176         // length_at will put us at the bytecode after the one modified
   177         // by 'wide'. We don't currently examine any of the bytecodes
   178         // modified by wide, but in case we do in the future...
   179         if (c == Bytecodes::_wide) {
   180           prefix_length = 1;
   181           c = (Bytecodes::Code)bcp[1];
   182         }
   183       }
   185       assert(bc_length != 0, "impossible bytecode length");
   187       switch (c) {
   188         case Bytecodes::_lookupswitch   : {
   189 #ifndef CC_INTERP
   190           Bytecode_lookupswitch* bc = Bytecode_lookupswitch_at(bcp);
   191           bc->set_code(
   192             bc->number_of_pairs() < BinarySwitchThreshold
   193             ? Bytecodes::_fast_linearswitch
   194             : Bytecodes::_fast_binaryswitch
   195           );
   196 #endif
   197           break;
   198         }
   199         case Bytecodes::_getstatic      : // fall through
   200         case Bytecodes::_putstatic      : // fall through
   201         case Bytecodes::_getfield       : // fall through
   202         case Bytecodes::_putfield       : // fall through
   203         case Bytecodes::_invokevirtual  : // fall through
   204         case Bytecodes::_invokespecial  : // fall through
   205         case Bytecodes::_invokestatic   :
   206         case Bytecodes::_invokeinterface:
   207           rewrite_member_reference(bcp, prefix_length+1);
   208           break;
   209         case Bytecodes::_invokedynamic:
   210           rewrite_invokedynamic(bcp, prefix_length+1, int(sizeof"@@@@DELETE ME"));
   211           break;
   212         case Bytecodes::_jsr            : // fall through
   213         case Bytecodes::_jsr_w          : nof_jsrs++;                   break;
   214         case Bytecodes::_monitorenter   : // fall through
   215         case Bytecodes::_monitorexit    : has_monitor_bytecodes = true; break;
   216       }
   217     }
   218   }
   220   // Update access flags
   221   if (has_monitor_bytecodes) {
   222     method->set_has_monitor_bytecodes();
   223   }
   225   // The present of a jsr bytecode implies that the method might potentially
   226   // have to be rewritten, so we run the oopMapGenerator on the method
   227   if (nof_jsrs > 0) {
   228     method->set_has_jsrs();
   229     // Second pass will revisit this method.
   230     assert(method->has_jsrs(), "");
   231   }
   232 }
   234 // After constant pool is created, revisit methods containing jsrs.
   235 methodHandle Rewriter::rewrite_jsrs(methodHandle method, TRAPS) {
   236   ResolveOopMapConflicts romc(method);
   237   methodHandle original_method = method;
   238   method = romc.do_potential_rewrite(CHECK_(methodHandle()));
   239   if (method() != original_method()) {
   240     // Insert invalid bytecode into original methodOop and set
   241     // interpreter entrypoint, so that a executing this method
   242     // will manifest itself in an easy recognizable form.
   243     address bcp = original_method->bcp_from(0);
   244     *bcp = (u1)Bytecodes::_shouldnotreachhere;
   245     int kind = Interpreter::method_kind(original_method);
   246     original_method->set_interpreter_kind(kind);
   247   }
   249   // Update monitor matching info.
   250   if (romc.monitor_safe()) {
   251     method->set_guaranteed_monitor_matching();
   252   }
   254   return method;
   255 }
   258 void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) {
   259   ResourceMark rm(THREAD);
   260   Rewriter     rw(klass, CHECK);
   261   // (That's all, folks.)
   262 }
   264 Rewriter::Rewriter(instanceKlassHandle klass, TRAPS)
   265   : _klass(klass),
   266     // gather starting points
   267     _pool(   THREAD, klass->constants()),
   268     _methods(THREAD, klass->methods())
   269 {
   270   assert(_pool->cache() == NULL, "constant pool cache must not be set yet");
   272   // determine index maps for methodOop rewriting
   273   compute_index_maps();
   275   if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) {
   276     int i = _methods->length();
   277     while (i-- > 0) {
   278       methodOop method = (methodOop)_methods->obj_at(i);
   279       if (method->intrinsic_id() == vmIntrinsics::_Object_init) {
   280         // rewrite the return bytecodes of Object.<init> to register the
   281         // object for finalization if needed.
   282         methodHandle m(THREAD, method);
   283         rewrite_Object_init(m, CHECK);
   284         break;
   285       }
   286     }
   287   }
   289   // rewrite methods, in two passes
   290   int i, len = _methods->length();
   292   for (i = len; --i >= 0; ) {
   293     methodOop method = (methodOop)_methods->obj_at(i);
   294     scan_method(method);
   295   }
   297   // allocate constant pool cache, now that we've seen all the bytecodes
   298   make_constant_pool_cache(CHECK);
   300   for (i = len; --i >= 0; ) {
   301     methodHandle m(THREAD, (methodOop)_methods->obj_at(i));
   303     if (m->has_jsrs()) {
   304       m = rewrite_jsrs(m, CHECK);
   305       // Method might have gotten rewritten.
   306       _methods->obj_at_put(i, m());
   307     }
   309     // Set up method entry points for compiler and interpreter.
   310     m->link_method(m, CHECK);
   311   }
   312 }

mercurial