src/share/vm/interpreter/bytecode.hpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 /*
    26  * This file has been modified by Loongson Technology in 2015. These
    27  * modifications are Copyright (c) 2015 Loongson Technology, and are made
    28  * available on the same license terms set forth above.
    29  */
    31 #ifndef SHARE_VM_INTERPRETER_BYTECODE_HPP
    32 #define SHARE_VM_INTERPRETER_BYTECODE_HPP
    34 #include "interpreter/bytecodes.hpp"
    35 #include "memory/allocation.hpp"
    36 #include "oops/method.hpp"
    37 #ifdef TARGET_ARCH_x86
    38 # include "bytes_x86.hpp"
    39 #endif
    40 #ifdef TARGET_ARCH_mips
    41 # include "bytes_mips.hpp"
    42 #endif
    43 #ifdef TARGET_ARCH_sparc
    44 # include "bytes_sparc.hpp"
    45 #endif
    46 #ifdef TARGET_ARCH_zero
    47 # include "bytes_zero.hpp"
    48 #endif
    49 #ifdef TARGET_ARCH_arm
    50 # include "bytes_arm.hpp"
    51 #endif
    52 #ifdef TARGET_ARCH_ppc
    53 # include "bytes_ppc.hpp"
    54 #endif
    56 class ciBytecodeStream;
    58 // The base class for different kinds of bytecode abstractions.
    59 // Provides the primitive operations to manipulate code relative
    60 // to the bcp.
    62 class Bytecode: public StackObj {
    63  protected:
    64   const address   _bcp;
    65   const Bytecodes::Code _code;
    67   // Address computation
    68   address addr_at            (int offset)        const     { return (address)_bcp + offset; }
    69   u_char byte_at(int offset) const               { return *addr_at(offset); }
    70   address aligned_addr_at    (int offset)        const     { return (address)round_to((intptr_t)addr_at(offset), jintSize); }
    71   int     aligned_offset     (int offset)        const     { return aligned_addr_at(offset) - addr_at(0); }
    73   // Word access:
    74   int     get_Java_u2_at     (int offset)        const     { return Bytes::get_Java_u2(addr_at(offset)); }
    75   int     get_Java_u4_at     (int offset)        const     { return Bytes::get_Java_u4(addr_at(offset)); }
    76   int     get_native_u2_at   (int offset)        const     { return Bytes::get_native_u2(addr_at(offset)); }
    77   int     get_native_u4_at   (int offset)        const     { return Bytes::get_native_u4(addr_at(offset)); }
    79  public:
    80   Bytecode(Method* method, address bcp): _bcp(bcp), _code(Bytecodes::code_at(method, addr_at(0))) {
    81     assert(method != NULL, "this form requires a valid Method*");
    82   }
    83   // Defined in ciStreams.hpp
    84   inline Bytecode(const ciBytecodeStream* stream, address bcp = NULL);
    86   // Attributes
    87   address bcp() const                            { return _bcp; }
    88   int instruction_size() const                   { return Bytecodes::length_for_code_at(_code, bcp()); }
    90   Bytecodes::Code code() const                   { return _code; }
    91   Bytecodes::Code java_code() const              { return Bytecodes::java_code(code()); }
    92   Bytecodes::Code invoke_code() const            { return (code() == Bytecodes::_invokehandle) ? code() : java_code(); }
    94   // Static functions for parsing bytecodes in place.
    95   int get_index_u1(Bytecodes::Code bc) const {
    96     assert_same_format_as(bc); assert_index_size(1, bc);
    97     return *(jubyte*)addr_at(1);
    98   }
    99   int get_index_u2(Bytecodes::Code bc, bool is_wide = false) const {
   100     assert_same_format_as(bc, is_wide); assert_index_size(2, bc, is_wide);
   101     address p = addr_at(is_wide ? 2 : 1);
   102     if (can_use_native_byte_order(bc, is_wide))
   103       return Bytes::get_native_u2(p);
   104     else  return Bytes::get_Java_u2(p);
   105   }
   106   int get_index_u1_cpcache(Bytecodes::Code bc) const {
   107     assert_same_format_as(bc); assert_index_size(1, bc);
   108     return *(jubyte*)addr_at(1) + ConstantPool::CPCACHE_INDEX_TAG;
   109   }
   110   int get_index_u2_cpcache(Bytecodes::Code bc) const {
   111     assert_same_format_as(bc); assert_index_size(2, bc); assert_native_index(bc);
   112     return Bytes::get_native_u2(addr_at(1)) + ConstantPool::CPCACHE_INDEX_TAG;
   113   }
   114   int get_index_u4(Bytecodes::Code bc) const {
   115     assert_same_format_as(bc); assert_index_size(4, bc);
   116     assert(can_use_native_byte_order(bc), "");
   117     return Bytes::get_native_u4(addr_at(1));
   118   }
   119   bool has_index_u4(Bytecodes::Code bc) const {
   120     return bc == Bytecodes::_invokedynamic;
   121   }
   123   int get_offset_s2(Bytecodes::Code bc) const {
   124     assert_same_format_as(bc); assert_offset_size(2, bc);
   125     return (jshort) Bytes::get_Java_u2(addr_at(1));
   126   }
   127   int get_offset_s4(Bytecodes::Code bc) const {
   128     assert_same_format_as(bc); assert_offset_size(4, bc);
   129     return (jint) Bytes::get_Java_u4(addr_at(1));
   130   }
   132   int get_constant_u1(int offset, Bytecodes::Code bc) const {
   133     assert_same_format_as(bc); assert_constant_size(1, offset, bc);
   134     return *(jbyte*)addr_at(offset);
   135   }
   136   int get_constant_u2(int offset, Bytecodes::Code bc, bool is_wide = false) const {
   137     assert_same_format_as(bc, is_wide); assert_constant_size(2, offset, bc, is_wide);
   138     return (jshort) Bytes::get_Java_u2(addr_at(offset));
   139   }
   141   // These are used locally and also from bytecode streams.
   142   void assert_same_format_as(Bytecodes::Code testbc, bool is_wide = false) const NOT_DEBUG_RETURN;
   143   static void assert_index_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
   144   static void assert_offset_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
   145   static void assert_constant_size(int required_size, int where, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
   146   static void assert_native_index(Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
   147   static bool can_use_native_byte_order(Bytecodes::Code bc, bool is_wide = false) {
   148     return (!Bytes::is_Java_byte_ordering_different() || Bytecodes::native_byte_order(bc /*, is_wide*/));
   149   }
   150 };
   153 // Abstractions for lookupswitch bytecode
   154 class LookupswitchPair VALUE_OBJ_CLASS_SPEC {
   155  private:
   156   const address _bcp;
   158   address addr_at            (int offset)        const     { return _bcp + offset; }
   159   int     get_Java_u4_at     (int offset)        const     { return Bytes::get_Java_u4(addr_at(offset)); }
   161  public:
   162   LookupswitchPair(address bcp): _bcp(bcp) {}
   163   int  match() const                             { return get_Java_u4_at(0 * jintSize); }
   164   int  offset() const                            { return get_Java_u4_at(1 * jintSize); }
   165 };
   168 class Bytecode_lookupswitch: public Bytecode {
   169  public:
   170   Bytecode_lookupswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
   171   // Defined in ciStreams.hpp
   172   inline Bytecode_lookupswitch(const ciBytecodeStream* stream);
   173   void verify() const PRODUCT_RETURN;
   175   // Attributes
   176   int  default_offset() const                    { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); }
   177   int  number_of_pairs() const                   { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); }
   178   LookupswitchPair pair_at(int i) const          {
   179     assert(0 <= i && i < number_of_pairs(), "pair index out of bounds");
   180     return LookupswitchPair(aligned_addr_at(1 + (1 + i)*2*jintSize));
   181   }
   182 };
   184 class Bytecode_tableswitch: public Bytecode {
   185  public:
   186   Bytecode_tableswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
   187   // Defined in ciStreams.hpp
   188   inline Bytecode_tableswitch(const ciBytecodeStream* stream);
   189   void verify() const PRODUCT_RETURN;
   191   // Attributes
   192   int  default_offset() const                    { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); }
   193   int  low_key() const                           { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); }
   194   int  high_key() const                          { return get_Java_u4_at(aligned_offset(1 + 2*jintSize)); }
   195   int  dest_offset_at(int i) const;
   196   int  length()                                  { return high_key()-low_key()+1; }
   197 };
   199 // Common code for decoding invokes and field references.
   201 class Bytecode_member_ref: public Bytecode {
   202  protected:
   203   const methodHandle _method;                          // method containing the bytecode
   205   Bytecode_member_ref(methodHandle method, int bci)  : Bytecode(method(), method()->bcp_from(bci)), _method(method) {}
   207   methodHandle method() const                    { return _method; }
   208   ConstantPool* constants() const              { return _method->constants(); }
   209   ConstantPoolCache* cpcache() const           { return _method->constants()->cache(); }
   210   ConstantPoolCacheEntry* cpcache_entry() const;
   212  public:
   213   int          index() const;                    // cache index (loaded from instruction)
   214   int          pool_index() const;               // constant pool index
   215   Symbol*      klass() const;                    // returns the klass of the method or field
   216   Symbol*      name() const;                     // returns the name of the method or field
   217   Symbol*      signature() const;                // returns the signature of the method or field
   219   BasicType    result_type() const;              // returns the result type of the getfield or invoke
   220 };
   222 // Abstraction for invoke_{virtual, static, interface, special}
   224 class Bytecode_invoke: public Bytecode_member_ref {
   225  protected:
   226   // Constructor that skips verification
   227   Bytecode_invoke(methodHandle method, int bci, bool unused)  : Bytecode_member_ref(method, bci) {}
   229  public:
   230   Bytecode_invoke(methodHandle method, int bci)  : Bytecode_member_ref(method, bci) { verify(); }
   231   void verify() const;
   233   // Attributes
   234   methodHandle static_target(TRAPS);             // "specified" method   (from constant pool)
   235   Handle       appendix(TRAPS);                  // if CPCE::has_appendix (from constant pool)
   237   // Testers
   238   bool is_invokeinterface() const                { return invoke_code() == Bytecodes::_invokeinterface; }
   239   bool is_invokevirtual() const                  { return invoke_code() == Bytecodes::_invokevirtual; }
   240   bool is_invokestatic() const                   { return invoke_code() == Bytecodes::_invokestatic; }
   241   bool is_invokespecial() const                  { return invoke_code() == Bytecodes::_invokespecial; }
   242   bool is_invokedynamic() const                  { return invoke_code() == Bytecodes::_invokedynamic; }
   243   bool is_invokehandle() const                   { return invoke_code() == Bytecodes::_invokehandle; }
   245   bool has_receiver() const                      { return !is_invokestatic() && !is_invokedynamic(); }
   247   bool is_valid() const                          { return is_invokeinterface() ||
   248                                                           is_invokevirtual()   ||
   249                                                           is_invokestatic()    ||
   250                                                           is_invokespecial()   ||
   251                                                           is_invokedynamic()   ||
   252                                                           is_invokehandle(); }
   254   bool has_appendix()                            { return cpcache_entry()->has_appendix(); }
   256  private:
   257   // Helper to skip verification.   Used is_valid() to check if the result is really an invoke
   258   inline friend Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci);
   259 };
   261 inline Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci) {
   262   return Bytecode_invoke(method, bci, false);
   263 }
   266 // Abstraction for all field accesses (put/get field/static)
   267 class Bytecode_field: public Bytecode_member_ref {
   268  public:
   269   Bytecode_field(methodHandle method, int bci)  : Bytecode_member_ref(method, bci) { verify(); }
   271   // Testers
   272   bool is_getfield() const                       { return java_code() == Bytecodes::_getfield; }
   273   bool is_putfield() const                       { return java_code() == Bytecodes::_putfield; }
   274   bool is_getstatic() const                      { return java_code() == Bytecodes::_getstatic; }
   275   bool is_putstatic() const                      { return java_code() == Bytecodes::_putstatic; }
   277   bool is_getter() const                         { return is_getfield()  || is_getstatic(); }
   278   bool is_static() const                         { return is_getstatic() || is_putstatic(); }
   280   bool is_valid() const                          { return is_getfield()   ||
   281                                                           is_putfield()   ||
   282                                                           is_getstatic()  ||
   283                                                           is_putstatic(); }
   284   void verify() const;
   285 };
   287 // Abstraction for checkcast
   288 class Bytecode_checkcast: public Bytecode {
   289  public:
   290   Bytecode_checkcast(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
   291   void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); }
   293   // Returns index
   294   long index() const   { return get_index_u2(Bytecodes::_checkcast); };
   295 };
   297 // Abstraction for instanceof
   298 class Bytecode_instanceof: public Bytecode {
   299  public:
   300   Bytecode_instanceof(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
   301   void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); }
   303   // Returns index
   304   long index() const   { return get_index_u2(Bytecodes::_instanceof); };
   305 };
   307 class Bytecode_new: public Bytecode {
   308  public:
   309   Bytecode_new(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
   310   void verify() const { assert(java_code() == Bytecodes::_new, "check new"); }
   312   // Returns index
   313   long index() const   { return get_index_u2(Bytecodes::_new); };
   314 };
   316 class Bytecode_multianewarray: public Bytecode {
   317  public:
   318   Bytecode_multianewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
   319   void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); }
   321   // Returns index
   322   long index() const   { return get_index_u2(Bytecodes::_multianewarray); };
   323 };
   325 class Bytecode_anewarray: public Bytecode {
   326  public:
   327   Bytecode_anewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
   328   void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); }
   330   // Returns index
   331   long index() const   { return get_index_u2(Bytecodes::_anewarray); };
   332 };
   334 // Abstraction for ldc, ldc_w and ldc2_w
   335 class Bytecode_loadconstant: public Bytecode {
   336  private:
   337   const methodHandle _method;
   339   int raw_index() const;
   341  public:
   342   Bytecode_loadconstant(methodHandle method, int bci): Bytecode(method(), method->bcp_from(bci)), _method(method) { verify(); }
   344   void verify() const {
   345     assert(_method.not_null(), "must supply method");
   346     Bytecodes::Code stdc = Bytecodes::java_code(code());
   347     assert(stdc == Bytecodes::_ldc ||
   348            stdc == Bytecodes::_ldc_w ||
   349            stdc == Bytecodes::_ldc2_w, "load constant");
   350   }
   352   // Only non-standard bytecodes (fast_aldc) have reference cache indexes.
   353   bool has_cache_index() const { return code() >= Bytecodes::number_of_java_codes; }
   355   int pool_index() const;               // index into constant pool
   356   int cache_index() const {             // index into reference cache (or -1 if none)
   357     return has_cache_index() ? raw_index() : -1;
   358   }
   360   BasicType result_type() const;        // returns the result type of the ldc
   362   oop resolve_constant(TRAPS) const;
   363 };
   365 #endif // SHARE_VM_INTERPRETER_BYTECODE_HPP

mercurial