src/share/vm/interpreter/rewriter.cpp

Mon, 01 Oct 2012 14:50:10 -0700

author
twisti
date
Mon, 01 Oct 2012 14:50:10 -0700
changeset 4133
f6b0eb4e44cf
parent 4037
da91efe96a93
child 4395
cc6a617fffd2
permissions
-rw-r--r--

7200949: JSR 292: rubybench/bench/time/bench_base64.rb fails with jruby.jar not on boot class path
Reviewed-by: jrose, kvn

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

mercurial