1 /* 2 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_PRIMS_METHODHANDLEWALK_HPP 26 #define SHARE_VM_PRIMS_METHODHANDLEWALK_HPP 27 28 #include "prims/methodHandles.hpp" 29 30 // Low-level parser for method handle chains. 31 class MethodHandleChain : StackObj { 32 public: 33 typedef MethodHandles::EntryKind EntryKind; 34 35 private: 36 Handle _root; // original target 37 Handle _method_handle; // current target 38 bool _is_last; // final guy in chain 39 bool _is_bound; // has a bound argument 40 BasicType _arg_type; // if is_bound, the bound argument type 41 int _arg_slot; // if is_bound or is_adapter, affected argument slot 42 jint _conversion; // conversion field of AMH or -1 43 methodHandle _last_method; // if is_last, which method we target 44 Bytecodes::Code _last_invoke; // if is_last, type of invoke 45 const char* _lose_message; // saved argument to lose() 46 47 void set_method_handle(Handle target, TRAPS); 48 void set_last_method(oop target, TRAPS); 49 static BasicType compute_bound_arg_type(oop target, methodOop m, int arg_slot, TRAPS); 50 51 oop MethodHandle_type_oop() { return java_lang_invoke_MethodHandle::type(method_handle_oop()); } 52 oop MethodHandle_vmtarget_oop() { return java_lang_invoke_MethodHandle::vmtarget(method_handle_oop()); } 53 int MethodHandle_vmslots() { return java_lang_invoke_MethodHandle::vmslots(method_handle_oop()); } 54 int DirectMethodHandle_vmindex() { return java_lang_invoke_DirectMethodHandle::vmindex(method_handle_oop()); } 55 oop BoundMethodHandle_argument_oop() { return java_lang_invoke_BoundMethodHandle::argument(method_handle_oop()); } 56 int BoundMethodHandle_vmargslot() { return java_lang_invoke_BoundMethodHandle::vmargslot(method_handle_oop()); } 57 int AdapterMethodHandle_conversion() { return java_lang_invoke_AdapterMethodHandle::conversion(method_handle_oop()); } 58 59 public: 60 MethodHandleChain(Handle root, TRAPS) 61 : _root(root) 62 { set_method_handle(root, THREAD); } 63 64 bool is_adapter() { return _conversion != -1; } 65 bool is_bound() { return _is_bound; } 66 bool is_last() { return _is_last; } 67 68 void next(TRAPS) { 69 assert(!is_last(), ""); 70 set_method_handle(MethodHandle_vmtarget_oop(), THREAD); 71 } 72 73 Handle method_handle() { return _method_handle; } 74 oop method_handle_oop() { return _method_handle(); } 75 oop method_type_oop() { return MethodHandle_type_oop(); } 76 oop vmtarget_oop() { return MethodHandle_vmtarget_oop(); } 77 78 jint adapter_conversion() { assert(is_adapter(), ""); return _conversion; } 79 int adapter_conversion_op() { return MethodHandles::adapter_conversion_op(adapter_conversion()); } 80 BasicType adapter_conversion_src_type() 81 { return MethodHandles::adapter_conversion_src_type(adapter_conversion()); } 82 BasicType adapter_conversion_dest_type() 83 { return MethodHandles::adapter_conversion_dest_type(adapter_conversion()); } 84 int adapter_conversion_stack_move() 85 { return MethodHandles::adapter_conversion_stack_move(adapter_conversion()); } 86 int adapter_conversion_stack_pushes() 87 { return adapter_conversion_stack_move() / MethodHandles::stack_move_unit(); } 88 int adapter_conversion_vminfo() 89 { return MethodHandles::adapter_conversion_vminfo(adapter_conversion()); } 90 int adapter_arg_slot() { assert(is_adapter(), ""); return _arg_slot; } 91 oop adapter_arg_oop() { assert(is_adapter(), ""); return BoundMethodHandle_argument_oop(); } 92 93 BasicType bound_arg_type() { assert(is_bound(), ""); return _arg_type; } 94 int bound_arg_slot() { assert(is_bound(), ""); return _arg_slot; } 95 oop bound_arg_oop() { assert(is_bound(), ""); return BoundMethodHandle_argument_oop(); } 96 97 methodOop last_method_oop() { assert(is_last(), ""); return _last_method(); } 98 Bytecodes::Code last_invoke_code() { assert(is_last(), ""); return _last_invoke; } 99 100 void lose(const char* msg, TRAPS); 101 const char* lose_message() { return _lose_message; } 102 }; 103 104 105 // Structure walker for method handles. 106 // Does abstract interpretation on top of low-level parsing. 107 // You supply the tokens shuffled by the abstract interpretation. 108 class MethodHandleWalker : StackObj { 109 public: 110 // Stack values: 111 enum TokenType { 112 tt_void, 113 tt_parameter, 114 tt_temporary, 115 tt_constant, 116 tt_illegal 117 }; 118 119 // Argument token: 120 class ArgToken { 121 private: 122 TokenType _tt; 123 BasicType _bt; 124 jvalue _value; 125 Handle _handle; 126 127 public: 128 ArgToken(TokenType tt = tt_illegal) : _tt(tt) {} 129 ArgToken(TokenType tt, BasicType bt, jvalue value) : _tt(tt), _bt(bt), _value(value) {} 130 131 ArgToken(TokenType tt, BasicType bt, int index) : _tt(tt), _bt(bt) { 132 _value.i = index; 133 } 134 135 ArgToken(TokenType tt, BasicType bt, Handle value) : _tt(tt), _bt(bt) { 136 _handle = value; 137 } 138 139 TokenType token_type() const { return _tt; } 140 BasicType basic_type() const { return _bt; } 141 int index() const { return _value.i; } 142 Handle object() const { return _handle; } 143 144 jint get_jint() const { return _value.i; } 145 jlong get_jlong() const { return _value.j; } 146 jfloat get_jfloat() const { return _value.f; } 147 jdouble get_jdouble() const { return _value.d; } 148 }; 149 150 // Abstract interpretation state: 151 struct SlotState { 152 BasicType _type; 153 ArgToken _arg; 154 SlotState() : _type(), _arg() {} 155 }; 156 static SlotState make_state(BasicType type, ArgToken arg) { 157 SlotState ss; 158 ss._type = type; ss._arg = arg; 159 return ss; 160 } 161 162 private: 163 MethodHandleChain _chain; 164 bool _for_invokedynamic; 165 int _local_index; 166 167 GrowableArray<SlotState> _outgoing; // current outgoing parameter slots 168 int _outgoing_argc; // # non-empty outgoing slots 169 170 // Replace a value of type old_type at slot (and maybe slot+1) with the new value. 171 // If old_type != T_VOID, remove the old argument at that point. 172 // If new_type != T_VOID, insert the new argument at that point. 173 // Insert or delete a second empty slot as needed. 174 void change_argument(BasicType old_type, int slot, BasicType new_type, const ArgToken& new_arg); 175 176 SlotState* slot_state(int slot) { 177 if (slot < 0 || slot >= _outgoing.length()) 178 return NULL; 179 return _outgoing.adr_at(slot); 180 } 181 BasicType slot_type(int slot) { 182 SlotState* ss = slot_state(slot); 183 if (ss == NULL) 184 return T_ILLEGAL; 185 return ss->_type; 186 } 187 bool slot_has_argument(int slot) { 188 return slot_type(slot) < T_VOID; 189 } 190 191 #ifdef ASSERT 192 int argument_count_slow(); 193 #endif 194 195 // Return a bytecode for converting src to dest, if one exists. 196 Bytecodes::Code conversion_code(BasicType src, BasicType dest); 197 198 void walk_incoming_state(TRAPS); 199 200 public: 201 MethodHandleWalker(Handle root, bool for_invokedynamic, TRAPS) 202 : _chain(root, THREAD), 203 _for_invokedynamic(for_invokedynamic), 204 _outgoing(THREAD, 10), 205 _outgoing_argc(0) 206 { 207 _local_index = for_invokedynamic ? 0 : 1; 208 } 209 210 MethodHandleChain& chain() { return _chain; } 211 212 bool for_invokedynamic() const { return _for_invokedynamic; } 213 214 int new_local_index(BasicType bt) { 215 //int index = _for_invokedynamic ? _local_index : _local_index - 1; 216 int index = _local_index; 217 _local_index += type2size[bt]; 218 return index; 219 } 220 221 int max_locals() const { return _local_index; } 222 223 // plug-in abstract interpretation steps: 224 virtual ArgToken make_parameter( BasicType type, klassOop tk, int argnum, TRAPS ) = 0; 225 virtual ArgToken make_prim_constant( BasicType type, jvalue* con, TRAPS ) = 0; 226 virtual ArgToken make_oop_constant( oop con, TRAPS ) = 0; 227 virtual ArgToken make_conversion( BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS ) = 0; 228 virtual ArgToken make_fetch( BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS ) = 0; 229 virtual ArgToken make_invoke( methodOop m, vmIntrinsics::ID iid, Bytecodes::Code op, bool tailcall, int argc, ArgToken* argv, TRAPS ) = 0; 230 231 // For make_invoke, the methodOop can be NULL if the intrinsic ID 232 // is something other than vmIntrinsics::_none. 233 234 // and in case anyone cares to related the previous actions to the chain: 235 virtual void set_method_handle(oop mh) { } 236 237 void lose(const char* msg, TRAPS) { chain().lose(msg, THREAD); } 238 const char* lose_message() { return chain().lose_message(); } 239 240 ArgToken walk(TRAPS); 241 }; 242 243 244 // An abstract interpreter for method handle chains. 245 // Produces an account of the semantics of a chain, in terms of a static IR. 246 // The IR happens to be JVM bytecodes. 247 class MethodHandleCompiler : public MethodHandleWalker { 248 private: 249 methodHandle _callee; 250 int _invoke_count; // count the original call site has been executed 251 KlassHandle _rklass; // Return type for casting. 252 BasicType _rtype; 253 KlassHandle _target_klass; 254 Thread* _thread; 255 256 // Fake constant pool entry. 257 class ConstantValue { 258 private: 259 int _tag; // Constant pool tag type. 260 JavaValue _value; 261 Handle _handle; 262 Symbol* _sym; 263 264 public: 265 // Constructor for oop types. 266 ConstantValue(int tag, Handle con) : _tag(tag), _handle(con) { 267 assert(tag == JVM_CONSTANT_Class || 268 tag == JVM_CONSTANT_String || 269 tag == JVM_CONSTANT_Object, "must be oop type"); 270 } 271 272 ConstantValue(int tag, Symbol* con) : _tag(tag), _sym(con) { 273 assert(tag == JVM_CONSTANT_Utf8, "must be symbol type"); 274 } 275 276 // Constructor for oop reference types. 277 ConstantValue(int tag, int index) : _tag(tag) { 278 assert(JVM_CONSTANT_Fieldref <= tag && tag <= JVM_CONSTANT_NameAndType, "must be ref type"); 279 _value.set_jint(index); 280 } 281 ConstantValue(int tag, int first_index, int second_index) : _tag(tag) { 282 assert(JVM_CONSTANT_Fieldref <= tag && tag <= JVM_CONSTANT_NameAndType, "must be ref type"); 283 _value.set_jint(first_index << 16 | second_index); 284 } 285 286 // Constructor for primitive types. 287 ConstantValue(BasicType bt, jvalue con) { 288 _value.set_type(bt); 289 switch (bt) { 290 case T_INT: _tag = JVM_CONSTANT_Integer; _value.set_jint( con.i); break; 291 case T_LONG: _tag = JVM_CONSTANT_Long; _value.set_jlong( con.j); break; 292 case T_FLOAT: _tag = JVM_CONSTANT_Float; _value.set_jfloat( con.f); break; 293 case T_DOUBLE: _tag = JVM_CONSTANT_Double; _value.set_jdouble(con.d); break; 294 default: ShouldNotReachHere(); 295 } 296 } 297 298 int tag() const { return _tag; } 299 Symbol* symbol() const { return _sym; } 300 klassOop klass_oop() const { return (klassOop) _handle(); } 301 oop object_oop() const { return _handle(); } 302 int index() const { return _value.get_jint(); } 303 int first_index() const { return _value.get_jint() >> 16; } 304 int second_index() const { return _value.get_jint() & 0x0000FFFF; } 305 306 bool is_primitive() const { return is_java_primitive(_value.get_type()); } 307 jint get_jint() const { return _value.get_jint(); } 308 jlong get_jlong() const { return _value.get_jlong(); } 309 jfloat get_jfloat() const { return _value.get_jfloat(); } 310 jdouble get_jdouble() const { return _value.get_jdouble(); } 311 }; 312 313 // Fake constant pool. 314 GrowableArray<ConstantValue*> _constants; 315 316 // Accumulated compiler state: 317 GrowableArray<unsigned char> _bytecode; 318 319 int _cur_stack; 320 int _max_stack; 321 int _num_params; 322 int _name_index; 323 int _signature_index; 324 325 void stack_push(BasicType bt) { 326 _cur_stack += type2size[bt]; 327 if (_cur_stack > _max_stack) _max_stack = _cur_stack; 328 } 329 void stack_pop(BasicType bt) { 330 _cur_stack -= type2size[bt]; 331 assert(_cur_stack >= 0, "sanity"); 332 } 333 334 unsigned char* bytecode() const { return _bytecode.adr_at(0); } 335 int bytecode_length() const { return _bytecode.length(); } 336 337 // Fake constant pool. 338 int cpool_oop_put(int tag, Handle con) { 339 if (con.is_null()) return 0; 340 ConstantValue* cv = new ConstantValue(tag, con); 341 return _constants.append(cv); 342 } 343 344 int cpool_symbol_put(int tag, Symbol* con) { 345 if (con == NULL) return 0; 346 ConstantValue* cv = new ConstantValue(tag, con); 347 con->increment_refcount(); 348 return _constants.append(cv); 349 } 350 351 int cpool_oop_reference_put(int tag, int first_index, int second_index) { 352 if (first_index == 0 && second_index == 0) return 0; 353 assert(first_index != 0 && second_index != 0, "no zero indexes"); 354 ConstantValue* cv = new ConstantValue(tag, first_index, second_index); 355 return _constants.append(cv); 356 } 357 358 int cpool_primitive_put(BasicType type, jvalue* con); 359 360 int cpool_int_put(jint value) { 361 jvalue con; con.i = value; 362 return cpool_primitive_put(T_INT, &con); 363 } 364 int cpool_long_put(jlong value) { 365 jvalue con; con.j = value; 366 return cpool_primitive_put(T_LONG, &con); 367 } 368 int cpool_float_put(jfloat value) { 369 jvalue con; con.f = value; 370 return cpool_primitive_put(T_FLOAT, &con); 371 } 372 int cpool_double_put(jdouble value) { 373 jvalue con; con.d = value; 374 return cpool_primitive_put(T_DOUBLE, &con); 375 } 376 377 int cpool_object_put(Handle obj) { 378 return cpool_oop_put(JVM_CONSTANT_Object, obj); 379 } 380 int cpool_symbol_put(Symbol* sym) { 381 return cpool_symbol_put(JVM_CONSTANT_Utf8, sym); 382 } 383 int cpool_klass_put(klassOop klass) { 384 return cpool_oop_put(JVM_CONSTANT_Class, klass); 385 } 386 int cpool_methodref_put(int class_index, int name_and_type_index) { 387 return cpool_oop_reference_put(JVM_CONSTANT_Methodref, class_index, name_and_type_index); 388 } 389 int cpool_name_and_type_put(int name_index, int signature_index) { 390 return cpool_oop_reference_put(JVM_CONSTANT_NameAndType, name_index, signature_index); 391 } 392 393 void emit_bc(Bytecodes::Code op, int index = 0); 394 void emit_load(BasicType bt, int index); 395 void emit_store(BasicType bt, int index); 396 void emit_load_constant(ArgToken arg); 397 398 virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) { 399 return ArgToken(tt_parameter, type, argnum); 400 } 401 virtual ArgToken make_oop_constant(oop con, TRAPS) { 402 Handle h(THREAD, con); 403 return ArgToken(tt_constant, T_OBJECT, h); 404 } 405 virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) { 406 return ArgToken(tt_constant, type, *con); 407 } 408 409 virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS); 410 virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS); 411 virtual ArgToken make_invoke(methodOop m, vmIntrinsics::ID iid, Bytecodes::Code op, bool tailcall, int argc, ArgToken* argv, TRAPS); 412 413 // Get a real constant pool. 414 constantPoolHandle get_constant_pool(TRAPS) const; 415 416 // Get a real methodOop. 417 methodHandle get_method_oop(TRAPS) const; 418 419 public: 420 MethodHandleCompiler(Handle root, methodHandle call_method, int invoke_count, bool for_invokedynamic, TRAPS); 421 422 // Compile the given MH chain into bytecode. 423 methodHandle compile(TRAPS); 424 425 // Tests if the given class is a MH adapter holder. 426 static bool klass_is_method_handle_adapter_holder(klassOop klass) { 427 return (klass == SystemDictionary::MethodHandle_klass()); 428 } 429 }; 430 431 #endif // SHARE_VM_PRIMS_METHODHANDLEWALK_HPP