src/share/vm/interpreter/rewriter.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/interpreter/rewriter.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,555 @@
     1.4 +/*
     1.5 + * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "interpreter/bytecodes.hpp"
    1.30 +#include "interpreter/interpreter.hpp"
    1.31 +#include "interpreter/rewriter.hpp"
    1.32 +#include "memory/gcLocker.hpp"
    1.33 +#include "memory/resourceArea.hpp"
    1.34 +#include "oops/generateOopMap.hpp"
    1.35 +#include "prims/methodHandles.hpp"
    1.36 +
    1.37 +// Computes a CPC map (new_index -> original_index) for constant pool entries
    1.38 +// that are referred to by the interpreter at runtime via the constant pool cache.
    1.39 +// Also computes a CP map (original_index -> new_index).
    1.40 +// Marks entries in CP which require additional processing.
    1.41 +void Rewriter::compute_index_maps() {
    1.42 +  const int length  = _pool->length();
    1.43 +  init_maps(length);
    1.44 +  bool saw_mh_symbol = false;
    1.45 +  for (int i = 0; i < length; i++) {
    1.46 +    int tag = _pool->tag_at(i).value();
    1.47 +    switch (tag) {
    1.48 +      case JVM_CONSTANT_InterfaceMethodref:
    1.49 +      case JVM_CONSTANT_Fieldref          : // fall through
    1.50 +      case JVM_CONSTANT_Methodref         : // fall through
    1.51 +        add_cp_cache_entry(i);
    1.52 +        break;
    1.53 +      case JVM_CONSTANT_String:
    1.54 +      case JVM_CONSTANT_MethodHandle      : // fall through
    1.55 +      case JVM_CONSTANT_MethodType        : // fall through
    1.56 +        add_resolved_references_entry(i);
    1.57 +        break;
    1.58 +      case JVM_CONSTANT_Utf8:
    1.59 +        if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle())
    1.60 +          saw_mh_symbol = true;
    1.61 +        break;
    1.62 +    }
    1.63 +  }
    1.64 +
    1.65 +  // Record limits of resolved reference map for constant pool cache indices
    1.66 +  record_map_limits();
    1.67 +
    1.68 +  guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1),
    1.69 +            "all cp cache indexes fit in a u2");
    1.70 +
    1.71 +  if (saw_mh_symbol)
    1.72 +    _method_handle_invokers.initialize(length, (int)0);
    1.73 +}
    1.74 +
    1.75 +// Unrewrite the bytecodes if an error occurs.
    1.76 +void Rewriter::restore_bytecodes() {
    1.77 +  int len = _methods->length();
    1.78 +  bool invokespecial_error = false;
    1.79 +
    1.80 +  for (int i = len-1; i >= 0; i--) {
    1.81 +    Method* method = _methods->at(i);
    1.82 +    scan_method(method, true, &invokespecial_error);
    1.83 +    assert(!invokespecial_error, "reversing should not get an invokespecial error");
    1.84 +  }
    1.85 +}
    1.86 +
    1.87 +// Creates a constant pool cache given a CPC map
    1.88 +void Rewriter::make_constant_pool_cache(TRAPS) {
    1.89 +  ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data();
    1.90 +  ConstantPoolCache* cache =
    1.91 +      ConstantPoolCache::allocate(loader_data, _cp_cache_map,
    1.92 +                                  _invokedynamic_cp_cache_map,
    1.93 +                                  _invokedynamic_references_map, CHECK);
    1.94 +
    1.95 +  // initialize object cache in constant pool
    1.96 +  _pool->initialize_resolved_references(loader_data, _resolved_references_map,
    1.97 +                                        _resolved_reference_limit,
    1.98 +                                        CHECK);
    1.99 +  _pool->set_cache(cache);
   1.100 +  cache->set_constant_pool(_pool());
   1.101 +}
   1.102 +
   1.103 +
   1.104 +
   1.105 +// The new finalization semantics says that registration of
   1.106 +// finalizable objects must be performed on successful return from the
   1.107 +// Object.<init> constructor.  We could implement this trivially if
   1.108 +// <init> were never rewritten but since JVMTI allows this to occur, a
   1.109 +// more complicated solution is required.  A special return bytecode
   1.110 +// is used only by Object.<init> to signal the finalization
   1.111 +// registration point.  Additionally local 0 must be preserved so it's
   1.112 +// available to pass to the registration function.  For simplicty we
   1.113 +// require that local 0 is never overwritten so it's available as an
   1.114 +// argument for registration.
   1.115 +
   1.116 +void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) {
   1.117 +  RawBytecodeStream bcs(method);
   1.118 +  while (!bcs.is_last_bytecode()) {
   1.119 +    Bytecodes::Code opcode = bcs.raw_next();
   1.120 +    switch (opcode) {
   1.121 +      case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break;
   1.122 +
   1.123 +      case Bytecodes::_istore:
   1.124 +      case Bytecodes::_lstore:
   1.125 +      case Bytecodes::_fstore:
   1.126 +      case Bytecodes::_dstore:
   1.127 +      case Bytecodes::_astore:
   1.128 +        if (bcs.get_index() != 0) continue;
   1.129 +
   1.130 +        // fall through
   1.131 +      case Bytecodes::_istore_0:
   1.132 +      case Bytecodes::_lstore_0:
   1.133 +      case Bytecodes::_fstore_0:
   1.134 +      case Bytecodes::_dstore_0:
   1.135 +      case Bytecodes::_astore_0:
   1.136 +        THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
   1.137 +                  "can't overwrite local 0 in Object.<init>");
   1.138 +        break;
   1.139 +    }
   1.140 +  }
   1.141 +}
   1.142 +
   1.143 +
   1.144 +// Rewrite a classfile-order CP index into a native-order CPC index.
   1.145 +void Rewriter::rewrite_member_reference(address bcp, int offset, bool reverse) {
   1.146 +  address p = bcp + offset;
   1.147 +  if (!reverse) {
   1.148 +    int  cp_index    = Bytes::get_Java_u2(p);
   1.149 +    int  cache_index = cp_entry_to_cp_cache(cp_index);
   1.150 +    Bytes::put_native_u2(p, cache_index);
   1.151 +    if (!_method_handle_invokers.is_empty())
   1.152 +      maybe_rewrite_invokehandle(p - 1, cp_index, cache_index, reverse);
   1.153 +  } else {
   1.154 +    int cache_index = Bytes::get_native_u2(p);
   1.155 +    int pool_index = cp_cache_entry_pool_index(cache_index);
   1.156 +    Bytes::put_Java_u2(p, pool_index);
   1.157 +    if (!_method_handle_invokers.is_empty())
   1.158 +      maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse);
   1.159 +  }
   1.160 +}
   1.161 +
   1.162 +// If the constant pool entry for invokespecial is InterfaceMethodref,
   1.163 +// we need to add a separate cpCache entry for its resolution, because it is
   1.164 +// different than the resolution for invokeinterface with InterfaceMethodref.
   1.165 +// These cannot share cpCache entries.  It's unclear if all invokespecial to
   1.166 +// InterfaceMethodrefs would resolve to the same thing so a new cpCache entry
   1.167 +// is created for each one.  This was added with lambda.
   1.168 +void Rewriter::rewrite_invokespecial(address bcp, int offset, bool reverse, bool* invokespecial_error) {
   1.169 +  address p = bcp + offset;
   1.170 +  if (!reverse) {
   1.171 +    int cp_index = Bytes::get_Java_u2(p);
   1.172 +    if (_pool->tag_at(cp_index).is_interface_method()) {
   1.173 +    int cache_index = add_invokespecial_cp_cache_entry(cp_index);
   1.174 +    if (cache_index != (int)(jushort) cache_index) {
   1.175 +      *invokespecial_error = true;
   1.176 +    }
   1.177 +    Bytes::put_native_u2(p, cache_index);
   1.178 +  } else {
   1.179 +      rewrite_member_reference(bcp, offset, reverse);
   1.180 +    }
   1.181 +  } else {
   1.182 +    rewrite_member_reference(bcp, offset, reverse);
   1.183 +  }
   1.184 +}
   1.185 +
   1.186 +
   1.187 +// Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.)
   1.188 +void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) {
   1.189 +  if (!reverse) {
   1.190 +    if ((*opc) == (u1)Bytecodes::_invokevirtual ||
   1.191 +        // allow invokespecial as an alias, although it would be very odd:
   1.192 +        (*opc) == (u1)Bytecodes::_invokespecial) {
   1.193 +      assert(_pool->tag_at(cp_index).is_method(), "wrong index");
   1.194 +      // Determine whether this is a signature-polymorphic method.
   1.195 +      if (cp_index >= _method_handle_invokers.length())  return;
   1.196 +      int status = _method_handle_invokers[cp_index];
   1.197 +      assert(status >= -1 && status <= 1, "oob tri-state");
   1.198 +      if (status == 0) {
   1.199 +        if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() &&
   1.200 +            MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(),
   1.201 +                                                         _pool->name_ref_at(cp_index))) {
   1.202 +          // we may need a resolved_refs entry for the appendix
   1.203 +          add_invokedynamic_resolved_references_entries(cp_index, cache_index);
   1.204 +          status = +1;
   1.205 +        } else {
   1.206 +          status = -1;
   1.207 +        }
   1.208 +        _method_handle_invokers[cp_index] = status;
   1.209 +      }
   1.210 +      // We use a special internal bytecode for such methods (if non-static).
   1.211 +      // The basic reason for this is that such methods need an extra "appendix" argument
   1.212 +      // to transmit the call site's intended call type.
   1.213 +      if (status > 0) {
   1.214 +        (*opc) = (u1)Bytecodes::_invokehandle;
   1.215 +      }
   1.216 +    }
   1.217 +  } else {
   1.218 +    // Do not need to look at cp_index.
   1.219 +    if ((*opc) == (u1)Bytecodes::_invokehandle) {
   1.220 +      (*opc) = (u1)Bytecodes::_invokevirtual;
   1.221 +      // Ignore corner case of original _invokespecial instruction.
   1.222 +      // This is safe because (a) the signature polymorphic method was final, and
   1.223 +      // (b) the implementation of MethodHandle will not call invokespecial on it.
   1.224 +    }
   1.225 +  }
   1.226 +}
   1.227 +
   1.228 +
   1.229 +void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) {
   1.230 +  address p = bcp + offset;
   1.231 +  assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode");
   1.232 +  if (!reverse) {
   1.233 +    int cp_index = Bytes::get_Java_u2(p);
   1.234 +    int cache_index = add_invokedynamic_cp_cache_entry(cp_index);
   1.235 +    int resolved_index = add_invokedynamic_resolved_references_entries(cp_index, cache_index);
   1.236 +    // Replace the trailing four bytes with a CPC index for the dynamic
   1.237 +    // call site.  Unlike other CPC entries, there is one per bytecode,
   1.238 +    // not just one per distinct CP entry.  In other words, the
   1.239 +    // CPC-to-CP relation is many-to-one for invokedynamic entries.
   1.240 +    // This means we must use a larger index size than u2 to address
   1.241 +    // all these entries.  That is the main reason invokedynamic
   1.242 +    // must have a five-byte instruction format.  (Of course, other JVM
   1.243 +    // implementations can use the bytes for other purposes.)
   1.244 +    // Note: We use native_u4 format exclusively for 4-byte indexes.
   1.245 +    Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index));
   1.246 +    // add the bcp in case we need to patch this bytecode if we also find a
   1.247 +    // invokespecial/InterfaceMethodref in the bytecode stream
   1.248 +    _patch_invokedynamic_bcps->push(p);
   1.249 +    _patch_invokedynamic_refs->push(resolved_index);
   1.250 +  } else {
   1.251 +    int cache_index = ConstantPool::decode_invokedynamic_index(
   1.252 +                        Bytes::get_native_u4(p));
   1.253 +    // We will reverse the bytecode rewriting _after_ adjusting them.
   1.254 +    // Adjust the cache index by offset to the invokedynamic entries in the
   1.255 +    // cpCache plus the delta if the invokedynamic bytecodes were adjusted.
   1.256 +    int adjustment = cp_cache_delta() + _first_iteration_cp_cache_limit;
   1.257 +    int cp_index = invokedynamic_cp_cache_entry_pool_index(cache_index - adjustment);
   1.258 +    assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index");
   1.259 +    // zero out 4 bytes
   1.260 +    Bytes::put_Java_u4(p, 0);
   1.261 +    Bytes::put_Java_u2(p, cp_index);
   1.262 +  }
   1.263 +}
   1.264 +
   1.265 +void Rewriter::patch_invokedynamic_bytecodes() {
   1.266 +  // If the end of the cp_cache is the same as after initializing with the
   1.267 +  // cpool, nothing needs to be done.  Invokedynamic bytecodes are at the
   1.268 +  // correct offsets. ie. no invokespecials added
   1.269 +  int delta = cp_cache_delta();
   1.270 +  if (delta > 0) {
   1.271 +    int length = _patch_invokedynamic_bcps->length();
   1.272 +    assert(length == _patch_invokedynamic_refs->length(),
   1.273 +           "lengths should match");
   1.274 +    for (int i = 0; i < length; i++) {
   1.275 +      address p = _patch_invokedynamic_bcps->at(i);
   1.276 +      int cache_index = ConstantPool::decode_invokedynamic_index(
   1.277 +                          Bytes::get_native_u4(p));
   1.278 +      Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index + delta));
   1.279 +
   1.280 +      // invokedynamic resolved references map also points to cp cache and must
   1.281 +      // add delta to each.
   1.282 +      int resolved_index = _patch_invokedynamic_refs->at(i);
   1.283 +      for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) {
   1.284 +        assert(_invokedynamic_references_map[resolved_index+entry] == cache_index,
   1.285 +             "should be the same index");
   1.286 +        _invokedynamic_references_map.at_put(resolved_index+entry,
   1.287 +                                             cache_index + delta);
   1.288 +      }
   1.289 +    }
   1.290 +  }
   1.291 +}
   1.292 +
   1.293 +
   1.294 +// Rewrite some ldc bytecodes to _fast_aldc
   1.295 +void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide,
   1.296 +                                 bool reverse) {
   1.297 +  if (!reverse) {
   1.298 +    assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), "not ldc bytecode");
   1.299 +    address p = bcp + offset;
   1.300 +    int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p);
   1.301 +    constantTag tag = _pool->tag_at(cp_index).value();
   1.302 +    if (tag.is_method_handle() || tag.is_method_type() || tag.is_string()) {
   1.303 +      int ref_index = cp_entry_to_resolved_references(cp_index);
   1.304 +      if (is_wide) {
   1.305 +        (*bcp) = Bytecodes::_fast_aldc_w;
   1.306 +        assert(ref_index == (u2)ref_index, "index overflow");
   1.307 +        Bytes::put_native_u2(p, ref_index);
   1.308 +      } else {
   1.309 +        (*bcp) = Bytecodes::_fast_aldc;
   1.310 +        assert(ref_index == (u1)ref_index, "index overflow");
   1.311 +        (*p) = (u1)ref_index;
   1.312 +      }
   1.313 +    }
   1.314 +  } else {
   1.315 +    Bytecodes::Code rewritten_bc =
   1.316 +              (is_wide ? Bytecodes::_fast_aldc_w : Bytecodes::_fast_aldc);
   1.317 +    if ((*bcp) == rewritten_bc) {
   1.318 +      address p = bcp + offset;
   1.319 +      int ref_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p);
   1.320 +      int pool_index = resolved_references_entry_to_pool_index(ref_index);
   1.321 +      if (is_wide) {
   1.322 +        (*bcp) = Bytecodes::_ldc_w;
   1.323 +        assert(pool_index == (u2)pool_index, "index overflow");
   1.324 +        Bytes::put_Java_u2(p, pool_index);
   1.325 +      } else {
   1.326 +        (*bcp) = Bytecodes::_ldc;
   1.327 +        assert(pool_index == (u1)pool_index, "index overflow");
   1.328 +        (*p) = (u1)pool_index;
   1.329 +      }
   1.330 +    }
   1.331 +  }
   1.332 +}
   1.333 +
   1.334 +
   1.335 +// Rewrites a method given the index_map information
   1.336 +void Rewriter::scan_method(Method* method, bool reverse, bool* invokespecial_error) {
   1.337 +
   1.338 +  int nof_jsrs = 0;
   1.339 +  bool has_monitor_bytecodes = false;
   1.340 +
   1.341 +  {
   1.342 +    // We cannot tolerate a GC in this block, because we've
   1.343 +    // cached the bytecodes in 'code_base'. If the Method*
   1.344 +    // moves, the bytecodes will also move.
   1.345 +    No_Safepoint_Verifier nsv;
   1.346 +    Bytecodes::Code c;
   1.347 +
   1.348 +    // Bytecodes and their length
   1.349 +    const address code_base = method->code_base();
   1.350 +    const int code_length = method->code_size();
   1.351 +
   1.352 +    int bc_length;
   1.353 +    for (int bci = 0; bci < code_length; bci += bc_length) {
   1.354 +      address bcp = code_base + bci;
   1.355 +      int prefix_length = 0;
   1.356 +      c = (Bytecodes::Code)(*bcp);
   1.357 +
   1.358 +      // Since we have the code, see if we can get the length
   1.359 +      // directly. Some more complicated bytecodes will report
   1.360 +      // a length of zero, meaning we need to make another method
   1.361 +      // call to calculate the length.
   1.362 +      bc_length = Bytecodes::length_for(c);
   1.363 +      if (bc_length == 0) {
   1.364 +        bc_length = Bytecodes::length_at(method, bcp);
   1.365 +
   1.366 +        // length_at will put us at the bytecode after the one modified
   1.367 +        // by 'wide'. We don't currently examine any of the bytecodes
   1.368 +        // modified by wide, but in case we do in the future...
   1.369 +        if (c == Bytecodes::_wide) {
   1.370 +          prefix_length = 1;
   1.371 +          c = (Bytecodes::Code)bcp[1];
   1.372 +        }
   1.373 +      }
   1.374 +
   1.375 +      assert(bc_length != 0, "impossible bytecode length");
   1.376 +
   1.377 +      switch (c) {
   1.378 +        case Bytecodes::_lookupswitch   : {
   1.379 +#ifndef CC_INTERP
   1.380 +          Bytecode_lookupswitch bc(method, bcp);
   1.381 +          (*bcp) = (
   1.382 +            bc.number_of_pairs() < BinarySwitchThreshold
   1.383 +            ? Bytecodes::_fast_linearswitch
   1.384 +            : Bytecodes::_fast_binaryswitch
   1.385 +          );
   1.386 +#endif
   1.387 +          break;
   1.388 +        }
   1.389 +        case Bytecodes::_fast_linearswitch:
   1.390 +        case Bytecodes::_fast_binaryswitch: {
   1.391 +#ifndef CC_INTERP
   1.392 +          (*bcp) = Bytecodes::_lookupswitch;
   1.393 +#endif
   1.394 +          break;
   1.395 +        }
   1.396 +
   1.397 +        case Bytecodes::_invokespecial  : {
   1.398 +          rewrite_invokespecial(bcp, prefix_length+1, reverse, invokespecial_error);
   1.399 +          break;
   1.400 +        }
   1.401 +
   1.402 +        case Bytecodes::_getstatic      : // fall through
   1.403 +        case Bytecodes::_putstatic      : // fall through
   1.404 +        case Bytecodes::_getfield       : // fall through
   1.405 +        case Bytecodes::_putfield       : // fall through
   1.406 +        case Bytecodes::_invokevirtual  : // fall through
   1.407 +        case Bytecodes::_invokestatic   :
   1.408 +        case Bytecodes::_invokeinterface:
   1.409 +        case Bytecodes::_invokehandle   : // if reverse=true
   1.410 +          rewrite_member_reference(bcp, prefix_length+1, reverse);
   1.411 +          break;
   1.412 +        case Bytecodes::_invokedynamic:
   1.413 +          rewrite_invokedynamic(bcp, prefix_length+1, reverse);
   1.414 +          break;
   1.415 +        case Bytecodes::_ldc:
   1.416 +        case Bytecodes::_fast_aldc:  // if reverse=true
   1.417 +          maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse);
   1.418 +          break;
   1.419 +        case Bytecodes::_ldc_w:
   1.420 +        case Bytecodes::_fast_aldc_w:  // if reverse=true
   1.421 +          maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse);
   1.422 +          break;
   1.423 +        case Bytecodes::_jsr            : // fall through
   1.424 +        case Bytecodes::_jsr_w          : nof_jsrs++;                   break;
   1.425 +        case Bytecodes::_monitorenter   : // fall through
   1.426 +        case Bytecodes::_monitorexit    : has_monitor_bytecodes = true; break;
   1.427 +      }
   1.428 +    }
   1.429 +  }
   1.430 +
   1.431 +  // Update access flags
   1.432 +  if (has_monitor_bytecodes) {
   1.433 +    method->set_has_monitor_bytecodes();
   1.434 +  }
   1.435 +
   1.436 +  // The present of a jsr bytecode implies that the method might potentially
   1.437 +  // have to be rewritten, so we run the oopMapGenerator on the method
   1.438 +  if (nof_jsrs > 0) {
   1.439 +    method->set_has_jsrs();
   1.440 +    // Second pass will revisit this method.
   1.441 +    assert(method->has_jsrs(), "didn't we just set this?");
   1.442 +  }
   1.443 +}
   1.444 +
   1.445 +// After constant pool is created, revisit methods containing jsrs.
   1.446 +methodHandle Rewriter::rewrite_jsrs(methodHandle method, TRAPS) {
   1.447 +  ResourceMark rm(THREAD);
   1.448 +  ResolveOopMapConflicts romc(method);
   1.449 +  methodHandle original_method = method;
   1.450 +  method = romc.do_potential_rewrite(CHECK_(methodHandle()));
   1.451 +  // Update monitor matching info.
   1.452 +  if (romc.monitor_safe()) {
   1.453 +    method->set_guaranteed_monitor_matching();
   1.454 +  }
   1.455 +
   1.456 +  return method;
   1.457 +}
   1.458 +
   1.459 +void Rewriter::rewrite_bytecodes(TRAPS) {
   1.460 +  assert(_pool->cache() == NULL, "constant pool cache must not be set yet");
   1.461 +
   1.462 +  // determine index maps for Method* rewriting
   1.463 +  compute_index_maps();
   1.464 +
   1.465 +  if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) {
   1.466 +    bool did_rewrite = false;
   1.467 +    int i = _methods->length();
   1.468 +    while (i-- > 0) {
   1.469 +      Method* method = _methods->at(i);
   1.470 +      if (method->intrinsic_id() == vmIntrinsics::_Object_init) {
   1.471 +        // rewrite the return bytecodes of Object.<init> to register the
   1.472 +        // object for finalization if needed.
   1.473 +        methodHandle m(THREAD, method);
   1.474 +        rewrite_Object_init(m, CHECK);
   1.475 +        did_rewrite = true;
   1.476 +        break;
   1.477 +      }
   1.478 +    }
   1.479 +    assert(did_rewrite, "must find Object::<init> to rewrite it");
   1.480 +  }
   1.481 +
   1.482 +  // rewrite methods, in two passes
   1.483 +  int len = _methods->length();
   1.484 +  bool invokespecial_error = false;
   1.485 +
   1.486 +  for (int i = len-1; i >= 0; i--) {
   1.487 +    Method* method = _methods->at(i);
   1.488 +    scan_method(method, false, &invokespecial_error);
   1.489 +    if (invokespecial_error) {
   1.490 +      // If you get an error here, there is no reversing bytecodes
   1.491 +      // This exception is stored for this class and no further attempt is
   1.492 +      // made at verifying or rewriting.
   1.493 +      THROW_MSG(vmSymbols::java_lang_InternalError(),
   1.494 +                "This classfile overflows invokespecial for interfaces "
   1.495 +                "and cannot be loaded");
   1.496 +      return;
   1.497 +     }
   1.498 +  }
   1.499 +
   1.500 +  // May have to fix invokedynamic bytecodes if invokestatic/InterfaceMethodref
   1.501 +  // entries had to be added.
   1.502 +  patch_invokedynamic_bytecodes();
   1.503 +}
   1.504 +
   1.505 +void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) {
   1.506 +  ResourceMark rm(THREAD);
   1.507 +  Rewriter     rw(klass, klass->constants(), klass->methods(), CHECK);
   1.508 +  // (That's all, folks.)
   1.509 +}
   1.510 +
   1.511 +
   1.512 +Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS)
   1.513 +  : _klass(klass),
   1.514 +    _pool(cpool),
   1.515 +    _methods(methods)
   1.516 +{
   1.517 +
   1.518 +  // Rewrite bytecodes - exception here exits.
   1.519 +  rewrite_bytecodes(CHECK);
   1.520 +
   1.521 +  // Stress restoring bytecodes
   1.522 +  if (StressRewriter) {
   1.523 +    restore_bytecodes();
   1.524 +    rewrite_bytecodes(CHECK);
   1.525 +  }
   1.526 +
   1.527 +  // allocate constant pool cache, now that we've seen all the bytecodes
   1.528 +  make_constant_pool_cache(THREAD);
   1.529 +
   1.530 +  // Restore bytecodes to their unrewritten state if there are exceptions
   1.531 +  // rewriting bytecodes or allocating the cpCache
   1.532 +  if (HAS_PENDING_EXCEPTION) {
   1.533 +    restore_bytecodes();
   1.534 +    return;
   1.535 +  }
   1.536 +
   1.537 +  // Relocate after everything, but still do this under the is_rewritten flag,
   1.538 +  // so methods with jsrs in custom class lists in aren't attempted to be
   1.539 +  // rewritten in the RO section of the shared archive.
   1.540 +  // Relocated bytecodes don't have to be restored, only the cp cache entries
   1.541 +  int len = _methods->length();
   1.542 +  for (int i = len-1; i >= 0; i--) {
   1.543 +    methodHandle m(THREAD, _methods->at(i));
   1.544 +
   1.545 +    if (m->has_jsrs()) {
   1.546 +      m = rewrite_jsrs(m, THREAD);
   1.547 +      // Restore bytecodes to their unrewritten state if there are exceptions
   1.548 +      // relocating bytecodes.  If some are relocated, that is ok because that
   1.549 +      // doesn't affect constant pool to cpCache rewriting.
   1.550 +      if (HAS_PENDING_EXCEPTION) {
   1.551 +        restore_bytecodes();
   1.552 +        return;
   1.553 +      }
   1.554 +      // Method might have gotten rewritten.
   1.555 +      methods->at_put(i, m());
   1.556 +    }
   1.557 +  }
   1.558 +}

mercurial