src/share/vm/prims/methodHandleWalk.cpp

Tue, 17 May 2011 19:11:51 -0700

author
never
date
Tue, 17 May 2011 19:11:51 -0700
changeset 2920
a80577f854f9
parent 2917
33ae33516634
child 2937
5ac411b3b8fc
child 2945
d3b9f2be46ab
permissions
-rw-r--r--

7045513: JSR 292 inlining causes crashes in methodHandleWalk.cpp
Reviewed-by: jrose

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

mercurial