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