0N/A/*
2204N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "ci/ciCPCache.hpp"
1879N/A#include "ci/ciCallSite.hpp"
1879N/A#include "ci/ciConstant.hpp"
1879N/A#include "ci/ciField.hpp"
1879N/A#include "ci/ciStreams.hpp"
1879N/A#include "ci/ciUtilities.hpp"
0N/A
0N/A// ciExceptionHandlerStream
0N/A//
0N/A// Walk over some selected set of a methods exception handlers.
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciExceptionHandlerStream::count
0N/A//
0N/A// How many exception handlers are there in this stream?
0N/A//
0N/A// Implementation note: Compiler2 needs this functionality, so I had
0N/Aint ciExceptionHandlerStream::count() {
0N/A int save_pos = _pos;
0N/A int save_end = _end;
0N/A
0N/A int count = 0;
0N/A
0N/A _pos = -1;
0N/A _end = _method->_handler_count;
0N/A
0N/A
0N/A next();
0N/A while (!is_done()) {
0N/A count++;
0N/A next();
0N/A }
0N/A
0N/A _pos = save_pos;
0N/A _end = save_end;
0N/A
0N/A return count;
0N/A}
0N/A
0N/Aint ciExceptionHandlerStream::count_remaining() {
0N/A int save_pos = _pos;
0N/A int save_end = _end;
0N/A
0N/A int count = 0;
0N/A
0N/A while (!is_done()) {
0N/A count++;
0N/A next();
0N/A }
0N/A
0N/A _pos = save_pos;
0N/A _end = save_end;
0N/A
0N/A return count;
0N/A}
0N/A
0N/A// ciBytecodeStream
0N/A//
0N/A// The class is used to iterate over the bytecodes of a method.
0N/A// It hides the details of constant pool structure/access by
0N/A// providing accessors for constant pool items.
0N/A
0N/A// ------------------------------------------------------------------
1485N/A// ciBytecodeStream::next_wide_or_table
0N/A//
0N/A// Special handling for switch ops
1485N/ABytecodes::Code ciBytecodeStream::next_wide_or_table(Bytecodes::Code bc) {
1485N/A switch (bc) { // Check for special bytecode handling
1485N/A case Bytecodes::_wide:
1485N/A // Special handling for the wide bytcode
1485N/A // Get following bytecode; do not return wide
1485N/A assert(Bytecodes::Code(_pc[0]) == Bytecodes::_wide, "");
1485N/A bc = Bytecodes::java_code(_raw_bc = (Bytecodes::Code)_pc[1]);
1485N/A assert(Bytecodes::wide_length_for(bc) > 2, "must make progress");
1485N/A _pc += Bytecodes::wide_length_for(bc);
1485N/A _was_wide = _pc; // Flag last wide bytecode found
1485N/A assert(is_wide(), "accessor works right");
1485N/A break;
0N/A
0N/A case Bytecodes::_lookupswitch:
0N/A _pc++; // Skip wide bytecode
0N/A _pc += (_start-_pc)&3; // Word align
0N/A _table_base = (jint*)_pc; // Capture for later usage
0N/A // table_base[0] is default far_dest
0N/A // Table has 2 lead elements (default, length), then pairs of u4 values.
0N/A // So load table length, and compute address at end of table
0N/A _pc = (address)&_table_base[2+ 2*Bytes::get_Java_u4((address)&_table_base[1])];
0N/A break;
0N/A
0N/A case Bytecodes::_tableswitch: {
0N/A _pc++; // Skip wide bytecode
0N/A _pc += (_start-_pc)&3; // Word align
0N/A _table_base = (jint*)_pc; // Capture for later usage
0N/A // table_base[0] is default far_dest
0N/A int lo = Bytes::get_Java_u4((address)&_table_base[1]);// Low bound
0N/A int hi = Bytes::get_Java_u4((address)&_table_base[2]);// High bound
0N/A int len = hi - lo + 1; // Dense table size
0N/A _pc = (address)&_table_base[3+len]; // Skip past table
0N/A break;
0N/A }
0N/A
0N/A default:
0N/A fatal("unhandled bytecode");
0N/A }
0N/A return bc;
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::reset_to_bci
0N/Avoid ciBytecodeStream::reset_to_bci( int bci ) {
0N/A _bc_start=_was_wide=0;
0N/A _pc = _start+bci;
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::force_bci
0N/Avoid ciBytecodeStream::force_bci(int bci) {
0N/A if (bci < 0) {
0N/A reset_to_bci(0);
0N/A _bc_start = _start + bci;
0N/A _bc = EOBC();
0N/A } else {
0N/A reset_to_bci(bci);
0N/A next();
0N/A }
0N/A}
0N/A
0N/A
0N/A// ------------------------------------------------------------------
0N/A// Constant pool access
0N/A// ------------------------------------------------------------------
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_klass_index
0N/A//
0N/A// If this bytecodes references a klass, return the index of the
0N/A// referenced klass.
0N/Aint ciBytecodeStream::get_klass_index() const {
0N/A switch(cur_bc()) {
0N/A case Bytecodes::_ldc:
1485N/A return get_index_u1();
0N/A case Bytecodes::_ldc_w:
0N/A case Bytecodes::_ldc2_w:
0N/A case Bytecodes::_checkcast:
0N/A case Bytecodes::_instanceof:
0N/A case Bytecodes::_anewarray:
0N/A case Bytecodes::_multianewarray:
0N/A case Bytecodes::_new:
0N/A case Bytecodes::_newarray:
1485N/A return get_index_u2();
0N/A default:
0N/A ShouldNotReachHere();
0N/A return 0;
0N/A }
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_klass
0N/A//
0N/A// If this bytecode is a new, newarray, multianewarray, instanceof,
0N/A// or checkcast, get the referenced klass.
0N/AciKlass* ciBytecodeStream::get_klass(bool& will_link) {
1138N/A VM_ENTRY_MARK;
1138N/A constantPoolHandle cpool(_method->get_methodOop()->constants());
1138N/A return CURRENT_ENV->get_klass_by_index(cpool, get_klass_index(), will_link, _holder);
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
1522N/A// ciBytecodeStream::get_constant_raw_index
0N/A//
0N/A// If this bytecode is one of the ldc variants, get the index of the
0N/A// referenced constant.
1522N/Aint ciBytecodeStream::get_constant_raw_index() const {
1522N/A // work-alike for Bytecode_loadconstant::raw_index()
1522N/A switch (cur_bc()) {
0N/A case Bytecodes::_ldc:
1485N/A return get_index_u1();
0N/A case Bytecodes::_ldc_w:
0N/A case Bytecodes::_ldc2_w:
1485N/A return get_index_u2();
0N/A default:
0N/A ShouldNotReachHere();
0N/A return 0;
0N/A }
0N/A}
1522N/A
1522N/A// ------------------------------------------------------------------
1522N/A// ciBytecodeStream::get_constant_pool_index
1522N/A// Decode any CP cache index into a regular pool index.
1522N/Aint ciBytecodeStream::get_constant_pool_index() const {
1522N/A // work-alike for Bytecode_loadconstant::pool_index()
1522N/A int index = get_constant_raw_index();
1522N/A if (has_cache_index()) {
1522N/A return get_cpcache()->get_pool_index(index);
1522N/A }
1522N/A return index;
1522N/A}
1522N/A
1522N/A// ------------------------------------------------------------------
1522N/A// ciBytecodeStream::get_constant_cache_index
1522N/A// Return the CP cache index, or -1 if there isn't any.
1522N/Aint ciBytecodeStream::get_constant_cache_index() const {
1522N/A // work-alike for Bytecode_loadconstant::cache_index()
1522N/A return has_cache_index() ? get_constant_raw_index() : -1;
1522N/A}
1522N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_constant
0N/A//
0N/A// If this bytecode is one of the ldc variants, get the referenced
0N/A// constant.
0N/AciConstant ciBytecodeStream::get_constant() {
1522N/A int pool_index = get_constant_raw_index();
1522N/A int cache_index = -1;
1522N/A if (has_cache_index()) {
1522N/A cache_index = pool_index;
1522N/A pool_index = -1;
1522N/A }
1138N/A VM_ENTRY_MARK;
1138N/A constantPoolHandle cpool(_method->get_methodOop()->constants());
1522N/A return CURRENT_ENV->get_constant_by_index(cpool, pool_index, cache_index, _holder);
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
1522N/A// ciBytecodeStream::get_constant_pool_tag
1522N/A//
1522N/A// If this bytecode is one of the ldc variants, get the referenced
1522N/A// constant.
1522N/AconstantTag ciBytecodeStream::get_constant_pool_tag(int index) const {
1522N/A VM_ENTRY_MARK;
1522N/A return _method->get_methodOop()->constants()->tag_at(index);
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_field_index
0N/A//
0N/A// If this is a field access bytecode, get the constant pool
0N/A// index of the referenced field.
0N/Aint ciBytecodeStream::get_field_index() {
0N/A assert(cur_bc() == Bytecodes::_getfield ||
0N/A cur_bc() == Bytecodes::_putfield ||
0N/A cur_bc() == Bytecodes::_getstatic ||
0N/A cur_bc() == Bytecodes::_putstatic, "wrong bc");
1485N/A return get_index_u2_cpcache();
0N/A}
0N/A
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_field
0N/A//
0N/A// If this bytecode is one of get_field, get_static, put_field,
0N/A// or put_static, get the referenced field.
0N/AciField* ciBytecodeStream::get_field(bool& will_link) {
0N/A ciField* f = CURRENT_ENV->get_field_by_index(_holder, get_field_index());
0N/A will_link = f->will_link(_holder, _bc);
0N/A return f;
0N/A}
0N/A
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_declared_field_holder
0N/A//
0N/A// Get the declared holder of the currently referenced field.
0N/A//
0N/A// Usage note: the holder() of a ciField class returns the canonical
0N/A// holder of the field, rather than the holder declared in the
0N/A// bytecodes.
0N/A//
0N/A// There is no "will_link" result passed back. The user is responsible
0N/A// for checking linkability when retrieving the associated field.
0N/AciInstanceKlass* ciBytecodeStream::get_declared_field_holder() {
1138N/A VM_ENTRY_MARK;
1138N/A constantPoolHandle cpool(_method->get_methodOop()->constants());
0N/A int holder_index = get_field_holder_index();
0N/A bool ignore;
1138N/A return CURRENT_ENV->get_klass_by_index(cpool, holder_index, ignore, _holder)
0N/A ->as_instance_klass();
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_field_holder_index
0N/A//
0N/A// Get the constant pool index of the declared holder of the field
0N/A// referenced by the current bytecode. Used for generating
0N/A// deoptimization information.
0N/Aint ciBytecodeStream::get_field_holder_index() {
1138N/A GUARDED_VM_ENTRY(
1138N/A constantPoolOop cpool = _holder->get_instanceKlass()->constants();
1138N/A return cpool->klass_ref_index_at(get_field_index());
1138N/A )
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_field_signature_index
0N/A//
0N/A// Get the constant pool index of the signature of the field
0N/A// referenced by the current bytecode. Used for generating
0N/A// deoptimization information.
0N/Aint ciBytecodeStream::get_field_signature_index() {
0N/A VM_ENTRY_MARK;
0N/A constantPoolOop cpool = _holder->get_instanceKlass()->constants();
0N/A int nt_index = cpool->name_and_type_ref_index_at(get_field_index());
0N/A return cpool->signature_ref_index_at(nt_index);
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_method_index
0N/A//
0N/A// If this is a method invocation bytecode, get the constant pool
0N/A// index of the invoked method.
0N/Aint ciBytecodeStream::get_method_index() {
726N/A#ifdef ASSERT
0N/A switch (cur_bc()) {
0N/A case Bytecodes::_invokeinterface:
0N/A case Bytecodes::_invokevirtual:
0N/A case Bytecodes::_invokespecial:
0N/A case Bytecodes::_invokestatic:
726N/A case Bytecodes::_invokedynamic:
726N/A break;
0N/A default:
0N/A ShouldNotReachHere();
0N/A }
726N/A#endif
1485N/A if (has_index_u4())
1485N/A return get_index_u4(); // invokedynamic
1485N/A return get_index_u2_cpcache();
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_method
0N/A//
0N/A// If this is a method invocation bytecode, get the invoked method.
3984N/A// Additionally return the declared signature to get more concrete
3984N/A// type information if required (Cf. invokedynamic and invokehandle).
3984N/AciMethod* ciBytecodeStream::get_method(bool& will_link, ciSignature* *declared_signature_result) {
1138N/A VM_ENTRY_MARK;
3984N/A ciEnv* env = CURRENT_ENV;
1138N/A constantPoolHandle cpool(_method->get_methodOop()->constants());
3984N/A ciMethod* m = env->get_method_by_index(cpool, get_method_index(), cur_bc(), _holder);
0N/A will_link = m->is_loaded();
4048N/A
4048N/A // Use the MethodType stored in the CP cache to create a signature
4048N/A // with correct types (in respect to class loaders).
4048N/A if (has_method_type()) {
4048N/A ciSymbol* sig_sym = env->get_symbol(cpool->symbol_at(get_method_signature_index()));
4048N/A ciKlass* pool_holder = env->get_object(cpool->pool_holder())->as_klass();
4048N/A ciMethodType* method_type = get_method_type();
4048N/A ciSignature* declared_signature = new (env->arena()) ciSignature(pool_holder, sig_sym, method_type);
4048N/A (*declared_signature_result) = declared_signature;
3984N/A } else {
3984N/A (*declared_signature_result) = m->signature();
3984N/A }
0N/A return m;
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
3932N/A// ciBytecodeStream::has_appendix
3932N/A//
3932N/A// Returns true if there is an appendix argument stored in the
3932N/A// constant pool cache at the current bci.
3932N/Abool ciBytecodeStream::has_appendix() {
3932N/A VM_ENTRY_MARK;
3932N/A constantPoolHandle cpool(_method->get_methodOop()->constants());
3932N/A return constantPoolOopDesc::has_appendix_at_if_loaded(cpool, get_method_index());
3932N/A}
3932N/A
3932N/A// ------------------------------------------------------------------
3932N/A// ciBytecodeStream::get_appendix
3932N/A//
3932N/A// Return the appendix argument stored in the constant pool cache at
3932N/A// the current bci.
3932N/AciObject* ciBytecodeStream::get_appendix() {
3932N/A VM_ENTRY_MARK;
3932N/A constantPoolHandle cpool(_method->get_methodOop()->constants());
3932N/A oop appendix_oop = constantPoolOopDesc::appendix_at_if_loaded(cpool, get_method_index());
3932N/A return CURRENT_ENV->get_object(appendix_oop);
3932N/A}
3932N/A
3932N/A// ------------------------------------------------------------------
4048N/A// ciBytecodeStream::has_method_type
4048N/A//
4048N/A// Returns true if there is a MethodType argument stored in the
4048N/A// constant pool cache at the current bci.
4048N/Abool ciBytecodeStream::has_method_type() {
4048N/A GUARDED_VM_ENTRY(
4048N/A constantPoolHandle cpool(_method->get_methodOop()->constants());
4048N/A return constantPoolOopDesc::has_method_type_at_if_loaded(cpool, get_method_index());
4048N/A )
4048N/A}
4048N/A
4048N/A// ------------------------------------------------------------------
4048N/A// ciBytecodeStream::get_method_type
4048N/A//
4048N/A// Return the MethodType stored in the constant pool cache at
4048N/A// the current bci.
4048N/AciMethodType* ciBytecodeStream::get_method_type() {
4048N/A GUARDED_VM_ENTRY(
4048N/A constantPoolHandle cpool(_method->get_methodOop()->constants());
4048N/A oop method_type_oop = constantPoolOopDesc::method_type_at_if_loaded(cpool, get_method_index());
4048N/A return CURRENT_ENV->get_object(method_type_oop)->as_method_type();
4048N/A )
4048N/A}
4048N/A
4048N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_declared_method_holder
0N/A//
0N/A// Get the declared holder of the currently referenced method.
0N/A//
0N/A// Usage note: the holder() of a ciMethod class returns the canonical
0N/A// holder of the method, rather than the holder declared in the
0N/A// bytecodes.
0N/A//
0N/A// There is no "will_link" result passed back. The user is responsible
0N/A// for checking linkability when retrieving the associated method.
0N/AciKlass* ciBytecodeStream::get_declared_method_holder() {
1138N/A VM_ENTRY_MARK;
1138N/A constantPoolHandle cpool(_method->get_methodOop()->constants());
0N/A bool ignore;
3932N/A // report as MethodHandle for invokedynamic, which is syntactically classless
726N/A if (cur_bc() == Bytecodes::_invokedynamic)
3932N/A return CURRENT_ENV->get_klass_by_name(_holder, ciSymbol::java_lang_invoke_MethodHandle(), false);
1138N/A return CURRENT_ENV->get_klass_by_index(cpool, get_method_holder_index(), ignore, _holder);
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_method_holder_index
0N/A//
0N/A// Get the constant pool index of the declared holder of the method
0N/A// referenced by the current bytecode. Used for generating
0N/A// deoptimization information.
0N/Aint ciBytecodeStream::get_method_holder_index() {
1138N/A constantPoolOop cpool = _method->get_methodOop()->constants();
0N/A return cpool->klass_ref_index_at(get_method_index());
0N/A}
0N/A
0N/A// ------------------------------------------------------------------
0N/A// ciBytecodeStream::get_method_signature_index
0N/A//
0N/A// Get the constant pool index of the signature of the method
0N/A// referenced by the current bytecode. Used for generating
0N/A// deoptimization information.
0N/Aint ciBytecodeStream::get_method_signature_index() {
3984N/A GUARDED_VM_ENTRY(
3984N/A constantPoolOop cpool = _holder->get_instanceKlass()->constants();
3984N/A const int method_index = get_method_index();
3984N/A const int name_and_type_index = cpool->name_and_type_ref_index_at(method_index);
3984N/A return cpool->signature_ref_index_at(name_and_type_index);
3984N/A )
0N/A}
1137N/A
1137N/A// ------------------------------------------------------------------
1137N/A// ciBytecodeStream::get_cpcache
1522N/AciCPCache* ciBytecodeStream::get_cpcache() const {
1522N/A if (_cpcache == NULL) {
1522N/A VM_ENTRY_MARK;
1522N/A // Get the constant pool.
1522N/A constantPoolOop cpool = _holder->get_instanceKlass()->constants();
1522N/A constantPoolCacheOop cpcache = cpool->cache();
1137N/A
1522N/A *(ciCPCache**)&_cpcache = CURRENT_ENV->get_object(cpcache)->as_cpcache();
1522N/A }
1522N/A return _cpcache;
1137N/A}
1138N/A
1138N/A// ------------------------------------------------------------------
1138N/A// ciBytecodeStream::get_call_site
1138N/AciCallSite* ciBytecodeStream::get_call_site() {
1138N/A VM_ENTRY_MARK;
1138N/A // Get the constant pool.
1138N/A constantPoolOop cpool = _holder->get_instanceKlass()->constants();
1138N/A constantPoolCacheOop cpcache = cpool->cache();
1138N/A
1138N/A // Get the CallSite from the constant pool cache.
1138N/A int method_index = get_method_index();
1138N/A ConstantPoolCacheEntry* cpcache_entry = cpcache->secondary_entry_at(method_index);
3932N/A oop call_site_oop = cpcache_entry->f1_as_instance();
1138N/A
1138N/A // Create a CallSite object and return it.
1138N/A return CURRENT_ENV->get_object(call_site_oop)->as_call_site();
1138N/A}