1 /*
  2  * Copyright (c) 1997, 2023, 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 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;
393       // return -1 otherwise
394       return (len > 0 && len == (int)len) ? (int)len : -1;
395     }
396 
397   case _lookupswitch:      // fall through
398   case _fast_binaryswitch: // fall through
399   case _fast_linearswitch:
400     { address aligned_bcp = align_up(bcp + 1, jintSize);
401       if (end != nullptr && aligned_bcp + 2*jintSize >= end) {
402         return -1; // don't read past end of code buffer
403       }
404       // Promote calculation to 64 bits to do range checks, used by the verifier.
405       int64_t npairs = (int)Bytes::get_Java_u4(aligned_bcp + jintSize);
406       int64_t len = (aligned_bcp - bcp) + (2 + 2*npairs)*jintSize;
407       // only return len if it can be represented as a positive int;
408       // return -1 otherwise
409       return (len > 0 && len == (int)len) ? (int)len : -1;
410     }
411   default:
412     // Note: Length functions must return <=0 for invalid bytecodes.
413     return 0;
414   }
415 }
416 
417 // At a breakpoint instruction, this returns the breakpoint's length,
418 // otherwise, it's the same as special_length_at().  This is used by
419 // the RawByteCodeStream, which wants to see the actual bytecode
420 // values (including breakpoint).  RawByteCodeStream is used by the
421 // verifier when reading in bytecode to verify.  Other mechanisms that
422 // run at runtime (such as generateOopMaps) need to iterate over the code
423 // and don't expect to see breakpoints: they want to see the instruction
424 // which was replaced so that they can get the correct length and find
425 // the next bytecode.
426 //
427 // 'end' indicates the end of the code buffer, which we should not try to read
428 // past.
429 int Bytecodes::raw_special_length_at(address bcp, address end) {
430   Code code = code_or_bp_at(bcp);
431   if (code == _breakpoint) {
432     return 1;
433   } else {
434     return special_length_at(code, bcp, end);
435   }
436 }
437 
438 void Bytecodes::def_flags(Code code, const char* format, const char* wide_format, bool can_trap, Code java_code) {
439   assert(wide_format == nullptr || format != nullptr, "short form must exist if there's a wide form");
440 #ifdef ASSERT
441   int len  = (format      != nullptr ? (int) strlen(format)      : 0);
442   int wlen = (wide_format != nullptr ? (int) strlen(wide_format) : 0);
443 #endif
444   jchar bc_flags = 0;
445   if (can_trap)           bc_flags |= _bc_can_trap;
446   if (java_code != code)  bc_flags |= _bc_can_rewrite;
447   _flags[(u1)code+0*(1<<BitsPerByte)] = compute_flags(format,      bc_flags);
448   _flags[(u1)code+1*(1<<BitsPerByte)] = compute_flags(wide_format, bc_flags);
449   assert(is_defined(code)      == (format != nullptr),      "");
450   assert(wide_is_defined(code) == (wide_format != nullptr), "");
451   assert(length_for(code)      == len,  "");
452   assert(wide_length_for(code) == wlen, "");
453 }
454 
455 
456 // Format strings interpretation:
457 //
458 // b: bytecode
459 // c: signed constant, Java byte-ordering
460 // i: unsigned local index, Java byte-ordering (I = native byte ordering)
461 // j: unsigned CP cache index, Java byte-ordering (J = native byte ordering)
462 // k: unsigned CP index, Java byte-ordering
463 // o: branch offset, Java byte-ordering
464 // _: unused/ignored
465 // w: wide bytecode
466 //
467 // Note: The format strings are used for 2 purposes:
468 //       1. to specify the length of the bytecode
469 //          (= number of characters in format string)
470 //       2. to derive bytecode format flags (_fmt_has_k, etc.)
471 //
472 // Note: For bytecodes with variable length, the format string is the empty string.
473 
474 jchar Bytecodes::compute_flags(const char* format, jchar more_flags) {
475   if (format == nullptr)  return 0;  // not even more_flags
476   jchar flags = more_flags;
477   const char* fp = format;
478   switch (*fp) {
479   case '\0':
480     flags |= _fmt_not_simple; // but variable
481     break;
482   case 'b':
483     flags |= _fmt_not_variable;  // but simple
484     ++fp;  // skip 'b'
485     break;
486   case 'w':
487     flags |= _fmt_not_variable | _fmt_not_simple;
488     ++fp;  // skip 'w'
489     guarantee(*fp == 'b', "wide format must start with 'wb'");
490     ++fp;  // skip 'b'
491     break;
492   }
493 
494   int has_nbo = 0, has_jbo = 0, has_size = 0;
495   for (;;) {
496     jchar this_flag = 0;
497     char fc = *fp++;
498     switch (fc) {
499     case '\0':  // end of string
500       return flags;
501 
502     case '_': continue;         // ignore these
503 
504     case 'j': this_flag = _fmt_has_j; has_jbo = 1; break;
505     case 'k': this_flag = _fmt_has_k; has_jbo = 1; break;
506     case 'i': this_flag = _fmt_has_i; has_jbo = 1; break;
507     case 'c': this_flag = _fmt_has_c; has_jbo = 1; break;
508     case 'o': this_flag = _fmt_has_o; has_jbo = 1; break;
509 
510     // uppercase versions mark native byte order (from Rewriter)
511     // actually, only the 'J' case happens currently
512     case 'J': this_flag = _fmt_has_j; has_nbo = 1; break;
513     case 'K': this_flag = _fmt_has_k; has_nbo = 1; break;
514     case 'I': this_flag = _fmt_has_i; has_nbo = 1; break;
515     case 'C': this_flag = _fmt_has_c; has_nbo = 1; break;
516     case 'O': this_flag = _fmt_has_o; has_nbo = 1; break;
517     default:  guarantee(false, "bad char in format");
518     }
519 
520     flags |= this_flag;
521 
522     guarantee(!(has_jbo && has_nbo), "mixed byte orders in format");
523     if (has_nbo)
524       flags |= _fmt_has_nbo;
525 
526     int this_size = 1;
527     if (*fp == fc) {
528       // advance beyond run of the same characters
529       this_size = 2;
530       while (*++fp == fc)  this_size++;
531       switch (this_size) {
532       case 2: flags |= _fmt_has_u2; break;
533       case 4: flags |= _fmt_has_u4; break;
534       default: guarantee(false, "bad rep count in format");
535       }
536     }
537     guarantee(has_size == 0 ||                     // no field yet
538               this_size == has_size ||             // same size
539               this_size < has_size && *fp == '\0', // last field can be short
540               "mixed field sizes in format");
541     has_size = this_size;
542   }
543 }
544 
545 void Bytecodes::initialize() {
546   if (_is_initialized) return;
547 
548   // initialize bytecode tables - didn't use static array initializers
549   // (such as {}) so we can do additional consistency checks and init-
550   // code is independent of actual bytecode numbering.
551   //
552   // Note 1: The result type is T_ILLEGAL for bytecodes where the top of stack
553   //         type after execution is not only determined by the bytecode itself.
554 
555 #define BYTECODE(code, name, format, wide_format, result_type, depth, can_trap, java_code)  \
556   assert(strcmp(_name[code], name) == 0, "bytecode name mismatch");                         \
557   assert(_result_type[code] == result_type, "bytecode result_type mismatch");               \
558   assert(_depth[code] == depth, "bytecode depth mismatch");                                 \
559   assert(_lengths[code] == ((STRING_SIZE(wide_format) << 4) | (STRING_SIZE(format) & 0xF)), \
560          "bytecode lengths mismatch");                                                      \
561   assert(_java_code[code] == java_code, "bytecode java_code mismatch");                     \
562   def_flags(code, format, wide_format, can_trap, java_code);
563   BYTECODES_DO(BYTECODE)
564 #undef BYTECODE
565 
566   // compare can_trap information for each bytecode with the
567   // can_trap information for the corresponding base bytecode
568   // (if a rewritten bytecode can trap, so must the base bytecode)
569   #ifdef ASSERT
570     { for (int i = 0; i < number_of_codes; i++) {
571         if (is_defined(i)) {
572           Code code = cast(i);
573           Code java = java_code(code);
574           if (can_trap(code) && !can_trap(java))
575             fatal("%s can trap => %s can trap, too", name(code), name(java));
576         }
577       }
578     }
579   #endif
580 
581   // initialization successful
582   _is_initialized = true;
583 }
584 
585 
586 void bytecodes_init() {
587   Bytecodes::initialize();
588 }
589 
590 // Restore optimization
591 #ifdef _M_AMD64
592 #pragma optimize ("", on)
593 #endif