duke@435: /* never@2462: * Copyright (c) 1997, 2011, 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: #include "precompiled.hpp" stefank@2314: #include "interpreter/bytecode.hpp" stefank@2314: #include "interpreter/linkResolver.hpp" stefank@2314: #include "oops/constantPoolOop.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/fieldType.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "runtime/safepoint.hpp" stefank@2314: #include "runtime/signature.hpp" duke@435: duke@435: // Implementation of Bytecode duke@435: jrose@1920: #ifdef ASSERT jrose@1920: jrose@1920: void Bytecode::assert_same_format_as(Bytecodes::Code testbc, bool is_wide) const { jrose@1920: Bytecodes::Code thisbc = Bytecodes::cast(byte_at(0)); jrose@1920: if (thisbc == Bytecodes::_breakpoint) return; // let the assertion fail silently jrose@1920: if (is_wide) { jrose@1920: assert(thisbc == Bytecodes::_wide, "expected a wide instruction"); jrose@1920: thisbc = Bytecodes::cast(byte_at(1)); jrose@1920: if (thisbc == Bytecodes::_breakpoint) return; jrose@1920: } jrose@1920: int thisflags = Bytecodes::flags(testbc, is_wide) & Bytecodes::_all_fmt_bits; jrose@1920: int testflags = Bytecodes::flags(thisbc, is_wide) & Bytecodes::_all_fmt_bits; jrose@1920: if (thisflags != testflags) jrose@1920: tty->print_cr("assert_same_format_as(%d) failed on bc=%d%s; %d != %d", jrose@1920: (int)testbc, (int)thisbc, (is_wide?"/wide":""), testflags, thisflags); jrose@1920: assert(thisflags == testflags, "expected format"); jrose@1920: } jrose@1920: jrose@1920: void Bytecode::assert_index_size(int size, Bytecodes::Code bc, bool is_wide) { jrose@1920: int have_fmt = (Bytecodes::flags(bc, is_wide) jrose@1920: & (Bytecodes::_fmt_has_u2 | Bytecodes::_fmt_has_u4 | jrose@1920: Bytecodes::_fmt_not_simple | jrose@1920: // Not an offset field: jrose@1920: Bytecodes::_fmt_has_o)); jrose@1920: int need_fmt = -1; jrose@1920: switch (size) { jrose@1920: case 1: need_fmt = 0; break; jrose@1920: case 2: need_fmt = Bytecodes::_fmt_has_u2; break; jrose@1920: case 4: need_fmt = Bytecodes::_fmt_has_u4; break; jrose@1920: } jrose@1920: if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple; jrose@1920: if (have_fmt != need_fmt) { jrose@1920: tty->print_cr("assert_index_size %d: bc=%d%s %d != %d", size, bc, (is_wide?"/wide":""), have_fmt, need_fmt); jrose@1920: assert(have_fmt == need_fmt, "assert_index_size"); jrose@1920: } jrose@1920: } jrose@1920: jrose@1920: void Bytecode::assert_offset_size(int size, Bytecodes::Code bc, bool is_wide) { jrose@1920: int have_fmt = Bytecodes::flags(bc, is_wide) & Bytecodes::_all_fmt_bits; jrose@1920: int need_fmt = -1; jrose@1920: switch (size) { jrose@1920: case 2: need_fmt = Bytecodes::_fmt_bo2; break; jrose@1920: case 4: need_fmt = Bytecodes::_fmt_bo4; break; jrose@1920: } jrose@1920: if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple; jrose@1920: if (have_fmt != need_fmt) { jrose@1920: tty->print_cr("assert_offset_size %d: bc=%d%s %d != %d", size, bc, (is_wide?"/wide":""), have_fmt, need_fmt); jrose@1920: assert(have_fmt == need_fmt, "assert_offset_size"); jrose@1920: } jrose@1920: } jrose@1920: jrose@1920: void Bytecode::assert_constant_size(int size, int where, Bytecodes::Code bc, bool is_wide) { jrose@1920: int have_fmt = Bytecodes::flags(bc, is_wide) & (Bytecodes::_all_fmt_bits jrose@1920: // Ignore any 'i' field (for iinc): jrose@1920: & ~Bytecodes::_fmt_has_i); jrose@1920: int need_fmt = -1; jrose@1920: switch (size) { jrose@1920: case 1: need_fmt = Bytecodes::_fmt_bc; break; jrose@1920: case 2: need_fmt = Bytecodes::_fmt_bc | Bytecodes::_fmt_has_u2; break; jrose@1920: } jrose@1920: if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple; jrose@1920: int length = is_wide ? Bytecodes::wide_length_for(bc) : Bytecodes::length_for(bc); jrose@1920: if (have_fmt != need_fmt || where + size != length) { jrose@1920: tty->print_cr("assert_constant_size %d @%d: bc=%d%s %d != %d", size, where, bc, (is_wide?"/wide":""), have_fmt, need_fmt); jrose@1920: } jrose@1920: assert(have_fmt == need_fmt, "assert_constant_size"); jrose@1920: assert(where + size == length, "assert_constant_size oob"); jrose@1920: } jrose@1920: jrose@1920: void Bytecode::assert_native_index(Bytecodes::Code bc, bool is_wide) { jrose@1920: assert((Bytecodes::flags(bc, is_wide) & Bytecodes::_fmt_has_nbo) != 0, "native index"); jrose@1920: } jrose@1920: jrose@1920: #endif //ASSERT duke@435: duke@435: // Implementation of Bytecode_tableupswitch duke@435: duke@435: int Bytecode_tableswitch::dest_offset_at(int i) const { jrose@1920: return get_Java_u4_at(aligned_offset(1 + (3 + i)*jintSize)); duke@435: } duke@435: duke@435: duke@435: // Implementation of Bytecode_invoke duke@435: duke@435: void Bytecode_invoke::verify() const { duke@435: assert(is_valid(), "check invoke"); jrose@1920: assert(method()->constants()->cache() != NULL, "do not call this from verifier or rewriter"); duke@435: } duke@435: duke@435: coleenp@2497: Symbol* Bytecode_member_ref::signature() const { duke@435: constantPoolOop constants = method()->constants(); duke@435: return constants->signature_ref_at(index()); duke@435: } duke@435: duke@435: coleenp@2497: Symbol* Bytecode_member_ref::name() const { duke@435: constantPoolOop constants = method()->constants(); duke@435: return constants->name_ref_at(index()); duke@435: } duke@435: duke@435: coleenp@2497: BasicType Bytecode_member_ref::result_type() const { coleenp@2497: ResultTypeFinder rts(signature()); duke@435: rts.iterate(); duke@435: return rts.type(); duke@435: } duke@435: duke@435: duke@435: methodHandle Bytecode_invoke::static_target(TRAPS) { duke@435: methodHandle m; duke@435: KlassHandle resolved_klass; duke@435: constantPoolHandle constants(THREAD, _method->constants()); duke@435: jrose@1957: if (java_code() == Bytecodes::_invokedynamic) { twisti@1570: LinkResolver::resolve_dynamic_method(m, resolved_klass, constants, index(), CHECK_(methodHandle())); jrose@1957: } else if (java_code() != Bytecodes::_invokeinterface) { duke@435: LinkResolver::resolve_method(m, resolved_klass, constants, index(), CHECK_(methodHandle())); duke@435: } else { duke@435: LinkResolver::resolve_interface_method(m, resolved_klass, constants, index(), CHECK_(methodHandle())); duke@435: } duke@435: return m; duke@435: } duke@435: duke@435: jrose@1957: int Bytecode_member_ref::index() const { jrose@1161: // Note: Rewriter::rewrite changes the Java_u2 of an invokedynamic to a native_u4, jrose@1161: // at the same time it allocates per-call-site CP cache entries. jrose@1957: Bytecodes::Code rawc = code(); never@2462: if (has_index_u4(rawc)) never@2462: return get_index_u4(rawc); jrose@1161: else never@2462: return get_index_u2_cpcache(rawc); duke@435: } duke@435: jrose@1957: int Bytecode_member_ref::pool_index() const { jrose@1957: int index = this->index(); jrose@1957: DEBUG_ONLY({ never@2462: if (!has_index_u4(code())) jrose@1957: index -= constantPoolOopDesc::CPCACHE_INDEX_TAG; jrose@1957: }); jrose@1957: return _method->constants()->cache()->entry_at(index)->constant_pool_index(); jrose@1957: } duke@435: duke@435: // Implementation of Bytecode_field duke@435: duke@435: void Bytecode_field::verify() const { jrose@1957: assert(is_valid(), "check field"); duke@435: } duke@435: duke@435: jrose@1957: // Implementation of Bytecode_loadconstant jrose@1957: jrose@1957: int Bytecode_loadconstant::raw_index() const { never@2462: Bytecodes::Code rawc = code(); jrose@1957: assert(rawc != Bytecodes::_wide, "verifier prevents this"); jrose@1957: if (Bytecodes::java_code(rawc) == Bytecodes::_ldc) never@2462: return get_index_u1(rawc); jrose@1957: else never@2462: return get_index_u2(rawc, false); duke@435: } duke@435: jrose@1957: int Bytecode_loadconstant::pool_index() const { jrose@1957: int index = raw_index(); jrose@1957: if (has_cache_index()) { jrose@1957: return _method->constants()->cache()->entry_at(index)->constant_pool_index(); jrose@1957: } jrose@1957: return index; duke@435: } duke@435: jrose@1957: BasicType Bytecode_loadconstant::result_type() const { jrose@1957: int index = pool_index(); jrose@1957: constantTag tag = _method->constants()->tag_at(index); jrose@1957: return tag.basic_type(); jrose@1957: } duke@435: jrose@1957: oop Bytecode_loadconstant::resolve_constant(TRAPS) const { jrose@1957: assert(_method.not_null(), "must supply method to resolve constant"); jrose@1957: int index = raw_index(); jrose@1957: constantPoolOop constants = _method->constants(); jrose@1957: if (has_cache_index()) { jrose@1957: return constants->resolve_cached_constant_at(index, THREAD); jrose@1957: } else { jrose@1957: return constants->resolve_constant_at(index, THREAD); jrose@1929: } duke@435: } duke@435: duke@435: //------------------------------------------------------------------------------ duke@435: // Non-product code duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: void Bytecode_lookupswitch::verify() const { duke@435: switch (Bytecodes::java_code(code())) { duke@435: case Bytecodes::_lookupswitch: duke@435: { int i = number_of_pairs() - 1; duke@435: while (i-- > 0) { never@2462: assert(pair_at(i).match() < pair_at(i+1).match(), "unsorted table entries"); duke@435: } duke@435: } duke@435: break; duke@435: default: duke@435: fatal("not a lookupswitch bytecode"); duke@435: } duke@435: } duke@435: duke@435: void Bytecode_tableswitch::verify() const { duke@435: switch (Bytecodes::java_code(code())) { duke@435: case Bytecodes::_tableswitch: duke@435: { int lo = low_key(); duke@435: int hi = high_key(); duke@435: assert (hi >= lo, "incorrect hi/lo values in tableswitch"); duke@435: int i = hi - lo - 1 ; duke@435: while (i-- > 0) { duke@435: // no special check needed duke@435: } duke@435: } duke@435: break; duke@435: default: duke@435: fatal("not a tableswitch bytecode"); duke@435: } duke@435: } duke@435: duke@435: #endif