aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_INTERPRETER_BYTECODE_HPP aoqi@0: #define SHARE_VM_INTERPRETER_BYTECODE_HPP aoqi@0: aoqi@0: #include "interpreter/bytecodes.hpp" aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "oops/method.hpp" aoqi@0: #ifdef TARGET_ARCH_x86 aoqi@0: # include "bytes_x86.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_sparc aoqi@0: # include "bytes_sparc.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_zero aoqi@0: # include "bytes_zero.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_arm aoqi@0: # include "bytes_arm.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_ppc aoqi@0: # include "bytes_ppc.hpp" aoqi@0: #endif aoqi@0: aoqi@0: class ciBytecodeStream; aoqi@0: aoqi@0: // The base class for different kinds of bytecode abstractions. aoqi@0: // Provides the primitive operations to manipulate code relative aoqi@0: // to the bcp. aoqi@0: aoqi@0: class Bytecode: public StackObj { aoqi@0: protected: aoqi@0: const address _bcp; aoqi@0: const Bytecodes::Code _code; aoqi@0: aoqi@0: // Address computation aoqi@0: address addr_at (int offset) const { return (address)_bcp + offset; } aoqi@0: u_char byte_at(int offset) const { return *addr_at(offset); } aoqi@0: address aligned_addr_at (int offset) const { return (address)round_to((intptr_t)addr_at(offset), jintSize); } aoqi@0: int aligned_offset (int offset) const { return aligned_addr_at(offset) - addr_at(0); } aoqi@0: aoqi@0: // Word access: aoqi@0: int get_Java_u2_at (int offset) const { return Bytes::get_Java_u2(addr_at(offset)); } aoqi@0: int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); } aoqi@0: int get_native_u2_at (int offset) const { return Bytes::get_native_u2(addr_at(offset)); } aoqi@0: int get_native_u4_at (int offset) const { return Bytes::get_native_u4(addr_at(offset)); } aoqi@0: aoqi@0: public: aoqi@0: Bytecode(Method* method, address bcp): _bcp(bcp), _code(Bytecodes::code_at(method, addr_at(0))) { aoqi@0: assert(method != NULL, "this form requires a valid Method*"); aoqi@0: } aoqi@0: // Defined in ciStreams.hpp aoqi@0: inline Bytecode(const ciBytecodeStream* stream, address bcp = NULL); aoqi@0: aoqi@0: // Attributes aoqi@0: address bcp() const { return _bcp; } aoqi@0: int instruction_size() const { return Bytecodes::length_for_code_at(_code, bcp()); } aoqi@0: aoqi@0: Bytecodes::Code code() const { return _code; } aoqi@0: Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); } aoqi@0: Bytecodes::Code invoke_code() const { return (code() == Bytecodes::_invokehandle) ? code() : java_code(); } aoqi@0: aoqi@0: // Static functions for parsing bytecodes in place. aoqi@0: int get_index_u1(Bytecodes::Code bc) const { aoqi@0: assert_same_format_as(bc); assert_index_size(1, bc); aoqi@0: return *(jubyte*)addr_at(1); aoqi@0: } aoqi@0: int get_index_u2(Bytecodes::Code bc, bool is_wide = false) const { aoqi@0: assert_same_format_as(bc, is_wide); assert_index_size(2, bc, is_wide); aoqi@0: address p = addr_at(is_wide ? 2 : 1); aoqi@0: if (can_use_native_byte_order(bc, is_wide)) aoqi@0: return Bytes::get_native_u2(p); aoqi@0: else return Bytes::get_Java_u2(p); aoqi@0: } aoqi@0: int get_index_u1_cpcache(Bytecodes::Code bc) const { aoqi@0: assert_same_format_as(bc); assert_index_size(1, bc); aoqi@0: return *(jubyte*)addr_at(1) + ConstantPool::CPCACHE_INDEX_TAG; aoqi@0: } aoqi@0: int get_index_u2_cpcache(Bytecodes::Code bc) const { aoqi@0: assert_same_format_as(bc); assert_index_size(2, bc); assert_native_index(bc); aoqi@0: return Bytes::get_native_u2(addr_at(1)) + ConstantPool::CPCACHE_INDEX_TAG; aoqi@0: } aoqi@0: int get_index_u4(Bytecodes::Code bc) const { aoqi@0: assert_same_format_as(bc); assert_index_size(4, bc); aoqi@0: assert(can_use_native_byte_order(bc), ""); aoqi@0: return Bytes::get_native_u4(addr_at(1)); aoqi@0: } aoqi@0: bool has_index_u4(Bytecodes::Code bc) const { aoqi@0: return bc == Bytecodes::_invokedynamic; aoqi@0: } aoqi@0: aoqi@0: int get_offset_s2(Bytecodes::Code bc) const { aoqi@0: assert_same_format_as(bc); assert_offset_size(2, bc); aoqi@0: return (jshort) Bytes::get_Java_u2(addr_at(1)); aoqi@0: } aoqi@0: int get_offset_s4(Bytecodes::Code bc) const { aoqi@0: assert_same_format_as(bc); assert_offset_size(4, bc); aoqi@0: return (jint) Bytes::get_Java_u4(addr_at(1)); aoqi@0: } aoqi@0: aoqi@0: int get_constant_u1(int offset, Bytecodes::Code bc) const { aoqi@0: assert_same_format_as(bc); assert_constant_size(1, offset, bc); aoqi@0: return *(jbyte*)addr_at(offset); aoqi@0: } aoqi@0: int get_constant_u2(int offset, Bytecodes::Code bc, bool is_wide = false) const { aoqi@0: assert_same_format_as(bc, is_wide); assert_constant_size(2, offset, bc, is_wide); aoqi@0: return (jshort) Bytes::get_Java_u2(addr_at(offset)); aoqi@0: } aoqi@0: aoqi@0: // These are used locally and also from bytecode streams. aoqi@0: void assert_same_format_as(Bytecodes::Code testbc, bool is_wide = false) const NOT_DEBUG_RETURN; aoqi@0: static void assert_index_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; aoqi@0: static void assert_offset_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; aoqi@0: static void assert_constant_size(int required_size, int where, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; aoqi@0: static void assert_native_index(Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; aoqi@0: static bool can_use_native_byte_order(Bytecodes::Code bc, bool is_wide = false) { aoqi@0: return (!Bytes::is_Java_byte_ordering_different() || Bytecodes::native_byte_order(bc /*, is_wide*/)); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // Abstractions for lookupswitch bytecode aoqi@0: class LookupswitchPair VALUE_OBJ_CLASS_SPEC { aoqi@0: private: aoqi@0: const address _bcp; aoqi@0: aoqi@0: address addr_at (int offset) const { return _bcp + offset; } aoqi@0: int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); } aoqi@0: aoqi@0: public: aoqi@0: LookupswitchPair(address bcp): _bcp(bcp) {} aoqi@0: int match() const { return get_Java_u4_at(0 * jintSize); } aoqi@0: int offset() const { return get_Java_u4_at(1 * jintSize); } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class Bytecode_lookupswitch: public Bytecode { aoqi@0: public: aoqi@0: Bytecode_lookupswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); } aoqi@0: // Defined in ciStreams.hpp aoqi@0: inline Bytecode_lookupswitch(const ciBytecodeStream* stream); aoqi@0: void verify() const PRODUCT_RETURN; aoqi@0: aoqi@0: // Attributes aoqi@0: int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); } aoqi@0: int number_of_pairs() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); } aoqi@0: LookupswitchPair pair_at(int i) const { aoqi@0: assert(0 <= i && i < number_of_pairs(), "pair index out of bounds"); aoqi@0: return LookupswitchPair(aligned_addr_at(1 + (1 + i)*2*jintSize)); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class Bytecode_tableswitch: public Bytecode { aoqi@0: public: aoqi@0: Bytecode_tableswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); } aoqi@0: // Defined in ciStreams.hpp aoqi@0: inline Bytecode_tableswitch(const ciBytecodeStream* stream); aoqi@0: void verify() const PRODUCT_RETURN; aoqi@0: aoqi@0: // Attributes aoqi@0: int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); } aoqi@0: int low_key() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); } aoqi@0: int high_key() const { return get_Java_u4_at(aligned_offset(1 + 2*jintSize)); } aoqi@0: int dest_offset_at(int i) const; aoqi@0: int length() { return high_key()-low_key()+1; } aoqi@0: }; aoqi@0: aoqi@0: // Common code for decoding invokes and field references. aoqi@0: aoqi@0: class Bytecode_member_ref: public Bytecode { aoqi@0: protected: aoqi@0: const methodHandle _method; // method containing the bytecode aoqi@0: aoqi@0: Bytecode_member_ref(methodHandle method, int bci) : Bytecode(method(), method()->bcp_from(bci)), _method(method) {} aoqi@0: aoqi@0: methodHandle method() const { return _method; } aoqi@0: ConstantPool* constants() const { return _method->constants(); } aoqi@0: ConstantPoolCache* cpcache() const { return _method->constants()->cache(); } aoqi@0: ConstantPoolCacheEntry* cpcache_entry() const; aoqi@0: aoqi@0: public: aoqi@0: int index() const; // cache index (loaded from instruction) aoqi@0: int pool_index() const; // constant pool index aoqi@0: Symbol* klass() const; // returns the klass of the method or field aoqi@0: Symbol* name() const; // returns the name of the method or field aoqi@0: Symbol* signature() const; // returns the signature of the method or field aoqi@0: aoqi@0: BasicType result_type() const; // returns the result type of the getfield or invoke aoqi@0: }; aoqi@0: aoqi@0: // Abstraction for invoke_{virtual, static, interface, special} aoqi@0: aoqi@0: class Bytecode_invoke: public Bytecode_member_ref { aoqi@0: protected: aoqi@0: // Constructor that skips verification aoqi@0: Bytecode_invoke(methodHandle method, int bci, bool unused) : Bytecode_member_ref(method, bci) {} aoqi@0: aoqi@0: public: aoqi@0: Bytecode_invoke(methodHandle method, int bci) : Bytecode_member_ref(method, bci) { verify(); } aoqi@0: void verify() const; aoqi@0: aoqi@0: // Attributes aoqi@0: methodHandle static_target(TRAPS); // "specified" method (from constant pool) aoqi@0: Handle appendix(TRAPS); // if CPCE::has_appendix (from constant pool) aoqi@0: aoqi@0: // Testers aoqi@0: bool is_invokeinterface() const { return invoke_code() == Bytecodes::_invokeinterface; } aoqi@0: bool is_invokevirtual() const { return invoke_code() == Bytecodes::_invokevirtual; } aoqi@0: bool is_invokestatic() const { return invoke_code() == Bytecodes::_invokestatic; } aoqi@0: bool is_invokespecial() const { return invoke_code() == Bytecodes::_invokespecial; } aoqi@0: bool is_invokedynamic() const { return invoke_code() == Bytecodes::_invokedynamic; } aoqi@0: bool is_invokehandle() const { return invoke_code() == Bytecodes::_invokehandle; } aoqi@0: aoqi@0: bool has_receiver() const { return !is_invokestatic() && !is_invokedynamic(); } aoqi@0: aoqi@0: bool is_valid() const { return is_invokeinterface() || aoqi@0: is_invokevirtual() || aoqi@0: is_invokestatic() || aoqi@0: is_invokespecial() || aoqi@0: is_invokedynamic() || aoqi@0: is_invokehandle(); } aoqi@0: aoqi@0: bool has_appendix() { return cpcache_entry()->has_appendix(); } aoqi@0: aoqi@0: private: aoqi@0: // Helper to skip verification. Used is_valid() to check if the result is really an invoke aoqi@0: inline friend Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci); aoqi@0: }; aoqi@0: aoqi@0: inline Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci) { aoqi@0: return Bytecode_invoke(method, bci, false); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Abstraction for all field accesses (put/get field/static) aoqi@0: class Bytecode_field: public Bytecode_member_ref { aoqi@0: public: aoqi@0: Bytecode_field(methodHandle method, int bci) : Bytecode_member_ref(method, bci) { verify(); } aoqi@0: aoqi@0: // Testers aoqi@0: bool is_getfield() const { return java_code() == Bytecodes::_getfield; } aoqi@0: bool is_putfield() const { return java_code() == Bytecodes::_putfield; } aoqi@0: bool is_getstatic() const { return java_code() == Bytecodes::_getstatic; } aoqi@0: bool is_putstatic() const { return java_code() == Bytecodes::_putstatic; } aoqi@0: aoqi@0: bool is_getter() const { return is_getfield() || is_getstatic(); } aoqi@0: bool is_static() const { return is_getstatic() || is_putstatic(); } aoqi@0: aoqi@0: bool is_valid() const { return is_getfield() || aoqi@0: is_putfield() || aoqi@0: is_getstatic() || aoqi@0: is_putstatic(); } aoqi@0: void verify() const; aoqi@0: }; aoqi@0: aoqi@0: // Abstraction for checkcast aoqi@0: class Bytecode_checkcast: public Bytecode { aoqi@0: public: aoqi@0: Bytecode_checkcast(Method* method, address bcp): Bytecode(method, bcp) { verify(); } aoqi@0: void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); } aoqi@0: aoqi@0: // Returns index aoqi@0: long index() const { return get_index_u2(Bytecodes::_checkcast); }; aoqi@0: }; aoqi@0: aoqi@0: // Abstraction for instanceof aoqi@0: class Bytecode_instanceof: public Bytecode { aoqi@0: public: aoqi@0: Bytecode_instanceof(Method* method, address bcp): Bytecode(method, bcp) { verify(); } aoqi@0: void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); } aoqi@0: aoqi@0: // Returns index aoqi@0: long index() const { return get_index_u2(Bytecodes::_instanceof); }; aoqi@0: }; aoqi@0: aoqi@0: class Bytecode_new: public Bytecode { aoqi@0: public: aoqi@0: Bytecode_new(Method* method, address bcp): Bytecode(method, bcp) { verify(); } aoqi@0: void verify() const { assert(java_code() == Bytecodes::_new, "check new"); } aoqi@0: aoqi@0: // Returns index aoqi@0: long index() const { return get_index_u2(Bytecodes::_new); }; aoqi@0: }; aoqi@0: aoqi@0: class Bytecode_multianewarray: public Bytecode { aoqi@0: public: aoqi@0: Bytecode_multianewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); } aoqi@0: void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); } aoqi@0: aoqi@0: // Returns index aoqi@0: long index() const { return get_index_u2(Bytecodes::_multianewarray); }; aoqi@0: }; aoqi@0: aoqi@0: class Bytecode_anewarray: public Bytecode { aoqi@0: public: aoqi@0: Bytecode_anewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); } aoqi@0: void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); } aoqi@0: aoqi@0: // Returns index aoqi@0: long index() const { return get_index_u2(Bytecodes::_anewarray); }; aoqi@0: }; aoqi@0: aoqi@0: // Abstraction for ldc, ldc_w and ldc2_w aoqi@0: class Bytecode_loadconstant: public Bytecode { aoqi@0: private: aoqi@0: const methodHandle _method; aoqi@0: aoqi@0: int raw_index() const; aoqi@0: aoqi@0: public: aoqi@0: Bytecode_loadconstant(methodHandle method, int bci): Bytecode(method(), method->bcp_from(bci)), _method(method) { verify(); } aoqi@0: aoqi@0: void verify() const { aoqi@0: assert(_method.not_null(), "must supply method"); aoqi@0: Bytecodes::Code stdc = Bytecodes::java_code(code()); aoqi@0: assert(stdc == Bytecodes::_ldc || aoqi@0: stdc == Bytecodes::_ldc_w || aoqi@0: stdc == Bytecodes::_ldc2_w, "load constant"); aoqi@0: } aoqi@0: aoqi@0: // Only non-standard bytecodes (fast_aldc) have reference cache indexes. aoqi@0: bool has_cache_index() const { return code() >= Bytecodes::number_of_java_codes; } aoqi@0: aoqi@0: int pool_index() const; // index into constant pool aoqi@0: int cache_index() const { // index into reference cache (or -1 if none) aoqi@0: return has_cache_index() ? raw_index() : -1; aoqi@0: } aoqi@0: aoqi@0: BasicType result_type() const; // returns the result type of the ldc aoqi@0: aoqi@0: oop resolve_constant(TRAPS) const; aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_INTERPRETER_BYTECODE_HPP