comparison src/share/vm/interpreter/rewriter.cpp @ 6275:957c266d8bc5

Merge with http://hg.openjdk.java.net/hsx/hsx24/hotspot/
author Doug Simon <doug.simon@oracle.com>
date Tue, 21 Aug 2012 10:39:19 +0200
parents 723df37192d6 1d7922586cf6
children e522a00b91aa
comparison
equal deleted inserted replaced
5891:fd8832ae511d 6275:957c266d8bc5
31 #include "memory/resourceArea.hpp" 31 #include "memory/resourceArea.hpp"
32 #include "oops/generateOopMap.hpp" 32 #include "oops/generateOopMap.hpp"
33 #include "oops/objArrayOop.hpp" 33 #include "oops/objArrayOop.hpp"
34 #include "oops/oop.inline.hpp" 34 #include "oops/oop.inline.hpp"
35 #include "prims/methodComparator.hpp" 35 #include "prims/methodComparator.hpp"
36 #include "prims/methodHandles.hpp"
36 37
37 // Computes a CPC map (new_index -> original_index) for constant pool entries 38 // Computes a CPC map (new_index -> original_index) for constant pool entries
38 // that are referred to by the interpreter at runtime via the constant pool cache. 39 // that are referred to by the interpreter at runtime via the constant pool cache.
39 // Also computes a CP map (original_index -> new_index). 40 // Also computes a CP map (original_index -> new_index).
40 // Marks entries in CP which require additional processing. 41 // Marks entries in CP which require additional processing.
41 void Rewriter::compute_index_maps() { 42 void Rewriter::compute_index_maps() {
42 const int length = _pool->length(); 43 const int length = _pool->length();
43 init_cp_map(length); 44 init_cp_map(length);
44 jint tag_mask = 0; 45 bool saw_mh_symbol = false;
45 for (int i = 0; i < length; i++) { 46 for (int i = 0; i < length; i++) {
46 int tag = _pool->tag_at(i).value(); 47 int tag = _pool->tag_at(i).value();
47 tag_mask |= (1 << tag);
48 switch (tag) { 48 switch (tag) {
49 case JVM_CONSTANT_InterfaceMethodref: 49 case JVM_CONSTANT_InterfaceMethodref:
50 case JVM_CONSTANT_Fieldref : // fall through 50 case JVM_CONSTANT_Fieldref : // fall through
51 case JVM_CONSTANT_Methodref : // fall through 51 case JVM_CONSTANT_Methodref : // fall through
52 case JVM_CONSTANT_MethodHandle : // fall through 52 case JVM_CONSTANT_MethodHandle : // fall through
53 case JVM_CONSTANT_MethodType : // fall through 53 case JVM_CONSTANT_MethodType : // fall through
54 case JVM_CONSTANT_InvokeDynamic : // fall through 54 case JVM_CONSTANT_InvokeDynamic : // fall through
55 add_cp_cache_entry(i); 55 add_cp_cache_entry(i);
56 break; 56 break;
57 case JVM_CONSTANT_Utf8:
58 if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle())
59 saw_mh_symbol = true;
60 break;
57 } 61 }
58 } 62 }
59 63
60 guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1), 64 guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1),
61 "all cp cache indexes fit in a u2"); 65 "all cp cache indexes fit in a u2");
62 66
63 _have_invoke_dynamic = ((tag_mask & (1 << JVM_CONSTANT_InvokeDynamic)) != 0); 67 if (saw_mh_symbol)
68 _method_handle_invokers.initialize(length, (int)0);
64 } 69 }
65 70
66 // Unrewrite the bytecodes if an error occurs. 71 // Unrewrite the bytecodes if an error occurs.
67 void Rewriter::restore_bytecodes() { 72 void Rewriter::restore_bytecodes() {
68 int len = _methods->length(); 73 int len = _methods->length();
78 const int length = _cp_cache_map.length(); 83 const int length = _cp_cache_map.length();
79 constantPoolCacheOop cache = 84 constantPoolCacheOop cache =
80 oopFactory::new_constantPoolCache(length, CHECK); 85 oopFactory::new_constantPoolCache(length, CHECK);
81 No_Safepoint_Verifier nsv; 86 No_Safepoint_Verifier nsv;
82 cache->initialize(_cp_cache_map); 87 cache->initialize(_cp_cache_map);
83
84 // Don't bother with the next pass if there is no JVM_CONSTANT_InvokeDynamic.
85 if (_have_invoke_dynamic) {
86 for (int i = 0; i < length; i++) {
87 int pool_index = cp_cache_entry_pool_index(i);
88 if (pool_index >= 0 &&
89 _pool->tag_at(pool_index).is_invoke_dynamic()) {
90 int bsm_index = _pool->invoke_dynamic_bootstrap_method_ref_index_at(pool_index);
91 assert(_pool->tag_at(bsm_index).is_method_handle(), "must be a MH constant");
92 // There is a CP cache entry holding the BSM for these calls.
93 int bsm_cache_index = cp_entry_to_cp_cache(bsm_index);
94 cache->entry_at(i)->initialize_bootstrap_method_index_in_cache(bsm_cache_index);
95 }
96 }
97 }
98
99 _pool->set_cache(cache); 88 _pool->set_cache(cache);
100 cache->set_constant_pool(_pool()); 89 cache->set_constant_pool(_pool());
101 } 90 }
102 91
103 92
150 address p = bcp + offset; 139 address p = bcp + offset;
151 if (!reverse) { 140 if (!reverse) {
152 int cp_index = Bytes::get_Java_u2(p); 141 int cp_index = Bytes::get_Java_u2(p);
153 int cache_index = cp_entry_to_cp_cache(cp_index); 142 int cache_index = cp_entry_to_cp_cache(cp_index);
154 Bytes::put_native_u2(p, cache_index); 143 Bytes::put_native_u2(p, cache_index);
144 if (!_method_handle_invokers.is_empty())
145 maybe_rewrite_invokehandle(p - 1, cp_index, reverse);
155 } else { 146 } else {
156 int cache_index = Bytes::get_native_u2(p); 147 int cache_index = Bytes::get_native_u2(p);
157 int pool_index = cp_cache_entry_pool_index(cache_index); 148 int pool_index = cp_cache_entry_pool_index(cache_index);
158 Bytes::put_Java_u2(p, pool_index); 149 Bytes::put_Java_u2(p, pool_index);
150 if (!_method_handle_invokers.is_empty())
151 maybe_rewrite_invokehandle(p - 1, pool_index, reverse);
152 }
153 }
154
155
156 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.)
157 void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, bool reverse) {
158 if (!reverse) {
159 if ((*opc) == (u1)Bytecodes::_invokevirtual ||
160 // allow invokespecial as an alias, although it would be very odd:
161 (*opc) == (u1)Bytecodes::_invokespecial) {
162 assert(_pool->tag_at(cp_index).is_method(), "wrong index");
163 // Determine whether this is a signature-polymorphic method.
164 if (cp_index >= _method_handle_invokers.length()) return;
165 int status = _method_handle_invokers[cp_index];
166 assert(status >= -1 && status <= 1, "oob tri-state");
167 if (status == 0) {
168 if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() &&
169 MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(),
170 _pool->name_ref_at(cp_index)))
171 status = +1;
172 else
173 status = -1;
174 _method_handle_invokers[cp_index] = status;
175 }
176 // We use a special internal bytecode for such methods (if non-static).
177 // The basic reason for this is that such methods need an extra "appendix" argument
178 // to transmit the call site's intended call type.
179 if (status > 0) {
180 (*opc) = (u1)Bytecodes::_invokehandle;
181 }
182 }
183 } else {
184 // Do not need to look at cp_index.
185 if ((*opc) == (u1)Bytecodes::_invokehandle) {
186 (*opc) = (u1)Bytecodes::_invokevirtual;
187 // Ignore corner case of original _invokespecial instruction.
188 // This is safe because (a) the signature polymorphic method was final, and
189 // (b) the implementation of MethodHandle will not call invokespecial on it.
190 }
159 } 191 }
160 } 192 }
161 193
162 194
163 void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) { 195 void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) {
301 case Bytecodes::_putfield : // fall through 333 case Bytecodes::_putfield : // fall through
302 case Bytecodes::_invokevirtual : // fall through 334 case Bytecodes::_invokevirtual : // fall through
303 case Bytecodes::_invokespecial : // fall through 335 case Bytecodes::_invokespecial : // fall through
304 case Bytecodes::_invokestatic : 336 case Bytecodes::_invokestatic :
305 case Bytecodes::_invokeinterface: 337 case Bytecodes::_invokeinterface:
338 case Bytecodes::_invokehandle : // if reverse=true
306 rewrite_member_reference(bcp, prefix_length+1, reverse); 339 rewrite_member_reference(bcp, prefix_length+1, reverse);
307 break; 340 break;
308 case Bytecodes::_invokedynamic: 341 case Bytecodes::_invokedynamic:
309 rewrite_invokedynamic(bcp, prefix_length+1, reverse); 342 rewrite_invokedynamic(bcp, prefix_length+1, reverse);
310 break; 343 break;
311 case Bytecodes::_ldc: 344 case Bytecodes::_ldc:
312 case Bytecodes::_fast_aldc: 345 case Bytecodes::_fast_aldc: // if reverse=true
313 maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse); 346 maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse);
314 break; 347 break;
315 case Bytecodes::_ldc_w: 348 case Bytecodes::_ldc_w:
316 case Bytecodes::_fast_aldc_w: 349 case Bytecodes::_fast_aldc_w: // if reverse=true
317 maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse); 350 maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse);
318 break; 351 break;
319 case Bytecodes::_jsr : // fall through 352 case Bytecodes::_jsr : // fall through
320 case Bytecodes::_jsr_w : nof_jsrs++; break; 353 case Bytecodes::_jsr_w : nof_jsrs++; break;
321 case Bytecodes::_monitorenter : // fall through 354 case Bytecodes::_monitorenter : // fall through