Fri, 06 May 2011 16:33:13 -0700
6939861: JVM should handle more conversion operations
Reviewed-by: twisti, jrose
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 */
35 // -----------------------------------------------------------------------------
36 // MethodHandleChain
38 void MethodHandleChain::set_method_handle(Handle mh, TRAPS) {
39 if (!java_lang_invoke_MethodHandle::is_instance(mh())) lose("bad method handle", CHECK);
41 // set current method handle and unpack partially
42 _method_handle = mh;
43 _is_last = false;
44 _is_bound = false;
45 _arg_slot = -1;
46 _arg_type = T_VOID;
47 _conversion = -1;
48 _last_invoke = Bytecodes::_nop; //arbitrary non-garbage
50 if (java_lang_invoke_DirectMethodHandle::is_instance(mh())) {
51 set_last_method(mh(), THREAD);
52 return;
53 }
54 if (java_lang_invoke_AdapterMethodHandle::is_instance(mh())) {
55 _conversion = AdapterMethodHandle_conversion();
56 assert(_conversion != -1, "bad conv value");
57 assert(java_lang_invoke_BoundMethodHandle::is_instance(mh()), "also BMH");
58 }
59 if (java_lang_invoke_BoundMethodHandle::is_instance(mh())) {
60 if (!is_adapter()) // keep AMH and BMH separate in this model
61 _is_bound = true;
62 _arg_slot = BoundMethodHandle_vmargslot();
63 oop target = MethodHandle_vmtarget_oop();
64 if (!is_bound() || java_lang_invoke_MethodHandle::is_instance(target)) {
65 _arg_type = compute_bound_arg_type(target, NULL, _arg_slot, CHECK);
66 } else if (target != NULL && target->is_method()) {
67 methodOop m = (methodOop) target;
68 _arg_type = compute_bound_arg_type(NULL, m, _arg_slot, CHECK);
69 set_last_method(mh(), CHECK);
70 } else {
71 _is_bound = false; // lose!
72 }
73 }
74 if (is_bound() && _arg_type == T_VOID) {
75 lose("bad vmargslot", CHECK);
76 }
77 if (!is_bound() && !is_adapter()) {
78 lose("unrecognized MH type", CHECK);
79 }
80 }
83 void MethodHandleChain::set_last_method(oop target, TRAPS) {
84 _is_last = true;
85 KlassHandle receiver_limit; int flags = 0;
86 _last_method = MethodHandles::decode_method(target, receiver_limit, flags);
87 if ((flags & MethodHandles::_dmf_has_receiver) == 0)
88 _last_invoke = Bytecodes::_invokestatic;
89 else if ((flags & MethodHandles::_dmf_does_dispatch) == 0)
90 _last_invoke = Bytecodes::_invokespecial;
91 else if ((flags & MethodHandles::_dmf_from_interface) != 0)
92 _last_invoke = Bytecodes::_invokeinterface;
93 else
94 _last_invoke = Bytecodes::_invokevirtual;
95 }
98 BasicType MethodHandleChain::compute_bound_arg_type(oop target, methodOop m, int arg_slot, TRAPS) {
99 // There is no direct indication of whether the argument is primitive or not.
100 // It is implied by the _vmentry code, and by the MethodType of the target.
101 BasicType arg_type = T_VOID;
102 if (target != NULL) {
103 oop mtype = java_lang_invoke_MethodHandle::type(target);
104 int arg_num = MethodHandles::argument_slot_to_argnum(mtype, arg_slot);
105 if (arg_num >= 0) {
106 oop ptype = java_lang_invoke_MethodType::ptype(mtype, arg_num);
107 arg_type = java_lang_Class::as_BasicType(ptype);
108 }
109 } else if (m != NULL) {
110 // figure out the argument type from the slot
111 // FIXME: make this explicit in the MH
112 int cur_slot = m->size_of_parameters();
113 if (arg_slot >= cur_slot)
114 return T_VOID;
115 if (!m->is_static()) {
116 cur_slot -= type2size[T_OBJECT];
117 if (cur_slot == arg_slot)
118 return T_OBJECT;
119 }
120 ResourceMark rm(THREAD);
121 for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
122 BasicType bt = ss.type();
123 cur_slot -= type2size[bt];
124 if (cur_slot <= arg_slot) {
125 if (cur_slot == arg_slot)
126 arg_type = bt;
127 break;
128 }
129 }
130 }
131 if (arg_type == T_ARRAY)
132 arg_type = T_OBJECT;
133 return arg_type;
134 }
137 void MethodHandleChain::lose(const char* msg, TRAPS) {
138 _lose_message = msg;
139 if (!THREAD->is_Java_thread() || ((JavaThread*)THREAD)->thread_state() != _thread_in_vm) {
140 // throw a preallocated exception
141 THROW_OOP(Universe::virtual_machine_error_instance());
142 }
143 THROW_MSG(vmSymbols::java_lang_InternalError(), msg);
144 }
147 // -----------------------------------------------------------------------------
148 // MethodHandleWalker
150 Bytecodes::Code MethodHandleWalker::conversion_code(BasicType src, BasicType dest) {
151 if (is_subword_type(src)) {
152 src = T_INT; // all subword src types act like int
153 }
154 if (src == dest) {
155 return Bytecodes::_nop;
156 }
158 #define SRC_DEST(s,d) (((int)(s) << 4) + (int)(d))
159 switch (SRC_DEST(src, dest)) {
160 case SRC_DEST(T_INT, T_LONG): return Bytecodes::_i2l;
161 case SRC_DEST(T_INT, T_FLOAT): return Bytecodes::_i2f;
162 case SRC_DEST(T_INT, T_DOUBLE): return Bytecodes::_i2d;
163 case SRC_DEST(T_INT, T_BYTE): return Bytecodes::_i2b;
164 case SRC_DEST(T_INT, T_CHAR): return Bytecodes::_i2c;
165 case SRC_DEST(T_INT, T_SHORT): return Bytecodes::_i2s;
167 case SRC_DEST(T_LONG, T_INT): return Bytecodes::_l2i;
168 case SRC_DEST(T_LONG, T_FLOAT): return Bytecodes::_l2f;
169 case SRC_DEST(T_LONG, T_DOUBLE): return Bytecodes::_l2d;
171 case SRC_DEST(T_FLOAT, T_INT): return Bytecodes::_f2i;
172 case SRC_DEST(T_FLOAT, T_LONG): return Bytecodes::_f2l;
173 case SRC_DEST(T_FLOAT, T_DOUBLE): return Bytecodes::_f2d;
175 case SRC_DEST(T_DOUBLE, T_INT): return Bytecodes::_d2i;
176 case SRC_DEST(T_DOUBLE, T_LONG): return Bytecodes::_d2l;
177 case SRC_DEST(T_DOUBLE, T_FLOAT): return Bytecodes::_d2f;
178 }
179 #undef SRC_DEST
181 // cannot do it in one step, or at all
182 return Bytecodes::_illegal;
183 }
186 // -----------------------------------------------------------------------------
187 // MethodHandleWalker::walk
188 //
189 MethodHandleWalker::ArgToken
190 MethodHandleWalker::walk(TRAPS) {
191 ArgToken empty = ArgToken(); // Empty return value.
193 walk_incoming_state(CHECK_(empty));
195 for (;;) {
196 set_method_handle(chain().method_handle_oop());
198 assert(_outgoing_argc == argument_count_slow(), "empty slots under control");
200 if (chain().is_adapter()) {
201 int conv_op = chain().adapter_conversion_op();
202 int arg_slot = chain().adapter_arg_slot();
203 SlotState* arg_state = slot_state(arg_slot);
204 if (arg_state == NULL
205 && conv_op > java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW) {
206 lose("bad argument index", CHECK_(empty));
207 }
209 // perform the adapter action
210 switch (chain().adapter_conversion_op()) {
211 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY:
212 // No changes to arguments; pass the bits through.
213 break;
215 case java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW: {
216 // To keep the verifier happy, emit bitwise ("raw") conversions as needed.
217 // See MethodHandles::same_basic_type_for_arguments for allowed conversions.
218 Handle incoming_mtype(THREAD, chain().method_type_oop());
219 oop outgoing_mh_oop = chain().vmtarget_oop();
220 if (!java_lang_invoke_MethodHandle::is_instance(outgoing_mh_oop))
221 lose("outgoing target not a MethodHandle", CHECK_(empty));
222 Handle outgoing_mtype(THREAD, java_lang_invoke_MethodHandle::type(outgoing_mh_oop));
223 outgoing_mh_oop = NULL; // GC safety
225 int nptypes = java_lang_invoke_MethodType::ptype_count(outgoing_mtype());
226 if (nptypes != java_lang_invoke_MethodType::ptype_count(incoming_mtype()))
227 lose("incoming and outgoing parameter count do not agree", CHECK_(empty));
229 for (int i = 0, slot = _outgoing.length() - 1; slot >= 0; slot--) {
230 SlotState* arg_state = slot_state(slot);
231 if (arg_state->_type == T_VOID) continue;
232 ArgToken arg = _outgoing.at(slot)._arg;
234 klassOop in_klass = NULL;
235 klassOop out_klass = NULL;
236 BasicType inpbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(incoming_mtype(), i), &in_klass);
237 BasicType outpbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::ptype(outgoing_mtype(), i), &out_klass);
238 assert(inpbt == arg.basic_type(), "sanity");
240 if (inpbt != outpbt) {
241 vmIntrinsics::ID iid = vmIntrinsics::for_raw_conversion(inpbt, outpbt);
242 if (iid == vmIntrinsics::_none) {
243 lose("no raw conversion method", CHECK_(empty));
244 }
245 ArgToken arglist[2];
246 arglist[0] = arg; // outgoing 'this'
247 arglist[1] = ArgToken(); // sentinel
248 arg = make_invoke(NULL, iid, Bytecodes::_invokestatic, false, 1, &arglist[0], CHECK_(empty));
249 change_argument(inpbt, slot, outpbt, arg);
250 }
252 i++; // We need to skip void slots at the top of the loop.
253 }
255 BasicType inrbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(incoming_mtype()));
256 BasicType outrbt = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(outgoing_mtype()));
257 if (inrbt != outrbt) {
258 if (inrbt == T_INT && outrbt == T_VOID) {
259 // See comments in MethodHandles::same_basic_type_for_arguments.
260 } else {
261 assert(false, "IMPLEMENT ME");
262 lose("no raw conversion method", CHECK_(empty));
263 }
264 }
265 break;
266 }
268 case java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST: {
269 // checkcast the Nth outgoing argument in place
270 klassOop dest_klass = NULL;
271 BasicType dest = java_lang_Class::as_BasicType(chain().adapter_arg_oop(), &dest_klass);
272 assert(dest == T_OBJECT, "");
273 assert(dest == arg_state->_type, "");
274 ArgToken arg = arg_state->_arg;
275 ArgToken new_arg = make_conversion(T_OBJECT, dest_klass, Bytecodes::_checkcast, arg, CHECK_(empty));
276 assert(arg.index() == new_arg.index(), "should be the same index");
277 debug_only(dest_klass = (klassOop)badOop);
278 break;
279 }
281 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM: {
282 // i2l, etc., on the Nth outgoing argument in place
283 BasicType src = chain().adapter_conversion_src_type(),
284 dest = chain().adapter_conversion_dest_type();
285 Bytecodes::Code bc = conversion_code(src, dest);
286 ArgToken arg = arg_state->_arg;
287 if (bc == Bytecodes::_nop) {
288 break;
289 } else if (bc != Bytecodes::_illegal) {
290 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
291 } else if (is_subword_type(dest)) {
292 bc = conversion_code(src, T_INT);
293 if (bc != Bytecodes::_illegal) {
294 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
295 bc = conversion_code(T_INT, dest);
296 arg = make_conversion(dest, NULL, bc, arg, CHECK_(empty));
297 }
298 }
299 if (bc == Bytecodes::_illegal) {
300 lose("bad primitive conversion", CHECK_(empty));
301 }
302 change_argument(src, arg_slot, dest, arg);
303 break;
304 }
306 case java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM: {
307 // checkcast to wrapper type & call intValue, etc.
308 BasicType dest = chain().adapter_conversion_dest_type();
309 ArgToken arg = arg_state->_arg;
310 arg = make_conversion(T_OBJECT, SystemDictionary::box_klass(dest),
311 Bytecodes::_checkcast, arg, CHECK_(empty));
312 vmIntrinsics::ID unboxer = vmIntrinsics::for_unboxing(dest);
313 if (unboxer == vmIntrinsics::_none) {
314 lose("no unboxing method", CHECK_(empty));
315 }
316 ArgToken arglist[2];
317 arglist[0] = arg; // outgoing 'this'
318 arglist[1] = ArgToken(); // sentinel
319 arg = make_invoke(NULL, unboxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_(empty));
320 change_argument(T_OBJECT, arg_slot, dest, arg);
321 break;
322 }
324 case java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF: {
325 // call wrapper type.valueOf
326 BasicType src = chain().adapter_conversion_src_type();
327 ArgToken arg = arg_state->_arg;
328 vmIntrinsics::ID boxer = vmIntrinsics::for_boxing(src);
329 if (boxer == vmIntrinsics::_none) {
330 lose("no boxing method", CHECK_(empty));
331 }
332 ArgToken arglist[2];
333 arglist[0] = arg; // outgoing value
334 arglist[1] = ArgToken(); // sentinel
335 arg = make_invoke(NULL, boxer, Bytecodes::_invokevirtual, false, 1, &arglist[0], CHECK_(empty));
336 change_argument(src, arg_slot, T_OBJECT, arg);
337 break;
338 }
340 case java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS: {
341 int dest_arg_slot = chain().adapter_conversion_vminfo();
342 if (!slot_has_argument(dest_arg_slot)) {
343 lose("bad swap index", CHECK_(empty));
344 }
345 // a simple swap between two arguments
346 SlotState* dest_arg_state = slot_state(dest_arg_slot);
347 SlotState temp = (*dest_arg_state);
348 (*dest_arg_state) = (*arg_state);
349 (*arg_state) = temp;
350 break;
351 }
353 case java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS: {
354 int dest_arg_slot = chain().adapter_conversion_vminfo();
355 if (!slot_has_argument(dest_arg_slot) || arg_slot == dest_arg_slot) {
356 lose("bad rotate index", CHECK_(empty));
357 }
358 SlotState* dest_arg_state = slot_state(dest_arg_slot);
359 // Rotate the source argument (plus following N slots) into the
360 // position occupied by the dest argument (plus following N slots).
361 int rotate_count = type2size[dest_arg_state->_type];
362 // (no other rotate counts are currently supported)
363 if (arg_slot < dest_arg_slot) {
364 for (int i = 0; i < rotate_count; i++) {
365 SlotState temp = _outgoing.at(arg_slot);
366 _outgoing.remove_at(arg_slot);
367 _outgoing.insert_before(dest_arg_slot + rotate_count - 1, temp);
368 }
369 } else { // arg_slot > dest_arg_slot
370 for (int i = 0; i < rotate_count; i++) {
371 SlotState temp = _outgoing.at(arg_slot + rotate_count - 1);
372 _outgoing.remove_at(arg_slot + rotate_count - 1);
373 _outgoing.insert_before(dest_arg_slot, temp);
374 }
375 }
376 break;
377 }
379 case java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS: {
380 int dup_slots = chain().adapter_conversion_stack_pushes();
381 if (dup_slots <= 0) {
382 lose("bad dup count", CHECK_(empty));
383 }
384 for (int i = 0; i < dup_slots; i++) {
385 SlotState* dup = slot_state(arg_slot + 2*i);
386 if (dup == NULL) break; // safety net
387 if (dup->_type != T_VOID) _outgoing_argc += 1;
388 _outgoing.insert_before(i, (*dup));
389 }
390 break;
391 }
393 case java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS: {
394 int drop_slots = -chain().adapter_conversion_stack_pushes();
395 if (drop_slots <= 0) {
396 lose("bad drop count", CHECK_(empty));
397 }
398 for (int i = 0; i < drop_slots; i++) {
399 SlotState* drop = slot_state(arg_slot);
400 if (drop == NULL) break; // safety net
401 if (drop->_type != T_VOID) _outgoing_argc -= 1;
402 _outgoing.remove_at(arg_slot);
403 }
404 break;
405 }
407 case java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS: { //NYI, may GC
408 lose("unimplemented", CHECK_(empty));
409 break;
410 }
412 case java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS: { //NYI, may GC
413 lose("unimplemented", CHECK_(empty));
414 break;
415 }
417 case java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS: {
418 klassOop array_klass_oop = NULL;
419 BasicType array_type = java_lang_Class::as_BasicType(chain().adapter_arg_oop(),
420 &array_klass_oop);
421 assert(array_type == T_OBJECT, "");
422 assert(Klass::cast(array_klass_oop)->oop_is_array(), "");
423 arrayKlassHandle array_klass(THREAD, array_klass_oop);
424 debug_only(array_klass_oop = (klassOop)badOop);
426 klassOop element_klass_oop = NULL;
427 BasicType element_type = java_lang_Class::as_BasicType(array_klass->component_mirror(),
428 &element_klass_oop);
429 KlassHandle element_klass(THREAD, element_klass_oop);
430 debug_only(element_klass_oop = (klassOop)badOop);
432 // Fetch the argument, which we will cast to the required array type.
433 assert(arg_state->_type == T_OBJECT, "");
434 ArgToken array_arg = arg_state->_arg;
435 array_arg = make_conversion(T_OBJECT, array_klass(), Bytecodes::_checkcast, array_arg, CHECK_(empty));
436 change_argument(T_OBJECT, arg_slot, T_VOID, ArgToken(tt_void));
438 // Check the required length.
439 int spread_slots = 1 + chain().adapter_conversion_stack_pushes();
440 int spread_length = spread_slots;
441 if (type2size[element_type] == 2) {
442 if (spread_slots % 2 != 0) spread_slots = -1; // force error
443 spread_length = spread_slots / 2;
444 }
445 if (spread_slots < 0) {
446 lose("bad spread length", CHECK_(empty));
447 }
449 jvalue length_jvalue; length_jvalue.i = spread_length;
450 ArgToken length_arg = make_prim_constant(T_INT, &length_jvalue, CHECK_(empty));
451 // Call a built-in method known to the JVM to validate the length.
452 ArgToken arglist[3];
453 arglist[0] = array_arg; // value to check
454 arglist[1] = length_arg; // length to check
455 arglist[2] = ArgToken(); // sentinel
456 make_invoke(NULL, vmIntrinsics::_checkSpreadArgument,
457 Bytecodes::_invokestatic, false, 3, &arglist[0], CHECK_(empty));
459 // Spread out the array elements.
460 Bytecodes::Code aload_op = Bytecodes::_nop;
461 switch (element_type) {
462 case T_INT: aload_op = Bytecodes::_iaload; break;
463 case T_LONG: aload_op = Bytecodes::_laload; break;
464 case T_FLOAT: aload_op = Bytecodes::_faload; break;
465 case T_DOUBLE: aload_op = Bytecodes::_daload; break;
466 case T_OBJECT: aload_op = Bytecodes::_aaload; break;
467 case T_BOOLEAN: // fall through:
468 case T_BYTE: aload_op = Bytecodes::_baload; break;
469 case T_CHAR: aload_op = Bytecodes::_caload; break;
470 case T_SHORT: aload_op = Bytecodes::_saload; break;
471 default: lose("primitive array NYI", CHECK_(empty));
472 }
473 int ap = arg_slot;
474 for (int i = 0; i < spread_length; i++) {
475 jvalue offset_jvalue; offset_jvalue.i = i;
476 ArgToken offset_arg = make_prim_constant(T_INT, &offset_jvalue, CHECK_(empty));
477 ArgToken element_arg = make_fetch(element_type, element_klass(), aload_op, array_arg, offset_arg, CHECK_(empty));
478 change_argument(T_VOID, ap, element_type, element_arg);
479 ap += type2size[element_type];
480 }
481 break;
482 }
484 default:
485 lose("bad adapter conversion", CHECK_(empty));
486 break;
487 }
488 }
490 if (chain().is_bound()) {
491 // push a new argument
492 BasicType arg_type = chain().bound_arg_type();
493 jint arg_slot = chain().bound_arg_slot();
494 oop arg_oop = chain().bound_arg_oop();
495 ArgToken arg;
496 if (arg_type == T_OBJECT) {
497 arg = make_oop_constant(arg_oop, CHECK_(empty));
498 } else {
499 jvalue arg_value;
500 BasicType bt = java_lang_boxing_object::get_value(arg_oop, &arg_value);
501 if (bt == arg_type) {
502 arg = make_prim_constant(arg_type, &arg_value, CHECK_(empty));
503 } else {
504 lose("bad bound value", CHECK_(empty));
505 }
506 }
507 debug_only(arg_oop = badOop);
508 change_argument(T_VOID, arg_slot, arg_type, arg);
509 }
511 // this test must come after the body of the loop
512 if (!chain().is_last()) {
513 chain().next(CHECK_(empty));
514 } else {
515 break;
516 }
517 }
519 // finish the sequence with a tail-call to the ultimate target
520 // parameters are passed in logical order (recv 1st), not slot order
521 ArgToken* arglist = NEW_RESOURCE_ARRAY(ArgToken, _outgoing.length() + 1);
522 int ap = 0;
523 for (int i = _outgoing.length() - 1; i >= 0; i--) {
524 SlotState* arg_state = slot_state(i);
525 if (arg_state->_type == T_VOID) continue;
526 arglist[ap++] = _outgoing.at(i)._arg;
527 }
528 assert(ap == _outgoing_argc, "");
529 arglist[ap] = ArgToken(); // add a sentinel, for the sake of asserts
530 return make_invoke(chain().last_method_oop(),
531 vmIntrinsics::_none,
532 chain().last_invoke_code(), true,
533 ap, arglist, THREAD);
534 }
537 // -----------------------------------------------------------------------------
538 // MethodHandleWalker::walk_incoming_state
539 //
540 void MethodHandleWalker::walk_incoming_state(TRAPS) {
541 Handle mtype(THREAD, chain().method_type_oop());
542 int nptypes = java_lang_invoke_MethodType::ptype_count(mtype());
543 _outgoing_argc = nptypes;
544 int argp = nptypes - 1;
545 if (argp >= 0) {
546 _outgoing.at_grow(argp, make_state(T_VOID, ArgToken(tt_void))); // presize
547 }
548 for (int i = 0; i < nptypes; i++) {
549 klassOop arg_type_klass = NULL;
550 BasicType arg_type = java_lang_Class::as_BasicType(
551 java_lang_invoke_MethodType::ptype(mtype(), i), &arg_type_klass);
552 int index = new_local_index(arg_type);
553 ArgToken arg = make_parameter(arg_type, arg_type_klass, index, CHECK);
554 debug_only(arg_type_klass = (klassOop) NULL);
555 _outgoing.at_put(argp, make_state(arg_type, arg));
556 if (type2size[arg_type] == 2) {
557 // add the extra slot, so we can model the JVM stack
558 _outgoing.insert_before(argp+1, make_state(T_VOID, ArgToken(tt_void)));
559 }
560 --argp;
561 }
562 // call make_parameter at the end of the list for the return type
563 klassOop ret_type_klass = NULL;
564 BasicType ret_type = java_lang_Class::as_BasicType(
565 java_lang_invoke_MethodType::rtype(mtype()), &ret_type_klass);
566 ArgToken ret = make_parameter(ret_type, ret_type_klass, -1, CHECK);
567 // ignore ret; client can catch it if needed
568 }
571 // -----------------------------------------------------------------------------
572 // MethodHandleWalker::change_argument
573 //
574 // This is messy because some kinds of arguments are paired with
575 // companion slots containing an empty value.
576 void MethodHandleWalker::change_argument(BasicType old_type, int slot, BasicType new_type,
577 const ArgToken& new_arg) {
578 int old_size = type2size[old_type];
579 int new_size = type2size[new_type];
580 if (old_size == new_size) {
581 // simple case first
582 _outgoing.at_put(slot, make_state(new_type, new_arg));
583 } else if (old_size > new_size) {
584 for (int i = old_size - 1; i >= new_size; i--) {
585 assert((i != 0) == (_outgoing.at(slot + i)._type == T_VOID), "");
586 _outgoing.remove_at(slot + i);
587 }
588 if (new_size > 0)
589 _outgoing.at_put(slot, make_state(new_type, new_arg));
590 else
591 _outgoing_argc -= 1; // deleted a real argument
592 } else {
593 for (int i = old_size; i < new_size; i++) {
594 _outgoing.insert_before(slot + i, make_state(T_VOID, ArgToken(tt_void)));
595 }
596 _outgoing.at_put(slot, make_state(new_type, new_arg));
597 if (old_size == 0)
598 _outgoing_argc += 1; // inserted a real argument
599 }
600 }
603 #ifdef ASSERT
604 int MethodHandleWalker::argument_count_slow() {
605 int args_seen = 0;
606 for (int i = _outgoing.length() - 1; i >= 0; i--) {
607 if (_outgoing.at(i)._type != T_VOID) {
608 ++args_seen;
609 }
610 }
611 return args_seen;
612 }
613 #endif
616 // -----------------------------------------------------------------------------
617 // MethodHandleCompiler
619 MethodHandleCompiler::MethodHandleCompiler(Handle root, methodHandle callee, bool is_invokedynamic, TRAPS)
620 : MethodHandleWalker(root, is_invokedynamic, THREAD),
621 _callee(callee),
622 _thread(THREAD),
623 _bytecode(THREAD, 50),
624 _constants(THREAD, 10),
625 _cur_stack(0),
626 _max_stack(0),
627 _rtype(T_ILLEGAL)
628 {
630 // Element zero is always the null constant.
631 (void) _constants.append(NULL);
633 // Set name and signature index.
634 _name_index = cpool_symbol_put(_callee->name());
635 _signature_index = cpool_symbol_put(_callee->signature());
637 // Get return type klass.
638 Handle first_mtype(THREAD, chain().method_type_oop());
639 // _rklass is NULL for primitives.
640 _rtype = java_lang_Class::as_BasicType(java_lang_invoke_MethodType::rtype(first_mtype()), &_rklass);
641 if (_rtype == T_ARRAY) _rtype = T_OBJECT;
643 int params = _callee->size_of_parameters(); // Incoming arguments plus receiver.
644 _num_params = for_invokedynamic() ? params - 1 : params; // XXX Check if callee is static?
645 }
648 // -----------------------------------------------------------------------------
649 // MethodHandleCompiler::compile
650 //
651 // Compile this MethodHandle into a bytecode adapter and return a
652 // methodOop.
653 methodHandle MethodHandleCompiler::compile(TRAPS) {
654 assert(_thread == THREAD, "must be same thread");
655 methodHandle nullHandle;
656 (void) walk(CHECK_(nullHandle));
657 return get_method_oop(CHECK_(nullHandle));
658 }
661 void MethodHandleCompiler::emit_bc(Bytecodes::Code op, int index) {
662 Bytecodes::check(op); // Are we legal?
664 switch (op) {
665 // b
666 case Bytecodes::_aconst_null:
667 case Bytecodes::_iconst_m1:
668 case Bytecodes::_iconst_0:
669 case Bytecodes::_iconst_1:
670 case Bytecodes::_iconst_2:
671 case Bytecodes::_iconst_3:
672 case Bytecodes::_iconst_4:
673 case Bytecodes::_iconst_5:
674 case Bytecodes::_lconst_0:
675 case Bytecodes::_lconst_1:
676 case Bytecodes::_fconst_0:
677 case Bytecodes::_fconst_1:
678 case Bytecodes::_fconst_2:
679 case Bytecodes::_dconst_0:
680 case Bytecodes::_dconst_1:
681 case Bytecodes::_iload_0:
682 case Bytecodes::_iload_1:
683 case Bytecodes::_iload_2:
684 case Bytecodes::_iload_3:
685 case Bytecodes::_lload_0:
686 case Bytecodes::_lload_1:
687 case Bytecodes::_lload_2:
688 case Bytecodes::_lload_3:
689 case Bytecodes::_fload_0:
690 case Bytecodes::_fload_1:
691 case Bytecodes::_fload_2:
692 case Bytecodes::_fload_3:
693 case Bytecodes::_dload_0:
694 case Bytecodes::_dload_1:
695 case Bytecodes::_dload_2:
696 case Bytecodes::_dload_3:
697 case Bytecodes::_aload_0:
698 case Bytecodes::_aload_1:
699 case Bytecodes::_aload_2:
700 case Bytecodes::_aload_3:
701 case Bytecodes::_istore_0:
702 case Bytecodes::_istore_1:
703 case Bytecodes::_istore_2:
704 case Bytecodes::_istore_3:
705 case Bytecodes::_lstore_0:
706 case Bytecodes::_lstore_1:
707 case Bytecodes::_lstore_2:
708 case Bytecodes::_lstore_3:
709 case Bytecodes::_fstore_0:
710 case Bytecodes::_fstore_1:
711 case Bytecodes::_fstore_2:
712 case Bytecodes::_fstore_3:
713 case Bytecodes::_dstore_0:
714 case Bytecodes::_dstore_1:
715 case Bytecodes::_dstore_2:
716 case Bytecodes::_dstore_3:
717 case Bytecodes::_astore_0:
718 case Bytecodes::_astore_1:
719 case Bytecodes::_astore_2:
720 case Bytecodes::_astore_3:
721 case Bytecodes::_i2l:
722 case Bytecodes::_i2f:
723 case Bytecodes::_i2d:
724 case Bytecodes::_i2b:
725 case Bytecodes::_i2c:
726 case Bytecodes::_i2s:
727 case Bytecodes::_l2i:
728 case Bytecodes::_l2f:
729 case Bytecodes::_l2d:
730 case Bytecodes::_f2i:
731 case Bytecodes::_f2l:
732 case Bytecodes::_f2d:
733 case Bytecodes::_d2i:
734 case Bytecodes::_d2l:
735 case Bytecodes::_d2f:
736 case Bytecodes::_ireturn:
737 case Bytecodes::_lreturn:
738 case Bytecodes::_freturn:
739 case Bytecodes::_dreturn:
740 case Bytecodes::_areturn:
741 case Bytecodes::_return:
742 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_b, "wrong bytecode format");
743 _bytecode.push(op);
744 break;
746 // bi
747 case Bytecodes::_ldc:
748 assert(Bytecodes::format_bits(op, false) == (Bytecodes::_fmt_b|Bytecodes::_fmt_has_k), "wrong bytecode format");
749 assert((char) index == index, "index does not fit in 8-bit");
750 _bytecode.push(op);
751 _bytecode.push(index);
752 break;
754 case Bytecodes::_iload:
755 case Bytecodes::_lload:
756 case Bytecodes::_fload:
757 case Bytecodes::_dload:
758 case Bytecodes::_aload:
759 case Bytecodes::_istore:
760 case Bytecodes::_lstore:
761 case Bytecodes::_fstore:
762 case Bytecodes::_dstore:
763 case Bytecodes::_astore:
764 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bi, "wrong bytecode format");
765 assert((char) index == index, "index does not fit in 8-bit");
766 _bytecode.push(op);
767 _bytecode.push(index);
768 break;
770 // bkk
771 case Bytecodes::_ldc_w:
772 case Bytecodes::_ldc2_w:
773 case Bytecodes::_checkcast:
774 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bkk, "wrong bytecode format");
775 assert((short) index == index, "index does not fit in 16-bit");
776 _bytecode.push(op);
777 _bytecode.push(index >> 8);
778 _bytecode.push(index);
779 break;
781 // bJJ
782 case Bytecodes::_invokestatic:
783 case Bytecodes::_invokespecial:
784 case Bytecodes::_invokevirtual:
785 assert(Bytecodes::format_bits(op, false) == Bytecodes::_fmt_bJJ, "wrong bytecode format");
786 assert((short) index == index, "index does not fit in 16-bit");
787 _bytecode.push(op);
788 _bytecode.push(index >> 8);
789 _bytecode.push(index);
790 break;
792 default:
793 ShouldNotReachHere();
794 }
795 }
798 void MethodHandleCompiler::emit_load(BasicType bt, int index) {
799 if (index <= 3) {
800 switch (bt) {
801 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
802 case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_iload_0 + index)); break;
803 case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lload_0 + index)); break;
804 case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fload_0 + index)); break;
805 case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dload_0 + index)); break;
806 case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_aload_0 + index)); break;
807 default:
808 ShouldNotReachHere();
809 }
810 }
811 else {
812 switch (bt) {
813 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
814 case T_INT: emit_bc(Bytecodes::_iload, index); break;
815 case T_LONG: emit_bc(Bytecodes::_lload, index); break;
816 case T_FLOAT: emit_bc(Bytecodes::_fload, index); break;
817 case T_DOUBLE: emit_bc(Bytecodes::_dload, index); break;
818 case T_OBJECT: emit_bc(Bytecodes::_aload, index); break;
819 default:
820 ShouldNotReachHere();
821 }
822 }
823 stack_push(bt);
824 }
826 void MethodHandleCompiler::emit_store(BasicType bt, int index) {
827 if (index <= 3) {
828 switch (bt) {
829 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
830 case T_INT: emit_bc(Bytecodes::cast(Bytecodes::_istore_0 + index)); break;
831 case T_LONG: emit_bc(Bytecodes::cast(Bytecodes::_lstore_0 + index)); break;
832 case T_FLOAT: emit_bc(Bytecodes::cast(Bytecodes::_fstore_0 + index)); break;
833 case T_DOUBLE: emit_bc(Bytecodes::cast(Bytecodes::_dstore_0 + index)); break;
834 case T_OBJECT: emit_bc(Bytecodes::cast(Bytecodes::_astore_0 + index)); break;
835 default:
836 ShouldNotReachHere();
837 }
838 }
839 else {
840 switch (bt) {
841 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
842 case T_INT: emit_bc(Bytecodes::_istore, index); break;
843 case T_LONG: emit_bc(Bytecodes::_lstore, index); break;
844 case T_FLOAT: emit_bc(Bytecodes::_fstore, index); break;
845 case T_DOUBLE: emit_bc(Bytecodes::_dstore, index); break;
846 case T_OBJECT: emit_bc(Bytecodes::_astore, index); break;
847 default:
848 ShouldNotReachHere();
849 }
850 }
851 stack_pop(bt);
852 }
855 void MethodHandleCompiler::emit_load_constant(ArgToken arg) {
856 BasicType bt = arg.basic_type();
857 switch (bt) {
858 case T_INT: {
859 jint value = arg.get_jint();
860 if (-1 <= value && value <= 5)
861 emit_bc(Bytecodes::cast(Bytecodes::_iconst_0 + value));
862 else
863 emit_bc(Bytecodes::_ldc, cpool_int_put(value));
864 break;
865 }
866 case T_LONG: {
867 jlong value = arg.get_jlong();
868 if (0 <= value && value <= 1)
869 emit_bc(Bytecodes::cast(Bytecodes::_lconst_0 + (int) value));
870 else
871 emit_bc(Bytecodes::_ldc2_w, cpool_long_put(value));
872 break;
873 }
874 case T_FLOAT: {
875 jfloat value = arg.get_jfloat();
876 if (value == 0.0 || value == 1.0 || value == 2.0)
877 emit_bc(Bytecodes::cast(Bytecodes::_fconst_0 + (int) value));
878 else
879 emit_bc(Bytecodes::_ldc, cpool_float_put(value));
880 break;
881 }
882 case T_DOUBLE: {
883 jdouble value = arg.get_jdouble();
884 if (value == 0.0 || value == 1.0)
885 emit_bc(Bytecodes::cast(Bytecodes::_dconst_0 + (int) value));
886 else
887 emit_bc(Bytecodes::_ldc2_w, cpool_double_put(value));
888 break;
889 }
890 case T_OBJECT: {
891 Handle value = arg.object();
892 if (value.is_null())
893 emit_bc(Bytecodes::_aconst_null);
894 else
895 emit_bc(Bytecodes::_ldc, cpool_object_put(value));
896 break;
897 }
898 default:
899 ShouldNotReachHere();
900 }
901 stack_push(bt);
902 }
905 MethodHandleWalker::ArgToken
906 MethodHandleCompiler::make_conversion(BasicType type, klassOop tk, Bytecodes::Code op,
907 const ArgToken& src, TRAPS) {
909 BasicType srctype = src.basic_type();
910 int index = src.index();
912 switch (op) {
913 case Bytecodes::_i2l:
914 case Bytecodes::_i2f:
915 case Bytecodes::_i2d:
916 case Bytecodes::_i2b:
917 case Bytecodes::_i2c:
918 case Bytecodes::_i2s:
920 case Bytecodes::_l2i:
921 case Bytecodes::_l2f:
922 case Bytecodes::_l2d:
924 case Bytecodes::_f2i:
925 case Bytecodes::_f2l:
926 case Bytecodes::_f2d:
928 case Bytecodes::_d2i:
929 case Bytecodes::_d2l:
930 case Bytecodes::_d2f:
931 emit_load(srctype, index);
932 stack_pop(srctype); // pop the src type
933 emit_bc(op);
934 stack_push(type); // push the dest value
935 if (srctype != type)
936 index = new_local_index(type);
937 emit_store(type, index);
938 break;
940 case Bytecodes::_checkcast:
941 emit_load(srctype, index);
942 emit_bc(op, cpool_klass_put(tk));
943 emit_store(srctype, index);
944 break;
946 default:
947 ShouldNotReachHere();
948 }
950 return make_parameter(type, tk, index, THREAD);
951 }
954 // -----------------------------------------------------------------------------
955 // MethodHandleCompiler
956 //
958 static jvalue zero_jvalue;
960 // Emit bytecodes for the given invoke instruction.
961 MethodHandleWalker::ArgToken
962 MethodHandleCompiler::make_invoke(methodOop m, vmIntrinsics::ID iid,
963 Bytecodes::Code op, bool tailcall,
964 int argc, MethodHandleWalker::ArgToken* argv,
965 TRAPS) {
966 if (m == NULL) {
967 // Get the intrinsic methodOop.
968 m = vmIntrinsics::method_for(iid);
969 if (m == NULL) {
970 ArgToken zero;
971 lose(vmIntrinsics::name_at(iid), CHECK_(zero));
972 }
973 }
975 klassOop klass = m->method_holder();
976 Symbol* name = m->name();
977 Symbol* signature = m->signature();
979 if (tailcall) {
980 // Actually, in order to make these methods more recognizable,
981 // let's put them in holder class MethodHandle. That way stack
982 // walkers and compiler heuristics can recognize them.
983 _target_klass = SystemDictionary::MethodHandle_klass();
984 }
986 // Inline the method.
987 InvocationCounter* ic = m->invocation_counter();
988 ic->set_carry_flag();
990 for (int i = 0; i < argc; i++) {
991 ArgToken arg = argv[i];
992 TokenType tt = arg.token_type();
993 BasicType bt = arg.basic_type();
995 switch (tt) {
996 case tt_parameter:
997 case tt_temporary:
998 emit_load(bt, arg.index());
999 break;
1000 case tt_constant:
1001 emit_load_constant(arg);
1002 break;
1003 case tt_illegal:
1004 // Sentinel.
1005 assert(i == (argc - 1), "sentinel must be last entry");
1006 break;
1007 case tt_void:
1008 default:
1009 ShouldNotReachHere();
1010 }
1011 }
1013 // Populate constant pool.
1014 int name_index = cpool_symbol_put(name);
1015 int signature_index = cpool_symbol_put(signature);
1016 int name_and_type_index = cpool_name_and_type_put(name_index, signature_index);
1017 int klass_index = cpool_klass_put(klass);
1018 int methodref_index = cpool_methodref_put(klass_index, name_and_type_index);
1020 // Generate invoke.
1021 switch (op) {
1022 case Bytecodes::_invokestatic:
1023 case Bytecodes::_invokespecial:
1024 case Bytecodes::_invokevirtual:
1025 emit_bc(op, methodref_index);
1026 break;
1027 case Bytecodes::_invokeinterface:
1028 Unimplemented();
1029 break;
1030 default:
1031 ShouldNotReachHere();
1032 }
1034 // If tailcall, we have walked all the way to a direct method handle.
1035 // Otherwise, make a recursive call to some helper routine.
1036 BasicType rbt = m->result_type();
1037 if (rbt == T_ARRAY) rbt = T_OBJECT;
1038 ArgToken ret;
1039 if (tailcall) {
1040 if (rbt != _rtype) {
1041 if (rbt == T_VOID) {
1042 // push a zero of the right sort
1043 ArgToken zero;
1044 if (_rtype == T_OBJECT) {
1045 zero = make_oop_constant(NULL, CHECK_(zero));
1046 } else {
1047 zero = make_prim_constant(_rtype, &zero_jvalue, CHECK_(zero));
1048 }
1049 emit_load_constant(zero);
1050 } else if (_rtype == T_VOID) {
1051 // We'll emit a _return with something on the stack.
1052 // It's OK to ignore what's on the stack.
1053 } else {
1054 tty->print_cr("*** rbt=%d != rtype=%d", rbt, _rtype);
1055 assert(false, "IMPLEMENT ME");
1056 }
1057 }
1058 switch (_rtype) {
1059 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1060 case T_INT: emit_bc(Bytecodes::_ireturn); break;
1061 case T_LONG: emit_bc(Bytecodes::_lreturn); break;
1062 case T_FLOAT: emit_bc(Bytecodes::_freturn); break;
1063 case T_DOUBLE: emit_bc(Bytecodes::_dreturn); break;
1064 case T_VOID: emit_bc(Bytecodes::_return); break;
1065 case T_OBJECT:
1066 if (_rklass.not_null() && _rklass() != SystemDictionary::Object_klass())
1067 emit_bc(Bytecodes::_checkcast, cpool_klass_put(_rklass()));
1068 emit_bc(Bytecodes::_areturn);
1069 break;
1070 default: ShouldNotReachHere();
1071 }
1072 ret = ArgToken(); // Dummy return value.
1073 }
1074 else {
1075 stack_push(rbt); // The return value is already pushed onto the stack.
1076 int index = new_local_index(rbt);
1077 switch (rbt) {
1078 case T_BOOLEAN: case T_BYTE: case T_CHAR: case T_SHORT:
1079 case T_INT: case T_LONG: case T_FLOAT: case T_DOUBLE:
1080 case T_OBJECT:
1081 emit_store(rbt, index);
1082 ret = ArgToken(tt_temporary, rbt, index);
1083 break;
1084 case T_VOID:
1085 ret = ArgToken(tt_void);
1086 break;
1087 default:
1088 ShouldNotReachHere();
1089 }
1090 }
1092 return ret;
1093 }
1095 MethodHandleWalker::ArgToken
1096 MethodHandleCompiler::make_fetch(BasicType type, klassOop tk, Bytecodes::Code op,
1097 const MethodHandleWalker::ArgToken& base,
1098 const MethodHandleWalker::ArgToken& offset,
1099 TRAPS) {
1100 Unimplemented();
1101 return ArgToken();
1102 }
1105 int MethodHandleCompiler::cpool_primitive_put(BasicType bt, jvalue* con) {
1106 jvalue con_copy;
1107 assert(bt < T_OBJECT, "");
1108 if (type2aelembytes(bt) < jintSize) {
1109 // widen to int
1110 con_copy = (*con);
1111 con = &con_copy;
1112 switch (bt) {
1113 case T_BOOLEAN: con->i = (con->z ? 1 : 0); break;
1114 case T_BYTE: con->i = con->b; break;
1115 case T_CHAR: con->i = con->c; break;
1116 case T_SHORT: con->i = con->s; break;
1117 default: ShouldNotReachHere();
1118 }
1119 bt = T_INT;
1120 }
1122 // for (int i = 1, imax = _constants.length(); i < imax; i++) {
1123 // ConstantValue* con = _constants.at(i);
1124 // if (con != NULL && con->is_primitive() && con->_type == bt) {
1125 // bool match = false;
1126 // switch (type2size[bt]) {
1127 // case 1: if (pcon->_value.i == con->i) match = true; break;
1128 // case 2: if (pcon->_value.j == con->j) match = true; break;
1129 // }
1130 // if (match)
1131 // return i;
1132 // }
1133 // }
1134 ConstantValue* cv = new ConstantValue(bt, *con);
1135 int index = _constants.append(cv);
1137 // long and double entries take 2 slots, we add another empty entry.
1138 if (type2size[bt] == 2)
1139 (void) _constants.append(NULL);
1141 return index;
1142 }
1145 constantPoolHandle MethodHandleCompiler::get_constant_pool(TRAPS) const {
1146 constantPoolHandle nullHandle;
1147 constantPoolOop cpool_oop = oopFactory::new_constantPool(_constants.length(),
1148 oopDesc::IsSafeConc,
1149 CHECK_(nullHandle));
1150 constantPoolHandle cpool(THREAD, cpool_oop);
1152 // Fill the real constant pool skipping the zero element.
1153 for (int i = 1; i < _constants.length(); i++) {
1154 ConstantValue* cv = _constants.at(i);
1155 switch (cv->tag()) {
1156 case JVM_CONSTANT_Utf8: cpool->symbol_at_put( i, cv->symbol() ); break;
1157 case JVM_CONSTANT_Integer: cpool->int_at_put( i, cv->get_jint() ); break;
1158 case JVM_CONSTANT_Float: cpool->float_at_put( i, cv->get_jfloat() ); break;
1159 case JVM_CONSTANT_Long: cpool->long_at_put( i, cv->get_jlong() ); break;
1160 case JVM_CONSTANT_Double: cpool->double_at_put( i, cv->get_jdouble() ); break;
1161 case JVM_CONSTANT_Class: cpool->klass_at_put( i, cv->klass_oop() ); break;
1162 case JVM_CONSTANT_Methodref: cpool->method_at_put( i, cv->first_index(), cv->second_index()); break;
1163 case JVM_CONSTANT_NameAndType: cpool->name_and_type_at_put(i, cv->first_index(), cv->second_index()); break;
1164 case JVM_CONSTANT_Object: cpool->object_at_put( i, cv->object_oop() ); break;
1165 default: ShouldNotReachHere();
1166 }
1168 switch (cv->tag()) {
1169 case JVM_CONSTANT_Long:
1170 case JVM_CONSTANT_Double:
1171 i++; // Skip empty entry.
1172 assert(_constants.at(i) == NULL, "empty entry");
1173 break;
1174 }
1175 }
1177 // Set the constant pool holder to the target method's class.
1178 cpool->set_pool_holder(_target_klass());
1180 return cpool;
1181 }
1184 methodHandle MethodHandleCompiler::get_method_oop(TRAPS) const {
1185 methodHandle nullHandle;
1186 // Create a method that holds the generated bytecode. invokedynamic
1187 // has no receiver, normal MH calls do.
1188 int flags_bits;
1189 if (for_invokedynamic())
1190 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC | JVM_ACC_STATIC);
1191 else
1192 flags_bits = (/*JVM_MH_INVOKE_BITS |*/ JVM_ACC_PUBLIC | JVM_ACC_FINAL | JVM_ACC_SYNTHETIC);
1194 methodOop m_oop = oopFactory::new_method(bytecode_length(),
1195 accessFlags_from(flags_bits),
1196 0, 0, 0, oopDesc::IsSafeConc, CHECK_(nullHandle));
1197 methodHandle m(THREAD, m_oop);
1198 m_oop = NULL; // oop not GC safe
1200 constantPoolHandle cpool = get_constant_pool(CHECK_(nullHandle));
1201 m->set_constants(cpool());
1203 m->set_name_index(_name_index);
1204 m->set_signature_index(_signature_index);
1206 m->set_code((address) bytecode());
1208 m->set_max_stack(_max_stack);
1209 m->set_max_locals(max_locals());
1210 m->set_size_of_parameters(_num_params);
1212 typeArrayHandle exception_handlers(THREAD, Universe::the_empty_int_array());
1213 m->set_exception_table(exception_handlers());
1215 // Set the carry bit of the invocation counter to force inlining of
1216 // the adapter.
1217 InvocationCounter* ic = m->invocation_counter();
1218 ic->set_carry_flag();
1220 // Rewrite the method and set up the constant pool cache.
1221 objArrayOop m_array = oopFactory::new_system_objArray(1, CHECK_(nullHandle));
1222 objArrayHandle methods(THREAD, m_array);
1223 methods->obj_at_put(0, m());
1224 Rewriter::rewrite(_target_klass(), cpool, methods, CHECK_(nullHandle)); // Use fake class.
1226 #ifndef PRODUCT
1227 if (TraceMethodHandles) {
1228 m->print();
1229 m->print_codes();
1230 }
1231 #endif //PRODUCT
1233 assert(m->is_method_handle_adapter(), "must be recognized as an adapter");
1234 return m;
1235 }
1238 #ifndef PRODUCT
1240 #if 0
1241 // MH printer for debugging.
1243 class MethodHandlePrinter : public MethodHandleWalker {
1244 private:
1245 outputStream* _out;
1246 bool _verbose;
1247 int _temp_num;
1248 stringStream _strbuf;
1249 const char* strbuf() {
1250 const char* s = _strbuf.as_string();
1251 _strbuf.reset();
1252 return s;
1253 }
1254 ArgToken token(const char* str) {
1255 return (ArgToken) str;
1256 }
1257 void start_params() {
1258 _out->print("(");
1259 }
1260 void end_params() {
1261 if (_verbose) _out->print("\n");
1262 _out->print(") => {");
1263 }
1264 void put_type_name(BasicType type, klassOop tk, outputStream* s) {
1265 const char* kname = NULL;
1266 if (tk != NULL)
1267 kname = Klass::cast(tk)->external_name();
1268 s->print("%s", (kname != NULL) ? kname : type2name(type));
1269 }
1270 ArgToken maybe_make_temp(const char* statement_op, BasicType type, const char* temp_name) {
1271 const char* value = strbuf();
1272 if (!_verbose) return token(value);
1273 // make an explicit binding for each separate value
1274 _strbuf.print("%s%d", temp_name, ++_temp_num);
1275 const char* temp = strbuf();
1276 _out->print("\n %s %s %s = %s;", statement_op, type2name(type), temp, value);
1277 return token(temp);
1278 }
1280 public:
1281 MethodHandlePrinter(Handle root, bool verbose, outputStream* out, TRAPS)
1282 : MethodHandleWalker(root, THREAD),
1283 _out(out),
1284 _verbose(verbose),
1285 _temp_num(0)
1286 {
1287 start_params();
1288 }
1289 virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) {
1290 if (argnum < 0) {
1291 end_params();
1292 return NULL;
1293 }
1294 if (argnum == 0) {
1295 _out->print(_verbose ? "\n " : "");
1296 } else {
1297 _out->print(_verbose ? ",\n " : ", ");
1298 }
1299 if (argnum >= _temp_num)
1300 _temp_num = argnum;
1301 // generate an argument name
1302 _strbuf.print("a%d", argnum);
1303 const char* arg = strbuf();
1304 put_type_name(type, tk, _out);
1305 _out->print(" %s", arg);
1306 return token(arg);
1307 }
1308 virtual ArgToken make_oop_constant(oop con, TRAPS) {
1309 if (con == NULL)
1310 _strbuf.print("null");
1311 else
1312 con->print_value_on(&_strbuf);
1313 if (_strbuf.size() == 0) { // yuck
1314 _strbuf.print("(a ");
1315 put_type_name(T_OBJECT, con->klass(), &_strbuf);
1316 _strbuf.print(")");
1317 }
1318 return maybe_make_temp("constant", T_OBJECT, "k");
1319 }
1320 virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) {
1321 java_lang_boxing_object::print(type, con, &_strbuf);
1322 return maybe_make_temp("constant", type, "k");
1323 }
1324 virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, ArgToken src, TRAPS) {
1325 _strbuf.print("%s(%s", Bytecodes::name(op), (const char*)src);
1326 if (tk != NULL) {
1327 _strbuf.print(", ");
1328 put_type_name(type, tk, &_strbuf);
1329 }
1330 _strbuf.print(")");
1331 return maybe_make_temp("convert", type, "v");
1332 }
1333 virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, ArgToken base, ArgToken offset, TRAPS) {
1334 _strbuf.print("%s(%s, %s", Bytecodes::name(op), (const char*)base, (const char*)offset);
1335 if (tk != NULL) {
1336 _strbuf.print(", ");
1337 put_type_name(type, tk, &_strbuf);
1338 }
1339 _strbuf.print(")");
1340 return maybe_make_temp("fetch", type, "x");
1341 }
1342 virtual ArgToken make_invoke(methodOop m, vmIntrinsics::ID iid,
1343 Bytecodes::Code op, bool tailcall,
1344 int argc, ArgToken* argv, TRAPS) {
1345 Symbol* name, sig;
1346 if (m != NULL) {
1347 name = m->name();
1348 sig = m->signature();
1349 } else {
1350 name = vmSymbols::symbol_at(vmIntrinsics::name_for(iid));
1351 sig = vmSymbols::symbol_at(vmIntrinsics::signature_for(iid));
1352 }
1353 _strbuf.print("%s %s%s(", Bytecodes::name(op), name->as_C_string(), sig->as_C_string());
1354 for (int i = 0; i < argc; i++) {
1355 _strbuf.print("%s%s", (i > 0 ? ", " : ""), (const char*)argv[i]);
1356 }
1357 _strbuf.print(")");
1358 if (!tailcall) {
1359 BasicType rt = char2type(sig->byte_at(sig->utf8_length()-1));
1360 if (rt == T_ILLEGAL) rt = T_OBJECT; // ';' at the end of '(...)L...;'
1361 return maybe_make_temp("invoke", rt, "x");
1362 } else {
1363 const char* ret = strbuf();
1364 _out->print(_verbose ? "\n return " : " ");
1365 _out->print("%s", ret);
1366 _out->print(_verbose ? "\n}\n" : " }");
1367 }
1368 return ArgToken();
1369 }
1371 virtual void set_method_handle(oop mh) {
1372 if (WizardMode && Verbose) {
1373 tty->print("\n--- next target: ");
1374 mh->print();
1375 }
1376 }
1378 static void print(Handle root, bool verbose, outputStream* out, TRAPS) {
1379 ResourceMark rm;
1380 MethodHandlePrinter printer(root, verbose, out, CHECK);
1381 printer.walk(CHECK);
1382 out->print("\n");
1383 }
1384 static void print(Handle root, bool verbose = Verbose, outputStream* out = tty) {
1385 EXCEPTION_MARK;
1386 ResourceMark rm;
1387 MethodHandlePrinter printer(root, verbose, out, THREAD);
1388 if (!HAS_PENDING_EXCEPTION)
1389 printer.walk(THREAD);
1390 if (HAS_PENDING_EXCEPTION) {
1391 oop ex = PENDING_EXCEPTION;
1392 CLEAR_PENDING_EXCEPTION;
1393 out->print("\n*** ");
1394 if (ex != Universe::virtual_machine_error_instance())
1395 ex->print_on(out);
1396 else
1397 out->print("lose: %s", printer.lose_message());
1398 out->print("\n}\n");
1399 }
1400 out->print("\n");
1401 }
1402 };
1403 #endif // 0
1405 extern "C"
1406 void print_method_handle(oop mh) {
1407 if (!mh->is_oop()) {
1408 tty->print_cr("*** not a method handle: "INTPTR_FORMAT, (intptr_t)mh);
1409 } else if (java_lang_invoke_MethodHandle::is_instance(mh)) {
1410 //MethodHandlePrinter::print(mh);
1411 } else {
1412 tty->print("*** not a method handle: ");
1413 mh->print();
1414 }
1415 }
1417 #endif // PRODUCT