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