Tue, 11 May 2010 14:35:43 -0700
6931180: Migration to recent versions of MS Platform SDK
6951582: Build problems on win64
Summary: Changes to enable building JDK7 with Microsoft Visual Studio 2010
Reviewed-by: ohair, art, ccheung, dcubed
1 /*
2 * Copyright 2008-2010 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 */
25 // Low-level parser for method handle chains.
26 class MethodHandleChain : StackObj {
27 public:
28 typedef MethodHandles::EntryKind EntryKind;
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()
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);
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()); }
54 public:
55 MethodHandleChain(Handle root, TRAPS)
56 : _root(root)
57 { set_method_handle(root, THREAD); }
59 bool is_adapter() { return _conversion != -1; }
60 bool is_bound() { return _is_bound; }
61 bool is_last() { return _is_last; }
63 void next(TRAPS) {
64 assert(!is_last(), "");
65 set_method_handle(MethodHandle_vmtarget_oop(), THREAD);
66 }
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(); }
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(); }
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(); }
92 methodOop last_method_oop() { assert(is_last(), ""); return _last_method(); }
93 Bytecodes::Code last_invoke_code() { assert(is_last(), ""); return _last_invoke; }
95 void lose(const char* msg, TRAPS);
96 const char* lose_message() { return _lose_message; }
97 };
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 };
114 // Argument token:
115 class ArgToken {
116 private:
117 TokenType _tt;
118 BasicType _bt;
119 jvalue _value;
120 Handle _handle;
122 public:
123 ArgToken(TokenType tt = tt_illegal) : _tt(tt) {}
124 ArgToken(TokenType tt, BasicType bt, jvalue value) : _tt(tt), _bt(bt), _value(value) {}
126 ArgToken(TokenType tt, BasicType bt, int index) : _tt(tt), _bt(bt) {
127 _value.i = index;
128 }
130 ArgToken(TokenType tt, BasicType bt, Handle value) : _tt(tt), _bt(bt) {
131 _handle = value;
132 }
134 TokenType token_type() const { return _tt; }
135 BasicType basic_type() const { return _bt; }
136 int index() const { return _value.i; }
137 Handle object() const { return _handle; }
139 jint get_jint() const { return _value.i; }
140 jlong get_jlong() const { return _value.j; }
141 jfloat get_jfloat() const { return _value.f; }
142 jdouble get_jdouble() const { return _value.d; }
143 };
145 // Abstract interpretation state:
146 struct SlotState {
147 BasicType _type;
148 ArgToken _arg;
149 SlotState() : _type(), _arg() {}
150 };
151 static SlotState make_state(BasicType type, ArgToken arg) {
152 SlotState ss;
153 ss._type = type; ss._arg = arg;
154 return ss;
155 }
157 private:
158 MethodHandleChain _chain;
159 bool _for_invokedynamic;
160 int _local_index;
162 GrowableArray<SlotState> _outgoing; // current outgoing parameter slots
163 int _outgoing_argc; // # non-empty outgoing slots
165 // Replace a value of type old_type at slot (and maybe slot+1) with the new value.
166 // If old_type != T_VOID, remove the old argument at that point.
167 // If new_type != T_VOID, insert the new argument at that point.
168 // Insert or delete a second empty slot as needed.
169 void change_argument(BasicType old_type, int slot, BasicType new_type, const ArgToken& new_arg);
171 SlotState* slot_state(int slot) {
172 if (slot < 0 || slot >= _outgoing.length())
173 return NULL;
174 return _outgoing.adr_at(slot);
175 }
176 BasicType slot_type(int slot) {
177 SlotState* ss = slot_state(slot);
178 if (ss == NULL)
179 return T_ILLEGAL;
180 return ss->_type;
181 }
182 bool slot_has_argument(int slot) {
183 return slot_type(slot) < T_VOID;
184 }
186 #ifdef ASSERT
187 int argument_count_slow();
188 #endif
190 // Return a bytecode for converting src to dest, if one exists.
191 Bytecodes::Code conversion_code(BasicType src, BasicType dest);
193 void walk_incoming_state(TRAPS);
195 public:
196 MethodHandleWalker(Handle root, bool for_invokedynamic, TRAPS)
197 : _chain(root, THREAD),
198 _for_invokedynamic(for_invokedynamic),
199 _outgoing(THREAD, 10),
200 _outgoing_argc(0)
201 {
202 _local_index = for_invokedynamic ? 0 : 1;
203 }
205 MethodHandleChain& chain() { return _chain; }
207 bool for_invokedynamic() const { return _for_invokedynamic; }
209 int new_local_index(BasicType bt) {
210 //int index = _for_invokedynamic ? _local_index : _local_index - 1;
211 int index = _local_index;
212 _local_index += type2size[bt];
213 return index;
214 }
216 int max_locals() const { return _local_index; }
218 // plug-in abstract interpretation steps:
219 virtual ArgToken make_parameter( BasicType type, klassOop tk, int argnum, TRAPS ) = 0;
220 virtual ArgToken make_prim_constant( BasicType type, jvalue* con, TRAPS ) = 0;
221 virtual ArgToken make_oop_constant( oop con, TRAPS ) = 0;
222 virtual ArgToken make_conversion( BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS ) = 0;
223 virtual ArgToken make_fetch( BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS ) = 0;
224 virtual ArgToken make_invoke( methodOop m, vmIntrinsics::ID iid, Bytecodes::Code op, bool tailcall, int argc, ArgToken* argv, TRAPS ) = 0;
226 // For make_invoke, the methodOop can be NULL if the intrinsic ID
227 // is something other than vmIntrinsics::_none.
229 // and in case anyone cares to related the previous actions to the chain:
230 virtual void set_method_handle(oop mh) { }
232 void lose(const char* msg, TRAPS) { chain().lose(msg, THREAD); }
233 const char* lose_message() { return chain().lose_message(); }
235 ArgToken walk(TRAPS);
236 };
239 // An abstract interpreter for method handle chains.
240 // Produces an account of the semantics of a chain, in terms of a static IR.
241 // The IR happens to be JVM bytecodes.
242 class MethodHandleCompiler : public MethodHandleWalker {
243 private:
244 methodHandle _callee;
245 KlassHandle _rklass; // Return type for casting.
246 BasicType _rtype;
247 KlassHandle _target_klass;
248 Thread* _thread;
250 // Fake constant pool entry.
251 class ConstantValue {
252 private:
253 int _tag; // Constant pool tag type.
254 JavaValue _value;
255 Handle _handle;
257 public:
258 // Constructor for oop types.
259 ConstantValue(int tag, Handle con) : _tag(tag), _handle(con) {
260 assert(tag == JVM_CONSTANT_Utf8 ||
261 tag == JVM_CONSTANT_Class ||
262 tag == JVM_CONSTANT_String ||
263 tag == JVM_CONSTANT_Object, "must be oop type");
264 }
266 // Constructor for oop reference types.
267 ConstantValue(int tag, int index) : _tag(tag) {
268 assert(JVM_CONSTANT_Fieldref <= tag && tag <= JVM_CONSTANT_NameAndType, "must be ref type");
269 _value.set_jint(index);
270 }
271 ConstantValue(int tag, int first_index, int second_index) : _tag(tag) {
272 assert(JVM_CONSTANT_Fieldref <= tag && tag <= JVM_CONSTANT_NameAndType, "must be ref type");
273 _value.set_jint(first_index << 16 | second_index);
274 }
276 // Constructor for primitive types.
277 ConstantValue(BasicType bt, jvalue con) {
278 _value.set_type(bt);
279 switch (bt) {
280 case T_INT: _tag = JVM_CONSTANT_Integer; _value.set_jint( con.i); break;
281 case T_LONG: _tag = JVM_CONSTANT_Long; _value.set_jlong( con.j); break;
282 case T_FLOAT: _tag = JVM_CONSTANT_Float; _value.set_jfloat( con.f); break;
283 case T_DOUBLE: _tag = JVM_CONSTANT_Double; _value.set_jdouble(con.d); break;
284 default: ShouldNotReachHere();
285 }
286 }
288 int tag() const { return _tag; }
289 symbolOop symbol_oop() const { return (symbolOop) _handle(); }
290 klassOop klass_oop() const { return (klassOop) _handle(); }
291 oop object_oop() const { return _handle(); }
292 int index() const { return _value.get_jint(); }
293 int first_index() const { return _value.get_jint() >> 16; }
294 int second_index() const { return _value.get_jint() & 0x0000FFFF; }
296 bool is_primitive() const { return is_java_primitive(_value.get_type()); }
297 jint get_jint() const { return _value.get_jint(); }
298 jlong get_jlong() const { return _value.get_jlong(); }
299 jfloat get_jfloat() const { return _value.get_jfloat(); }
300 jdouble get_jdouble() const { return _value.get_jdouble(); }
301 };
303 // Fake constant pool.
304 GrowableArray<ConstantValue*> _constants;
306 // Accumulated compiler state:
307 GrowableArray<unsigned char> _bytecode;
309 int _cur_stack;
310 int _max_stack;
311 int _num_params;
312 int _name_index;
313 int _signature_index;
315 void stack_push(BasicType bt) {
316 _cur_stack += type2size[bt];
317 if (_cur_stack > _max_stack) _max_stack = _cur_stack;
318 }
319 void stack_pop(BasicType bt) {
320 _cur_stack -= type2size[bt];
321 assert(_cur_stack >= 0, "sanity");
322 }
324 unsigned char* bytecode() const { return _bytecode.adr_at(0); }
325 int bytecode_length() const { return _bytecode.length(); }
327 // Fake constant pool.
328 int cpool_oop_put(int tag, Handle con) {
329 if (con.is_null()) return 0;
330 ConstantValue* cv = new ConstantValue(tag, con);
331 return _constants.append(cv);
332 }
334 int cpool_oop_reference_put(int tag, int first_index, int second_index) {
335 if (first_index == 0 && second_index == 0) return 0;
336 assert(first_index != 0 && second_index != 0, "no zero indexes");
337 ConstantValue* cv = new ConstantValue(tag, first_index, second_index);
338 return _constants.append(cv);
339 }
341 int cpool_primitive_put(BasicType type, jvalue* con);
343 int cpool_int_put(jint value) {
344 jvalue con; con.i = value;
345 return cpool_primitive_put(T_INT, &con);
346 }
347 int cpool_long_put(jlong value) {
348 jvalue con; con.j = value;
349 return cpool_primitive_put(T_LONG, &con);
350 }
351 int cpool_float_put(jfloat value) {
352 jvalue con; con.f = value;
353 return cpool_primitive_put(T_FLOAT, &con);
354 }
355 int cpool_double_put(jdouble value) {
356 jvalue con; con.d = value;
357 return cpool_primitive_put(T_DOUBLE, &con);
358 }
360 int cpool_object_put(Handle obj) {
361 return cpool_oop_put(JVM_CONSTANT_Object, obj);
362 }
363 int cpool_symbol_put(symbolOop sym) {
364 return cpool_oop_put(JVM_CONSTANT_Utf8, sym);
365 }
366 int cpool_klass_put(klassOop klass) {
367 return cpool_oop_put(JVM_CONSTANT_Class, klass);
368 }
369 int cpool_methodref_put(int class_index, int name_and_type_index) {
370 return cpool_oop_reference_put(JVM_CONSTANT_Methodref, class_index, name_and_type_index);
371 }
372 int cpool_name_and_type_put(int name_index, int signature_index) {
373 return cpool_oop_reference_put(JVM_CONSTANT_NameAndType, name_index, signature_index);
374 }
376 void emit_bc(Bytecodes::Code op, int index = 0);
377 void emit_load(BasicType bt, int index);
378 void emit_store(BasicType bt, int index);
379 void emit_load_constant(ArgToken arg);
381 virtual ArgToken make_parameter(BasicType type, klassOop tk, int argnum, TRAPS) {
382 return ArgToken(tt_parameter, type, argnum);
383 }
384 virtual ArgToken make_oop_constant(oop con, TRAPS) {
385 Handle h(THREAD, con);
386 return ArgToken(tt_constant, T_OBJECT, h);
387 }
388 virtual ArgToken make_prim_constant(BasicType type, jvalue* con, TRAPS) {
389 return ArgToken(tt_constant, type, *con);
390 }
392 virtual ArgToken make_conversion(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& src, TRAPS);
393 virtual ArgToken make_fetch(BasicType type, klassOop tk, Bytecodes::Code op, const ArgToken& base, const ArgToken& offset, TRAPS);
394 virtual ArgToken make_invoke(methodOop m, vmIntrinsics::ID iid, Bytecodes::Code op, bool tailcall, int argc, ArgToken* argv, TRAPS);
396 // Get a real constant pool.
397 constantPoolHandle get_constant_pool(TRAPS) const;
399 // Get a real methodOop.
400 methodHandle get_method_oop(TRAPS) const;
402 public:
403 MethodHandleCompiler(Handle root, methodHandle call_method, bool for_invokedynamic, TRAPS);
405 // Compile the given MH chain into bytecode.
406 methodHandle compile(TRAPS);
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 }
413 };