Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/interpreter/bytecode.cpp
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#include "precompiled.hpp"25#include "interpreter/bytecode.hpp"26#include "interpreter/linkResolver.hpp"27#include "oops/constantPool.hpp"28#include "oops/oop.inline.hpp"29#include "runtime/fieldType.hpp"30#include "runtime/handles.inline.hpp"31#include "runtime/safepoint.hpp"32#include "runtime/signature.hpp"3334// Implementation of Bytecode3536#ifdef ASSERT3738void Bytecode::assert_same_format_as(Bytecodes::Code testbc, bool is_wide) const {39Bytecodes::Code thisbc = Bytecodes::cast(byte_at(0));40if (thisbc == Bytecodes::_breakpoint) return; // let the assertion fail silently41if (is_wide) {42assert(thisbc == Bytecodes::_wide, "expected a wide instruction");43thisbc = Bytecodes::cast(byte_at(1));44if (thisbc == Bytecodes::_breakpoint) return;45}46int thisflags = Bytecodes::flags(testbc, is_wide) & Bytecodes::_all_fmt_bits;47int testflags = Bytecodes::flags(thisbc, is_wide) & Bytecodes::_all_fmt_bits;48if (thisflags != testflags)49tty->print_cr("assert_same_format_as(%d) failed on bc=%d%s; %d != %d",50(int)testbc, (int)thisbc, (is_wide?"/wide":""), testflags, thisflags);51assert(thisflags == testflags, "expected format");52}5354void Bytecode::assert_index_size(int size, Bytecodes::Code bc, bool is_wide) {55int have_fmt = (Bytecodes::flags(bc, is_wide)56& (Bytecodes::_fmt_has_u2 | Bytecodes::_fmt_has_u4 |57Bytecodes::_fmt_not_simple |58// Not an offset field:59Bytecodes::_fmt_has_o));60int need_fmt = -1;61switch (size) {62case 1: need_fmt = 0; break;63case 2: need_fmt = Bytecodes::_fmt_has_u2; break;64case 4: need_fmt = Bytecodes::_fmt_has_u4; break;65}66if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple;67if (have_fmt != need_fmt) {68tty->print_cr("assert_index_size %d: bc=%d%s %d != %d", size, bc, (is_wide?"/wide":""), have_fmt, need_fmt);69assert(have_fmt == need_fmt, "assert_index_size");70}71}7273void Bytecode::assert_offset_size(int size, Bytecodes::Code bc, bool is_wide) {74int have_fmt = Bytecodes::flags(bc, is_wide) & Bytecodes::_all_fmt_bits;75int need_fmt = -1;76switch (size) {77case 2: need_fmt = Bytecodes::_fmt_bo2; break;78case 4: need_fmt = Bytecodes::_fmt_bo4; break;79}80if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple;81if (have_fmt != need_fmt) {82tty->print_cr("assert_offset_size %d: bc=%d%s %d != %d", size, bc, (is_wide?"/wide":""), have_fmt, need_fmt);83assert(have_fmt == need_fmt, "assert_offset_size");84}85}8687void Bytecode::assert_constant_size(int size, int where, Bytecodes::Code bc, bool is_wide) {88int have_fmt = Bytecodes::flags(bc, is_wide) & (Bytecodes::_all_fmt_bits89// Ignore any 'i' field (for iinc):90& ~Bytecodes::_fmt_has_i);91int need_fmt = -1;92switch (size) {93case 1: need_fmt = Bytecodes::_fmt_bc; break;94case 2: need_fmt = Bytecodes::_fmt_bc | Bytecodes::_fmt_has_u2; break;95}96if (is_wide) need_fmt |= Bytecodes::_fmt_not_simple;97int length = is_wide ? Bytecodes::wide_length_for(bc) : Bytecodes::length_for(bc);98if (have_fmt != need_fmt || where + size != length) {99tty->print_cr("assert_constant_size %d @%d: bc=%d%s %d != %d", size, where, bc, (is_wide?"/wide":""), have_fmt, need_fmt);100}101assert(have_fmt == need_fmt, "assert_constant_size");102assert(where + size == length, "assert_constant_size oob");103}104105void Bytecode::assert_native_index(Bytecodes::Code bc, bool is_wide) {106assert((Bytecodes::flags(bc, is_wide) & Bytecodes::_fmt_has_nbo) != 0, "native index");107}108109#endif //ASSERT110111// Implementation of Bytecode_tableupswitch112113int Bytecode_tableswitch::dest_offset_at(int i) const {114return get_Java_u4_at(aligned_offset(1 + (3 + i)*jintSize));115}116117118// Implementation of Bytecode_invoke119120void Bytecode_invoke::verify() const {121assert(is_valid(), "check invoke");122assert(cpcache() != NULL, "do not call this from verifier or rewriter");123}124125126Symbol* Bytecode_member_ref::klass() const {127return constants()->klass_ref_at_noresolve(index());128}129130131Symbol* Bytecode_member_ref::name() const {132return constants()->name_ref_at(index());133}134135136Symbol* Bytecode_member_ref::signature() const {137return constants()->signature_ref_at(index());138}139140141BasicType Bytecode_member_ref::result_type() const {142ResultTypeFinder rts(signature());143rts.iterate();144return rts.type();145}146147148methodHandle Bytecode_invoke::static_target(TRAPS) {149methodHandle m;150KlassHandle resolved_klass;151constantPoolHandle constants(THREAD, this->constants());152153Bytecodes::Code bc = invoke_code();154LinkResolver::resolve_method_statically(m, resolved_klass, bc, constants, index(), CHECK_(methodHandle()));155return m;156}157158Handle Bytecode_invoke::appendix(TRAPS) {159ConstantPoolCacheEntry* cpce = cpcache_entry();160if (cpce->has_appendix())161return Handle(THREAD, cpce->appendix_if_resolved(constants()));162return Handle(); // usual case163}164165int Bytecode_member_ref::index() const {166// Note: Rewriter::rewrite changes the Java_u2 of an invokedynamic to a native_u4,167// at the same time it allocates per-call-site CP cache entries.168Bytecodes::Code rawc = code();169if (has_index_u4(rawc))170return get_index_u4(rawc);171else172return get_index_u2_cpcache(rawc);173}174175int Bytecode_member_ref::pool_index() const {176return cpcache_entry()->constant_pool_index();177}178179ConstantPoolCacheEntry* Bytecode_member_ref::cpcache_entry() const {180int index = this->index();181return cpcache()->entry_at(ConstantPool::decode_cpcache_index(index, true));182}183184// Implementation of Bytecode_field185186void Bytecode_field::verify() const {187assert(is_valid(), "check field");188}189190191// Implementation of Bytecode_loadconstant192193int Bytecode_loadconstant::raw_index() const {194Bytecodes::Code rawc = code();195assert(rawc != Bytecodes::_wide, "verifier prevents this");196if (Bytecodes::java_code(rawc) == Bytecodes::_ldc)197return get_index_u1(rawc);198else199return get_index_u2(rawc, false);200}201202int Bytecode_loadconstant::pool_index() const {203int index = raw_index();204if (has_cache_index()) {205return _method->constants()->object_to_cp_index(index);206}207return index;208}209210BasicType Bytecode_loadconstant::result_type() const {211int index = pool_index();212constantTag tag = _method->constants()->tag_at(index);213return tag.basic_type();214}215216oop Bytecode_loadconstant::resolve_constant(TRAPS) const {217assert(_method.not_null(), "must supply method to resolve constant");218int index = raw_index();219ConstantPool* constants = _method->constants();220if (has_cache_index()) {221return constants->resolve_cached_constant_at(index, THREAD);222} else {223return constants->resolve_constant_at(index, THREAD);224}225}226227//------------------------------------------------------------------------------228// Non-product code229230#ifndef PRODUCT231232void Bytecode_lookupswitch::verify() const {233switch (Bytecodes::java_code(code())) {234case Bytecodes::_lookupswitch:235{ int i = number_of_pairs() - 1;236while (i-- > 0) {237assert(pair_at(i).match() < pair_at(i+1).match(), "unsorted table entries");238}239}240break;241default:242fatal("not a lookupswitch bytecode");243}244}245246void Bytecode_tableswitch::verify() const {247switch (Bytecodes::java_code(code())) {248case Bytecodes::_tableswitch:249{ int lo = low_key();250int hi = high_key();251assert (hi >= lo, "incorrect hi/lo values in tableswitch");252int i = hi - lo - 1 ;253while (i-- > 0) {254// no special check needed255}256}257break;258default:259fatal("not a tableswitch bytecode");260}261}262263#endif264265266