Thu, 23 Jun 2011 17:14:06 -0700
7056328: JSR 292 invocation sometimes fails in adapters for types not on boot class path
Reviewed-by: never
1 /*
2 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "interpreter/rewriter.hpp"
27 #include "memory/oopFactory.hpp"
28 #include "prims/methodHandleWalk.hpp"
30 /*
31 * JSR 292 reference implementation: method handle structure analysis
32 */
34 #ifdef PRODUCT
35 #define print_method_handle(mh) {}
36 #else //PRODUCT
37 extern "C" void print_method_handle(oop mh);
38 #endif //PRODUCT
40 // -----------------------------------------------------------------------------
41 // MethodHandleChain
43 void MethodHandleChain::set_method_handle(Handle mh, TRAPS) {
44 if (!java_lang_invoke_MethodHandle::is_instance(mh())) lose("bad method handle", CHECK);
46 // set current method handle and unpack partially
47 _method_handle = mh;
48 _is_last = false;
49 _is_bound = false;
50 _arg_slot = -1;
51 _arg_type = T_VOID;
52 _conversion = -1;
53 _last_invoke = Bytecodes::_nop; //arbitrary non-garbage
55 if (java_lang_invoke_DirectMethodHandle::is_instance(mh())) {
56 set_last_method(mh(), THREAD);
57 return;
58 }
59 if (java_lang_invoke_AdapterMethodHandle::is_instance(mh())) {
60 _conversion = AdapterMethodHandle_conversion();
61 assert(_conversion != -1, "bad conv value");
62 assert(java_lang_invoke_BoundMethodHandle::is_instance(mh()), "also BMH");
63 }
64 if (java_lang_invoke_BoundMethodHandle::is_instance(mh())) {
65 if (!is_adapter()) // keep AMH and BMH separate in this model
66 _is_bound = true;
67 _arg_slot = BoundMethodHandle_vmargslot();
68 oop target = MethodHandle_vmtarget_oop();
69 if (!is_bound() || java_lang_invoke_MethodHandle::is_instance(target)) {
70 _arg_type = compute_bound_arg_type(target, NULL, _arg_slot, CHECK);
71 } else if (target != NULL && target->is_method()) {
72 methodOop m = (methodOop) target;
73 _arg_type = compute_bound_arg_type(NULL, m, _arg_slot, CHECK);
74 set_last_method(mh(), CHECK);
75 } else {
76 _is_bound = false; // lose!
77 }
78 }
79 if (is_bound() && _arg_type == T_VOID) {
80 lose("bad vmargslot", CHECK);
81 }
82 if (!is_bound() && !is_adapter()) {
83 lose("unrecognized MH type", CHECK);
84 }
85 }
88 void MethodHandleChain::set_last_method(oop target, TRAPS) {
89 _is_last = true;
90 KlassHandle receiver_limit; int flags = 0;
91 _last_method = MethodHandles::decode_method(target, receiver_limit, flags);
92 if ((flags & MethodHandles::_dmf_has_receiver) == 0)
93 _last_invoke = Bytecodes::_invokestatic;
94 else if ((flags & MethodHandles::_dmf_does_dispatch) == 0)
95 _last_invoke = Bytecodes::_invokespecial;
96 else if ((flags & MethodHandles::_dmf_from_interface) != 0)
97 _last_invoke = Bytecodes::_invokeinterface;
98 else
99 _last_invoke = Bytecodes::_invokevirtual;
100 }
103 BasicType MethodHandleChain::compute_bound_arg_type(oop target, methodOop m, int arg_slot, TRAPS) {
104 // There is no direct indication of whether the argument is primitive or not.
105 // It is implied by the _vmentry code, and by the MethodType of the target.
106 BasicType arg_type = T_VOID;
107 if (target != NULL) {
108 oop mtype = java_lang_invoke_MethodHandle::type(target);
109 int arg_num = MethodHandles::argument_slot_to_argnum(mtype, arg_slot);
110 if (arg_num >= 0) {
111 oop ptype = java_lang_invoke_MethodType::ptype(mtype, arg_num);
112 arg_type = java_lang_Class::as_BasicType(ptype);
113 }
114 } else if (m != NULL) {
115 // figure out the argument type from the slot
116 // FIXME: make this explicit in the MH
117 int cur_slot = m->size_of_parameters();
118 if (arg_slot >= cur_slot)
119 return T_VOID;
120 if (!m->is_static()) {
121 cur_slot -= type2size[T_OBJECT];
122 if (cur_slot == arg_slot)
123 return T_OBJECT;
124 }
125 ResourceMark rm(THREAD);
126 for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
127 BasicType bt = ss.type();
128 cur_slot -= type2size[bt];
129 if (cur_slot <= arg_slot) {
130 if (cur_slot == arg_slot)
131 arg_type = bt;
132 break;
133 }
134 }
135 }
136 if (arg_type == T_ARRAY)
137 arg_type = T_OBJECT;
138 return arg_type;
139 }
142 void MethodHandleChain::lose(const char* msg, TRAPS) {
143 _lose_message = msg;
144 #ifdef ASSERT
145 if (Verbose) {
146 tty->print_cr(INTPTR_FORMAT " lose: %s", _method_handle(), msg);
147 print();
148 }
149 #endif
150 if (!THREAD->is_Java_thread() || ((JavaThread*)THREAD)->thread_state() != _thread_in_vm) {
151 // throw a preallocated exception
152 THROW_OOP(Universe::virtual_machine_error_instance());
153 }
154 THROW_MSG(vmSymbols::java_lang_InternalError(), msg);
155 }
158 #ifdef ASSERT
159 static const char* adapter_ops[] = {
160 "retype_only" ,
161 "retype_raw" ,
162 "check_cast" ,
163 "prim_to_prim" ,
164 "ref_to_prim" ,
165 "prim_to_ref" ,
166 "swap_args" ,
167 "rot_args" ,
168 "dup_args" ,
169 "drop_args" ,
170 "collect_args" ,
171 "spread_args" ,
172 "fold_args"
173 };
175 static const char* adapter_op_to_string(int op) {
176 if (op >= 0 && op < (int)ARRAY_SIZE(adapter_ops))
177 return adapter_ops[op];
178 return "unknown_op";
179 }
181 void MethodHandleChain::print(oopDesc* m) {
182 HandleMark hm;
183 ResourceMark rm;
184 Handle mh(m);
185 print(mh);
186 }
188 void MethodHandleChain::print(Handle mh) {
189 EXCEPTION_MARK;
190 MethodHandleChain mhc(mh, THREAD);
191 if (HAS_PENDING_EXCEPTION) {
192 oop ex = THREAD->pending_exception();
193 CLEAR_PENDING_EXCEPTION;
194 ex->print();
195 return;
196 }
197 mhc.print();
198 }
201 void MethodHandleChain::print() {
202 EXCEPTION_MARK;
203 print_impl(THREAD);
204 if (HAS_PENDING_EXCEPTION) {
205 oop ex = THREAD->pending_exception();
206 CLEAR_PENDING_EXCEPTION;
207 ex->print();
208 }
209 }
211 void MethodHandleChain::print_impl(TRAPS) {
212 ResourceMark rm;
214 MethodHandleChain chain(_root, CHECK);
215 for (;;) {
216 tty->print(INTPTR_FORMAT ": ", chain.method_handle()());
217 if (chain.is_bound()) {
218 tty->print("bound: arg_type %s arg_slot %d",
219 type2name(chain.bound_arg_type()),
220 chain.bound_arg_slot());
221 oop o = chain.bound_arg_oop();
222 if (o != NULL) {
223 if (o->is_instance()) {
224 tty->print(" instance %s", o->klass()->klass_part()->internal_name());
225 } else {
226 o->print();
227 }
228 }
229 } else if (chain.is_adapter()) {
230 tty->print("adapter: arg_slot %d conversion op %s",
231 chain.adapter_arg_slot(),
232 adapter_op_to_string(chain.adapter_conversion_op()));
233 switch (chain.adapter_conversion_op()) {
234 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
235 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW:
236 case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST:
237 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM:
238 case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM:
239 break;
241 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: {
242 tty->print(" src_type = %s", type2name(chain.adapter_conversion_src_type()));
243 break;
244 }
246 case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS:
247 case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: {
248 int dest_arg_slot = chain.adapter_conversion_vminfo();
249 tty->print(" dest_arg_slot %d type %s", dest_arg_slot, type2name(chain.adapter_conversion_src_type()));
250 break;
251 }
253 case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS:
254 case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: {
255 int dup_slots = chain.adapter_conversion_stack_pushes();
256 tty->print(" pushes %d", dup_slots);
257 break;
258 }
260 case java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS:
261 case java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS: {
262 int coll_slots = chain.MethodHandle_vmslots();
263 tty->print(" coll_slots %d", coll_slots);
264 break;
265 }
267 case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: {
268 // Check the required length.
269 int spread_slots = 1 + chain.adapter_conversion_stack_pushes();
270 tty->print(" spread_slots %d", spread_slots);
271 break;
272 }
274 default:
275 tty->print_cr("bad adapter conversion");
276 break;
277 }
278 } else {
279 // DMH
280 tty->print("direct: ");
281 chain.last_method_oop()->print_short_name(tty);
282 }
284 tty->print(" (");
285 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(chain.method_type_oop());
286 for (int i = ptypes->length() - 1; i >= 0; i--) {
287 BasicType t = java_lang_Class::as_BasicType(ptypes->obj_at(i));
288 if (t == T_ARRAY) t = T_OBJECT;
289 tty->print("%c", type2char(t));
290 if (t == T_LONG || t == T_DOUBLE) tty->print("_");
291 }
292 tty->print(")");
293 BasicType rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(chain.method_type_oop()));
294 if (rtype == T_ARRAY) rtype = T_OBJECT;
295 tty->print("%c", type2char(rtype));
296 tty->cr();
297 if (!chain.is_last()) {
298 chain.next(CHECK);
299 } else {
300 break;
301 }
302 }
303 }
304 #endif
307 // -----------------------------------------------------------------------------
308 // MethodHandleWalker
310 Bytecodes::Code MethodHandleWalker::conversion_code(BasicType src, BasicType dest) {
311 if (is_subword_type(src)) {
312 src = T_INT; // all subword src types act like int
313 }
314 if (src == dest) {
315 return Bytecodes::_nop;
316 }
318 #define SRC_DEST(s,d) (((int)(s) << 4) + (int)(d))
319 switch (SRC_DEST(src, dest)) {
320 case SRC_DEST(T_INT, T_LONG): return Bytecodes::_i2l;
321 case SRC_DEST(T_INT, T_FLOAT): return Bytecodes::_i2f;
322 case SRC_DEST(T_INT, T_DOUBLE): return Bytecodes::_i2d;
323 case SRC_DEST(T_INT, T_BYTE): return Bytecodes::_i2b;
324 case SRC_DEST(T_INT, T_CHAR): return Bytecodes::_i2c;
325 case SRC_DEST(T_INT, T_SHORT): return Bytecodes::_i2s;
327 case SRC_DEST(T_LONG, T_INT): return Bytecodes::_l2i;
328 case SRC_DEST(T_LONG, T_FLOAT): return Bytecodes::_l2f;
329 case SRC_DEST(T_LONG, T_DOUBLE): return Bytecodes::_l2d;
331 case SRC_DEST(T_FLOAT, T_INT): return Bytecodes::_f2i;
332 case SRC_DEST(T_FLOAT, T_LONG): return Bytecodes::_f2l;
333 case SRC_DEST(T_FLOAT, T_DOUBLE): return Bytecodes::_f2d;
335 case SRC_DEST(T_DOUBLE, T_INT): return Bytecodes::_d2i;
336 case SRC_DEST(T_DOUBLE, T_LONG): return Bytecodes::_d2l;
337 case SRC_DEST(T_DOUBLE, T_FLOAT): return Bytecodes::_d2f;
338 }
339 #undef SRC_DEST
341 // cannot do it in one step, or at all
342 return Bytecodes::_illegal;
343 }
346 // -----------------------------------------------------------------------------
347 // MethodHandleWalker::walk
348 //
349 MethodHandleWalker::ArgToken
350 MethodHandleWalker::walk(TRAPS) {
351 ArgToken empty = ArgToken(); // Empty return value.
353 walk_incoming_state(CHECK_(empty));
355 for (;;) {
356 set_method_handle(chain().method_handle_oop());
358 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
360 if (chain().is_adapter()) {
361 int conv_op = chain().adapter_conversion_op();
362 int arg_slot = chain().adapter_arg_slot();
364 // Check that the arg_slot is valid. In most cases it must be
365 // within range of the current arguments but there are some
366 // exceptions. Those are sanity checked in their implemention
367 // below.
368 if ((arg_slot < 0 || arg_slot >= _outgoing.length()) &&
369 conv_op > java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW &&
370 conv_op != java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS &&
371 conv_op != java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS) {
372 lose(err_msg("bad argument index %d", arg_slot), CHECK_(empty));
373 }
375 bool retain_original_args = false; // used by fold/collect logic
377 // perform the adapter action
378 switch (conv_op) {
379 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
380 // No changes to arguments; pass the bits through.
381 break;
383 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW: {
384 // To keep the verifier happy, emit bitwise ("raw") conversions as needed.
385 // See MethodHandles::same_basic_type_for_arguments for allowed conversions.
386 Handle incoming_mtype(THREAD, chain().method_type_oop());
387 Handle outgoing_mtype;
388 {
389 oop outgoing_mh_oop = chain().vmtarget_oop();
390 if (!java_lang_invoke_MethodHandle::is_instance(outgoing_mh_oop))
391 lose("outgoing target not a MethodHandle", CHECK_(empty));
392 outgoing_mtype = Handle(THREAD, java_lang_invoke_MethodHandle::type(outgoing_mh_oop));
393 }
395 int nptypes = java_lang_invoke_MethodType::ptype_count(outgoing_mtype());
396 if (nptypes != java_lang_invoke_MethodType::ptype_count(incoming_mtype()))
397 lose("incoming and outgoing parameter count do not agree", CHECK_(empty));
399 // Argument types.
400 for (int i = 0, slot = _outgoing.length() - 1; slot >= 0; slot--) {
401 if (arg_type(slot) == T_VOID) continue;
403 klassOop src_klass = NULL;
404 klassOop dst_klass = NULL;
405 BasicType src = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(incoming_mtype(), i), &src_klass);
406 BasicType dst = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(outgoing_mtype(), i), &dst_klass);
407 retype_raw_argument_type(src, dst, slot, CHECK_(empty));
408 i++; // We need to skip void slots at the top of the loop.
409 }
411 // Return type.
412 {
413 BasicType src = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(incoming_mtype()));
414 BasicType dst = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(outgoing_mtype()));
415 retype_raw_return_type(src, dst, CHECK_(empty));
416 }
417 break;
418 }
420 case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST: {
421 // checkcast the Nth outgoing argument in place
422 klassOop dest_klass = NULL;
423 BasicType dest = java_lang_Class::as_BasicType(chain().adapter_arg_oop(), &dest_klass);
424 assert(dest == T_OBJECT, "");
425 ArgToken arg = _outgoing.at(arg_slot);
426 assert(dest == arg.basic_type(), "");
427 arg = make_conversion(T_OBJECT, dest_klass, Bytecodes::_checkcast, arg, CHECK_(empty));
428 // replace the object by the result of the cast, to make the compiler happy:
429 change_argument(T_OBJECT, arg_slot, T_OBJECT, arg);
430 debug_only(dest_klass = (klassOop)badOop);
431 break;
432 }
434 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM: {
435 // i2l, etc., on the Nth outgoing argument in place
436 BasicType src = chain().adapter_conversion_src_type(),
437 dest = chain().adapter_conversion_dest_type();
438 ArgToken arg = _outgoing.at(arg_slot);
439 Bytecodes::Code bc = conversion_code(src, dest);
440 if (bc == Bytecodes::_nop) {
441 break;
442 } else if (bc != Bytecodes::_illegal) {
443 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
444 } else if (is_subword_type(dest)) {
445 bc = conversion_code(src, T_INT);
446 if (bc != Bytecodes::_illegal) {
447 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
448 bc = conversion_code(T_INT, dest);
449 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
450 }
451 }
452 if (bc == Bytecodes::_illegal) {
453 lose(err_msg("bad primitive conversion for %s -> %s", type2name(src), type2name(dest)), CHECK_(empty));
454 }
455 change_argument(src, arg_slot, dest, arg);
456 break;
457 }
459 case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM: {
460 // checkcast to wrapper type & call intValue, etc.
461 BasicType dest = chain().adapter_conversion_dest_type();
462 ArgToken arg = _outgoing.at(arg_slot);
463 arg = make_conversion(T_OBJECT, SystemDictionary::box_klass(dest),
464 Bytecodes::_checkcast, arg, CHECK_(empty));
465 vmIntrinsics::ID unboxer = vmIntrinsics::for_unboxing(dest);
466 if (unboxer == vmIntrinsics::_none) {
467 lose("no unboxing method", CHECK_(empty));
468 }
469 ArgToken arglist[2];
470 arglist[0] = arg; // outgoing 'this'
471 arglist[1] = ArgToken(); // sentinel
472 arg = make_invoke(methodHandle(), unboxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_(empty));
473 change_argument(T_OBJECT, arg_slot, dest, arg);
474 break;
475 }
477 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: {
478 // call wrapper type.valueOf
479 BasicType src = chain().adapter_conversion_src_type();
480 vmIntrinsics::ID boxer = vmIntrinsics::for_boxing(src);
481 if (boxer == vmIntrinsics::_none) {
482 lose("no boxing method", CHECK_(empty));
483 }
484 ArgToken arg = _outgoing.at(arg_slot);
485 ArgToken arglist[2];
486 arglist[0] = arg; // outgoing value
487 arglist[1] = ArgToken(); // sentinel
488 arg = make_invoke(methodHandle(), boxer, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK_(empty));
489 change_argument(src, arg_slot, T_OBJECT, arg);
490 break;
491 }
493 case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS: {
494 int dest_arg_slot = chain().adapter_conversion_vminfo();
495 if (!has_argument(dest_arg_slot)) {
496 lose("bad swap index", CHECK_(empty));
497 }
498 // a simple swap between two arguments
499 if (arg_slot > dest_arg_slot) {
500 int tmp = arg_slot;
501 arg_slot = dest_arg_slot;
502 dest_arg_slot = tmp;
503 }
504 ArgToken a1 = _outgoing.at(arg_slot);
505 ArgToken a2 = _outgoing.at(dest_arg_slot);
506 change_argument(a2.basic_type(), dest_arg_slot, a1);
507 change_argument(a1.basic_type(), arg_slot, a2);
508 break;
509 }
511 case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: {
512 int limit_raw = chain().adapter_conversion_vminfo();
513 bool rot_down = (arg_slot < limit_raw);
514 int limit_bias = (rot_down ? MethodHandles::OP_ROT_ARGS_DOWN_LIMIT_BIAS : 0);
515 int limit_slot = limit_raw - limit_bias;
516 if ((uint)limit_slot > (uint)_outgoing.length()) {
517 lose("bad rotate index", CHECK_(empty));
518 }
519 // Rotate the source argument (plus following N slots) into the
520 // position occupied by the dest argument (plus following N slots).
521 int rotate_count = type2size[chain().adapter_conversion_src_type()];
522 // (no other rotate counts are currently supported)
523 if (rot_down) {
524 for (int i = 0; i < rotate_count; i++) {
525 ArgToken temp = _outgoing.at(arg_slot);
526 _outgoing.remove_at(arg_slot);
527 _outgoing.insert_before(limit_slot - 1, temp);
528 }
529 } else { // arg_slot > limit_slot => rotate_up
530 for (int i = 0; i < rotate_count; i++) {
531 ArgToken temp = _outgoing.at(arg_slot + rotate_count - 1);
532 _outgoing.remove_at(arg_slot + rotate_count - 1);
533 _outgoing.insert_before(limit_slot, temp);
534 }
535 }
536 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
537 break;
538 }
540 case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS: {
541 int dup_slots = chain().adapter_conversion_stack_pushes();
542 if (dup_slots <= 0) {
543 lose("bad dup count", CHECK_(empty));
544 }
545 for (int i = 0; i < dup_slots; i++) {
546 ArgToken dup = _outgoing.at(arg_slot + 2*i);
547 if (dup.basic_type() != T_VOID) _outgoing_argc += 1;
548 _outgoing.insert_before(i, dup);
549 }
550 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
551 break;
552 }
554 case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: {
555 int drop_slots = -chain().adapter_conversion_stack_pushes();
556 if (drop_slots <= 0) {
557 lose("bad drop count", CHECK_(empty));
558 }
559 for (int i = 0; i < drop_slots; i++) {
560 ArgToken drop = _outgoing.at(arg_slot);
561 if (drop.basic_type() != T_VOID) _outgoing_argc -= 1;
562 _outgoing.remove_at(arg_slot);
563 }
564 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
565 break;
566 }
568 case java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS:
569 retain_original_args = true; // and fall through:
570 case java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS: {
571 // call argument MH recursively
572 //{static int x; if (!x++) print_method_handle(chain().method_handle_oop()); --x;}
573 Handle recursive_mh(THREAD, chain().adapter_arg_oop());
574 if (!java_lang_invoke_MethodHandle::is_instance(recursive_mh())) {
575 lose("recursive target not a MethodHandle", CHECK_(empty));
576 }
577 Handle recursive_mtype(THREAD, java_lang_invoke_MethodHandle::type(recursive_mh()));
578 int argc = java_lang_invoke_MethodType::ptype_count(recursive_mtype());
579 int coll_slots = java_lang_invoke_MethodHandle::vmslots(recursive_mh());
580 BasicType rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(recursive_mtype()));
581 ArgToken* arglist = NEW_RESOURCE_ARRAY(ArgToken, 1 + argc + 1); // 1+: mh, +1: sentinel
582 arglist[0] = make_oop_constant(recursive_mh(), CHECK_(empty));
583 if (arg_slot < 0 || coll_slots < 0 || arg_slot + coll_slots > _outgoing.length()) {
584 lose("bad fold/collect arg slot", CHECK_(empty));
585 }
586 for (int i = 0, slot = arg_slot + coll_slots - 1; slot >= arg_slot; slot--) {
587 ArgToken arg_state = _outgoing.at(slot);
588 BasicType arg_type = arg_state.basic_type();
589 if (arg_type == T_VOID) continue;
590 ArgToken arg = _outgoing.at(slot);
591 if (i >= argc) { lose("bad fold/collect arg", CHECK_(empty)); }
592 arglist[1+i] = arg;
593 if (!retain_original_args)
594 change_argument(arg_type, slot, T_VOID, ArgToken(tt_void));
595 i++;
596 }
597 arglist[1+argc] = ArgToken(); // sentinel
598 oop invoker = java_lang_invoke_MethodTypeForm::vmlayout(
599 java_lang_invoke_MethodType::form(recursive_mtype()) );
600 if (invoker == NULL || !invoker->is_method()) {
601 lose("bad vmlayout slot", CHECK_(empty));
602 }
603 // FIXME: consider inlining the invokee at the bytecode level
604 ArgToken ret = make_invoke(methodHandle(THREAD, methodOop(invoker)), vmIntrinsics::_invokeGeneric,
605 Bytecodes::_invokevirtual, false, 1+argc, &arglist[0], CHECK_(empty));
606 // The iid = _invokeGeneric really means to adjust reference types as needed.
607 DEBUG_ONLY(invoker = NULL);
608 if (rtype == T_OBJECT) {
609 klassOop rklass = java_lang_Class::as_klassOop( java_lang_invoke_MethodType::rtype(recursive_mtype()) );
610 if (rklass != SystemDictionary::Object_klass() &&
611 !Klass::cast(rklass)->is_interface()) {
612 // preserve type safety
613 ret = make_conversion(T_OBJECT, rklass, Bytecodes::_checkcast, ret, CHECK_(empty));
614 }
615 }
616 if (rtype != T_VOID) {
617 int ret_slot = arg_slot + (retain_original_args ? coll_slots : 0);
618 change_argument(T_VOID, ret_slot, rtype, ret);
619 }
620 break;
621 }
623 case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: {
624 klassOop array_klass_oop = NULL;
625 BasicType array_type = java_lang_Class::as_BasicType(chain().adapter_arg_oop(),
626 &array_klass_oop);
627 assert(array_type == T_OBJECT, "");
628 assert(Klass::cast(array_klass_oop)->oop_is_array(), "");
629 arrayKlassHandle array_klass(THREAD, array_klass_oop);
630 debug_only(array_klass_oop = (klassOop)badOop);
632 klassOop element_klass_oop = NULL;
633 BasicType element_type = java_lang_Class::as_BasicType(array_klass->component_mirror(),
634 &element_klass_oop);
635 KlassHandle element_klass(THREAD, element_klass_oop);
636 debug_only(element_klass_oop = (klassOop)badOop);
638 // Fetch the argument, which we will cast to the required array type.
639 ArgToken arg = _outgoing.at(arg_slot);
640 assert(arg.basic_type() == T_OBJECT, "");
641 ArgToken array_arg = arg;
642 array_arg = make_conversion(T_OBJECT, array_klass(), Bytecodes::_checkcast, array_arg, CHECK_(empty));
643 change_argument(T_OBJECT, arg_slot, T_VOID, ArgToken(tt_void));
645 // Check the required length.
646 int spread_slots = 1 + chain().adapter_conversion_stack_pushes();
647 int spread_length = spread_slots;
648 if (type2size[element_type] == 2) {
649 if (spread_slots % 2 != 0) spread_slots = -1; // force error
650 spread_length = spread_slots / 2;
651 }
652 if (spread_slots < 0) {
653 lose("bad spread length", CHECK_(empty));
654 }
656 jvalue length_jvalue; length_jvalue.i = spread_length;
657 ArgToken length_arg = make_prim_constant(T_INT, &length_jvalue, CHECK_(empty));
658 // Call a built-in method known to the JVM to validate the length.
659 ArgToken arglist[3];
660 arglist[0] = array_arg; // value to check
661 arglist[1] = length_arg; // length to check
662 arglist[2] = ArgToken(); // sentinel
663 make_invoke(methodHandle(), vmIntrinsics::_checkSpreadArgument,
664 Bytecodes::_invokestatic, false, 2, &arglist[0], CHECK_(empty));
666 // Spread out the array elements.
667 Bytecodes::Code aload_op = Bytecodes::_nop;
668 switch (element_type) {
669 case T_INT: aload_op = Bytecodes::_iaload; break;
670 case T_LONG: aload_op = Bytecodes::_laload; break;
671 case T_FLOAT: aload_op = Bytecodes::_faload; break;
672 case T_DOUBLE: aload_op = Bytecodes::_daload; break;
673 case T_OBJECT: aload_op = Bytecodes::_aaload; break;
674 case T_BOOLEAN: // fall through:
675 case T_BYTE: aload_op = Bytecodes::_baload; break;
676 case T_CHAR: aload_op = Bytecodes::_caload; break;
677 case T_SHORT: aload_op = Bytecodes::_saload; break;
678 default: lose("primitive array NYI", CHECK_(empty));
679 }
680 int ap = arg_slot;
681 for (int i = 0; i < spread_length; i++) {
682 jvalue offset_jvalue; offset_jvalue.i = i;
683 ArgToken offset_arg = make_prim_constant(T_INT, &offset_jvalue, CHECK_(empty));
684 ArgToken element_arg = make_fetch(element_type, element_klass(), aload_op, array_arg, offset_arg, CHECK_(empty));
685 change_argument(T_VOID, ap, element_type, element_arg);
686 //ap += type2size[element_type]; // don't do this; insert next arg to *right* of previous
687 }
688 break;
689 }
691 default:
692 lose("bad adapter conversion", CHECK_(empty));
693 break;
694 }
695 }
697 if (chain().is_bound()) {
698 // push a new argument
699 BasicType arg_type = chain().bound_arg_type();
700 jint arg_slot = chain().bound_arg_slot();
701 oop arg_oop = chain().bound_arg_oop();
702 ArgToken arg;
703 if (arg_type == T_OBJECT) {
704 arg = make_oop_constant(arg_oop, CHECK_(empty));
705 } else {
706 jvalue arg_value;
707 BasicType bt = java_lang_boxing_object::get_value(arg_oop, &arg_value);
708 if (bt == arg_type || (bt == T_INT && is_subword_type(arg_type))) {
709 arg = make_prim_constant(arg_type, &arg_value, CHECK_(empty));
710 } else {
711 lose(err_msg("bad bound value: arg_type %s boxing %s", type2name(arg_type), type2name(bt)), CHECK_(empty));
712 }
713 }
714 DEBUG_ONLY(arg_oop = badOop);
715 change_argument(T_VOID, arg_slot, arg_type, arg);
716 }
718 // this test must come after the body of the loop
719 if (!chain().is_last()) {
720 chain().next(CHECK_(empty));
721 } else {
722 break;
723 }
724 }
726 // finish the sequence with a tail-call to the ultimate target
727 // parameters are passed in logical order (recv 1st), not slot order
728 ArgToken* arglist = NEW_RESOURCE_ARRAY(ArgToken, _outgoing.length() + 1);
729 int ap = 0;
730 for (int i = _outgoing.length() - 1; i >= 0; i--) {
731 ArgToken arg_state = _outgoing.at(i);
732 if (arg_state.basic_type() == T_VOID) continue;
733 arglist[ap++] = _outgoing.at(i);
734 }
735 assert(ap == _outgoing_argc, "");
736 arglist[ap] = ArgToken(); // add a sentinel, for the sake of asserts
737 return make_invoke(chain().last_method(),
738 vmIntrinsics::_none,
739 chain().last_invoke_code(), true,
740 ap, arglist, THREAD);
741 }
744 // -----------------------------------------------------------------------------
745 // MethodHandleWalker::walk_incoming_state
746 //
747 void MethodHandleWalker::walk_incoming_state(TRAPS) {
748 Handle mtype(THREAD, chain().method_type_oop());
749 int nptypes = java_lang_invoke_MethodType::ptype_count(mtype());
750 _outgoing_argc = nptypes;
751 int argp = nptypes - 1;
752 if (argp >= 0) {
753 _outgoing.at_grow(argp, ArgToken(tt_void)); // presize
754 }
755 for (int i = 0; i < nptypes; i++) {
756 klassOop arg_type_klass = NULL;
757 BasicType arg_type = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(mtype(), i), &arg_type_klass);
758 int index = new_local_index(arg_type);
759 ArgToken arg = make_parameter(arg_type, arg_type_klass, index, CHECK);
760 DEBUG_ONLY(arg_type_klass = (klassOop) NULL);
761 _outgoing.at_put(argp, arg);
762 if (type2size[arg_type] == 2) {
763 // add the extra slot, so we can model the JVM stack
764 _outgoing.insert_before(argp+1, ArgToken(tt_void));
765 }
766 --argp;
767 }
768 // call make_parameter at the end of the list for the return type
769 klassOop ret_type_klass = NULL;
770 BasicType ret_type = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(mtype()), &ret_type_klass);
771 ArgToken ret = make_parameter(ret_type, ret_type_klass, -1, CHECK);
772 // ignore ret; client can catch it if needed
774 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
776 verify_args_and_signature(CHECK);
777 }
780 #ifdef ASSERT
781 void MethodHandleWalker::verify_args_and_signature(TRAPS) {
782 int index = _outgoing.length() - 1;
783 objArrayOop ptypes = java_lang_invoke_MethodType::ptypes(chain().method_type_oop());
784 for (int i = 0, limit = ptypes->length(); i < limit; i++) {
785 BasicType t = java_lang_Class::as_BasicType(ptypes->obj_at(i));
786 if (t == T_ARRAY) t = T_OBJECT;
787 if (t == T_LONG || t == T_DOUBLE) {
788 assert(T_VOID == _outgoing.at(index).basic_type(), "types must match");
789 index--;
790 }
791 assert(t == _outgoing.at(index).basic_type(), "types must match");
792 index--;
793 }
794 }
795 #endif
798 // -----------------------------------------------------------------------------
799 // MethodHandleWalker::change_argument
800 //
801 // This is messy because some kinds of arguments are paired with
802 // companion slots containing an empty value.
803 void MethodHandleWalker::change_argument(BasicType old_type, int slot, const ArgToken& new_arg) {
804 BasicType new_type = new_arg.basic_type();
805 int old_size = type2size[old_type];
806 int new_size = type2size[new_type];
807 if (old_size == new_size) {
808 // simple case first
809 _outgoing.at_put(slot, new_arg);
810 } else if (old_size > new_size) {
811 for (int i = old_size - 1; i >= new_size; i--) {
812 assert((i != 0) == (_outgoing.at(slot + i).basic_type() == T_VOID), "");
813 _outgoing.remove_at(slot + i);
814 }
815 if (new_size > 0)
816 _outgoing.at_put(slot, new_arg);
817 else
818 _outgoing_argc -= 1; // deleted a real argument
819 } else {
820 for (int i = old_size; i < new_size; i++) {
821 _outgoing.insert_before(slot + i, ArgToken(tt_void));
822 }
823 _outgoing.at_put(slot, new_arg);
824 if (old_size == 0)
825 _outgoing_argc += 1; // inserted a real argument
826 }
827 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
828 }
831 #ifdef ASSERT
832 int MethodHandleWalker::argument_count_slow() {
833 int args_seen = 0;
834 for (int i = _outgoing.length() - 1; i >= 0; i--) {
835 if (_outgoing.at(i).basic_type() != T_VOID) {
836 ++args_seen;
837 if (_outgoing.at(i).basic_type() == T_LONG ||
838 _outgoing.at(i).basic_type() == T_DOUBLE) {
839 assert(_outgoing.at(i + 1).basic_type() == T_VOID, "should only follow two word");
840 }
841 } else {
842 assert(_outgoing.at(i - 1).basic_type() == T_LONG ||
843 _outgoing.at(i - 1).basic_type() == T_DOUBLE, "should only follow two word");
844 }
845 }
846 return args_seen;
847 }
848 #endif
851 // -----------------------------------------------------------------------------
852 // MethodHandleWalker::retype_raw_conversion
853 //
854 // Do the raw retype conversions for OP_RETYPE_RAW.
855 void MethodHandleWalker::retype_raw_conversion(BasicType src, BasicType dst, bool for_return, int slot, TRAPS) {
856 if (src != dst) {
857 if (MethodHandles::same_basic_type_for_returns(src, dst, /*raw*/ true)) {
858 if (MethodHandles::is_float_fixed_reinterpretation_cast(src, dst)) {
859 vmIntrinsics::ID iid = vmIntrinsics::for_raw_conversion(src, dst);
860 if (iid == vmIntrinsics::_none) {
861 lose("no raw conversion method", CHECK);
862 }
863 ArgToken arglist[2];
864 if (!for_return) {
865 // argument type conversion
866 ArgToken arg = _outgoing.at(slot);
867 assert(arg.token_type() >= tt_symbolic || src == arg.basic_type(), "sanity");
868 arglist[0] = arg; // outgoing 'this'
869 arglist[1] = ArgToken(); // sentinel
870 arg = make_invoke(methodHandle(), iid, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK);
871 change_argument(src, slot, dst, arg);
872 } else {
873 // return type conversion
874 if (_return_conv == vmIntrinsics::_none) {
875 _return_conv = iid;
876 } else if (_return_conv == vmIntrinsics::for_raw_conversion(dst, src)) {
877 _return_conv = vmIntrinsics::_none;
878 } else if (_return_conv != zero_return_conv()) {
879 lose(err_msg("requested raw return conversion not allowed: %s -> %s (before %s)", type2name(src), type2name(dst), vmIntrinsics::name_at(_return_conv)), CHECK);
880 }
881 }
882 } else {
883 // Nothing to do.
884 }
885 } else if (for_return && (!is_subword_type(src) || !is_subword_type(dst))) {
886 // This can occur in exception-throwing MHs, which have a fictitious return value encoded as Void or Empty.
887 _return_conv = zero_return_conv();
888 } else if (src == T_OBJECT && is_java_primitive(dst)) {
889 // ref-to-prim: discard ref, push zero
890 lose("requested ref-to-prim conversion not expected", CHECK);
891 } else {
892 lose(err_msg("requested raw conversion not allowed: %s -> %s", type2name(src), type2name(dst)), CHECK);
893 }
894 }
895 }
898 // -----------------------------------------------------------------------------
899 // MethodHandleCompiler
901 MethodHandleCompiler::MethodHandleCompiler(Handle root, Symbol* name, Symbol* signature, int invoke_count, bool is_invokedynamic, TRAPS)
902 : MethodHandleWalker(root, is_invokedynamic, THREAD),
903 _invoke_count(invoke_count),
904 _thread(THREAD),
905 _bytecode(THREAD, 50),
906 _constants(THREAD, 10),
907 _non_bcp_klasses(THREAD, 5),
908 _cur_stack(0),
909 _max_stack(0),
910 _rtype(T_ILLEGAL)
911 {
913 // Element zero is always the null constant.
914 (void) _constants.append(NULL);
916 // Set name and signature index.
917 _name_index = cpool_symbol_put(name);
918 _signature_index = cpool_symbol_put(signature);
920 // To make the resulting methods more recognizable by
921 // stack walkers and compiler heuristics,
922 // we put them in holder class MethodHandle.
923 // See klass_is_method_handle_adapter_holder
924 // and methodOopDesc::is_method_handle_adapter.
925 _target_klass = SystemDictionaryHandles::MethodHandle_klass();
927 check_non_bcp_klasses(java_lang_invoke_MethodHandle::type(root()), CHECK);
929 // Get return type klass.
930 Handle first_mtype(THREAD, chain().method_type_oop());
931 // _rklass is NULL for primitives.
932 _rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(first_mtype()), &_rklass);
933 if (_rtype == T_ARRAY) _rtype = T_OBJECT;
935 ArgumentSizeComputer args(signature);
936 int params = args.size() + 1; // Incoming arguments plus receiver.
937 _num_params = for_invokedynamic() ? params - 1 : params; // XXX Check if callee is static?
938 }
941 // -----------------------------------------------------------------------------
942 // MethodHandleCompiler::compile
943 //
944 // Compile this MethodHandle into a bytecode adapter and return a
945 // methodOop.
946 methodHandle MethodHandleCompiler::compile(TRAPS) {
947 assert(_thread == THREAD, "must be same thread");
948 methodHandle nullHandle;
949 (void) walk(CHECK_(nullHandle));
950 record_non_bcp_klasses();
951 return get_method_oop(CHECK_(nullHandle));
952 }
955 void MethodHandleCompiler::emit_bc(Bytecodes::Code op, int index, int args_size) {
956 Bytecodes::check(op); // Are we legal?
958 switch (op) {
959 // b
960 case Bytecodes::_aconst_null:
961 case Bytecodes::_iconst_m1:
962 case Bytecodes::_iconst_0:
963 case Bytecodes::_iconst_1:
964 case Bytecodes::_iconst_2:
965 case Bytecodes::_iconst_3:
966 case Bytecodes::_iconst_4:
967 case Bytecodes::_iconst_5:
968 case Bytecodes::_lconst_0:
969 case Bytecodes::_lconst_1:
970 case Bytecodes::_fconst_0:
971 case Bytecodes::_fconst_1:
972 case Bytecodes::_fconst_2:
973 case Bytecodes::_dconst_0:
974 case Bytecodes::_dconst_1:
975 case Bytecodes::_iload_0:
976 case Bytecodes::_iload_1:
977 case Bytecodes::_iload_2:
978 case Bytecodes::_iload_3:
979 case Bytecodes::_lload_0:
980 case Bytecodes::_lload_1:
981 case Bytecodes::_lload_2:
982 case Bytecodes::_lload_3:
983 case Bytecodes::_fload_0:
984 case Bytecodes::_fload_1:
985 case Bytecodes::_fload_2:
986 case Bytecodes::_fload_3:
987 case Bytecodes::_dload_0:
988 case Bytecodes::_dload_1:
989 case Bytecodes::_dload_2:
990 case Bytecodes::_dload_3:
991 case Bytecodes::_aload_0:
992 case Bytecodes::_aload_1:
993 case Bytecodes::_aload_2:
994 case Bytecodes::_aload_3:
995 case Bytecodes::_istore_0:
996 case Bytecodes::_istore_1:
997 case Bytecodes::_istore_2:
998 case Bytecodes::_istore_3:
999 case Bytecodes::_lstore_0:
1000 case Bytecodes::_lstore_1:
1001 case Bytecodes::_lstore_2:
1002 case Bytecodes::_lstore_3:
1003 case Bytecodes::_fstore_0:
1004 case Bytecodes::_fstore_1:
1005 case Bytecodes::_fstore_2:
1006 case Bytecodes::_fstore_3:
1007 case Bytecodes::_dstore_0:
1008 case Bytecodes::_dstore_1:
1009 case Bytecodes::_dstore_2:
1010 case Bytecodes::_dstore_3:
1011 case Bytecodes::_astore_0:
1012 case Bytecodes::_astore_1:
1013 case Bytecodes::_astore_2:
1014 case Bytecodes::_astore_3:
1015 case Bytecodes::_iand:
1016 case Bytecodes::_i2l:
1017 case Bytecodes::_i2f:
1018 case Bytecodes::_i2d:
1019 case Bytecodes::_i2b:
1020 case Bytecodes::_i2c:
1021 case Bytecodes::_i2s:
1022 case Bytecodes::_l2i:
1023 case Bytecodes::_l2f:
1024 case Bytecodes::_l2d:
1025 case Bytecodes::_f2i:
1026 case Bytecodes::_f2l:
1027 case Bytecodes::_f2d:
1028 case Bytecodes::_d2i:
1029 case Bytecodes::_d2l:
1030 case Bytecodes::_d2f:
1031 case Bytecodes::_iaload:
1032 case Bytecodes::_laload:
1033 case Bytecodes::_faload:
1034 case Bytecodes::_daload:
1035 case Bytecodes::_aaload:
1036 case Bytecodes::_baload:
1037 case Bytecodes::_caload:
1038 case Bytecodes::_saload:
1039 case Bytecodes::_ireturn:
1040 case Bytecodes::_lreturn:
1041 case Bytecodes::_freturn:
1042 case Bytecodes::_dreturn:
1043 case Bytecodes::_areturn:
1044 case Bytecodes::_return:
1045 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_b, "wrong bytecode format");
1046 _bytecode.push(op);
1047 break;
1049 // bi
1050 case Bytecodes::_ldc:
1051 assert(Bytecodes::format_bits(op, false) == (Bytecodes::_fmt_b|Bytecodes::_fmt_has_k), "wrong bytecode format");
1052 if (index == (index & 0xff)) {
1053 _bytecode.push(op);
1054 _bytecode.push(index);
1055 } else {
1056 _bytecode.push(Bytecodes::_ldc_w);
1057 _bytecode.push(index >> 8);
1058 _bytecode.push(index);
1059 }
1060 break;
1062 case Bytecodes::_iload:
1063 case Bytecodes::_lload:
1064 case Bytecodes::_fload:
1065 case Bytecodes::_dload:
1066 case Bytecodes::_aload:
1067 case Bytecodes::_istore:
1068 case Bytecodes::_lstore:
1069 case Bytecodes::_fstore:
1070 case Bytecodes::_dstore:
1071 case Bytecodes::_astore:
1072 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bi, "wrong bytecode format");
1073 if (index == (index & 0xff)) {
1074 _bytecode.push(op);
1075 _bytecode.push(index);
1076 } else {
1077 // doesn't fit in a u2
1078 _bytecode.push(Bytecodes::_wide);
1079 _bytecode.push(op);
1080 _bytecode.push(index >> 8);
1081 _bytecode.push(index);
1082 }
1083 break;
1085 // bkk
1086 case Bytecodes::_ldc_w:
1087 case Bytecodes::_ldc2_w:
1088 case Bytecodes::_checkcast:
1089 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bkk, "wrong bytecode format");
1090 assert((unsigned short) index == index, "index does not fit in 16-bit");
1091 _bytecode.push(op);
1092 _bytecode.push(index >> 8);
1093 _bytecode.push(index);
1094 break;
1096 // bJJ
1097 case Bytecodes::_invokestatic:
1098 case Bytecodes::_invokespecial:
1099 case Bytecodes::_invokevirtual:
1100 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
1101 assert((unsigned short) index == index, "index does not fit in 16-bit");
1102 _bytecode.push(op);
1103 _bytecode.push(index >> 8);
1104 _bytecode.push(index);
1105 break;
1107 case Bytecodes::_invokeinterface:
1108 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
1109 assert((unsigned short) index == index, "index does not fit in 16-bit");
1110 assert(args_size > 0, "valid args_size");
1111 _bytecode.push(op);
1112 _bytecode.push(index >> 8);
1113 _bytecode.push(index);
1114 _bytecode.push(args_size);
1115 _bytecode.push(0);
1116 break;
1118 default:
1119 ShouldNotReachHere();
1120 }
1121 }
1124 void MethodHandleCompiler::emit_load(BasicType bt, int index) {
1125 if (index <= 3) {
1126 switch (bt) {
1127 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1128 case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_iload_0 + index)); break;
1129 case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lload_0 + index)); break;
1130 case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fload_0 + index)); break;
1131 case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dload_0 + index)); break;
1132 case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_aload_0 + index)); break;
1133 default:
1134 ShouldNotReachHere();
1135 }
1136 }
1137 else {
1138 switch (bt) {
1139 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1140 case T_INT: emit_bc(Bytecodes::_iload, index); break;
1141 case T_LONG: emit_bc(Bytecodes::_lload, index); break;
1142 case T_FLOAT: emit_bc(Bytecodes::_fload, index); break;
1143 case T_DOUBLE: emit_bc(Bytecodes::_dload, index); break;
1144 case T_OBJECT: emit_bc(Bytecodes::_aload, index); break;
1145 default:
1146 ShouldNotReachHere();
1147 }
1148 }
1149 stack_push(bt);
1150 }
1152 void MethodHandleCompiler::emit_store(BasicType bt, int index) {
1153 if (index <= 3) {
1154 switch (bt) {
1155 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1156 case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_istore_0 + index)); break;
1157 case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lstore_0 + index)); break;
1158 case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fstore_0 + index)); break;
1159 case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dstore_0 + index)); break;
1160 case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_astore_0 + index)); break;
1161 default:
1162 ShouldNotReachHere();
1163 }
1164 }
1165 else {
1166 switch (bt) {
1167 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1168 case T_INT: emit_bc(Bytecodes::_istore, index); break;
1169 case T_LONG: emit_bc(Bytecodes::_lstore, index); break;
1170 case T_FLOAT: emit_bc(Bytecodes::_fstore, index); break;
1171 case T_DOUBLE: emit_bc(Bytecodes::_dstore, index); break;
1172 case T_OBJECT: emit_bc(Bytecodes::_astore, index); break;
1173 default:
1174 ShouldNotReachHere();
1175 }
1176 }
1177 stack_pop(bt);
1178 }
1181 void MethodHandleCompiler::emit_load_constant(ArgToken arg) {
1182 BasicType bt = arg.basic_type();
1183 if (is_subword_type(bt)) bt = T_INT;
1184 switch (bt) {
1185 case T_INT: {
1186 jint value = arg.get_jint();
1187 if (-1 <= value && value <= 5)
1188 emit_bc(Bytecodes::cast(Bytecodes::_iconst_0 + value));
1189 else
1190 emit_bc(Bytecodes::_ldc, cpool_int_put(value));
1191 break;
1192 }
1193 case T_LONG: {
1194 jlong value = arg.get_jlong();
1195 if (0 <= value && value <= 1)
1196 emit_bc(Bytecodes::cast(Bytecodes::_lconst_0 + (int) value));
1197 else
1198 emit_bc(Bytecodes::_ldc2_w, cpool_long_put(value));
1199 break;
1200 }
1201 case T_FLOAT: {
1202 jfloat value = arg.get_jfloat();
1203 if (value == 0.0 || value == 1.0 || value == 2.0)
1204 emit_bc(Bytecodes::cast(Bytecodes::_fconst_0 + (int) value));
1205 else
1206 emit_bc(Bytecodes::_ldc, cpool_float_put(value));
1207 break;
1208 }
1209 case T_DOUBLE: {
1210 jdouble value = arg.get_jdouble();
1211 if (value == 0.0 || value == 1.0)
1212 emit_bc(Bytecodes::cast(Bytecodes::_dconst_0 + (int) value));
1213 else
1214 emit_bc(Bytecodes::_ldc2_w, cpool_double_put(value));
1215 break;
1216 }
1217 case T_OBJECT: {
1218 Handle value = arg.object();
1219 if (value.is_null()) {
1220 emit_bc(Bytecodes::_aconst_null);
1221 break;
1222 }
1223 if (java_lang_Class::is_instance(value())) {
1224 klassOop k = java_lang_Class::as_klassOop(value());
1225 if (k != NULL) {
1226 emit_bc(Bytecodes::_ldc, cpool_klass_put(k));
1227 break;
1228 }
1229 }
1230 emit_bc(Bytecodes::_ldc, cpool_object_put(value));
1231 break;
1232 }
1233 default:
1234 ShouldNotReachHere();
1235 }
1236 stack_push(bt);
1237 }
1240 MethodHandleWalker::ArgToken
1241 MethodHandleCompiler::make_conversion(BasicType type, klassOop tk, Bytecodes::Code op,
1242 const ArgToken& src, TRAPS) {
1244 BasicType srctype = src.basic_type();
1245 TokenType tt = src.token_type();
1246 int index = -1;
1248 switch (op) {
1249 case Bytecodes::_i2l:
1250 case Bytecodes::_i2f:
1251 case Bytecodes::_i2d:
1252 case Bytecodes::_i2b:
1253 case Bytecodes::_i2c:
1254 case Bytecodes::_i2s:
1256 case Bytecodes::_l2i:
1257 case Bytecodes::_l2f:
1258 case Bytecodes::_l2d:
1260 case Bytecodes::_f2i:
1261 case Bytecodes::_f2l:
1262 case Bytecodes::_f2d:
1264 case Bytecodes::_d2i:
1265 case Bytecodes::_d2l:
1266 case Bytecodes::_d2f:
1267 if (tt == tt_constant) {
1268 emit_load_constant(src);
1269 } else {
1270 emit_load(srctype, src.index());
1271 }
1272 stack_pop(srctype); // pop the src type
1273 emit_bc(op);
1274 stack_push(type); // push the dest value
1275 if (tt != tt_constant)
1276 index = src.index();
1277 if (srctype != type || index == -1)
1278 index = new_local_index(type);
1279 emit_store(type, index);
1280 break;
1282 case Bytecodes::_checkcast:
1283 if (tt == tt_constant) {
1284 emit_load_constant(src);
1285 } else {
1286 emit_load(srctype, src.index());
1287 index = src.index();
1288 }
1289 emit_bc(op, cpool_klass_put(tk));
1290 check_non_bcp_klass(tk, CHECK_(src));
1291 // Allocate a new local for the type so that we don't hide the
1292 // previous type from the verifier.
1293 index = new_local_index(type);
1294 emit_store(srctype, index);
1295 break;
1297 case Bytecodes::_nop:
1298 // nothing to do
1299 return src;
1301 default:
1302 if (op == Bytecodes::_illegal)
1303 lose(err_msg("no such primitive conversion: %s -> %s", type2name(src.basic_type()), type2name(type)), THREAD);
1304 else
1305 lose(err_msg("bad primitive conversion op: %s", Bytecodes::name(op)), THREAD);
1306 return make_prim_constant(type, &zero_jvalue, THREAD);
1307 }
1309 return make_parameter(type, tk, index, THREAD);
1310 }
1313 // -----------------------------------------------------------------------------
1314 // MethodHandleCompiler
1315 //
1317 // Values used by the compiler.
1318 jvalue MethodHandleCompiler::zero_jvalue = { 0 };
1319 jvalue MethodHandleCompiler::one_jvalue = { 1 };
1321 // Emit bytecodes for the given invoke instruction.
1322 MethodHandleWalker::ArgToken
1323 MethodHandleCompiler::make_invoke(methodHandle m, vmIntrinsics::ID iid,
1324 Bytecodes::Code op, bool tailcall,
1325 int argc, MethodHandleWalker::ArgToken* argv,
1326 TRAPS) {
1327 ArgToken zero;
1328 if (m.is_null()) {
1329 // Get the intrinsic methodOop.
1330 m = methodHandle(THREAD, vmIntrinsics::method_for(iid));
1331 if (m.is_null()) {
1332 lose(vmIntrinsics::name_at(iid), CHECK_(zero));
1333 }
1334 }
1336 klassOop klass = m->method_holder();
1337 Symbol* name = m->name();
1338 Symbol* signature = m->signature();
1340 if (iid == vmIntrinsics::_invokeGeneric &&
1341 argc >= 1 && argv[0].token_type() == tt_constant) {
1342 assert(m->intrinsic_id() == vmIntrinsics::_invokeExact, "");
1343 Handle receiver = argv[0].object();
1344 Handle rtype(THREAD, java_lang_invoke_MethodHandle::type(receiver()));
1345 Handle mtype(THREAD, m->method_handle_type());
1346 if (rtype() != mtype()) {
1347 assert(java_lang_invoke_MethodType::form(rtype()) ==
1348 java_lang_invoke_MethodType::form(mtype()),
1349 "must be the same shape");
1350 // customize m to the exact required rtype
1351 bool has_non_bcp_klass = check_non_bcp_klasses(rtype(), CHECK_(zero));
1352 TempNewSymbol sig2 = java_lang_invoke_MethodType::as_signature(rtype(), true, CHECK_(zero));
1353 methodHandle m2;
1354 if (!has_non_bcp_klass) {
1355 methodOop m2_oop = SystemDictionary::find_method_handle_invoke(m->name(), sig2,
1356 KlassHandle(), CHECK_(zero));
1357 m2 = methodHandle(THREAD, m2_oop);
1358 }
1359 if (m2.is_null()) {
1360 // just build it fresh
1361 m2 = methodOopDesc::make_invoke_method(klass, m->name(), sig2, rtype, CHECK_(zero));
1362 if (m2.is_null())
1363 lose(err_msg("no customized invoker %s", sig2->as_utf8()), CHECK_(zero));
1364 }
1365 m = m2;
1366 signature = m->signature();
1367 }
1368 }
1370 check_non_bcp_klass(klass, CHECK_(zero));
1371 if (m->is_method_handle_invoke()) {
1372 check_non_bcp_klasses(m->method_handle_type(), CHECK_(zero));
1373 }
1375 // Count the number of arguments, not the size
1376 ArgumentCount asc(signature);
1377 assert(argc == asc.size() + ((op == Bytecodes::_invokestatic || op == Bytecodes::_invokedynamic) ? 0 : 1),
1378 "argc mismatch");
1380 // Inline the method.
1381 InvocationCounter* ic = m->invocation_counter();
1382 ic->set_carry_flag();
1384 for (int i = 0; i < argc; i++) {
1385 ArgToken arg = argv[i];
1386 TokenType tt = arg.token_type();
1387 BasicType bt = arg.basic_type();
1389 switch (tt) {
1390 case tt_parameter:
1391 case tt_temporary:
1392 emit_load(bt, arg.index());
1393 break;
1394 case tt_constant:
1395 emit_load_constant(arg);
1396 break;
1397 case tt_illegal:
1398 // Sentinel.
1399 assert(i == (argc - 1), "sentinel must be last entry");
1400 break;
1401 case tt_void:
1402 default:
1403 ShouldNotReachHere();
1404 }
1405 }
1407 // Populate constant pool.
1408 int name_index = cpool_symbol_put(name);
1409 int signature_index = cpool_symbol_put(signature);
1410 int name_and_type_index = cpool_name_and_type_put(name_index, signature_index);
1411 int klass_index = cpool_klass_put(klass);
1412 int methodref_index = cpool_methodref_put(op, klass_index, name_and_type_index, m);
1414 // Generate invoke.
1415 switch (op) {
1416 case Bytecodes::_invokestatic:
1417 case Bytecodes::_invokespecial:
1418 case Bytecodes::_invokevirtual:
1419 emit_bc(op, methodref_index);
1420 break;
1422 case Bytecodes::_invokeinterface: {
1423 ArgumentSizeComputer asc(signature);
1424 emit_bc(op, methodref_index, asc.size() + 1);
1425 break;
1426 }
1428 default:
1429 ShouldNotReachHere();
1430 }
1432 // If tailcall, we have walked all the way to a direct method handle.
1433 // Otherwise, make a recursive call to some helper routine.
1434 BasicType rbt = m->result_type();
1435 if (rbt == T_ARRAY) rbt = T_OBJECT;
1436 stack_push(rbt); // The return value is already pushed onto the stack.
1437 ArgToken ret;
1438 if (tailcall) {
1439 if (return_conv() == zero_return_conv()) {
1440 rbt = T_VOID; // discard value
1441 } else if (return_conv() != vmIntrinsics::_none) {
1442 // return value conversion
1443 int index = new_local_index(rbt);
1444 emit_store(rbt, index);
1445 ArgToken arglist[2];
1446 arglist[0] = ArgToken(tt_temporary, rbt, index);
1447 arglist[1] = ArgToken(); // sentinel
1448 ret = make_invoke(methodHandle(), return_conv(), Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK_(zero));
1449 set_return_conv(vmIntrinsics::_none);
1450 rbt = ret.basic_type();
1451 emit_load(rbt, ret.index());
1452 }
1453 if (rbt != _rtype) {
1454 if (rbt == T_VOID) {
1455 // push a zero of the right sort
1456 if (_rtype == T_OBJECT) {
1457 zero = make_oop_constant(NULL, CHECK_(zero));
1458 } else {
1459 zero = make_prim_constant(_rtype, &zero_jvalue, CHECK_(zero));
1460 }
1461 emit_load_constant(zero);
1462 } else if (_rtype == T_VOID) {
1463 // We'll emit a _return with something on the stack.
1464 // It's OK to ignore what's on the stack.
1465 } else if (rbt == T_INT && is_subword_type(_rtype)) {
1466 // Convert value to match return type.
1467 switch (_rtype) {
1468 case T_BOOLEAN: {
1469 // boolean is treated as a one-bit unsigned integer.
1470 // Cf. API documentation: java/lang/invoke/MethodHandles.html#explicitCastArguments
1471 ArgToken one = make_prim_constant(T_INT, &one_jvalue, CHECK_(zero));
1472 emit_load_constant(one);
1473 emit_bc(Bytecodes::_iand);
1474 break;
1475 }
1476 case T_BYTE: emit_bc(Bytecodes::_i2b); break;
1477 case T_CHAR: emit_bc(Bytecodes::_i2c); break;
1478 case T_SHORT: emit_bc(Bytecodes::_i2s); break;
1479 default: ShouldNotReachHere();
1480 }
1481 } else if (is_subword_type(rbt) && (is_subword_type(_rtype) || (_rtype == T_INT))) {
1482 // The subword type was returned as an int and will be passed
1483 // on as an int.
1484 } else {
1485 lose("unknown conversion", CHECK_(zero));
1486 }
1487 }
1488 switch (_rtype) {
1489 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1490 case T_INT: emit_bc(Bytecodes::_ireturn); break;
1491 case T_LONG: emit_bc(Bytecodes::_lreturn); break;
1492 case T_FLOAT: emit_bc(Bytecodes::_freturn); break;
1493 case T_DOUBLE: emit_bc(Bytecodes::_dreturn); break;
1494 case T_VOID: emit_bc(Bytecodes::_return); break;
1495 case T_OBJECT:
1496 if (_rklass.not_null() && _rklass() != SystemDictionary::Object_klass() && !Klass::cast(_rklass())->is_interface()) {
1497 emit_bc(Bytecodes::_checkcast, cpool_klass_put(_rklass()));
1498 check_non_bcp_klass(_rklass(), CHECK_(zero));
1499 }
1500 emit_bc(Bytecodes::_areturn);
1501 break;
1502 default: ShouldNotReachHere();
1503 }
1504 ret = ArgToken(); // Dummy return value.
1505 }
1506 else {
1507 int index = new_local_index(rbt);
1508 switch (rbt) {
1509 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1510 case T_INT: case T_LONG: case T_FLOAT: case T_DOUBLE:
1511 case T_OBJECT:
1512 emit_store(rbt, index);
1513 ret = ArgToken(tt_temporary, rbt, index);
1514 break;
1515 case T_VOID:
1516 ret = ArgToken(tt_void);
1517 break;
1518 default:
1519 ShouldNotReachHere();
1520 }
1521 }
1523 return ret;
1524 }
1526 MethodHandleWalker::ArgToken
1527 MethodHandleCompiler::make_fetch(BasicType type, klassOop tk, Bytecodes::Code op,
1528 const MethodHandleWalker::ArgToken& base,
1529 const MethodHandleWalker::ArgToken& offset,
1530 TRAPS) {
1531 switch (base.token_type()) {
1532 case tt_parameter:
1533 case tt_temporary:
1534 emit_load(base.basic_type(), base.index());
1535 break;
1536 case tt_constant:
1537 emit_load_constant(base);
1538 break;
1539 default:
1540 ShouldNotReachHere();
1541 }
1542 switch (offset.token_type()) {
1543 case tt_parameter:
1544 case tt_temporary:
1545 emit_load(offset.basic_type(), offset.index());
1546 break;
1547 case tt_constant:
1548 emit_load_constant(offset);
1549 break;
1550 default:
1551 ShouldNotReachHere();
1552 }
1553 emit_bc(op);
1554 int index = new_local_index(type);
1555 emit_store(type, index);
1556 return ArgToken(tt_temporary, type, index);
1557 }
1560 int MethodHandleCompiler::cpool_primitive_put(BasicType bt, jvalue* con) {
1561 jvalue con_copy;
1562 assert(bt < T_OBJECT, "");
1563 if (type2aelembytes(bt) < jintSize) {
1564 // widen to int
1565 con_copy = (*con);
1566 con = &con_copy;
1567 switch (bt) {
1568 case T_BOOLEAN: con->i = (con->z ? 1 : 0); break;
1569 case T_BYTE: con->i = con->b; break;
1570 case T_CHAR: con->i = con->c; break;
1571 case T_SHORT: con->i = con->s; break;
1572 default: ShouldNotReachHere();
1573 }
1574 bt = T_INT;
1575 }
1577 // for (int i = 1, imax = _constants.length(); i < imax; i++) {
1578 // ConstantValue* con = _constants.at(i);
1579 // if (con != NULL && con->is_primitive() && con.basic_type() == bt) {
1580 // bool match = false;
1581 // switch (type2size[bt]) {
1582 // case 1: if (pcon->_value.i == con->i) match = true; break;
1583 // case 2: if (pcon->_value.j == con->j) match = true; break;
1584 // }
1585 // if (match)
1586 // return i;
1587 // }
1588 // }
1589 ConstantValue* cv = new ConstantValue(bt, *con);
1590 int index = _constants.append(cv);
1592 // long and double entries take 2 slots, we add another empty entry.
1593 if (type2size[bt] == 2)
1594 (void) _constants.append(NULL);
1596 return index;
1597 }
1599 bool MethodHandleCompiler::check_non_bcp_klasses(Handle method_type, TRAPS) {
1600 bool res = false;
1601 for (int i = -1, len = java_lang_invoke_MethodType::ptype_count(method_type()); i < len; i++) {
1602 oop ptype = (i == -1
1603 ? java_lang_invoke_MethodType::rtype(method_type())
1604 : java_lang_invoke_MethodType::ptype(method_type(), i));
1605 res |= check_non_bcp_klass(java_lang_Class::as_klassOop(ptype), CHECK_(false));
1606 }
1607 return res;
1608 }
1610 bool MethodHandleCompiler::check_non_bcp_klass(klassOop klass, TRAPS) {
1611 klass = methodOopDesc::check_non_bcp_klass(klass);
1612 if (klass != NULL) {
1613 Symbol* name = Klass::cast(klass)->name();
1614 for (int i = _non_bcp_klasses.length() - 1; i >= 0; i--) {
1615 klassOop k2 = _non_bcp_klasses.at(i)();
1616 if (Klass::cast(k2)->name() == name) {
1617 if (k2 != klass) {
1618 lose(err_msg("unsupported klass name alias %s", name->as_utf8()), THREAD);
1619 }
1620 return true;
1621 }
1622 }
1623 _non_bcp_klasses.append(KlassHandle(THREAD, klass));
1624 return true;
1625 }
1626 return false;
1627 }
1629 void MethodHandleCompiler::record_non_bcp_klasses() {
1630 // Append extra klasses to constant pool, to guide klass lookup.
1631 for (int k = 0; k < _non_bcp_klasses.length(); k++) {
1632 klassOop non_bcp_klass = _non_bcp_klasses.at(k)();
1633 bool add_to_cp = true;
1634 for (int j = 1; j < _constants.length(); j++) {
1635 ConstantValue* cv = _constants.at(j);
1636 if (cv != NULL && cv->tag() == JVM_CONSTANT_Class
1637 && cv->klass_oop() == non_bcp_klass) {
1638 add_to_cp = false;
1639 break;
1640 }
1641 }
1642 if (add_to_cp) cpool_klass_put(non_bcp_klass);
1643 }
1644 }
1646 constantPoolHandle MethodHandleCompiler::get_constant_pool(TRAPS) const {
1647 constantPoolHandle nullHandle;
1648 constantPoolOop cpool_oop = oopFactory::new_constantPool(_constants.length(),
1649 oopDesc::IsSafeConc,
1650 CHECK_(nullHandle));
1651 constantPoolHandle cpool(THREAD, cpool_oop);
1653 // Fill the real constant pool skipping the zero element.
1654 for (int i = 1; i < _constants.length(); i++) {
1655 ConstantValue* cv = _constants.at(i);
1656 switch (cv->tag()) {
1657 case JVM_CONSTANT_Utf8: cpool->symbol_at_put( i, cv->symbol() ); break;
1658 case JVM_CONSTANT_Integer: cpool->int_at_put( i, cv->get_jint() ); break;
1659 case JVM_CONSTANT_Float: cpool->float_at_put( i, cv->get_jfloat() ); break;
1660 case JVM_CONSTANT_Long: cpool->long_at_put( i, cv->get_jlong() ); break;
1661 case JVM_CONSTANT_Double: cpool->double_at_put( i, cv->get_jdouble() ); break;
1662 case JVM_CONSTANT_Class: cpool->klass_at_put( i, cv->klass_oop() ); break;
1663 case JVM_CONSTANT_Methodref: cpool->method_at_put( i, cv->first_index(), cv->second_index()); break;
1664 case JVM_CONSTANT_InterfaceMethodref:
1665 cpool->interface_method_at_put(i, cv->first_index(), cv->second_index()); break;
1666 case JVM_CONSTANT_NameAndType: cpool->name_and_type_at_put(i, cv->first_index(), cv->second_index()); break;
1667 case JVM_CONSTANT_Object: cpool->object_at_put( i, cv->object_oop() ); break;
1668 default: ShouldNotReachHere();
1669 }
1671 switch (cv->tag()) {
1672 case JVM_CONSTANT_Long:
1673 case JVM_CONSTANT_Double:
1674 i++; // Skip empty entry.
1675 assert(_constants.at(i) == NULL, "empty entry");
1676 break;
1677 }
1678 }
1680 cpool->set_preresolution();
1682 // Set the constant pool holder to the target method's class.
1683 cpool->set_pool_holder(_target_klass());
1685 return cpool;
1686 }
1689 methodHandle MethodHandleCompiler::get_method_oop(TRAPS) const {
1690 methodHandle empty;
1691 // Create a method that holds the generated bytecode. invokedynamic
1692 // has no receiver, normal MH calls do.
1693 int flags_bits;
1694 if (for_invokedynamic())
1695 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC | JVM_ACC_STATIC);
1696 else
1697 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC);
1699 // Create a new method
1700 methodHandle m;
1701 {
1702 methodOop m_oop = oopFactory::new_method(bytecode_length(),
1703 accessFlags_from(flags_bits),
1704 0, 0, 0, oopDesc::IsSafeConc, CHECK_(empty));
1705 m = methodHandle(THREAD, m_oop);
1706 }
1708 constantPoolHandle cpool = get_constant_pool(CHECK_(empty));
1709 m->set_constants(cpool());
1711 m->set_name_index(_name_index);
1712 m->set_signature_index(_signature_index);
1714 m->set_code((address) bytecode());
1716 m->set_max_stack(_max_stack);
1717 m->set_max_locals(max_locals());
1718 m->set_size_of_parameters(_num_params);
1720 typeArrayHandle exception_handlers(THREAD, Universe::the_empty_int_array());
1721 m->set_exception_table(exception_handlers());
1723 // Rewrite the method and set up the constant pool cache.
1724 objArrayOop m_array = oopFactory::new_system_objArray(1, CHECK_(empty));
1725 objArrayHandle methods(THREAD, m_array);
1726 methods->obj_at_put(0, m());
1727 Rewriter::rewrite(_target_klass(), cpool, methods, CHECK_(empty)); // Use fake class.
1728 Rewriter::relocate_and_link(_target_klass(), methods, CHECK_(empty)); // Use fake class.
1730 // Pre-resolve selected CP cache entries, to avoid problems with class loader scoping.
1731 constantPoolCacheHandle cpc(THREAD, cpool->cache());
1732 for (int i = 0; i < cpc->length(); i++) {
1733 ConstantPoolCacheEntry* e = cpc->entry_at(i);
1734 assert(!e->is_secondary_entry(), "no indy instructions in here, yet");
1735 int constant_pool_index = e->constant_pool_index();
1736 ConstantValue* cv = _constants.at(constant_pool_index);
1737 if (!cv->has_linkage()) continue;
1738 methodHandle m = cv->linkage();
1739 int index;
1740 switch (cv->tag()) {
1741 case JVM_CONSTANT_Methodref:
1742 index = m->vtable_index();
1743 if (m->is_static()) {
1744 e->set_method(Bytecodes::_invokestatic, m, index);
1745 } else {
1746 e->set_method(Bytecodes::_invokespecial, m, index);
1747 e->set_method(Bytecodes::_invokevirtual, m, index);
1748 }
1749 break;
1750 case JVM_CONSTANT_InterfaceMethodref:
1751 index = klassItable::compute_itable_index(m());
1752 e->set_interface_call(m, index);
1753 break;
1754 }
1755 }
1757 // Set the invocation counter's count to the invoke count of the
1758 // original call site.
1759 InvocationCounter* ic = m->invocation_counter();
1760 ic->set(InvocationCounter::wait_for_compile, _invoke_count);
1762 // Create a new MDO
1763 {
1764 methodDataOop mdo = oopFactory::new_methodData(m, CHECK_(empty));
1765 assert(m->method_data() == NULL, "there should not be an MDO yet");
1766 m->set_method_data(mdo);
1768 // Iterate over all profile data and set the count of the counter
1769 // data entries to the original call site counter.
1770 for (ProfileData* profile_data = mdo->first_data();
1771 mdo->is_valid(profile_data);
1772 profile_data = mdo->next_data(profile_data)) {
1773 if (profile_data->is_CounterData()) {
1774 CounterData* counter_data = profile_data->as_CounterData();
1775 counter_data->set_count(_invoke_count);
1776 }
1777 }
1778 }
1780 #ifndef PRODUCT
1781 if (TraceMethodHandles) {
1782 m->print();
1783 m->print_codes();
1784 }
1785 #endif //PRODUCT
1787 assert(m->is_method_handle_adapter(), "must be recognized as an adapter");
1788 return m;
1789 }
1792 #ifndef PRODUCT
1794 // MH printer for debugging.
1796 class MethodHandlePrinter : public MethodHandleWalker {
1797 private:
1798 outputStream* _out;
1799 bool _verbose;
1800 int _temp_num;
1801 int _param_state;
1802 stringStream _strbuf;
1803 const char* strbuf() {
1804 const char* s = _strbuf.as_string();
1805 _strbuf.reset();
1806 return s;
1807 }
1808 ArgToken token(const char* str, BasicType type) {
1809 return ArgToken(str, type);
1810 }
1811 const char* string(ArgToken token) {
1812 return token.str();
1813 }
1814 void start_params() {
1815 _param_state <<= 1;
1816 _out->print("(");
1817 }
1818 void end_params() {
1819 if (_verbose) _out->print("\n");
1820 _out->print(") => {");
1821 _param_state >>= 1;
1822 }
1823 void put_type_name(BasicType type, klassOop tk, outputStream* s) {
1824 const char* kname = NULL;
1825 if (tk != NULL)
1826 kname = Klass::cast(tk)->external_name();
1827 s->print("%s", (kname != NULL) ? kname : type2name(type));
1828 }
1829 ArgToken maybe_make_temp(const char* statement_op, BasicType type, const char* temp_name) {
1830 const char* value = strbuf();
1831 if (!_verbose) return token(value, type);
1832 // make an explicit binding for each separate value
1833 _strbuf.print("%s%d", temp_name, ++_temp_num);
1834 const char* temp = strbuf();
1835 _out->print("\n %s %s %s = %s;", statement_op, type2name(type), temp, value);
1836 return token(temp, type);
1837 }
1839 public:
1840 MethodHandlePrinter(Handle root, bool verbose, outputStream* out, TRAPS)
1841 : MethodHandleWalker(root, false, THREAD),
1842 _out(out),
1843 _verbose(verbose),
1844 _param_state(0),
1845 _temp_num(0)
1846 {
1847 out->print("MethodHandle:");
1848 java_lang_invoke_MethodType::print_signature(java_lang_invoke_MethodHandle::type(root()), out);
1849 out->print(" : #");
1850 start_params();
1851 }
1852 virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) {
1853 if (argnum < 0) {
1854 end_params();
1855 return token("return", type);
1856 }
1857 if ((_param_state & 1) == 0) {
1858 _param_state |= 1;
1859 _out->print(_verbose ? "\n " : "");
1860 } else {
1861 _out->print(_verbose ? ",\n " : ", ");
1862 }
1863 if (argnum >= _temp_num)
1864 _temp_num = argnum;
1865 // generate an argument name
1866 _strbuf.print("a%d", argnum);
1867 const char* arg = strbuf();
1868 put_type_name(type, tk, _out);
1869 _out->print(" %s", arg);
1870 return token(arg, type);
1871 }
1872 virtual ArgToken make_oop_constant(oop con, TRAPS) {
1873 if (con == NULL)
1874 _strbuf.print("null");
1875 else
1876 con->print_value_on(&_strbuf);
1877 if (_strbuf.size() == 0) { // yuck
1878 _strbuf.print("(a ");
1879 put_type_name(T_OBJECT, con->klass(), &_strbuf);
1880 _strbuf.print(")");
1881 }
1882 return maybe_make_temp("constant", T_OBJECT, "k");
1883 }
1884 virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) {
1885 java_lang_boxing_object::print(type, con, &_strbuf);
1886 return maybe_make_temp("constant", type, "k");
1887 }
1888 void print_bytecode_name(Bytecodes::Code op) {
1889 if (Bytecodes::is_defined(op))
1890 _strbuf.print("%s", Bytecodes::name(op));
1891 else
1892 _strbuf.print("bytecode_%d", (int) op);
1893 }
1894 virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS) {
1895 print_bytecode_name(op);
1896 _strbuf.print("(%s", string(src));
1897 if (tk != NULL) {
1898 _strbuf.print(", ");
1899 put_type_name(type, tk, &_strbuf);
1900 }
1901 _strbuf.print(")");
1902 return maybe_make_temp("convert", type, "v");
1903 }
1904 virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS) {
1905 _strbuf.print("%s(%s, %s", Bytecodes::name(op), string(base), string(offset));
1906 if (tk != NULL) {
1907 _strbuf.print(", ");
1908 put_type_name(type, tk, &_strbuf);
1909 }
1910 _strbuf.print(")");
1911 return maybe_make_temp("fetch", type, "x");
1912 }
1913 virtual ArgToken make_invoke(methodHandle m, vmIntrinsics::ID iid,
1914 Bytecodes::Code op, bool tailcall,
1915 int argc, ArgToken* argv, TRAPS) {
1916 Symbol* name;
1917 Symbol* sig;
1918 if (m.not_null()) {
1919 name = m->name();
1920 sig = m->signature();
1921 } else {
1922 name = vmSymbols::symbol_at(vmIntrinsics::name_for(iid));
1923 sig = vmSymbols::symbol_at(vmIntrinsics::signature_for(iid));
1924 }
1925 _strbuf.print("%s %s%s(", Bytecodes::name(op), name->as_C_string(), sig->as_C_string());
1926 for (int i = 0; i < argc; i++) {
1927 _strbuf.print("%s%s", (i > 0 ? ", " : ""), string(argv[i]));
1928 }
1929 _strbuf.print(")");
1930 if (!tailcall) {
1931 BasicType rt = char2type(sig->byte_at(sig->utf8_length()-1));
1932 if (rt == T_ILLEGAL) rt = T_OBJECT; // ';' at the end of '(...)L...;'
1933 return maybe_make_temp("invoke", rt, "x");
1934 } else {
1935 const char* ret = strbuf();
1936 _out->print(_verbose ? "\n return " : " ");
1937 _out->print("%s", ret);
1938 _out->print(_verbose ? "\n}\n" : " }");
1939 }
1940 return ArgToken();
1941 }
1943 virtual void set_method_handle(oop mh) {
1944 if (WizardMode && Verbose) {
1945 tty->print("\n--- next target: ");
1946 mh->print();
1947 }
1948 }
1950 static void print(Handle root, bool verbose, outputStream* out, TRAPS) {
1951 ResourceMark rm;
1952 MethodHandlePrinter printer(root, verbose, out, CHECK);
1953 printer.walk(CHECK);
1954 out->print("\n");
1955 }
1956 static void print(Handle root, bool verbose = Verbose, outputStream* out = tty) {
1957 Thread* THREAD = Thread::current();
1958 ResourceMark rm;
1959 MethodHandlePrinter printer(root, verbose, out, THREAD);
1960 if (!HAS_PENDING_EXCEPTION)
1961 printer.walk(THREAD);
1962 if (HAS_PENDING_EXCEPTION) {
1963 oop ex = PENDING_EXCEPTION;
1964 CLEAR_PENDING_EXCEPTION;
1965 out->print(" *** ");
1966 if (printer.lose_message() != NULL) out->print("%s ", printer.lose_message());
1967 out->print("}");
1968 }
1969 out->print("\n");
1970 }
1971 };
1973 extern "C"
1974 void print_method_handle(oop mh) {
1975 if (!mh->is_oop()) {
1976 tty->print_cr("*** not a method handle: "PTR_FORMAT, (intptr_t)mh);
1977 } else if (java_lang_invoke_MethodHandle::is_instance(mh)) {
1978 MethodHandlePrinter::print(mh);
1979 } else {
1980 tty->print("*** not a method handle: ");
1981 mh->print();
1982 }
1983 }
1985 #endif // PRODUCT