1/*
2 * Copyright (c) 1997, 2019, 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_BYTECODES_HPP
26#define SHARE_INTERPRETER_BYTECODES_HPP
27
28#include "memory/allocation.hpp"
29
30// Bytecodes specifies all bytecodes used in the VM and
31// provides utility functions to get bytecode attributes.
32
33class Method;
34
35// NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/interpreter/Bytecodes.java
36class Bytecodes: AllStatic {
37 public:
38 enum Code {
39 _illegal = -1,
40
41 // Java bytecodes
42 _nop = 0, // 0x00
43 _aconst_null = 1, // 0x01
44 _iconst_m1 = 2, // 0x02
45 _iconst_0 = 3, // 0x03
46 _iconst_1 = 4, // 0x04
47 _iconst_2 = 5, // 0x05
48 _iconst_3 = 6, // 0x06
49 _iconst_4 = 7, // 0x07
50 _iconst_5 = 8, // 0x08
51 _lconst_0 = 9, // 0x09
52 _lconst_1 = 10, // 0x0a
53 _fconst_0 = 11, // 0x0b
54 _fconst_1 = 12, // 0x0c
55 _fconst_2 = 13, // 0x0d
56 _dconst_0 = 14, // 0x0e
57 _dconst_1 = 15, // 0x0f
58 _bipush = 16, // 0x10
59 _sipush = 17, // 0x11
60 _ldc = 18, // 0x12
61 _ldc_w = 19, // 0x13
62 _ldc2_w = 20, // 0x14
63 _iload = 21, // 0x15
64 _lload = 22, // 0x16
65 _fload = 23, // 0x17
66 _dload = 24, // 0x18
67 _aload = 25, // 0x19
68 _iload_0 = 26, // 0x1a
69 _iload_1 = 27, // 0x1b
70 _iload_2 = 28, // 0x1c
71 _iload_3 = 29, // 0x1d
72 _lload_0 = 30, // 0x1e
73 _lload_1 = 31, // 0x1f
74 _lload_2 = 32, // 0x20
75 _lload_3 = 33, // 0x21
76 _fload_0 = 34, // 0x22
77 _fload_1 = 35, // 0x23
78 _fload_2 = 36, // 0x24
79 _fload_3 = 37, // 0x25
80 _dload_0 = 38, // 0x26
81 _dload_1 = 39, // 0x27
82 _dload_2 = 40, // 0x28
83 _dload_3 = 41, // 0x29
84 _aload_0 = 42, // 0x2a
85 _aload_1 = 43, // 0x2b
86 _aload_2 = 44, // 0x2c
87 _aload_3 = 45, // 0x2d
88 _iaload = 46, // 0x2e
89 _laload = 47, // 0x2f
90 _faload = 48, // 0x30
91 _daload = 49, // 0x31
92 _aaload = 50, // 0x32
93 _baload = 51, // 0x33
94 _caload = 52, // 0x34
95 _saload = 53, // 0x35
96 _istore = 54, // 0x36
97 _lstore = 55, // 0x37
98 _fstore = 56, // 0x38
99 _dstore = 57, // 0x39
100 _astore = 58, // 0x3a
101 _istore_0 = 59, // 0x3b
102 _istore_1 = 60, // 0x3c
103 _istore_2 = 61, // 0x3d
104 _istore_3 = 62, // 0x3e
105 _lstore_0 = 63, // 0x3f
106 _lstore_1 = 64, // 0x40
107 _lstore_2 = 65, // 0x41
108 _lstore_3 = 66, // 0x42
109 _fstore_0 = 67, // 0x43
110 _fstore_1 = 68, // 0x44
111 _fstore_2 = 69, // 0x45
112 _fstore_3 = 70, // 0x46
113 _dstore_0 = 71, // 0x47
114 _dstore_1 = 72, // 0x48
115 _dstore_2 = 73, // 0x49
116 _dstore_3 = 74, // 0x4a
117 _astore_0 = 75, // 0x4b
118 _astore_1 = 76, // 0x4c
119 _astore_2 = 77, // 0x4d
120 _astore_3 = 78, // 0x4e
121 _iastore = 79, // 0x4f
122 _lastore = 80, // 0x50
123 _fastore = 81, // 0x51
124 _dastore = 82, // 0x52
125 _aastore = 83, // 0x53
126 _bastore = 84, // 0x54
127 _castore = 85, // 0x55
128 _sastore = 86, // 0x56
129 _pop = 87, // 0x57
130 _pop2 = 88, // 0x58
131 _dup = 89, // 0x59
132 _dup_x1 = 90, // 0x5a
133 _dup_x2 = 91, // 0x5b
134 _dup2 = 92, // 0x5c
135 _dup2_x1 = 93, // 0x5d
136 _dup2_x2 = 94, // 0x5e
137 _swap = 95, // 0x5f
138 _iadd = 96, // 0x60
139 _ladd = 97, // 0x61
140 _fadd = 98, // 0x62
141 _dadd = 99, // 0x63
142 _isub = 100, // 0x64
143 _lsub = 101, // 0x65
144 _fsub = 102, // 0x66
145 _dsub = 103, // 0x67
146 _imul = 104, // 0x68
147 _lmul = 105, // 0x69
148 _fmul = 106, // 0x6a
149 _dmul = 107, // 0x6b
150 _idiv = 108, // 0x6c
151 _ldiv = 109, // 0x6d
152 _fdiv = 110, // 0x6e
153 _ddiv = 111, // 0x6f
154 _irem = 112, // 0x70
155 _lrem = 113, // 0x71
156 _frem = 114, // 0x72
157 _drem = 115, // 0x73
158 _ineg = 116, // 0x74
159 _lneg = 117, // 0x75
160 _fneg = 118, // 0x76
161 _dneg = 119, // 0x77
162 _ishl = 120, // 0x78
163 _lshl = 121, // 0x79
164 _ishr = 122, // 0x7a
165 _lshr = 123, // 0x7b
166 _iushr = 124, // 0x7c
167 _lushr = 125, // 0x7d
168 _iand = 126, // 0x7e
169 _land = 127, // 0x7f
170 _ior = 128, // 0x80
171 _lor = 129, // 0x81
172 _ixor = 130, // 0x82
173 _lxor = 131, // 0x83
174 _iinc = 132, // 0x84
175 _i2l = 133, // 0x85
176 _i2f = 134, // 0x86
177 _i2d = 135, // 0x87
178 _l2i = 136, // 0x88
179 _l2f = 137, // 0x89
180 _l2d = 138, // 0x8a
181 _f2i = 139, // 0x8b
182 _f2l = 140, // 0x8c
183 _f2d = 141, // 0x8d
184 _d2i = 142, // 0x8e
185 _d2l = 143, // 0x8f
186 _d2f = 144, // 0x90
187 _i2b = 145, // 0x91
188 _i2c = 146, // 0x92
189 _i2s = 147, // 0x93
190 _lcmp = 148, // 0x94
191 _fcmpl = 149, // 0x95
192 _fcmpg = 150, // 0x96
193 _dcmpl = 151, // 0x97
194 _dcmpg = 152, // 0x98
195 _ifeq = 153, // 0x99
196 _ifne = 154, // 0x9a
197 _iflt = 155, // 0x9b
198 _ifge = 156, // 0x9c
199 _ifgt = 157, // 0x9d
200 _ifle = 158, // 0x9e
201 _if_icmpeq = 159, // 0x9f
202 _if_icmpne = 160, // 0xa0
203 _if_icmplt = 161, // 0xa1
204 _if_icmpge = 162, // 0xa2
205 _if_icmpgt = 163, // 0xa3
206 _if_icmple = 164, // 0xa4
207 _if_acmpeq = 165, // 0xa5
208 _if_acmpne = 166, // 0xa6
209 _goto = 167, // 0xa7
210 _jsr = 168, // 0xa8
211 _ret = 169, // 0xa9
212 _tableswitch = 170, // 0xaa
213 _lookupswitch = 171, // 0xab
214 _ireturn = 172, // 0xac
215 _lreturn = 173, // 0xad
216 _freturn = 174, // 0xae
217 _dreturn = 175, // 0xaf
218 _areturn = 176, // 0xb0
219 _return = 177, // 0xb1
220 _getstatic = 178, // 0xb2
221 _putstatic = 179, // 0xb3
222 _getfield = 180, // 0xb4
223 _putfield = 181, // 0xb5
224 _invokevirtual = 182, // 0xb6
225 _invokespecial = 183, // 0xb7
226 _invokestatic = 184, // 0xb8
227 _invokeinterface = 185, // 0xb9
228 _invokedynamic = 186, // 0xba
229 _new = 187, // 0xbb
230 _newarray = 188, // 0xbc
231 _anewarray = 189, // 0xbd
232 _arraylength = 190, // 0xbe
233 _athrow = 191, // 0xbf
234 _checkcast = 192, // 0xc0
235 _instanceof = 193, // 0xc1
236 _monitorenter = 194, // 0xc2
237 _monitorexit = 195, // 0xc3
238 _wide = 196, // 0xc4
239 _multianewarray = 197, // 0xc5
240 _ifnull = 198, // 0xc6
241 _ifnonnull = 199, // 0xc7
242 _goto_w = 200, // 0xc8
243 _jsr_w = 201, // 0xc9
244 _breakpoint = 202, // 0xca
245
246 number_of_java_codes,
247
248 // JVM bytecodes
249 _fast_agetfield = number_of_java_codes,
250 _fast_bgetfield ,
251 _fast_cgetfield ,
252 _fast_dgetfield ,
253 _fast_fgetfield ,
254 _fast_igetfield ,
255 _fast_lgetfield ,
256 _fast_sgetfield ,
257
258 _fast_aputfield ,
259 _fast_bputfield ,
260 _fast_zputfield ,
261 _fast_cputfield ,
262 _fast_dputfield ,
263 _fast_fputfield ,
264 _fast_iputfield ,
265 _fast_lputfield ,
266 _fast_sputfield ,
267
268 _fast_aload_0 ,
269 _fast_iaccess_0 ,
270 _fast_aaccess_0 ,
271 _fast_faccess_0 ,
272
273 _fast_iload ,
274 _fast_iload2 ,
275 _fast_icaload ,
276
277 _fast_invokevfinal ,
278 _fast_linearswitch ,
279 _fast_binaryswitch ,
280
281 // special handling of oop constants:
282 _fast_aldc ,
283 _fast_aldc_w ,
284
285 _return_register_finalizer ,
286
287 // special handling of signature-polymorphic methods:
288 _invokehandle ,
289
290 // These bytecodes are rewritten at CDS dump time, so that we can prevent them from being
291 // rewritten at run time. This way, the ConstMethods can be placed in the CDS ReadOnly
292 // section, and RewriteByteCodes/RewriteFrequentPairs can rewrite non-CDS bytecodes
293 // at run time.
294 //
295 // Rewritten at CDS dump time to | Original bytecode
296 // _invoke_virtual rewritten on sparc, will be disabled if UseSharedSpaces turned on.
297 // ------------------------------+------------------
298 _nofast_getfield , // <- _getfield
299 _nofast_putfield , // <- _putfield
300 _nofast_aload_0 , // <- _aload_0
301 _nofast_iload , // <- _iload
302
303 _shouldnotreachhere , // For debugging
304
305
306 number_of_codes
307 };
308
309 // Flag bits derived from format strings, can_trap, can_rewrite, etc.:
310 enum Flags {
311 // semantic flags:
312 _bc_can_trap = 1<<0, // bytecode execution can trap or block
313 _bc_can_rewrite = 1<<1, // bytecode execution has an alternate form
314
315 // format bits (determined only by the format string):
316 _fmt_has_c = 1<<2, // constant, such as sipush "bcc"
317 _fmt_has_j = 1<<3, // constant pool cache index, such as getfield "bjj"
318 _fmt_has_k = 1<<4, // constant pool index, such as ldc "bk"
319 _fmt_has_i = 1<<5, // local index, such as iload
320 _fmt_has_o = 1<<6, // offset, such as ifeq
321 _fmt_has_nbo = 1<<7, // contains native-order field(s)
322 _fmt_has_u2 = 1<<8, // contains double-byte field(s)
323 _fmt_has_u4 = 1<<9, // contains quad-byte field
324 _fmt_not_variable = 1<<10, // not of variable length (simple or wide)
325 _fmt_not_simple = 1<<11, // either wide or variable length
326 _all_fmt_bits = (_fmt_not_simple*2 - _fmt_has_c),
327
328 // Example derived format syndromes:
329 _fmt_b = _fmt_not_variable,
330 _fmt_bc = _fmt_b | _fmt_has_c,
331 _fmt_bi = _fmt_b | _fmt_has_i,
332 _fmt_bkk = _fmt_b | _fmt_has_k | _fmt_has_u2,
333 _fmt_bJJ = _fmt_b | _fmt_has_j | _fmt_has_u2 | _fmt_has_nbo,
334 _fmt_bo2 = _fmt_b | _fmt_has_o | _fmt_has_u2,
335 _fmt_bo4 = _fmt_b | _fmt_has_o | _fmt_has_u4
336 };
337
338 private:
339 static bool _is_initialized;
340 static const char* _name [number_of_codes];
341 static BasicType _result_type [number_of_codes];
342 static s_char _depth [number_of_codes];
343 static u_char _lengths [number_of_codes];
344 static Code _java_code [number_of_codes];
345 static jchar _flags [(1<<BitsPerByte)*2]; // all second page for wide formats
346
347 static void def(Code code, const char* name, const char* format, const char* wide_format, BasicType result_type, int depth, bool can_trap);
348 static void def(Code code, const char* name, const char* format, const char* wide_format, BasicType result_type, int depth, bool can_trap, Code java_code);
349
350 // Verify that bcp points into method
351#ifdef ASSERT
352 static bool check_method(const Method* method, address bcp);
353#endif
354 static bool check_must_rewrite(Bytecodes::Code bc);
355
356 public:
357 // Conversion
358 static void check (Code code) { assert(is_defined(code), "illegal code: %d", (int)code); }
359 static void wide_check (Code code) { assert(wide_is_defined(code), "illegal code: %d", (int)code); }
360 static Code cast (int code) { return (Code)code; }
361
362
363 // Fetch a bytecode, hiding breakpoints as necessary. The method
364 // argument is used for conversion of breakpoints into the original
365 // bytecode. The CI uses these methods but guarantees that
366 // breakpoints are hidden so the method argument should be passed as
367 // NULL since in that case the bcp and Method* are unrelated
368 // memory.
369 static Code code_at(const Method* method, address bcp) {
370 assert(method == NULL || check_method(method, bcp), "bcp must point into method");
371 Code code = cast(*bcp);
372 assert(code != _breakpoint || method != NULL, "need Method* to decode breakpoint");
373 return (code != _breakpoint) ? code : non_breakpoint_code_at(method, bcp);
374 }
375 static Code java_code_at(const Method* method, address bcp) {
376 return java_code(code_at(method, bcp));
377 }
378
379 // Fetch a bytecode or a breakpoint:
380 static Code code_or_bp_at(address bcp) { return (Code)cast(*bcp); }
381
382 static Code code_at(Method* method, int bci);
383
384 // find a bytecode, behind a breakpoint if necessary:
385 static Code non_breakpoint_code_at(const Method* method, address bcp);
386
387 // Bytecode attributes
388 static bool is_valid (int code) { return 0 <= code && code < number_of_codes; }
389 static bool is_defined (int code) { return is_valid(code) && flags(code, false) != 0; }
390 static bool wide_is_defined(int code) { return is_defined(code) && flags(code, true) != 0; }
391 static const char* name (Code code) { check(code); return _name [code]; }
392 static BasicType result_type (Code code) { check(code); return _result_type [code]; }
393 static int depth (Code code) { check(code); return _depth [code]; }
394 // Note: Length functions must return <=0 for invalid bytecodes.
395 // Calling check(code) in length functions would throw an unwanted assert.
396 static int length_for (Code code) { return is_valid(code) ? _lengths[code] & 0xF : -1; }
397 static int wide_length_for(Code code) { return is_valid(code) ? _lengths[code] >> 4 : -1; }
398 static bool can_trap (Code code) { check(code); return has_all_flags(code, _bc_can_trap, false); }
399 static Code java_code (Code code) { check(code); return _java_code [code]; }
400 static bool can_rewrite (Code code) { check(code); return has_all_flags(code, _bc_can_rewrite, false); }
401 static bool must_rewrite(Bytecodes::Code code) { return can_rewrite(code) && check_must_rewrite(code); }
402 static bool native_byte_order(Code code) { check(code); return has_all_flags(code, _fmt_has_nbo, false); }
403 static bool uses_cp_cache (Code code) { check(code); return has_all_flags(code, _fmt_has_j, false); }
404 // if 'end' is provided, it indicates the end of the code buffer which
405 // should not be read past when parsing.
406 static int special_length_at(Bytecodes::Code code, address bcp, address end = NULL);
407 static int raw_special_length_at(address bcp, address end = NULL);
408 static int length_for_code_at(Bytecodes::Code code, address bcp) { int l = length_for(code); return l > 0 ? l : special_length_at(code, bcp); }
409 static int length_at (Method* method, address bcp) { return length_for_code_at(code_at(method, bcp), bcp); }
410 static int java_length_at (Method* method, address bcp) { return length_for_code_at(java_code_at(method, bcp), bcp); }
411 static bool is_java_code (Code code) { return 0 <= code && code < number_of_java_codes; }
412
413 static bool is_store_into_local(Code code){ return (_istore <= code && code <= _astore_3); }
414 static bool is_const (Code code) { return (_aconst_null <= code && code <= _ldc2_w); }
415 static bool is_zero_const (Code code) { return (code == _aconst_null || code == _iconst_0
416 || code == _fconst_0 || code == _dconst_0); }
417 static bool is_return (Code code) { return (_ireturn <= code && code <= _return); }
418 static bool is_invoke (Code code) { return (_invokevirtual <= code && code <= _invokedynamic); }
419 static bool has_receiver (Code code) { assert(is_invoke(code), ""); return code == _invokevirtual ||
420 code == _invokespecial ||
421 code == _invokeinterface; }
422 static bool has_optional_appendix(Code code) { return code == _invokedynamic || code == _invokehandle; }
423
424 static int compute_flags (const char* format, int more_flags = 0); // compute the flags
425 static int flags (int code, bool is_wide) {
426 assert(code == (u_char)code, "must be a byte");
427 return _flags[code + (is_wide ? (1<<BitsPerByte) : 0)];
428 }
429 static bool has_all_flags (Code code, int test_flags, bool is_wide) {
430 return (flags(code, is_wide) & test_flags) == test_flags;
431 }
432
433 // Initialization
434 static void initialize ();
435};
436
437#endif // SHARE_INTERPRETER_BYTECODES_HPP
438