src/share/vm/interpreter/rewriter.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6307
10c9507f544a
parent 0
f90c822e73f8
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "interpreter/bytecodes.hpp"
aoqi@0 27 #include "interpreter/interpreter.hpp"
aoqi@0 28 #include "interpreter/rewriter.hpp"
aoqi@0 29 #include "memory/gcLocker.hpp"
aoqi@0 30 #include "memory/resourceArea.hpp"
aoqi@0 31 #include "oops/generateOopMap.hpp"
aoqi@0 32 #include "prims/methodHandles.hpp"
aoqi@0 33
aoqi@0 34 // Computes a CPC map (new_index -> original_index) for constant pool entries
aoqi@0 35 // that are referred to by the interpreter at runtime via the constant pool cache.
aoqi@0 36 // Also computes a CP map (original_index -> new_index).
aoqi@0 37 // Marks entries in CP which require additional processing.
aoqi@0 38 void Rewriter::compute_index_maps() {
aoqi@0 39 const int length = _pool->length();
aoqi@0 40 init_maps(length);
aoqi@0 41 bool saw_mh_symbol = false;
aoqi@0 42 for (int i = 0; i < length; i++) {
aoqi@0 43 int tag = _pool->tag_at(i).value();
aoqi@0 44 switch (tag) {
aoqi@0 45 case JVM_CONSTANT_InterfaceMethodref:
aoqi@0 46 case JVM_CONSTANT_Fieldref : // fall through
aoqi@0 47 case JVM_CONSTANT_Methodref : // fall through
aoqi@0 48 add_cp_cache_entry(i);
aoqi@0 49 break;
aoqi@0 50 case JVM_CONSTANT_String:
aoqi@0 51 case JVM_CONSTANT_MethodHandle : // fall through
aoqi@0 52 case JVM_CONSTANT_MethodType : // fall through
aoqi@0 53 add_resolved_references_entry(i);
aoqi@0 54 break;
aoqi@0 55 case JVM_CONSTANT_Utf8:
aoqi@0 56 if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle())
aoqi@0 57 saw_mh_symbol = true;
aoqi@0 58 break;
aoqi@0 59 }
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 // Record limits of resolved reference map for constant pool cache indices
aoqi@0 63 record_map_limits();
aoqi@0 64
aoqi@0 65 guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1),
aoqi@0 66 "all cp cache indexes fit in a u2");
aoqi@0 67
aoqi@0 68 if (saw_mh_symbol)
aoqi@0 69 _method_handle_invokers.initialize(length, (int)0);
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 // Unrewrite the bytecodes if an error occurs.
aoqi@0 73 void Rewriter::restore_bytecodes() {
aoqi@0 74 int len = _methods->length();
aoqi@0 75 bool invokespecial_error = false;
aoqi@0 76
aoqi@0 77 for (int i = len-1; i >= 0; i--) {
aoqi@0 78 Method* method = _methods->at(i);
aoqi@0 79 scan_method(method, true, &invokespecial_error);
aoqi@0 80 assert(!invokespecial_error, "reversing should not get an invokespecial error");
aoqi@0 81 }
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 // Creates a constant pool cache given a CPC map
aoqi@0 85 void Rewriter::make_constant_pool_cache(TRAPS) {
aoqi@0 86 ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data();
aoqi@0 87 ConstantPoolCache* cache =
aoqi@0 88 ConstantPoolCache::allocate(loader_data, _cp_cache_map,
aoqi@0 89 _invokedynamic_cp_cache_map,
aoqi@0 90 _invokedynamic_references_map, CHECK);
aoqi@0 91
aoqi@0 92 // initialize object cache in constant pool
aoqi@0 93 _pool->initialize_resolved_references(loader_data, _resolved_references_map,
aoqi@0 94 _resolved_reference_limit,
aoqi@0 95 CHECK);
aoqi@0 96 _pool->set_cache(cache);
aoqi@0 97 cache->set_constant_pool(_pool());
aoqi@0 98 }
aoqi@0 99
aoqi@0 100
aoqi@0 101
aoqi@0 102 // The new finalization semantics says that registration of
aoqi@0 103 // finalizable objects must be performed on successful return from the
aoqi@0 104 // Object.<init> constructor. We could implement this trivially if
aoqi@0 105 // <init> were never rewritten but since JVMTI allows this to occur, a
aoqi@0 106 // more complicated solution is required. A special return bytecode
aoqi@0 107 // is used only by Object.<init> to signal the finalization
aoqi@0 108 // registration point. Additionally local 0 must be preserved so it's
aoqi@0 109 // available to pass to the registration function. For simplicty we
aoqi@0 110 // require that local 0 is never overwritten so it's available as an
aoqi@0 111 // argument for registration.
aoqi@0 112
aoqi@0 113 void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) {
aoqi@0 114 RawBytecodeStream bcs(method);
aoqi@0 115 while (!bcs.is_last_bytecode()) {
aoqi@0 116 Bytecodes::Code opcode = bcs.raw_next();
aoqi@0 117 switch (opcode) {
aoqi@0 118 case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break;
aoqi@0 119
aoqi@0 120 case Bytecodes::_istore:
aoqi@0 121 case Bytecodes::_lstore:
aoqi@0 122 case Bytecodes::_fstore:
aoqi@0 123 case Bytecodes::_dstore:
aoqi@0 124 case Bytecodes::_astore:
aoqi@0 125 if (bcs.get_index() != 0) continue;
aoqi@0 126
aoqi@0 127 // fall through
aoqi@0 128 case Bytecodes::_istore_0:
aoqi@0 129 case Bytecodes::_lstore_0:
aoqi@0 130 case Bytecodes::_fstore_0:
aoqi@0 131 case Bytecodes::_dstore_0:
aoqi@0 132 case Bytecodes::_astore_0:
aoqi@0 133 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
aoqi@0 134 "can't overwrite local 0 in Object.<init>");
aoqi@0 135 break;
aoqi@0 136 }
aoqi@0 137 }
aoqi@0 138 }
aoqi@0 139
aoqi@0 140
aoqi@0 141 // Rewrite a classfile-order CP index into a native-order CPC index.
aoqi@0 142 void Rewriter::rewrite_member_reference(address bcp, int offset, bool reverse) {
aoqi@0 143 address p = bcp + offset;
aoqi@0 144 if (!reverse) {
aoqi@0 145 int cp_index = Bytes::get_Java_u2(p);
aoqi@0 146 int cache_index = cp_entry_to_cp_cache(cp_index);
aoqi@0 147 Bytes::put_native_u2(p, cache_index);
aoqi@0 148 if (!_method_handle_invokers.is_empty())
aoqi@0 149 maybe_rewrite_invokehandle(p - 1, cp_index, cache_index, reverse);
aoqi@0 150 } else {
aoqi@0 151 int cache_index = Bytes::get_native_u2(p);
aoqi@0 152 int pool_index = cp_cache_entry_pool_index(cache_index);
aoqi@0 153 Bytes::put_Java_u2(p, pool_index);
aoqi@0 154 if (!_method_handle_invokers.is_empty())
aoqi@0 155 maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse);
aoqi@0 156 }
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 // If the constant pool entry for invokespecial is InterfaceMethodref,
aoqi@0 160 // we need to add a separate cpCache entry for its resolution, because it is
aoqi@0 161 // different than the resolution for invokeinterface with InterfaceMethodref.
aoqi@0 162 // These cannot share cpCache entries. It's unclear if all invokespecial to
aoqi@0 163 // InterfaceMethodrefs would resolve to the same thing so a new cpCache entry
aoqi@0 164 // is created for each one. This was added with lambda.
aoqi@0 165 void Rewriter::rewrite_invokespecial(address bcp, int offset, bool reverse, bool* invokespecial_error) {
aoqi@0 166 address p = bcp + offset;
aoqi@0 167 if (!reverse) {
aoqi@0 168 int cp_index = Bytes::get_Java_u2(p);
aoqi@0 169 if (_pool->tag_at(cp_index).is_interface_method()) {
aoqi@0 170 int cache_index = add_invokespecial_cp_cache_entry(cp_index);
aoqi@0 171 if (cache_index != (int)(jushort) cache_index) {
aoqi@0 172 *invokespecial_error = true;
aoqi@0 173 }
aoqi@0 174 Bytes::put_native_u2(p, cache_index);
aoqi@0 175 } else {
aoqi@0 176 rewrite_member_reference(bcp, offset, reverse);
aoqi@0 177 }
aoqi@0 178 } else {
aoqi@0 179 rewrite_member_reference(bcp, offset, reverse);
aoqi@0 180 }
aoqi@0 181 }
aoqi@0 182
aoqi@0 183
aoqi@0 184 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.)
aoqi@0 185 void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) {
aoqi@0 186 if (!reverse) {
aoqi@0 187 if ((*opc) == (u1)Bytecodes::_invokevirtual ||
aoqi@0 188 // allow invokespecial as an alias, although it would be very odd:
aoqi@0 189 (*opc) == (u1)Bytecodes::_invokespecial) {
aoqi@0 190 assert(_pool->tag_at(cp_index).is_method(), "wrong index");
aoqi@0 191 // Determine whether this is a signature-polymorphic method.
aoqi@0 192 if (cp_index >= _method_handle_invokers.length()) return;
aoqi@0 193 int status = _method_handle_invokers[cp_index];
aoqi@0 194 assert(status >= -1 && status <= 1, "oob tri-state");
aoqi@0 195 if (status == 0) {
aoqi@0 196 if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() &&
aoqi@0 197 MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(),
aoqi@0 198 _pool->name_ref_at(cp_index))) {
aoqi@0 199 // we may need a resolved_refs entry for the appendix
aoqi@0 200 add_invokedynamic_resolved_references_entries(cp_index, cache_index);
aoqi@0 201 status = +1;
aoqi@0 202 } else {
aoqi@0 203 status = -1;
aoqi@0 204 }
aoqi@0 205 _method_handle_invokers[cp_index] = status;
aoqi@0 206 }
aoqi@0 207 // We use a special internal bytecode for such methods (if non-static).
aoqi@0 208 // The basic reason for this is that such methods need an extra "appendix" argument
aoqi@0 209 // to transmit the call site's intended call type.
aoqi@0 210 if (status > 0) {
aoqi@0 211 (*opc) = (u1)Bytecodes::_invokehandle;
aoqi@0 212 }
aoqi@0 213 }
aoqi@0 214 } else {
aoqi@0 215 // Do not need to look at cp_index.
aoqi@0 216 if ((*opc) == (u1)Bytecodes::_invokehandle) {
aoqi@0 217 (*opc) = (u1)Bytecodes::_invokevirtual;
aoqi@0 218 // Ignore corner case of original _invokespecial instruction.
aoqi@0 219 // This is safe because (a) the signature polymorphic method was final, and
aoqi@0 220 // (b) the implementation of MethodHandle will not call invokespecial on it.
aoqi@0 221 }
aoqi@0 222 }
aoqi@0 223 }
aoqi@0 224
aoqi@0 225
aoqi@0 226 void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) {
aoqi@0 227 address p = bcp + offset;
aoqi@0 228 assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode");
aoqi@0 229 if (!reverse) {
aoqi@0 230 int cp_index = Bytes::get_Java_u2(p);
aoqi@0 231 int cache_index = add_invokedynamic_cp_cache_entry(cp_index);
aoqi@0 232 int resolved_index = add_invokedynamic_resolved_references_entries(cp_index, cache_index);
aoqi@0 233 // Replace the trailing four bytes with a CPC index for the dynamic
aoqi@0 234 // call site. Unlike other CPC entries, there is one per bytecode,
aoqi@0 235 // not just one per distinct CP entry. In other words, the
aoqi@0 236 // CPC-to-CP relation is many-to-one for invokedynamic entries.
aoqi@0 237 // This means we must use a larger index size than u2 to address
aoqi@0 238 // all these entries. That is the main reason invokedynamic
aoqi@0 239 // must have a five-byte instruction format. (Of course, other JVM
aoqi@0 240 // implementations can use the bytes for other purposes.)
aoqi@0 241 // Note: We use native_u4 format exclusively for 4-byte indexes.
aoqi@0 242 Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index));
aoqi@0 243 // add the bcp in case we need to patch this bytecode if we also find a
aoqi@0 244 // invokespecial/InterfaceMethodref in the bytecode stream
aoqi@0 245 _patch_invokedynamic_bcps->push(p);
aoqi@0 246 _patch_invokedynamic_refs->push(resolved_index);
aoqi@0 247 } else {
aoqi@0 248 int cache_index = ConstantPool::decode_invokedynamic_index(
aoqi@0 249 Bytes::get_native_u4(p));
aoqi@0 250 // We will reverse the bytecode rewriting _after_ adjusting them.
aoqi@0 251 // Adjust the cache index by offset to the invokedynamic entries in the
aoqi@0 252 // cpCache plus the delta if the invokedynamic bytecodes were adjusted.
aoqi@0 253 int adjustment = cp_cache_delta() + _first_iteration_cp_cache_limit;
aoqi@0 254 int cp_index = invokedynamic_cp_cache_entry_pool_index(cache_index - adjustment);
aoqi@0 255 assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index");
aoqi@0 256 // zero out 4 bytes
aoqi@0 257 Bytes::put_Java_u4(p, 0);
aoqi@0 258 Bytes::put_Java_u2(p, cp_index);
aoqi@0 259 }
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 void Rewriter::patch_invokedynamic_bytecodes() {
aoqi@0 263 // If the end of the cp_cache is the same as after initializing with the
aoqi@0 264 // cpool, nothing needs to be done. Invokedynamic bytecodes are at the
aoqi@0 265 // correct offsets. ie. no invokespecials added
aoqi@0 266 int delta = cp_cache_delta();
aoqi@0 267 if (delta > 0) {
aoqi@0 268 int length = _patch_invokedynamic_bcps->length();
aoqi@0 269 assert(length == _patch_invokedynamic_refs->length(),
aoqi@0 270 "lengths should match");
aoqi@0 271 for (int i = 0; i < length; i++) {
aoqi@0 272 address p = _patch_invokedynamic_bcps->at(i);
aoqi@0 273 int cache_index = ConstantPool::decode_invokedynamic_index(
aoqi@0 274 Bytes::get_native_u4(p));
aoqi@0 275 Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index + delta));
aoqi@0 276
aoqi@0 277 // invokedynamic resolved references map also points to cp cache and must
aoqi@0 278 // add delta to each.
aoqi@0 279 int resolved_index = _patch_invokedynamic_refs->at(i);
aoqi@0 280 for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) {
aoqi@0 281 assert(_invokedynamic_references_map[resolved_index+entry] == cache_index,
aoqi@0 282 "should be the same index");
aoqi@0 283 _invokedynamic_references_map.at_put(resolved_index+entry,
aoqi@0 284 cache_index + delta);
aoqi@0 285 }
aoqi@0 286 }
aoqi@0 287 }
aoqi@0 288 }
aoqi@0 289
aoqi@0 290
aoqi@0 291 // Rewrite some ldc bytecodes to _fast_aldc
aoqi@0 292 void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide,
aoqi@0 293 bool reverse) {
aoqi@0 294 if (!reverse) {
aoqi@0 295 assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), "not ldc bytecode");
aoqi@0 296 address p = bcp + offset;
aoqi@0 297 int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p);
aoqi@0 298 constantTag tag = _pool->tag_at(cp_index).value();
aoqi@0 299 if (tag.is_method_handle() || tag.is_method_type() || tag.is_string()) {
aoqi@0 300 int ref_index = cp_entry_to_resolved_references(cp_index);
aoqi@0 301 if (is_wide) {
aoqi@0 302 (*bcp) = Bytecodes::_fast_aldc_w;
aoqi@0 303 assert(ref_index == (u2)ref_index, "index overflow");
aoqi@0 304 Bytes::put_native_u2(p, ref_index);
aoqi@0 305 } else {
aoqi@0 306 (*bcp) = Bytecodes::_fast_aldc;
aoqi@0 307 assert(ref_index == (u1)ref_index, "index overflow");
aoqi@0 308 (*p) = (u1)ref_index;
aoqi@0 309 }
aoqi@0 310 }
aoqi@0 311 } else {
aoqi@0 312 Bytecodes::Code rewritten_bc =
aoqi@0 313 (is_wide ? Bytecodes::_fast_aldc_w : Bytecodes::_fast_aldc);
aoqi@0 314 if ((*bcp) == rewritten_bc) {
aoqi@0 315 address p = bcp + offset;
aoqi@0 316 int ref_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p);
aoqi@0 317 int pool_index = resolved_references_entry_to_pool_index(ref_index);
aoqi@0 318 if (is_wide) {
aoqi@0 319 (*bcp) = Bytecodes::_ldc_w;
aoqi@0 320 assert(pool_index == (u2)pool_index, "index overflow");
aoqi@0 321 Bytes::put_Java_u2(p, pool_index);
aoqi@0 322 } else {
aoqi@0 323 (*bcp) = Bytecodes::_ldc;
aoqi@0 324 assert(pool_index == (u1)pool_index, "index overflow");
aoqi@0 325 (*p) = (u1)pool_index;
aoqi@0 326 }
aoqi@0 327 }
aoqi@0 328 }
aoqi@0 329 }
aoqi@0 330
aoqi@0 331
aoqi@0 332 // Rewrites a method given the index_map information
aoqi@0 333 void Rewriter::scan_method(Method* method, bool reverse, bool* invokespecial_error) {
aoqi@0 334
aoqi@0 335 int nof_jsrs = 0;
aoqi@0 336 bool has_monitor_bytecodes = false;
aoqi@0 337
aoqi@0 338 {
aoqi@0 339 // We cannot tolerate a GC in this block, because we've
aoqi@0 340 // cached the bytecodes in 'code_base'. If the Method*
aoqi@0 341 // moves, the bytecodes will also move.
aoqi@0 342 No_Safepoint_Verifier nsv;
aoqi@0 343 Bytecodes::Code c;
aoqi@0 344
aoqi@0 345 // Bytecodes and their length
aoqi@0 346 const address code_base = method->code_base();
aoqi@0 347 const int code_length = method->code_size();
aoqi@0 348
aoqi@0 349 int bc_length;
aoqi@0 350 for (int bci = 0; bci < code_length; bci += bc_length) {
aoqi@0 351 address bcp = code_base + bci;
aoqi@0 352 int prefix_length = 0;
aoqi@0 353 c = (Bytecodes::Code)(*bcp);
aoqi@0 354
aoqi@0 355 // Since we have the code, see if we can get the length
aoqi@0 356 // directly. Some more complicated bytecodes will report
aoqi@0 357 // a length of zero, meaning we need to make another method
aoqi@0 358 // call to calculate the length.
aoqi@0 359 bc_length = Bytecodes::length_for(c);
aoqi@0 360 if (bc_length == 0) {
aoqi@0 361 bc_length = Bytecodes::length_at(method, bcp);
aoqi@0 362
aoqi@0 363 // length_at will put us at the bytecode after the one modified
aoqi@0 364 // by 'wide'. We don't currently examine any of the bytecodes
aoqi@0 365 // modified by wide, but in case we do in the future...
aoqi@0 366 if (c == Bytecodes::_wide) {
aoqi@0 367 prefix_length = 1;
aoqi@0 368 c = (Bytecodes::Code)bcp[1];
aoqi@0 369 }
aoqi@0 370 }
aoqi@0 371
aoqi@0 372 assert(bc_length != 0, "impossible bytecode length");
aoqi@0 373
aoqi@0 374 switch (c) {
aoqi@0 375 case Bytecodes::_lookupswitch : {
aoqi@0 376 #ifndef CC_INTERP
aoqi@0 377 Bytecode_lookupswitch bc(method, bcp);
aoqi@0 378 (*bcp) = (
aoqi@0 379 bc.number_of_pairs() < BinarySwitchThreshold
aoqi@0 380 ? Bytecodes::_fast_linearswitch
aoqi@0 381 : Bytecodes::_fast_binaryswitch
aoqi@0 382 );
aoqi@0 383 #endif
aoqi@0 384 break;
aoqi@0 385 }
aoqi@0 386 case Bytecodes::_fast_linearswitch:
aoqi@0 387 case Bytecodes::_fast_binaryswitch: {
aoqi@0 388 #ifndef CC_INTERP
aoqi@0 389 (*bcp) = Bytecodes::_lookupswitch;
aoqi@0 390 #endif
aoqi@0 391 break;
aoqi@0 392 }
aoqi@0 393
aoqi@0 394 case Bytecodes::_invokespecial : {
aoqi@0 395 rewrite_invokespecial(bcp, prefix_length+1, reverse, invokespecial_error);
aoqi@0 396 break;
aoqi@0 397 }
aoqi@0 398
aoqi@0 399 case Bytecodes::_getstatic : // fall through
aoqi@0 400 case Bytecodes::_putstatic : // fall through
aoqi@0 401 case Bytecodes::_getfield : // fall through
aoqi@0 402 case Bytecodes::_putfield : // fall through
aoqi@0 403 case Bytecodes::_invokevirtual : // fall through
aoqi@0 404 case Bytecodes::_invokestatic :
aoqi@0 405 case Bytecodes::_invokeinterface:
aoqi@0 406 case Bytecodes::_invokehandle : // if reverse=true
aoqi@0 407 rewrite_member_reference(bcp, prefix_length+1, reverse);
aoqi@0 408 break;
aoqi@0 409 case Bytecodes::_invokedynamic:
aoqi@0 410 rewrite_invokedynamic(bcp, prefix_length+1, reverse);
aoqi@0 411 break;
aoqi@0 412 case Bytecodes::_ldc:
aoqi@0 413 case Bytecodes::_fast_aldc: // if reverse=true
aoqi@0 414 maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse);
aoqi@0 415 break;
aoqi@0 416 case Bytecodes::_ldc_w:
aoqi@0 417 case Bytecodes::_fast_aldc_w: // if reverse=true
aoqi@0 418 maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse);
aoqi@0 419 break;
aoqi@0 420 case Bytecodes::_jsr : // fall through
aoqi@0 421 case Bytecodes::_jsr_w : nof_jsrs++; break;
aoqi@0 422 case Bytecodes::_monitorenter : // fall through
aoqi@0 423 case Bytecodes::_monitorexit : has_monitor_bytecodes = true; break;
aoqi@0 424 }
aoqi@0 425 }
aoqi@0 426 }
aoqi@0 427
aoqi@0 428 // Update access flags
aoqi@0 429 if (has_monitor_bytecodes) {
aoqi@0 430 method->set_has_monitor_bytecodes();
aoqi@0 431 }
aoqi@0 432
aoqi@0 433 // The present of a jsr bytecode implies that the method might potentially
aoqi@0 434 // have to be rewritten, so we run the oopMapGenerator on the method
aoqi@0 435 if (nof_jsrs > 0) {
aoqi@0 436 method->set_has_jsrs();
aoqi@0 437 // Second pass will revisit this method.
aoqi@0 438 assert(method->has_jsrs(), "didn't we just set this?");
aoqi@0 439 }
aoqi@0 440 }
aoqi@0 441
aoqi@0 442 // After constant pool is created, revisit methods containing jsrs.
aoqi@0 443 methodHandle Rewriter::rewrite_jsrs(methodHandle method, TRAPS) {
aoqi@0 444 ResourceMark rm(THREAD);
aoqi@0 445 ResolveOopMapConflicts romc(method);
aoqi@0 446 methodHandle original_method = method;
aoqi@0 447 method = romc.do_potential_rewrite(CHECK_(methodHandle()));
aoqi@0 448 // Update monitor matching info.
aoqi@0 449 if (romc.monitor_safe()) {
aoqi@0 450 method->set_guaranteed_monitor_matching();
aoqi@0 451 }
aoqi@0 452
aoqi@0 453 return method;
aoqi@0 454 }
aoqi@0 455
aoqi@0 456 void Rewriter::rewrite_bytecodes(TRAPS) {
aoqi@0 457 assert(_pool->cache() == NULL, "constant pool cache must not be set yet");
aoqi@0 458
aoqi@0 459 // determine index maps for Method* rewriting
aoqi@0 460 compute_index_maps();
aoqi@0 461
aoqi@0 462 if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) {
aoqi@0 463 bool did_rewrite = false;
aoqi@0 464 int i = _methods->length();
aoqi@0 465 while (i-- > 0) {
aoqi@0 466 Method* method = _methods->at(i);
aoqi@0 467 if (method->intrinsic_id() == vmIntrinsics::_Object_init) {
aoqi@0 468 // rewrite the return bytecodes of Object.<init> to register the
aoqi@0 469 // object for finalization if needed.
aoqi@0 470 methodHandle m(THREAD, method);
aoqi@0 471 rewrite_Object_init(m, CHECK);
aoqi@0 472 did_rewrite = true;
aoqi@0 473 break;
aoqi@0 474 }
aoqi@0 475 }
aoqi@0 476 assert(did_rewrite, "must find Object::<init> to rewrite it");
aoqi@0 477 }
aoqi@0 478
aoqi@0 479 // rewrite methods, in two passes
aoqi@0 480 int len = _methods->length();
aoqi@0 481 bool invokespecial_error = false;
aoqi@0 482
aoqi@0 483 for (int i = len-1; i >= 0; i--) {
aoqi@0 484 Method* method = _methods->at(i);
aoqi@0 485 scan_method(method, false, &invokespecial_error);
aoqi@0 486 if (invokespecial_error) {
aoqi@0 487 // If you get an error here, there is no reversing bytecodes
aoqi@0 488 // This exception is stored for this class and no further attempt is
aoqi@0 489 // made at verifying or rewriting.
aoqi@0 490 THROW_MSG(vmSymbols::java_lang_InternalError(),
aoqi@0 491 "This classfile overflows invokespecial for interfaces "
aoqi@0 492 "and cannot be loaded");
aoqi@0 493 return;
aoqi@0 494 }
aoqi@0 495 }
aoqi@0 496
aoqi@0 497 // May have to fix invokedynamic bytecodes if invokestatic/InterfaceMethodref
aoqi@0 498 // entries had to be added.
aoqi@0 499 patch_invokedynamic_bytecodes();
aoqi@0 500 }
aoqi@0 501
aoqi@0 502 void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) {
aoqi@0 503 ResourceMark rm(THREAD);
aoqi@0 504 Rewriter rw(klass, klass->constants(), klass->methods(), CHECK);
aoqi@0 505 // (That's all, folks.)
aoqi@0 506 }
aoqi@0 507
aoqi@0 508
aoqi@0 509 Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS)
aoqi@0 510 : _klass(klass),
aoqi@0 511 _pool(cpool),
aoqi@0 512 _methods(methods)
aoqi@0 513 {
aoqi@0 514
aoqi@0 515 // Rewrite bytecodes - exception here exits.
aoqi@0 516 rewrite_bytecodes(CHECK);
aoqi@0 517
aoqi@0 518 // Stress restoring bytecodes
aoqi@0 519 if (StressRewriter) {
aoqi@0 520 restore_bytecodes();
aoqi@0 521 rewrite_bytecodes(CHECK);
aoqi@0 522 }
aoqi@0 523
aoqi@0 524 // allocate constant pool cache, now that we've seen all the bytecodes
aoqi@0 525 make_constant_pool_cache(THREAD);
aoqi@0 526
aoqi@0 527 // Restore bytecodes to their unrewritten state if there are exceptions
aoqi@0 528 // rewriting bytecodes or allocating the cpCache
aoqi@0 529 if (HAS_PENDING_EXCEPTION) {
aoqi@0 530 restore_bytecodes();
aoqi@0 531 return;
aoqi@0 532 }
aoqi@0 533
aoqi@0 534 // Relocate after everything, but still do this under the is_rewritten flag,
aoqi@0 535 // so methods with jsrs in custom class lists in aren't attempted to be
aoqi@0 536 // rewritten in the RO section of the shared archive.
aoqi@0 537 // Relocated bytecodes don't have to be restored, only the cp cache entries
aoqi@0 538 int len = _methods->length();
aoqi@0 539 for (int i = len-1; i >= 0; i--) {
aoqi@0 540 methodHandle m(THREAD, _methods->at(i));
aoqi@0 541
aoqi@0 542 if (m->has_jsrs()) {
aoqi@0 543 m = rewrite_jsrs(m, THREAD);
aoqi@0 544 // Restore bytecodes to their unrewritten state if there are exceptions
aoqi@0 545 // relocating bytecodes. If some are relocated, that is ok because that
aoqi@0 546 // doesn't affect constant pool to cpCache rewriting.
aoqi@0 547 if (HAS_PENDING_EXCEPTION) {
aoqi@0 548 restore_bytecodes();
aoqi@0 549 return;
aoqi@0 550 }
aoqi@0 551 // Method might have gotten rewritten.
aoqi@0 552 methods->at_put(i, m());
aoqi@0 553 }
aoqi@0 554 }
aoqi@0 555 }

mercurial