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