1.1 --- a/src/share/vm/interpreter/rewriter.cpp Fri Aug 31 16:39:35 2012 -0700 1.2 +++ b/src/share/vm/interpreter/rewriter.cpp Sat Sep 01 13:25:18 2012 -0400 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -27,6 +27,7 @@ 1.11 #include "interpreter/interpreter.hpp" 1.12 #include "interpreter/rewriter.hpp" 1.13 #include "memory/gcLocker.hpp" 1.14 +#include "memory/metadataFactory.hpp" 1.15 #include "memory/oopFactory.hpp" 1.16 #include "memory/resourceArea.hpp" 1.17 #include "oops/generateOopMap.hpp" 1.18 @@ -41,7 +42,7 @@ 1.19 // Marks entries in CP which require additional processing. 1.20 void Rewriter::compute_index_maps() { 1.21 const int length = _pool->length(); 1.22 - init_cp_map(length); 1.23 + init_maps(length); 1.24 bool saw_mh_symbol = false; 1.25 for (int i = 0; i < length; i++) { 1.26 int tag = _pool->tag_at(i).value(); 1.27 @@ -49,10 +50,13 @@ 1.28 case JVM_CONSTANT_InterfaceMethodref: 1.29 case JVM_CONSTANT_Fieldref : // fall through 1.30 case JVM_CONSTANT_Methodref : // fall through 1.31 + add_cp_cache_entry(i); 1.32 + break; 1.33 + case JVM_CONSTANT_String: 1.34 + case JVM_CONSTANT_Object: 1.35 case JVM_CONSTANT_MethodHandle : // fall through 1.36 case JVM_CONSTANT_MethodType : // fall through 1.37 - case JVM_CONSTANT_InvokeDynamic : // fall through 1.38 - add_cp_cache_entry(i); 1.39 + add_resolved_references_entry(i); 1.40 break; 1.41 case JVM_CONSTANT_Utf8: 1.42 if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle()) 1.43 @@ -61,6 +65,9 @@ 1.44 } 1.45 } 1.46 1.47 + // Record limits of resolved reference map for constant pool cache indices 1.48 + record_map_limits(); 1.49 + 1.50 guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1), 1.51 "all cp cache indexes fit in a u2"); 1.52 1.53 @@ -73,7 +80,7 @@ 1.54 int len = _methods->length(); 1.55 1.56 for (int i = len-1; i >= 0; i--) { 1.57 - methodOop method = (methodOop)_methods->obj_at(i); 1.58 + Method* method = _methods->at(i); 1.59 scan_method(method, true); 1.60 } 1.61 } 1.62 @@ -81,10 +88,17 @@ 1.63 // Creates a constant pool cache given a CPC map 1.64 void Rewriter::make_constant_pool_cache(TRAPS) { 1.65 const int length = _cp_cache_map.length(); 1.66 - constantPoolCacheOop cache = 1.67 - oopFactory::new_constantPoolCache(length, CHECK); 1.68 + ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data(); 1.69 + ConstantPoolCache* cache = 1.70 + ConstantPoolCache::allocate(loader_data, length, CHECK); 1.71 + 1.72 + // initialize object cache in constant pool 1.73 + _pool->initialize_resolved_references(loader_data, _resolved_references_map, 1.74 + _resolved_reference_limit, 1.75 + CHECK); 1.76 + 1.77 No_Safepoint_Verifier nsv; 1.78 - cache->initialize(_cp_cache_map); 1.79 + cache->initialize(_cp_cache_map, _invokedynamic_references_map); 1.80 _pool->set_cache(cache); 1.81 cache->set_constant_pool(_pool()); 1.82 } 1.83 @@ -138,19 +152,19 @@ 1.84 int cache_index = cp_entry_to_cp_cache(cp_index); 1.85 Bytes::put_native_u2(p, cache_index); 1.86 if (!_method_handle_invokers.is_empty()) 1.87 - maybe_rewrite_invokehandle(p - 1, cp_index, reverse); 1.88 + maybe_rewrite_invokehandle(p - 1, cp_index, cache_index, reverse); 1.89 } else { 1.90 int cache_index = Bytes::get_native_u2(p); 1.91 int pool_index = cp_cache_entry_pool_index(cache_index); 1.92 Bytes::put_Java_u2(p, pool_index); 1.93 if (!_method_handle_invokers.is_empty()) 1.94 - maybe_rewrite_invokehandle(p - 1, pool_index, reverse); 1.95 + maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse); 1.96 } 1.97 } 1.98 1.99 1.100 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.) 1.101 -void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, bool reverse) { 1.102 +void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) { 1.103 if (!reverse) { 1.104 if ((*opc) == (u1)Bytecodes::_invokevirtual || 1.105 // allow invokespecial as an alias, although it would be very odd: 1.106 @@ -163,10 +177,13 @@ 1.107 if (status == 0) { 1.108 if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() && 1.109 MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(), 1.110 - _pool->name_ref_at(cp_index))) 1.111 + _pool->name_ref_at(cp_index))) { 1.112 + // we may need a resolved_refs entry for the appendix 1.113 + add_invokedynamic_resolved_references_entry(cp_index, cache_index); 1.114 status = +1; 1.115 - else 1.116 + } else { 1.117 status = -1; 1.118 + } 1.119 _method_handle_invokers[cp_index] = status; 1.120 } 1.121 // We use a special internal bytecode for such methods (if non-static). 1.122 @@ -193,9 +210,8 @@ 1.123 assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode"); 1.124 if (!reverse) { 1.125 int cp_index = Bytes::get_Java_u2(p); 1.126 - int cpc = maybe_add_cp_cache_entry(cp_index); // add lazily 1.127 - int cpc2 = add_secondary_cp_cache_entry(cpc); 1.128 - 1.129 + int cache_index = add_invokedynamic_cp_cache_entry(cp_index); 1.130 + add_invokedynamic_resolved_references_entry(cp_index, cache_index); 1.131 // Replace the trailing four bytes with a CPC index for the dynamic 1.132 // call site. Unlike other CPC entries, there is one per bytecode, 1.133 // not just one per distinct CP entry. In other words, the 1.134 @@ -204,17 +220,17 @@ 1.135 // all these entries. That is the main reason invokedynamic 1.136 // must have a five-byte instruction format. (Of course, other JVM 1.137 // implementations can use the bytes for other purposes.) 1.138 - Bytes::put_native_u4(p, constantPoolCacheOopDesc::encode_secondary_index(cpc2)); 1.139 + Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index)); 1.140 // Note: We use native_u4 format exclusively for 4-byte indexes. 1.141 } else { 1.142 - int cache_index = constantPoolCacheOopDesc::decode_secondary_index( 1.143 + // callsite index 1.144 + int cache_index = ConstantPool::decode_invokedynamic_index( 1.145 Bytes::get_native_u4(p)); 1.146 - int secondary_index = cp_cache_secondary_entry_main_index(cache_index); 1.147 - int pool_index = cp_cache_entry_pool_index(secondary_index); 1.148 - assert(_pool->tag_at(pool_index).is_invoke_dynamic(), "wrong index"); 1.149 + int cp_index = cp_cache_entry_pool_index(cache_index); 1.150 + assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index"); 1.151 // zero out 4 bytes 1.152 Bytes::put_Java_u4(p, 0); 1.153 - Bytes::put_Java_u2(p, pool_index); 1.154 + Bytes::put_Java_u2(p, cp_index); 1.155 } 1.156 } 1.157 1.158 @@ -227,16 +243,16 @@ 1.159 address p = bcp + offset; 1.160 int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p); 1.161 constantTag tag = _pool->tag_at(cp_index).value(); 1.162 - if (tag.is_method_handle() || tag.is_method_type()) { 1.163 - int cache_index = cp_entry_to_cp_cache(cp_index); 1.164 + if (tag.is_method_handle() || tag.is_method_type() || tag.is_string() || tag.is_object()) { 1.165 + int ref_index = cp_entry_to_resolved_references(cp_index); 1.166 if (is_wide) { 1.167 (*bcp) = Bytecodes::_fast_aldc_w; 1.168 - assert(cache_index == (u2)cache_index, "index overflow"); 1.169 - Bytes::put_native_u2(p, cache_index); 1.170 + assert(ref_index == (u2)ref_index, "index overflow"); 1.171 + Bytes::put_native_u2(p, ref_index); 1.172 } else { 1.173 (*bcp) = Bytecodes::_fast_aldc; 1.174 - assert(cache_index == (u1)cache_index, "index overflow"); 1.175 - (*p) = (u1)cache_index; 1.176 + assert(ref_index == (u1)ref_index, "index overflow"); 1.177 + (*p) = (u1)ref_index; 1.178 } 1.179 } 1.180 } else { 1.181 @@ -244,8 +260,8 @@ 1.182 (is_wide ? Bytecodes::_fast_aldc_w : Bytecodes::_fast_aldc); 1.183 if ((*bcp) == rewritten_bc) { 1.184 address p = bcp + offset; 1.185 - int cache_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p); 1.186 - int pool_index = cp_cache_entry_pool_index(cache_index); 1.187 + int ref_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p); 1.188 + int pool_index = resolved_references_entry_to_pool_index(ref_index); 1.189 if (is_wide) { 1.190 (*bcp) = Bytecodes::_ldc_w; 1.191 assert(pool_index == (u2)pool_index, "index overflow"); 1.192 @@ -261,14 +277,14 @@ 1.193 1.194 1.195 // Rewrites a method given the index_map information 1.196 -void Rewriter::scan_method(methodOop method, bool reverse) { 1.197 +void Rewriter::scan_method(Method* method, bool reverse) { 1.198 1.199 int nof_jsrs = 0; 1.200 bool has_monitor_bytecodes = false; 1.201 1.202 { 1.203 // We cannot tolerate a GC in this block, because we've 1.204 - // cached the bytecodes in 'code_base'. If the methodOop 1.205 + // cached the bytecodes in 'code_base'. If the Method* 1.206 // moves, the bytecodes will also move. 1.207 No_Safepoint_Verifier nsv; 1.208 Bytecodes::Code c; 1.209 @@ -371,16 +387,6 @@ 1.210 ResolveOopMapConflicts romc(method); 1.211 methodHandle original_method = method; 1.212 method = romc.do_potential_rewrite(CHECK_(methodHandle())); 1.213 - if (method() != original_method()) { 1.214 - // Insert invalid bytecode into original methodOop and set 1.215 - // interpreter entrypoint, so that a executing this method 1.216 - // will manifest itself in an easy recognizable form. 1.217 - address bcp = original_method->bcp_from(0); 1.218 - *bcp = (u1)Bytecodes::_shouldnotreachhere; 1.219 - int kind = Interpreter::method_kind(original_method); 1.220 - original_method->set_interpreter_kind(kind); 1.221 - } 1.222 - 1.223 // Update monitor matching info. 1.224 if (romc.monitor_safe()) { 1.225 method->set_guaranteed_monitor_matching(); 1.226 @@ -396,28 +402,28 @@ 1.227 } 1.228 1.229 1.230 -void Rewriter::rewrite(instanceKlassHandle klass, constantPoolHandle cpool, objArrayHandle methods, TRAPS) { 1.231 +void Rewriter::rewrite(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS) { 1.232 ResourceMark rm(THREAD); 1.233 Rewriter rw(klass, cpool, methods, CHECK); 1.234 // (That's all, folks.) 1.235 } 1.236 1.237 1.238 -Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, objArrayHandle methods, TRAPS) 1.239 +Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS) 1.240 : _klass(klass), 1.241 _pool(cpool), 1.242 _methods(methods) 1.243 { 1.244 assert(_pool->cache() == NULL, "constant pool cache must not be set yet"); 1.245 1.246 - // determine index maps for methodOop rewriting 1.247 + // determine index maps for Method* rewriting 1.248 compute_index_maps(); 1.249 1.250 if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) { 1.251 bool did_rewrite = false; 1.252 int i = _methods->length(); 1.253 while (i-- > 0) { 1.254 - methodOop method = (methodOop)_methods->obj_at(i); 1.255 + Method* method = _methods->at(i); 1.256 if (method->intrinsic_id() == vmIntrinsics::_Object_init) { 1.257 // rewrite the return bytecodes of Object.<init> to register the 1.258 // object for finalization if needed. 1.259 @@ -434,7 +440,7 @@ 1.260 int len = _methods->length(); 1.261 1.262 for (int i = len-1; i >= 0; i--) { 1.263 - methodOop method = (methodOop)_methods->obj_at(i); 1.264 + Method* method = _methods->at(i); 1.265 scan_method(method); 1.266 } 1.267 1.268 @@ -455,20 +461,19 @@ 1.269 // Link and check jvmti dependencies while we're iterating over the methods. 1.270 // JSR292 code calls with a different set of methods, so two entry points. 1.271 void Rewriter::relocate_and_link(instanceKlassHandle this_oop, TRAPS) { 1.272 - objArrayHandle methods(THREAD, this_oop->methods()); 1.273 - relocate_and_link(this_oop, methods, THREAD); 1.274 + relocate_and_link(this_oop, this_oop->methods(), THREAD); 1.275 } 1.276 1.277 void Rewriter::relocate_and_link(instanceKlassHandle this_oop, 1.278 - objArrayHandle methods, TRAPS) { 1.279 + Array<Method*>* methods, TRAPS) { 1.280 int len = methods->length(); 1.281 for (int i = len-1; i >= 0; i--) { 1.282 - methodHandle m(THREAD, (methodOop)methods->obj_at(i)); 1.283 + methodHandle m(THREAD, methods->at(i)); 1.284 1.285 if (m->has_jsrs()) { 1.286 m = rewrite_jsrs(m, CHECK); 1.287 // Method might have gotten rewritten. 1.288 - methods->obj_at_put(i, m()); 1.289 + methods->at_put(i, m()); 1.290 } 1.291 1.292 // Set up method entry points for compiler and interpreter . 1.293 @@ -481,7 +486,7 @@ 1.294 for (int j = i; j >= 0 && j >= i-4; j--) { 1.295 if ((++nmc % 1000) == 0) tty->print_cr("Have run MethodComparator %d times...", nmc); 1.296 bool z = MethodComparator::methods_EMCP(m(), 1.297 - (methodOop)methods->obj_at(j)); 1.298 + methods->at(j)); 1.299 if (j == i && !z) { 1.300 tty->print("MethodComparator FAIL: "); m->print(); m->print_codes(); 1.301 assert(z, "method must compare equal to itself");