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