1 /*
2 * Copyright (c) 1997, 2021, 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_INTERPRETER_BYTECODE_HPP
26 #define SHARE_INTERPRETER_BYTECODE_HPP
27
28 #include "interpreter/bytecodes.hpp"
29 #include "memory/allocation.hpp"
30 #include "oops/method.hpp"
31 #include "utilities/align.hpp"
32 #include "utilities/bytes.hpp"
33
34 class ciBytecodeStream;
35
36 // The base class for different kinds of bytecode abstractions.
37 // Provides the primitive operations to manipulate code relative
38 // to the bcp.
39
40 class Bytecode: public StackObj {
41 protected:
42 const address _bcp;
43 const Bytecodes::Code _code;
44
45 // Address computation
addr_at(int offset) const46 address addr_at (int offset) const { return (address)_bcp + offset; }
byte_at(int offset) const47 u_char byte_at(int offset) const { return *addr_at(offset); }
aligned_addr_at(int offset) const48 address aligned_addr_at (int offset) const { return align_up(addr_at(offset), jintSize); }
49
50 // Word access:
get_Java_u2_at(int offset) const51 int get_Java_u2_at (int offset) const { return Bytes::get_Java_u2(addr_at(offset)); }
get_Java_u4_at(int offset) const52 int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); }
get_aligned_Java_u4_at(int offset) const53 int get_aligned_Java_u4_at(int offset) const { return Bytes::get_Java_u4(aligned_addr_at(offset)); }
get_native_u2_at(int offset) const54 int get_native_u2_at (int offset) const { return Bytes::get_native_u2(addr_at(offset)); }
get_native_u4_at(int offset) const55 int get_native_u4_at (int offset) const { return Bytes::get_native_u4(addr_at(offset)); }
56
57 public:
Bytecode(Method * method,address bcp)58 Bytecode(Method* method, address bcp): _bcp(bcp), _code(Bytecodes::code_at(method, addr_at(0))) {
59 assert(method != NULL, "this form requires a valid Method*");
60 }
61 // Defined in ciStreams.hpp
62 inline Bytecode(const ciBytecodeStream* stream, address bcp = NULL);
63
64 // Attributes
bcp() const65 address bcp() const { return _bcp; }
instruction_size() const66 int instruction_size() const { return Bytecodes::length_for_code_at(_code, bcp()); }
67
code() const68 Bytecodes::Code code() const { return _code; }
java_code() const69 Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); }
invoke_code() const70 Bytecodes::Code invoke_code() const { return (code() == Bytecodes::_invokehandle) ? code() : java_code(); }
71
72 // Static functions for parsing bytecodes in place.
get_index_u1(Bytecodes::Code bc) const73 int get_index_u1(Bytecodes::Code bc) const {
74 assert_same_format_as(bc); assert_index_size(1, bc);
75 return *(jubyte*)addr_at(1);
76 }
get_index_u2(Bytecodes::Code bc,bool is_wide=false) const77 int get_index_u2(Bytecodes::Code bc, bool is_wide = false) const {
78 assert_same_format_as(bc, is_wide); assert_index_size(2, bc, is_wide);
79 address p = addr_at(is_wide ? 2 : 1);
80 if (can_use_native_byte_order(bc, is_wide))
81 return Bytes::get_native_u2(p);
82 else return Bytes::get_Java_u2(p);
83 }
get_index_u1_cpcache(Bytecodes::Code bc) const84 int get_index_u1_cpcache(Bytecodes::Code bc) const {
85 assert_same_format_as(bc); assert_index_size(1, bc);
86 return *(jubyte*)addr_at(1) + ConstantPool::CPCACHE_INDEX_TAG;
87 }
get_index_u2_cpcache(Bytecodes::Code bc) const88 int get_index_u2_cpcache(Bytecodes::Code bc) const {
89 assert_same_format_as(bc); assert_index_size(2, bc); assert_native_index(bc);
90 return Bytes::get_native_u2(addr_at(1)) + ConstantPool::CPCACHE_INDEX_TAG;
91 }
get_index_u4(Bytecodes::Code bc) const92 int get_index_u4(Bytecodes::Code bc) const {
93 assert_same_format_as(bc); assert_index_size(4, bc);
94 assert(can_use_native_byte_order(bc), "");
95 return Bytes::get_native_u4(addr_at(1));
96 }
has_index_u4(Bytecodes::Code bc) const97 bool has_index_u4(Bytecodes::Code bc) const {
98 return bc == Bytecodes::_invokedynamic;
99 }
100
get_offset_s2(Bytecodes::Code bc) const101 int get_offset_s2(Bytecodes::Code bc) const {
102 assert_same_format_as(bc); assert_offset_size(2, bc);
103 return (jshort) Bytes::get_Java_u2(addr_at(1));
104 }
get_offset_s4(Bytecodes::Code bc) const105 int get_offset_s4(Bytecodes::Code bc) const {
106 assert_same_format_as(bc); assert_offset_size(4, bc);
107 return (jint) Bytes::get_Java_u4(addr_at(1));
108 }
109
get_constant_u1(int offset,Bytecodes::Code bc) const110 int get_constant_u1(int offset, Bytecodes::Code bc) const {
111 assert_same_format_as(bc); assert_constant_size(1, offset, bc);
112 return *(jbyte*)addr_at(offset);
113 }
get_constant_u2(int offset,Bytecodes::Code bc,bool is_wide=false) const114 int get_constant_u2(int offset, Bytecodes::Code bc, bool is_wide = false) const {
115 assert_same_format_as(bc, is_wide); assert_constant_size(2, offset, bc, is_wide);
116 return (jshort) Bytes::get_Java_u2(addr_at(offset));
117 }
118
119 // These are used locally and also from bytecode streams.
120 void assert_same_format_as(Bytecodes::Code testbc, bool is_wide = false) const NOT_DEBUG_RETURN;
121 static void assert_index_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
122 static void assert_offset_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
123 static void assert_constant_size(int required_size, int where, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
124 static void assert_native_index(Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
can_use_native_byte_order(Bytecodes::Code bc,bool is_wide=false)125 static bool can_use_native_byte_order(Bytecodes::Code bc, bool is_wide = false) {
126 return (!Endian::is_Java_byte_ordering_different() || Bytecodes::native_byte_order(bc /*, is_wide*/));
127 }
128 };
129
130
131 // Abstractions for lookupswitch bytecode
132 class LookupswitchPair {
133 private:
134 const address _bcp;
135
addr_at(int offset) const136 address addr_at (int offset) const { return _bcp + offset; }
get_Java_u4_at(int offset) const137 int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); }
138
139 public:
LookupswitchPair(address bcp)140 LookupswitchPair(address bcp): _bcp(bcp) {}
match() const141 int match() const { return get_Java_u4_at(0 * jintSize); }
offset() const142 int offset() const { return get_Java_u4_at(1 * jintSize); }
143 };
144
145
146 class Bytecode_lookupswitch: public Bytecode {
147 public:
Bytecode_lookupswitch(Method * method,address bcp)148 Bytecode_lookupswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
149 // Defined in ciStreams.hpp
150 inline Bytecode_lookupswitch(const ciBytecodeStream* stream);
151 void verify() const PRODUCT_RETURN;
152
153 // Attributes
default_offset() const154 int default_offset() const { return get_aligned_Java_u4_at(1 + 0*jintSize); }
number_of_pairs() const155 int number_of_pairs() const { return get_aligned_Java_u4_at(1 + 1*jintSize); }
pair_at(int i) const156 LookupswitchPair pair_at(int i) const {
157 assert(0 <= i && i < number_of_pairs(), "pair index out of bounds");
158 return LookupswitchPair(aligned_addr_at(1 + (1 + i)*2*jintSize));
159 }
160 };
161
162 class Bytecode_tableswitch: public Bytecode {
163 public:
Bytecode_tableswitch(Method * method,address bcp)164 Bytecode_tableswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
165 // Defined in ciStreams.hpp
166 inline Bytecode_tableswitch(const ciBytecodeStream* stream);
167 void verify() const PRODUCT_RETURN;
168
169 // Attributes
default_offset() const170 int default_offset() const { return get_aligned_Java_u4_at(1 + 0*jintSize); }
low_key() const171 int low_key() const { return get_aligned_Java_u4_at(1 + 1*jintSize); }
high_key() const172 int high_key() const { return get_aligned_Java_u4_at(1 + 2*jintSize); }
173 int dest_offset_at(int i) const;
length()174 int length() { return high_key()-low_key()+1; }
175 };
176
177 // Common code for decoding invokes and field references.
178
179 class Bytecode_member_ref: public Bytecode {
180 protected:
181 const Method* _method; // method containing the bytecode
182
Bytecode_member_ref(const methodHandle & method,int bci)183 Bytecode_member_ref(const methodHandle& method, int bci) : Bytecode(method(), method()->bcp_from(bci)), _method(method()) {}
184
method() const185 const Method* method() const { return _method; }
constants() const186 ConstantPool* constants() const { return _method->constants(); }
cpcache() const187 ConstantPoolCache* cpcache() const { return _method->constants()->cache(); }
188 ConstantPoolCacheEntry* cpcache_entry() const;
189
190 public:
191 int index() const; // cache index (loaded from instruction)
192 int pool_index() const; // constant pool index
193 Symbol* klass() const; // returns the klass of the method or field
194 Symbol* name() const; // returns the name of the method or field
195 Symbol* signature() const; // returns the signature of the method or field
196
197 BasicType result_type() const; // returns the result type of the getfield or invoke
198 };
199
200 // Abstraction for invoke_{virtual, static, interface, special, dynamic, handle}
201
202 class Bytecode_invoke: public Bytecode_member_ref {
203 protected:
204 // Constructor that skips verification
Bytecode_invoke(const methodHandle & method,int bci,bool unused)205 Bytecode_invoke(const methodHandle& method, int bci, bool unused) : Bytecode_member_ref(method, bci) {}
206
207 public:
Bytecode_invoke(const methodHandle & method,int bci)208 Bytecode_invoke(const methodHandle& method, int bci) : Bytecode_member_ref(method, bci) { verify(); }
209 void verify() const;
210
211 // Attributes
212 Method* static_target(TRAPS); // "specified" method (from constant pool)
213
214 // Testers
is_invokeinterface() const215 bool is_invokeinterface() const { return invoke_code() == Bytecodes::_invokeinterface; }
is_invokevirtual() const216 bool is_invokevirtual() const { return invoke_code() == Bytecodes::_invokevirtual; }
is_invokestatic() const217 bool is_invokestatic() const { return invoke_code() == Bytecodes::_invokestatic; }
is_invokespecial() const218 bool is_invokespecial() const { return invoke_code() == Bytecodes::_invokespecial; }
is_invokedynamic() const219 bool is_invokedynamic() const { return invoke_code() == Bytecodes::_invokedynamic; }
is_invokehandle() const220 bool is_invokehandle() const { return invoke_code() == Bytecodes::_invokehandle; }
221
has_receiver() const222 bool has_receiver() const { return !is_invokestatic() && !is_invokedynamic(); }
223
is_valid() const224 bool is_valid() const { return is_invokeinterface() ||
225 is_invokevirtual() ||
226 is_invokestatic() ||
227 is_invokespecial() ||
228 is_invokedynamic() ||
229 is_invokehandle(); }
230
231 bool has_appendix();
232
233 int size_of_parameters() const;
234
235 private:
236 // Helper to skip verification. Used is_valid() to check if the result is really an invoke
237 inline friend Bytecode_invoke Bytecode_invoke_check(const methodHandle& method, int bci);
238 };
239
Bytecode_invoke_check(const methodHandle & method,int bci)240 inline Bytecode_invoke Bytecode_invoke_check(const methodHandle& method, int bci) {
241 return Bytecode_invoke(method, bci, false);
242 }
243
244
245 // Abstraction for all field accesses (put/get field/static)
246 class Bytecode_field: public Bytecode_member_ref {
247 public:
Bytecode_field(const methodHandle & method,int bci)248 Bytecode_field(const methodHandle& method, int bci) : Bytecode_member_ref(method, bci) { verify(); }
249
250 // Testers
is_getfield() const251 bool is_getfield() const { return java_code() == Bytecodes::_getfield; }
is_putfield() const252 bool is_putfield() const { return java_code() == Bytecodes::_putfield; }
is_getstatic() const253 bool is_getstatic() const { return java_code() == Bytecodes::_getstatic; }
is_putstatic() const254 bool is_putstatic() const { return java_code() == Bytecodes::_putstatic; }
255
is_getter() const256 bool is_getter() const { return is_getfield() || is_getstatic(); }
is_static() const257 bool is_static() const { return is_getstatic() || is_putstatic(); }
258
is_valid() const259 bool is_valid() const { return is_getfield() ||
260 is_putfield() ||
261 is_getstatic() ||
262 is_putstatic(); }
263 void verify() const;
264 };
265
266 // Abstraction for checkcast
267 class Bytecode_checkcast: public Bytecode {
268 public:
Bytecode_checkcast(Method * method,address bcp)269 Bytecode_checkcast(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
verify() const270 void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); }
271
272 // Returns index
index() const273 long index() const { return get_index_u2(Bytecodes::_checkcast); };
274 };
275
276 // Abstraction for instanceof
277 class Bytecode_instanceof: public Bytecode {
278 public:
Bytecode_instanceof(Method * method,address bcp)279 Bytecode_instanceof(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
verify() const280 void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); }
281
282 // Returns index
index() const283 long index() const { return get_index_u2(Bytecodes::_instanceof); };
284 };
285
286 class Bytecode_new: public Bytecode {
287 public:
Bytecode_new(Method * method,address bcp)288 Bytecode_new(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
verify() const289 void verify() const { assert(java_code() == Bytecodes::_new, "check new"); }
290
291 // Returns index
index() const292 long index() const { return get_index_u2(Bytecodes::_new); };
293 };
294
295 class Bytecode_multianewarray: public Bytecode {
296 public:
Bytecode_multianewarray(Method * method,address bcp)297 Bytecode_multianewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
verify() const298 void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); }
299
300 // Returns index
index() const301 long index() const { return get_index_u2(Bytecodes::_multianewarray); };
302 };
303
304 class Bytecode_anewarray: public Bytecode {
305 public:
Bytecode_anewarray(Method * method,address bcp)306 Bytecode_anewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
verify() const307 void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); }
308
309 // Returns index
index() const310 long index() const { return get_index_u2(Bytecodes::_anewarray); };
311 };
312
313 // Abstraction for ldc, ldc_w and ldc2_w
314 class Bytecode_loadconstant: public Bytecode {
315 private:
316 const Method* _method;
317
318 int raw_index() const;
319
320 public:
Bytecode_loadconstant(const methodHandle & method,int bci)321 Bytecode_loadconstant(const methodHandle& method, int bci): Bytecode(method(), method->bcp_from(bci)), _method(method()) { verify(); }
322
verify() const323 void verify() const {
324 assert(_method != NULL, "must supply method");
325 Bytecodes::Code stdc = Bytecodes::java_code(code());
326 assert(stdc == Bytecodes::_ldc ||
327 stdc == Bytecodes::_ldc_w ||
328 stdc == Bytecodes::_ldc2_w, "load constant");
329 }
330
331 // Only non-standard bytecodes (fast_aldc) have reference cache indexes.
has_cache_index() const332 bool has_cache_index() const { return code() >= Bytecodes::number_of_java_codes; }
333
334 int pool_index() const; // index into constant pool
cache_index() const335 int cache_index() const { // index into reference cache (or -1 if none)
336 return has_cache_index() ? raw_index() : -1;
337 }
338
339 BasicType result_type() const; // returns the result type of the ldc
340
341 oop resolve_constant(TRAPS) const;
342 };
343
344 #endif // SHARE_INTERPRETER_BYTECODE_HPP
345