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