< prev index next >

src/share/vm/interpreter/rewriter.cpp

Print this page
rev 10555 : imported patch primitive arrays
rev 10556 : imported patch update dates
   1 /*
   2  * Copyright (c) 1998, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


  48       case JVM_CONSTANT_Methodref         : // fall through
  49         add_cp_cache_entry(i);
  50         break;
  51       case JVM_CONSTANT_String:
  52       case JVM_CONSTANT_MethodHandle      : // fall through
  53       case JVM_CONSTANT_MethodType        : // fall through
  54         add_resolved_references_entry(i);
  55         break;
  56       case JVM_CONSTANT_Utf8:
  57         if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle() ||
  58             _pool->symbol_at(i) == vmSymbols::java_lang_invoke_VarHandle()) {
  59           saw_mh_symbol = true;
  60         }
  61         break;
  62     }
  63   }
  64 
  65   // Record limits of resolved reference map for constant pool cache indices
  66   record_map_limits();
  67 
  68   guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1),
  69             "all cp cache indexes fit in a u2");
  70 
  71   if (saw_mh_symbol)
  72     _method_handle_invokers.initialize(length, (int)0);

  73 }
  74 
  75 // Unrewrite the bytecodes if an error occurs.
  76 void Rewriter::restore_bytecodes() {
  77   int len = _methods->length();
  78   bool invokespecial_error = false;
  79 
  80   for (int i = len-1; i >= 0; i--) {
  81     Method* method = _methods->at(i);
  82     scan_method(method, true, &invokespecial_error);
  83     assert(!invokespecial_error, "reversing should not get an invokespecial error");
  84   }
  85 }
  86 
  87 // Creates a constant pool cache given a CPC map
  88 void Rewriter::make_constant_pool_cache(TRAPS) {
  89   ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data();
  90   ConstantPoolCache* cache =
  91       ConstantPoolCache::allocate(loader_data, _cp_cache_map,
  92                                   _invokedynamic_cp_cache_map,


 176       }
 177       Bytes::put_native_u2(p, cache_index);
 178     } else {
 179       rewrite_member_reference(bcp, offset, reverse);
 180     }
 181   } else {
 182     rewrite_member_reference(bcp, offset, reverse);
 183   }
 184 }
 185 
 186 
 187 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.)
 188 void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) {
 189   if (!reverse) {
 190     if ((*opc) == (u1)Bytecodes::_invokevirtual ||
 191         // allow invokespecial as an alias, although it would be very odd:
 192         (*opc) == (u1)Bytecodes::_invokespecial) {
 193       assert(_pool->tag_at(cp_index).is_method(), "wrong index");
 194       // Determine whether this is a signature-polymorphic method.
 195       if (cp_index >= _method_handle_invokers.length())  return;
 196       int status = _method_handle_invokers[cp_index];
 197       assert(status >= -1 && status <= 1, "oob tri-state");
 198       if (status == 0) {
 199         if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() &&
 200             MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(),
 201                                                          _pool->name_ref_at(cp_index))) {
 202           // we may need a resolved_refs entry for the appendix
 203           add_invokedynamic_resolved_references_entries(cp_index, cache_index);
 204           status = +1;
 205         } else if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_VarHandle() &&
 206                    MethodHandles::is_signature_polymorphic_name(SystemDictionary::VarHandle_klass(),
 207                                                                 _pool->name_ref_at(cp_index))) {
 208           // we may need a resolved_refs entry for the appendix
 209           add_invokedynamic_resolved_references_entries(cp_index, cache_index);
 210           status = +1;
 211         } else {
 212           status = -1;
 213         }
 214         _method_handle_invokers[cp_index] = status;
 215       }
 216       // We use a special internal bytecode for such methods (if non-static).
 217       // The basic reason for this is that such methods need an extra "appendix" argument
 218       // to transmit the call site's intended call type.
 219       if (status > 0) {
 220         (*opc) = (u1)Bytecodes::_invokehandle;
 221       }
 222     }
 223   } else {
 224     // Do not need to look at cp_index.
 225     if ((*opc) == (u1)Bytecodes::_invokehandle) {
 226       (*opc) = (u1)Bytecodes::_invokevirtual;
 227       // Ignore corner case of original _invokespecial instruction.
 228       // This is safe because (a) the signature polymorphic method was final, and
 229       // (b) the implementation of MethodHandle will not call invokespecial on it.
 230     }
 231   }
 232 }
 233 
 234 


 270 
 271 void Rewriter::patch_invokedynamic_bytecodes() {
 272   // If the end of the cp_cache is the same as after initializing with the
 273   // cpool, nothing needs to be done.  Invokedynamic bytecodes are at the
 274   // correct offsets. ie. no invokespecials added
 275   int delta = cp_cache_delta();
 276   if (delta > 0) {
 277     int length = _patch_invokedynamic_bcps->length();
 278     assert(length == _patch_invokedynamic_refs->length(),
 279            "lengths should match");
 280     for (int i = 0; i < length; i++) {
 281       address p = _patch_invokedynamic_bcps->at(i);
 282       int cache_index = ConstantPool::decode_invokedynamic_index(
 283                           Bytes::get_native_u4(p));
 284       Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index + delta));
 285 
 286       // invokedynamic resolved references map also points to cp cache and must
 287       // add delta to each.
 288       int resolved_index = _patch_invokedynamic_refs->at(i);
 289       for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) {
 290         assert(_invokedynamic_references_map[resolved_index+entry] == cache_index,
 291              "should be the same index");
 292         _invokedynamic_references_map.at_put(resolved_index+entry,
 293                                              cache_index + delta);
 294       }
 295     }
 296   }
 297 }
 298 
 299 
 300 // Rewrite some ldc bytecodes to _fast_aldc
 301 void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide,
 302                                  bool reverse) {
 303   if (!reverse) {
 304     assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), "not ldc bytecode");
 305     address p = bcp + offset;
 306     int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p);
 307     constantTag tag = _pool->tag_at(cp_index).value();
 308     if (tag.is_method_handle() || tag.is_method_type() || tag.is_string()) {
 309       int ref_index = cp_entry_to_resolved_references(cp_index);
 310       if (is_wide) {


 503      }
 504   }
 505 
 506   // May have to fix invokedynamic bytecodes if invokestatic/InterfaceMethodref
 507   // entries had to be added.
 508   patch_invokedynamic_bytecodes();
 509 }
 510 
 511 void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) {
 512   if (!DumpSharedSpaces) {
 513     assert(!MetaspaceShared::is_in_shared_space(klass()), "archive methods must not be rewritten at run time");
 514   }
 515   ResourceMark rm(THREAD);
 516   Rewriter     rw(klass, klass->constants(), klass->methods(), CHECK);
 517   // (That's all, folks.)
 518 }
 519 
 520 Rewriter::Rewriter(instanceKlassHandle klass, const constantPoolHandle& cpool, Array<Method*>* methods, TRAPS)
 521   : _klass(klass),
 522     _pool(cpool),
 523     _methods(methods)







 524 {
 525 
 526   // Rewrite bytecodes - exception here exits.
 527   rewrite_bytecodes(CHECK);
 528 
 529   // Stress restoring bytecodes
 530   if (StressRewriter) {
 531     restore_bytecodes();
 532     rewrite_bytecodes(CHECK);
 533   }
 534 
 535   // allocate constant pool cache, now that we've seen all the bytecodes
 536   make_constant_pool_cache(THREAD);
 537 
 538   // Restore bytecodes to their unrewritten state if there are exceptions
 539   // rewriting bytecodes or allocating the cpCache
 540   if (HAS_PENDING_EXCEPTION) {
 541     restore_bytecodes();
 542     return;
 543   }


   1 /*
   2  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


  48       case JVM_CONSTANT_Methodref         : // fall through
  49         add_cp_cache_entry(i);
  50         break;
  51       case JVM_CONSTANT_String:
  52       case JVM_CONSTANT_MethodHandle      : // fall through
  53       case JVM_CONSTANT_MethodType        : // fall through
  54         add_resolved_references_entry(i);
  55         break;
  56       case JVM_CONSTANT_Utf8:
  57         if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle() ||
  58             _pool->symbol_at(i) == vmSymbols::java_lang_invoke_VarHandle()) {
  59           saw_mh_symbol = true;
  60         }
  61         break;
  62     }
  63   }
  64 
  65   // Record limits of resolved reference map for constant pool cache indices
  66   record_map_limits();
  67 
  68   guarantee((int) _cp_cache_map.length() - 1 <= (int) ((u2)-1),
  69             "all cp cache indexes fit in a u2");
  70 
  71   if (saw_mh_symbol) {
  72     _method_handle_invokers.at_grow(length, 0);
  73   }
  74 }
  75 
  76 // Unrewrite the bytecodes if an error occurs.
  77 void Rewriter::restore_bytecodes() {
  78   int len = _methods->length();
  79   bool invokespecial_error = false;
  80 
  81   for (int i = len-1; i >= 0; i--) {
  82     Method* method = _methods->at(i);
  83     scan_method(method, true, &invokespecial_error);
  84     assert(!invokespecial_error, "reversing should not get an invokespecial error");
  85   }
  86 }
  87 
  88 // Creates a constant pool cache given a CPC map
  89 void Rewriter::make_constant_pool_cache(TRAPS) {
  90   ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data();
  91   ConstantPoolCache* cache =
  92       ConstantPoolCache::allocate(loader_data, _cp_cache_map,
  93                                   _invokedynamic_cp_cache_map,


 177       }
 178       Bytes::put_native_u2(p, cache_index);
 179     } else {
 180       rewrite_member_reference(bcp, offset, reverse);
 181     }
 182   } else {
 183     rewrite_member_reference(bcp, offset, reverse);
 184   }
 185 }
 186 
 187 
 188 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.)
 189 void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) {
 190   if (!reverse) {
 191     if ((*opc) == (u1)Bytecodes::_invokevirtual ||
 192         // allow invokespecial as an alias, although it would be very odd:
 193         (*opc) == (u1)Bytecodes::_invokespecial) {
 194       assert(_pool->tag_at(cp_index).is_method(), "wrong index");
 195       // Determine whether this is a signature-polymorphic method.
 196       if (cp_index >= _method_handle_invokers.length())  return;
 197       int status = _method_handle_invokers.at(cp_index);
 198       assert(status >= -1 && status <= 1, "oob tri-state");
 199       if (status == 0) {
 200         if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() &&
 201             MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(),
 202                                                          _pool->name_ref_at(cp_index))) {
 203           // we may need a resolved_refs entry for the appendix
 204           add_invokedynamic_resolved_references_entries(cp_index, cache_index);
 205           status = +1;
 206         } else if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_VarHandle() &&
 207                    MethodHandles::is_signature_polymorphic_name(SystemDictionary::VarHandle_klass(),
 208                                                                 _pool->name_ref_at(cp_index))) {
 209           // we may need a resolved_refs entry for the appendix
 210           add_invokedynamic_resolved_references_entries(cp_index, cache_index);
 211           status = +1;
 212         } else {
 213           status = -1;
 214         }
 215         _method_handle_invokers.at(cp_index) = status;
 216       }
 217       // We use a special internal bytecode for such methods (if non-static).
 218       // The basic reason for this is that such methods need an extra "appendix" argument
 219       // to transmit the call site's intended call type.
 220       if (status > 0) {
 221         (*opc) = (u1)Bytecodes::_invokehandle;
 222       }
 223     }
 224   } else {
 225     // Do not need to look at cp_index.
 226     if ((*opc) == (u1)Bytecodes::_invokehandle) {
 227       (*opc) = (u1)Bytecodes::_invokevirtual;
 228       // Ignore corner case of original _invokespecial instruction.
 229       // This is safe because (a) the signature polymorphic method was final, and
 230       // (b) the implementation of MethodHandle will not call invokespecial on it.
 231     }
 232   }
 233 }
 234 
 235 


 271 
 272 void Rewriter::patch_invokedynamic_bytecodes() {
 273   // If the end of the cp_cache is the same as after initializing with the
 274   // cpool, nothing needs to be done.  Invokedynamic bytecodes are at the
 275   // correct offsets. ie. no invokespecials added
 276   int delta = cp_cache_delta();
 277   if (delta > 0) {
 278     int length = _patch_invokedynamic_bcps->length();
 279     assert(length == _patch_invokedynamic_refs->length(),
 280            "lengths should match");
 281     for (int i = 0; i < length; i++) {
 282       address p = _patch_invokedynamic_bcps->at(i);
 283       int cache_index = ConstantPool::decode_invokedynamic_index(
 284                           Bytes::get_native_u4(p));
 285       Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index + delta));
 286 
 287       // invokedynamic resolved references map also points to cp cache and must
 288       // add delta to each.
 289       int resolved_index = _patch_invokedynamic_refs->at(i);
 290       for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) {
 291         assert(_invokedynamic_references_map.at(resolved_index + entry) == cache_index,
 292              "should be the same index");
 293         _invokedynamic_references_map.at_put(resolved_index+entry,
 294                                              cache_index + delta);
 295       }
 296     }
 297   }
 298 }
 299 
 300 
 301 // Rewrite some ldc bytecodes to _fast_aldc
 302 void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide,
 303                                  bool reverse) {
 304   if (!reverse) {
 305     assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), "not ldc bytecode");
 306     address p = bcp + offset;
 307     int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p);
 308     constantTag tag = _pool->tag_at(cp_index).value();
 309     if (tag.is_method_handle() || tag.is_method_type() || tag.is_string()) {
 310       int ref_index = cp_entry_to_resolved_references(cp_index);
 311       if (is_wide) {


 504      }
 505   }
 506 
 507   // May have to fix invokedynamic bytecodes if invokestatic/InterfaceMethodref
 508   // entries had to be added.
 509   patch_invokedynamic_bytecodes();
 510 }
 511 
 512 void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) {
 513   if (!DumpSharedSpaces) {
 514     assert(!MetaspaceShared::is_in_shared_space(klass()), "archive methods must not be rewritten at run time");
 515   }
 516   ResourceMark rm(THREAD);
 517   Rewriter     rw(klass, klass->constants(), klass->methods(), CHECK);
 518   // (That's all, folks.)
 519 }
 520 
 521 Rewriter::Rewriter(instanceKlassHandle klass, const constantPoolHandle& cpool, Array<Method*>* methods, TRAPS)
 522   : _klass(klass),
 523     _pool(cpool),
 524     _methods(methods),
 525     _cp_map(cpool->length()),
 526     _cp_cache_map(cpool->length() / 2),
 527     _reference_map(cpool->length()),
 528     _resolved_references_map(cpool->length() / 2),
 529     _invokedynamic_references_map(cpool->length() / 2),
 530     _method_handle_invokers(cpool->length()),
 531     _invokedynamic_cp_cache_map(cpool->length() / 4)
 532 {
 533 
 534   // Rewrite bytecodes - exception here exits.
 535   rewrite_bytecodes(CHECK);
 536 
 537   // Stress restoring bytecodes
 538   if (StressRewriter) {
 539     restore_bytecodes();
 540     rewrite_bytecodes(CHECK);
 541   }
 542 
 543   // allocate constant pool cache, now that we've seen all the bytecodes
 544   make_constant_pool_cache(THREAD);
 545 
 546   // Restore bytecodes to their unrewritten state if there are exceptions
 547   // rewriting bytecodes or allocating the cpCache
 548   if (HAS_PENDING_EXCEPTION) {
 549     restore_bytecodes();
 550     return;
 551   }


< prev index next >