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