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