0N/A/*
2273N/A * Copyright (c) 1997, 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 "code/compiledIC.hpp"
1879N/A#include "code/icBuffer.hpp"
1879N/A#include "code/nmethod.hpp"
1879N/A#include "code/scopeDesc.hpp"
1879N/A#include "gc_interface/collectedHeap.inline.hpp"
1879N/A#include "interpreter/interpreter.hpp"
1879N/A#include "interpreter/linkResolver.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "memory/universe.inline.hpp"
1879N/A#include "oops/methodOop.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "oops/oop.inline2.hpp"
1879N/A#include "runtime/mutexLocker.hpp"
1879N/A#include "runtime/stubRoutines.hpp"
1879N/A#ifdef TARGET_ARCH_x86
1879N/A# include "assembler_x86.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_ARCH_sparc
1879N/A# include "assembler_sparc.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_ARCH_zero
1879N/A# include "assembler_zero.inline.hpp"
1879N/A#endif
2073N/A#ifdef TARGET_ARCH_arm
2073N/A# include "assembler_arm.inline.hpp"
2073N/A#endif
2073N/A#ifdef TARGET_ARCH_ppc
2073N/A# include "assembler_ppc.inline.hpp"
2073N/A#endif
0N/A
0N/A
0N/ADEF_STUB_INTERFACE(ICStub);
0N/A
0N/AStubQueue* InlineCacheBuffer::_buffer = NULL;
0N/AICStub* InlineCacheBuffer::_next_stub = NULL;
0N/A
0N/A
0N/Avoid ICStub::finalize() {
0N/A if (!is_empty()) {
0N/A ResourceMark rm;
0N/A CompiledIC *ic = CompiledIC_at(ic_site());
0N/A assert(CodeCache::find_nmethod(ic->instruction_address()) != NULL, "inline cache in non-nmethod?");
0N/A
0N/A assert(this == ICStub_from_destination_address(ic->stub_address()), "wrong owner of ic buffer");
0N/A ic->set_cached_oop(cached_oop());
0N/A ic->set_ic_destination(destination());
0N/A }
0N/A}
0N/A
0N/A
0N/Aaddress ICStub::destination() const {
0N/A return InlineCacheBuffer::ic_buffer_entry_point(code_begin());
0N/A}
0N/A
0N/Aoop ICStub::cached_oop() const {
0N/A return InlineCacheBuffer::ic_buffer_cached_oop(code_begin());
0N/A}
0N/A
0N/A
0N/Avoid ICStub::set_stub(CompiledIC *ic, oop cached_value, address dest_addr) {
0N/A // We cannot store a pointer to the 'ic' object, since it is resource allocated. Instead we
0N/A // store the location of the inline cache. Then we have enough information recreate the CompiledIC
0N/A // object when we need to remove the stub.
0N/A _ic_site = ic->instruction_address();
0N/A
0N/A // Assemble new stub
0N/A InlineCacheBuffer::assemble_ic_buffer_code(code_begin(), cached_value, dest_addr);
0N/A assert(destination() == dest_addr, "can recover destination");
0N/A assert(cached_oop() == cached_value, "can recover destination");
0N/A}
0N/A
0N/A
0N/Avoid ICStub::clear() {
0N/A _ic_site = NULL;
0N/A}
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/A// anybody calling to this stub will trap
0N/A
0N/Avoid ICStub::verify() {
0N/A}
0N/A
0N/Avoid ICStub::print() {
0N/A tty->print_cr("ICStub: site: " INTPTR_FORMAT, _ic_site);
0N/A}
0N/A#endif
0N/A
0N/A//-----------------------------------------------------------------------------------------------
0N/A// Implementation of InlineCacheBuffer
0N/A
0N/Avoid InlineCacheBuffer::init_next_stub() {
0N/A ICStub* ic_stub = (ICStub*)buffer()->request_committed (ic_stub_code_size());
0N/A assert (ic_stub != NULL, "no room for a single stub");
0N/A set_next_stub(ic_stub);
0N/A}
0N/A
0N/Avoid InlineCacheBuffer::initialize() {
0N/A if (_buffer != NULL) return; // already initialized
0N/A _buffer = new StubQueue(new ICStubInterface, 10*K, InlineCacheBuffer_lock, "InlineCacheBuffer");
0N/A assert (_buffer != NULL, "cannot allocate InlineCacheBuffer");
0N/A init_next_stub();
0N/A}
0N/A
0N/A
0N/AICStub* InlineCacheBuffer::new_ic_stub() {
0N/A while (true) {
0N/A ICStub* ic_stub = (ICStub*)buffer()->request_committed(ic_stub_code_size());
0N/A if (ic_stub != NULL) {
0N/A return ic_stub;
0N/A }
0N/A // we ran out of inline cache buffer space; must enter safepoint.
0N/A // We do this by forcing a safepoint
0N/A EXCEPTION_MARK;
0N/A
0N/A VM_ForceSafepoint vfs;
0N/A VMThread::execute(&vfs);
0N/A // We could potential get an async. exception at this point.
0N/A // In that case we will rethrow it to ourselvs.
0N/A if (HAS_PENDING_EXCEPTION) {
0N/A oop exception = PENDING_EXCEPTION;
0N/A CLEAR_PENDING_EXCEPTION;
0N/A Thread::send_async_exception(JavaThread::current()->threadObj(), exception);
0N/A }
0N/A }
0N/A ShouldNotReachHere();
0N/A return NULL;
0N/A}
0N/A
0N/A
0N/Avoid InlineCacheBuffer::update_inline_caches() {
0N/A if (buffer()->number_of_stubs() > 1) {
0N/A if (TraceICBuffer) {
0N/A tty->print_cr("[updating inline caches with %d stubs]", buffer()->number_of_stubs());
0N/A }
0N/A buffer()->remove_all();
0N/A init_next_stub();
0N/A }
0N/A}
0N/A
0N/A
0N/Abool InlineCacheBuffer::contains(address instruction_address) {
0N/A return buffer()->contains(instruction_address);
0N/A}
0N/A
0N/A
0N/Abool InlineCacheBuffer::is_empty() {
0N/A return buffer()->number_of_stubs() == 1; // always has sentinel
0N/A}
0N/A
0N/A
0N/Avoid InlineCacheBuffer_init() {
0N/A InlineCacheBuffer::initialize();
0N/A}
0N/A
0N/A
0N/Avoid InlineCacheBuffer::create_transition_stub(CompiledIC *ic, oop cached_oop, address entry) {
0N/A assert(!SafepointSynchronize::is_at_safepoint(), "should not be called during a safepoint");
0N/A assert (CompiledIC_lock->is_locked(), "");
0N/A assert(cached_oop == NULL || cached_oop->is_perm(), "must belong to perm. space");
0N/A if (TraceICBuffer) { tty->print_cr(" create transition stub for " INTPTR_FORMAT, ic->instruction_address()); }
0N/A
0N/A // If an transition stub is already associate with the inline cache, then we remove the association.
0N/A if (ic->is_in_transition_state()) {
0N/A ICStub* old_stub = ICStub_from_destination_address(ic->stub_address());
0N/A old_stub->clear();
0N/A }
0N/A
0N/A // allocate and initialize new "out-of-line" inline-cache
0N/A ICStub* ic_stub = get_next_stub();
0N/A ic_stub->set_stub(ic, cached_oop, entry);
0N/A
0N/A // Update inline cache in nmethod to point to new "out-of-line" allocated inline cache
0N/A ic->set_ic_destination(ic_stub->code_begin());
0N/A
0N/A set_next_stub(new_ic_stub()); // can cause safepoint synchronization
0N/A}
0N/A
0N/A
0N/Aaddress InlineCacheBuffer::ic_destination_for(CompiledIC *ic) {
0N/A ICStub* stub = ICStub_from_destination_address(ic->stub_address());
0N/A return stub->destination();
0N/A}
0N/A
0N/A
0N/Aoop InlineCacheBuffer::cached_oop_for(CompiledIC *ic) {
0N/A ICStub* stub = ICStub_from_destination_address(ic->stub_address());
0N/A return stub->cached_oop();
0N/A}