68 if (saw_mh_symbol) |
68 if (saw_mh_symbol) |
69 _method_handle_invokers.initialize(length, (int)0); |
69 _method_handle_invokers.initialize(length, (int)0); |
70 } |
70 } |
71 |
71 |
72 // Unrewrite the bytecodes if an error occurs. |
72 // Unrewrite the bytecodes if an error occurs. |
73 void Rewriter::restore_bytecodes() { |
73 void Rewriter::restore_bytecodes(TRAPS) { |
74 int len = _methods->length(); |
74 int len = _methods->length(); |
75 |
75 |
76 for (int i = len-1; i >= 0; i--) { |
76 for (int i = len-1; i >= 0; i--) { |
77 Method* method = _methods->at(i); |
77 Method* method = _methods->at(i); |
78 scan_method(method, true); |
78 scan_method(method, true, CHECK); |
79 } |
79 } |
80 } |
80 } |
81 |
81 |
82 // Creates a constant pool cache given a CPC map |
82 // Creates a constant pool cache given a CPC map |
83 void Rewriter::make_constant_pool_cache(TRAPS) { |
83 void Rewriter::make_constant_pool_cache(TRAPS) { |
84 const int length = _cp_cache_map.length(); |
|
85 ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data(); |
84 ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data(); |
86 ConstantPoolCache* cache = |
85 ConstantPoolCache* cache = |
87 ConstantPoolCache::allocate(loader_data, length, _cp_cache_map, |
86 ConstantPoolCache::allocate(loader_data, _cp_cache_map, |
|
87 _invokedynamic_cp_cache_map, |
88 _invokedynamic_references_map, CHECK); |
88 _invokedynamic_references_map, CHECK); |
89 |
89 |
90 // initialize object cache in constant pool |
90 // initialize object cache in constant pool |
91 _pool->initialize_resolved_references(loader_data, _resolved_references_map, |
91 _pool->initialize_resolved_references(loader_data, _resolved_references_map, |
92 _resolved_reference_limit, |
92 _resolved_reference_limit, |
149 int cache_index = Bytes::get_native_u2(p); |
149 int cache_index = Bytes::get_native_u2(p); |
150 int pool_index = cp_cache_entry_pool_index(cache_index); |
150 int pool_index = cp_cache_entry_pool_index(cache_index); |
151 Bytes::put_Java_u2(p, pool_index); |
151 Bytes::put_Java_u2(p, pool_index); |
152 if (!_method_handle_invokers.is_empty()) |
152 if (!_method_handle_invokers.is_empty()) |
153 maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse); |
153 maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse); |
|
154 } |
|
155 } |
|
156 |
|
157 // If the constant pool entry for invokespecial is InterfaceMethodref, |
|
158 // we need to add a separate cpCache entry for its resolution, because it is |
|
159 // different than the resolution for invokeinterface with InterfaceMethodref. |
|
160 // These cannot share cpCache entries. It's unclear if all invokespecial to |
|
161 // InterfaceMethodrefs would resolve to the same thing so a new cpCache entry |
|
162 // is created for each one. This was added with lambda. |
|
163 void Rewriter::rewrite_invokespecial(address bcp, int offset, bool reverse, TRAPS) { |
|
164 static int count = 0; |
|
165 address p = bcp + offset; |
|
166 if (!reverse) { |
|
167 int cp_index = Bytes::get_Java_u2(p); |
|
168 int cache_index = add_invokespecial_cp_cache_entry(cp_index); |
|
169 if (cache_index != (int)(jushort) cache_index) { |
|
170 THROW_MSG(vmSymbols::java_lang_InternalError(), |
|
171 "This classfile overflows invokespecial for interfaces " |
|
172 "and cannot be loaded"); |
|
173 } |
|
174 Bytes::put_native_u2(p, cache_index); |
|
175 } else { |
|
176 int cache_index = Bytes::get_native_u2(p); |
|
177 int cp_index = cp_cache_entry_pool_index(cache_index); |
|
178 Bytes::put_Java_u2(p, cp_index); |
154 } |
179 } |
155 } |
180 } |
156 |
181 |
157 |
182 |
158 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.) |
183 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.) |
201 address p = bcp + offset; |
226 address p = bcp + offset; |
202 assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode"); |
227 assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode"); |
203 if (!reverse) { |
228 if (!reverse) { |
204 int cp_index = Bytes::get_Java_u2(p); |
229 int cp_index = Bytes::get_Java_u2(p); |
205 int cache_index = add_invokedynamic_cp_cache_entry(cp_index); |
230 int cache_index = add_invokedynamic_cp_cache_entry(cp_index); |
206 add_invokedynamic_resolved_references_entries(cp_index, cache_index); |
231 int resolved_index = add_invokedynamic_resolved_references_entries(cp_index, cache_index); |
207 // Replace the trailing four bytes with a CPC index for the dynamic |
232 // Replace the trailing four bytes with a CPC index for the dynamic |
208 // call site. Unlike other CPC entries, there is one per bytecode, |
233 // call site. Unlike other CPC entries, there is one per bytecode, |
209 // not just one per distinct CP entry. In other words, the |
234 // not just one per distinct CP entry. In other words, the |
210 // CPC-to-CP relation is many-to-one for invokedynamic entries. |
235 // CPC-to-CP relation is many-to-one for invokedynamic entries. |
211 // This means we must use a larger index size than u2 to address |
236 // This means we must use a larger index size than u2 to address |
212 // all these entries. That is the main reason invokedynamic |
237 // all these entries. That is the main reason invokedynamic |
213 // must have a five-byte instruction format. (Of course, other JVM |
238 // must have a five-byte instruction format. (Of course, other JVM |
214 // implementations can use the bytes for other purposes.) |
239 // implementations can use the bytes for other purposes.) |
|
240 // Note: We use native_u4 format exclusively for 4-byte indexes. |
215 Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index)); |
241 Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index)); |
216 // Note: We use native_u4 format exclusively for 4-byte indexes. |
242 // add the bcp in case we need to patch this bytecode if we also find a |
|
243 // invokespecial/InterfaceMethodref in the bytecode stream |
|
244 _patch_invokedynamic_bcps->push(p); |
|
245 _patch_invokedynamic_refs->push(resolved_index); |
217 } else { |
246 } else { |
218 // callsite index |
|
219 int cache_index = ConstantPool::decode_invokedynamic_index( |
247 int cache_index = ConstantPool::decode_invokedynamic_index( |
220 Bytes::get_native_u4(p)); |
248 Bytes::get_native_u4(p)); |
221 int cp_index = cp_cache_entry_pool_index(cache_index); |
249 // We will reverse the bytecode rewriting _after_ adjusting them. |
|
250 // Adjust the cache index by offset to the invokedynamic entries in the |
|
251 // cpCache plus the delta if the invokedynamic bytecodes were adjusted. |
|
252 cache_index = cp_cache_delta() + _first_iteration_cp_cache_limit; |
|
253 int cp_index = invokedynamic_cp_cache_entry_pool_index(cache_index); |
222 assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index"); |
254 assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index"); |
223 // zero out 4 bytes |
255 // zero out 4 bytes |
224 Bytes::put_Java_u4(p, 0); |
256 Bytes::put_Java_u4(p, 0); |
225 Bytes::put_Java_u2(p, cp_index); |
257 Bytes::put_Java_u2(p, cp_index); |
|
258 } |
|
259 } |
|
260 |
|
261 void Rewriter::patch_invokedynamic_bytecodes() { |
|
262 // If the end of the cp_cache is the same as after initializing with the |
|
263 // cpool, nothing needs to be done. Invokedynamic bytecodes are at the |
|
264 // correct offsets. ie. no invokespecials added |
|
265 int delta = cp_cache_delta(); |
|
266 if (delta > 0) { |
|
267 int length = _patch_invokedynamic_bcps->length(); |
|
268 assert(length == _patch_invokedynamic_refs->length(), |
|
269 "lengths should match"); |
|
270 for (int i = 0; i < length; i++) { |
|
271 address p = _patch_invokedynamic_bcps->at(i); |
|
272 int cache_index = ConstantPool::decode_invokedynamic_index( |
|
273 Bytes::get_native_u4(p)); |
|
274 Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index + delta)); |
|
275 |
|
276 // invokedynamic resolved references map also points to cp cache and must |
|
277 // add delta to each. |
|
278 int resolved_index = _patch_invokedynamic_refs->at(i); |
|
279 for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) { |
|
280 assert(_invokedynamic_references_map[resolved_index+entry] == cache_index, |
|
281 "should be the same index"); |
|
282 _invokedynamic_references_map.at_put(resolved_index+entry, |
|
283 cache_index + delta); |
|
284 } |
|
285 } |
226 } |
286 } |
227 } |
287 } |
228 |
288 |
229 |
289 |
230 // Rewrite some ldc bytecodes to _fast_aldc |
290 // Rewrite some ldc bytecodes to _fast_aldc |
327 #ifndef CC_INTERP |
387 #ifndef CC_INTERP |
328 (*bcp) = Bytecodes::_lookupswitch; |
388 (*bcp) = Bytecodes::_lookupswitch; |
329 #endif |
389 #endif |
330 break; |
390 break; |
331 } |
391 } |
|
392 |
|
393 case Bytecodes::_invokespecial : { |
|
394 int offset = prefix_length + 1; |
|
395 address p = bcp + offset; |
|
396 int cp_index = Bytes::get_Java_u2(p); |
|
397 // InterfaceMethodref |
|
398 if (_pool->tag_at(cp_index).is_interface_method()) { |
|
399 rewrite_invokespecial(bcp, offset, reverse, CHECK); |
|
400 } else { |
|
401 rewrite_member_reference(bcp, offset, reverse); |
|
402 } |
|
403 break; |
|
404 } |
|
405 |
332 case Bytecodes::_getstatic : // fall through |
406 case Bytecodes::_getstatic : // fall through |
333 case Bytecodes::_putstatic : // fall through |
407 case Bytecodes::_putstatic : // fall through |
334 case Bytecodes::_getfield : // fall through |
408 case Bytecodes::_getfield : // fall through |
335 case Bytecodes::_putfield : // fall through |
409 case Bytecodes::_putfield : // fall through |
336 case Bytecodes::_invokevirtual : // fall through |
410 case Bytecodes::_invokevirtual : // fall through |
337 case Bytecodes::_invokespecial : // fall through |
|
338 case Bytecodes::_invokestatic : |
411 case Bytecodes::_invokestatic : |
339 case Bytecodes::_invokeinterface: |
412 case Bytecodes::_invokeinterface: |
340 case Bytecodes::_invokehandle : // if reverse=true |
413 case Bytecodes::_invokehandle : // if reverse=true |
341 rewrite_member_reference(bcp, prefix_length+1, reverse); |
414 rewrite_member_reference(bcp, prefix_length+1, reverse); |
342 break; |
415 break; |
424 // rewrite methods, in two passes |
497 // rewrite methods, in two passes |
425 int len = _methods->length(); |
498 int len = _methods->length(); |
426 |
499 |
427 for (int i = len-1; i >= 0; i--) { |
500 for (int i = len-1; i >= 0; i--) { |
428 Method* method = _methods->at(i); |
501 Method* method = _methods->at(i); |
429 scan_method(method); |
502 scan_method(method, false, CHECK); // If you get an error here, |
430 } |
503 // there is no reversing bytecodes |
|
504 } |
|
505 |
|
506 // May have to fix invokedynamic bytecodes if invokestatic/InterfaceMethodref |
|
507 // entries had to be added. |
|
508 patch_invokedynamic_bytecodes(); |
431 |
509 |
432 // allocate constant pool cache, now that we've seen all the bytecodes |
510 // allocate constant pool cache, now that we've seen all the bytecodes |
433 make_constant_pool_cache(THREAD); |
511 make_constant_pool_cache(THREAD); |
434 |
512 |
435 // Restore bytecodes to their unrewritten state if there are exceptions |
513 // Restore bytecodes to their unrewritten state if there are exceptions |
436 // rewriting bytecodes or allocating the cpCache |
514 // rewriting bytecodes or allocating the cpCache |
437 if (HAS_PENDING_EXCEPTION) { |
515 if (HAS_PENDING_EXCEPTION) { |
438 restore_bytecodes(); |
516 restore_bytecodes(CATCH); |
439 return; |
517 return; |
440 } |
518 } |
441 |
519 |
442 // Relocate after everything, but still do this under the is_rewritten flag, |
520 // Relocate after everything, but still do this under the is_rewritten flag, |
443 // so methods with jsrs in custom class lists in aren't attempted to be |
521 // so methods with jsrs in custom class lists in aren't attempted to be |