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