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