1 /*
   2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "code/compiledIC.hpp"
  27 #include "code/icBuffer.hpp"
  28 #include "code/nmethod.hpp"
  29 #include "code/scopeDesc.hpp"
  30 #include "gc_interface/collectedHeap.inline.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "interpreter/linkResolver.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "memory/universe.inline.hpp"
  35 #include "oops/methodOop.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "oops/oop.inline2.hpp"
  38 #include "runtime/mutexLocker.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #ifdef TARGET_ARCH_x86
  41 # include "assembler_x86.inline.hpp"
  42 #endif
  43 #ifdef TARGET_ARCH_sparc
  44 # include "assembler_sparc.inline.hpp"
  45 #endif
  46 #ifdef TARGET_ARCH_zero
  47 # include "assembler_zero.inline.hpp"
  48 #endif
  49 
  50 
  51 DEF_STUB_INTERFACE(ICStub);
  52 
  53 StubQueue* InlineCacheBuffer::_buffer    = NULL;
  54 ICStub*    InlineCacheBuffer::_next_stub = NULL;
  55 
  56 
  57 void ICStub::finalize() {
  58   if (!is_empty()) {
  59     ResourceMark rm;
  60     CompiledIC *ic = CompiledIC_at(ic_site());
  61     assert(CodeCache::find_nmethod(ic->instruction_address()) != NULL, "inline cache in non-nmethod?");
  62 
  63     assert(this == ICStub_from_destination_address(ic->stub_address()), "wrong owner of ic buffer");
  64     ic->set_cached_oop(cached_oop());
  65     ic->set_ic_destination(destination());
  66   }
  67 }
  68 
  69 
  70 address ICStub::destination() const {
  71   return InlineCacheBuffer::ic_buffer_entry_point(code_begin());
  72 }
  73 
  74 oop ICStub::cached_oop() const {
  75   return InlineCacheBuffer::ic_buffer_cached_oop(code_begin());
  76 }
  77 
  78 
  79 void ICStub::set_stub(CompiledIC *ic, oop cached_value, address dest_addr) {
  80   // We cannot store a pointer to the 'ic' object, since it is resource allocated. Instead we
  81   // store the location of the inline cache. Then we have enough information recreate the CompiledIC
  82   // object when we need to remove the stub.
  83   _ic_site = ic->instruction_address();
  84 
  85   // Assemble new stub
  86   InlineCacheBuffer::assemble_ic_buffer_code(code_begin(), cached_value, dest_addr);
  87   assert(destination() == dest_addr,   "can recover destination");
  88   assert(cached_oop() == cached_value, "can recover destination");
  89 }
  90 
  91 
  92 void ICStub::clear() {
  93   _ic_site = NULL;
  94 }
  95 
  96 
  97 #ifndef PRODUCT
  98 // anybody calling to this stub will trap
  99 
 100 void ICStub::verify() {
 101 }
 102 
 103 void ICStub::print() {
 104   tty->print_cr("ICStub: site: " INTPTR_FORMAT, _ic_site);
 105 }
 106 #endif
 107 
 108 //-----------------------------------------------------------------------------------------------
 109 // Implementation of InlineCacheBuffer
 110 
 111 void InlineCacheBuffer::init_next_stub() {
 112   ICStub* ic_stub = (ICStub*)buffer()->request_committed (ic_stub_code_size());
 113   assert (ic_stub != NULL, "no room for a single stub");
 114   set_next_stub(ic_stub);
 115 }
 116 
 117 void InlineCacheBuffer::initialize() {
 118   if (_buffer != NULL) return; // already initialized
 119   _buffer = new StubQueue(new ICStubInterface, 10*K, InlineCacheBuffer_lock, "InlineCacheBuffer");
 120   assert (_buffer != NULL, "cannot allocate InlineCacheBuffer");
 121   init_next_stub();
 122 }
 123 
 124 
 125 ICStub* InlineCacheBuffer::new_ic_stub() {
 126   while (true) {
 127     ICStub* ic_stub = (ICStub*)buffer()->request_committed(ic_stub_code_size());
 128     if (ic_stub != NULL) {
 129       return ic_stub;
 130     }
 131     // we ran out of inline cache buffer space; must enter safepoint.
 132     // We do this by forcing a safepoint
 133     EXCEPTION_MARK;
 134 
 135     VM_ForceSafepoint vfs;
 136     VMThread::execute(&vfs);
 137     // We could potential get an async. exception at this point.
 138     // In that case we will rethrow it to ourselvs.
 139     if (HAS_PENDING_EXCEPTION) {
 140       oop exception = PENDING_EXCEPTION;
 141       CLEAR_PENDING_EXCEPTION;
 142       Thread::send_async_exception(JavaThread::current()->threadObj(), exception);
 143     }
 144   }
 145   ShouldNotReachHere();
 146   return NULL;
 147 }
 148 
 149 
 150 void InlineCacheBuffer::update_inline_caches() {
 151   if (buffer()->number_of_stubs() > 1) {
 152     if (TraceICBuffer) {
 153       tty->print_cr("[updating inline caches with %d stubs]", buffer()->number_of_stubs());
 154     }
 155     buffer()->remove_all();
 156     init_next_stub();
 157   }
 158 }
 159 
 160 
 161 bool InlineCacheBuffer::contains(address instruction_address) {
 162   return buffer()->contains(instruction_address);
 163 }
 164 
 165 
 166 bool InlineCacheBuffer::is_empty() {
 167   return buffer()->number_of_stubs() == 1;    // always has sentinel
 168 }
 169 
 170 
 171 void InlineCacheBuffer_init() {
 172   InlineCacheBuffer::initialize();
 173 }
 174 
 175 
 176 void InlineCacheBuffer::create_transition_stub(CompiledIC *ic, oop cached_oop, address entry) {
 177   assert(!SafepointSynchronize::is_at_safepoint(), "should not be called during a safepoint");
 178   assert (CompiledIC_lock->is_locked(), "");
 179   assert(cached_oop == NULL || cached_oop->is_perm(), "must belong to perm. space");
 180   if (TraceICBuffer) { tty->print_cr("  create transition stub for " INTPTR_FORMAT, ic->instruction_address()); }
 181 
 182   // If an transition stub is already associate with the inline cache, then we remove the association.
 183   if (ic->is_in_transition_state()) {
 184     ICStub* old_stub = ICStub_from_destination_address(ic->stub_address());
 185     old_stub->clear();
 186   }
 187 
 188   // allocate and initialize new "out-of-line" inline-cache
 189   ICStub* ic_stub = get_next_stub();
 190   ic_stub->set_stub(ic, cached_oop, entry);
 191 
 192   // Update inline cache in nmethod to point to new "out-of-line" allocated inline cache
 193   ic->set_ic_destination(ic_stub->code_begin());
 194 
 195   set_next_stub(new_ic_stub()); // can cause safepoint synchronization
 196 }
 197 
 198 
 199 address InlineCacheBuffer::ic_destination_for(CompiledIC *ic) {
 200   ICStub* stub = ICStub_from_destination_address(ic->stub_address());
 201   return stub->destination();
 202 }
 203 
 204 
 205 oop InlineCacheBuffer::cached_oop_for(CompiledIC *ic) {
 206   ICStub* stub = ICStub_from_destination_address(ic->stub_address());
 207   return stub->cached_oop();
 208 }