Path: blob/master/src/hotspot/share/interpreter/bytecode.hpp
40949 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_INTERPRETER_BYTECODE_HPP25#define SHARE_INTERPRETER_BYTECODE_HPP2627#include "interpreter/bytecodes.hpp"28#include "memory/allocation.hpp"29#include "oops/method.hpp"30#include "utilities/align.hpp"31#include "utilities/bytes.hpp"3233class ciBytecodeStream;3435// The base class for different kinds of bytecode abstractions.36// Provides the primitive operations to manipulate code relative37// to the bcp.3839class Bytecode: public StackObj {40protected:41const address _bcp;42const Bytecodes::Code _code;4344// Address computation45address addr_at (int offset) const { return (address)_bcp + offset; }46u_char byte_at(int offset) const { return *addr_at(offset); }47address aligned_addr_at (int offset) const { return align_up(addr_at(offset), jintSize); }4849// Word access:50int get_Java_u2_at (int offset) const { return Bytes::get_Java_u2(addr_at(offset)); }51int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); }52int get_aligned_Java_u4_at(int offset) const { return Bytes::get_Java_u4(aligned_addr_at(offset)); }53int get_native_u2_at (int offset) const { return Bytes::get_native_u2(addr_at(offset)); }54int get_native_u4_at (int offset) const { return Bytes::get_native_u4(addr_at(offset)); }5556public:57Bytecode(Method* method, address bcp): _bcp(bcp), _code(Bytecodes::code_at(method, addr_at(0))) {58assert(method != NULL, "this form requires a valid Method*");59}60// Defined in ciStreams.hpp61inline Bytecode(const ciBytecodeStream* stream, address bcp = NULL);6263// Attributes64address bcp() const { return _bcp; }65int instruction_size() const { return Bytecodes::length_for_code_at(_code, bcp()); }6667Bytecodes::Code code() const { return _code; }68Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); }69Bytecodes::Code invoke_code() const { return (code() == Bytecodes::_invokehandle) ? code() : java_code(); }7071// Static functions for parsing bytecodes in place.72int get_index_u1(Bytecodes::Code bc) const {73assert_same_format_as(bc); assert_index_size(1, bc);74return *(jubyte*)addr_at(1);75}76int get_index_u2(Bytecodes::Code bc, bool is_wide = false) const {77assert_same_format_as(bc, is_wide); assert_index_size(2, bc, is_wide);78address p = addr_at(is_wide ? 2 : 1);79if (can_use_native_byte_order(bc, is_wide))80return Bytes::get_native_u2(p);81else return Bytes::get_Java_u2(p);82}83int get_index_u1_cpcache(Bytecodes::Code bc) const {84assert_same_format_as(bc); assert_index_size(1, bc);85return *(jubyte*)addr_at(1) + ConstantPool::CPCACHE_INDEX_TAG;86}87int get_index_u2_cpcache(Bytecodes::Code bc) const {88assert_same_format_as(bc); assert_index_size(2, bc); assert_native_index(bc);89return Bytes::get_native_u2(addr_at(1)) + ConstantPool::CPCACHE_INDEX_TAG;90}91int get_index_u4(Bytecodes::Code bc) const {92assert_same_format_as(bc); assert_index_size(4, bc);93assert(can_use_native_byte_order(bc), "");94return Bytes::get_native_u4(addr_at(1));95}96bool has_index_u4(Bytecodes::Code bc) const {97return bc == Bytecodes::_invokedynamic;98}99100int get_offset_s2(Bytecodes::Code bc) const {101assert_same_format_as(bc); assert_offset_size(2, bc);102return (jshort) Bytes::get_Java_u2(addr_at(1));103}104int get_offset_s4(Bytecodes::Code bc) const {105assert_same_format_as(bc); assert_offset_size(4, bc);106return (jint) Bytes::get_Java_u4(addr_at(1));107}108109int get_constant_u1(int offset, Bytecodes::Code bc) const {110assert_same_format_as(bc); assert_constant_size(1, offset, bc);111return *(jbyte*)addr_at(offset);112}113int get_constant_u2(int offset, Bytecodes::Code bc, bool is_wide = false) const {114assert_same_format_as(bc, is_wide); assert_constant_size(2, offset, bc, is_wide);115return (jshort) Bytes::get_Java_u2(addr_at(offset));116}117118// These are used locally and also from bytecode streams.119void assert_same_format_as(Bytecodes::Code testbc, bool is_wide = false) const NOT_DEBUG_RETURN;120static void assert_index_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;121static void assert_offset_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;122static void assert_constant_size(int required_size, int where, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;123static void assert_native_index(Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;124static bool can_use_native_byte_order(Bytecodes::Code bc, bool is_wide = false) {125return (!Endian::is_Java_byte_ordering_different() || Bytecodes::native_byte_order(bc /*, is_wide*/));126}127};128129130// Abstractions for lookupswitch bytecode131class LookupswitchPair {132private:133const address _bcp;134135address addr_at (int offset) const { return _bcp + offset; }136int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); }137138public:139LookupswitchPair(address bcp): _bcp(bcp) {}140int match() const { return get_Java_u4_at(0 * jintSize); }141int offset() const { return get_Java_u4_at(1 * jintSize); }142};143144145class Bytecode_lookupswitch: public Bytecode {146public:147Bytecode_lookupswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }148// Defined in ciStreams.hpp149inline Bytecode_lookupswitch(const ciBytecodeStream* stream);150void verify() const PRODUCT_RETURN;151152// Attributes153int default_offset() const { return get_aligned_Java_u4_at(1 + 0*jintSize); }154int number_of_pairs() const { return get_aligned_Java_u4_at(1 + 1*jintSize); }155LookupswitchPair pair_at(int i) const {156assert(0 <= i && i < number_of_pairs(), "pair index out of bounds");157return LookupswitchPair(aligned_addr_at(1 + (1 + i)*2*jintSize));158}159};160161class Bytecode_tableswitch: public Bytecode {162public:163Bytecode_tableswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }164// Defined in ciStreams.hpp165inline Bytecode_tableswitch(const ciBytecodeStream* stream);166void verify() const PRODUCT_RETURN;167168// Attributes169int default_offset() const { return get_aligned_Java_u4_at(1 + 0*jintSize); }170int low_key() const { return get_aligned_Java_u4_at(1 + 1*jintSize); }171int high_key() const { return get_aligned_Java_u4_at(1 + 2*jintSize); }172int dest_offset_at(int i) const;173int length() { return high_key()-low_key()+1; }174};175176// Common code for decoding invokes and field references.177178class Bytecode_member_ref: public Bytecode {179protected:180const Method* _method; // method containing the bytecode181182Bytecode_member_ref(const methodHandle& method, int bci) : Bytecode(method(), method()->bcp_from(bci)), _method(method()) {}183184const Method* method() const { return _method; }185ConstantPool* constants() const { return _method->constants(); }186ConstantPoolCache* cpcache() const { return _method->constants()->cache(); }187ConstantPoolCacheEntry* cpcache_entry() const;188189public:190int index() const; // cache index (loaded from instruction)191int pool_index() const; // constant pool index192Symbol* klass() const; // returns the klass of the method or field193Symbol* name() const; // returns the name of the method or field194Symbol* signature() const; // returns the signature of the method or field195196BasicType result_type() const; // returns the result type of the getfield or invoke197};198199// Abstraction for invoke_{virtual, static, interface, special, dynamic, handle}200201class Bytecode_invoke: public Bytecode_member_ref {202protected:203// Constructor that skips verification204Bytecode_invoke(const methodHandle& method, int bci, bool unused) : Bytecode_member_ref(method, bci) {}205206public:207Bytecode_invoke(const methodHandle& method, int bci) : Bytecode_member_ref(method, bci) { verify(); }208void verify() const;209210// Attributes211Method* static_target(TRAPS); // "specified" method (from constant pool)212213// Testers214bool is_invokeinterface() const { return invoke_code() == Bytecodes::_invokeinterface; }215bool is_invokevirtual() const { return invoke_code() == Bytecodes::_invokevirtual; }216bool is_invokestatic() const { return invoke_code() == Bytecodes::_invokestatic; }217bool is_invokespecial() const { return invoke_code() == Bytecodes::_invokespecial; }218bool is_invokedynamic() const { return invoke_code() == Bytecodes::_invokedynamic; }219bool is_invokehandle() const { return invoke_code() == Bytecodes::_invokehandle; }220221bool has_receiver() const { return !is_invokestatic() && !is_invokedynamic(); }222223bool is_valid() const { return is_invokeinterface() ||224is_invokevirtual() ||225is_invokestatic() ||226is_invokespecial() ||227is_invokedynamic() ||228is_invokehandle(); }229230bool has_appendix();231232int size_of_parameters() const;233234private:235// Helper to skip verification. Used is_valid() to check if the result is really an invoke236inline friend Bytecode_invoke Bytecode_invoke_check(const methodHandle& method, int bci);237};238239inline Bytecode_invoke Bytecode_invoke_check(const methodHandle& method, int bci) {240return Bytecode_invoke(method, bci, false);241}242243244// Abstraction for all field accesses (put/get field/static)245class Bytecode_field: public Bytecode_member_ref {246public:247Bytecode_field(const methodHandle& method, int bci) : Bytecode_member_ref(method, bci) { verify(); }248249// Testers250bool is_getfield() const { return java_code() == Bytecodes::_getfield; }251bool is_putfield() const { return java_code() == Bytecodes::_putfield; }252bool is_getstatic() const { return java_code() == Bytecodes::_getstatic; }253bool is_putstatic() const { return java_code() == Bytecodes::_putstatic; }254255bool is_getter() const { return is_getfield() || is_getstatic(); }256bool is_static() const { return is_getstatic() || is_putstatic(); }257258bool is_valid() const { return is_getfield() ||259is_putfield() ||260is_getstatic() ||261is_putstatic(); }262void verify() const;263};264265// Abstraction for checkcast266class Bytecode_checkcast: public Bytecode {267public:268Bytecode_checkcast(Method* method, address bcp): Bytecode(method, bcp) { verify(); }269void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); }270271// Returns index272long index() const { return get_index_u2(Bytecodes::_checkcast); };273};274275// Abstraction for instanceof276class Bytecode_instanceof: public Bytecode {277public:278Bytecode_instanceof(Method* method, address bcp): Bytecode(method, bcp) { verify(); }279void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); }280281// Returns index282long index() const { return get_index_u2(Bytecodes::_instanceof); };283};284285class Bytecode_new: public Bytecode {286public:287Bytecode_new(Method* method, address bcp): Bytecode(method, bcp) { verify(); }288void verify() const { assert(java_code() == Bytecodes::_new, "check new"); }289290// Returns index291long index() const { return get_index_u2(Bytecodes::_new); };292};293294class Bytecode_multianewarray: public Bytecode {295public:296Bytecode_multianewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }297void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); }298299// Returns index300long index() const { return get_index_u2(Bytecodes::_multianewarray); };301};302303class Bytecode_anewarray: public Bytecode {304public:305Bytecode_anewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }306void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); }307308// Returns index309long index() const { return get_index_u2(Bytecodes::_anewarray); };310};311312// Abstraction for ldc, ldc_w and ldc2_w313class Bytecode_loadconstant: public Bytecode {314private:315const Method* _method;316317int raw_index() const;318319public:320Bytecode_loadconstant(const methodHandle& method, int bci): Bytecode(method(), method->bcp_from(bci)), _method(method()) { verify(); }321322void verify() const {323assert(_method != NULL, "must supply method");324Bytecodes::Code stdc = Bytecodes::java_code(code());325assert(stdc == Bytecodes::_ldc ||326stdc == Bytecodes::_ldc_w ||327stdc == Bytecodes::_ldc2_w, "load constant");328}329330// Only non-standard bytecodes (fast_aldc) have reference cache indexes.331bool has_cache_index() const { return code() >= Bytecodes::number_of_java_codes; }332333int pool_index() const; // index into constant pool334int cache_index() const { // index into reference cache (or -1 if none)335return has_cache_index() ? raw_index() : -1;336}337338BasicType result_type() const; // returns the result type of the ldc339340oop resolve_constant(TRAPS) const;341};342343#endif // SHARE_INTERPRETER_BYTECODE_HPP344345346