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