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