Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/interpreter/bytecode.hpp
32285 views
/*1* Copyright (c) 1997, 2012, 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_VM_INTERPRETER_BYTECODE_HPP25#define SHARE_VM_INTERPRETER_BYTECODE_HPP2627#include "interpreter/bytecodes.hpp"28#include "memory/allocation.hpp"29#include "oops/method.hpp"30#ifdef TARGET_ARCH_x8631# include "bytes_x86.hpp"32#endif33#ifdef TARGET_ARCH_aarch3234# include "bytes_aarch32.hpp"35#endif36#ifdef TARGET_ARCH_aarch6437# include "bytes_aarch64.hpp"38#endif39#ifdef TARGET_ARCH_sparc40# include "bytes_sparc.hpp"41#endif42#ifdef TARGET_ARCH_zero43# include "bytes_zero.hpp"44#endif45#ifdef TARGET_ARCH_arm46# include "bytes_arm.hpp"47#endif48#ifdef TARGET_ARCH_ppc49# include "bytes_ppc.hpp"50#endif5152class ciBytecodeStream;5354// The base class for different kinds of bytecode abstractions.55// Provides the primitive operations to manipulate code relative56// to the bcp.5758class Bytecode: public StackObj {59protected:60const address _bcp;61const Bytecodes::Code _code;6263// Address computation64address addr_at (int offset) const { return (address)_bcp + offset; }65u_char byte_at(int offset) const { return *addr_at(offset); }66address aligned_addr_at (int offset) const { return (address)round_to((intptr_t)addr_at(offset), jintSize); }67int aligned_offset (int offset) const { return aligned_addr_at(offset) - addr_at(0); }6869// Word access:70int get_Java_u2_at (int offset) const { return Bytes::get_Java_u2(addr_at(offset)); }71int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); }72int get_native_u2_at (int offset) const { return Bytes::get_native_u2(addr_at(offset)); }73int get_native_u4_at (int offset) const { return Bytes::get_native_u4(addr_at(offset)); }7475public:76Bytecode(Method* method, address bcp): _bcp(bcp), _code(Bytecodes::code_at(method, addr_at(0))) {77assert(method != NULL, "this form requires a valid Method*");78}79// Defined in ciStreams.hpp80inline Bytecode(const ciBytecodeStream* stream, address bcp = NULL);8182// Attributes83address bcp() const { return _bcp; }84int instruction_size() const { return Bytecodes::length_for_code_at(_code, bcp()); }8586Bytecodes::Code code() const { return _code; }87Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); }88Bytecodes::Code invoke_code() const { return (code() == Bytecodes::_invokehandle) ? code() : java_code(); }8990// Static functions for parsing bytecodes in place.91int get_index_u1(Bytecodes::Code bc) const {92assert_same_format_as(bc); assert_index_size(1, bc);93return *(jubyte*)addr_at(1);94}95int get_index_u2(Bytecodes::Code bc, bool is_wide = false) const {96assert_same_format_as(bc, is_wide); assert_index_size(2, bc, is_wide);97address p = addr_at(is_wide ? 2 : 1);98if (can_use_native_byte_order(bc, is_wide))99return Bytes::get_native_u2(p);100else return Bytes::get_Java_u2(p);101}102int get_index_u1_cpcache(Bytecodes::Code bc) const {103assert_same_format_as(bc); assert_index_size(1, bc);104return *(jubyte*)addr_at(1) + ConstantPool::CPCACHE_INDEX_TAG;105}106int get_index_u2_cpcache(Bytecodes::Code bc) const {107assert_same_format_as(bc); assert_index_size(2, bc); assert_native_index(bc);108return Bytes::get_native_u2(addr_at(1)) + ConstantPool::CPCACHE_INDEX_TAG;109}110int get_index_u4(Bytecodes::Code bc) const {111assert_same_format_as(bc); assert_index_size(4, bc);112assert(can_use_native_byte_order(bc), "");113return Bytes::get_native_u4(addr_at(1));114}115bool has_index_u4(Bytecodes::Code bc) const {116return bc == Bytecodes::_invokedynamic;117}118119int get_offset_s2(Bytecodes::Code bc) const {120assert_same_format_as(bc); assert_offset_size(2, bc);121return (jshort) Bytes::get_Java_u2(addr_at(1));122}123int get_offset_s4(Bytecodes::Code bc) const {124assert_same_format_as(bc); assert_offset_size(4, bc);125return (jint) Bytes::get_Java_u4(addr_at(1));126}127128int get_constant_u1(int offset, Bytecodes::Code bc) const {129assert_same_format_as(bc); assert_constant_size(1, offset, bc);130return *(jbyte*)addr_at(offset);131}132int get_constant_u2(int offset, Bytecodes::Code bc, bool is_wide = false) const {133assert_same_format_as(bc, is_wide); assert_constant_size(2, offset, bc, is_wide);134return (jshort) Bytes::get_Java_u2(addr_at(offset));135}136137// These are used locally and also from bytecode streams.138void assert_same_format_as(Bytecodes::Code testbc, bool is_wide = false) const NOT_DEBUG_RETURN;139static void assert_index_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;140static void assert_offset_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;141static void assert_constant_size(int required_size, int where, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;142static void assert_native_index(Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;143static bool can_use_native_byte_order(Bytecodes::Code bc, bool is_wide = false) {144return (!Bytes::is_Java_byte_ordering_different() || Bytecodes::native_byte_order(bc /*, is_wide*/));145}146};147148149// Abstractions for lookupswitch bytecode150class LookupswitchPair VALUE_OBJ_CLASS_SPEC {151private:152const address _bcp;153154address addr_at (int offset) const { return _bcp + offset; }155int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); }156157public:158LookupswitchPair(address bcp): _bcp(bcp) {}159int match() const { return get_Java_u4_at(0 * jintSize); }160int offset() const { return get_Java_u4_at(1 * jintSize); }161};162163164class Bytecode_lookupswitch: public Bytecode {165public:166Bytecode_lookupswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }167// Defined in ciStreams.hpp168inline Bytecode_lookupswitch(const ciBytecodeStream* stream);169void verify() const PRODUCT_RETURN;170171// Attributes172int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); }173int number_of_pairs() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); }174LookupswitchPair pair_at(int i) const {175assert(0 <= i && i < number_of_pairs(), "pair index out of bounds");176return LookupswitchPair(aligned_addr_at(1 + (1 + i)*2*jintSize));177}178};179180class Bytecode_tableswitch: public Bytecode {181public:182Bytecode_tableswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }183// Defined in ciStreams.hpp184inline Bytecode_tableswitch(const ciBytecodeStream* stream);185void verify() const PRODUCT_RETURN;186187// Attributes188int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); }189int low_key() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); }190int high_key() const { return get_Java_u4_at(aligned_offset(1 + 2*jintSize)); }191int dest_offset_at(int i) const;192int length() { return high_key()-low_key()+1; }193};194195// Common code for decoding invokes and field references.196197class Bytecode_member_ref: public Bytecode {198protected:199const methodHandle _method; // method containing the bytecode200201Bytecode_member_ref(methodHandle method, int bci) : Bytecode(method(), method()->bcp_from(bci)), _method(method) {}202203methodHandle method() const { return _method; }204ConstantPool* constants() const { return _method->constants(); }205ConstantPoolCache* cpcache() const { return _method->constants()->cache(); }206ConstantPoolCacheEntry* cpcache_entry() const;207208public:209int index() const; // cache index (loaded from instruction)210int pool_index() const; // constant pool index211Symbol* klass() const; // returns the klass of the method or field212Symbol* name() const; // returns the name of the method or field213Symbol* signature() const; // returns the signature of the method or field214215BasicType result_type() const; // returns the result type of the getfield or invoke216};217218// Abstraction for invoke_{virtual, static, interface, special}219220class Bytecode_invoke: public Bytecode_member_ref {221protected:222// Constructor that skips verification223Bytecode_invoke(methodHandle method, int bci, bool unused) : Bytecode_member_ref(method, bci) {}224225public:226Bytecode_invoke(methodHandle method, int bci) : Bytecode_member_ref(method, bci) { verify(); }227void verify() const;228229// Attributes230methodHandle static_target(TRAPS); // "specified" method (from constant pool)231Handle appendix(TRAPS); // if CPCE::has_appendix (from constant pool)232233// Testers234bool is_invokeinterface() const { return invoke_code() == Bytecodes::_invokeinterface; }235bool is_invokevirtual() const { return invoke_code() == Bytecodes::_invokevirtual; }236bool is_invokestatic() const { return invoke_code() == Bytecodes::_invokestatic; }237bool is_invokespecial() const { return invoke_code() == Bytecodes::_invokespecial; }238bool is_invokedynamic() const { return invoke_code() == Bytecodes::_invokedynamic; }239bool is_invokehandle() const { return invoke_code() == Bytecodes::_invokehandle; }240241bool has_receiver() const { return !is_invokestatic() && !is_invokedynamic(); }242243bool is_valid() const { return is_invokeinterface() ||244is_invokevirtual() ||245is_invokestatic() ||246is_invokespecial() ||247is_invokedynamic() ||248is_invokehandle(); }249250bool has_appendix() { return cpcache_entry()->has_appendix(); }251252private:253// Helper to skip verification. Used is_valid() to check if the result is really an invoke254inline friend Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci);255};256257inline Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci) {258return Bytecode_invoke(method, bci, false);259}260261262// Abstraction for all field accesses (put/get field/static)263class Bytecode_field: public Bytecode_member_ref {264public:265Bytecode_field(methodHandle method, int bci) : Bytecode_member_ref(method, bci) { verify(); }266267// Testers268bool is_getfield() const { return java_code() == Bytecodes::_getfield; }269bool is_putfield() const { return java_code() == Bytecodes::_putfield; }270bool is_getstatic() const { return java_code() == Bytecodes::_getstatic; }271bool is_putstatic() const { return java_code() == Bytecodes::_putstatic; }272273bool is_getter() const { return is_getfield() || is_getstatic(); }274bool is_static() const { return is_getstatic() || is_putstatic(); }275276bool is_valid() const { return is_getfield() ||277is_putfield() ||278is_getstatic() ||279is_putstatic(); }280void verify() const;281};282283// Abstraction for checkcast284class Bytecode_checkcast: public Bytecode {285public:286Bytecode_checkcast(Method* method, address bcp): Bytecode(method, bcp) { verify(); }287void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); }288289// Returns index290long index() const { return get_index_u2(Bytecodes::_checkcast); };291};292293// Abstraction for instanceof294class Bytecode_instanceof: public Bytecode {295public:296Bytecode_instanceof(Method* method, address bcp): Bytecode(method, bcp) { verify(); }297void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); }298299// Returns index300long index() const { return get_index_u2(Bytecodes::_instanceof); };301};302303class Bytecode_new: public Bytecode {304public:305Bytecode_new(Method* method, address bcp): Bytecode(method, bcp) { verify(); }306void verify() const { assert(java_code() == Bytecodes::_new, "check new"); }307308// Returns index309long index() const { return get_index_u2(Bytecodes::_new); };310};311312class Bytecode_multianewarray: public Bytecode {313public:314Bytecode_multianewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }315void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); }316317// Returns index318long index() const { return get_index_u2(Bytecodes::_multianewarray); };319};320321class Bytecode_anewarray: public Bytecode {322public:323Bytecode_anewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }324void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); }325326// Returns index327long index() const { return get_index_u2(Bytecodes::_anewarray); };328};329330// Abstraction for ldc, ldc_w and ldc2_w331class Bytecode_loadconstant: public Bytecode {332private:333const methodHandle _method;334335int raw_index() const;336337public:338Bytecode_loadconstant(methodHandle method, int bci): Bytecode(method(), method->bcp_from(bci)), _method(method) { verify(); }339340void verify() const {341assert(_method.not_null(), "must supply method");342Bytecodes::Code stdc = Bytecodes::java_code(code());343assert(stdc == Bytecodes::_ldc ||344stdc == Bytecodes::_ldc_w ||345stdc == Bytecodes::_ldc2_w, "load constant");346}347348// Only non-standard bytecodes (fast_aldc) have reference cache indexes.349bool has_cache_index() const { return code() >= Bytecodes::number_of_java_codes; }350351int pool_index() const; // index into constant pool352int cache_index() const { // index into reference cache (or -1 if none)353return has_cache_index() ? raw_index() : -1;354}355356BasicType result_type() const; // returns the result type of the ldc357358oop resolve_constant(TRAPS) const;359};360361#endif // SHARE_VM_INTERPRETER_BYTECODE_HPP362363364