< prev index next >

hotspot/src/share/vm/interpreter/rewriter.cpp

Print this page
rev 10453 : 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  *


  46       case JVM_CONSTANT_InterfaceMethodref:
  47       case JVM_CONSTANT_Fieldref          : // fall through
  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           saw_mh_symbol = true;
  59         break;
  60     }
  61   }
  62 
  63   // Record limits of resolved reference map for constant pool cache indices
  64   record_map_limits();
  65 
  66   guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1),
  67             "all cp cache indexes fit in a u2");
  68 
  69   if (saw_mh_symbol)
  70     _method_handle_invokers.initialize(length, (int)0);

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


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


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


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







 516 {
 517 
 518   // Rewrite bytecodes - exception here exits.
 519   rewrite_bytecodes(CHECK);
 520 
 521   // Stress restoring bytecodes
 522   if (StressRewriter) {
 523     restore_bytecodes();
 524     rewrite_bytecodes(CHECK);
 525   }
 526 
 527   // allocate constant pool cache, now that we've seen all the bytecodes
 528   make_constant_pool_cache(THREAD);
 529 
 530   // Restore bytecodes to their unrewritten state if there are exceptions
 531   // rewriting bytecodes or allocating the cpCache
 532   if (HAS_PENDING_EXCEPTION) {
 533     restore_bytecodes();
 534     return;
 535   }


   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  *


  46       case JVM_CONSTANT_InterfaceMethodref:
  47       case JVM_CONSTANT_Fieldref          : // fall through
  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           saw_mh_symbol = true;
  59         break;
  60     }
  61   }
  62 
  63   // Record limits of resolved reference map for constant pool cache indices
  64   record_map_limits();
  65 
  66   guarantee((int) _cp_cache_map.length() - 1 <= (int) ((u2)-1),
  67             "all cp cache indexes fit in a u2");
  68 
  69   if (saw_mh_symbol) {
  70     _method_handle_invokers.at_grow(length, 0);
  71   }
  72 }
  73 
  74 // Unrewrite the bytecodes if an error occurs.
  75 void Rewriter::restore_bytecodes() {
  76   int len = _methods->length();
  77   bool invokespecial_error = false;
  78 
  79   for (int i = len-1; i >= 0; i--) {
  80     Method* method = _methods->at(i);
  81     scan_method(method, true, &invokespecial_error);
  82     assert(!invokespecial_error, "reversing should not get an invokespecial error");
  83   }
  84 }
  85 
  86 // Creates a constant pool cache given a CPC map
  87 void Rewriter::make_constant_pool_cache(TRAPS) {
  88   ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data();
  89   ConstantPoolCache* cache =
  90       ConstantPoolCache::allocate(loader_data, _cp_cache_map,
  91                                   _invokedynamic_cp_cache_map,


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


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


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


< prev index next >