Tue, 15 May 2012 10:10:23 +0200
7133857: exp() and pow() should use the x87 ISA on x86
Summary: use x87 instructions to implement exp() and pow() in interpreter/c1/c2.
Reviewed-by: kvn, never, twisti
duke@435 | 1 | /* |
never@2462 | 2 | * Copyright (c) 1998, 2011, 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" |
stefank@2314 | 30 | #include "memory/oopFactory.hpp" |
stefank@2314 | 31 | #include "memory/resourceArea.hpp" |
stefank@2314 | 32 | #include "oops/generateOopMap.hpp" |
stefank@2314 | 33 | #include "oops/objArrayOop.hpp" |
stefank@2314 | 34 | #include "oops/oop.inline.hpp" |
stefank@2314 | 35 | #include "prims/methodComparator.hpp" |
duke@435 | 36 | |
jrose@1161 | 37 | // Computes a CPC map (new_index -> original_index) for constant pool entries |
duke@435 | 38 | // that are referred to by the interpreter at runtime via the constant pool cache. |
jrose@1161 | 39 | // Also computes a CP map (original_index -> new_index). |
jrose@1161 | 40 | // Marks entries in CP which require additional processing. |
jrose@1161 | 41 | void Rewriter::compute_index_maps() { |
jrose@1161 | 42 | const int length = _pool->length(); |
jrose@1161 | 43 | init_cp_map(length); |
jrose@2015 | 44 | jint tag_mask = 0; |
duke@435 | 45 | for (int i = 0; i < length; i++) { |
jrose@1161 | 46 | int tag = _pool->tag_at(i).value(); |
jrose@2015 | 47 | tag_mask |= (1 << tag); |
jrose@1161 | 48 | switch (tag) { |
jrose@1161 | 49 | case JVM_CONSTANT_InterfaceMethodref: |
duke@435 | 50 | case JVM_CONSTANT_Fieldref : // fall through |
duke@435 | 51 | case JVM_CONSTANT_Methodref : // fall through |
jrose@1957 | 52 | case JVM_CONSTANT_MethodHandle : // fall through |
jrose@1957 | 53 | case JVM_CONSTANT_MethodType : // fall through |
jrose@2015 | 54 | case JVM_CONSTANT_InvokeDynamic : // fall through |
jrose@1161 | 55 | add_cp_cache_entry(i); |
jrose@1161 | 56 | break; |
duke@435 | 57 | } |
duke@435 | 58 | } |
jrose@1161 | 59 | |
jrose@1161 | 60 | guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1), |
jrose@1161 | 61 | "all cp cache indexes fit in a u2"); |
jrose@2015 | 62 | |
jrose@2015 | 63 | _have_invoke_dynamic = ((tag_mask & (1 << JVM_CONSTANT_InvokeDynamic)) != 0); |
duke@435 | 64 | } |
duke@435 | 65 | |
coleenp@2945 | 66 | // Unrewrite the bytecodes if an error occurs. |
coleenp@2945 | 67 | void Rewriter::restore_bytecodes() { |
coleenp@2945 | 68 | int len = _methods->length(); |
coleenp@2945 | 69 | |
coleenp@2945 | 70 | for (int i = len-1; i >= 0; i--) { |
coleenp@2945 | 71 | methodOop method = (methodOop)_methods->obj_at(i); |
coleenp@2945 | 72 | scan_method(method, true); |
coleenp@2945 | 73 | } |
coleenp@2945 | 74 | } |
duke@435 | 75 | |
jrose@1161 | 76 | // Creates a constant pool cache given a CPC map |
jrose@1161 | 77 | void Rewriter::make_constant_pool_cache(TRAPS) { |
jrose@1161 | 78 | const int length = _cp_cache_map.length(); |
jrose@1161 | 79 | constantPoolCacheOop cache = |
ysr@2533 | 80 | oopFactory::new_constantPoolCache(length, CHECK); |
ysr@2533 | 81 | No_Safepoint_Verifier nsv; |
jrose@1161 | 82 | cache->initialize(_cp_cache_map); |
jrose@2015 | 83 | |
jrose@2353 | 84 | // Don't bother with the next pass if there is no JVM_CONSTANT_InvokeDynamic. |
jrose@2015 | 85 | if (_have_invoke_dynamic) { |
jrose@2015 | 86 | for (int i = 0; i < length; i++) { |
jrose@2015 | 87 | int pool_index = cp_cache_entry_pool_index(i); |
jrose@2015 | 88 | if (pool_index >= 0 && |
jrose@2015 | 89 | _pool->tag_at(pool_index).is_invoke_dynamic()) { |
jrose@2015 | 90 | int bsm_index = _pool->invoke_dynamic_bootstrap_method_ref_index_at(pool_index); |
jrose@2742 | 91 | assert(_pool->tag_at(bsm_index).is_method_handle(), "must be a MH constant"); |
jrose@2742 | 92 | // There is a CP cache entry holding the BSM for these calls. |
jrose@2742 | 93 | int bsm_cache_index = cp_entry_to_cp_cache(bsm_index); |
jrose@2742 | 94 | cache->entry_at(i)->initialize_bootstrap_method_index_in_cache(bsm_cache_index); |
jrose@2015 | 95 | } |
jrose@2015 | 96 | } |
jrose@2015 | 97 | } |
jrose@2015 | 98 | |
jrose@1161 | 99 | _pool->set_cache(cache); |
jrose@1161 | 100 | cache->set_constant_pool(_pool()); |
duke@435 | 101 | } |
duke@435 | 102 | |
duke@435 | 103 | |
duke@435 | 104 | |
duke@435 | 105 | // The new finalization semantics says that registration of |
duke@435 | 106 | // finalizable objects must be performed on successful return from the |
duke@435 | 107 | // Object.<init> constructor. We could implement this trivially if |
duke@435 | 108 | // <init> were never rewritten but since JVMTI allows this to occur, a |
duke@435 | 109 | // more complicated solution is required. A special return bytecode |
duke@435 | 110 | // is used only by Object.<init> to signal the finalization |
duke@435 | 111 | // registration point. Additionally local 0 must be preserved so it's |
duke@435 | 112 | // available to pass to the registration function. For simplicty we |
duke@435 | 113 | // require that local 0 is never overwritten so it's available as an |
duke@435 | 114 | // argument for registration. |
duke@435 | 115 | |
duke@435 | 116 | void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) { |
duke@435 | 117 | RawBytecodeStream bcs(method); |
duke@435 | 118 | while (!bcs.is_last_bytecode()) { |
duke@435 | 119 | Bytecodes::Code opcode = bcs.raw_next(); |
duke@435 | 120 | switch (opcode) { |
duke@435 | 121 | case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break; |
duke@435 | 122 | |
duke@435 | 123 | case Bytecodes::_istore: |
duke@435 | 124 | case Bytecodes::_lstore: |
duke@435 | 125 | case Bytecodes::_fstore: |
duke@435 | 126 | case Bytecodes::_dstore: |
duke@435 | 127 | case Bytecodes::_astore: |
duke@435 | 128 | if (bcs.get_index() != 0) continue; |
duke@435 | 129 | |
duke@435 | 130 | // fall through |
duke@435 | 131 | case Bytecodes::_istore_0: |
duke@435 | 132 | case Bytecodes::_lstore_0: |
duke@435 | 133 | case Bytecodes::_fstore_0: |
duke@435 | 134 | case Bytecodes::_dstore_0: |
duke@435 | 135 | case Bytecodes::_astore_0: |
duke@435 | 136 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), |
duke@435 | 137 | "can't overwrite local 0 in Object.<init>"); |
duke@435 | 138 | break; |
duke@435 | 139 | } |
duke@435 | 140 | } |
duke@435 | 141 | } |
duke@435 | 142 | |
duke@435 | 143 | |
jrose@1161 | 144 | // Rewrite a classfile-order CP index into a native-order CPC index. |
coleenp@2945 | 145 | void Rewriter::rewrite_member_reference(address bcp, int offset, bool reverse) { |
jrose@1161 | 146 | address p = bcp + offset; |
coleenp@2945 | 147 | if (!reverse) { |
coleenp@2945 | 148 | int cp_index = Bytes::get_Java_u2(p); |
coleenp@2945 | 149 | int cache_index = cp_entry_to_cp_cache(cp_index); |
coleenp@2945 | 150 | Bytes::put_native_u2(p, cache_index); |
coleenp@2945 | 151 | } else { |
coleenp@2945 | 152 | int cache_index = Bytes::get_native_u2(p); |
coleenp@2945 | 153 | int pool_index = cp_cache_entry_pool_index(cache_index); |
coleenp@2945 | 154 | Bytes::put_Java_u2(p, pool_index); |
coleenp@2945 | 155 | } |
jrose@1161 | 156 | } |
jrose@1161 | 157 | |
jrose@1161 | 158 | |
coleenp@2945 | 159 | void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) { |
jrose@1161 | 160 | address p = bcp + offset; |
coleenp@2945 | 161 | assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode"); |
coleenp@2945 | 162 | if (!reverse) { |
coleenp@2945 | 163 | int cp_index = Bytes::get_Java_u2(p); |
coleenp@2945 | 164 | int cpc = maybe_add_cp_cache_entry(cp_index); // add lazily |
coleenp@2945 | 165 | int cpc2 = add_secondary_cp_cache_entry(cpc); |
jrose@1161 | 166 | |
coleenp@2945 | 167 | // Replace the trailing four bytes with a CPC index for the dynamic |
coleenp@2945 | 168 | // call site. Unlike other CPC entries, there is one per bytecode, |
coleenp@2945 | 169 | // not just one per distinct CP entry. In other words, the |
coleenp@2945 | 170 | // CPC-to-CP relation is many-to-one for invokedynamic entries. |
coleenp@2945 | 171 | // This means we must use a larger index size than u2 to address |
coleenp@2945 | 172 | // all these entries. That is the main reason invokedynamic |
coleenp@2945 | 173 | // must have a five-byte instruction format. (Of course, other JVM |
coleenp@2945 | 174 | // implementations can use the bytes for other purposes.) |
coleenp@2945 | 175 | Bytes::put_native_u4(p, constantPoolCacheOopDesc::encode_secondary_index(cpc2)); |
coleenp@2945 | 176 | // Note: We use native_u4 format exclusively for 4-byte indexes. |
coleenp@2945 | 177 | } else { |
coleenp@2945 | 178 | int cache_index = constantPoolCacheOopDesc::decode_secondary_index( |
coleenp@2945 | 179 | Bytes::get_native_u4(p)); |
coleenp@2945 | 180 | int secondary_index = cp_cache_secondary_entry_main_index(cache_index); |
coleenp@2945 | 181 | int pool_index = cp_cache_entry_pool_index(secondary_index); |
coleenp@2945 | 182 | assert(_pool->tag_at(pool_index).is_invoke_dynamic(), "wrong index"); |
coleenp@2945 | 183 | // zero out 4 bytes |
coleenp@2945 | 184 | Bytes::put_Java_u4(p, 0); |
coleenp@2945 | 185 | Bytes::put_Java_u2(p, pool_index); |
coleenp@2945 | 186 | } |
jrose@1161 | 187 | } |
jrose@1161 | 188 | |
jrose@1161 | 189 | |
jrose@1957 | 190 | // Rewrite some ldc bytecodes to _fast_aldc |
coleenp@2945 | 191 | void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide, |
coleenp@2945 | 192 | bool reverse) { |
coleenp@2945 | 193 | if (!reverse) { |
coleenp@2945 | 194 | assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), "not ldc bytecode"); |
coleenp@2945 | 195 | address p = bcp + offset; |
coleenp@2945 | 196 | int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p); |
coleenp@2945 | 197 | constantTag tag = _pool->tag_at(cp_index).value(); |
coleenp@2945 | 198 | if (tag.is_method_handle() || tag.is_method_type()) { |
coleenp@2945 | 199 | int cache_index = cp_entry_to_cp_cache(cp_index); |
coleenp@2945 | 200 | if (is_wide) { |
coleenp@2945 | 201 | (*bcp) = Bytecodes::_fast_aldc_w; |
coleenp@2945 | 202 | assert(cache_index == (u2)cache_index, "index overflow"); |
coleenp@2945 | 203 | Bytes::put_native_u2(p, cache_index); |
coleenp@2945 | 204 | } else { |
coleenp@2945 | 205 | (*bcp) = Bytecodes::_fast_aldc; |
coleenp@2945 | 206 | assert(cache_index == (u1)cache_index, "index overflow"); |
coleenp@2945 | 207 | (*p) = (u1)cache_index; |
coleenp@2945 | 208 | } |
coleenp@2945 | 209 | } |
coleenp@2945 | 210 | } else { |
coleenp@2945 | 211 | Bytecodes::Code rewritten_bc = |
coleenp@2945 | 212 | (is_wide ? Bytecodes::_fast_aldc_w : Bytecodes::_fast_aldc); |
coleenp@2945 | 213 | if ((*bcp) == rewritten_bc) { |
coleenp@2945 | 214 | address p = bcp + offset; |
coleenp@2945 | 215 | int cache_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p); |
coleenp@2945 | 216 | int pool_index = cp_cache_entry_pool_index(cache_index); |
coleenp@2945 | 217 | if (is_wide) { |
coleenp@2945 | 218 | (*bcp) = Bytecodes::_ldc_w; |
coleenp@2945 | 219 | assert(pool_index == (u2)pool_index, "index overflow"); |
coleenp@2945 | 220 | Bytes::put_Java_u2(p, pool_index); |
coleenp@2945 | 221 | } else { |
coleenp@2945 | 222 | (*bcp) = Bytecodes::_ldc; |
coleenp@2945 | 223 | assert(pool_index == (u1)pool_index, "index overflow"); |
coleenp@2945 | 224 | (*p) = (u1)pool_index; |
coleenp@2945 | 225 | } |
jrose@1957 | 226 | } |
jrose@1957 | 227 | } |
jrose@1957 | 228 | } |
jrose@1957 | 229 | |
jrose@1957 | 230 | |
duke@435 | 231 | // Rewrites a method given the index_map information |
coleenp@2945 | 232 | void Rewriter::scan_method(methodOop method, bool reverse) { |
duke@435 | 233 | |
duke@435 | 234 | int nof_jsrs = 0; |
duke@435 | 235 | bool has_monitor_bytecodes = false; |
duke@435 | 236 | |
duke@435 | 237 | { |
duke@435 | 238 | // We cannot tolerate a GC in this block, because we've |
duke@435 | 239 | // cached the bytecodes in 'code_base'. If the methodOop |
duke@435 | 240 | // moves, the bytecodes will also move. |
duke@435 | 241 | No_Safepoint_Verifier nsv; |
duke@435 | 242 | Bytecodes::Code c; |
duke@435 | 243 | |
duke@435 | 244 | // Bytecodes and their length |
duke@435 | 245 | const address code_base = method->code_base(); |
duke@435 | 246 | const int code_length = method->code_size(); |
duke@435 | 247 | |
duke@435 | 248 | int bc_length; |
duke@435 | 249 | for (int bci = 0; bci < code_length; bci += bc_length) { |
duke@435 | 250 | address bcp = code_base + bci; |
jrose@1161 | 251 | int prefix_length = 0; |
duke@435 | 252 | c = (Bytecodes::Code)(*bcp); |
duke@435 | 253 | |
duke@435 | 254 | // Since we have the code, see if we can get the length |
duke@435 | 255 | // directly. Some more complicated bytecodes will report |
duke@435 | 256 | // a length of zero, meaning we need to make another method |
duke@435 | 257 | // call to calculate the length. |
duke@435 | 258 | bc_length = Bytecodes::length_for(c); |
duke@435 | 259 | if (bc_length == 0) { |
never@2462 | 260 | bc_length = Bytecodes::length_at(method, bcp); |
duke@435 | 261 | |
duke@435 | 262 | // length_at will put us at the bytecode after the one modified |
duke@435 | 263 | // by 'wide'. We don't currently examine any of the bytecodes |
duke@435 | 264 | // modified by wide, but in case we do in the future... |
duke@435 | 265 | if (c == Bytecodes::_wide) { |
jrose@1161 | 266 | prefix_length = 1; |
duke@435 | 267 | c = (Bytecodes::Code)bcp[1]; |
duke@435 | 268 | } |
duke@435 | 269 | } |
duke@435 | 270 | |
duke@435 | 271 | assert(bc_length != 0, "impossible bytecode length"); |
duke@435 | 272 | |
duke@435 | 273 | switch (c) { |
duke@435 | 274 | case Bytecodes::_lookupswitch : { |
duke@435 | 275 | #ifndef CC_INTERP |
never@2462 | 276 | Bytecode_lookupswitch bc(method, bcp); |
jrose@1920 | 277 | (*bcp) = ( |
never@2462 | 278 | bc.number_of_pairs() < BinarySwitchThreshold |
duke@435 | 279 | ? Bytecodes::_fast_linearswitch |
duke@435 | 280 | : Bytecodes::_fast_binaryswitch |
duke@435 | 281 | ); |
duke@435 | 282 | #endif |
duke@435 | 283 | break; |
duke@435 | 284 | } |
coleenp@2945 | 285 | case Bytecodes::_fast_linearswitch: |
coleenp@2945 | 286 | case Bytecodes::_fast_binaryswitch: { |
coleenp@2945 | 287 | #ifndef CC_INTERP |
coleenp@2945 | 288 | (*bcp) = Bytecodes::_lookupswitch; |
coleenp@2945 | 289 | #endif |
coleenp@2945 | 290 | break; |
coleenp@2945 | 291 | } |
duke@435 | 292 | case Bytecodes::_getstatic : // fall through |
duke@435 | 293 | case Bytecodes::_putstatic : // fall through |
duke@435 | 294 | case Bytecodes::_getfield : // fall through |
duke@435 | 295 | case Bytecodes::_putfield : // fall through |
duke@435 | 296 | case Bytecodes::_invokevirtual : // fall through |
duke@435 | 297 | case Bytecodes::_invokespecial : // fall through |
jrose@1161 | 298 | case Bytecodes::_invokestatic : |
jrose@1161 | 299 | case Bytecodes::_invokeinterface: |
coleenp@2945 | 300 | rewrite_member_reference(bcp, prefix_length+1, reverse); |
duke@435 | 301 | break; |
jrose@1161 | 302 | case Bytecodes::_invokedynamic: |
coleenp@2945 | 303 | rewrite_invokedynamic(bcp, prefix_length+1, reverse); |
jrose@1161 | 304 | break; |
jrose@1957 | 305 | case Bytecodes::_ldc: |
coleenp@2945 | 306 | case Bytecodes::_fast_aldc: |
coleenp@2945 | 307 | maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse); |
jrose@1957 | 308 | break; |
jrose@1957 | 309 | case Bytecodes::_ldc_w: |
coleenp@2945 | 310 | case Bytecodes::_fast_aldc_w: |
coleenp@2945 | 311 | maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse); |
jrose@1957 | 312 | break; |
duke@435 | 313 | case Bytecodes::_jsr : // fall through |
duke@435 | 314 | case Bytecodes::_jsr_w : nof_jsrs++; break; |
duke@435 | 315 | case Bytecodes::_monitorenter : // fall through |
duke@435 | 316 | case Bytecodes::_monitorexit : has_monitor_bytecodes = true; break; |
duke@435 | 317 | } |
duke@435 | 318 | } |
duke@435 | 319 | } |
duke@435 | 320 | |
duke@435 | 321 | // Update access flags |
duke@435 | 322 | if (has_monitor_bytecodes) { |
duke@435 | 323 | method->set_has_monitor_bytecodes(); |
duke@435 | 324 | } |
duke@435 | 325 | |
duke@435 | 326 | // The present of a jsr bytecode implies that the method might potentially |
duke@435 | 327 | // have to be rewritten, so we run the oopMapGenerator on the method |
duke@435 | 328 | if (nof_jsrs > 0) { |
duke@435 | 329 | method->set_has_jsrs(); |
jrose@1161 | 330 | // Second pass will revisit this method. |
coleenp@2945 | 331 | assert(method->has_jsrs(), "didn't we just set this?"); |
jrose@1161 | 332 | } |
jrose@1161 | 333 | } |
duke@435 | 334 | |
jrose@1161 | 335 | // After constant pool is created, revisit methods containing jsrs. |
jrose@1161 | 336 | methodHandle Rewriter::rewrite_jsrs(methodHandle method, TRAPS) { |
coleenp@2945 | 337 | ResourceMark rm(THREAD); |
jrose@1161 | 338 | ResolveOopMapConflicts romc(method); |
jrose@1161 | 339 | methodHandle original_method = method; |
jrose@1161 | 340 | method = romc.do_potential_rewrite(CHECK_(methodHandle())); |
jrose@1161 | 341 | if (method() != original_method()) { |
jrose@1161 | 342 | // Insert invalid bytecode into original methodOop and set |
jrose@1161 | 343 | // interpreter entrypoint, so that a executing this method |
jrose@1161 | 344 | // will manifest itself in an easy recognizable form. |
jrose@1161 | 345 | address bcp = original_method->bcp_from(0); |
jrose@1161 | 346 | *bcp = (u1)Bytecodes::_shouldnotreachhere; |
jrose@1161 | 347 | int kind = Interpreter::method_kind(original_method); |
jrose@1161 | 348 | original_method->set_interpreter_kind(kind); |
duke@435 | 349 | } |
duke@435 | 350 | |
jrose@1161 | 351 | // Update monitor matching info. |
jrose@1161 | 352 | if (romc.monitor_safe()) { |
jrose@1161 | 353 | method->set_guaranteed_monitor_matching(); |
jrose@1161 | 354 | } |
duke@435 | 355 | |
duke@435 | 356 | return method; |
duke@435 | 357 | } |
duke@435 | 358 | |
duke@435 | 359 | void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) { |
duke@435 | 360 | ResourceMark rm(THREAD); |
twisti@1573 | 361 | Rewriter rw(klass, klass->constants(), klass->methods(), CHECK); |
jrose@1161 | 362 | // (That's all, folks.) |
jrose@1161 | 363 | } |
jrose@1161 | 364 | |
twisti@1573 | 365 | |
twisti@1573 | 366 | void Rewriter::rewrite(instanceKlassHandle klass, constantPoolHandle cpool, objArrayHandle methods, TRAPS) { |
twisti@1573 | 367 | ResourceMark rm(THREAD); |
twisti@1573 | 368 | Rewriter rw(klass, cpool, methods, CHECK); |
twisti@1573 | 369 | // (That's all, folks.) |
twisti@1573 | 370 | } |
twisti@1573 | 371 | |
twisti@1573 | 372 | |
twisti@1573 | 373 | Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, objArrayHandle methods, TRAPS) |
jrose@1161 | 374 | : _klass(klass), |
twisti@1573 | 375 | _pool(cpool), |
twisti@1573 | 376 | _methods(methods) |
jrose@1161 | 377 | { |
jrose@1161 | 378 | assert(_pool->cache() == NULL, "constant pool cache must not be set yet"); |
duke@435 | 379 | |
duke@435 | 380 | // determine index maps for methodOop rewriting |
jrose@1161 | 381 | compute_index_maps(); |
duke@435 | 382 | |
jrose@1161 | 383 | if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) { |
jrose@1291 | 384 | bool did_rewrite = false; |
jrose@1161 | 385 | int i = _methods->length(); |
duke@435 | 386 | while (i-- > 0) { |
jrose@1161 | 387 | methodOop method = (methodOop)_methods->obj_at(i); |
duke@435 | 388 | if (method->intrinsic_id() == vmIntrinsics::_Object_init) { |
duke@435 | 389 | // rewrite the return bytecodes of Object.<init> to register the |
duke@435 | 390 | // object for finalization if needed. |
duke@435 | 391 | methodHandle m(THREAD, method); |
duke@435 | 392 | rewrite_Object_init(m, CHECK); |
jrose@1291 | 393 | did_rewrite = true; |
duke@435 | 394 | break; |
duke@435 | 395 | } |
duke@435 | 396 | } |
jrose@1291 | 397 | assert(did_rewrite, "must find Object::<init> to rewrite it"); |
duke@435 | 398 | } |
duke@435 | 399 | |
jrose@1161 | 400 | // rewrite methods, in two passes |
coleenp@2945 | 401 | int len = _methods->length(); |
jrose@1161 | 402 | |
coleenp@2945 | 403 | for (int i = len-1; i >= 0; i--) { |
jrose@1161 | 404 | methodOop method = (methodOop)_methods->obj_at(i); |
jrose@1161 | 405 | scan_method(method); |
jrose@1161 | 406 | } |
jrose@1161 | 407 | |
jrose@1161 | 408 | // allocate constant pool cache, now that we've seen all the bytecodes |
coleenp@2945 | 409 | make_constant_pool_cache(THREAD); |
jrose@1161 | 410 | |
coleenp@2945 | 411 | // Restore bytecodes to their unrewritten state if there are exceptions |
coleenp@2945 | 412 | // rewriting bytecodes or allocating the cpCache |
coleenp@2945 | 413 | if (HAS_PENDING_EXCEPTION) { |
coleenp@2945 | 414 | restore_bytecodes(); |
coleenp@2945 | 415 | return; |
coleenp@2945 | 416 | } |
coleenp@2945 | 417 | } |
coleenp@2945 | 418 | |
coleenp@2945 | 419 | // Relocate jsr/rets in a method. This can't be done with the rewriter |
coleenp@2945 | 420 | // stage because it can throw other exceptions, leaving the bytecodes |
coleenp@2945 | 421 | // pointing at constant pool cache entries. |
coleenp@2945 | 422 | // Link and check jvmti dependencies while we're iterating over the methods. |
coleenp@2945 | 423 | // JSR292 code calls with a different set of methods, so two entry points. |
coleenp@2945 | 424 | void Rewriter::relocate_and_link(instanceKlassHandle this_oop, TRAPS) { |
coleenp@2945 | 425 | objArrayHandle methods(THREAD, this_oop->methods()); |
coleenp@2945 | 426 | relocate_and_link(this_oop, methods, THREAD); |
coleenp@2945 | 427 | } |
coleenp@2945 | 428 | |
coleenp@2945 | 429 | void Rewriter::relocate_and_link(instanceKlassHandle this_oop, |
coleenp@2945 | 430 | objArrayHandle methods, TRAPS) { |
coleenp@2945 | 431 | int len = methods->length(); |
coleenp@2945 | 432 | for (int i = len-1; i >= 0; i--) { |
coleenp@2945 | 433 | methodHandle m(THREAD, (methodOop)methods->obj_at(i)); |
jrose@1161 | 434 | |
jrose@1161 | 435 | if (m->has_jsrs()) { |
jrose@1161 | 436 | m = rewrite_jsrs(m, CHECK); |
duke@435 | 437 | // Method might have gotten rewritten. |
coleenp@2945 | 438 | methods->obj_at_put(i, m()); |
duke@435 | 439 | } |
jrose@1161 | 440 | |
coleenp@2945 | 441 | // Set up method entry points for compiler and interpreter . |
jrose@1161 | 442 | m->link_method(m, CHECK); |
jrose@1929 | 443 | |
coleenp@2945 | 444 | // This is for JVMTI and unrelated to relocator but the last thing we do |
jrose@1929 | 445 | #ifdef ASSERT |
jrose@1929 | 446 | if (StressMethodComparator) { |
jrose@1929 | 447 | static int nmc = 0; |
jrose@1929 | 448 | for (int j = i; j >= 0 && j >= i-4; j--) { |
jrose@1929 | 449 | if ((++nmc % 1000) == 0) tty->print_cr("Have run MethodComparator %d times...", nmc); |
coleenp@2945 | 450 | bool z = MethodComparator::methods_EMCP(m(), |
coleenp@2945 | 451 | (methodOop)methods->obj_at(j)); |
jrose@1929 | 452 | if (j == i && !z) { |
jrose@1929 | 453 | tty->print("MethodComparator FAIL: "); m->print(); m->print_codes(); |
jrose@1929 | 454 | assert(z, "method must compare equal to itself"); |
jrose@1929 | 455 | } |
jrose@1929 | 456 | } |
jrose@1929 | 457 | } |
jrose@1929 | 458 | #endif //ASSERT |
duke@435 | 459 | } |
duke@435 | 460 | } |