1 /*
  2  * Copyright (c) 1997, 2024, 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 #include "precompiled.hpp"
 26 #include "interpreter/bytecodes.hpp"
 27 #include "memory/resourceArea.hpp"
 28 #include "oops/method.hpp"
 29 #include "utilities/align.hpp"
 30 #include "utilities/bytes.hpp"
 31 
 32 #define JVM_BYTECODES_DO(def)                                                                                                     \
 33   def(_fast_agetfield            , "fast_agetfield"            , "bJJ"  , nullptr    , T_OBJECT ,  0, true , _getfield          ) \
 34   def(_fast_bgetfield            , "fast_bgetfield"            , "bJJ"  , nullptr    , T_INT    ,  0, true , _getfield          ) \
 35   def(_fast_cgetfield            , "fast_cgetfield"            , "bJJ"  , nullptr    , T_CHAR   ,  0, true , _getfield          ) \
 36   def(_fast_dgetfield            , "fast_dgetfield"            , "bJJ"  , nullptr    , T_DOUBLE ,  0, true , _getfield          ) \
 37   def(_fast_fgetfield            , "fast_fgetfield"            , "bJJ"  , nullptr    , T_FLOAT  ,  0, true , _getfield          ) \
 38   def(_fast_igetfield            , "fast_igetfield"            , "bJJ"  , nullptr    , T_INT    ,  0, true , _getfield          ) \
 39   def(_fast_lgetfield            , "fast_lgetfield"            , "bJJ"  , nullptr    , T_LONG   ,  0, true , _getfield          ) \
 40   def(_fast_sgetfield            , "fast_sgetfield"            , "bJJ"  , nullptr    , T_SHORT  ,  0, true , _getfield          ) \
 41                                                                                                                                   \
 42   def(_fast_aputfield            , "fast_aputfield"            , "bJJ"  , nullptr    , T_OBJECT ,  0, true , _putfield          ) \
 43   def(_fast_bputfield            , "fast_bputfield"            , "bJJ"  , nullptr    , T_INT    ,  0, true , _putfield          ) \
 44   def(_fast_zputfield            , "fast_zputfield"            , "bJJ"  , nullptr    , T_INT    ,  0, true , _putfield          ) \
 45   def(_fast_cputfield            , "fast_cputfield"            , "bJJ"  , nullptr    , T_CHAR   ,  0, true , _putfield          ) \
 46   def(_fast_dputfield            , "fast_dputfield"            , "bJJ"  , nullptr    , T_DOUBLE ,  0, true , _putfield          ) \
 47   def(_fast_fputfield            , "fast_fputfield"            , "bJJ"  , nullptr    , T_FLOAT  ,  0, true , _putfield          ) \
 48   def(_fast_iputfield            , "fast_iputfield"            , "bJJ"  , nullptr    , T_INT    ,  0, true , _putfield          ) \
 49   def(_fast_lputfield            , "fast_lputfield"            , "bJJ"  , nullptr    , T_LONG   ,  0, true , _putfield          ) \
 50   def(_fast_sputfield            , "fast_sputfield"            , "bJJ"  , nullptr    , T_SHORT  ,  0, true , _putfield          ) \
 51                                                                                                                                   \
 52   def(_fast_aload_0              , "fast_aload_0"              , "b"    , nullptr    , T_OBJECT ,  1, true , _aload_0           ) \
 53   def(_fast_iaccess_0            , "fast_iaccess_0"            , "b_JJ" , nullptr    , T_INT    ,  1, true , _aload_0           ) \
 54   def(_fast_aaccess_0            , "fast_aaccess_0"            , "b_JJ" , nullptr    , T_OBJECT ,  1, true , _aload_0           ) \
 55   def(_fast_faccess_0            , "fast_faccess_0"            , "b_JJ" , nullptr    , T_OBJECT ,  1, true , _aload_0           ) \
 56                                                                                                                                   \
 57   def(_fast_iload                , "fast_iload"                , "bi"   , nullptr    , T_INT    ,  1, false, _iload             ) \
 58   def(_fast_iload2               , "fast_iload2"               , "bi_i" , nullptr    , T_INT    ,  2, false, _iload             ) \
 59   def(_fast_icaload              , "fast_icaload"              , "bi_"  , nullptr    , T_INT    ,  0, false, _iload             ) \
 60                                                                                                                                   \
 61   def(_fast_invokevfinal         , "fast_invokevfinal"         , "bJJ"  , nullptr    , T_ILLEGAL, -1, true , _invokevirtual     ) \
 62   def(_fast_linearswitch         , "fast_linearswitch"         , ""     , nullptr    , T_VOID   , -1, false, _lookupswitch      ) \
 63   def(_fast_binaryswitch         , "fast_binaryswitch"         , ""     , nullptr    , T_VOID   , -1, false, _lookupswitch      ) \
 64                                                                                                                                   \
 65   def(_fast_aldc                 , "fast_aldc"                 , "bj"   , nullptr    , T_OBJECT ,  1, true ,  _ldc              ) \
 66   def(_fast_aldc_w               , "fast_aldc_w"               , "bJJ"  , nullptr    , T_OBJECT ,  1, true ,  _ldc_w            ) \
 67                                                                                                                                   \
 68   def(_return_register_finalizer , "return_register_finalizer" , "b"     , nullptr    , T_VOID  ,  0, true , _return            ) \
 69                                                                                                                                   \
 70   def(_invokehandle              , "invokehandle"              , "bJJ"  , nullptr    , T_ILLEGAL, -1, true , _invokevirtual     ) \
 71                                                                                                                                   \
 72   def(_nofast_getfield           , "nofast_getfield"           , "bJJ"  , nullptr    , T_ILLEGAL,  0, true ,  _getfield         ) \
 73   def(_nofast_putfield           , "nofast_putfield"           , "bJJ"  , nullptr    , T_ILLEGAL, -2, true , _putfield          ) \
 74   def(_nofast_aload_0            , "nofast_aload_0"            , "b"    , nullptr    , T_OBJECT ,  1, true , _aload_0           ) \
 75   def(_nofast_iload              , "nofast_iload"              , "bi"   , nullptr    , T_INT    ,  1, false, _iload             ) \
 76                                                                                                                                   \
 77   def(_shouldnotreachhere        , "_shouldnotreachhere"       , "b"    , nullptr    , T_VOID   ,  0, false, _shouldnotreachhere)
 78 
 79 #define BYTECODES_DO(def)                                                                                  \
 80   def(_nop             , "nop"             , "b"    , nullptr    , T_VOID   ,  0, false, _nop            ) \
 81   def(_aconst_null     , "aconst_null"     , "b"    , nullptr    , T_OBJECT ,  1, false, _aconst_null    ) \
 82   def(_iconst_m1       , "iconst_m1"       , "b"    , nullptr    , T_INT    ,  1, false, _iconst_m1      ) \
 83   def(_iconst_0        , "iconst_0"        , "b"    , nullptr    , T_INT    ,  1, false, _iconst_0       ) \
 84   def(_iconst_1        , "iconst_1"        , "b"    , nullptr    , T_INT    ,  1, false, _iconst_1       ) \
 85   def(_iconst_2        , "iconst_2"        , "b"    , nullptr    , T_INT    ,  1, false, _iconst_2       ) \
 86   def(_iconst_3        , "iconst_3"        , "b"    , nullptr    , T_INT    ,  1, false, _iconst_3       ) \
 87   def(_iconst_4        , "iconst_4"        , "b"    , nullptr    , T_INT    ,  1, false, _iconst_4       ) \
 88   def(_iconst_5        , "iconst_5"        , "b"    , nullptr    , T_INT    ,  1, false, _iconst_5       ) \
 89   def(_lconst_0        , "lconst_0"        , "b"    , nullptr    , T_LONG   ,  2, false, _lconst_0       ) \
 90   def(_lconst_1        , "lconst_1"        , "b"    , nullptr    , T_LONG   ,  2, false, _lconst_1       ) \
 91   def(_fconst_0        , "fconst_0"        , "b"    , nullptr    , T_FLOAT  ,  1, false, _fconst_0       ) \
 92   def(_fconst_1        , "fconst_1"        , "b"    , nullptr    , T_FLOAT  ,  1, false, _fconst_1       ) \
 93   def(_fconst_2        , "fconst_2"        , "b"    , nullptr    , T_FLOAT  ,  1, false, _fconst_2       ) \
 94   def(_dconst_0        , "dconst_0"        , "b"    , nullptr    , T_DOUBLE ,  2, false, _dconst_0       ) \
 95   def(_dconst_1        , "dconst_1"        , "b"    , nullptr    , T_DOUBLE ,  2, false, _dconst_1       ) \
 96   def(_bipush          , "bipush"          , "bc"   , nullptr    , T_INT    ,  1, false, _bipush         ) \
 97   def(_sipush          , "sipush"          , "bcc"  , nullptr    , T_INT    ,  1, false, _sipush         ) \
 98   def(_ldc             , "ldc"             , "bk"   , nullptr    , T_ILLEGAL,  1, true , _ldc            ) \
 99   def(_ldc_w           , "ldc_w"           , "bkk"  , nullptr    , T_ILLEGAL,  1, true , _ldc_w          ) \
100   def(_ldc2_w          , "ldc2_w"          , "bkk"  , nullptr    , T_ILLEGAL,  2, true , _ldc2_w         ) \
101   def(_iload           , "iload"           , "bi"   , "wbii"     , T_INT    ,  1, false, _iload          ) \
102   def(_lload           , "lload"           , "bi"   , "wbii"     , T_LONG   ,  2, false, _lload          ) \
103   def(_fload           , "fload"           , "bi"   , "wbii"     , T_FLOAT  ,  1, false, _fload          ) \
104   def(_dload           , "dload"           , "bi"   , "wbii"     , T_DOUBLE ,  2, false, _dload          ) \
105   def(_aload           , "aload"           , "bi"   , "wbii"     , T_OBJECT ,  1, false, _aload          ) \
106   def(_iload_0         , "iload_0"         , "b"    , nullptr    , T_INT    ,  1, false, _iload_0        ) \
107   def(_iload_1         , "iload_1"         , "b"    , nullptr    , T_INT    ,  1, false, _iload_1        ) \
108   def(_iload_2         , "iload_2"         , "b"    , nullptr    , T_INT    ,  1, false, _iload_2        ) \
109   def(_iload_3         , "iload_3"         , "b"    , nullptr    , T_INT    ,  1, false, _iload_3        ) \
110   def(_lload_0         , "lload_0"         , "b"    , nullptr    , T_LONG   ,  2, false, _lload_0        ) \
111   def(_lload_1         , "lload_1"         , "b"    , nullptr    , T_LONG   ,  2, false, _lload_1        ) \
112   def(_lload_2         , "lload_2"         , "b"    , nullptr    , T_LONG   ,  2, false, _lload_2        ) \
113   def(_lload_3         , "lload_3"         , "b"    , nullptr    , T_LONG   ,  2, false, _lload_3        ) \
114   def(_fload_0         , "fload_0"         , "b"    , nullptr    , T_FLOAT  ,  1, false, _fload_0        ) \
115   def(_fload_1         , "fload_1"         , "b"    , nullptr    , T_FLOAT  ,  1, false, _fload_1        ) \
116   def(_fload_2         , "fload_2"         , "b"    , nullptr    , T_FLOAT  ,  1, false, _fload_2        ) \
117   def(_fload_3         , "fload_3"         , "b"    , nullptr    , T_FLOAT  ,  1, false, _fload_3        ) \
118   def(_dload_0         , "dload_0"         , "b"    , nullptr    , T_DOUBLE ,  2, false, _dload_0        ) \
119   def(_dload_1         , "dload_1"         , "b"    , nullptr    , T_DOUBLE ,  2, false, _dload_1        ) \
120   def(_dload_2         , "dload_2"         , "b"    , nullptr    , T_DOUBLE ,  2, false, _dload_2        ) \
121   def(_dload_3         , "dload_3"         , "b"    , nullptr    , T_DOUBLE ,  2, false, _dload_3        ) \
122   def(_aload_0         , "aload_0"         , "b"    , nullptr    , T_OBJECT ,  1, true , _aload_0        ) \
123   def(_aload_1         , "aload_1"         , "b"    , nullptr    , T_OBJECT ,  1, false, _aload_1        ) \
124   def(_aload_2         , "aload_2"         , "b"    , nullptr    , T_OBJECT ,  1, false, _aload_2        ) \
125   def(_aload_3         , "aload_3"         , "b"    , nullptr    , T_OBJECT ,  1, false, _aload_3        ) \
126   def(_iaload          , "iaload"          , "b"    , nullptr    , T_INT    , -1, true , _iaload         ) \
127   def(_laload          , "laload"          , "b"    , nullptr    , T_LONG   ,  0, true , _laload         ) \
128   def(_faload          , "faload"          , "b"    , nullptr    , T_FLOAT  , -1, true , _faload         ) \
129   def(_daload          , "daload"          , "b"    , nullptr    , T_DOUBLE ,  0, true , _daload         ) \
130   def(_aaload          , "aaload"          , "b"    , nullptr    , T_OBJECT , -1, true , _aaload         ) \
131   def(_baload          , "baload"          , "b"    , nullptr    , T_INT    , -1, true , _baload         ) \
132   def(_caload          , "caload"          , "b"    , nullptr    , T_INT    , -1, true , _caload         ) \
133   def(_saload          , "saload"          , "b"    , nullptr    , T_INT    , -1, true , _saload         ) \
134   def(_istore          , "istore"          , "bi"   , "wbii"     , T_VOID   , -1, false, _istore         ) \
135   def(_lstore          , "lstore"          , "bi"   , "wbii"     , T_VOID   , -2, false, _lstore         ) \
136   def(_fstore          , "fstore"          , "bi"   , "wbii"     , T_VOID   , -1, false, _fstore         ) \
137   def(_dstore          , "dstore"          , "bi"   , "wbii"     , T_VOID   , -2, false, _dstore         ) \
138   def(_astore          , "astore"          , "bi"   , "wbii"     , T_VOID   , -1, false, _astore         ) \
139   def(_istore_0        , "istore_0"        , "b"    , nullptr    , T_VOID   , -1, false, _istore_0       ) \
140   def(_istore_1        , "istore_1"        , "b"    , nullptr    , T_VOID   , -1, false, _istore_1       ) \
141   def(_istore_2        , "istore_2"        , "b"    , nullptr    , T_VOID   , -1, false, _istore_2       ) \
142   def(_istore_3        , "istore_3"        , "b"    , nullptr    , T_VOID   , -1, false, _istore_3       ) \
143   def(_lstore_0        , "lstore_0"        , "b"    , nullptr    , T_VOID   , -2, false, _lstore_0       ) \
144   def(_lstore_1        , "lstore_1"        , "b"    , nullptr    , T_VOID   , -2, false, _lstore_1       ) \
145   def(_lstore_2        , "lstore_2"        , "b"    , nullptr    , T_VOID   , -2, false, _lstore_2       ) \
146   def(_lstore_3        , "lstore_3"        , "b"    , nullptr    , T_VOID   , -2, false, _lstore_3       ) \
147   def(_fstore_0        , "fstore_0"        , "b"    , nullptr    , T_VOID   , -1, false, _fstore_0       ) \
148   def(_fstore_1        , "fstore_1"        , "b"    , nullptr    , T_VOID   , -1, false, _fstore_1       ) \
149   def(_fstore_2        , "fstore_2"        , "b"    , nullptr    , T_VOID   , -1, false, _fstore_2       ) \
150   def(_fstore_3        , "fstore_3"        , "b"    , nullptr    , T_VOID   , -1, false, _fstore_3       ) \
151   def(_dstore_0        , "dstore_0"        , "b"    , nullptr    , T_VOID   , -2, false, _dstore_0       ) \
152   def(_dstore_1        , "dstore_1"        , "b"    , nullptr    , T_VOID   , -2, false, _dstore_1       ) \
153   def(_dstore_2        , "dstore_2"        , "b"    , nullptr    , T_VOID   , -2, false, _dstore_2       ) \
154   def(_dstore_3        , "dstore_3"        , "b"    , nullptr    , T_VOID   , -2, false, _dstore_3       ) \
155   def(_astore_0        , "astore_0"        , "b"    , nullptr    , T_VOID   , -1, false, _astore_0       ) \
156   def(_astore_1        , "astore_1"        , "b"    , nullptr    , T_VOID   , -1, false, _astore_1       ) \
157   def(_astore_2        , "astore_2"        , "b"    , nullptr    , T_VOID   , -1, false, _astore_2       ) \
158   def(_astore_3        , "astore_3"        , "b"    , nullptr    , T_VOID   , -1, false, _astore_3       ) \
159   def(_iastore         , "iastore"         , "b"    , nullptr    , T_VOID   , -3, true , _iastore        ) \
160   def(_lastore         , "lastore"         , "b"    , nullptr    , T_VOID   , -4, true , _lastore        ) \
161   def(_fastore         , "fastore"         , "b"    , nullptr    , T_VOID   , -3, true , _fastore        ) \
162   def(_dastore         , "dastore"         , "b"    , nullptr    , T_VOID   , -4, true , _dastore        ) \
163   def(_aastore         , "aastore"         , "b"    , nullptr    , T_VOID   , -3, true , _aastore        ) \
164   def(_bastore         , "bastore"         , "b"    , nullptr    , T_VOID   , -3, true , _bastore        ) \
165   def(_castore         , "castore"         , "b"    , nullptr    , T_VOID   , -3, true , _castore        ) \
166   def(_sastore         , "sastore"         , "b"    , nullptr    , T_VOID   , -3, true , _sastore        ) \
167   def(_pop             , "pop"             , "b"    , nullptr    , T_VOID   , -1, false, _pop            ) \
168   def(_pop2            , "pop2"            , "b"    , nullptr    , T_VOID   , -2, false, _pop2           ) \
169   def(_dup             , "dup"             , "b"    , nullptr    , T_VOID   ,  1, false, _dup            ) \
170   def(_dup_x1          , "dup_x1"          , "b"    , nullptr    , T_VOID   ,  1, false, _dup_x1         ) \
171   def(_dup_x2          , "dup_x2"          , "b"    , nullptr    , T_VOID   ,  1, false, _dup_x2         ) \
172   def(_dup2            , "dup2"            , "b"    , nullptr    , T_VOID   ,  2, false, _dup2           ) \
173   def(_dup2_x1         , "dup2_x1"         , "b"    , nullptr    , T_VOID   ,  2, false, _dup2_x1        ) \
174   def(_dup2_x2         , "dup2_x2"         , "b"    , nullptr    , T_VOID   ,  2, false, _dup2_x2        ) \
175   def(_swap            , "swap"            , "b"    , nullptr    , T_VOID   ,  0, false, _swap           ) \
176   def(_iadd            , "iadd"            , "b"    , nullptr    , T_INT    , -1, false, _iadd           ) \
177   def(_ladd            , "ladd"            , "b"    , nullptr    , T_LONG   , -2, false, _ladd           ) \
178   def(_fadd            , "fadd"            , "b"    , nullptr    , T_FLOAT  , -1, false, _fadd           ) \
179   def(_dadd            , "dadd"            , "b"    , nullptr    , T_DOUBLE , -2, false, _dadd           ) \
180   def(_isub            , "isub"            , "b"    , nullptr    , T_INT    , -1, false, _isub           ) \
181   def(_lsub            , "lsub"            , "b"    , nullptr    , T_LONG   , -2, false, _lsub           ) \
182   def(_fsub            , "fsub"            , "b"    , nullptr    , T_FLOAT  , -1, false, _fsub           ) \
183   def(_dsub            , "dsub"            , "b"    , nullptr    , T_DOUBLE , -2, false, _dsub           ) \
184   def(_imul            , "imul"            , "b"    , nullptr    , T_INT    , -1, false, _imul           ) \
185   def(_lmul            , "lmul"            , "b"    , nullptr    , T_LONG   , -2, false, _lmul           ) \
186   def(_fmul            , "fmul"            , "b"    , nullptr    , T_FLOAT  , -1, false, _fmul           ) \
187   def(_dmul            , "dmul"            , "b"    , nullptr    , T_DOUBLE , -2, false, _dmul           ) \
188   def(_idiv            , "idiv"            , "b"    , nullptr    , T_INT    , -1, true , _idiv           ) \
189   def(_ldiv            , "ldiv"            , "b"    , nullptr    , T_LONG   , -2, true , _ldiv           ) \
190   def(_fdiv            , "fdiv"            , "b"    , nullptr    , T_FLOAT  , -1, false, _fdiv           ) \
191   def(_ddiv            , "ddiv"            , "b"    , nullptr    , T_DOUBLE , -2, false, _ddiv           ) \
192   def(_irem            , "irem"            , "b"    , nullptr    , T_INT    , -1, true , _irem           ) \
193   def(_lrem            , "lrem"            , "b"    , nullptr    , T_LONG   , -2, true , _lrem           ) \
194   def(_frem            , "frem"            , "b"    , nullptr    , T_FLOAT  , -1, false, _frem           ) \
195   def(_drem            , "drem"            , "b"    , nullptr    , T_DOUBLE , -2, false, _drem           ) \
196   def(_ineg            , "ineg"            , "b"    , nullptr    , T_INT    ,  0, false, _ineg           ) \
197   def(_lneg            , "lneg"            , "b"    , nullptr    , T_LONG   ,  0, false, _lneg           ) \
198   def(_fneg            , "fneg"            , "b"    , nullptr    , T_FLOAT  ,  0, false, _fneg           ) \
199   def(_dneg            , "dneg"            , "b"    , nullptr    , T_DOUBLE ,  0, false, _dneg           ) \
200   def(_ishl            , "ishl"            , "b"    , nullptr    , T_INT    , -1, false, _ishl           ) \
201   def(_lshl            , "lshl"            , "b"    , nullptr    , T_LONG   , -1, false, _lshl           ) \
202   def(_ishr            , "ishr"            , "b"    , nullptr    , T_INT    , -1, false, _ishr           ) \
203   def(_lshr            , "lshr"            , "b"    , nullptr    , T_LONG   , -1, false, _lshr           ) \
204   def(_iushr           , "iushr"           , "b"    , nullptr    , T_INT    , -1, false, _iushr          ) \
205   def(_lushr           , "lushr"           , "b"    , nullptr    , T_LONG   , -1, false, _lushr          ) \
206   def(_iand            , "iand"            , "b"    , nullptr    , T_INT    , -1, false, _iand           ) \
207   def(_land            , "land"            , "b"    , nullptr    , T_LONG   , -2, false, _land           ) \
208   def(_ior             , "ior"             , "b"    , nullptr    , T_INT    , -1, false, _ior            ) \
209   def(_lor             , "lor"             , "b"    , nullptr    , T_LONG   , -2, false, _lor            ) \
210   def(_ixor            , "ixor"            , "b"    , nullptr    , T_INT    , -1, false, _ixor           ) \
211   def(_lxor            , "lxor"            , "b"    , nullptr    , T_LONG   , -2, false, _lxor           ) \
212   def(_iinc            , "iinc"            , "bic"  , "wbiicc",    T_VOID   ,  0, false, _iinc           ) \
213   def(_i2l             , "i2l"             , "b"    , nullptr    , T_LONG   ,  1, false, _i2l            ) \
214   def(_i2f             , "i2f"             , "b"    , nullptr    , T_FLOAT  ,  0, false, _i2f            ) \
215   def(_i2d             , "i2d"             , "b"    , nullptr    , T_DOUBLE ,  1, false, _i2d            ) \
216   def(_l2i             , "l2i"             , "b"    , nullptr    , T_INT    , -1, false, _l2i            ) \
217   def(_l2f             , "l2f"             , "b"    , nullptr    , T_FLOAT  , -1, false, _l2f            ) \
218   def(_l2d             , "l2d"             , "b"    , nullptr    , T_DOUBLE ,  0, false, _l2d            ) \
219   def(_f2i             , "f2i"             , "b"    , nullptr    , T_INT    ,  0, false, _f2i            ) \
220   def(_f2l             , "f2l"             , "b"    , nullptr    , T_LONG   ,  1, false, _f2l            ) \
221   def(_f2d             , "f2d"             , "b"    , nullptr    , T_DOUBLE ,  1, false, _f2d            ) \
222   def(_d2i             , "d2i"             , "b"    , nullptr    , T_INT    , -1, false, _d2i            ) \
223   def(_d2l             , "d2l"             , "b"    , nullptr    , T_LONG   ,  0, false, _d2l            ) \
224   def(_d2f             , "d2f"             , "b"    , nullptr    , T_FLOAT  , -1, false, _d2f            ) \
225   def(_i2b             , "i2b"             , "b"    , nullptr    , T_BYTE   ,  0, false, _i2b            ) \
226   def(_i2c             , "i2c"             , "b"    , nullptr    , T_CHAR   ,  0, false, _i2c            ) \
227   def(_i2s             , "i2s"             , "b"    , nullptr    , T_SHORT  ,  0, false, _i2s            ) \
228   def(_lcmp            , "lcmp"            , "b"    , nullptr    , T_VOID   , -3, false, _lcmp           ) \
229   def(_fcmpl           , "fcmpl"           , "b"    , nullptr    , T_VOID   , -1, false, _fcmpl          ) \
230   def(_fcmpg           , "fcmpg"           , "b"    , nullptr    , T_VOID   , -1, false, _fcmpg          ) \
231   def(_dcmpl           , "dcmpl"           , "b"    , nullptr    , T_VOID   , -3, false, _dcmpl          ) \
232   def(_dcmpg           , "dcmpg"           , "b"    , nullptr    , T_VOID   , -3, false, _dcmpg          ) \
233   def(_ifeq            , "ifeq"            , "boo"  , nullptr    , T_VOID   , -1, false, _ifeq           ) \
234   def(_ifne            , "ifne"            , "boo"  , nullptr    , T_VOID   , -1, false, _ifne           ) \
235   def(_iflt            , "iflt"            , "boo"  , nullptr    , T_VOID   , -1, false, _iflt           ) \
236   def(_ifge            , "ifge"            , "boo"  , nullptr    , T_VOID   , -1, false, _ifge           ) \
237   def(_ifgt            , "ifgt"            , "boo"  , nullptr    , T_VOID   , -1, false, _ifgt           ) \
238   def(_ifle            , "ifle"            , "boo"  , nullptr    , T_VOID   , -1, false, _ifle           ) \
239   def(_if_icmpeq       , "if_icmpeq"       , "boo"  , nullptr    , T_VOID   , -2, false, _if_icmpeq      ) \
240   def(_if_icmpne       , "if_icmpne"       , "boo"  , nullptr    , T_VOID   , -2, false, _if_icmpne      ) \
241   def(_if_icmplt       , "if_icmplt"       , "boo"  , nullptr    , T_VOID   , -2, false, _if_icmplt      ) \
242   def(_if_icmpge       , "if_icmpge"       , "boo"  , nullptr    , T_VOID   , -2, false, _if_icmpge      ) \
243   def(_if_icmpgt       , "if_icmpgt"       , "boo"  , nullptr    , T_VOID   , -2, false, _if_icmpgt      ) \
244   def(_if_icmple       , "if_icmple"       , "boo"  , nullptr    , T_VOID   , -2, false, _if_icmple      ) \
245   def(_if_acmpeq       , "if_acmpeq"       , "boo"  , nullptr    , T_VOID   , -2, false, _if_acmpeq      ) \
246   def(_if_acmpne       , "if_acmpne"       , "boo"  , nullptr    , T_VOID   , -2, false, _if_acmpne      ) \
247   def(_goto            , "goto"            , "boo"  , nullptr    , T_VOID   ,  0, false, _goto           ) \
248   def(_jsr             , "jsr"             , "boo"  , nullptr    , T_INT    ,  0, false, _jsr            ) \
249   def(_ret             , "ret"             , "bi"   , "wbii"     , T_VOID   ,  0, false, _ret            ) \
250   def(_tableswitch     , "tableswitch"     , ""     , nullptr    , T_VOID   , -1, false, _tableswitch    ) \
251   def(_lookupswitch    , "lookupswitch"    , ""     , nullptr    , T_VOID   , -1, false, _lookupswitch   ) \
252   def(_ireturn         , "ireturn"         , "b"    , nullptr    , T_INT    , -1, true , _ireturn        ) \
253   def(_lreturn         , "lreturn"         , "b"    , nullptr    , T_LONG   , -2, true , _lreturn        ) \
254   def(_freturn         , "freturn"         , "b"    , nullptr    , T_FLOAT  , -1, true , _freturn        ) \
255   def(_dreturn         , "dreturn"         , "b"    , nullptr    , T_DOUBLE , -2, true , _dreturn        ) \
256   def(_areturn         , "areturn"         , "b"    , nullptr    , T_OBJECT , -1, true , _areturn        ) \
257   def(_return          , "return"          , "b"    , nullptr    , T_VOID   ,  0, true , _return         ) \
258   def(_getstatic       , "getstatic"       , "bJJ"  , nullptr    , T_ILLEGAL,  1, true , _getstatic      ) \
259   def(_putstatic       , "putstatic"       , "bJJ"  , nullptr    , T_ILLEGAL, -1, true , _putstatic      ) \
260   def(_getfield        , "getfield"        , "bJJ"  , nullptr    , T_ILLEGAL,  0, true , _getfield       ) \
261   def(_putfield        , "putfield"        , "bJJ"  , nullptr    , T_ILLEGAL, -2, true , _putfield       ) \
262   def(_invokevirtual   , "invokevirtual"   , "bJJ"  , nullptr    , T_ILLEGAL, -1, true , _invokevirtual  ) \
263   def(_invokespecial   , "invokespecial"   , "bJJ"  , nullptr    , T_ILLEGAL, -1, true , _invokespecial  ) \
264   def(_invokestatic    , "invokestatic"    , "bJJ"  , nullptr    , T_ILLEGAL,  0, true , _invokestatic   ) \
265   def(_invokeinterface , "invokeinterface" , "bJJ__", nullptr    , T_ILLEGAL, -1, true , _invokeinterface) \
266   def(_invokedynamic   , "invokedynamic"   , "bJJJJ", nullptr    , T_ILLEGAL,  0, true , _invokedynamic  ) \
267   def(_new             , "new"             , "bkk"  , nullptr    , T_OBJECT ,  1, true , _new            ) \
268   def(_newarray        , "newarray"        , "bc"   , nullptr    , T_OBJECT ,  0, true , _newarray       ) \
269   def(_anewarray       , "anewarray"       , "bkk"  , nullptr    , T_OBJECT ,  0, true , _anewarray      ) \
270   def(_arraylength     , "arraylength"     , "b"    , nullptr    , T_INT    ,  0, true , _arraylength    ) \
271   def(_athrow          , "athrow"          , "b"    , nullptr    , T_VOID   , -1, true , _athrow         ) \
272   def(_checkcast       , "checkcast"       , "bkk"  , nullptr    , T_OBJECT ,  0, true , _checkcast      ) \
273   def(_instanceof      , "instanceof"      , "bkk"  , nullptr    , T_INT    ,  0, true , _instanceof     ) \
274   def(_monitorenter    , "monitorenter"    , "b"    , nullptr    , T_VOID   , -1, true , _monitorenter   ) \
275   def(_monitorexit     , "monitorexit"     , "b"    , nullptr    , T_VOID   , -1, true , _monitorexit    ) \
276   def(_wide            , "wide"            , ""     , nullptr    , T_VOID   ,  0, false, _wide           ) \
277   def(_multianewarray  , "multianewarray"  , "bkkc" , nullptr    , T_OBJECT ,  1, true , _multianewarray ) \
278   def(_ifnull          , "ifnull"          , "boo"  , nullptr    , T_VOID   , -1, false, _ifnull         ) \
279   def(_ifnonnull       , "ifnonnull"       , "boo"  , nullptr    , T_VOID   , -1, false, _ifnonnull      ) \
280   def(_goto_w          , "goto_w"          , "boooo", nullptr    , T_VOID   ,  0, false, _goto_w         ) \
281   def(_jsr_w           , "jsr_w"           , "boooo", nullptr    , T_INT    ,  0, false, _jsr_w          ) \
282   def(_breakpoint      , "breakpoint"      , ""     , nullptr    , T_VOID   ,  0, true , _breakpoint     ) \
283   JVM_BYTECODES_DO(def)
284 
285 bool Bytecodes::_is_initialized = false;
286 
287 const char* const Bytecodes::_name[Bytecodes::number_of_codes] = {
288 #define BYTECODE_NAME(code, name, format, wide_format, result_type, depth, can_trap, java_code) name,
289   BYTECODES_DO(BYTECODE_NAME)
290 #undef BYTECODE_NAME
291 };
292 
293 const BasicType Bytecodes::_result_type[Bytecodes::number_of_codes] = {
294 #define BYTECODE_RESULT_TYPE(code, name, format, wide_format, result_type, depth, can_trap, java_code) result_type,
295   BYTECODES_DO(BYTECODE_RESULT_TYPE)
296 #undef BYTECODE_RESULT_TYPE
297 };
298 
299 const s_char Bytecodes::_depth[Bytecodes::number_of_codes] = {
300 #define BYTECODE_DEPTH(code, name, format, wide_format, result_type, depth, can_trap, java_code) depth,
301   BYTECODES_DO(BYTECODE_DEPTH)
302 #undef BYTECODE_DEPTH
303 };
304 
305 // Helper for determining the size (a.k.a. length) of a string literal.
306 struct StringLiteralSize {
307   template <size_t N>
308   static constexpr size_t invoke(const char (&)[N]) {
309     static_assert(N > 0, "N must be greater than 0");
310     // The size is N - 1, as C strings have an implicit NUL at the end. So "foo" will result in N
311     // being 4, but we actually want 3.
312     return N - 1;
313   }
314 
315   static constexpr size_t invoke(std::nullptr_t) {
316     return 0;
317   }
318 };
319 
320 #define STRING_SIZE(string) StringLiteralSize::invoke(string)
321 
322 const u_char Bytecodes::_lengths[Bytecodes::number_of_codes] = {
323 #define BYTECODE_LENGTHS(code, name, format, wide_format, result_type, depth, can_trap, java_code) static_cast<u_char>((STRING_SIZE(wide_format) << 4) | (STRING_SIZE(format) & 0xf)),
324   BYTECODES_DO(BYTECODE_LENGTHS)
325 #undef BYTECODE_LENGTHS
326 };
327 
328 const Bytecodes::Code Bytecodes::_java_code[Bytecodes::number_of_codes] = {
329 #define BYTECODE_JAVA_CODE(code, name, format, wide_format, result_type, depth, can_trap, java_code) Bytecodes::java_code,
330   BYTECODES_DO(BYTECODE_JAVA_CODE)
331 #undef BYTECODE_JAVA_CODE
332 };
333 
334 jchar Bytecodes::_flags[(1<<BitsPerByte)*2];
335 
336 #ifdef ASSERT
337 bool Bytecodes::check_method(const Method* method, address bcp) {
338   return method->contains(bcp);
339 }
340 #endif
341 
342 bool Bytecodes::check_must_rewrite(Bytecodes::Code code) {
343   assert(can_rewrite(code), "post-check only");
344 
345   // Some codes are conditionally rewriting.  Look closely at them.
346   switch (code) {
347   case Bytecodes::_aload_0:
348     // Even if RewriteFrequentPairs is turned on,
349     // the _aload_0 code might delay its rewrite until
350     // a following _getfield rewrites itself.
351     return false;
352 
353   case Bytecodes::_lookupswitch:
354     return false;  // the rewrite is not done by the interpreter
355 
356   case Bytecodes::_new:
357     // (Could actually look at the class here, but the profit would be small.)
358     return false;  // the rewrite is not always done
359 
360   default:
361     // No other special cases.
362     return true;
363   }
364 }
365 
366 Bytecodes::Code Bytecodes::code_at(Method* method, int bci) {
367   return code_at(method, method->bcp_from(bci));
368 }
369 
370 Bytecodes::Code Bytecodes::non_breakpoint_code_at(const Method* method, address bcp) {
371   assert(method != nullptr, "must have the method for breakpoint conversion");
372   assert(method->contains(bcp), "must be valid bcp in method");
373   return method->orig_bytecode_at(method->bci_from(bcp));
374 }
375 
376 int Bytecodes::special_length_at(Bytecodes::Code code, address bcp, address end) {
377   switch (code) {
378   case _wide:
379     if (end != nullptr && bcp + 1 >= end) {
380       return -1; // don't read past end of code buffer
381     }
382     return wide_length_for(cast(*(bcp + 1)));
383   case _tableswitch:
384     { address aligned_bcp = align_up(bcp + 1, jintSize);
385       if (end != nullptr && aligned_bcp + 3*jintSize >= end) {
386         return -1; // don't read past end of code buffer
387       }
388       // Promote calculation to signed 64 bits to do range checks, used by the verifier.
389       int64_t lo = (int)Bytes::get_Java_u4(aligned_bcp + 1*jintSize);
390       int64_t hi = (int)Bytes::get_Java_u4(aligned_bcp + 2*jintSize);
391       int64_t len = (aligned_bcp - bcp) + (3 + hi - lo + 1)*jintSize;
392       // Only return len if it can be represented as a positive int and lo <= hi.
393       // The caller checks for bytecode stream overflow.
394       if (lo <= hi && len == (int)len) {
395         assert(len > 0, "must be");
396         return (int)len;
397       } else {
398         return -1;
399       }
400     }
401 
402   case _lookupswitch:      // fall through
403   case _fast_binaryswitch: // fall through
404   case _fast_linearswitch:
405     { address aligned_bcp = align_up(bcp + 1, jintSize);
406       if (end != nullptr && aligned_bcp + 2*jintSize >= end) {
407         return -1; // don't read past end of code buffer
408       }
409       // Promote calculation to 64 bits to do range checks, used by the verifier.
410       int64_t npairs = (int)Bytes::get_Java_u4(aligned_bcp + jintSize);
411       int64_t len = (aligned_bcp - bcp) + (2 + 2*npairs)*jintSize;
412       // Only return len if it can be represented as a positive int and npairs >= 0.
413       if (npairs >= 0 && len == (int)len) {
414         assert(len > 0, "must be");
415         return (int)len;
416       } else {
417         return -1;
418       }
419     }
420   default:
421     // Note: Length functions must return <=0 for invalid bytecodes.
422     return 0;
423   }
424 }
425 
426 // At a breakpoint instruction, this returns the breakpoint's length,
427 // otherwise, it's the same as special_length_at().  This is used by
428 // the RawByteCodeStream, which wants to see the actual bytecode
429 // values (including breakpoint).  RawByteCodeStream is used by the
430 // verifier when reading in bytecode to verify.  Other mechanisms that
431 // run at runtime (such as generateOopMaps) need to iterate over the code
432 // and don't expect to see breakpoints: they want to see the instruction
433 // which was replaced so that they can get the correct length and find
434 // the next bytecode.
435 //
436 // 'end' indicates the end of the code buffer, which we should not try to read
437 // past.
438 int Bytecodes::raw_special_length_at(address bcp, address end) {
439   Code code = code_or_bp_at(bcp);
440   if (code == _breakpoint) {
441     return 1;
442   } else {
443     return special_length_at(code, bcp, end);
444   }
445 }
446 
447 void Bytecodes::def_flags(Code code, const char* format, const char* wide_format, bool can_trap, Code java_code) {
448   assert(wide_format == nullptr || format != nullptr, "short form must exist if there's a wide form");
449 #ifdef ASSERT
450   int len  = (format      != nullptr ? (int) strlen(format)      : 0);
451   int wlen = (wide_format != nullptr ? (int) strlen(wide_format) : 0);
452 #endif
453   jchar bc_flags = 0;
454   if (can_trap)           bc_flags |= _bc_can_trap;
455   if (java_code != code)  bc_flags |= _bc_can_rewrite;
456   _flags[(u1)code+0*(1<<BitsPerByte)] = compute_flags(format,      bc_flags);
457   _flags[(u1)code+1*(1<<BitsPerByte)] = compute_flags(wide_format, bc_flags);
458   assert(is_defined(code)      == (format != nullptr),      "");
459   assert(wide_is_defined(code) == (wide_format != nullptr), "");
460   assert(length_for(code)      == len,  "");
461   assert(wide_length_for(code) == wlen, "");
462 }
463 
464 
465 // Format strings interpretation:
466 //
467 // b: bytecode
468 // c: signed constant, Java byte-ordering
469 // i: unsigned local index, Java byte-ordering (I = native byte ordering)
470 // j: unsigned CP cache index, Java byte-ordering (J = native byte ordering)
471 // k: unsigned CP index, Java byte-ordering
472 // o: branch offset, Java byte-ordering
473 // _: unused/ignored
474 // w: wide bytecode
475 //
476 // Note: The format strings are used for 2 purposes:
477 //       1. to specify the length of the bytecode
478 //          (= number of characters in format string)
479 //       2. to derive bytecode format flags (_fmt_has_k, etc.)
480 //
481 // Note: For bytecodes with variable length, the format string is the empty string.
482 
483 jchar Bytecodes::compute_flags(const char* format, jchar more_flags) {
484   if (format == nullptr)  return 0;  // not even more_flags
485   jchar flags = more_flags;
486   const char* fp = format;
487   switch (*fp) {
488   case '\0':
489     flags |= _fmt_not_simple; // but variable
490     break;
491   case 'b':
492     flags |= _fmt_not_variable;  // but simple
493     ++fp;  // skip 'b'
494     break;
495   case 'w':
496     flags |= _fmt_not_variable | _fmt_not_simple;
497     ++fp;  // skip 'w'
498     guarantee(*fp == 'b', "wide format must start with 'wb'");
499     ++fp;  // skip 'b'
500     break;
501   }
502 
503   int has_nbo = 0, has_jbo = 0, has_size = 0;
504   for (;;) {
505     jchar this_flag = 0;
506     char fc = *fp++;
507     switch (fc) {
508     case '\0':  // end of string
509       return flags;
510 
511     case '_': continue;         // ignore these
512 
513     case 'j': this_flag = _fmt_has_j; has_jbo = 1; break;
514     case 'k': this_flag = _fmt_has_k; has_jbo = 1; break;
515     case 'i': this_flag = _fmt_has_i; has_jbo = 1; break;
516     case 'c': this_flag = _fmt_has_c; has_jbo = 1; break;
517     case 'o': this_flag = _fmt_has_o; has_jbo = 1; break;
518 
519     // uppercase versions mark native byte order (from Rewriter)
520     // actually, only the 'J' case happens currently
521     case 'J': this_flag = _fmt_has_j; has_nbo = 1; break;
522     case 'K': this_flag = _fmt_has_k; has_nbo = 1; break;
523     case 'I': this_flag = _fmt_has_i; has_nbo = 1; break;
524     case 'C': this_flag = _fmt_has_c; has_nbo = 1; break;
525     case 'O': this_flag = _fmt_has_o; has_nbo = 1; break;
526     default:  guarantee(false, "bad char in format");
527     }
528 
529     flags |= this_flag;
530 
531     guarantee(!(has_jbo && has_nbo), "mixed byte orders in format");
532     if (has_nbo)
533       flags |= _fmt_has_nbo;
534 
535     int this_size = 1;
536     if (*fp == fc) {
537       // advance beyond run of the same characters
538       this_size = 2;
539       while (*++fp == fc)  this_size++;
540       switch (this_size) {
541       case 2: flags |= _fmt_has_u2; break;
542       case 4: flags |= _fmt_has_u4; break;
543       default: guarantee(false, "bad rep count in format");
544       }
545     }
546     guarantee(has_size == 0 ||                     // no field yet
547               this_size == has_size ||             // same size
548               (this_size < has_size && *fp == '\0'), // last field can be short
549               "mixed field sizes in format");
550     has_size = this_size;
551   }
552 }
553 
554 void Bytecodes::initialize() {
555   if (_is_initialized) return;
556 
557   // initialize bytecode tables - didn't use static array initializers
558   // (such as {}) so we can do additional consistency checks and init-
559   // code is independent of actual bytecode numbering.
560   //
561   // Note 1: The result type is T_ILLEGAL for bytecodes where the top of stack
562   //         type after execution is not only determined by the bytecode itself.
563 
564 #define BYTECODE(code, name, format, wide_format, result_type, depth, can_trap, java_code)  \
565   assert(strcmp(_name[code], name) == 0, "bytecode name mismatch");                         \
566   assert(_result_type[code] == result_type, "bytecode result_type mismatch");               \
567   assert(_depth[code] == depth, "bytecode depth mismatch");                                 \
568   assert(_lengths[code] == ((STRING_SIZE(wide_format) << 4) | (STRING_SIZE(format) & 0xF)), \
569          "bytecode lengths mismatch");                                                      \
570   assert(_java_code[code] == java_code, "bytecode java_code mismatch");                     \
571   def_flags(code, format, wide_format, can_trap, java_code);
572   BYTECODES_DO(BYTECODE)
573 #undef BYTECODE
574 
575   // compare can_trap information for each bytecode with the
576   // can_trap information for the corresponding base bytecode
577   // (if a rewritten bytecode can trap, so must the base bytecode)
578   #ifdef ASSERT
579     { for (int i = 0; i < number_of_codes; i++) {
580         if (is_defined(i)) {
581           Code code = cast(i);
582           Code java = java_code(code);
583           if (can_trap(code) && !can_trap(java))
584             fatal("%s can trap => %s can trap, too", name(code), name(java));
585         }
586       }
587     }
588   #endif
589 
590   // initialization successful
591   _is_initialized = true;
592 }
593 
594 
595 void bytecodes_init() {
596   Bytecodes::initialize();
597 }
598 
599 // Restore optimization
600 #ifdef _M_AMD64
601 #pragma optimize ("", on)
602 #endif