Path: blob/master/src/hotspot/share/interpreter/bytecode.cpp
40949 views
/*1* Copyright (c) 1997, 2021, 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.inline.hpp"26#include "interpreter/linkResolver.hpp"27#include "oops/constantPool.hpp"28#include "oops/cpCache.inline.hpp"29#include "oops/oop.inline.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_aligned_Java_u4_at(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}124125int Bytecode_invoke::size_of_parameters() const {126ArgumentSizeComputer asc(signature());127return asc.size() + (has_receiver() ? 1 : 0);128}129130131Symbol* Bytecode_member_ref::klass() const {132return constants()->klass_ref_at_noresolve(index());133}134135136Symbol* Bytecode_member_ref::name() const {137return constants()->name_ref_at(index());138}139140141Symbol* Bytecode_member_ref::signature() const {142return constants()->signature_ref_at(index());143}144145146BasicType Bytecode_member_ref::result_type() const {147ResultTypeFinder rts(signature());148return rts.type();149}150151152Method* Bytecode_invoke::static_target(TRAPS) {153constantPoolHandle constants(THREAD, this->constants());154155Bytecodes::Code bc = invoke_code();156return LinkResolver::resolve_method_statically(bc, constants, index(), THREAD);157}158159int Bytecode_member_ref::index() const {160// Note: Rewriter::rewrite changes the Java_u2 of an invokedynamic to a native_u4,161// at the same time it allocates per-call-site CP cache entries.162Bytecodes::Code rawc = code();163if (has_index_u4(rawc))164return get_index_u4(rawc);165else166return get_index_u2_cpcache(rawc);167}168169int Bytecode_member_ref::pool_index() const {170return cpcache_entry()->constant_pool_index();171}172173ConstantPoolCacheEntry* Bytecode_member_ref::cpcache_entry() const {174int index = this->index();175return cpcache()->entry_at(ConstantPool::decode_cpcache_index(index, true));176}177178// Implementation of Bytecode_field179180void Bytecode_field::verify() const {181assert(is_valid(), "check field");182}183184185// Implementation of Bytecode_loadconstant186187int Bytecode_loadconstant::raw_index() const {188Bytecodes::Code rawc = code();189assert(rawc != Bytecodes::_wide, "verifier prevents this");190if (Bytecodes::java_code(rawc) == Bytecodes::_ldc)191return get_index_u1(rawc);192else193return get_index_u2(rawc, false);194}195196int Bytecode_loadconstant::pool_index() const {197int index = raw_index();198if (has_cache_index()) {199return _method->constants()->object_to_cp_index(index);200}201return index;202}203204BasicType Bytecode_loadconstant::result_type() const {205int index = pool_index();206return _method->constants()->basic_type_for_constant_at(index);207}208209oop Bytecode_loadconstant::resolve_constant(TRAPS) const {210assert(_method != NULL, "must supply method to resolve constant");211int index = raw_index();212ConstantPool* constants = _method->constants();213if (has_cache_index()) {214return constants->resolve_cached_constant_at(index, THREAD);215} else if (_method->constants()->tag_at(index).is_dynamic_constant()) {216return constants->resolve_possibly_cached_constant_at(index, THREAD);217} else {218return constants->resolve_constant_at(index, THREAD);219}220}221222//------------------------------------------------------------------------------223// Non-product code224225#ifndef PRODUCT226227void Bytecode_lookupswitch::verify() const {228switch (Bytecodes::java_code(code())) {229case Bytecodes::_lookupswitch:230{ int i = number_of_pairs() - 1;231while (i-- > 0) {232assert(pair_at(i).match() < pair_at(i+1).match(), "unsorted table entries");233}234}235break;236default:237fatal("not a lookupswitch bytecode");238}239}240241void Bytecode_tableswitch::verify() const {242switch (Bytecodes::java_code(code())) {243case Bytecodes::_tableswitch:244{ int lo = low_key();245int hi = high_key();246assert (hi >= lo, "incorrect hi/lo values in tableswitch");247int i = hi - lo - 1 ;248while (i-- > 0) {249// no special check needed250}251}252break;253default:254fatal("not a tableswitch bytecode");255}256}257258#endif259260261