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  */
  24 
  25 #ifndef SHARE_VM_INTERPRETER_BYTECODE_HPP
  26 #define SHARE_VM_INTERPRETER_BYTECODE_HPP
  27 
  28 #include "interpreter/bytecodes.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "oops/method.hpp"
  31 #ifdef TARGET_ARCH_x86
  32 # include "bytes_x86.hpp"
  33 #endif
  34 #ifdef TARGET_ARCH_aarch64
  35 # include "bytes_aarch64.hpp"
  36 #endif
  37 #ifdef TARGET_ARCH_sparc
  38 # include "bytes_sparc.hpp"
  39 #endif
  40 #ifdef TARGET_ARCH_zero
  41 # include "bytes_zero.hpp"
  42 #endif
  43 #ifdef TARGET_ARCH_arm
  44 # include "bytes_arm.hpp"
  45 #endif
  46 #ifdef TARGET_ARCH_ppc
  47 # include "bytes_ppc.hpp"
  48 #endif
  49 
  50 class ciBytecodeStream;
  51 
  52 // The base class for different kinds of bytecode abstractions.
  53 // Provides the primitive operations to manipulate code relative
  54 // to the bcp.
  55 
  56 class Bytecode: public StackObj {
  57  protected:
  58   const address   _bcp;
  59   const Bytecodes::Code _code;
  60 
  61   // Address computation
  62   address addr_at            (int offset)        const     { return (address)_bcp + offset; }
  63   u_char byte_at(int offset) const               { return *addr_at(offset); }
  64   address aligned_addr_at    (int offset)        const     { return (address)round_to((intptr_t)addr_at(offset), jintSize); }
  65   int     aligned_offset     (int offset)        const     { return aligned_addr_at(offset) - addr_at(0); }
  66 
  67   // Word access:
  68   int     get_Java_u2_at     (int offset)        const     { return Bytes::get_Java_u2(addr_at(offset)); }
  69   int     get_Java_u4_at     (int offset)        const     { return Bytes::get_Java_u4(addr_at(offset)); }
  70   int     get_native_u2_at   (int offset)        const     { return Bytes::get_native_u2(addr_at(offset)); }
  71   int     get_native_u4_at   (int offset)        const     { return Bytes::get_native_u4(addr_at(offset)); }
  72 
  73  public:
  74   Bytecode(Method* method, address bcp): _bcp(bcp), _code(Bytecodes::code_at(method, addr_at(0))) {
  75     assert(method != NULL, "this form requires a valid Method*");
  76   }
  77   // Defined in ciStreams.hpp
  78   inline Bytecode(const ciBytecodeStream* stream, address bcp = NULL);
  79 
  80   // Attributes
  81   address bcp() const                            { return _bcp; }
  82   int instruction_size() const                   { return Bytecodes::length_for_code_at(_code, bcp()); }
  83 
  84   Bytecodes::Code code() const                   { return _code; }
  85   Bytecodes::Code java_code() const              { return Bytecodes::java_code(code()); }
  86   Bytecodes::Code invoke_code() const            { return (code() == Bytecodes::_invokehandle) ? code() : java_code(); }
  87 
  88   // Static functions for parsing bytecodes in place.
  89   int get_index_u1(Bytecodes::Code bc) const {
  90     assert_same_format_as(bc); assert_index_size(1, bc);
  91     return *(jubyte*)addr_at(1);
  92   }
  93   int get_index_u2(Bytecodes::Code bc, bool is_wide = false) const {
  94     assert_same_format_as(bc, is_wide); assert_index_size(2, bc, is_wide);
  95     address p = addr_at(is_wide ? 2 : 1);
  96     if (can_use_native_byte_order(bc, is_wide))
  97       return Bytes::get_native_u2(p);
  98     else  return Bytes::get_Java_u2(p);
  99   }
 100   int get_index_u1_cpcache(Bytecodes::Code bc) const {
 101     assert_same_format_as(bc); assert_index_size(1, bc);
 102     return *(jubyte*)addr_at(1) + ConstantPool::CPCACHE_INDEX_TAG;
 103   }
 104   int get_index_u2_cpcache(Bytecodes::Code bc) const {
 105     assert_same_format_as(bc); assert_index_size(2, bc); assert_native_index(bc);
 106     return Bytes::get_native_u2(addr_at(1)) + ConstantPool::CPCACHE_INDEX_TAG;
 107   }
 108   int get_index_u4(Bytecodes::Code bc) const {
 109     assert_same_format_as(bc); assert_index_size(4, bc);
 110     assert(can_use_native_byte_order(bc), "");
 111     return Bytes::get_native_u4(addr_at(1));
 112   }
 113   bool has_index_u4(Bytecodes::Code bc) const {
 114     return bc == Bytecodes::_invokedynamic;
 115   }
 116 
 117   int get_offset_s2(Bytecodes::Code bc) const {
 118     assert_same_format_as(bc); assert_offset_size(2, bc);
 119     return (jshort) Bytes::get_Java_u2(addr_at(1));
 120   }
 121   int get_offset_s4(Bytecodes::Code bc) const {
 122     assert_same_format_as(bc); assert_offset_size(4, bc);
 123     return (jint) Bytes::get_Java_u4(addr_at(1));
 124   }
 125 
 126   int get_constant_u1(int offset, Bytecodes::Code bc) const {
 127     assert_same_format_as(bc); assert_constant_size(1, offset, bc);
 128     return *(jbyte*)addr_at(offset);
 129   }
 130   int get_constant_u2(int offset, Bytecodes::Code bc, bool is_wide = false) const {
 131     assert_same_format_as(bc, is_wide); assert_constant_size(2, offset, bc, is_wide);
 132     return (jshort) Bytes::get_Java_u2(addr_at(offset));
 133   }
 134 
 135   // These are used locally and also from bytecode streams.
 136   void assert_same_format_as(Bytecodes::Code testbc, bool is_wide = false) const NOT_DEBUG_RETURN;
 137   static void assert_index_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
 138   static void assert_offset_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
 139   static void assert_constant_size(int required_size, int where, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
 140   static void assert_native_index(Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
 141   static bool can_use_native_byte_order(Bytecodes::Code bc, bool is_wide = false) {
 142     return (!Bytes::is_Java_byte_ordering_different() || Bytecodes::native_byte_order(bc /*, is_wide*/));
 143   }
 144 };
 145 
 146 
 147 // Abstractions for lookupswitch bytecode
 148 class LookupswitchPair VALUE_OBJ_CLASS_SPEC {
 149  private:
 150   const address _bcp;
 151 
 152   address addr_at            (int offset)        const     { return _bcp + offset; }
 153   int     get_Java_u4_at     (int offset)        const     { return Bytes::get_Java_u4(addr_at(offset)); }
 154 
 155  public:
 156   LookupswitchPair(address bcp): _bcp(bcp) {}
 157   int  match() const                             { return get_Java_u4_at(0 * jintSize); }
 158   int  offset() const                            { return get_Java_u4_at(1 * jintSize); }
 159 };
 160 
 161 
 162 class Bytecode_lookupswitch: public Bytecode {
 163  public:
 164   Bytecode_lookupswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
 165   // Defined in ciStreams.hpp
 166   inline Bytecode_lookupswitch(const ciBytecodeStream* stream);
 167   void verify() const PRODUCT_RETURN;
 168 
 169   // Attributes
 170   int  default_offset() const                    { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); }
 171   int  number_of_pairs() const                   { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); }
 172   LookupswitchPair pair_at(int i) const          {
 173     assert(0 <= i && i < number_of_pairs(), "pair index out of bounds");
 174     return LookupswitchPair(aligned_addr_at(1 + (1 + i)*2*jintSize));
 175   }
 176 };
 177 
 178 class Bytecode_tableswitch: public Bytecode {
 179  public:
 180   Bytecode_tableswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
 181   // Defined in ciStreams.hpp
 182   inline Bytecode_tableswitch(const ciBytecodeStream* stream);
 183   void verify() const PRODUCT_RETURN;
 184 
 185   // Attributes
 186   int  default_offset() const                    { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); }
 187   int  low_key() const                           { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); }
 188   int  high_key() const                          { return get_Java_u4_at(aligned_offset(1 + 2*jintSize)); }
 189   int  dest_offset_at(int i) const;
 190   int  length()                                  { return high_key()-low_key()+1; }
 191 };
 192 
 193 // Common code for decoding invokes and field references.
 194 
 195 class Bytecode_member_ref: public Bytecode {
 196  protected:
 197   const methodHandle _method;                          // method containing the bytecode
 198 
 199   Bytecode_member_ref(methodHandle method, int bci)  : Bytecode(method(), method()->bcp_from(bci)), _method(method) {}
 200 
 201   methodHandle method() const                    { return _method; }
 202   ConstantPool* constants() const              { return _method->constants(); }
 203   ConstantPoolCache* cpcache() const           { return _method->constants()->cache(); }
 204   ConstantPoolCacheEntry* cpcache_entry() const;
 205 
 206  public:
 207   int          index() const;                    // cache index (loaded from instruction)
 208   int          pool_index() const;               // constant pool index
 209   Symbol*      klass() const;                    // returns the klass of the method or field
 210   Symbol*      name() const;                     // returns the name of the method or field
 211   Symbol*      signature() const;                // returns the signature of the method or field
 212 
 213   BasicType    result_type() const;              // returns the result type of the getfield or invoke
 214 };
 215 
 216 // Abstraction for invoke_{virtual, static, interface, special}
 217 
 218 class Bytecode_invoke: public Bytecode_member_ref {
 219  protected:
 220   // Constructor that skips verification
 221   Bytecode_invoke(methodHandle method, int bci, bool unused)  : Bytecode_member_ref(method, bci) {}
 222 
 223  public:
 224   Bytecode_invoke(methodHandle method, int bci)  : Bytecode_member_ref(method, bci) { verify(); }
 225   void verify() const;
 226 
 227   // Attributes
 228   methodHandle static_target(TRAPS);             // "specified" method   (from constant pool)
 229   Handle       appendix(TRAPS);                  // if CPCE::has_appendix (from constant pool)
 230 
 231   // Testers
 232   bool is_invokeinterface() const                { return invoke_code() == Bytecodes::_invokeinterface; }
 233   bool is_invokevirtual() const                  { return invoke_code() == Bytecodes::_invokevirtual; }
 234   bool is_invokestatic() const                   { return invoke_code() == Bytecodes::_invokestatic; }
 235   bool is_invokespecial() const                  { return invoke_code() == Bytecodes::_invokespecial; }
 236   bool is_invokedynamic() const                  { return invoke_code() == Bytecodes::_invokedynamic; }
 237   bool is_invokehandle() const                   { return invoke_code() == Bytecodes::_invokehandle; }
 238 
 239   bool has_receiver() const                      { return !is_invokestatic() && !is_invokedynamic(); }
 240 
 241   bool is_valid() const                          { return is_invokeinterface() ||
 242                                                           is_invokevirtual()   ||
 243                                                           is_invokestatic()    ||
 244                                                           is_invokespecial()   ||
 245                                                           is_invokedynamic()   ||
 246                                                           is_invokehandle(); }
 247 
 248   bool has_appendix()                            { return cpcache_entry()->has_appendix(); }
 249 
 250  private:
 251   // Helper to skip verification.   Used is_valid() to check if the result is really an invoke
 252   inline friend Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci);
 253 };
 254 
 255 inline Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci) {
 256   return Bytecode_invoke(method, bci, false);
 257 }
 258 
 259 
 260 // Abstraction for all field accesses (put/get field/static)
 261 class Bytecode_field: public Bytecode_member_ref {
 262  public:
 263   Bytecode_field(methodHandle method, int bci)  : Bytecode_member_ref(method, bci) { verify(); }
 264 
 265   // Testers
 266   bool is_getfield() const                       { return java_code() == Bytecodes::_getfield; }
 267   bool is_putfield() const                       { return java_code() == Bytecodes::_putfield; }
 268   bool is_getstatic() const                      { return java_code() == Bytecodes::_getstatic; }
 269   bool is_putstatic() const                      { return java_code() == Bytecodes::_putstatic; }
 270 
 271   bool is_getter() const                         { return is_getfield()  || is_getstatic(); }
 272   bool is_static() const                         { return is_getstatic() || is_putstatic(); }
 273 
 274   bool is_valid() const                          { return is_getfield()   ||
 275                                                           is_putfield()   ||
 276                                                           is_getstatic()  ||
 277                                                           is_putstatic(); }
 278   void verify() const;
 279 };
 280 
 281 // Abstraction for checkcast
 282 class Bytecode_checkcast: public Bytecode {
 283  public:
 284   Bytecode_checkcast(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
 285   void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); }
 286 
 287   // Returns index
 288   long index() const   { return get_index_u2(Bytecodes::_checkcast); };
 289 };
 290 
 291 // Abstraction for instanceof
 292 class Bytecode_instanceof: public Bytecode {
 293  public:
 294   Bytecode_instanceof(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
 295   void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); }
 296 
 297   // Returns index
 298   long index() const   { return get_index_u2(Bytecodes::_instanceof); };
 299 };
 300 
 301 class Bytecode_new: public Bytecode {
 302  public:
 303   Bytecode_new(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
 304   void verify() const { assert(java_code() == Bytecodes::_new, "check new"); }
 305 
 306   // Returns index
 307   long index() const   { return get_index_u2(Bytecodes::_new); };
 308 };
 309 
 310 class Bytecode_multianewarray: public Bytecode {
 311  public:
 312   Bytecode_multianewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
 313   void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); }
 314 
 315   // Returns index
 316   long index() const   { return get_index_u2(Bytecodes::_multianewarray); };
 317 };
 318 
 319 class Bytecode_anewarray: public Bytecode {
 320  public:
 321   Bytecode_anewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
 322   void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); }
 323 
 324   // Returns index
 325   long index() const   { return get_index_u2(Bytecodes::_anewarray); };
 326 };
 327 
 328 // Abstraction for ldc, ldc_w and ldc2_w
 329 class Bytecode_loadconstant: public Bytecode {
 330  private:
 331   const methodHandle _method;
 332 
 333   int raw_index() const;
 334 
 335  public:
 336   Bytecode_loadconstant(methodHandle method, int bci): Bytecode(method(), method->bcp_from(bci)), _method(method) { verify(); }
 337 
 338   void verify() const {
 339     assert(_method.not_null(), "must supply method");
 340     Bytecodes::Code stdc = Bytecodes::java_code(code());
 341     assert(stdc == Bytecodes::_ldc ||
 342            stdc == Bytecodes::_ldc_w ||
 343            stdc == Bytecodes::_ldc2_w, "load constant");
 344   }
 345 
 346   // Only non-standard bytecodes (fast_aldc) have reference cache indexes.
 347   bool has_cache_index() const { return code() >= Bytecodes::number_of_java_codes; }
 348 
 349   int pool_index() const;               // index into constant pool
 350   int cache_index() const {             // index into reference cache (or -1 if none)
 351     return has_cache_index() ? raw_index() : -1;
 352   }
 353 
 354   BasicType result_type() const;        // returns the result type of the ldc
 355 
 356   oop resolve_constant(TRAPS) const;
 357 };
 358 
 359 #endif // SHARE_VM_INTERPRETER_BYTECODE_HPP