Mercurial > hg > truffle
comparison src/share/vm/interpreter/bytecode.hpp @ 1565:ab102d5d923e
6939207: refactor constant pool index processing
Summary: Factored cleanup of instruction decode which prepares for enhanced ldc semantics.
Reviewed-by: twisti
author | jrose |
---|---|
date | Sun, 23 May 2010 01:38:26 -0700 |
parents | dd57230ba8fe |
children | e9ff18c4ace7 |
comparison
equal
deleted
inserted
replaced
1564:61b2245abf36 | 1565:ab102d5d923e |
---|---|
1 /* | 1 /* |
2 * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. | 2 * Copyright 1997-2010 Sun Microsystems, Inc. All Rights Reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | 4 * |
5 * This code is free software; you can redistribute it and/or modify it | 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 | 6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
24 | 24 |
25 // Base class for different kinds of abstractions working | 25 // Base class for different kinds of abstractions working |
26 // relative to an objects 'this' pointer. | 26 // relative to an objects 'this' pointer. |
27 | 27 |
28 class ThisRelativeObj VALUE_OBJ_CLASS_SPEC { | 28 class ThisRelativeObj VALUE_OBJ_CLASS_SPEC { |
29 private: | |
30 int sign_extend (int x, int size) const { const int s = (BytesPerInt - size)*BitsPerByte; return (x << s) >> s; } | |
31 | |
32 public: | 29 public: |
33 // Address computation | 30 // Address computation |
34 address addr_at (int offset) const { return (address)this + offset; } | 31 address addr_at (int offset) const { return (address)this + offset; } |
32 int byte_at (int offset) const { return *(addr_at(offset)); } | |
35 address aligned_addr_at (int offset) const { return (address)round_to((intptr_t)addr_at(offset), jintSize); } | 33 address aligned_addr_at (int offset) const { return (address)round_to((intptr_t)addr_at(offset), jintSize); } |
36 int aligned_offset (int offset) const { return aligned_addr_at(offset) - addr_at(0); } | 34 int aligned_offset (int offset) const { return aligned_addr_at(offset) - addr_at(0); } |
37 | 35 |
38 // Java unsigned accessors (using Java spec byte ordering) | 36 // Word access: |
39 int java_byte_at (int offset) const { return *(jubyte*)addr_at(offset); } | 37 int get_Java_u2_at (int offset) const { return Bytes::get_Java_u2(addr_at(offset)); } |
40 int java_hwrd_at (int offset) const { return java_byte_at(offset) << (1 * BitsPerByte) | java_byte_at(offset + 1); } | 38 int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); } |
41 int java_word_at (int offset) const { return java_hwrd_at(offset) << (2 * BitsPerByte) | java_hwrd_at(offset + 2); } | 39 int get_native_u2_at (int offset) const { return Bytes::get_native_u2(addr_at(offset)); } |
42 | 40 int get_native_u4_at (int offset) const { return Bytes::get_native_u4(addr_at(offset)); } |
43 // Java signed accessors (using Java spec byte ordering) | |
44 int java_signed_byte_at(int offset) const { return sign_extend(java_byte_at(offset), 1); } | |
45 int java_signed_hwrd_at(int offset) const { return sign_extend(java_hwrd_at(offset), 2); } | |
46 int java_signed_word_at(int offset) const { return java_word_at(offset) ; } | |
47 | |
48 // Fast accessors (using the machine's natural byte ordering) | |
49 int fast_byte_at (int offset) const { return *(jubyte *)addr_at(offset); } | |
50 int fast_hwrd_at (int offset) const { return *(jushort*)addr_at(offset); } | |
51 int fast_word_at (int offset) const { return *(juint *)addr_at(offset); } | |
52 | |
53 // Fast signed accessors (using the machine's natural byte ordering) | |
54 int fast_signed_byte_at(int offset) const { return *(jbyte *)addr_at(offset); } | |
55 int fast_signed_hwrd_at(int offset) const { return *(jshort*)addr_at(offset); } | |
56 int fast_signed_word_at(int offset) const { return *(jint *)addr_at(offset); } | |
57 | |
58 // Fast manipulators (using the machine's natural byte ordering) | |
59 void set_fast_byte_at (int offset, int x) const { *(jbyte *)addr_at(offset) = (jbyte )x; } | |
60 void set_fast_hwrd_at (int offset, int x) const { *(jshort*)addr_at(offset) = (jshort)x; } | |
61 void set_fast_word_at (int offset, int x) const { *(jint *)addr_at(offset) = (jint )x; } | |
62 }; | 41 }; |
63 | 42 |
64 | 43 |
65 // The base class for different kinds of bytecode abstractions. | 44 // The base class for different kinds of bytecode abstractions. |
66 // Provides the primitive operations to manipulate code relative | 45 // Provides the primitive operations to manipulate code relative |
67 // to an objects 'this' pointer. | 46 // to an objects 'this' pointer. |
47 // FIXME: Make this a ResourceObj, include the enclosing methodOop, and cache the opcode. | |
68 | 48 |
69 class Bytecode: public ThisRelativeObj { | 49 class Bytecode: public ThisRelativeObj { |
70 protected: | 50 protected: |
71 u_char byte_at(int offset) const { return *addr_at(offset); } | 51 u_char byte_at(int offset) const { return *addr_at(offset); } |
72 bool check_must_rewrite() const; | 52 bool check_must_rewrite(Bytecodes::Code bc) const; |
73 | 53 |
74 public: | 54 public: |
75 // Attributes | 55 // Attributes |
76 address bcp() const { return addr_at(0); } | 56 address bcp() const { return addr_at(0); } |
77 address next_bcp() const { return addr_at(0) + Bytecodes::length_at(bcp()); } | |
78 int instruction_size() const { return Bytecodes::length_at(bcp()); } | 57 int instruction_size() const { return Bytecodes::length_at(bcp()); } |
79 | 58 |
59 // Warning: Use code() with caution on live bytecode streams. 4926272 | |
80 Bytecodes::Code code() const { return Bytecodes::code_at(addr_at(0)); } | 60 Bytecodes::Code code() const { return Bytecodes::code_at(addr_at(0)); } |
81 Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); } | 61 Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); } |
82 bool must_rewrite() const { return Bytecodes::can_rewrite(code()) && check_must_rewrite(); } | 62 bool must_rewrite(Bytecodes::Code code) const { return Bytecodes::can_rewrite(code) && check_must_rewrite(code); } |
83 bool is_active_breakpoint() const { return Bytecodes::is_active_breakpoint_at(bcp()); } | |
84 | |
85 int one_byte_index() const { assert_index_size(1); return byte_at(1); } | |
86 int two_byte_index() const { assert_index_size(2); return (byte_at(1) << 8) + byte_at(2); } | |
87 | |
88 int offset() const { return (two_byte_index() << 16) >> 16; } | |
89 address destination() const { return bcp() + offset(); } | |
90 | |
91 // Attribute modification | |
92 void set_code(Bytecodes::Code code); | |
93 | 63 |
94 // Creation | 64 // Creation |
95 inline friend Bytecode* Bytecode_at(address bcp); | 65 inline friend Bytecode* Bytecode_at(address bcp); |
96 | 66 |
97 private: | 67 // Static functions for parsing bytecodes in place. |
98 void assert_index_size(int required_size) const { | 68 int get_index_u1(Bytecodes::Code bc) const { |
99 #ifdef ASSERT | 69 assert_same_format_as(bc); assert_index_size(1, bc); |
100 int isize = instruction_size() - 1; | 70 return *(jubyte*)addr_at(1); |
101 if (isize == 2 && code() == Bytecodes::_iinc) | 71 } |
102 isize = 1; | 72 int get_index_u2(Bytecodes::Code bc, bool is_wide = false) const { |
103 else if (isize <= 2) | 73 assert_same_format_as(bc, is_wide); assert_index_size(2, bc, is_wide); |
104 ; // no change | 74 address p = addr_at(is_wide ? 2 : 1); |
105 else if (code() == Bytecodes::_invokedynamic) | 75 if (can_use_native_byte_order(bc, is_wide)) |
106 isize = 4; | 76 return Bytes::get_native_u2(p); |
107 else | 77 else return Bytes::get_Java_u2(p); |
108 isize = 2; | 78 } |
109 assert(isize = required_size, "wrong index size"); | 79 int get_index_u2_cpcache(Bytecodes::Code bc) const { |
110 #endif | 80 assert_same_format_as(bc); assert_index_size(2, bc); assert_native_index(bc); |
81 return Bytes::get_native_u2(addr_at(1)) DEBUG_ONLY(+ constantPoolOopDesc::CPCACHE_INDEX_TAG); | |
82 } | |
83 int get_index_u4(Bytecodes::Code bc) const { | |
84 assert_same_format_as(bc); assert_index_size(4, bc); | |
85 assert(can_use_native_byte_order(bc), ""); | |
86 return Bytes::get_native_u4(addr_at(1)); | |
87 } | |
88 bool has_index_u4(Bytecodes::Code bc) const { | |
89 return bc == Bytecodes::_invokedynamic; | |
90 } | |
91 | |
92 int get_offset_s2(Bytecodes::Code bc) const { | |
93 assert_same_format_as(bc); assert_offset_size(2, bc); | |
94 return (jshort) Bytes::get_Java_u2(addr_at(1)); | |
95 } | |
96 int get_offset_s4(Bytecodes::Code bc) const { | |
97 assert_same_format_as(bc); assert_offset_size(4, bc); | |
98 return (jint) Bytes::get_Java_u4(addr_at(1)); | |
99 } | |
100 | |
101 int get_constant_u1(int offset, Bytecodes::Code bc) const { | |
102 assert_same_format_as(bc); assert_constant_size(1, offset, bc); | |
103 return *(jbyte*)addr_at(offset); | |
104 } | |
105 int get_constant_u2(int offset, Bytecodes::Code bc, bool is_wide = false) const { | |
106 assert_same_format_as(bc, is_wide); assert_constant_size(2, offset, bc, is_wide); | |
107 return (jshort) Bytes::get_Java_u2(addr_at(offset)); | |
108 } | |
109 | |
110 // These are used locally and also from bytecode streams. | |
111 void assert_same_format_as(Bytecodes::Code testbc, bool is_wide = false) const NOT_DEBUG_RETURN; | |
112 static void assert_index_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; | |
113 static void assert_offset_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; | |
114 static void assert_constant_size(int required_size, int where, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; | |
115 static void assert_native_index(Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; | |
116 static bool can_use_native_byte_order(Bytecodes::Code bc, bool is_wide = false) { | |
117 return (!Bytes::is_Java_byte_ordering_different() || Bytecodes::native_byte_order(bc /*, is_wide*/)); | |
111 } | 118 } |
112 }; | 119 }; |
113 | 120 |
114 inline Bytecode* Bytecode_at(address bcp) { | 121 inline Bytecode* Bytecode_at(address bcp) { |
122 // Warning: Use with caution on live bytecode streams. 4926272 | |
115 return (Bytecode*)bcp; | 123 return (Bytecode*)bcp; |
116 } | 124 } |
117 | 125 |
118 | 126 |
119 // Abstractions for lookupswitch bytecode | 127 // Abstractions for lookupswitch bytecode |
122 private: | 130 private: |
123 int _match; | 131 int _match; |
124 int _offset; | 132 int _offset; |
125 | 133 |
126 public: | 134 public: |
127 int match() const { return java_signed_word_at(0 * jintSize); } | 135 int match() const { return get_Java_u4_at(0 * jintSize); } |
128 int offset() const { return java_signed_word_at(1 * jintSize); } | 136 int offset() const { return get_Java_u4_at(1 * jintSize); } |
129 }; | 137 }; |
130 | 138 |
131 | 139 |
132 class Bytecode_lookupswitch: public Bytecode { | 140 class Bytecode_lookupswitch: public Bytecode { |
133 public: | 141 public: |
134 void verify() const PRODUCT_RETURN; | 142 void verify() const PRODUCT_RETURN; |
135 | 143 |
136 // Attributes | 144 // Attributes |
137 int default_offset() const { return java_signed_word_at(aligned_offset(1 + 0*jintSize)); } | 145 int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); } |
138 int number_of_pairs() const { return java_signed_word_at(aligned_offset(1 + 1*jintSize)); } | 146 int number_of_pairs() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); } |
139 LookupswitchPair* pair_at(int i) const { assert(0 <= i && i < number_of_pairs(), "pair index out of bounds"); | 147 LookupswitchPair* pair_at(int i) const { assert(0 <= i && i < number_of_pairs(), "pair index out of bounds"); |
140 return (LookupswitchPair*)aligned_addr_at(1 + (1 + i)*2*jintSize); } | 148 return (LookupswitchPair*)aligned_addr_at(1 + (1 + i)*2*jintSize); } |
141 // Creation | 149 // Creation |
142 inline friend Bytecode_lookupswitch* Bytecode_lookupswitch_at(address bcp); | 150 inline friend Bytecode_lookupswitch* Bytecode_lookupswitch_at(address bcp); |
143 }; | 151 }; |
152 class Bytecode_tableswitch: public Bytecode { | 160 class Bytecode_tableswitch: public Bytecode { |
153 public: | 161 public: |
154 void verify() const PRODUCT_RETURN; | 162 void verify() const PRODUCT_RETURN; |
155 | 163 |
156 // Attributes | 164 // Attributes |
157 int default_offset() const { return java_signed_word_at(aligned_offset(1 + 0*jintSize)); } | 165 int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); } |
158 int low_key() const { return java_signed_word_at(aligned_offset(1 + 1*jintSize)); } | 166 int low_key() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); } |
159 int high_key() const { return java_signed_word_at(aligned_offset(1 + 2*jintSize)); } | 167 int high_key() const { return get_Java_u4_at(aligned_offset(1 + 2*jintSize)); } |
160 int dest_offset_at(int i) const; | 168 int dest_offset_at(int i) const; |
161 int length() { return high_key()-low_key()+1; } | 169 int length() { return high_key()-low_key()+1; } |
162 | 170 |
163 // Creation | 171 // Creation |
164 inline friend Bytecode_tableswitch* Bytecode_tableswitch_at(address bcp); | 172 inline friend Bytecode_tableswitch* Bytecode_tableswitch_at(address bcp); |
204 bool is_invokestatic() const { return adjusted_invoke_code() == Bytecodes::_invokestatic; } | 212 bool is_invokestatic() const { return adjusted_invoke_code() == Bytecodes::_invokestatic; } |
205 bool is_invokespecial() const { return adjusted_invoke_code() == Bytecodes::_invokespecial; } | 213 bool is_invokespecial() const { return adjusted_invoke_code() == Bytecodes::_invokespecial; } |
206 bool is_invokedynamic() const { return adjusted_invoke_code() == Bytecodes::_invokedynamic; } | 214 bool is_invokedynamic() const { return adjusted_invoke_code() == Bytecodes::_invokedynamic; } |
207 | 215 |
208 bool has_receiver() const { return !is_invokestatic() && !is_invokedynamic(); } | 216 bool has_receiver() const { return !is_invokestatic() && !is_invokedynamic(); } |
209 bool has_giant_index() const { return is_invokedynamic(); } | |
210 | 217 |
211 bool is_valid() const { return is_invokeinterface() || | 218 bool is_valid() const { return is_invokeinterface() || |
212 is_invokevirtual() || | 219 is_invokevirtual() || |
213 is_invokestatic() || | 220 is_invokestatic() || |
214 is_invokespecial() || | 221 is_invokespecial() || |
250 debug_only(b->verify()); | 257 debug_only(b->verify()); |
251 return b; | 258 return b; |
252 } | 259 } |
253 | 260 |
254 | 261 |
255 // Abstraction for {get,put}static | |
256 | |
257 class Bytecode_static: public Bytecode { | |
258 public: | |
259 void verify() const; | |
260 | |
261 // Returns the result type of the send by inspecting the field ref | |
262 BasicType result_type(methodOop method) const; | |
263 | |
264 // Creation | |
265 inline friend Bytecode_static* Bytecode_static_at(const methodOop method, address bcp); | |
266 }; | |
267 | |
268 inline Bytecode_static* Bytecode_static_at(const methodOop method, address bcp) { | |
269 Bytecode_static* b = (Bytecode_static*)bcp; | |
270 debug_only(b->verify()); | |
271 return b; | |
272 } | |
273 | |
274 | |
275 // Abstraction for checkcast | 262 // Abstraction for checkcast |
276 | 263 |
277 class Bytecode_checkcast: public Bytecode { | 264 class Bytecode_checkcast: public Bytecode { |
278 public: | 265 public: |
279 void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); } | 266 void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); } |
280 | 267 |
281 // Returns index | 268 // Returns index |
282 long index() const { return java_hwrd_at(1); }; | 269 long index() const { return get_index_u2(Bytecodes::_checkcast); }; |
283 | 270 |
284 // Creation | 271 // Creation |
285 inline friend Bytecode_checkcast* Bytecode_checkcast_at(address bcp); | 272 inline friend Bytecode_checkcast* Bytecode_checkcast_at(address bcp); |
286 }; | 273 }; |
287 | 274 |
297 class Bytecode_instanceof: public Bytecode { | 284 class Bytecode_instanceof: public Bytecode { |
298 public: | 285 public: |
299 void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); } | 286 void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); } |
300 | 287 |
301 // Returns index | 288 // Returns index |
302 long index() const { return java_hwrd_at(1); }; | 289 long index() const { return get_index_u2(Bytecodes::_instanceof); }; |
303 | 290 |
304 // Creation | 291 // Creation |
305 inline friend Bytecode_instanceof* Bytecode_instanceof_at(address bcp); | 292 inline friend Bytecode_instanceof* Bytecode_instanceof_at(address bcp); |
306 }; | 293 }; |
307 | 294 |
315 class Bytecode_new: public Bytecode { | 302 class Bytecode_new: public Bytecode { |
316 public: | 303 public: |
317 void verify() const { assert(java_code() == Bytecodes::_new, "check new"); } | 304 void verify() const { assert(java_code() == Bytecodes::_new, "check new"); } |
318 | 305 |
319 // Returns index | 306 // Returns index |
320 long index() const { return java_hwrd_at(1); }; | 307 long index() const { return get_index_u2(Bytecodes::_new); }; |
321 | 308 |
322 // Creation | 309 // Creation |
323 inline friend Bytecode_new* Bytecode_new_at(address bcp); | 310 inline friend Bytecode_new* Bytecode_new_at(address bcp); |
324 }; | 311 }; |
325 | 312 |
333 class Bytecode_multianewarray: public Bytecode { | 320 class Bytecode_multianewarray: public Bytecode { |
334 public: | 321 public: |
335 void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); } | 322 void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); } |
336 | 323 |
337 // Returns index | 324 // Returns index |
338 long index() const { return java_hwrd_at(1); }; | 325 long index() const { return get_index_u2(Bytecodes::_multianewarray); }; |
339 | 326 |
340 // Creation | 327 // Creation |
341 inline friend Bytecode_multianewarray* Bytecode_multianewarray_at(address bcp); | 328 inline friend Bytecode_multianewarray* Bytecode_multianewarray_at(address bcp); |
342 }; | 329 }; |
343 | 330 |
351 class Bytecode_anewarray: public Bytecode { | 338 class Bytecode_anewarray: public Bytecode { |
352 public: | 339 public: |
353 void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); } | 340 void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); } |
354 | 341 |
355 // Returns index | 342 // Returns index |
356 long index() const { return java_hwrd_at(1); }; | 343 long index() const { return get_index_u2(Bytecodes::_anewarray); }; |
357 | 344 |
358 // Creation | 345 // Creation |
359 inline friend Bytecode_anewarray* Bytecode_anewarray_at(address bcp); | 346 inline friend Bytecode_anewarray* Bytecode_anewarray_at(address bcp); |
360 }; | 347 }; |
361 | 348 |