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